From 2f8f5fdd3307eceeeaf31f95b62e151d711a3267 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Wed, 4 Sep 2019 13:12:22 +0300 Subject: [PATCH] net: socket: Set default protocol if proto == 0 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 #18873 Signed-off-by: Jukka Rissanen --- subsys/net/lib/sockets/sockets.c | 10 ++++++++++ subsys/net/lib/sockets/sockets_packet.c | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/subsys/net/lib/sockets/sockets.c b/subsys/net/lib/sockets/sockets.c index b7bf1f921a7d..fbf507588f4d 100644 --- a/subsys/net/lib/sockets/sockets.c +++ b/subsys/net/lib/sockets/sockets.c @@ -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); diff --git a/subsys/net/lib/sockets/sockets_packet.c b/subsys/net/lib/sockets/sockets_packet.c index d6d7005fd934..15f7315d75c4 100644 --- a/subsys/net/lib/sockets/sockets_packet.c +++ b/subsys/net/lib/sockets/sockets_packet.c @@ -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);