]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/b_sock.c
Make BIO_sock_error return a proper error code when getsockopt fails
[thirdparty/openssl.git] / crypto / bio / b_sock.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <errno.h>
61 #include "bio_lcl.h"
62 #if defined(NETWARE_CLIB)
63 # include <sys/ioctl.h>
64 NETDB_DEFINE_CONTEXT
65 #endif
66 #ifndef OPENSSL_NO_SOCK
67 # define SOCKET_PROTOCOL IPPROTO_TCP
68 # ifdef SO_MAXCONN
69 # define MAX_LISTEN SO_MAXCONN
70 # elif defined(SOMAXCONN)
71 # define MAX_LISTEN SOMAXCONN
72 # else
73 # define MAX_LISTEN 32
74 # endif
75 # if defined(OPENSSL_SYS_WINDOWS)
76 static int wsa_init_done = 0;
77 # endif
78
79 # if OPENSSL_API_COMPAT < 0x10100000L
80 int BIO_get_host_ip(const char *str, unsigned char *ip)
81 {
82 BIO_ADDRINFO *res = NULL;
83 int ret = 0;
84
85 if (BIO_sock_init() != 1)
86 return 0; /* don't generate another error code here */
87
88 if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
89 size_t l;
90
91 if (BIO_ADDRINFO_family(res) != AF_INET) {
92 BIOerr(BIO_F_BIO_GET_HOST_IP,
93 BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
94 } else {
95 BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), NULL, &l);
96 /* Because only AF_INET addresses will reach this far,
97 we can assert that l should be 4 */
98 OPENSSL_assert(l == 4);
99
100 BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), ip, &l);
101 ret = 1;
102 }
103 BIO_ADDRINFO_free(res);
104 } else {
105 ERR_add_error_data(2, "host=", str);
106 }
107
108 return ret;
109 }
110
111 int BIO_get_port(const char *str, unsigned short *port_ptr)
112 {
113 BIO_ADDRINFO *res = NULL;
114 int ret = 0;
115
116 if (str == NULL) {
117 BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED);
118 return (0);
119 }
120
121 if (BIO_sock_init() != 1)
122 return 0; /* don't generate another error code here */
123
124 if (BIO_lookup(NULL, str, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
125 if (BIO_ADDRINFO_family(res) != AF_INET) {
126 BIOerr(BIO_F_BIO_GET_PORT,
127 BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET);
128 } else {
129 *port_ptr = ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res)));
130 ret = 1;
131 }
132 BIO_ADDRINFO_free(res);
133 } else {
134 ERR_add_error_data(2, "host=", str);
135 }
136
137 return ret;
138 }
139 # endif
140
141 int BIO_sock_error(int sock)
142 {
143 int j = 0, i;
144 socklen_t size = sizeof(j);
145
146 /*
147 * Note: under Windows the third parameter is of type (char *) whereas
148 * under other systems it is (void *) if you don't have a cast it will
149 * choke the compiler: if you do have a cast then you can either go for
150 * (char *) or (void *).
151 */
152 i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size);
153 if (i < 0)
154 return (get_last_socket_error());
155 else
156 return (j);
157 }
158
159 # if OPENSSL_API_COMPAT < 0x10100000L
160 struct hostent *BIO_gethostbyname(const char *name)
161 {
162 /*
163 * Caching gethostbyname() results forever is wrong, so we have to let
164 * the true gethostbyname() worry about this
165 */
166 # if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__))
167 return gethostbyname((char *)name);
168 # else
169 return gethostbyname(name);
170 # endif
171 }
172 # endif
173
174 int BIO_sock_init(void)
175 {
176 # ifdef OPENSSL_SYS_WINDOWS
177 static struct WSAData wsa_state;
178
179 if (!wsa_init_done) {
180 int err;
181
182 wsa_init_done = 1;
183 memset(&wsa_state, 0, sizeof(wsa_state));
184 /*
185 * Not making wsa_state available to the rest of the code is formally
186 * wrong. But the structures we use are [believed to be] invariable
187 * among Winsock DLLs, while API availability is [expected to be]
188 * probed at run-time with DSO_global_lookup.
189 */
190 if (WSAStartup(0x0202, &wsa_state) != 0) {
191 err = WSAGetLastError();
192 SYSerr(SYS_F_WSASTARTUP, err);
193 BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP);
194 return (-1);
195 }
196 }
197 # endif /* OPENSSL_SYS_WINDOWS */
198 # ifdef WATT32
199 extern int _watt_do_exit;
200 _watt_do_exit = 0; /* don't make sock_init() call exit() */
201 if (sock_init())
202 return (-1);
203 # endif
204
205 return (1);
206 }
207
208 void bio_sock_cleanup_int(void)
209 {
210 # ifdef OPENSSL_SYS_WINDOWS
211 if (wsa_init_done) {
212 wsa_init_done = 0;
213 WSACleanup();
214 }
215 # endif
216 }
217
218 # if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000
219
220 int BIO_socket_ioctl(int fd, long type, void *arg)
221 {
222 int i;
223
224 # ifdef __DJGPP__
225 i = ioctlsocket(fd, type, (char *)arg);
226 # else
227 # if defined(OPENSSL_SYS_VMS)
228 /*-
229 * 2011-02-18 SMS.
230 * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
231 * observe that all the consumers pass in an "unsigned long *",
232 * so we arrange a local copy with a short pointer, and use
233 * that, instead.
234 */
235 # if __INITIAL_POINTER_SIZE == 64
236 # define ARG arg_32p
237 # pragma pointer_size save
238 # pragma pointer_size 32
239 unsigned long arg_32;
240 unsigned long *arg_32p;
241 # pragma pointer_size restore
242 arg_32p = &arg_32;
243 arg_32 = *((unsigned long *)arg);
244 # else /* __INITIAL_POINTER_SIZE == 64 */
245 # define ARG arg
246 # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
247 # else /* defined(OPENSSL_SYS_VMS) */
248 # define ARG arg
249 # endif /* defined(OPENSSL_SYS_VMS) [else] */
250
251 i = ioctlsocket(fd, type, ARG);
252 # endif /* __DJGPP__ */
253 if (i < 0)
254 SYSerr(SYS_F_IOCTLSOCKET, get_last_socket_error());
255 return (i);
256 }
257 # endif /* __VMS_VER */
258
259 # if OPENSSL_API_COMPAT < 0x10100000L
260 int BIO_get_accept_socket(char *host, int bind_mode)
261 {
262 int s = INVALID_SOCKET;
263 char *h = NULL, *p = NULL;
264 BIO_ADDRINFO *res = NULL;
265
266 if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
267 return INVALID_SOCKET;
268
269 if (BIO_sock_init() != 1)
270 return INVALID_SOCKET;
271
272 if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
273 goto err;
274
275 if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
276 BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) {
277 s = INVALID_SOCKET;
278 goto err;
279 }
280
281 if (!BIO_listen(s, BIO_ADDRINFO_address(res),
282 bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
283 BIO_closesocket(s);
284 s = INVALID_SOCKET;
285 }
286
287 err:
288 BIO_ADDRINFO_free(res);
289 OPENSSL_free(h);
290 OPENSSL_free(p);
291
292 return s;
293 }
294
295 int BIO_accept(int sock, char **ip_port)
296 {
297 BIO_ADDR res;
298 int ret = -1;
299
300 ret = BIO_accept_ex(sock, &res, 0);
301 if (ret == (int)INVALID_SOCKET) {
302 if (BIO_sock_should_retry(ret)) {
303 ret = -2;
304 goto end;
305 }
306 SYSerr(SYS_F_ACCEPT, get_last_socket_error());
307 BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR);
308 goto end;
309 }
310
311 if (ip_port != NULL) {
312 char *host = BIO_ADDR_hostname_string(&res, 1);
313 char *port = BIO_ADDR_service_string(&res, 1);
314 *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2);
315 strcpy(*ip_port, host);
316 strcat(*ip_port, ":");
317 strcat(*ip_port, port);
318 OPENSSL_free(host);
319 OPENSSL_free(port);
320 }
321
322 end:
323 return ret;
324 }
325 # endif
326
327 int BIO_set_tcp_ndelay(int s, int on)
328 {
329 int ret = 0;
330 # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
331 int opt;
332
333 # ifdef SOL_TCP
334 opt = SOL_TCP;
335 # else
336 # ifdef IPPROTO_TCP
337 opt = IPPROTO_TCP;
338 # endif
339 # endif
340
341 ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
342 # endif
343 return (ret == 0);
344 }
345
346 int BIO_socket_nbio(int s, int mode)
347 {
348 int ret = -1;
349 int l;
350
351 l = mode;
352 # ifdef FIONBIO
353 l = mode;
354
355 ret = BIO_socket_ioctl(s, FIONBIO, &l);
356 # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
357 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
358
359 l = fcntl(s, F_GETFL, 0);
360 if (l == -1) {
361 SYSerr(SYS_F_FCNTL, get_last_rtl_error());
362 ret = -1;
363 } else {
364 # if defined(O_NONBLOCK)
365 l &= ~O_NONBLOCK;
366 # else
367 l &= ~FNDELAY; /* BSD4.x */
368 # endif
369 if (mode) {
370 # if defined(O_NONBLOCK)
371 l |= O_NONBLOCK;
372 # else
373 l |= FNDELAY; /* BSD4.x */
374 # endif
375 }
376 ret = fcntl(s, F_SETFL, l);
377
378 if (ret < 0) {
379 SYSerr(SYS_F_FCNTL, get_last_rtl_error());
380 }
381 }
382 # else
383 /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
384 BIOerr(BIO_F_BIO_SOCKET_NBIO, ERR_R_PASSED_INVALID_ARGUMENT);
385 # endif
386
387 return (ret == 0);
388 }
389
390 int BIO_sock_info(int sock,
391 enum BIO_sock_info_type type, union BIO_sock_info_u *info)
392 {
393 switch (type) {
394 case BIO_SOCK_INFO_ADDRESS:
395 {
396 socklen_t addr_len;
397 int ret = 0;
398 addr_len = sizeof(*info->addr);
399 ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr),
400 &addr_len);
401 if (ret == -1) {
402 SYSerr(SYS_F_GETSOCKNAME, get_last_socket_error());
403 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_ERROR);
404 return 0;
405 }
406 if ((size_t)addr_len > sizeof(*info->addr)) {
407 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS);
408 return 0;
409 }
410 }
411 break;
412 default:
413 BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_UNKNOWN_INFO_TYPE);
414 return 0;
415 }
416 return 1;
417 }
418
419 #endif