os/ossrv/ssl/libcrypto/src/crypto/bio/bss_conn.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* crypto/bio/bss_conn.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
/*
sl@0
    59
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
sl@0
    62
sl@0
    63
#include <stdio.h>
sl@0
    64
#include <errno.h>
sl@0
    65
#define USE_SOCKETS
sl@0
    66
#include "cryptlib.h"
sl@0
    67
#include <openssl/bio.h>
sl@0
    68
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    69
#include "libcrypto_wsd_macros.h"
sl@0
    70
#include "libcrypto_wsd.h"
sl@0
    71
#endif
sl@0
    72
sl@0
    73
#ifndef OPENSSL_NO_SOCK
sl@0
    74
sl@0
    75
#ifdef OPENSSL_SYS_WIN16
sl@0
    76
#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
sl@0
    77
#else
sl@0
    78
#define SOCKET_PROTOCOL IPPROTO_TCP
sl@0
    79
#endif
sl@0
    80
sl@0
    81
#if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000)
sl@0
    82
/* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */
sl@0
    83
#undef FIONBIO
sl@0
    84
#endif
sl@0
    85
sl@0
    86
sl@0
    87
typedef struct bio_connect_st
sl@0
    88
	{
sl@0
    89
	int state;
sl@0
    90
sl@0
    91
	char *param_hostname;
sl@0
    92
	char *param_port;
sl@0
    93
	int nbio;
sl@0
    94
sl@0
    95
	unsigned char ip[4];
sl@0
    96
	unsigned short port;
sl@0
    97
sl@0
    98
	struct sockaddr_in them;
sl@0
    99
sl@0
   100
	/* int socket; this will be kept in bio->num so that it is
sl@0
   101
	 * compatible with the bss_sock bio */ 
sl@0
   102
sl@0
   103
	/* called when the connection is initially made
sl@0
   104
	 *  callback(BIO,state,ret);  The callback should return
sl@0
   105
	 * 'ret'.  state is for compatibility with the ssl info_callback */
sl@0
   106
	int (*info_callback)(const BIO *bio,int state,int ret);
sl@0
   107
	} BIO_CONNECT;
sl@0
   108
sl@0
   109
static int conn_write(BIO *h, const char *buf, int num);
sl@0
   110
static int conn_read(BIO *h, char *buf, int size);
sl@0
   111
static int conn_puts(BIO *h, const char *str);
sl@0
   112
static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
sl@0
   113
static int conn_new(BIO *h);
sl@0
   114
static int conn_free(BIO *data);
sl@0
   115
static long conn_callback_ctrl(BIO *h, int cmd, bio_info_cb *);
sl@0
   116
sl@0
   117
static int conn_state(BIO *b, BIO_CONNECT *c);
sl@0
   118
static void conn_close_socket(BIO *data);
sl@0
   119
BIO_CONNECT *BIO_CONNECT_new(void );
sl@0
   120
void BIO_CONNECT_free(BIO_CONNECT *a);
sl@0
   121
sl@0
   122
#ifndef EMULATOR
sl@0
   123
static BIO_METHOD methods_connectp=
sl@0
   124
	{
sl@0
   125
	BIO_TYPE_CONNECT,
sl@0
   126
	"socket connect",
sl@0
   127
	conn_write,
sl@0
   128
	conn_read,
sl@0
   129
	conn_puts,
sl@0
   130
	NULL, /* connect_gets, */
sl@0
   131
	conn_ctrl,
sl@0
   132
	conn_new,
sl@0
   133
	conn_free,
sl@0
   134
	conn_callback_ctrl,
sl@0
   135
	};
sl@0
   136
#else
sl@0
   137
sl@0
   138
GET_STATIC_VAR_FROM_TLS(methods_connectp,bss_conn,BIO_METHOD)
sl@0
   139
#define methods_connectp (*GET_WSD_VAR_NAME(methods_connectp,bss_conn,s)())
sl@0
   140
const BIO_METHOD temp_s_methods_connectp=
sl@0
   141
	{
sl@0
   142
	BIO_TYPE_CONNECT,
sl@0
   143
	"socket connect",
sl@0
   144
	conn_write,
sl@0
   145
	conn_read,
sl@0
   146
	conn_puts,
sl@0
   147
	NULL, /* connect_gets, */
sl@0
   148
	conn_ctrl,
sl@0
   149
	conn_new,
sl@0
   150
	conn_free,
sl@0
   151
	conn_callback_ctrl,
sl@0
   152
	};
