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
14 #ifndef OPENSSL_NO_SOCK
16 typedef struct bio_accept_st {
19 int bind_mode; /* Socket mode for BIO_listen */
20 int accepted_mode; /* Socket mode for BIO_accept (set on accepted sock) */
26 BIO_ADDRINFO *addr_first;
27 const BIO_ADDRINFO *addr_iter;
28 BIO_ADDR cache_accepting_addr; /* Useful if we asked for port 0 */
29 char *cache_accepting_name, *cache_accepting_serv;
30 BIO_ADDR cache_peer_addr;
31 char *cache_peer_name, *cache_peer_serv;
36 static int acpt_write(BIO *h, const char *buf, int num);
37 static int acpt_read(BIO *h, char *buf, int size);
38 static int acpt_puts(BIO *h, const char *str);
39 static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
40 static int acpt_new(BIO *h);
41 static int acpt_free(BIO *data);
42 static int acpt_state(BIO *b, BIO_ACCEPT *c);
43 static void acpt_close_socket(BIO *data);
44 static BIO_ACCEPT *BIO_ACCEPT_new(void);
45 static void BIO_ACCEPT_free(BIO_ACCEPT *a);
47 # define ACPT_S_BEFORE 1
48 # define ACPT_S_GET_ADDR 2
49 # define ACPT_S_CREATE_SOCKET 3
50 # define ACPT_S_LISTEN 4
51 # define ACPT_S_ACCEPT 5
54 static const BIO_METHOD methods_acceptp = {
57 /* TODO: Convert to new style write function */
60 /* TODO: Convert to new style read function */
64 NULL, /* connect_gets, */
68 NULL, /* connect_callback_ctrl */
71 const BIO_METHOD *BIO_s_accept(void)
73 return &methods_acceptp;
76 static int acpt_new(BIO *bi)
81 bi->num = (int)INVALID_SOCKET;
83 if ((ba = BIO_ACCEPT_new()) == NULL)
86 ba->state = ACPT_S_BEFORE;
91 static BIO_ACCEPT *BIO_ACCEPT_new(void)
95 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
97 ret->accept_family = BIO_FAMILY_IPANY;
98 ret->accept_sock = (int)INVALID_SOCKET;
102 static void BIO_ACCEPT_free(BIO_ACCEPT *a)
106 OPENSSL_free(a->param_addr);
107 OPENSSL_free(a->param_serv);
108 BIO_ADDRINFO_free(a->addr_first);
109 OPENSSL_free(a->cache_accepting_name);
110 OPENSSL_free(a->cache_accepting_serv);
111 OPENSSL_free(a->cache_peer_name);
112 OPENSSL_free(a->cache_peer_serv);
113 BIO_free(a->bio_chain);
117 static void acpt_close_socket(BIO *bio)
121 c = (BIO_ACCEPT *)bio->ptr;
122 if (c->accept_sock != (int)INVALID_SOCKET) {
123 shutdown(c->accept_sock, 2);
124 closesocket(c->accept_sock);
125 c->accept_sock = (int)INVALID_SOCKET;
126 bio->num = (int)INVALID_SOCKET;
130 static int acpt_free(BIO *a)
136 data = (BIO_ACCEPT *)a->ptr;
139 acpt_close_socket(a);
140 BIO_ACCEPT_free(data);
148 static int acpt_state(BIO *b, BIO_ACCEPT *c)
150 BIO *bio = NULL, *dbio;
151 int s = -1, ret = -1;
156 if (c->param_addr == NULL && c->param_serv == NULL) {
157 BIOerr(BIO_F_ACPT_STATE, BIO_R_NO_ACCEPT_ADDR_OR_SERVICE_SPECIFIED);
158 ERR_add_error_data(4,
159 "hostname=", c->param_addr,
160 " service=", c->param_serv);
164 /* Because we're starting a new bind, any cached name and serv
165 * are now obsolete and need to be cleaned out.
166 * QUESTION: should this be done in acpt_close_socket() instead?
168 OPENSSL_free(c->cache_accepting_name);
169 c->cache_accepting_name = NULL;
170 OPENSSL_free(c->cache_accepting_serv);
171 c->cache_accepting_serv = NULL;
172 OPENSSL_free(c->cache_peer_name);
173 c->cache_peer_name = NULL;
174 OPENSSL_free(c->cache_peer_serv);
175 c->cache_peer_serv = NULL;
177 c->state = ACPT_S_GET_ADDR;
180 case ACPT_S_GET_ADDR:
182 int family = AF_UNSPEC;
183 switch (c->accept_family) {
184 case BIO_FAMILY_IPV6:
185 if (1) { /* This is a trick we use to avoid bit rot.
186 * at least the "else" part will always be
193 BIOerr(BIO_F_ACPT_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);
197 case BIO_FAMILY_IPV4:
200 case BIO_FAMILY_IPANY:
204 BIOerr(BIO_F_ACPT_STATE, BIO_R_UNSUPPORTED_IP_FAMILY);
207 if (BIO_lookup(c->param_addr, c->param_serv, BIO_LOOKUP_SERVER,
208 family, SOCK_STREAM, &c->addr_first) == 0)
211 if (c->addr_first == NULL) {
212 BIOerr(BIO_F_ACPT_STATE, BIO_R_LOOKUP_RETURNED_NOTHING);
215 /* We're currently not iterating, but set this as preparation
216 * for possible future development in that regard
218 c->addr_iter = c->addr_first;
219 c->state = ACPT_S_CREATE_SOCKET;
222 case ACPT_S_CREATE_SOCKET:
223 ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
224 BIO_ADDRINFO_socktype(c->addr_iter),
225 BIO_ADDRINFO_protocol(c->addr_iter), 0);
226 if (ret == (int)INVALID_SOCKET) {
227 SYSerr(SYS_F_SOCKET, get_last_socket_error());
228 ERR_add_error_data(4,
229 "hostname=", c->param_addr,
230 " service=", c->param_serv);
231 BIOerr(BIO_F_ACPT_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
234 c->accept_sock = ret;
236 c->state = ACPT_S_LISTEN;
241 if (!BIO_listen(c->accept_sock,
242 BIO_ADDRINFO_address(c->addr_iter),
244 BIO_closesocket(c->accept_sock);
250 union BIO_sock_info_u info;
252 info.addr = &c->cache_accepting_addr;
253 if (!BIO_sock_info(c->accept_sock, BIO_SOCK_INFO_ADDRESS,
255 BIO_closesocket(c->accept_sock);
260 c->cache_accepting_name =
261 BIO_ADDR_hostname_string(&c->cache_accepting_addr, 1);
262 c->cache_accepting_serv =
263 BIO_ADDR_service_string(&c->cache_accepting_addr, 1);
264 c->state = ACPT_S_ACCEPT;
270 if (b->next_bio != NULL) {
271 c->state = ACPT_S_OK;
274 BIO_clear_retry_flags(b);
277 OPENSSL_free(c->cache_peer_name);
278 c->cache_peer_name = NULL;
279 OPENSSL_free(c->cache_peer_serv);
280 c->cache_peer_serv = NULL;
282 s = BIO_accept_ex(c->accept_sock, &c->cache_peer_addr,
285 /* If the returned socket is invalid, this might still be
289 if (BIO_sock_should_retry(s)) {
290 BIO_set_retry_special(b);
291 b->retry_reason = BIO_RR_ACCEPT;
296 /* If it wasn't retryable, we fail */
302 bio = BIO_new_socket(s, BIO_CLOSE);
306 BIO_set_callback(bio, BIO_get_callback(b));
307 BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
310 * If the accept BIO has an bio_chain, we dup it and put the new
313 if (c->bio_chain != NULL) {
314 if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
316 if (!BIO_push(dbio, bio))
320 if (BIO_push(b, bio) == NULL)
324 BIO_ADDR_hostname_string(&c->cache_peer_addr, 1);
326 BIO_ADDR_service_string(&c->cache_peer_addr, 1);
327 c->state = ACPT_S_OK;
333 if (b->next_bio == NULL) {
334 c->state = ACPT_S_ACCEPT;
355 static int acpt_read(BIO *b, char *out, int outl)
360 BIO_clear_retry_flags(b);
361 data = (BIO_ACCEPT *)b->ptr;
363 while (b->next_bio == NULL) {
364 ret = acpt_state(b, data);
369 ret = BIO_read(b->next_bio, out, outl);
370 BIO_copy_next_retry(b);
374 static int acpt_write(BIO *b, const char *in, int inl)
379 BIO_clear_retry_flags(b);
380 data = (BIO_ACCEPT *)b->ptr;
382 while (b->next_bio == NULL) {
383 ret = acpt_state(b, data);
388 ret = BIO_write(b->next_bio, in, inl);
389 BIO_copy_next_retry(b);
393 static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
400 data = (BIO_ACCEPT *)b->ptr;
405 data->state = ACPT_S_BEFORE;
406 acpt_close_socket(b);
407 BIO_ADDRINFO_free(data->addr_first);
408 data->addr_first = NULL;
411 case BIO_C_DO_STATE_MACHINE:
412 /* use this one to start the connection */
413 ret = (long)acpt_state(b, data);
415 case BIO_C_SET_ACCEPT:
418 char *hold_serv = data->param_serv;
419 /* We affect the hostname regardless. However, the input
420 * string might contain a host:service spec, so we must
421 * parse it, which might or might not affect the service
423 OPENSSL_free(data->param_addr);
424 data->param_addr = NULL;
425 ret = BIO_parse_hostserv(ptr,
428 BIO_PARSE_PRIO_SERV);
429 if (hold_serv != data->param_serv)
430 OPENSSL_free(hold_serv);
432 } else if (num == 1) {
433 OPENSSL_free(data->param_serv);
434 data->param_serv = BUF_strdup(ptr);
436 } else if (num == 2) {
437 data->bind_mode |= BIO_SOCK_NONBLOCK;
438 } else if (num == 3) {
439 BIO_free(data->bio_chain);
440 data->bio_chain = (BIO *)ptr;
441 } else if (num == 4) {
442 data->accept_family = *(int *)ptr;
446 data->bind_mode &= ~BIO_SOCK_NONBLOCK;
452 data->accepted_mode |= BIO_SOCK_NONBLOCK;
454 data->accepted_mode &= ~BIO_SOCK_NONBLOCK;
457 b->num = *((int *)ptr);
458 data->accept_sock = b->num;
459 data->state = ACPT_S_ACCEPT;
460 b->shutdown = (int)num;
467 *ip = data->accept_sock;
468 ret = data->accept_sock;
472 case BIO_C_GET_ACCEPT:
474 if (num == 0 && ptr != NULL) {
476 *pp = data->cache_accepting_name;
477 } else if (num == 1 && ptr != NULL) {
479 *pp = data->cache_accepting_serv;
480 } else if (num == 2 && ptr != NULL) {
482 *pp = data->cache_peer_name;
483 } else if (num == 3 && ptr != NULL) {
485 *pp = data->cache_peer_serv;
486 } else if (num == 4) {
487 switch (BIO_ADDRINFO_family(data->addr_iter)) {
490 ret = BIO_FAMILY_IPV6;
494 ret = BIO_FAMILY_IPV4;
497 ret = data->accept_family;
508 case BIO_CTRL_GET_CLOSE:
511 case BIO_CTRL_SET_CLOSE:
512 b->shutdown = (int)num;
514 case BIO_CTRL_PENDING:
515 case BIO_CTRL_WPENDING:
520 case BIO_C_SET_BIND_MODE:
521 data->bind_mode = (int)num;
523 case BIO_C_GET_BIND_MODE:
524 ret = (long)data->bind_mode;
536 static int acpt_puts(BIO *bp, const char *str)
541 ret = acpt_write(bp, str, n);
545 BIO *BIO_new_accept(const char *str)
549 ret = BIO_new(BIO_s_accept());
552 if (BIO_set_accept_name(ret, str))