Add a -sctp option to s_server
[openssl.git] / apps / s_socket.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 /* socket-related functions used by s_client and s_server */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <signal.h>
16 #include <openssl/opensslconf.h>
17
18 /*
19  * With IPv6, it looks like Digital has mixed up the proper order of
20  * recursive header file inclusion, resulting in the compiler complaining
21  * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
22  * needed to have fileno() declared correctly...  So let's define u_int
23  */
24 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
25 # define __U_INT
26 typedef unsigned int u_int;
27 #endif
28
29 #ifndef OPENSSL_NO_SOCK
30
31 # define USE_SOCKETS
32 # include "apps.h"
33 # undef USE_SOCKETS
34 # include "s_apps.h"
35
36 # include <openssl/bio.h>
37 # include <openssl/err.h>
38
39 /*
40  * init_client - helper routine to set up socket communication
41  * @sock: pointer to storage of resulting socket.
42  * @host: the host name or path (for AF_UNIX) to connect to.
43  * @port: the port to connect to (ignored for AF_UNIX).
44  * @family: desired socket family, may be AF_INET, AF_INET6, AF_UNIX or
45  *  AF_UNSPEC
46  * @type: socket type, must be SOCK_STREAM or SOCK_DGRAM
47  *
48  * This will create a socket and use it to connect to a host:port, or if
49  * family == AF_UNIX, to the path found in host.
50  *
51  * If the host has more than one address, it will try them one by one until
52  * a successful connection is established.  The resulting socket will be
53  * found in *sock on success, it will be given INVALID_SOCKET otherwise.
54  *
55  * Returns 1 on success, 0 on failure.
56  */
57 int init_client(int *sock, const char *host, const char *port,
58                 int family, int type)
59 {
60     BIO_ADDRINFO *res = NULL;
61     const BIO_ADDRINFO *ai = NULL;
62     int ret;
63
64     if (!BIO_sock_init())
65         return 0;
66
67     ret = BIO_lookup(host, port, BIO_LOOKUP_CLIENT, family, type, &res);
68     if (ret == 0) {
69         ERR_print_errors(bio_err);
70         return 0;
71     }
72
73     ret = 0;
74     for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) {
75         /* Admittedly, these checks are quite paranoid, we should not get
76          * anything in the BIO_ADDRINFO chain that we haven't
77          * asked for. */
78         OPENSSL_assert((family == AF_UNSPEC || family == BIO_ADDRINFO_family(res))
79                        && (type == 0 || type == BIO_ADDRINFO_socktype(res)));
80
81         *sock = BIO_socket(BIO_ADDRINFO_family(ai), BIO_ADDRINFO_socktype(ai),
82                            BIO_ADDRINFO_protocol(res), 0);
83         if (*sock == INVALID_SOCKET) {
84             /* Maybe the kernel doesn't support the socket family, even if
85              * BIO_lookup() added it in the returned result...
86              */
87             continue;
88         }
89         if (!BIO_connect(*sock, BIO_ADDRINFO_address(ai), 0)) {
90             BIO_closesocket(*sock);
91             *sock = INVALID_SOCKET;
92             continue;
93         }
94
95         /* Success, don't try any more addresses */
96         break;
97     }
98
99     if (*sock == INVALID_SOCKET) {
100         ERR_print_errors(bio_err);
101     } else {
102         /* Remove any stale errors from previous connection attempts */
103         ERR_clear_error();
104         ret = 1;
105     }
106     BIO_ADDRINFO_free(res);
107     return ret;
108 }
109
110 /*
111  * do_server - helper routine to perform a server operation
112  * @accept_sock: pointer to storage of resulting socket.
113  * @host: the host name or path (for AF_UNIX) to connect to.
114  * @port: the port to connect to (ignored for AF_UNIX).
115  * @family: desired socket family, may be AF_INET, AF_INET6, AF_UNIX or
116  *  AF_UNSPEC
117  * @type: socket type, must be SOCK_STREAM or SOCK_DGRAM
118  * @cb: pointer to a function that receives the accepted socket and
119  *  should perform the communication with the connecting client.
120  * @context: pointer to memory that's passed verbatim to the cb function.
121  * @naccept: number of times an incoming connect should be accepted.  If -1,
122  *  unlimited number.
123  *
124  * This will create a socket and use it to listen to a host:port, or if
125  * family == AF_UNIX, to the path found in host, then start accepting
126  * incoming connections and run cb on the resulting socket.
127  *
128  * 0 on failure, something other on success.
129  */
130 int do_server(int *accept_sock, const char *host, const char *port,
131               int family, int type, int protocol, do_server_cb cb,
132               unsigned char *context, int naccept)
133 {
134     int asock = 0;
135     int sock;
136     int i;
137     BIO_ADDRINFO *res = NULL;
138     int ret = 0;
139
140     if (!BIO_sock_init())
141         return 0;
142
143     if (!BIO_lookup_ex(host, port, BIO_LOOKUP_SERVER, family, type, protocol,
144                        &res)) {
145         ERR_print_errors(bio_err);
146         return 0;
147     }
148
149     /* Admittedly, these checks are quite paranoid, we should not get
150      * anything in the BIO_ADDRINFO chain that we haven't asked for */
151     OPENSSL_assert((family == AF_UNSPEC || family == BIO_ADDRINFO_family(res))
152                    && (type == 0 || type == BIO_ADDRINFO_socktype(res))
153                    && (protocol == 0 || protocol == BIO_ADDRINFO_protocol(res)));
154
155     asock = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
156                        BIO_ADDRINFO_protocol(res), 0);
157     if (asock == INVALID_SOCKET
158         || !BIO_listen(asock, BIO_ADDRINFO_address(res), BIO_SOCK_REUSEADDR)) {
159         BIO_ADDRINFO_free(res);
160         ERR_print_errors(bio_err);
161         if (asock != INVALID_SOCKET)
162             BIO_closesocket(asock);
163         goto end;
164     }
165
166 #ifndef OPENSSL_NO_SCTP
167     if (protocol == IPPROTO_SCTP) {
168         /*
169          * For SCTP we have to set various options on the socket prior to
170          * accepting. This is done automatically by BIO_new_dgram_sctp().
171          * We don't actually need the created BIO though so we free it again
172          * immediately.
173          */
174         BIO *tmpbio = BIO_new_dgram_sctp(asock, BIO_NOCLOSE);
175
176         if (tmpbio == NULL) {
177             BIO_closesocket(asock);
178             ERR_print_errors(bio_err);
179             goto end;
180         }
181         BIO_free(tmpbio);
182     }
183 #endif
184
185     BIO_ADDRINFO_free(res);
186     res = NULL;
187
188     if (accept_sock != NULL)
189         *accept_sock = asock;
190     for (;;) {
191         if (type == SOCK_STREAM) {
192             do {
193                 sock = BIO_accept_ex(asock, NULL, 0);
194             } while (sock < 0 && BIO_sock_should_retry(ret));
195             if (sock < 0) {
196                 ERR_print_errors(bio_err);
197                 BIO_closesocket(asock);
198                 break;
199             }
200             i = (*cb)(sock, type, protocol, context);
201             BIO_closesocket(sock);
202         } else {
203             i = (*cb)(asock, type, protocol, context);
204         }
205
206         if (naccept != -1)
207             naccept--;
208         if (i < 0 || naccept == 0) {
209             BIO_closesocket(asock);
210             ret = i;
211             break;
212         }
213     }
214  end:
215 # ifdef AF_UNIX
216     if (family == AF_UNIX)
217         unlink(host);
218 # endif
219     return ret;
220 }
221
222 #endif  /* OPENSSL_NO_SOCK */