b_sock.c revision 9dc0df1bac950d6e491f9a7c7e4888f2b301cb15
/* 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 <errno.h>
#define USE_SOCKETS
#include "cryptlib.h"
#if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
#include "netdb.h"
#endif
#ifndef OPENSSL_NO_SOCK
#ifdef OPENSSL_SYS_WIN16
#define SOCKET_PROTOCOL 0 /* more microsoft stupidity */
#else
#define SOCKET_PROTOCOL IPPROTO_TCP
#endif
#ifdef SO_MAXCONN
#define MAX_LISTEN SO_MAXCONN
#define MAX_LISTEN SOMAXCONN
#else
#define MAX_LISTEN 32
#endif
static int wsa_init_done=0;
#endif
#if 0
static unsigned long BIO_ghbn_hits=0L;
static unsigned long BIO_ghbn_miss=0L;
#define GHBN_NUM 4
static struct ghbn_cache_st
{
char name[129];
unsigned long order;
} ghbn_cache[GHBN_NUM];
#endif
#if 0
#endif
{
int i;
int err = 1;
int locked = 0;
if (i < 0)
{
goto err;
}
/* At this point, we have something that is most probably correct
in some way, so let's init the socket. */
if (BIO_sock_init() != 1)
return 0; /* don't generate another error code here */
/* If the string actually contained an IP address, we need not do
anything more */
if (i > 0) return(1);
/* do a gethostbyname */
locked = 1;
{
goto err;
}
/* cast to short because of win16 winsock definition */
{
goto err;
}
for (i=0; i<4; i++)
err = 0;
err:
if (locked)
if (err)
{
return 0;
}
else
return 1;
}
{
int i;
struct servent *s;
{
return(0);
}
if (i != 0)
*port_ptr=(unsigned short)i;
else
{
/* Note: under VMS with SOCKETSHR, it seems like the first
* parameter is 'char *', instead of 'const char *'
*/
#ifndef CONST_STRICT
(char *)
#endif
str,"tcp");
if(s != NULL)
if(s == NULL)
{
*port_ptr=80;
*port_ptr=23;
*port_ptr=1080;
*port_ptr=443;
*port_ptr=443;
*port_ptr=21;
*port_ptr=70;
#if 0
*port_ptr=21;
#endif
else
{
return(0);
}
}
}
return(1);
}
int BIO_sock_error(int sock)
{
int j,i;
int size;
size=sizeof(int);
/* Note: under Windows the third parameter is of type (char *)
* 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 (char *) or (void *).
*/
if (i < 0)
return(1);
else
return(j);
}
#if 0
{
int i;
char **p;
switch (cmd)
{
case BIO_GHBN_CTRL_HITS:
return(BIO_ghbn_hits);
/* break; */
case BIO_GHBN_CTRL_MISSES:
return(BIO_ghbn_miss);
/* break; */
case BIO_GHBN_CTRL_CACHE_SIZE:
return(GHBN_NUM);
/* break; */
case BIO_GHBN_CTRL_GET_ENTRY:
{
p=(char **)parg;
if (p == NULL) return(0);
return(1);
}
return(0);
/* break; */
case BIO_GHBN_CTRL_FLUSH:
for (i=0; i<GHBN_NUM; i++)
ghbn_cache[i].order=0;
break;
default:
return(0);
}
return(1);
}
#endif
#if 0
{
int i,j;
MemCheck_off();
;
i++;
goto err;
for (i=0; a->h_addr_list[i] != NULL; i++)
;
i++;
goto err;
{
}
for (i=0; a->h_addr_list[i] != NULL; i++)
{
goto err;
}
if (0)
{
err:
}
MemCheck_on();
return(ret);
}
{
int i;
if(a == NULL)
return;
{
OPENSSL_free(a->h_aliases[i]);
OPENSSL_free(a->h_aliases);
}
if (a->h_addr_list != NULL)
{
for (i=0; a->h_addr_list[i] != NULL; i++)
OPENSSL_free(a->h_addr_list[i]);
OPENSSL_free(a->h_addr_list);
}
OPENSSL_free(a);
}
#endif
{
#if 1
/* Caching gethostbyname() results forever is wrong,
* so we have to let the true gethostbyname() worry about this */
return gethostbyname(name);
#else
int i,lowi=0,j;
unsigned long low= (unsigned long)-1;
# if 0
/* It doesn't make sense to use locking here: The function interface
* is not thread-safe, because threads can never be sure when
* some other thread destroys the data they were given a pointer to.
*/
# endif
if (j < 128)
{
for (i=0; i<GHBN_NUM; i++)
{
{
lowi=i;
}
if (ghbn_cache[i].order > 0)
{
break;
}
}
}
else
i=GHBN_NUM;
if (i == GHBN_NUM) /* no hit*/
{
/* Note: under VMS with SOCKETSHR, it seems like the first
* parameter is 'char *', instead of 'const char *'
*/
# ifndef CONST_STRICT
(char *)
# endif
name);
goto end;
if (j > 128) /* too big to cache */
{
# if 0
/* If we were trying to make this function thread-safe (which
* is bound to fail), we'd have to give up in this case
* (or allocate more memory). */
# endif
goto end;
}
/* else add to cache */
{
goto end;
}
}
else
{
}
end:
# if 0
# endif
return(ret);
#endif
}
int BIO_sock_init(void)
{
#ifdef OPENSSL_SYS_WINDOWS
if (!wsa_init_done)
{
int err;
#ifdef SIGINT
#endif
{
return(-1);
}
}
#endif /* OPENSSL_SYS_WINDOWS */
#ifdef WATT32
extern int _watt_do_exit;
_watt_do_exit = 0; /* don't make sock_init() call exit() */
if (sock_init())
return (-1);
#endif
#if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
int err;
if (!wsa_init_done)
{
# ifdef SIGINT
# endif
if (err != 0)
{
return(-1);
}
}
#endif
return(1);
}
void BIO_sock_cleanup(void)
{
#ifdef OPENSSL_SYS_WINDOWS
if (wsa_init_done)
{
#ifndef OPENSSL_SYS_WINCE
#endif
WSACleanup();
}
if (wsa_init_done)
{
WSACleanup();
}
#endif
}
{
int i;
#ifdef __DJGPP__
#else
#endif /* __DJGPP__ */
if (i < 0)
return(i);
}
#endif /* __VMS_VER */
/* The reason I have implemented this instead of using sscanf is because
* Visual C 1.52c gives an unresolved external when linking a DLL :-( */
{
unsigned int tmp[4];
for (;;)
{
c= *(str++);
if ((c >= '0') && (c <= '9'))
{
ok=1;
}
else if (c == '.')
{
if (!ok) return(-1);
if (num == 3) return(0);
num++;
ok=0;
}
break;
else
return(0);
}
return(1);
}
{
int ret=0;
int s=INVALID_SOCKET,cs;
unsigned char ip[4];
unsigned short port;
const char *h,*p;
unsigned long l;
int err_num;
h=p=NULL;
h=str;
for (e=str; *e; e++)
{
if (*e == ':')
{
p= &(e[1]);
*e='\0';
}
else if (*e == '/')
{
*e='\0';
break;
}
}
if (p == NULL)
{
p=h;
h="*";
}
if (strcmp(h,"*") == 0)
else
{
l=(unsigned long)
((unsigned long)ip[0]<<24L)|
((unsigned long)ip[3]);
}
if (s == INVALID_SOCKET)
{
goto err;
}
#ifdef SO_REUSEADDR
if (bind_mode == BIO_BIND_REUSEADDR)
{
int i=1;
}
#endif
{
#ifdef SO_REUSEADDR
if ((bind_mode == BIO_BIND_REUSEADDR_IF_UNUSED) &&
(err_num == EADDRINUSE))
{
if (strcmp(h,"*") == 0)
if (cs != INVALID_SOCKET)
{
int ii;
sizeof(client));
if (ii == INVALID_SOCKET)
{
closesocket(s);
goto again;
}
/* else error */
}
/* else error */
}
#endif
goto err;
}
{
goto err;
}
ret=1;
err:
if ((ret == 0) && (s != INVALID_SOCKET))
{
closesocket(s);
s= INVALID_SOCKET;
}
return(s);
}
{
int ret=INVALID_SOCKET;
static struct sockaddr_in from;
unsigned long l;
unsigned short port;
int len;
char *p;
/* 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)
{
goto end;
}
{
{
goto end;
}
*addr=p;
}
(unsigned char)(l>>24L)&0xff,
(unsigned char)(l>>16L)&0xff,
(unsigned char)(l>> 8L)&0xff,
(unsigned char)(l )&0xff,
port);
end:
return(ret);
}
int BIO_set_tcp_ndelay(int s, int on)
{
int ret=0;
int opt;
#ifdef SOL_TCP
#else
#ifdef IPPROTO_TCP
#endif
#endif
#endif
return(ret == 0);
}
#endif
int BIO_socket_nbio(int s, int mode)
{
int ret= -1;
int l;
l=mode;
#ifdef FIONBIO
#endif
return(ret == 0);
}