1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
57 /* ====================================================================
58 * Copyright (c) 199-2015 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * licensing@OpenSSL.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
106 /* socket-related functions used by s_client and s_server */
114 * With IPv6, it looks like Digital has mixed up the proper order of
115 * recursive header file inclusion, resulting in the compiler complaining
116 * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
117 * needed to have fileno() declared correctly... So let's define u_int
119 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
121 typedef unsigned int u_int;
128 #include <openssl/ssl.h>
133 # include "../e_os.h"
136 #ifndef OPENSSL_NO_SOCK
138 # if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
142 # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
143 static void ssl_sock_cleanup(void);
145 static int ssl_sock_init(void);
146 static int init_client_ip(int *sock, const unsigned char ip[4], int port,
148 static int init_server(int *sock, int port, int type);
149 static int init_server_long(int *sock, int port, char *ip, int type);
150 static int do_accept(int acc_sock, int *sock, char **host);
151 static int host_ip(const char *str, unsigned char ip[4]);
153 static int init_server_unix(int *sock, const char *path);
154 static int do_accept_unix(int acc_sock, int *sock);
157 # if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
158 static int wsa_init_done = 0;
161 # ifdef OPENSSL_SYS_WINDOWS
162 static struct WSAData wsa_state;
163 static int wsa_init_done = 0;
165 # endif /* OPENSSL_SYS_WINDOWS */
167 # ifdef OPENSSL_SYS_WINDOWS
168 static void ssl_sock_cleanup(void)
172 # ifndef OPENSSL_SYS_WINCE
173 WSACancelBlockingCall();
178 # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
179 static void sock_cleanup(void)
188 static int ssl_sock_init(void)
191 extern int _watt_do_exit;
195 # elif defined(OPENSSL_SYS_WINDOWS)
196 if (!wsa_init_done) {
200 signal(SIGINT, (void (*)(int))ssl_sock_cleanup);
203 memset(&wsa_state, 0, sizeof(wsa_state));
204 if (WSAStartup(0x0101, &wsa_state) != 0) {
205 err = WSAGetLastError();
206 BIO_printf(bio_err, "unable to start WINSOCK, error code=%d\n",
211 # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
216 if (!wsa_init_done) {
219 signal(SIGINT, (void (*)(int))sock_cleanup);
223 wVerReq = MAKEWORD(2, 0);
224 err = WSAStartup(wVerReq, &wsaData);
226 BIO_printf(bio_err, "unable to start WINSOCK2, error code=%d\n",
235 int init_client(int *sock, const char *host, int port, int type)
239 ip[0] = ip[1] = ip[2] = ip[3] = 0;
240 if (!host_ip(host, &(ip[0])))
242 return init_client_ip(sock, ip, port, type);
245 static int init_client_ip(int *sock, const unsigned char ip[4], int port,
249 struct sockaddr_in them;
252 if (!ssl_sock_init())
255 memset(&them, 0, sizeof(them));
256 them.sin_family = AF_INET;
257 them.sin_port = htons((unsigned short)port);
258 addr = (unsigned long)
259 ((unsigned long)ip[0] << 24L) |
260 ((unsigned long)ip[1] << 16L) |
261 ((unsigned long)ip[2] << 8L) | ((unsigned long)ip[3]);
262 them.sin_addr.s_addr = htonl(addr);
264 if (type == SOCK_STREAM)
265 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
266 else /* ( type == SOCK_DGRAM) */
267 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
269 if (s == INVALID_SOCKET) {
273 # if defined(SO_KEEPALIVE)
274 if (type == SOCK_STREAM) {
276 i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof(i));
285 if (connect(s, (struct sockaddr *)&them, sizeof(them)) == -1) {
295 int init_client_unix(int *sock, const char *server)
297 struct sockaddr_un them;
300 if (strlen(server) > (UNIX_PATH_MAX + 1))
302 if (!ssl_sock_init())
305 s = socket(AF_UNIX, SOCK_STREAM, 0);
306 if (s == INVALID_SOCKET) {
311 memset(&them, 0, sizeof(them));
312 them.sun_family = AF_UNIX;
313 strcpy(them.sun_path, server);
315 if (connect(s, (struct sockaddr *)&them, sizeof(them)) == -1) {
325 int do_server(int port, int type, int *ret,
326 int (*cb) (char *hostname, int s, int stype,
327 unsigned char *context), unsigned char *context,
332 int accept_socket = 0;
335 if (!init_server(&accept_socket, port, type))
339 *ret = accept_socket;
343 if (type == SOCK_STREAM) {
344 # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
345 if (do_accept(accept_socket, &sock, NULL) == 0)
347 if (do_accept(accept_socket, &sock, &name) == 0)
350 SHUTDOWN(accept_socket);
354 sock = accept_socket;
355 i = (*cb) (name, sock, type, context);
357 if (type == SOCK_STREAM)
361 if (i < 0 || naccept == 0) {
362 SHUTDOWN2(accept_socket);
369 int do_server_unix(const char *path, int *ret,
370 int (*cb) (char *hostname, int s, int stype,
371 unsigned char *context), unsigned char *context,
375 int accept_socket = 0;
378 if (!init_server_unix(&accept_socket, path))
382 *ret = accept_socket;
384 if (do_accept_unix(accept_socket, &sock) == 0) {
385 SHUTDOWN(accept_socket);
389 i = (*cb) (NULL, sock, 0, context);
393 if (i < 0 || naccept == 0) {
394 SHUTDOWN2(accept_socket);
404 static int init_server_long(int *sock, int port, char *ip, int type)
407 struct sockaddr_in server;
410 if (!ssl_sock_init())
413 memset(&server, 0, sizeof(server));
414 server.sin_family = AF_INET;
415 server.sin_port = htons((unsigned short)port);
417 server.sin_addr.s_addr = INADDR_ANY;
419 /* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
420 # ifndef BIT_FIELD_LIMITS
421 memcpy(&server.sin_addr.s_addr, ip, 4);
423 memcpy(&server.sin_addr, ip, 4);
426 if (type == SOCK_STREAM)
427 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
428 else /* type == SOCK_DGRAM */
429 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
431 if (s == INVALID_SOCKET)
433 # if defined SOL_SOCKET && defined SO_REUSEADDR
436 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&j, sizeof j);
439 if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) {
440 # ifndef OPENSSL_SYS_WINDOWS
445 /* Make it 128 for linux */
446 if (type == SOCK_STREAM && listen(s, 128) == -1)
451 if ((ret == 0) && (s != -1)) {
457 static int init_server(int *sock, int port, int type)
459 return (init_server_long(sock, port, NULL, type));
463 static int init_server_unix(int *sock, const char *path)
466 struct sockaddr_un server;
469 if (strlen(path) > (UNIX_PATH_MAX + 1))
471 if (!ssl_sock_init())
474 s = socket(AF_UNIX, SOCK_STREAM, 0);
475 if (s == INVALID_SOCKET)
478 memset(&server, 0, sizeof(server));
479 server.sun_family = AF_UNIX;
480 strcpy(server.sun_path, path);
482 if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) {
483 # ifndef OPENSSL_SYS_WINDOWS
488 /* Make it 128 for linux */
489 if (listen(s, 128) == -1) {
490 # ifndef OPENSSL_SYS_WINDOWS
499 if ((ret == 0) && (s != -1)) {
506 static int do_accept(int acc_sock, int *sock, char **host)
509 struct hostent *h1, *h2;
510 static struct sockaddr_in from;
512 /* struct linger ling; */
514 if (!ssl_sock_init())
517 # ifndef OPENSSL_SYS_WINDOWS
521 memset(&from, 0, sizeof(from));
524 * Note: under VMS with SOCKETSHR the fourth parameter is currently of
525 * type (int *) whereas under other systems it is (void *) if you don't
526 * have a cast it will choke the compiler: if you do have a cast then you
527 * can either go for (int *) or (void *).
529 ret = accept(acc_sock, (struct sockaddr *)&from, (void *)&len);
530 if (ret == INVALID_SOCKET) {
531 # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
533 i = WSAGetLastError();
534 BIO_printf(bio_err, "accept error %d\n", i);
536 if (errno == EINTR) {
542 fprintf(stderr, "errno=%d ", errno);
550 # ifndef BIT_FIELD_LIMITS
551 /* I should use WSAAsyncGetHostByName() under windows */
552 h1 = gethostbyaddr((char *)&from.sin_addr.s_addr,
553 sizeof(from.sin_addr.s_addr), AF_INET);
555 h1 = gethostbyaddr((char *)&from.sin_addr,
556 sizeof(struct in_addr), AF_INET);
559 BIO_printf(bio_err, "bad gethostbyaddr\n");
563 *host = app_malloc(strlen(h1->h_name) + 1, "copy hostname");
564 BUF_strlcpy(*host, h1->h_name, strlen(h1->h_name) + 1);
566 h2 = gethostbyname(*host);
568 BIO_printf(bio_err, "gethostbyname failure\n");
572 if (h2->h_addrtype != AF_INET) {
573 BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
584 static int do_accept_unix(int acc_sock, int *sock)
588 if (!ssl_sock_init())
592 ret = accept(acc_sock, NULL, NULL);
593 if (ret == INVALID_SOCKET) {
594 if (errno == EINTR) {
600 fprintf(stderr, "errno=%d ", errno);
610 int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
611 unsigned short *port_ptr)
616 p = strchr(str, ':');
618 BIO_printf(bio_err, "no port defined\n");
623 if ((ip != NULL) && !host_ip(str, ip))
625 if (host_ptr != NULL)
628 if (!extract_port(p, port_ptr))
635 static int host_ip(const char *str, unsigned char ip[4])
640 if (sscanf(str, "%u.%u.%u.%u", &(in[0]), &(in[1]), &(in[2]), &(in[3])) ==
642 for (i = 0; i < 4; i++)
644 BIO_printf(bio_err, "invalid IP address\n");
651 } else { /* do a gethostbyname */
654 if (!ssl_sock_init())
657 he = gethostbyname(str);
659 BIO_printf(bio_err, "gethostbyname failure\n");
662 if (he->h_addrtype != AF_INET) {
663 BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
666 ip[0] = he->h_addr_list[0][0];
667 ip[1] = he->h_addr_list[0][1];
668 ip[2] = he->h_addr_list[0][2];
669 ip[3] = he->h_addr_list[0][3];
676 int extract_port(const char *str, unsigned short *port_ptr)
683 *port_ptr = (unsigned short)i;
685 s = getservbyname(str, "tcp");
687 BIO_printf(bio_err, "getservbyname failure for %s\n", str);
690 *port_ptr = ntohs((unsigned short)s->s_port);