2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #if defined(NETWARE_CLIB)
15 # include <sys/ioctl.h>
18 #ifndef OPENSSL_NO_SOCK
19 # define SOCKET_PROTOCOL IPPROTO_TCP
21 # define MAX_LISTEN SO_MAXCONN
22 # elif defined(SOMAXCONN)
23 # define MAX_LISTEN SOMAXCONN
25 # define MAX_LISTEN 32
27 # if defined(OPENSSL_SYS_WINDOWS)
28 static int wsa_init_done = 0;
31 # if OPENSSL_API_COMPAT < 0x10100000L
32 int BIO_get_host_ip(const char *str, unsigned char *ip)
34 BIO_ADDRINFO *res = NULL;
37 if (BIO_sock_init() != 1)
38 return 0; /* don't generate another error code here */
40 if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
43 if (BIO_ADDRINFO_family(res) != AF_INET) {
44 BIOerr(BIO_F_BIO_GET_HOST_IP,
45 BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
47 BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), NULL, &l);
48 /* Because only AF_INET addresses will reach this far,
49 we can assert that l should be 4 */
50 OPENSSL_assert(l == 4);
52 BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), ip, &l);
55 BIO_ADDRINFO_free(res);
57 ERR_add_error_data(2, "host=", str);
63 int BIO_get_port(const char *str, unsigned short *port_ptr)
65 BIO_ADDRINFO *res = NULL;
69 BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED);
73 if (BIO_sock_init() != 1)
74 return 0; /* don't generate another error code here */
76 if (BIO_lookup(NULL, str, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
77 if (BIO_ADDRINFO_family(res) != AF_INET) {
78 BIOerr(BIO_F_BIO_GET_PORT,
79 BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET);
81 *port_ptr = ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res)));
84 BIO_ADDRINFO_free(res);
86 ERR_add_error_data(2, "host=", str);
93 int BIO_sock_error(int sock)
96 socklen_t size = sizeof(j);
99 * Note: under Windows the third parameter is of type (char *) whereas
100 * under other systems it is (void *) if you don't have a cast it will
101 * choke the compiler: if you do have a cast then you can either go for
102 * (char *) or (void *).
104 i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size);
106 return (get_last_socket_error());
111 # if OPENSSL_API_COMPAT < 0x10100000L
112 struct hostent *BIO_gethostbyname(const char *name)
115 * Caching gethostbyname() results forever is wrong, so we have to let
116 * the true gethostbyname() worry about this
118 # if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
119 return gethostbyname((char *)name);
121 return gethostbyname(name);
126 int BIO_sock_init(void)
128 # ifdef OPENSSL_SYS_WINDOWS
129 static struct WSAData wsa_state;
131 if (!wsa_init_done) {
135 memset(&wsa_state, 0, sizeof(wsa_state));
137 * Not making wsa_state available to the rest of the code is formally
138 * wrong. But the structures we use are [believed to be] invariable
139 * among Winsock DLLs, while API availability is [expected to be]
140 * probed at run-time with DSO_global_lookup.
142 if (WSAStartup(0x0202, &wsa_state) != 0) {
143 err = WSAGetLastError();
144 SYSerr(SYS_F_WSASTARTUP, err);
145 BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP);
149 # endif /* OPENSSL_SYS_WINDOWS */
151 extern int _watt_do_exit;
152 _watt_do_exit = 0; /* don't make sock_init() call exit() */
160 void bio_sock_cleanup_int(void)
162 # ifdef OPENSSL_SYS_WINDOWS
170 # if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000
172 int BIO_socket_ioctl(int fd, long type, void *arg)
177 i = ioctlsocket(fd, type, (char *)arg);
179 # if defined(OPENSSL_SYS_VMS)
182 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
183 * observe that all the consumers pass in an "unsigned long *",
184 * so we arrange a local copy with a short pointer, and use
187 # if __INITIAL_POINTER_SIZE == 64
189 # pragma pointer_size save
190 # pragma pointer_size 32
191 unsigned long arg_32;
192 unsigned long *arg_32p;
193 # pragma pointer_size restore
195 arg_32 = *((unsigned long *)arg);
196 # else /* __INITIAL_POINTER_SIZE == 64 */
198 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
199 # else /* defined(OPENSSL_SYS_VMS) */
201 # endif /* defined(OPENSSL_SYS_VMS) [else] */
203 i = ioctlsocket(fd, type, ARG);
204 # endif /* __DJGPP__ */
206 SYSerr(SYS_F_IOCTLSOCKET, get_last_socket_error());
209 # endif /* __VMS_VER */
211 # if OPENSSL_API_COMPAT < 0x10100000L
212 int BIO_get_accept_socket(char *host, int bind_mode)
214 int s = INVALID_SOCKET;
215 char *h = NULL, *p = NULL;
216 BIO_ADDRINFO *res = NULL;
218 if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
219 return INVALID_SOCKET;
221 if (BIO_sock_init() != 1)
222 return INVALID_SOCKET;
224 if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
227 if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
228 BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) {
233 if (!BIO_listen(s, BIO_ADDRINFO_address(res),
234 bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
240 BIO_ADDRINFO_free(res);
247 int BIO_accept(int sock, char **ip_port)
252 ret = BIO_accept_ex(sock, &res, 0);
253 if (ret == (int)INVALID_SOCKET) {
254 if (BIO_sock_should_retry(ret)) {
258 SYSerr(SYS_F_ACCEPT, get_last_socket_error());
259 BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR);
263 if (ip_port != NULL) {
264 char *host = BIO_ADDR_hostname_string(&res, 1);
265 char *port = BIO_ADDR_service_string(&res, 1);
266 if (host != NULL && port != NULL)
267 *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2);
271 if (*ip_port == NULL) {
272 BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE);
273 BIO_closesocket(ret);
274 ret = (int)INVALID_SOCKET;
276 strcpy(*ip_port, host);
277 strcat(*ip_port, ":");
278 strcat(*ip_port, port);
289 int BIO_set_tcp_ndelay(int s, int on)
292 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
303 ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
308 int BIO_socket_nbio(int s, int mode)
317 ret = BIO_socket_ioctl(s, FIONBIO, &l);
318 # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
319 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
321 l = fcntl(s, F_GETFL, 0);
323 SYSerr(SYS_F_FCNTL, get_last_rtl_error());
326 # if defined(O_NONBLOCK)
329 l &= ~FNDELAY; /* BSD4.x */
332 # if defined(O_NONBLOCK)
335 l |= FNDELAY; /* BSD4.x */
338 ret = fcntl(s, F_SETFL, l);
341 SYSerr(SYS_F_FCNTL, get_last_rtl_error());
345 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
346 BIOerr(BIO_F_BIO_SOCKET_NBIO, ERR_R_PASSED_INVALID_ARGUMENT);
352 int BIO_sock_info(int sock,
353 enum BIO_sock_info_type type, union BIO_sock_info_u *info)
356 case BIO_SOCK_INFO_ADDRESS:
360 addr_len = sizeof(*info->addr);
361 ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr),
364 SYSerr(SYS_F_GETSOCKNAME, get_last_socket_error());
365 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_ERROR);
368 if ((size_t)addr_len > sizeof(*info->addr)) {
369 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS);
375 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_UNKNOWN_INFO_TYPE);