2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 #define OPENSSL_SUPPRESS_DEPRECATED
14 #include "bio_local.h"
16 #ifndef OPENSSL_NO_SOCK
18 typedef struct bio_accept_st {
21 int bind_mode; /* Socket mode for BIO_listen */
22 int accepted_mode; /* Socket mode for BIO_accept (set on accepted sock) */
28 BIO_ADDRINFO *addr_first;
29 const BIO_ADDRINFO *addr_iter;
30 BIO_ADDR cache_accepting_addr; /* Useful if we asked for port 0 */
31 char *cache_accepting_name, *cache_accepting_serv;
32 BIO_ADDR cache_peer_addr;
33 char *cache_peer_name, *cache_peer_serv;
38 static int acpt_write(BIO *h, const char *buf, int num);
39 static int acpt_read(BIO *h, char *buf, int size);
40 static int acpt_puts(BIO *h, const char *str);
41 static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
42 static int acpt_new(BIO *h);
43 static int acpt_free(BIO *data);
44 static int acpt_state(BIO *b, BIO_ACCEPT *c);
45 static void acpt_close_socket(BIO *data);
46 static BIO_ACCEPT *BIO_ACCEPT_new(void);
47 static void BIO_ACCEPT_free(BIO_ACCEPT *a);
49 # define ACPT_S_BEFORE 1
50 # define ACPT_S_GET_ADDR 2
51 # define ACPT_S_CREATE_SOCKET 3
52 # define ACPT_S_LISTEN 4
53 # define ACPT_S_ACCEPT 5
56 static const BIO_METHOD methods_acceptp = {
59 /* TODO: Convert to new style write function */
62 /* TODO: Convert to new style read function */
66 NULL, /* connect_gets, */
70 NULL, /* connect_callback_ctrl */
73 const BIO_METHOD *BIO_s_accept(void)
75 return &methods_acceptp;
78 static int acpt_new(BIO *bi)
83 bi->num = (int)INVALID_SOCKET;
85 if ((ba = BIO_ACCEPT_new()) == NULL)
88 ba->state = ACPT_S_BEFORE;
93 static BIO_ACCEPT *BIO_ACCEPT_new(void)
97 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
98 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
101 ret->accept_family = BIO_FAMILY_IPANY;
102 ret->accept_sock = (int)INVALID_SOCKET;
106 static void BIO_ACCEPT_free(BIO_ACCEPT *a)
110 OPENSSL_free(a->param_addr);
111 OPENSSL_free(a->param_serv);
112 BIO_ADDRINFO_free(a->addr_first);
113 OPENSSL_free(a->cache_accepting_name);
114 OPENSSL_free(a->cache_accepting_serv);
115 OPENSSL_free(a->cache_peer_name);
116 OPENSSL_free(a->cache_peer_serv);
117 BIO_free(a->bio_chain);
121 static void acpt_close_socket(BIO *bio)
125 c = (BIO_ACCEPT *)bio->ptr;
126 if (c->accept_sock != (int)INVALID_SOCKET) {
127 shutdown(c->accept_sock, 2);
128 closesocket(c->accept_sock);
129 c->accept_sock = (int)INVALID_SOCKET;
130 bio->num = (int)INVALID_SOCKET;
134 static int acpt_free(BIO *a)
140 data = (BIO_ACCEPT *)a->ptr;
143 acpt_close_socket(a);
144 BIO_ACCEPT_free(data);
152 static int acpt_state(BIO *b, BIO_ACCEPT *c)
154 BIO *bio = NULL, *dbio;
155 int s = -1, ret = -1;
160 if (c->param_addr == NULL && c->param_serv == NULL) {
161 ERR_raise_data(ERR_LIB_BIO,
162 BIO_R_NO_ACCEPT_ADDR_OR_SERVICE_SPECIFIED,
163 "hostname=%s, service=%s",
164 c->param_addr, c->param_serv);
168 /* Because we're starting a new bind, any cached name and serv
169 * are now obsolete and need to be cleaned out.
170 * QUESTION: should this be done in acpt_close_socket() instead?
172 OPENSSL_free(c->cache_accepting_name);
173 c->cache_accepting_name = NULL;
174 OPENSSL_free(c->cache_accepting_serv);
175 c->cache_accepting_serv = NULL;
176 OPENSSL_free(c->cache_peer_name);
177 c->cache_peer_name = NULL;
178 OPENSSL_free(c->cache_peer_serv);
179 c->cache_peer_serv = NULL;
181 c->state = ACPT_S_GET_ADDR;
184 case ACPT_S_GET_ADDR:
186 int family = AF_UNSPEC;
187 switch (c->accept_family) {
188 case BIO_FAMILY_IPV6:
189 if (1) { /* This is a trick we use to avoid bit rot.
190 * at least the "else" part will always be
197 ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY);
201 case BIO_FAMILY_IPV4:
204 case BIO_FAMILY_IPANY:
208 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY);
211 if (BIO_lookup(c->param_addr, c->param_serv, BIO_LOOKUP_SERVER,
212 family, SOCK_STREAM, &c->addr_first) == 0)
215 if (c->addr_first == NULL) {
216 ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING);
219 /* We're currently not iterating, but set this as preparation
220 * for possible future development in that regard
222 c->addr_iter = c->addr_first;
223 c->state = ACPT_S_CREATE_SOCKET;
226 case ACPT_S_CREATE_SOCKET:
227 s = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
228 BIO_ADDRINFO_socktype(c->addr_iter),
229 BIO_ADDRINFO_protocol(c->addr_iter), 0);
230 if (s == (int)INVALID_SOCKET) {
231 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
232 "calling socket(%s, %s)",
233 c->param_addr, c->param_serv);
234 ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
239 c->state = ACPT_S_LISTEN;
245 if (!BIO_listen(c->accept_sock,
246 BIO_ADDRINFO_address(c->addr_iter),
248 BIO_closesocket(c->accept_sock);
254 union BIO_sock_info_u info;
256 info.addr = &c->cache_accepting_addr;
257 if (!BIO_sock_info(c->accept_sock, BIO_SOCK_INFO_ADDRESS,
259 BIO_closesocket(c->accept_sock);
264 c->cache_accepting_name =
265 BIO_ADDR_hostname_string(&c->cache_accepting_addr, 1);
266 c->cache_accepting_serv =
267 BIO_ADDR_service_string(&c->cache_accepting_addr, 1);
268 c->state = ACPT_S_ACCEPT;
274 if (b->next_bio != NULL) {
275 c->state = ACPT_S_OK;
278 BIO_clear_retry_flags(b);
281 OPENSSL_free(c->cache_peer_name);
282 c->cache_peer_name = NULL;
283 OPENSSL_free(c->cache_peer_serv);
284 c->cache_peer_serv = NULL;
286 s = BIO_accept_ex(c->accept_sock, &c->cache_peer_addr,
289 /* If the returned socket is invalid, this might still be
293 if (BIO_sock_should_retry(s)) {
294 BIO_set_retry_special(b);
295 b->retry_reason = BIO_RR_ACCEPT;
300 /* If it wasn't retryable, we fail */
306 bio = BIO_new_socket(s, BIO_CLOSE);
310 BIO_set_callback_ex(bio, BIO_get_callback_ex(b));
311 #ifndef OPENSSL_NO_DEPRECATED_3_0
312 BIO_set_callback(bio, BIO_get_callback(b));
314 BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
316 * If the accept BIO has an bio_chain, we dup it and put the new
319 if (c->bio_chain != NULL) {
320 if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
322 if (!BIO_push(dbio, bio))
326 if (BIO_push(b, bio) == NULL)
330 BIO_ADDR_hostname_string(&c->cache_peer_addr, 1);
332 BIO_ADDR_service_string(&c->cache_peer_addr, 1);
333 c->state = ACPT_S_OK;
339 if (b->next_bio == NULL) {
340 c->state = ACPT_S_ACCEPT;
361 static int acpt_read(BIO *b, char *out, int outl)
366 BIO_clear_retry_flags(b);
367 data = (BIO_ACCEPT *)b->ptr;
369 while (b->next_bio == NULL) {
370 ret = acpt_state(b, data);
375 ret = BIO_read(b->next_bio, out, outl);
376 BIO_copy_next_retry(b);
380 static int acpt_write(BIO *b, const char *in, int inl)
385 BIO_clear_retry_flags(b);
386 data = (BIO_ACCEPT *)b->ptr;
388 while (b->next_bio == NULL) {
389 ret = acpt_state(b, data);
394 ret = BIO_write(b->next_bio, in, inl);
395 BIO_copy_next_retry(b);
399 static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
406 data = (BIO_ACCEPT *)b->ptr;
411 data->state = ACPT_S_BEFORE;
412 acpt_close_socket(b);
413 BIO_ADDRINFO_free(data->addr_first);
414 data->addr_first = NULL;
417 case BIO_C_DO_STATE_MACHINE:
418 /* use this one to start the connection */
419 ret = (long)acpt_state(b, data);
421 case BIO_C_SET_ACCEPT:
424 char *hold_serv = data->param_serv;
425 /* We affect the hostname regardless. However, the input
426 * string might contain a host:service spec, so we must
427 * parse it, which might or might not affect the service
429 OPENSSL_free(data->param_addr);
430 data->param_addr = NULL;
431 ret = BIO_parse_hostserv(ptr,
434 BIO_PARSE_PRIO_SERV);
435 if (hold_serv != data->param_serv)
436 OPENSSL_free(hold_serv);
438 } else if (num == 1) {
439 OPENSSL_free(data->param_serv);
440 if ((data->param_serv = OPENSSL_strdup(ptr)) == NULL)
444 } else if (num == 2) {
445 data->bind_mode |= BIO_SOCK_NONBLOCK;
446 } else if (num == 3) {
447 BIO_free(data->bio_chain);
448 data->bio_chain = (BIO *)ptr;
449 } else if (num == 4) {
450 data->accept_family = *(int *)ptr;
454 data->bind_mode &= ~BIO_SOCK_NONBLOCK;
460 data->accepted_mode |= BIO_SOCK_NONBLOCK;
462 data->accepted_mode &= ~BIO_SOCK_NONBLOCK;
465 b->num = *((int *)ptr);
466 data->accept_sock = b->num;
467 data->state = ACPT_S_ACCEPT;
468 b->shutdown = (int)num;
475 *ip = data->accept_sock;
476 ret = data->accept_sock;
480 case BIO_C_GET_ACCEPT:
482 if (num == 0 && ptr != NULL) {
484 *pp = data->cache_accepting_name;
485 } else if (num == 1 && ptr != NULL) {
487 *pp = data->cache_accepting_serv;
488 } else if (num == 2 && ptr != NULL) {
490 *pp = data->cache_peer_name;
491 } else if (num == 3 && ptr != NULL) {
493 *pp = data->cache_peer_serv;
494 } else if (num == 4) {
495 switch (BIO_ADDRINFO_family(data->addr_iter)) {
498 ret = BIO_FAMILY_IPV6;
502 ret = BIO_FAMILY_IPV4;
505 ret = data->accept_family;
516 case BIO_CTRL_GET_CLOSE:
519 case BIO_CTRL_SET_CLOSE:
520 b->shutdown = (int)num;
522 case BIO_CTRL_PENDING:
523 case BIO_CTRL_WPENDING:
528 case BIO_C_SET_BIND_MODE:
529 data->bind_mode = (int)num;
531 case BIO_C_GET_BIND_MODE:
532 ret = (long)data->bind_mode;
537 if (b->next_bio == NULL)
540 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
549 static int acpt_puts(BIO *bp, const char *str)
554 ret = acpt_write(bp, str, n);
558 BIO *BIO_new_accept(const char *str)
562 ret = BIO_new(BIO_s_accept());
565 if (BIO_set_accept_name(ret, str))