2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
net_ws.c - win network interface
|
|
|
|
Copyright (C) 2007 Uncle Mike
|
|
|
|
|
|
|
|
This program 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
*/
|
|
|
|
|
2019-08-08 15:09:32 +02:00
|
|
|
#include "common.h"
|
|
|
|
#include "client.h" // ConnectionProgress
|
|
|
|
#include "netchan.h"
|
2020-02-10 11:07:06 +01:00
|
|
|
#include "xash3d_mathlib.h"
|
2022-03-11 10:11:49 +01:00
|
|
|
#include "ipv6text.h"
|
2019-11-24 01:52:08 +01:00
|
|
|
#if XASH_WIN32
|
2022-03-20 03:19:26 +01:00
|
|
|
#include "platform/win32/net.h"
|
|
|
|
#elif defined XASH_NO_NETWORK
|
2020-02-08 16:45:50 +01:00
|
|
|
#include "platform/stub/net_stub.h"
|
2022-03-20 03:19:26 +01:00
|
|
|
#else
|
|
|
|
#include "platform/posix/net.h"
|
2019-07-01 03:03:11 +02:00
|
|
|
#endif
|
2023-02-13 20:53:17 +01:00
|
|
|
#if XASH_PSVITA
|
|
|
|
#include "platform/psvita/net_psvita.h"
|
|
|
|
static const struct in6_addr in6addr_any;
|
|
|
|
#endif
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2020-02-08 16:45:50 +01:00
|
|
|
#define NET_USE_FRAGMENTS
|
|
|
|
|
|
|
|
#define PORT_ANY -1
|
|
|
|
#define MAX_LOOPBACK 4
|
|
|
|
#define MASK_LOOPBACK (MAX_LOOPBACK - 1)
|
|
|
|
|
|
|
|
#define MAX_ROUTEABLE_PACKET 1400
|
|
|
|
#define SPLITPACKET_MIN_SIZE 508 // RFC 791: 576(min ip packet) - 60 (ip header) - 8 (udp header)
|
|
|
|
#define SPLITPACKET_MAX_SIZE 64000
|
2022-11-17 17:34:46 +01:00
|
|
|
#define NET_MAX_FRAGMENTS ( NET_MAX_FRAGMENT / (SPLITPACKET_MIN_SIZE - sizeof( SPLITPACKET )))
|
2020-02-08 16:45:50 +01:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
// ff02:1
|
|
|
|
static const uint8_t k_ipv6Bytes_LinkLocalAllNodes[16] =
|
|
|
|
{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
byte data[NET_MAX_MESSAGE];
|
|
|
|
int datalen;
|
|
|
|
} net_loopmsg_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
net_loopmsg_t msgs[MAX_LOOPBACK];
|
|
|
|
int get, send;
|
|
|
|
} net_loopback_t;
|
|
|
|
|
|
|
|
typedef struct packetlag_s
|
|
|
|
{
|
|
|
|
byte *data; // Raw stream data is stored.
|
|
|
|
int size;
|
|
|
|
netadr_t from;
|
|
|
|
float receivedtime;
|
|
|
|
struct packetlag_s *next;
|
|
|
|
struct packetlag_s *prev;
|
|
|
|
} packetlag_t;
|
|
|
|
|
|
|
|
// split long packets. Anything over 1460 is failing on some routers.
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int current_sequence;
|
|
|
|
int split_count;
|
|
|
|
int total_size;
|
|
|
|
char buffer[NET_MAX_FRAGMENT];
|
|
|
|
} LONGPACKET;
|
|
|
|
|
|
|
|
// use this to pick apart the network stream, must be packed
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int net_id;
|
|
|
|
int sequence_number;
|
|
|
|
short packet_id;
|
|
|
|
} SPLITPACKET;
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
net_loopback_t loopbacks[NS_COUNT];
|
|
|
|
packetlag_t lagdata[NS_COUNT];
|
|
|
|
int losscount[NS_COUNT];
|
|
|
|
float fakelag; // cached fakelag value
|
|
|
|
LONGPACKET split;
|
|
|
|
int split_flags[NET_MAX_FRAGMENTS];
|
2018-12-05 17:57:05 +01:00
|
|
|
int sequence_number;
|
2018-04-13 18:23:45 +02:00
|
|
|
int ip_sockets[NS_COUNT];
|
2022-03-11 10:11:49 +01:00
|
|
|
int ip6_sockets[NS_COUNT];
|
2018-04-13 18:23:45 +02:00
|
|
|
qboolean initialized;
|
2018-06-01 18:29:47 +02:00
|
|
|
qboolean threads_initialized;
|
2018-04-13 18:23:45 +02:00
|
|
|
qboolean configured;
|
|
|
|
qboolean allow_ip;
|
2022-03-11 10:11:49 +01:00
|
|
|
qboolean allow_ip6;
|
2019-11-24 01:52:08 +01:00
|
|
|
#if XASH_WIN32
|
2018-04-14 02:27:52 +02:00
|
|
|
WSADATA winsockdata;
|
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
} net_state_t;
|
|
|
|
|
|
|
|
static net_state_t net;
|
2023-05-19 07:22:30 +02:00
|
|
|
static CVAR_DEFINE_AUTO( net_address, "0", FCVAR_PRIVILEGED|FCVAR_READ_ONLY, "contain local address of current client" );
|
|
|
|
static CVAR_DEFINE( net_ipname, "ip", "localhost", FCVAR_PRIVILEGED, "network ip address" );
|
|
|
|
static CVAR_DEFINE( net_iphostport, "ip_hostport", "0", FCVAR_READ_ONLY, "network ip host port" );
|
|
|
|
static CVAR_DEFINE( net_hostport, "hostport", "0", FCVAR_READ_ONLY, "network default host port" );
|
|
|
|
static CVAR_DEFINE( net_ipclientport, "ip_clientport", "0", FCVAR_READ_ONLY, "network ip client port" );
|
|
|
|
static CVAR_DEFINE( net_clientport, "clientport", "0", FCVAR_READ_ONLY, "network default client port" );
|
|
|
|
static CVAR_DEFINE( net_fakelag, "fakelag", "0", FCVAR_PRIVILEGED, "lag all incoming network data (including loopback) by xxx ms." );
|
|
|
|
static CVAR_DEFINE( net_fakeloss, "fakeloss", "0", FCVAR_PRIVILEGED, "act like we dropped the packet this % of the time." );
|
|
|
|
CVAR_DEFINE( net_clockwindow, "clockwindow", "0.5", FCVAR_PRIVILEGED, "timewindow to execute client moves" );
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
netadr_t net_local;
|
2022-03-11 10:11:49 +01:00
|
|
|
netadr_t net6_local;
|
|
|
|
|
|
|
|
// cvars equivalents for IPv6
|
2023-05-19 07:22:30 +02:00
|
|
|
static CVAR_DEFINE( net_ip6name, "ip6", "localhost", FCVAR_PRIVILEGED, "network ip6 address" );
|
|
|
|
static CVAR_DEFINE( net_ip6hostport, "ip6_hostport", "0", FCVAR_READ_ONLY, "network ip6 host port" );
|
|
|
|
static CVAR_DEFINE( net_ip6clientport, "ip6_clientport", "0", FCVAR_READ_ONLY, "network ip6 client port" );
|
|
|
|
static CVAR_DEFINE_AUTO( net6_address, "0", FCVAR_PRIVILEGED|FCVAR_READ_ONLY, "contain local IPv6 address of current client" );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_ErrorString
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
char *NET_ErrorString( void )
|
|
|
|
{
|
2019-11-24 01:52:08 +01:00
|
|
|
#if XASH_WIN32
|
2018-04-13 18:23:45 +02:00
|
|
|
int err = WSANOTINITIALISED;
|
|
|
|
|
|
|
|
if( net.initialized )
|
2019-07-01 03:03:11 +02:00
|
|
|
err = WSAGetLastError();
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
switch( err )
|
|
|
|
{
|
|
|
|
case WSAEINTR: return "WSAEINTR";
|
|
|
|
case WSAEBADF: return "WSAEBADF";
|
|
|
|
case WSAEACCES: return "WSAEACCES";
|
|
|
|
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";
|
2018-06-01 18:29:47 +02:00
|
|
|
case WSAEDISCON: return "WSAEDISCON";
|
2018-04-13 18:23:45 +02:00
|
|
|
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";
|
|
|
|
}
|
2018-04-14 02:27:52 +02:00
|
|
|
#else
|
|
|
|
return strerror( errno );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-02-05 03:39:32 +01:00
|
|
|
_inline socklen_t NET_SockAddrLen( const struct sockaddr_storage *addr )
|
|
|
|
{
|
|
|
|
switch ( addr->ss_family )
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
return sizeof( struct sockaddr_in );
|
|
|
|
case AF_INET6:
|
|
|
|
return sizeof( struct sockaddr_in6 );
|
|
|
|
default:
|
|
|
|
return sizeof( *addr ); // what the fuck is this?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 02:27:52 +02:00
|
|
|
_inline qboolean NET_IsSocketError( int retval )
|
|
|
|
{
|
2020-02-08 16:45:50 +01:00
|
|
|
#if XASH_WIN32 || XASH_DOS4GW
|
2018-04-14 02:27:52 +02:00
|
|
|
return retval == SOCKET_ERROR ? true : false;
|
|
|
|
#else
|
|
|
|
return retval < 0 ? true : false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
_inline qboolean NET_IsSocketValid( int socket )
|
|
|
|
{
|
2020-02-08 16:45:50 +01:00
|
|
|
#if XASH_WIN32 || XASH_DOS4GW
|
2018-04-14 02:27:52 +02:00
|
|
|
return socket != INVALID_SOCKET;
|
|
|
|
#else
|
2018-06-01 18:29:47 +02:00
|
|
|
return socket >= 0;
|
2018-04-14 02:27:52 +02:00
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:40 +02:00
|
|
|
void NET_NetadrToIP6Bytes( uint8_t *ip6, const netadr_t *adr )
|
2022-03-11 10:11:49 +01:00
|
|
|
{
|
2022-03-28 03:07:40 +02:00
|
|
|
#if XASH_LITTLE_ENDIAN
|
|
|
|
memcpy( ip6, adr->ip6, sizeof( adr->ip6 ));
|
|
|
|
#elif XASH_BIG_ENDIAN
|
|
|
|
memcpy( ip6, adr->ip6_0, sizeof( adr->ip6_0 ));
|
|
|
|
memcpy( ip6 + sizeof( adr->ip6_0 ), adr->ip6_2, sizeof( adr->ip6_2 ));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void NET_IP6BytesToNetadr( netadr_t *adr, const uint8_t *ip6 )
|
|
|
|
{
|
|
|
|
#if XASH_LITTLE_ENDIAN
|
2022-11-17 17:34:46 +01:00
|
|
|
memcpy( adr->ip6, ip6, sizeof( adr->ip6 ));
|
2022-03-28 03:07:40 +02:00
|
|
|
#elif XASH_BIG_ENDIAN
|
|
|
|
memcpy( adr->ip6_0, ip6, sizeof( adr->ip6_0 ));
|
|
|
|
memcpy( adr->ip6_2, ip6 + sizeof( adr->ip6_0 ), sizeof( adr->ip6_2 ));
|
|
|
|
#endif
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 03:07:40 +02:00
|
|
|
_inline int NET_NetadrIP6Compare( const netadr_t *a, const netadr_t *b )
|
2022-03-11 10:11:49 +01:00
|
|
|
{
|
2022-03-28 03:07:40 +02:00
|
|
|
#if XASH_LITTLE_ENDIAN
|
|
|
|
return memcmp( a->ip6, b->ip6, sizeof( a->ip6 ));
|
|
|
|
#elif XASH_BIG_ENDIAN
|
|
|
|
int ret = memcmp( a->ip6_0, b->ip6_0, sizeof( a->ip6_0 ));
|
|
|
|
if( !ret )
|
|
|
|
return memcmp( a->ip6_2, b->ip6_2, sizeof( a->ip6_2 ));
|
|
|
|
return ret;
|
|
|
|
#endif
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_NetadrToSockadr
|
|
|
|
====================
|
|
|
|
*/
|
2022-03-11 10:11:49 +01:00
|
|
|
static void NET_NetadrToSockadr( netadr_t *a, struct sockaddr_storage *s )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
memset( s, 0, sizeof( *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;
|
|
|
|
}
|
2022-03-28 03:07:40 +02:00
|
|
|
else if( a->type == NA_IP )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
((struct sockaddr_in *)s)->sin_family = AF_INET;
|
|
|
|
((struct sockaddr_in *)s)->sin_port = a->port;
|
2023-10-22 17:40:20 +02:00
|
|
|
((struct sockaddr_in *)s)->sin_addr.s_addr = a->ip4;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
else if( a->type6 == NA_IP6 )
|
|
|
|
{
|
2022-03-28 03:07:40 +02:00
|
|
|
struct in6_addr ip6;
|
|
|
|
|
|
|
|
NET_NetadrToIP6Bytes( ip6.s6_addr, a );
|
|
|
|
|
|
|
|
if( IN6_IS_ADDR_V4MAPPED( &ip6 ))
|
|
|
|
{
|
|
|
|
((struct sockaddr_in *)s)->sin_family = AF_INET;
|
|
|
|
((struct sockaddr_in *)s)->sin_addr.s_addr = *(uint32_t *)(ip6.s6_addr + 12);
|
|
|
|
((struct sockaddr_in *)s)->sin_port = a->port;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
((struct sockaddr_in6 *)s)->sin6_family = AF_INET6;
|
|
|
|
memcpy( &((struct sockaddr_in6 *)s)->sin6_addr, &ip6, sizeof( struct in6_addr ));
|
|
|
|
((struct sockaddr_in6 *)s)->sin6_port = a->port;
|
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
|
|
|
else if( a->type6 == NA_MULTICAST_IP6 )
|
|
|
|
{
|
|
|
|
((struct sockaddr_in6 *)s)->sin6_family = AF_INET6;
|
|
|
|
memcpy(((struct sockaddr_in6 *)s)->sin6_addr.s6_addr, k_ipv6Bytes_LinkLocalAllNodes, sizeof( struct in6_addr ));
|
|
|
|
((struct sockaddr_in6 *)s)->sin6_port = a->port;
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_SockadrToNetAdr
|
|
|
|
====================
|
|
|
|
*/
|
2022-03-11 10:11:49 +01:00
|
|
|
static void NET_SockadrToNetadr( const struct sockaddr_storage *s, netadr_t *a )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
if( s->ss_family == AF_INET )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
a->type = NA_IP;
|
2023-10-22 17:40:20 +02:00
|
|
|
a->ip4 = ((struct sockaddr_in *)s)->sin_addr.s_addr;
|
2018-04-13 18:23:45 +02:00
|
|
|
a->port = ((struct sockaddr_in *)s)->sin_port;
|
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
else if( s->ss_family == AF_INET6 )
|
|
|
|
{
|
|
|
|
NET_IP6BytesToNetadr( a, ((struct sockaddr_in6 *)s)->sin6_addr.s6_addr );
|
2022-03-20 03:19:26 +01:00
|
|
|
|
|
|
|
if( IN6_IS_ADDR_V4MAPPED( &((struct sockaddr_in6 *)s)->sin6_addr ))
|
|
|
|
a->type = NA_IP;
|
|
|
|
else
|
|
|
|
a->type6 = NA_IP6;
|
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
a->port = ((struct sockaddr_in6 *)s)->sin6_port;
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 18:29:47 +02:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
NET_GetHostByName
|
|
|
|
============
|
|
|
|
*/
|
2022-03-11 10:11:49 +01:00
|
|
|
qboolean NET_GetHostByName( const char *hostname, int family, struct sockaddr_storage *addr )
|
2018-06-01 18:29:47 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
#if defined HAVE_GETADDRINFO
|
2018-06-01 18:29:47 +02:00
|
|
|
struct addrinfo *ai = NULL, *cur;
|
|
|
|
struct addrinfo hints;
|
2022-03-11 10:11:49 +01:00
|
|
|
qboolean ret = false;
|
2018-06-01 18:29:47 +02:00
|
|
|
|
|
|
|
memset( &hints, 0, sizeof( hints ));
|
2022-03-11 10:11:49 +01:00
|
|
|
hints.ai_family = family;
|
2018-06-01 18:29:47 +02:00
|
|
|
|
2019-07-01 03:03:11 +02:00
|
|
|
if( !getaddrinfo( hostname, NULL, &hints, &ai ))
|
2018-06-01 18:29:47 +02:00
|
|
|
{
|
|
|
|
for( cur = ai; cur; cur = cur->ai_next )
|
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
if( family == AF_UNSPEC || cur->ai_family == family )
|
2018-06-01 18:29:47 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
memcpy( addr, cur->ai_addr, cur->ai_addrlen );
|
|
|
|
ret = true;
|
2018-06-01 18:29:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ai )
|
|
|
|
freeaddrinfo( ai );
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
return ret;
|
2018-06-01 18:29:47 +02:00
|
|
|
#else
|
|
|
|
struct hostent *h;
|
2019-07-01 03:03:11 +02:00
|
|
|
if(!( h = gethostbyname( hostname )))
|
2022-03-11 10:11:49 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
((struct sockaddr_in *)addr)->sin_family = AF_INET;
|
2022-03-11 10:51:58 +01:00
|
|
|
((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *)h->h_addr_list[0];
|
2022-03-11 10:11:49 +01:00
|
|
|
|
|
|
|
return true;
|
2018-06-01 18:29:47 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-13 06:09:16 +01:00
|
|
|
#if !XASH_EMSCRIPTEN && !XASH_DOS4GW && !defined XASH_NO_ASYNC_NS_RESOLVE
|
2018-04-14 02:27:52 +02:00
|
|
|
#define CAN_ASYNC_NS_RESOLVE
|
2023-01-13 06:09:16 +01:00
|
|
|
#endif // !XASH_EMSCRIPTEN && !XASH_DOS4GW && !defined XASH_NO_ASYNC_NS_RESOLVE
|
2018-04-14 02:27:52 +02:00
|
|
|
|
|
|
|
#ifdef CAN_ASYNC_NS_RESOLVE
|
|
|
|
static void NET_ResolveThread( void );
|
2018-06-01 18:29:47 +02:00
|
|
|
|
2019-11-24 01:52:08 +01:00
|
|
|
#if !XASH_WIN32
|
2018-04-14 02:27:52 +02:00
|
|
|
#include <pthread.h>
|
|
|
|
#define mutex_lock pthread_mutex_lock
|
|
|
|
#define mutex_unlock pthread_mutex_unlock
|
|
|
|
#define exit_thread( x ) pthread_exit(x)
|
|
|
|
#define create_thread( pfn ) !pthread_create( &nsthread.thread, NULL, (pfn), NULL )
|
|
|
|
#define detach_thread( x ) pthread_detach(x)
|
|
|
|
#define mutex_t pthread_mutex_t
|
|
|
|
#define thread_t pthread_t
|
|
|
|
void *NET_ThreadStart( void *unused )
|
|
|
|
{
|
|
|
|
NET_ResolveThread();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else // WIN32
|
2019-06-29 21:01:32 +02:00
|
|
|
#define mutex_lock EnterCriticalSection
|
|
|
|
#define mutex_unlock LeaveCriticalSection
|
2018-04-14 02:27:52 +02:00
|
|
|
#define detach_thread( x ) CloseHandle(x)
|
2023-04-02 23:14:38 +02:00
|
|
|
#define create_thread( pfn ) ( nsthread.thread = CreateThread( NULL, 0, pfn, NULL, 0, NULL ))
|
2019-06-29 21:01:32 +02:00
|
|
|
#define mutex_t CRITICAL_SECTION
|
2018-04-14 02:27:52 +02:00
|
|
|
#define thread_t HANDLE
|
|
|
|
DWORD WINAPI NET_ThreadStart( LPVOID unused )
|
|
|
|
{
|
|
|
|
NET_ResolveThread();
|
2023-01-13 06:09:16 +01:00
|
|
|
ExitThread( 0 );
|
2018-04-14 02:27:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-01 18:29:47 +02:00
|
|
|
#endif // !_WIN32
|
2018-04-14 02:27:52 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_RESOLVE
|
|
|
|
#define RESOLVE_DBG(x) Sys_PrintLog(x)
|
|
|
|
#else
|
|
|
|
#define RESOLVE_DBG(x)
|
|
|
|
#endif // DEBUG_RESOLVE
|
|
|
|
|
|
|
|
static struct nsthread_s
|
|
|
|
{
|
|
|
|
mutex_t mutexns;
|
|
|
|
mutex_t mutexres;
|
|
|
|
thread_t thread;
|
|
|
|
int result;
|
|
|
|
string hostname;
|
2022-03-11 10:11:49 +01:00
|
|
|
int family;
|
|
|
|
struct sockaddr_storage addr;
|
2018-04-14 02:27:52 +02:00
|
|
|
qboolean busy;
|
|
|
|
} nsthread
|
2019-11-24 01:52:08 +01:00
|
|
|
#if !XASH_WIN32
|
2018-04-14 02:27:52 +02:00
|
|
|
= { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER }
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
static void NET_InitializeCriticalSections( void )
|
|
|
|
{
|
2018-06-01 18:29:47 +02:00
|
|
|
net.threads_initialized = true;
|
2023-01-13 06:09:16 +01:00
|
|
|
#if XASH_WIN32
|
|
|
|
InitializeCriticalSection( &nsthread.mutexns );
|
|
|
|
InitializeCriticalSection( &nsthread.mutexres );
|
2018-04-14 02:27:52 +02:00
|
|
|
#endif
|
2023-01-13 06:09:16 +01:00
|
|
|
}
|
2018-04-14 02:27:52 +02:00
|
|
|
|
|
|
|
void NET_ResolveThread( void )
|
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
struct sockaddr_storage addr;
|
2023-10-24 17:11:31 +02:00
|
|
|
qboolean res;
|
2018-04-14 02:27:52 +02:00
|
|
|
|
|
|
|
RESOLVE_DBG( "[resolve thread] starting resolve for " );
|
|
|
|
RESOLVE_DBG( nsthread.hostname );
|
2018-06-01 18:29:47 +02:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
2018-04-14 02:27:52 +02:00
|
|
|
RESOLVE_DBG( " with getaddrinfo\n" );
|
2018-06-01 18:29:47 +02:00
|
|
|
#else
|
|
|
|
RESOLVE_DBG( " with gethostbyname\n" );
|
|
|
|
#endif
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2023-10-24 17:11:31 +02:00
|
|
|
if(( res = NET_GetHostByName( nsthread.hostname, nsthread.family, &addr )))
|
2018-06-01 18:29:47 +02:00
|
|
|
RESOLVE_DBG( "[resolve thread] success\n" );
|
2018-04-14 02:27:52 +02:00
|
|
|
else
|
2018-06-01 18:29:47 +02:00
|
|
|
RESOLVE_DBG( "[resolve thread] failed\n" );
|
2018-04-14 02:27:52 +02:00
|
|
|
mutex_lock( &nsthread.mutexres );
|
2022-03-11 10:11:49 +01:00
|
|
|
nsthread.addr = addr;
|
2018-04-14 02:27:52 +02:00
|
|
|
nsthread.busy = false;
|
2023-10-24 17:11:31 +02:00
|
|
|
nsthread.result = res ? NET_EAI_OK : NET_EAI_NONAME;
|
2018-04-14 02:27:52 +02:00
|
|
|
RESOLVE_DBG( "[resolve thread] returning result\n" );
|
|
|
|
mutex_unlock( &nsthread.mutexres );
|
|
|
|
RESOLVE_DBG( "[resolve thread] exiting thread\n" );
|
|
|
|
}
|
|
|
|
#endif // CAN_ASYNC_NS_RESOLVE
|
|
|
|
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
=============
|
|
|
|
NET_StringToAdr
|
|
|
|
|
|
|
|
localhost
|
|
|
|
idnewt
|
|
|
|
idnewt:28000
|
|
|
|
192.246.40.70
|
|
|
|
192.246.40.70:28000
|
|
|
|
=============
|
|
|
|
*/
|
2023-04-02 23:14:38 +02:00
|
|
|
static net_gai_state_t NET_StringToSockaddr( const char *s, struct sockaddr_storage *sadr, qboolean nonblocking, int family )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
int ret = 0, port;
|
2018-04-13 18:23:45 +02:00
|
|
|
char *colon;
|
|
|
|
char copy[128];
|
2022-03-11 10:11:49 +01:00
|
|
|
byte ip6[16];
|
|
|
|
struct sockaddr_storage temp;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-06-01 18:29:47 +02:00
|
|
|
if( !net.initialized )
|
2023-04-02 23:14:38 +02:00
|
|
|
return NET_EAI_NONAME;
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
memset( sadr, 0, sizeof( *sadr ));
|
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
// try to parse it as IPv6 first
|
|
|
|
if(( family == AF_UNSPEC || family == AF_INET6 ) && ParseIPv6Addr( s, ip6, &port, NULL ))
|
|
|
|
{
|
|
|
|
((struct sockaddr_in6 *)sadr)->sin6_family = AF_INET6;
|
|
|
|
((struct sockaddr_in6 *)sadr)->sin6_port = htons((short)port);
|
|
|
|
memcpy(((struct sockaddr_in6 *)sadr)->sin6_addr.s6_addr, ip6, sizeof( struct in6_addr ));
|
|
|
|
|
2023-04-02 23:14:38 +02:00
|
|
|
return NET_EAI_OK;
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
Q_strncpy( copy, s, sizeof( copy ));
|
|
|
|
|
|
|
|
// strip off a trailing :port if present
|
2022-03-11 10:11:49 +01:00
|
|
|
((struct sockaddr_in *)sadr)->sin_port = 0;
|
2018-04-13 18:23:45 +02:00
|
|
|
for( colon = copy; *colon; colon++ )
|
|
|
|
{
|
|
|
|
if( *colon == ':' )
|
|
|
|
{
|
|
|
|
*colon = 0;
|
2019-07-01 03:03:11 +02:00
|
|
|
((struct sockaddr_in *)sadr)->sin_port = htons((short)Q_atoi( colon + 1 ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 02:27:52 +02:00
|
|
|
if( copy[0] >= '0' && copy[0] <= '9' )
|
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
((struct sockaddr_in *)sadr)->sin_family = AF_INET;
|
|
|
|
((struct sockaddr_in *)sadr)->sin_addr.s_addr = inet_addr( copy );
|
2018-04-14 02:27:52 +02:00
|
|
|
}
|
|
|
|
else
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-06-01 18:29:47 +02:00
|
|
|
qboolean asyncfailed = true;
|
|
|
|
|
2018-04-14 02:27:52 +02:00
|
|
|
#ifdef CAN_ASYNC_NS_RESOLVE
|
2022-03-11 10:11:49 +01:00
|
|
|
if( net.threads_initialized && nonblocking )
|
2018-04-14 02:27:52 +02:00
|
|
|
{
|
2018-06-01 18:29:47 +02:00
|
|
|
mutex_lock( &nsthread.mutexres );
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2018-06-01 18:29:47 +02:00
|
|
|
if( nsthread.busy )
|
|
|
|
{
|
|
|
|
mutex_unlock( &nsthread.mutexres );
|
2023-04-02 23:14:38 +02:00
|
|
|
return NET_EAI_AGAIN;
|
2018-06-01 18:29:47 +02:00
|
|
|
}
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( !Q_strcmp( copy, nsthread.hostname ))
|
2018-06-01 18:29:47 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
ret = nsthread.result;
|
|
|
|
|
2023-04-02 23:14:38 +02:00
|
|
|
nsthread.hostname[0] = '\0';
|
2022-03-11 10:11:49 +01:00
|
|
|
nsthread.family = AF_UNSPEC;
|
|
|
|
temp = nsthread.addr;
|
|
|
|
memset( &nsthread.addr, 0, sizeof( nsthread.addr ));
|
|
|
|
|
2018-06-01 18:29:47 +02:00
|
|
|
detach_thread( nsthread.thread );
|
2023-10-24 17:11:31 +02:00
|
|
|
asyncfailed = false;
|
2018-04-14 02:27:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
Q_strncpy( nsthread.hostname, copy, sizeof( nsthread.hostname ));
|
|
|
|
nsthread.family = family;
|
2018-06-01 18:29:47 +02:00
|
|
|
nsthread.busy = true;
|
|
|
|
mutex_unlock( &nsthread.mutexres );
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( create_thread( NET_ThreadStart ))
|
2018-04-14 02:27:52 +02:00
|
|
|
{
|
2018-06-01 18:29:47 +02:00
|
|
|
asyncfailed = false;
|
2023-04-02 23:14:38 +02:00
|
|
|
return NET_EAI_AGAIN;
|
2018-04-14 02:27:52 +02:00
|
|
|
}
|
2018-06-01 18:29:47 +02:00
|
|
|
else // failed to create thread
|
2018-04-14 02:27:52 +02:00
|
|
|
{
|
2023-04-02 23:14:38 +02:00
|
|
|
Con_Reportf( S_ERROR "NET_StringToSockaddr: failed to create thread!\n");
|
2018-06-01 18:29:47 +02:00
|
|
|
nsthread.busy = false;
|
2018-04-14 02:27:52 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-01 18:29:47 +02:00
|
|
|
|
|
|
|
mutex_unlock( &nsthread.mutexres );
|
2018-04-14 02:27:52 +02:00
|
|
|
}
|
|
|
|
#endif // CAN_ASYNC_NS_RESOLVE
|
|
|
|
|
2018-06-01 18:29:47 +02:00
|
|
|
if( asyncfailed )
|
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
ret = NET_GetHostByName( copy, family, &temp );
|
2018-04-14 02:27:52 +02:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
if( !ret )
|
|
|
|
{
|
|
|
|
if( family == AF_INET6 )
|
|
|
|
sadr->ss_family = AF_INET6;
|
|
|
|
else sadr->ss_family = AF_INET;
|
2023-04-02 23:14:38 +02:00
|
|
|
|
|
|
|
return NET_EAI_NONAME;
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
sadr->ss_family = temp.ss_family;
|
|
|
|
|
|
|
|
if( temp.ss_family == AF_INET )
|
|
|
|
{
|
|
|
|
((struct sockaddr_in *)sadr)->sin_addr =
|
|
|
|
((struct sockaddr_in*)&temp)->sin_addr;
|
|
|
|
}
|
|
|
|
else if( temp.ss_family == AF_INET6 )
|
|
|
|
{
|
|
|
|
memcpy(&((struct sockaddr_in6 *)sadr)->sin6_addr,
|
|
|
|
&((struct sockaddr_in6*)&temp)->sin6_addr,
|
|
|
|
sizeof( struct in6_addr ));
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2023-04-02 23:14:38 +02:00
|
|
|
return NET_EAI_OK;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-06-09 02:07:19 +02:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_StringToFilterAdr
|
|
|
|
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
qboolean NET_StringToFilterAdr( const char *s, netadr_t *adr, uint *prefixlen )
|
|
|
|
{
|
|
|
|
char copy[128], *temp;
|
|
|
|
qboolean hasCIDR = false;
|
|
|
|
byte ip6[16];
|
|
|
|
uint len;
|
|
|
|
|
|
|
|
if( !COM_CheckStringEmpty( s ))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
memset( adr, 0, sizeof( *adr ));
|
|
|
|
|
|
|
|
// copy the string and remove CIDR prefix
|
|
|
|
Q_strncpy( copy, s, sizeof( copy ));
|
|
|
|
temp = Q_strrchr( copy, '/' );
|
|
|
|
|
|
|
|
if( temp )
|
|
|
|
{
|
|
|
|
*temp = 0;
|
|
|
|
if( Q_isdigit( temp + 1 ))
|
|
|
|
{
|
|
|
|
len = Q_atoi( temp + 1 );
|
|
|
|
hasCIDR = len != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to parse as IPv6 first
|
|
|
|
if( ParseIPv6Addr( copy, ip6, NULL, NULL ))
|
|
|
|
{
|
|
|
|
NET_IP6BytesToNetadr( adr, ip6 );
|
|
|
|
adr->type6 = NA_IP6;
|
|
|
|
|
|
|
|
if( !hasCIDR )
|
|
|
|
*prefixlen = 128;
|
|
|
|
else
|
|
|
|
*prefixlen = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int num = 0;
|
|
|
|
int octet = 0;
|
|
|
|
|
|
|
|
// parse as ipv4 but we don't need to allow all forms here
|
|
|
|
for( temp = copy; *temp; temp++ )
|
|
|
|
{
|
|
|
|
char c = *temp;
|
|
|
|
|
|
|
|
if( c >= '0' && c <= '9' )
|
|
|
|
{
|
|
|
|
num *= 10;
|
|
|
|
num += c - '0';
|
|
|
|
}
|
|
|
|
else if( c == '.' )
|
|
|
|
{
|
|
|
|
if( num > 255 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
adr->ip[octet++] = num;
|
|
|
|
num = 0;
|
|
|
|
|
|
|
|
if( octet > 3 )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( num > 255 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
adr->ip[octet++] = num;
|
|
|
|
|
|
|
|
if( !hasCIDR )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
*prefixlen = 32;
|
|
|
|
|
|
|
|
for( i = 3; i >= 0; i-- )
|
|
|
|
{
|
|
|
|
if( !adr->ip[i] )
|
|
|
|
*prefixlen -= 8;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uint32_t mask;
|
|
|
|
|
|
|
|
len = bound( 0, len, 32 );
|
|
|
|
*prefixlen = len;
|
|
|
|
|
|
|
|
// drop unneeded bits
|
|
|
|
mask = htonl( adr->ip4 ) & ( 0xFFFFFFFF << ( 32 - len ));
|
|
|
|
adr->ip4 = ntohl( mask );
|
|
|
|
}
|
|
|
|
|
|
|
|
adr->type = NA_IP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_AdrToString
|
|
|
|
====================
|
|
|
|
*/
|
2021-07-20 15:02:59 +02:00
|
|
|
const char *NET_AdrToString( const netadr_t a )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-04 16:08:28 +01:00
|
|
|
static char s[64];
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
if( a.type == NA_LOOPBACK )
|
|
|
|
return "loopback";
|
2023-08-03 00:11:54 +02:00
|
|
|
if( a.type6 == NA_IP6 || a.type6 == NA_MULTICAST_IP6 )
|
2022-03-11 10:11:49 +01:00
|
|
|
{
|
|
|
|
uint8_t ip6[16];
|
|
|
|
|
|
|
|
NET_NetadrToIP6Bytes( ip6, &a );
|
|
|
|
IPv6AddrToString( s, ip6, ntohs( a.port ), 0 );
|
2022-03-20 03:19:26 +01:00
|
|
|
|
2022-12-04 16:08:28 +01:00
|
|
|
return s;
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
|
|
|
|
2023-04-23 21:56:05 +02:00
|
|
|
Q_snprintf( s, sizeof( s ),
|
|
|
|
"%i.%i.%i.%i:%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3], ntohs( a.port ));
|
2022-12-04 16:08:28 +01:00
|
|
|
|
|
|
|
return s;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_BaseAdrToString
|
|
|
|
====================
|
|
|
|
*/
|
2021-07-20 15:02:59 +02:00
|
|
|
const char *NET_BaseAdrToString( const netadr_t a )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-04 16:08:28 +01:00
|
|
|
static char s[64];
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
if( a.type == NA_LOOPBACK )
|
|
|
|
return "loopback";
|
2023-08-03 00:11:54 +02:00
|
|
|
if( a.type6 == NA_IP6 || a.type6 == NA_MULTICAST_IP6 )
|
2022-03-11 10:11:49 +01:00
|
|
|
{
|
|
|
|
uint8_t ip6[16];
|
|
|
|
|
|
|
|
NET_NetadrToIP6Bytes( ip6, &a );
|
|
|
|
IPv6IPToString( s, ip6 );
|
2022-03-20 03:19:26 +01:00
|
|
|
|
2022-12-04 16:08:28 +01:00
|
|
|
return s;
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
2022-12-04 16:08:28 +01:00
|
|
|
|
2023-04-23 21:56:05 +02:00
|
|
|
Q_snprintf( s, sizeof( s ),
|
|
|
|
"%i.%i.%i.%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3] );
|
2022-12-04 16:08:28 +01:00
|
|
|
|
|
|
|
return s;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===================
|
|
|
|
NET_CompareBaseAdr
|
|
|
|
|
|
|
|
Compares without the port
|
|
|
|
===================
|
|
|
|
*/
|
|
|
|
qboolean NET_CompareBaseAdr( const netadr_t a, const netadr_t b )
|
|
|
|
{
|
2022-06-09 02:07:19 +02:00
|
|
|
if( a.type6 != b.type6 )
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if( a.type == NA_LOOPBACK )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( a.type == NA_IP )
|
2022-06-09 02:07:19 +02:00
|
|
|
return a.ip4 == b.ip4;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
if( a.type6 == NA_IP6 )
|
|
|
|
{
|
2022-03-28 03:07:40 +02:00
|
|
|
if( !NET_NetadrIP6Compare( &a, &b ))
|
2022-03-11 10:11:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_CompareClassBAdr
|
|
|
|
|
|
|
|
Compare local masks
|
|
|
|
====================
|
|
|
|
*/
|
2022-06-09 02:07:19 +02:00
|
|
|
qboolean NET_CompareClassBAdr( const netadr_t a, const netadr_t b )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-06-09 02:07:19 +02:00
|
|
|
if( a.type6 != b.type6 )
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if( a.type == NA_LOOPBACK )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( a.type == NA_IP )
|
|
|
|
{
|
|
|
|
if( a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1] )
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
// NOTE: we don't check for IPv6 here
|
|
|
|
// this check is very dumb and only used for LAN restriction
|
|
|
|
// Actual check is in IsReservedAdr
|
2022-03-11 10:11:49 +01:00
|
|
|
|
2022-06-09 02:07:19 +02:00
|
|
|
// for real mask compare use NET_CompareAdrByMask
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_CompareAdrByMask
|
|
|
|
|
|
|
|
Checks if adr is a part of subnet
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
qboolean NET_CompareAdrByMask( const netadr_t a, const netadr_t b, uint prefixlen )
|
|
|
|
{
|
|
|
|
if( a.type6 != b.type6 || a.type == NA_LOOPBACK )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( a.type == NA_IP )
|
|
|
|
{
|
|
|
|
uint32_t ipa = htonl( a.ip4 );
|
|
|
|
uint32_t ipb = htonl( b.ip4 );
|
|
|
|
|
|
|
|
if(( ipa & (( 0xFFFFFFFFU ) << ( 32 - prefixlen ))) == ipb )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if( a.type6 == NA_IP6 )
|
|
|
|
{
|
|
|
|
uint16_t a_[8], b_[8];
|
|
|
|
size_t check = prefixlen / 16;
|
|
|
|
size_t remaining = prefixlen % 16;
|
|
|
|
|
|
|
|
// convert to 16-bit pieces first
|
|
|
|
NET_NetadrToIP6Bytes( (uint8_t*)a_, &a );
|
|
|
|
NET_NetadrToIP6Bytes( (uint8_t*)b_, &b );
|
|
|
|
|
|
|
|
// check complete hextets first, if not equal, then it's different subnets
|
|
|
|
if( check && memcmp( a_, b_, check * sizeof( uint16_t )))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// check by bits now, similar to v4 check but with 16-bit type
|
|
|
|
if( remaining )
|
|
|
|
{
|
|
|
|
uint16_t hexa, hexb, mask = 0xFFFFU << ( 16 - remaining );
|
|
|
|
|
|
|
|
hexa = htons( a_[check] );
|
|
|
|
hexb = htons( b_[check] );
|
|
|
|
|
|
|
|
if(( hexa & mask ) == ( hexb & mask ))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_IsReservedAdr
|
|
|
|
|
|
|
|
Check for reserved ip's
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
qboolean NET_IsReservedAdr( netadr_t a )
|
|
|
|
{
|
|
|
|
if( a.type == NA_LOOPBACK )
|
|
|
|
return true;
|
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
// Following checks was imported from GameNetworkingSockets library
|
2018-04-13 18:23:45 +02:00
|
|
|
if( a.type == NA_IP )
|
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
if(( a.ip[0] == 10 ) || // 10.x.x.x is reserved
|
|
|
|
( a.ip[0] == 127 ) || // 127.x.x.x
|
|
|
|
( a.ip[0] == 169 && a.ip[1] == 254 ) || // 169.254.x.x is link-local ipv4
|
|
|
|
( a.ip[0] == 172 && a.ip[1] >= 16 && a.ip[1] <= 31 ) || // 172.16.x.x - 172.31.x.x
|
|
|
|
( a.ip[0] == 192 && a.ip[1] >= 168 )) // 192.168.x.x
|
|
|
|
{
|
2018-04-13 18:23:45 +02:00
|
|
|
return true;
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
if( a.type6 == NA_IP6 )
|
|
|
|
{
|
2022-11-17 17:34:46 +01:00
|
|
|
uint8_t ip6[16];
|
|
|
|
|
|
|
|
NET_NetadrToIP6Bytes( ip6, &a );
|
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
// Private addresses, fc00::/7
|
|
|
|
// Range is fc00:: to fdff:ffff:etc
|
2022-11-17 17:34:46 +01:00
|
|
|
if( ip6[0] >= 0xFC && ip6[1] <= 0xFD )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
// Link-local fe80::/10
|
|
|
|
// Range is fe80:: to febf::
|
2022-11-17 17:34:46 +01:00
|
|
|
if( ip6[0] == 0xFE && ( ip6[1] >= 0x80 && ip6[1] <= 0xBF ))
|
2022-03-11 10:11:49 +01:00
|
|
|
{
|
2018-04-13 18:23:45 +02:00
|
|
|
return true;
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_CompareAdr
|
|
|
|
|
|
|
|
Compare full address
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
qboolean NET_CompareAdr( const netadr_t a, const netadr_t b )
|
|
|
|
{
|
2022-06-09 02:07:19 +02:00
|
|
|
if( a.type6 != b.type6 )
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if( a.type == NA_LOOPBACK )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( a.type == NA_IP )
|
|
|
|
{
|
2022-06-09 02:07:19 +02:00
|
|
|
if( a.ip4 == b.ip4 && a.port == b.port )
|
2018-04-13 18:23:45 +02:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
if( a.type6 == NA_IP6 )
|
|
|
|
{
|
2022-03-28 03:07:40 +02:00
|
|
|
if( a.port == b.port && !NET_NetadrIP6Compare( &a, &b ))
|
2022-03-11 10:11:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-27 22:31:55 +02:00
|
|
|
Con_DPrintf( S_ERROR "NET_CompareAdr: bad address type\n" );
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-11 22:17:28 +01:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_CompareAdrSort
|
|
|
|
|
|
|
|
Network address sorting comparator
|
2022-12-15 10:24:56 +01:00
|
|
|
guaranteed to return -1, 0 or 1
|
2022-12-11 22:17:28 +01:00
|
|
|
====================
|
|
|
|
*/
|
|
|
|
int NET_CompareAdrSort( const void *_a, const void *_b )
|
|
|
|
{
|
2022-12-15 10:24:56 +01:00
|
|
|
const netadr_t *a = _a, *b = _b;
|
|
|
|
int porta, portb, portdiff, addrdiff;
|
2022-12-11 22:17:28 +01:00
|
|
|
|
|
|
|
if( a->type6 != b->type6 )
|
2022-12-15 10:24:56 +01:00
|
|
|
return bound( -1, (int)a->type6 - (int)b->type6, 1 );
|
|
|
|
|
|
|
|
porta = ntohs( a->port );
|
|
|
|
portb = ntohs( b->port );
|
|
|
|
if( porta < portb )
|
|
|
|
portdiff = -1;
|
|
|
|
else if( porta > portb )
|
|
|
|
portdiff = 1;
|
|
|
|
else
|
|
|
|
portdiff = 0;
|
2022-12-11 22:17:28 +01:00
|
|
|
|
|
|
|
switch( a->type6 )
|
|
|
|
{
|
|
|
|
case NA_IP6:
|
2022-12-15 10:24:56 +01:00
|
|
|
if(( addrdiff = NET_NetadrIP6Compare( a, b )))
|
|
|
|
return addrdiff;
|
|
|
|
// fallthrough
|
2022-12-11 22:17:28 +01:00
|
|
|
case NA_MULTICAST_IP6:
|
|
|
|
return portdiff;
|
|
|
|
}
|
|
|
|
|
2022-12-15 10:24:56 +01:00
|
|
|
// don't check for full type earlier, as it's value depends on v6 address
|
2022-12-11 22:17:28 +01:00
|
|
|
if( a->type != b->type )
|
2022-12-15 10:24:56 +01:00
|
|
|
return bound( -1, (int)a->type - (int)b->type, 1 );
|
2022-12-11 22:17:28 +01:00
|
|
|
|
|
|
|
switch( a->type )
|
|
|
|
{
|
|
|
|
case NA_IP:
|
2022-12-15 10:24:56 +01:00
|
|
|
if(( addrdiff = memcmp( a->ip, b->ip, sizeof( a->ipx ))))
|
|
|
|
return addrdiff;
|
|
|
|
// fallthrough
|
2022-12-11 22:17:28 +01:00
|
|
|
case NA_BROADCAST:
|
2022-12-15 10:24:56 +01:00
|
|
|
return portdiff;
|
|
|
|
|
|
|
|
case NA_IPX:
|
|
|
|
if(( addrdiff = memcmp( a->ipx, b->ipx, sizeof( a->ipx ))))
|
|
|
|
return addrdiff;
|
|
|
|
// fallthrough
|
2022-12-11 22:17:28 +01:00
|
|
|
case NA_BROADCAST_IPX:
|
|
|
|
return portdiff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_IsLocalAddress
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
qboolean NET_IsLocalAddress( netadr_t adr )
|
|
|
|
{
|
|
|
|
return (adr.type == NA_LOOPBACK) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
NET_StringToAdr
|
|
|
|
|
|
|
|
idnewt
|
|
|
|
192.246.40.70
|
|
|
|
=============
|
|
|
|
*/
|
2022-03-11 10:11:49 +01:00
|
|
|
qboolean NET_StringToAdrEx( const char *string, netadr_t *adr, int family )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
struct sockaddr_storage s;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-14 02:27:52 +02:00
|
|
|
memset( adr, 0, sizeof( netadr_t ));
|
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( !Q_stricmp( string, "localhost" ) || !Q_stricmp( string, "loopback" ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
adr->type = NA_LOOPBACK;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:14:38 +02:00
|
|
|
if( NET_StringToSockaddr( string, &s, false, family ) != NET_EAI_OK )
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
NET_SockadrToNetadr( &s, adr );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
|
|
|
|
qboolean NET_StringToAdr( const char *string, netadr_t *adr )
|
|
|
|
{
|
|
|
|
return NET_StringToAdrEx( string, adr, AF_UNSPEC );
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:14:38 +02:00
|
|
|
net_gai_state_t NET_StringToAdrNB( const char *string, netadr_t *adr )
|
2018-04-14 02:27:52 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
struct sockaddr_storage s;
|
2023-04-02 23:14:38 +02:00
|
|
|
net_gai_state_t res;
|
2018-04-14 02:27:52 +02:00
|
|
|
|
|
|
|
memset( adr, 0, sizeof( netadr_t ));
|
2023-04-02 23:14:38 +02:00
|
|
|
if( !Q_stricmp( string, "localhost" ) || !Q_stricmp( string, "loopback" ))
|
2018-04-14 02:27:52 +02:00
|
|
|
{
|
|
|
|
adr->type = NA_LOOPBACK;
|
2023-09-12 19:40:14 +02:00
|
|
|
return NET_EAI_OK;
|
2018-04-14 02:27:52 +02:00
|
|
|
}
|
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
res = NET_StringToSockaddr( string, &s, true, AF_UNSPEC );
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2023-04-02 23:14:38 +02:00
|
|
|
if( res == NET_EAI_OK )
|
|
|
|
NET_SockadrToNetadr( &s, adr );
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2023-04-02 23:14:38 +02:00
|
|
|
return res;
|
2018-04-14 02:27:52 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
=============================================================================
|
|
|
|
|
|
|
|
LOOPBACK BUFFERS FOR LOCAL PLAYER
|
|
|
|
|
|
|
|
=============================================================================
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_GetLoopPacket
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
static qboolean NET_GetLoopPacket( netsrc_t sock, netadr_t *from, byte *data, size_t *length )
|
|
|
|
{
|
|
|
|
net_loopback_t *loop;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if( !data || !length )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
loop = &net.loopbacks[sock];
|
|
|
|
|
|
|
|
if( loop->send - loop->get > MAX_LOOPBACK )
|
|
|
|
loop->get = loop->send - MAX_LOOPBACK;
|
|
|
|
|
|
|
|
if( loop->get >= loop->send )
|
|
|
|
return false;
|
|
|
|
i = loop->get & MASK_LOOPBACK;
|
|
|
|
loop->get++;
|
|
|
|
|
|
|
|
memcpy( data, loop->msgs[i].data, loop->msgs[i].datalen );
|
|
|
|
*length = loop->msgs[i].datalen;
|
|
|
|
|
|
|
|
memset( from, 0, sizeof( *from ));
|
|
|
|
from->type = NA_LOOPBACK;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_SendLoopPacket
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
static void NET_SendLoopPacket( netsrc_t sock, size_t length, const void *data, netadr_t to )
|
|
|
|
{
|
|
|
|
net_loopback_t *loop;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
loop = &net.loopbacks[sock^1];
|
|
|
|
|
|
|
|
i = loop->send & MASK_LOOPBACK;
|
|
|
|
loop->send++;
|
|
|
|
|
|
|
|
memcpy( loop->msgs[i].data, data, length );
|
|
|
|
loop->msgs[i].datalen = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_ClearLoopback
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
static void NET_ClearLoopback( void )
|
|
|
|
{
|
|
|
|
net.loopbacks[0].send = net.loopbacks[0].get = 0;
|
|
|
|
net.loopbacks[1].send = net.loopbacks[1].get = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============================================================================
|
|
|
|
|
|
|
|
LAG & LOSS SIMULATION SYSTEM (network debugging)
|
|
|
|
|
|
|
|
=============================================================================
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
NET_RemoveFromPacketList
|
|
|
|
|
|
|
|
double linked list remove entry
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
static void NET_RemoveFromPacketList( packetlag_t *p )
|
|
|
|
{
|
|
|
|
p->prev->next = p->next;
|
|
|
|
p->next->prev = p->prev;
|
|
|
|
p->prev = NULL;
|
|
|
|
p->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
NET_ClearLaggedList
|
|
|
|
|
|
|
|
double linked list remove queue
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
static void NET_ClearLaggedList( packetlag_t *list )
|
|
|
|
{
|
|
|
|
packetlag_t *p, *n;
|
|
|
|
|
|
|
|
p = list->next;
|
|
|
|
while( p && p != list )
|
|
|
|
{
|
|
|
|
n = p->next;
|
|
|
|
|
|
|
|
NET_RemoveFromPacketList( p );
|
|
|
|
|
|
|
|
if( p->data )
|
|
|
|
{
|
|
|
|
Mem_Free( p->data );
|
|
|
|
p->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mem_Free( p );
|
|
|
|
p = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->prev = list;
|
|
|
|
list->next = list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
NET_AddToLagged
|
|
|
|
|
|
|
|
add lagged packet to stream
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
static void NET_AddToLagged( netsrc_t sock, packetlag_t *list, packetlag_t *packet, netadr_t *from, size_t length, const void *data, float timestamp )
|
|
|
|
{
|
|
|
|
byte *pStart;
|
|
|
|
|
|
|
|
if( packet->prev || packet->next )
|
|
|
|
return;
|
|
|
|
|
|
|
|
packet->prev = list->prev;
|
|
|
|
list->prev->next = packet;
|
|
|
|
list->prev = packet;
|
|
|
|
packet->next = list;
|
|
|
|
|
|
|
|
pStart = (byte *)Z_Malloc( length );
|
|
|
|
memcpy( pStart, data, length );
|
|
|
|
packet->data = pStart;
|
|
|
|
packet->size = length;
|
|
|
|
packet->receivedtime = timestamp;
|
|
|
|
memcpy( &packet->from, from, sizeof( netadr_t ));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
NET_AdjustLag
|
|
|
|
|
|
|
|
adjust time to next fake lag
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
static void NET_AdjustLag( void )
|
|
|
|
{
|
|
|
|
static double lasttime = 0.0;
|
|
|
|
float diff, converge;
|
|
|
|
double dt;
|
|
|
|
|
|
|
|
dt = host.realtime - lasttime;
|
|
|
|
dt = bound( 0.0, dt, 0.1 );
|
|
|
|
lasttime = host.realtime;
|
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
if( host_developer.value || !net_fakelag.value )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2023-05-19 07:22:30 +02:00
|
|
|
if( net_fakelag.value != net.fakelag )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2023-05-19 07:22:30 +02:00
|
|
|
diff = net_fakelag.value - net.fakelag;
|
2018-04-13 18:23:45 +02:00
|
|
|
converge = dt * 200.0f;
|
|
|
|
if( fabs( diff ) < converge )
|
|
|
|
converge = fabs( diff );
|
2019-10-06 05:50:32 +02:00
|
|
|
if( diff < 0.0f )
|
2018-04-13 18:23:45 +02:00
|
|
|
converge = -converge;
|
|
|
|
net.fakelag += converge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Con_Printf( "Server must enable dev-mode to activate fakelag\n" );
|
|
|
|
Cvar_SetValue( "fakelag", 0.0 );
|
|
|
|
net.fakelag = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
NET_LagPacket
|
|
|
|
|
|
|
|
add fake lagged packet into rececived message
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
static qboolean NET_LagPacket( qboolean newdata, netsrc_t sock, netadr_t *from, size_t *length, void *data )
|
|
|
|
{
|
|
|
|
packetlag_t *pNewPacketLag;
|
|
|
|
packetlag_t *pPacket;
|
|
|
|
int ninterval;
|
|
|
|
float curtime;
|
|
|
|
|
|
|
|
if( net.fakelag <= 0.0f )
|
|
|
|
{
|
|
|
|
NET_ClearLagData( true, true );
|
|
|
|
return newdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
curtime = host.realtime;
|
|
|
|
|
|
|
|
if( newdata )
|
|
|
|
{
|
2023-05-19 07:22:30 +02:00
|
|
|
if( net_fakeloss.value != 0.0f )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
if( host_developer.value )
|
|
|
|
{
|
|
|
|
net.losscount[sock]++;
|
2023-05-19 07:22:30 +02:00
|
|
|
if( net_fakeloss.value <= 0.0f )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2023-05-19 07:22:30 +02:00
|
|
|
ninterval = fabs( net_fakeloss.value );
|
2018-04-13 18:23:45 +02:00
|
|
|
if( ninterval < 2 ) ninterval = 2;
|
|
|
|
|
|
|
|
if(( net.losscount[sock] % ninterval ) == 0 )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-19 07:22:30 +02:00
|
|
|
if( COM_RandomLong( 0, 100 ) <= net_fakeloss.value )
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Cvar_SetValue( "fakeloss", 0.0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pNewPacketLag = (packetlag_t *)Z_Malloc( sizeof( packetlag_t ));
|
|
|
|
// queue packet to simulate fake lag
|
|
|
|
NET_AddToLagged( sock, &net.lagdata[sock], pNewPacketLag, from, *length, data, curtime );
|
|
|
|
}
|
|
|
|
|
|
|
|
pPacket = net.lagdata[sock].next;
|
|
|
|
|
|
|
|
while( pPacket != &net.lagdata[sock] )
|
|
|
|
{
|
2019-10-06 05:50:32 +02:00
|
|
|
if( pPacket->receivedtime <= curtime - ( net.fakelag / 1000.0f ))
|
2018-04-13 18:23:45 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
pPacket = pPacket->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pPacket == &net.lagdata[sock] )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NET_RemoveFromPacketList( pPacket );
|
|
|
|
|
|
|
|
// delivery packet from fake lag queue
|
|
|
|
memcpy( data, pPacket->data, pPacket->size );
|
|
|
|
memcpy( &net_from, &pPacket->from, sizeof( netadr_t ));
|
|
|
|
*length = pPacket->size;
|
|
|
|
|
|
|
|
if( pPacket->data )
|
|
|
|
Mem_Free( pPacket->data );
|
|
|
|
|
|
|
|
Mem_Free( pPacket );
|
|
|
|
|
2018-04-14 02:27:52 +02:00
|
|
|
return true;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
NET_GetLong
|
|
|
|
|
|
|
|
receive long packet from network
|
|
|
|
==================
|
|
|
|
*/
|
2019-07-01 03:03:11 +02:00
|
|
|
qboolean NET_GetLong( byte *pData, int size, size_t *outSize, int splitsize )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
int i, sequence_number, offset;
|
|
|
|
SPLITPACKET *pHeader = (SPLITPACKET *)pData;
|
|
|
|
int packet_number;
|
|
|
|
int packet_count;
|
|
|
|
short packet_id;
|
2019-01-29 21:44:48 +01:00
|
|
|
int body_size = splitsize - sizeof( SPLITPACKET );
|
|
|
|
|
|
|
|
if( body_size < 0 )
|
|
|
|
return false;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( size < sizeof( SPLITPACKET ))
|
|
|
|
{
|
|
|
|
Con_Printf( S_ERROR "invalid split packet length %i\n", size );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sequence_number = pHeader->sequence_number;
|
|
|
|
packet_id = pHeader->packet_id;
|
|
|
|
packet_count = ( packet_id & 0xFF );
|
|
|
|
packet_number = ( packet_id >> 8 );
|
|
|
|
|
|
|
|
if( packet_number >= NET_MAX_FRAGMENTS || packet_count > NET_MAX_FRAGMENTS )
|
|
|
|
{
|
|
|
|
Con_Printf( S_ERROR "malformed packet number (%i/%i)\n", packet_number + 1, packet_count );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( net.split.current_sequence == -1 || sequence_number != net.split.current_sequence )
|
|
|
|
{
|
|
|
|
net.split.current_sequence = sequence_number;
|
|
|
|
net.split.split_count = packet_count;
|
|
|
|
net.split.total_size = 0;
|
|
|
|
|
|
|
|
// clear part's sequence
|
|
|
|
for( i = 0; i < NET_MAX_FRAGMENTS; i++ )
|
|
|
|
net.split_flags[i] = -1;
|
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
if( net_showpackets.value == 4.0f )
|
2018-04-13 18:23:45 +02:00
|
|
|
Con_Printf( "<-- Split packet restart %i count %i seq\n", net.split.split_count, sequence_number );
|
|
|
|
}
|
|
|
|
|
|
|
|
size -= sizeof( SPLITPACKET );
|
|
|
|
|
|
|
|
if( net.split_flags[packet_number] != sequence_number )
|
|
|
|
{
|
|
|
|
if( packet_number == ( packet_count - 1 ))
|
2019-01-29 21:44:48 +01:00
|
|
|
net.split.total_size = size + body_size * ( packet_count - 1 );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
net.split.split_count--;
|
|
|
|
net.split_flags[packet_number] = sequence_number;
|
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
if( net_showpackets.value == 4.0f )
|
2018-04-13 18:23:45 +02:00
|
|
|
Con_Printf( "<-- Split packet %i of %i, %i bytes %i seq\n", packet_number + 1, packet_count, size, sequence_number );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Con_DPrintf( "NET_GetLong: Ignoring duplicated split packet %i of %i ( %i bytes )\n", packet_number + 1, packet_count, size );
|
|
|
|
}
|
|
|
|
|
2019-01-29 21:44:48 +01:00
|
|
|
offset = (packet_number * body_size);
|
2018-04-13 18:23:45 +02:00
|
|
|
memcpy( net.split.buffer + offset, pData + sizeof( SPLITPACKET ), size );
|
|
|
|
|
|
|
|
// have we received all of the pieces to the packet?
|
|
|
|
if( net.split.split_count <= 0 )
|
|
|
|
{
|
|
|
|
net.split.current_sequence = -1; // Clear packet
|
|
|
|
|
|
|
|
if( net.split.total_size > sizeof( net.split.buffer ))
|
|
|
|
{
|
|
|
|
Con_Printf( "Split packet too large! %d bytes\n", net.split.total_size );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( pData, net.split.buffer, net.split.total_size );
|
|
|
|
*outSize = net.split.total_size;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
NET_QueuePacket
|
|
|
|
|
|
|
|
queue normal and lagged packets
|
|
|
|
==================
|
|
|
|
*/
|
2022-03-11 10:11:49 +01:00
|
|
|
static qboolean NET_QueuePacket( netsrc_t sock, netadr_t *from, byte *data, size_t *length )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
byte buf[NET_MAX_FRAGMENT];
|
2022-03-20 03:19:26 +01:00
|
|
|
int ret, protocol;
|
2018-04-13 18:23:45 +02:00
|
|
|
int net_socket;
|
2019-07-01 03:03:11 +02:00
|
|
|
WSAsize_t addr_len;
|
2023-02-08 18:00:51 +01:00
|
|
|
struct sockaddr_storage addr = { 0 };
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
*length = 0;
|
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
for( protocol = 0; protocol < 2; protocol++ )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
switch( protocol )
|
|
|
|
{
|
|
|
|
case 0: net_socket = net.ip_sockets[sock]; break;
|
|
|
|
case 1: net_socket = net.ip6_sockets[sock]; break;
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( !NET_IsSocketValid( net_socket ))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
addr_len = sizeof( addr );
|
|
|
|
ret = recvfrom( net_socket, buf, sizeof( buf ), 0, (struct sockaddr *)&addr, &addr_len );
|
2022-03-11 10:11:49 +01:00
|
|
|
|
2023-01-31 02:33:27 +01:00
|
|
|
NET_SockadrToNetadr( &addr, from );
|
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( !NET_IsSocketError( ret ))
|
|
|
|
{
|
|
|
|
if( ret < NET_MAX_FRAGMENT )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-20 03:19:26 +01:00
|
|
|
// Transfer data
|
|
|
|
memcpy( data, buf, ret );
|
|
|
|
*length = ret;
|
2019-11-24 01:52:08 +01:00
|
|
|
#if !XASH_DEDICATED
|
2022-11-17 17:34:46 +01:00
|
|
|
if( CL_LegacyMode( ))
|
2022-03-11 10:11:49 +01:00
|
|
|
return NET_LagPacket( true, sock, from, length, data );
|
2022-03-20 03:19:26 +01:00
|
|
|
|
|
|
|
// check for split message
|
|
|
|
if( sock == NS_CLIENT && *(int *)data == NET_HEADER_SPLITPACKET )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-11-17 17:34:46 +01:00
|
|
|
return NET_GetLong( data, ret, length, CL_GetSplitSize( ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2022-03-20 03:19:26 +01:00
|
|
|
#endif
|
|
|
|
// lag the packet, if needed
|
|
|
|
return NET_LagPacket( true, sock, from, length, data );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-20 03:19:26 +01:00
|
|
|
Con_Reportf( "NET_QueuePacket: oversize packet from %s\n", NET_AdrToString( *from ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int err = WSAGetLastError();
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
switch( err )
|
|
|
|
{
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
case WSAECONNRESET:
|
|
|
|
case WSAECONNREFUSED:
|
|
|
|
case WSAEMSGSIZE:
|
|
|
|
case WSAETIMEDOUT:
|
|
|
|
break;
|
|
|
|
default: // let's continue even after errors
|
|
|
|
Con_DPrintf( S_ERROR "NET_QueuePacket: %s from %s\n", NET_ErrorString(), NET_AdrToString( *from ));
|
|
|
|
break;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NET_LagPacket( false, sock, from, length, data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
NET_GetPacket
|
|
|
|
|
|
|
|
Never called by the game logic, just the system event queing
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
qboolean NET_GetPacket( netsrc_t sock, netadr_t *from, byte *data, size_t *length )
|
|
|
|
{
|
|
|
|
if( !data || !length )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
NET_AdjustLag();
|
|
|
|
|
|
|
|
if( NET_GetLoopPacket( sock, from, data, length ))
|
|
|
|
{
|
|
|
|
return NET_LagPacket( true, sock, from, length, data );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NET_QueuePacket( sock, from, data, length );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
NET_SendLong
|
|
|
|
|
|
|
|
Fragment long packets, send short directly
|
|
|
|
==================
|
|
|
|
*/
|
2022-03-11 10:11:49 +01:00
|
|
|
int NET_SendLong( netsrc_t sock, int net_socket, const char *buf, size_t len, int flags, const struct sockaddr_storage *to, size_t tolen, size_t splitsize )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
#ifdef NET_USE_FRAGMENTS
|
|
|
|
// do we need to break this packet up?
|
2019-01-29 21:44:48 +01:00
|
|
|
if( splitsize > sizeof( SPLITPACKET ) && sock == NS_SERVER && len > splitsize )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2019-01-29 21:44:48 +01:00
|
|
|
char packet[SPLITPACKET_MAX_SIZE];
|
2018-04-13 18:23:45 +02:00
|
|
|
int total_sent, size, packet_count;
|
|
|
|
int ret, packet_number;
|
2019-01-29 21:44:48 +01:00
|
|
|
int body_size = splitsize - sizeof( SPLITPACKET );
|
2018-04-13 18:23:45 +02:00
|
|
|
SPLITPACKET *pPacket;
|
|
|
|
|
|
|
|
net.sequence_number++;
|
|
|
|
if( net.sequence_number <= 0 )
|
|
|
|
net.sequence_number = 1;
|
|
|
|
|
|
|
|
pPacket = (SPLITPACKET *)packet;
|
|
|
|
pPacket->sequence_number = net.sequence_number;
|
|
|
|
pPacket->net_id = NET_HEADER_SPLITPACKET;
|
|
|
|
packet_number = 0;
|
|
|
|
total_sent = 0;
|
2019-01-29 21:44:48 +01:00
|
|
|
packet_count = (len + body_size - 1) / body_size;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
while( len > 0 )
|
|
|
|
{
|
2019-01-29 21:44:48 +01:00
|
|
|
size = Q_min( body_size, len );
|
2018-04-13 18:23:45 +02:00
|
|
|
pPacket->packet_id = (packet_number << 8) + packet_count;
|
2019-01-29 21:44:48 +01:00
|
|
|
memcpy( packet + sizeof( SPLITPACKET ), buf + ( packet_number * body_size ), size );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
if( net_showpackets.value == 3.0f )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
netadr_t adr;
|
|
|
|
|
|
|
|
memset( &adr, 0, sizeof( adr ));
|
2022-03-11 10:11:49 +01:00
|
|
|
NET_SockadrToNetadr( to, &adr );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
Con_Printf( "Sending split %i of %i with %i bytes and seq %i to %s\n",
|
|
|
|
packet_number + 1, packet_count, size, net.sequence_number, NET_AdrToString( adr ));
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
ret = sendto( net_socket, packet, size + sizeof( SPLITPACKET ), flags, (const struct sockaddr *)to, tolen );
|
2018-04-13 18:23:45 +02:00
|
|
|
if( ret < 0 ) return ret; // error
|
|
|
|
|
|
|
|
if( ret >= size )
|
|
|
|
total_sent += size;
|
|
|
|
len -= size;
|
|
|
|
packet_number++;
|
2018-04-22 16:17:35 +02:00
|
|
|
Sys_Sleep( 1 );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return total_sent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// no fragmenantion for client connection
|
2022-03-11 10:11:49 +01:00
|
|
|
return sendto( net_socket, buf, len, flags, (const struct sockaddr *)to, tolen );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
2019-01-29 21:44:48 +01:00
|
|
|
NET_SendPacketEx
|
2018-04-13 18:23:45 +02:00
|
|
|
==================
|
|
|
|
*/
|
2019-01-29 21:44:48 +01:00
|
|
|
void NET_SendPacketEx( netsrc_t sock, size_t length, const void *data, netadr_t to, size_t splitsize )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-04-21 01:50:50 +02:00
|
|
|
int ret;
|
2023-02-08 18:00:51 +01:00
|
|
|
struct sockaddr_storage addr = { 0 };
|
2019-12-23 04:48:17 +01:00
|
|
|
SOCKET net_socket = 0;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( !net.initialized || to.type == NA_LOOPBACK )
|
|
|
|
{
|
|
|
|
NET_SendLoopPacket( sock, length, data, to );
|
|
|
|
return;
|
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
else if( to.type == NA_BROADCAST || to.type == NA_IP )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
net_socket = net.ip_sockets[sock];
|
2022-03-11 10:11:49 +01:00
|
|
|
if( !NET_IsSocketValid( net_socket ))
|
2018-04-13 18:23:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
else if( to.type6 == NA_MULTICAST_IP6 || to.type6 == NA_IP6 )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
net_socket = net.ip6_sockets[sock];
|
|
|
|
if( !NET_IsSocketValid( net_socket ))
|
2018-04-13 18:23:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
Host_Error( "NET_SendPacket: bad address type %i (%i)\n", to.type, to.type6 );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NET_NetadrToSockadr( &to, &addr );
|
|
|
|
|
2023-02-05 03:39:32 +01:00
|
|
|
ret = NET_SendLong( sock, net_socket, data, length, 0, &addr, NET_SockAddrLen( &addr ), splitsize );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-21 01:50:50 +02:00
|
|
|
if( NET_IsSocketError( ret ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2019-07-01 03:03:11 +02:00
|
|
|
int err = WSAGetLastError();
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-06-01 18:29:47 +02:00
|
|
|
// WSAEWOULDBLOCK is silent
|
|
|
|
if( err == WSAEWOULDBLOCK )
|
|
|
|
return;
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2018-06-01 18:29:47 +02:00
|
|
|
// some PPP links don't allow broadcasts
|
2022-03-11 10:11:49 +01:00
|
|
|
if( err == WSAEADDRNOTAVAIL && ( to.type == NA_BROADCAST || to.type6 == NA_MULTICAST_IP6 ))
|
2018-06-01 18:29:47 +02:00
|
|
|
return;
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( Host_IsDedicated( ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-10-27 22:31:55 +02:00
|
|
|
Con_DPrintf( S_ERROR "NET_SendPacket: %s to %s\n", NET_ErrorString(), NET_AdrToString( to ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
else if( err == WSAEADDRNOTAVAIL || err == WSAENOBUFS )
|
|
|
|
{
|
2018-10-27 22:31:55 +02:00
|
|
|
Con_DPrintf( S_ERROR "NET_SendPacket: %s to %s\n", NET_ErrorString(), NET_AdrToString( to ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-01 21:17:13 +02:00
|
|
|
Con_Printf( S_ERROR "NET_SendPacket: %s to %s\n", NET_ErrorString(), NET_AdrToString( to ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-14 02:27:52 +02:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 21:44:48 +01:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
NET_SendPacket
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void NET_SendPacket( netsrc_t sock, size_t length, const void *data, netadr_t to )
|
|
|
|
{
|
2019-06-29 21:01:32 +02:00
|
|
|
NET_SendPacketEx( sock, length, data, to, 0 );
|
2019-01-29 21:44:48 +01:00
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
====================
|
2022-03-10 02:51:38 +01:00
|
|
|
NET_IPSocket
|
2018-04-13 18:23:45 +02:00
|
|
|
====================
|
|
|
|
*/
|
2022-03-20 03:19:26 +01:00
|
|
|
static int NET_IPSocket( const char *net_iface, int port, int family )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2023-02-08 18:00:51 +01:00
|
|
|
struct sockaddr_storage addr = { 0 };
|
2018-04-13 18:23:45 +02:00
|
|
|
int err, net_socket;
|
|
|
|
uint optval = 1;
|
2018-04-14 02:27:52 +02:00
|
|
|
dword _true = 1;
|
2022-12-02 19:21:53 +01:00
|
|
|
int pfamily = PF_INET;
|
2022-03-20 03:19:26 +01:00
|
|
|
|
|
|
|
if( family == AF_INET6 )
|
|
|
|
pfamily = PF_INET6;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( NET_IsSocketError(( net_socket = socket( pfamily, SOCK_DGRAM, IPPROTO_UDP ))))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2019-07-01 03:03:11 +02:00
|
|
|
err = WSAGetLastError();
|
2018-04-13 18:23:45 +02:00
|
|
|
if( err != WSAEAFNOSUPPORT )
|
2022-03-20 03:19:26 +01:00
|
|
|
Con_DPrintf( S_WARN "NET_UDPSocket: port: %d socket: %s\n", port, NET_ErrorString( ));
|
2018-04-13 18:23:45 +02:00
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( NET_IsSocketError( ioctlsocket( net_socket, FIONBIO, (void*)&_true )))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2020-02-08 16:45:50 +01:00
|
|
|
struct timeval timeout;
|
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
Con_DPrintf( S_WARN "NET_UDPSocket: port: %d ioctl FIONBIO: %s\n", port, NET_ErrorString( ));
|
2020-02-08 16:45:50 +01:00
|
|
|
// try timeout instead of NBIO
|
|
|
|
timeout.tv_sec = timeout.tv_usec = 0;
|
|
|
|
setsockopt( net_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// make it broadcast capable
|
2022-11-17 17:34:46 +01:00
|
|
|
if( NET_IsSocketError( setsockopt( net_socket, SOL_SOCKET, SO_BROADCAST, (char *)&_true, sizeof( _true ))))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-20 03:19:26 +01:00
|
|
|
Con_DPrintf( S_WARN "NET_UDPSocket: port: %d setsockopt SO_BROADCAST: %s\n", port, NET_ErrorString( ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( NET_IsSocketError( setsockopt( net_socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&optval, sizeof( optval ))))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-20 03:19:26 +01:00
|
|
|
Con_DPrintf( S_WARN "NET_UDPSocket: port: %d setsockopt SO_REUSEADDR: %s\n", port, NET_ErrorString( ));
|
|
|
|
closesocket( net_socket );
|
|
|
|
return INVALID_SOCKET;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
addr.ss_family = family;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( family == AF_INET6 )
|
|
|
|
{
|
|
|
|
if( NET_IsSocketError( setsockopt( net_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&_true, sizeof( _true ))))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-20 03:19:26 +01:00
|
|
|
Con_DPrintf( S_WARN "NET_UDPSocket: port: %d setsockopt IPV6_V6ONLY: %s\n", port, NET_ErrorString( ));
|
2019-07-30 17:41:24 +02:00
|
|
|
closesocket( net_socket );
|
2018-04-13 18:23:45 +02:00
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( Sys_CheckParm( "-loopback" ))
|
2022-03-11 10:11:49 +01:00
|
|
|
{
|
2022-03-20 03:19:26 +01:00
|
|
|
if( NET_IsSocketError( setsockopt( net_socket, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (char *)&_true, sizeof( _true ))))
|
|
|
|
Con_DPrintf( S_WARN "NET_UDPSocket: port %d setsockopt IPV6_MULTICAST_LOOP: %s\n", port, NET_ErrorString( ));
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( COM_CheckStringEmpty( net_iface ) && Q_stricmp( net_iface, "localhost" ))
|
|
|
|
NET_StringToSockaddr( net_iface, &addr, false, AF_INET6 );
|
|
|
|
else memcpy(((struct sockaddr_in6 *)&addr)->sin6_addr.s6_addr, &in6addr_any, sizeof( struct in6_addr ));
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
if( port == PORT_ANY ) ((struct sockaddr_in6 *)&addr)->sin6_port = 0;
|
|
|
|
else ((struct sockaddr_in6 *)&addr)->sin6_port = htons((short)port);
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2023-02-05 03:39:32 +01:00
|
|
|
if( NET_IsSocketError( bind( net_socket, (struct sockaddr *)&addr, sizeof( struct sockaddr_in6 ))))
|
2022-03-20 03:19:26 +01:00
|
|
|
{
|
|
|
|
Con_DPrintf( S_WARN "NET_UDPSocket: port: %d bind6: %s\n", port, NET_ErrorString( ));
|
|
|
|
closesocket( net_socket );
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
2022-03-20 03:19:26 +01:00
|
|
|
else if( family == AF_INET )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-20 03:19:26 +01:00
|
|
|
if( Sys_CheckParm( "-tos" ))
|
|
|
|
{
|
|
|
|
optval = 0x10; // IPTOS_LOWDELAY
|
|
|
|
Con_Printf( "Enabling LOWDELAY TOS option\n" );
|
2022-03-11 10:11:49 +01:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( NET_IsSocketError( setsockopt( net_socket, IPPROTO_IP, IP_TOS, (const char *)&optval, sizeof( optval ))))
|
|
|
|
{
|
|
|
|
err = WSAGetLastError();
|
|
|
|
if( err != WSAENOPROTOOPT )
|
|
|
|
Con_Printf( S_WARN "NET_UDPSocket: port: %d setsockopt IP_TOS: %s\n", port, NET_ErrorString( ));
|
|
|
|
closesocket( net_socket );
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( Sys_CheckParm( "-loopback" ))
|
|
|
|
{
|
|
|
|
if( NET_IsSocketError( setsockopt( net_socket, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&_true, sizeof( _true ))))
|
|
|
|
Con_DPrintf( S_WARN "NET_UDPSocket: port %d setsockopt IP_MULTICAST_LOOP: %s\n", port, NET_ErrorString( ));
|
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( COM_CheckStringEmpty( net_iface ) && Q_stricmp( net_iface, "localhost" ))
|
|
|
|
NET_StringToSockaddr( net_iface, &addr, false, AF_INET );
|
|
|
|
else ((struct sockaddr_in *)&addr)->sin_addr.s_addr = INADDR_ANY;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( port == PORT_ANY ) ((struct sockaddr_in *)&addr)->sin_port = 0;
|
|
|
|
else ((struct sockaddr_in *)&addr)->sin_port = htons((short)port);
|
|
|
|
|
2023-02-05 03:39:32 +01:00
|
|
|
if( NET_IsSocketError( bind( net_socket, (struct sockaddr *)&addr, sizeof( struct sockaddr_in ))))
|
2022-03-20 03:19:26 +01:00
|
|
|
{
|
|
|
|
Con_DPrintf( S_WARN "NET_UDPSocket: port: %d bind: %s\n", port, NET_ErrorString( ));
|
|
|
|
closesocket( net_socket );
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return net_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_OpenIP
|
|
|
|
====================
|
|
|
|
*/
|
2022-11-17 17:34:46 +01:00
|
|
|
static void NET_OpenIP( qboolean change_port, int *sockets, const char *net_iface, int hostport, int clientport, int family )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
int port;
|
2022-07-10 00:07:25 +02:00
|
|
|
qboolean sv_nat = Cvar_VariableInteger("sv_nat");
|
|
|
|
qboolean cl_nat = Cvar_VariableInteger("cl_nat");
|
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
if( change_port && ( FBitSet( net_hostport.flags, FCVAR_CHANGED ) || sv_nat ))
|
2022-07-10 00:07:25 +02:00
|
|
|
{
|
|
|
|
// reopen socket to set random port
|
2022-11-17 17:34:46 +01:00
|
|
|
if( NET_IsSocketValid( sockets[NS_SERVER] ))
|
|
|
|
closesocket( sockets[NS_SERVER] );
|
|
|
|
|
|
|
|
sockets[NS_SERVER] = INVALID_SOCKET;
|
2023-05-19 07:22:30 +02:00
|
|
|
ClearBits( net_hostport.flags, FCVAR_CHANGED );
|
2022-07-10 00:07:25 +02:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( !NET_IsSocketValid( sockets[NS_SERVER] ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-30 00:03:43 +01:00
|
|
|
port = hostport;
|
2022-07-10 00:07:25 +02:00
|
|
|
if( !port )
|
|
|
|
{
|
2023-05-19 07:22:30 +02:00
|
|
|
port = sv_nat ? PORT_ANY : net_hostport.value;
|
2022-07-10 00:07:25 +02:00
|
|
|
|
|
|
|
if( !port )
|
|
|
|
port = PORT_SERVER; // forcing to default
|
|
|
|
}
|
2022-03-20 03:19:26 +01:00
|
|
|
sockets[NS_SERVER] = NET_IPSocket( net_iface, port, family );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( !NET_IsSocketValid( sockets[NS_SERVER] ) && Host_IsDedicated( ))
|
2022-12-30 00:03:43 +01:00
|
|
|
return;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// dedicated servers don't need client ports
|
2022-11-17 17:34:46 +01:00
|
|
|
if( Host_IsDedicated( )) return;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
if( change_port && ( FBitSet( net_clientport.flags, FCVAR_CHANGED ) || cl_nat ))
|
2022-07-10 00:07:25 +02:00
|
|
|
{
|
|
|
|
// reopen socket to set random port
|
2022-11-17 17:34:46 +01:00
|
|
|
if( NET_IsSocketValid( sockets[NS_CLIENT] ))
|
|
|
|
closesocket( sockets[NS_CLIENT] );
|
2022-07-10 00:07:25 +02:00
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
sockets[NS_CLIENT] = INVALID_SOCKET;
|
2023-05-19 07:22:30 +02:00
|
|
|
ClearBits( net_clientport.flags, FCVAR_CHANGED );
|
2022-07-10 00:07:25 +02:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( !NET_IsSocketValid( sockets[NS_CLIENT] ))
|
2022-03-11 10:11:49 +01:00
|
|
|
{
|
2022-12-30 00:03:43 +01:00
|
|
|
port = clientport;
|
2022-07-10 00:07:25 +02:00
|
|
|
if( !port )
|
|
|
|
{
|
2023-05-19 07:22:30 +02:00
|
|
|
port = cl_nat ? PORT_ANY : net_clientport.value;
|
2022-07-10 00:07:25 +02:00
|
|
|
|
|
|
|
if( !port )
|
|
|
|
port = PORT_ANY; // forcing to default
|
|
|
|
}
|
2022-03-20 03:19:26 +01:00
|
|
|
sockets[NS_CLIENT] = NET_IPSocket( net_iface, port, family );
|
2022-03-11 10:11:49 +01:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( !NET_IsSocketValid( sockets[NS_CLIENT] ))
|
2023-05-19 07:22:30 +02:00
|
|
|
sockets[NS_CLIENT] = NET_IPSocket( net_ipname.string, PORT_ANY, family );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2022-12-30 00:03:43 +01:00
|
|
|
|
|
|
|
return;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
NET_GetLocalAddress
|
|
|
|
|
|
|
|
Returns the servers' ip address as a string.
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
void NET_GetLocalAddress( void )
|
|
|
|
{
|
2022-03-20 03:19:26 +01:00
|
|
|
char hostname[512];
|
2018-04-13 18:23:45 +02:00
|
|
|
char buff[512];
|
2022-03-11 10:11:49 +01:00
|
|
|
struct sockaddr_storage address;
|
2019-07-01 03:03:11 +02:00
|
|
|
WSAsize_t namelen;
|
2022-12-04 16:08:28 +01:00
|
|
|
const char *net_addr_string;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
memset( &net_local, 0, sizeof( netadr_t ));
|
2022-03-11 10:11:49 +01:00
|
|
|
memset( &net6_local, 0, sizeof( netadr_t ));
|
2022-03-20 03:19:26 +01:00
|
|
|
|
|
|
|
if( !net.allow_ip && !net.allow_ip6 )
|
|
|
|
{
|
|
|
|
Con_Printf( "TCP/IP Disabled.\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gethostname( hostname, sizeof( hostname ));
|
|
|
|
hostname[sizeof(hostname) - 1] = 0;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( net.allow_ip )
|
|
|
|
{
|
|
|
|
// If we have changed the ip var from the command line, use that instead.
|
2023-05-19 07:22:30 +02:00
|
|
|
if( Q_stricmp( net_ipname.string, "localhost" ))
|
|
|
|
Q_strncpy( buff, net_ipname.string, sizeof( buff ));
|
2022-03-20 03:19:26 +01:00
|
|
|
else Q_strncpy( buff, hostname, sizeof( buff ));
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
if( NET_StringToAdrEx( buff, &net_local, AF_INET ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-20 03:19:26 +01:00
|
|
|
namelen = sizeof( struct sockaddr_in );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( !NET_IsSocketError( getsockname( net.ip_sockets[NS_SERVER], (struct sockaddr *)&address, &namelen )))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
net_local.port = ((struct sockaddr_in *)&address)->sin_port;
|
2022-12-04 16:08:28 +01:00
|
|
|
net_addr_string = NET_AdrToString( net_local );
|
|
|
|
Con_Printf( "Server IPv4 address %s\n", net_addr_string );
|
2023-05-19 07:22:30 +02:00
|
|
|
Cvar_FullSet( "net_address", net_addr_string, net_address.flags );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2022-03-20 03:19:26 +01:00
|
|
|
else Con_DPrintf( S_ERROR "Could not get TCP/IPv4 address. Reason: %s\n", NET_ErrorString( ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2022-03-20 03:19:26 +01:00
|
|
|
else Con_DPrintf( S_ERROR "Could not get TCP/IPv4 address, Invalid hostname: '%s'\n", buff );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
|
|
|
|
if( net.allow_ip6 )
|
|
|
|
{
|
|
|
|
// If we have changed the ip var from the command line, use that instead.
|
2023-05-19 07:22:30 +02:00
|
|
|
if( Q_stricmp( net_ip6name.string, "localhost" ))
|
|
|
|
Q_strncpy( buff, net_ip6name.string, sizeof( buff ));
|
2022-03-20 03:19:26 +01:00
|
|
|
else Q_strncpy( buff, hostname, sizeof( buff ));
|
2022-03-11 10:11:49 +01:00
|
|
|
|
|
|
|
if( NET_StringToAdrEx( buff, &net6_local, AF_INET6 ))
|
|
|
|
{
|
2022-03-20 03:19:26 +01:00
|
|
|
namelen = sizeof( struct sockaddr_in6 );
|
2022-03-11 10:11:49 +01:00
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
if( !NET_IsSocketError( getsockname( net.ip6_sockets[NS_SERVER], (struct sockaddr *)&address, &namelen )))
|
2022-03-11 10:11:49 +01:00
|
|
|
{
|
|
|
|
net6_local.port = ((struct sockaddr_in6 *)&address)->sin6_port;
|
2022-12-04 16:08:28 +01:00
|
|
|
net_addr_string = NET_AdrToString( net6_local );
|
|
|
|
Con_Printf( "Server IPv6 address %s\n", net_addr_string );
|
2023-05-19 07:22:30 +02:00
|
|
|
Cvar_FullSet( "net6_address", net_addr_string, net6_address.flags );
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
2022-03-20 03:19:26 +01:00
|
|
|
else Con_DPrintf( S_ERROR "Could not get TCP/IPv6 address. Reason: %s\n", NET_ErrorString( ));
|
2022-03-11 10:11:49 +01:00
|
|
|
}
|
2022-03-20 03:19:26 +01:00
|
|
|
else Con_DPrintf( S_ERROR "Could not get TCP/IPv6 address, Invalid hostname: '%s'\n", buff );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_Config
|
|
|
|
|
|
|
|
A single player game will only use the loopback code
|
|
|
|
====================
|
|
|
|
*/
|
2022-07-10 00:04:13 +02:00
|
|
|
void NET_Config( qboolean multiplayer, qboolean changeport )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
static qboolean bFirst = true;
|
|
|
|
static qboolean old_config;
|
|
|
|
|
|
|
|
if( !net.initialized )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( old_config == multiplayer )
|
|
|
|
return;
|
|
|
|
|
|
|
|
old_config = multiplayer;
|
|
|
|
|
|
|
|
if( multiplayer )
|
2021-01-03 02:28:45 +01:00
|
|
|
{
|
2018-04-13 18:23:45 +02:00
|
|
|
// open sockets
|
2022-03-20 03:19:26 +01:00
|
|
|
if( net.allow_ip )
|
2023-05-19 07:22:30 +02:00
|
|
|
NET_OpenIP( changeport, net.ip_sockets, net_ipname.string, net_iphostport.value, net_ipclientport.value, AF_INET );
|
2022-03-20 03:19:26 +01:00
|
|
|
|
|
|
|
if( net.allow_ip6 )
|
2023-05-19 07:22:30 +02:00
|
|
|
NET_OpenIP( changeport, net.ip6_sockets, net_ip6name.string, net_ip6hostport.value, net_ip6clientport.value, AF_INET6 );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-30 00:03:43 +01:00
|
|
|
// validate sockets for dedicated
|
|
|
|
if( Host_IsDedicated( ))
|
|
|
|
{
|
|
|
|
qboolean nov4, nov6;
|
|
|
|
nov4 = net.allow_ip && NET_IsSocketError( net.ip_sockets[NS_SERVER] );
|
|
|
|
nov6 = net.allow_ip6 && NET_IsSocketError( net.ip6_sockets[NS_SERVER] );
|
|
|
|
|
|
|
|
if( nov4 && nov6 )
|
2023-04-17 03:37:16 +02:00
|
|
|
Host_Error( "Couldn't allocate IPv4 and IPv6 server ports.\n" );
|
2022-12-30 00:03:43 +01:00
|
|
|
else if( nov4 && !nov6 )
|
2023-04-17 03:37:16 +02:00
|
|
|
Con_Printf( S_ERROR "Couldn't allocate IPv4 server port\n" );
|
2022-12-30 00:03:43 +01:00
|
|
|
else if( !nov4 && nov6 )
|
2023-04-17 03:37:16 +02:00
|
|
|
Con_Printf( S_ERROR "Couldn't allocate IPv6 server_port\n" );
|
2022-12-30 00:03:43 +01:00
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// get our local address, if possible
|
|
|
|
if( bFirst )
|
|
|
|
{
|
|
|
|
NET_GetLocalAddress();
|
|
|
|
bFirst = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2021-01-03 02:28:45 +01:00
|
|
|
{
|
2018-04-13 18:23:45 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// shut down any existing sockets
|
|
|
|
for( i = 0; i < NS_COUNT; i++ )
|
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
if( NET_IsSocketValid( net.ip_sockets[i] ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2019-07-30 17:41:24 +02:00
|
|
|
closesocket( net.ip_sockets[i] );
|
2018-04-13 18:23:45 +02:00
|
|
|
net.ip_sockets[i] = INVALID_SOCKET;
|
|
|
|
}
|
2022-03-11 10:11:49 +01:00
|
|
|
|
|
|
|
if( NET_IsSocketValid( net.ip6_sockets[i] ))
|
|
|
|
{
|
|
|
|
closesocket( net.ip6_sockets[i] );
|
|
|
|
net.ip6_sockets[i] = INVALID_SOCKET;
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NET_ClearLoopback ();
|
|
|
|
|
|
|
|
net.configured = multiplayer ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_IsConfigured
|
|
|
|
|
|
|
|
Is winsock ip initialized?
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
qboolean NET_IsConfigured( void )
|
|
|
|
{
|
|
|
|
return net.configured;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_IsActive
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
qboolean NET_IsActive( void )
|
|
|
|
{
|
|
|
|
return net.initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_Sleep
|
|
|
|
|
|
|
|
sleeps msec or until net socket is ready
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_Sleep( int msec )
|
|
|
|
{
|
2020-02-08 16:45:50 +01:00
|
|
|
#ifndef XASH_NO_NETWORK
|
2018-04-13 18:23:45 +02:00
|
|
|
struct timeval timeout;
|
|
|
|
fd_set fdset;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if( !net.initialized || host.type == HOST_NORMAL )
|
|
|
|
return; // we're not a dedicated server, just run full speed
|
|
|
|
|
|
|
|
FD_ZERO( &fdset );
|
|
|
|
|
|
|
|
if( net.ip_sockets[NS_SERVER] != INVALID_SOCKET )
|
|
|
|
{
|
|
|
|
FD_SET( net.ip_sockets[NS_SERVER], &fdset ); // network socket
|
|
|
|
i = net.ip_sockets[NS_SERVER];
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout.tv_sec = msec / 1000;
|
|
|
|
timeout.tv_usec = (msec % 1000) * 1000;
|
2019-07-01 03:03:11 +02:00
|
|
|
select( i+1, &fdset, NULL, NULL, &timeout );
|
2020-02-08 16:45:50 +01:00
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_ClearLagData
|
|
|
|
|
|
|
|
clear fakelag list
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_ClearLagData( qboolean bClient, qboolean bServer )
|
|
|
|
{
|
|
|
|
if( bClient ) NET_ClearLaggedList( &net.lagdata[NS_CLIENT] );
|
|
|
|
if( bServer ) NET_ClearLaggedList( &net.lagdata[NS_SERVER] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_Init
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_Init( void )
|
|
|
|
{
|
|
|
|
char cmd[64];
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
if( net.initialized ) return;
|
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
Cvar_RegisterVariable( &net_address );
|
|
|
|
Cvar_RegisterVariable( &net_ipname );
|
|
|
|
Cvar_RegisterVariable( &net_iphostport );
|
|
|
|
Cvar_RegisterVariable( &net_hostport );
|
|
|
|
Cvar_RegisterVariable( &net_ipclientport );
|
|
|
|
Cvar_RegisterVariable( &net_clientport );
|
|
|
|
Cvar_RegisterVariable( &net_fakelag );
|
|
|
|
Cvar_RegisterVariable( &net_fakeloss );
|
|
|
|
|
|
|
|
Q_snprintf( cmd, sizeof( cmd ), "%i", PORT_SERVER );
|
2023-07-22 04:50:35 +02:00
|
|
|
Cvar_FullSet( "hostport", cmd, FCVAR_READ_ONLY );
|
2023-05-19 07:22:30 +02:00
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
// cvar equivalents for IPv6
|
2023-05-19 07:22:30 +02:00
|
|
|
Cvar_RegisterVariable( &net_ip6name );
|
|
|
|
Cvar_RegisterVariable( &net_ip6hostport );
|
|
|
|
Cvar_RegisterVariable( &net_ip6clientport );
|
|
|
|
Cvar_RegisterVariable( &net6_address );
|
2022-03-11 10:11:49 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// prepare some network data
|
|
|
|
for( i = 0; i < NS_COUNT; i++ )
|
|
|
|
{
|
|
|
|
net.lagdata[i].prev = &net.lagdata[i];
|
|
|
|
net.lagdata[i].next = &net.lagdata[i];
|
2022-03-20 03:19:26 +01:00
|
|
|
net.ip_sockets[i] = INVALID_SOCKET;
|
2022-03-11 10:11:49 +01:00
|
|
|
net.ip6_sockets[i] = INVALID_SOCKET;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2019-11-24 01:52:08 +01:00
|
|
|
#if XASH_WIN32
|
2022-11-17 17:34:46 +01:00
|
|
|
if( WSAStartup( MAKEWORD( 1, 1 ), &net.winsockdata ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-10-27 22:31:55 +02:00
|
|
|
Con_DPrintf( S_ERROR "network initialization failed.\n" );
|
2018-04-13 18:23:45 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-01-13 06:09:16 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CAN_ASYNC_NS_RESOLVE
|
|
|
|
NET_InitializeCriticalSections();
|
2018-04-14 02:27:52 +02:00
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
net.allow_ip = !Sys_CheckParm( "-noip" );
|
|
|
|
net.allow_ip6 = !Sys_CheckParm( "-noip6" );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
// specify custom host port
|
|
|
|
if( Sys_GetParmFromCmdLine( "-port", cmd ) && Q_isdigit( cmd ))
|
|
|
|
Cvar_FullSet( "hostport", cmd, FCVAR_READ_ONLY );
|
|
|
|
|
2022-03-20 03:19:26 +01:00
|
|
|
// specify custom IPv6 host port
|
|
|
|
if( Sys_GetParmFromCmdLine( "-port6", cmd ) && Q_isdigit( cmd ))
|
|
|
|
Cvar_FullSet( "ip6_hostport", cmd, FCVAR_READ_ONLY );
|
|
|
|
|
2018-11-01 23:06:23 +01:00
|
|
|
// specify custom ip
|
|
|
|
if( Sys_GetParmFromCmdLine( "-ip", cmd ))
|
2023-05-19 07:22:30 +02:00
|
|
|
Cvar_FullSet( "ip", cmd, net_ipname.flags );
|
2018-11-01 23:06:23 +01:00
|
|
|
|
2022-03-11 10:11:49 +01:00
|
|
|
// specify custom ip6
|
|
|
|
if( Sys_GetParmFromCmdLine( "-ip6", cmd ))
|
2023-05-19 07:22:30 +02:00
|
|
|
Cvar_FullSet( "ip6", cmd, net_ip6name.flags );
|
2022-03-11 10:11:49 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// adjust clockwindow
|
|
|
|
if( Sys_GetParmFromCmdLine( "-clockwindow", cmd ))
|
|
|
|
Cvar_SetValue( "clockwindow", Q_atof( cmd ));
|
|
|
|
|
|
|
|
net.sequence_number = 1;
|
|
|
|
net.initialized = true;
|
2018-10-27 22:31:55 +02:00
|
|
|
Con_Reportf( "Base networking initialized.\n" );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
NET_Shutdown
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void NET_Shutdown( void )
|
|
|
|
{
|
|
|
|
if( !net.initialized )
|
|
|
|
return;
|
|
|
|
|
|
|
|
NET_ClearLagData( true, true );
|
|
|
|
|
2022-07-10 00:04:13 +02:00
|
|
|
NET_Config( false, false );
|
2019-11-24 01:52:08 +01:00
|
|
|
#if XASH_WIN32
|
2019-06-29 21:01:32 +02:00
|
|
|
WSACleanup();
|
2018-04-14 02:27:52 +02:00
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
net.initialized = false;
|
2018-04-14 02:27:52 +02:00
|
|
|
}
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================================================
|
|
|
|
|
|
|
|
HTTP downloader
|
|
|
|
|
|
|
|
=================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct httpserver_s
|
|
|
|
{
|
|
|
|
char host[256];
|
|
|
|
int port;
|
2019-03-19 23:18:26 +01:00
|
|
|
char path[MAX_SYSPATH];
|
2019-02-01 23:15:59 +01:00
|
|
|
qboolean needfree;
|
|
|
|
struct httpserver_s *next;
|
|
|
|
|
|
|
|
} httpserver_t;
|
|
|
|
|
|
|
|
enum connectionstate
|
|
|
|
{
|
|
|
|
HTTP_QUEUE = 0,
|
|
|
|
HTTP_OPENED,
|
|
|
|
HTTP_SOCKET,
|
|
|
|
HTTP_NS_RESOLVED,
|
|
|
|
HTTP_CONNECTED,
|
|
|
|
HTTP_REQUEST,
|
|
|
|
HTTP_REQUEST_SENT,
|
|
|
|
HTTP_RESPONSE_RECEIVED,
|
|
|
|
HTTP_FREE
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct httpfile_s
|
|
|
|
{
|
|
|
|
struct httpfile_s *next;
|
|
|
|
httpserver_t *server;
|
2019-03-19 23:18:26 +01:00
|
|
|
char path[MAX_SYSPATH];
|
2019-02-01 23:15:59 +01:00
|
|
|
file_t *file;
|
|
|
|
int socket;
|
|
|
|
int size;
|
|
|
|
int downloaded;
|
|
|
|
int lastchecksize;
|
|
|
|
float checktime;
|
|
|
|
float blocktime;
|
|
|
|
int id;
|
|
|
|
enum connectionstate state;
|
|
|
|
qboolean process;
|
|
|
|
|
|
|
|
// query or response
|
2019-09-28 20:41:41 +02:00
|
|
|
char buf[BUFSIZ+1];
|
2019-02-01 23:15:59 +01:00
|
|
|
int header_size, query_length, bytes_sent;
|
|
|
|
} httpfile_t;
|
|
|
|
|
|
|
|
static struct http_static_s
|
|
|
|
{
|
|
|
|
// file and server lists
|
|
|
|
httpfile_t *first_file, *last_file;
|
|
|
|
httpserver_t *first_server, *last_server;
|
|
|
|
} http;
|
|
|
|
|
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
static CVAR_DEFINE_AUTO( http_useragent, "", FCVAR_ARCHIVE | FCVAR_PRIVILEGED, "User-Agent string" );
|
|
|
|
static CVAR_DEFINE_AUTO( http_autoremove, "1", FCVAR_ARCHIVE | FCVAR_PRIVILEGED, "remove broken files" );
|
|
|
|
static CVAR_DEFINE_AUTO( http_timeout, "45", FCVAR_ARCHIVE | FCVAR_PRIVILEGED, "timeout for http downloader" );
|
|
|
|
static CVAR_DEFINE_AUTO( http_maxconnections, "4", FCVAR_ARCHIVE | FCVAR_PRIVILEGED, "maximum http connection number" );
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
========================
|
|
|
|
HTTP_ClearCustomServers
|
|
|
|
========================
|
|
|
|
*/
|
|
|
|
void HTTP_ClearCustomServers( void )
|
|
|
|
{
|
|
|
|
if( http.first_file )
|
|
|
|
return; // may be referenced
|
|
|
|
|
|
|
|
while( http.first_server && http.first_server->needfree )
|
|
|
|
{
|
|
|
|
httpserver_t *tmp = http.first_server;
|
|
|
|
|
|
|
|
http.first_server = http.first_server->next;
|
|
|
|
Mem_Free( tmp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
HTTP_FreeFile
|
|
|
|
|
|
|
|
Skip to next server/file
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
static void HTTP_FreeFile( httpfile_t *file, qboolean error )
|
|
|
|
{
|
|
|
|
char incname[256];
|
|
|
|
|
|
|
|
// Allways close file and socket
|
|
|
|
if( file->file )
|
|
|
|
FS_Close( file->file );
|
|
|
|
|
|
|
|
file->file = NULL;
|
|
|
|
|
|
|
|
if( file->socket != -1 )
|
2019-07-30 17:41:24 +02:00
|
|
|
closesocket( file->socket );
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
file->socket = -1;
|
|
|
|
|
2019-09-28 20:41:41 +02:00
|
|
|
Q_snprintf( incname, 256, "downloaded/%s.incomplete", file->path );
|
2019-02-01 23:15:59 +01:00
|
|
|
if( error )
|
|
|
|
{
|
|
|
|
// Switch to next fastdl server if present
|
2022-11-17 17:34:46 +01:00
|
|
|
if( file->server && ( file->state > HTTP_QUEUE ) && ( file->state != HTTP_FREE ))
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
|
|
|
file->server = file->server->next;
|
|
|
|
file->state = HTTP_QUEUE; // Reset download state, HTTP_Run() will open file again
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called because there was no servers to download, free file now
|
2023-05-19 07:22:30 +02:00
|
|
|
if( http_autoremove.value == 1 ) // remove broken file
|
2019-02-01 23:15:59 +01:00
|
|
|
FS_Delete( incname );
|
|
|
|
else // autoremove disabled, keep file
|
|
|
|
Con_Printf( "cannot download %s from any server. "
|
|
|
|
"You may remove %s now\n", file->path, incname ); // Warn about trash file
|
|
|
|
|
|
|
|
if( file->process )
|
|
|
|
CL_ProcessFile( false, file->path ); // Process file, increase counter
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Success, rename and process file
|
|
|
|
char name[256];
|
|
|
|
|
2019-09-28 20:41:41 +02:00
|
|
|
Q_snprintf( name, 256, "downloaded/%s", file->path );
|
2019-02-01 23:15:59 +01:00
|
|
|
FS_Rename( incname, name );
|
|
|
|
|
|
|
|
if( file->process )
|
|
|
|
CL_ProcessFile( true, name );
|
|
|
|
else
|
|
|
|
Con_Printf( "successfully downloaded %s, processing disabled!\n", name );
|
|
|
|
}
|
|
|
|
|
|
|
|
file->state = HTTP_FREE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===================
|
|
|
|
HTTP_AutoClean
|
|
|
|
|
|
|
|
remove files with HTTP_FREE state from list
|
|
|
|
===================
|
|
|
|
*/
|
|
|
|
static void HTTP_AutoClean( void )
|
|
|
|
{
|
|
|
|
httpfile_t *curfile, *prevfile = 0;
|
|
|
|
|
|
|
|
// clean all files marked to free
|
|
|
|
for( curfile = http.first_file; curfile; curfile = curfile->next )
|
|
|
|
{
|
|
|
|
if( curfile->state != HTTP_FREE )
|
|
|
|
{
|
|
|
|
prevfile = curfile;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( curfile == http.first_file )
|
|
|
|
{
|
|
|
|
http.first_file = http.first_file->next;
|
|
|
|
Mem_Free( curfile );
|
|
|
|
curfile = http.first_file;
|
|
|
|
if( !curfile )
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( prevfile )
|
|
|
|
prevfile->next = curfile->next;
|
|
|
|
Mem_Free( curfile );
|
|
|
|
curfile = prevfile;
|
|
|
|
if( !curfile )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
http.last_file = prevfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===================
|
|
|
|
HTTP_ProcessStream
|
|
|
|
|
|
|
|
process incoming data
|
|
|
|
===================
|
|
|
|
*/
|
|
|
|
static qboolean HTTP_ProcessStream( httpfile_t *curfile )
|
|
|
|
{
|
|
|
|
char buf[BUFSIZ+1];
|
|
|
|
char *begin = 0;
|
|
|
|
int res;
|
|
|
|
|
2019-09-28 20:41:41 +02:00
|
|
|
if( curfile->header_size >= BUFSIZ )
|
|
|
|
{
|
|
|
|
Con_Reportf( S_ERROR "Header to big\n");
|
|
|
|
HTTP_FreeFile( curfile, true );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
while( ( res = recv( curfile->socket, buf, BUFSIZ - curfile->header_size, 0 )) > 0) // if we got there, we are receiving data
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
|
|
|
curfile->blocktime = 0;
|
|
|
|
|
|
|
|
if( curfile->state < HTTP_RESPONSE_RECEIVED ) // Response still not received
|
|
|
|
{
|
|
|
|
memcpy( curfile->buf + curfile->header_size, buf, res );
|
2019-09-28 20:41:41 +02:00
|
|
|
curfile->buf[curfile->header_size + res] = 0;
|
2019-02-01 23:15:59 +01:00
|
|
|
begin = Q_strstr( curfile->buf, "\r\n\r\n" );
|
|
|
|
|
|
|
|
if( begin ) // Got full header
|
|
|
|
{
|
|
|
|
int cutheadersize = begin - curfile->buf + 4; // after that begin of data
|
|
|
|
char *length;
|
|
|
|
|
|
|
|
Con_Reportf( "HTTP: Got response!\n" );
|
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( !Q_strstr( curfile->buf, "200 OK" ))
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
|
|
|
*begin = 0; // cut string to print out response
|
|
|
|
begin = Q_strchr( curfile->buf, '\r' );
|
|
|
|
|
|
|
|
if( !begin ) begin = Q_strchr( curfile->buf, '\n' );
|
|
|
|
if( begin )
|
|
|
|
*begin = 0;
|
|
|
|
|
2019-09-28 20:41:41 +02:00
|
|
|
Con_Printf( S_ERROR "%s: bad response: %s\n", curfile->path, curfile->buf );
|
2019-02-01 23:15:59 +01:00
|
|
|
HTTP_FreeFile( curfile, true );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// print size
|
|
|
|
length = Q_stristr( curfile->buf, "Content-Length: " );
|
|
|
|
if( length )
|
|
|
|
{
|
|
|
|
int size = Q_atoi( length += 16 );
|
|
|
|
|
|
|
|
Con_Reportf( "HTTP: File size is %d\n", size );
|
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( ( curfile->size != -1 ) && ( curfile->size != size )) // check size if specified, not used
|
2019-02-01 23:15:59 +01:00
|
|
|
Con_Reportf( S_WARN "Server reports wrong file size!\n" );
|
|
|
|
|
|
|
|
curfile->size = size;
|
2019-09-28 20:41:41 +02:00
|
|
|
curfile->header_size = 0;
|
2019-02-01 23:15:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( curfile->size == -1 )
|
|
|
|
{
|
|
|
|
// Usually fastdl's reports file size if link is correct
|
|
|
|
Con_Printf( S_ERROR "file size is unknown, refusing download!\n" );
|
|
|
|
HTTP_FreeFile( curfile, true );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
curfile->state = HTTP_RESPONSE_RECEIVED; // got response, let's start download
|
|
|
|
begin += 4;
|
|
|
|
|
|
|
|
// Write remaining message part
|
|
|
|
if( res - cutheadersize - curfile->header_size > 0 )
|
|
|
|
{
|
|
|
|
int ret = FS_Write( curfile->file, begin, res - cutheadersize - curfile->header_size );
|
|
|
|
|
|
|
|
if( ret != res - cutheadersize - curfile->header_size ) // could not write file
|
|
|
|
{
|
|
|
|
// close it and go to next
|
|
|
|
Con_Printf( S_ERROR "write failed for %s!\n", curfile->path );
|
|
|
|
HTTP_FreeFile( curfile, true );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
curfile->downloaded += ret;
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 20:41:41 +02:00
|
|
|
else
|
|
|
|
curfile->header_size += res;
|
2019-02-01 23:15:59 +01:00
|
|
|
}
|
|
|
|
else if( res > 0 )
|
|
|
|
{
|
|
|
|
// data download
|
|
|
|
int ret = FS_Write( curfile->file, buf, res );
|
|
|
|
|
|
|
|
if ( ret != res )
|
|
|
|
{
|
|
|
|
// close it and go to next
|
|
|
|
Con_Printf( S_ERROR "write failed for %s!\n", curfile->path );
|
|
|
|
curfile->state = HTTP_FREE;
|
|
|
|
HTTP_FreeFile( curfile, true );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
curfile->downloaded += ret;
|
|
|
|
curfile->lastchecksize += ret;
|
|
|
|
|
|
|
|
// as after it will run in same frame
|
|
|
|
if( curfile->checktime > 5 )
|
|
|
|
{
|
2019-10-06 05:50:32 +02:00
|
|
|
float speed = (float)curfile->lastchecksize / ( 5.0f * 1024 );
|
|
|
|
|
2019-02-01 23:15:59 +01:00
|
|
|
curfile->checktime = 0;
|
2019-10-06 05:50:32 +02:00
|
|
|
Con_Reportf( "download speed %f KB/s\n", speed );
|
2019-02-01 23:15:59 +01:00
|
|
|
curfile->lastchecksize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curfile->checktime += host.frametime;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
HTTP_Run
|
|
|
|
|
|
|
|
Download next file block of each active file
|
|
|
|
Call every frame
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void HTTP_Run( void )
|
|
|
|
{
|
|
|
|
httpfile_t *curfile;
|
|
|
|
int iActiveCount = 0;
|
|
|
|
int iProgressCount = 0;
|
2019-02-04 19:11:12 +01:00
|
|
|
float flProgress = 0;
|
2019-02-01 23:15:59 +01:00
|
|
|
qboolean fResolving = false;
|
|
|
|
|
|
|
|
for( curfile = http.first_file; curfile; curfile = curfile->next )
|
|
|
|
{
|
2022-03-11 10:11:49 +01:00
|
|
|
struct sockaddr_storage addr;
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
if( curfile->state == HTTP_FREE )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( curfile->state == HTTP_QUEUE )
|
|
|
|
{
|
2019-03-19 23:18:26 +01:00
|
|
|
char name[MAX_SYSPATH];
|
2019-02-01 23:15:59 +01:00
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
if( iActiveCount > http_maxconnections.value )
|
2019-02-01 23:15:59 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if( !curfile->server )
|
|
|
|
{
|
|
|
|
Con_Printf( S_ERROR "no servers to download %s!\n", curfile->path );
|
|
|
|
HTTP_FreeFile( curfile, true );
|
2019-09-28 20:41:41 +02:00
|
|
|
break;
|
2019-02-01 23:15:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Con_Reportf( "HTTP: Starting download %s from %s\n", curfile->path, curfile->server->host );
|
2019-09-28 20:41:41 +02:00
|
|
|
Q_snprintf( name, sizeof( name ), "downloaded/%s.incomplete", curfile->path );
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
curfile->file = FS_Open( name, "wb", true );
|
|
|
|
|
|
|
|
if( !curfile->file )
|
|
|
|
{
|
|
|
|
Con_Printf( S_ERROR "cannot open %s!\n", name );
|
|
|
|
HTTP_FreeFile( curfile, true );
|
2019-09-28 20:41:41 +02:00
|
|
|
break;
|
2019-02-01 23:15:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
curfile->state = HTTP_OPENED;
|
|
|
|
curfile->blocktime = 0;
|
|
|
|
curfile->downloaded = 0;
|
|
|
|
curfile->lastchecksize = 0;
|
|
|
|
curfile->checktime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
iActiveCount++;
|
|
|
|
|
|
|
|
if( curfile->state < HTTP_SOCKET ) // Socket is not created
|
|
|
|
{
|
|
|
|
dword mode;
|
|
|
|
|
2019-07-01 03:03:11 +02:00
|
|
|
curfile->socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
// Now set non-blocking mode
|
|
|
|
// You may skip this if not supported by system,
|
|
|
|
// but download will lock engine, maybe you will need to add manual returns
|
|
|
|
mode = 1;
|
2020-02-08 16:45:50 +01:00
|
|
|
ioctlsocket( curfile->socket, FIONBIO, (void*)&mode );
|
2019-11-24 01:52:08 +01:00
|
|
|
#if XASH_LINUX
|
2019-02-01 23:15:59 +01:00
|
|
|
// SOCK_NONBLOCK is not portable, so use fcntl
|
|
|
|
fcntl( curfile->socket, F_SETFL, fcntl( curfile->socket, F_GETFL, 0 ) | O_NONBLOCK );
|
2019-11-24 01:52:08 +01:00
|
|
|
#endif
|
2019-02-01 23:15:59 +01:00
|
|
|
curfile->state = HTTP_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( curfile->state < HTTP_NS_RESOLVED )
|
|
|
|
{
|
2023-04-02 23:14:38 +02:00
|
|
|
net_gai_state_t res;
|
2023-03-13 04:08:36 +01:00
|
|
|
char hostport[MAX_VA_STRING];
|
|
|
|
|
2019-02-01 23:15:59 +01:00
|
|
|
if( fResolving )
|
|
|
|
continue;
|
|
|
|
|
2023-03-13 04:08:36 +01:00
|
|
|
Q_snprintf( hostport, sizeof( hostport ), "%s:%d", curfile->server->host, curfile->server->port );
|
|
|
|
|
|
|
|
res = NET_StringToSockaddr( hostport, &addr, true, AF_INET );
|
2019-02-01 23:15:59 +01:00
|
|
|
|
2023-04-02 23:14:38 +02:00
|
|
|
if( res == NET_EAI_AGAIN )
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
|
|
|
fResolving = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:14:38 +02:00
|
|
|
if( res == NET_EAI_NONAME )
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
|
|
|
Con_Printf( S_ERROR "failed to resolve server address for %s!\n", curfile->server->host );
|
|
|
|
HTTP_FreeFile( curfile, true ); // Cannot connect
|
2019-09-28 20:41:41 +02:00
|
|
|
break;
|
2019-02-01 23:15:59 +01:00
|
|
|
}
|
|
|
|
curfile->state = HTTP_NS_RESOLVED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( curfile->state < HTTP_CONNECTED ) // Connection not enstabilished
|
|
|
|
{
|
2023-04-02 23:14:38 +02:00
|
|
|
int res = connect( curfile->socket, (struct sockaddr*)&addr, NET_SockAddrLen( &addr ) );
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
if( res )
|
|
|
|
{
|
2019-07-01 03:03:11 +02:00
|
|
|
if( WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK ) // Should give EWOOLDBLOCK if try recv too soon
|
2019-02-01 23:15:59 +01:00
|
|
|
curfile->state = HTTP_CONNECTED;
|
|
|
|
else
|
|
|
|
{
|
2022-11-17 17:34:46 +01:00
|
|
|
Con_Printf( S_ERROR "cannot connect to server: %s\n", NET_ErrorString( ));
|
2019-02-01 23:15:59 +01:00
|
|
|
HTTP_FreeFile( curfile, true ); // Cannot connect
|
2019-09-28 20:41:41 +02:00
|
|
|
break;
|
2019-02-01 23:15:59 +01:00
|
|
|
}
|
|
|
|
continue; // skip to next file
|
|
|
|
}
|
|
|
|
curfile->state = HTTP_CONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( curfile->state < HTTP_REQUEST ) // Request not formatted
|
|
|
|
{
|
2023-03-31 00:16:17 +02:00
|
|
|
string useragent;
|
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
if( !COM_CheckStringEmpty( http_useragent.string ) || !Q_strcmp( http_useragent.string, "xash3d" ))
|
2023-03-31 00:16:17 +02:00
|
|
|
{
|
|
|
|
Q_snprintf( useragent, sizeof( useragent ), "%s/%s (%s-%s; build %d; %s)",
|
|
|
|
XASH_ENGINE_NAME, XASH_VERSION, Q_buildos( ), Q_buildarch( ), Q_buildnum( ), Q_buildcommit( ));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-19 07:22:30 +02:00
|
|
|
Q_strncpy( useragent, http_useragent.string, sizeof( useragent ));
|
2023-03-31 00:16:17 +02:00
|
|
|
}
|
|
|
|
|
2019-02-01 23:15:59 +01:00
|
|
|
curfile->query_length = Q_snprintf( curfile->buf, sizeof( curfile->buf ),
|
|
|
|
"GET %s%s HTTP/1.0\r\n"
|
|
|
|
"Host: %s\r\n"
|
|
|
|
"User-Agent: %s\r\n\r\n", curfile->server->path,
|
2023-03-31 00:16:17 +02:00
|
|
|
curfile->path, curfile->server->host, useragent );
|
2019-02-01 23:15:59 +01:00
|
|
|
curfile->header_size = 0;
|
|
|
|
curfile->bytes_sent = 0;
|
|
|
|
curfile->state = HTTP_REQUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( curfile->state < HTTP_REQUEST_SENT ) // Request not sent
|
|
|
|
{
|
|
|
|
qboolean wait = false;
|
|
|
|
|
|
|
|
while( curfile->bytes_sent < curfile->query_length )
|
|
|
|
{
|
2023-04-02 23:14:38 +02:00
|
|
|
int res = send( curfile->socket, curfile->buf + curfile->bytes_sent, curfile->query_length - curfile->bytes_sent, 0 );
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
if( res < 0 )
|
|
|
|
{
|
2019-07-01 03:03:11 +02:00
|
|
|
if( WSAGetLastError() != WSAEWOULDBLOCK && WSAGetLastError() != WSAENOTCONN )
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
2022-11-17 17:34:46 +01:00
|
|
|
Con_Printf( S_ERROR "failed to send request: %s\n", NET_ErrorString( ));
|
2019-02-01 23:15:59 +01:00
|
|
|
HTTP_FreeFile( curfile, true );
|
|
|
|
wait = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// blocking while waiting connection
|
|
|
|
// increase counter when blocking
|
|
|
|
curfile->blocktime += host.frametime;
|
|
|
|
wait = true;
|
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
if( curfile->blocktime > http_timeout.value )
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
|
|
|
Con_Printf( S_ERROR "timeout on request send:\n%s\n", curfile->buf );
|
|
|
|
HTTP_FreeFile( curfile, true );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curfile->bytes_sent += res;
|
|
|
|
curfile->blocktime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( wait )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Con_Reportf( "HTTP: Request sent!\n");
|
2022-11-17 17:34:46 +01:00
|
|
|
memset( curfile->buf, 0, sizeof( curfile->buf ));
|
2019-02-01 23:15:59 +01:00
|
|
|
curfile->state = HTTP_REQUEST_SENT;
|
|
|
|
}
|
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
if( !HTTP_ProcessStream( curfile ))
|
2019-09-28 20:41:41 +02:00
|
|
|
break;
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
if( curfile->size > 0 )
|
|
|
|
{
|
|
|
|
flProgress += (float)curfile->downloaded / curfile->size;
|
|
|
|
iProgressCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( curfile->size > 0 && curfile->downloaded >= curfile->size )
|
|
|
|
{
|
|
|
|
HTTP_FreeFile( curfile, false ); // success
|
2019-09-28 20:41:41 +02:00
|
|
|
break;
|
2019-02-01 23:15:59 +01:00
|
|
|
}
|
2022-11-17 17:34:46 +01:00
|
|
|
else if(( WSAGetLastError( ) != WSAEWOULDBLOCK ) && ( WSAGetLastError( ) != WSAEINPROGRESS ))
|
|
|
|
Con_Reportf( "problem downloading %s:\n%s\n", curfile->path, NET_ErrorString( ));
|
2019-02-01 23:15:59 +01:00
|
|
|
else
|
|
|
|
curfile->blocktime += host.frametime;
|
|
|
|
|
2023-05-19 07:22:30 +02:00
|
|
|
if( curfile->blocktime > http_timeout.value )
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
|
|
|
Con_Printf( S_ERROR "timeout on receiving data!\n");
|
|
|
|
HTTP_FreeFile( curfile, true );
|
2019-09-28 20:41:41 +02:00
|
|
|
break;
|
2019-02-01 23:15:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update progress
|
2022-05-02 01:22:35 +02:00
|
|
|
if( !Host_IsDedicated() && iProgressCount != 0 )
|
2019-03-22 16:26:44 +01:00
|
|
|
Cvar_SetValue( "scr_download", flProgress/iProgressCount * 100 );
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
HTTP_AutoClean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===================
|
|
|
|
HTTP_AddDownload
|
|
|
|
|
|
|
|
Add new download to end of queue
|
|
|
|
===================
|
|
|
|
*/
|
|
|
|
void HTTP_AddDownload( const char *path, int size, qboolean process )
|
|
|
|
{
|
2022-11-17 17:34:46 +01:00
|
|
|
httpfile_t *httpfile = Z_Calloc( sizeof( httpfile_t ));
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
Con_Reportf( "File %s queued to download\n", path );
|
|
|
|
|
|
|
|
httpfile->size = size;
|
|
|
|
httpfile->downloaded = 0;
|
|
|
|
httpfile->socket = -1;
|
2022-11-17 17:34:46 +01:00
|
|
|
Q_strncpy ( httpfile->path, path, sizeof( httpfile->path ));
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
if( http.last_file )
|
|
|
|
{
|
|
|
|
// Add next to last download
|
|
|
|
httpfile->id = http.last_file->id + 1;
|
|
|
|
http.last_file->next= httpfile;
|
|
|
|
http.last_file = httpfile;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It will be the only download
|
|
|
|
httpfile->id = 0;
|
|
|
|
http.last_file = http.first_file = httpfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
httpfile->file = NULL;
|
|
|
|
httpfile->next = NULL;
|
|
|
|
httpfile->state = HTTP_QUEUE;
|
|
|
|
httpfile->server = http.first_server;
|
|
|
|
httpfile->process = process;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===============
|
|
|
|
HTTP_Download_f
|
|
|
|
|
|
|
|
Console wrapper
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
static void HTTP_Download_f( void )
|
|
|
|
{
|
|
|
|
if( Cmd_Argc() < 2 )
|
|
|
|
{
|
|
|
|
Con_Printf( S_USAGE "download <gamedir_path>\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HTTP_AddDownload( Cmd_Argv( 1 ), -1, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
HTTP_ParseURL
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
static httpserver_t *HTTP_ParseURL( const char *url )
|
|
|
|
{
|
|
|
|
httpserver_t *server;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
url = Q_strstr( url, "http://" );
|
|
|
|
|
|
|
|
if( !url )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
url += 7;
|
2022-11-17 17:34:46 +01:00
|
|
|
server = Z_Calloc( sizeof( httpserver_t ));
|
2019-02-01 23:15:59 +01:00
|
|
|
i = 0;
|
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
while( *url && ( *url != ':' ) && ( *url != '/' ) && ( *url != '\r' ) && ( *url != '\n' ))
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
2022-11-17 17:34:46 +01:00
|
|
|
if( i > sizeof( server->host ))
|
2019-02-01 23:15:59 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
server->host[i++] = *url++;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->host[i] = 0;
|
|
|
|
|
|
|
|
if( *url == ':' )
|
|
|
|
{
|
|
|
|
server->port = Q_atoi( ++url );
|
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
while( *url && ( *url != '/' ) && ( *url != '\r' ) && ( *url != '\n' ))
|
2019-02-01 23:15:59 +01:00
|
|
|
url++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
server->port = 80;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
2022-11-17 17:34:46 +01:00
|
|
|
while( *url && ( *url != '\r' ) && ( *url != '\n' ))
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
2022-11-17 17:34:46 +01:00
|
|
|
if( i > sizeof( server->path ))
|
2019-02-01 23:15:59 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
server->path[i++] = *url++;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->path[i] = 0;
|
|
|
|
server->next = NULL;
|
|
|
|
server->needfree = false;
|
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=======================
|
|
|
|
HTTP_AddCustomServer
|
|
|
|
=======================
|
|
|
|
*/
|
|
|
|
void HTTP_AddCustomServer( const char *url )
|
|
|
|
{
|
|
|
|
httpserver_t *server = HTTP_ParseURL( url );
|
|
|
|
|
|
|
|
if( !server )
|
|
|
|
{
|
|
|
|
Con_Printf( S_ERROR "\"%s\" is not valid url!\n", url );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->needfree = true;
|
|
|
|
server->next = http.first_server;
|
|
|
|
http.first_server = server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=======================
|
|
|
|
HTTP_AddCustomServer_f
|
|
|
|
=======================
|
|
|
|
*/
|
|
|
|
static void HTTP_AddCustomServer_f( void )
|
|
|
|
{
|
|
|
|
if( Cmd_Argc() == 2 )
|
|
|
|
{
|
2022-11-17 17:34:46 +01:00
|
|
|
HTTP_AddCustomServer( Cmd_Argv( 1 ));
|
2019-02-01 23:15:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
HTTP_Clear_f
|
|
|
|
|
|
|
|
Clear all queue
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
static void HTTP_Clear_f( void )
|
|
|
|
{
|
|
|
|
http.last_file = NULL;
|
|
|
|
|
|
|
|
while( http.first_file )
|
|
|
|
{
|
|
|
|
httpfile_t *file = http.first_file;
|
|
|
|
|
|
|
|
http.first_file = http.first_file->next;
|
|
|
|
|
|
|
|
if( file->file )
|
|
|
|
FS_Close( file->file );
|
|
|
|
|
|
|
|
if( file->socket != -1 )
|
2020-02-08 16:45:50 +01:00
|
|
|
closesocket( file->socket );
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
Mem_Free( file );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
HTTP_Cancel_f
|
|
|
|
|
|
|
|
Stop current download, skip to next file
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
static void HTTP_Cancel_f( void )
|
|
|
|
{
|
|
|
|
if( !http.first_file )
|
|
|
|
return;
|
|
|
|
|
|
|
|
http.first_file->state = HTTP_FREE;
|
|
|
|
HTTP_FreeFile( http.first_file, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
HTTP_Skip_f
|
|
|
|
|
|
|
|
Stop current download, skip to next server
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
static void HTTP_Skip_f( void )
|
|
|
|
{
|
|
|
|
if( http.first_file )
|
|
|
|
HTTP_FreeFile( http.first_file, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
HTTP_List_f
|
|
|
|
|
|
|
|
Print all pending downloads to console
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
static void HTTP_List_f( void )
|
|
|
|
{
|
|
|
|
httpfile_t *file = http.first_file;
|
|
|
|
|
|
|
|
while( file )
|
|
|
|
{
|
|
|
|
if ( file->server )
|
|
|
|
Con_Printf ( "\t%d %d http://%s:%d/%s%s %d\n", file->id, file->state,
|
|
|
|
file->server->host, file->server->port, file->server->path,
|
|
|
|
file->path, file->downloaded );
|
|
|
|
else
|
|
|
|
Con_Printf ( "\t%d %d (no server) %s\n", file->id, file->state, file->path );
|
|
|
|
|
|
|
|
file = file->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
HTTP_ResetProcessState
|
|
|
|
|
|
|
|
When connected to new server, all old files should not increase counter
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
void HTTP_ResetProcessState( void )
|
|
|
|
{
|
|
|
|
httpfile_t *file = http.first_file;
|
|
|
|
|
|
|
|
while( file )
|
|
|
|
{
|
|
|
|
file->process = false;
|
|
|
|
file = file->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============
|
|
|
|
HTTP_Init
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
void HTTP_Init( void )
|
|
|
|
{
|
|
|
|
char *serverfile, *line, token[1024];
|
|
|
|
|
|
|
|
http.last_server = NULL;
|
|
|
|
|
|
|
|
http.first_file = http.last_file = NULL;
|
|
|
|
|
2023-03-31 00:22:41 +02:00
|
|
|
Cmd_AddRestrictedCommand( "http_download", HTTP_Download_f, "add file to download queue" );
|
|
|
|
Cmd_AddRestrictedCommand( "http_skip", HTTP_Skip_f, "skip current download server" );
|
|
|
|
Cmd_AddRestrictedCommand( "http_cancel", HTTP_Cancel_f, "cancel current download" );
|
|
|
|
Cmd_AddRestrictedCommand( "http_clear", HTTP_Clear_f, "cancel all downloads" );
|
|
|
|
Cmd_AddRestrictedCommand( "http_list", HTTP_List_f, "list all queued downloads" );
|
|
|
|
Cmd_AddCommand( "http_addcustomserver", HTTP_AddCustomServer_f, "add custom fastdl server");
|
2023-05-19 07:22:30 +02:00
|
|
|
|
|
|
|
Cvar_RegisterVariable( &http_useragent );
|
|
|
|
Cvar_RegisterVariable( &http_autoremove );
|
|
|
|
Cvar_RegisterVariable( &http_timeout );
|
|
|
|
Cvar_RegisterVariable( &http_maxconnections );
|
2019-02-01 23:15:59 +01:00
|
|
|
|
|
|
|
// Read servers from fastdl.txt
|
|
|
|
line = serverfile = (char *)FS_LoadFile( "fastdl.txt", 0, false );
|
|
|
|
|
|
|
|
if( serverfile )
|
|
|
|
{
|
2022-11-17 17:34:46 +01:00
|
|
|
while(( line = COM_ParseFile( line, token, sizeof( token ))))
|
2019-02-01 23:15:59 +01:00
|
|
|
{
|
|
|
|
httpserver_t *server = HTTP_ParseURL( token );
|
|
|
|
|
|
|
|
if( !server )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( !http.last_server )
|
|
|
|
http.last_server = http.first_server = server;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
http.last_server->next = server;
|
|
|
|
http.last_server = server;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Mem_Free( serverfile );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
HTTP_Shutdown
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void HTTP_Shutdown( void )
|
|
|
|
{
|
|
|
|
HTTP_Clear_f();
|
|
|
|
|
|
|
|
while( http.first_server )
|
|
|
|
{
|
|
|
|
httpserver_t *tmp = http.first_server;
|
|
|
|
|
|
|
|
http.first_server = http.first_server->next;
|
|
|
|
Mem_Free( tmp );
|
|
|
|
}
|
|
|
|
|
2019-09-28 20:41:41 +02:00
|
|
|
http.last_server = NULL;
|
2019-06-29 21:01:32 +02:00
|
|
|
}
|