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 <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2019-02-27 16:45:18 +02:00
parent ae89c2239b
commit 0032f68669
2 changed files with 14 additions and 3 deletions

View file

@ -306,6 +306,10 @@ int net_conn_register(u16_t proto, u8_t family,
if (net_sin(local_addr)->sin_addr.s_addr) {
flags |= NET_CONN_LOCAL_ADDR_SPEC;
}
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) &&
local_addr->sa_family == AF_CAN) {
memcpy(&conn->local_addr, local_addr,
sizeof(struct sockaddr_can));
} else {
NET_ERR("Local address family not set");
goto error;

View file

@ -1645,6 +1645,7 @@ static enum net_verdict net_context_raw_packet_received(
static int recv_raw(struct net_context *context,
net_context_recv_cb_t cb,
s32_t timeout,
struct sockaddr *local_addr,
void *user_data)
{
int ret;
@ -1665,7 +1666,7 @@ static int recv_raw(struct net_context *context,
ret = net_conn_register(net_context_get_ip_proto(context),
net_context_get_family(context),
NULL, NULL, 0, 0,
NULL, local_addr, 0, 0,
net_context_raw_packet_received,
user_data,
&context->conn_handler);
@ -1704,10 +1705,16 @@ int net_context_recv(struct net_context *context,
} else {
if (IS_ENABLED(CONFIG_NET_SOCKETS_PACKET) &&
net_context_get_family(context) == AF_PACKET) {
ret = recv_raw(context, cb, timeout, user_data);
ret = recv_raw(context, cb, timeout, NULL, user_data);
} else if (IS_ENABLED(CONFIG_NET_SOCKETS_CAN) &&
net_context_get_family(context) == AF_CAN) {
ret = recv_raw(context, cb, timeout, user_data);
struct sockaddr_can local_addr = {
.can_family = AF_CAN,
};
ret = recv_raw(context, cb, timeout,
(struct sockaddr *)&local_addr,
user_data);
if (ret == -EALREADY) {
/* This is perfectly normal for CAN sockets.
* The SocketCAN will dispatch the packet to