Skip to content

Commit

Permalink
net: socket: Set default protocol if proto == 0
Browse files Browse the repository at this point in the history
If proto field in socket() call is set to 0, then we should have
a sane default for it that depends on the type of the socket.

Fixes zephyrproject-rtos#18873

Signed-off-by: Jukka Rissanen <[email protected]>
  • Loading branch information
jukkar committed Sep 12, 2019
1 parent e182f31 commit 2f8f5fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions subsys/net/lib/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ int zsock_socket_internal(int family, int type, int proto)
return -1;
}

if (proto == 0) {
if (family == AF_INET || family == AF_INET6) {
if (type == SOCK_DGRAM) {
proto = IPPROTO_UDP;
} else if (type == SOCK_STREAM) {
proto = IPPROTO_TCP;
}
}
}

res = net_context_get(family, type, proto, &ctx);
if (res < 0) {
z_free_fd(fd);
Expand Down
6 changes: 6 additions & 0 deletions subsys/net/lib/sockets/sockets_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ static int zpacket_socket(int family, int type, int proto)
return -1;
}

if (proto == 0) {
if (type == SOCK_RAW) {
proto = IPPROTO_RAW;
}
}

ret = net_context_get(family, type, proto, &ctx);
if (ret < 0) {
z_free_fd(fd);
Expand Down

0 comments on commit 2f8f5fd

Please sign in to comment.