i=select(width,(void *)&readfds,NULL,NULL,NULL); if (i <= 0) continue; if (FD_ISSET(fileno(stdin),&readfds)) #endif
i=select(width,(void *)&readfds,NULL,NULL,NULL); if (i <= 0) continue; if (FD_ISSET(fileno(stdin),&readfds)) read_from_terminal = 1; #endif if (FD_ISSET(s,&readfds)) read_from_sslcon = 1; } if (read_from_terminal)
static int sv_body(char *hostname, int s, unsigned char *context)	{	char *buf=NULL;	fd_set readfds;	int ret=1,width;	int k,i;	unsigned long l;	SSL *con=NULL;	BIO *sbio;#ifdef WINDOWS	struct timeval tv;#endif	if ((buf=Malloc(bufsize)) == NULL)		{		BIO_printf(bio_err,"out of memory\n");		goto err;		}#ifdef FIONBIO		if (s_nbio)		{		unsigned long sl=1;		if (!s_quiet)			BIO_printf(bio_err,"turning on non blocking io\n");		if (BIO_socket_ioctl(s,FIONBIO,&sl) < 0)			ERR_print_errors(bio_err);		}#endif	if (con == NULL) {		con=SSL_new(ctx);		if(context)		      SSL_set_session_id_context(con, context,						 strlen((char *)context));	}	SSL_clear(con);	sbio=BIO_new_socket(s,BIO_NOCLOSE);	if (s_nbio_test)		{		BIO *test;		test=BIO_new(BIO_f_nbio_test());		sbio=BIO_push(test,sbio);		}	SSL_set_bio(con,sbio,sbio);	SSL_set_accept_state(con);	/* SSL_set_fd(con,s); */	if (s_debug)		{		con->debug=1;		BIO_set_callback(SSL_get_rbio(con),bio_dump_cb);		BIO_set_callback_arg(SSL_get_rbio(con),bio_s_out);		}	width=s+1;	for (;;)		{		FD_ZERO(&readfds);#ifndef WINDOWS		FD_SET(fileno(stdin),&readfds);#endif		FD_SET(s,&readfds);		/* Note: under VMS with SOCKETSHR the second parameter is		 * currently of type (int *) whereas under other systems		 * it is (void *) if you don't have a cast it will choke		 * the compiler: if you do have a cast then you can either		 * go for (int *) or (void *).		 */#ifdef WINDOWS		/* Under Windows we can't select on stdin: only		 * on sockets. As a workaround we timeout the select every		 * second and check for any keypress. In a proper Windows		 * application we wouldn't do this because it is inefficient.		 */		tv.tv_sec = 1;		tv.tv_usec = 0;		i=select(width,(void *)&readfds,NULL,NULL,&tv);		if((i < 0) || (!i && !_kbhit() ) )continue;		if(_kbhit())#else  		i=select(width,(void *)&readfds,NULL,NULL,NULL);  		if (i <= 0) continue;  		if (FD_ISSET(fileno(stdin),&readfds))#endif			{			if (s_crlf)				{				int j, lf_num;				i=read(fileno(stdin), buf, bufsize/2);				lf_num = 0;				/* both loops are skipped when i <= 0 */				for (j = 0; j < i; j++)					if (buf[j] == '\n')						lf_num++;				for (j = i-1; j >= 0; j--)					{					buf[j+lf_num] = buf[j];					if (buf[j] == '\n')						{						lf_num--;						i++;						buf[j+lf_num] = '\r';						}					}				assert(lf_num == 0);				}			else				i=read(fileno(stdin),buf,bufsize);			if (!s_quiet)				{				if ((i <= 0) || (buf[0] == 'Q'))					{					BIO_printf(bio_s_out,"DONE\n");					SHUTDOWN(s);					close_accept_socket();					ret= -11;					goto err;					}				if ((i <= 0) || (buf[0] == 'q'))					{					BIO_printf(bio_s_out,"DONE\n");					SHUTDOWN(s);	/*				close_accept_socket();					ret= -11;*/					goto err;					}				if ((buf[0] == 'r') && 					((buf[1] == '\n') || (buf[1] == '\r')))					{					SSL_renegotiate(con);					i=SSL_do_handshake(con);					printf("SSL_do_handshake -> %d\n",i);					i=0; /*13; */					continue;					/* strcpy(buf,"server side RE-NEGOTIATE\n"); */					}				if ((buf[0] == 'R') &&					((buf[1] == '\n') || (buf[1] == '\r')))					{					SSL_set_verify(con,						SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,NULL);					SSL_renegotiate(con);					i=SSL_do_handshake(con);					printf("SSL_do_handshake -> %d\n",i);					i=0; /* 13; */					continue;					/* strcpy(buf,"server side RE-NEGOTIATE asking for client cert\n"); */					}				if (buf[0] == 'P')					{					static char *str="Lets print some clear text\n";					BIO_write(SSL_get_wbio(con),str,strlen(str));					}				if (buf[0] == 'S')					{					print_stats(bio_s_out,SSL_get_SSL_CTX(con));					}				}#ifdef CHARSET_EBCDIC			ebcdic2ascii(buf,buf,i);#endif			l=k=0;			for (;;)				{				/* should do a select for the write */#ifdef RENEG{ static count=0; if (++count == 100) { count=0; SSL_renegotiate(con); } }#endif				k=SSL_write(con,&(buf[l]),(unsigned int)i);				switch (SSL_get_error(con,k))					{				case SSL_ERROR_NONE:					break;				case SSL_ERROR_WANT_WRITE:				case SSL_ERROR_WANT_READ:				case SSL_ERROR_WANT_X509_LOOKUP:					BIO_printf(bio_s_out,"Write BLOCK\n");					break;				case SSL_ERROR_SYSCALL:				case SSL_ERROR_SSL:					BIO_printf(bio_s_out,"ERROR\n");					ERR_print_errors(bio_err);					ret=1;					goto err;					/* break; */				case SSL_ERROR_ZERO_RETURN:					BIO_printf(bio_s_out,"DONE\n");					ret=1;					goto err;					}				l+=k;				i-=k;				if (i <= 0) break;				}			}		if (FD_ISSET(s,&readfds))			{			if (!SSL_is_init_finished(con))				{				i=init_ssl_connection(con);								if (i < 0)					{					ret=0;					goto err;					}				else if (i == 0)					{					ret=1;					goto err;					}				}			else				{again:					i=SSL_read(con,(char *)buf,bufsize);				switch (SSL_get_error(con,i))					{				case SSL_ERROR_NONE:#ifdef CHARSET_EBCDIC					ascii2ebcdic(buf,buf,i);#endif					write(fileno(stdout),buf,						(unsigned int)i);					if (SSL_pending(con)) goto again;					break;				case SSL_ERROR_WANT_WRITE:				case SSL_ERROR_WANT_READ:				case SSL_ERROR_WANT_X509_LOOKUP:					BIO_printf(bio_s_out,"Read BLOCK\n");					break;				case SSL_ERROR_SYSCALL:				case SSL_ERROR_SSL:					BIO_printf(bio_s_out,"ERROR\n");					ERR_print_errors(bio_err);					ret=1;					goto err;				case SSL_ERROR_ZERO_RETURN:					BIO_printf(bio_s_out,"DONE\n");					ret=1;					goto err;					}				}			}		}err:	BIO_printf(bio_s_out,"shutting down SSL\n");#if 1	SSL_set_shutdown(con,SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);#else	SSL_shutdown(con);#endif	if (con != NULL) SSL_free(con);	BIO_printf(bio_s_out,"CONNECTION CLOSED\n");	if (buf != NULL)		{		memset(buf,0,bufsize);		Free(buf);		}	if (ret >= 0)		BIO_printf(bio_s_out,"ACCEPT\n");	return(ret);	}