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.]
65 #include <openssl/bio.h>
68 #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
70 #define SOCKET_PROTOCOL IPPROTO_TCP
73 #if (defined(VMS) && __VMS_VER < 70000000)
74 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
79 typedef struct bio_connect_st
90 struct sockaddr_in them;
92 /* int socket; this will be kept in bio->num so that it is
93 * compatable with the bss_sock bio */
95 /* called when the connection is initially made
96 * callback(BIO,state,ret); The callback should return
97 * 'ret'. state is for compatablity with the ssl info_callback */
98 int (*info_callback)();
101 union int_fn_to_char_u
107 static int conn_write(BIO *h,char *buf,int num);
108 static int conn_read(BIO *h,char *buf,int size);
109 static int conn_puts(BIO *h,char *str);
110 static long conn_ctrl(BIO *h,int cmd,long arg1,char *arg2);
111 static int conn_new(BIO *h);
112 static int conn_free(BIO *data);
114 static int conn_state(BIO *b, BIO_CONNECT *c);
115 static void conn_close_socket(BIO *data);
116 BIO_CONNECT *BIO_CONNECT_new(void );
117 void BIO_CONNECT_free(BIO_CONNECT *a);
119 static BIO_METHOD methods_connectp=
126 NULL, /* connect_gets, */
132 static int conn_state(BIO *b, BIO_CONNECT *c)
139 if (c->info_callback != NULL)
146 case BIO_CONN_S_BEFORE:
150 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
153 for ( ; *p != '\0'; p++)
155 if ((*p == ':') || (*p == '/')) break;
159 if ((i == ':') || (i == '/'))
171 if (c->param_port != NULL)
173 c->param_port=BUF_strdup(p);
177 if (c->param_port == NULL)
179 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
180 ERR_add_error_data(2,"host=",c->param_hostname);
183 c->state=BIO_CONN_S_GET_IP;
186 case BIO_CONN_S_GET_IP:
187 if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
189 c->state=BIO_CONN_S_GET_PORT;
192 case BIO_CONN_S_GET_PORT:
193 if (c->param_port == NULL)
198 else if (BIO_get_port(c->param_port,&c->port) <= 0)
200 c->state=BIO_CONN_S_CREATE_SOCKET;
203 case BIO_CONN_S_CREATE_SOCKET:
204 /* now setup address */
205 memset((char *)&c->them,0,sizeof(c->them));
206 c->them.sin_family=AF_INET;
207 c->them.sin_port=htons((unsigned short)c->port);
209 ((unsigned long)c->ip[0]<<24L)|
210 ((unsigned long)c->ip[1]<<16L)|
211 ((unsigned long)c->ip[2]<< 8L)|
212 ((unsigned long)c->ip[3]);
213 c->them.sin_addr.s_addr=htonl(l);
214 c->state=BIO_CONN_S_CREATE_SOCKET;
216 ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
217 if (ret == INVALID_SOCKET)
219 SYSerr(SYS_F_SOCKET,get_last_socket_error());
220 ERR_add_error_data(4,"host=",c->param_hostname,
222 BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
226 c->state=BIO_CONN_S_NBIO;
229 case BIO_CONN_S_NBIO:
232 if (!BIO_socket_nbio(b->num,1))
234 BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
235 ERR_add_error_data(4,"host=",
241 c->state=BIO_CONN_S_CONNECT;
245 i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
248 SYSerr(SYS_F_SOCKET,get_last_socket_error());
249 ERR_add_error_data(4,"host=",c->param_hostname,
251 BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
257 case BIO_CONN_S_CONNECT:
258 BIO_clear_retry_flags(b);
260 (struct sockaddr *)&c->them,
265 if (BIO_sock_should_retry(ret))
267 BIO_set_retry_special(b);
268 c->state=BIO_CONN_S_BLOCKED_CONNECT;
269 b->retry_reason=BIO_RR_CONNECT;
273 SYSerr(SYS_F_CONNECT,get_last_socket_error());
274 ERR_add_error_data(4,"host=",
277 BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
282 c->state=BIO_CONN_S_OK;
285 case BIO_CONN_S_BLOCKED_CONNECT:
286 i=BIO_sock_error(b->num);
289 BIO_clear_retry_flags(b);
290 SYSerr(SYS_F_CONNECT,i);
291 ERR_add_error_data(4,"host=",
294 BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
299 c->state=BIO_CONN_S_OK;
312 if (!(ret=cb((BIO *)b,c->state,ret)))
317 /* Loop does not exit */
320 ret=cb((BIO *)b,c->state,ret);
325 BIO_CONNECT *BIO_CONNECT_new(void)
329 if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL)
331 ret->state=BIO_CONN_S_BEFORE;
332 ret->param_hostname=NULL;
333 ret->param_port=NULL;
334 ret->info_callback=NULL;
341 memset((char *)&ret->them,0,sizeof(ret->them));
345 void BIO_CONNECT_free(BIO_CONNECT *a)
350 if (a->param_hostname != NULL)
351 Free(a->param_hostname);
352 if (a->param_port != NULL)
357 BIO_METHOD *BIO_s_connect(void)
359 return(&methods_connectp);
362 static int conn_new(BIO *bi)
365 bi->num=INVALID_SOCKET;
367 if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
373 static void conn_close_socket(BIO *bio)
377 c=(BIO_CONNECT *)bio->ptr;
378 if (bio->num != INVALID_SOCKET)
380 /* Only do a shutdown if things were established */
381 if (c->state == BIO_CONN_S_OK)
382 shutdown(bio->num,2);
383 closesocket(bio->num);
384 bio->num=INVALID_SOCKET;
388 static int conn_free(BIO *a)
392 if (a == NULL) return(0);
393 data=(BIO_CONNECT *)a->ptr;
397 conn_close_socket(a);
398 BIO_CONNECT_free(data);
406 static int conn_read(BIO *b, char *out, int outl)
411 data=(BIO_CONNECT *)b->ptr;
412 if (data->state != BIO_CONN_S_OK)
414 ret=conn_state(b,data);
421 clear_socket_error();
422 ret=readsocket(b->num,out,outl);
423 BIO_clear_retry_flags(b);
426 if (BIO_sock_should_retry(ret))
427 BIO_set_retry_read(b);
433 static int conn_write(BIO *b, char *in, int inl)
438 data=(BIO_CONNECT *)b->ptr;
439 if (data->state != BIO_CONN_S_OK)
441 ret=conn_state(b,data);
442 if (ret <= 0) return(ret);
445 clear_socket_error();
446 ret=writesocket(b->num,in,inl);
447 BIO_clear_retry_flags(b);
450 if (BIO_sock_should_retry(ret))
451 BIO_set_retry_write(b);
456 static long conn_ctrl(BIO *b, int cmd, long num, char *ptr)
464 data=(BIO_CONNECT *)b->ptr;
470 data->state=BIO_CONN_S_BEFORE;
471 conn_close_socket(b);
474 case BIO_C_DO_STATE_MACHINE:
475 /* use this one to start the connection */
476 if (!data->state != BIO_CONN_S_OK)
477 ret=(long)conn_state(b,data);
481 case BIO_C_GET_CONNECT:
484 pptr=(const char **)ptr;
487 *pptr=data->param_hostname;
492 *pptr=data->param_port;
496 *pptr= (char *)&(data->ip[0]);
500 *((int *)ptr)=data->port;
502 if ((!b->init) || (ptr == NULL))
503 *pptr="not initalised";
507 case BIO_C_SET_CONNECT:
513 if (data->param_hostname != NULL)
514 Free(data->param_hostname);
515 data->param_hostname=BUF_strdup(ptr);
519 if (data->param_port != NULL)
520 Free(data->param_port);
521 data->param_port=BUF_strdup(ptr);
527 sprintf(buf,"%d.%d.%d.%d",
528 ptr[0],ptr[1],ptr[2],ptr[3]);
529 if (data->param_hostname != NULL)
530 Free(data->param_hostname);
531 data->param_hostname=BUF_strdup(buf);
532 memcpy(&(data->ip[0]),ptr,4);
538 sprintf(buf,"%d",*(int *)ptr);
539 if (data->param_port != NULL)
540 Free(data->param_port);
541 data->param_port=BUF_strdup(buf);
542 data->port= *(int *)ptr;
560 case BIO_CTRL_GET_CLOSE:
563 case BIO_CTRL_SET_CLOSE:
564 b->shutdown=(int)num;
566 case BIO_CTRL_PENDING:
567 case BIO_CTRL_WPENDING:
574 union int_fn_to_char_u tmp_cb;
577 if (data->param_port)
578 BIO_set_conn_port(dbio,data->param_port);
579 if (data->param_hostname)
580 BIO_set_conn_hostname(dbio,data->param_hostname);
581 BIO_set_nbio(dbio,data->nbio);
582 tmp_cb.fn_p=data->info_callback;
583 (void)BIO_set_info_callback(dbio,tmp_cb.char_p);
586 case BIO_CTRL_SET_CALLBACK:
588 union int_fn_to_char_u tmp_cb;
591 data->info_callback=tmp_cb.fn_p;
594 case BIO_CTRL_GET_CALLBACK:
598 fptr=(int (**)())ptr;
599 *fptr=data->info_callback;
609 static int conn_puts(BIO *bp, char *str)
614 ret=conn_write(bp,str,n);
618 BIO *BIO_new_connect(char *str)
622 ret=BIO_new(BIO_s_connect());
623 if (ret == NULL) return(NULL);
624 if (BIO_set_conn_hostname(ret,str))