3ec1388f2e8ac8b5ea94f03b13b2362de3afb5a6
[oweals/openssl.git] / crypto / bio / bss_conn.c
1 /* crypto/bio/bss_conn.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
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.
8  * 
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).
15  * 
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.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
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)"
40  * 
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
51  * SUCH DAMAGE.
52  * 
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.]
57  */
58
59 #ifndef NO_SOCK
60
61 #include <stdio.h>
62 #include <errno.h>
63 #define USE_SOCKETS
64 #include "cryptlib.h"
65 #include "bio.h"
66
67 /*      BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */
68
69 #ifdef WIN16
70 #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
71 #else
72 #define SOCKET_PROTOCOL IPPROTO_TCP
73 #endif
74
75 typedef struct bio_connect_st
76         {
77         int state;
78
79         char *param_hostname;
80         char *param_port;
81         int nbio;
82
83         unsigned char ip[4];
84         unsigned short port;
85
86         struct sockaddr_in them;
87
88         /* int socket; this will be kept in bio->num so that it is
89          * compatable with the bss_sock bio */ 
90
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)();
95         } BIO_CONNECT;
96
97 #ifndef NOPROTO
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);
104 #else
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();
111 #endif
112
113 #ifndef NOPROTO
114
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);
119
120 #else
121
122 static int conn_state();
123 static void conn_close_socket();
124 BIO_CONNECT *BIO_CONNECT_new();
125 void BIO_CONNECT_free();
126
127 #endif
128
129 static BIO_METHOD methods_connectp=
130         {
131         BIO_TYPE_CONNECT,
132         "socket connect",
133         conn_write,
134         conn_read,
135         conn_puts,
136         NULL, /* connect_gets, */
137         conn_ctrl,
138         conn_new,
139         conn_free,
140         };
141
142 static int conn_state(b,c)
143 BIO *b;
144 BIO_CONNECT *c;
145         {
146         int ret= -1,i;
147         unsigned long l;
148         char *p,*q;
149         int (*cb)()=NULL;
150
151         if (c->info_callback != NULL)
152                 cb=c->info_callback;
153
154         for (;;)
155                 {
156                 switch (c->state)
157                         {
158                 case BIO_CONN_S_BEFORE:
159                         p=c->param_hostname;
160                         if (p == NULL)
161                                 {
162                                 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTHNAME_SPECIFIED);
163                                 goto exit_loop;
164                                 }
165                         for ( ; *p != '\0'; p++)
166                                 {
167                                 if ((*p == ':') || (*p == '/')) break;
168                                 }
169
170                         i= *p;
171                         if ((i == ':') || (i == '/'))
172                                 {
173
174                                 *(p++)='\0';
175                                 if (i == ':')
176                                         {
177                                         for (q=p; *q; q++)
178                                                 if (*q == '/')
179                                                         {
180                                                         *q='\0';
181                                                         break;
182                                                         }
183                                         if (c->param_port != NULL)
184                                                 Free(c->param_port);
185                                         c->param_port=BUF_strdup(p);
186                                         }
187                                 }
188
189                         if (c->param_port == NULL)
190                                 {
191                                 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
192                                 ERR_add_error_data(2,"host=",c->param_hostname);
193                                 goto exit_loop;
194                                 }
195                         c->state=BIO_CONN_S_GET_IP;
196                         break;
197
198                 case BIO_CONN_S_GET_IP:
199                         if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
200                                 goto exit_loop;
201                         c->state=BIO_CONN_S_GET_PORT;
202                         break;
203
204                 case BIO_CONN_S_GET_PORT:
205                         if (c->param_port == NULL)
206                                 {
207                                 abort();
208                                 goto exit_loop;
209                                 }
210                         else if (BIO_get_port(c->param_port,&c->port) <= 0)
211                                 goto exit_loop;
212                         c->state=BIO_CONN_S_CREATE_SOCKET;
213                         break;
214
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);
220                         l=(unsigned long)
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;
227
228                         ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
229                         if (ret == INVALID_SOCKET)
230                                 {
231                                 SYSerr(SYS_F_SOCKET,get_last_socket_error());
232                                 ERR_add_error_data(4,"host=",c->param_hostname,
233                                         ":",c->param_port);
234                                 BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
235                                 goto exit_loop;
236                                 }
237                         b->num=ret;
238                         c->state=BIO_CONN_S_NBIO;
239                         break;
240
241                 case BIO_CONN_S_NBIO:
242                         if (c->nbio)
243                                 {
244                                 if (!BIO_socket_nbio(b->num,1))
245                                         {
246                                         BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
247                                         ERR_add_error_data(4,"host=",
248                                                 c->param_hostname,
249                                                 ":",c->param_port);
250                                         goto exit_loop;
251                                         }
252                                 }
253                         c->state=BIO_CONN_S_CONNECT;
254
255 #ifdef SO_KEEPALIVE
256                         i=1;
257                         i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
258                         if (i < 0)
259                                 {
260                                 SYSerr(SYS_F_SOCKET,get_last_socket_error());
261                                 ERR_add_error_data(4,"host=",c->param_hostname,
262                                         ":",c->param_port);
263                                 BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
264                                 goto exit_loop;
265                                 }
266 #endif
267                         break;
268
269                 case BIO_CONN_S_CONNECT:
270                         BIO_clear_retry_flags(b);
271                         ret=connect(b->num,
272                                 (struct sockaddr *)&c->them,
273                                 sizeof(c->them));
274                         b->retry_reason=0;
275                         if (ret < 0)
276                                 {
277                                 if (BIO_sock_should_retry(ret))
278                                         {
279                                         BIO_set_retry_special(b);
280                                         c->state=BIO_CONN_S_BLOCKED_CONNECT;
281                                         b->retry_reason=BIO_RR_CONNECT;
282                                         }
283                                 else
284                                         {
285                                         SYSerr(SYS_F_CONNECT,get_last_socket_error());
286                                         ERR_add_error_data(4,"host=",
287                                                 c->param_hostname,
288                                                 ":",c->param_port);
289                                         BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
290                                         }
291                                 goto exit_loop;
292                                 }
293                         else
294                                 c->state=BIO_CONN_S_OK;
295                         break;
296
297                 case BIO_CONN_S_BLOCKED_CONNECT:
298                         i=BIO_sock_error(b->num);
299                         if (i)
300                                 {
301                                 BIO_clear_retry_flags(b);
302                                 SYSerr(SYS_F_CONNECT,i);
303                                 ERR_add_error_data(4,"host=",
304                                         c->param_hostname,
305                                         ":",c->param_port);
306                                 BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
307                                 ret=0;
308                                 goto exit_loop;
309                                 }
310                         else
311                                 c->state=BIO_CONN_S_OK;
312                         break;
313
314                 case BIO_CONN_S_OK:
315                         ret=1;
316                         goto exit_loop;
317                 default:
318                         abort();
319                         goto exit_loop;
320                         }
321
322                 if (cb != NULL)
323                         {
324                         if (!(ret=cb((BIO *)b,c->state,ret)))
325                                 goto end;
326                         }
327                 }
328
329         /* Loop does not exit */
330 exit_loop:
331         if (cb != NULL)
332                 ret=cb((BIO *)b,c->state,ret);
333 end:
334         return(ret);
335         }
336
337 BIO_CONNECT *BIO_CONNECT_new()
338         {
339         BIO_CONNECT *ret;
340
341         if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL)
342                 return(NULL);
343         ret->state=BIO_CONN_S_BEFORE;
344         ret->param_hostname=NULL;
345         ret->param_port=NULL;
346         ret->info_callback=NULL;
347         ret->nbio=0;
348         ret->ip[0]=0;
349         ret->ip[1]=0;
350         ret->ip[2]=0;
351         ret->ip[3]=0;
352         ret->port=0;
353         memset((char *)&ret->them,0,sizeof(ret->them));
354         return(ret);
355         }
356
357 void BIO_CONNECT_free(a)
358 BIO_CONNECT *a;
359         {
360         if (a->param_hostname != NULL)
361                 Free(a->param_hostname);
362         if (a->param_port != NULL)
363                 Free(a->param_port);
364         Free(a);
365         }
366
367 BIO_METHOD *BIO_s_connect()
368         {
369         return(&methods_connectp);
370         }
371
372 static int conn_new(bi)
373 BIO *bi;
374         {
375         bi->init=0;
376         bi->num=INVALID_SOCKET;
377         bi->flags=0;
378         if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
379                 return(0);
380         else
381                 return(1);
382         }
383
384 static void conn_close_socket(bio)
385 BIO *bio;
386         {
387         BIO_CONNECT *c;
388
389         c=(BIO_CONNECT *)bio->ptr;
390         if (bio->num != INVALID_SOCKET)
391                 {
392                 /* Only do a shutdown if things were established */
393                 if (c->state == BIO_CONN_S_OK)
394                         shutdown(bio->num,2);
395                 closesocket(bio->num);
396                 bio->num=INVALID_SOCKET;
397                 }
398         }
399
400 static int conn_free(a)
401 BIO *a;
402         {
403         BIO_CONNECT *data;
404
405         if (a == NULL) return(0);
406         data=(BIO_CONNECT *)a->ptr;
407          
408         if (a->shutdown)
409                 {
410                 conn_close_socket(a);
411                 BIO_CONNECT_free(data);
412                 a->ptr=NULL;
413                 a->flags=0;
414                 a->init=0;
415                 }
416         return(1);
417         }
418         
419 static int conn_read(b,out,outl)
420 BIO *b;
421 char *out;
422 int outl;
423         {
424         int ret=0;
425         BIO_CONNECT *data;
426
427         data=(BIO_CONNECT *)b->ptr;
428         if (data->state != BIO_CONN_S_OK)
429                 {
430                 ret=conn_state(b,data);
431                 if (ret <= 0)
432                                 return(ret);
433                 }
434
435         if (out != NULL)
436                 {
437                 clear_socket_error();
438                 ret=readsocket(b->num,out,outl);
439                 BIO_clear_retry_flags(b);
440                 if (ret <= 0)
441                         {
442                         if (BIO_sock_should_retry(ret))
443                                 BIO_set_retry_read(b);
444                         }
445                 }
446         return(ret);
447         }
448
449 static int conn_write(b,in,inl)
450 BIO *b;
451 char *in;
452 int inl;
453         {
454         int ret;
455         BIO_CONNECT *data;
456
457         data=(BIO_CONNECT *)b->ptr;
458         if (data->state != BIO_CONN_S_OK)
459                 {
460                 ret=conn_state(b,data);
461                 if (ret <= 0) return(ret);
462                 }
463
464         clear_socket_error();
465         ret=writesocket(b->num,in,inl);
466         BIO_clear_retry_flags(b);
467         if (ret <= 0)
468                 {
469                 if (BIO_sock_should_retry(ret))
470                         BIO_set_retry_write(b);
471                 }
472         return(ret);
473         }
474
475 static long conn_ctrl(b,cmd,num,ptr)
476 BIO *b;
477 int cmd;
478 long num;
479 char *ptr;
480         {
481         BIO *dbio;
482         int *ip;
483         char **pptr;
484         long ret=1;
485         BIO_CONNECT *data;
486
487         data=(BIO_CONNECT *)b->ptr;
488
489         switch (cmd)
490                 {
491         case BIO_CTRL_RESET:
492                 ret=0;
493                 data->state=BIO_CONN_S_BEFORE;
494                 conn_close_socket(b);
495                 b->flags=0;
496                 break;
497         case BIO_C_DO_STATE_MACHINE:
498                 /* use this one to start the connection */
499                 if (!data->state != BIO_CONN_S_OK)
500                         ret=(long)conn_state(b,data);
501                 else
502                         ret=1;
503                 break;
504         case BIO_C_GET_CONNECT:
505                 if (ptr != NULL)
506                         {
507                         pptr=(char **)ptr;
508                         if (num == 0)
509                                 {
510                                 *pptr=data->param_hostname;
511
512                                 }
513                         else if (num == 1)
514                                 {
515                                 *pptr=data->param_port;
516                                 }
517                         else if (num == 2)
518                                 {
519                                 *pptr= (char *)&(data->ip[0]);
520                                 }
521                         else if (num == 3)
522                                 {
523                                 *((int *)ptr)=data->port;
524                                 }
525                         if ((!b->init) || (ptr == NULL))
526                                 *pptr="not initalised";
527                         ret=1;
528                         }
529                 break;
530         case BIO_C_SET_CONNECT:
531                 if (ptr != NULL)
532                         {
533                         b->init=1;
534                         if (num == 0)
535                                 {
536                                 if (data->param_hostname != NULL)
537                                         Free(data->param_hostname);
538                                 data->param_hostname=BUF_strdup(ptr);
539                                 }
540                         else if (num == 1)
541                                 {
542                                 if (data->param_port != NULL)
543                                         Free(data->param_port);
544                                 data->param_port=BUF_strdup(ptr);
545                                 }
546                         else if (num == 2)
547                                 {
548                                 char buf[16];
549
550                                 sprintf(buf,"%d.%d.%d.%d",
551                                         ptr[0],ptr[1],ptr[2],ptr[3]);
552                                 if (data->param_hostname != NULL)
553                                         Free(data->param_hostname);
554                                 data->param_hostname=BUF_strdup(buf);
555                                 memcpy(&(data->ip[0]),ptr,4);
556                                 }
557                         else if (num == 3)
558                                 {
559                                 char buf[16];
560
561                                 sprintf(buf,"%d",*(int *)ptr);
562                                 if (data->param_port != NULL)
563                                         Free(data->param_port);
564                                 data->param_port=BUF_strdup(buf);
565                                 data->port= *(int *)ptr;
566                                 }
567                         }
568                 break;
569         case BIO_C_SET_NBIO:
570                 data->nbio=(int)num;
571                 break;
572         case BIO_C_GET_FD:
573                 if (b->init)
574                         {
575                         ip=(int *)ptr;
576                         if (ip != NULL)
577                                 *ip=b->num;
578                         ret=b->num;
579                         }
580                 else
581                         ret= -1;
582                 break;
583         case BIO_CTRL_GET_CLOSE:
584                 ret=b->shutdown;
585                 break;
586         case BIO_CTRL_SET_CLOSE:
587                 b->shutdown=(int)num;
588                 break;
589         case BIO_CTRL_PENDING:
590         case BIO_CTRL_WPENDING:
591                 ret=0;
592                 break;
593         case BIO_CTRL_FLUSH:
594                 break;
595         case BIO_CTRL_DUP:
596                 dbio=(BIO *)ptr;
597                 if (data->param_port)
598                         BIO_set_conn_port(dbio,data->param_port);
599                 if (data->param_hostname)
600                         BIO_set_conn_hostname(dbio,data->param_hostname);
601                 BIO_set_nbio(dbio,data->nbio);
602                 BIO_set_info_callback(dbio,data->info_callback);
603                 break;
604         case BIO_CTRL_SET_CALLBACK:
605                 data->info_callback=(int (*)())ptr;
606                 break;
607         case BIO_CTRL_GET_CALLBACK:
608                 {
609                 int (**fptr)();
610
611                 fptr=(int (**)())ptr;
612                 *fptr=data->info_callback;
613                 }
614                 break;
615         default:
616                 ret=0;
617                 break;
618                 }
619         return(ret);
620         }
621
622 static int conn_puts(bp,str)
623 BIO *bp;
624 char *str;
625         {
626         int n,ret;
627
628         n=strlen(str);
629         ret=conn_write(bp,str,n);
630         return(ret);
631         }
632
633 BIO *BIO_new_connect(str)
634 char *str;
635         {
636         BIO *ret;
637
638         ret=BIO_new(BIO_s_connect());
639         if (ret == NULL) return(NULL);
640         if (BIO_set_conn_hostname(ret,str))
641                 return(ret);
642         else
643                 {
644                 BIO_free(ret);
645                 return(NULL);
646                 }
647         }
648
649 #endif
650