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