2 * Copyright 1995-2018 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
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) {
96 BIOerr(BIO_F_BIO_ACCEPT_NEW, ERR_R_MALLOC_FAILURE);
99 ret->accept_family = BIO_FAMILY_IPANY;
100 ret->accept_sock = (int)INVALID_SOCKET;
104 static void BIO_ACCEPT_free(BIO_ACCEPT *a)
108 OPENSSL_free(a->param_addr);
109 OPENSSL_free(a->param_serv);
110 BIO_ADDRINFO_free(a->addr_first);
111 OPENSSL_free(a->cache_accepting_name);
112 OPENSSL_free(a->cache_accepting_serv);
113 OPENSSL_free(a->cache_peer_name);
114 OPENSSL_free(a->cache_peer_serv);
115 BIO_free(a->bio_chain);
119 static void acpt_close_socket(BIO *bio)
123 c = (BIO_ACCEPT *)bio->ptr;
124 if (c->accept_sock != (int)INVALID_SOCKET) {
125 shutdown(c->accept_sock, 2);
126 closesocket(c->accept_sock);
127 c->accept_sock = (int)INVALID_SOCKET;
128 bio->num = (int)INVALID_SOCKET;
132 static int acpt_free(BIO *a)
138 data = (BIO_ACCEPT *)a->ptr;
141 acpt_close_socket(a);
142 BIO_ACCEPT_free(data);
150 static int acpt_state(BIO *b, BIO_ACCEPT *c)
152 BIO *bio = NULL, *dbio;
153 int s = -1, ret = -1;
158 if (c->param_addr == NULL && c->param_serv == NULL) {
159 BIOerr(BIO_F_ACPT_STATE, BIO_R_NO_ACCEPT_ADDR_OR_SERVICE_SPECIFIED);
160 ERR_add_error_data(4,
161 "hostname=", c->param_addr,
162 " service=", c->param_serv);
166 /* Because we're starting a new bind, any cached name and serv
167 * are now obsolete and need to be cleaned out.
168 * QUESTION: should this be done in acpt_close_socket() instead?
170 OPENSSL_free(c->cache_accepting_name);
171 c->cache_accepting_name = NULL;
172 OPENSSL_free(c->cache_accepting_serv);
173 c->cache_accepting_serv = NULL;
174 OPENSSL_free(c->cache_peer_name);
175 c->cache_peer_name = NULL;
176 OPENSSL_free(c->cache_peer_serv);
177 c->cache_peer_serv = NULL;
179 c->state = ACPT_S_GET_ADDR;
182 case ACPT_S_GET_ADDR:
184 int family = AF_UNSPEC;
185 switch (c->accept_family) {
186 case BIO_FAMILY_IPV6:
187 if (1) { /* This is a trick we use to avoid bit rot.
188 * at least the "else" part will always be
195 BIOerr(BIO_F_ACPT_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);
199 case BIO_FAMILY_IPV4:
202 case BIO_FAMILY_IPANY:
206 BIOerr(BIO_F_ACPT_STATE, BIO_R_UNSUPPORTED_IP_FAMILY);
209 if (BIO_lookup(c->param_addr, c->param_serv, BIO_LOOKUP_SERVER,
210 family, SOCK_STREAM, &c->addr_first) == 0)
213 if (c->addr_first == NULL) {
214 BIOerr(BIO_F_ACPT_STATE, BIO_R_LOOKUP_RETURNED_NOTHING);
217 /* We're currently not iterating, but set this as preparation
218 * for possible future development in that regard
220 c->addr_iter = c->addr_first;
221 c->state = ACPT_S_CREATE_SOCKET;
224 case ACPT_S_CREATE_SOCKET:
225 ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
226 BIO_ADDRINFO_socktype(c->addr_iter),
227 BIO_ADDRINFO_protocol(c->addr_iter), 0);
228 if (ret == (int)INVALID_SOCKET) {
229 FUNCerr("socket", get_last_socket_error());
230 ERR_add_error_data(4,
231 "hostname=", c->param_addr,
232 " service=", c->param_serv);
233 BIOerr(BIO_F_ACPT_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
236 c->accept_sock = ret;
238 c->state = ACPT_S_LISTEN;
243 if (!BIO_listen(c->accept_sock,
244 BIO_ADDRINFO_address(c->addr_iter),
246 BIO_closesocket(c->accept_sock);
252 union BIO_sock_info_u info;
254 info.addr = &c->cache_accepting_addr;
255 if (!BIO_sock_info(c->accept_sock, BIO_SOCK_INFO_ADDRESS,
257 BIO_closesocket(c->accept_sock);
262 c->cache_accepting_name =
263 BIO_ADDR_hostname_string(&c->cache_accepting_addr, 1);
264 c->cache_accepting_serv =
265 BIO_ADDR_service_string(&c->cache_accepting_addr, 1);
266 c->state = ACPT_S_ACCEPT;
272 if (b->next_bio != NULL) {
273 c->state = ACPT_S_OK;
276 BIO_clear_retry_flags(b);
279 OPENSSL_free(c->cache_peer_name);
280 c->cache_peer_name = NULL;
281 OPENSSL_free(c->cache_peer_serv);
282 c->cache_peer_serv = NULL;
284 s = BIO_accept_ex(c->accept_sock, &c->cache_peer_addr,
287 /* If the returned socket is invalid, this might still be
291 if (BIO_sock_should_retry(s)) {
292 BIO_set_retry_special(b);
293 b->retry_reason = BIO_RR_ACCEPT;
298 /* If it wasn't retryable, we fail */
304 bio = BIO_new_socket(s, BIO_CLOSE);
308 BIO_set_callback(bio, BIO_get_callback(b));
309 BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
312 * If the accept BIO has an bio_chain, we dup it and put the new
315 if (c->bio_chain != NULL) {
316 if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
318 if (!BIO_push(dbio, bio))
322 if (BIO_push(b, bio) == NULL)
326 BIO_ADDR_hostname_string(&c->cache_peer_addr, 1);
328 BIO_ADDR_service_string(&c->cache_peer_addr, 1);
329 c->state = ACPT_S_OK;
335 if (b->next_bio == NULL) {
336 c->state = ACPT_S_ACCEPT;
357 static int acpt_read(BIO *b, char *out, int outl)
362 BIO_clear_retry_flags(b);
363 data = (BIO_ACCEPT *)b->ptr;
365 while (b->next_bio == NULL) {
366 ret = acpt_state(b, data);
371 ret = BIO_read(b->next_bio, out, outl);
372 BIO_copy_next_retry(b);
376 static int acpt_write(BIO *b, const char *in, int inl)
381 BIO_clear_retry_flags(b);
382 data = (BIO_ACCEPT *)b->ptr;
384 while (b->next_bio == NULL) {
385 ret = acpt_state(b, data);
390 ret = BIO_write(b->next_bio, in, inl);
391 BIO_copy_next_retry(b);
395 static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
402 data = (BIO_ACCEPT *)b->ptr;
407 data->state = ACPT_S_BEFORE;
408 acpt_close_socket(b);
409 BIO_ADDRINFO_free(data->addr_first);
410 data->addr_first = NULL;
413 case BIO_C_DO_STATE_MACHINE:
414 /* use this one to start the connection */
415 ret = (long)acpt_state(b, data);
417 case BIO_C_SET_ACCEPT:
420 char *hold_serv = data->param_serv;
421 /* We affect the hostname regardless. However, the input
422 * string might contain a host:service spec, so we must
423 * parse it, which might or might not affect the service
425 OPENSSL_free(data->param_addr);
426 data->param_addr = NULL;
427 ret = BIO_parse_hostserv(ptr,
430 BIO_PARSE_PRIO_SERV);
431 if (hold_serv != data->param_serv)
432 OPENSSL_free(hold_serv);
434 } else if (num == 1) {
435 OPENSSL_free(data->param_serv);
436 data->param_serv = BUF_strdup(ptr);
438 } else if (num == 2) {
439 data->bind_mode |= BIO_SOCK_NONBLOCK;
440 } else if (num == 3) {
441 BIO_free(data->bio_chain);
442 data->bio_chain = (BIO *)ptr;
443 } else if (num == 4) {
444 data->accept_family = *(int *)ptr;
448 data->bind_mode &= ~BIO_SOCK_NONBLOCK;
454 data->accepted_mode |= BIO_SOCK_NONBLOCK;
456 data->accepted_mode &= ~BIO_SOCK_NONBLOCK;
459 b->num = *((int *)ptr);
460 data->accept_sock = b->num;
461 data->state = ACPT_S_ACCEPT;
462 b->shutdown = (int)num;
469 *ip = data->accept_sock;
470 ret = data->accept_sock;
474 case BIO_C_GET_ACCEPT:
476 if (num == 0 && ptr != NULL) {
478 *pp = data->cache_accepting_name;
479 } else if (num == 1 && ptr != NULL) {
481 *pp = data->cache_accepting_serv;
482 } else if (num == 2 && ptr != NULL) {
484 *pp = data->cache_peer_name;
485 } else if (num == 3 && ptr != NULL) {
487 *pp = data->cache_peer_serv;
488 } else if (num == 4) {
489 switch (BIO_ADDRINFO_family(data->addr_iter)) {
492 ret = BIO_FAMILY_IPV6;
496 ret = BIO_FAMILY_IPV4;
499 ret = data->accept_family;
510 case BIO_CTRL_GET_CLOSE:
513 case BIO_CTRL_SET_CLOSE:
514 b->shutdown = (int)num;
516 case BIO_CTRL_PENDING:
517 case BIO_CTRL_WPENDING:
522 case BIO_C_SET_BIND_MODE:
523 data->bind_mode = (int)num;
525 case BIO_C_GET_BIND_MODE:
526 ret = (long)data->bind_mode;
538 static int acpt_puts(BIO *bp, const char *str)
543 ret = acpt_write(bp, str, n);
547 BIO *BIO_new_accept(const char *str)
551 ret = BIO_new(BIO_s_accept());
554 if (BIO_set_accept_name(ret, str))