summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio/bss_conn.c
diff options
context:
space:
mode:
authormarkus <>2002-09-05 12:51:50 +0000
committermarkus <>2002-09-05 12:51:50 +0000
commit15b5d84f9da2ce4bfae8580e56e34a859f74ad71 (patch)
treebf939e82d7fd73cc8a01cf6959002209972091bc /src/lib/libcrypto/bio/bss_conn.c
parent027351f729b9e837200dae6e1520cda6577ab930 (diff)
downloadopenbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.gz
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.bz2
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.zip
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libcrypto/bio/bss_conn.c')
-rw-r--r--src/lib/libcrypto/bio/bss_conn.c220
1 files changed, 112 insertions, 108 deletions
diff --git a/src/lib/libcrypto/bio/bss_conn.c b/src/lib/libcrypto/bio/bss_conn.c
index 6e547bf866..f91ae4c8c6 100644
--- a/src/lib/libcrypto/bio/bss_conn.c
+++ b/src/lib/libcrypto/bio/bss_conn.c
@@ -56,22 +56,26 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#ifndef NO_SOCK 59#ifndef OPENSSL_NO_SOCK
60 60
61#include <stdio.h> 61#include <stdio.h>
62#include <errno.h> 62#include <errno.h>
63#define USE_SOCKETS 63#define USE_SOCKETS
64#include "cryptlib.h" 64#include "cryptlib.h"
65#include "bio.h" 65#include <openssl/bio.h>
66 66
67/* BIOerr(BIO_F_WSASTARTUP,BIO_R_WSASTARTUP ); */ 67#ifdef OPENSSL_SYS_WIN16
68
69#ifdef WIN16
70#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ 68#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
71#else 69#else
72#define SOCKET_PROTOCOL IPPROTO_TCP 70#define SOCKET_PROTOCOL IPPROTO_TCP
73#endif 71#endif
74 72
73#if (defined(OPENSSL_SYS_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
75typedef struct bio_connect_st 79typedef struct bio_connect_st
76 { 80 {
77 int state; 81 int state;
@@ -81,52 +85,32 @@ typedef struct bio_connect_st
81 int nbio; 85 int nbio;
82 86
83 unsigned char ip[4]; 87 unsigned char ip[4];
84 short port; 88 unsigned short port;
85 89
86 struct sockaddr_in them; 90 struct sockaddr_in them;
87 91
88 /* int socket; this will be kept in bio->num so that it is 92 /* int socket; this will be kept in bio->num so that it is
89 * compatable with the bss_sock bio */ 93 * compatible with the bss_sock bio */
90 int error;
91 94
92 /* called when the connection is initially made 95 /* called when the connection is initially made
93 * callback(BIO,state,ret); The callback should return 96 * callback(BIO,state,ret); The callback should return
94 * 'ret'. state is for compatablity with the ssl info_callback */ 97 * 'ret'. state is for compatibility with the ssl info_callback */
95 int (*info_callback)(); 98 int (*info_callback)(const BIO *bio,int state,int ret);
96 } BIO_CONNECT; 99 } BIO_CONNECT;
97 100
98#ifndef NOPROTO 101static int conn_write(BIO *h, const char *buf, int num);
99static int conn_write(BIO *h,char *buf,int num); 102static int conn_read(BIO *h, char *buf, int size);
100static int conn_read(BIO *h,char *buf,int size); 103static int conn_puts(BIO *h, const char *str);
101static int conn_puts(BIO *h,char *str); 104static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
102static long conn_ctrl(BIO *h,int cmd,long arg1,char *arg2);
103static int conn_new(BIO *h); 105static int conn_new(BIO *h);
104static int conn_free(BIO *data); 106static int conn_free(BIO *data);
105#else 107static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
106static int conn_write();
107static int conn_read();
108static int conn_puts();
109static long conn_ctrl();
110static int conn_new();
111static int conn_free();
112#endif
113
114#ifndef NOPROTO
115 108
116static int conn_state(BIO *b, BIO_CONNECT *c); 109static int conn_state(BIO *b, BIO_CONNECT *c);
117static void conn_close_socket(BIO *data); 110static void conn_close_socket(BIO *data);
118BIO_CONNECT *BIO_CONNECT_new(void ); 111BIO_CONNECT *BIO_CONNECT_new(void );
119void BIO_CONNECT_free(BIO_CONNECT *a); 112void BIO_CONNECT_free(BIO_CONNECT *a);
120 113
121#else
122
123static int conn_state();
124static void conn_close_socket();
125BIO_CONNECT *BIO_CONNECT_new();
126void BIO_CONNECT_free();
127
128#endif
129
130static BIO_METHOD methods_connectp= 114static BIO_METHOD methods_connectp=
131 { 115 {
132 BIO_TYPE_CONNECT, 116 BIO_TYPE_CONNECT,
@@ -138,11 +122,10 @@ static BIO_METHOD methods_connectp=
138 conn_ctrl, 122 conn_ctrl,
139 conn_new, 123 conn_new,
140 conn_free, 124 conn_free,
125 conn_callback_ctrl,
141 }; 126 };
142 127
143static int conn_state(b,c) 128static int conn_state(BIO *b, BIO_CONNECT *c)
144BIO *b;
145BIO_CONNECT *c;
146 { 129 {
147 int ret= -1,i; 130 int ret= -1,i;
148 unsigned long l; 131 unsigned long l;
@@ -160,7 +143,7 @@ BIO_CONNECT *c;
160 p=c->param_hostname; 143 p=c->param_hostname;
161 if (p == NULL) 144 if (p == NULL)
162 { 145 {
163 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTHNAME_SPECIFIED); 146 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
164 goto exit_loop; 147 goto exit_loop;
165 } 148 }
166 for ( ; *p != '\0'; p++) 149 for ( ; *p != '\0'; p++)
@@ -182,12 +165,12 @@ BIO_CONNECT *c;
182 break; 165 break;
183 } 166 }
184 if (c->param_port != NULL) 167 if (c->param_port != NULL)
185 Free(c->param_port); 168 OPENSSL_free(c->param_port);
186 c->param_port=BUF_strdup(p); 169 c->param_port=BUF_strdup(p);
187 } 170 }
188 } 171 }
189 172
190 if (p == NULL) 173 if (c->param_port == NULL)
191 { 174 {
192 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED); 175 BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
193 ERR_add_error_data(2,"host=",c->param_hostname); 176 ERR_add_error_data(2,"host=",c->param_hostname);
@@ -203,7 +186,12 @@ BIO_CONNECT *c;
203 break; 186 break;
204 187
205 case BIO_CONN_S_GET_PORT: 188 case BIO_CONN_S_GET_PORT:
206 if (BIO_get_port(c->param_port,&c->port) <= 0) 189 if (c->param_port == NULL)
190 {
191 /* abort(); */
192 goto exit_loop;
193 }
194 else if (BIO_get_port(c->param_port,&c->port) <= 0)
207 goto exit_loop; 195 goto exit_loop;
208 c->state=BIO_CONN_S_CREATE_SOCKET; 196 c->state=BIO_CONN_S_CREATE_SOCKET;
209 break; 197 break;
@@ -235,12 +223,9 @@ BIO_CONNECT *c;
235 break; 223 break;
236 224
237 case BIO_CONN_S_NBIO: 225 case BIO_CONN_S_NBIO:
238#ifdef FIONBIO
239 if (c->nbio) 226 if (c->nbio)
240 { 227 {
241 l=1; 228 if (!BIO_socket_nbio(b->num,1))
242 ret=BIO_socket_ioctl(b->num,FIONBIO,&l);
243 if (ret < 0)
244 { 229 {
245 BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO); 230 BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
246 ERR_add_error_data(4,"host=", 231 ERR_add_error_data(4,"host=",
@@ -249,10 +234,9 @@ BIO_CONNECT *c;
249 goto exit_loop; 234 goto exit_loop;
250 } 235 }
251 } 236 }
252#endif
253 c->state=BIO_CONN_S_CONNECT; 237 c->state=BIO_CONN_S_CONNECT;
254 238
255#ifdef SO_KEEPALIVE 239#if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
256 i=1; 240 i=1;
257 i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); 241 i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
258 if (i < 0) 242 if (i < 0)
@@ -315,7 +299,7 @@ BIO_CONNECT *c;
315 ret=1; 299 ret=1;
316 goto exit_loop; 300 goto exit_loop;
317 default: 301 default:
318 abort(); 302 /* abort(); */
319 goto exit_loop; 303 goto exit_loop;
320 } 304 }
321 305
@@ -326,21 +310,19 @@ BIO_CONNECT *c;
326 } 310 }
327 } 311 }
328 312
329 if (1) 313 /* Loop does not exit */
330 {
331exit_loop: 314exit_loop:
332 if (cb != NULL) 315 if (cb != NULL)
333 ret=cb((BIO *)b,c->state,ret); 316 ret=cb((BIO *)b,c->state,ret);
334 }
335end: 317end:
336 return(ret); 318 return(ret);
337 } 319 }
338 320
339BIO_CONNECT *BIO_CONNECT_new() 321BIO_CONNECT *BIO_CONNECT_new(void)
340 { 322 {
341 BIO_CONNECT *ret; 323 BIO_CONNECT *ret;
342 324
343 if ((ret=(BIO_CONNECT *)Malloc(sizeof(BIO_CONNECT))) == NULL) 325 if ((ret=(BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL)
344 return(NULL); 326 return(NULL);
345 ret->state=BIO_CONN_S_BEFORE; 327 ret->state=BIO_CONN_S_BEFORE;
346 ret->param_hostname=NULL; 328 ret->param_hostname=NULL;
@@ -353,27 +335,27 @@ BIO_CONNECT *BIO_CONNECT_new()
353 ret->ip[3]=0; 335 ret->ip[3]=0;
354 ret->port=0; 336 ret->port=0;
355 memset((char *)&ret->them,0,sizeof(ret->them)); 337 memset((char *)&ret->them,0,sizeof(ret->them));
356 ret->error=0;
357 return(ret); 338 return(ret);
358 } 339 }
359 340
360void BIO_CONNECT_free(a) 341void BIO_CONNECT_free(BIO_CONNECT *a)
361BIO_CONNECT *a;
362 { 342 {
343 if(a == NULL)
344 return;
345
363 if (a->param_hostname != NULL) 346 if (a->param_hostname != NULL)
364 Free(a->param_hostname); 347 OPENSSL_free(a->param_hostname);
365 if (a->param_port != NULL) 348 if (a->param_port != NULL)
366 Free(a->param_port); 349 OPENSSL_free(a->param_port);
367 Free(a); 350 OPENSSL_free(a);
368 } 351 }
369 352
370BIO_METHOD *BIO_s_connect() 353BIO_METHOD *BIO_s_connect(void)
371 { 354 {
372 return(&methods_connectp); 355 return(&methods_connectp);
373 } 356 }
374 357
375static int conn_new(bi) 358static int conn_new(BIO *bi)
376BIO *bi;
377 { 359 {
378 bi->init=0; 360 bi->init=0;
379 bi->num=INVALID_SOCKET; 361 bi->num=INVALID_SOCKET;
@@ -384,8 +366,7 @@ BIO *bi;
384 return(1); 366 return(1);
385 } 367 }
386 368
387static void conn_close_socket(bio) 369static void conn_close_socket(BIO *bio)
388BIO *bio;
389 { 370 {
390 BIO_CONNECT *c; 371 BIO_CONNECT *c;
391 372
@@ -395,17 +376,12 @@ BIO *bio;
395 /* Only do a shutdown if things were established */ 376 /* Only do a shutdown if things were established */
396 if (c->state == BIO_CONN_S_OK) 377 if (c->state == BIO_CONN_S_OK)
397 shutdown(bio->num,2); 378 shutdown(bio->num,2);
398# ifdef WINDOWS
399 closesocket(bio->num); 379 closesocket(bio->num);
400# else
401 close(bio->num);
402# endif
403 bio->num=INVALID_SOCKET; 380 bio->num=INVALID_SOCKET;
404 } 381 }
405 } 382 }
406 383
407static int conn_free(a) 384static int conn_free(BIO *a)
408BIO *a;
409 { 385 {
410 BIO_CONNECT *data; 386 BIO_CONNECT *data;
411 387
@@ -423,10 +399,7 @@ BIO *a;
423 return(1); 399 return(1);
424 } 400 }
425 401
426static int conn_read(b,out,outl) 402static int conn_read(BIO *b, char *out, int outl)
427BIO *b;
428char *out;
429int outl;
430 { 403 {
431 int ret=0; 404 int ret=0;
432 BIO_CONNECT *data; 405 BIO_CONNECT *data;
@@ -442,11 +415,7 @@ int outl;
442 if (out != NULL) 415 if (out != NULL)
443 { 416 {
444 clear_socket_error(); 417 clear_socket_error();
445#if defined(WINDOWS) 418 ret=readsocket(b->num,out,outl);
446 ret=recv(b->num,out,outl,0);
447#else
448 ret=read(b->num,out,outl);
449#endif
450 BIO_clear_retry_flags(b); 419 BIO_clear_retry_flags(b);
451 if (ret <= 0) 420 if (ret <= 0)
452 { 421 {
@@ -457,10 +426,7 @@ int outl;
457 return(ret); 426 return(ret);
458 } 427 }
459 428
460static int conn_write(b,in,inl) 429static int conn_write(BIO *b, const char *in, int inl)
461BIO *b;
462char *in;
463int inl;
464 { 430 {
465 int ret; 431 int ret;
466 BIO_CONNECT *data; 432 BIO_CONNECT *data;
@@ -473,11 +439,7 @@ int inl;
473 } 439 }
474 440
475 clear_socket_error(); 441 clear_socket_error();
476#if defined(WINDOWS) 442 ret=writesocket(b->num,in,inl);
477 ret=send(b->num,in,inl,0);
478#else
479 ret=write(b->num,in,inl);
480#endif
481 BIO_clear_retry_flags(b); 443 BIO_clear_retry_flags(b);
482 if (ret <= 0) 444 if (ret <= 0)
483 { 445 {
@@ -487,15 +449,11 @@ int inl;
487 return(ret); 449 return(ret);
488 } 450 }
489 451
490static long conn_ctrl(b,cmd,num,ptr) 452static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
491BIO *b;
492int cmd;
493long num;
494char *ptr;
495 { 453 {
496 BIO *dbio; 454 BIO *dbio;
497 int *ip; 455 int *ip;
498 char **pptr; 456 const char **pptr;
499 long ret=1; 457 long ret=1;
500 BIO_CONNECT *data; 458 BIO_CONNECT *data;
501 459
@@ -519,7 +477,7 @@ char *ptr;
519 case BIO_C_GET_CONNECT: 477 case BIO_C_GET_CONNECT:
520 if (ptr != NULL) 478 if (ptr != NULL)
521 { 479 {
522 pptr=(char **)ptr; 480 pptr=(const char **)ptr;
523 if (num == 0) 481 if (num == 0)
524 { 482 {
525 *pptr=data->param_hostname; 483 *pptr=data->param_hostname;
@@ -538,7 +496,7 @@ char *ptr;
538 *((int *)ptr)=data->port; 496 *((int *)ptr)=data->port;
539 } 497 }
540 if ((!b->init) || (ptr == NULL)) 498 if ((!b->init) || (ptr == NULL))
541 *pptr="not initalised"; 499 *pptr="not initialized";
542 ret=1; 500 ret=1;
543 } 501 }
544 break; 502 break;
@@ -549,19 +507,37 @@ char *ptr;
549 if (num == 0) 507 if (num == 0)
550 { 508 {
551 if (data->param_hostname != NULL) 509 if (data->param_hostname != NULL)
552 Free(data->param_hostname); 510 OPENSSL_free(data->param_hostname);
553 data->param_hostname=BUF_strdup(ptr); 511 data->param_hostname=BUF_strdup(ptr);
554 } 512 }
555 else if (num == 1) 513 else if (num == 1)
556 { 514 {
557 if (data->param_port != NULL) 515 if (data->param_port != NULL)
558 Free(data->param_port); 516 OPENSSL_free(data->param_port);
559 data->param_port=BUF_strdup(ptr); 517 data->param_port=BUF_strdup(ptr);
560 } 518 }
561 else if (num == 2) 519 else if (num == 2)
562 memcpy(data->ip,ptr,4); 520 {
521 char buf[16];
522 char *p = ptr;
523
524 sprintf(buf,"%d.%d.%d.%d",
525 p[0],p[1],p[2],p[3]);
526 if (data->param_hostname != NULL)
527 OPENSSL_free(data->param_hostname);
528 data->param_hostname=BUF_strdup(buf);
529 memcpy(&(data->ip[0]),ptr,4);
530 }
563 else if (num == 3) 531 else if (num == 3)
532 {
533 char buf[16];
534
535 sprintf(buf,"%d",*(int *)ptr);
536 if (data->param_port != NULL)
537 OPENSSL_free(data->param_port);
538 data->param_port=BUF_strdup(buf);
564 data->port= *(int *)ptr; 539 data->port= *(int *)ptr;
540 }
565 } 541 }
566 break; 542 break;
567 case BIO_C_SET_NBIO: 543 case BIO_C_SET_NBIO:
@@ -591,16 +567,26 @@ char *ptr;
591 case BIO_CTRL_FLUSH: 567 case BIO_CTRL_FLUSH:
592 break; 568 break;
593 case BIO_CTRL_DUP: 569 case BIO_CTRL_DUP:
570 {
594 dbio=(BIO *)ptr; 571 dbio=(BIO *)ptr;
595 if (data->param_port) 572 if (data->param_port)
596 BIO_set_conn_port(dbio,data->param_port); 573 BIO_set_conn_port(dbio,data->param_port);
597 if (data->param_hostname) 574 if (data->param_hostname)
598 BIO_set_conn_hostname(dbio,data->param_hostname); 575 BIO_set_conn_hostname(dbio,data->param_hostname);
599 BIO_set_nbio(dbio,data->nbio); 576 BIO_set_nbio(dbio,data->nbio);
600 BIO_set_info_callback(dbio,data->info_callback); 577 /* FIXME: the cast of the function seems unlikely to be a good idea */
578 (void)BIO_set_info_callback(dbio,(bio_info_cb *)data->info_callback);
579 }
601 break; 580 break;
602 case BIO_CTRL_SET_CALLBACK: 581 case BIO_CTRL_SET_CALLBACK:
603 data->info_callback=(int (*)())ptr; 582 {
583#if 0 /* FIXME: Should this be used? -- Richard Levitte */
584 BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
585 ret = -1;
586#else
587 ret=0;
588#endif
589 }
604 break; 590 break;
605 case BIO_CTRL_GET_CALLBACK: 591 case BIO_CTRL_GET_CALLBACK:
606 { 592 {
@@ -617,9 +603,28 @@ char *ptr;
617 return(ret); 603 return(ret);
618 } 604 }
619 605
620static int conn_puts(bp,str) 606static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
621BIO *bp; 607 {
622char *str; 608 long ret=1;
609 BIO_CONNECT *data;
610
611 data=(BIO_CONNECT *)b->ptr;
612
613 switch (cmd)
614 {
615 case BIO_CTRL_SET_CALLBACK:
616 {
617 data->info_callback=(int (*)(const struct bio_st *, int, int))fp;
618 }
619 break;
620 default:
621 ret=0;
622 break;
623 }
624 return(ret);
625 }
626
627static int conn_puts(BIO *bp, const char *str)
623 { 628 {
624 int n,ret; 629 int n,ret;
625 630
@@ -628,8 +633,7 @@ char *str;
628 return(ret); 633 return(ret);
629 } 634 }
630 635
631BIO *BIO_new_connect(str) 636BIO *BIO_new_connect(char *str)
632char *str;
633 { 637 {
634 BIO *ret; 638 BIO *ret;
635 639