2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Name: l_net_wins.c
|
|
|
|
// Function: WinSock
|
|
|
|
// Programmer: MrElusive
|
|
|
|
// Last update: TTimo: cross-platform version, l_net library
|
|
|
|
// Tab Size: 2
|
|
|
|
// Notes:
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
//#include <windows.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "l_net.h"
|
|
|
|
#include "l_net_wins.h"
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#define SOCKET_ERROR -1
|
|
|
|
#define INVALID_SOCKET -1
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
extern void WinPrint( char *str, ... );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
#define WinError WinPrint
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define qtrue 1
|
|
|
|
#define qfalse 0
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
#define ioctlsocket ioctl
|
|
|
|
#define closesocket close
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int WSAGetLastError(){
|
2007-11-04 03:34:51 +00:00
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
typedef struct tag_error_struct
|
|
|
|
{
|
2007-11-04 03:34:51 +00:00
|
|
|
int errnum;
|
|
|
|
LPSTR errstr;
|
2012-03-17 20:01:54 +00:00
|
|
|
} ERROR_STRUCT;
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
typedef struct tag_error_struct
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
int errnum;
|
|
|
|
const char *errstr;
|
2007-11-04 03:34:51 +00:00
|
|
|
} ERROR_STRUCT;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define NET_NAMELEN 64
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
static char my_tcpip_address[NET_NAMELEN];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define DEFAULTnet_hostport 26000
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define MAXHOSTNAMELEN 256
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
static int net_acceptsocket = -1; // socket for fielding new connections
|
2007-11-04 03:34:51 +00:00
|
|
|
static int net_controlsocket;
|
2012-03-17 20:01:54 +00:00
|
|
|
static int net_hostport; // udp port number for acceptsocket
|
2007-11-04 03:34:51 +00:00
|
|
|
static int net_broadcastsocket = 0;
|
|
|
|
//static qboolean ifbcastinit = qfalse;
|
|
|
|
//static struct sockaddr_s broadcastaddr;
|
|
|
|
static struct sockaddr_s broadcastaddr;
|
|
|
|
|
|
|
|
static unsigned long myAddr;
|
|
|
|
|
|
|
|
ERROR_STRUCT errlist[] = {
|
2012-03-17 20:01:54 +00:00
|
|
|
{EACCES,"EACCES - The address is protected, user is not root"},
|
|
|
|
{EAGAIN,"EAGAIN - Operation on non-blocking socket that cannot return immediatly"},
|
|
|
|
{EBADF, "EBADF - sockfd is not a valid descriptor"},
|
|
|
|
{EFAULT, "EFAULT - The parameter is not in a writable part of the user address space"},
|
|
|
|
{EINVAL,"EINVAL - The socket is already bound to an address"},
|
|
|
|
{ENOBUFS,"ENOBUFS - not enough memory"},
|
|
|
|
{ENOMEM, "ENOMEM - not enough memory"},
|
|
|
|
{ENOTCONN, "ENOTCONN - not connected"},
|
|
|
|
{ENOTSOCK,"ENOTSOCK - Argument is file descriptor not a socket"},
|
|
|
|
{EOPNOTSUPP,"ENOTSUPP - The referenced socket is not of type SOCK_STREAM"},
|
|
|
|
{EPERM, "EPERM - Firewall rules forbid connection"},
|
|
|
|
{-1, NULL}
|
2007-11-04 03:34:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
char *WINS_ErrorMessage( int error ){
|
|
|
|
int search = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !error ) {
|
|
|
|
return "No error occurred";
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( search = 0; errlist[search].errstr; search++ )
|
|
|
|
{
|
|
|
|
if ( error == errlist[search].errnum ) {
|
|
|
|
return (char *)errlist[search].errstr;
|
|
|
|
}
|
|
|
|
} //end for
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return "Unknown error";
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function WINS_ErrorMessage
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_Init( void ){
|
|
|
|
int i;
|
2007-11-04 03:34:51 +00:00
|
|
|
struct hostent *local;
|
2012-03-17 20:01:54 +00:00
|
|
|
char buff[MAXHOSTNAMELEN];
|
2007-11-04 03:34:51 +00:00
|
|
|
struct sockaddr_s addr;
|
2012-03-17 20:01:54 +00:00
|
|
|
char *p;
|
2008-06-26 11:21:13 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
linux doesn't have anything to initialize for the net
|
|
|
|
"Windows .. built for the internet .. the internet .. built with unix"
|
2007-11-04 03:34:51 +00:00
|
|
|
*/
|
|
|
|
#if 0
|
2012-03-17 20:01:54 +00:00
|
|
|
int r;
|
|
|
|
WORD wVersionRequested;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
wVersionRequested = MAKEWORD( 2, 2 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
r = WSAStartup( wVersionRequested, &winsockdata );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( r ) {
|
|
|
|
WinPrint( "Winsock initialization failed.\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
i = COM_CheckParm ("-udpport");
|
|
|
|
if (i == 0)*/
|
|
|
|
net_hostport = DEFAULTnet_hostport;
|
2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
else if (i < com_argc-1)
|
|
|
|
net_hostport = Q_atoi (com_argv[i+1]);
|
|
|
|
else
|
|
|
|
Sys_Error ("WINS_Init: you must specify a number after -udpport");
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// determine my name & address
|
2012-03-17 20:01:54 +00:00
|
|
|
gethostname( buff, MAXHOSTNAMELEN );
|
|
|
|
local = gethostbyname( buff );
|
2008-04-14 20:15:34 +00:00
|
|
|
|
|
|
|
// When hostname can not be resolved, return gracefully
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( local == 0 ) {
|
|
|
|
WinError( "WINS_Init: Unable to resolve hostname\n" );
|
2008-04-14 20:15:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
myAddr = *(int *)local->h_addr_list[0];
|
|
|
|
|
|
|
|
// if the quake hostname isn't set, set it to the machine name
|
|
|
|
// if (Q_strcmp(hostname.string, "UNNAMED") == 0)
|
|
|
|
{
|
|
|
|
// see if it's a text IP address (well, close enough)
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( p = buff; *p; p++ )
|
|
|
|
if ( ( *p < '0' || *p > '9' ) && *p != '.' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// if it is a real name, strip off the domain; we only want the host
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( *p ) {
|
|
|
|
for ( i = 0; i < 15; i++ )
|
|
|
|
if ( buff[i] == '.' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
buff[i] = 0;
|
|
|
|
}
|
|
|
|
// Cvar_Set ("hostname", buff);
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
//++timo WTF is that net_controlsocket? it's sole purpose is to retrieve the local IP?
|
|
|
|
if ( ( net_controlsocket = WINS_OpenSocket( 0 ) ) == SOCKET_ERROR ) {
|
|
|
|
WinError( "WINS_Init: Unable to open control socket\n" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
( (struct sockaddr_in *)&broadcastaddr )->sin_family = AF_INET;
|
|
|
|
( (struct sockaddr_in *)&broadcastaddr )->sin_addr.s_addr = INADDR_BROADCAST;
|
|
|
|
( (struct sockaddr_in *)&broadcastaddr )->sin_port = htons( (u_short)net_hostport );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
WINS_GetSocketAddr( net_controlsocket, &addr );
|
|
|
|
strcpy( my_tcpip_address, WINS_AddrToString( &addr ) );
|
|
|
|
p = strrchr( my_tcpip_address, ':' );
|
|
|
|
if ( p ) {
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
WinPrint( "Winsock Initialized\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return net_controlsocket;
|
|
|
|
} //end of the function WINS_Init
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
char *WINS_MyAddress( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
return my_tcpip_address;
|
|
|
|
} //end of the function WINS_MyAddress
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
void WINS_Shutdown( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
//WINS_Listen(0);
|
2012-03-17 20:01:54 +00:00
|
|
|
WINS_CloseSocket( net_controlsocket );
|
2007-11-04 03:34:51 +00:00
|
|
|
// WSACleanup();
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
WinPrint( "Winsock Shutdown\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function WINS_Shutdown
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
void WINS_Listen(int state)
|
|
|
|
{
|
|
|
|
// enable listening
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
if (net_acceptsocket != -1)
|
|
|
|
return;
|
|
|
|
if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
|
|
|
|
WinError ("WINS_Listen: Unable to open accept socket\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable listening
|
|
|
|
if (net_acceptsocket == -1)
|
|
|
|
return;
|
|
|
|
WINS_CloseSocket (net_acceptsocket);
|
|
|
|
net_acceptsocket = -1;
|
|
|
|
} //end of the function WINS_Listen*/
|
2007-11-04 03:34:51 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_OpenSocket( int port ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int newsocket;
|
|
|
|
struct sockaddr_in address;
|
|
|
|
u_long _true = 1;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( newsocket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == SOCKET_ERROR ) {
|
|
|
|
WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ioctlsocket( newsocket, FIONBIO, &_true ) == SOCKET_ERROR ) {
|
|
|
|
WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
|
|
|
closesocket( newsocket );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( (char *) &address, 0, sizeof( address ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
address.sin_family = AF_INET;
|
|
|
|
address.sin_addr.s_addr = INADDR_ANY;
|
2012-03-17 20:01:54 +00:00
|
|
|
address.sin_port = htons( (u_short)port );
|
|
|
|
if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
|
|
|
|
WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
|
|
|
closesocket( newsocket );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
|
|
|
|
|
|
|
return newsocket;
|
|
|
|
} //end of the function WINS_OpenSocket
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_OpenReliableSocket( int port ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int newsocket;
|
|
|
|
struct sockaddr_in address;
|
|
|
|
qboolean _true = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
//IPPROTO_TCP
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( newsocket = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
|
|
|
|
WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( (char *) &address, 0, sizeof( address ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
address.sin_family = AF_INET;
|
2012-03-17 20:01:54 +00:00
|
|
|
address.sin_addr.s_addr = htonl( INADDR_ANY );
|
|
|
|
address.sin_port = htons( (u_short)port );
|
|
|
|
if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
|
|
|
|
WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
|
|
|
closesocket( newsocket );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
//
|
|
|
|
if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == -1 ) {
|
|
|
|
WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
|
|
|
WinPrint( "setsockopt error\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end if
|
|
|
|
|
|
|
|
return newsocket;
|
|
|
|
} //end of the function WINS_OpenReliableSocket
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_Listen( int socket ){
|
2007-11-04 03:34:51 +00:00
|
|
|
u_long _true = 1;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ioctlsocket( socket, FIONBIO, &_true ) == -1 ) {
|
|
|
|
WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( listen( socket, SOMAXCONN ) == SOCKET_ERROR ) {
|
|
|
|
WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
|
|
|
return 0;
|
|
|
|
} //end of the function WINS_Listen
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_Accept( int socket, struct sockaddr_s *addr ){
|
2013-05-03 06:57:29 +00:00
|
|
|
socklen_t addrlen = sizeof( struct sockaddr_s );
|
2007-11-04 03:34:51 +00:00
|
|
|
int newsocket;
|
|
|
|
qboolean _true = 1;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
newsocket = accept( socket, (struct sockaddr *)addr, &addrlen );
|
|
|
|
if ( newsocket == INVALID_SOCKET ) {
|
|
|
|
if ( errno == EAGAIN ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
//
|
|
|
|
if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == SOCKET_ERROR ) {
|
|
|
|
WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
|
|
|
WinPrint( "setsockopt error\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end if
|
|
|
|
return newsocket;
|
|
|
|
} //end of the function WINS_Accept
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_CloseSocket( int socket ){
|
2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
if (socket == net_broadcastsocket)
|
|
|
|
net_broadcastsocket = 0;
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
// shutdown(socket, SD_SEND);
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( closesocket( socket ) == SOCKET_ERROR ) {
|
|
|
|
WinPrint( "WINS_CloseSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return SOCKET_ERROR;
|
|
|
|
} //end if
|
|
|
|
return 0;
|
|
|
|
} //end of the function WINS_CloseSocket
|
|
|
|
//===========================================================================
|
|
|
|
// this lets you type only as much of the net address as required, using
|
|
|
|
// the local network components to fill in the rest
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
static int PartialIPAddress( char *in, struct sockaddr_s *hostaddr ){
|
2007-11-04 03:34:51 +00:00
|
|
|
char buff[256];
|
|
|
|
char *b;
|
|
|
|
int addr;
|
|
|
|
int num;
|
|
|
|
int mask;
|
2008-06-26 11:21:13 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
buff[0] = '.';
|
|
|
|
b = buff;
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( buff + 1, in );
|
|
|
|
if ( buff[1] == '.' ) {
|
|
|
|
b++;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
addr = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
mask = -1;
|
|
|
|
while ( *b == '.' )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
num = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( *++b < '0' || *b > '9' ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while ( !( *b < '0' || *b > '9' ) )
|
|
|
|
num = num * 10 + *( b++ ) - '0';
|
|
|
|
mask <<= 8;
|
|
|
|
addr = ( addr << 8 ) + num;
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2008-06-26 11:21:13 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
hostaddr->sa_family = AF_INET;
|
2012-03-17 20:01:54 +00:00
|
|
|
( (struct sockaddr_in *)hostaddr )->sin_port = htons( (u_short)net_hostport );
|
|
|
|
( (struct sockaddr_in *)hostaddr )->sin_addr.s_addr = ( myAddr & htonl( mask ) ) | htonl( addr );
|
2008-06-26 11:21:13 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
|
|
|
} //end of the function PartialIPAddress
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_Connect( int socket, struct sockaddr_s *addr ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int ret;
|
|
|
|
u_long _true2 = 0xFFFFFFFF;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
ret = connect( socket, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
|
|
|
|
if ( ret == SOCKET_ERROR ) {
|
|
|
|
WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ioctlsocket( socket, FIONBIO, &_true2 ) == -1 ) {
|
|
|
|
WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end if
|
|
|
|
return 0;
|
|
|
|
} //end of the function WINS_Connect
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_CheckNewConnections( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
char buf[4];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( net_acceptsocket == -1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( recvfrom( net_acceptsocket, buf, 4, MSG_PEEK, NULL, NULL ) > 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return net_acceptsocket;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
|
|
|
} //end of the function WINS_CheckNewConnections
|
|
|
|
//===========================================================================
|
|
|
|
// returns the number of bytes read
|
|
|
|
// 0 if no bytes available
|
|
|
|
// -1 on failure
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_Read( int socket, byte *buf, int len, struct sockaddr_s *addr ){
|
2013-05-03 06:57:29 +00:00
|
|
|
socklen_t addrlen = sizeof( struct sockaddr_s );
|
2007-11-04 03:34:51 +00:00
|
|
|
int ret;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( addr ) {
|
|
|
|
ret = recvfrom( socket, buf, len, 0, (struct sockaddr *)addr, &addrlen );
|
|
|
|
if ( ret == -1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
// errno = WSAGetLastError();
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( errno == EAGAIN || errno == ENOTCONN ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end if
|
|
|
|
} //end if
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
ret = recv( socket, buf, len, 0 );
|
|
|
|
// if there's no data on the socket ret == -1 and errno == EAGAIN
|
|
|
|
// MSDN states that if ret == 0 the socket has been closed
|
|
|
|
// man recv doesn't say anything
|
|
|
|
if ( ret == 0 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ( ret == SOCKET_ERROR ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
// errno = WSAGetLastError();
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( errno == EAGAIN || errno == ENOTCONN ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end if
|
|
|
|
} //end else
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ret == SOCKET_ERROR ) {
|
|
|
|
WinPrint( "WINS_Read: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end if
|
|
|
|
return ret;
|
|
|
|
} //end of the function WINS_Read
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_MakeSocketBroadcastCapable( int socket ){
|
|
|
|
int i = 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// make this socket broadcast capable
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( setsockopt( socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof( i ) ) < 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
net_broadcastsocket = socket;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} //end of the function WINS_MakeSocketBroadcastCapable
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_Broadcast( int socket, byte *buf, int len ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int ret;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( socket != net_broadcastsocket ) {
|
|
|
|
if ( net_broadcastsocket != 0 ) {
|
|
|
|
WinError( "Attempted to use multiple broadcasts sockets\n" );
|
|
|
|
}
|
|
|
|
ret = WINS_MakeSocketBroadcastCapable( socket );
|
|
|
|
if ( ret == -1 ) {
|
|
|
|
WinPrint( "Unable to make socket broadcast capable\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return WINS_Write( socket, buf, len, &broadcastaddr );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function WINS_Broadcast
|
|
|
|
//===========================================================================
|
|
|
|
// returns qtrue on success or qfalse on failure
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_Write( int socket, byte *buf, int len, struct sockaddr_s *addr ){
|
2008-06-26 11:21:13 +00:00
|
|
|
int ret = 0, written;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( addr ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
written = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( written < len )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
ret = sendto( socket, &buf[written], len - written, 0, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
|
|
|
|
if ( ret == SOCKET_ERROR ) {
|
|
|
|
if ( WSAGetLastError() != EAGAIN ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return qfalse;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
//++timo FIXME: what is this used for?
|
2007-11-04 03:34:51 +00:00
|
|
|
// Sleep(1000);
|
|
|
|
} //end if
|
|
|
|
else
|
|
|
|
{
|
|
|
|
written += ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} //end if
|
|
|
|
else
|
|
|
|
{
|
|
|
|
written = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( written < len )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
ret = send( socket, buf, len, 0 );
|
|
|
|
if ( ret == SOCKET_ERROR ) {
|
|
|
|
if ( WSAGetLastError() != EAGAIN ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return qfalse;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
//++timo FIXME: what is this used for?
|
2007-11-04 03:34:51 +00:00
|
|
|
// Sleep(1000);
|
|
|
|
} //end if
|
|
|
|
else
|
|
|
|
{
|
|
|
|
written += ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} //end else
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ret == SOCKET_ERROR ) {
|
|
|
|
WinPrint( "WINS_Write: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end if
|
2012-03-17 20:01:54 +00:00
|
|
|
return ( ret == len );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function WINS_Write
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
char *WINS_AddrToString( struct sockaddr_s *addr ){
|
2007-11-04 03:34:51 +00:00
|
|
|
static char buffer[22];
|
|
|
|
int haddr;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
haddr = ntohl( ( (struct sockaddr_in *)addr )->sin_addr.s_addr );
|
|
|
|
sprintf( buffer, "%d.%d.%d.%d:%d", ( haddr >> 24 ) & 0xff, ( haddr >> 16 ) & 0xff, ( haddr >> 8 ) & 0xff, haddr & 0xff, ntohs( ( (struct sockaddr_in *)addr )->sin_port ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return buffer;
|
|
|
|
} //end of the function WINS_AddrToString
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_StringToAddr( char *string, struct sockaddr_s *addr ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int ha1, ha2, ha3, ha4, hp;
|
|
|
|
int ipaddr;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
sscanf( string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp );
|
|
|
|
ipaddr = ( ha1 << 24 ) | ( ha2 << 16 ) | ( ha3 << 8 ) | ha4;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
addr->sa_family = AF_INET;
|
2012-03-17 20:01:54 +00:00
|
|
|
( (struct sockaddr_in *)addr )->sin_addr.s_addr = htonl( ipaddr );
|
|
|
|
( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)hp );
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
|
|
|
} //end of the function WINS_StringToAddr
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_GetSocketAddr( int socket, struct sockaddr_s *addr ){
|
2013-05-03 06:57:29 +00:00
|
|
|
socklen_t addrlen = sizeof( struct sockaddr_s );
|
2007-11-04 03:34:51 +00:00
|
|
|
unsigned int a;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( addr, 0, sizeof( struct sockaddr_s ) );
|
|
|
|
getsockname( socket, (struct sockaddr *)addr, &addrlen );
|
|
|
|
a = ( (struct sockaddr_in *)addr )->sin_addr.s_addr;
|
|
|
|
if ( a == 0 || a == inet_addr( "127.0.0.1" ) ) {
|
|
|
|
( (struct sockaddr_in *)addr )->sin_addr.s_addr = myAddr;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
} //end of the function WINS_GetSocketAddr
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_GetNameFromAddr( struct sockaddr_s *addr, char *name ){
|
2007-11-04 03:34:51 +00:00
|
|
|
struct hostent *hostentry;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
hostentry = gethostbyaddr( (char *)&( (struct sockaddr_in *)addr )->sin_addr, sizeof( struct in_addr ), AF_INET );
|
|
|
|
if ( hostentry ) {
|
|
|
|
strncpy( name, (char *)hostentry->h_name, NET_NAMELEN - 1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( name, WINS_AddrToString( addr ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
|
|
|
} //end of the function WINS_GetNameFromAddr
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_GetAddrFromName( char *name, struct sockaddr_s *addr ){
|
2007-11-04 03:34:51 +00:00
|
|
|
struct hostent *hostentry;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( name[0] >= '0' && name[0] <= '9' ) {
|
|
|
|
return PartialIPAddress( name, addr );
|
|
|
|
}
|
2008-06-26 11:21:13 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
hostentry = gethostbyname( name );
|
|
|
|
if ( !hostentry ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
addr->sa_family = AF_INET;
|
2012-03-17 20:01:54 +00:00
|
|
|
( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)net_hostport );
|
|
|
|
( (struct sockaddr_in *)addr )->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
} //end of the function WINS_GetAddrFromName
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_AddrCompare( struct sockaddr_s *addr1, struct sockaddr_s *addr2 ){
|
|
|
|
if ( addr1->sa_family != addr2->sa_family ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( (struct sockaddr_in *)addr1 )->sin_addr.s_addr != ( (struct sockaddr_in *)addr2 )->sin_addr.s_addr ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( (struct sockaddr_in *)addr1 )->sin_port != ( (struct sockaddr_in *)addr2 )->sin_port ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
} //end of the function WINS_AddrCompare
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_GetSocketPort( struct sockaddr_s *addr ){
|
|
|
|
return ntohs( ( (struct sockaddr_in *)addr )->sin_port );
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end of the function WINS_GetSocketPort
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Parameter: -
|
|
|
|
// Returns: -
|
|
|
|
// Changes Globals: -
|
|
|
|
//===========================================================================
|
2012-03-17 20:01:54 +00:00
|
|
|
int WINS_SetSocketPort( struct sockaddr_s *addr, int port ){
|
|
|
|
( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)port );
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
|
|
|
} //end of the function WINS_SetSocketPort
|