From: Robin Ebert Subject: [PATCH v3 2/2] server: Get socket protocol from kernel if not given. Message-Id: <20200824171442.3771-2-ebertrobin2002@gmail.com> Date: Mon, 24 Aug 2020 19:14:42 +0200 In-Reply-To: <20200824171442.3771-1-ebertrobin2002@gmail.com> References: <20200824171442.3771-1-ebertrobin2002@gmail.com> The kernel selects a protocol if zero is given but the corresponding sock struct is not set accordingly. This causes problems in WS_getsockopt when trying to get socket's WSAPROTOCOL_INFOW struct. Signed-off-by: Robin Ebert --- v2: Fixed protolen type --- server/sock.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/server/sock.c b/server/sock.c index 1a53ce4b..fc432324 100644 --- a/server/sock.c +++ b/server/sock.c @@ -38,6 +38,9 @@ #ifdef HAVE_SYS_SOCKET_H # include #endif +#ifdef HAVE_NETINET_IN_H +# include +#endif #ifdef HAVE_SYS_IOCTL_H #include #endif @@ -659,6 +662,8 @@ static struct object *create_socket( int family, int type, int protocol, unsigne { struct sock *sock; int sockfd; + int proto; + socklen_t protolen; sockfd = socket( family, type, protocol ); if (sockfd == -1) @@ -676,7 +681,19 @@ static struct object *create_socket( int family, int type, int protocol, unsigne init_sock( sock ); sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0; sock->flags = flags; - sock->proto = protocol; + if(!protocol) + { + /* if protocol is 0 get it from the kernel */ + protolen = sizeof(int); + if(getsockopt(sockfd, SOL_SOCKET, SO_PROTOCOL, &proto, &protolen)) + { + release_object( sock ); + return NULL; + } + sock->proto = proto; + } + else + sock->proto = protocol; sock->type = type; sock->family = family; -- 2.20.1