1*0Sstevel@tonic-gate /* crypto/bio/bss_conn.c */ 2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * This package is an SSL implementation written 6*0Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 7*0Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 10*0Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 11*0Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 12*0Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*0Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 14*0Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*0Sstevel@tonic-gate * 16*0Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 17*0Sstevel@tonic-gate * the code are not to be removed. 18*0Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 19*0Sstevel@tonic-gate * as the author of the parts of the library used. 20*0Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 21*0Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 22*0Sstevel@tonic-gate * 23*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 24*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 25*0Sstevel@tonic-gate * are met: 26*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 27*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 28*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 29*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 30*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 31*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 32*0Sstevel@tonic-gate * must display the following acknowledgement: 33*0Sstevel@tonic-gate * "This product includes cryptographic software written by 34*0Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 35*0Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 36*0Sstevel@tonic-gate * being used are not cryptographic related :-). 37*0Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 38*0Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 39*0Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*0Sstevel@tonic-gate * SUCH DAMAGE. 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 54*0Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 55*0Sstevel@tonic-gate * copied and put under another distribution licence 56*0Sstevel@tonic-gate * [including the GNU Public Licence.] 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SOCK 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #include <stdio.h> 62*0Sstevel@tonic-gate #include <errno.h> 63*0Sstevel@tonic-gate #define USE_SOCKETS 64*0Sstevel@tonic-gate #include "cryptlib.h" 65*0Sstevel@tonic-gate #include <openssl/bio.h> 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN16 68*0Sstevel@tonic-gate #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */ 69*0Sstevel@tonic-gate #else 70*0Sstevel@tonic-gate #define SOCKET_PROTOCOL IPPROTO_TCP 71*0Sstevel@tonic-gate #endif 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000) 74*0Sstevel@tonic-gate /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ 75*0Sstevel@tonic-gate #undef FIONBIO 76*0Sstevel@tonic-gate #endif 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate typedef struct bio_connect_st 80*0Sstevel@tonic-gate { 81*0Sstevel@tonic-gate int state; 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate char *param_hostname; 84*0Sstevel@tonic-gate char *param_port; 85*0Sstevel@tonic-gate int nbio; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate unsigned char ip[4]; 88*0Sstevel@tonic-gate unsigned short port; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate struct sockaddr_in them; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate /* int socket; this will be kept in bio->num so that it is 93*0Sstevel@tonic-gate * compatible with the bss_sock bio */ 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate /* called when the connection is initially made 96*0Sstevel@tonic-gate * callback(BIO,state,ret); The callback should return 97*0Sstevel@tonic-gate * 'ret'. state is for compatibility with the ssl info_callback */ 98*0Sstevel@tonic-gate int (*info_callback)(const BIO *bio,int state,int ret); 99*0Sstevel@tonic-gate } BIO_CONNECT; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate static int conn_write(BIO *h, const char *buf, int num); 102*0Sstevel@tonic-gate static int conn_read(BIO *h, char *buf, int size); 103*0Sstevel@tonic-gate static int conn_puts(BIO *h, const char *str); 104*0Sstevel@tonic-gate static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2); 105*0Sstevel@tonic-gate static int conn_new(BIO *h); 106*0Sstevel@tonic-gate static int conn_free(BIO *data); 107*0Sstevel@tonic-gate static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *); 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate static int conn_state(BIO *b, BIO_CONNECT *c); 110*0Sstevel@tonic-gate static void conn_close_socket(BIO *data); 111*0Sstevel@tonic-gate BIO_CONNECT *BIO_CONNECT_new(void ); 112*0Sstevel@tonic-gate void BIO_CONNECT_free(BIO_CONNECT *a); 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate static BIO_METHOD methods_connectp= 115*0Sstevel@tonic-gate { 116*0Sstevel@tonic-gate BIO_TYPE_CONNECT, 117*0Sstevel@tonic-gate "socket connect", 118*0Sstevel@tonic-gate conn_write, 119*0Sstevel@tonic-gate conn_read, 120*0Sstevel@tonic-gate conn_puts, 121*0Sstevel@tonic-gate NULL, /* connect_gets, */ 122*0Sstevel@tonic-gate conn_ctrl, 123*0Sstevel@tonic-gate conn_new, 124*0Sstevel@tonic-gate conn_free, 125*0Sstevel@tonic-gate conn_callback_ctrl, 126*0Sstevel@tonic-gate }; 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate static int conn_state(BIO *b, BIO_CONNECT *c) 129*0Sstevel@tonic-gate { 130*0Sstevel@tonic-gate int ret= -1,i; 131*0Sstevel@tonic-gate unsigned long l; 132*0Sstevel@tonic-gate char *p,*q; 133*0Sstevel@tonic-gate int (*cb)()=NULL; 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate if (c->info_callback != NULL) 136*0Sstevel@tonic-gate cb=c->info_callback; 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate for (;;) 139*0Sstevel@tonic-gate { 140*0Sstevel@tonic-gate switch (c->state) 141*0Sstevel@tonic-gate { 142*0Sstevel@tonic-gate case BIO_CONN_S_BEFORE: 143*0Sstevel@tonic-gate p=c->param_hostname; 144*0Sstevel@tonic-gate if (p == NULL) 145*0Sstevel@tonic-gate { 146*0Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED); 147*0Sstevel@tonic-gate goto exit_loop; 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate for ( ; *p != '\0'; p++) 150*0Sstevel@tonic-gate { 151*0Sstevel@tonic-gate if ((*p == ':') || (*p == '/')) break; 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate i= *p; 155*0Sstevel@tonic-gate if ((i == ':') || (i == '/')) 156*0Sstevel@tonic-gate { 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate *(p++)='\0'; 159*0Sstevel@tonic-gate if (i == ':') 160*0Sstevel@tonic-gate { 161*0Sstevel@tonic-gate for (q=p; *q; q++) 162*0Sstevel@tonic-gate if (*q == '/') 163*0Sstevel@tonic-gate { 164*0Sstevel@tonic-gate *q='\0'; 165*0Sstevel@tonic-gate break; 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate if (c->param_port != NULL) 168*0Sstevel@tonic-gate OPENSSL_free(c->param_port); 169*0Sstevel@tonic-gate c->param_port=BUF_strdup(p); 170*0Sstevel@tonic-gate } 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate if (c->param_port == NULL) 174*0Sstevel@tonic-gate { 175*0Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED); 176*0Sstevel@tonic-gate ERR_add_error_data(2,"host=",c->param_hostname); 177*0Sstevel@tonic-gate goto exit_loop; 178*0Sstevel@tonic-gate } 179*0Sstevel@tonic-gate c->state=BIO_CONN_S_GET_IP; 180*0Sstevel@tonic-gate break; 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate case BIO_CONN_S_GET_IP: 183*0Sstevel@tonic-gate if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0) 184*0Sstevel@tonic-gate goto exit_loop; 185*0Sstevel@tonic-gate c->state=BIO_CONN_S_GET_PORT; 186*0Sstevel@tonic-gate break; 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate case BIO_CONN_S_GET_PORT: 189*0Sstevel@tonic-gate if (c->param_port == NULL) 190*0Sstevel@tonic-gate { 191*0Sstevel@tonic-gate /* abort(); */ 192*0Sstevel@tonic-gate goto exit_loop; 193*0Sstevel@tonic-gate } 194*0Sstevel@tonic-gate else if (BIO_get_port(c->param_port,&c->port) <= 0) 195*0Sstevel@tonic-gate goto exit_loop; 196*0Sstevel@tonic-gate c->state=BIO_CONN_S_CREATE_SOCKET; 197*0Sstevel@tonic-gate break; 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate case BIO_CONN_S_CREATE_SOCKET: 200*0Sstevel@tonic-gate /* now setup address */ 201*0Sstevel@tonic-gate memset((char *)&c->them,0,sizeof(c->them)); 202*0Sstevel@tonic-gate c->them.sin_family=AF_INET; 203*0Sstevel@tonic-gate c->them.sin_port=htons((unsigned short)c->port); 204*0Sstevel@tonic-gate l=(unsigned long) 205*0Sstevel@tonic-gate ((unsigned long)c->ip[0]<<24L)| 206*0Sstevel@tonic-gate ((unsigned long)c->ip[1]<<16L)| 207*0Sstevel@tonic-gate ((unsigned long)c->ip[2]<< 8L)| 208*0Sstevel@tonic-gate ((unsigned long)c->ip[3]); 209*0Sstevel@tonic-gate c->them.sin_addr.s_addr=htonl(l); 210*0Sstevel@tonic-gate c->state=BIO_CONN_S_CREATE_SOCKET; 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); 213*0Sstevel@tonic-gate if (ret == INVALID_SOCKET) 214*0Sstevel@tonic-gate { 215*0Sstevel@tonic-gate SYSerr(SYS_F_SOCKET,get_last_socket_error()); 216*0Sstevel@tonic-gate ERR_add_error_data(4,"host=",c->param_hostname, 217*0Sstevel@tonic-gate ":",c->param_port); 218*0Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET); 219*0Sstevel@tonic-gate goto exit_loop; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate b->num=ret; 222*0Sstevel@tonic-gate c->state=BIO_CONN_S_NBIO; 223*0Sstevel@tonic-gate break; 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate case BIO_CONN_S_NBIO: 226*0Sstevel@tonic-gate if (c->nbio) 227*0Sstevel@tonic-gate { 228*0Sstevel@tonic-gate if (!BIO_socket_nbio(b->num,1)) 229*0Sstevel@tonic-gate { 230*0Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO); 231*0Sstevel@tonic-gate ERR_add_error_data(4,"host=", 232*0Sstevel@tonic-gate c->param_hostname, 233*0Sstevel@tonic-gate ":",c->param_port); 234*0Sstevel@tonic-gate goto exit_loop; 235*0Sstevel@tonic-gate } 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate c->state=BIO_CONN_S_CONNECT; 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate #if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE) 240*0Sstevel@tonic-gate i=1; 241*0Sstevel@tonic-gate i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i)); 242*0Sstevel@tonic-gate if (i < 0) 243*0Sstevel@tonic-gate { 244*0Sstevel@tonic-gate SYSerr(SYS_F_SOCKET,get_last_socket_error()); 245*0Sstevel@tonic-gate ERR_add_error_data(4,"host=",c->param_hostname, 246*0Sstevel@tonic-gate ":",c->param_port); 247*0Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE); 248*0Sstevel@tonic-gate goto exit_loop; 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate #endif 251*0Sstevel@tonic-gate break; 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate case BIO_CONN_S_CONNECT: 254*0Sstevel@tonic-gate BIO_clear_retry_flags(b); 255*0Sstevel@tonic-gate ret=connect(b->num, 256*0Sstevel@tonic-gate (struct sockaddr *)&c->them, 257*0Sstevel@tonic-gate sizeof(c->them)); 258*0Sstevel@tonic-gate b->retry_reason=0; 259*0Sstevel@tonic-gate if (ret < 0) 260*0Sstevel@tonic-gate { 261*0Sstevel@tonic-gate if (BIO_sock_should_retry(ret)) 262*0Sstevel@tonic-gate { 263*0Sstevel@tonic-gate BIO_set_retry_special(b); 264*0Sstevel@tonic-gate c->state=BIO_CONN_S_BLOCKED_CONNECT; 265*0Sstevel@tonic-gate b->retry_reason=BIO_RR_CONNECT; 266*0Sstevel@tonic-gate } 267*0Sstevel@tonic-gate else 268*0Sstevel@tonic-gate { 269*0Sstevel@tonic-gate SYSerr(SYS_F_CONNECT,get_last_socket_error()); 270*0Sstevel@tonic-gate ERR_add_error_data(4,"host=", 271*0Sstevel@tonic-gate c->param_hostname, 272*0Sstevel@tonic-gate ":",c->param_port); 273*0Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR); 274*0Sstevel@tonic-gate } 275*0Sstevel@tonic-gate goto exit_loop; 276*0Sstevel@tonic-gate } 277*0Sstevel@tonic-gate else 278*0Sstevel@tonic-gate c->state=BIO_CONN_S_OK; 279*0Sstevel@tonic-gate break; 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate case BIO_CONN_S_BLOCKED_CONNECT: 282*0Sstevel@tonic-gate i=BIO_sock_error(b->num); 283*0Sstevel@tonic-gate if (i) 284*0Sstevel@tonic-gate { 285*0Sstevel@tonic-gate BIO_clear_retry_flags(b); 286*0Sstevel@tonic-gate SYSerr(SYS_F_CONNECT,i); 287*0Sstevel@tonic-gate ERR_add_error_data(4,"host=", 288*0Sstevel@tonic-gate c->param_hostname, 289*0Sstevel@tonic-gate ":",c->param_port); 290*0Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR); 291*0Sstevel@tonic-gate ret=0; 292*0Sstevel@tonic-gate goto exit_loop; 293*0Sstevel@tonic-gate } 294*0Sstevel@tonic-gate else 295*0Sstevel@tonic-gate c->state=BIO_CONN_S_OK; 296*0Sstevel@tonic-gate break; 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate case BIO_CONN_S_OK: 299*0Sstevel@tonic-gate ret=1; 300*0Sstevel@tonic-gate goto exit_loop; 301*0Sstevel@tonic-gate default: 302*0Sstevel@tonic-gate /* abort(); */ 303*0Sstevel@tonic-gate goto exit_loop; 304*0Sstevel@tonic-gate } 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate if (cb != NULL) 307*0Sstevel@tonic-gate { 308*0Sstevel@tonic-gate if (!(ret=cb((BIO *)b,c->state,ret))) 309*0Sstevel@tonic-gate goto end; 310*0Sstevel@tonic-gate } 311*0Sstevel@tonic-gate } 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate /* Loop does not exit */ 314*0Sstevel@tonic-gate exit_loop: 315*0Sstevel@tonic-gate if (cb != NULL) 316*0Sstevel@tonic-gate ret=cb((BIO *)b,c->state,ret); 317*0Sstevel@tonic-gate end: 318*0Sstevel@tonic-gate return(ret); 319*0Sstevel@tonic-gate } 320*0Sstevel@tonic-gate 321*0Sstevel@tonic-gate BIO_CONNECT *BIO_CONNECT_new(void) 322*0Sstevel@tonic-gate { 323*0Sstevel@tonic-gate BIO_CONNECT *ret; 324*0Sstevel@tonic-gate 325*0Sstevel@tonic-gate if ((ret=(BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL) 326*0Sstevel@tonic-gate return(NULL); 327*0Sstevel@tonic-gate ret->state=BIO_CONN_S_BEFORE; 328*0Sstevel@tonic-gate ret->param_hostname=NULL; 329*0Sstevel@tonic-gate ret->param_port=NULL; 330*0Sstevel@tonic-gate ret->info_callback=NULL; 331*0Sstevel@tonic-gate ret->nbio=0; 332*0Sstevel@tonic-gate ret->ip[0]=0; 333*0Sstevel@tonic-gate ret->ip[1]=0; 334*0Sstevel@tonic-gate ret->ip[2]=0; 335*0Sstevel@tonic-gate ret->ip[3]=0; 336*0Sstevel@tonic-gate ret->port=0; 337*0Sstevel@tonic-gate memset((char *)&ret->them,0,sizeof(ret->them)); 338*0Sstevel@tonic-gate return(ret); 339*0Sstevel@tonic-gate } 340*0Sstevel@tonic-gate 341*0Sstevel@tonic-gate void BIO_CONNECT_free(BIO_CONNECT *a) 342*0Sstevel@tonic-gate { 343*0Sstevel@tonic-gate if(a == NULL) 344*0Sstevel@tonic-gate return; 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gate if (a->param_hostname != NULL) 347*0Sstevel@tonic-gate OPENSSL_free(a->param_hostname); 348*0Sstevel@tonic-gate if (a->param_port != NULL) 349*0Sstevel@tonic-gate OPENSSL_free(a->param_port); 350*0Sstevel@tonic-gate OPENSSL_free(a); 351*0Sstevel@tonic-gate } 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate BIO_METHOD *BIO_s_connect(void) 354*0Sstevel@tonic-gate { 355*0Sstevel@tonic-gate return(&methods_connectp); 356*0Sstevel@tonic-gate } 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate static int conn_new(BIO *bi) 359*0Sstevel@tonic-gate { 360*0Sstevel@tonic-gate bi->init=0; 361*0Sstevel@tonic-gate bi->num=INVALID_SOCKET; 362*0Sstevel@tonic-gate bi->flags=0; 363*0Sstevel@tonic-gate if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL) 364*0Sstevel@tonic-gate return(0); 365*0Sstevel@tonic-gate else 366*0Sstevel@tonic-gate return(1); 367*0Sstevel@tonic-gate } 368*0Sstevel@tonic-gate 369*0Sstevel@tonic-gate static void conn_close_socket(BIO *bio) 370*0Sstevel@tonic-gate { 371*0Sstevel@tonic-gate BIO_CONNECT *c; 372*0Sstevel@tonic-gate 373*0Sstevel@tonic-gate c=(BIO_CONNECT *)bio->ptr; 374*0Sstevel@tonic-gate if (bio->num != INVALID_SOCKET) 375*0Sstevel@tonic-gate { 376*0Sstevel@tonic-gate /* Only do a shutdown if things were established */ 377*0Sstevel@tonic-gate if (c->state == BIO_CONN_S_OK) 378*0Sstevel@tonic-gate shutdown(bio->num,2); 379*0Sstevel@tonic-gate closesocket(bio->num); 380*0Sstevel@tonic-gate bio->num=INVALID_SOCKET; 381*0Sstevel@tonic-gate } 382*0Sstevel@tonic-gate } 383*0Sstevel@tonic-gate 384*0Sstevel@tonic-gate static int conn_free(BIO *a) 385*0Sstevel@tonic-gate { 386*0Sstevel@tonic-gate BIO_CONNECT *data; 387*0Sstevel@tonic-gate 388*0Sstevel@tonic-gate if (a == NULL) return(0); 389*0Sstevel@tonic-gate data=(BIO_CONNECT *)a->ptr; 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate if (a->shutdown) 392*0Sstevel@tonic-gate { 393*0Sstevel@tonic-gate conn_close_socket(a); 394*0Sstevel@tonic-gate BIO_CONNECT_free(data); 395*0Sstevel@tonic-gate a->ptr=NULL; 396*0Sstevel@tonic-gate a->flags=0; 397*0Sstevel@tonic-gate a->init=0; 398*0Sstevel@tonic-gate } 399*0Sstevel@tonic-gate return(1); 400*0Sstevel@tonic-gate } 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate static int conn_read(BIO *b, char *out, int outl) 403*0Sstevel@tonic-gate { 404*0Sstevel@tonic-gate int ret=0; 405*0Sstevel@tonic-gate BIO_CONNECT *data; 406*0Sstevel@tonic-gate 407*0Sstevel@tonic-gate data=(BIO_CONNECT *)b->ptr; 408*0Sstevel@tonic-gate if (data->state != BIO_CONN_S_OK) 409*0Sstevel@tonic-gate { 410*0Sstevel@tonic-gate ret=conn_state(b,data); 411*0Sstevel@tonic-gate if (ret <= 0) 412*0Sstevel@tonic-gate return(ret); 413*0Sstevel@tonic-gate } 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate if (out != NULL) 416*0Sstevel@tonic-gate { 417*0Sstevel@tonic-gate clear_socket_error(); 418*0Sstevel@tonic-gate ret=readsocket(b->num,out,outl); 419*0Sstevel@tonic-gate BIO_clear_retry_flags(b); 420*0Sstevel@tonic-gate if (ret <= 0) 421*0Sstevel@tonic-gate { 422*0Sstevel@tonic-gate if (BIO_sock_should_retry(ret)) 423*0Sstevel@tonic-gate BIO_set_retry_read(b); 424*0Sstevel@tonic-gate } 425*0Sstevel@tonic-gate } 426*0Sstevel@tonic-gate return(ret); 427*0Sstevel@tonic-gate } 428*0Sstevel@tonic-gate 429*0Sstevel@tonic-gate static int conn_write(BIO *b, const char *in, int inl) 430*0Sstevel@tonic-gate { 431*0Sstevel@tonic-gate int ret; 432*0Sstevel@tonic-gate BIO_CONNECT *data; 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gate data=(BIO_CONNECT *)b->ptr; 435*0Sstevel@tonic-gate if (data->state != BIO_CONN_S_OK) 436*0Sstevel@tonic-gate { 437*0Sstevel@tonic-gate ret=conn_state(b,data); 438*0Sstevel@tonic-gate if (ret <= 0) return(ret); 439*0Sstevel@tonic-gate } 440*0Sstevel@tonic-gate 441*0Sstevel@tonic-gate clear_socket_error(); 442*0Sstevel@tonic-gate ret=writesocket(b->num,in,inl); 443*0Sstevel@tonic-gate BIO_clear_retry_flags(b); 444*0Sstevel@tonic-gate if (ret <= 0) 445*0Sstevel@tonic-gate { 446*0Sstevel@tonic-gate if (BIO_sock_should_retry(ret)) 447*0Sstevel@tonic-gate BIO_set_retry_write(b); 448*0Sstevel@tonic-gate } 449*0Sstevel@tonic-gate return(ret); 450*0Sstevel@tonic-gate } 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) 453*0Sstevel@tonic-gate { 454*0Sstevel@tonic-gate BIO *dbio; 455*0Sstevel@tonic-gate int *ip; 456*0Sstevel@tonic-gate const char **pptr; 457*0Sstevel@tonic-gate long ret=1; 458*0Sstevel@tonic-gate BIO_CONNECT *data; 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate data=(BIO_CONNECT *)b->ptr; 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate switch (cmd) 463*0Sstevel@tonic-gate { 464*0Sstevel@tonic-gate case BIO_CTRL_RESET: 465*0Sstevel@tonic-gate ret=0; 466*0Sstevel@tonic-gate data->state=BIO_CONN_S_BEFORE; 467*0Sstevel@tonic-gate conn_close_socket(b); 468*0Sstevel@tonic-gate b->flags=0; 469*0Sstevel@tonic-gate break; 470*0Sstevel@tonic-gate case BIO_C_DO_STATE_MACHINE: 471*0Sstevel@tonic-gate /* use this one to start the connection */ 472*0Sstevel@tonic-gate if (!data->state != BIO_CONN_S_OK) 473*0Sstevel@tonic-gate ret=(long)conn_state(b,data); 474*0Sstevel@tonic-gate else 475*0Sstevel@tonic-gate ret=1; 476*0Sstevel@tonic-gate break; 477*0Sstevel@tonic-gate case BIO_C_GET_CONNECT: 478*0Sstevel@tonic-gate if (ptr != NULL) 479*0Sstevel@tonic-gate { 480*0Sstevel@tonic-gate pptr=(const char **)ptr; 481*0Sstevel@tonic-gate if (num == 0) 482*0Sstevel@tonic-gate { 483*0Sstevel@tonic-gate *pptr=data->param_hostname; 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate } 486*0Sstevel@tonic-gate else if (num == 1) 487*0Sstevel@tonic-gate { 488*0Sstevel@tonic-gate *pptr=data->param_port; 489*0Sstevel@tonic-gate } 490*0Sstevel@tonic-gate else if (num == 2) 491*0Sstevel@tonic-gate { 492*0Sstevel@tonic-gate *pptr= (char *)&(data->ip[0]); 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate else if (num == 3) 495*0Sstevel@tonic-gate { 496*0Sstevel@tonic-gate *((int *)ptr)=data->port; 497*0Sstevel@tonic-gate } 498*0Sstevel@tonic-gate if ((!b->init) || (ptr == NULL)) 499*0Sstevel@tonic-gate *pptr="not initialized"; 500*0Sstevel@tonic-gate ret=1; 501*0Sstevel@tonic-gate } 502*0Sstevel@tonic-gate break; 503*0Sstevel@tonic-gate case BIO_C_SET_CONNECT: 504*0Sstevel@tonic-gate if (ptr != NULL) 505*0Sstevel@tonic-gate { 506*0Sstevel@tonic-gate b->init=1; 507*0Sstevel@tonic-gate if (num == 0) 508*0Sstevel@tonic-gate { 509*0Sstevel@tonic-gate if (data->param_hostname != NULL) 510*0Sstevel@tonic-gate OPENSSL_free(data->param_hostname); 511*0Sstevel@tonic-gate data->param_hostname=BUF_strdup(ptr); 512*0Sstevel@tonic-gate } 513*0Sstevel@tonic-gate else if (num == 1) 514*0Sstevel@tonic-gate { 515*0Sstevel@tonic-gate if (data->param_port != NULL) 516*0Sstevel@tonic-gate OPENSSL_free(data->param_port); 517*0Sstevel@tonic-gate data->param_port=BUF_strdup(ptr); 518*0Sstevel@tonic-gate } 519*0Sstevel@tonic-gate else if (num == 2) 520*0Sstevel@tonic-gate { 521*0Sstevel@tonic-gate char buf[16]; 522*0Sstevel@tonic-gate unsigned char *p = ptr; 523*0Sstevel@tonic-gate 524*0Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"%d.%d.%d.%d", 525*0Sstevel@tonic-gate p[0],p[1],p[2],p[3]); 526*0Sstevel@tonic-gate if (data->param_hostname != NULL) 527*0Sstevel@tonic-gate OPENSSL_free(data->param_hostname); 528*0Sstevel@tonic-gate data->param_hostname=BUF_strdup(buf); 529*0Sstevel@tonic-gate memcpy(&(data->ip[0]),ptr,4); 530*0Sstevel@tonic-gate } 531*0Sstevel@tonic-gate else if (num == 3) 532*0Sstevel@tonic-gate { 533*0Sstevel@tonic-gate char buf[DECIMAL_SIZE(int)+1]; 534*0Sstevel@tonic-gate 535*0Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"%d",*(int *)ptr); 536*0Sstevel@tonic-gate if (data->param_port != NULL) 537*0Sstevel@tonic-gate OPENSSL_free(data->param_port); 538*0Sstevel@tonic-gate data->param_port=BUF_strdup(buf); 539*0Sstevel@tonic-gate data->port= *(int *)ptr; 540*0Sstevel@tonic-gate } 541*0Sstevel@tonic-gate } 542*0Sstevel@tonic-gate break; 543*0Sstevel@tonic-gate case BIO_C_SET_NBIO: 544*0Sstevel@tonic-gate data->nbio=(int)num; 545*0Sstevel@tonic-gate break; 546*0Sstevel@tonic-gate case BIO_C_GET_FD: 547*0Sstevel@tonic-gate if (b->init) 548*0Sstevel@tonic-gate { 549*0Sstevel@tonic-gate ip=(int *)ptr; 550*0Sstevel@tonic-gate if (ip != NULL) 551*0Sstevel@tonic-gate *ip=b->num; 552*0Sstevel@tonic-gate ret=b->num; 553*0Sstevel@tonic-gate } 554*0Sstevel@tonic-gate else 555*0Sstevel@tonic-gate ret= -1; 556*0Sstevel@tonic-gate break; 557*0Sstevel@tonic-gate case BIO_CTRL_GET_CLOSE: 558*0Sstevel@tonic-gate ret=b->shutdown; 559*0Sstevel@tonic-gate break; 560*0Sstevel@tonic-gate case BIO_CTRL_SET_CLOSE: 561*0Sstevel@tonic-gate b->shutdown=(int)num; 562*0Sstevel@tonic-gate break; 563*0Sstevel@tonic-gate case BIO_CTRL_PENDING: 564*0Sstevel@tonic-gate case BIO_CTRL_WPENDING: 565*0Sstevel@tonic-gate ret=0; 566*0Sstevel@tonic-gate break; 567*0Sstevel@tonic-gate case BIO_CTRL_FLUSH: 568*0Sstevel@tonic-gate break; 569*0Sstevel@tonic-gate case BIO_CTRL_DUP: 570*0Sstevel@tonic-gate { 571*0Sstevel@tonic-gate dbio=(BIO *)ptr; 572*0Sstevel@tonic-gate if (data->param_port) 573*0Sstevel@tonic-gate BIO_set_conn_port(dbio,data->param_port); 574*0Sstevel@tonic-gate if (data->param_hostname) 575*0Sstevel@tonic-gate BIO_set_conn_hostname(dbio,data->param_hostname); 576*0Sstevel@tonic-gate BIO_set_nbio(dbio,data->nbio); 577*0Sstevel@tonic-gate /* FIXME: the cast of the function seems unlikely to be a good idea */ 578*0Sstevel@tonic-gate (void)BIO_set_info_callback(dbio,(bio_info_cb *)data->info_callback); 579*0Sstevel@tonic-gate } 580*0Sstevel@tonic-gate break; 581*0Sstevel@tonic-gate case BIO_CTRL_SET_CALLBACK: 582*0Sstevel@tonic-gate { 583*0Sstevel@tonic-gate #if 0 /* FIXME: Should this be used? -- Richard Levitte */ 584*0Sstevel@tonic-gate BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 585*0Sstevel@tonic-gate ret = -1; 586*0Sstevel@tonic-gate #else 587*0Sstevel@tonic-gate ret=0; 588*0Sstevel@tonic-gate #endif 589*0Sstevel@tonic-gate } 590*0Sstevel@tonic-gate break; 591*0Sstevel@tonic-gate case BIO_CTRL_GET_CALLBACK: 592*0Sstevel@tonic-gate { 593*0Sstevel@tonic-gate int (**fptr)(); 594*0Sstevel@tonic-gate 595*0Sstevel@tonic-gate fptr=(int (**)())ptr; 596*0Sstevel@tonic-gate *fptr=data->info_callback; 597*0Sstevel@tonic-gate } 598*0Sstevel@tonic-gate break; 599*0Sstevel@tonic-gate default: 600*0Sstevel@tonic-gate ret=0; 601*0Sstevel@tonic-gate break; 602*0Sstevel@tonic-gate } 603*0Sstevel@tonic-gate return(ret); 604*0Sstevel@tonic-gate } 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gate static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 607*0Sstevel@tonic-gate { 608*0Sstevel@tonic-gate long ret=1; 609*0Sstevel@tonic-gate BIO_CONNECT *data; 610*0Sstevel@tonic-gate 611*0Sstevel@tonic-gate data=(BIO_CONNECT *)b->ptr; 612*0Sstevel@tonic-gate 613*0Sstevel@tonic-gate switch (cmd) 614*0Sstevel@tonic-gate { 615*0Sstevel@tonic-gate case BIO_CTRL_SET_CALLBACK: 616*0Sstevel@tonic-gate { 617*0Sstevel@tonic-gate data->info_callback=(int (*)(const struct bio_st *, int, int))fp; 618*0Sstevel@tonic-gate } 619*0Sstevel@tonic-gate break; 620*0Sstevel@tonic-gate default: 621*0Sstevel@tonic-gate ret=0; 622*0Sstevel@tonic-gate break; 623*0Sstevel@tonic-gate } 624*0Sstevel@tonic-gate return(ret); 625*0Sstevel@tonic-gate } 626*0Sstevel@tonic-gate 627*0Sstevel@tonic-gate static int conn_puts(BIO *bp, const char *str) 628*0Sstevel@tonic-gate { 629*0Sstevel@tonic-gate int n,ret; 630*0Sstevel@tonic-gate 631*0Sstevel@tonic-gate n=strlen(str); 632*0Sstevel@tonic-gate ret=conn_write(bp,str,n); 633*0Sstevel@tonic-gate return(ret); 634*0Sstevel@tonic-gate } 635*0Sstevel@tonic-gate 636*0Sstevel@tonic-gate BIO *BIO_new_connect(char *str) 637*0Sstevel@tonic-gate { 638*0Sstevel@tonic-gate BIO *ret; 639*0Sstevel@tonic-gate 640*0Sstevel@tonic-gate ret=BIO_new(BIO_s_connect()); 641*0Sstevel@tonic-gate if (ret == NULL) return(NULL); 642*0Sstevel@tonic-gate if (BIO_set_conn_hostname(ret,str)) 643*0Sstevel@tonic-gate return(ret); 644*0Sstevel@tonic-gate else 645*0Sstevel@tonic-gate { 646*0Sstevel@tonic-gate BIO_free(ret); 647*0Sstevel@tonic-gate return(NULL); 648*0Sstevel@tonic-gate } 649*0Sstevel@tonic-gate } 650*0Sstevel@tonic-gate 651*0Sstevel@tonic-gate #endif 652*0Sstevel@tonic-gate 653