Skip to content

Commit 0032f68

Browse files
committed
net: context: Set local address properly for AF_CAN connections
At the moment there is no real address for local CANBUS socket, but we can still set protocol family of local socket to AF_CAN so that for example net-shell "net conn" command can show information about it. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent ae89c22 commit 0032f68

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

subsys/net/ip/connection.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,10 @@ int net_conn_register(u16_t proto, u8_t family,
306306
if (net_sin(local_addr)->sin_addr.s_addr) {
307307
flags |= NET_CONN_LOCAL_ADDR_SPEC;
308308
}
309+
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) &&
310+
local_addr->sa_family == AF_CAN) {
311+
memcpy(&conn->local_addr, local_addr,
312+
sizeof(struct sockaddr_can));
309313
} else {
310314
NET_ERR("Local address family not set");
311315
goto error;

subsys/net/ip/net_context.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,7 @@ static enum net_verdict net_context_raw_packet_received(
16451645
static int recv_raw(struct net_context *context,
16461646
net_context_recv_cb_t cb,
16471647
s32_t timeout,
1648+
struct sockaddr *local_addr,
16481649
void *user_data)
16491650
{
16501651
int ret;
@@ -1665,7 +1666,7 @@ static int recv_raw(struct net_context *context,
16651666

16661667
ret = net_conn_register(net_context_get_ip_proto(context),
16671668
net_context_get_family(context),
1668-
NULL, NULL, 0, 0,
1669+
NULL, local_addr, 0, 0,
16691670
net_context_raw_packet_received,
16701671
user_data,
16711672
&context->conn_handler);
@@ -1704,10 +1705,16 @@ int net_context_recv(struct net_context *context,
17041705
} else {
17051706
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) &&
17061707
net_context_get_family(context) == AF_PACKET) {
1707-
ret = recv_raw(context, cb, timeout, user_data);
1708+
ret = recv_raw(context, cb, timeout, NULL, user_data);
17081709
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) &&
17091710
net_context_get_family(context) == AF_CAN) {
1710-
ret = recv_raw(context, cb, timeout, user_data);
1711+
struct sockaddr_can local_addr = {
1712+
.can_family = AF_CAN,
1713+
};
1714+
1715+
ret = recv_raw(context, cb, timeout,
1716+
(struct sockaddr *)&local_addr,
1717+
user_data);
17111718
if (ret == -EALREADY) {
17121719
/* This is perfectly normal for CAN sockets.
17131720
* The SocketCAN will dispatch the packet to

0 commit comments

Comments
 (0)