1 /* crypto/bio/bss_conn.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
63 #include <openssl/bio.h>
65 #ifndef OPENSSL_NO_SOCK
67 #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
68 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
73 typedef struct bio_connect_st
84 struct sockaddr_in them;
86 /* int socket; this will be kept in bio->num so that it is
87 * compatible with the bss_sock bio */
89 /* called when the connection is initially made
90 * callback(BIO,state,ret); The callback should return
91 * 'ret'. state is for compatibility with the ssl info_callback */
92 int (*info_callback)(const BIO *bio,int state,int ret);
95 static int conn_write(BIO *h, const char *buf, int num);
96 static int conn_read(BIO *h, char *buf, int size);
97 static int conn_puts(BIO *h, const char *str);
98 static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
99 static int conn_new(BIO *h);
100 static int conn_free(BIO *data);
101 static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
103 static int conn_state(BIO *b, BIO_CONNECT *c);
104 static void conn_close_socket(BIO *data);
105 BIO_CONNECT *BIO_CONNECT_new(void );
106 void BIO_CONNECT_free(BIO_CONNECT *a);
108 static BIO_METHOD methods_connectp=
115 NULL, /* connect_gets, */
122 static int conn_state(BIO *b, BIO_CONNECT *c)
127 int (*cb)(const BIO *,int,int)=NULL;
129 if (c->info_callback != NULL)
136 case BIO_CONN_S_BEFORE:
140 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
143 for ( ; *p != '\0'; p++)
145 if ((*p == ':') || (*p == '/')) break;
149 if ((i == ':') || (i == '/'))
161 if (c->param_port != NULL)
162 OPENSSL_free(c->param_port);
163 c->param_port=BUF_strdup(p);
167 if (c->param_port == NULL)
169 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
170 ERR_add_error_data(2,"host=",c->param_hostname);
173 c->state=BIO_CONN_S_GET_IP;
176 case BIO_CONN_S_GET_IP:
177 if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
179 c->state=BIO_CONN_S_GET_PORT;
182 case BIO_CONN_S_GET_PORT:
183 if (c->param_port == NULL)
188 else if (BIO_get_port(c->param_port,&c->port) <= 0)
190 c->state=BIO_CONN_S_CREATE_SOCKET;
193 case BIO_CONN_S_CREATE_SOCKET:
194 /* now setup address */
195 memset((char *)&c->them,0,sizeof(c->them));
196 c->them.sin_family=AF_INET;
197 c->them.sin_port=htons((unsigned short)c->port);
199 ((unsigned long)c->ip[0]<<24L)|
200 ((unsigned long)c->ip[1]<<16L)|
201 ((unsigned long)c->ip[2]<< 8L)|
202 ((unsigned long)c->ip[3]);
203 c->them.sin_addr.s_addr=htonl(l);
204 c->state=BIO_CONN_S_CREATE_SOCKET;
206 ret=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
207 if (ret == INVALID_SOCKET)
209 SYSerr(SYS_F_SOCKET,get_last_socket_error());
210 ERR_add_error_data(4,"host=",c->param_hostname,
212 BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
216 c->state=BIO_CONN_S_NBIO;
219 case BIO_CONN_S_NBIO:
222 if (!BIO_socket_nbio(b->num,1))
224 BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
225 ERR_add_error_data(4,"host=",
231 c->state=BIO_CONN_S_CONNECT;
233 #if defined(SO_KEEPALIVE)
235 i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
238 SYSerr(SYS_F_SOCKET,get_last_socket_error());
239 ERR_add_error_data(4,"host=",c->param_hostname,
241 BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
247 case BIO_CONN_S_CONNECT:
248 BIO_clear_retry_flags(b);
250 (struct sockaddr *)&c->them,
255 if (BIO_sock_should_retry(ret))
257 BIO_set_retry_special(b);
258 c->state=BIO_CONN_S_BLOCKED_CONNECT;
259 b->retry_reason=BIO_RR_CONNECT;
263 SYSerr(SYS_F_CONNECT,get_last_socket_error());
264 ERR_add_error_data(4,"host=",
267 BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
272 c->state=BIO_CONN_S_OK;
275 case BIO_CONN_S_BLOCKED_CONNECT:
276 i=BIO_sock_error(b->num);
279 BIO_clear_retry_flags(b);
280 SYSerr(SYS_F_CONNECT,i);
281 ERR_add_error_data(4,"host=",
284 BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
289 c->state=BIO_CONN_S_OK;
302 if (!(ret=cb((BIO *)b,c->state,ret)))
307 /* Loop does not exit */
310 ret=cb((BIO *)b,c->state,ret);
315 BIO_CONNECT *BIO_CONNECT_new(void)
319 if ((ret=(BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL)
321 ret->state=BIO_CONN_S_BEFORE;
322 ret->param_hostname=NULL;
323 ret->param_port=NULL;
324 ret->info_callback=NULL;
331 memset((char *)&ret->them,0,sizeof(ret->them));
335 void BIO_CONNECT_free(BIO_CONNECT *a)
340 if (a->param_hostname != NULL)
341 OPENSSL_free(a->param_hostname);
342 if (a->param_port != NULL)
343 OPENSSL_free(a->param_port);
347 BIO_METHOD *BIO_s_connect(void)
349 return(&methods_connectp);
352 static int conn_new(BIO *bi)
355 bi->num=INVALID_SOCKET;
357 if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
363 static void conn_close_socket(BIO *bio)
367 c=(BIO_CONNECT *)bio->ptr;
368 if (bio->num != INVALID_SOCKET)
370 /* Only do a shutdown if things were established */
371 if (c->state == BIO_CONN_S_OK)
372 shutdown(bio->num,2);
373 closesocket(bio->num);
374 bio->num=INVALID_SOCKET;
378 static int conn_free(BIO *a)
382 if (a == NULL) return(0);
383 data=(BIO_CONNECT *)a->ptr;
387 conn_close_socket(a);
388 BIO_CONNECT_free(data);
396 static int conn_read(BIO *b, char *out, int outl)
401 data=(BIO_CONNECT *)b->ptr;
402 if (data->state != BIO_CONN_S_OK)
404 ret=conn_state(b,data);
411 clear_socket_error();
412 ret=readsocket(b->num,out,outl);
413 BIO_clear_retry_flags(b);
416 if (BIO_sock_should_retry(ret))
417 BIO_set_retry_read(b);
423 static int conn_write(BIO *b, const char *in, int inl)
428 data=(BIO_CONNECT *)b->ptr;
429 if (data->state != BIO_CONN_S_OK)
431 ret=conn_state(b,data);
432 if (ret <= 0) return(ret);
435 clear_socket_error();
436 ret=writesocket(b->num,in,inl);
437 BIO_clear_retry_flags(b);
440 if (BIO_sock_should_retry(ret))
441 BIO_set_retry_write(b);
446 static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
454 data=(BIO_CONNECT *)b->ptr;
460 data->state=BIO_CONN_S_BEFORE;
461 conn_close_socket(b);
464 case BIO_C_DO_STATE_MACHINE:
465 /* use this one to start the connection */
466 if (data->state != BIO_CONN_S_OK)
467 ret=(long)conn_state(b,data);
471 case BIO_C_GET_CONNECT:
474 pptr=(const char **)ptr;
477 *pptr=data->param_hostname;
482 *pptr=data->param_port;
486 *pptr= (char *)&(data->ip[0]);
490 *((int *)ptr)=data->port;
492 if ((!b->init) || (ptr == NULL))
493 *pptr="not initialized";
497 case BIO_C_SET_CONNECT:
503 if (data->param_hostname != NULL)
504 OPENSSL_free(data->param_hostname);
505 data->param_hostname=BUF_strdup(ptr);
509 if (data->param_port != NULL)
510 OPENSSL_free(data->param_port);
511 data->param_port=BUF_strdup(ptr);
516 unsigned char *p = ptr;
518 BIO_snprintf(buf,sizeof buf,"%d.%d.%d.%d",
519 p[0],p[1],p[2],p[3]);
520 if (data->param_hostname != NULL)
521 OPENSSL_free(data->param_hostname);
522 data->param_hostname=BUF_strdup(buf);
523 memcpy(&(data->ip[0]),ptr,4);
527 char buf[DECIMAL_SIZE(int)+1];
529 BIO_snprintf(buf,sizeof buf,"%d",*(int *)ptr);
530 if (data->param_port != NULL)
531 OPENSSL_free(data->param_port);
532 data->param_port=BUF_strdup(buf);
533 data->port= *(int *)ptr;
551 case BIO_CTRL_GET_CLOSE:
554 case BIO_CTRL_SET_CLOSE:
555 b->shutdown=(int)num;
557 case BIO_CTRL_PENDING:
558 case BIO_CTRL_WPENDING:
566 if (data->param_port)
567 BIO_set_conn_port(dbio,data->param_port);
568 if (data->param_hostname)
569 BIO_set_conn_hostname(dbio,data->param_hostname);
570 BIO_set_nbio(dbio,data->nbio);
571 /* FIXME: the cast of the function seems unlikely to be a good idea */
572 (void)BIO_set_info_callback(dbio,(bio_info_cb *)data->info_callback);
575 case BIO_CTRL_SET_CALLBACK:
577 #if 0 /* FIXME: Should this be used? -- Richard Levitte */
578 BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
585 case BIO_CTRL_GET_CALLBACK:
587 int (**fptr)(const BIO *bio,int state,int xret);
589 fptr=(int (**)(const BIO *bio,int state,int xret))ptr;
590 *fptr=data->info_callback;
600 static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
605 data=(BIO_CONNECT *)b->ptr;
609 case BIO_CTRL_SET_CALLBACK:
611 data->info_callback=(int (*)(const struct bio_st *, int, int))fp;
621 static int conn_puts(BIO *bp, const char *str)
626 ret=conn_write(bp,str,n);
630 BIO *BIO_new_connect(const char *str)
634 ret=BIO_new(BIO_s_connect());
635 if (ret == NULL) return(NULL);
636 if (BIO_set_conn_hostname(ret,str))