From: Andy Polyakov Date: Thu, 7 Jan 2010 13:12:30 +0000 (+0000) Subject: http://cvs.openssl.org/chngview?cn=19053 made me wonder if bind() and X-Git-Tag: OpenSSL-fips-2_0-rc1~1358 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=25d1d62275316c9b13163bd910c831c850467f81;p=oweals%2Fopenssl.git cvs.openssl.org/chngview?cn=19053 made me wonder if bind() and connect() are as finicky as sendto() when it comes to socket address length. As it turned out they are, therefore the fix. Note that you can't reproduce the problem on Linux, it was failing on Solaris, FreeBSD, most likely on more... --- diff --git a/crypto/bio/b_sock.c b/crypto/bio/b_sock.c index 8a69e99f82..5ea621c0cf 100644 --- a/crypto/bio/b_sock.c +++ b/crypto/bio/b_sock.c @@ -606,7 +606,7 @@ int BIO_get_accept_socket(char *host, int bind_mode) struct sockaddr_in6 sa_in6; #endif } server,client; - int s=INVALID_SOCKET,cs; + int s=INVALID_SOCKET,cs,addrlen; unsigned char ip[4]; unsigned short port; char *str=NULL,*e; @@ -677,8 +677,10 @@ int BIO_get_accept_socket(char *host, int bind_mode) if ((*p_getaddrinfo.f)(h,p,&hint,&res)) break; - memcpy(&server, res->ai_addr, - res->ai_addrlen<=sizeof(server)?res->ai_addrlen:sizeof(server)); + addrlen = res->ai_addrlen<=sizeof(server) ? + res->ai_addrlen : + sizeof(server); + memcpy(&server, res->ai_addr, addrlen); (*p_freeaddrinfo.f)(res); goto again; @@ -690,6 +692,7 @@ int BIO_get_accept_socket(char *host, int bind_mode) memset((char *)&server,0,sizeof(server)); server.sa_in.sin_family=AF_INET; server.sa_in.sin_port=htons(port); + addrlen = sizeof(server.sa_in); if (h == NULL || strcmp(h,"*") == 0) server.sa_in.sin_addr.s_addr=INADDR_ANY; @@ -723,7 +726,7 @@ again: bind_mode=BIO_BIND_NORMAL; } #endif - if (bind(s,&server.sa,sizeof(server)) == -1) + if (bind(s,&server.sa,addrlen) == -1) { #ifdef SO_REUSEADDR err_num=get_last_socket_error(); @@ -751,7 +754,7 @@ again: if (cs != INVALID_SOCKET) { int ii; - ii=connect(cs,&client.sa,sizeof(client)); + ii=connect(cs,&client.sa,addrlen); closesocket(cs); if (ii == INVALID_SOCKET) {