tcpip.c revision e0e0c19eefceaf5d4ec40f9466b58a771f50e799
/* Copyright (c) 2001, Stanford University
* All rights reserved
*
* See the file LICENSE.txt for information on redistributing this software.
*/
#ifdef WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
# ifndef VBOX
typedef int ssize_t;
# endif
#else
#ifdef OSF1
typedef int socklen_t;
#endif
#include <netdb.h>
#include <unistd.h>
#endif
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#ifdef AIX
#include <strings.h>
#endif
#ifdef LINUX
#include <unistd.h>
#endif
#include "cr_error.h"
#include "cr_mem.h"
#include "cr_string.h"
#include "cr_bufpool.h"
#include "cr_net.h"
#include "cr_endian.h"
#include "cr_threads.h"
#include "cr_environment.h"
#include "net_internals.h"
#ifdef ADDRINFO
#endif
#ifdef WINDOWS
#define EADDRINUSE WSAEADDRINUSE
#define ECONNREFUSED WSAECONNREFUSED
#endif
#ifdef WINDOWS
#define ECONNRESET WSAECONNRESET
int crTCPIPErrno( void )
{
return WSAGetLastError( );
}
char *crTCPIPErrorString( int err )
{
switch ( err )
{
case WSAECONNREFUSED: X( "connection refused" );
case WSAECONNRESET: X( "connection reset" );
default:
if ( temp )
{
}
}
#undef X
{
*temp = '\0';
temp--;
}
return buf;
}
#else /* WINDOWS */
int crTCPIPErrno( void )
{
errno = 0;
return err;
}
char *crTCPIPErrorString( int err )
{
if ( temp )
{
}
else
{
}
return buf;
}
#endif /* WINDOWS */
/*
* Socket callbacks. When a socket is created or destroyed we will
* call these callback functions.
* XXX Maybe have lists of callbacks?
*/
void
{
if (mode == CR_SOCKET_CREATE) {
}
else if (mode == CR_SOCKET_DESTROY) {
}
else {
}
}
{
int fail;
if (sock <= 0)
return;
if (SocketDestroyCallback) {
}
#ifdef WINDOWS
#else
#endif
if ( fail )
{
int err = crTCPIPErrno( );
crWarning( "crCloseSocket( sock=%d ): %s",
}
}
/**
* Read len bytes from socket, and store in buffer.
* \return 1 if success, -1 if error, 0 if sender exited.
*/
int
{
/*
* Shouldn't write to a non-existent socket, ie when
* crTCPIPDoDisconnect has removed it from the pool
*/
if ( sock <= 0 )
return 1;
while ( len > 0 )
{
#ifdef WINDOWS_XXXX
/* MWE: why is this necessary for windows??? Does it return a
"good" value for num_bytes despite having a reset
connection? */
if ( crTCPIPErrno( ) == ECONNRESET )
return -1;
#endif
if ( num_read < 0 )
{
int error = crTCPIPErrno();
switch( error )
{
case EINTR:
crWarning( "__tcpip_read_exact(TCPIP): "
"caught an EINTR, looping for more data" );
continue;
case EFAULT:
crWarning( "EFAULT" );
break;
case EINVAL:
crWarning( "EINVAL" );
break;
default:
break;
}
return -1;
}
if ( num_read == 0 )
{
/* client exited gracefully */
return 0;
}
}
return 1;
}
void
{
{
}
}
/**
* Write the given buffer of len bytes on the socket.
* \return 1 if OK, negative value if error.
*/
int
{
/*
* Shouldn't write to a non-existent socket, ie when
* crTCPIPDoDisconnect has removed it from the pool
*/
if ( sock <= 0 )
return 1;
while ( len > 0 )
{
if ( num_written <= 0 )
{
int err;
{
crWarning("__tcpip_write_exact(TCPIP): caught an EINTR, continuing");
continue;
}
return -err;
}
len -= num_written;
src += num_written;
}
return 1;
}
void
{
{
}
}
/**
* Make sockets do what we want:
*
* 2) Turn off Nagle's algorithm
*/
static void
{
/* why do we do 1) ? things work much better for me to push the
* the buffer size way up -- karl
*/
#ifdef LINUX
#else
#endif
int so_reuseaddr = 1;
int tcp_nodelay = 1;
{
int err = crTCPIPErrno( );
crWarning( "setsockopt( SO_SNDBUF=%d ) : %s",
}
{
int err = crTCPIPErrno( );
crWarning( "setsockopt( SO_RCVBUF=%d ) : %s",
}
(char *) &so_reuseaddr, sizeof(so_reuseaddr) ) )
{
int err = crTCPIPErrno( );
crWarning( "setsockopt( SO_REUSEADDR=%d ) : %s",
}
(char *) &tcp_nodelay, sizeof(tcp_nodelay) ) )
{
int err = crTCPIPErrno( );
crWarning( "setsockopt( TCP_NODELAY=%d )"
}
}
typedef int socklen_t;
#endif
/**
* Create a listening socket using the given port.
* Caller can then pass the socket to accept().
* previously create socket.
*/
static int
{
/* XXX should use an unbounded list here instead of parallel arrays... */
#define MAX_PORTS 100
static int count = 0;
int i, sock = -1;
/* search to see if we've seen this port before */
for (i = 0; i < count; i++) {
return sockets[i];
}
}
/* new port so create new socket */
{
int err;
#ifndef ADDRINFO
struct sockaddr_in servaddr;
#endif
/* with the new OOB stuff, we can have multiple ports being
* accepted on, so we need to redo the server socket every time.
*/
#ifndef ADDRINFO
if ( sock == -1 )
{
err = crTCPIPErrno( );
}
spankSocket( sock );
{
err = crTCPIPErrno( );
crError( "Couldn't bind to socket (port=%d): %s",
}
{
err = crTCPIPErrno( );
}
#else
char port_s[NI_MAXSERV];
if ( err )
crError( "Couldn't find local TCP port %s: %s",
{
if ( sock == -1 )
{
err = crTCPIPErrno( );
if (err != EAFNOSUPPORT)
crWarning("Couldn't create socket of family %i: %s, trying another",
continue;
}
spankSocket( sock );
{
err = crTCPIPErrno( );
crWarning( "Couldn't bind to socket (port=%d): %s",
crCloseSocket( sock );
continue;
}
{
err = crTCPIPErrno( );
crCloseSocket( sock );
continue;
}
break;
}
if (!cur)
#endif
}
}
count++;
return sock;
}
void
{
int err;
#ifndef ADDRINFO
#else
struct sockaddr_storage addr;
char host[NI_MAXHOST];
#endif
/* If brokered, we'll contact the mothership to broker the network
* connection. We'll send the mothership our hostname, the port and
* our endianness and will get in return a connection ID number.
*/
crError("There shouldn't be any brokered connections in VirtualBox");
}
addr_length = sizeof( addr );
{
err = crTCPIPErrno( );
}
if (SocketCreateCallback) {
}
#ifndef ADDRINFO
{
}
#else
NULL, 0, NI_NAMEREQD);
if ( err )
{
NULL, 0, NI_NUMERICHOST);
if ( err ) /* shouldn't ever happen */
else
}
#endif
else
{
char *temp;
#ifndef ADDRINFO
#else
#endif
temp++;
*temp = '\0';
}
#ifdef RECV_BAIL_OUT
err = sizeof(unsigned int);
{
conn->krecv_buf_size = 0;
}
#endif
}
void *
{
#ifdef CHROMIUM_THREADSAFE
#endif
{
crDebug("Buffer pool %p was empty; allocated new %d byte buffer.",
buf = (CRTCPIPBuffer *)
}
#ifdef CHROMIUM_THREADSAFE
#endif
return (void *)( buf + 1 );
}
static void
{
}
static void
{
return;
if (!bufp) {
/* We're sending a user-allocated buffer.
* Simply write the length & the payload and return.
*/
return;
}
else {
/* The region [start .. start + len + 1] lies within a buffer that
* was allocated with crTCPIPAlloc() and can be put into the free
* buffer pool when we're done sending it.
*/
unsigned int *lenp;
/* All of the buffers passed to the send function were allocated
* with crTCPIPAlloc(), which includes a header with a 4 byte
* pad field, to insure that we always have a place to write
* the length field, even when start == *bufp.
*/
/* Reclaim this pointer for reuse */
#ifdef CHROMIUM_THREADSAFE
#endif
#ifdef CHROMIUM_THREADSAFE
#endif
/* Since the buffer's now in the 'free' buffer pool, the caller can't
* use it any more. Setting bufp to NULL will make sure the caller
* doesn't try to re-use the buffer.
*/
}
}
void
{
crDebug( "Dead connection (sock=%d, host=%s), removing from pool",
/* remove from connection pool */
}
int
{
for ( ; ; )
{
{
/* We re-init everytime for Linux, as it corrupts
* the timeout structure, but other OS's
* don't have a problem with it.
*/
}
else
if ( num_ready >= 0 )
{
return num_ready;
}
err = crTCPIPErrno( );
{
crWarning( "select interruped by an unblocked signal, trying again" );
}
else
{
}
}
}
void
{
switch ( tcpip_buffer->kind )
{
case CRTCPIPMemory:
#ifdef CHROMIUM_THREADSAFE
#endif
/* pool may have been deallocated just a bit earlier in response
* to a SIGPIPE (Broken Pipe) signal.
*/
}
#ifdef CHROMIUM_THREADSAFE
#endif
break;
case CRTCPIPMemoryBig:
crFree( tcpip_buffer );
break;
default:
}
}
/**
* Check if message type is GATHER. If so, process it specially.
* \return number of bytes which were consumed
*/
static int
{
/* grab the offset and the length */
unsigned int buf[2];
{
}
/* read the rest into the userbuf */
{
crDebug("userbuf for Gather Message is too small!");
return len;
}
{
}
}
else {
return 0;
}
}
/**
* Receive the next message on the given connection.
* If we're being called by crTCPIPRecv(), we already know there's
* something to receive.
*/
static void
{
/* Our gigE board is acting odd. If we recv() an amount
* less than what is already in the RECVBUF, performance
* goes into the toilet (somewhere around a factor of 3).
* This is an ugly hack, but seems to get around whatever
* funk is being produced
*
* Remember to set your kernel recv buffers to be bigger
* than the framebuffer 'chunk' you are sending (see
* sysctl -a | grep rmem) , or this will really have no
* effect. --karl
*/
#ifdef RECV_BAIL_OUT
{
int inbuf;
return;
}
#endif
/* this reads the length of the message */
{
return;
}
{
/* put in pre-allocated buffer */
}
else
{
/* allocate new buffer */
tcpip_buffer->pad = 0;
}
/* if we have set a userbuf, and there is room in it, we probably
* want to stick the message into that, instead of our allocated
* buffer.
*/
leftover = 0;
{
total = sizeof(CRMessageHeader);
}
{
crFree( tcpip_buffer );
return;
}
{
}
/* if there is still data pending, it should go into the user buffer */
if (leftover)
{
/* if there is anything left, plop it into the recv_buffer */
{
{
crFree( tcpip_buffer );
return;
}
}
}
#if 0
#endif
/* CR_MESSAGE_OPCODES is freed in crserverlib/server_stream.c with crNetFree.
* OOB messages are the programmer's problem. -- Humper 12/17/01
*/
if (cached_type != CR_MESSAGE_OPCODES
&& cached_type != CR_MESSAGE_OOB
&& cached_type != CR_MESSAGE_GATHER)
{
}
}
/**
* that are ready.
*/
int
crTCPIPRecv( void )
{
/* ensure we don't get caught with a new thread connecting */
int none_left = 1;
#endif
#ifdef CHROMIUM_THREADSAFE
#endif
/*
* that are ready to be read.
*/
max_fd = 0;
for ( i = 0; i < num_conns; i++ )
{
continue;
none_left = 0;
#endif
{
/*
* NOTE: may want to always put the FD in the descriptor
* set so we'll notice broken connections. Down in the
* loop that iterates over the ready sockets only peek
* (MSG_PEEK flag to recv()?) if the connection isn't
* enabled.
*/
#if 0 /* not used - see below */
#ifndef ADDRINFO
struct sockaddr s;
#else
struct sockaddr_storage s;
#endif
#endif
/* KLUDGE CITY......
*
* With threads there's a race condition between
* TCPIPRecv and TCPIPSingleRecv when new
* clients are connecting, thus new mothership
* connections are also being established.
* This code below is to check that we're not
* in a state of accepting the socket without
* connecting to it otherwise we fail with
* ENOTCONN later. But, this is really a side
* effect of this routine catching a motherships
* socket connection and reading data that wasn't
* really meant for us. It was really meant for
* TCPIPSingleRecv. So, if we detect an
* in-progress connection we set the msock id
* so that we can assume the motherships socket
* and skip over them.
*/
#if 0 /* Disabled on Dec 13 2005 by BrianP - seems to cause trouble */
slen = sizeof( s );
/* Check that the socket is REALLY connected */
/* Doesn't this call introduce some inefficiency??? (BP) */
/* Another kludge.....
* If we disconnect a socket without writing
* anything to it, we end up here. Detect
* the disconnected socket by checking if
* we've ever sent something and then
* disconnect it.
*
* I think the networking layer needs
* a bit of a re-write.... Alan.
*/
if (conn->total_bytes_sent > 0) {
}
}
#endif
/*
* Nope, that last socket we've just caught in
* the connecting phase. We've probably found
* a mothership connection here, and we shouldn't
* process it
*/
}
}
if (none_left) {
/*
* Caught no more connections.
* Review this if we want to try
* restarting crserver's dynamically.
*/
#ifdef CHROMIUM_THREADSAFE
#endif
crError("No more connections to process, terminating...\n");
exit(0); /* shouldn't get here */
}
#endif
if (!max_fd) {
#ifdef CHROMIUM_THREADSAFE
#endif
return 0;
}
if ( num_conns ) {
}
else {
crWarning( "Waiting for first connection..." );
}
if ( num_ready == 0 ) {
#ifdef CHROMIUM_THREADSAFE
#endif
return 0;
}
/*
* we determined are ready above.
*/
for ( i = 0; i < num_conns; i++ )
{
continue;
continue;
continue;
continue;
}
#ifdef CHROMIUM_THREADSAFE
#endif
return 1;
}
static void
{
/* build a header so we can delete the message later */
}
static void
{
}
void
unsigned int mtu )
{
(void) mtu;
if ( cr_tcpip.initialized )
{
return;
}
#ifdef CHROMIUM_THREADSAFE
#endif
}
/**
* The function that actually connects. This should only be called by clients
* Servers have another way to set up the socket.
*/
int
{
int err;
#ifndef ADDRINFO
struct sockaddr_in servaddr;
int i;
if ( conn->tcp_socket < 0 )
{
int err = crTCPIPErrno( );
return 0;
}
if (SocketCreateCallback) {
}
/* Set up the socket the way *we* want. */
/* Standard Berkeley sockets mumbo jumbo */
if ( !hp )
{
return 0;
}
#else
char port_s[NI_MAXSERV];
if ( err )
{
return 0;
}
#endif
/* If brokered, we'll contact the mothership to broker the network
* connection. We'll send the mothership our hostname, the port and
* our endianness and will get in return a connection ID number.
*/
{
crError("There shouldn't be any brokered connections in VirtualBox");
}
#ifndef ADDRINFO
for (i=1;i;)
#else
#endif
{
#ifndef ADDRINFO
#ifdef RECV_BAIL_OUT
err = sizeof(unsigned int);
{
conn->krecv_buf_size = 0;
}
#endif
sizeof(servaddr) ) )
return 1;
#else
if ( conn->tcp_socket < 0 )
{
int err = crTCPIPErrno( );
if (err != EAFNOSUPPORT)
continue;
}
if (SocketCreateCallback) {
}
err = 1;
/* Set up the socket the way *we* want. */
#if RECV_BAIL_OUT
err = sizeof(unsigned int);
{
conn->krecv_buf_size = 0;
}
#endif
return 1;
}
#endif
err = crTCPIPErrno( );
crWarning( "Connection refused to %s:%d, %s",
{
crWarning( "connection to %s:%d "
continue;
}
else
crWarning( "Couldn't connect to %s:%d, %s",
#ifndef ADDRINFO
i=0;
#else
#endif
}
#ifdef ADDRINFO
#endif
return 0;
}
/**
* Disconnect this connection, but don't free(conn).
*/
void
{
int none_left = 1;
int i;
/* If this connection has already been disconnected (e.g.
* if the connection has been lost and disabled through
* a call to __tcpip_dead_connection(), which will then
* call this routine), don't disconnect it again; if we
* do, and if a new valid connection appears in the same
* slot (conn->index), we'll effectively disable the
* valid connection by mistake, leaving us unable to
* receive inbound data on that connection.
*/
}
conn->tcp_socket = 0;
/* see if any connections remain */
for (i = 0; i < num_conns; i++)
{
none_left = 0; /* found a live connection */
}
#if 0 /* disabled on 13 Dec 2005 by BrianP - this prevents future client
* connections after the last one goes away.
*/
{
crDebug("Closing master socket (probably quitting).");
#ifdef CHROMIUM_THREADSAFE
#endif
last_port = 0;
cr_tcpip.initialized = 0;
}
#endif
}
/**
* InitConnection() function (and from the UDP module).
*/
void
{
int i, found = 0;
int n_bytes;
conn->krecv_buf_size = 0;
/* Find a free slot */
found = 1;
break;
}
}
/* Realloc connection stack if we couldn't find a free slot */
if (found == 0) {
}
}
{
const char *override;
int ret;
if (override)
{
ret = 0;
}
else
return ret;
}
{
}