ioef/code/qcommon/net_ip.c
Thilo Schulz 70cd2ccfab - include Winsock2.h for windows
- add a __BSD__ define for all bsd platforms if it doesn't exist.
2008-04-05 14:28:57 +00:00

1427 lines
33 KiB
C

/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code 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.
Quake III Arena source code 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.
You should have received a copy of the GNU General Public License
along with Quake III Arena source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "../qcommon/q_shared.h"
#include "../qcommon/qcommon.h"
#ifdef _WIN32
#include <Winsock2.h>
#include <Ws2tcpip.h>
#include <Wspiapi.h>
typedef int socklen_t;
#define sa_family_t ADDRESS_FAMILY
#define EAGAIN WSAEWOULDBLOCK
#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#define ECONNRESET WSAECONNRESET
#define socketError WSAGetLastError( )
static WSADATA winsockdata;
static qboolean winsockInitialized = qfalse;
#else
#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
// needed for socklen_t on OSX 10.2
# define _BSD_SOCKLEN_T_
#endif
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#ifndef __sun
#include <ifaddrs.h>
#endif
#ifdef __sun
#include <sys/filio.h>
#endif
typedef int SOCKET;
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket close
#define ioctlsocket ioctl
#define socketError errno
#endif
static qboolean usingSocks = qfalse;
static int networkingEnabled = 0;
#define NET_ENABLEV4 0x01
#define NET_ENABLEV6 0x02
// if this flag is set, always attempt ipv6 connections instead of ipv4 if a v6 address is found.
#define NET_PRIOV6 0x04
static cvar_t *net_enabled;
static cvar_t *net_socksEnabled;
static cvar_t *net_socksServer;
static cvar_t *net_socksPort;
static cvar_t *net_socksUsername;
static cvar_t *net_socksPassword;
static cvar_t *net_ip;
static cvar_t *net_ip6;
static cvar_t *net_port;
static cvar_t *net_port6;
static struct sockaddr socksRelayAddr;
static SOCKET ip_socket = INVALID_SOCKET;
static SOCKET ip6_socket = INVALID_SOCKET;
static SOCKET socks_socket = INVALID_SOCKET;
#define MAX_IPS 16
static int numIP;
static int numIP6;
static byte localIP[MAX_IPS][4];
static byte localIPmask[MAX_IPS][4];
static byte localIP6[MAX_IPS][16];
static byte localIP6mask[MAX_IPS][16];
//=============================================================================
/*
====================
NET_ErrorString
====================
*/
char *NET_ErrorString( void ) {
#ifdef _WIN32
//FIXME: replace with FormatMessage?
switch( socketError ) {
case WSAEINTR: return "WSAEINTR";
case WSAEBADF: return "WSAEBADF";
case WSAEACCES: return "WSAEACCES";
case WSAEDISCON: return "WSAEDISCON";
case WSAEFAULT: return "WSAEFAULT";
case WSAEINVAL: return "WSAEINVAL";
case WSAEMFILE: return "WSAEMFILE";
case WSAEWOULDBLOCK: return "WSAEWOULDBLOCK";
case WSAEINPROGRESS: return "WSAEINPROGRESS";
case WSAEALREADY: return "WSAEALREADY";
case WSAENOTSOCK: return "WSAENOTSOCK";
case WSAEDESTADDRREQ: return "WSAEDESTADDRREQ";
case WSAEMSGSIZE: return "WSAEMSGSIZE";
case WSAEPROTOTYPE: return "WSAEPROTOTYPE";
case WSAENOPROTOOPT: return "WSAENOPROTOOPT";
case WSAEPROTONOSUPPORT: return "WSAEPROTONOSUPPORT";
case WSAESOCKTNOSUPPORT: return "WSAESOCKTNOSUPPORT";
case WSAEOPNOTSUPP: return "WSAEOPNOTSUPP";
case WSAEPFNOSUPPORT: return "WSAEPFNOSUPPORT";
case WSAEAFNOSUPPORT: return "WSAEAFNOSUPPORT";
case WSAEADDRINUSE: return "WSAEADDRINUSE";
case WSAEADDRNOTAVAIL: return "WSAEADDRNOTAVAIL";
case WSAENETDOWN: return "WSAENETDOWN";
case WSAENETUNREACH: return "WSAENETUNREACH";
case WSAENETRESET: return "WSAENETRESET";
case WSAECONNABORTED: return "WSWSAECONNABORTEDAEINTR";
case WSAECONNRESET: return "WSAECONNRESET";
case WSAENOBUFS: return "WSAENOBUFS";
case WSAEISCONN: return "WSAEISCONN";
case WSAENOTCONN: return "WSAENOTCONN";
case WSAESHUTDOWN: return "WSAESHUTDOWN";
case WSAETOOMANYREFS: return "WSAETOOMANYREFS";
case WSAETIMEDOUT: return "WSAETIMEDOUT";
case WSAECONNREFUSED: return "WSAECONNREFUSED";
case WSAELOOP: return "WSAELOOP";
case WSAENAMETOOLONG: return "WSAENAMETOOLONG";
case WSAEHOSTDOWN: return "WSAEHOSTDOWN";
case WSASYSNOTREADY: return "WSASYSNOTREADY";
case WSAVERNOTSUPPORTED: return "WSAVERNOTSUPPORTED";
case WSANOTINITIALISED: return "WSANOTINITIALISED";
case WSAHOST_NOT_FOUND: return "WSAHOST_NOT_FOUND";
case WSATRY_AGAIN: return "WSATRY_AGAIN";
case WSANO_RECOVERY: return "WSANO_RECOVERY";
case WSANO_DATA: return "WSANO_DATA";
default: return "NO ERROR";
}
#else
return strerror (errno);
#endif
}
static void NetadrToSockadr( netadr_t *a, struct sockaddr *s ) {
if( a->type == NA_BROADCAST ) {
((struct sockaddr_in *)s)->sin_family = AF_INET;
((struct sockaddr_in *)s)->sin_port = a->port;
((struct sockaddr_in *)s)->sin_addr.s_addr = INADDR_BROADCAST;
}
else if( a->type == NA_IP ) {
((struct sockaddr_in *)s)->sin_family = AF_INET;
((struct sockaddr_in *)s)->sin_addr.s_addr = *(int *)&a->ip;
((struct sockaddr_in *)s)->sin_port = a->port;
}
else if( a->type == NA_IP6 ) {
((struct sockaddr_in6 *)s)->sin6_family = AF_INET6;
((struct sockaddr_in6 *)s)->sin6_addr = * ((struct in6_addr *) &a->ip6);
((struct sockaddr_in6 *)s)->sin6_port = a->port;
}
}
static void SockadrToNetadr( struct sockaddr *s, netadr_t *a ) {
if (s->sa_family == AF_INET) {
a->type = NA_IP;
*(int *)&a->ip = ((struct sockaddr_in *)s)->sin_addr.s_addr;
a->port = ((struct sockaddr_in *)s)->sin_port;
}
else if(s->sa_family == AF_INET6)
{
a->type = NA_IP6;
memcpy(a->ip6, &((struct sockaddr_in6 *)s)->sin6_addr, sizeof(a->ip6));
a->port = ((struct sockaddr_in6 *)s)->sin6_port;
}
}
static struct addrinfo *SearchAddrInfo(struct addrinfo *hints, sa_family_t family)
{
while(hints)
{
if(hints->ai_family == family)
return hints;
hints = hints->ai_next;
}
return NULL;
}
/*
=============
Sys_StringToSockaddr
=============
*/
static qboolean Sys_StringToSockaddr(const char *s, struct sockaddr *sadr, int sadr_len, sa_family_t family)
{
struct addrinfo hints, *res = NULL, *search;
int retval;
memset(sadr, '\0', sizeof(*sadr));
memset(&hints, '\0', sizeof(hints));
hints.ai_family = family;
retval = getaddrinfo(s, NULL, &hints, &res);
if(!retval)
{
if(family == AF_UNSPEC)
{
// Decide here and now which protocol family to use
if((net_enabled->integer & NET_ENABLEV6) && (net_enabled->integer & NET_PRIOV6))
search = SearchAddrInfo(res, AF_INET6);
else
search = SearchAddrInfo(res, AF_INET);
if(!search)
{
if((net_enabled->integer & NET_ENABLEV6) &&
(net_enabled->integer & NET_PRIOV6) &&
(net_enabled->integer & NET_ENABLEV4))
search = SearchAddrInfo(res, AF_INET);
else if(net_enabled->integer & NET_ENABLEV6)
search = SearchAddrInfo(res, AF_INET6);
}
}
else
search = SearchAddrInfo(res, family);
if(search)
{
if(res->ai_addrlen > sadr_len)
res->ai_addrlen = sadr_len;
memcpy(sadr, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
return qtrue;
}
else
Com_Printf("Sys_StringToSockaddr: Error resolving %s: No address of required type found.\n", s);
}
else
Com_Printf("Sys_StringToSockaddr: Error resolving %s: %s\n", s, gai_strerror(retval));
if(res)
freeaddrinfo(res);
return qfalse;
}
/*
=============
Sys_SockaddrToString
=============
*/
static void Sys_SockaddrToString(char *dest, int destlen, struct sockaddr *input, int inputlen)
{
getnameinfo(input, inputlen, dest, destlen, NULL, 0, NI_NUMERICHOST);
}
/*
=============
Sys_StringToAdr
=============
*/
qboolean Sys_StringToAdr( const char *s, netadr_t *a, netadrtype_t family ) {
struct sockaddr_storage sadr;
sa_family_t fam;
switch(family)
{
case NA_IP:
fam = AF_INET;
break;
case NA_IP6:
fam = AF_INET6;
break;
default:
fam = AF_UNSPEC;
break;
}
if( !Sys_StringToSockaddr(s, (struct sockaddr *) &sadr, sizeof(sadr), fam ) ) {
return qfalse;
}
SockadrToNetadr( (struct sockaddr *) &sadr, a );
return qtrue;
}
/*
===================
NET_CompareBaseAdr
Compares without the port
===================
*/
qboolean NET_CompareBaseAdr (netadr_t a, netadr_t b)
{
if (a.type != b.type)
return qfalse;
if (a.type == NA_LOOPBACK)
return qtrue;
if (a.type == NA_IP)
{
if(!memcmp(a.ip, b.ip, sizeof(a.ip)))
return qtrue;
return qfalse;
}
if (a.type == NA_IP6)
{
if(!memcmp(a.ip6, b.ip6, sizeof(a.ip6)))
return qtrue;
return qfalse;
}
Com_Printf ("NET_CompareBaseAdr: bad address type\n");
return qfalse;
}
const char *NET_AdrToString (netadr_t a)
{
static char s[NET_ADDRSTRMAXLEN];
if (a.type == NA_LOOPBACK) {
Com_sprintf (s, sizeof(s), "loopback");
} else if (a.type == NA_BOT) {
Com_sprintf (s, sizeof(s), "bot");
}
else if (a.type == NA_IP || a.type == NA_IP6)
{
struct sockaddr_storage sadr;
memset(&sadr, 0, sizeof(sadr));
NetadrToSockadr(&a, (struct sockaddr *) &sadr);
Sys_SockaddrToString(s, sizeof(s), (struct sockaddr *) &sadr, sizeof(sadr));
}
return s;
}
const char *NET_AdrToStringwPort (netadr_t a)
{
static char s[NET_ADDRSTRMAXLEN];
if (a.type == NA_LOOPBACK) {
Com_sprintf (s, sizeof(s), "loopback");
} else if (a.type == NA_BOT) {
Com_sprintf (s, sizeof(s), "bot");
}
else if (a.type == NA_IP || a.type == NA_IP6)
{
if(a.type == NA_IP)
Com_sprintf(s, sizeof(s), "%s:%hu", NET_AdrToString(a), ntohs(a.port));
else if(a.type == NA_IP6)
Com_sprintf(s, sizeof(s), "[%s]:%hu", NET_AdrToString(a), ntohs(a.port));
}
return s;
}
qboolean NET_CompareAdr (netadr_t a, netadr_t b)
{
if(!NET_CompareBaseAdr(a, b))
return qfalse;
if (a.type == NA_IP || a.type == NA_IP6)
{
if (a.port == b.port)
return qtrue;
}
else
return qtrue;
return qfalse;
}
qboolean NET_IsLocalAddress( netadr_t adr ) {
return adr.type == NA_LOOPBACK;
}
//=============================================================================
/*
==================
Sys_GetPacket
Never called by the game logic, just the system event queing
==================
*/
#ifdef _DEBUG
int recvfromCount;
#endif
qboolean Sys_GetPacket( netadr_t *net_from, msg_t *net_message ) {
int ret;
struct sockaddr_storage from;
socklen_t fromlen;
int err;
#ifdef _DEBUG
recvfromCount++; // performance check
#endif
if(ip_socket != INVALID_SOCKET)
{
fromlen = sizeof(from);
ret = recvfrom( ip_socket, net_message->data, net_message->maxsize, 0, (struct sockaddr *)&from, &fromlen );
if (ret == SOCKET_ERROR)
{
err = socketError;
if( err != EAGAIN && err != ECONNRESET )
Com_Printf( "NET_GetPacket: %s\n", NET_ErrorString() );
}
else
{
memset( ((struct sockaddr_in *)&from)->sin_zero, 0, 8 );
if ( usingSocks && memcmp( &from, &socksRelayAddr, fromlen ) == 0 ) {
if ( ret < 10 || net_message->data[0] != 0 || net_message->data[1] != 0 || net_message->data[2] != 0 || net_message->data[3] != 1 ) {
return qfalse;
}
net_from->type = NA_IP;
net_from->ip[0] = net_message->data[4];
net_from->ip[1] = net_message->data[5];
net_from->ip[2] = net_message->data[6];
net_from->ip[3] = net_message->data[7];
net_from->port = *(short *)&net_message->data[8];
net_message->readcount = 10;
}
else {
SockadrToNetadr( (struct sockaddr *) &from, net_from );
net_message->readcount = 0;
}
if( ret == net_message->maxsize ) {
Com_Printf( "Oversize packet from %s\n", NET_AdrToString (*net_from) );
return qfalse;
}
net_message->cursize = ret;
return qtrue;
}
}
if(ip6_socket != INVALID_SOCKET)
{
fromlen = sizeof(from);
ret = recvfrom(ip6_socket, net_message->data, net_message->maxsize, 0, (struct sockaddr *)&from, &fromlen);
if (ret == SOCKET_ERROR)
{
err = socketError;
if( err != EAGAIN && err != ECONNRESET )
Com_Printf( "NET_GetPacket: %s\n", NET_ErrorString() );
}
else
{
SockadrToNetadr((struct sockaddr *) &from, net_from);
net_message->readcount = 0;
if(ret == net_message->maxsize)
{
Com_Printf( "Oversize packet from %s\n", NET_AdrToString (*net_from) );
return qfalse;
}
net_message->cursize = ret;
return qtrue;
}
}
return qfalse;
}
//=============================================================================
static char socksBuf[4096];
/*
==================
Sys_SendPacket
==================
*/
void Sys_SendPacket( int length, const void *data, netadr_t to ) {
int ret;
struct sockaddr_storage addr;
if( to.type != NA_BROADCAST && to.type != NA_IP && to.type != NA_IP6 ) {
Com_Error( ERR_FATAL, "Sys_SendPacket: bad address type" );
return;
}
if((ip_socket == INVALID_SOCKET && to.type == NA_IP) || (ip6_socket == INVALID_SOCKET && to.type == NA_IP6))
return;
memset(&addr, 0, sizeof(addr));
NetadrToSockadr( &to, (struct sockaddr *) &addr );
if( usingSocks && to.type == NA_IP ) {
socksBuf[0] = 0; // reserved
socksBuf[1] = 0;
socksBuf[2] = 0; // fragment (not fragmented)
socksBuf[3] = 1; // address type: IPV4
*(int *)&socksBuf[4] = ((struct sockaddr_in *)&addr)->sin_addr.s_addr;
*(short *)&socksBuf[8] = ((struct sockaddr_in *)&addr)->sin_port;
memcpy( &socksBuf[10], data, length );
ret = sendto( ip_socket, socksBuf, length+10, 0, &socksRelayAddr, sizeof(socksRelayAddr) );
}
else {
if(addr.ss_family == AF_INET)
ret = sendto( ip_socket, data, length, 0, (struct sockaddr *) &addr, sizeof(addr) );
else
ret = sendto( ip6_socket, data, length, 0, (struct sockaddr *) &addr, sizeof(addr) );
}
if( ret == SOCKET_ERROR ) {
int err = socketError;
// wouldblock is silent
if( err == EAGAIN ) {
return;
}
// some PPP links do not allow broadcasts and return an error
if( ( err == EADDRNOTAVAIL ) && ( ( to.type == NA_BROADCAST ) ) ) {
return;
}
Com_Printf( "NET_SendPacket: %s\n", NET_ErrorString() );
}
}
//=============================================================================
/*
==================
Sys_IsLANAddress
LAN clients will have their rate var ignored
==================
*/
qboolean Sys_IsLANAddress( netadr_t adr ) {
int index, run;
qboolean differed;
if( adr.type == NA_LOOPBACK ) {
return qtrue;
}
if( adr.type == NA_IP )
{
// RFC1918:
// 10.0.0.0 - 10.255.255.255 (10/8 prefix)
// 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
// 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
if(adr.ip[0] == 10)
return qtrue;
if(adr.ip[0] == 172 && (adr.ip[1]&0xf0) == 16)
return qtrue;
if(adr.ip[0] == 192 && adr.ip[1] == 168)
return qtrue;
if(adr.ip[0] == 127)
return qtrue;
for(index = 0; index < numIP; index++)
{
differed = qfalse;
for(run = 0; run < sizeof(*localIP); run++)
{
if((localIP[index][run] & localIPmask[index][run]) != (adr.ip[run] & localIPmask[index][run]))
{
differed = qtrue;
break;
}
}
if(!differed)
return qtrue;
}
}
else if(adr.type == NA_IP6)
{
if(adr.ip6[0] == 0xfe && (adr.ip6[1] & 0xc0) == 0x80)
return qtrue;
if((adr.ip6[0] & 0xfe) == 0xfc)
return qtrue;
for(index = 0; index < numIP6; index++)
{
differed = qfalse;
for(run = 0; run < sizeof(*localIP6); run++)
{
if((localIP6[index][run] & localIP6mask[index][run]) != (adr.ip6[run] & localIP6mask[index][run]))
{
differed = qtrue;
break;
}
}
if(!differed)
return qtrue;
}
}
return qfalse;
}
/*
==================
Sys_ShowIP
==================
*/
void Sys_ShowIP(void) {
int i;
char buf[NET_ADDRSTRMAXLEN];
struct sockaddr_in sa;
struct sockaddr_in6 sa6;
for (i = 0; i < numIP; i++)
{
memset(&sa, 0, sizeof(sa));
memcpy( &sa.sin_addr.s_addr, localIP[i], sizeof(sa.sin_addr.s_addr) );
sa.sin_family = AF_INET;
Sys_SockaddrToString(buf, sizeof(buf), (struct sockaddr *) &sa, sizeof(sa));
Com_Printf( "IP: %s\n", buf);
}
for (i = 0; i < numIP6; i++)
{
memset(&sa6, 0, sizeof(sa6));
memcpy( &sa6.sin6_addr, localIP6[i], sizeof(sa6.sin6_addr) );
sa6.sin6_family = AF_INET6;
Sys_SockaddrToString(buf, sizeof(buf), (struct sockaddr *) &sa6, sizeof(sa6));
Com_Printf( "IP6: %s\n", buf);
}
}
//=============================================================================
/*
====================
NET_IPSocket
====================
*/
int NET_IPSocket( char *net_interface, int port, int *err ) {
SOCKET newsocket;
struct sockaddr_in address;
qboolean _true = qtrue;
int i = 1;
*err = 0;
if( net_interface ) {
Com_Printf( "Opening IP socket: %s:%i\n", net_interface, port );
}
else {
Com_Printf( "Opening IP socket: localhost:%i\n", port );
}
if( ( newsocket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == INVALID_SOCKET ) {
*err = socketError;
Com_Printf( "WARNING: NET_IPSocket: socket: %s\n", NET_ErrorString() );
return newsocket;
}
// make it non-blocking
if( ioctlsocket( newsocket, FIONBIO, (u_long *)&_true ) == SOCKET_ERROR ) {
Com_Printf( "WARNING: NET_IPSocket: ioctl FIONBIO: %s\n", NET_ErrorString() );
*err = socketError;
closesocket(newsocket);
return INVALID_SOCKET;
}
// make it broadcast capable
if( setsockopt( newsocket, SOL_SOCKET, SO_BROADCAST, (char *) &i, sizeof(i) ) == SOCKET_ERROR ) {
Com_Printf( "WARNING: NET_IPSocket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString() );
// it is not that bad if this one fails.
// return newsocket;
}
if( !net_interface || !net_interface[0] || !Q_stricmp(net_interface, "localhost") ) {
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
}
else
{
if(!Sys_StringToSockaddr( net_interface, (struct sockaddr *)&address, sizeof(address), AF_INET))
{
closesocket(newsocket);
return INVALID_SOCKET;
}
}
if( port == PORT_ANY ) {
address.sin_port = 0;
}
else {
address.sin_port = htons( (short)port );
}
if( bind( newsocket, (void *)&address, sizeof(address) ) == SOCKET_ERROR ) {
Com_Printf( "WARNING: NET_IPSocket: bind: %s\n", NET_ErrorString() );
*err = socketError;
closesocket( newsocket );
return INVALID_SOCKET;
}
return newsocket;
}
/*
====================
NET_IP6Socket
====================
*/
int NET_IP6Socket( char *net_interface, int port, int *err ) {
SOCKET newsocket;
struct sockaddr_in6 address;
qboolean _true = qtrue;
int i = 1;
*err = 0;
if( net_interface ) {
Com_Printf( "Opening IP6 socket: [%s]:%i\n", net_interface, port );
}
else {
Com_Printf( "Opening IP6 socket: localhost:%i\n", port );
}
if( ( newsocket = socket( PF_INET6, SOCK_DGRAM, IPPROTO_UDP ) ) == INVALID_SOCKET ) {
*err = socketError;
Com_Printf( "WARNING: NET_IP6Socket: socket: %s\n", NET_ErrorString() );
return newsocket;
}
// make it non-blocking
if( ioctlsocket( newsocket, FIONBIO, (u_long *)&_true ) == SOCKET_ERROR ) {
Com_Printf( "WARNING: NET_IP6Socket: ioctl FIONBIO: %s\n", NET_ErrorString() );
*err = socketError;
closesocket(newsocket);
return INVALID_SOCKET;
}
// ipv4 addresses should not be allowed to connect via this socket.
if(setsockopt(newsocket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &i, sizeof(i)) == SOCKET_ERROR)
{
// win32 systems don't seem to support this anyways.
Com_DPrintf("WARNING: NET_IP6Socket: setsockopt IPV6_V6ONLY: %s\n", NET_ErrorString());
}
// make it broadcast capable
if( setsockopt( newsocket, SOL_SOCKET, SO_BROADCAST, (char *) &i, sizeof(i) ) == SOCKET_ERROR ) {
Com_Printf( "WARNING: NET_IP6Socket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString() );
// return newsocket;
}
if( !net_interface || !net_interface[0] || !Q_stricmp(net_interface, "localhost") ) {
address.sin6_family = AF_INET6;
address.sin6_addr = in6addr_any;
}
else
{
if(!Sys_StringToSockaddr( net_interface, (struct sockaddr *)&address, sizeof(address), AF_INET6))
{
closesocket(newsocket);
return INVALID_SOCKET;
}
}
if( port == PORT_ANY ) {
address.sin6_port = 0;
}
else {
address.sin6_port = htons( (short)port );
}
if( bind( newsocket, (void *)&address, sizeof(address) ) == SOCKET_ERROR ) {
Com_Printf( "WARNING: NET_IP6Socket: bind: %s\n", NET_ErrorString() );
*err = socketError;
closesocket( newsocket );
return INVALID_SOCKET;
}
return newsocket;
}
/*
====================
NET_OpenSocks
====================
*/
void NET_OpenSocks( int port ) {
struct sockaddr_in address;
int err;
struct hostent *h;
int len;
qboolean rfc1929;
unsigned char buf[64];
usingSocks = qfalse;
Com_Printf( "Opening connection to SOCKS server.\n" );
if ( ( socks_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) == INVALID_SOCKET ) {
err = socketError;
Com_Printf( "WARNING: NET_OpenSocks: socket: %s\n", NET_ErrorString() );
return;
}
h = gethostbyname( net_socksServer->string );
if ( h == NULL ) {
err = socketError;
Com_Printf( "WARNING: NET_OpenSocks: gethostbyname: %s\n", NET_ErrorString() );
return;
}
if ( h->h_addrtype != AF_INET ) {
Com_Printf( "WARNING: NET_OpenSocks: gethostbyname: address type was not AF_INET\n" );
return;
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = *(int *)h->h_addr_list[0];
address.sin_port = htons( (short)net_socksPort->integer );
if ( connect( socks_socket, (struct sockaddr *)&address, sizeof( address ) ) == SOCKET_ERROR ) {
err = socketError;
Com_Printf( "NET_OpenSocks: connect: %s\n", NET_ErrorString() );
return;
}
// send socks authentication handshake
if ( *net_socksUsername->string || *net_socksPassword->string ) {
rfc1929 = qtrue;
}
else {
rfc1929 = qfalse;
}
buf[0] = 5; // SOCKS version
// method count
if ( rfc1929 ) {
buf[1] = 2;
len = 4;
}
else {
buf[1] = 1;
len = 3;
}
buf[2] = 0; // method #1 - method id #00: no authentication
if ( rfc1929 ) {
buf[2] = 2; // method #2 - method id #02: username/password
}
if ( send( socks_socket, buf, len, 0 ) == SOCKET_ERROR ) {
err = socketError;
Com_Printf( "NET_OpenSocks: send: %s\n", NET_ErrorString() );
return;
}
// get the response
len = recv( socks_socket, buf, 64, 0 );
if ( len == SOCKET_ERROR ) {
err = socketError;
Com_Printf( "NET_OpenSocks: recv: %s\n", NET_ErrorString() );
return;
}
if ( len != 2 || buf[0] != 5 ) {
Com_Printf( "NET_OpenSocks: bad response\n" );
return;
}
switch( buf[1] ) {
case 0: // no authentication
break;
case 2: // username/password authentication
break;
default:
Com_Printf( "NET_OpenSocks: request denied\n" );
return;
}
// do username/password authentication if needed
if ( buf[1] == 2 ) {
int ulen;
int plen;
// build the request
ulen = strlen( net_socksUsername->string );
plen = strlen( net_socksPassword->string );
buf[0] = 1; // username/password authentication version
buf[1] = ulen;
if ( ulen ) {
memcpy( &buf[2], net_socksUsername->string, ulen );
}
buf[2 + ulen] = plen;
if ( plen ) {
memcpy( &buf[3 + ulen], net_socksPassword->string, plen );
}
// send it
if ( send( socks_socket, buf, 3 + ulen + plen, 0 ) == SOCKET_ERROR ) {
err = socketError;
Com_Printf( "NET_OpenSocks: send: %s\n", NET_ErrorString() );
return;
}
// get the response
len = recv( socks_socket, buf, 64, 0 );
if ( len == SOCKET_ERROR ) {
err = socketError;
Com_Printf( "NET_OpenSocks: recv: %s\n", NET_ErrorString() );
return;
}
if ( len != 2 || buf[0] != 1 ) {
Com_Printf( "NET_OpenSocks: bad response\n" );
return;
}
if ( buf[1] != 0 ) {
Com_Printf( "NET_OpenSocks: authentication failed\n" );
return;
}
}
// send the UDP associate request
buf[0] = 5; // SOCKS version
buf[1] = 3; // command: UDP associate
buf[2] = 0; // reserved
buf[3] = 1; // address type: IPV4
*(int *)&buf[4] = INADDR_ANY;
*(short *)&buf[8] = htons( (short)port ); // port
if ( send( socks_socket, buf, 10, 0 ) == SOCKET_ERROR ) {
err = socketError;
Com_Printf( "NET_OpenSocks: send: %s\n", NET_ErrorString() );
return;
}
// get the response
len = recv( socks_socket, buf, 64, 0 );
if( len == SOCKET_ERROR ) {
err = socketError;
Com_Printf( "NET_OpenSocks: recv: %s\n", NET_ErrorString() );
return;
}
if( len < 2 || buf[0] != 5 ) {
Com_Printf( "NET_OpenSocks: bad response\n" );
return;
}
// check completion code
if( buf[1] != 0 ) {
Com_Printf( "NET_OpenSocks: request denied: %i\n", buf[1] );
return;
}
if( buf[3] != 1 ) {
Com_Printf( "NET_OpenSocks: relay address is not IPV4: %i\n", buf[3] );
return;
}
((struct sockaddr_in *)&socksRelayAddr)->sin_family = AF_INET;
((struct sockaddr_in *)&socksRelayAddr)->sin_addr.s_addr = *(int *)&buf[4];
((struct sockaddr_in *)&socksRelayAddr)->sin_port = *(short *)&buf[8];
memset( ((struct sockaddr_in *)&socksRelayAddr)->sin_zero, 0, 8 );
usingSocks = qtrue;
}
/*
=====================
NET_GetLocalAddress
=====================
*/
int NET_AddLocalAddress(struct sockaddr *sa)
{
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
int index;
if(sa->sa_family == AF_INET)
{
if(numIP > MAX_IPS)
return -1;
sin = (struct sockaddr_in *) sa;
for(index = 0; index < numIP; index++)
{
if( !memcmp(&localIP[index], &sin->sin_addr.s_addr, sizeof(*localIP)) )
break;
}
if(index >= numIP)
memcpy(localIP[numIP++], &sin->sin_addr.s_addr, sizeof(*localIP));
return numIP - 1;
}
else if(sa->sa_family == AF_INET6)
{
if(numIP6 > MAX_IPS)
return -1;
sin6 = (struct sockaddr_in6 *) sa;
for(index = 0; index < numIP6; index++)
{
if( !memcmp(&localIP6[index], &sin6->sin6_addr, sizeof(*localIP6)) )
break;
}
if(index >= numIP6)
memcpy(localIP6[numIP6++], &sin6->sin6_addr, sizeof(*localIP6));
return numIP6 - 1;
}
return -1;
}
void NET_AddLocalNetmask(struct sockaddr *sa, int position)
{
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
if(sa->sa_family == AF_INET)
{
sin = (struct sockaddr_in *) sa;
memcpy(localIPmask[position], &sin->sin_addr.s_addr, sizeof(*localIPmask));
}
else if(sa->sa_family == AF_INET6)
{
sin6 = (struct sockaddr_in6 *) sa;
memcpy(localIP6mask[position], &sin6->sin6_addr, sizeof(*localIP6mask));
}
}
#if defined(__linux__) || defined(MACOSX) || defined(__BSD__)
void NET_GetLocalAddress(void)
{
struct ifaddrs *ifap, *search;
int retval;
if(getifaddrs(&ifap))
Com_Printf("NET_GetLocalAddress: Unable to get list of network interfaces: %s\n", NET_ErrorString());
else
{
for(search = ifap; search; search = search->ifa_next)
{
if((retval = NET_AddLocalAddress(search->ifa_addr)) >= 0)
NET_AddLocalNetmask(search->ifa_netmask, retval);
}
freeifaddrs(ifap);
Sys_ShowIP();
}
}
#else
void NET_GetLocalAddress( void ) {
char hostname[256];
struct addrinfo hint;
struct addrinfo *res = NULL;
struct addrinfo *search;
if(gethostname( hostname, 256 ) == SOCKET_ERROR)
return;
Com_Printf( "Hostname: %s\n", hostname );
memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_DGRAM;
if(getaddrinfo(hostname, NULL, &hint, &res))
return;
// add all IPs from returned list.
for(search = res; search; search = search->ai_next)
NET_AddLocalAddress(search->ai_addr);
memset(localIPmask, 0, sizeof(localIPmask));
memset(localIP6mask, 0xFF, sizeof(localIP6mask));
Sys_ShowIP();
}
#endif
/*
====================
NET_OpenIP
====================
*/
void NET_OpenIP( void ) {
int i;
int err;
int port;
int port6;
net_ip = Cvar_Get( "net_ip", "localhost", CVAR_LATCH );
net_ip6 = Cvar_Get( "net_ip6", "localhost", CVAR_LATCH );
net_port = Cvar_Get( "net_port", va( "%i", PORT_SERVER ), CVAR_LATCH );
net_port6 = Cvar_Get( "net_port6", va( "%i", PORT_SERVER ), CVAR_LATCH );
port = net_port->integer;
port6 = net_port6->integer;
// automatically scan for a valid port, so multiple
// dedicated servers can be started without requiring
// a different net_port for each one
if(net_enabled->integer & NET_ENABLEV4)
{
for( i = 0 ; i < 10 ; i++ ) {
ip_socket = NET_IPSocket( net_ip->string, port + i, &err );
if (ip_socket != INVALID_SOCKET) {
Cvar_SetValue( "net_port", port + i );
if (net_socksEnabled->integer)
NET_OpenSocks( port + i );
break;
}
else
{
if(err == EAFNOSUPPORT)
break;
}
}
if(ip_socket == INVALID_SOCKET)
Com_Printf( "WARNING: Couldn't bind to a v4 ip address.\n");
}
if(net_enabled->integer & NET_ENABLEV6)
{
for( i = 0 ; i < 10 ; i++ )
{
ip6_socket = NET_IP6Socket(net_ip6->string, port6 + i, &err);
if (ip6_socket != INVALID_SOCKET)
{
Cvar_SetValue( "net_port6", port6 + i );
break;
}
else
{
if(err == EAFNOSUPPORT)
break;
}
}
if(ip6_socket == INVALID_SOCKET)
Com_Printf( "WARNING: Couldn't bind to a v6 ip address.\n");
}
NET_GetLocalAddress();
}
//===================================================================
/*
====================
NET_GetCvars
====================
*/
static qboolean NET_GetCvars( void ) {
qboolean modified;
modified = qfalse;
if( net_enabled && net_enabled->modified ) {
modified = qtrue;
}
#ifdef DEDICATED
// I want server owners to explicitly turn ipv6 support on.
net_enabled = Cvar_Get( "net_enabled", "1", CVAR_LATCH | CVAR_ARCHIVE );
#else
/* End users have it enabled so they can connect to ipv6-only hosts, but ipv4 will be
* used if available due to ping */
net_enabled = Cvar_Get( "net_enabled", "3", CVAR_LATCH | CVAR_ARCHIVE );
#endif
if( net_socksEnabled && net_socksEnabled->modified ) {
modified = qtrue;
}
net_socksEnabled = Cvar_Get( "net_socksEnabled", "0", CVAR_LATCH | CVAR_ARCHIVE );
if( net_socksServer && net_socksServer->modified ) {
modified = qtrue;
}
net_socksServer = Cvar_Get( "net_socksServer", "", CVAR_LATCH | CVAR_ARCHIVE );
if( net_socksPort && net_socksPort->modified ) {
modified = qtrue;
}
net_socksPort = Cvar_Get( "net_socksPort", "1080", CVAR_LATCH | CVAR_ARCHIVE );
if( net_socksUsername && net_socksUsername->modified ) {
modified = qtrue;
}
net_socksUsername = Cvar_Get( "net_socksUsername", "", CVAR_LATCH | CVAR_ARCHIVE );
if( net_socksPassword && net_socksPassword->modified ) {
modified = qtrue;
}
net_socksPassword = Cvar_Get( "net_socksPassword", "", CVAR_LATCH | CVAR_ARCHIVE );
return modified;
}
/*
====================
NET_Config
====================
*/
void NET_Config( qboolean enableNetworking ) {
qboolean modified;
qboolean stop;
qboolean start;
// get any latched changes to cvars
modified = NET_GetCvars();
if( !net_enabled->integer ) {
enableNetworking = 0;
}
// if enable state is the same and no cvars were modified, we have nothing to do
if( enableNetworking == networkingEnabled && !modified ) {
return;
}
if( enableNetworking == networkingEnabled ) {
if( enableNetworking ) {
stop = qtrue;
start = qtrue;
}
else {
stop = qfalse;
start = qfalse;
}
}
else {
if( enableNetworking ) {
stop = qfalse;
start = qtrue;
}
else {
stop = qtrue;
start = qfalse;
}
networkingEnabled = enableNetworking;
}
if( stop ) {
if ( ip_socket != INVALID_SOCKET ) {
closesocket( ip_socket );
ip_socket = INVALID_SOCKET;
}
if ( ip6_socket != INVALID_SOCKET ) {
closesocket( ip6_socket );
ip6_socket = INVALID_SOCKET;
}
if ( socks_socket != INVALID_SOCKET ) {
closesocket( socks_socket );
socks_socket = INVALID_SOCKET;
}
}
if( start ) {
if (net_enabled->integer) {
NET_OpenIP();
}
}
}
/*
====================
NET_Init
====================
*/
void NET_Init( void ) {
#ifdef _WIN32
int r;
r = WSAStartup( MAKEWORD( 1, 1 ), &winsockdata );
if( r ) {
Com_Printf( "WARNING: Winsock initialization failed, returned %d\n", r );
return;
}
winsockInitialized = qtrue;
Com_Printf( "Winsock Initialized\n" );
#endif
// this is really just to get the cvars registered
NET_GetCvars();
NET_Config( qtrue );
}
/*
====================
NET_Shutdown
====================
*/
void NET_Shutdown( void ) {
if ( !networkingEnabled ) {
return;
}
NET_Config( qfalse );
#ifdef _WIN32
WSACleanup();
winsockInitialized = qfalse;
#endif
}
/*
====================
NET_Sleep
Sleeps msec or until something happens on the network
====================
*/
void NET_Sleep( int msec ) {
struct timeval timeout;
fd_set fdset;
int highestfd = -1;
if (!com_dedicated->integer)
return; // we're not a server, just run full speed
if (ip_socket == INVALID_SOCKET && ip6_socket == INVALID_SOCKET)
return;
if (msec < 0 )
return;
FD_ZERO(&fdset);
if(ip_socket != INVALID_SOCKET)
{
FD_SET(ip_socket, &fdset);
if(ip_socket > highestfd)
highestfd = ip_socket;
}
if(ip6_socket != INVALID_SOCKET)
{
FD_SET(ip6_socket, &fdset);
if(ip6_socket > highestfd)
highestfd = ip6_socket;
}
timeout.tv_sec = msec/1000;
timeout.tv_usec = (msec%1000)*1000;
select(ip_socket+1, &fdset, NULL, NULL, &timeout);
}
/*
====================
NET_Restart_f
====================
*/
void NET_Restart( void ) {
NET_Config( networkingEnabled );
}