1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
57 #include <openssl/bio.h>
63 #if !defined(OPENSSL_WINDOWS)
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
70 #include <openssl/buf.h>
71 #include <openssl/err.h>
72 #include <openssl/mem.h>
79 BIO_CONN_S_BLOCKED_CONNECT,
83 typedef struct bio_connect_st {
93 struct sockaddr_storage them;
94 socklen_t them_length;
96 /* the file descriptor is kept in bio->num in order to match the socket
99 /* info_callback is called when the connection is initially made
100 * callback(BIO,state,ret); The callback should return 'ret', state is for
101 * compatibility with the SSL info_callback. */
102 int (*info_callback)(const BIO *bio, int state, int ret);
105 #if !defined(OPENSSL_WINDOWS)
106 static int closesocket(int sock) {
111 /* maybe_copy_ipv4_address sets |*ipv4| to the IPv4 address from |ss| (in
112 * big-endian order), if |ss| contains an IPv4 socket address. */
113 static void maybe_copy_ipv4_address(uint8_t *ipv4,
114 const struct sockaddr_storage *ss) {
115 const struct sockaddr_in *sin;
117 if (ss->ss_family != AF_INET) {
121 sin = (const struct sockaddr_in*) ss;
122 memcpy(ipv4, &sin->sin_addr, 4);
125 static int conn_state(BIO *bio, BIO_CONNECT *c) {
128 int (*cb)(const BIO *, int, int) = NULL;
130 if (c->info_callback != NULL) {
131 cb = c->info_callback;
136 case BIO_CONN_S_BEFORE:
137 p = c->param_hostname;
139 OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_HOSTNAME_SPECIFIED);
142 for (; *p != 0; p++) {
143 if (*p == ':' || *p == '/') {
149 if (i == ':' || i == '/') {
152 for (q = p; *q; q++) {
158 if (c->param_port != NULL) {
159 OPENSSL_free(c->param_port);
161 c->param_port = BUF_strdup(p);
165 if (c->param_port == NULL) {
166 OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_PORT_SPECIFIED);
167 ERR_add_error_data(2, "host=", c->param_hostname);
171 if (!bio_ip_and_port_to_socket_and_addr(
172 &bio->num, &c->them, &c->them_length, c->param_hostname,
174 OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_UNABLE_TO_CREATE_SOCKET);
175 ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
180 maybe_copy_ipv4_address(c->ip, &c->them);
183 if (!bio_socket_nbio(bio->num, 1)) {
184 OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_ERROR_SETTING_NBIO);
185 ERR_add_error_data(4, "host=", c->param_hostname, ":",
192 ret = setsockopt(bio->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
195 OPENSSL_PUT_SYSTEM_ERROR(setsockopt);
196 OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_KEEPALIVE);
197 ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
201 BIO_clear_retry_flags(bio);
202 ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length);
204 if (bio_fd_should_retry(ret)) {
205 BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
206 c->state = BIO_CONN_S_BLOCKED_CONNECT;
207 bio->retry_reason = BIO_RR_CONNECT;
209 OPENSSL_PUT_SYSTEM_ERROR(connect);
210 OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_CONNECT_ERROR);
211 ERR_add_error_data(4, "host=", c->param_hostname, ":",
216 c->state = BIO_CONN_S_OK;
220 case BIO_CONN_S_BLOCKED_CONNECT:
221 i = bio_sock_error(bio->num);
223 if (bio_fd_should_retry(ret)) {
224 BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
225 c->state = BIO_CONN_S_BLOCKED_CONNECT;
226 bio->retry_reason = BIO_RR_CONNECT;
229 BIO_clear_retry_flags(bio);
230 OPENSSL_PUT_SYSTEM_ERROR(connect);
231 OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NBIO_CONNECT_ERROR);
232 ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
237 c->state = BIO_CONN_S_OK;
250 ret = cb((BIO *)bio, c->state, ret);
259 ret = cb((BIO *)bio, c->state, ret);
266 static BIO_CONNECT *BIO_CONNECT_new(void) {
267 BIO_CONNECT *ret = OPENSSL_malloc(sizeof(BIO_CONNECT));
272 memset(ret, 0, sizeof(BIO_CONNECT));
274 ret->state = BIO_CONN_S_BEFORE;
278 static void BIO_CONNECT_free(BIO_CONNECT *c) {
283 if (c->param_hostname != NULL) {
284 OPENSSL_free(c->param_hostname);
286 if (c->param_port != NULL) {
287 OPENSSL_free(c->param_port);
292 static int conn_new(BIO *bio) {
296 bio->ptr = (char *)BIO_CONNECT_new();
297 return bio->ptr != NULL;
300 static void conn_close_socket(BIO *bio) {
301 BIO_CONNECT *c = (BIO_CONNECT *) bio->ptr;
303 if (bio->num == -1) {
307 /* Only do a shutdown if things were established */
308 if (c->state == BIO_CONN_S_OK) {
309 shutdown(bio->num, 2);
311 closesocket(bio->num);
315 static int conn_free(BIO *bio) {
321 conn_close_socket(bio);
324 BIO_CONNECT_free((BIO_CONNECT*) bio->ptr);
329 static int conn_read(BIO *bio, char *out, int out_len) {
333 data = (BIO_CONNECT *)bio->ptr;
334 if (data->state != BIO_CONN_S_OK) {
335 ret = conn_state(bio, data);
341 bio_clear_socket_error();
342 ret = recv(bio->num, out, out_len, 0);
343 BIO_clear_retry_flags(bio);
345 if (bio_fd_should_retry(ret)) {
346 BIO_set_retry_read(bio);
353 static int conn_write(BIO *bio, const char *in, int in_len) {
357 data = (BIO_CONNECT *)bio->ptr;
358 if (data->state != BIO_CONN_S_OK) {
359 ret = conn_state(bio, data);
365 bio_clear_socket_error();
366 ret = send(bio->num, in, in_len, 0);
367 BIO_clear_retry_flags(bio);
369 if (bio_fd_should_retry(ret)) {
370 BIO_set_retry_write(bio);
377 static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) {
383 data = (BIO_CONNECT *)bio->ptr;
388 data->state = BIO_CONN_S_BEFORE;
389 conn_close_socket(bio);
392 case BIO_C_DO_STATE_MACHINE:
393 /* use this one to start the connection */
394 if (data->state != BIO_CONN_S_OK)
395 ret = (long)conn_state(bio, data);
399 case BIO_C_GET_CONNECT:
400 /* TODO(fork): can this be removed? (Or maybe this whole file). */
402 pptr = (const char **)ptr;
404 *pptr = data->param_hostname;
405 } else if (num == 1) {
406 *pptr = data->param_port;
407 } else if (num == 2) {
408 *pptr = (char *) &data->ip[0];
409 } else if (num == 3) {
410 *((int *)ptr) = data->port;
413 *pptr = "not initialized";
418 case BIO_C_SET_CONNECT:
422 if (data->param_hostname != NULL) {
423 OPENSSL_free(data->param_hostname);
425 data->param_hostname = BUF_strdup(ptr);
426 } else if (num == 1) {
427 if (data->param_port != NULL) {
428 OPENSSL_free(data->param_port);
430 data->param_port = BUF_strdup(ptr);
437 data->nbio = (int)num;
450 case BIO_CTRL_GET_CLOSE:
453 case BIO_CTRL_SET_CLOSE:
454 bio->shutdown = (int)num;
456 case BIO_CTRL_PENDING:
457 case BIO_CTRL_WPENDING:
462 case BIO_CTRL_SET_CALLBACK: {
463 #if 0 /* FIXME: Should this be used? -- Richard Levitte */
464 OPENSSL_PUT_ERROR(BIO, XXX, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
470 case BIO_CTRL_GET_CALLBACK: {
471 int (**fptr)(const BIO *bio, int state, int xret);
472 fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
473 *fptr = data->info_callback;
482 static long conn_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
486 data = (BIO_CONNECT *)bio->ptr;
489 case BIO_CTRL_SET_CALLBACK: {
490 data->info_callback = (int (*)(const struct bio_st *, int, int))fp;
499 static int conn_puts(BIO *bp, const char *str) {
500 return conn_write(bp, str, strlen(str));
503 BIO *BIO_new_connect(const char *hostname) {
506 ret = BIO_new(BIO_s_connect());
510 if (!BIO_set_conn_hostname(ret, hostname)) {
517 static const BIO_METHOD methods_connectp = {
518 BIO_TYPE_CONNECT, "socket connect", conn_write, conn_read,
519 conn_puts, NULL /* connect_gets, */, conn_ctrl, conn_new,
520 conn_free, conn_callback_ctrl,
523 const BIO_METHOD *BIO_s_connect(void) { return &methods_connectp; }
525 int BIO_set_conn_hostname(BIO *bio, const char *name) {
526 return BIO_ctrl(bio, BIO_C_SET_CONNECT, 0, (void*) name);
529 int BIO_set_conn_port(BIO *bio, const char *port_str) {
530 return BIO_ctrl(bio, BIO_C_SET_CONNECT, 1, (void*) port_str);
533 int BIO_set_nbio(BIO *bio, int on) {
534 return BIO_ctrl(bio, BIO_C_SET_NBIO, on, NULL);