2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
net_wins.c
|
|
|
|
|
2011-07-26 05:15:41 +00:00
|
|
|
Winsock lan driver.
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
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 2
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-01-22 04:27:39 +00:00
|
|
|
#include <winsock2.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#include "winquake.h"
|
|
|
|
|
2001-11-24 08:19:34 +00:00
|
|
|
#include "QF/cvar.h"
|
2011-09-04 00:48:16 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-11-24 08:19:34 +00:00
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
|
|
|
|
#include "compat.h"
|
2003-02-12 20:08:55 +00:00
|
|
|
#include "netmain.h"
|
2001-11-24 08:19:34 +00:00
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
#define ADDR_SIZE 4
|
|
|
|
|
|
|
|
typedef union address {
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_in s4;
|
|
|
|
} AF_address_t;
|
|
|
|
|
|
|
|
#undef SA_LEN
|
|
|
|
#undef SS_LEN
|
|
|
|
|
|
|
|
#ifdef HAVE_SA_LEN
|
|
|
|
#define SA_LEN(sa) (sa)->sa_len
|
|
|
|
#else
|
|
|
|
#define SA_LEN(sa) (sizeof(struct sockaddr_in))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SS_LEN
|
|
|
|
#define SS_LEN(ss) (ss)->ss_len
|
|
|
|
#else
|
|
|
|
#define SS_LEN(ss) (sizeof(struct sockaddr_in))
|
|
|
|
#endif
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#define MAXHOSTNAMELEN 256
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
static int net_acceptsocket = -1; // socket for fielding new
|
|
|
|
|
|
|
|
// connections
|
|
|
|
static int net_controlsocket;
|
|
|
|
static int net_broadcastsocket = 0;
|
2011-09-04 00:48:16 +00:00
|
|
|
static netadr_t broadcastaddr;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
static unsigned long myAddr;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
qboolean winsock_lib_initialized;
|
|
|
|
|
|
|
|
int (PASCAL FAR * pWSAStartup) (WORD wVersionRequired,
|
|
|
|
|
|
|
|
LPWSADATA lpWSAData);
|
|
|
|
int (PASCAL FAR * pWSACleanup) (void);
|
|
|
|
int (PASCAL FAR * pWSAGetLastError) (void);
|
|
|
|
|
|
|
|
SOCKET (PASCAL FAR * psocket) (int af, int type, int protocol);
|
|
|
|
int (PASCAL FAR * pioctlsocket) (SOCKET s, long cmd, u_long FAR * argp);
|
|
|
|
int (PASCAL FAR * psetsockopt) (SOCKET s, int level, int optname,
|
|
|
|
const char FAR * optval, int optlen);
|
|
|
|
int (PASCAL FAR * precvfrom) (SOCKET s, char FAR * buf, int len,
|
|
|
|
int flags, struct sockaddr FAR * from,
|
|
|
|
int FAR * fromlen);
|
|
|
|
int (PASCAL FAR * psendto) (SOCKET s, const char FAR * buf, int len,
|
|
|
|
int flags, const struct sockaddr FAR * to,
|
|
|
|
int tolen);
|
|
|
|
int (PASCAL FAR * pclosesocket) (SOCKET s);
|
|
|
|
int (PASCAL FAR * pgethostname) (char FAR * name, int namelen);
|
|
|
|
struct hostent FAR *(PASCAL FAR * pgethostbyname) (const char FAR * name);
|
|
|
|
struct hostent FAR *(PASCAL FAR * pgethostbyaddr) (const char FAR * addr,
|
|
|
|
int len, int type);
|
|
|
|
int (PASCAL FAR * pgetsockname) (SOCKET s, struct sockaddr FAR * name,
|
|
|
|
int FAR * namelen);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#include "net_wins.h"
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int winsock_initialized = 0;
|
|
|
|
WSADATA winsockdata;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
static void
|
|
|
|
NetadrToSockadr (netadr_t *a, AF_address_t *s)
|
|
|
|
{
|
|
|
|
memset (s, 0, sizeof (*s));
|
|
|
|
s->s4.sin_family = AF_INET;
|
|
|
|
|
|
|
|
memcpy (&s->s4.sin_addr, &a->ip, ADDR_SIZE);
|
|
|
|
s->s4.sin_port = a->port;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
SockadrToNetadr (AF_address_t *s, netadr_t *a)
|
|
|
|
{
|
|
|
|
memcpy (&a->ip, &s->s4.sin_addr, ADDR_SIZE);
|
|
|
|
a->port = s->s4.sin_port;
|
|
|
|
a->family = s->s4.sin_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
static double blocktime;
|
|
|
|
|
2013-01-22 04:27:39 +00:00
|
|
|
#ifdef _WIN64
|
|
|
|
static INT_PTR PASCAL FAR
|
|
|
|
#else
|
2003-01-06 18:28:13 +00:00
|
|
|
static BOOL PASCAL FAR
|
2013-01-22 04:27:39 +00:00
|
|
|
#endif
|
2001-02-26 06:48:02 +00:00
|
|
|
BlockingHook (void)
|
|
|
|
{
|
|
|
|
MSG msg;
|
|
|
|
BOOL ret;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((Sys_DoubleTime () - blocktime) > 2.0) {
|
|
|
|
WSACancelBlockingCall ();
|
2001-02-19 21:15:25 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
/* get the next message, if any */
|
|
|
|
ret = (BOOL) PeekMessage (&msg, NULL, 0, 0, PM_REMOVE);
|
|
|
|
|
|
|
|
/* if we got one, process it */
|
|
|
|
if (ret) {
|
|
|
|
TranslateMessage (&msg);
|
|
|
|
DispatchMessage (&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TRUE if we got a message */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
|
|
|
WINS_GetLocalAddress (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
struct hostent *local = NULL;
|
|
|
|
char buff[MAXHOSTNAMELEN];
|
|
|
|
unsigned long addr;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (myAddr != INADDR_ANY)
|
|
|
|
return;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (pgethostname (buff, MAXHOSTNAMELEN) == SOCKET_ERROR)
|
2001-02-19 21:15:25 +00:00
|
|
|
return;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
blocktime = Sys_DoubleTime ();
|
|
|
|
WSASetBlockingHook (BlockingHook);
|
|
|
|
local = pgethostbyname (buff);
|
|
|
|
WSAUnhookBlockingHook ();
|
2001-02-19 21:15:25 +00:00
|
|
|
if (local == NULL)
|
|
|
|
return;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
myAddr = *(int *) local->h_addr_list[0];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
addr = ntohl (myAddr);
|
2002-01-19 20:45:45 +00:00
|
|
|
snprintf (my_tcpip_address, sizeof (my_tcpip_address), "%ld.%ld.%ld.%ld",
|
2001-02-26 06:48:02 +00:00
|
|
|
(addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff,
|
|
|
|
addr & 0xff);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
WINS_Init (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int i;
|
|
|
|
char buff[MAXHOSTNAMELEN];
|
|
|
|
char *p;
|
|
|
|
int r;
|
|
|
|
WORD wVersionRequested;
|
|
|
|
HINSTANCE hInst;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// initialize the Winsock function vectors (we do this instead of statically linking
|
|
|
|
// so we can run on Win 3.1, where there isn't necessarily Winsock)
|
2001-02-26 06:48:02 +00:00
|
|
|
hInst = LoadLibrary ("wsock32.dll");
|
|
|
|
|
|
|
|
if (hInst == NULL) {
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("Failed to load winsock.dll\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
winsock_lib_initialized = false;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
winsock_lib_initialized = true;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
pWSAStartup = (void *) GetProcAddress (hInst, "WSAStartup");
|
|
|
|
pWSACleanup = (void *) GetProcAddress (hInst, "WSACleanup");
|
|
|
|
pWSAGetLastError = (void *) GetProcAddress (hInst, "WSAGetLastError");
|
|
|
|
psocket = (void *) GetProcAddress (hInst, "socket");
|
|
|
|
pioctlsocket = (void *) GetProcAddress (hInst, "ioctlsocket");
|
|
|
|
psetsockopt = (void *) GetProcAddress (hInst, "setsockopt");
|
|
|
|
precvfrom = (void *) GetProcAddress (hInst, "recvfrom");
|
|
|
|
psendto = (void *) GetProcAddress (hInst, "sendto");
|
|
|
|
pclosesocket = (void *) GetProcAddress (hInst, "closesocket");
|
|
|
|
pgethostname = (void *) GetProcAddress (hInst, "gethostname");
|
|
|
|
pgethostbyname = (void *) GetProcAddress (hInst, "gethostbyname");
|
|
|
|
pgethostbyaddr = (void *) GetProcAddress (hInst, "gethostbyaddr");
|
|
|
|
pgetsockname = (void *) GetProcAddress (hInst, "getsockname");
|
|
|
|
|
|
|
|
if (!pWSAStartup || !pWSACleanup || !pWSAGetLastError ||
|
2001-02-19 21:15:25 +00:00
|
|
|
!psocket || !pioctlsocket || !psetsockopt ||
|
|
|
|
!precvfrom || !psendto || !pclosesocket ||
|
2001-02-26 06:48:02 +00:00
|
|
|
!pgethostname || !pgethostbyname || !pgethostbyaddr || !pgetsockname) {
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("Couldn't GetProcAddress from winsock.dll\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (COM_CheckParm ("-noudp"))
|
|
|
|
return -1;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (winsock_initialized == 0) {
|
|
|
|
wVersionRequested = MAKEWORD (1, 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2012-12-14 08:07:53 +00:00
|
|
|
r = pWSAStartup (wVersionRequested, &winsockdata);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (r) {
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("Winsock initialization failed.\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
winsock_initialized++;
|
|
|
|
|
|
|
|
// determine my name
|
2001-02-26 06:48:02 +00:00
|
|
|
if (pgethostname (buff, MAXHOSTNAMELEN) == SOCKET_ERROR) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_net, "Winsock TCP/IP Initialization failed.\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
if (--winsock_initialized == 0)
|
|
|
|
pWSACleanup ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// if the quake hostname isn't set, set it to the machine name
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if (strcmp (hostname, "UNNAMED") == 0) {
|
2001-02-19 21:15:25 +00:00
|
|
|
// see if it's a text IP address (well, close enough)
|
|
|
|
for (p = buff; *p; p++)
|
|
|
|
if ((*p < '0' || *p > '9') && *p != '.')
|
|
|
|
break;
|
|
|
|
|
2010-01-13 06:42:26 +00:00
|
|
|
// if it is a real name, strip off the domain; we want only the host
|
2001-02-26 06:48:02 +00:00
|
|
|
if (*p) {
|
2001-02-19 21:15:25 +00:00
|
|
|
for (i = 0; i < 15; i++)
|
|
|
|
if (buff[i] == '.')
|
|
|
|
break;
|
|
|
|
buff[i] = 0;
|
|
|
|
}
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
Cvar_Set ("hostname", buff);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i = COM_CheckParm ("-ip");
|
2001-02-26 06:48:02 +00:00
|
|
|
if (i) {
|
|
|
|
if (i < com_argc - 1) {
|
|
|
|
myAddr = inet_addr (com_argv[i + 1]);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (myAddr == INADDR_NONE)
|
2001-02-26 06:48:02 +00:00
|
|
|
Sys_Error ("%s is not a valid IP address", com_argv[i + 1]);
|
|
|
|
strcpy (my_tcpip_address, com_argv[i + 1]);
|
|
|
|
} else {
|
2001-02-19 21:15:25 +00:00
|
|
|
Sys_Error ("NET_Init: you must specify an IP address after -ip");
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
} else {
|
2001-02-19 21:15:25 +00:00
|
|
|
myAddr = INADDR_ANY;
|
2001-02-26 06:48:02 +00:00
|
|
|
strcpy (my_tcpip_address, "INADDR_ANY");
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if ((net_controlsocket = WINS_OpenSocket (0)) == -1) {
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("WINS_Init: Unable to open control socket\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
if (--winsock_initialized == 0)
|
|
|
|
pWSACleanup ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
{
|
|
|
|
AF_address_t t;
|
|
|
|
t.s4.sin_family = AF_INET;
|
|
|
|
t.s4.sin_addr.s_addr = INADDR_BROADCAST;
|
|
|
|
t.s4.sin_port = htons (net_hostport);
|
|
|
|
SockadrToNetadr (&t, &broadcastaddr);
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("Winsock TCP/IP Initialized\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
tcpipAvailable = true;
|
|
|
|
|
|
|
|
return net_controlsocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
WINS_Shutdown (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
WINS_Listen (false);
|
|
|
|
WINS_CloseSocket (net_controlsocket);
|
|
|
|
if (--winsock_initialized == 0)
|
|
|
|
pWSACleanup ();
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
WINS_Listen (qboolean state)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
// enable listening
|
2001-02-26 06:48:02 +00:00
|
|
|
if (state) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (net_acceptsocket != -1)
|
|
|
|
return;
|
2001-02-26 06:48:02 +00:00
|
|
|
WINS_GetLocalAddress ();
|
2001-02-19 21:15:25 +00:00
|
|
|
if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("WINS_Listen: Unable to open accept socket");
|
2001-02-19 21:15:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// disable listening
|
|
|
|
if (net_acceptsocket == -1)
|
|
|
|
return;
|
|
|
|
WINS_CloseSocket (net_acceptsocket);
|
|
|
|
net_acceptsocket = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
WINS_OpenSocket (int port)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int newsocket;
|
2011-09-04 00:48:16 +00:00
|
|
|
AF_address_t address;
|
|
|
|
netadr_t na;
|
2001-02-26 06:48:02 +00:00
|
|
|
u_long _true = 1;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if ((newsocket = psocket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (pioctlsocket (newsocket, FIONBIO, &_true) == -1)
|
|
|
|
goto ErrorReturn;
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
address.s4.sin_family = AF_INET;
|
|
|
|
address.s4.sin_addr.s_addr = myAddr;
|
|
|
|
address.s4.sin_port = htons ((unsigned short) port);
|
|
|
|
if (bind (newsocket, &address.sa, SA_LEN (&address)) == 0)
|
2001-02-19 21:15:25 +00:00
|
|
|
return newsocket;
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
SockadrToNetadr (&address, &na);
|
|
|
|
Sys_Error ("Unable to bind to %s", WINS_AddrToString (&na));
|
2001-02-26 06:48:02 +00:00
|
|
|
ErrorReturn:
|
2001-02-19 21:15:25 +00:00
|
|
|
pclosesocket (newsocket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
WINS_CloseSocket (int socket)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (socket == net_broadcastsocket)
|
|
|
|
net_broadcastsocket = 0;
|
|
|
|
return pclosesocket (socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
PartialIPAddress
|
|
|
|
|
|
|
|
this lets you type only as much of the net address as required, using
|
|
|
|
the local network components to fill in the rest
|
|
|
|
============
|
|
|
|
*/
|
2001-02-26 06:48:02 +00:00
|
|
|
static int
|
2011-09-04 00:48:16 +00:00
|
|
|
PartialIPAddress (const char *in, netadr_t *hostaddr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
char buff[256];
|
|
|
|
char *b;
|
|
|
|
int addr;
|
|
|
|
int num;
|
|
|
|
int mask;
|
|
|
|
int run;
|
|
|
|
int port;
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
buff[0] = '.';
|
|
|
|
b = buff;
|
2001-02-26 06:48:02 +00:00
|
|
|
strcpy (buff + 1, in);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (buff[1] == '.')
|
|
|
|
b++;
|
|
|
|
|
|
|
|
addr = 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
mask = -1;
|
|
|
|
while (*b == '.') {
|
2001-02-19 21:15:25 +00:00
|
|
|
b++;
|
|
|
|
num = 0;
|
|
|
|
run = 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
while (!(*b < '0' || *b > '9')) {
|
|
|
|
num = num * 10 + *b++ - '0';
|
|
|
|
if (++run > 3)
|
|
|
|
return -1;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
|
|
|
|
return -1;
|
|
|
|
if (num < 0 || num > 255)
|
|
|
|
return -1;
|
2001-02-26 06:48:02 +00:00
|
|
|
mask <<= 8;
|
|
|
|
addr = (addr << 8) + num;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
if (*b++ == ':')
|
2001-11-24 08:19:34 +00:00
|
|
|
port = atoi (b);
|
2001-02-19 21:15:25 +00:00
|
|
|
else
|
|
|
|
port = net_hostport;
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
hostaddr->family = AF_INET;
|
|
|
|
hostaddr->port = htons ((short) port);
|
|
|
|
{
|
|
|
|
int32_t t = myAddr & htonl (mask);
|
|
|
|
t |= htonl (addr);
|
|
|
|
memcpy (hostaddr->ip, &t, ADDR_SIZE);
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//=============================================================================
|
2021-03-27 11:09:37 +00:00
|
|
|
static int WINS_Connect_called;
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-09-04 00:48:16 +00:00
|
|
|
WINS_Connect (int socket, netadr_t *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2021-03-27 11:09:37 +00:00
|
|
|
WINS_Connect_called++;
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
WINS_CheckNewConnections (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
char buf[4096];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (net_acceptsocket == -1)
|
|
|
|
return -1;
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
if (precvfrom (net_acceptsocket, buf, sizeof (buf),
|
|
|
|
MSG_PEEK, NULL, NULL) >= 0) {
|
2001-02-19 21:15:25 +00:00
|
|
|
return net_acceptsocket;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-09-04 00:48:16 +00:00
|
|
|
WINS_Read (int socket, byte *buf, int len, netadr_t *from)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2011-09-04 00:48:16 +00:00
|
|
|
int addrlen = sizeof (AF_address_t);
|
2001-02-26 06:48:02 +00:00
|
|
|
int ret;
|
2011-09-04 00:48:16 +00:00
|
|
|
AF_address_t addr;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
ret = precvfrom (socket, (char *) buf, len, 0, &addr.sa, &addrlen);
|
|
|
|
SockadrToNetadr (&addr, from);
|
2001-02-26 06:48:02 +00:00
|
|
|
if (ret == -1) {
|
2001-11-24 08:19:34 +00:00
|
|
|
int err = pWSAGetLastError ();
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-11-24 08:19:34 +00:00
|
|
|
if (err == WSAEWOULDBLOCK || err == WSAECONNREFUSED)
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2001-02-26 06:48:02 +00:00
|
|
|
WINS_MakeSocketBroadcastCapable (int socket)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int i = 1;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// make this socket broadcast capable
|
2001-02-26 06:48:02 +00:00
|
|
|
if (psetsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) &i, sizeof (i))
|
|
|
|
< 0)
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
net_broadcastsocket = socket;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
WINS_Broadcast (int socket, byte * buf, int len)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int ret;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (socket != net_broadcastsocket) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (net_broadcastsocket != 0)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Attempted to use multiple broadcasts sockets");
|
2001-02-26 06:48:02 +00:00
|
|
|
WINS_GetLocalAddress ();
|
2001-02-19 21:15:25 +00:00
|
|
|
ret = WINS_MakeSocketBroadcastCapable (socket);
|
2001-02-26 06:48:02 +00:00
|
|
|
if (ret == -1) {
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("Unable to make socket broadcast capable\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return WINS_Write (socket, buf, len, &broadcastaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-09-04 00:48:16 +00:00
|
|
|
WINS_Write (int socket, byte *buf, int len, netadr_t *to)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int ret;
|
2011-09-04 00:48:16 +00:00
|
|
|
AF_address_t addr;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
NetadrToSockadr (to, &addr);
|
|
|
|
ret = psendto (socket, (char *) buf, len, 0, &addr.sa, SA_LEN (&addr));
|
2001-02-19 21:15:25 +00:00
|
|
|
if (ret == -1)
|
2001-02-26 06:48:02 +00:00
|
|
|
if (pWSAGetLastError () == WSAEWOULDBLOCK)
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-11-24 08:19:34 +00:00
|
|
|
const char *
|
2011-09-04 00:48:16 +00:00
|
|
|
WINS_AddrToString (netadr_t *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2011-09-04 00:48:16 +00:00
|
|
|
static dstring_t *buffer;
|
|
|
|
|
2021-03-27 11:09:37 +00:00
|
|
|
if (!buffer) {
|
|
|
|
buffer = dstring_new ();
|
|
|
|
}
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
dsprintf (buffer, "%d.%d.%d.%d:%d", addr->ip[0],
|
|
|
|
addr->ip[1], addr->ip[2], addr->ip[3],
|
|
|
|
ntohs (addr->port));
|
|
|
|
return buffer->str;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-09-04 00:48:16 +00:00
|
|
|
WINS_GetSocketAddr (int socket, netadr_t *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2011-09-04 00:48:16 +00:00
|
|
|
int addrlen = sizeof (AF_address_t);
|
|
|
|
AF_address_t ta;
|
2001-02-19 21:15:25 +00:00
|
|
|
unsigned int a;
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
memset (&ta, 0, sizeof (AF_address_t));
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
pgetsockname (socket, &ta.sa, &addrlen);
|
|
|
|
SockadrToNetadr (&ta, addr);
|
|
|
|
memcpy (&a, addr->ip, ADDR_SIZE);
|
2001-02-26 06:48:02 +00:00
|
|
|
if (a == 0 || a == inet_addr ("127.0.0.1"))
|
2011-09-04 00:48:16 +00:00
|
|
|
memcpy (addr->ip, &myAddr, ADDR_SIZE);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-09-04 00:48:16 +00:00
|
|
|
WINS_GetNameFromAddr (netadr_t *addr, char *name)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
struct hostent *hostentry;
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
hostentry = pgethostbyaddr ((char *) addr->ip, ADDR_SIZE, AF_INET);
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
if (hostentry) {
|
2001-11-24 08:19:34 +00:00
|
|
|
strncpy (name, (char *) hostentry->h_name, NET_NAMELEN - 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-24 08:19:34 +00:00
|
|
|
strcpy (name, WINS_AddrToString (addr));
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-09-04 00:48:16 +00:00
|
|
|
WINS_GetAddrFromName (const char *name, netadr_t *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
struct hostent *hostentry;
|
|
|
|
|
|
|
|
if (name[0] >= '0' && name[0] <= '9')
|
|
|
|
return PartialIPAddress (name, addr);
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
hostentry = pgethostbyname (name);
|
|
|
|
if (!hostentry)
|
|
|
|
return -1;
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
addr->family = AF_INET;
|
|
|
|
addr->port = htons ((unsigned short) net_hostport);
|
|
|
|
memcpy (addr->ip, hostentry->h_addr_list[0], ADDR_SIZE);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-09-04 00:48:16 +00:00
|
|
|
WINS_AddrCompare (netadr_t *addr1, netadr_t *addr2)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2011-09-04 00:48:16 +00:00
|
|
|
if (addr1->family != addr2->family)
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
if (memcmp (addr1->ip, addr2->ip, ADDR_SIZE))
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
|
2011-09-04 00:48:16 +00:00
|
|
|
if (addr1->port != addr2->port)
|
2001-02-19 21:15:25 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-09-04 00:48:16 +00:00
|
|
|
WINS_GetSocketPort (netadr_t *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2011-09-04 00:48:16 +00:00
|
|
|
return ntohs (addr->port);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-09-04 00:48:16 +00:00
|
|
|
WINS_SetSocketPort (netadr_t *addr, int port)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2011-09-04 00:48:16 +00:00
|
|
|
addr->port = htons ((unsigned short) port);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|