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
12 #include "bio_local.h"
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 ERR_raise(ERR_LIB_BIO
, 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 ERR_raise_data(ERR_LIB_BIO
,
160 BIO_R_NO_ACCEPT_ADDR_OR_SERVICE_SPECIFIED
,
161 "hostname=%s, service=%s",
162 c
->param_addr
, 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 ERR_raise(ERR_LIB_BIO
, BIO_R_UNAVAILABLE_IP_FAMILY
);
199 case BIO_FAMILY_IPV4
:
202 case BIO_FAMILY_IPANY
:
206 ERR_raise(ERR_LIB_BIO
, 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 ERR_raise(ERR_LIB_BIO
, 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 s
= 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 (s
== (int)INVALID_SOCKET
) {
229 ERR_raise_data(ERR_LIB_SYS
, get_last_socket_error(),
230 "calling socket(%s, %s)",
231 c
->param_addr
, c
->param_serv
);
232 ERR_raise(ERR_LIB_BIO
, BIO_R_UNABLE_TO_CREATE_SOCKET
);
237 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 if ((data
->param_serv
= OPENSSL_strdup(ptr
)) == NULL
)
440 } else if (num
== 2) {
441 data
->bind_mode
|= BIO_SOCK_NONBLOCK
;
442 } else if (num
== 3) {
443 BIO_free(data
->bio_chain
);
444 data
->bio_chain
= (BIO
*)ptr
;
445 } else if (num
== 4) {
446 data
->accept_family
= *(int *)ptr
;
450 data
->bind_mode
&= ~BIO_SOCK_NONBLOCK
;
456 data
->accepted_mode
|= BIO_SOCK_NONBLOCK
;
458 data
->accepted_mode
&= ~BIO_SOCK_NONBLOCK
;
461 b
->num
= *((int *)ptr
);
462 data
->accept_sock
= b
->num
;
463 data
->state
= ACPT_S_ACCEPT
;
464 b
->shutdown
= (int)num
;
471 *ip
= data
->accept_sock
;
472 ret
= data
->accept_sock
;
476 case BIO_C_GET_ACCEPT
:
478 if (num
== 0 && ptr
!= NULL
) {
480 *pp
= data
->cache_accepting_name
;
481 } else if (num
== 1 && ptr
!= NULL
) {
483 *pp
= data
->cache_accepting_serv
;
484 } else if (num
== 2 && ptr
!= NULL
) {
486 *pp
= data
->cache_peer_name
;
487 } else if (num
== 3 && ptr
!= NULL
) {
489 *pp
= data
->cache_peer_serv
;
490 } else if (num
== 4) {
491 switch (BIO_ADDRINFO_family(data
->addr_iter
)) {
494 ret
= BIO_FAMILY_IPV6
;
498 ret
= BIO_FAMILY_IPV4
;
501 ret
= data
->accept_family
;
512 case BIO_CTRL_GET_CLOSE
:
515 case BIO_CTRL_SET_CLOSE
:
516 b
->shutdown
= (int)num
;
518 case BIO_CTRL_PENDING
:
519 case BIO_CTRL_WPENDING
:
524 case BIO_C_SET_BIND_MODE
:
525 data
->bind_mode
= (int)num
;
527 case BIO_C_GET_BIND_MODE
:
528 ret
= (long)data
->bind_mode
;
533 if (b
->next_bio
== NULL
)
536 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
545 static int acpt_puts(BIO
*bp
, const char *str
)
550 ret
= acpt_write(bp
, str
, n
);
554 BIO
*BIO_new_accept(const char *str
)
558 ret
= BIO_new(BIO_s_accept());
561 if (BIO_set_accept_name(ret
, str
))