6deee1a35fd8fe29e050b45fd197b3420d6d718f
[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 <openssl/bio.h>
66
67 #ifdef WIN16
68 #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
69 #else
70 #define SOCKET_PROTOCOL IPPROTO_TCP
71 #endif
72
73 #if (defined(VMS) && __VMS_VER < 70000000)
74 /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
75 #undef FIONBIO
76 #endif
77
78
79 typedef struct bio_connect_st
80         {
81         int state;
82
83         char *param_hostname;
84         char *param_port;
85         int nbio;
86
87         unsigned char ip[4];
88         unsigned short port;
89
90         struct sockaddr_in them;
91
92         /* int socket; this will be kept in bio->num so that it is
93          * compatible with the bss_sock bio */ 
94
95         /* called when the connection is initially made
96          *  callback(BIO,state,ret);  The callback should return
97          * 'ret'.  state is for compatibility with the ssl info_callback */
98         int (*info_callback)();
99         } BIO_CONNECT;
100
101 union int_fn_to_char_u
102         {
103         char *char_p;
104         int (*fn_p)();
105         };
106
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);
113
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);
118
119 static BIO_METHOD methods_connectp=
120         {
121         BIO_TYPE_CONNECT,
122         "socket connect",
123         conn_write,
124         conn_read,
125         conn_puts,
126         NULL, /* connect_gets, */
127         conn_ctrl,
128         conn_new,
129         conn_free,
130         };
131
132 static int conn_state(BIO *b, BIO_CONNECT *c)
133         {
134         int ret= -1,i;
135         unsigned long l;
136         char *p,*q;
137         int (*cb)()=NULL;
138
139         if (c->info_callback != NULL)
140                 cb=c->info_callback;
141
142         for (;;)
143                 {
144                 switch (c->state)
145                         {
146                 case BIO_CONN_S_BEFORE:
147                         p=c->param_hostname;
148                         if (p == NULL)
149                                 {
150                                 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
151                                 goto exit_loop;
152                                 }
153                         for ( ; *p != '\0'; p++)
154                                 {
155                                 if ((*p == ':') || (*p == '/')) break;
156                                 }
157
158                         i= *p;
159                         if ((i == ':') || (i == '/'))
160                                 {
161
162                                 *(p++)='\0';
163                                 if (i == ':')
164                                         {
165                                         for (q=p; *q; q++)
166                                                 if (*q == '/')
167                                                         {
168                                                         *q='\0';
169                                                         break;
170                                                         }
171                                         if (c->param_port != NULL)
172                                                 Free(c->param_port);
173                                         c->param_port=BUF_strdup(p);
174                                         }
175                                 }
176
177                         if (c->param_port == NULL)
178                                 {
179                                 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
180                                 ERR_add_error_data(2,"host=",c->param_hostname);
181                                 goto exit_loop;
182                                 }
183                         c->state=BIO_CONN_S_GET_IP;
184                         break;
185
186                 case BIO_CONN_S_GET_IP:
187                         if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
188                                 goto exit_loop;
189                         c->state=BIO_CONN_S_GET_PORT;
190                         break;
191
192                 case BIO_CONN_S_GET_PORT:
193                         if (c->param_port == NULL)
194                                 {
195                                 abort();
196                                 goto exit_loop;
197                                 }
198                         else if (BIO_get_port(c->param_port,&c->port) <= 0)
199                                 goto exit_loop;
200                         c->state=BIO_CONN_S_CREATE_SOCKET;
201                         break;
202
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);
208                         l=(unsigned long)
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;
215
216                         ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
217                         if (ret == INVALID_SOCKET)
218                                 {
219                                 SYSerr(SYS_F_SOCKET,get_last_socket_error());
220                                 ERR_add_error_data(4,"host=",c->param_hostname,
221                                         ":",c->param_port);
222                                 BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
223                                 goto exit_loop;
224                                 }
225                         b->num=ret;
226                         c->state=BIO_CONN_S_NBIO;
227                         break;
228
229                 case BIO_CONN_S_NBIO:
230                         if (c->nbio)
231                                 {
232                                 if (!BIO_socket_nbio(b->num,1))
233                                         {
234                                         BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
235                                         ERR_add_error_data(4,"host=",
236                                                 c->param_hostname,
237                                                 ":",c->param_port);
238                                         goto exit_loop;
239                                         }
240                                 }
241                         c->state=BIO_CONN_S_CONNECT;
242
243 #ifdef SO_KEEPALIVE
244                         i=1;
245                         i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
246                         if (i < 0)
247                                 {
248                                 SYSerr(SYS_F_SOCKET,get_last_socket_error());
249                                 ERR_add_error_data(4,"host=",c->param_hostname,
250                                         ":",c->param_port);
251                                 BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
252                                 goto exit_loop;
253                                 }
254 #endif
255                         break;
256
257                 case BIO_CONN_S_CONNECT:
258                         BIO_clear_retry_flags(b);
259                         ret=connect(b->num,
260                                 (struct sockaddr *)&c->them,
261                                 sizeof(c->them));
262                         b->retry_reason=0;
263                         if (ret < 0)
264                                 {
265                                 if (BIO_sock_should_retry(ret))
266                                         {
267                                         BIO_set_retry_special(b);
268                                         c->state=BIO_CONN_S_BLOCKED_CONNECT;
269                                         b->retry_reason=BIO_RR_CONNECT;
270                                         }
271                                 else
272                                         {
273                                         SYSerr(SYS_F_CONNECT,get_last_socket_error());
274                                         ERR_add_error_data(4,"host=",
275                                                 c->param_hostname,
276                                                 ":",c->param_port);
277                                         BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
278                                         }
279                                 goto exit_loop;
280                                 }
281                         else
282                                 c->state=BIO_CONN_S_OK;
283                         break;
284
285                 case BIO_CONN_S_BLOCKED_CONNECT:
286                         i=BIO_sock_error(b->num);
287                         if (i)
288                                 {
289                                 BIO_clear_retry_flags(b);
290                                 SYSerr(SYS_F_CONNECT,i);
291                                 ERR_add_error_data(4,"host=",
292                                         c->param_hostname,
293                                         ":",c->param_port);
294                                 BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
295                                 ret=0;
296                                 goto exit_loop;
297                                 }
298                         else
299                                 c->state=BIO_CONN_S_OK;
300                         break;
301
302                 case BIO_CONN_S_OK:
303                         ret=1;
304                         goto exit_loop;
305                 default:
306                         abort();
307                         goto exit_loop;
308                         }
309
310                 if (cb != NULL)
311                         {
312                         if (!(ret=cb((BIO *)b,c->state,ret)))
313                                 goto end;
314                         }
315                 }
316
317         /* Loop does not exit */
318 exit_loop:
319         if (cb != NULL)
320                 ret=cb((BIO *)b,c->state,ret);
321 end:
322         return(ret);
323         }
324
325 BIO_CONNECT *BIO_CONNECT_new(void)
326         {
327         BIO_CONNECT *ret;
328
329         if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL)
330                 return(NULL);
331         ret->state=BIO_CONN_S_BEFORE;
332         ret->param_hostname=NULL;
333         ret->param_port=NULL;
334         ret->info_callback=NULL;
335         ret->nbio=0;
336         ret->ip[0]=0;
337         ret->ip[1]=0;
338         ret->ip[2]=0;
339         ret->ip[3]=0;
340         ret->port=0;
341         memset((char *)&ret->them,0,sizeof(ret->them));
342         return(ret);
343         }
344
345 void BIO_CONNECT_free(BIO_CONNECT *a)
346         {
347         if(a == NULL)
348             return;
349
350         if (a->param_hostname != NULL)
351                 Free(a->param_hostname);
352         if (a->param_port != NULL)
353                 Free(a->param_port);
354         Free(a);
355         }
356
357 BIO_METHOD *BIO_s_connect(void)
358         {
359         return(&methods_connectp);
360         }
361
362 static int conn_new(BIO *bi)
363         {
364         bi->init=0;
365         bi->num=INVALID_SOCKET;
366         bi->flags=0;
367         if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
368                 return(0);
369         else
370                 return(1);
371         }
372
373 static void conn_close_socket(BIO *bio)
374         {
375         BIO_CONNECT *c;
376
377         c=(BIO_CONNECT *)bio->ptr;
378         if (bio->num != INVALID_SOCKET)
379                 {
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;
385                 }
386         }
387
388 static int conn_free(BIO *a)
389         {
390         BIO_CONNECT *data;
391
392         if (a == NULL) return(0);
393         data=(BIO_CONNECT *)a->ptr;
394          
395         if (a->shutdown)
396                 {
397                 conn_close_socket(a);
398                 BIO_CONNECT_free(data);
399                 a->ptr=NULL;
400                 a->flags=0;
401                 a->init=0;
402                 }
403         return(1);
404         }
405         
406 static int conn_read(BIO *b, char *out, int outl)
407         {
408         int ret=0;
409         BIO_CONNECT *data;
410
411         data=(BIO_CONNECT *)b->ptr;
412         if (data->state != BIO_CONN_S_OK)
413                 {
414                 ret=conn_state(b,data);
415                 if (ret <= 0)
416                                 return(ret);
417                 }
418
419         if (out != NULL)
420                 {
421                 clear_socket_error();
422                 ret=readsocket(b->num,out,outl);
423                 BIO_clear_retry_flags(b);
424                 if (ret <= 0)
425                         {
426                         if (BIO_sock_should_retry(ret))
427                                 BIO_set_retry_read(b);
428                         }
429                 }
430         return(ret);
431         }
432
433 static int conn_write(BIO *b, char *in, int inl)
434         {
435         int ret;
436         BIO_CONNECT *data;
437
438         data=(BIO_CONNECT *)b->ptr;
439         if (data->state != BIO_CONN_S_OK)
440                 {
441                 ret=conn_state(b,data);
442                 if (ret <= 0) return(ret);
443                 }
444
445         clear_socket_error();
446         ret=writesocket(b->num,in,inl);
447         BIO_clear_retry_flags(b);
448         if (ret <= 0)
449                 {
450                 if (BIO_sock_should_retry(ret))
451                         BIO_set_retry_write(b);
452                 }
453         return(ret);
454         }
455
456 static long conn_ctrl(BIO *b, int cmd, long num, char *ptr)
457         {
458         BIO *dbio;
459         int *ip;
460         const char **pptr;
461         long ret=1;
462         BIO_CONNECT *data;
463
464         data=(BIO_CONNECT *)b->ptr;
465
466         switch (cmd)
467                 {
468         case BIO_CTRL_RESET:
469                 ret=0;
470                 data->state=BIO_CONN_S_BEFORE;
471                 conn_close_socket(b);
472                 b->flags=0;
473                 break;
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);
478                 else
479                         ret=1;
480                 break;
481         case BIO_C_GET_CONNECT:
482                 if (ptr != NULL)
483                         {
484                         pptr=(const char **)ptr;
485                         if (num == 0)
486                                 {
487                                 *pptr=data->param_hostname;
488
489                                 }
490                         else if (num == 1)
491                                 {
492                                 *pptr=data->param_port;
493                                 }
494                         else if (num == 2)
495                                 {
496                                 *pptr= (char *)&(data->ip[0]);
497                                 }
498                         else if (num == 3)
499                                 {
500                                 *((int *)ptr)=data->port;
501                                 }
502                         if ((!b->init) || (ptr == NULL))
503                                 *pptr="not initialized";
504                         ret=1;
505                         }
506                 break;
507         case BIO_C_SET_CONNECT:
508                 if (ptr != NULL)
509                         {
510                         b->init=1;
511                         if (num == 0)
512                                 {
513                                 if (data->param_hostname != NULL)
514                                         Free(data->param_hostname);
515                                 data->param_hostname=BUF_strdup(ptr);
516                                 }
517                         else if (num == 1)
518                                 {
519                                 if (data->param_port != NULL)
520                                         Free(data->param_port);
521                                 data->param_port=BUF_strdup(ptr);
522                                 }
523                         else if (num == 2)
524                                 {
525                                 char buf[16];
526
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);
533                                 }
534                         else if (num == 3)
535                                 {
536                                 char buf[16];
537
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;
543                                 }
544                         }
545                 break;
546         case BIO_C_SET_NBIO:
547                 data->nbio=(int)num;
548                 break;
549         case BIO_C_GET_FD:
550                 if (b->init)
551                         {
552                         ip=(int *)ptr;
553                         if (ip != NULL)
554                                 *ip=b->num;
555                         ret=b->num;
556                         }
557                 else
558                         ret= -1;
559                 break;
560         case BIO_CTRL_GET_CLOSE:
561                 ret=b->shutdown;
562                 break;
563         case BIO_CTRL_SET_CLOSE:
564                 b->shutdown=(int)num;
565                 break;
566         case BIO_CTRL_PENDING:
567         case BIO_CTRL_WPENDING:
568                 ret=0;
569                 break;
570         case BIO_CTRL_FLUSH:
571                 break;
572         case BIO_CTRL_DUP:
573                 {
574                 union int_fn_to_char_u tmp_cb;
575                 
576                 dbio=(BIO *)ptr;
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);
584                 }
585                 break;
586         case BIO_CTRL_SET_CALLBACK:
587                 {
588                 union int_fn_to_char_u tmp_cb;
589                 
590                 tmp_cb.char_p=ptr;
591                 data->info_callback=tmp_cb.fn_p;
592                 }
593                 break;
594         case BIO_CTRL_GET_CALLBACK:
595                 {
596                 int (**fptr)();
597
598                 fptr=(int (**)())ptr;
599                 *fptr=data->info_callback;
600                 }
601                 break;
602         default:
603                 ret=0;
604                 break;
605                 }
606         return(ret);
607         }
608
609 static int conn_puts(BIO *bp, char *str)
610         {
611         int n,ret;
612
613         n=strlen(str);
614         ret=conn_write(bp,str,n);
615         return(ret);
616         }
617
618 BIO *BIO_new_connect(char *str)
619         {
620         BIO *ret;
621
622         ret=BIO_new(BIO_s_connect());
623         if (ret == NULL) return(NULL);
624         if (BIO_set_conn_hostname(ret,str))
625                 return(ret);
626         else
627                 {
628                 BIO_free(ret);
629                 return(NULL);
630                 }
631         }
632
633 #endif
634