2 * Copyright 1995-2020 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
13 #include "bio_local.h"
15 #ifndef OPENSSL_NO_SOCK
17 typedef struct bio_connect_st {
24 BIO_ADDRINFO *addr_first;
25 const BIO_ADDRINFO *addr_iter;
27 * int socket; this will be kept in bio->num so that it is compatible
28 * with the bss_sock bio
31 * called when the connection is initially made callback(BIO,state,ret);
32 * The callback should return 'ret'. state is for compatibility with the
35 BIO_info_cb *info_callback;
38 static int conn_write(BIO *h, const char *buf, int num);
39 static int conn_read(BIO *h, char *buf, int size);
40 static int conn_puts(BIO *h, const char *str);
41 static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
42 static int conn_new(BIO *h);
43 static int conn_free(BIO *data);
44 static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
46 static int conn_state(BIO *b, BIO_CONNECT *c);
47 static void conn_close_socket(BIO *data);
48 BIO_CONNECT *BIO_CONNECT_new(void);
49 void BIO_CONNECT_free(BIO_CONNECT *a);
51 #define BIO_CONN_S_BEFORE 1
52 #define BIO_CONN_S_GET_ADDR 2
53 #define BIO_CONN_S_CREATE_SOCKET 3
54 #define BIO_CONN_S_CONNECT 4
55 #define BIO_CONN_S_OK 5
56 #define BIO_CONN_S_BLOCKED_CONNECT 6
57 #define BIO_CONN_S_CONNECT_ERROR 7
59 static const BIO_METHOD methods_connectp = {
62 /* TODO: Convert to new style write function */
65 /* TODO: Convert to new style read function */
69 NULL, /* conn_gets, */
76 static int conn_state(BIO *b, BIO_CONNECT *c)
79 BIO_info_cb *cb = NULL;
81 if (c->info_callback != NULL)
82 cb = c->info_callback;
86 case BIO_CONN_S_BEFORE:
87 if (c->param_hostname == NULL && c->param_service == NULL) {
88 BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED);
90 "hostname=", c->param_hostname,
91 " service=", c->param_service);
94 c->state = BIO_CONN_S_GET_ADDR;
97 case BIO_CONN_S_GET_ADDR:
99 int family = AF_UNSPEC;
100 switch (c->connect_family) {
101 case BIO_FAMILY_IPV6:
102 if (1) { /* This is a trick we use to avoid bit rot.
103 * at least the "else" part will always be
110 BIOerr(BIO_F_CONN_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);
114 case BIO_FAMILY_IPV4:
117 case BIO_FAMILY_IPANY:
121 BIOerr(BIO_F_CONN_STATE, BIO_R_UNSUPPORTED_IP_FAMILY);
124 if (BIO_lookup(c->param_hostname, c->param_service,
126 family, SOCK_STREAM, &c->addr_first) == 0)
129 if (c->addr_first == NULL) {
130 BIOerr(BIO_F_CONN_STATE, BIO_R_LOOKUP_RETURNED_NOTHING);
133 c->addr_iter = c->addr_first;
134 c->state = BIO_CONN_S_CREATE_SOCKET;
137 case BIO_CONN_S_CREATE_SOCKET:
138 ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
139 BIO_ADDRINFO_socktype(c->addr_iter),
140 BIO_ADDRINFO_protocol(c->addr_iter), 0);
141 if (ret == (int)INVALID_SOCKET) {
142 SYSerr(SYS_F_SOCKET, get_last_socket_error());
143 ERR_add_error_data(4,
144 "hostname=", c->param_hostname,
145 " service=", c->param_service);
146 BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
150 c->state = BIO_CONN_S_CONNECT;
153 case BIO_CONN_S_CONNECT:
154 BIO_clear_retry_flags(b);
155 ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
156 BIO_SOCK_KEEPALIVE | c->connect_mode);
159 if (BIO_sock_should_retry(ret)) {
160 BIO_set_retry_special(b);
161 c->state = BIO_CONN_S_BLOCKED_CONNECT;
162 b->retry_reason = BIO_RR_CONNECT;
164 } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
167 * if there are more addresses to try, do that first
169 BIO_closesocket(b->num);
170 c->state = BIO_CONN_S_CREATE_SOCKET;
174 SYSerr(SYS_F_CONNECT, get_last_socket_error());
175 ERR_add_error_data(4,
176 "hostname=", c->param_hostname,
177 " service=", c->param_service);
178 c->state = BIO_CONN_S_CONNECT_ERROR;
183 c->state = BIO_CONN_S_OK;
187 case BIO_CONN_S_BLOCKED_CONNECT:
188 i = BIO_sock_error(b->num);
190 BIO_clear_retry_flags(b);
191 if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) {
193 * if there are more addresses to try, do that first
195 BIO_closesocket(b->num);
196 c->state = BIO_CONN_S_CREATE_SOCKET;
200 SYSerr(SYS_F_CONNECT, i);
201 ERR_add_error_data(4,
202 "hostname=", c->param_hostname,
203 " service=", c->param_service);
204 BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);
208 c->state = BIO_CONN_S_OK;
211 case BIO_CONN_S_CONNECT_ERROR:
212 BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);
225 if ((ret = cb((BIO *)b, c->state, ret)) == 0)
230 /* Loop does not exit */
233 ret = cb((BIO *)b, c->state, ret);
238 BIO_CONNECT *BIO_CONNECT_new(void)
242 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
243 BIOerr(BIO_F_BIO_CONNECT_NEW, ERR_R_MALLOC_FAILURE);
246 ret->state = BIO_CONN_S_BEFORE;
247 ret->connect_family = BIO_FAMILY_IPANY;
251 void BIO_CONNECT_free(BIO_CONNECT *a)
255 OPENSSL_free(a->param_hostname);
256 OPENSSL_free(a->param_service);
257 BIO_ADDRINFO_free(a->addr_first);
261 const BIO_METHOD *BIO_s_connect(void)
263 return &methods_connectp;
266 static int conn_new(BIO *bi)
269 bi->num = (int)INVALID_SOCKET;
271 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
277 static void conn_close_socket(BIO *bio)
281 c = (BIO_CONNECT *)bio->ptr;
282 if (bio->num != (int)INVALID_SOCKET) {
283 /* Only do a shutdown if things were established */
284 if (c->state == BIO_CONN_S_OK)
285 shutdown(bio->num, 2);
286 BIO_closesocket(bio->num);
287 bio->num = (int)INVALID_SOCKET;
291 static int conn_free(BIO *a)
297 data = (BIO_CONNECT *)a->ptr;
300 conn_close_socket(a);
301 BIO_CONNECT_free(data);
309 static int conn_read(BIO *b, char *out, int outl)
314 data = (BIO_CONNECT *)b->ptr;
315 if (data->state != BIO_CONN_S_OK) {
316 ret = conn_state(b, data);
322 clear_socket_error();
323 ret = readsocket(b->num, out, outl);
324 BIO_clear_retry_flags(b);
326 if (BIO_sock_should_retry(ret))
327 BIO_set_retry_read(b);
329 b->flags |= BIO_FLAGS_IN_EOF;
335 static int conn_write(BIO *b, const char *in, int inl)
340 data = (BIO_CONNECT *)b->ptr;
341 if (data->state != BIO_CONN_S_OK) {
342 ret = conn_state(b, data);
347 clear_socket_error();
348 ret = writesocket(b->num, in, inl);
349 BIO_clear_retry_flags(b);
351 if (BIO_sock_should_retry(ret))
352 BIO_set_retry_write(b);
357 static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
361 const char **pptr = NULL;
365 data = (BIO_CONNECT *)b->ptr;
370 data->state = BIO_CONN_S_BEFORE;
371 conn_close_socket(b);
372 BIO_ADDRINFO_free(data->addr_first);
373 data->addr_first = NULL;
376 case BIO_C_DO_STATE_MACHINE:
377 /* use this one to start the connection */
378 if (data->state != BIO_CONN_S_OK)
379 ret = (long)conn_state(b, data);
383 case BIO_C_GET_CONNECT:
385 pptr = (const char **)ptr;
387 *pptr = data->param_hostname;
388 } else if (num == 1) {
389 *pptr = data->param_service;
390 } else if (num == 2) {
391 *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
392 } else if (num == 3) {
393 switch (BIO_ADDRINFO_family(data->addr_iter)) {
396 ret = BIO_FAMILY_IPV6;
400 ret = BIO_FAMILY_IPV4;
403 ret = data->connect_family;
416 case BIO_C_SET_CONNECT:
419 if (num == 0) { /* BIO_set_conn_hostname */
420 char *hold_service = data->param_service;
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
426 OPENSSL_free(data->param_hostname);
427 data->param_hostname = NULL;
428 ret = BIO_parse_hostserv(ptr,
429 &data->param_hostname,
430 &data->param_service,
431 BIO_PARSE_PRIO_HOST);
432 if (hold_service != data->param_service)
433 OPENSSL_free(hold_service);
434 } else if (num == 1) { /* BIO_set_conn_port */
435 OPENSSL_free(data->param_service);
436 if ((data->param_service = OPENSSL_strdup(ptr)) == NULL)
438 } else if (num == 2) { /* BIO_set_conn_address */
439 const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
440 char *host = BIO_ADDR_hostname_string(addr, 1);
441 char *service = BIO_ADDR_service_string(addr, 1);
443 ret = host != NULL && service != NULL;
445 OPENSSL_free(data->param_hostname);
446 data->param_hostname = host;
447 OPENSSL_free(data->param_service);
448 data->param_service = service;
449 BIO_ADDRINFO_free(data->addr_first);
450 data->addr_first = NULL;
451 data->addr_iter = NULL;
454 OPENSSL_free(service);
456 } else if (num == 3) { /* BIO_set_conn_ip_family */
457 data->connect_family = *(int *)ptr;
465 data->connect_mode |= BIO_SOCK_NONBLOCK;
467 data->connect_mode &= ~BIO_SOCK_NONBLOCK;
469 case BIO_C_SET_CONNECT_MODE:
470 data->connect_mode = (int)num;
481 case BIO_CTRL_GET_CLOSE:
484 case BIO_CTRL_SET_CLOSE:
485 b->shutdown = (int)num;
487 case BIO_CTRL_PENDING:
488 case BIO_CTRL_WPENDING:
496 if (data->param_hostname)
497 BIO_set_conn_hostname(dbio, data->param_hostname);
498 if (data->param_service)
499 BIO_set_conn_port(dbio, data->param_service);
500 BIO_set_conn_ip_family(dbio, data->connect_family);
501 BIO_set_conn_mode(dbio, data->connect_mode);
503 * FIXME: the cast of the function seems unlikely to be a good
506 (void)BIO_set_info_callback(dbio, data->info_callback);
509 case BIO_CTRL_SET_CALLBACK:
510 ret = 0; /* use callback ctrl */
512 case BIO_CTRL_GET_CALLBACK:
516 fptr = (BIO_info_cb **)ptr;
517 *fptr = data->info_callback;
521 ret = (b->flags & BIO_FLAGS_IN_EOF) != 0 ? 1 : 0;
530 static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
535 data = (BIO_CONNECT *)b->ptr;
538 case BIO_CTRL_SET_CALLBACK:
540 data->info_callback = fp;
550 static int conn_puts(BIO *bp, const char *str)
555 ret = conn_write(bp, str, n);
559 BIO *BIO_new_connect(const char *str)
563 ret = BIO_new(BIO_s_connect());
566 if (BIO_set_conn_hostname(ret, str))