bss_acpt.c revision 656d9c7f52f88b3a3daccafa7655dec086c4756e
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* crypto/bio/bss_acpt.c */
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All rights reserved.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This package is an SSL implementation written
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * by Eric Young (eay@cryptsoft.com).
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The implementation was written so as to conform with Netscapes SSL.
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This library is free for commercial and non-commercial use as long as
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the following conditions are aheared to.  The following conditions
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * apply to all code found in this distribution, be it the RC4, RSA,
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * included with this distribution is covered by the same copyright terms
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright remains Eric Young's, and as such any Copyright notices in
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the code are not to be removed.
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * If this package is used in a product, Eric Young should be given attribution
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * as the author of the parts of the library used.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This can be in the form of a textual message at program startup or
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * in documentation (online or textual) provided with the package.
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * modification, are permitted provided that the following conditions
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * are met:
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1. Redistributions of source code must retain the copyright
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    documentation and/or other materials provided with the distribution.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 3. All advertising materials mentioning features or use of this software
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    must display the following acknowledgement:
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    "This product includes cryptographic software written by
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     Eric Young (eay@cryptsoft.com)"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    The word 'cryptographic' can be left out if the rouines from the library
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    being used are not cryptographic related :-).
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 4. If you include any Windows specific code (or a derivative thereof) from
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    the apps directory (application code) you must include an acknowledgement:
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SUCH DAMAGE.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The licence and distribution terms for any publically available version or
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * derivative of this code cannot be changed.  i.e. this code cannot simply be
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copied and put under another distribution licence
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * [including the GNU Public Licence.]
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <stdio.h>
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <errno.h>
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define USE_SOCKETS
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "cryptlib.h"
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <openssl/bio.h>
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef OPENSSL_NO_SOCK
665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifdef OPENSSL_SYS_WIN16
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#else
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SOCKET_PROTOCOL IPPROTO_TCP
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef FIONBIO
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef struct bio_accept_st
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	{
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int state;
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	char *param_addr;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int accept_sock;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int accept_nbio;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	char *addr;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int nbio;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* If 0, it means normal, if 1, do a connect on bind failure,
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 * and if there is no-one listening, bind with SO_REUSEADDR.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 * If 2, always use SO_REUSEADDR. */
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int bind_mode;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	BIO *bio_chain;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} BIO_ACCEPT;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int acpt_write(BIO *h, const char *buf, int num);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int acpt_read(BIO *h, char *buf, int size);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int acpt_puts(BIO *h, const char *str);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int acpt_new(BIO *h);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int acpt_free(BIO *data);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int acpt_state(BIO *b, BIO_ACCEPT *c);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void acpt_close_socket(BIO *data);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BIO_ACCEPT *BIO_ACCEPT_new(void );
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void BIO_ACCEPT_free(BIO_ACCEPT *a);
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define ACPT_S_BEFORE			1
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define ACPT_S_GET_ACCEPT_SOCKET	2
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define ACPT_S_OK			3
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static BIO_METHOD methods_acceptp=
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	{
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	BIO_TYPE_ACCEPT,
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	"socket accept",
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	acpt_write,
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	acpt_read,
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	acpt_puts,
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	NULL, /* connect_gets, */
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	acpt_ctrl,
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	acpt_new,
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	acpt_free,
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	NULL,
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	};
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BIO_METHOD *BIO_s_accept(void)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	{
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return(&methods_acceptp);
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	}
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int acpt_new(BIO *bi)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	{
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	BIO_ACCEPT *ba;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bi->init=0;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bi->num=INVALID_SOCKET;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bi->flags=0;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if ((ba=BIO_ACCEPT_new()) == NULL)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return(0);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bi->ptr=(char *)ba;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	ba->state=ACPT_S_BEFORE;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	bi->shutdown=1;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return(1);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)BIO_ACCEPT *BIO_ACCEPT_new(void)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	{
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	BIO_ACCEPT *ret;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if ((ret=(BIO_ACCEPT *)OPENSSL_malloc(sizeof(BIO_ACCEPT))) == NULL)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return(NULL);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	memset(ret,0,sizeof(BIO_ACCEPT));
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	ret->accept_sock=INVALID_SOCKET;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	ret->bind_mode=BIO_BIND_NORMAL;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return(ret);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void BIO_ACCEPT_free(BIO_ACCEPT *a)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	{
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if(a == NULL)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    return;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (a->param_addr != NULL) OPENSSL_free(a->param_addr);
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (a->addr != NULL) OPENSSL_free(a->addr);
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (a->bio_chain != NULL) BIO_free(a->bio_chain);
165	OPENSSL_free(a);
166	}
167
168static void acpt_close_socket(BIO *bio)
169	{
170	BIO_ACCEPT *c;
171
172	c=(BIO_ACCEPT *)bio->ptr;
173	if (c->accept_sock != INVALID_SOCKET)
174		{
175		shutdown(c->accept_sock,2);
176		closesocket(c->accept_sock);
177		c->accept_sock=INVALID_SOCKET;
178		bio->num=INVALID_SOCKET;
179		}
180	}
181
182static int acpt_free(BIO *a)
183	{
184	BIO_ACCEPT *data;
185
186	if (a == NULL) return(0);
187	data=(BIO_ACCEPT *)a->ptr;
188
189	if (a->shutdown)
190		{
191		acpt_close_socket(a);
192		BIO_ACCEPT_free(data);
193		a->ptr=NULL;
194		a->flags=0;
195		a->init=0;
196		}
197	return(1);
198	}
199
200static int acpt_state(BIO *b, BIO_ACCEPT *c)
201	{
202	BIO *bio=NULL,*dbio;
203	int s= -1;
204	int i;
205
206again:
207	switch (c->state)
208		{
209	case ACPT_S_BEFORE:
210		if (c->param_addr == NULL)
211			{
212			BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED);
213			return(-1);
214			}
215		s=BIO_get_accept_socket(c->param_addr,c->bind_mode);
216		if (s == INVALID_SOCKET)
217			return(-1);
218
219		if (c->accept_nbio)
220			{
221			if (!BIO_socket_nbio(s,1))
222				{
223				closesocket(s);
224				BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
225				return(-1);
226				}
227			}
228		c->accept_sock=s;
229		b->num=s;
230		c->state=ACPT_S_GET_ACCEPT_SOCKET;
231		return(1);
232		/* break; */
233	case ACPT_S_GET_ACCEPT_SOCKET:
234		if (b->next_bio != NULL)
235			{
236			c->state=ACPT_S_OK;
237			goto again;
238			}
239		BIO_clear_retry_flags(b);
240		b->retry_reason=0;
241		i=BIO_accept(c->accept_sock,&(c->addr));
242
243		/* -2 return means we should retry */
244		if(i == -2)
245			{
246			BIO_set_retry_special(b);
247			b->retry_reason=BIO_RR_ACCEPT;
248			return -1;
249			}
250
251		if (i < 0) return(i);
252
253		bio=BIO_new_socket(i,BIO_CLOSE);
254		if (bio == NULL) goto err;
255
256		BIO_set_callback(bio,BIO_get_callback(b));
257		BIO_set_callback_arg(bio,BIO_get_callback_arg(b));
258
259		if (c->nbio)
260			{
261			if (!BIO_socket_nbio(i,1))
262				{
263				BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
264				goto err;
265				}
266			}
267
268		/* If the accept BIO has an bio_chain, we dup it and
269		 * put the new socket at the end. */
270		if (c->bio_chain != NULL)
271			{
272			if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL)
273				goto err;
274			if (!BIO_push(dbio,bio)) goto err;
275			bio=dbio;
276			}
277		if (BIO_push(b,bio) == NULL) goto err;
278
279		c->state=ACPT_S_OK;
280		return(1);
281err:
282		if (bio != NULL)
283			BIO_free(bio);
284		else if (s >= 0)
285			closesocket(s);
286		return(0);
287		/* break; */
288	case ACPT_S_OK:
289		if (b->next_bio == NULL)
290			{
291			c->state=ACPT_S_GET_ACCEPT_SOCKET;
292			goto again;
293			}
294		return(1);
295		/* break; */
296	default:
297		return(0);
298		/* break; */
299		}
300
301	}
302
303static int acpt_read(BIO *b, char *out, int outl)
304	{
305	int ret=0;
306	BIO_ACCEPT *data;
307
308	BIO_clear_retry_flags(b);
309	data=(BIO_ACCEPT *)b->ptr;
310
311	while (b->next_bio == NULL)
312		{
313		ret=acpt_state(b,data);
314		if (ret <= 0) return(ret);
315		}
316
317	ret=BIO_read(b->next_bio,out,outl);
318	BIO_copy_next_retry(b);
319	return(ret);
320	}
321
322static int acpt_write(BIO *b, const char *in, int inl)
323	{
324	int ret;
325	BIO_ACCEPT *data;
326
327	BIO_clear_retry_flags(b);
328	data=(BIO_ACCEPT *)b->ptr;
329
330	while (b->next_bio == NULL)
331		{
332		ret=acpt_state(b,data);
333		if (ret <= 0) return(ret);
334		}
335
336	ret=BIO_write(b->next_bio,in,inl);
337	BIO_copy_next_retry(b);
338	return(ret);
339	}
340
341static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
342	{
343	BIO *dbio;
344	int *ip;
345	long ret=1;
346	BIO_ACCEPT *data;
347	char **pp;
348
349	data=(BIO_ACCEPT *)b->ptr;
350
351	switch (cmd)
352		{
353	case BIO_CTRL_RESET:
354		ret=0;
355		data->state=ACPT_S_BEFORE;
356		acpt_close_socket(b);
357		b->flags=0;
358		break;
359	case BIO_C_DO_STATE_MACHINE:
360		/* use this one to start the connection */
361		ret=(long)acpt_state(b,data);
362		break;
363	case BIO_C_SET_ACCEPT:
364		if (ptr != NULL)
365			{
366			if (num == 0)
367				{
368				b->init=1;
369				if (data->param_addr != NULL)
370					OPENSSL_free(data->param_addr);
371				data->param_addr=BUF_strdup(ptr);
372				}
373			else if (num == 1)
374				{
375				data->accept_nbio=(ptr != NULL);
376				}
377			else if (num == 2)
378				{
379				if (data->bio_chain != NULL)
380					BIO_free(data->bio_chain);
381				data->bio_chain=(BIO *)ptr;
382				}
383			}
384		break;
385	case BIO_C_SET_NBIO:
386		data->nbio=(int)num;
387		break;
388	case BIO_C_SET_FD:
389		b->init=1;
390		b->num= *((int *)ptr);
391		data->accept_sock=b->num;
392		data->state=ACPT_S_GET_ACCEPT_SOCKET;
393		b->shutdown=(int)num;
394		b->init=1;
395		break;
396	case BIO_C_GET_FD:
397		if (b->init)
398			{
399			ip=(int *)ptr;
400			if (ip != NULL)
401				*ip=data->accept_sock;
402			ret=data->accept_sock;
403			}
404		else
405			ret= -1;
406		break;
407	case BIO_C_GET_ACCEPT:
408		if (b->init)
409			{
410			if (ptr != NULL)
411				{
412				pp=(char **)ptr;
413				*pp=data->param_addr;
414				}
415			else
416				ret= -1;
417			}
418		else
419			ret= -1;
420		break;
421	case BIO_CTRL_GET_CLOSE:
422		ret=b->shutdown;
423		break;
424	case BIO_CTRL_SET_CLOSE:
425		b->shutdown=(int)num;
426		break;
427	case BIO_CTRL_PENDING:
428	case BIO_CTRL_WPENDING:
429		ret=0;
430		break;
431	case BIO_CTRL_FLUSH:
432		break;
433	case BIO_C_SET_BIND_MODE:
434		data->bind_mode=(int)num;
435		break;
436	case BIO_C_GET_BIND_MODE:
437		ret=(long)data->bind_mode;
438		break;
439	case BIO_CTRL_DUP:
440		dbio=(BIO *)ptr;
441/*		if (data->param_port) EAY EAY
442			BIO_set_port(dbio,data->param_port);
443		if (data->param_hostname)
444			BIO_set_hostname(dbio,data->param_hostname);
445		BIO_set_nbio(dbio,data->nbio); */
446		break;
447
448	default:
449		ret=0;
450		break;
451		}
452	return(ret);
453	}
454
455static int acpt_puts(BIO *bp, const char *str)
456	{
457	int n,ret;
458
459	n=strlen(str);
460	ret=acpt_write(bp,str,n);
461	return(ret);
462	}
463
464BIO *BIO_new_accept(char *str)
465	{
466	BIO *ret;
467
468	ret=BIO_new(BIO_s_accept());
469	if (ret == NULL) return(NULL);
470	if (BIO_set_accept_port(ret,str))
471		return(ret);
472	else
473		{
474		BIO_free(ret);
475		return(NULL);
476		}
477	}
478
479#endif
480