s_socket.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* apps/s_socket.c - socket-related functions used by s_client and s_server */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
/* With IPv6, it looks like Digital has mixed up the proper order of
recursive header file inclusion, resulting in the compiler complaining
that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which
is needed to have fileno() declared correctly... So let's define u_int */
#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
#define __U_INT
typedef unsigned int u_int;
#endif
#define USE_SOCKETS
#define NON_MAIN
#include "apps.h"
#include "s_apps.h"
#ifdef OPENSSL_SYS_WINDOWS
static void ssl_sock_cleanup(void);
#endif
static int ssl_sock_init(void);
#ifdef OPENSSL_SYS_WIN16
#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
#else
#define SOCKET_PROTOCOL IPPROTO_TCP
#endif
#ifdef OPENSSL_SYS_WINDOWS
static int wsa_init_done=0;
#ifdef OPENSSL_SYS_WIN16
{
{
switch(message)
{
case WM_DESTROY:
case WM_CLOSE:
break;
}
}
}
{
return(FALSE);
}
#endif /* OPENSSL_SYS_WIN32 */
#endif /* OPENSSL_SYS_WINDOWS */
#ifdef OPENSSL_SYS_WINDOWS
static void ssl_sock_cleanup(void)
{
if (wsa_init_done)
{
#ifndef OPENSSL_SYS_WINCE
#endif
WSACleanup();
}
}
#endif
static int ssl_sock_init(void)
{
#ifdef WATT32
extern int _watt_do_exit;
_watt_do_exit = 0;
dbug_init();
if (sock_init())
return (0);
#elif defined(OPENSSL_SYS_WINDOWS)
if (!wsa_init_done)
{
int err;
#ifdef SIGINT
#endif
{
return(0);
}
#ifdef OPENSSL_SYS_WIN16
#endif /* OPENSSL_SYS_WIN16 */
}
#endif /* OPENSSL_SYS_WINDOWS */
return(1);
}
{
unsigned char ip[4];
short p=0;
{
return(0);
}
if (p != 0) port=p;
}
{
unsigned long addr;
struct sockaddr_in them;
int s,i;
if (!ssl_sock_init()) return(0);
addr=(unsigned long)
((unsigned long)ip[0]<<24L)|
((unsigned long)ip[3]);
#ifndef OPENSSL_SYS_MPE
i=0;
if (i < 0) { perror("keepalive"); return(0); }
#endif
*sock=s;
return(1);
}
{
int sock;
char *name;
int accept_socket;
int i;
{
/* return(1);*/
}
for (;;)
{
{
return(0);
}
if (i < 0)
{
return(i);
}
}
}
{
int ret=0;
struct sockaddr_in server;
int s= -1,i;
if (!ssl_sock_init()) return(0);
else
/* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
#ifndef BIT_FIELD_LIMITS
#else
#endif
if (s == INVALID_SOCKET) goto err;
#if defined SOL_SOCKET && defined SO_REUSEADDR
{
int j = 1;
(void *) &j, sizeof j);
}
#endif
{
#ifndef OPENSSL_SYS_WINDOWS
perror("bind");
#endif
goto err;
}
/* Make it 128 for linux */
i=0;
*sock=s;
ret=1;
err:
if ((ret == 0) && (s != -1))
{
SHUTDOWN(s);
}
return(ret);
}
{
}
{
int ret,i;
static struct sockaddr_in from;
int len;
/* struct linger ling; */
if (!ssl_sock_init()) return(0);
#ifndef OPENSSL_SYS_WINDOWS
#endif
/* Note: under VMS with SOCKETSHR the fourth 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 *).
*/
if (ret == INVALID_SOCKET)
{
#ifdef OPENSSL_SYS_WINDOWS
i=WSAGetLastError();
#else
{
/*check_timeout(); */
goto redoit;
}
perror("accept");
#endif
return(0);
}
/*
ling.l_onoff=1;
ling.l_linger=0;
i=setsockopt(ret,SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling));
if (i < 0) { perror("linger"); return(0); }
i=0;
i=setsockopt(ret,SOL_SOCKET,SO_KEEPALIVE,(char *)&i,sizeof(i));
if (i < 0) { perror("keepalive"); return(0); }
*/
#ifndef BIT_FIELD_LIMITS
/* I should use WSAAsyncGetHostByName() under windows */
#else
#endif
{
/* return(0); */
}
else
{
{
perror("OPENSSL_malloc");
return(0);
}
{
return(0);
}
i=0;
{
return(0);
}
}
end:
return(1);
}
short *port_ptr)
{
char *h,*p;
h=str;
if (p == NULL)
{
return(0);
}
*(p++)='\0';
goto err;
if (!extract_port(p,port_ptr))
goto err;
return(1);
err:
return(0);
}
{
unsigned int in[4];
int i;
{
for (i=0; i<4; i++)
if (in[i] > 255)
{
goto err;
}
}
else
{ /* do a gethostbyname */
if (!ssl_sock_init()) return(0);
{
goto err;
}
/* cast to short because of win16 winsock definition */
{
return(0);
}
}
return(1);
err:
return(0);
}
{
int i;
struct servent *s;
if (i != 0)
*port_ptr=(unsigned short)i;
else
{
if (s == NULL)
{
return(0);
}
}
return(1);
}
#define GHBN_NUM 4
static struct ghbn_cache_st
{
char name[128];
unsigned long order;
} ghbn_cache[GHBN_NUM];
static unsigned long ghbn_hits=0L;
static unsigned long ghbn_miss=0L;
{
int i,lowi=0;
unsigned long low= (unsigned long)-1;
for (i=0; i<GHBN_NUM; i++)
{
{
lowi=i;
}
if (ghbn_cache[i].order > 0)
{
break;
}
}
if (i == GHBN_NUM) /* no hit*/
{
ghbn_miss++;
/* else add to cache */
{
}
return(ret);
}
else
{
ghbn_hits++;
return(ret);
}
}