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.]
67 /* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */
70 #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
72 #define SOCKET_PROTOCOL IPPROTO_TCP
75 typedef struct bio_connect_st
86 struct sockaddr_in them
;
88 /* int socket; this will be kept in bio->num so that it is
89 * compatable with the bss_sock bio */
91 /* called when the connection is initially made
92 * callback(BIO,state,ret); The callback should return
93 * 'ret'. state is for compatablity with the ssl info_callback */
94 int (*info_callback
)();
98 static int conn_write(BIO
*h
,char *buf
,int num
);
99 static int conn_read(BIO
*h
,char *buf
,int size
);
100 static int conn_puts(BIO
*h
,char *str
);
101 static long conn_ctrl(BIO
*h
,int cmd
,long arg1
,char *arg2
);
102 static int conn_new(BIO
*h
);
103 static int conn_free(BIO
*data
);
105 static int conn_write();
106 static int conn_read();
107 static int conn_puts();
108 static long conn_ctrl();
109 static int conn_new();
110 static int conn_free();
115 static int conn_state(BIO
*b
, BIO_CONNECT
*c
);
116 static void conn_close_socket(BIO
*data
);
117 BIO_CONNECT
*BIO_CONNECT_new(void );
118 void BIO_CONNECT_free(BIO_CONNECT
*a
);
122 static int conn_state();
123 static void conn_close_socket();
124 BIO_CONNECT
*BIO_CONNECT_new();
125 void BIO_CONNECT_free();
129 static BIO_METHOD methods_connectp
=
136 NULL
, /* connect_gets, */
142 static int conn_state(b
,c
)
151 if (c
->info_callback
!= NULL
)
158 case BIO_CONN_S_BEFORE
:
162 BIOerr(BIO_F_CONN_STATE
,BIO_R_NO_HOSTHNAME_SPECIFIED
);
165 for ( ; *p
!= '\0'; p
++)
167 if ((*p
== ':') || (*p
== '/')) break;
171 if ((i
== ':') || (i
== '/'))
183 if (c
->param_port
!= NULL
)
185 c
->param_port
=BUF_strdup(p
);
189 if (c
->param_port
== NULL
)
191 BIOerr(BIO_F_CONN_STATE
,BIO_R_NO_PORT_SPECIFIED
);
192 ERR_add_error_data(2,"host=",c
->param_hostname
);
195 c
->state
=BIO_CONN_S_GET_IP
;
198 case BIO_CONN_S_GET_IP
:
199 if (BIO_get_host_ip(c
->param_hostname
,&(c
->ip
[0])) <= 0)
201 c
->state
=BIO_CONN_S_GET_PORT
;
204 case BIO_CONN_S_GET_PORT
:
205 if (c
->param_port
== NULL
)
210 else if (BIO_get_port(c
->param_port
,&c
->port
) <= 0)
212 c
->state
=BIO_CONN_S_CREATE_SOCKET
;
215 case BIO_CONN_S_CREATE_SOCKET
:
216 /* now setup address */
217 memset((char *)&c
->them
,0,sizeof(c
->them
));
218 c
->them
.sin_family
=AF_INET
;
219 c
->them
.sin_port
=htons((unsigned short)c
->port
);
221 ((unsigned long)c
->ip
[0]<<24L)|
222 ((unsigned long)c
->ip
[1]<<16L)|
223 ((unsigned long)c
->ip
[2]<< 8L)|
224 ((unsigned long)c
->ip
[3]);
225 c
->them
.sin_addr
.s_addr
=htonl(l
);
226 c
->state
=BIO_CONN_S_CREATE_SOCKET
;
228 ret
=socket(AF_INET
,SOCK_STREAM
,SOCKET_PROTOCOL
);
229 if (ret
== INVALID_SOCKET
)
231 SYSerr(SYS_F_SOCKET
,get_last_socket_error());
232 ERR_add_error_data(4,"host=",c
->param_hostname
,
234 BIOerr(BIO_F_CONN_STATE
,BIO_R_UNABLE_TO_CREATE_SOCKET
);
238 c
->state
=BIO_CONN_S_NBIO
;
241 case BIO_CONN_S_NBIO
:
244 if (!BIO_socket_nbio(b
->num
,1))
246 BIOerr(BIO_F_CONN_STATE
,BIO_R_ERROR_SETTING_NBIO
);
247 ERR_add_error_data(4,"host=",
253 c
->state
=BIO_CONN_S_CONNECT
;
257 i
=setsockopt(b
->num
,SOL_SOCKET
,SO_KEEPALIVE
,(char *)&i
,sizeof(i
));
260 SYSerr(SYS_F_SOCKET
,get_last_socket_error());
261 ERR_add_error_data(4,"host=",c
->param_hostname
,
263 BIOerr(BIO_F_CONN_STATE
,BIO_R_KEEPALIVE
);
269 case BIO_CONN_S_CONNECT
:
270 BIO_clear_retry_flags(b
);
272 (struct sockaddr
*)&c
->them
,
277 if (BIO_sock_should_retry(ret
))
279 BIO_set_retry_special(b
);
280 c
->state
=BIO_CONN_S_BLOCKED_CONNECT
;
281 b
->retry_reason
=BIO_RR_CONNECT
;
285 SYSerr(SYS_F_CONNECT
,get_last_socket_error());
286 ERR_add_error_data(4,"host=",
289 BIOerr(BIO_F_CONN_STATE
,BIO_R_CONNECT_ERROR
);
294 c
->state
=BIO_CONN_S_OK
;
297 case BIO_CONN_S_BLOCKED_CONNECT
:
298 i
=BIO_sock_error(b
->num
);
301 BIO_clear_retry_flags(b
);
302 SYSerr(SYS_F_CONNECT
,i
);
303 ERR_add_error_data(4,"host=",
306 BIOerr(BIO_F_CONN_STATE
,BIO_R_NBIO_CONNECT_ERROR
);
311 c
->state
=BIO_CONN_S_OK
;
324 if (!(ret
=cb((BIO
*)b
,c
->state
,ret
)))
329 /* Loop does not exit */
332 ret
=cb((BIO
*)b
,c
->state
,ret
);
337 BIO_CONNECT
*BIO_CONNECT_new()
341 if ((ret
=(BIO_CONNECT
*)Malloc(sizeof(BIO_CONNECT
))) == NULL
)
343 ret
->state
=BIO_CONN_S_BEFORE
;
344 ret
->param_hostname
=NULL
;
345 ret
->param_port
=NULL
;
346 ret
->info_callback
=NULL
;
353 memset((char *)&ret
->them
,0,sizeof(ret
->them
));
357 void BIO_CONNECT_free(a
)
363 if (a
->param_hostname
!= NULL
)
364 Free(a
->param_hostname
);
365 if (a
->param_port
!= NULL
)
370 BIO_METHOD
*BIO_s_connect()
372 return(&methods_connectp
);
375 static int conn_new(bi
)
379 bi
->num
=INVALID_SOCKET
;
381 if ((bi
->ptr
=(char *)BIO_CONNECT_new()) == NULL
)
387 static void conn_close_socket(bio
)
392 c
=(BIO_CONNECT
*)bio
->ptr
;
393 if (bio
->num
!= INVALID_SOCKET
)
395 /* Only do a shutdown if things were established */
396 if (c
->state
== BIO_CONN_S_OK
)
397 shutdown(bio
->num
,2);
398 closesocket(bio
->num
);
399 bio
->num
=INVALID_SOCKET
;
403 static int conn_free(a
)
408 if (a
== NULL
) return(0);
409 data
=(BIO_CONNECT
*)a
->ptr
;
413 conn_close_socket(a
);
414 BIO_CONNECT_free(data
);
422 static int conn_read(b
,out
,outl
)
430 data
=(BIO_CONNECT
*)b
->ptr
;
431 if (data
->state
!= BIO_CONN_S_OK
)
433 ret
=conn_state(b
,data
);
440 clear_socket_error();
441 ret
=readsocket(b
->num
,out
,outl
);
442 BIO_clear_retry_flags(b
);
445 if (BIO_sock_should_retry(ret
))
446 BIO_set_retry_read(b
);
452 static int conn_write(b
,in
,inl
)
460 data
=(BIO_CONNECT
*)b
->ptr
;
461 if (data
->state
!= BIO_CONN_S_OK
)
463 ret
=conn_state(b
,data
);
464 if (ret
<= 0) return(ret
);
467 clear_socket_error();
468 ret
=writesocket(b
->num
,in
,inl
);
469 BIO_clear_retry_flags(b
);
472 if (BIO_sock_should_retry(ret
))
473 BIO_set_retry_write(b
);
478 static long conn_ctrl(b
,cmd
,num
,ptr
)
490 data
=(BIO_CONNECT
*)b
->ptr
;
496 data
->state
=BIO_CONN_S_BEFORE
;
497 conn_close_socket(b
);
500 case BIO_C_DO_STATE_MACHINE
:
501 /* use this one to start the connection */
502 if (!data
->state
!= BIO_CONN_S_OK
)
503 ret
=(long)conn_state(b
,data
);
507 case BIO_C_GET_CONNECT
:
513 *pptr
=data
->param_hostname
;
518 *pptr
=data
->param_port
;
522 *pptr
= (char *)&(data
->ip
[0]);
526 *((int *)ptr
)=data
->port
;
528 if ((!b
->init
) || (ptr
== NULL
))
529 *pptr
="not initalised";
533 case BIO_C_SET_CONNECT
:
539 if (data
->param_hostname
!= NULL
)
540 Free(data
->param_hostname
);
541 data
->param_hostname
=BUF_strdup(ptr
);
545 if (data
->param_port
!= NULL
)
546 Free(data
->param_port
);
547 data
->param_port
=BUF_strdup(ptr
);
553 sprintf(buf
,"%d.%d.%d.%d",
554 ptr
[0],ptr
[1],ptr
[2],ptr
[3]);
555 if (data
->param_hostname
!= NULL
)
556 Free(data
->param_hostname
);
557 data
->param_hostname
=BUF_strdup(buf
);
558 memcpy(&(data
->ip
[0]),ptr
,4);
564 sprintf(buf
,"%d",*(int *)ptr
);
565 if (data
->param_port
!= NULL
)
566 Free(data
->param_port
);
567 data
->param_port
=BUF_strdup(buf
);
568 data
->port
= *(int *)ptr
;
586 case BIO_CTRL_GET_CLOSE
:
589 case BIO_CTRL_SET_CLOSE
:
590 b
->shutdown
=(int)num
;
592 case BIO_CTRL_PENDING
:
593 case BIO_CTRL_WPENDING
:
600 if (data
->param_port
)
601 BIO_set_conn_port(dbio
,data
->param_port
);
602 if (data
->param_hostname
)
603 BIO_set_conn_hostname(dbio
,data
->param_hostname
);
604 BIO_set_nbio(dbio
,data
->nbio
);
605 BIO_set_info_callback(dbio
,data
->info_callback
);
607 case BIO_CTRL_SET_CALLBACK
:
608 data
->info_callback
=(int (*)())ptr
;
610 case BIO_CTRL_GET_CALLBACK
:
614 fptr
=(int (**)())ptr
;
615 *fptr
=data
->info_callback
;
625 static int conn_puts(bp
,str
)
632 ret
=conn_write(bp
,str
,n
);
636 BIO
*BIO_new_connect(str
)
641 ret
=BIO_new(BIO_s_connect());
642 if (ret
== NULL
) return(NULL
);
643 if (BIO_set_conn_hostname(ret
,str
))