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
)
107 OPENSSL_free(a
->param_addr
);
108 OPENSSL_free(a
->param_serv
);
109 BIO_ADDRINFO_free(a
->addr_first
);
110 OPENSSL_free(a
->cache_accepting_name
);
111 OPENSSL_free(a
->cache_accepting_serv
);
112 OPENSSL_free(a
->cache_peer_name
);
113 OPENSSL_free(a
->cache_peer_serv
);
114 BIO_free(a
->bio_chain
);
118 static void acpt_close_socket(BIO
*bio
)
122 c
= (BIO_ACCEPT
*)bio
->ptr
;
123 if (c
->accept_sock
!= (int)INVALID_SOCKET
) {
124 shutdown(c
->accept_sock
, 2);
125 closesocket(c
->accept_sock
);
126 c
->accept_sock
= (int)INVALID_SOCKET
;
127 bio
->num
= (int)INVALID_SOCKET
;
131 static int acpt_free(BIO
*a
)
137 data
= (BIO_ACCEPT
*)a
->ptr
;
140 acpt_close_socket(a
);
141 BIO_ACCEPT_free(data
);
149 static int acpt_state(BIO
*b
, BIO_ACCEPT
*c
)
151 BIO
*bio
= NULL
, *dbio
;
152 int s
= -1, ret
= -1;
157 if (c
->param_addr
== NULL
&& c
->param_serv
== NULL
) {
158 BIOerr(BIO_F_ACPT_STATE
, BIO_R_NO_ACCEPT_ADDR_OR_SERVICE_SPECIFIED
);
159 ERR_add_error_data(4,
160 "hostname=", c
->param_addr
,
161 " service=", c
->param_serv
);
165 /* Because we're starting a new bind, any cached name and serv
166 * are now obsolete and need to be cleaned out.
167 * QUESTION: should this be done in acpt_close_socket() instead?
169 OPENSSL_free(c
->cache_accepting_name
);
170 c
->cache_accepting_name
= NULL
;
171 OPENSSL_free(c
->cache_accepting_serv
);
172 c
->cache_accepting_serv
= NULL
;
173 OPENSSL_free(c
->cache_peer_name
);
174 c
->cache_peer_name
= NULL
;
175 OPENSSL_free(c
->cache_peer_serv
);
176 c
->cache_peer_serv
= NULL
;
178 c
->state
= ACPT_S_GET_ADDR
;
181 case ACPT_S_GET_ADDR
:
183 int family
= AF_UNSPEC
;
184 switch (c
->accept_family
) {
185 case BIO_FAMILY_IPV6
:
186 if (1) { /* This is a trick we use to avoid bit rot.
187 * at least the "else" part will always be
194 BIOerr(BIO_F_ACPT_STATE
, BIO_R_UNAVAILABLE_IP_FAMILY
);
198 case BIO_FAMILY_IPV4
:
201 case BIO_FAMILY_IPANY
:
205 BIOerr(BIO_F_ACPT_STATE
, BIO_R_UNSUPPORTED_IP_FAMILY
);
208 if (BIO_lookup(c
->param_addr
, c
->param_serv
, BIO_LOOKUP_SERVER
,
209 family
, SOCK_STREAM
, &c
->addr_first
) == 0)
212 if (c
->addr_first
== NULL
) {
213 BIOerr(BIO_F_ACPT_STATE
, BIO_R_LOOKUP_RETURNED_NOTHING
);
216 /* We're currently not iterating, but set this as preparation
217 * for possible future development in that regard
219 c
->addr_iter
= c
->addr_first
;
220 c
->state
= ACPT_S_CREATE_SOCKET
;
223 case ACPT_S_CREATE_SOCKET
:
224 ret
= BIO_socket(BIO_ADDRINFO_family(c
->addr_iter
),
225 BIO_ADDRINFO_socktype(c
->addr_iter
),
226 BIO_ADDRINFO_protocol(c
->addr_iter
), 0);
227 if (ret
== (int)INVALID_SOCKET
) {
228 SYSerr(SYS_F_SOCKET
, get_last_socket_error());
229 ERR_add_error_data(4,
230 "hostname=", c
->param_addr
,
231 " service=", c
->param_serv
);
232 BIOerr(BIO_F_ACPT_STATE
, BIO_R_UNABLE_TO_CREATE_SOCKET
);
235 c
->accept_sock
= ret
;
237 c
->state
= ACPT_S_LISTEN
;
242 if (!BIO_listen(c
->accept_sock
,
243 BIO_ADDRINFO_address(c
->addr_iter
),
245 BIO_closesocket(c
->accept_sock
);
251 union BIO_sock_info_u info
;
253 info
.addr
= &c
->cache_accepting_addr
;
254 if (!BIO_sock_info(c
->accept_sock
, BIO_SOCK_INFO_ADDRESS
,
256 BIO_closesocket(c
->accept_sock
);
261 c
->cache_accepting_name
=
262 BIO_ADDR_hostname_string(&c
->cache_accepting_addr
, 1);
263 c
->cache_accepting_serv
=
264 BIO_ADDR_service_string(&c
->cache_accepting_addr
, 1);
265 c
->state
= ACPT_S_ACCEPT
;
271 if (b
->next_bio
!= NULL
) {
272 c
->state
= ACPT_S_OK
;
275 BIO_clear_retry_flags(b
);
278 OPENSSL_free(c
->cache_peer_name
);
279 c
->cache_peer_name
= NULL
;
280 OPENSSL_free(c
->cache_peer_serv
);
281 c
->cache_peer_serv
= NULL
;
283 s
= BIO_accept_ex(c
->accept_sock
, &c
->cache_peer_addr
,
286 /* If the returned socket is invalid, this might still be
290 if (BIO_sock_should_retry(s
)) {
291 BIO_set_retry_special(b
);
292 b
->retry_reason
= BIO_RR_ACCEPT
;
297 /* If it wasn't retryable, we fail */
303 bio
= BIO_new_socket(s
, BIO_CLOSE
);
307 BIO_set_callback(bio
, BIO_get_callback(b
));
308 BIO_set_callback_arg(bio
, BIO_get_callback_arg(b
));
311 * If the accept BIO has an bio_chain, we dup it and put the new
314 if (c
->bio_chain
!= NULL
) {
315 if ((dbio
= BIO_dup_chain(c
->bio_chain
)) == NULL
)
317 if (!BIO_push(dbio
, bio
))
321 if (BIO_push(b
, bio
) == NULL
)
325 BIO_ADDR_hostname_string(&c
->cache_peer_addr
, 1);
327 BIO_ADDR_service_string(&c
->cache_peer_addr
, 1);
328 c
->state
= ACPT_S_OK
;
334 if (b
->next_bio
== NULL
) {
335 c
->state
= ACPT_S_ACCEPT
;
356 static int acpt_read(BIO
*b
, char *out
, int outl
)
361 BIO_clear_retry_flags(b
);
362 data
= (BIO_ACCEPT
*)b
->ptr
;
364 while (b
->next_bio
== NULL
) {
365 ret
= acpt_state(b
, data
);
370 ret
= BIO_read(b
->next_bio
, out
, outl
);
371 BIO_copy_next_retry(b
);
375 static int acpt_write(BIO
*b
, const char *in
, int inl
)
380 BIO_clear_retry_flags(b
);
381 data
= (BIO_ACCEPT
*)b
->ptr
;
383 while (b
->next_bio
== NULL
) {
384 ret
= acpt_state(b
, data
);
389 ret
= BIO_write(b
->next_bio
, in
, inl
);
390 BIO_copy_next_retry(b
);
394 static long acpt_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
401 data
= (BIO_ACCEPT
*)b
->ptr
;
406 data
->state
= ACPT_S_BEFORE
;
407 acpt_close_socket(b
);
408 BIO_ADDRINFO_free(data
->addr_first
);
409 data
->addr_first
= NULL
;
412 case BIO_C_DO_STATE_MACHINE
:
413 /* use this one to start the connection */
414 ret
= (long)acpt_state(b
, data
);
416 case BIO_C_SET_ACCEPT
:
419 char *hold_serv
= data
->param_serv
;
420 /* We affect the hostname regardless. However, the input
421 * string might contain a host:service spec, so we must
422 * parse it, which might or might not affect the service
424 OPENSSL_free(data
->param_addr
);
425 data
->param_addr
= NULL
;
426 ret
= BIO_parse_hostserv(ptr
,
429 BIO_PARSE_PRIO_SERV
);
430 if (hold_serv
!= data
->param_serv
)
431 OPENSSL_free(hold_serv
);
433 } else if (num
== 1) {
434 OPENSSL_free(data
->param_serv
);
435 data
->param_serv
= BUF_strdup(ptr
);
437 } else if (num
== 2) {
438 data
->bind_mode
|= BIO_SOCK_NONBLOCK
;
439 } else if (num
== 3) {
440 BIO_free(data
->bio_chain
);
441 data
->bio_chain
= (BIO
*)ptr
;
442 } else if (num
== 4) {
443 data
->accept_family
= *(int *)ptr
;
447 data
->bind_mode
&= ~BIO_SOCK_NONBLOCK
;
453 data
->accepted_mode
|= BIO_SOCK_NONBLOCK
;
455 data
->accepted_mode
&= ~BIO_SOCK_NONBLOCK
;
458 b
->num
= *((int *)ptr
);
459 data
->accept_sock
= b
->num
;
460 data
->state
= ACPT_S_ACCEPT
;
461 b
->shutdown
= (int)num
;
468 *ip
= data
->accept_sock
;
469 ret
= data
->accept_sock
;
473 case BIO_C_GET_ACCEPT
:
475 if (num
== 0 && ptr
!= NULL
) {
477 *pp
= data
->cache_accepting_name
;
478 } else if (num
== 1 && ptr
!= NULL
) {
480 *pp
= data
->cache_accepting_serv
;
481 } else if (num
== 2 && ptr
!= NULL
) {
483 *pp
= data
->cache_peer_name
;
484 } else if (num
== 3 && ptr
!= NULL
) {
486 *pp
= data
->cache_peer_serv
;
487 } else if (num
== 4) {
488 switch (BIO_ADDRINFO_family(data
->addr_iter
)) {
491 ret
= BIO_FAMILY_IPV6
;
495 ret
= BIO_FAMILY_IPV4
;
498 ret
= data
->accept_family
;
509 case BIO_CTRL_GET_CLOSE
:
512 case BIO_CTRL_SET_CLOSE
:
513 b
->shutdown
= (int)num
;
515 case BIO_CTRL_PENDING
:
516 case BIO_CTRL_WPENDING
:
521 case BIO_C_SET_BIND_MODE
:
522 data
->bind_mode
= (int)num
;
524 case BIO_C_GET_BIND_MODE
:
525 ret
= (long)data
->bind_mode
;
537 static int acpt_puts(BIO
*bp
, const char *str
)
542 ret
= acpt_write(bp
, str
, n
);
546 BIO
*BIO_new_accept(const char *str
)
550 ret
= BIO_new(BIO_s_accept());
553 if (BIO_set_accept_name(ret
, str
))