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