sl@0: /* crypto/bio/b_sock.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #define USE_SOCKETS sl@0: #include "cryptlib.h" sl@0: #include sl@0: #if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK) sl@0: #include "netdb.h" sl@0: #endif sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: sl@0: #ifndef OPENSSL_NO_SOCK sl@0: sl@0: #ifdef OPENSSL_SYS_WIN16 sl@0: #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ sl@0: #else sl@0: #define SOCKET_PROTOCOL IPPROTO_TCP sl@0: #endif sl@0: sl@0: #ifdef SO_MAXCONN sl@0: #define MAX_LISTEN SO_MAXCONN sl@0: #elif defined(SOMAXCONN) sl@0: #define MAX_LISTEN SOMAXCONN sl@0: #else sl@0: #define MAX_LISTEN 32 sl@0: #endif sl@0: sl@0: #if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)) sl@0: static int wsa_init_done=0; sl@0: #endif sl@0: sl@0: #if 0 sl@0: static unsigned long BIO_ghbn_hits=0L; sl@0: static unsigned long BIO_ghbn_miss=0L; sl@0: sl@0: #define GHBN_NUM 4 sl@0: static struct ghbn_cache_st sl@0: { sl@0: char name[129]; sl@0: struct hostent *ent; sl@0: unsigned long order; sl@0: } ghbn_cache[GHBN_NUM]; sl@0: #endif sl@0: sl@0: static int get_ip(const char *str,unsigned char *ip); sl@0: #if 0 sl@0: static void ghbn_free(struct hostent *a); sl@0: static struct hostent *ghbn_dup(struct hostent *a); sl@0: #endif sl@0: EXPORT_C int BIO_get_host_ip(const char *str, unsigned char *ip) sl@0: { sl@0: int i; sl@0: int err = 1; sl@0: int locked = 0; sl@0: struct hostent *he; sl@0: sl@0: i=get_ip(str,ip); sl@0: if (i < 0) sl@0: { sl@0: BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_INVALID_IP_ADDRESS); sl@0: goto err; sl@0: } sl@0: sl@0: /* At this point, we have something that is most probably correct sl@0: in some way, so let's init the socket. */ sl@0: if (BIO_sock_init() != 1) sl@0: return 0; /* don't generate another error code here */ sl@0: sl@0: /* If the string actually contained an IP address, we need not do sl@0: anything more */ sl@0: if (i > 0) return(1); sl@0: sl@0: /* do a gethostbyname */ sl@0: CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME); sl@0: locked = 1; sl@0: he=BIO_gethostbyname(str); sl@0: if (he == NULL) sl@0: { sl@0: BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_BAD_HOSTNAME_LOOKUP); sl@0: goto err; sl@0: } sl@0: sl@0: /* cast to short because of win16 winsock definition */ sl@0: if ((short)he->h_addrtype != AF_INET) sl@0: { sl@0: BIOerr(BIO_F_BIO_GET_HOST_IP,BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET); sl@0: goto err; sl@0: } sl@0: for (i=0; i<4; i++) sl@0: ip[i]=he->h_addr_list[0][i]; sl@0: err = 0; sl@0: sl@0: err: sl@0: if (locked) sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME); sl@0: if (err) sl@0: { sl@0: ERR_add_error_data(2,"host=",str); sl@0: return 0; sl@0: } sl@0: else sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int BIO_get_port(const char *str, unsigned short *port_ptr) sl@0: { sl@0: int i; sl@0: struct servent *s; sl@0: sl@0: if (str == NULL) sl@0: { sl@0: BIOerr(BIO_F_BIO_GET_PORT,BIO_R_NO_PORT_DEFINED); sl@0: return(0); sl@0: } sl@0: i=atoi(str); sl@0: if (i != 0) sl@0: *port_ptr=(unsigned short)i; sl@0: else sl@0: { sl@0: CRYPTO_w_lock(CRYPTO_LOCK_GETSERVBYNAME); sl@0: /* Note: under VMS with SOCKETSHR, it seems like the first sl@0: * parameter is 'char *', instead of 'const char *' sl@0: */ sl@0: s=getservbyname( sl@0: #ifndef CONST_STRICT sl@0: (char *) sl@0: #endif sl@0: str,"tcp"); sl@0: if(s != NULL) sl@0: *port_ptr=ntohs((unsigned short)s->s_port); sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_GETSERVBYNAME); sl@0: if(s == NULL) sl@0: { sl@0: if (strcmp(str,"http") == 0) sl@0: *port_ptr=80; sl@0: else if (strcmp(str,"telnet") == 0) sl@0: *port_ptr=23; sl@0: else if (strcmp(str,"socks") == 0) sl@0: *port_ptr=1080; sl@0: else if (strcmp(str,"https") == 0) sl@0: *port_ptr=443; sl@0: else if (strcmp(str,"ssl") == 0) sl@0: *port_ptr=443; sl@0: else if (strcmp(str,"ftp") == 0) sl@0: *port_ptr=21; sl@0: else if (strcmp(str,"gopher") == 0) sl@0: *port_ptr=70; sl@0: #if 0 sl@0: else if (strcmp(str,"wais") == 0) sl@0: *port_ptr=21; sl@0: #endif sl@0: else sl@0: { sl@0: SYSerr(SYS_F_GETSERVBYNAME,get_last_socket_error()); sl@0: ERR_add_error_data(3,"service='",str,"'"); sl@0: return(0); sl@0: } sl@0: } sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C int BIO_sock_error(int sock) sl@0: { sl@0: int j,i; sl@0: int size; sl@0: sl@0: size=sizeof(int); sl@0: /* Note: under Windows the third parameter is of type (char *) sl@0: * whereas under other systems it is (void *) if you don't have sl@0: * a cast it will choke the compiler: if you do have a cast then sl@0: * you can either go for (char *) or (void *). sl@0: */ sl@0: i=getsockopt(sock,SOL_SOCKET,SO_ERROR,(void *)&j,(void *)&size); sl@0: if (i < 0) sl@0: return(1); sl@0: else sl@0: return(j); sl@0: } sl@0: sl@0: #if 0 sl@0: long BIO_ghbn_ctrl(int cmd, int iarg, char *parg) sl@0: { sl@0: int i; sl@0: char **p; sl@0: sl@0: switch (cmd) sl@0: { sl@0: case BIO_GHBN_CTRL_HITS: sl@0: return(BIO_ghbn_hits); sl@0: /* break; */ sl@0: case BIO_GHBN_CTRL_MISSES: sl@0: return(BIO_ghbn_miss); sl@0: /* break; */ sl@0: case BIO_GHBN_CTRL_CACHE_SIZE: sl@0: return(GHBN_NUM); sl@0: /* break; */ sl@0: case BIO_GHBN_CTRL_GET_ENTRY: sl@0: if ((iarg >= 0) && (iarg 0)) sl@0: { sl@0: p=(char **)parg; sl@0: if (p == NULL) return(0); sl@0: *p=ghbn_cache[iarg].name; sl@0: ghbn_cache[iarg].name[128]='\0'; sl@0: return(1); sl@0: } sl@0: return(0); sl@0: /* break; */ sl@0: case BIO_GHBN_CTRL_FLUSH: sl@0: for (i=0; ih_aliases[i] != NULL; i++) sl@0: ; sl@0: i++; sl@0: ret->h_aliases = (char **)OPENSSL_malloc(i*sizeof(char *)); sl@0: if (ret->h_aliases == NULL) sl@0: goto err; sl@0: memset(ret->h_aliases, 0, i*sizeof(char *)); sl@0: sl@0: for (i=0; a->h_addr_list[i] != NULL; i++) sl@0: ; sl@0: i++; sl@0: ret->h_addr_list=(char **)OPENSSL_malloc(i*sizeof(char *)); sl@0: if (ret->h_addr_list == NULL) sl@0: goto err; sl@0: memset(ret->h_addr_list, 0, i*sizeof(char *)); sl@0: sl@0: j=strlen(a->h_name)+1; sl@0: if ((ret->h_name=OPENSSL_malloc(j)) == NULL) goto err; sl@0: memcpy((char *)ret->h_name,a->h_name,j); sl@0: for (i=0; a->h_aliases[i] != NULL; i++) sl@0: { sl@0: j=strlen(a->h_aliases[i])+1; sl@0: if ((ret->h_aliases[i]=OPENSSL_malloc(j)) == NULL) goto err; sl@0: memcpy(ret->h_aliases[i],a->h_aliases[i],j); sl@0: } sl@0: ret->h_length=a->h_length; sl@0: ret->h_addrtype=a->h_addrtype; sl@0: for (i=0; a->h_addr_list[i] != NULL; i++) sl@0: { sl@0: if ((ret->h_addr_list[i]=OPENSSL_malloc(a->h_length)) == NULL) sl@0: goto err; sl@0: memcpy(ret->h_addr_list[i],a->h_addr_list[i],a->h_length); sl@0: } sl@0: if (0) sl@0: { sl@0: err: sl@0: if (ret != NULL) sl@0: ghbn_free(ret); sl@0: ret=NULL; sl@0: } sl@0: MemCheck_on(); sl@0: return(ret); sl@0: } sl@0: sl@0: static void ghbn_free(struct hostent *a) sl@0: { sl@0: int i; sl@0: sl@0: if(a == NULL) sl@0: return; sl@0: sl@0: if (a->h_aliases != NULL) sl@0: { sl@0: for (i=0; a->h_aliases[i] != NULL; i++) sl@0: OPENSSL_free(a->h_aliases[i]); sl@0: OPENSSL_free(a->h_aliases); sl@0: } sl@0: if (a->h_addr_list != NULL) sl@0: { sl@0: for (i=0; a->h_addr_list[i] != NULL; i++) sl@0: OPENSSL_free(a->h_addr_list[i]); sl@0: OPENSSL_free(a->h_addr_list); sl@0: } sl@0: if (a->h_name != NULL) OPENSSL_free(a->h_name); sl@0: OPENSSL_free(a); sl@0: } sl@0: sl@0: #endif sl@0: sl@0: EXPORT_C struct hostent *BIO_gethostbyname(const char *name) sl@0: { sl@0: #if 1 sl@0: /* Caching gethostbyname() results forever is wrong, sl@0: * so we have to let the true gethostbyname() worry about this */ sl@0: return gethostbyname(name); sl@0: #else sl@0: struct hostent *ret; sl@0: int i,lowi=0,j; sl@0: unsigned long low= (unsigned long)-1; sl@0: sl@0: sl@0: # if 0 sl@0: /* It doesn't make sense to use locking here: The function interface sl@0: * is not thread-safe, because threads can never be sure when sl@0: * some other thread destroys the data they were given a pointer to. sl@0: */ sl@0: CRYPTO_w_lock(CRYPTO_LOCK_GETHOSTBYNAME); sl@0: # endif sl@0: j=strlen(name); sl@0: if (j < 128) sl@0: { sl@0: for (i=0; i ghbn_cache[i].order) sl@0: { sl@0: low=ghbn_cache[i].order; sl@0: lowi=i; sl@0: } sl@0: if (ghbn_cache[i].order > 0) sl@0: { sl@0: if (strncmp(name,ghbn_cache[i].name,128) == 0) sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: else sl@0: i=GHBN_NUM; sl@0: sl@0: if (i == GHBN_NUM) /* no hit*/ sl@0: { sl@0: BIO_ghbn_miss++; sl@0: /* Note: under VMS with SOCKETSHR, it seems like the first sl@0: * parameter is 'char *', instead of 'const char *' sl@0: */ sl@0: ret=gethostbyname( sl@0: # ifndef CONST_STRICT sl@0: (char *) sl@0: # endif sl@0: name); sl@0: sl@0: if (ret == NULL) sl@0: goto end; sl@0: if (j > 128) /* too big to cache */ sl@0: { sl@0: # if 0 sl@0: /* If we were trying to make this function thread-safe (which sl@0: * is bound to fail), we'd have to give up in this case sl@0: * (or allocate more memory). */ sl@0: ret = NULL; sl@0: # endif sl@0: goto end; sl@0: } sl@0: sl@0: /* else add to cache */ sl@0: if (ghbn_cache[lowi].ent != NULL) sl@0: ghbn_free(ghbn_cache[lowi].ent); /* XXX not thread-safe */ sl@0: ghbn_cache[lowi].name[0] = '\0'; sl@0: sl@0: if((ret=ghbn_cache[lowi].ent=ghbn_dup(ret)) == NULL) sl@0: { sl@0: BIOerr(BIO_F_BIO_GETHOSTBYNAME,ERR_R_MALLOC_FAILURE); sl@0: goto end; sl@0: } sl@0: strncpy(ghbn_cache[lowi].name,name,128); sl@0: ghbn_cache[lowi].order=BIO_ghbn_miss+BIO_ghbn_hits; sl@0: } sl@0: else sl@0: { sl@0: BIO_ghbn_hits++; sl@0: ret= ghbn_cache[i].ent; sl@0: ghbn_cache[i].order=BIO_ghbn_miss+BIO_ghbn_hits; sl@0: } sl@0: end: sl@0: # if 0 sl@0: CRYPTO_w_unlock(CRYPTO_LOCK_GETHOSTBYNAME); sl@0: # endif sl@0: return(ret); sl@0: #endif sl@0: } sl@0: sl@0: sl@0: EXPORT_C int BIO_sock_init(void) sl@0: { sl@0: #ifdef OPENSSL_SYS_WINDOWS sl@0: static struct WSAData wsa_state; sl@0: sl@0: if (!wsa_init_done) sl@0: { sl@0: int err; sl@0: sl@0: wsa_init_done=1; sl@0: memset(&wsa_state,0,sizeof(wsa_state)); sl@0: if (WSAStartup(0x0101,&wsa_state)!=0) sl@0: { sl@0: err=WSAGetLastError(); sl@0: SYSerr(SYS_F_WSASTARTUP,err); sl@0: BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP); sl@0: return(-1); sl@0: } sl@0: } sl@0: #endif /* OPENSSL_SYS_WINDOWS */ sl@0: #ifdef WATT32 sl@0: extern int _watt_do_exit; sl@0: _watt_do_exit = 0; /* don't make sock_init() call exit() */ sl@0: if (sock_init()) sl@0: return (-1); sl@0: #endif sl@0: sl@0: #if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK) sl@0: WORD wVerReq; sl@0: WSADATA wsaData; sl@0: int err; sl@0: sl@0: if (!wsa_init_done) sl@0: { sl@0: sl@0: sl@0: wsa_init_done=1; sl@0: wVerReq = MAKEWORD( 2, 0 ); sl@0: err = WSAStartup(wVerReq,&wsaData); sl@0: if (err != 0) sl@0: { sl@0: SYSerr(SYS_F_WSASTARTUP,err); sl@0: BIOerr(BIO_F_BIO_SOCK_INIT,BIO_R_WSASTARTUP); sl@0: return(-1); sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C void BIO_sock_cleanup(void) sl@0: { sl@0: #ifdef OPENSSL_SYS_WINDOWS sl@0: if (wsa_init_done) sl@0: { sl@0: wsa_init_done=0; sl@0: #ifndef OPENSSL_SYS_WINCE sl@0: WSACancelBlockingCall(); sl@0: #endif sl@0: WSACleanup(); sl@0: } sl@0: #elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK) sl@0: if (wsa_init_done) sl@0: { sl@0: wsa_init_done=0; sl@0: WSACleanup(); sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: #if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000 sl@0: sl@0: EXPORT_C int BIO_socket_ioctl(int fd, long type, void *arg) sl@0: { sl@0: int i; sl@0: sl@0: #ifdef __DJGPP__ sl@0: i=ioctlsocket(fd,type,(char *)arg); sl@0: #else sl@0: i=ioctlsocket(fd,type,arg); sl@0: #endif /* __DJGPP__ */ sl@0: if (i < 0) sl@0: SYSerr(SYS_F_IOCTLSOCKET,get_last_socket_error()); sl@0: return(i); sl@0: } sl@0: #endif /* __VMS_VER */ sl@0: sl@0: /* The reason I have implemented this instead of using sscanf is because sl@0: * Visual C 1.52c gives an unresolved external when linking a DLL :-( */ sl@0: static int get_ip(const char *str, unsigned char ip[4]) sl@0: { sl@0: unsigned int tmp[4]; sl@0: int num=0,c,ok=0; sl@0: sl@0: tmp[0]=tmp[1]=tmp[2]=tmp[3]=0; sl@0: sl@0: for (;;) sl@0: { sl@0: c= *(str++); sl@0: if ((c >= '0') && (c <= '9')) sl@0: { sl@0: ok=1; sl@0: tmp[num]=tmp[num]*10+c-'0'; sl@0: if (tmp[num] > 255) return(0); sl@0: } sl@0: else if (c == '.') sl@0: { sl@0: if (!ok) return(-1); sl@0: if (num == 3) return(0); sl@0: num++; sl@0: ok=0; sl@0: } sl@0: else if (c == '\0' && (num == 3) && ok) sl@0: break; sl@0: else sl@0: return(0); sl@0: } sl@0: ip[0]=tmp[0]; sl@0: ip[1]=tmp[1]; sl@0: ip[2]=tmp[2]; sl@0: ip[3]=tmp[3]; sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C int BIO_get_accept_socket(char *host, int bind_mode) sl@0: { sl@0: int ret=0; sl@0: struct sockaddr_in server,client; sl@0: int s=INVALID_SOCKET,cs; sl@0: unsigned char ip[4]; sl@0: unsigned short port; sl@0: char *str=NULL,*e; sl@0: const char *h,*p; sl@0: unsigned long l; sl@0: int err_num; sl@0: sl@0: if (BIO_sock_init() != 1) return(INVALID_SOCKET); sl@0: sl@0: if ((str=BUF_strdup(host)) == NULL) return(INVALID_SOCKET); sl@0: sl@0: h=p=NULL; sl@0: h=str; sl@0: for (e=str; *e; e++) sl@0: { sl@0: if (*e == ':') sl@0: { sl@0: p= &(e[1]); sl@0: *e='\0'; sl@0: } sl@0: else if (*e == '/') sl@0: { sl@0: *e='\0'; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: if (p == NULL) sl@0: { sl@0: p=h; sl@0: h="*"; sl@0: } sl@0: sl@0: if (!BIO_get_port(p,&port)) goto err; sl@0: sl@0: memset((char *)&server,0,sizeof(server)); sl@0: server.sin_family=AF_INET; sl@0: server.sin_port=htons(port); sl@0: sl@0: if (strcmp(h,"*") == 0) sl@0: server.sin_addr.s_addr=INADDR_ANY; sl@0: else sl@0: { sl@0: if (!BIO_get_host_ip(h,&(ip[0]))) goto err; sl@0: l=(unsigned long) sl@0: ((unsigned long)ip[0]<<24L)| sl@0: ((unsigned long)ip[1]<<16L)| sl@0: ((unsigned long)ip[2]<< 8L)| sl@0: ((unsigned long)ip[3]); sl@0: server.sin_addr.s_addr=htonl(l); sl@0: } sl@0: sl@0: again: sl@0: s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); sl@0: if (s == INVALID_SOCKET) sl@0: { sl@0: SYSerr(SYS_F_SOCKET,get_last_socket_error()); sl@0: ERR_add_error_data(3,"port='",host,"'"); sl@0: BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_CREATE_SOCKET); sl@0: goto err; sl@0: } sl@0: sl@0: #ifdef SO_REUSEADDR sl@0: if (bind_mode == BIO_BIND_REUSEADDR) sl@0: { sl@0: int i=1; sl@0: sl@0: ret=setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&i,sizeof(i)); sl@0: bind_mode=BIO_BIND_NORMAL; sl@0: } sl@0: #endif sl@0: if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1) sl@0: { sl@0: #ifdef SO_REUSEADDR sl@0: err_num=get_last_socket_error(); sl@0: if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) && sl@0: (err_num == EADDRINUSE)) sl@0: { sl@0: memcpy((char *)&client,(char *)&server,sizeof(server)); sl@0: if (strcmp(h,"*") == 0) sl@0: client.sin_addr.s_addr=htonl(0x7F000001); sl@0: cs=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); sl@0: if (cs != INVALID_SOCKET) sl@0: { sl@0: int ii; sl@0: ii=connect(cs,(struct sockaddr *)&client, sl@0: sizeof(client)); sl@0: closesocket(cs); sl@0: if (ii == INVALID_SOCKET) sl@0: { sl@0: bind_mode=BIO_BIND_REUSEADDR; sl@0: closesocket(s); sl@0: goto again; sl@0: } sl@0: /* else error */ sl@0: } sl@0: /* else error */ sl@0: } sl@0: #endif sl@0: SYSerr(SYS_F_BIND,err_num); sl@0: ERR_add_error_data(3,"port='",host,"'"); sl@0: BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_BIND_SOCKET); sl@0: goto err; sl@0: } sl@0: if (listen(s,MAX_LISTEN) == -1) sl@0: { sl@0: SYSerr(SYS_F_BIND,get_last_socket_error()); sl@0: ERR_add_error_data(3,"port='",host,"'"); sl@0: BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET,BIO_R_UNABLE_TO_LISTEN_SOCKET); sl@0: goto err; sl@0: } sl@0: ret=1; sl@0: err: sl@0: if (str != NULL) OPENSSL_free(str); sl@0: if ((ret == 0) && (s != INVALID_SOCKET)) sl@0: { sl@0: closesocket(s); sl@0: s= INVALID_SOCKET; sl@0: } sl@0: return(s); sl@0: } sl@0: #ifdef EMULATOR sl@0: GET_STATIC_VAR_FROM_TLS(from,b_sock,struct sockaddr_in) sl@0: #define from (*GET_WSD_VAR_NAME(from,b_sock, s)()) sl@0: #endif sl@0: sl@0: EXPORT_C int BIO_accept(int sock, char **addr) sl@0: { sl@0: int ret=INVALID_SOCKET; sl@0: #ifndef EMULATOR sl@0: static struct sockaddr_in from; sl@0: #endif sl@0: unsigned long l; sl@0: unsigned short port; sl@0: int len; sl@0: char *p; sl@0: sl@0: memset((char *)&from,0,sizeof(from)); sl@0: len=sizeof(from); sl@0: /* Note: under VMS with SOCKETSHR the fourth parameter is currently sl@0: * of type (int *) whereas under other systems it is (void *) if sl@0: * you don't have a cast it will choke the compiler: if you do sl@0: * have a cast then you can either go for (int *) or (void *). sl@0: */ sl@0: ret=accept(sock,(struct sockaddr *)&from,(void *)&len); sl@0: if (ret == INVALID_SOCKET) sl@0: { sl@0: if(BIO_sock_should_retry(ret)) return -2; sl@0: SYSerr(SYS_F_ACCEPT,get_last_socket_error()); sl@0: BIOerr(BIO_F_BIO_ACCEPT,BIO_R_ACCEPT_ERROR); sl@0: goto end; sl@0: } sl@0: sl@0: if (addr == NULL) goto end; sl@0: sl@0: l=ntohl(from.sin_addr.s_addr); sl@0: port=ntohs(from.sin_port); sl@0: if (*addr == NULL) sl@0: { sl@0: if ((p=OPENSSL_malloc(24)) == NULL) sl@0: { sl@0: BIOerr(BIO_F_BIO_ACCEPT,ERR_R_MALLOC_FAILURE); sl@0: goto end; sl@0: } sl@0: *addr=p; sl@0: } sl@0: BIO_snprintf(*addr,24,"%d.%d.%d.%d:%d", sl@0: (unsigned char)(l>>24L)&0xff, sl@0: (unsigned char)(l>>16L)&0xff, sl@0: (unsigned char)(l>> 8L)&0xff, sl@0: (unsigned char)(l )&0xff, sl@0: port); sl@0: end: sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int BIO_set_tcp_ndelay(int s, int on) sl@0: { sl@0: int ret=0; sl@0: #if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) sl@0: int opt; sl@0: sl@0: #ifdef SOL_TCP sl@0: opt=SOL_TCP; sl@0: #else sl@0: #ifdef IPPROTO_TCP sl@0: opt=IPPROTO_TCP; sl@0: #endif sl@0: #endif sl@0: sl@0: ret=setsockopt(s,opt,TCP_NODELAY,(char *)&on,sizeof(on)); sl@0: #endif sl@0: return(ret == 0); sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int BIO_socket_nbio(int s, int mode) sl@0: { sl@0: int ret= -1; sl@0: int l; sl@0: sl@0: l=mode; sl@0: #ifdef FIONBIO sl@0: ret=BIO_socket_ioctl(s,FIONBIO,&l); sl@0: #endif sl@0: return(ret == 0); sl@0: }