diff options
Diffstat (limited to 'crypto/bio/bio_sock.c')
| -rw-r--r-- | crypto/bio/bio_sock.c | 209 |
1 files changed, 104 insertions, 105 deletions
diff --git a/crypto/bio/bio_sock.c b/crypto/bio/bio_sock.c index 84496de6f6bd..49c611245043 100644 --- a/crypto/bio/bio_sock.c +++ b/crypto/bio/bio_sock.c @@ -11,45 +11,45 @@ #include <stdlib.h> #include "bio_local.h" #ifndef OPENSSL_NO_SOCK -# define SOCKET_PROTOCOL IPPROTO_TCP -# ifdef SO_MAXCONN -# define MAX_LISTEN SO_MAXCONN -# elif defined(SOMAXCONN) -# define MAX_LISTEN SOMAXCONN -# else -# define MAX_LISTEN 32 -# endif -# if defined(OPENSSL_SYS_WINDOWS) +#define SOCKET_PROTOCOL IPPROTO_TCP +#ifdef SO_MAXCONN +#define MAX_LISTEN SO_MAXCONN +#elif defined(SOMAXCONN) +#define MAX_LISTEN SOMAXCONN +#else +#define MAX_LISTEN 32 +#endif +#if defined(OPENSSL_SYS_WINDOWS) static int wsa_init_done = 0; -# endif +#endif -# if defined __TANDEM -# include <unistd.h> -# include <sys/time.h> /* select */ -# if defined(OPENSSL_TANDEM_FLOSS) -# include <floss.h(floss_select)> -# endif -# elif defined _WIN32 -# include <winsock.h> /* for type fd_set */ -# else -# include <unistd.h> -# if defined __VMS -# include <sys/socket.h> -# elif defined _HPUX_SOURCE -# include <sys/time.h> -# else -# include <sys/select.h> -# endif -# endif +#if defined __TANDEM +#include <unistd.h> +#include <sys/time.h> /* select */ +#if defined(OPENSSL_TANDEM_FLOSS) +#include <floss.h(floss_select)> +#endif +#elif defined _WIN32 +#include <winsock.h> /* for type fd_set */ +#else +#include <unistd.h> +#if defined __VMS +#include <sys/socket.h> +#elif defined _HPUX_SOURCE +#include <sys/time.h> +#else +#include <sys/select.h> +#endif +#endif -# ifndef OPENSSL_NO_DEPRECATED_1_1_0 +#ifndef OPENSSL_NO_DEPRECATED_1_1_0 int BIO_get_host_ip(const char *str, unsigned char *ip) { BIO_ADDRINFO *res = NULL; int ret = 0; if (BIO_sock_init() != 1) - return 0; /* don't generate another error code here */ + return 0; /* don't generate another error code here */ if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) { size_t l; @@ -83,7 +83,7 @@ int BIO_get_port(const char *str, unsigned short *port_ptr) } if (BIO_sock_init() != 1) - return 0; /* don't generate another error code here */ + return 0; /* don't generate another error code here */ if (BIO_lookup(NULL, str, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) { if (BIO_ADDRINFO_family(res) != AF_INET) { @@ -99,7 +99,7 @@ int BIO_get_port(const char *str, unsigned short *port_ptr) return ret; } -# endif +#endif int BIO_sock_error(int sock) { @@ -119,7 +119,7 @@ int BIO_sock_error(int sock) return j; } -# ifndef OPENSSL_NO_DEPRECATED_1_1_0 +#ifndef OPENSSL_NO_DEPRECATED_1_1_0 struct hostent *BIO_gethostbyname(const char *name) { /* @@ -128,11 +128,11 @@ struct hostent *BIO_gethostbyname(const char *name) */ return gethostbyname(name); } -# endif +#endif int BIO_sock_init(void) { -# ifdef OPENSSL_SYS_WINDOWS +#ifdef OPENSSL_SYS_WINDOWS static struct WSAData wsa_state; if (!wsa_init_done) { @@ -146,40 +146,40 @@ int BIO_sock_init(void) */ if (WSAStartup(0x0202, &wsa_state) != 0) { ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), - "calling wsastartup()"); + "calling wsastartup()"); ERR_raise(ERR_LIB_BIO, BIO_R_WSASTARTUP); return -1; } } -# endif /* OPENSSL_SYS_WINDOWS */ -# ifdef WATT32 +#endif /* OPENSSL_SYS_WINDOWS */ +#ifdef WATT32 extern int _watt_do_exit; - _watt_do_exit = 0; /* don't make sock_init() call exit() */ + _watt_do_exit = 0; /* don't make sock_init() call exit() */ if (sock_init()) return -1; -# endif +#endif return 1; } void bio_sock_cleanup_int(void) { -# ifdef OPENSSL_SYS_WINDOWS +#ifdef OPENSSL_SYS_WINDOWS if (wsa_init_done) { wsa_init_done = 0; WSACleanup(); } -# endif +#endif } int BIO_socket_ioctl(int fd, long type, void *arg) { int i; -# ifdef __DJGPP__ +#ifdef __DJGPP__ i = ioctlsocket(fd, type, (char *)arg); -# else -# if defined(OPENSSL_SYS_VMS) +#else +#if defined(OPENSSL_SYS_VMS) /*- * 2011-02-18 SMS. * VMS ioctl() can't tolerate a 64-bit "void *arg", but we @@ -187,31 +187,31 @@ int BIO_socket_ioctl(int fd, long type, void *arg) * so we arrange a local copy with a short pointer, and use * that, instead. */ -# if __INITIAL_POINTER_SIZE == 64 -# define ARG arg_32p -# pragma pointer_size save -# pragma pointer_size 32 +#if __INITIAL_POINTER_SIZE == 64 +#define ARG arg_32p +#pragma pointer_size save +#pragma pointer_size 32 unsigned long arg_32; unsigned long *arg_32p; -# pragma pointer_size restore +#pragma pointer_size restore arg_32p = &arg_32; arg_32 = *((unsigned long *)arg); -# else /* __INITIAL_POINTER_SIZE == 64 */ -# define ARG arg -# endif /* __INITIAL_POINTER_SIZE == 64 [else] */ -# else /* defined(OPENSSL_SYS_VMS) */ -# define ARG arg -# endif /* defined(OPENSSL_SYS_VMS) [else] */ +#else /* __INITIAL_POINTER_SIZE == 64 */ +#define ARG arg +#endif /* __INITIAL_POINTER_SIZE == 64 [else] */ +#else /* defined(OPENSSL_SYS_VMS) */ +#define ARG arg +#endif /* defined(OPENSSL_SYS_VMS) [else] */ i = ioctlsocket(fd, type, ARG); -# endif /* __DJGPP__ */ +#endif /* __DJGPP__ */ if (i < 0) ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), - "calling ioctlsocket()"); + "calling ioctlsocket()"); return i; } -# ifndef OPENSSL_NO_DEPRECATED_1_1_0 +#ifndef OPENSSL_NO_DEPRECATED_1_1_0 int BIO_get_accept_socket(char *host, int bind_mode) { int s = INVALID_SOCKET; @@ -224,22 +224,23 @@ int BIO_get_accept_socket(char *host, int bind_mode) if (BIO_sock_init() != 1) goto err; - if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0) + if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) == 0) goto err; if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res), - BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) { + BIO_ADDRINFO_protocol(res), 0)) + == INVALID_SOCKET) { s = INVALID_SOCKET; goto err; } if (!BIO_listen(s, BIO_ADDRINFO_address(res), - bind_mode ? BIO_SOCK_REUSEADDR : 0)) { + bind_mode ? BIO_SOCK_REUSEADDR : 0)) { BIO_closesocket(s); s = INVALID_SOCKET; } - err: +err: BIO_ADDRINFO_free(res); OPENSSL_free(h); OPENSSL_free(p); @@ -259,7 +260,7 @@ int BIO_accept(int sock, char **ip_port) goto end; } ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), - "calling accept()"); + "calling accept()"); ERR_raise(ERR_LIB_BIO, BIO_R_ACCEPT_ERROR); goto end; } @@ -285,27 +286,27 @@ int BIO_accept(int sock, char **ip_port) OPENSSL_free(port); } - end: +end: return ret; } -# endif +#endif int BIO_set_tcp_ndelay(int s, int on) { int ret = 0; -# if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) +#if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) int opt; -# ifdef SOL_TCP +#ifdef SOL_TCP opt = SOL_TCP; -# else -# ifdef IPPROTO_TCP +#else +#ifdef IPPROTO_TCP opt = IPPROTO_TCP; -# endif -# endif +#endif +#endif ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on)); -# endif +#endif return (ret == 0); } @@ -315,69 +316,67 @@ int BIO_socket_nbio(int s, int mode) int l; l = mode; -# ifdef FIONBIO +#ifdef FIONBIO l = mode; ret = BIO_socket_ioctl(s, FIONBIO, &l); -# elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY)) +#elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY)) /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */ l = fcntl(s, F_GETFL, 0); if (l == -1) { ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(), - "calling fcntl()"); + "calling fcntl()"); ret = -1; } else { -# if defined(O_NONBLOCK) +#if defined(O_NONBLOCK) l &= ~O_NONBLOCK; -# else +#else l &= ~FNDELAY; /* BSD4.x */ -# endif +#endif if (mode) { -# if defined(O_NONBLOCK) +#if defined(O_NONBLOCK) l |= O_NONBLOCK; -# else +#else l |= FNDELAY; /* BSD4.x */ -# endif +#endif } ret = fcntl(s, F_SETFL, l); if (ret < 0) { ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(), - "calling fcntl()"); + "calling fcntl()"); } } -# else +#else /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */ ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_INVALID_ARGUMENT); -# endif +#endif return (ret == 0); } int BIO_sock_info(int sock, - enum BIO_sock_info_type type, union BIO_sock_info_u *info) + enum BIO_sock_info_type type, union BIO_sock_info_u *info) { switch (type) { - case BIO_SOCK_INFO_ADDRESS: - { - socklen_t addr_len; - int ret = 0; - addr_len = sizeof(*info->addr); - ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr), - &addr_len); - if (ret == -1) { - ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), - "calling getsockname()"); - ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_ERROR); - return 0; - } - if ((size_t)addr_len > sizeof(*info->addr)) { - ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS); - return 0; - } + case BIO_SOCK_INFO_ADDRESS: { + socklen_t addr_len; + int ret = 0; + addr_len = sizeof(*info->addr); + ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr), + &addr_len); + if (ret == -1) { + ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), + "calling getsockname()"); + ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_ERROR); + return 0; + } + if ((size_t)addr_len > sizeof(*info->addr)) { + ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS); + return 0; } - break; + } break; default: ERR_raise(ERR_LIB_BIO, BIO_R_UNKNOWN_INFO_TYPE); return 0; @@ -414,6 +413,6 @@ int BIO_socket_wait(int fd, int for_read, time_t max_time) tv.tv_usec = 0; tv.tv_sec = (long)(max_time - now); /* might overflow */ return select(fd + 1, for_read ? &confds : NULL, - for_read ? NULL : &confds, NULL, &tv); + for_read ? NULL : &confds, NULL, &tv); } #endif /* !defined(OPENSSL_NO_SOCK) */ |