sl@0
   153
#endif		
sl@0
   154
sl@0
   155
static int conn_state(BIO *b, BIO_CONNECT *c)
sl@0
   156
	{
sl@0
   157
	int ret= -1,i;
sl@0
   158
	unsigned long l;
sl@0
   159
	char *p,*q;
sl@0
   160
	int (*cb)(const BIO *,int,int)=NULL;
sl@0
   161
sl@0
   162
	if (c->info_callback != NULL)
sl@0
   163
		cb=c->info_callback;
sl@0
   164
sl@0
   165
	for (;;)
sl@0
   166
		{
sl@0
   167
		switch (c->state)
sl@0
   168
			{
sl@0
   169
		case BIO_CONN_S_BEFORE:
sl@0
   170
			p=c->param_hostname;
sl@0
   171
			if (p == NULL)
sl@0
   172
				{
sl@0
   173
				BIOerr(BIO_F_CONN_STATE,BIO_R_NO_HOSTNAME_SPECIFIED);
sl@0
   174
				goto exit_loop;
sl@0
   175
				}
sl@0
   176
			for ( ; *p != '\0'; p++)
sl@0
   177
				{
sl@0
   178
				if ((*p == ':') || (*p == '/')) break;
sl@0
   179
				}
sl@0
   180
sl@0
   181
			i= *p;
sl@0
   182
			if ((i == ':') || (i == '/'))
sl@0
   183
				{
sl@0
   184
sl@0
   185
				*(p++)='\0';
sl@0
   186
				if (i == ':')
sl@0
   187
					{
sl@0
   188
					for (q=p; *q; q++)
sl@0
   189
						if (*q == '/')
sl@0
   190
							{
sl@0
   191
							*q='\0';
sl@0
   192
							break;
sl@0
   193
							}
sl@0
   194
					if (c->param_port != NULL)
sl@0
   195
						OPENSSL_free(c->param_port);
sl@0
   196
					c->param_port=BUF_strdup(p);
sl@0
   197
					}
sl@0
   198
				}
sl@0
   199
sl@0
   200
			if (c->param_port == NULL)
sl@0
   201
				{
sl@0
   202
				BIOerr(BIO_F_CONN_STATE,BIO_R_NO_PORT_SPECIFIED);
sl@0
   203
				ERR_add_error_data(2,"host=",c->param_hostname);
sl@0
   204
				goto exit_loop;
sl@0
   205
				}
sl@0
   206
			c->state=BIO_CONN_S_GET_IP;
sl@0
   207
			break;
sl@0
   208
sl@0
   209
		case BIO_CONN_S_GET_IP:
sl@0
   210
			if (BIO_get_host_ip(c->param_hostname,&(c->ip[0])) <= 0)
sl@0
   211
				goto exit_loop;
sl@0
   212
			c->state=BIO_CONN_S_GET_PORT;
sl@0
   213
			break;
sl@0
   214
sl@0
   215
		case BIO_CONN_S_GET_PORT:
sl@0
   216
			if (c->param_port == NULL)
sl@0
   217
				{
sl@0
   218
				/* abort(); */
sl@0
   219
				goto exit_loop;
sl@0
   220
				}
sl@0
   221
			else if (BIO_get_port(c->param_port,&c->port) <= 0)
sl@0
   222
				goto exit_loop;
sl@0
   223
			c->state=BIO_CONN_S_CREATE_SOCKET;
sl@0
   224
			break;
sl@0
   225
sl@0
   226
		case BIO_CONN_S_CREATE_SOCKET:
sl@0
   227
			/* now setup address */
sl@0
   228
			memset((char *)&c->them,0,sizeof(c->them));
sl@0
   229
			c->them.sin_family=AF_INET;
sl@0
   230
			c->them.sin_port=htons((unsigned short)c->port);
sl@0
   231
			l=(unsigned long)
sl@0
   232
				((unsigned long)c->ip[0]<<24L)|
sl@0
   233
				((unsigned long)c->ip[1]<<16L)|
sl@0
   234
				((unsigned long)c->ip[2]<< 8L)|
sl@0
   235
				((unsigned long)c->ip[3]);
sl@0
   236
			c->them.sin_addr.s_addr=htonl(l);
sl@0
   237
			c->state=BIO_CONN_S_CREATE_SOCKET;
sl@0
   238
sl@0
   239
			ret=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL);
sl@0
   240
			if (ret == INVALID_SOCKET)
sl@0
   241
				{
sl@0
   242
				SYSerr(SYS_F_SOCKET,get_last_socket_error());
sl@0
   243
				ERR_add_error_data(4,"host=",c->param_hostname,
sl@0
   244
					":",c->param_port);
sl@0
   245
				BIOerr(BIO_F_CONN_STATE,BIO_R_UNABLE_TO_CREATE_SOCKET);
sl@0
   246
				goto exit_loop;
sl@0
   247
				}
sl@0
   248
			b->num=ret;
sl@0
   249
			c->state=BIO_CONN_S_NBIO;
sl@0
   250
			break;
sl@0
   251
sl@0
   252
		case BIO_CONN_S_NBIO:
sl@0
   253
			if (c->nbio)
sl@0
   254
				{
sl@0
   255
				if (!BIO_socket_nbio(b->num,1))
sl@0
   256
					{
sl@0
   257
					BIOerr(BIO_F_CONN_STATE,BIO_R_ERROR_SETTING_NBIO);
sl@0
   258
					ERR_add_error_data(4,"host=",
sl@0
   259
						c->param_hostname,
sl@0
   260
						":",c->param_port);
sl@0
   261
					goto exit_loop;
sl@0
   262
					}
sl@0
   263
				}
sl@0
   264
			c->state=BIO_CONN_S_CONNECT;
sl@0
   265
sl@0
   266
#if defined(SO_KEEPALIVE) && !defined(OPENSSL_SYS_MPE)
sl@0
   267
			i=1;
sl@0
   268
			i=setsockopt(b->num,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
sl@0
   269
			if (i < 0)
sl@0
   270
				{
sl@0
   271
				SYSerr(SYS_F_SOCKET,get_last_socket_error());
sl@0
   272
				ERR_add_error_data(4,"host=",c->param_hostname,
sl@0
   273
					":",c->param_port);
sl@0
   274
				BIOerr(BIO_F_CONN_STATE,BIO_R_KEEPALIVE);
sl@0
   275
				goto exit_loop;
sl@0
   276
				}
sl@0
   277
#endif
sl@0
   278
			break;
sl@0
   279
sl@0
   280
		case BIO_CONN_S_CONNECT:
sl@0
   281
			BIO_clear_retry_flags(b);
sl@0
   282
			ret=connect(b->num,
sl@0
   283
				(struct sockaddr *)&c->them,
sl@0
   284
				sizeof(c->them));
sl@0
   285
			b->retry_reason=0;
sl@0
   286
			if (ret < 0)
sl@0
   287
				{
sl@0
   288
				if (BIO_sock_should_retry(ret))
sl@0
   289
					{
sl@0
   290
					BIO_set_retry_special(b);
sl@0
   291
					c->state=BIO_CONN_S_BLOCKED_CONNECT;
sl@0
   292
					b->retry_reason=BIO_RR_CONNECT;
sl@0
   293
					}
sl@0
   294
				else
sl@0
   295
					{
sl@0
   296
					SYSerr(SYS_F_CONNECT,get_last_socket_error());
sl@0
   297
					ERR_add_error_data(4,"host=",
sl@0
   298
						c->param_hostname,
sl@0
   299
						":",c->param_port);
sl@0
   300
					BIOerr(BIO_F_CONN_STATE,BIO_R_CONNECT_ERROR);
sl@0
   301
					}
sl@0
   302
				goto exit_loop;
sl@0
   303
				}
sl@0
   304
			else
sl@0
   305
				c->state=BIO_CONN_S_OK;
sl@0
   306
			break;
sl@0
   307
sl@0
   308
		case BIO_CONN_S_BLOCKED_CONNECT:
sl@0
   309
			i=BIO_sock_error(b->num);
sl@0
   310
			if (i)
sl@0
   311
				{
sl@0
   312
				BIO_clear_retry_flags(b);
sl@0
   313
				SYSerr(SYS_F_CONNECT,i);
sl@0
   314
				ERR_add_error_data(4,"host=",
sl@0
   315
					c->param_hostname,
sl@0
   316
					":",c->param_port);
sl@0
   317
				BIOerr(BIO_F_CONN_STATE,BIO_R_NBIO_CONNECT_ERROR);
sl@0
   318
				ret=0;
sl@0
   319
				goto exit_loop;
sl@0
   320
				}
sl@0
   321
			else
sl@0
   322
				c->state=BIO_CONN_S_OK;
sl@0
   323
			break;
sl@0
   324
sl@0
   325
		case BIO_CONN_S_OK:
sl@0
   326
			ret=1;
sl@0
   327
			goto exit_loop;
sl@0
   328
		default:
sl@0
   329
			/* abort(); */
sl@0
   330
			goto exit_loop;
sl@0
   331
			}
sl@0
   332
sl@0
   333
		if (cb != NULL)
sl@0
   334
			{
sl@0
   335
			if (!(ret=cb((BIO *)b,c->state,ret)))
sl@0
   336
				goto end;
sl@0
   337
			}
sl@0
   338
		}
sl@0
   339
sl@0
   340
	/* Loop does not exit */
sl@0
   341
exit_loop:
sl@0
   342
	if (cb != NULL)
sl@0
   343
		ret=cb((BIO *)b,c->state,ret);
sl@0
   344
end:
sl@0
   345
	return(ret);
sl@0
   346
	}
