10Sstevel@tonic-gate /* crypto/bio/bss_conn.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <errno.h>
610Sstevel@tonic-gate #define USE_SOCKETS
620Sstevel@tonic-gate #include "cryptlib.h"
630Sstevel@tonic-gate #include <openssl/bio.h>
640Sstevel@tonic-gate
65*2139Sjp161948 #ifndef OPENSSL_NO_SOCK
66*2139Sjp161948
670Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN16
680Sstevel@tonic-gate #define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
690Sstevel@tonic-gate #else
700Sstevel@tonic-gate #define SOCKET_PROTOCOL IPPROTO_TCP
710Sstevel@tonic-gate #endif
720Sstevel@tonic-gate
730Sstevel@tonic-gate #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
740Sstevel@tonic-gate /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
750Sstevel@tonic-gate #undef FIONBIO
760Sstevel@tonic-gate #endif
770Sstevel@tonic-gate
780Sstevel@tonic-gate
790Sstevel@tonic-gate typedef struct bio_connect_st
800Sstevel@tonic-gate {
810Sstevel@tonic-gate int state;
820Sstevel@tonic-gate
830Sstevel@tonic-gate char *param_hostname;
840Sstevel@tonic-gate char *param_port;
850Sstevel@tonic-gate int nbio;
860Sstevel@tonic-gate
870Sstevel@tonic-gate unsigned char ip[4];
880Sstevel@tonic-gate unsigned short port;
890Sstevel@tonic-gate
900Sstevel@tonic-gate struct sockaddr_in them;
910Sstevel@tonic-gate
920Sstevel@tonic-gate /* int socket; this will be kept in bio->num so that it is
930Sstevel@tonic-gate * compatible with the bss_sock bio */
940Sstevel@tonic-gate
950Sstevel@tonic-gate /* called when the connection is initially made
960Sstevel@tonic-gate * callback(BIO,state,ret); The callback should return
970Sstevel@tonic-gate * 'ret'. state is for compatibility with the ssl info_callback */
980Sstevel@tonic-gate int (*info_callback)(const BIO *bio,int state,int ret);
990Sstevel@tonic-gate } BIO_CONNECT;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate static int conn_write(BIO *h, const char *buf, int num);
1020Sstevel@tonic-gate static int conn_read(BIO *h, char *buf, int size);
1030Sstevel@tonic-gate static int conn_puts(BIO *h, const char *str);
1040Sstevel@tonic-gate static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
1050Sstevel@tonic-gate static int conn_new(BIO *h);
1060Sstevel@tonic-gate static int conn_free(BIO *data);
1070Sstevel@tonic-gate static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate static int conn_state(BIO *b, BIO_CONNECT *c);
1100Sstevel@tonic-gate static void conn_close_socket(BIO *data);
1110Sstevel@tonic-gate BIO_CONNECT *BIO_CONNECT_new(void );
1120Sstevel@tonic-gate void BIO_CONNECT_free(BIO_CONNECT *a);
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate static BIO_METHOD methods_connectp=
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate BIO_TYPE_CONNECT,
1170Sstevel@tonic-gate "socket connect",
1180Sstevel@tonic-gate conn_write,
1190Sstevel@tonic-gate conn_read,
1200Sstevel@tonic-gate conn_puts,
1210Sstevel@tonic-gate NULL, /* connect_gets, */
1220Sstevel@tonic-gate conn_ctrl,
1230Sstevel@tonic-gate conn_new,
1240Sstevel@tonic-gate conn_free,
1250Sstevel@tonic-gate conn_callback_ctrl,
1260Sstevel@tonic-gate };
1270Sstevel@tonic-gate
conn_state(BIO * b,BIO_CONNECT * c)1280Sstevel@tonic-gate static int conn_state(BIO *b, BIO_CONNECT *c)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate int ret= -1,i;
1310Sstevel@tonic-gate unsigned long l;
1320Sstevel@tonic-gate char *p,*q;
133*2139Sjp161948 int (*cb)(const BIO *,int,int)=NULL;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate if (c->info_callback != NULL)
1360Sstevel@tonic-gate cb=c->info_callback;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate for (;;)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate switch (c->state)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate case BIO_CONN_S_BEFORE:
1430Sstevel@tonic-gate p=c->param_hostname;
1440Sstevel@tonic-gate if (p == NULL)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
1470Sstevel@tonic-gate goto exit_loop;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate for ( ; *p != '\0'; p++)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate if ((*p == ':') || (*p == '/')) break;
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate i= *p;
1550Sstevel@tonic-gate if ((i == ':') || (i == '/'))
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate *(p++)='\0';
1590Sstevel@tonic-gate if (i == ':')
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate for (q=p; *q; q++)
1620Sstevel@tonic-gate if (*q == '/')
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate *q='\0';
1650Sstevel@tonic-gate break;
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate if (c->param_port != NULL)
1680Sstevel@tonic-gate OPENSSL_free(c->param_port);
1690Sstevel@tonic-gate c->param_port=BUF_strdup(p);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate if (c->param_port == NULL)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
1760Sstevel@tonic-gate ERR_add_error_data(2,"host=",c->param_hostname);
1770Sstevel@tonic-gate goto exit_loop;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate c->state=BIO_CONN_S_GET_IP;
1800Sstevel@tonic-gate break;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate case BIO_CONN_S_GET_IP:
1830Sstevel@tonic-gate if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
1840Sstevel@tonic-gate goto exit_loop;
1850Sstevel@tonic-gate c->state=BIO_CONN_S_GET_PORT;
1860Sstevel@tonic-gate break;
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate case BIO_CONN_S_GET_PORT:
1890Sstevel@tonic-gate if (c->param_port == NULL)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate /* abort(); */
1920Sstevel@tonic-gate goto exit_loop;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate else if (BIO_get_port(c->param_port,&c->port) <= 0)
1950Sstevel@tonic-gate goto exit_loop;
1960Sstevel@tonic-gate c->state=BIO_CONN_S_CREATE_SOCKET;
1970Sstevel@tonic-gate break;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate case BIO_CONN_S_CREATE_SOCKET:
2000Sstevel@tonic-gate /* now setup address */
2010Sstevel@tonic-gate memset((char *)&c->them,0,sizeof(c->them));
2020Sstevel@tonic-gate c->them.sin_family=AF_INET;
2030Sstevel@tonic-gate c->them.sin_port=htons((unsigned short)c->port);
2040Sstevel@tonic-gate l=(unsigned long)
2050Sstevel@tonic-gate ((unsigned long)c->ip[0]<<24L)|
2060Sstevel@tonic-gate ((unsigned long)c->ip[1]<<16L)|
2070Sstevel@tonic-gate ((unsigned long)c->ip[2]<< 8L)|
2080Sstevel@tonic-gate ((unsigned long)c->ip[3]);
2090Sstevel@tonic-gate c->them.sin_addr.s_addr=htonl(l);
2100Sstevel@tonic-gate c->state=BIO_CONN_S_CREATE_SOCKET;
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
2130Sstevel@tonic-gate if (ret == INVALID_SOCKET)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate SYSerr(SYS_F_SOCKET,get_last_socket_error());
2160Sstevel@tonic-gate ERR_add_error_data(4,"host=",c->param_hostname,
2170Sstevel@tonic-gate ":",c->param_port);
2180Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
2190Sstevel@tonic-gate goto exit_loop;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate b->num=ret;
2220Sstevel@tonic-gate c->state=BIO_CONN_S_NBIO;
2230Sstevel@tonic-gate break;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate case BIO_CONN_S_NBIO:
2260Sstevel@tonic-gate if (c->nbio)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate if (!BIO_socket_nbio(b->num,1))
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
2310Sstevel@tonic-gate ERR_add_error_data(4,"host=",
2320Sstevel@tonic-gate c->param_hostname,
2330Sstevel@tonic-gate ":",c->param_port);
2340Sstevel@tonic-gate goto exit_loop;
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate c->state=BIO_CONN_S_CONNECT;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate #if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
2400Sstevel@tonic-gate i=1;
2410Sstevel@tonic-gate i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
2420Sstevel@tonic-gate if (i < 0)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate SYSerr(SYS_F_SOCKET,get_last_socket_error());
2450Sstevel@tonic-gate ERR_add_error_data(4,"host=",c->param_hostname,
2460Sstevel@tonic-gate ":",c->param_port);
2470Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
2480Sstevel@tonic-gate goto exit_loop;
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate #endif
2510Sstevel@tonic-gate break;
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate case BIO_CONN_S_CONNECT:
2540Sstevel@tonic-gate BIO_clear_retry_flags(b);
2550Sstevel@tonic-gate ret=connect(b->num,
2560Sstevel@tonic-gate (struct sockaddr *)&c->them,
2570Sstevel@tonic-gate sizeof(c->them));
2580Sstevel@tonic-gate b->retry_reason=0;
2590Sstevel@tonic-gate if (ret < 0)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate if (BIO_sock_should_retry(ret))
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate BIO_set_retry_special(b);
2640Sstevel@tonic-gate c->state=BIO_CONN_S_BLOCKED_CONNECT;
2650Sstevel@tonic-gate b->retry_reason=BIO_RR_CONNECT;
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate else
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate SYSerr(SYS_F_CONNECT,get_last_socket_error());
2700Sstevel@tonic-gate ERR_add_error_data(4,"host=",
2710Sstevel@tonic-gate c->param_hostname,
2720Sstevel@tonic-gate ":",c->param_port);
2730Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate goto exit_loop;
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate else
2780Sstevel@tonic-gate c->state=BIO_CONN_S_OK;
2790Sstevel@tonic-gate break;
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate case BIO_CONN_S_BLOCKED_CONNECT:
2820Sstevel@tonic-gate i=BIO_sock_error(b->num);
2830Sstevel@tonic-gate if (i)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate BIO_clear_retry_flags(b);
2860Sstevel@tonic-gate SYSerr(SYS_F_CONNECT,i);
2870Sstevel@tonic-gate ERR_add_error_data(4,"host=",
2880Sstevel@tonic-gate c->param_hostname,
2890Sstevel@tonic-gate ":",c->param_port);
2900Sstevel@tonic-gate BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
2910Sstevel@tonic-gate ret=0;
2920Sstevel@tonic-gate goto exit_loop;
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate else
2950Sstevel@tonic-gate c->state=BIO_CONN_S_OK;
2960Sstevel@tonic-gate break;
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate case BIO_CONN_S_OK:
2990Sstevel@tonic-gate ret=1;
3000Sstevel@tonic-gate goto exit_loop;
3010Sstevel@tonic-gate default:
3020Sstevel@tonic-gate /* abort(); */
3030Sstevel@tonic-gate goto exit_loop;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate if (cb != NULL)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate if (!(ret=cb((BIO *)b,c->state,ret)))
3090Sstevel@tonic-gate goto end;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate /* Loop does not exit */
3140Sstevel@tonic-gate exit_loop:
3150Sstevel@tonic-gate if (cb != NULL)
3160Sstevel@tonic-gate ret=cb((BIO *)b,c->state,ret);
3170Sstevel@tonic-gate end:
3180Sstevel@tonic-gate return(ret);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate
BIO_CONNECT_new(void)3210Sstevel@tonic-gate BIO_CONNECT *BIO_CONNECT_new(void)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate BIO_CONNECT *ret;
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate if ((ret=(BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL)
3260Sstevel@tonic-gate return(NULL);
3270Sstevel@tonic-gate ret->state=BIO_CONN_S_BEFORE;
3280Sstevel@tonic-gate ret->param_hostname=NULL;
3290Sstevel@tonic-gate ret->param_port=NULL;
3300Sstevel@tonic-gate ret->info_callback=NULL;
3310Sstevel@tonic-gate ret->nbio=0;
3320Sstevel@tonic-gate ret->ip[0]=0;
3330Sstevel@tonic-gate ret->ip[1]=0;
3340Sstevel@tonic-gate ret->ip[2]=0;
3350Sstevel@tonic-gate ret->ip[3]=0;
3360Sstevel@tonic-gate ret->port=0;
3370Sstevel@tonic-gate memset((char *)&ret->them,0,sizeof(ret->them));
3380Sstevel@tonic-gate return(ret);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate
BIO_CONNECT_free(BIO_CONNECT * a)3410Sstevel@tonic-gate void BIO_CONNECT_free(BIO_CONNECT *a)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate if(a == NULL)
3440Sstevel@tonic-gate return;
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate if (a->param_hostname != NULL)
3470Sstevel@tonic-gate OPENSSL_free(a->param_hostname);
3480Sstevel@tonic-gate if (a->param_port != NULL)
3490Sstevel@tonic-gate OPENSSL_free(a->param_port);
3500Sstevel@tonic-gate OPENSSL_free(a);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate
BIO_s_connect(void)3530Sstevel@tonic-gate BIO_METHOD *BIO_s_connect(void)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate return(&methods_connectp);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate
conn_new(BIO * bi)3580Sstevel@tonic-gate static int conn_new(BIO *bi)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate bi->init=0;
3610Sstevel@tonic-gate bi->num=INVALID_SOCKET;
3620Sstevel@tonic-gate bi->flags=0;
3630Sstevel@tonic-gate if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
3640Sstevel@tonic-gate return(0);
3650Sstevel@tonic-gate else
3660Sstevel@tonic-gate return(1);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate
conn_close_socket(BIO * bio)3690Sstevel@tonic-gate static void conn_close_socket(BIO *bio)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate BIO_CONNECT *c;
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate c=(BIO_CONNECT *)bio->ptr;
3740Sstevel@tonic-gate if (bio->num != INVALID_SOCKET)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate /* Only do a shutdown if things were established */
3770Sstevel@tonic-gate if (c->state == BIO_CONN_S_OK)
3780Sstevel@tonic-gate shutdown(bio->num,2);
3790Sstevel@tonic-gate closesocket(bio->num);
3800Sstevel@tonic-gate bio->num=INVALID_SOCKET;
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate
conn_free(BIO * a)3840Sstevel@tonic-gate static int conn_free(BIO *a)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate BIO_CONNECT *data;
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate if (a == NULL) return(0);
3890Sstevel@tonic-gate data=(BIO_CONNECT *)a->ptr;
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate if (a->shutdown)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate conn_close_socket(a);
3940Sstevel@tonic-gate BIO_CONNECT_free(data);
3950Sstevel@tonic-gate a->ptr=NULL;
3960Sstevel@tonic-gate a->flags=0;
3970Sstevel@tonic-gate a->init=0;
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate return(1);
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
conn_read(BIO * b,char * out,int outl)4020Sstevel@tonic-gate static int conn_read(BIO *b, char *out, int outl)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate int ret=0;
4050Sstevel@tonic-gate BIO_CONNECT *data;
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate data=(BIO_CONNECT *)b->ptr;
4080Sstevel@tonic-gate if (data->state != BIO_CONN_S_OK)
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate ret=conn_state(b,data);
4110Sstevel@tonic-gate if (ret <= 0)
4120Sstevel@tonic-gate return(ret);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate if (out != NULL)
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate clear_socket_error();
4180Sstevel@tonic-gate ret=readsocket(b->num,out,outl);
4190Sstevel@tonic-gate BIO_clear_retry_flags(b);
4200Sstevel@tonic-gate if (ret <= 0)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate if (BIO_sock_should_retry(ret))
4230Sstevel@tonic-gate BIO_set_retry_read(b);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate return(ret);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate
conn_write(BIO * b,const char * in,int inl)4290Sstevel@tonic-gate static int conn_write(BIO *b, const char *in, int inl)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate int ret;
4320Sstevel@tonic-gate BIO_CONNECT *data;
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate data=(BIO_CONNECT *)b->ptr;
4350Sstevel@tonic-gate if (data->state != BIO_CONN_S_OK)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate ret=conn_state(b,data);
4380Sstevel@tonic-gate if (ret <= 0) return(ret);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate clear_socket_error();
4420Sstevel@tonic-gate ret=writesocket(b->num,in,inl);
4430Sstevel@tonic-gate BIO_clear_retry_flags(b);
4440Sstevel@tonic-gate if (ret <= 0)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate if (BIO_sock_should_retry(ret))
4470Sstevel@tonic-gate BIO_set_retry_write(b);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate return(ret);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate
conn_ctrl(BIO * b,int cmd,long num,void * ptr)4520Sstevel@tonic-gate static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate BIO *dbio;
4550Sstevel@tonic-gate int *ip;
4560Sstevel@tonic-gate const char **pptr;
4570Sstevel@tonic-gate long ret=1;
4580Sstevel@tonic-gate BIO_CONNECT *data;
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate data=(BIO_CONNECT *)b->ptr;
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate switch (cmd)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate case BIO_CTRL_RESET:
4650Sstevel@tonic-gate ret=0;
4660Sstevel@tonic-gate data->state=BIO_CONN_S_BEFORE;
4670Sstevel@tonic-gate conn_close_socket(b);
4680Sstevel@tonic-gate b->flags=0;
4690Sstevel@tonic-gate break;
4700Sstevel@tonic-gate case BIO_C_DO_STATE_MACHINE:
4710Sstevel@tonic-gate /* use this one to start the connection */
472*2139Sjp161948 if (!(data->state != BIO_CONN_S_OK))
4730Sstevel@tonic-gate ret=(long)conn_state(b,data);
4740Sstevel@tonic-gate else
4750Sstevel@tonic-gate ret=1;
4760Sstevel@tonic-gate break;
4770Sstevel@tonic-gate case BIO_C_GET_CONNECT:
4780Sstevel@tonic-gate if (ptr != NULL)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate pptr=(const char **)ptr;
4810Sstevel@tonic-gate if (num == 0)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate *pptr=data->param_hostname;
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate else if (num == 1)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate *pptr=data->param_port;
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate else if (num == 2)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate *pptr= (char *)&(data->ip[0]);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate else if (num == 3)
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate *((int *)ptr)=data->port;
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate if ((!b->init) || (ptr == NULL))
4990Sstevel@tonic-gate *pptr="not initialized";
5000Sstevel@tonic-gate ret=1;
5010Sstevel@tonic-gate }
5020Sstevel@tonic-gate break;
5030Sstevel@tonic-gate case BIO_C_SET_CONNECT:
5040Sstevel@tonic-gate if (ptr != NULL)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate b->init=1;
5070Sstevel@tonic-gate if (num == 0)
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate if (data->param_hostname != NULL)
5100Sstevel@tonic-gate OPENSSL_free(data->param_hostname);
5110Sstevel@tonic-gate data->param_hostname=BUF_strdup(ptr);
5120Sstevel@tonic-gate }
5130Sstevel@tonic-gate else if (num == 1)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate if (data->param_port != NULL)
5160Sstevel@tonic-gate OPENSSL_free(data->param_port);
5170Sstevel@tonic-gate data->param_port=BUF_strdup(ptr);
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate else if (num == 2)
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate char buf[16];
5220Sstevel@tonic-gate unsigned char *p = ptr;
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"%d.%d.%d.%d",
5250Sstevel@tonic-gate p[0],p[1],p[2],p[3]);
5260Sstevel@tonic-gate if (data->param_hostname != NULL)
5270Sstevel@tonic-gate OPENSSL_free(data->param_hostname);
5280Sstevel@tonic-gate data->param_hostname=BUF_strdup(buf);
5290Sstevel@tonic-gate memcpy(&(data->ip[0]),ptr,4);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate else if (num == 3)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate char buf[DECIMAL_SIZE(int)+1];
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate BIO_snprintf(buf,sizeof buf,"%d",*(int *)ptr);
5360Sstevel@tonic-gate if (data->param_port != NULL)
5370Sstevel@tonic-gate OPENSSL_free(data->param_port);
5380Sstevel@tonic-gate data->param_port=BUF_strdup(buf);
5390Sstevel@tonic-gate data->port= *(int *)ptr;
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate break;
5430Sstevel@tonic-gate case BIO_C_SET_NBIO:
5440Sstevel@tonic-gate data->nbio=(int)num;
5450Sstevel@tonic-gate break;
5460Sstevel@tonic-gate case BIO_C_GET_FD:
5470Sstevel@tonic-gate if (b->init)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate ip=(int *)ptr;
5500Sstevel@tonic-gate if (ip != NULL)
5510Sstevel@tonic-gate *ip=b->num;
5520Sstevel@tonic-gate ret=b->num;
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate else
5550Sstevel@tonic-gate ret= -1;
5560Sstevel@tonic-gate break;
5570Sstevel@tonic-gate case BIO_CTRL_GET_CLOSE:
5580Sstevel@tonic-gate ret=b->shutdown;
5590Sstevel@tonic-gate break;
5600Sstevel@tonic-gate case BIO_CTRL_SET_CLOSE:
5610Sstevel@tonic-gate b->shutdown=(int)num;
5620Sstevel@tonic-gate break;
5630Sstevel@tonic-gate case BIO_CTRL_PENDING:
5640Sstevel@tonic-gate case BIO_CTRL_WPENDING:
5650Sstevel@tonic-gate ret=0;
5660Sstevel@tonic-gate break;
5670Sstevel@tonic-gate case BIO_CTRL_FLUSH:
5680Sstevel@tonic-gate break;
5690Sstevel@tonic-gate case BIO_CTRL_DUP:
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate dbio=(BIO *)ptr;
5720Sstevel@tonic-gate if (data->param_port)
5730Sstevel@tonic-gate BIO_set_conn_port(dbio,data->param_port);
5740Sstevel@tonic-gate if (data->param_hostname)
5750Sstevel@tonic-gate BIO_set_conn_hostname(dbio,data->param_hostname);
5760Sstevel@tonic-gate BIO_set_nbio(dbio,data->nbio);
5770Sstevel@tonic-gate /* FIXME: the cast of the function seems unlikely to be a good idea */
5780Sstevel@tonic-gate (void)BIO_set_info_callback(dbio,(bio_info_cb *)data->info_callback);
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate break;
5810Sstevel@tonic-gate case BIO_CTRL_SET_CALLBACK:
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate #if 0 /* FIXME: Should this be used? -- Richard Levitte */
5840Sstevel@tonic-gate BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
5850Sstevel@tonic-gate ret = -1;
5860Sstevel@tonic-gate #else
5870Sstevel@tonic-gate ret=0;
5880Sstevel@tonic-gate #endif
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate break;
5910Sstevel@tonic-gate case BIO_CTRL_GET_CALLBACK:
5920Sstevel@tonic-gate {
593*2139Sjp161948 int (**fptr)(const BIO *bio,int state,int xret);
5940Sstevel@tonic-gate
595*2139Sjp161948 fptr=(int (**)(const BIO *bio,int state,int xret))ptr;
5960Sstevel@tonic-gate *fptr=data->info_callback;
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate break;
5990Sstevel@tonic-gate default:
6000Sstevel@tonic-gate ret=0;
6010Sstevel@tonic-gate break;
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate return(ret);
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate
conn_callback_ctrl(BIO * b,int cmd,bio_info_cb * fp)6060Sstevel@tonic-gate static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate long ret=1;
6090Sstevel@tonic-gate BIO_CONNECT *data;
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate data=(BIO_CONNECT *)b->ptr;
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate switch (cmd)
6140Sstevel@tonic-gate {
6150Sstevel@tonic-gate case BIO_CTRL_SET_CALLBACK:
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate data->info_callback=(int (*)(const struct bio_st *, int, int))fp;
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate break;
6200Sstevel@tonic-gate default:
6210Sstevel@tonic-gate ret=0;
6220Sstevel@tonic-gate break;
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate return(ret);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate
conn_puts(BIO * bp,const char * str)6270Sstevel@tonic-gate static int conn_puts(BIO *bp, const char *str)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate int n,ret;
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate n=strlen(str);
6320Sstevel@tonic-gate ret=conn_write(bp,str,n);
6330Sstevel@tonic-gate return(ret);
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate
BIO_new_connect(char * str)6360Sstevel@tonic-gate BIO *BIO_new_connect(char *str)
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate BIO *ret;
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate ret=BIO_new(BIO_s_connect());
6410Sstevel@tonic-gate if (ret == NULL) return(NULL);
6420Sstevel@tonic-gate if (BIO_set_conn_hostname(ret,str))
6430Sstevel@tonic-gate return(ret);
6440Sstevel@tonic-gate else
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate BIO_free(ret);
6470Sstevel@tonic-gate return(NULL);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate #endif
6520Sstevel@tonic-gate
653