]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/bss_conn.c
Replace BUF_ string function calls with OPENSSL_ ones
[thirdparty/openssl.git] / crypto / bio / bss_conn.c
CommitLineData
b1322259 1/*
c4d3c19b 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
09abbca1 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
d02b48c6
RE
10#include <stdio.h>
11#include <errno.h>
d02b48c6 12
706457b7 13#include "bio_local.h"
b00b2124 14
417be660 15#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
16
17typedef struct bio_connect_st {
18 int state;
417be660 19 int connect_family;
0f113f3e 20 char *param_hostname;
417be660
RL
21 char *param_service;
22 int connect_mode;
23
24 BIO_ADDRINFO *addr_first;
25 const BIO_ADDRINFO *addr_iter;
0f113f3e
MC
26 /*
27 * int socket; this will be kept in bio->num so that it is compatible
28 * with the bss_sock bio
29 */
30 /*
31 * called when the connection is initially made callback(BIO,state,ret);
32 * The callback should return 'ret'. state is for compatibility with the
33 * ssl info_callback
34 */
fce78bd4 35 BIO_info_cb *info_callback;
0f113f3e 36} BIO_CONNECT;
d02b48c6 37
0e1c0612
UM
38static int conn_write(BIO *h, const char *buf, int num);
39static int conn_read(BIO *h, char *buf, int size);
40static int conn_puts(BIO *h, const char *str);
41static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
d02b48c6
RE
42static int conn_new(BIO *h);
43static int conn_free(BIO *data);
fce78bd4 44static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
d02b48c6
RE
45
46static int conn_state(BIO *b, BIO_CONNECT *c);
47static void conn_close_socket(BIO *data);
0f113f3e 48BIO_CONNECT *BIO_CONNECT_new(void);
d02b48c6
RE
49void BIO_CONNECT_free(BIO_CONNECT *a);
50
417be660
RL
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
04f6b0fd 58static const BIO_METHOD methods_connectp = {
0f113f3e
MC
59 BIO_TYPE_CONNECT,
60 "socket connect",
3befffa3
MC
61 /* TODO: Convert to new style write function */
62 bwrite_conv,
0f113f3e 63 conn_write,
d07aee2c
MC
64 /* TODO: Convert to new style read function */
65 bread_conv,
0f113f3e
MC
66 conn_read,
67 conn_puts,
b4ff6622 68 NULL, /* conn_gets, */
0f113f3e
MC
69 conn_ctrl,
70 conn_new,
71 conn_free,
72 conn_callback_ctrl,
73};
d02b48c6 74
6b691a5c 75static int conn_state(BIO *b, BIO_CONNECT *c)
0f113f3e
MC
76{
77 int ret = -1, i;
fce78bd4 78 BIO_info_cb *cb = NULL;
0f113f3e
MC
79
80 if (c->info_callback != NULL)
81 cb = c->info_callback;
82
83 for (;;) {
84 switch (c->state) {
85 case BIO_CONN_S_BEFORE:
417be660
RL
86 if (c->param_hostname == NULL && c->param_service == NULL) {
87 BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED);
88 ERR_add_error_data(4,
89 "hostname=", c->param_hostname,
90 " service=", c->param_service);
0f113f3e
MC
91 goto exit_loop;
92 }
417be660
RL
93 c->state = BIO_CONN_S_GET_ADDR;
94 break;
0f113f3e 95
417be660
RL
96 case BIO_CONN_S_GET_ADDR:
97 {
98 int family = AF_UNSPEC;
99 switch (c->connect_family) {
100 case BIO_FAMILY_IPV6:
101 if (1) { /* This is a trick we use to avoid bit rot.
102 * at least the "else" part will always be
103 * compiled.
104 */
105#ifdef AF_INET6
106 family = AF_INET6;
107 } else {
108#endif
109 BIOerr(BIO_F_CONN_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);
110 goto exit_loop;
111 }
112 break;
113 case BIO_FAMILY_IPV4:
114 family = AF_INET;
115 break;
116 case BIO_FAMILY_IPANY:
117 family = AF_UNSPEC;
118 break;
119 default:
120 BIOerr(BIO_F_CONN_STATE, BIO_R_UNSUPPORTED_IP_FAMILY);
121 goto exit_loop;
0f113f3e 122 }
417be660
RL
123 if (BIO_lookup(c->param_hostname, c->param_service,
124 BIO_LOOKUP_CLIENT,
125 family, SOCK_STREAM, &c->addr_first) == 0)
126 goto exit_loop;
0f113f3e 127 }
417be660
RL
128 if (c->addr_first == NULL) {
129 BIOerr(BIO_F_CONN_STATE, BIO_R_LOOKUP_RETURNED_NOTHING);
0f113f3e
MC
130 goto exit_loop;
131 }
417be660 132 c->addr_iter = c->addr_first;
0f113f3e
MC
133 c->state = BIO_CONN_S_CREATE_SOCKET;
134 break;
135
136 case BIO_CONN_S_CREATE_SOCKET:
417be660
RL
137 ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
138 BIO_ADDRINFO_socktype(c->addr_iter),
139 BIO_ADDRINFO_protocol(c->addr_iter), 0);
b13fdc48 140 if (ret == (int)INVALID_SOCKET) {
ff988500
RS
141 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
142 "calling socket(%s, %s)",
143 c->param_hostname, c->param_service);
0f113f3e
MC
144 BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
145 goto exit_loop;
146 }
147 b->num = ret;
0f113f3e 148 c->state = BIO_CONN_S_CONNECT;
0f113f3e
MC
149 break;
150
151 case BIO_CONN_S_CONNECT:
152 BIO_clear_retry_flags(b);
417be660
RL
153 ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
154 BIO_SOCK_KEEPALIVE | c->connect_mode);
0f113f3e 155 b->retry_reason = 0;
a043d0b9 156 if (ret == 0) {
0f113f3e
MC
157 if (BIO_sock_should_retry(ret)) {
158 BIO_set_retry_special(b);
159 c->state = BIO_CONN_S_BLOCKED_CONNECT;
160 b->retry_reason = BIO_RR_CONNECT;
417be660
RL
161 ERR_clear_error();
162 } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
163 != NULL) {
164 /*
165 * if there are more addresses to try, do that first
166 */
167 BIO_closesocket(b->num);
168 c->state = BIO_CONN_S_CREATE_SOCKET;
169 ERR_clear_error();
170 break;
0f113f3e 171 } else {
ff988500
RS
172 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
173 "calling connect(%s, %s)",
174 c->param_hostname, c->param_service);
0f113f3e
MC
175 BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);
176 }
177 goto exit_loop;
417be660 178 } else {
0f113f3e 179 c->state = BIO_CONN_S_OK;
417be660 180 }
0f113f3e
MC
181 break;
182
183 case BIO_CONN_S_BLOCKED_CONNECT:
184 i = BIO_sock_error(b->num);
185 if (i) {
186 BIO_clear_retry_flags(b);
ff988500
RS
187 ERR_raise_data(ERR_LIB_SYS, i,
188 "calling connect(%s, %s)",
189 c->param_hostname, c->param_service);
0f113f3e
MC
190 BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);
191 ret = 0;
192 goto exit_loop;
193 } else
194 c->state = BIO_CONN_S_OK;
195 break;
196
197 case BIO_CONN_S_OK:
198 ret = 1;
199 goto exit_loop;
200 default:
201 /* abort(); */
202 goto exit_loop;
203 }
204
205 if (cb != NULL) {
75ebbd9a 206 if ((ret = cb((BIO *)b, c->state, ret)) == 0)
0f113f3e
MC
207 goto end;
208 }
209 }
210
211 /* Loop does not exit */
212 exit_loop:
213 if (cb != NULL)
214 ret = cb((BIO *)b, c->state, ret);
215 end:
26a7d938 216 return ret;
0f113f3e 217}
d02b48c6 218
6b691a5c 219BIO_CONNECT *BIO_CONNECT_new(void)
0f113f3e
MC
220{
221 BIO_CONNECT *ret;
222
f06080cb
F
223 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
224 BIOerr(BIO_F_BIO_CONNECT_NEW, ERR_R_MALLOC_FAILURE);
26a7d938 225 return NULL;
f06080cb 226 }
0f113f3e 227 ret->state = BIO_CONN_S_BEFORE;
417be660 228 ret->connect_family = BIO_FAMILY_IPANY;
26a7d938 229 return ret;
0f113f3e 230}
d02b48c6 231
6b691a5c 232void BIO_CONNECT_free(BIO_CONNECT *a)
0f113f3e 233{
e6e9170d
RS
234 if (a == NULL)
235 return;
b548a1f1 236 OPENSSL_free(a->param_hostname);
417be660
RL
237 OPENSSL_free(a->param_service);
238 BIO_ADDRINFO_free(a->addr_first);
0f113f3e
MC
239 OPENSSL_free(a);
240}
d02b48c6 241
04f6b0fd 242const BIO_METHOD *BIO_s_connect(void)
0f113f3e 243{
26a7d938 244 return &methods_connectp;
0f113f3e 245}
d02b48c6 246
6b691a5c 247static int conn_new(BIO *bi)
0f113f3e
MC
248{
249 bi->init = 0;
b13fdc48 250 bi->num = (int)INVALID_SOCKET;
0f113f3e
MC
251 bi->flags = 0;
252 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
26a7d938 253 return 0;
0f113f3e 254 else
208fb891 255 return 1;
0f113f3e 256}
d02b48c6 257
6b691a5c 258static void conn_close_socket(BIO *bio)
0f113f3e
MC
259{
260 BIO_CONNECT *c;
261
262 c = (BIO_CONNECT *)bio->ptr;
b13fdc48 263 if (bio->num != (int)INVALID_SOCKET) {
0f113f3e
MC
264 /* Only do a shutdown if things were established */
265 if (c->state == BIO_CONN_S_OK)
266 shutdown(bio->num, 2);
417be660 267 BIO_closesocket(bio->num);
b13fdc48 268 bio->num = (int)INVALID_SOCKET;
0f113f3e
MC
269 }
270}
d02b48c6 271
6b691a5c 272static int conn_free(BIO *a)
0f113f3e
MC
273{
274 BIO_CONNECT *data;
275
276 if (a == NULL)
26a7d938 277 return 0;
0f113f3e
MC
278 data = (BIO_CONNECT *)a->ptr;
279
280 if (a->shutdown) {
281 conn_close_socket(a);
282 BIO_CONNECT_free(data);
283 a->ptr = NULL;
284 a->flags = 0;
285 a->init = 0;
286 }
208fb891 287 return 1;
0f113f3e
MC
288}
289
6b691a5c 290static int conn_read(BIO *b, char *out, int outl)
0f113f3e
MC
291{
292 int ret = 0;
293 BIO_CONNECT *data;
294
295 data = (BIO_CONNECT *)b->ptr;
296 if (data->state != BIO_CONN_S_OK) {
297 ret = conn_state(b, data);
298 if (ret <= 0)
26a7d938 299 return ret;
0f113f3e
MC
300 }
301
302 if (out != NULL) {
303 clear_socket_error();
304 ret = readsocket(b->num, out, outl);
305 BIO_clear_retry_flags(b);
306 if (ret <= 0) {
307 if (BIO_sock_should_retry(ret))
308 BIO_set_retry_read(b);
309 }
310 }
26a7d938 311 return ret;
0f113f3e 312}
d02b48c6 313
0e1c0612 314static int conn_write(BIO *b, const char *in, int inl)
0f113f3e
MC
315{
316 int ret;
317 BIO_CONNECT *data;
318
319 data = (BIO_CONNECT *)b->ptr;
320 if (data->state != BIO_CONN_S_OK) {
321 ret = conn_state(b, data);
322 if (ret <= 0)
26a7d938 323 return ret;
0f113f3e
MC
324 }
325
326 clear_socket_error();
327 ret = writesocket(b->num, in, inl);
328 BIO_clear_retry_flags(b);
329 if (ret <= 0) {
330 if (BIO_sock_should_retry(ret))
331 BIO_set_retry_write(b);
332 }
26a7d938 333 return ret;
0f113f3e 334}
d02b48c6 335
0e1c0612 336static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
337{
338 BIO *dbio;
339 int *ip;
4b72d5f8 340 const char **pptr = NULL;
0f113f3e
MC
341 long ret = 1;
342 BIO_CONNECT *data;
343
344 data = (BIO_CONNECT *)b->ptr;
345
346 switch (cmd) {
347 case BIO_CTRL_RESET:
348 ret = 0;
349 data->state = BIO_CONN_S_BEFORE;
350 conn_close_socket(b);
417be660
RL
351 BIO_ADDRINFO_free(data->addr_first);
352 data->addr_first = NULL;
0f113f3e
MC
353 b->flags = 0;
354 break;
355 case BIO_C_DO_STATE_MACHINE:
356 /* use this one to start the connection */
357 if (data->state != BIO_CONN_S_OK)
358 ret = (long)conn_state(b, data);
359 else
360 ret = 1;
361 break;
362 case BIO_C_GET_CONNECT:
363 if (ptr != NULL) {
364 pptr = (const char **)ptr;
417be660
RL
365 if (num == 0) {
366 *pptr = data->param_hostname;
367 } else if (num == 1) {
368 *pptr = data->param_service;
369 } else if (num == 2) {
370 *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
371 } else if (num == 3) {
372 switch (BIO_ADDRINFO_family(data->addr_iter)) {
373# ifdef AF_INET6
374 case AF_INET6:
375 ret = BIO_FAMILY_IPV6;
376 break;
377# endif
378 case AF_INET:
379 ret = BIO_FAMILY_IPV4;
380 break;
381 case 0:
382 ret = data->connect_family;
383 break;
384 default:
385 ret = -1;
386 break;
4b72d5f8 387 }
417be660
RL
388 } else {
389 ret = 0;
4b72d5f8
RL
390 }
391 } else {
4b72d5f8 392 ret = 0;
0f113f3e
MC
393 }
394 break;
395 case BIO_C_SET_CONNECT:
396 if (ptr != NULL) {
397 b->init = 1;
398 if (num == 0) {
417be660
RL
399 char *hold_service = data->param_service;
400 /* We affect the hostname regardless. However, the input
401 * string might contain a host:service spec, so we must
402 * parse it, which might or might not affect the service
403 */
b548a1f1 404 OPENSSL_free(data->param_hostname);
417be660
RL
405 data->param_hostname = NULL;
406 ret = BIO_parse_hostserv(ptr,
407 &data->param_hostname,
408 &data->param_service,
409 BIO_PARSE_PRIO_HOST);
410 if (hold_service != data->param_service)
411 OPENSSL_free(hold_service);
0f113f3e 412 } else if (num == 1) {
417be660 413 OPENSSL_free(data->param_service);
3d484574 414 data->param_service = OPENSSL_strdup(ptr);
0f113f3e 415 } else if (num == 2) {
417be660
RL
416 const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
417 if (ret) {
418 data->param_hostname = BIO_ADDR_hostname_string(addr, 1);
419 data->param_service = BIO_ADDR_service_string(addr, 1);
420 BIO_ADDRINFO_free(data->addr_first);
421 data->addr_first = NULL;
422 data->addr_iter = NULL;
423 }
0f113f3e 424 } else if (num == 3) {
417be660
RL
425 data->connect_family = *(int *)ptr;
426 } else {
427 ret = 0;
0f113f3e
MC
428 }
429 }
430 break;
431 case BIO_C_SET_NBIO:
417be660
RL
432 if (num != 0)
433 data->connect_mode |= BIO_SOCK_NONBLOCK;
434 else
435 data->connect_mode &= ~BIO_SOCK_NONBLOCK;
436 break;
437 case BIO_C_SET_CONNECT_MODE:
438 data->connect_mode = (int)num;
0f113f3e
MC
439 break;
440 case BIO_C_GET_FD:
441 if (b->init) {
442 ip = (int *)ptr;
443 if (ip != NULL)
444 *ip = b->num;
445 ret = b->num;
446 } else
447 ret = -1;
448 break;
449 case BIO_CTRL_GET_CLOSE:
450 ret = b->shutdown;
451 break;
452 case BIO_CTRL_SET_CLOSE:
453 b->shutdown = (int)num;
454 break;
455 case BIO_CTRL_PENDING:
456 case BIO_CTRL_WPENDING:
457 ret = 0;
458 break;
459 case BIO_CTRL_FLUSH:
460 break;
461 case BIO_CTRL_DUP:
462 {
463 dbio = (BIO *)ptr;
0f113f3e
MC
464 if (data->param_hostname)
465 BIO_set_conn_hostname(dbio, data->param_hostname);
417be660
RL
466 if (data->param_service)
467 BIO_set_conn_port(dbio, data->param_service);
468 BIO_set_conn_ip_family(dbio, data->connect_family);
469 BIO_set_conn_mode(dbio, data->connect_mode);
0f113f3e
MC
470 /*
471 * FIXME: the cast of the function seems unlikely to be a good
472 * idea
473 */
fce78bd4 474 (void)BIO_set_info_callback(dbio, data->info_callback);
0f113f3e
MC
475 }
476 break;
477 case BIO_CTRL_SET_CALLBACK:
2722ff50 478 ret = 0; /* use callback ctrl */
0f113f3e
MC
479 break;
480 case BIO_CTRL_GET_CALLBACK:
481 {
fce78bd4 482 BIO_info_cb **fptr;
0f113f3e 483
fce78bd4 484 fptr = (BIO_info_cb **)ptr;
0f113f3e
MC
485 *fptr = data->info_callback;
486 }
487 break;
488 default:
489 ret = 0;
490 break;
491 }
26a7d938 492 return ret;
0f113f3e 493}
d02b48c6 494
fce78bd4 495static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
0f113f3e
MC
496{
497 long ret = 1;
498 BIO_CONNECT *data;
499
500 data = (BIO_CONNECT *)b->ptr;
501
502 switch (cmd) {
503 case BIO_CTRL_SET_CALLBACK:
504 {
fce78bd4 505 data->info_callback = fp;
0f113f3e
MC
506 }
507 break;
508 default:
509 ret = 0;
510 break;
511 }
26a7d938 512 return ret;
0f113f3e 513}
d3442bc7 514
0e1c0612 515static int conn_puts(BIO *bp, const char *str)
0f113f3e
MC
516{
517 int n, ret;
d02b48c6 518
0f113f3e
MC
519 n = strlen(str);
520 ret = conn_write(bp, str, n);
26a7d938 521 return ret;
0f113f3e 522}
d02b48c6 523
c45a48c1 524BIO *BIO_new_connect(const char *str)
0f113f3e
MC
525{
526 BIO *ret;
527
528 ret = BIO_new(BIO_s_connect());
529 if (ret == NULL)
26a7d938 530 return NULL;
0f113f3e 531 if (BIO_set_conn_hostname(ret, str))
26a7d938 532 return ret;
ca3a82c3 533 BIO_free(ret);
26a7d938 534 return NULL;
0f113f3e 535}
d02b48c6
RE
536
537#endif