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
);
46 } else if (BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res
), NULL
, &l
)) {
48 * Because only AF_INET addresses will reach this far, we can assert
51 if (ossl_assert(l
== 4))
52 ret
= BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res
), ip
, &l
);
54 BIO_ADDRINFO_free(res
);
56 ERR_add_error_data(2, "host=", str
);
62 int BIO_get_port(const char *str
, unsigned short *port_ptr
)
64 BIO_ADDRINFO
*res
= NULL
;
68 BIOerr(BIO_F_BIO_GET_PORT
, BIO_R_NO_PORT_DEFINED
);
72 if (BIO_sock_init() != 1)
73 return 0; /* don't generate another error code here */
75 if (BIO_lookup(NULL
, str
, BIO_LOOKUP_CLIENT
, AF_INET
, SOCK_STREAM
, &res
)) {
76 if (BIO_ADDRINFO_family(res
) != AF_INET
) {
77 BIOerr(BIO_F_BIO_GET_PORT
,
78 BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET
);
80 *port_ptr
= ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res
)));
83 BIO_ADDRINFO_free(res
);
85 ERR_add_error_data(2, "host=", str
);
92 int BIO_sock_error(int sock
)
95 socklen_t size
= sizeof(j
);
98 * Note: under Windows the third parameter is of type (char *) whereas
99 * under other systems it is (void *) if you don't have a cast it will
100 * choke the compiler: if you do have a cast then you can either go for
101 * (char *) or (void *).
103 i
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, (void *)&j
, &size
);
105 return get_last_socket_error();
110 # if OPENSSL_API_COMPAT < 0x10100000L
111 struct hostent
*BIO_gethostbyname(const char *name
)
114 * Caching gethostbyname() results forever is wrong, so we have to let
115 * the true gethostbyname() worry about this
117 # if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
118 return gethostbyname((char *)name
);
120 return gethostbyname(name
);
125 int BIO_sock_init(void)
127 # ifdef OPENSSL_SYS_WINDOWS
128 static struct WSAData wsa_state
;
130 if (!wsa_init_done
) {
134 memset(&wsa_state
, 0, sizeof(wsa_state
));
136 * Not making wsa_state available to the rest of the code is formally
137 * wrong. But the structures we use are [believed to be] invariable
138 * among Winsock DLLs, while API availability is [expected to be]
139 * probed at run-time with DSO_global_lookup.
141 if (WSAStartup(0x0202, &wsa_state
) != 0) {
142 err
= WSAGetLastError();
143 SYSerr(SYS_F_WSASTARTUP
, err
);
144 BIOerr(BIO_F_BIO_SOCK_INIT
, BIO_R_WSASTARTUP
);
148 # endif /* OPENSSL_SYS_WINDOWS */
150 extern int _watt_do_exit
;
151 _watt_do_exit
= 0; /* don't make sock_init() call exit() */
159 void bio_sock_cleanup_int(void)
161 # ifdef OPENSSL_SYS_WINDOWS
169 int BIO_socket_ioctl(int fd
, long type
, void *arg
)
174 i
= ioctlsocket(fd
, type
, (char *)arg
);
176 # if defined(OPENSSL_SYS_VMS)
179 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
180 * observe that all the consumers pass in an "unsigned long *",
181 * so we arrange a local copy with a short pointer, and use
184 # if __INITIAL_POINTER_SIZE == 64
186 # pragma pointer_size save
187 # pragma pointer_size 32
188 unsigned long arg_32
;
189 unsigned long *arg_32p
;
190 # pragma pointer_size restore
192 arg_32
= *((unsigned long *)arg
);
193 # else /* __INITIAL_POINTER_SIZE == 64 */
195 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
196 # else /* defined(OPENSSL_SYS_VMS) */
198 # endif /* defined(OPENSSL_SYS_VMS) [else] */
200 i
= ioctlsocket(fd
, type
, ARG
);
201 # endif /* __DJGPP__ */
203 SYSerr(SYS_F_IOCTLSOCKET
, get_last_socket_error());
207 # if OPENSSL_API_COMPAT < 0x10100000L
208 int BIO_get_accept_socket(char *host
, int bind_mode
)
210 int s
= INVALID_SOCKET
;
211 char *h
= NULL
, *p
= NULL
;
212 BIO_ADDRINFO
*res
= NULL
;
214 if (!BIO_parse_hostserv(host
, &h
, &p
, BIO_PARSE_PRIO_SERV
))
215 return INVALID_SOCKET
;
217 if (BIO_sock_init() != 1)
218 return INVALID_SOCKET
;
220 if (BIO_lookup(h
, p
, BIO_LOOKUP_SERVER
, AF_UNSPEC
, SOCK_STREAM
, &res
) != 0)
223 if ((s
= BIO_socket(BIO_ADDRINFO_family(res
), BIO_ADDRINFO_socktype(res
),
224 BIO_ADDRINFO_protocol(res
), 0)) == INVALID_SOCKET
) {
229 if (!BIO_listen(s
, BIO_ADDRINFO_address(res
),
230 bind_mode
? BIO_SOCK_REUSEADDR
: 0)) {
236 BIO_ADDRINFO_free(res
);
243 int BIO_accept(int sock
, char **ip_port
)
248 ret
= BIO_accept_ex(sock
, &res
, 0);
249 if (ret
== (int)INVALID_SOCKET
) {
250 if (BIO_sock_should_retry(ret
)) {
254 SYSerr(SYS_F_ACCEPT
, get_last_socket_error());
255 BIOerr(BIO_F_BIO_ACCEPT
, BIO_R_ACCEPT_ERROR
);
259 if (ip_port
!= NULL
) {
260 char *host
= BIO_ADDR_hostname_string(&res
, 1);
261 char *port
= BIO_ADDR_service_string(&res
, 1);
262 if (host
!= NULL
&& port
!= NULL
)
263 *ip_port
= OPENSSL_zalloc(strlen(host
) + strlen(port
) + 2);
267 if (*ip_port
== NULL
) {
268 BIOerr(BIO_F_BIO_ACCEPT
, ERR_R_MALLOC_FAILURE
);
269 BIO_closesocket(ret
);
270 ret
= (int)INVALID_SOCKET
;
272 strcpy(*ip_port
, host
);
273 strcat(*ip_port
, ":");
274 strcat(*ip_port
, port
);
285 int BIO_set_tcp_ndelay(int s
, int on
)
288 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
299 ret
= setsockopt(s
, opt
, TCP_NODELAY
, (char *)&on
, sizeof(on
));
304 int BIO_socket_nbio(int s
, int mode
)
313 ret
= BIO_socket_ioctl(s
, FIONBIO
, &l
);
314 # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
315 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
317 l
= fcntl(s
, F_GETFL
, 0);
319 SYSerr(SYS_F_FCNTL
, get_last_rtl_error());
322 # if defined(O_NONBLOCK)
325 l
&= ~FNDELAY
; /* BSD4.x */
328 # if defined(O_NONBLOCK)
331 l
|= FNDELAY
; /* BSD4.x */
334 ret
= fcntl(s
, F_SETFL
, l
);
337 SYSerr(SYS_F_FCNTL
, get_last_rtl_error());
341 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
342 BIOerr(BIO_F_BIO_SOCKET_NBIO
, ERR_R_PASSED_INVALID_ARGUMENT
);
348 int BIO_sock_info(int sock
,
349 enum BIO_sock_info_type type
, union BIO_sock_info_u
*info
)
352 case BIO_SOCK_INFO_ADDRESS
:
356 addr_len
= sizeof(*info
->addr
);
357 ret
= getsockname(sock
, BIO_ADDR_sockaddr_noconst(info
->addr
),
360 SYSerr(SYS_F_GETSOCKNAME
, get_last_socket_error());
361 BIOerr(BIO_F_BIO_SOCK_INFO
, BIO_R_GETSOCKNAME_ERROR
);
364 if ((size_t)addr_len
> sizeof(*info
->addr
)) {
365 BIOerr(BIO_F_BIO_SOCK_INFO
, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS
);
371 BIOerr(BIO_F_BIO_SOCK_INFO
, BIO_R_UNKNOWN_INFO_TYPE
);