sl@0
   347
sl@0
   348
EXPORT_C BIO_CONNECT *BIO_CONNECT_new(void)
sl@0
   349
	{
sl@0
   350
	BIO_CONNECT *ret;
sl@0
   351
sl@0
   352
	if ((ret=(BIO_CONNECT *)OPENSSL_malloc(sizeof(BIO_CONNECT))) == NULL)
sl@0
   353
		return(NULL);
sl@0
   354
	ret->state=BIO_CONN_S_BEFORE;
sl@0
   355
	ret->param_hostname=NULL;
sl@0
   356
	ret->param_port=NULL;
sl@0
   357
	ret->info_callback=NULL;
sl@0
   358
	ret->nbio=0;
sl@0
   359
	ret->ip[0]=0;
sl@0
   360
	ret->ip[1]=0;
sl@0
   361
	ret->ip[2]=0;
sl@0
   362
	ret->ip[3]=0;
sl@0
   363
	ret->port=0;
sl@0
   364
	memset((char *)&ret->them,0,sizeof(ret->them));
sl@0
   365
	return(ret);
sl@0
   366
	}
sl@0
   367
sl@0
   368
EXPORT_C void BIO_CONNECT_free(BIO_CONNECT *a)
sl@0
   369
	{
sl@0
   370
	if(a == NULL)
sl@0
   371
	    return;
sl@0
   372
sl@0
   373
	if (a->param_hostname != NULL)
sl@0
   374
		OPENSSL_free(a->param_hostname);
sl@0
   375
	if (a->param_port != NULL)
sl@0
   376
		OPENSSL_free(a->param_port);
sl@0
   377
	OPENSSL_free(a);
sl@0
   378
	}
