2005-08-26 17:39:27 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
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
|
2005-10-29 01:53:09 +00:00
|
|
|
along with Quake III Arena source code; if not, write to the Free Software
|
2005-08-26 17:39:27 +00:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
2005-10-26 19:45:21 +00:00
|
|
|
#include "../qcommon/q_shared.h"
|
2005-08-26 17:39:27 +00:00
|
|
|
#include "../qcommon/qcommon.h"
|
2007-08-28 16:42:13 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2008-08-09 19:12:30 +00:00
|
|
|
# include <winsock2.h>
|
|
|
|
# include <ws2tcpip.h>
|
|
|
|
# if WINVER < 0x501
|
|
|
|
# ifdef __MINGW32__
|
|
|
|
// wspiapi.h isn't available on MinGW, so if it's
|
|
|
|
// present it's because the end user has added it
|
|
|
|
// and we should look for it in our tree
|
|
|
|
# include "wspiapi.h"
|
|
|
|
# else
|
|
|
|
# include <wspiapi.h>
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# include <ws2spi.h>
|
|
|
|
# endif
|
2007-08-28 16:42:13 +00:00
|
|
|
|
|
|
|
typedef int socklen_t;
|
2008-08-09 19:12:30 +00:00
|
|
|
# ifdef ADDRESS_FAMILY
|
|
|
|
# define sa_family_t ADDRESS_FAMILY
|
|
|
|
# else
|
2008-04-05 16:01:58 +00:00
|
|
|
typedef unsigned short sa_family_t;
|
2008-08-09 19:12:30 +00:00
|
|
|
# endif
|
2008-04-09 14:37:42 +00:00
|
|
|
|
2008-08-09 19:12:30 +00:00
|
|
|
# define EAGAIN WSAEWOULDBLOCK
|
|
|
|
# define EADDRNOTAVAIL WSAEADDRNOTAVAIL
|
|
|
|
# define EAFNOSUPPORT WSAEAFNOSUPPORT
|
|
|
|
# define ECONNRESET WSAECONNRESET
|
2011-02-06 20:26:18 +00:00
|
|
|
typedef u_long ioctlarg_t;
|
2008-08-09 19:12:30 +00:00
|
|
|
# define socketError WSAGetLastError( )
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
static WSADATA winsockdata;
|
|
|
|
static qboolean winsockInitialized = qfalse;
|
2007-08-28 16:42:13 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2008-08-09 19:12:30 +00:00
|
|
|
# if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
|
|
|
|
// needed for socklen_t on OSX 10.2
|
|
|
|
# define _BSD_SOCKLEN_T_
|
|
|
|
# endif
|
|
|
|
|
2011-06-06 14:59:54 +00:00
|
|
|
# include <sys/socket.h>
|
2008-08-09 19:12:30 +00:00
|
|
|
# include <errno.h>
|
|
|
|
# include <netdb.h>
|
|
|
|
# include <netinet/in.h>
|
2011-06-06 14:59:54 +00:00
|
|
|
# include <arpa/inet.h>
|
2008-08-09 19:12:30 +00:00
|
|
|
# include <net/if.h>
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# if !defined(__sun) && !defined(__sgi)
|
|
|
|
# include <ifaddrs.h>
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef __sun
|
|
|
|
# include <sys/filio.h>
|
|
|
|
# endif
|
2007-08-28 16:42:13 +00:00
|
|
|
|
|
|
|
typedef int SOCKET;
|
2008-08-09 19:12:30 +00:00
|
|
|
# define INVALID_SOCKET -1
|
|
|
|
# define SOCKET_ERROR -1
|
|
|
|
# define closesocket close
|
|
|
|
# define ioctlsocket ioctl
|
2011-02-06 20:26:18 +00:00
|
|
|
typedef int ioctlarg_t;
|
2008-08-09 19:12:30 +00:00
|
|
|
# define socketError errno
|
2007-08-28 16:42:13 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
static qboolean usingSocks = qfalse;
|
2008-04-04 23:22:22 +00:00
|
|
|
static int networkingEnabled = 0;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
static cvar_t *net_enabled;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
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;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
|
|
|
static cvar_t *net_ip;
|
|
|
|
static cvar_t *net_ip6;
|
|
|
|
static cvar_t *net_port;
|
|
|
|
static cvar_t *net_port6;
|
2008-04-09 14:37:42 +00:00
|
|
|
static cvar_t *net_mcast6addr;
|
|
|
|
static cvar_t *net_mcast6iface;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
static cvar_t *net_dropsim;
|
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
static struct sockaddr socksRelayAddr;
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
static SOCKET ip_socket = INVALID_SOCKET;
|
|
|
|
static SOCKET ip6_socket = INVALID_SOCKET;
|
|
|
|
static SOCKET socks_socket = INVALID_SOCKET;
|
2008-04-09 14:37:42 +00:00
|
|
|
static SOCKET multicast6_socket = INVALID_SOCKET;
|
|
|
|
|
|
|
|
// Keep track of currently joined multicast group.
|
|
|
|
static struct ipv6_mreq curgroup;
|
|
|
|
// And the currently bound address.
|
|
|
|
static struct sockaddr_in6 boundto;
|
|
|
|
|
|
|
|
#ifndef IF_NAMESIZE
|
|
|
|
#define IF_NAMESIZE 16
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// use an admin local address per default so that network admins can decide on how to handle quake3 traffic.
|
|
|
|
#define NET_MULTICAST_IP6 "ff04::696f:7175:616b:6533"
|
|
|
|
|
|
|
|
#define MAX_IPS 32
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char ifname[IF_NAMESIZE];
|
|
|
|
|
|
|
|
netadrtype_t type;
|
|
|
|
sa_family_t family;
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
struct sockaddr_storage netmask;
|
|
|
|
} nip_localaddr_t;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
static nip_localaddr_t localIP[MAX_IPS];
|
|
|
|
static int numIP;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_ErrorString
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
char *NET_ErrorString( void ) {
|
2007-08-28 16:42:13 +00:00
|
|
|
#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
|
2011-02-02 16:46:23 +00:00
|
|
|
return strerror(socketError);
|
2007-08-28 16:42:13 +00:00
|
|
|
#endif
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2007-08-28 16:42:13 +00:00
|
|
|
static void NetadrToSockadr( netadr_t *a, struct sockaddr *s ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
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;
|
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
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;
|
2008-08-31 19:54:29 +00:00
|
|
|
((struct sockaddr_in6 *)s)->sin6_scope_id = a->scope_id;
|
2008-04-04 23:22:22 +00:00
|
|
|
}
|
2008-04-09 14:37:42 +00:00
|
|
|
else if(a->type == NA_MULTICAST6)
|
|
|
|
{
|
|
|
|
((struct sockaddr_in6 *)s)->sin6_family = AF_INET6;
|
|
|
|
((struct sockaddr_in6 *)s)->sin6_addr = curgroup.ipv6mr_multiaddr;
|
|
|
|
((struct sockaddr_in6 *)s)->sin6_port = a->port;
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-28 16:42:13 +00:00
|
|
|
static void SockadrToNetadr( struct sockaddr *s, netadr_t *a ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
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;
|
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
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;
|
2008-08-31 19:54:29 +00:00
|
|
|
a->scope_id = ((struct sockaddr_in6 *)s)->sin6_scope_id;
|
2008-04-04 23:22:22 +00:00
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
/*
|
|
|
|
=============
|
2007-08-28 16:42:13 +00:00
|
|
|
Sys_StringToSockaddr
|
2005-08-26 17:39:27 +00:00
|
|
|
=============
|
|
|
|
*/
|
2008-04-04 23:22:22 +00:00
|
|
|
static qboolean Sys_StringToSockaddr(const char *s, struct sockaddr *sadr, int sadr_len, sa_family_t family)
|
|
|
|
{
|
2009-09-14 19:31:42 +00:00
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *res = NULL;
|
|
|
|
struct addrinfo *search = NULL;
|
2008-05-17 10:46:41 +00:00
|
|
|
struct addrinfo *hintsp;
|
2008-04-04 23:22:22 +00:00
|
|
|
int retval;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
memset(sadr, '\0', sizeof(*sadr));
|
|
|
|
memset(&hints, '\0', sizeof(hints));
|
2008-05-17 10:46:41 +00:00
|
|
|
|
2008-08-31 19:54:29 +00:00
|
|
|
hintsp = &hints;
|
|
|
|
hintsp->ai_family = family;
|
|
|
|
hintsp->ai_socktype = SOCK_DGRAM;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2008-05-17 10:50:08 +00:00
|
|
|
retval = getaddrinfo(s, NULL, hintsp, &res);
|
2007-08-28 16:42:13 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
if(!retval)
|
|
|
|
{
|
|
|
|
if(family == AF_UNSPEC)
|
|
|
|
{
|
|
|
|
// Decide here and now which protocol family to use
|
2009-06-23 00:59:47 +00:00
|
|
|
if(net_enabled->integer & NET_PRIOV6)
|
2009-06-23 00:19:08 +00:00
|
|
|
{
|
|
|
|
if(net_enabled->integer & NET_ENABLEV6)
|
|
|
|
search = SearchAddrInfo(res, AF_INET6);
|
|
|
|
|
|
|
|
if(!search && (net_enabled->integer & NET_ENABLEV4))
|
|
|
|
search = SearchAddrInfo(res, AF_INET);
|
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
else
|
|
|
|
{
|
2009-06-23 00:19:08 +00:00
|
|
|
if(net_enabled->integer & NET_ENABLEV4)
|
2008-04-04 23:22:22 +00:00
|
|
|
search = SearchAddrInfo(res, AF_INET);
|
2009-06-23 00:19:08 +00:00
|
|
|
|
|
|
|
if(!search && (net_enabled->integer & NET_ENABLEV6))
|
2008-04-04 23:22:22 +00:00
|
|
|
search = SearchAddrInfo(res, AF_INET6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
search = SearchAddrInfo(res, family);
|
|
|
|
|
|
|
|
if(search)
|
|
|
|
{
|
2011-01-24 01:55:12 +00:00
|
|
|
if(search->ai_addrlen > sadr_len)
|
|
|
|
search->ai_addrlen = sadr_len;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2011-01-24 01:55:12 +00:00
|
|
|
memcpy(sadr, search->ai_addr, search->ai_addrlen);
|
2012-04-05 21:29:51 +00:00
|
|
|
freeaddrinfo(res);
|
2008-04-04 23:22:22 +00:00
|
|
|
|
|
|
|
return qtrue;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
else
|
|
|
|
Com_Printf("Sys_StringToSockaddr: Error resolving %s: No address of required type found.\n", s);
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
else
|
|
|
|
Com_Printf("Sys_StringToSockaddr: Error resolving %s: %s\n", s, gai_strerror(retval));
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
if(res)
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
Sys_SockaddrToString
|
|
|
|
=============
|
|
|
|
*/
|
2008-10-29 15:53:46 +00:00
|
|
|
static void Sys_SockaddrToString(char *dest, int destlen, struct sockaddr *input)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2008-10-29 15:53:46 +00:00
|
|
|
socklen_t inputlen;
|
|
|
|
|
|
|
|
if (input->sa_family == AF_INET6)
|
|
|
|
inputlen = sizeof(struct sockaddr_in6);
|
|
|
|
else
|
|
|
|
inputlen = sizeof(struct sockaddr_in);
|
|
|
|
|
2009-06-26 18:11:45 +00:00
|
|
|
if(getnameinfo(input, inputlen, dest, destlen, NULL, 0, NI_NUMERICHOST) && destlen > 0)
|
|
|
|
*dest = '\0';
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
Sys_StringToAdr
|
|
|
|
=============
|
|
|
|
*/
|
2008-04-04 23:22:22 +00:00
|
|
|
qboolean Sys_StringToAdr( const char *s, netadr_t *a, netadrtype_t family ) {
|
|
|
|
struct sockaddr_storage sadr;
|
|
|
|
sa_family_t fam;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
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 ) ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
SockadrToNetadr( (struct sockaddr *) &sadr, a );
|
2005-08-26 17:39:27 +00:00
|
|
|
return qtrue;
|
|
|
|
}
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
/*
|
|
|
|
===================
|
2009-05-24 16:58:08 +00:00
|
|
|
NET_CompareBaseAdrMask
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2009-05-24 16:58:08 +00:00
|
|
|
Compare without port, and up to the bit number given in netmask.
|
2008-04-04 23:22:22 +00:00
|
|
|
===================
|
|
|
|
*/
|
2009-05-24 16:58:08 +00:00
|
|
|
qboolean NET_CompareBaseAdrMask(netadr_t a, netadr_t b, int netmask)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2009-05-24 16:58:08 +00:00
|
|
|
byte cmpmask, *addra, *addrb;
|
|
|
|
int curbyte;
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
if (a.type != b.type)
|
|
|
|
return qfalse;
|
|
|
|
|
|
|
|
if (a.type == NA_LOOPBACK)
|
|
|
|
return qtrue;
|
|
|
|
|
2009-05-24 16:58:08 +00:00
|
|
|
if(a.type == NA_IP)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2009-05-24 16:58:08 +00:00
|
|
|
addra = (byte *) &a.ip;
|
|
|
|
addrb = (byte *) &b.ip;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2009-05-24 16:58:08 +00:00
|
|
|
if(netmask < 0 || netmask > 32)
|
|
|
|
netmask = 32;
|
2008-04-04 23:22:22 +00:00
|
|
|
}
|
2009-05-24 16:58:08 +00:00
|
|
|
else if(a.type == NA_IP6)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2009-05-24 16:58:08 +00:00
|
|
|
addra = (byte *) &a.ip6;
|
|
|
|
addrb = (byte *) &b.ip6;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2009-05-24 16:58:08 +00:00
|
|
|
if(netmask < 0 || netmask > 128)
|
|
|
|
netmask = 128;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Com_Printf ("NET_CompareBaseAdr: bad address type\n");
|
2008-04-04 23:22:22 +00:00
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
2011-04-26 02:30:12 +00:00
|
|
|
curbyte = netmask >> 3;
|
2009-05-24 16:58:08 +00:00
|
|
|
|
2011-04-26 02:30:12 +00:00
|
|
|
if(curbyte && memcmp(addra, addrb, curbyte))
|
|
|
|
return qfalse;
|
2009-05-24 16:58:08 +00:00
|
|
|
|
2011-04-26 02:32:05 +00:00
|
|
|
netmask &= 0x07;
|
2009-05-24 16:58:08 +00:00
|
|
|
if(netmask)
|
|
|
|
{
|
|
|
|
cmpmask = (1 << netmask) - 1;
|
|
|
|
cmpmask <<= 8 - netmask;
|
|
|
|
|
|
|
|
if((addra[curbyte] & cmpmask) == (addrb[curbyte] & cmpmask))
|
|
|
|
return qtrue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return qtrue;
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
2009-05-24 16:58:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
===================
|
|
|
|
NET_CompareBaseAdr
|
|
|
|
|
|
|
|
Compares without the port
|
|
|
|
===================
|
|
|
|
*/
|
|
|
|
qboolean NET_CompareBaseAdr (netadr_t a, netadr_t b)
|
|
|
|
{
|
|
|
|
return NET_CompareBaseAdrMask(a, b, -1);
|
|
|
|
}
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
const char *NET_AdrToString (netadr_t a)
|
|
|
|
{
|
|
|
|
static char s[NET_ADDRSTRMAXLEN];
|
|
|
|
|
2009-05-21 21:10:25 +00:00
|
|
|
if (a.type == NA_LOOPBACK)
|
2008-04-04 23:22:22 +00:00
|
|
|
Com_sprintf (s, sizeof(s), "loopback");
|
2009-05-21 21:10:25 +00:00
|
|
|
else if (a.type == NA_BOT)
|
2008-04-04 23:22:22 +00:00
|
|
|
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);
|
2008-10-29 15:53:46 +00:00
|
|
|
Sys_SockaddrToString(s, sizeof(s), (struct sockaddr *) &sadr);
|
2008-04-04 23:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *NET_AdrToStringwPort (netadr_t a)
|
|
|
|
{
|
|
|
|
static char s[NET_ADDRSTRMAXLEN];
|
|
|
|
|
2009-05-21 21:10:25 +00:00
|
|
|
if (a.type == NA_LOOPBACK)
|
2008-04-04 23:22:22 +00:00
|
|
|
Com_sprintf (s, sizeof(s), "loopback");
|
2009-05-21 21:10:25 +00:00
|
|
|
else if (a.type == NA_BOT)
|
2008-04-04 23:22:22 +00:00
|
|
|
Com_sprintf (s, sizeof(s), "bot");
|
2009-05-21 21:10:25 +00:00
|
|
|
else 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));
|
2008-04-04 23:22:22 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
2011-02-02 16:46:23 +00:00
|
|
|
NET_GetPacket
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
Receive one packet
|
2005-08-26 17:39:27 +00:00
|
|
|
==================
|
|
|
|
*/
|
2011-02-02 16:46:23 +00:00
|
|
|
qboolean NET_GetPacket(netadr_t *net_from, msg_t *net_message, fd_set *fdr)
|
|
|
|
{
|
2005-08-26 17:39:27 +00:00
|
|
|
int ret;
|
2008-04-04 23:22:22 +00:00
|
|
|
struct sockaddr_storage from;
|
2007-08-28 16:42:13 +00:00
|
|
|
socklen_t fromlen;
|
2005-08-26 17:39:27 +00:00
|
|
|
int err;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
if(ip_socket != INVALID_SOCKET && FD_ISSET(ip_socket, fdr))
|
2007-08-28 16:42:13 +00:00
|
|
|
{
|
2008-04-04 23:22:22 +00:00
|
|
|
fromlen = sizeof(from);
|
2008-05-10 18:51:02 +00:00
|
|
|
ret = recvfrom( ip_socket, (void *)net_message->data, net_message->maxsize, 0, (struct sockaddr *) &from, &fromlen );
|
2008-04-04 23:22:22 +00:00
|
|
|
|
|
|
|
if (ret == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
err = socketError;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
if( err != EAGAIN && err != ECONNRESET )
|
|
|
|
Com_Printf( "NET_GetPacket: %s\n", NET_ErrorString() );
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
if( ret >= net_message->maxsize ) {
|
2008-04-04 23:22:22 +00:00
|
|
|
Com_Printf( "Oversize packet from %s\n", NET_AdrToString (*net_from) );
|
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_message->cursize = ret;
|
|
|
|
return qtrue;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
2007-08-28 16:42:13 +00:00
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
if(ip6_socket != INVALID_SOCKET && FD_ISSET(ip6_socket, fdr))
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
|
|
|
fromlen = sizeof(from);
|
2008-05-10 18:51:02 +00:00
|
|
|
ret = recvfrom(ip6_socket, (void *)net_message->data, net_message->maxsize, 0, (struct sockaddr *) &from, &fromlen);
|
2008-04-09 14:37:42 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
if(ret >= net_message->maxsize)
|
2008-04-09 14:37:42 +00:00
|
|
|
{
|
|
|
|
Com_Printf( "Oversize packet from %s\n", NET_AdrToString (*net_from) );
|
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_message->cursize = ret;
|
|
|
|
return qtrue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
if(multicast6_socket != INVALID_SOCKET && multicast6_socket != ip6_socket && FD_ISSET(multicast6_socket, fdr))
|
2008-04-09 14:37:42 +00:00
|
|
|
{
|
|
|
|
fromlen = sizeof(from);
|
2008-05-10 18:51:02 +00:00
|
|
|
ret = recvfrom(multicast6_socket, (void *)net_message->data, net_message->maxsize, 0, (struct sockaddr *) &from, &fromlen);
|
2008-04-04 23:22:22 +00:00
|
|
|
|
|
|
|
if (ret == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
err = socketError;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
if( err != EAGAIN && err != ECONNRESET )
|
|
|
|
Com_Printf( "NET_GetPacket: %s\n", NET_ErrorString() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SockadrToNetadr((struct sockaddr *) &from, net_from);
|
|
|
|
net_message->readcount = 0;
|
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
if(ret >= net_message->maxsize)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
|
|
|
Com_Printf( "Oversize packet from %s\n", NET_AdrToString (*net_from) );
|
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_message->cursize = ret;
|
|
|
|
return qtrue;
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
return qfalse;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
static char socksBuf[4096];
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_SendPacket
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void Sys_SendPacket( int length, const void *data, netadr_t to ) {
|
2008-04-05 16:01:58 +00:00
|
|
|
int ret = SOCKET_ERROR;
|
2008-04-04 23:22:22 +00:00
|
|
|
struct sockaddr_storage addr;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
if( to.type != NA_BROADCAST && to.type != NA_IP && to.type != NA_IP6 && to.type != NA_MULTICAST6)
|
|
|
|
{
|
2005-08-26 17:39:27 +00:00
|
|
|
Com_Error( ERR_FATAL, "Sys_SendPacket: bad address type" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
if( (ip_socket == INVALID_SOCKET && to.type == NA_IP) ||
|
2012-07-05 13:33:55 +00:00
|
|
|
(ip_socket == INVALID_SOCKET && to.type == NA_BROADCAST) ||
|
2008-04-09 14:37:42 +00:00
|
|
|
(ip6_socket == INVALID_SOCKET && to.type == NA_IP6) ||
|
|
|
|
(ip6_socket == INVALID_SOCKET && to.type == NA_MULTICAST6) )
|
|
|
|
return;
|
|
|
|
|
2008-04-12 17:03:18 +00:00
|
|
|
if(to.type == NA_MULTICAST6 && (net_enabled->integer & NET_DISABLEMCAST))
|
2005-08-26 17:39:27 +00:00
|
|
|
return;
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
NetadrToSockadr( &to, (struct sockaddr *) &addr );
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
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 );
|
2007-08-28 16:42:13 +00:00
|
|
|
ret = sendto( ip_socket, socksBuf, length+10, 0, &socksRelayAddr, sizeof(socksRelayAddr) );
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-04-04 23:22:22 +00:00
|
|
|
if(addr.ss_family == AF_INET)
|
2008-04-05 15:10:50 +00:00
|
|
|
ret = sendto( ip_socket, data, length, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in) );
|
|
|
|
else if(addr.ss_family == AF_INET6)
|
|
|
|
ret = sendto( ip6_socket, data, length, 0, (struct sockaddr *) &addr, sizeof(struct sockaddr_in6) );
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
if( ret == SOCKET_ERROR ) {
|
2007-08-28 16:42:13 +00:00
|
|
|
int err = socketError;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
// wouldblock is silent
|
2007-08-28 16:42:13 +00:00
|
|
|
if( err == EAGAIN ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// some PPP links do not allow broadcasts and return an error
|
2007-08-28 16:42:13 +00:00
|
|
|
if( ( err == EADDRNOTAVAIL ) && ( ( to.type == NA_BROADCAST ) ) ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-05 13:33:55 +00:00
|
|
|
Com_Printf( "Sys_SendPacket: %s\n", NET_ErrorString() );
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_IsLANAddress
|
|
|
|
|
|
|
|
LAN clients will have their rate var ignored
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
qboolean Sys_IsLANAddress( netadr_t adr ) {
|
2008-04-09 14:37:42 +00:00
|
|
|
int index, run, addrsize;
|
2008-04-04 23:22:22 +00:00
|
|
|
qboolean differed;
|
2008-04-09 14:37:42 +00:00
|
|
|
byte *compareadr, *comparemask, *compareip;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
if( adr.type == NA_LOOPBACK ) {
|
|
|
|
return qtrue;
|
|
|
|
}
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
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;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
if(adr.ip[0] == 127)
|
|
|
|
return qtrue;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
else if(adr.type == NA_IP6)
|
|
|
|
{
|
|
|
|
if(adr.ip6[0] == 0xfe && (adr.ip6[1] & 0xc0) == 0x80)
|
|
|
|
return qtrue;
|
|
|
|
if((adr.ip6[0] & 0xfe) == 0xfc)
|
2005-08-26 17:39:27 +00:00
|
|
|
return qtrue;
|
2008-04-09 14:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now compare against the networks this computer is member of.
|
|
|
|
for(index = 0; index < numIP; index++)
|
|
|
|
{
|
|
|
|
if(localIP[index].type == adr.type)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2008-04-09 14:37:42 +00:00
|
|
|
if(adr.type == NA_IP)
|
|
|
|
{
|
|
|
|
compareip = (byte *) &((struct sockaddr_in *) &localIP[index].addr)->sin_addr.s_addr;
|
|
|
|
comparemask = (byte *) &((struct sockaddr_in *) &localIP[index].netmask)->sin_addr.s_addr;
|
|
|
|
compareadr = adr.ip;
|
|
|
|
|
|
|
|
addrsize = sizeof(adr.ip);
|
|
|
|
}
|
2008-04-09 22:20:04 +00:00
|
|
|
else
|
2008-04-09 14:37:42 +00:00
|
|
|
{
|
2008-08-31 19:54:29 +00:00
|
|
|
// TODO? should we check the scope_id here?
|
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
compareip = (byte *) &((struct sockaddr_in6 *) &localIP[index].addr)->sin6_addr;
|
|
|
|
comparemask = (byte *) &((struct sockaddr_in6 *) &localIP[index].netmask)->sin6_addr;
|
|
|
|
compareadr = adr.ip6;
|
|
|
|
|
|
|
|
addrsize = sizeof(adr.ip6);
|
|
|
|
}
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
differed = qfalse;
|
2008-04-09 14:37:42 +00:00
|
|
|
for(run = 0; run < addrsize; run++)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2008-04-09 14:37:42 +00:00
|
|
|
if((compareip[run] & comparemask[run]) != (compareadr[run] & comparemask[run]))
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
|
|
|
differed = qtrue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!differed)
|
|
|
|
return qtrue;
|
2008-04-09 14:37:42 +00:00
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Sys_ShowIP
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void Sys_ShowIP(void) {
|
|
|
|
int i;
|
2008-04-09 14:37:42 +00:00
|
|
|
char addrbuf[NET_ADDRSTRMAXLEN];
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
for(i = 0; i < numIP; i++)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2008-10-29 15:53:46 +00:00
|
|
|
Sys_SockaddrToString(addrbuf, sizeof(addrbuf), (struct sockaddr *) &localIP[i].addr);
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
if(localIP[i].type == NA_IP)
|
|
|
|
Com_Printf( "IP: %s\n", addrbuf);
|
|
|
|
else if(localIP[i].type == NA_IP6)
|
|
|
|
Com_Printf( "IP6: %s\n", addrbuf);
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_IPSocket
|
|
|
|
====================
|
|
|
|
*/
|
2013-03-18 21:04:53 +00:00
|
|
|
SOCKET NET_IPSocket( char *net_interface, int port, int *err ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
SOCKET newsocket;
|
|
|
|
struct sockaddr_in address;
|
2011-02-06 20:26:18 +00:00
|
|
|
ioctlarg_t _true = 1;
|
2005-08-26 17:39:27 +00:00
|
|
|
int i = 1;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
|
|
|
*err = 0;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
if( net_interface ) {
|
|
|
|
Com_Printf( "Opening IP socket: %s:%i\n", net_interface, port );
|
|
|
|
}
|
|
|
|
else {
|
2008-06-12 18:32:29 +00:00
|
|
|
Com_Printf( "Opening IP socket: 0.0.0.0:%i\n", port );
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
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;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
// make it non-blocking
|
2008-11-10 23:55:22 +00:00
|
|
|
if( ioctlsocket( newsocket, FIONBIO, &_true ) == SOCKET_ERROR ) {
|
2008-04-04 23:22:22 +00:00
|
|
|
Com_Printf( "WARNING: NET_IPSocket: ioctl FIONBIO: %s\n", NET_ErrorString() );
|
|
|
|
*err = socketError;
|
|
|
|
closesocket(newsocket);
|
|
|
|
return INVALID_SOCKET;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make it broadcast capable
|
2008-04-04 23:22:22 +00:00
|
|
|
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() );
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 18:32:29 +00:00
|
|
|
if( !net_interface || !net_interface[0]) {
|
2008-04-04 23:22:22 +00:00
|
|
|
address.sin_family = AF_INET;
|
2005-08-26 17:39:27 +00:00
|
|
|
address.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!Sys_StringToSockaddr( net_interface, (struct sockaddr *)&address, sizeof(address), AF_INET))
|
|
|
|
{
|
|
|
|
closesocket(newsocket);
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( port == PORT_ANY ) {
|
|
|
|
address.sin_port = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
address.sin_port = htons( (short)port );
|
|
|
|
}
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
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
|
|
|
|
====================
|
|
|
|
*/
|
2013-03-18 21:04:53 +00:00
|
|
|
SOCKET NET_IP6Socket( char *net_interface, int port, struct sockaddr_in6 *bindto, int *err ) {
|
2008-04-04 23:22:22 +00:00
|
|
|
SOCKET newsocket;
|
|
|
|
struct sockaddr_in6 address;
|
2011-02-06 20:26:18 +00:00
|
|
|
ioctlarg_t _true = 1;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
|
|
|
*err = 0;
|
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
if( net_interface )
|
|
|
|
{
|
|
|
|
// Print the name in brackets if there is a colon:
|
|
|
|
if(Q_CountChar(net_interface, ':'))
|
|
|
|
Com_Printf( "Opening IP6 socket: [%s]:%i\n", net_interface, port );
|
|
|
|
else
|
|
|
|
Com_Printf( "Opening IP6 socket: %s:%i\n", net_interface, port );
|
2008-04-04 23:22:22 +00:00
|
|
|
}
|
2008-04-09 14:37:42 +00:00
|
|
|
else
|
2008-06-12 18:32:29 +00:00
|
|
|
Com_Printf( "Opening IP6 socket: [::]:%i\n", port );
|
2008-04-04 23:22:22 +00:00
|
|
|
|
|
|
|
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
|
2008-11-10 23:55:22 +00:00
|
|
|
if( ioctlsocket( newsocket, FIONBIO, &_true ) == SOCKET_ERROR ) {
|
2008-04-04 23:22:22 +00:00
|
|
|
Com_Printf( "WARNING: NET_IP6Socket: ioctl FIONBIO: %s\n", NET_ErrorString() );
|
|
|
|
*err = socketError;
|
|
|
|
closesocket(newsocket);
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2008-04-05 16:01:58 +00:00
|
|
|
#ifdef IPV6_V6ONLY
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2009-06-22 23:32:11 +00:00
|
|
|
int i = 1;
|
2008-05-10 18:51:02 +00:00
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
}
|
2008-04-05 16:01:58 +00:00
|
|
|
#endif
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2008-06-12 18:32:29 +00:00
|
|
|
if( !net_interface || !net_interface[0]) {
|
2008-04-04 23:22:22 +00:00
|
|
|
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 );
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
if( bind( newsocket, (void *)&address, sizeof(address) ) == SOCKET_ERROR ) {
|
2008-04-04 23:22:22 +00:00
|
|
|
Com_Printf( "WARNING: NET_IP6Socket: bind: %s\n", NET_ErrorString() );
|
|
|
|
*err = socketError;
|
2005-08-26 17:39:27 +00:00
|
|
|
closesocket( newsocket );
|
2008-04-04 23:22:22 +00:00
|
|
|
return INVALID_SOCKET;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
2008-04-09 14:37:42 +00:00
|
|
|
|
|
|
|
if(bindto)
|
|
|
|
*bindto = address;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
return newsocket;
|
|
|
|
}
|
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_SetMulticast
|
|
|
|
Set the current multicast group
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_SetMulticast6(void)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 addr;
|
|
|
|
|
|
|
|
if(!*net_mcast6addr->string || !Sys_StringToSockaddr(net_mcast6addr->string, (struct sockaddr *) &addr, sizeof(addr), AF_INET6))
|
|
|
|
{
|
|
|
|
Com_Printf("WARNING: NET_JoinMulticast6: Incorrect multicast address given, "
|
|
|
|
"please set cvar %s to a sane value.\n", net_mcast6addr->name);
|
|
|
|
|
2008-04-10 15:37:25 +00:00
|
|
|
Cvar_SetValue(net_enabled->name, net_enabled->integer | NET_DISABLEMCAST);
|
2008-04-09 14:37:42 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&curgroup.ipv6mr_multiaddr, &addr.sin6_addr, sizeof(curgroup.ipv6mr_multiaddr));
|
|
|
|
|
2008-04-16 20:57:08 +00:00
|
|
|
if(*net_mcast6iface->string)
|
2008-04-09 14:37:42 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2009-11-04 00:42:22 +00:00
|
|
|
curgroup.ipv6mr_interface = net_mcast6iface->integer;
|
2008-04-09 14:37:42 +00:00
|
|
|
#else
|
|
|
|
curgroup.ipv6mr_interface = if_nametoindex(net_mcast6iface->string);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
curgroup.ipv6mr_interface = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_JoinMulticast
|
|
|
|
Join an ipv6 multicast group
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_JoinMulticast6(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2008-04-12 17:03:18 +00:00
|
|
|
if(ip6_socket == INVALID_SOCKET || multicast6_socket != INVALID_SOCKET || (net_enabled->integer & NET_DISABLEMCAST))
|
2008-04-09 14:37:42 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(IN6_IS_ADDR_MULTICAST(&boundto.sin6_addr) || IN6_IS_ADDR_UNSPECIFIED(&boundto.sin6_addr))
|
|
|
|
{
|
|
|
|
// The way the socket was bound does not prohibit receiving multi-cast packets. So we don't need to open a new one.
|
|
|
|
multicast6_socket = ip6_socket;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if((multicast6_socket = NET_IP6Socket(net_mcast6addr->string, ntohs(boundto.sin6_port), NULL, &err)) == INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
// If the OS does not support binding to multicast addresses, like WinXP, at least try with the normal file descriptor.
|
|
|
|
multicast6_socket = ip6_socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(curgroup.ipv6mr_interface)
|
|
|
|
{
|
|
|
|
if (setsockopt(multicast6_socket, IPPROTO_IPV6, IPV6_MULTICAST_IF,
|
2010-01-10 18:28:32 +00:00
|
|
|
(char *) &curgroup.ipv6mr_interface, sizeof(curgroup.ipv6mr_interface)) < 0)
|
2008-04-09 14:37:42 +00:00
|
|
|
{
|
2010-01-10 18:28:32 +00:00
|
|
|
Com_Printf("NET_JoinMulticast6: Couldn't set scope on multicast socket: %s\n", NET_ErrorString());
|
2008-04-09 14:37:42 +00:00
|
|
|
|
2010-01-10 18:28:32 +00:00
|
|
|
if(multicast6_socket != ip6_socket)
|
|
|
|
{
|
|
|
|
closesocket(multicast6_socket);
|
|
|
|
multicast6_socket = INVALID_SOCKET;
|
|
|
|
return;
|
2008-04-09 14:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-10 18:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (setsockopt(multicast6_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *) &curgroup, sizeof(curgroup)))
|
|
|
|
{
|
|
|
|
Com_Printf("NET_JoinMulticast6: Couldn't join multicast group: %s\n", NET_ErrorString());
|
|
|
|
|
|
|
|
if(multicast6_socket != ip6_socket)
|
|
|
|
{
|
|
|
|
closesocket(multicast6_socket);
|
|
|
|
multicast6_socket = INVALID_SOCKET;
|
|
|
|
return;
|
2008-04-09 14:37:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NET_LeaveMulticast6()
|
|
|
|
{
|
|
|
|
if(multicast6_socket != INVALID_SOCKET)
|
|
|
|
{
|
|
|
|
if(multicast6_socket != ip6_socket)
|
|
|
|
closesocket(multicast6_socket);
|
|
|
|
else
|
|
|
|
setsockopt(multicast6_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP, (char *) &curgroup, sizeof(curgroup));
|
|
|
|
|
|
|
|
multicast6_socket = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_OpenSocks
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_OpenSocks( int port ) {
|
|
|
|
struct sockaddr_in address;
|
|
|
|
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 ) {
|
|
|
|
Com_Printf( "WARNING: NET_OpenSocks: socket: %s\n", NET_ErrorString() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = gethostbyname( net_socksServer->string );
|
|
|
|
if ( h == NULL ) {
|
|
|
|
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 ) {
|
|
|
|
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
|
|
|
|
}
|
2008-05-10 18:51:02 +00:00
|
|
|
if ( send( socks_socket, (void *)buf, len, 0 ) == SOCKET_ERROR ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
Com_Printf( "NET_OpenSocks: send: %s\n", NET_ErrorString() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the response
|
2008-05-10 18:51:02 +00:00
|
|
|
len = recv( socks_socket, (void *)buf, 64, 0 );
|
2005-08-26 17:39:27 +00:00
|
|
|
if ( len == SOCKET_ERROR ) {
|
|
|
|
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
|
2008-05-10 18:51:02 +00:00
|
|
|
if ( send( socks_socket, (void *)buf, 3 + ulen + plen, 0 ) == SOCKET_ERROR ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
Com_Printf( "NET_OpenSocks: send: %s\n", NET_ErrorString() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the response
|
2008-05-10 18:51:02 +00:00
|
|
|
len = recv( socks_socket, (void *)buf, 64, 0 );
|
2005-08-26 17:39:27 +00:00
|
|
|
if ( len == SOCKET_ERROR ) {
|
|
|
|
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
|
2008-05-10 18:51:02 +00:00
|
|
|
if ( send( socks_socket, (void *)buf, 10, 0 ) == SOCKET_ERROR ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
Com_Printf( "NET_OpenSocks: send: %s\n", NET_ErrorString() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the response
|
2008-05-10 18:51:02 +00:00
|
|
|
len = recv( socks_socket, (void *)buf, 64, 0 );
|
2005-08-26 17:39:27 +00:00
|
|
|
if( len == SOCKET_ERROR ) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
=====================
|
2010-01-10 18:28:32 +00:00
|
|
|
NET_AddLocalAddress
|
2005-08-26 17:39:27 +00:00
|
|
|
=====================
|
|
|
|
*/
|
2010-01-10 18:28:32 +00:00
|
|
|
static void NET_AddLocalAddress(char *ifname, struct sockaddr *addr, struct sockaddr *netmask)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2008-04-09 14:37:42 +00:00
|
|
|
int addrlen;
|
2008-04-21 13:34:44 +00:00
|
|
|
sa_family_t family;
|
|
|
|
|
|
|
|
// only add addresses that have all required info.
|
|
|
|
if(!addr || !netmask || !ifname)
|
|
|
|
return;
|
|
|
|
|
|
|
|
family = addr->sa_family;
|
2007-08-28 16:42:13 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
if(numIP < MAX_IPS)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2008-04-09 14:37:42 +00:00
|
|
|
if(family == AF_INET)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2008-04-09 14:37:42 +00:00
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
localIP[numIP].type = NA_IP;
|
2008-04-04 23:22:22 +00:00
|
|
|
}
|
2008-04-09 14:37:42 +00:00
|
|
|
else if(family == AF_INET6)
|
2008-04-04 23:22:22 +00:00
|
|
|
{
|
2008-04-09 14:37:42 +00:00
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
localIP[numIP].type = NA_IP6;
|
2007-08-28 16:42:13 +00:00
|
|
|
}
|
2008-04-09 14:37:42 +00:00
|
|
|
else
|
|
|
|
return;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
Q_strncpyz(localIP[numIP].ifname, ifname, sizeof(localIP[numIP].ifname));
|
|
|
|
|
|
|
|
localIP[numIP].family = family;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
memcpy(&localIP[numIP].addr, addr, addrlen);
|
|
|
|
memcpy(&localIP[numIP].netmask, netmask, addrlen);
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
numIP++;
|
2008-04-04 23:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-05 13:18:09 +00:00
|
|
|
#if defined(__linux__) || defined(MACOSX) || defined(__BSD__)
|
2010-01-10 18:28:32 +00:00
|
|
|
static void NET_GetLocalAddress(void)
|
2008-04-05 13:18:09 +00:00
|
|
|
{
|
|
|
|
struct ifaddrs *ifap, *search;
|
|
|
|
|
2011-06-21 13:31:19 +00:00
|
|
|
numIP = 0;
|
|
|
|
|
2008-04-05 13:18:09 +00:00
|
|
|
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)
|
|
|
|
{
|
2008-04-09 14:37:42 +00:00
|
|
|
// Only add interfaces that are up.
|
|
|
|
if(ifap->ifa_flags & IFF_UP)
|
|
|
|
NET_AddLocalAddress(search->ifa_name, search->ifa_addr, search->ifa_netmask);
|
2008-04-05 13:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
|
|
|
|
Sys_ShowIP();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2010-01-10 18:28:32 +00:00
|
|
|
static void NET_GetLocalAddress( void ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
char hostname[256];
|
2010-01-10 18:28:32 +00:00
|
|
|
struct addrinfo hint;
|
|
|
|
struct addrinfo *res = NULL;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2011-06-21 13:31:19 +00:00
|
|
|
numIP = 0;
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
if(gethostname( hostname, 256 ) == SOCKET_ERROR)
|
2005-08-26 17:39:27 +00:00
|
|
|
return;
|
2008-04-04 23:22:22 +00:00
|
|
|
|
|
|
|
Com_Printf( "Hostname: %s\n", hostname );
|
|
|
|
|
|
|
|
memset(&hint, 0, sizeof(hint));
|
|
|
|
|
|
|
|
hint.ai_family = AF_UNSPEC;
|
|
|
|
hint.ai_socktype = SOCK_DGRAM;
|
|
|
|
|
2009-06-26 18:11:45 +00:00
|
|
|
if(!getaddrinfo(hostname, NULL, &hint, &res))
|
2008-04-09 14:37:42 +00:00
|
|
|
{
|
2009-06-26 18:11:45 +00:00
|
|
|
struct sockaddr_in mask4;
|
|
|
|
struct sockaddr_in6 mask6;
|
2010-01-10 18:28:32 +00:00
|
|
|
struct addrinfo *search;
|
2009-06-26 18:11:45 +00:00
|
|
|
|
|
|
|
/* On operating systems where it's more difficult to find out the configured interfaces, we'll just assume a
|
|
|
|
* netmask with all bits set. */
|
|
|
|
|
|
|
|
memset(&mask4, 0, sizeof(mask4));
|
|
|
|
memset(&mask6, 0, sizeof(mask6));
|
|
|
|
mask4.sin_family = AF_INET;
|
|
|
|
memset(&mask4.sin_addr.s_addr, 0xFF, sizeof(mask4.sin_addr.s_addr));
|
|
|
|
mask6.sin6_family = AF_INET6;
|
|
|
|
memset(&mask6.sin6_addr, 0xFF, sizeof(mask6.sin6_addr));
|
|
|
|
|
|
|
|
// add all IPs from returned list.
|
|
|
|
for(search = res; search; search = search->ai_next)
|
|
|
|
{
|
|
|
|
if(search->ai_family == AF_INET)
|
|
|
|
NET_AddLocalAddress("", search->ai_addr, (struct sockaddr *) &mask4);
|
|
|
|
else if(search->ai_family == AF_INET6)
|
|
|
|
NET_AddLocalAddress("", search->ai_addr, (struct sockaddr *) &mask6);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sys_ShowIP();
|
2008-04-09 14:37:42 +00:00
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2009-06-26 18:11:45 +00:00
|
|
|
if(res)
|
|
|
|
freeaddrinfo(res);
|
2008-04-04 23:22:22 +00:00
|
|
|
}
|
2007-08-28 16:42:13 +00:00
|
|
|
#endif
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_OpenIP
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_OpenIP( void ) {
|
|
|
|
int i;
|
2008-04-04 23:22:22 +00:00
|
|
|
int err;
|
|
|
|
int port;
|
|
|
|
int port6;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
port = net_port->integer;
|
|
|
|
port6 = net_port6->integer;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
NET_GetLocalAddress();
|
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
// automatically scan for a valid port, so multiple
|
|
|
|
// dedicated servers can be started without requiring
|
|
|
|
// a different net_port for each one
|
2008-06-12 18:32:33 +00:00
|
|
|
|
|
|
|
if(net_enabled->integer & NET_ENABLEV6)
|
|
|
|
{
|
|
|
|
for( i = 0 ; i < 10 ; i++ )
|
|
|
|
{
|
|
|
|
ip6_socket = NET_IP6Socket(net_ip6->string, port6 + i, &boundto, &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");
|
|
|
|
}
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
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");
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===================================================================
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_GetCvars
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
static qboolean NET_GetCvars( void ) {
|
2009-05-29 03:14:49 +00:00
|
|
|
int modified;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
#ifdef DEDICATED
|
2008-04-09 14:37:42 +00:00
|
|
|
// I want server owners to explicitly turn on ipv6 support.
|
2008-04-04 23:22:22 +00:00
|
|
|
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
|
2009-05-29 03:14:49 +00:00
|
|
|
modified = net_enabled->modified;
|
|
|
|
net_enabled->modified = qfalse;
|
|
|
|
|
|
|
|
net_ip = Cvar_Get( "net_ip", "0.0.0.0", CVAR_LATCH );
|
|
|
|
modified += net_ip->modified;
|
|
|
|
net_ip->modified = qfalse;
|
|
|
|
|
|
|
|
net_ip6 = Cvar_Get( "net_ip6", "::", CVAR_LATCH );
|
|
|
|
modified += net_ip6->modified;
|
|
|
|
net_ip6->modified = qfalse;
|
|
|
|
|
|
|
|
net_port = Cvar_Get( "net_port", va( "%i", PORT_SERVER ), CVAR_LATCH );
|
|
|
|
modified += net_port->modified;
|
|
|
|
net_port->modified = qfalse;
|
|
|
|
|
|
|
|
net_port6 = Cvar_Get( "net_port6", va( "%i", PORT_SERVER ), CVAR_LATCH );
|
|
|
|
modified += net_port6->modified;
|
|
|
|
net_port6->modified = qfalse;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
// Some cvars for configuring multicast options which facilitates scanning for servers on local subnets.
|
|
|
|
net_mcast6addr = Cvar_Get( "net_mcast6addr", NET_MULTICAST_IP6, CVAR_LATCH | CVAR_ARCHIVE );
|
2009-05-29 03:14:49 +00:00
|
|
|
modified += net_mcast6addr->modified;
|
|
|
|
net_mcast6addr->modified = qfalse;
|
2008-04-09 14:37:42 +00:00
|
|
|
|
2009-11-04 00:42:22 +00:00
|
|
|
#ifdef _WIN32
|
2008-04-09 14:37:42 +00:00
|
|
|
net_mcast6iface = Cvar_Get( "net_mcast6iface", "0", CVAR_LATCH | CVAR_ARCHIVE );
|
2009-11-04 00:42:22 +00:00
|
|
|
#else
|
|
|
|
net_mcast6iface = Cvar_Get( "net_mcast6iface", "", CVAR_LATCH | CVAR_ARCHIVE );
|
|
|
|
#endif
|
2010-01-10 18:28:32 +00:00
|
|
|
modified += net_mcast6iface->modified;
|
2009-05-29 03:14:49 +00:00
|
|
|
net_mcast6iface->modified = qfalse;
|
2008-04-09 14:37:42 +00:00
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
net_socksEnabled = Cvar_Get( "net_socksEnabled", "0", CVAR_LATCH | CVAR_ARCHIVE );
|
2010-01-10 18:28:32 +00:00
|
|
|
modified += net_socksEnabled->modified;
|
2009-05-29 03:14:49 +00:00
|
|
|
net_socksEnabled->modified = qfalse;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
net_socksServer = Cvar_Get( "net_socksServer", "", CVAR_LATCH | CVAR_ARCHIVE );
|
2010-01-10 18:28:32 +00:00
|
|
|
modified += net_socksServer->modified;
|
2009-05-29 03:14:49 +00:00
|
|
|
net_socksServer->modified = qfalse;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
net_socksPort = Cvar_Get( "net_socksPort", "1080", CVAR_LATCH | CVAR_ARCHIVE );
|
2010-01-10 18:28:32 +00:00
|
|
|
modified += net_socksPort->modified;
|
2009-05-29 03:14:49 +00:00
|
|
|
net_socksPort->modified = qfalse;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
net_socksUsername = Cvar_Get( "net_socksUsername", "", CVAR_LATCH | CVAR_ARCHIVE );
|
2010-01-10 18:28:32 +00:00
|
|
|
modified += net_socksUsername->modified;
|
2009-05-29 03:14:49 +00:00
|
|
|
net_socksUsername->modified = qfalse;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
net_socksPassword = Cvar_Get( "net_socksPassword", "", CVAR_LATCH | CVAR_ARCHIVE );
|
2010-01-10 18:28:32 +00:00
|
|
|
modified += net_socksPassword->modified;
|
2009-05-29 03:14:49 +00:00
|
|
|
net_socksPassword->modified = qfalse;
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
net_dropsim = Cvar_Get("net_dropsim", "", CVAR_TEMP);
|
|
|
|
|
2009-05-29 03:14:49 +00:00
|
|
|
return modified ? qtrue : qfalse;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_Config
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_Config( qboolean enableNetworking ) {
|
|
|
|
qboolean modified;
|
|
|
|
qboolean stop;
|
|
|
|
qboolean start;
|
|
|
|
|
|
|
|
// get any latched changes to cvars
|
|
|
|
modified = NET_GetCvars();
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
if( !net_enabled->integer ) {
|
|
|
|
enableNetworking = 0;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 ) {
|
2008-04-04 23:22:22 +00:00
|
|
|
if ( ip_socket != INVALID_SOCKET ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
closesocket( ip_socket );
|
2008-04-04 23:22:22 +00:00
|
|
|
ip_socket = INVALID_SOCKET;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2012-06-18 16:03:06 +00:00
|
|
|
if(multicast6_socket != INVALID_SOCKET)
|
2008-04-09 14:37:42 +00:00
|
|
|
{
|
|
|
|
if(multicast6_socket != ip6_socket)
|
|
|
|
closesocket(multicast6_socket);
|
|
|
|
|
|
|
|
multicast6_socket = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2008-04-04 23:22:22 +00:00
|
|
|
if ( ip6_socket != INVALID_SOCKET ) {
|
|
|
|
closesocket( ip6_socket );
|
|
|
|
ip6_socket = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( socks_socket != INVALID_SOCKET ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
closesocket( socks_socket );
|
2008-04-04 23:22:22 +00:00
|
|
|
socks_socket = INVALID_SOCKET;
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2008-04-09 14:37:42 +00:00
|
|
|
if( start )
|
|
|
|
{
|
|
|
|
if (net_enabled->integer)
|
|
|
|
{
|
2005-08-26 17:39:27 +00:00
|
|
|
NET_OpenIP();
|
2008-04-09 14:37:42 +00:00
|
|
|
NET_SetMulticast6();
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_Init
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_Init( void ) {
|
2007-08-28 16:42:13 +00:00
|
|
|
#ifdef _WIN32
|
2005-08-26 17:39:27 +00:00
|
|
|
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" );
|
2007-08-28 16:42:13 +00:00
|
|
|
#endif
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
NET_Config( qtrue );
|
2009-05-29 03:14:49 +00:00
|
|
|
|
|
|
|
Cmd_AddCommand ("net_restart", NET_Restart_f);
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_Shutdown
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_Shutdown( void ) {
|
2007-08-28 16:42:13 +00:00
|
|
|
if ( !networkingEnabled ) {
|
2005-08-26 17:39:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-08-28 16:42:13 +00:00
|
|
|
|
2005-08-26 17:39:27 +00:00
|
|
|
NET_Config( qfalse );
|
2007-08-28 16:42:13 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2005-08-26 17:39:27 +00:00
|
|
|
WSACleanup();
|
|
|
|
winsockInitialized = qfalse;
|
2007-08-28 16:42:13 +00:00
|
|
|
#endif
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_Event
|
|
|
|
|
|
|
|
Called from NET_Sleep which uses select() to determine which sockets have seen action.
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
|
|
|
|
void NET_Event(fd_set *fdr)
|
|
|
|
{
|
|
|
|
byte bufData[MAX_MSGLEN + 1];
|
|
|
|
netadr_t from;
|
|
|
|
msg_t netmsg;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
MSG_Init(&netmsg, bufData, sizeof(bufData));
|
|
|
|
|
|
|
|
if(NET_GetPacket(&from, &netmsg, fdr))
|
|
|
|
{
|
|
|
|
if(net_dropsim->value > 0.0f && net_dropsim->value <= 100.0f)
|
|
|
|
{
|
|
|
|
// com_dropsim->value percent of incoming packets get dropped.
|
|
|
|
if(rand() < (int) (((double) RAND_MAX) / 100.0 * (double) net_dropsim->value))
|
|
|
|
continue; // drop this packet
|
2012-06-19 22:36:54 +00:00
|
|
|
}
|
2011-02-02 16:46:23 +00:00
|
|
|
|
|
|
|
if(com_sv_running->integer)
|
|
|
|
Com_RunAndTimeServerPacket(&from, &netmsg);
|
|
|
|
else
|
|
|
|
CL_PacketEvent(from, &netmsg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_Sleep
|
|
|
|
|
2007-09-15 02:22:58 +00:00
|
|
|
Sleeps msec or until something happens on the network
|
2005-08-26 17:39:27 +00:00
|
|
|
====================
|
|
|
|
*/
|
2011-02-02 16:46:23 +00:00
|
|
|
void NET_Sleep(int msec)
|
|
|
|
{
|
2007-08-28 16:42:13 +00:00
|
|
|
struct timeval timeout;
|
2011-02-02 16:46:23 +00:00
|
|
|
fd_set fdr;
|
2012-06-19 16:12:16 +00:00
|
|
|
int retval;
|
|
|
|
SOCKET highestfd = INVALID_SOCKET;
|
2007-08-28 16:42:13 +00:00
|
|
|
|
2011-07-15 16:51:54 +00:00
|
|
|
if(msec < 0)
|
|
|
|
msec = 0;
|
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
FD_ZERO(&fdr);
|
2008-04-04 23:22:22 +00:00
|
|
|
|
|
|
|
if(ip_socket != INVALID_SOCKET)
|
|
|
|
{
|
2011-02-02 16:46:23 +00:00
|
|
|
FD_SET(ip_socket, &fdr);
|
2008-04-04 23:22:22 +00:00
|
|
|
|
2009-06-05 18:54:47 +00:00
|
|
|
highestfd = ip_socket;
|
2008-04-04 23:22:22 +00:00
|
|
|
}
|
|
|
|
if(ip6_socket != INVALID_SOCKET)
|
|
|
|
{
|
2011-02-02 16:46:23 +00:00
|
|
|
FD_SET(ip6_socket, &fdr);
|
2012-06-19 22:36:54 +00:00
|
|
|
|
|
|
|
if(highestfd == INVALID_SOCKET || ip6_socket > highestfd)
|
2008-04-04 23:22:22 +00:00
|
|
|
highestfd = ip6_socket;
|
|
|
|
}
|
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
#ifdef _WIN32
|
2012-06-19 16:12:16 +00:00
|
|
|
if(highestfd == INVALID_SOCKET)
|
2011-02-02 16:46:23 +00:00
|
|
|
{
|
|
|
|
// windows ain't happy when select is called without valid FDs
|
|
|
|
SleepEx(msec, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2005-08-26 17:39:27 +00:00
|
|
|
|
2012-06-19 22:36:54 +00:00
|
|
|
timeout.tv_sec = msec/1000;
|
|
|
|
timeout.tv_usec = (msec%1000)*1000;
|
|
|
|
|
2011-02-02 16:46:23 +00:00
|
|
|
retval = select(highestfd + 1, &fdr, NULL, NULL, &timeout);
|
2012-06-19 22:36:54 +00:00
|
|
|
|
|
|
|
if(retval == SOCKET_ERROR)
|
2011-02-02 16:46:23 +00:00
|
|
|
Com_Printf("Warning: select() syscall failed: %s\n", NET_ErrorString());
|
|
|
|
else if(retval > 0)
|
|
|
|
NET_Event(&fdr);
|
|
|
|
}
|
2005-08-26 17:39:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_Restart_f
|
|
|
|
====================
|
|
|
|
*/
|
2011-12-03 02:23:38 +00:00
|
|
|
void NET_Restart_f(void)
|
|
|
|
{
|
|
|
|
NET_Config(qtrue);
|
2005-08-26 17:39:27 +00:00
|
|
|
}
|