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
66 # if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
67 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
71 typedef struct bio_connect_st {
78 struct sockaddr_in them;
80 * int socket; this will be kept in bio->num so that it is compatible
81 * with the bss_sock bio
84 * called when the connection is initially made callback(BIO,state,ret);
85 * The callback should return 'ret'. state is for compatibility with the
88 int (*info_callback) (const BIO *bio, int state, int ret);
91 static int conn_write(BIO *h, const char *buf, int num);
92 static int conn_read(BIO *h, char *buf, int size);
93 static int conn_puts(BIO *h, const char *str);
94 static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
95 static int conn_new(BIO *h);
96 static int conn_free(BIO *data);
97 static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
99 static int conn_state(BIO *b, BIO_CONNECT *c);
100 static void conn_close_socket(BIO *data);
101 BIO_CONNECT *BIO_CONNECT_new(void);
102 void BIO_CONNECT_free(BIO_CONNECT *a);
104 static BIO_METHOD methods_connectp = {
110 NULL, /* connect_gets, */
117 static int conn_state(BIO *b, BIO_CONNECT *c)
122 int (*cb) (const BIO *, int, int) = NULL;
124 if (c->info_callback != NULL)
125 cb = c->info_callback;
129 case BIO_CONN_S_BEFORE:
130 p = c->param_hostname;
132 BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_SPECIFIED);
135 for (; *p != '\0'; p++) {
136 if ((*p == ':') || (*p == '/'))
141 if ((i == ':') || (i == '/')) {
150 OPENSSL_free(c->param_port);
151 c->param_port = OPENSSL_strdup(p);
155 if (c->param_port == NULL) {
156 BIOerr(BIO_F_CONN_STATE, BIO_R_NO_PORT_SPECIFIED);
157 ERR_add_error_data(2, "host=", c->param_hostname);
160 c->state = BIO_CONN_S_GET_IP;
163 case BIO_CONN_S_GET_IP:
164 if (BIO_get_host_ip(c->param_hostname, &(c->ip[0])) <= 0)
166 c->state = BIO_CONN_S_GET_PORT;
169 case BIO_CONN_S_GET_PORT:
170 if (c->param_port == NULL) {
173 } else if (BIO_get_port(c->param_port, &c->port) <= 0)
175 c->state = BIO_CONN_S_CREATE_SOCKET;
178 case BIO_CONN_S_CREATE_SOCKET:
179 /* now setup address */
180 memset(&c->them, 0, sizeof(c->them));
181 c->them.sin_family = AF_INET;
182 c->them.sin_port = htons((unsigned short)c->port);
184 ((unsigned long)c->ip[0] << 24L) |
185 ((unsigned long)c->ip[1] << 16L) |
186 ((unsigned long)c->ip[2] << 8L) | ((unsigned long)c->ip[3]);
187 c->them.sin_addr.s_addr = htonl(l);
188 c->state = BIO_CONN_S_CREATE_SOCKET;
190 ret = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
191 if (ret == (int)INVALID_SOCKET) {
192 SYSerr(SYS_F_SOCKET, get_last_socket_error());
193 ERR_add_error_data(4, "host=", c->param_hostname,
195 BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
199 c->state = BIO_CONN_S_NBIO;
202 case BIO_CONN_S_NBIO:
204 if (!BIO_socket_nbio(b->num, 1)) {
205 BIOerr(BIO_F_CONN_STATE, BIO_R_ERROR_SETTING_NBIO);
206 ERR_add_error_data(4, "host=",
207 c->param_hostname, ":", c->param_port);
211 c->state = BIO_CONN_S_CONNECT;
213 # if defined(SO_KEEPALIVE)
215 i = setsockopt(b->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
218 SYSerr(SYS_F_SOCKET, get_last_socket_error());
219 ERR_add_error_data(4, "host=", c->param_hostname,
221 BIOerr(BIO_F_CONN_STATE, BIO_R_KEEPALIVE);
227 case BIO_CONN_S_CONNECT:
228 BIO_clear_retry_flags(b);
229 ret = connect(b->num,
230 (struct sockaddr *)&c->them, sizeof(c->them));
233 if (BIO_sock_should_retry(ret)) {
234 BIO_set_retry_special(b);
235 c->state = BIO_CONN_S_BLOCKED_CONNECT;
236 b->retry_reason = BIO_RR_CONNECT;
238 SYSerr(SYS_F_CONNECT, get_last_socket_error());
239 ERR_add_error_data(4, "host=",
240 c->param_hostname, ":", c->param_port);
241 BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);
245 c->state = BIO_CONN_S_OK;
248 case BIO_CONN_S_BLOCKED_CONNECT:
249 i = BIO_sock_error(b->num);
251 BIO_clear_retry_flags(b);
252 SYSerr(SYS_F_CONNECT, i);
253 ERR_add_error_data(4, "host=",
254 c->param_hostname, ":", c->param_port);
255 BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);
259 c->state = BIO_CONN_S_OK;
271 if ((ret = cb((BIO *)b, c->state, ret)) == 0)
276 /* Loop does not exit */
279 ret = cb((BIO *)b, c->state, ret);
284 BIO_CONNECT *BIO_CONNECT_new(void)
288 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
290 ret->state = BIO_CONN_S_BEFORE;
291 ret->param_hostname = NULL;
292 ret->param_port = NULL;
293 ret->info_callback = NULL;
297 void BIO_CONNECT_free(BIO_CONNECT *a)
302 OPENSSL_free(a->param_hostname);
303 OPENSSL_free(a->param_port);
307 BIO_METHOD *BIO_s_connect(void)
309 return (&methods_connectp);
312 static int conn_new(BIO *bi)
315 bi->num = (int)INVALID_SOCKET;
317 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
323 static void conn_close_socket(BIO *bio)
327 c = (BIO_CONNECT *)bio->ptr;
328 if (bio->num != (int)INVALID_SOCKET) {
329 /* Only do a shutdown if things were established */
330 if (c->state == BIO_CONN_S_OK)
331 shutdown(bio->num, 2);
332 closesocket(bio->num);
333 bio->num = (int)INVALID_SOCKET;
337 static int conn_free(BIO *a)
343 data = (BIO_CONNECT *)a->ptr;
346 conn_close_socket(a);
347 BIO_CONNECT_free(data);
355 static int conn_read(BIO *b, char *out, int outl)
360 data = (BIO_CONNECT *)b->ptr;
361 if (data->state != BIO_CONN_S_OK) {
362 ret = conn_state(b, data);
368 clear_socket_error();
369 ret = readsocket(b->num, out, outl);
370 BIO_clear_retry_flags(b);
372 if (BIO_sock_should_retry(ret))
373 BIO_set_retry_read(b);
379 static int conn_write(BIO *b, const char *in, int inl)
384 data = (BIO_CONNECT *)b->ptr;
385 if (data->state != BIO_CONN_S_OK) {
386 ret = conn_state(b, data);
391 clear_socket_error();
392 ret = writesocket(b->num, in, inl);
393 BIO_clear_retry_flags(b);
395 if (BIO_sock_should_retry(ret))
396 BIO_set_retry_write(b);
401 static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
405 const char **pptr = NULL;
409 data = (BIO_CONNECT *)b->ptr;
414 data->state = BIO_CONN_S_BEFORE;
415 conn_close_socket(b);
418 case BIO_C_DO_STATE_MACHINE:
419 /* use this one to start the connection */
420 if (data->state != BIO_CONN_S_OK)
421 ret = (long)conn_state(b, data);
425 case BIO_C_GET_CONNECT:
427 pptr = (const char **)ptr;
434 *pptr = data->param_hostname;
435 } else if (num == 1) {
436 *pptr = data->param_port;
437 } else if (num == 2) {
438 *pptr = (char *)&(data->ip[0]);
448 *pptr = "not initialized";
452 case BIO_C_SET_CONNECT:
456 OPENSSL_free(data->param_hostname);
457 data->param_hostname = OPENSSL_strdup(ptr);
458 } else if (num == 1) {
459 OPENSSL_free(data->param_port);
460 data->param_port = OPENSSL_strdup(ptr);
461 } else if (num == 2) {
463 unsigned char *p = ptr;
465 BIO_snprintf(buf, sizeof buf, "%d.%d.%d.%d",
466 p[0], p[1], p[2], p[3]);
467 OPENSSL_free(data->param_hostname);
468 data->param_hostname = OPENSSL_strdup(buf);
469 memcpy(&(data->ip[0]), ptr, 4);
470 } else if (num == 3) {
471 char buf[DECIMAL_SIZE(int) + 1];
473 BIO_snprintf(buf, sizeof buf, "%d", *(int *)ptr);
474 OPENSSL_free(data->param_port);
475 data->param_port = OPENSSL_strdup(buf);
476 data->port = *(int *)ptr;
481 data->nbio = (int)num;
492 case BIO_CTRL_GET_CLOSE:
495 case BIO_CTRL_SET_CLOSE:
496 b->shutdown = (int)num;
498 case BIO_CTRL_PENDING:
499 case BIO_CTRL_WPENDING:
507 if (data->param_port)
508 BIO_set_conn_port(dbio, data->param_port);
509 if (data->param_hostname)
510 BIO_set_conn_hostname(dbio, data->param_hostname);
511 BIO_set_nbio(dbio, data->nbio);
513 * FIXME: the cast of the function seems unlikely to be a good
516 (void)BIO_set_info_callback(dbio,
517 (bio_info_cb *)data->info_callback);
520 case BIO_CTRL_SET_CALLBACK:
522 # if 0 /* FIXME: Should this be used? -- Richard
524 BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
531 case BIO_CTRL_GET_CALLBACK:
533 int (**fptr) (const BIO *bio, int state, int xret);
535 fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
536 *fptr = data->info_callback;
546 static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
551 data = (BIO_CONNECT *)b->ptr;
554 case BIO_CTRL_SET_CALLBACK:
556 data->info_callback =
557 (int (*)(const struct bio_st *, int, int))fp;
567 static int conn_puts(BIO *bp, const char *str)
572 ret = conn_write(bp, str, n);
576 BIO *BIO_new_connect(const char *str)
580 ret = BIO_new(BIO_s_connect());
583 if (BIO_set_conn_hostname(ret, str))