]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/b_sock.c
Remove OPENSSL_assert() from crypto/asn1/bio_asn1.c
[thirdparty/openssl.git] / crypto / bio / b_sock.c
CommitLineData
b1322259
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
b1322259
RS
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
d02b48c6
RE
8 */
9
d02b48c6 10#include <stdio.h>
58964a49 11#include <stdlib.h>
d02b48c6 12#include <errno.h>
28a0841b
RL
13#include "bio_lcl.h"
14#if defined(NETWARE_CLIB)
15# include <sys/ioctl.h>
eef0c1f3
DSH
16NETDB_DEFINE_CONTEXT
17#endif
9ba4cc00 18#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
19# define SOCKET_PROTOCOL IPPROTO_TCP
20# ifdef SO_MAXCONN
21# define MAX_LISTEN SO_MAXCONN
22# elif defined(SOMAXCONN)
23# define MAX_LISTEN SOMAXCONN
24# else
25# define MAX_LISTEN 32
26# endif
1fbab1dc 27# if defined(OPENSSL_SYS_WINDOWS)
0f113f3e
MC
28static int wsa_init_done = 0;
29# endif
d02b48c6 30
d33b215b 31# if OPENSSL_API_COMPAT < 0x10100000L
6b691a5c 32int BIO_get_host_ip(const char *str, unsigned char *ip)
0f113f3e 33{
5bca70ca
RL
34 BIO_ADDRINFO *res = NULL;
35 int ret = 0;
0f113f3e 36
0f113f3e
MC
37 if (BIO_sock_init() != 1)
38 return 0; /* don't generate another error code here */
39
5bca70ca
RL
40 if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
41 size_t l;
0f113f3e 42
5bca70ca
RL
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 {
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);
0f113f3e 51
5bca70ca
RL
52 BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), ip, &l);
53 ret = 1;
54 }
55 BIO_ADDRINFO_free(res);
56 } else {
57 ERR_add_error_data(2, "host=", str);
0f113f3e 58 }
ba9f2808 59
5bca70ca 60 return ret;
0f113f3e 61}
d02b48c6 62
6b691a5c 63int BIO_get_port(const char *str, unsigned short *port_ptr)
0f113f3e 64{
5bca70ca
RL
65 BIO_ADDRINFO *res = NULL;
66 int ret = 0;
0f113f3e
MC
67
68 if (str == NULL) {
69 BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED);
70 return (0);
71 }
5bca70ca
RL
72
73 if (BIO_sock_init() != 1)
74 return 0; /* don't generate another error code here */
75
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);
80 } else {
81 *port_ptr = ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res)));
82 ret = 1;
0f113f3e 83 }
5bca70ca
RL
84 BIO_ADDRINFO_free(res);
85 } else {
86 ERR_add_error_data(2, "host=", str);
0f113f3e 87 }
5bca70ca
RL
88
89 return ret;
0f113f3e 90}
d33b215b 91# endif
d02b48c6 92
6b691a5c 93int BIO_sock_error(int sock)
0f113f3e 94{
5bca70ca 95 int j = 0, i;
2bd8c853 96 socklen_t size = sizeof(j);
5bca70ca 97
0f113f3e
MC
98 /*
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 *).
103 */
5bca70ca 104 i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size);
0f113f3e 105 if (i < 0)
2bd8c853 106 return (get_last_socket_error());
0f113f3e
MC
107 else
108 return (j);
109}
110
d33b215b 111# if OPENSSL_API_COMPAT < 0x10100000L
6b691a5c 112struct hostent *BIO_gethostbyname(const char *name)
0f113f3e 113{
0f113f3e
MC
114 /*
115 * Caching gethostbyname() results forever is wrong, so we have to let
116 * the true gethostbyname() worry about this
117 */
5bca70ca 118# if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
0f113f3e 119 return gethostbyname((char *)name);
5bca70ca 120# else
4d428cd2 121 return gethostbyname(name);
5bca70ca 122# endif
0f113f3e 123}
d33b215b 124# endif
c602e7f4 125
6b691a5c 126int BIO_sock_init(void)
0f113f3e
MC
127{
128# ifdef OPENSSL_SYS_WINDOWS
129 static struct WSAData wsa_state;
130
131 if (!wsa_init_done) {
132 int err;
133
134 wsa_init_done = 1;
135 memset(&wsa_state, 0, sizeof(wsa_state));
136 /*
137 * Not making wsa_state available to the rest of the code is formally
0d4fb843 138 * wrong. But the structures we use are [believed to be] invariable
0f113f3e
MC
139 * among Winsock DLLs, while API availability is [expected to be]
140 * probed at run-time with DSO_global_lookup.
141 */
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);
146 return (-1);
147 }
148 }
149# endif /* OPENSSL_SYS_WINDOWS */
150# ifdef WATT32
151 extern int _watt_do_exit;
152 _watt_do_exit = 0; /* don't make sock_init() call exit() */
153 if (sock_init())
154 return (-1);
155# endif
156
0f113f3e
MC
157 return (1);
158}
d02b48c6 159
b3599dbb 160void bio_sock_cleanup_int(void)
0f113f3e
MC
161{
162# ifdef OPENSSL_SYS_WINDOWS
163 if (wsa_init_done) {
164 wsa_init_done = 0;
4d8743f4 165 WSACleanup();
0f113f3e 166 }
0f113f3e
MC
167# endif
168}
d02b48c6 169
c029841e 170int BIO_socket_ioctl(int fd, long type, void *arg)
0f113f3e
MC
171{
172 int i;
173
174# ifdef __DJGPP__
175 i = ioctlsocket(fd, type, (char *)arg);
176# else
177# if defined(OPENSSL_SYS_VMS)
35a1cc90
MC
178 /*-
179 * 2011-02-18 SMS.
180 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
181 * observe that all the consumers pass in an "unsigned long *",
182 * so we arrange a local copy with a short pointer, and use
183 * that, instead.
184 */
0f113f3e
MC
185# if __INITIAL_POINTER_SIZE == 64
186# define ARG arg_32p
187# pragma pointer_size save
188# pragma pointer_size 32
189 unsigned long arg_32;
190 unsigned long *arg_32p;
191# pragma pointer_size restore
192 arg_32p = &arg_32;
193 arg_32 = *((unsigned long *)arg);
194# else /* __INITIAL_POINTER_SIZE == 64 */
195# define ARG arg
196# endif /* __INITIAL_POINTER_SIZE == 64 [else] */
197# else /* defined(OPENSSL_SYS_VMS) */
198# define ARG arg
199# endif /* defined(OPENSSL_SYS_VMS) [else] */
200
201 i = ioctlsocket(fd, type, ARG);
202# endif /* __DJGPP__ */
203 if (i < 0)
204 SYSerr(SYS_F_IOCTLSOCKET, get_last_socket_error());
205 return (i);
206}
0f113f3e 207
d33b215b 208# if OPENSSL_API_COMPAT < 0x10100000L
6b691a5c 209int BIO_get_accept_socket(char *host, int bind_mode)
0f113f3e 210{
5bca70ca
RL
211 int s = INVALID_SOCKET;
212 char *h = NULL, *p = NULL;
213 BIO_ADDRINFO *res = NULL;
0f113f3e 214
5bca70ca
RL
215 if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
216 return INVALID_SOCKET;
0f113f3e 217
5bca70ca
RL
218 if (BIO_sock_init() != 1)
219 return INVALID_SOCKET;
0f113f3e 220
5bca70ca 221 if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
0f113f3e
MC
222 goto err;
223
5bca70ca
RL
224 if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
225 BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) {
226 s = INVALID_SOCKET;
0f113f3e
MC
227 goto err;
228 }
0f113f3e 229
5bca70ca
RL
230 if (!BIO_listen(s, BIO_ADDRINFO_address(res),
231 bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
232 BIO_closesocket(s);
233 s = INVALID_SOCKET;
0f113f3e 234 }
5bca70ca 235
0f113f3e 236 err:
5bca70ca
RL
237 BIO_ADDRINFO_free(res);
238 OPENSSL_free(h);
239 OPENSSL_free(p);
240
241 return s;
0f113f3e 242}
d02b48c6 243
5bca70ca 244int BIO_accept(int sock, char **ip_port)
0f113f3e 245{
d9d8e7a9 246 BIO_ADDR res;
5bca70ca 247 int ret = -1;
0f113f3e 248
d9d8e7a9 249 ret = BIO_accept_ex(sock, &res, 0);
b13fdc48 250 if (ret == (int)INVALID_SOCKET) {
5bca70ca
RL
251 if (BIO_sock_should_retry(ret)) {
252 ret = -2;
253 goto end;
254 }
0f113f3e
MC
255 SYSerr(SYS_F_ACCEPT, get_last_socket_error());
256 BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR);
257 goto end;
258 }
259
5bca70ca 260 if (ip_port != NULL) {
d9d8e7a9
RS
261 char *host = BIO_ADDR_hostname_string(&res, 1);
262 char *port = BIO_ADDR_service_string(&res, 1);
ad9a0562
MC
263 if (host != NULL && port != NULL)
264 *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2);
265 else
266 *ip_port = NULL;
267
268 if (*ip_port == NULL) {
269 BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE);
270 BIO_closesocket(ret);
271 ret = (int)INVALID_SOCKET;
272 } else {
273 strcpy(*ip_port, host);
274 strcat(*ip_port, ":");
275 strcat(*ip_port, port);
276 }
5bca70ca
RL
277 OPENSSL_free(host);
278 OPENSSL_free(port);
0f113f3e 279 }
5bca70ca 280
0f113f3e 281 end:
5bca70ca 282 return ret;
0f113f3e 283}
d33b215b 284# endif
d02b48c6 285
6b691a5c 286int BIO_set_tcp_ndelay(int s, int on)
0f113f3e
MC
287{
288 int ret = 0;
289# if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
290 int opt;
d02b48c6 291
0f113f3e
MC
292# ifdef SOL_TCP
293 opt = SOL_TCP;
294# else
295# ifdef IPPROTO_TCP
296 opt = IPPROTO_TCP;
297# endif
298# endif
dfeab068 299
0f113f3e
MC
300 ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
301# endif
302 return (ret == 0);
303}
304
305int BIO_socket_nbio(int s, int mode)
306{
307 int ret = -1;
308 int l;
309
310 l = mode;
311# ifdef FIONBIO
27f172d9
RS
312 l = mode;
313
0f113f3e 314 ret = BIO_socket_ioctl(s, FIONBIO, &l);
27f172d9
RS
315# elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
316 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
317
318 l = fcntl(s, F_GETFL, 0);
319 if (l == -1) {
320 SYSerr(SYS_F_FCNTL, get_last_rtl_error());
321 ret = -1;
322 } else {
323# if defined(O_NONBLOCK)
324 l &= ~O_NONBLOCK;
325# else
326 l &= ~FNDELAY; /* BSD4.x */
327# endif
328 if (mode) {
329# if defined(O_NONBLOCK)
330 l |= O_NONBLOCK;
331# else
332 l |= FNDELAY; /* BSD4.x */
333# endif
334 }
335 ret = fcntl(s, F_SETFL, l);
336
337 if (ret < 0) {
338 SYSerr(SYS_F_FCNTL, get_last_rtl_error());
339 }
340 }
341# else
342 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
343 BIOerr(BIO_F_BIO_SOCKET_NBIO, ERR_R_PASSED_INVALID_ARGUMENT);
0f113f3e 344# endif
27f172d9 345
0f113f3e
MC
346 return (ret == 0);
347}
d33b215b
RL
348
349int BIO_sock_info(int sock,
350 enum BIO_sock_info_type type, union BIO_sock_info_u *info)
351{
352 switch (type) {
353 case BIO_SOCK_INFO_ADDRESS:
354 {
355 socklen_t addr_len;
356 int ret = 0;
357 addr_len = sizeof(*info->addr);
358 ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr),
359 &addr_len);
360 if (ret == -1) {
361 SYSerr(SYS_F_GETSOCKNAME, get_last_socket_error());
362 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_ERROR);
363 return 0;
364 }
59d9bb59 365 if ((size_t)addr_len > sizeof(*info->addr)) {
d33b215b
RL
366 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS);
367 return 0;
368 }
369 }
370 break;
371 default:
372 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_UNKNOWN_INFO_TYPE);
373 return 0;
374 }
375 return 1;
376}
377
4a1fbd13 378#endif