sl@0
   379
sl@0
   380
EXPORT_C BIO_METHOD *BIO_s_connect(void)
sl@0
   381
	{
sl@0
   382
	return(&methods_connectp);
sl@0
   383
	}
sl@0
   384
sl@0
   385
static int conn_new(BIO *bi)
sl@0
   386
	{
sl@0
   387
	bi->init=0;
sl@0
   388
	bi->num=INVALID_SOCKET;
sl@0
   389
	bi->flags=0;
sl@0
   390
	if ((bi->ptr=(char *)BIO_CONNECT_new()) == NULL)
sl@0
   391
		return(0);
sl@0
   392
	else
sl@0
   393
		return(1);
sl@0
   394
	}
sl@0
   395
sl@0
   396
static void conn_close_socket(BIO *bio)
sl@0
   397
	{
sl@0
   398
	BIO_CONNECT *c;
sl@0
   399
sl@0
   400
	c=(BIO_CONNECT *)bio->ptr;
sl@0
   401
	if (bio->num != INVALID_SOCKET)
sl@0
   402
		{
sl@0
   403
		/* Only do a shutdown if things were established */
sl@0
   404
		if (c->state == BIO_CONN_S_OK)
sl@0
   405
			shutdown(bio->num,2);
sl@0
   406
		closesocket(bio->num);
sl@0
   407
		bio->num=INVALID_SOCKET;
sl@0
   408
		}
sl@0
   409
	}
