2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 #ifndef OPENSSL_NO_SOCK
15 # define SOCKET_PROTOCOL IPPROTO_TCP
17 # define MAX_LISTEN SO_MAXCONN
18 # elif defined(SOMAXCONN)
19 # define MAX_LISTEN SOMAXCONN
21 # define MAX_LISTEN 32
23 # if defined(OPENSSL_SYS_WINDOWS)
24 static int wsa_init_done
= 0;
27 # if !OPENSSL_API_1_1_0
28 int BIO_get_host_ip(const char *str
, unsigned char *ip
)
30 BIO_ADDRINFO
*res
= NULL
;
33 if (BIO_sock_init() != 1)
34 return 0; /* don't generate another error code here */
36 if (BIO_lookup(str
, NULL
, BIO_LOOKUP_CLIENT
, AF_INET
, SOCK_STREAM
, &res
)) {
39 if (BIO_ADDRINFO_family(res
) != AF_INET
) {
40 BIOerr(BIO_F_BIO_GET_HOST_IP
,
41 BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET
);
42 } else if (BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res
), NULL
, &l
)) {
44 * Because only AF_INET addresses will reach this far, we can assert
47 if (ossl_assert(l
== 4))
48 ret
= BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res
), ip
, &l
);
50 BIO_ADDRINFO_free(res
);
52 ERR_add_error_data(2, "host=", str
);
58 int BIO_get_port(const char *str
, unsigned short *port_ptr
)
60 BIO_ADDRINFO
*res
= NULL
;
64 BIOerr(BIO_F_BIO_GET_PORT
, BIO_R_NO_PORT_DEFINED
);
68 if (BIO_sock_init() != 1)
69 return 0; /* don't generate another error code here */
71 if (BIO_lookup(NULL
, str
, BIO_LOOKUP_CLIENT
, AF_INET
, SOCK_STREAM
, &res
)) {
72 if (BIO_ADDRINFO_family(res
) != AF_INET
) {
73 BIOerr(BIO_F_BIO_GET_PORT
,
74 BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET
);
76 *port_ptr
= ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res
)));
79 BIO_ADDRINFO_free(res
);
81 ERR_add_error_data(2, "host=", str
);
88 int BIO_sock_error(int sock
)
91 socklen_t size
= sizeof(j
);
94 * Note: under Windows the third parameter is of type (char *) whereas
95 * under other systems it is (void *) if you don't have a cast it will
96 * choke the compiler: if you do have a cast then you can either go for
97 * (char *) or (void *).
99 i
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, (void *)&j
, &size
);
101 return get_last_socket_error();
106 # if !OPENSSL_API_1_1_0
107 struct hostent
*BIO_gethostbyname(const char *name
)
110 * Caching gethostbyname() results forever is wrong, so we have to let
111 * the true gethostbyname() worry about this
113 return gethostbyname(name
);
117 int BIO_sock_init(void)
119 # ifdef OPENSSL_SYS_WINDOWS
120 static struct WSAData wsa_state
;
122 if (!wsa_init_done
) {
124 memset(&wsa_state
, 0, sizeof(wsa_state
));
126 * Not making wsa_state available to the rest of the code is formally
127 * wrong. But the structures we use are [believed to be] invariable
128 * among Winsock DLLs, while API availability is [expected to be]
129 * probed at run-time with DSO_global_lookup.
131 if (WSAStartup(0x0202, &wsa_state
) != 0) {
132 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
133 "calling wsastartup()");
134 BIOerr(BIO_F_BIO_SOCK_INIT
, BIO_R_WSASTARTUP
);
138 # endif /* OPENSSL_SYS_WINDOWS */
140 extern int _watt_do_exit
;
141 _watt_do_exit
= 0; /* don't make sock_init() call exit() */
149 void bio_sock_cleanup_int(void)
151 # ifdef OPENSSL_SYS_WINDOWS
159 int BIO_socket_ioctl(int fd
, long type
, void *arg
)
164 i
= ioctlsocket(fd
, type
, (char *)arg
);
166 # if defined(OPENSSL_SYS_VMS)
169 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
170 * observe that all the consumers pass in an "unsigned long *",
171 * so we arrange a local copy with a short pointer, and use
174 # if __INITIAL_POINTER_SIZE == 64
176 # pragma pointer_size save
177 # pragma pointer_size 32
178 unsigned long arg_32
;
179 unsigned long *arg_32p
;
180 # pragma pointer_size restore
182 arg_32
= *((unsigned long *)arg
);
183 # else /* __INITIAL_POINTER_SIZE == 64 */
185 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
186 # else /* defined(OPENSSL_SYS_VMS) */
188 # endif /* defined(OPENSSL_SYS_VMS) [else] */
190 i
= ioctlsocket(fd
, type
, ARG
);
191 # endif /* __DJGPP__ */
193 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
194 "calling ioctlsocket()");
198 # if !OPENSSL_API_1_1_0
199 int BIO_get_accept_socket(char *host
, int bind_mode
)
201 int s
= INVALID_SOCKET
;
202 char *h
= NULL
, *p
= NULL
;
203 BIO_ADDRINFO
*res
= NULL
;
205 if (!BIO_parse_hostserv(host
, &h
, &p
, BIO_PARSE_PRIO_SERV
))
206 return INVALID_SOCKET
;
208 if (BIO_sock_init() != 1)
209 return INVALID_SOCKET
;
211 if (BIO_lookup(h
, p
, BIO_LOOKUP_SERVER
, AF_UNSPEC
, SOCK_STREAM
, &res
) != 0)
214 if ((s
= BIO_socket(BIO_ADDRINFO_family(res
), BIO_ADDRINFO_socktype(res
),
215 BIO_ADDRINFO_protocol(res
), 0)) == INVALID_SOCKET
) {
220 if (!BIO_listen(s
, BIO_ADDRINFO_address(res
),
221 bind_mode
? BIO_SOCK_REUSEADDR
: 0)) {
227 BIO_ADDRINFO_free(res
);
234 int BIO_accept(int sock
, char **ip_port
)
239 ret
= BIO_accept_ex(sock
, &res
, 0);
240 if (ret
== (int)INVALID_SOCKET
) {
241 if (BIO_sock_should_retry(ret
)) {
245 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
247 BIOerr(BIO_F_BIO_ACCEPT
, BIO_R_ACCEPT_ERROR
);
251 if (ip_port
!= NULL
) {
252 char *host
= BIO_ADDR_hostname_string(&res
, 1);
253 char *port
= BIO_ADDR_service_string(&res
, 1);
254 if (host
!= NULL
&& port
!= NULL
)
255 *ip_port
= OPENSSL_zalloc(strlen(host
) + strlen(port
) + 2);
259 if (*ip_port
== NULL
) {
260 BIOerr(BIO_F_BIO_ACCEPT
, ERR_R_MALLOC_FAILURE
);
261 BIO_closesocket(ret
);
262 ret
= (int)INVALID_SOCKET
;
264 strcpy(*ip_port
, host
);
265 strcat(*ip_port
, ":");
266 strcat(*ip_port
, port
);
277 int BIO_set_tcp_ndelay(int s
, int on
)
280 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
291 ret
= setsockopt(s
, opt
, TCP_NODELAY
, (char *)&on
, sizeof(on
));
296 int BIO_socket_nbio(int s
, int mode
)
305 ret
= BIO_socket_ioctl(s
, FIONBIO
, &l
);
306 # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
307 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
309 l
= fcntl(s
, F_GETFL
, 0);
311 ERR_raise_data(ERR_LIB_SYS
, get_last_sys_error(),
315 # if defined(O_NONBLOCK)
318 l
&= ~FNDELAY
; /* BSD4.x */
321 # if defined(O_NONBLOCK)
324 l
|= FNDELAY
; /* BSD4.x */
327 ret
= fcntl(s
, F_SETFL
, l
);
330 ERR_raise_data(ERR_LIB_SYS
, get_last_sys_error(),
335 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
336 BIOerr(BIO_F_BIO_SOCKET_NBIO
, ERR_R_PASSED_INVALID_ARGUMENT
);
342 int BIO_sock_info(int sock
,
343 enum BIO_sock_info_type type
, union BIO_sock_info_u
*info
)
346 case BIO_SOCK_INFO_ADDRESS
:
350 addr_len
= sizeof(*info
->addr
);
351 ret
= getsockname(sock
, BIO_ADDR_sockaddr_noconst(info
->addr
),
354 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
355 "calling getsockname()");
356 BIOerr(BIO_F_BIO_SOCK_INFO
, BIO_R_GETSOCKNAME_ERROR
);
359 if ((size_t)addr_len
> sizeof(*info
->addr
)) {
360 BIOerr(BIO_F_BIO_SOCK_INFO
, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS
);
366 BIOerr(BIO_F_BIO_SOCK_INFO
, BIO_R_UNKNOWN_INFO_TYPE
);