]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/b_sock.c
Fix invalid function type casts.
[thirdparty/openssl.git] / crypto / bio / b_sock.c
1 /*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include "bio_lcl.h"
14 #if defined(NETWARE_CLIB)
15 # include <sys/ioctl.h>
16 NETDB_DEFINE_CONTEXT
17 #endif
18 #ifndef OPENSSL_NO_SOCK
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
27 # if defined(OPENSSL_SYS_WINDOWS)
28 static int wsa_init_done = 0;
29 # endif
30
31 # if OPENSSL_API_COMPAT < 0x10100000L
32 int BIO_get_host_ip(const char *str, unsigned char *ip)
33 {
34 BIO_ADDRINFO *res = NULL;
35 int ret = 0;
36
37 if (BIO_sock_init() != 1)
38 return 0; /* don't generate another error code here */
39
40 if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
41 size_t l;
42
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)) {
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);
53 }
54 BIO_ADDRINFO_free(res);
55 } else {
56 ERR_add_error_data(2, "host=", str);
57 }
58
59 return ret;
60 }
61
62 int BIO_get_port(const char *str, unsigned short *port_ptr)
63 {
64 BIO_ADDRINFO *res = NULL;
65 int ret = 0;
66
67 if (str == NULL) {
68 BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED);
69 return 0;
70 }
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;
82 }
83 BIO_ADDRINFO_free(res);
84 } else {
85 ERR_add_error_data(2, "host=", str);
86 }
87
88 return ret;
89 }
90 # endif
91
92 int BIO_sock_error(int sock)
93 {
94 int j = 0, i;
95 socklen_t size = sizeof(j);
96
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 */
103 i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size);
104 if (i < 0)
105 return get_last_socket_error();
106 else
107 return j;
108 }
109
110 # if OPENSSL_API_COMPAT < 0x10100000L
111 struct hostent *BIO_gethostbyname(const char *name)
112 {
113 /*
114 * Caching gethostbyname() results forever is wrong, so we have to let
115 * the true gethostbyname() worry about this
116 */
117 # if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
118 return gethostbyname((char *)name);
119 # else
120 return gethostbyname(name);
121 # endif
122 }
123 # endif
124
125 int BIO_sock_init(void)
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
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.
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);
145 return -1;
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())
153 return -1;
154 # endif
155
156 return 1;
157 }
158
159 void bio_sock_cleanup_int(void)
160 {
161 # ifdef OPENSSL_SYS_WINDOWS
162 if (wsa_init_done) {
163 wsa_init_done = 0;
164 WSACleanup();
165 }
166 # endif
167 }
168
169 int BIO_socket_ioctl(int fd, long type, void *arg)
170 {
171 int i;
172
173 # ifdef __DJGPP__
174 i = ioctlsocket(fd, type, (char *)arg);
175 # else
176 # if defined(OPENSSL_SYS_VMS)
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 */
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());
204 return i;
205 }
206
207 # if OPENSSL_API_COMPAT < 0x10100000L
208 int BIO_get_accept_socket(char *host, int bind_mode)
209 {
210 int s = INVALID_SOCKET;
211 char *h = NULL, *p = NULL;
212 BIO_ADDRINFO *res = NULL;
213
214 if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
215 return INVALID_SOCKET;
216
217 if (BIO_sock_init() != 1)
218 return INVALID_SOCKET;
219
220 if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
221 goto err;
222
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;
226 goto err;
227 }
228
229 if (!BIO_listen(s, BIO_ADDRINFO_address(res),
230 bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
231 BIO_closesocket(s);
232 s = INVALID_SOCKET;
233 }
234
235 err:
236 BIO_ADDRINFO_free(res);
237 OPENSSL_free(h);
238 OPENSSL_free(p);
239
240 return s;
241 }
242
243 int BIO_accept(int sock, char **ip_port)
244 {
245 BIO_ADDR res;
246 int ret = -1;
247
248 ret = BIO_accept_ex(sock, &res, 0);
249 if (ret == (int)INVALID_SOCKET) {
250 if (BIO_sock_should_retry(ret)) {
251 ret = -2;
252 goto end;
253 }
254 SYSerr(SYS_F_ACCEPT, get_last_socket_error());
255 BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR);
256 goto end;
257 }
258
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);
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 }
276 OPENSSL_free(host);
277 OPENSSL_free(port);
278 }
279
280 end:
281 return ret;
282 }
283 # endif
284
285 int BIO_set_tcp_ndelay(int s, int on)
286 {
287 int ret = 0;
288 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
289 int opt;
290
291 # ifdef SOL_TCP
292 opt = SOL_TCP;
293 # else
294 # ifdef IPPROTO_TCP
295 opt = IPPROTO_TCP;
296 # endif
297 # endif
298
299 ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
300 # endif
301 return (ret == 0);
302 }
303
304 int BIO_socket_nbio(int s, int mode)
305 {
306 int ret = -1;
307 int l;
308
309 l = mode;
310 # ifdef FIONBIO
311 l = mode;
312
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. */
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);
343 # endif
344
345 return (ret == 0);
346 }
347
348 int 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 }
364 if ((size_t)addr_len > sizeof(*info->addr)) {
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
377 #endif