sl@0
   410
sl@0
   411
static int conn_free(BIO *a)
sl@0
   412
	{
sl@0
   413
	BIO_CONNECT *data;
sl@0
   414
sl@0
   415
	if (a == NULL) return(0);
sl@0
   416
	data=(BIO_CONNECT *)a->ptr;
sl@0
   417
	 
sl@0
   418
	if (a->shutdown)
sl@0
   419
		{
sl@0
   420
		conn_close_socket(a);
sl@0
   421
		BIO_CONNECT_free(data);
sl@0
   422
		a->ptr=NULL;
sl@0
   423
		a->flags=0;
sl@0
   424
		a->init=0;
sl@0
   425
		}
sl@0
   426
	return(1);
sl@0
   427
	}
sl@0
   428
	
sl@0
   429
static int conn_read(BIO *b, char *out, int outl)
sl@0
   430
	{
sl@0
   431
	int ret=0;
sl@0
   432
	BIO_CONNECT *data;
sl@0
   433
sl@0
   434
	data=(BIO_CONNECT *)b->ptr;
sl@0
   435
	if (data->state != BIO_CONN_S_OK)
sl@0
   436
		{
sl@0
   437
		ret=conn_state(b,data);
sl@0
   438
		if (ret <= 0)
sl@0
   439
				return(ret);
sl@0
   440
		}
sl@0
   441
sl@0
   442
	if (out != NULL)
sl@0
   443
		{
sl@0
   444
		clear_socket_error();
sl@0
   445
		ret=readsocket(b->num,out,outl);
sl@0
   446
		BIO_clear_retry_flags(b);
sl@0
   447
		if (ret <= 0)
sl@0
   448
			{
sl@0
   449
			if (BIO_sock_should_retry(ret))
sl@0
   450
				BIO_set_retry_read(b);
sl@0
   451
			}
sl@0
   452
		}
sl@0
   453
	return(ret);
sl@0
   454
	}
sl@0
   455
sl@0
   456
static int conn_write(BIO *b, const char *in, int inl)
sl@0
   457
	{
sl@0
   458
	int ret;
sl@0
   459
	BIO_CONNECT *data;
sl@0
   460
sl@0
   461
	data=(BIO_CONNECT *)b->ptr;
sl@0
   462
	if (data->state != BIO_CONN_S_OK)
sl@0
   463
		{
sl@0
   464
		ret=conn_state(b,data);
sl@0
   465
		if (ret <= 0) return(ret);
sl@0
   466
		}
sl@0
   467
sl@0
   468
	clear_socket_error();
sl@0
   469
	ret=writesocket(b->num,in,inl);
sl@0
   470
	BIO_clear_retry_flags(b);
sl@0
   471
	if (ret <= 0)
sl@0
   472
		{
sl@0
   473
		if (BIO_sock_should_retry(ret))
sl@0
   474
			BIO_set_retry_write(b);
sl@0
   475
		}
sl@0
   476
	return(ret);
sl@0
   477
	}
sl@0
   478
sl@0
   479
