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.]
61 #include "internal/cryptlib.h"
62 #include <openssl/bio.h>
64 #ifndef OPENSSL_NO_SOCK
67 * We are currently using deprecated functions here, and GCC warns
68 * us about them, but since we know, we don't want to hear it.
70 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
72 # if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
73 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
77 typedef struct bio_connect_st {
84 struct sockaddr_in them;
86 * int socket; this will be kept in bio->num so that it is compatible
87 * with the bss_sock bio
90 * called when the connection is initially made callback(BIO,state,ret);
91 * The callback should return 'ret'. state is for compatibility with the
94 int (*info_callback) (const BIO *bio, int state, int ret);
97 static int conn_write(BIO *h, const char *buf, int num);
98 static int conn_read(BIO *h, char *buf, int size);
99 static int conn_puts(BIO *h, const char *str);
100 static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
101 static int conn_new(BIO *h);
102 static int conn_free(BIO *data);
103 static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
105 static int conn_state(BIO *b, BIO_CONNECT *c);
106 static void conn_close_socket(BIO *data);
107 BIO_CONNECT *BIO_CONNECT_new(void);
108 void BIO_CONNECT_free(BIO_CONNECT *a);
110 static BIO_METHOD methods_connectp = {
116 NULL, /* connect_gets, */
123 static int conn_state(BIO *b, BIO_CONNECT *c)
128 int (*cb) (const BIO *, int, int) = NULL;
130 if (c->info_callback != NULL)
131 cb = c->info_callback;
135 case BIO_CONN_S_BEFORE:
136 p = c->param_hostname;
138 BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_SPECIFIED);
141 for (; *p != '\0'; p++) {
142 if ((*p == ':') || (*p == '/'))
147 if ((i == ':') || (i == '/')) {
156 OPENSSL_free(c->param_port);
157 c->param_port = OPENSSL_strdup(p);
161 if (c->param_port == NULL) {
162 BIOerr(BIO_F_CONN_STATE, BIO_R_NO_PORT_SPECIFIED);
163 ERR_add_error_data(2, "host=", c->param_hostname);
166 c->state = BIO_CONN_S_GET_IP;
169 case BIO_CONN_S_GET_IP:
170 if (BIO_get_host_ip(c->param_hostname, &(c->ip[0])) <= 0)
172 c->state = BIO_CONN_S_GET_PORT;
175 case BIO_CONN_S_GET_PORT:
176 if (c->param_port == NULL) {
179 } else if (BIO_get_port(c->param_port, &c->port) <= 0)
181 c->state = BIO_CONN_S_CREATE_SOCKET;
184 case BIO_CONN_S_CREATE_SOCKET:
185 /* now setup address */
186 memset(&c->them, 0, sizeof(c->them));
187 c->them.sin_family = AF_INET;
188 c->them.sin_port = htons((unsigned short)c->port);
190 ((unsigned long)c->ip[0] << 24L) |
191 ((unsigned long)c->ip[1] << 16L) |
192 ((unsigned long)c->ip[2] << 8L) | ((unsigned long)c->ip[3]);
193 c->them.sin_addr.s_addr = htonl(l);
194 c->state = BIO_CONN_S_CREATE_SOCKET;
196 ret = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
197 if (ret == (int)INVALID_SOCKET) {
198 SYSerr(SYS_F_SOCKET, get_last_socket_error());
199 ERR_add_error_data(4, "host=", c->param_hostname,
201 BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
205 c->state = BIO_CONN_S_NBIO;
208 case BIO_CONN_S_NBIO:
210 if (!BIO_socket_nbio(b->num, 1)) {
211 BIOerr(BIO_F_CONN_STATE, BIO_R_ERROR_SETTING_NBIO);
212 ERR_add_error_data(4, "host=",
213 c->param_hostname, ":", c->param_port);
217 c->state = BIO_CONN_S_CONNECT;
219 # if defined(SO_KEEPALIVE)
221 i = setsockopt(b->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
224 SYSerr(SYS_F_SOCKET, get_last_socket_error());
225 ERR_add_error_data(4, "host=", c->param_hostname,
227 BIOerr(BIO_F_CONN_STATE, BIO_R_KEEPALIVE);
233 case BIO_CONN_S_CONNECT:
234 BIO_clear_retry_flags(b);
235 ret = connect(b->num,
236 (struct sockaddr *)&c->them, sizeof(c->them));
239 if (BIO_sock_should_retry(ret)) {
240 BIO_set_retry_special(b);
241 c->state = BIO_CONN_S_BLOCKED_CONNECT;
242 b->retry_reason = BIO_RR_CONNECT;
244 SYSerr(SYS_F_CONNECT, get_last_socket_error());
245 ERR_add_error_data(4, "host=",
246 c->param_hostname, ":", c->param_port);
247 BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);
251 c->state = BIO_CONN_S_OK;
254 case BIO_CONN_S_BLOCKED_CONNECT:
255 i = BIO_sock_error(b->num);
257 BIO_clear_retry_flags(b);
258 SYSerr(SYS_F_CONNECT, i);
259 ERR_add_error_data(4, "host=",
260 c->param_hostname, ":", c->param_port);
261 BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);
265 c->state = BIO_CONN_S_OK;
277 if ((ret = cb((BIO *)b, c->state, ret)) == 0)
282 /* Loop does not exit */
285 ret = cb((BIO *)b, c->state, ret);
290 BIO_CONNECT *BIO_CONNECT_new(void)
294 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
296 ret->state = BIO_CONN_S_BEFORE;
297 ret->param_hostname = NULL;
298 ret->param_port = NULL;
299 ret->info_callback = NULL;
303 void BIO_CONNECT_free(BIO_CONNECT *a)
308 OPENSSL_free(a->param_hostname);
309 OPENSSL_free(a->param_port);
313 BIO_METHOD *BIO_s_connect(void)
315 return (&methods_connectp);
318 static int conn_new(BIO *bi)
321 bi->num = (int)INVALID_SOCKET;
323 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
329 static void conn_close_socket(BIO *bio)
333 c = (BIO_CONNECT *)bio->ptr;
334 if (bio->num != (int)INVALID_SOCKET) {
335 /* Only do a shutdown if things were established */
336 if (c->state == BIO_CONN_S_OK)
337 shutdown(bio->num, 2);
338 closesocket(bio->num);
339 bio->num = (int)INVALID_SOCKET;
343 static int conn_free(BIO *a)
349 data = (BIO_CONNECT *)a->ptr;
352 conn_close_socket(a);
353 BIO_CONNECT_free(data);
361 static int conn_read(BIO *b, char *out, int outl)
366 data = (BIO_CONNECT *)b->ptr;
367 if (data->state != BIO_CONN_S_OK) {
368 ret = conn_state(b, data);
374 clear_socket_error();
375 ret = readsocket(b->num, out, outl);
376 BIO_clear_retry_flags(b);
378 if (BIO_sock_should_retry(ret))
379 BIO_set_retry_read(b);
385 static int conn_write(BIO *b, const char *in, int inl)
390 data = (BIO_CONNECT *)b->ptr;
391 if (data->state != BIO_CONN_S_OK) {
392 ret = conn_state(b, data);
397 clear_socket_error();
398 ret = writesocket(b->num, in, inl);
399 BIO_clear_retry_flags(b);
401 if (BIO_sock_should_retry(ret))
402 BIO_set_retry_write(b);
407 static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
411 const char **pptr = NULL;
415 data = (BIO_CONNECT *)b->ptr;
420 data->state = BIO_CONN_S_BEFORE;
421 conn_close_socket(b);
424 case BIO_C_DO_STATE_MACHINE:
425 /* use this one to start the connection */
426 if (data->state != BIO_CONN_S_OK)
427 ret = (long)conn_state(b, data);
431 case BIO_C_GET_CONNECT:
433 pptr = (const char **)ptr;
440 *pptr = data->param_hostname;
441 } else if (num == 1) {
442 *pptr = data->param_port;
443 } else if (num == 2) {
444 *pptr = (char *)&(data->ip[0]);
454 *pptr = "not initialized";
458 case BIO_C_SET_CONNECT:
462 OPENSSL_free(data->param_hostname);
463 data->param_hostname = OPENSSL_strdup(ptr);
464 } else if (num == 1) {
465 OPENSSL_free(data->param_port);
466 data->param_port = OPENSSL_strdup(ptr);
467 } else if (num == 2) {
469 unsigned char *p = ptr;
471 BIO_snprintf(buf, sizeof buf, "%d.%d.%d.%d",
472 p[0], p[1], p[2], p[3]);
473 OPENSSL_free(data->param_hostname);
474 data->param_hostname = OPENSSL_strdup(buf);
475 memcpy(&(data->ip[0]), ptr, 4);
476 } else if (num == 3) {
477 char buf[DECIMAL_SIZE(int) + 1];
479 BIO_snprintf(buf, sizeof buf, "%d", *(int *)ptr);
480 OPENSSL_free(data->param_port);
481 data->param_port = OPENSSL_strdup(buf);
482 data->port = *(int *)ptr;
487 data->nbio = (int)num;
498 case BIO_CTRL_GET_CLOSE:
501 case BIO_CTRL_SET_CLOSE:
502 b->shutdown = (int)num;
504 case BIO_CTRL_PENDING:
505 case BIO_CTRL_WPENDING:
513 if (data->param_port)
514 BIO_set_conn_port(dbio, data->param_port);
515 if (data->param_hostname)
516 BIO_set_conn_hostname(dbio, data->param_hostname);
517 BIO_set_nbio(dbio, data->nbio);
519 * FIXME: the cast of the function seems unlikely to be a good
522 (void)BIO_set_info_callback(dbio,
523 (bio_info_cb *)data->info_callback);
526 case BIO_CTRL_SET_CALLBACK:
528 # if 0 /* FIXME: Should this be used? -- Richard
530 BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
537 case BIO_CTRL_GET_CALLBACK:
539 int (**fptr) (const BIO *bio, int state, int xret);
541 fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
542 *fptr = data->info_callback;
552 static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
557 data = (BIO_CONNECT *)b->ptr;
560 case BIO_CTRL_SET_CALLBACK:
562 data->info_callback =
563 (int (*)(const struct bio_st *, int, int))fp;
573 static int conn_puts(BIO *bp, const char *str)
578 ret = conn_write(bp, str, n);
582 BIO *BIO_new_connect(const char *str)
586 ret = BIO_new(BIO_s_connect());
589 if (BIO_set_conn_hostname(ret, str))