Handle find-doc-nits script rename
[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 {
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);
51
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);
58     }
59
60     return ret;
61 }
62
63 int BIO_get_port(const char *str, unsigned short *port_ptr)
64 {
65     BIO_ADDRINFO *res = NULL;
66     int ret = 0;
67
68     if (str == NULL) {
69         BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED);
70         return (0);
71     }
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;
83         }
84         BIO_ADDRINFO_free(res);
85     } else {
86         ERR_add_error_data(2, "host=", str);
87     }
88
89     return ret;
90 }
91 # endif
92
93 int BIO_sock_error(int sock)
94 {
95     int j = 0, i;
96     socklen_t size = sizeof(j);
97
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      */
104     i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size);
105     if (i < 0)
106         return (get_last_socket_error());
107     else
108         return (j);
109 }
110
111 # if OPENSSL_API_COMPAT < 0x10100000L
112 struct hostent *BIO_gethostbyname(const char *name)
113 {
114     /*
115      * Caching gethostbyname() results forever is wrong, so we have to let
116      * the true gethostbyname() worry about this
117      */
118 #  if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
119     return gethostbyname((char *)name);
120 #  else
121     return gethostbyname(name);
122 #  endif
123 }
124 # endif
125
126 int BIO_sock_init(void)
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
138          * wrong. But the structures we use are [believed to be] invariable
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
157     return (1);
158 }
159
160 void bio_sock_cleanup_int(void)
161 {
162 # ifdef OPENSSL_SYS_WINDOWS
163     if (wsa_init_done) {
164         wsa_init_done = 0;
165         WSACleanup();
166     }
167 # endif
168 }
169
170 # if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000
171
172 int BIO_socket_ioctl(int fd, long type, void *arg)
173 {
174     int i;
175
176 #  ifdef __DJGPP__
177     i = ioctlsocket(fd, type, (char *)arg);
178 #  else
179 #   if defined(OPENSSL_SYS_VMS)
180     /*-
181      * 2011-02-18 SMS.
182      * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
183      * observe that all the consumers pass in an "unsigned long *",
184      * so we arrange a local copy with a short pointer, and use
185      * that, instead.
186      */
187 #    if __INITIAL_POINTER_SIZE == 64
188 #     define ARG arg_32p
189 #     pragma pointer_size save
190 #     pragma pointer_size 32
191     unsigned long arg_32;
192     unsigned long *arg_32p;
193 #     pragma pointer_size restore
194     arg_32p = &arg_32;
195     arg_32 = *((unsigned long *)arg);
196 #    else                       /* __INITIAL_POINTER_SIZE == 64 */
197 #     define ARG arg
198 #    endif                      /* __INITIAL_POINTER_SIZE == 64 [else] */
199 #   else                        /* defined(OPENSSL_SYS_VMS) */
200 #    define ARG arg
201 #   endif                       /* defined(OPENSSL_SYS_VMS) [else] */
202
203     i = ioctlsocket(fd, type, ARG);
204 #  endif                        /* __DJGPP__ */
205     if (i < 0)
206         SYSerr(SYS_F_IOCTLSOCKET, get_last_socket_error());
207     return (i);
208 }
209 # endif                         /* __VMS_VER */
210
211 # if OPENSSL_API_COMPAT < 0x10100000L
212 int BIO_get_accept_socket(char *host, int bind_mode)
213 {
214     int s = INVALID_SOCKET;
215     char *h = NULL, *p = NULL;
216     BIO_ADDRINFO *res = NULL;
217
218     if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
219         return INVALID_SOCKET;
220
221     if (BIO_sock_init() != 1)
222         return INVALID_SOCKET;
223
224     if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
225         goto err;
226
227     if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
228                         BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) {
229         s = INVALID_SOCKET;
230         goto err;
231     }
232
233     if (!BIO_listen(s, BIO_ADDRINFO_address(res),
234                     bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
235         BIO_closesocket(s);
236         s = INVALID_SOCKET;
237     }
238
239  err:
240     BIO_ADDRINFO_free(res);
241     OPENSSL_free(h);
242     OPENSSL_free(p);
243
244     return s;
245 }
246
247 int BIO_accept(int sock, char **ip_port)
248 {
249     BIO_ADDR res;
250     int ret = -1;
251
252     ret = BIO_accept_ex(sock, &res, 0);
253     if (ret == (int)INVALID_SOCKET) {
254         if (BIO_sock_should_retry(ret)) {
255             ret = -2;
256             goto end;
257         }
258         SYSerr(SYS_F_ACCEPT, get_last_socket_error());
259         BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR);
260         goto end;
261     }
262
263     if (ip_port != NULL) {
264         char *host = BIO_ADDR_hostname_string(&res, 1);
265         char *port = BIO_ADDR_service_string(&res, 1);
266         if (host != NULL && port != NULL)
267             *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2);
268         else
269             *ip_port = NULL;
270
271         if (*ip_port == NULL) {
272             BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE);
273             BIO_closesocket(ret);
274             ret = (int)INVALID_SOCKET;
275         } else {
276             strcpy(*ip_port, host);
277             strcat(*ip_port, ":");
278             strcat(*ip_port, port);
279         }
280         OPENSSL_free(host);
281         OPENSSL_free(port);
282     }
283
284  end:
285     return ret;
286 }
287 # endif
288
289 int BIO_set_tcp_ndelay(int s, int on)
290 {
291     int ret = 0;
292 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
293     int opt;
294
295 #  ifdef SOL_TCP
296     opt = SOL_TCP;
297 #  else
298 #   ifdef IPPROTO_TCP
299     opt = IPPROTO_TCP;
300 #   endif
301 #  endif
302
303     ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
304 # endif
305     return (ret == 0);
306 }
307
308 int BIO_socket_nbio(int s, int mode)
309 {
310     int ret = -1;
311     int l;
312
313     l = mode;
314 # ifdef FIONBIO
315     l = mode;
316
317     ret = BIO_socket_ioctl(s, FIONBIO, &l);
318 # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
319     /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
320
321     l = fcntl(s, F_GETFL, 0);
322     if (l == -1) {
323         SYSerr(SYS_F_FCNTL, get_last_rtl_error());
324         ret = -1;
325     } else {
326 #  if defined(O_NONBLOCK)
327         l &= ~O_NONBLOCK;
328 #  else
329         l &= ~FNDELAY; /* BSD4.x */
330 #  endif
331         if (mode) {
332 #  if defined(O_NONBLOCK)
333             l |= O_NONBLOCK;
334 #  else
335             l |= FNDELAY; /* BSD4.x */
336 #  endif
337         }
338         ret = fcntl(s, F_SETFL, l);
339
340         if (ret < 0) {
341             SYSerr(SYS_F_FCNTL, get_last_rtl_error());
342         }
343     }
344 # else
345     /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
346     BIOerr(BIO_F_BIO_SOCKET_NBIO, ERR_R_PASSED_INVALID_ARGUMENT);
347 # endif
348
349     return (ret == 0);
350 }
351
352 int BIO_sock_info(int sock,
353                   enum BIO_sock_info_type type, union BIO_sock_info_u *info)
354 {
355     switch (type) {
356     case BIO_SOCK_INFO_ADDRESS:
357         {
358             socklen_t addr_len;
359             int ret = 0;
360             addr_len = sizeof(*info->addr);
361             ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr),
362                               &addr_len);
363             if (ret == -1) {
364                 SYSerr(SYS_F_GETSOCKNAME, get_last_socket_error());
365                 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_ERROR);
366                 return 0;
367             }
368             if ((size_t)addr_len > sizeof(*info->addr)) {
369                 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS);
370                 return 0;
371             }
372         }
373         break;
374     default:
375         BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_UNKNOWN_INFO_TYPE);
376         return 0;
377     }
378     return 1;
379 }
380
381 #endif