From: Michel van Langen Subject: [PATCH 2/2] ws2_32/socket: changed interface_bind to dgram_apply_restrictions Message-Id: <440d722a-46cb-9787-235b-de819ce365b5@gmail.com> Date: Sat, 6 Apr 2019 05:26:28 +0200 The new name and signature reflect the real behaviour of this function --- dlls/ws2_32/socket.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/dlls/ws2_32/socket.c b/dlls/ws2_32/socket.c index 4ccfe58eef..3408ed955e 100644 --- a/dlls/ws2_32/socket.c +++ b/dlls/ws2_32/socket.c @@ -3258,30 +3258,27 @@ static int WINAPI WS2_WSARecvMsg( SOCKET s, LPWSAMSG msg, LPDWORD lpNumberOfByte } /*********************************************************************** - * interface_bind (INTERNAL) + * dgram_apply_restrictions (INTERNAL) * * Take bind() calls on any name corresponding to a local network adapter and restrict the given socket to * operating only on the specified interface. This restriction consists of two components: * 1) An outgoing packet restriction suggesting the egress interface for all packets. * 2) An incoming packet restriction dropping packets not meant for the interface. - * If the function succeeds in placing these restrictions (returns TRUE) then the name for the bind() may - * safely be changed to INADDR_ANY, permitting the transmission and receipt of broadcast packets on the - * socket. This behavior is only relevant to UDP sockets and is needed for applications that expect to be able - * to receive broadcast packets on a socket that is bound to a specific network interface. + * This behaviour is only relevant to UDP sockets. */ -static BOOL interface_bind( SOCKET s, int fd, struct sockaddr *addr ) +static void dgram_apply_restrictions( SOCKET s, int fd, struct sockaddr *addr ) { struct sockaddr_in *in_sock = (struct sockaddr_in *) addr; in_addr_t bind_addr = in_sock->sin_addr.s_addr; PIP_ADAPTER_INFO adapters = NULL, adapter; - BOOL ret = FALSE; + BOOL successful = FALSE; DWORD adap_size; int enable = 1; if (bind_addr == htonl(INADDR_ANY) || bind_addr == htonl(INADDR_LOOPBACK)) - return FALSE; /* Not binding to a network adapter, special interface binding unnecessary. */ + return; /* Not binding to a network adapter, special interface binding unnecessary. */ if (_get_fd_type(fd) != SOCK_DGRAM) - return FALSE; /* Special interface binding is only necessary for UDP datagrams. */ + return; /* Special interface binding is only necessary for UDP datagrams. */ if (GetAdaptersInfo(NULL, &adap_size) != ERROR_BUFFER_OVERFLOW) goto cleanup; adapters = HeapAlloc(GetProcessHeap(), 0, adap_size); @@ -3298,7 +3295,7 @@ static BOOL interface_bind( SOCKET s, int fd, struct sockaddr *addr ) /* IP_BOUND_IF sets both the incoming and outgoing restriction at once */ if (setsockopt(fd, IPPROTO_IP, IP_BOUND_IF, &adapter->Index, sizeof(adapter->Index)) != 0) goto cleanup; - ret = TRUE; + successful = TRUE; #elif defined(LINUX_BOUND_IF) in_addr_t ifindex = (in_addr_t) htonl(adapter->Index); struct interface_filter specific_interface_filter; @@ -3313,7 +3310,7 @@ static BOOL interface_bind( SOCKET s, int fd, struct sockaddr *addr ) filter_prog.filter = (struct sock_filter *) &specific_interface_filter; if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog)) != 0) goto cleanup; /* Failed to specify incoming packet filter */ - ret = TRUE; + successful = TRUE; #else FIXME("Broadcast packets on interface-bound sockets are not currently supported on this platform, " "receiving broadcast packets will not work on socket %04lx.\n", s); @@ -3322,16 +3319,15 @@ static BOOL interface_bind( SOCKET s, int fd, struct sockaddr *addr ) } } /* Will soon be switching to INADDR_ANY: permit address reuse */ - if (ret && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == 0) + if (successful && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == 0) TRACE("Socket %04lx bound to interface index %d\n", s, adapter->Index); else - ret = FALSE; + successful = FALSE; cleanup: - if(!ret) + if(!successful) ERR("Failed to bind to interface, receiving broadcast packets will not work on socket %04lx.\n", s); HeapFree(GetProcessHeap(), 0, adapters); - return ret; } /*********************************************************************** @@ -3373,7 +3369,7 @@ int WINAPI WS_bind(SOCKET s, const struct WS_sockaddr* name, int namelen) } else { - interface_bind(s, fd, &uaddr.addr); + dgram_apply_restrictions(s, fd, &uaddr.addr); } } if (bind(fd, &uaddr.addr, uaddrlen) < 0)