68c46e3d699418fd539c61f5ce5e4c2715b4f9fd
[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          * compatable 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 compatablity with the ssl info_callback */
98         int (*info_callback)();
99         } BIO_CONNECT;
100
101 static int conn_write(BIO *h,char *buf,int num);
102 static int conn_read(BIO *h,char *buf,int size);
103 static int conn_puts(BIO *h,char *str);
104 static long conn_ctrl(BIO *h,int cmd,long arg1,char *arg2);
105 static int conn_new(BIO *h);
106 static int conn_free(BIO *data);
107
108 static int conn_state(BIO *b, BIO_CONNECT *c);
109 static void conn_close_socket(BIO *data);
110 BIO_CONNECT *BIO_CONNECT_new(void );
111 void BIO_CONNECT_free(BIO_CONNECT *a);
112
113 static BIO_METHOD methods_connectp=
114         {
115         BIO_TYPE_CONNECT,
116         "socket connect",
117         conn_write,
118         conn_read,
119         conn_puts,
120         NULL, /* connect_gets, */
121         conn_ctrl,
122         conn_new,
123         conn_free,
124         };
125
126 static int conn_state(BIO *b, BIO_CONNECT *c)
127         {
128         int ret= -1,i;
129         unsigned long l;
130         char *p,*q;
131         int (*cb)()=NULL;
132
133         if (c->info_callback != NULL)
134                 cb=c->info_callback;
135
136         for (;;)
137                 {
138                 switch (c->state)
139                         {
140                 case BIO_CONN_S_BEFORE:
141                         p=c->param_hostname;
142                         if (p == NULL)
143                                 {
144                                 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
145                                 goto exit_loop;
146                                 }
147                         for ( ; *p != '\0'; p++)
148                                 {
149                                 if ((*p == ':') || (*p == '/')) break;
150                                 }
151
152                         i= *p;
153                         if ((i == ':') || (i == '/'))
154                                 {
155
156                                 *(p++)='\0';
157                                 if (i == ':')
158                                         {
159                                         for (q=p; *q; q++)
160                                                 if (*q == '/')
161                                                         {
162                                                         *q='\0';
163                                                         break;
164                                                         }
165                                         if (c->param_port != NULL)
166                                                 Free(c->param_port);
167                                         c->param_port=BUF_strdup(p);
168                                         }
169                                 }
170
171                         if (c->param_port == NULL)
172                                 {
173                                 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
174                                 ERR_add_error_data(2,"host=",c->param_hostname);
175                                 goto exit_loop;
176                                 }
177                         c->state=BIO_CONN_S_GET_IP;
178                         break;
179
180                 case BIO_CONN_S_GET_IP:
181                         if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
182                                 goto exit_loop;
183                         c->state=BIO_CONN_S_GET_PORT;
184                         break;
185
186                 case BIO_CONN_S_GET_PORT:
187                         if (c->param_port == NULL)
188                                 {
189                                 abort();
190                                 goto exit_loop;
191                                 }
192                         else if (BIO_get_port(c->param_port,&c->port) <= 0)
193                                 goto exit_loop;
194                         c->state=BIO_CONN_S_CREATE_SOCKET;
195                         break;
196
197                 case BIO_CONN_S_CREATE_SOCKET:
198                         /* now setup address */
199                         memset((char *)&c->them,0,sizeof(c->them));
200                         c->them.sin_family=AF_INET;
201                         c->them.sin_port=htons((unsigned short)c->port);
202                         l=(unsigned long)
203                                 ((unsigned long)c->ip[0]<<24L)|
204                                 ((unsigned long)c->ip[1]<<16L)|
205                                 ((unsigned long)c->ip[2]<< 8L)|
206                                 ((unsigned long)c->ip[3]);
207                         c->them.sin_addr.s_addr=htonl(l);
208                         c->state=BIO_CONN_S_CREATE_SOCKET;
209
210                         ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
211                         if (ret == INVALID_SOCKET)
212                                 {
213                                 SYSerr(SYS_F_SOCKET,get_last_socket_error());
214                                 ERR_add_error_data(4,"host=",c->param_hostname,
215                                         ":",c->param_port);
216                                 BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
217                                 goto exit_loop;
218                                 }
219                         b->num=ret;
220                         c->state=BIO_CONN_S_NBIO;
221                         break;
222
223                 case BIO_CONN_S_NBIO:
224                         if (c->nbio)
225                                 {
226                                 if (!BIO_socket_nbio(b->num,1))
227                                         {
228                                         BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
229                                         ERR_add_error_data(4,"host=",
230                                                 c->param_hostname,
231                                                 ":",c->param_port);
232                                         goto exit_loop;
233                                         }
234                                 }
235                         c->state=BIO_CONN_S_CONNECT;
236
237 #ifdef SO_KEEPALIVE
238                         i=1;
239                         i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
240                         if (i < 0)
241                                 {
242                                 SYSerr(SYS_F_SOCKET,get_last_socket_error());
243                                 ERR_add_error_data(4,"host=",c->param_hostname,
244                                         ":",c->param_port);
245                                 BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
246                                 goto exit_loop;
247                                 }
248 #endif
249                         break;
250
251                 case BIO_CONN_S_CONNECT:
252                         BIO_clear_retry_flags(b);
253                         ret=connect(b->num,
254                                 (struct sockaddr *)&c->them,
255                                 sizeof(c->them));
256                         b->retry_reason=0;
257                         if (ret < 0)
258                                 {
259                                 if (BIO_sock_should_retry(ret))
260                                         {
261                                         BIO_set_retry_special(b);
262                                         c->state=BIO_CONN_S_BLOCKED_CONNECT;
263                                         b->retry_reason=BIO_RR_CONNECT;
264                                         }
265                                 else
266                                         {
267                                         SYSerr(SYS_F_CONNECT,get_last_socket_error());
268                                         ERR_add_error_data(4,"host=",
269                                                 c->param_hostname,
270                                                 ":",c->param_port);
271                                         BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
272                                         }
273                                 goto exit_loop;
274                                 }
275                         else
276                                 c->state=BIO_CONN_S_OK;
277                         break;
278
279                 case BIO_CONN_S_BLOCKED_CONNECT:
280                         i=BIO_sock_error(b->num);
281                         if (i)
282                                 {
283                                 BIO_clear_retry_flags(b);
284                                 SYSerr(SYS_F_CONNECT,i);
285                                 ERR_add_error_data(4,"host=",
286                                         c->param_hostname,
287                                         ":",c->param_port);
288                                 BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
289                                 ret=0;
290                                 goto exit_loop;
291                                 }
292                         else
293                                 c->state=BIO_CONN_S_OK;
294                         break;
295
296                 case BIO_CONN_S_OK:
297                         ret=1;
298                         goto exit_loop;
299                 default:
300                         abort();
301                         goto exit_loop;
302                         }
303
304                 if (cb != NULL)
305                         {
306                         if (!(ret=cb((BIO *)b,c->state,ret)))
307                                 goto end;
308                         }
309                 }
310
311         /* Loop does not exit */
312 exit_loop:
313         if (cb != NULL)
314                 ret=cb((BIO *)b,c->state,ret);
315 end:
316         return(ret);
317         }
318
319 BIO_CONNECT *BIO_CONNECT_new(void)
320         {
321         BIO_CONNECT *ret;
322
323         if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL)
324                 return(NULL);
325         ret->state=BIO_CONN_S_BEFORE;
326         ret->param_hostname=NULL;
327         ret->param_port=NULL;
328         ret->info_callback=NULL;
329         ret->nbio=0;
330         ret->ip[0]=0;
331         ret->ip[1]=0;
332         ret->ip[2]=0;
333         ret->ip[3]=0;
334         ret->port=0;
335         memset((char *)&ret->them,0,sizeof(ret->them));
336         return(ret);
337         }
338
339 void BIO_CONNECT_free(BIO_CONNECT *a)
340         {
341         if(a == NULL)
342             return;
343
344         if (a->param_hostname != NULL)
345                 Free(a->param_hostname);
346         if (a->param_port != NULL)
347                 Free(a->param_port);
348         Free(a);
349         }
350
351 BIO_METHOD *BIO_s_connect(void)
352         {
353         return(&methods_connectp);
354         }
355
356 static int conn_new(BIO *bi)
357         {
358         bi->init=0;
359         bi->num=INVALID_SOCKET;
360         bi->flags=0;
361         if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
362                 return(0);
363         else
364                 return(1);
365         }
366
367 static void conn_close_socket(BIO *bio)
368         {
369         BIO_CONNECT *c;
370
371         c=(BIO_CONNECT *)bio->ptr;
372         if (bio->num != INVALID_SOCKET)
373                 {
374                 /* Only do a shutdown if things were established */
375                 if (c->state == BIO_CONN_S_OK)
376                         shutdown(bio->num,2);
377                 closesocket(bio->num);
378                 bio->num=INVALID_SOCKET;
379                 }
380         }
381
382 static int conn_free(BIO *a)
383         {
384         BIO_CONNECT *data;
385
386         if (a == NULL) return(0);
387         data=(BIO_CONNECT *)a->ptr;
388          
389         if (a->shutdown)
390                 {
391                 conn_close_socket(a);
392                 BIO_CONNECT_free(data);
393                 a->ptr=NULL;
394                 a->flags=0;
395                 a->init=0;
396                 }
397         return(1);
398         }
399         
400 static int conn_read(BIO *b, char *out, int outl)
401         {
402         int ret=0;
403         BIO_CONNECT *data;
404
405         data=(BIO_CONNECT *)b->ptr;
406         if (data->state != BIO_CONN_S_OK)
407                 {
408                 ret=conn_state(b,data);
409                 if (ret <= 0)
410                                 return(ret);
411                 }
412
413         if (out != NULL)
414                 {
415                 clear_socket_error();
416                 ret=readsocket(b->num,out,outl);
417                 BIO_clear_retry_flags(b);
418                 if (ret <= 0)
419                         {
420                         if (BIO_sock_should_retry(ret))
421                                 BIO_set_retry_read(b);
422                         }
423                 }
424         return(ret);
425         }
426
427 static int conn_write(BIO *b, char *in, int inl)
428         {
429         int ret;
430         BIO_CONNECT *data;
431
432         data=(BIO_CONNECT *)b->ptr;
433         if (data->state != BIO_CONN_S_OK)
434                 {
435                 ret=conn_state(b,data);
436                 if (ret <= 0) return(ret);
437                 }
438
439         clear_socket_error();
440         ret=writesocket(b->num,in,inl);
441         BIO_clear_retry_flags(b);
442         if (ret <= 0)
443                 {
444                 if (BIO_sock_should_retry(ret))
445                         BIO_set_retry_write(b);
446                 }
447         return(ret);
448         }
449
450 static long conn_ctrl(BIO *b, int cmd, long num, char *ptr)
451         {
452         BIO *dbio;
453         int *ip;
454         const char **pptr;
455         long ret=1;
456         BIO_CONNECT *data;
457
458         data=(BIO_CONNECT *)b->ptr;
459
460         switch (cmd)
461                 {
462         case BIO_CTRL_RESET:
463                 ret=0;
464                 data->state=BIO_CONN_S_BEFORE;
465                 conn_close_socket(b);
466                 b->flags=0;
467                 break;
468         case BIO_C_DO_STATE_MACHINE:
469                 /* use this one to start the connection */
470                 if (!data->state != BIO_CONN_S_OK)
471                         ret=(long)conn_state(b,data);
472                 else
473                         ret=1;
474                 break;
475         case BIO_C_GET_CONNECT:
476                 if (ptr != NULL)
477                         {
478                         pptr=(const char **)ptr;
479                         if (num == 0)
480                                 {
481                                 *pptr=data->param_hostname;
482
483                                 }
484                         else if (num == 1)
485                                 {
486                                 *pptr=data->param_port;
487                                 }
488                         else if (num == 2)
489                                 {
490                                 *pptr= (char *)&(data->ip[0]);
491                                 }
492                         else if (num == 3)
493                                 {
494                                 *((int *)ptr)=data->port;
495                                 }
496                         if ((!b->init) || (ptr == NULL))
497                                 *pptr="not initalised";
498                         ret=1;
499                         }
500                 break;
501         case BIO_C_SET_CONNECT:
502                 if (ptr != NULL)
503                         {
504                         b->init=1;
505                         if (num == 0)
506                                 {
507                                 if (data->param_hostname != NULL)
508                                         Free(data->param_hostname);
509                                 data->param_hostname=BUF_strdup(ptr);
510                                 }
511                         else if (num == 1)
512                                 {
513                                 if (data->param_port != NULL)
514                                         Free(data->param_port);
515                                 data->param_port=BUF_strdup(ptr);
516                                 }
517                         else if (num == 2)
518                                 {
519                                 char buf[16];
520
521                                 sprintf(buf,"%d.%d.%d.%d",
522                                         ptr[0],ptr[1],ptr[2],ptr[3]);
523                                 if (data->param_hostname != NULL)
524                                         Free(data->param_hostname);
525                                 data->param_hostname=BUF_strdup(buf);
526                                 memcpy(&(data->ip[0]),ptr,4);
527                                 }
528                         else if (num == 3)
529                                 {
530                                 char buf[16];
531
532                                 sprintf(buf,"%d",*(int *)ptr);
533                                 if (data->param_port != NULL)
534                                         Free(data->param_port);
535                                 data->param_port=BUF_strdup(buf);
536                                 data->port= *(int *)ptr;
537                                 }
538                         }
539                 break;
540         case BIO_C_SET_NBIO:
541                 data->nbio=(int)num;
542                 break;
543         case BIO_C_GET_FD:
544                 if (b->init)
545                         {
546                         ip=(int *)ptr;
547                         if (ip != NULL)
548                                 *ip=b->num;
549                         ret=b->num;
550                         }
551                 else
552                         ret= -1;
553                 break;
554         case BIO_CTRL_GET_CLOSE:
555                 ret=b->shutdown;
556                 break;
557         case BIO_CTRL_SET_CLOSE:
558                 b->shutdown=(int)num;
559                 break;
560         case BIO_CTRL_PENDING:
561         case BIO_CTRL_WPENDING:
562                 ret=0;
563                 break;
564         case BIO_CTRL_FLUSH:
565                 break;
566         case BIO_CTRL_DUP:
567                 dbio=(BIO *)ptr;
568                 if (data->param_port)
569                         BIO_set_conn_port(dbio,data->param_port);
570                 if (data->param_hostname)
571                         BIO_set_conn_hostname(dbio,data->param_hostname);
572                 BIO_set_nbio(dbio,data->nbio);
573                 (void)BIO_set_info_callback(dbio,data->info_callback);
574                 break;
575         case BIO_CTRL_SET_CALLBACK:
576                 data->info_callback=(int (*)())ptr;
577                 break;
578         case BIO_CTRL_GET_CALLBACK:
579                 {
580                 int (**fptr)();
581
582                 fptr=(int (**)())ptr;
583                 *fptr=data->info_callback;
584                 }
585                 break;
586         default:
587                 ret=0;
588                 break;
589                 }
590         return(ret);
591         }
592
593 static int conn_puts(BIO *bp, char *str)
594         {
595         int n,ret;
596
597         n=strlen(str);
598         ret=conn_write(bp,str,n);
599         return(ret);
600         }
601
602 BIO *BIO_new_connect(char *str)
603         {
604         BIO *ret;
605
606         ret=BIO_new(BIO_s_connect());
607         if (ret == NULL) return(NULL);
608         if (BIO_set_conn_hostname(ret,str))
609                 return(ret);
610         else
611                 {
612                 BIO_free(ret);
613                 return(NULL);
614                 }
615         }
616
617 #endif
618