2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* socket-related functions used by s_client and s_server */
16 #include <openssl/opensslconf.h>
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
24 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
26 typedef unsigned int u_int;
29 #ifndef OPENSSL_NO_SOCK
33 # include "internal/sockets.h"
35 # include <openssl/bio.h>
36 # include <openssl/err.h>
38 /* Keep track of our peer's address for the cookie callback */
39 BIO_ADDR *ourpeer = NULL;
42 * init_client - helper routine to set up socket communication
43 * @sock: pointer to storage of resulting socket.
44 * @host: the host name or path (for AF_UNIX) to connect to.
45 * @port: the port to connect to (ignored for AF_UNIX).
46 * @family: desired socket family, may be AF_INET, AF_INET6, AF_UNIX or
48 * @type: socket type, must be SOCK_STREAM or SOCK_DGRAM
49 * @protocol: socket protocol, e.g. IPPROTO_TCP or IPPROTO_UDP (or 0 for any)
51 * This will create a socket and use it to connect to a host:port, or if
52 * family == AF_UNIX, to the path found in host.
54 * If the host has more than one address, it will try them one by one until
55 * a successful connection is established. The resulting socket will be
56 * found in *sock on success, it will be given INVALID_SOCKET otherwise.
58 * Returns 1 on success, 0 on failure.
60 int init_client(int *sock, const char *host, const char *port,
61 int family, int type, int protocol)
63 BIO_ADDRINFO *res = NULL;
64 const BIO_ADDRINFO *ai = NULL;
67 if (BIO_sock_init() != 1)
70 ret = BIO_lookup_ex(host, port, BIO_LOOKUP_CLIENT, family, type, protocol,
73 ERR_print_errors(bio_err);
78 for (ai = res; ai != NULL; ai = BIO_ADDRINFO_next(ai)) {
79 /* Admittedly, these checks are quite paranoid, we should not get
80 * anything in the BIO_ADDRINFO chain that we haven't
82 OPENSSL_assert((family == AF_UNSPEC
83 || family == BIO_ADDRINFO_family(ai))
84 && (type == 0 || type == BIO_ADDRINFO_socktype(ai))
86 || protocol == BIO_ADDRINFO_protocol(ai)));
88 *sock = BIO_socket(BIO_ADDRINFO_family(ai), BIO_ADDRINFO_socktype(ai),
89 BIO_ADDRINFO_protocol(ai), 0);
90 if (*sock == INVALID_SOCKET) {
91 /* Maybe the kernel doesn't support the socket family, even if
92 * BIO_lookup() added it in the returned result...
97 #ifndef OPENSSL_NO_SCTP
98 if (protocol == IPPROTO_SCTP) {
100 * For SCTP we have to set various options on the socket prior to
101 * connecting. This is done automatically by BIO_new_dgram_sctp().
102 * We don't actually need the created BIO though so we free it again
105 BIO *tmpbio = BIO_new_dgram_sctp(*sock, BIO_NOCLOSE);
107 if (tmpbio == NULL) {
108 ERR_print_errors(bio_err);
115 if (!BIO_connect(*sock, BIO_ADDRINFO_address(ai), 0)) {
116 BIO_closesocket(*sock);
117 *sock = INVALID_SOCKET;
121 /* Success, don't try any more addresses */
125 if (*sock == INVALID_SOCKET) {
126 ERR_print_errors(bio_err);
128 /* Remove any stale errors from previous connection attempts */
132 BIO_ADDRINFO_free(res);
137 * do_server - helper routine to perform a server operation
138 * @accept_sock: pointer to storage of resulting socket.
139 * @host: the host name or path (for AF_UNIX) to connect to.
140 * @port: the port to connect to (ignored for AF_UNIX).
141 * @family: desired socket family, may be AF_INET, AF_INET6, AF_UNIX or
143 * @type: socket type, must be SOCK_STREAM or SOCK_DGRAM
144 * @cb: pointer to a function that receives the accepted socket and
145 * should perform the communication with the connecting client.
146 * @context: pointer to memory that's passed verbatim to the cb function.
147 * @naccept: number of times an incoming connect should be accepted. If -1,
150 * This will create a socket and use it to listen to a host:port, or if
151 * family == AF_UNIX, to the path found in host, then start accepting
152 * incoming connections and run cb on the resulting socket.
154 * 0 on failure, something other on success.
156 int do_server(int *accept_sock, const char *host, const char *port,
157 int family, int type, int protocol, do_server_cb cb,
158 unsigned char *context, int naccept)
163 BIO_ADDRINFO *res = NULL;
164 const BIO_ADDRINFO *next;
165 int sock_family, sock_type, sock_protocol;
166 const BIO_ADDR *sock_address;
167 int sock_options = BIO_SOCK_REUSEADDR;
170 if (BIO_sock_init() != 1)
173 if (!BIO_lookup_ex(host, port, BIO_LOOKUP_SERVER, family, type, protocol,
175 ERR_print_errors(bio_err);
179 /* Admittedly, these checks are quite paranoid, we should not get
180 * anything in the BIO_ADDRINFO chain that we haven't asked for */
181 OPENSSL_assert((family == AF_UNSPEC || family == BIO_ADDRINFO_family(res))
182 && (type == 0 || type == BIO_ADDRINFO_socktype(res))
183 && (protocol == 0 || protocol == BIO_ADDRINFO_protocol(res)));
185 sock_family = BIO_ADDRINFO_family(res);
186 sock_type = BIO_ADDRINFO_socktype(res);
187 sock_protocol = BIO_ADDRINFO_protocol(res);
188 sock_address = BIO_ADDRINFO_address(res);
189 next = BIO_ADDRINFO_next(res);
190 if (sock_family == AF_INET6)
191 sock_options |= BIO_SOCK_V6_ONLY;
193 && BIO_ADDRINFO_socktype(next) == sock_type
194 && BIO_ADDRINFO_protocol(next) == sock_protocol) {
195 if (sock_family == AF_INET
196 && BIO_ADDRINFO_family(next) == AF_INET6) {
197 sock_family = AF_INET6;
198 sock_address = BIO_ADDRINFO_address(next);
199 } else if (sock_family == AF_INET6
200 && BIO_ADDRINFO_family(next) == AF_INET) {
201 sock_options &= ~BIO_SOCK_V6_ONLY;
205 asock = BIO_socket(sock_family, sock_type, sock_protocol, 0);
206 if (asock == INVALID_SOCKET
207 || !BIO_listen(asock, sock_address, sock_options)) {
208 BIO_ADDRINFO_free(res);
209 ERR_print_errors(bio_err);
210 if (asock != INVALID_SOCKET)
211 BIO_closesocket(asock);
215 #ifndef OPENSSL_NO_SCTP
216 if (protocol == IPPROTO_SCTP) {
218 * For SCTP we have to set various options on the socket prior to
219 * accepting. This is done automatically by BIO_new_dgram_sctp().
220 * We don't actually need the created BIO though so we free it again
223 BIO *tmpbio = BIO_new_dgram_sctp(asock, BIO_NOCLOSE);
225 if (tmpbio == NULL) {
226 BIO_closesocket(asock);
227 ERR_print_errors(bio_err);
234 BIO_ADDRINFO_free(res);
237 if (accept_sock != NULL)
238 *accept_sock = asock;
240 if (type == SOCK_STREAM) {
241 BIO_ADDR_free(ourpeer);
242 ourpeer = BIO_ADDR_new();
243 if (ourpeer == NULL) {
244 BIO_closesocket(asock);
245 ERR_print_errors(bio_err);
249 sock = BIO_accept_ex(asock, ourpeer, 0);
250 } while (sock < 0 && BIO_sock_should_retry(sock));
252 ERR_print_errors(bio_err);
253 BIO_closesocket(asock);
256 i = (*cb)(sock, type, protocol, context);
259 * Give the socket time to send its last data before we close it.
260 * No amount of setting SO_LINGER etc on the socket seems to
261 * persuade Windows to send the data before closing the socket...
262 * but sleeping for a short time seems to do it (units in ms)
263 * TODO: Find a better way to do this
265 #if defined(OPENSSL_SYS_WINDOWS)
267 #elif defined(OPENSSL_SYS_CYGWIN)
272 * If we ended with an alert being sent, but still with data in the
273 * network buffer to be read, then calling BIO_closesocket() will
274 * result in a TCP-RST being sent. On some platforms (notably
275 * Windows) then this will result in the peer immediately abandoning
276 * the connection including any buffered alert data before it has
277 * had a chance to be read. Shutting down the sending side first,
278 * and then closing the socket sends TCP-FIN first followed by
279 * TCP-RST. This seems to allow the peer to read the alert data.
281 shutdown(sock, 1); /* SHUT_WR */
282 BIO_closesocket(sock);
284 i = (*cb)(asock, type, protocol, context);
289 if (i < 0 || naccept == 0) {
290 BIO_closesocket(asock);
297 if (family == AF_UNIX)
300 BIO_ADDR_free(ourpeer);
305 #endif /* OPENSSL_NO_SOCK */