Skip to content

Commit 0789962

Browse files
committed
net: connection: Register connection type
Register connection type along with family and protocol, so that it's possible to differentiate between connection listening for raw IP datagrams and TCP/UDP/other packets. Signed-off-by: Robert Lubos <[email protected]>
1 parent 0f852c8 commit 0789962

File tree

6 files changed

+27
-14
lines changed

6 files changed

+27
-14
lines changed

subsys/net/ip/connection.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ static int net_conn_change_remote(struct net_conn *conn,
325325
return 0;
326326
}
327327

328-
int net_conn_register(uint16_t proto, uint8_t family,
328+
int net_conn_register(uint16_t proto, enum net_sock_type type, uint8_t family,
329329
const struct sockaddr *remote_addr,
330330
const struct sockaddr *local_addr,
331331
uint16_t remote_port,
@@ -408,6 +408,7 @@ int net_conn_register(uint16_t proto, uint8_t family,
408408

409409
conn->flags = flags;
410410
conn->proto = proto;
411+
conn->type = type;
411412
conn->family = family;
412413
conn->context = context;
413414

subsys/net/ip/connection.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ struct net_conn {
7474
/** Connection protocol */
7575
uint16_t proto;
7676

77+
/** Connection type */
78+
enum net_sock_type type;
79+
7780
/** Protocol family */
7881
uint8_t family;
7982

@@ -90,6 +93,7 @@ struct net_conn {
9093
*
9194
* @param proto Protocol for the connection (depends on the protocol
9295
* family, e.g. UDP/TCP in the case of AF_INET/AF_INET6)
96+
* @param type Connection type (SOCK_STREAM/DGRAM/RAW)
9397
* @param family Protocol family (AF_*)
9498
* @param remote_addr Remote address of the connection end point.
9599
* @param local_addr Local address of the connection end point.
@@ -103,7 +107,7 @@ struct net_conn {
103107
* @return Return 0 if the registration succeed, <0 otherwise.
104108
*/
105109
#if defined(CONFIG_NET_NATIVE)
106-
int net_conn_register(uint16_t proto, uint8_t family,
110+
int net_conn_register(uint16_t proto, enum net_sock_type type, uint8_t family,
107111
const struct sockaddr *remote_addr,
108112
const struct sockaddr *local_addr,
109113
uint16_t remote_port,
@@ -113,7 +117,8 @@ int net_conn_register(uint16_t proto, uint8_t family,
113117
void *user_data,
114118
struct net_conn_handle **handle);
115119
#else
116-
static inline int net_conn_register(uint16_t proto, uint8_t family,
120+
static inline int net_conn_register(uint16_t proto, enum net_sock_type type,
121+
uint8_t family,
117122
const struct sockaddr *remote_addr,
118123
const struct sockaddr *local_addr,
119124
uint16_t remote_port,

subsys/net/ip/net_context.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,7 @@ static int recv_udp(struct net_context *context,
28732873
context->recv_cb = cb;
28742874

28752875
ret = net_conn_register(net_context_get_proto(context),
2876+
net_context_get_type(context),
28762877
net_context_get_family(context),
28772878
context->flags & NET_CONTEXT_REMOTE_ADDR_SET ?
28782879
&context->remote : NULL,
@@ -2958,6 +2959,7 @@ static int recv_raw(struct net_context *context,
29582959
context->recv_cb = cb;
29592960

29602961
ret = net_conn_register(net_context_get_proto(context),
2962+
net_context_get_type(context),
29612963
net_context_get_family(context),
29622964
NULL, local_addr, 0, 0,
29632965
context,

subsys/net/ip/tcp.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,7 +2483,7 @@ static struct tcp *tcp_conn_new(struct net_pkt *pkt)
24832483
net_sprint_addr(context->remote.sa_family,
24842484
(const void *)&net_sin(&context->remote)->sin_addr));
24852485

2486-
ret = net_conn_register(IPPROTO_TCP, af,
2486+
ret = net_conn_register(IPPROTO_TCP, SOCK_STREAM, af,
24872487
&context->remote, &local_addr,
24882488
ntohs(conn->dst.sin.sin_port),/* local port */
24892489
ntohs(conn->src.sin.sin_port),/* remote port */
@@ -3922,6 +3922,7 @@ int net_tcp_connect(struct net_context *context,
39223922
net_context_set_state(context, NET_CONTEXT_CONNECTING);
39233923

39243924
ret = net_conn_register(net_context_get_proto(context),
3925+
net_context_get_type(context),
39253926
net_context_get_family(context),
39263927
remote_addr, local_addr,
39273928
ntohs(remote_port), ntohs(local_port),
@@ -4063,6 +4064,7 @@ int net_tcp_accept(struct net_context *context, net_tcp_accept_cb_t cb,
40634064
net_conn_unregister(context->conn_handler);
40644065

40654066
return net_conn_register(net_context_get_proto(context),
4067+
net_context_get_type(context),
40664068
local_addr.sa_family,
40674069
context->flags & NET_CONTEXT_REMOTE_ADDR_SET ?
40684070
&context->remote : NULL,
@@ -4363,13 +4365,15 @@ enum net_verdict tp_input(struct net_conn *net_conn,
43634365
return verdict;
43644366
}
43654367

4366-
static void test_cb_register(sa_family_t family, uint8_t proto, uint16_t remote_port,
4368+
static void test_cb_register(sa_family_t family, enum net_sock_type type,
4369+
uint8_t proto, uint16_t remote_port,
43674370
uint16_t local_port, net_conn_cb_t cb)
43684371
{
43694372
struct net_conn_handle *conn_handle = NULL;
43704373
const struct sockaddr addr = { .sa_family = family, };
43714374

43724375
int ret = net_conn_register(proto,
4376+
type,
43734377
family,
43744378
&addr, /* remote address */
43754379
&addr, /* local address */
@@ -4632,10 +4636,10 @@ void net_tcp_init(void)
46324636
int rto;
46334637
#if defined(CONFIG_NET_TEST_PROTOCOL)
46344638
/* Register inputs for TTCN-3 based TCP sanity check */
4635-
test_cb_register(AF_INET, IPPROTO_TCP, 4242, 4242, tcp_input);
4636-
test_cb_register(AF_INET6, IPPROTO_TCP, 4242, 4242, tcp_input);
4637-
test_cb_register(AF_INET, IPPROTO_UDP, 4242, 4242, tp_input);
4638-
test_cb_register(AF_INET6, IPPROTO_UDP, 4242, 4242, tp_input);
4639+
test_cb_register(AF_INET, SOCK_STREAM, IPPROTO_TCP, 4242, 4242, tcp_input);
4640+
test_cb_register(AF_INET6, SOCK_STREAM, IPPROTO_TCP, 4242, 4242, tcp_input);
4641+
test_cb_register(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 4242, 4242, tp_input);
4642+
test_cb_register(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, 4242, 4242, tp_input);
46394643

46404644
tcp_recv_cb = tp_tcp_recv_cb;
46414645
#endif

subsys/net/ip/udp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ int net_udp_register(uint8_t family,
137137
void *user_data,
138138
struct net_conn_handle **handle)
139139
{
140-
return net_conn_register(IPPROTO_UDP, family, remote_addr, local_addr,
141-
remote_port, local_port, context, cb,
142-
user_data, handle);
140+
return net_conn_register(IPPROTO_UDP, SOCK_DGRAM, family, remote_addr,
141+
local_addr, remote_port, local_port, context,
142+
cb, user_data, handle);
143143
}
144144

145145
int net_udp_unregister(struct net_conn_handle *handle)

tests/net/ipv4_fragment/src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,9 @@ static void setup_tcp_handler(const struct in_addr *raddr, const struct in_addr
558558
net_ipaddr_copy(&net_sin(&remote_addr)->sin_addr, raddr);
559559
remote_addr.sa_family = AF_INET;
560560

561-
ret = net_conn_register(IPPROTO_TCP, AF_INET, &local_addr, &remote_addr, local_port,
562-
remote_port, NULL, tcp_data_received, NULL, &handle);
561+
ret = net_conn_register(IPPROTO_TCP, SOCK_STREAM, AF_INET, &local_addr,
562+
&remote_addr, local_port, remote_port, NULL,
563+
tcp_data_received, NULL, &handle);
563564

564565
zassert_equal(ret, 0, "Cannot register TCP connection");
565566
}

0 commit comments

Comments
 (0)