static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
sl@0
   480
	{
sl@0
   481
	BIO *dbio;
sl@0
   482
	int *ip;
sl@0
   483
	const char **pptr;
sl@0
   484
	long ret=1;
sl@0
   485
	BIO_CONNECT *data;
sl@0
   486
sl@0
   487
	data=(BIO_CONNECT *)b->ptr;
sl@0
   488
sl@0
   489
	switch (cmd)
sl@0
   490
		{
sl@0
   491
	case BIO_CTRL_RESET:
sl@0
   492
		ret=0;
sl@0
   493
		data->state=BIO_CONN_S_BEFORE;
sl@0
   494
		conn_close_socket(b);
sl@0
   495
		b->flags=0;
sl@0
   496
		break;
sl@0
   497
	case BIO_C_DO_STATE_MACHINE:
sl@0
   498
		/* use this one to start the connection */
sl@0
   499
		if (data->state != BIO_CONN_S_OK)
sl@0
   500
		ret=(long)conn_state(b,data);
sl@0
   501
		else
sl@0
   502
			ret=1;
sl@0
   503
		break;
sl@0
   504
	case BIO_C_GET_CONNECT:
sl@0
   505
		if (ptr != NULL)
sl@0
   506
			{
sl@0
   507
			pptr=(const char **)ptr;
sl@0
   508
			if (num == 0)
sl@0
   509
				{
sl@0
   510
				*pptr=data->param_hostname;
sl@0
   511
sl@0
   512
				}
sl@0
   513
			else if (num == 1)
sl@0
   514
				{
sl@0
   515
				*pptr=data->param_port;
sl@0
   516
				}
sl@0
   517
			else if (num == 2)
sl@0
   518
				{
sl@0
   519
				*pptr= (char *)&(data->ip[0]);
sl@0
   520
				}
sl@0
   521
			else if (num == 3)
sl@0
   522
				{
sl@0
   523
				*((int *)ptr)=data->port;
sl@0
   524
				}
sl@0
   525
			if ((!b->init) || (ptr == NULL))
sl@0
   526
				*pptr="not initialized";
sl@0
   527
			ret=1;
sl@0
   528
			}
sl@0
   529
		break;
sl@0
   530
	case BIO_C_SET_CONNECT:
sl@0
   531
		if (ptr != NULL)
sl@0
   532
			{
sl@0
   533
			b->init=1;
sl@0
   534
			if (num == 0)
sl@0
   535
				{
sl@0
   536
				if (data->param_hostname != NULL)
sl@0
   537
					OPENSSL_free(data->param_hostname);
sl@0
   538
				data->param_hostname=BUF_strdup(ptr);
sl@0
   539
				}
sl@0
   540
			else if (num == 1)
sl@0
   541
				{
sl@0
   542
				if (data->param_port != NULL)
sl@0
   543
					OPENSSL_free(data->param_port);
sl@0
   544
				data->param_port=BUF_strdup(ptr);
sl@0
   545
				}
sl@0
   546
			else if (num == 2)
sl@0
   547
				{
sl@0
   548
				char buf[16];
sl@0
   549
				unsigned char *p = ptr;
sl@0
   550
sl@0
   551
				BIO_snprintf(buf,sizeof buf,"%d.%d.%d.%d",
sl@0
   552
					     p[0],p[1],p[2],p[3]);
sl@0
   553
				if (data->param_hostname != NULL)
sl@0
   554
					OPENSSL_free(data->param_hostname);
sl@0
   555
				data->param_hostname=BUF_strdup(buf);
sl@0
   556
				memcpy(&(data->ip[0]),ptr,4);
sl@0
   557
				}
sl@0
   558
			else if (num == 3)
sl@0
   559
				{
sl@0
   560
				char buf[DECIMAL_SIZE(int)+1];
sl@0
   561
sl@0
   562
				BIO_snprintf(buf,sizeof buf,"%d",*(int *)ptr);
sl@0
   563
				if (data->param_port != NULL)
sl@0
   564
					OPENSSL_free(data->param_port);
sl@0
   565
				data->param_port=BUF_strdup(buf);
sl@0
   566
				data->port= *(int *)ptr;
sl@0
   567
				}
sl@0
   568
			}
sl@0
   569
		break;
sl@0
   570
	case BIO_C_SET_NBIO:
sl@0
   571
		data->nbio=(int)num;
sl@0
   572
		break;
sl@0
   573
	case BIO_C_GET_FD:
sl@0
   574
		if (b->init)
sl@0
   575
			{
sl@0
   576
			ip=(int *)ptr;
sl@0
   577
			if (ip != NULL)
sl@0
   578
				*ip=b->num;
sl@0
   579
			ret=b->num;
sl@0
   580
			}
sl@0
   581
		else
sl@0
   582
			ret= -1;
sl@0
   583
		break;
sl@0
   584
	case BIO_CTRL_GET_CLOSE:
sl@0
   585
		ret=b->shutdown;
sl@0
   586
		break;
sl@0
   587
	case BIO_CTRL_SET_CLOSE:
sl@0
   588
		b->shutdown=(int)num;
sl@0
   589
		break;
sl@0
   590
	case BIO_CTRL_PENDING:
sl@0
   591
	case BIO_CTRL_WPENDING:
sl@0
   592
		ret=0;
sl@0
   593
		break;
sl@0
   594
	case BIO_CTRL_FLUSH:
sl@0
   595
		break;
sl@0
   596
	case BIO_CTRL_DUP:
sl@0
   597
		{
sl@0
   598
		dbio=(BIO *)ptr;
sl@0
   599
		if (data->param_port)
sl@0
   600
			BIO_set_conn_port(dbio,data->param_port);
sl@0
   601
		if (data->param_hostname)
sl@0
   602
			BIO_set_conn_hostname(dbio,data->param_hostname);
sl@0
   603
		BIO_set_nbio(dbio,data->nbio);
sl@0
   604
		/* FIXME: the cast of the function seems unlikely to be a good idea */
sl@0
   605
                (void)BIO_set_info_callback(dbio,(bio_info_cb *)data->info_callback);
sl@0
   606
		}
sl@0
   607
		break;
sl@0
   608
	case BIO_CTRL_SET_CALLBACK:
sl@0
   609
		{
sl@0
   610
#if 0 /* FIXME: Should this be used?  -- Richard Levitte */
sl@0
   611
		BIOerr(BIO_F_CONN_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
sl@0
   612
		ret = -1;
sl@0
   613
#else
sl@0
   614
		ret=0;
sl@0
   615
#endif
sl@0
   616
		}
sl@0
   617
		break;
sl@0
   618
	case BIO_CTRL_GET_CALLBACK:
sl@0
   619
		{
sl@0
   620
		int (**fptr)(const BIO *bio,int state,int xret);
sl@0
   621
sl@0
   622
		fptr=(int (**)(const BIO *bio,int state,int xret))ptr;
sl@0
   623
		*fptr=data->info_callback;
sl@0
   624
		}
sl@0
   625
		break;
sl@0
   626
	default:
sl@0
   627
		ret=0;
sl@0
   628
		break;
sl@0
   629
		}
sl@0
   630
	return(ret);
sl@0
   631
	}
sl@0
   632
sl@0
   633
static long conn_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
sl@0
   634
	{
sl@0
   635
	long ret=1;
sl@0
   636
	BIO_CONNECT *data;
sl@0
   637
sl@0
   638
	data=(BIO_CONNECT *)b->ptr;
sl@0
   639
sl@0
   640
	switch (cmd)
sl@0
   641
		{
sl@0
   642
	case BIO_CTRL_SET_CALLBACK:
sl@0
   643
		{
sl@0
   644
		data->info_callback=(int (*)(const struct bio_st *, int, int))fp;
sl@0
   645
		}
sl@0
   646
		break;
sl@0
   647
	default:
sl@0
   648
		ret=0;
sl@0
   649
		break;
sl@0
   650
		}
sl@0
   651
	return(ret);
sl@0
   652
	}
sl@0
   653
sl@0
   654
static int conn_puts(BIO *bp, const char *str)
sl@0
   655
	{
sl@0
   656
	int n,ret;
sl@0
   657
sl@0
   658
	n=strlen(str);
sl@0
   659
	ret=conn_write(bp,str,n);
sl@0
   660
	return(ret);
sl@0
   661
	}
sl@0
   662
sl@0
   663
EXPORT_C BIO *BIO_new_connect(char *str)
sl@0
   664
	{
sl@0
   665
	BIO *ret;
sl@0
   666
sl@0
   667
	ret=BIO_new(BIO_s_connect());
sl@0
   668
	if (ret == NULL) return(NULL);
sl@0
   669
	if (BIO_set_conn_hostname(ret,str))
sl@0
   670
		return(ret);
sl@0
   671
	else
sl@0
   672
		{
sl@0
   673
		BIO_free(ret);
sl@0
   674
		return(NULL);
sl@0
   675
		}
sl@0
   676
	}
sl@0
   677
sl@0
   678
#endif
sl@0
   679