xref: /onnv-gate/usr/src/common/openssl/crypto/bio/bss_acpt.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/bio/bss_acpt.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 typedef struct bio_accept_st
79*0Sstevel@tonic-gate 	{
80*0Sstevel@tonic-gate 	int state;
81*0Sstevel@tonic-gate 	char *param_addr;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	int accept_sock;
84*0Sstevel@tonic-gate 	int accept_nbio;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	char *addr;
87*0Sstevel@tonic-gate 	int nbio;
88*0Sstevel@tonic-gate 	/* If 0, it means normal, if 1, do a connect on bind failure,
89*0Sstevel@tonic-gate 	 * and if there is no-one listening, bind with SO_REUSEADDR.
90*0Sstevel@tonic-gate 	 * If 2, always use SO_REUSEADDR. */
91*0Sstevel@tonic-gate 	int bind_mode;
92*0Sstevel@tonic-gate 	BIO *bio_chain;
93*0Sstevel@tonic-gate 	} BIO_ACCEPT;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate static int acpt_write(BIO *h, const char *buf, int num);
96*0Sstevel@tonic-gate static int acpt_read(BIO *h, char *buf, int size);
97*0Sstevel@tonic-gate static int acpt_puts(BIO *h, const char *str);
98*0Sstevel@tonic-gate static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
99*0Sstevel@tonic-gate static int acpt_new(BIO *h);
100*0Sstevel@tonic-gate static int acpt_free(BIO *data);
101*0Sstevel@tonic-gate static int acpt_state(BIO *b, BIO_ACCEPT *c);
102*0Sstevel@tonic-gate static void acpt_close_socket(BIO *data);
103*0Sstevel@tonic-gate BIO_ACCEPT *BIO_ACCEPT_new(void );
104*0Sstevel@tonic-gate void BIO_ACCEPT_free(BIO_ACCEPT *a);
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate #define ACPT_S_BEFORE			1
107*0Sstevel@tonic-gate #define ACPT_S_GET_ACCEPT_SOCKET	2
108*0Sstevel@tonic-gate #define ACPT_S_OK			3
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate static BIO_METHOD methods_acceptp=
111*0Sstevel@tonic-gate 	{
112*0Sstevel@tonic-gate 	BIO_TYPE_ACCEPT,
113*0Sstevel@tonic-gate 	"socket accept",
114*0Sstevel@tonic-gate 	acpt_write,
115*0Sstevel@tonic-gate 	acpt_read,
116*0Sstevel@tonic-gate 	acpt_puts,
117*0Sstevel@tonic-gate 	NULL, /* connect_gets, */
118*0Sstevel@tonic-gate 	acpt_ctrl,
119*0Sstevel@tonic-gate 	acpt_new,
120*0Sstevel@tonic-gate 	acpt_free,
121*0Sstevel@tonic-gate 	NULL,
122*0Sstevel@tonic-gate 	};
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate BIO_METHOD *BIO_s_accept(void)
125*0Sstevel@tonic-gate 	{
126*0Sstevel@tonic-gate 	return(&methods_acceptp);
127*0Sstevel@tonic-gate 	}
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate static int acpt_new(BIO *bi)
130*0Sstevel@tonic-gate 	{
131*0Sstevel@tonic-gate 	BIO_ACCEPT *ba;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	bi->init=0;
134*0Sstevel@tonic-gate 	bi->num=INVALID_SOCKET;
135*0Sstevel@tonic-gate 	bi->flags=0;
136*0Sstevel@tonic-gate 	if ((ba=BIO_ACCEPT_new()) == NULL)
137*0Sstevel@tonic-gate 		return(0);
138*0Sstevel@tonic-gate 	bi->ptr=(char *)ba;
139*0Sstevel@tonic-gate 	ba->state=ACPT_S_BEFORE;
140*0Sstevel@tonic-gate 	bi->shutdown=1;
141*0Sstevel@tonic-gate 	return(1);
142*0Sstevel@tonic-gate 	}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate BIO_ACCEPT *BIO_ACCEPT_new(void)
145*0Sstevel@tonic-gate 	{
146*0Sstevel@tonic-gate 	BIO_ACCEPT *ret;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if ((ret=(BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL)
149*0Sstevel@tonic-gate 		return(NULL);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	memset(ret,0,sizeof(BIO_ACCEPT));
152*0Sstevel@tonic-gate 	ret->accept_sock=INVALID_SOCKET;
153*0Sstevel@tonic-gate 	ret->bind_mode=BIO_BIND_NORMAL;
154*0Sstevel@tonic-gate 	return(ret);
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate void BIO_ACCEPT_free(BIO_ACCEPT *a)
158*0Sstevel@tonic-gate 	{
159*0Sstevel@tonic-gate 	if(a == NULL)
160*0Sstevel@tonic-gate 	    return;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	if (a->param_addr != NULL) OPENSSL_free(a->param_addr);
163*0Sstevel@tonic-gate 	if (a->addr != NULL) OPENSSL_free(a->addr);
164*0Sstevel@tonic-gate 	if (a->bio_chain != NULL) BIO_free(a->bio_chain);
165*0Sstevel@tonic-gate 	OPENSSL_free(a);
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate static void acpt_close_socket(BIO *bio)
169*0Sstevel@tonic-gate 	{
170*0Sstevel@tonic-gate 	BIO_ACCEPT *c;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	c=(BIO_ACCEPT *)bio->ptr;
173*0Sstevel@tonic-gate 	if (c->accept_sock != INVALID_SOCKET)
174*0Sstevel@tonic-gate 		{
175*0Sstevel@tonic-gate 		shutdown(c->accept_sock,2);
176*0Sstevel@tonic-gate 		closesocket(c->accept_sock);
177*0Sstevel@tonic-gate 		c->accept_sock=INVALID_SOCKET;
178*0Sstevel@tonic-gate 		bio->num=INVALID_SOCKET;
179*0Sstevel@tonic-gate 		}
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate static int acpt_free(BIO *a)
183*0Sstevel@tonic-gate 	{
184*0Sstevel@tonic-gate 	BIO_ACCEPT *data;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	if (a == NULL) return(0);
187*0Sstevel@tonic-gate 	data=(BIO_ACCEPT *)a->ptr;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	if (a->shutdown)
190*0Sstevel@tonic-gate 		{
191*0Sstevel@tonic-gate 		acpt_close_socket(a);
192*0Sstevel@tonic-gate 		BIO_ACCEPT_free(data);
193*0Sstevel@tonic-gate 		a->ptr=NULL;
194*0Sstevel@tonic-gate 		a->flags=0;
195*0Sstevel@tonic-gate 		a->init=0;
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate 	return(1);
198*0Sstevel@tonic-gate 	}
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate static int acpt_state(BIO *b, BIO_ACCEPT *c)
201*0Sstevel@tonic-gate 	{
202*0Sstevel@tonic-gate 	BIO *bio=NULL,*dbio;
203*0Sstevel@tonic-gate 	int s= -1;
204*0Sstevel@tonic-gate 	int i;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate again:
207*0Sstevel@tonic-gate 	switch (c->state)
208*0Sstevel@tonic-gate 		{
209*0Sstevel@tonic-gate 	case ACPT_S_BEFORE:
210*0Sstevel@tonic-gate 		if (c->param_addr == NULL)
211*0Sstevel@tonic-gate 			{
212*0Sstevel@tonic-gate 			BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED);
213*0Sstevel@tonic-gate 			return(-1);
214*0Sstevel@tonic-gate 			}
215*0Sstevel@tonic-gate 		s=BIO_get_accept_socket(c->param_addr,c->bind_mode);
216*0Sstevel@tonic-gate 		if (s == INVALID_SOCKET)
217*0Sstevel@tonic-gate 			return(-1);
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 		if (c->accept_nbio)
220*0Sstevel@tonic-gate 			{
221*0Sstevel@tonic-gate 			if (!BIO_socket_nbio(s,1))
222*0Sstevel@tonic-gate 				{
223*0Sstevel@tonic-gate 				closesocket(s);
224*0Sstevel@tonic-gate 				BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
225*0Sstevel@tonic-gate 				return(-1);
226*0Sstevel@tonic-gate 				}
227*0Sstevel@tonic-gate 			}
228*0Sstevel@tonic-gate 		c->accept_sock=s;
229*0Sstevel@tonic-gate 		b->num=s;
230*0Sstevel@tonic-gate 		c->state=ACPT_S_GET_ACCEPT_SOCKET;
231*0Sstevel@tonic-gate 		return(1);
232*0Sstevel@tonic-gate 		/* break; */
233*0Sstevel@tonic-gate 	case ACPT_S_GET_ACCEPT_SOCKET:
234*0Sstevel@tonic-gate 		if (b->next_bio != NULL)
235*0Sstevel@tonic-gate 			{
236*0Sstevel@tonic-gate 			c->state=ACPT_S_OK;
237*0Sstevel@tonic-gate 			goto again;
238*0Sstevel@tonic-gate 			}
239*0Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
240*0Sstevel@tonic-gate 		b->retry_reason=0;
241*0Sstevel@tonic-gate 		i=BIO_accept(c->accept_sock,&(c->addr));
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 		/* -2 return means we should retry */
244*0Sstevel@tonic-gate 		if(i == -2)
245*0Sstevel@tonic-gate 			{
246*0Sstevel@tonic-gate 			BIO_set_retry_special(b);
247*0Sstevel@tonic-gate 			b->retry_reason=BIO_RR_ACCEPT;
248*0Sstevel@tonic-gate 			return -1;
249*0Sstevel@tonic-gate 			}
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 		if (i < 0) return(i);
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 		bio=BIO_new_socket(i,BIO_CLOSE);
254*0Sstevel@tonic-gate 		if (bio == NULL) goto err;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 		BIO_set_callback(bio,BIO_get_callback(b));
257*0Sstevel@tonic-gate 		BIO_set_callback_arg(bio,BIO_get_callback_arg(b));
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 		if (c->nbio)
260*0Sstevel@tonic-gate 			{
261*0Sstevel@tonic-gate 			if (!BIO_socket_nbio(i,1))
262*0Sstevel@tonic-gate 				{
263*0Sstevel@tonic-gate 				BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
264*0Sstevel@tonic-gate 				goto err;
265*0Sstevel@tonic-gate 				}
266*0Sstevel@tonic-gate 			}
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 		/* If the accept BIO has an bio_chain, we dup it and
269*0Sstevel@tonic-gate 		 * put the new socket at the end. */
270*0Sstevel@tonic-gate 		if (c->bio_chain != NULL)
271*0Sstevel@tonic-gate 			{
272*0Sstevel@tonic-gate 			if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL)
273*0Sstevel@tonic-gate 				goto err;
274*0Sstevel@tonic-gate 			if (!BIO_push(dbio,bio)) goto err;
275*0Sstevel@tonic-gate 			bio=dbio;
276*0Sstevel@tonic-gate 			}
277*0Sstevel@tonic-gate 		if (BIO_push(b,bio) == NULL) goto err;
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 		c->state=ACPT_S_OK;
280*0Sstevel@tonic-gate 		return(1);
281*0Sstevel@tonic-gate err:
282*0Sstevel@tonic-gate 		if (bio != NULL)
283*0Sstevel@tonic-gate 			BIO_free(bio);
284*0Sstevel@tonic-gate 		else if (s >= 0)
285*0Sstevel@tonic-gate 			closesocket(s);
286*0Sstevel@tonic-gate 		return(0);
287*0Sstevel@tonic-gate 		/* break; */
288*0Sstevel@tonic-gate 	case ACPT_S_OK:
289*0Sstevel@tonic-gate 		if (b->next_bio == NULL)
290*0Sstevel@tonic-gate 			{
291*0Sstevel@tonic-gate 			c->state=ACPT_S_GET_ACCEPT_SOCKET;
292*0Sstevel@tonic-gate 			goto again;
293*0Sstevel@tonic-gate 			}
294*0Sstevel@tonic-gate 		return(1);
295*0Sstevel@tonic-gate 		/* break; */
296*0Sstevel@tonic-gate 	default:
297*0Sstevel@tonic-gate 		return(0);
298*0Sstevel@tonic-gate 		/* break; */
299*0Sstevel@tonic-gate 		}
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	}
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate static int acpt_read(BIO *b, char *out, int outl)
304*0Sstevel@tonic-gate 	{
305*0Sstevel@tonic-gate 	int ret=0;
306*0Sstevel@tonic-gate 	BIO_ACCEPT *data;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
309*0Sstevel@tonic-gate 	data=(BIO_ACCEPT *)b->ptr;
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	while (b->next_bio == NULL)
312*0Sstevel@tonic-gate 		{
313*0Sstevel@tonic-gate 		ret=acpt_state(b,data);
314*0Sstevel@tonic-gate 		if (ret <= 0) return(ret);
315*0Sstevel@tonic-gate 		}
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	ret=BIO_read(b->next_bio,out,outl);
318*0Sstevel@tonic-gate 	BIO_copy_next_retry(b);
319*0Sstevel@tonic-gate 	return(ret);
320*0Sstevel@tonic-gate 	}
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate static int acpt_write(BIO *b, const char *in, int inl)
323*0Sstevel@tonic-gate 	{
324*0Sstevel@tonic-gate 	int ret;
325*0Sstevel@tonic-gate 	BIO_ACCEPT *data;
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
328*0Sstevel@tonic-gate 	data=(BIO_ACCEPT *)b->ptr;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	while (b->next_bio == NULL)
331*0Sstevel@tonic-gate 		{
332*0Sstevel@tonic-gate 		ret=acpt_state(b,data);
333*0Sstevel@tonic-gate 		if (ret <= 0) return(ret);
334*0Sstevel@tonic-gate 		}
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	ret=BIO_write(b->next_bio,in,inl);
337*0Sstevel@tonic-gate 	BIO_copy_next_retry(b);
338*0Sstevel@tonic-gate 	return(ret);
339*0Sstevel@tonic-gate 	}
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
342*0Sstevel@tonic-gate 	{
343*0Sstevel@tonic-gate 	BIO *dbio;
344*0Sstevel@tonic-gate 	int *ip;
345*0Sstevel@tonic-gate 	long ret=1;
346*0Sstevel@tonic-gate 	BIO_ACCEPT *data;
347*0Sstevel@tonic-gate 	char **pp;
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	data=(BIO_ACCEPT *)b->ptr;
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	switch (cmd)
352*0Sstevel@tonic-gate 		{
353*0Sstevel@tonic-gate 	case BIO_CTRL_RESET:
354*0Sstevel@tonic-gate 		ret=0;
355*0Sstevel@tonic-gate 		data->state=ACPT_S_BEFORE;
356*0Sstevel@tonic-gate 		acpt_close_socket(b);
357*0Sstevel@tonic-gate 		b->flags=0;
358*0Sstevel@tonic-gate 		break;
359*0Sstevel@tonic-gate 	case BIO_C_DO_STATE_MACHINE:
360*0Sstevel@tonic-gate 		/* use this one to start the connection */
361*0Sstevel@tonic-gate 		ret=(long)acpt_state(b,data);
362*0Sstevel@tonic-gate 		break;
363*0Sstevel@tonic-gate 	case BIO_C_SET_ACCEPT:
364*0Sstevel@tonic-gate 		if (ptr != NULL)
365*0Sstevel@tonic-gate 			{
366*0Sstevel@tonic-gate 			if (num == 0)
367*0Sstevel@tonic-gate 				{
368*0Sstevel@tonic-gate 				b->init=1;
369*0Sstevel@tonic-gate 				if (data->param_addr != NULL)
370*0Sstevel@tonic-gate 					OPENSSL_free(data->param_addr);
371*0Sstevel@tonic-gate 				data->param_addr=BUF_strdup(ptr);
372*0Sstevel@tonic-gate 				}
373*0Sstevel@tonic-gate 			else if (num == 1)
374*0Sstevel@tonic-gate 				{
375*0Sstevel@tonic-gate 				data->accept_nbio=(ptr != NULL);
376*0Sstevel@tonic-gate 				}
377*0Sstevel@tonic-gate 			else if (num == 2)
378*0Sstevel@tonic-gate 				{
379*0Sstevel@tonic-gate 				if (data->bio_chain != NULL)
380*0Sstevel@tonic-gate 					BIO_free(data->bio_chain);
381*0Sstevel@tonic-gate 				data->bio_chain=(BIO *)ptr;
382*0Sstevel@tonic-gate 				}
383*0Sstevel@tonic-gate 			}
384*0Sstevel@tonic-gate 		break;
385*0Sstevel@tonic-gate 	case BIO_C_SET_NBIO:
386*0Sstevel@tonic-gate 		data->nbio=(int)num;
387*0Sstevel@tonic-gate 		break;
388*0Sstevel@tonic-gate 	case BIO_C_SET_FD:
389*0Sstevel@tonic-gate 		b->init=1;
390*0Sstevel@tonic-gate 		b->num= *((int *)ptr);
391*0Sstevel@tonic-gate 		data->accept_sock=b->num;
392*0Sstevel@tonic-gate 		data->state=ACPT_S_GET_ACCEPT_SOCKET;
393*0Sstevel@tonic-gate 		b->shutdown=(int)num;
394*0Sstevel@tonic-gate 		b->init=1;
395*0Sstevel@tonic-gate 		break;
396*0Sstevel@tonic-gate 	case BIO_C_GET_FD:
397*0Sstevel@tonic-gate 		if (b->init)
398*0Sstevel@tonic-gate 			{
399*0Sstevel@tonic-gate 			ip=(int *)ptr;
400*0Sstevel@tonic-gate 			if (ip != NULL)
401*0Sstevel@tonic-gate 				*ip=data->accept_sock;
402*0Sstevel@tonic-gate 			ret=data->accept_sock;
403*0Sstevel@tonic-gate 			}
404*0Sstevel@tonic-gate 		else
405*0Sstevel@tonic-gate 			ret= -1;
406*0Sstevel@tonic-gate 		break;
407*0Sstevel@tonic-gate 	case BIO_C_GET_ACCEPT:
408*0Sstevel@tonic-gate 		if (b->init)
409*0Sstevel@tonic-gate 			{
410*0Sstevel@tonic-gate 			if (ptr != NULL)
411*0Sstevel@tonic-gate 				{
412*0Sstevel@tonic-gate 				pp=(char **)ptr;
413*0Sstevel@tonic-gate 				*pp=data->param_addr;
414*0Sstevel@tonic-gate 				}
415*0Sstevel@tonic-gate 			else
416*0Sstevel@tonic-gate 				ret= -1;
417*0Sstevel@tonic-gate 			}
418*0Sstevel@tonic-gate 		else
419*0Sstevel@tonic-gate 			ret= -1;
420*0Sstevel@tonic-gate 		break;
421*0Sstevel@tonic-gate 	case BIO_CTRL_GET_CLOSE:
422*0Sstevel@tonic-gate 		ret=b->shutdown;
423*0Sstevel@tonic-gate 		break;
424*0Sstevel@tonic-gate 	case BIO_CTRL_SET_CLOSE:
425*0Sstevel@tonic-gate 		b->shutdown=(int)num;
426*0Sstevel@tonic-gate 		break;
427*0Sstevel@tonic-gate 	case BIO_CTRL_PENDING:
428*0Sstevel@tonic-gate 	case BIO_CTRL_WPENDING:
429*0Sstevel@tonic-gate 		ret=0;
430*0Sstevel@tonic-gate 		break;
431*0Sstevel@tonic-gate 	case BIO_CTRL_FLUSH:
432*0Sstevel@tonic-gate 		break;
433*0Sstevel@tonic-gate 	case BIO_C_SET_BIND_MODE:
434*0Sstevel@tonic-gate 		data->bind_mode=(int)num;
435*0Sstevel@tonic-gate 		break;
436*0Sstevel@tonic-gate 	case BIO_C_GET_BIND_MODE:
437*0Sstevel@tonic-gate 		ret=(long)data->bind_mode;
438*0Sstevel@tonic-gate 		break;
439*0Sstevel@tonic-gate 	case BIO_CTRL_DUP:
440*0Sstevel@tonic-gate 		dbio=(BIO *)ptr;
441*0Sstevel@tonic-gate /*		if (data->param_port) EAY EAY
442*0Sstevel@tonic-gate 			BIO_set_port(dbio,data->param_port);
443*0Sstevel@tonic-gate 		if (data->param_hostname)
444*0Sstevel@tonic-gate 			BIO_set_hostname(dbio,data->param_hostname);
445*0Sstevel@tonic-gate 		BIO_set_nbio(dbio,data->nbio); */
446*0Sstevel@tonic-gate 		break;
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 	default:
449*0Sstevel@tonic-gate 		ret=0;
450*0Sstevel@tonic-gate 		break;
451*0Sstevel@tonic-gate 		}
452*0Sstevel@tonic-gate 	return(ret);
453*0Sstevel@tonic-gate 	}
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate static int acpt_puts(BIO *bp, const char *str)
456*0Sstevel@tonic-gate 	{
457*0Sstevel@tonic-gate 	int n,ret;
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	n=strlen(str);
460*0Sstevel@tonic-gate 	ret=acpt_write(bp,str,n);
461*0Sstevel@tonic-gate 	return(ret);
462*0Sstevel@tonic-gate 	}
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate BIO *BIO_new_accept(char *str)
465*0Sstevel@tonic-gate 	{
466*0Sstevel@tonic-gate 	BIO *ret;
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 	ret=BIO_new(BIO_s_accept());
469*0Sstevel@tonic-gate 	if (ret == NULL) return(NULL);
470*0Sstevel@tonic-gate 	if (BIO_set_accept_port(ret,str))
471*0Sstevel@tonic-gate 		return(ret);
472*0Sstevel@tonic-gate 	else
473*0Sstevel@tonic-gate 		{
474*0Sstevel@tonic-gate 		BIO_free(ret);
475*0Sstevel@tonic-gate 		return(NULL);
476*0Sstevel@tonic-gate 		}
477*0Sstevel@tonic-gate 	}
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate #endif
480