bss_acpt.c revision 55714
1/* crypto/bio/bss_acpt.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to.  The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 *    must display the following acknowledgement:
33 *    "This product includes cryptographic software written by
34 *     Eric Young (eay@cryptsoft.com)"
35 *    The word 'cryptographic' can be left out if the rouines from the library
36 *    being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 *    the apps directory (application code) you must include an acknowledgement:
39 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef NO_SOCK
60
61#include <stdio.h>
62#include <errno.h>
63#define USE_SOCKETS
64#include "cryptlib.h"
65#include <openssl/bio.h>
66
67#ifdef WIN16
68#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
69#else
70#define SOCKET_PROTOCOL IPPROTO_TCP
71#endif
72
73#if (defined(VMS) && __VMS_VER < 70000000)
74/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
75#undef FIONBIO
76#endif
77
78typedef struct bio_accept_st
79	{
80	int state;
81	char *param_addr;
82
83	int accept_sock;
84	int accept_nbio;
85
86	char *addr;
87	int nbio;
88	/* If 0, it means normal, if 1, do a connect on bind failure,
89	 * and if there is no-one listening, bind with SO_REUSEADDR.
90	 * If 2, always use SO_REUSEADDR. */
91	int bind_mode;
92	BIO *bio_chain;
93	} BIO_ACCEPT;
94
95static int acpt_write(BIO *h,char *buf,int num);
96static int acpt_read(BIO *h,char *buf,int size);
97static int acpt_puts(BIO *h,char *str);
98static long acpt_ctrl(BIO *h,int cmd,long arg1,char *arg2);
99static int acpt_new(BIO *h);
100static int acpt_free(BIO *data);
101static int acpt_state(BIO *b, BIO_ACCEPT *c);
102static void acpt_close_socket(BIO *data);
103BIO_ACCEPT *BIO_ACCEPT_new(void );
104void BIO_ACCEPT_free(BIO_ACCEPT *a);
105
106#define ACPT_S_BEFORE			1
107#define ACPT_S_GET_ACCEPT_SOCKET	2
108#define ACPT_S_OK			3
109
110static BIO_METHOD methods_acceptp=
111	{
112	BIO_TYPE_ACCEPT,
113	"socket accept",
114	acpt_write,
115	acpt_read,
116	acpt_puts,
117	NULL, /* connect_gets, */
118	acpt_ctrl,
119	acpt_new,
120	acpt_free,
121	};
122
123BIO_METHOD *BIO_s_accept(void)
124	{
125	return(&methods_acceptp);
126	}
127
128static int acpt_new(BIO *bi)
129	{
130	BIO_ACCEPT *ba;
131
132	bi->init=0;
133	bi->num=INVALID_SOCKET;
134	bi->flags=0;
135	if ((ba=BIO_ACCEPT_new()) == NULL)
136		return(0);
137	bi->ptr=(char *)ba;
138	ba->state=ACPT_S_BEFORE;
139	bi->shutdown=1;
140	return(1);
141	}
142
143BIO_ACCEPT *BIO_ACCEPT_new(void)
144	{
145	BIO_ACCEPT *ret;
146
147	if ((ret=(BIO_ACCEPT *)Malloc(sizeof(BIO_ACCEPT))) == NULL)
148		return(NULL);
149
150	memset(ret,0,sizeof(BIO_ACCEPT));
151	ret->accept_sock=INVALID_SOCKET;
152	ret->bind_mode=BIO_BIND_NORMAL;
153	return(ret);
154	}
155
156void BIO_ACCEPT_free(BIO_ACCEPT *a)
157	{
158	if(a == NULL)
159	    return;
160
161	if (a->param_addr != NULL) Free(a->param_addr);
162	if (a->addr != NULL) Free(a->addr);
163	if (a->bio_chain != NULL) BIO_free(a->bio_chain);
164	Free(a);
165	}
166
167static void acpt_close_socket(BIO *bio)
168	{
169	BIO_ACCEPT *c;
170
171	c=(BIO_ACCEPT *)bio->ptr;
172	if (c->accept_sock != INVALID_SOCKET)
173		{
174		shutdown(c->accept_sock,2);
175		closesocket(c->accept_sock);
176		c->accept_sock=INVALID_SOCKET;
177		bio->num=INVALID_SOCKET;
178		}
179	}
180
181static int acpt_free(BIO *a)
182	{
183	BIO_ACCEPT *data;
184
185	if (a == NULL) return(0);
186	data=(BIO_ACCEPT *)a->ptr;
187
188	if (a->shutdown)
189		{
190		acpt_close_socket(a);
191		BIO_ACCEPT_free(data);
192		a->ptr=NULL;
193		a->flags=0;
194		a->init=0;
195		}
196	return(1);
197	}
198
199static int acpt_state(BIO *b, BIO_ACCEPT *c)
200	{
201	BIO *bio=NULL,*dbio;
202	int s= -1;
203	int i;
204
205again:
206	switch (c->state)
207		{
208	case ACPT_S_BEFORE:
209		if (c->param_addr == NULL)
210			{
211			BIOerr(BIO_F_ACPT_STATE,BIO_R_NO_ACCEPT_PORT_SPECIFIED);
212			return(-1);
213			}
214		s=BIO_get_accept_socket(c->param_addr,c->bind_mode);
215		if (s == INVALID_SOCKET)
216			return(-1);
217
218		if (c->accept_nbio)
219			{
220			if (!BIO_socket_nbio(s,1))
221				{
222				closesocket(s);
223				BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET);
224				return(-1);
225				}
226			}
227		c->accept_sock=s;
228		b->num=s;
229		c->state=ACPT_S_GET_ACCEPT_SOCKET;
230		return(1);
231		/* break; */
232	case ACPT_S_GET_ACCEPT_SOCKET:
233		if (b->next_bio != NULL)
234			{
235			c->state=ACPT_S_OK;
236			goto again;
237			}
238		i=BIO_accept(c->accept_sock,&(c->addr));
239		if (i < 0) return(i);
240		bio=BIO_new_socket(i,BIO_CLOSE);
241		if (bio == NULL) goto err;
242
243		BIO_set_callback(bio,BIO_get_callback(b));
244		BIO_set_callback_arg(bio,BIO_get_callback_arg(b));
245
246		if (c->nbio)
247			{
248			if (!BIO_socket_nbio(i,1))
249				{
250				BIOerr(BIO_F_ACPT_STATE,BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET);
251				goto err;
252				}
253			}
254
255		/* If the accept BIO has an bio_chain, we dup it and
256		 * put the new socket at the end. */
257		if (c->bio_chain != NULL)
258			{
259			if ((dbio=BIO_dup_chain(c->bio_chain)) == NULL)
260				goto err;
261			if (!BIO_push(dbio,bio)) goto err;
262			bio=dbio;
263			}
264		if (BIO_push(b,bio) == NULL) goto err;
265
266		c->state=ACPT_S_OK;
267		return(1);
268err:
269		if (bio != NULL)
270			BIO_free(bio);
271		else if (s >= 0)
272			closesocket(s);
273		return(0);
274		/* break; */
275	case ACPT_S_OK:
276		if (b->next_bio == NULL)
277			{
278			c->state=ACPT_S_GET_ACCEPT_SOCKET;
279			goto again;
280			}
281		return(1);
282		/* break; */
283	default:
284		return(0);
285		/* break; */
286		}
287
288	}
289
290static int acpt_read(BIO *b, char *out, int outl)
291	{
292	int ret=0;
293	BIO_ACCEPT *data;
294
295	BIO_clear_retry_flags(b);
296	data=(BIO_ACCEPT *)b->ptr;
297
298	while (b->next_bio == NULL)
299		{
300		ret=acpt_state(b,data);
301		if (ret <= 0) return(ret);
302		}
303
304	ret=BIO_read(b->next_bio,out,outl);
305	BIO_copy_next_retry(b);
306	return(ret);
307	}
308
309static int acpt_write(BIO *b, char *in, int inl)
310	{
311	int ret;
312	BIO_ACCEPT *data;
313
314	BIO_clear_retry_flags(b);
315	data=(BIO_ACCEPT *)b->ptr;
316
317	while (b->next_bio == NULL)
318		{
319		ret=acpt_state(b,data);
320		if (ret <= 0) return(ret);
321		}
322
323	ret=BIO_write(b->next_bio,in,inl);
324	BIO_copy_next_retry(b);
325	return(ret);
326	}
327
328static long acpt_ctrl(BIO *b, int cmd, long num, char *ptr)
329	{
330	BIO *dbio;
331	int *ip;
332	long ret=1;
333	BIO_ACCEPT *data;
334	char **pp;
335
336	data=(BIO_ACCEPT *)b->ptr;
337
338	switch (cmd)
339		{
340	case BIO_CTRL_RESET:
341		ret=0;
342		data->state=ACPT_S_BEFORE;
343		acpt_close_socket(b);
344		b->flags=0;
345		break;
346	case BIO_C_DO_STATE_MACHINE:
347		/* use this one to start the connection */
348		ret=(long)acpt_state(b,data);
349		break;
350	case BIO_C_SET_ACCEPT:
351		if (ptr != NULL)
352			{
353			if (num == 0)
354				{
355				b->init=1;
356				if (data->param_addr != NULL)
357					Free(data->param_addr);
358				data->param_addr=BUF_strdup(ptr);
359				}
360			else if (num == 1)
361				{
362				data->accept_nbio=(ptr != NULL);
363				}
364			else if (num == 2)
365				{
366				if (data->bio_chain != NULL)
367					BIO_free(data->bio_chain);
368				data->bio_chain=(BIO *)ptr;
369				}
370			}
371		break;
372	case BIO_C_SET_NBIO:
373		data->nbio=(int)num;
374		break;
375	case BIO_C_SET_FD:
376		b->init=1;
377		b->num= *((int *)ptr);
378		data->accept_sock=b->num;
379		data->state=ACPT_S_GET_ACCEPT_SOCKET;
380		b->shutdown=(int)num;
381		b->init=1;
382		break;
383	case BIO_C_GET_FD:
384		if (b->init)
385			{
386			ip=(int *)ptr;
387			if (ip != NULL)
388				*ip=data->accept_sock;
389			ret=data->accept_sock;
390			}
391		else
392			ret= -1;
393		break;
394	case BIO_C_GET_ACCEPT:
395		if (b->init)
396			{
397			if (ptr != NULL)
398				{
399				pp=(char **)ptr;
400				*pp=data->param_addr;
401				}
402			else
403				ret= -1;
404			}
405		else
406			ret= -1;
407		break;
408	case BIO_CTRL_GET_CLOSE:
409		ret=b->shutdown;
410		break;
411	case BIO_CTRL_SET_CLOSE:
412		b->shutdown=(int)num;
413		break;
414	case BIO_CTRL_PENDING:
415	case BIO_CTRL_WPENDING:
416		ret=0;
417		break;
418	case BIO_CTRL_FLUSH:
419		break;
420	case BIO_C_SET_BIND_MODE:
421		data->bind_mode=(int)num;
422		break;
423	case BIO_C_GET_BIND_MODE:
424		ret=(long)data->bind_mode;
425		break;
426	case BIO_CTRL_DUP:
427		dbio=(BIO *)ptr;
428/*		if (data->param_port) EAY EAY
429			BIO_set_port(dbio,data->param_port);
430		if (data->param_hostname)
431			BIO_set_hostname(dbio,data->param_hostname);
432		BIO_set_nbio(dbio,data->nbio); */
433		break;
434
435	default:
436		ret=0;
437		break;
438		}
439	return(ret);
440	}
441
442static int acpt_puts(BIO *bp, char *str)
443	{
444	int n,ret;
445
446	n=strlen(str);
447	ret=acpt_write(bp,str,n);
448	return(ret);
449	}
450
451BIO *BIO_new_accept(char *str)
452	{
453	BIO *ret;
454
455	ret=BIO_new(BIO_s_accept());
456	if (ret == NULL) return(NULL);
457	if (BIO_set_accept_port(ret,str))
458		return(ret);
459	else
460		{
461		BIO_free(ret);
462		return(NULL);
463		}
464	}
465
466#endif
467