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