xref: /onnv-gate/usr/src/common/openssl/crypto/bio/bss_acpt.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/bio/bss_acpt.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 typedef struct bio_accept_st
790Sstevel@tonic-gate 	{
800Sstevel@tonic-gate 	int state;
810Sstevel@tonic-gate 	char *param_addr;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	int accept_sock;
840Sstevel@tonic-gate 	int accept_nbio;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	char *addr;
870Sstevel@tonic-gate 	int nbio;
880Sstevel@tonic-gate 	/* If 0, it means normal, if 1, do a connect on bind failure,
890Sstevel@tonic-gate 	 * and if there is no-one listening, bind with SO_REUSEADDR.
900Sstevel@tonic-gate 	 * If 2, always use SO_REUSEADDR. */
910Sstevel@tonic-gate 	int bind_mode;
920Sstevel@tonic-gate 	BIO *bio_chain;
930Sstevel@tonic-gate 	} BIO_ACCEPT;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static int acpt_write(BIO *h, const char *buf, int num);
960Sstevel@tonic-gate static int acpt_read(BIO *h, char *buf, int size);
970Sstevel@tonic-gate static int acpt_puts(BIO *h, const char *str);
980Sstevel@tonic-gate static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
990Sstevel@tonic-gate static int acpt_new(BIO *h);
1000Sstevel@tonic-gate static int acpt_free(BIO *data);
1010Sstevel@tonic-gate static int acpt_state(BIO *b, BIO_ACCEPT *c);
1020Sstevel@tonic-gate static void acpt_close_socket(BIO *data);
1030Sstevel@tonic-gate BIO_ACCEPT *BIO_ACCEPT_new(void );
1040Sstevel@tonic-gate void BIO_ACCEPT_free(BIO_ACCEPT *a);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #define ACPT_S_BEFORE			1
1070Sstevel@tonic-gate #define ACPT_S_GET_ACCEPT_SOCKET	2
1080Sstevel@tonic-gate #define ACPT_S_OK			3
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static BIO_METHOD methods_acceptp=
1110Sstevel@tonic-gate 	{
1120Sstevel@tonic-gate 	BIO_TYPE_ACCEPT,
1130Sstevel@tonic-gate 	"socket accept",
1140Sstevel@tonic-gate 	acpt_write,
1150Sstevel@tonic-gate 	acpt_read,
1160Sstevel@tonic-gate 	acpt_puts,
1170Sstevel@tonic-gate 	NULL, /* connect_gets, */
1180Sstevel@tonic-gate 	acpt_ctrl,
1190Sstevel@tonic-gate 	acpt_new,
1200Sstevel@tonic-gate 	acpt_free,
1210Sstevel@tonic-gate 	NULL,
1220Sstevel@tonic-gate 	};
1230Sstevel@tonic-gate 
BIO_s_accept(void)1240Sstevel@tonic-gate BIO_METHOD *BIO_s_accept(void)
1250Sstevel@tonic-gate 	{
1260Sstevel@tonic-gate 	return(&methods_acceptp);
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 
acpt_new(BIO * bi)1290Sstevel@tonic-gate static int acpt_new(BIO *bi)
1300Sstevel@tonic-gate 	{
1310Sstevel@tonic-gate 	BIO_ACCEPT *ba;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	bi->init=0;
1340Sstevel@tonic-gate 	bi->num=INVALID_SOCKET;
1350Sstevel@tonic-gate 	bi->flags=0;
1360Sstevel@tonic-gate 	if ((ba=BIO_ACCEPT_new()) == NULL)
1370Sstevel@tonic-gate 		return(0);
1380Sstevel@tonic-gate 	bi->ptr=(char *)ba;
1390Sstevel@tonic-gate 	ba->state=ACPT_S_BEFORE;
1400Sstevel@tonic-gate 	bi->shutdown=1;
1410Sstevel@tonic-gate 	return(1);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
BIO_ACCEPT_new(void)1440Sstevel@tonic-gate BIO_ACCEPT *BIO_ACCEPT_new(void)
1450Sstevel@tonic-gate 	{
1460Sstevel@tonic-gate 	BIO_ACCEPT *ret;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if ((ret=(BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL)
1490Sstevel@tonic-gate 		return(NULL);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	memset(ret,0,sizeof(BIO_ACCEPT));
1520Sstevel@tonic-gate 	ret->accept_sock=INVALID_SOCKET;
1530Sstevel@tonic-gate 	ret->bind_mode=BIO_BIND_NORMAL;
1540Sstevel@tonic-gate 	return(ret);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
BIO_ACCEPT_free(BIO_ACCEPT * a)1570Sstevel@tonic-gate void BIO_ACCEPT_free(BIO_ACCEPT *a)
1580Sstevel@tonic-gate 	{
1590Sstevel@tonic-gate 	if(a == NULL)
1600Sstevel@tonic-gate 	    return;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if (a->param_addr != NULL) OPENSSL_free(a->param_addr);
1630Sstevel@tonic-gate 	if (a->addr != NULL) OPENSSL_free(a->addr);
1640Sstevel@tonic-gate 	if (a->bio_chain != NULL) BIO_free(a->bio_chain);
1650Sstevel@tonic-gate 	OPENSSL_free(a);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
acpt_close_socket(BIO * bio)1680Sstevel@tonic-gate static void acpt_close_socket(BIO *bio)
1690Sstevel@tonic-gate 	{
1700Sstevel@tonic-gate 	BIO_ACCEPT *c;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	c=(BIO_ACCEPT *)bio->ptr;
1730Sstevel@tonic-gate 	if (c->accept_sock != INVALID_SOCKET)
1740Sstevel@tonic-gate 		{
1750Sstevel@tonic-gate 		shutdown(c->accept_sock,2);
1760Sstevel@tonic-gate 		closesocket(c->accept_sock);
1770Sstevel@tonic-gate 		c->accept_sock=INVALID_SOCKET;
1780Sstevel@tonic-gate 		bio->num=INVALID_SOCKET;
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
acpt_free(BIO * a)1820Sstevel@tonic-gate static int acpt_free(BIO *a)
1830Sstevel@tonic-gate 	{
1840Sstevel@tonic-gate 	BIO_ACCEPT *data;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (a == NULL) return(0);
1870Sstevel@tonic-gate 	data=(BIO_ACCEPT *)a->ptr;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (a->shutdown)
1900Sstevel@tonic-gate 		{
1910Sstevel@tonic-gate 		acpt_close_socket(a);
1920Sstevel@tonic-gate 		BIO_ACCEPT_free(data);
1930Sstevel@tonic-gate 		a->ptr=NULL;
1940Sstevel@tonic-gate 		a->flags=0;
1950Sstevel@tonic-gate 		a->init=0;
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 	return(1);
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
acpt_state(BIO * b,BIO_ACCEPT * c)2000Sstevel@tonic-gate static int acpt_state(BIO *b, BIO_ACCEPT *c)
2010Sstevel@tonic-gate 	{
2020Sstevel@tonic-gate 	BIO *bio=NULL,*dbio;
2030Sstevel@tonic-gate 	int s= -1;
2040Sstevel@tonic-gate 	int i;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate again:
2070Sstevel@tonic-gate 	switch (c->state)
2080Sstevel@tonic-gate 		{
2090Sstevel@tonic-gate 	case ACPT_S_BEFORE:
2100Sstevel@tonic-gate 		if (c->param_addr == NULL)
2110Sstevel@tonic-gate 			{
2120Sstevel@tonic-gate 			BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED);
2130Sstevel@tonic-gate 			return(-1);
2140Sstevel@tonic-gate 			}
2150Sstevel@tonic-gate 		s=BIO_get_accept_socket(c->param_addr,c->bind_mode);
2160Sstevel@tonic-gate 		if (s == INVALID_SOCKET)
2170Sstevel@tonic-gate 			return(-1);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		if (c->accept_nbio)
2200Sstevel@tonic-gate 			{
2210Sstevel@tonic-gate 			if (!BIO_socket_nbio(s,1))
2220Sstevel@tonic-gate 				{
2230Sstevel@tonic-gate 				closesocket(s);
2240Sstevel@tonic-gate 				BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
2250Sstevel@tonic-gate 				return(-1);
2260Sstevel@tonic-gate 				}
2270Sstevel@tonic-gate 			}
2280Sstevel@tonic-gate 		c->accept_sock=s;
2290Sstevel@tonic-gate 		b->num=s;
2300Sstevel@tonic-gate 		c->state=ACPT_S_GET_ACCEPT_SOCKET;
2310Sstevel@tonic-gate 		return(1);
2320Sstevel@tonic-gate 		/* break; */
2330Sstevel@tonic-gate 	case ACPT_S_GET_ACCEPT_SOCKET:
2340Sstevel@tonic-gate 		if (b->next_bio != NULL)
2350Sstevel@tonic-gate 			{
2360Sstevel@tonic-gate 			c->state=ACPT_S_OK;
2370Sstevel@tonic-gate 			goto again;
2380Sstevel@tonic-gate 			}
2390Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
2400Sstevel@tonic-gate 		b->retry_reason=0;
2410Sstevel@tonic-gate 		i=BIO_accept(c->accept_sock,&(c->addr));
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		/* -2 return means we should retry */
2440Sstevel@tonic-gate 		if(i == -2)
2450Sstevel@tonic-gate 			{
2460Sstevel@tonic-gate 			BIO_set_retry_special(b);
2470Sstevel@tonic-gate 			b->retry_reason=BIO_RR_ACCEPT;
2480Sstevel@tonic-gate 			return -1;
2490Sstevel@tonic-gate 			}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 		if (i < 0) return(i);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		bio=BIO_new_socket(i,BIO_CLOSE);
2540Sstevel@tonic-gate 		if (bio == NULL) goto err;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 		BIO_set_callback(bio,BIO_get_callback(b));
2570Sstevel@tonic-gate 		BIO_set_callback_arg(bio,BIO_get_callback_arg(b));
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 		if (c->nbio)
2600Sstevel@tonic-gate 			{
2610Sstevel@tonic-gate 			if (!BIO_socket_nbio(i,1))
2620Sstevel@tonic-gate 				{
2630Sstevel@tonic-gate 				BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
2640Sstevel@tonic-gate 				goto err;
2650Sstevel@tonic-gate 				}
2660Sstevel@tonic-gate 			}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		/* If the accept BIO has an bio_chain, we dup it and
2690Sstevel@tonic-gate 		 * put the new socket at the end. */
2700Sstevel@tonic-gate 		if (c->bio_chain != NULL)
2710Sstevel@tonic-gate 			{
2720Sstevel@tonic-gate 			if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL)
2730Sstevel@tonic-gate 				goto err;
2740Sstevel@tonic-gate 			if (!BIO_push(dbio,bio)) goto err;
2750Sstevel@tonic-gate 			bio=dbio;
2760Sstevel@tonic-gate 			}
2770Sstevel@tonic-gate 		if (BIO_push(b,bio) == NULL) goto err;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		c->state=ACPT_S_OK;
2800Sstevel@tonic-gate 		return(1);
2810Sstevel@tonic-gate err:
2820Sstevel@tonic-gate 		if (bio != NULL)
2830Sstevel@tonic-gate 			BIO_free(bio);
2840Sstevel@tonic-gate 		else if (s >= 0)
2850Sstevel@tonic-gate 			closesocket(s);
2860Sstevel@tonic-gate 		return(0);
2870Sstevel@tonic-gate 		/* break; */
2880Sstevel@tonic-gate 	case ACPT_S_OK:
2890Sstevel@tonic-gate 		if (b->next_bio == NULL)
2900Sstevel@tonic-gate 			{
2910Sstevel@tonic-gate 			c->state=ACPT_S_GET_ACCEPT_SOCKET;
2920Sstevel@tonic-gate 			goto again;
2930Sstevel@tonic-gate 			}
2940Sstevel@tonic-gate 		return(1);
2950Sstevel@tonic-gate 		/* break; */
2960Sstevel@tonic-gate 	default:
2970Sstevel@tonic-gate 		return(0);
2980Sstevel@tonic-gate 		/* break; */
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
acpt_read(BIO * b,char * out,int outl)3030Sstevel@tonic-gate static int acpt_read(BIO *b, char *out, int outl)
3040Sstevel@tonic-gate 	{
3050Sstevel@tonic-gate 	int ret=0;
3060Sstevel@tonic-gate 	BIO_ACCEPT *data;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
3090Sstevel@tonic-gate 	data=(BIO_ACCEPT *)b->ptr;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	while (b->next_bio == NULL)
3120Sstevel@tonic-gate 		{
3130Sstevel@tonic-gate 		ret=acpt_state(b,data);
3140Sstevel@tonic-gate 		if (ret <= 0) return(ret);
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	ret=BIO_read(b->next_bio,out,outl);
3180Sstevel@tonic-gate 	BIO_copy_next_retry(b);
3190Sstevel@tonic-gate 	return(ret);
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
acpt_write(BIO * b,const char * in,int inl)3220Sstevel@tonic-gate static int acpt_write(BIO *b, const char *in, int inl)
3230Sstevel@tonic-gate 	{
3240Sstevel@tonic-gate 	int ret;
3250Sstevel@tonic-gate 	BIO_ACCEPT *data;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
3280Sstevel@tonic-gate 	data=(BIO_ACCEPT *)b->ptr;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	while (b->next_bio == NULL)
3310Sstevel@tonic-gate 		{
3320Sstevel@tonic-gate 		ret=acpt_state(b,data);
3330Sstevel@tonic-gate 		if (ret <= 0) return(ret);
3340Sstevel@tonic-gate 		}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	ret=BIO_write(b->next_bio,in,inl);
3370Sstevel@tonic-gate 	BIO_copy_next_retry(b);
3380Sstevel@tonic-gate 	return(ret);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
acpt_ctrl(BIO * b,int cmd,long num,void * ptr)3410Sstevel@tonic-gate static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
3420Sstevel@tonic-gate 	{
3430Sstevel@tonic-gate 	BIO *dbio;
3440Sstevel@tonic-gate 	int *ip;
3450Sstevel@tonic-gate 	long ret=1;
3460Sstevel@tonic-gate 	BIO_ACCEPT *data;
3470Sstevel@tonic-gate 	char **pp;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	data=(BIO_ACCEPT *)b->ptr;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	switch (cmd)
3520Sstevel@tonic-gate 		{
3530Sstevel@tonic-gate 	case BIO_CTRL_RESET:
3540Sstevel@tonic-gate 		ret=0;
3550Sstevel@tonic-gate 		data->state=ACPT_S_BEFORE;
3560Sstevel@tonic-gate 		acpt_close_socket(b);
3570Sstevel@tonic-gate 		b->flags=0;
3580Sstevel@tonic-gate 		break;
3590Sstevel@tonic-gate 	case BIO_C_DO_STATE_MACHINE:
3600Sstevel@tonic-gate 		/* use this one to start the connection */
3610Sstevel@tonic-gate 		ret=(long)acpt_state(b,data);
3620Sstevel@tonic-gate 		break;
3630Sstevel@tonic-gate 	case BIO_C_SET_ACCEPT:
3640Sstevel@tonic-gate 		if (ptr != NULL)
3650Sstevel@tonic-gate 			{
3660Sstevel@tonic-gate 			if (num == 0)
3670Sstevel@tonic-gate 				{
3680Sstevel@tonic-gate 				b->init=1;
3690Sstevel@tonic-gate 				if (data->param_addr != NULL)
3700Sstevel@tonic-gate 					OPENSSL_free(data->param_addr);
3710Sstevel@tonic-gate 				data->param_addr=BUF_strdup(ptr);
3720Sstevel@tonic-gate 				}
3730Sstevel@tonic-gate 			else if (num == 1)
3740Sstevel@tonic-gate 				{
3750Sstevel@tonic-gate 				data->accept_nbio=(ptr != NULL);
3760Sstevel@tonic-gate 				}
3770Sstevel@tonic-gate 			else if (num == 2)
3780Sstevel@tonic-gate 				{
3790Sstevel@tonic-gate 				if (data->bio_chain != NULL)
3800Sstevel@tonic-gate 					BIO_free(data->bio_chain);
3810Sstevel@tonic-gate 				data->bio_chain=(BIO *)ptr;
3820Sstevel@tonic-gate 				}
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 		break;
3850Sstevel@tonic-gate 	case BIO_C_SET_NBIO:
3860Sstevel@tonic-gate 		data->nbio=(int)num;
3870Sstevel@tonic-gate 		break;
3880Sstevel@tonic-gate 	case BIO_C_SET_FD:
3890Sstevel@tonic-gate 		b->init=1;
3900Sstevel@tonic-gate 		b->num= *((int *)ptr);
3910Sstevel@tonic-gate 		data->accept_sock=b->num;
3920Sstevel@tonic-gate 		data->state=ACPT_S_GET_ACCEPT_SOCKET;
3930Sstevel@tonic-gate 		b->shutdown=(int)num;
3940Sstevel@tonic-gate 		b->init=1;
3950Sstevel@tonic-gate 		break;
3960Sstevel@tonic-gate 	case BIO_C_GET_FD:
3970Sstevel@tonic-gate 		if (b->init)
3980Sstevel@tonic-gate 			{
3990Sstevel@tonic-gate 			ip=(int *)ptr;
4000Sstevel@tonic-gate 			if (ip != NULL)
4010Sstevel@tonic-gate 				*ip=data->accept_sock;
4020Sstevel@tonic-gate 			ret=data->accept_sock;
4030Sstevel@tonic-gate 			}
4040Sstevel@tonic-gate 		else
4050Sstevel@tonic-gate 			ret= -1;
4060Sstevel@tonic-gate 		break;
4070Sstevel@tonic-gate 	case BIO_C_GET_ACCEPT:
4080Sstevel@tonic-gate 		if (b->init)
4090Sstevel@tonic-gate 			{
4100Sstevel@tonic-gate 			if (ptr != NULL)
4110Sstevel@tonic-gate 				{
4120Sstevel@tonic-gate 				pp=(char **)ptr;
4130Sstevel@tonic-gate 				*pp=data->param_addr;
4140Sstevel@tonic-gate 				}
4150Sstevel@tonic-gate 			else
4160Sstevel@tonic-gate 				ret= -1;
4170Sstevel@tonic-gate 			}
4180Sstevel@tonic-gate 		else
4190Sstevel@tonic-gate 			ret= -1;
4200Sstevel@tonic-gate 		break;
4210Sstevel@tonic-gate 	case BIO_CTRL_GET_CLOSE:
4220Sstevel@tonic-gate 		ret=b->shutdown;
4230Sstevel@tonic-gate 		break;
4240Sstevel@tonic-gate 	case BIO_CTRL_SET_CLOSE:
4250Sstevel@tonic-gate 		b->shutdown=(int)num;
4260Sstevel@tonic-gate 		break;
4270Sstevel@tonic-gate 	case BIO_CTRL_PENDING:
4280Sstevel@tonic-gate 	case BIO_CTRL_WPENDING:
4290Sstevel@tonic-gate 		ret=0;
4300Sstevel@tonic-gate 		break;
4310Sstevel@tonic-gate 	case BIO_CTRL_FLUSH:
4320Sstevel@tonic-gate 		break;
4330Sstevel@tonic-gate 	case BIO_C_SET_BIND_MODE:
4340Sstevel@tonic-gate 		data->bind_mode=(int)num;
4350Sstevel@tonic-gate 		break;
4360Sstevel@tonic-gate 	case BIO_C_GET_BIND_MODE:
4370Sstevel@tonic-gate 		ret=(long)data->bind_mode;
4380Sstevel@tonic-gate 		break;
4390Sstevel@tonic-gate 	case BIO_CTRL_DUP:
4400Sstevel@tonic-gate 		dbio=(BIO *)ptr;
4410Sstevel@tonic-gate /*		if (data->param_port) EAY EAY
4420Sstevel@tonic-gate 			BIO_set_port(dbio,data->param_port);
4430Sstevel@tonic-gate 		if (data->param_hostname)
4440Sstevel@tonic-gate 			BIO_set_hostname(dbio,data->param_hostname);
4450Sstevel@tonic-gate 		BIO_set_nbio(dbio,data->nbio); */
4460Sstevel@tonic-gate 		break;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	default:
4490Sstevel@tonic-gate 		ret=0;
4500Sstevel@tonic-gate 		break;
4510Sstevel@tonic-gate 		}
4520Sstevel@tonic-gate 	return(ret);
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
acpt_puts(BIO * bp,const char * str)4550Sstevel@tonic-gate static int acpt_puts(BIO *bp, const char *str)
4560Sstevel@tonic-gate 	{
4570Sstevel@tonic-gate 	int n,ret;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	n=strlen(str);
4600Sstevel@tonic-gate 	ret=acpt_write(bp,str,n);
4610Sstevel@tonic-gate 	return(ret);
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
BIO_new_accept(char * str)4640Sstevel@tonic-gate BIO *BIO_new_accept(char *str)
4650Sstevel@tonic-gate 	{
4660Sstevel@tonic-gate 	BIO *ret;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	ret=BIO_new(BIO_s_accept());
4690Sstevel@tonic-gate 	if (ret == NULL) return(NULL);
4700Sstevel@tonic-gate 	if (BIO_set_accept_port(ret,str))
4710Sstevel@tonic-gate 		return(ret);
4720Sstevel@tonic-gate 	else
4730Sstevel@tonic-gate 		{
4740Sstevel@tonic-gate 		BIO_free(ret);
4750Sstevel@tonic-gate 		return(NULL);
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate #endif
480