2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
net_udp6.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Copyright (C) 2000 Marcus Sundberg [mackan@stacken.kth.se]
|
|
|
|
Copyright (C) 1999,2000 contributors of the QuakeForge project
|
|
|
|
Please see the file "AUTHORS" for a list of contributors
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
2001-08-29 02:12:57 +00:00
|
|
|
/* Sun's model_t in sys/model.h conflicts w/ Quake's model_t */
|
|
|
|
#define model_t quakeforgemodel_t
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2001-08-29 02:12:57 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
# include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
# include <netdb.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
# include <sys/filio.h>
|
|
|
|
#endif
|
|
|
|
#ifdef NeXT
|
|
|
|
# include <libc.h>
|
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-06-15 18:08:26 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
# undef EWOULDBLOCK
|
|
|
|
# define EWOULDBLOCK WSAEWOULDBLOCK
|
2001-02-19 21:15:25 +00:00
|
|
|
# ifdef HAVE_IPV6
|
|
|
|
# include <winsock2.h>
|
2001-06-15 18:08:26 +00:00
|
|
|
# undef IP_MULTICAST_IF
|
|
|
|
# undef IP_MULTICAST_TTL
|
|
|
|
# undef IP_MULTICAST_LOOP
|
|
|
|
# undef IP_ADD_MEMBERSHIP
|
|
|
|
# undef IP_DROP_MEMBERSHIP
|
|
|
|
# define ip_mreq ip_mreq_icky_hack
|
2001-02-19 21:15:25 +00:00
|
|
|
# include <ws2tcpip.h>
|
2001-06-15 18:08:26 +00:00
|
|
|
# undef ip_mreq
|
|
|
|
# ifndef WINSOCK_API_LINKAGE
|
|
|
|
# define WINSOCK_API_LINKAGE
|
|
|
|
# endif
|
|
|
|
# ifndef _WINSOCK2API_
|
|
|
|
# define _WINSOCK2API_
|
|
|
|
# endif
|
2001-02-19 21:15:25 +00:00
|
|
|
# define _WINSOCKAPI_
|
|
|
|
# define HAVE_SOCKLEN_T
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#undef model_t
|
|
|
|
|
2003-05-08 23:24:02 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-03-27 23:36:02 +00:00
|
|
|
#include "QF/msg.h"
|
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/sys.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-02-11 22:48:57 +00:00
|
|
|
#include "netchan.h"
|
2001-06-15 18:08:26 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifndef MAXHOSTNAMELEN
|
|
|
|
# define MAXHOSTNAMELEN 512
|
|
|
|
#endif
|
|
|
|
|
2010-12-29 11:36:43 +00:00
|
|
|
#if defined(__GLIBC__) && !defined(s6_addr32) // glibc macro
|
2001-02-19 21:15:25 +00:00
|
|
|
# define s6_addr32 in6_u.u6_addr32
|
2001-06-05 17:23:03 +00:00
|
|
|
# if ! __GLIBC_PREREQ (2,2)
|
2010-12-29 11:36:43 +00:00
|
|
|
# define ss_family __ss_family
|
2001-06-05 17:23:03 +00:00
|
|
|
# endif
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
|
[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
|
|
|
static char *net_family;
|
|
|
|
static cvar_t net_family_cvar = {
|
|
|
|
.name = "net_family",
|
|
|
|
.description =
|
|
|
|
"Set the address family to ipv4, ipv6 or unspecified",
|
|
|
|
.default_value = "unspecified",
|
|
|
|
.flags = CVAR_ROM,
|
|
|
|
.value = { .type = 0, .value = &net_family },
|
|
|
|
};
|
2011-09-03 04:04:47 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
netadr_t net_from;
|
2001-08-29 02:12:57 +00:00
|
|
|
netadr_t net_local_adr;
|
2004-05-17 22:01:18 +00:00
|
|
|
netadr_t net_loopback_adr;
|
2001-02-19 21:15:25 +00:00
|
|
|
int net_socket;
|
|
|
|
|
2001-02-23 23:16:13 +00:00
|
|
|
static sizebuf_t _net_message_message;
|
2004-01-20 06:11:00 +00:00
|
|
|
static qmsg_t _net_message = {0, 0, &_net_message_message};
|
|
|
|
qmsg_t *net_message = &_net_message;
|
2001-02-23 23:16:13 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#define MAX_UDP_PACKET (MAX_MSGLEN*2)
|
|
|
|
byte net_message_buffer[MAX_UDP_PACKET];
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA winsockdata;
|
|
|
|
#endif
|
|
|
|
|
2010-12-29 11:58:14 +00:00
|
|
|
typedef union address {
|
|
|
|
struct sockaddr_storage ss;
|
2011-07-23 05:58:45 +00:00
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_in s4;
|
|
|
|
struct sockaddr_in6 s6;
|
2010-12-29 11:58:14 +00:00
|
|
|
} AF_address_t;
|
|
|
|
|
2011-08-31 09:07:41 +00:00
|
|
|
#undef SA_LEN
|
|
|
|
#undef SS_LEN
|
|
|
|
|
|
|
|
#ifdef HAVE_SA_LEN
|
|
|
|
#define SA_LEN(sa) (sa)->sa_len
|
|
|
|
#else
|
|
|
|
#define SA_LEN(sa) (((sa)->sa_family == AF_INET6) \
|
|
|
|
? sizeof(struct sockaddr_in6) \
|
|
|
|
: sizeof(struct sockaddr_in))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SS_LEN
|
|
|
|
#define SS_LEN(ss) (ss)->ss_len
|
|
|
|
#else
|
|
|
|
#define SS_LEN(ss) (((ss)->ss_family == AF_INET6) \
|
|
|
|
? sizeof(struct sockaddr_in6) \
|
|
|
|
: sizeof(struct sockaddr_in))
|
|
|
|
#endif
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-02-11 05:59:08 +00:00
|
|
|
static void
|
2010-12-29 11:58:14 +00:00
|
|
|
NetadrToSockadr (netadr_t *a, AF_address_t *s)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
memset (s, 0, sizeof (*s));
|
|
|
|
|
2010-12-29 11:58:14 +00:00
|
|
|
switch (a->family) {
|
|
|
|
case AF_INET: {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_net, "err, converting v4 to v6...\n");
|
2010-12-30 09:46:44 +00:00
|
|
|
s->ss.ss_family = AF_INET6;
|
|
|
|
s->s6.sin6_addr.s6_addr[10] = s->s6.sin6_addr.s6_addr[11] = 0xff;
|
|
|
|
memcpy (&s->s6.sin6_addr.s6_addr[12], &a->ip, sizeof (s->s4.sin_addr));
|
2010-12-29 11:58:14 +00:00
|
|
|
s->s4.sin_port = a->port;
|
|
|
|
#ifdef HAVE_SS_LEN
|
2010-12-30 09:46:44 +00:00
|
|
|
s->ss.ss_len = sizeof (struct sockaddr_in6);
|
2010-12-29 11:58:14 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_INET6: {
|
2010-12-30 09:46:44 +00:00
|
|
|
s->ss.ss_family = a->family;
|
2010-12-29 11:58:14 +00:00
|
|
|
memcpy (&s->s6.sin6_addr, &a->ip, sizeof (s->s6.sin6_addr));
|
|
|
|
s->s6.sin6_port = a->port;
|
|
|
|
#ifdef HAVE_SS_LEN
|
|
|
|
s->ss.ss_len = sizeof (struct sockaddr_in6);
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
2010-12-29 11:58:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_net, "%s: Unknown address family %d", __FUNCTION__, a->family);
|
2010-12-29 11:58:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2003-02-11 05:59:08 +00:00
|
|
|
static void
|
2010-12-29 11:58:14 +00:00
|
|
|
SockadrToNetadr (AF_address_t *s, netadr_t *a)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2010-12-29 11:58:14 +00:00
|
|
|
a->family = s->ss.ss_family;
|
|
|
|
switch (a->family) {
|
|
|
|
case AF_INET: {
|
|
|
|
memcpy (a->ip, &(s->s4.sin_addr), sizeof (s->s4.sin_addr));
|
|
|
|
a->port = s->s4.sin_port;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_INET6: {
|
|
|
|
memcpy (a->ip, &(s->s6.sin6_addr), sizeof (s->s6.sin6_addr));
|
|
|
|
a->port = s->s6.sin6_port;
|
|
|
|
break;
|
|
|
|
}
|
2010-12-30 11:38:20 +00:00
|
|
|
default:
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_net, "%s: Unknown address family 0x%x\n", __FUNCTION__, s->ss.ss_family);
|
2010-12-29 11:58:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2010-12-29 11:58:14 +00:00
|
|
|
|
2003-05-14 16:29:27 +00:00
|
|
|
/*
|
2003-02-11 05:59:08 +00:00
|
|
|
static qboolean
|
2001-02-19 21:15:25 +00:00
|
|
|
NET_AdrIsLoopback (netadr_t a)
|
|
|
|
{
|
|
|
|
if (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *) &a.ip))
|
|
|
|
return true;
|
|
|
|
else if (IN6_IS_ADDR_V4MAPPED ((struct in6_addr *) &a.ip) &&
|
|
|
|
((struct in_addr *) &a.ip[3])->s_addr == htonl (INADDR_LOOPBACK))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2003-05-14 16:29:27 +00:00
|
|
|
*/
|
2011-08-23 05:33:51 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
qboolean
|
|
|
|
NET_CompareBaseAdr (netadr_t a, netadr_t b)
|
|
|
|
{
|
2002-04-26 16:05:24 +00:00
|
|
|
if (memcmp (a.ip, b.ip, sizeof (a.ip)) == 0)
|
2001-02-19 21:15:25 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean
|
|
|
|
NET_CompareAdr (netadr_t a, netadr_t b)
|
|
|
|
{
|
2002-04-26 16:05:24 +00:00
|
|
|
if (memcmp (a.ip, b.ip, sizeof (a.ip)) == 0 && a.port == b.port)
|
2001-02-19 21:15:25 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-08-24 04:09:56 +00:00
|
|
|
const char *
|
2001-02-19 21:15:25 +00:00
|
|
|
NET_AdrToString (netadr_t a)
|
|
|
|
{
|
2011-12-08 13:16:59 +00:00
|
|
|
static dstring_t *s;
|
2010-12-30 09:46:44 +00:00
|
|
|
char base[64];
|
|
|
|
AF_address_t ss;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Yes, this duplication is lame, but we want to know the real address
|
|
|
|
family of the address so that we can know whether or not to put a
|
2011-07-23 05:58:45 +00:00
|
|
|
bracket around it, and this is less ugly than trying to check the
|
2010-12-30 09:46:44 +00:00
|
|
|
string returned from NET_BaseAdrToString()
|
|
|
|
*/
|
|
|
|
memset (&ss, 0, sizeof (ss));
|
2011-08-23 05:33:51 +00:00
|
|
|
NetadrToSockadr (&a, &ss);
|
2010-12-30 09:46:44 +00:00
|
|
|
|
|
|
|
// Convert any "mapped" addresses back to v4
|
|
|
|
if (a.family == AF_INET6 && IN6_IS_ADDR_V4MAPPED (&(ss.s6.sin6_addr))) {
|
|
|
|
#ifdef HAVE_SS_LEN
|
|
|
|
ss.ss.ss_len = sizeof (ss.s4);
|
|
|
|
#endif
|
|
|
|
ss.ss.ss_family = AF_INET;
|
|
|
|
memcpy (&(ss.s4.sin_addr), &ss.s6.sin6_addr.s6_addr[12], sizeof (ss.s4.sin_addr));
|
|
|
|
}
|
|
|
|
|
2011-08-31 09:07:41 +00:00
|
|
|
if (getnameinfo (&ss.sa, SS_LEN(&ss.ss), base, sizeof (base),
|
2010-12-30 09:46:44 +00:00
|
|
|
NULL, 0, NI_NUMERICHOST)) strcpy (base, "<invalid>");
|
|
|
|
|
2011-12-08 13:16:59 +00:00
|
|
|
if (!s)
|
|
|
|
s = dstring_new ();
|
2010-12-30 09:46:44 +00:00
|
|
|
if (ss.ss.ss_family == AF_INET6) {
|
2011-12-08 13:16:59 +00:00
|
|
|
dsprintf (s, "[%s]:%d", base, ntohs (a.port));
|
2010-12-30 09:46:44 +00:00
|
|
|
} else {
|
2011-12-08 13:16:59 +00:00
|
|
|
dsprintf (s, "%s:%d", base, ntohs (a.port));
|
2010-12-30 09:46:44 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2011-12-08 13:16:59 +00:00
|
|
|
return s->str;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-08-24 04:09:56 +00:00
|
|
|
const char *
|
2001-02-19 21:15:25 +00:00
|
|
|
NET_BaseAdrToString (netadr_t a)
|
|
|
|
{
|
|
|
|
static char s[64];
|
2010-12-29 12:47:15 +00:00
|
|
|
AF_address_t ss;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2010-12-30 09:46:44 +00:00
|
|
|
memset (&ss, 0, sizeof (ss));
|
2011-08-23 05:33:51 +00:00
|
|
|
NetadrToSockadr (&a, &ss);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2010-12-30 09:46:44 +00:00
|
|
|
// Convert any "mapped" addresses back to v4
|
|
|
|
if (a.family == AF_INET6 && IN6_IS_ADDR_V4MAPPED (&(ss.s6.sin6_addr))) {
|
|
|
|
#ifdef HAVE_SS_LEN
|
|
|
|
ss.ss.ss_len = sizeof (ss.s4);
|
|
|
|
#endif
|
|
|
|
ss.ss.ss_family = AF_INET;
|
2011-08-31 09:07:41 +00:00
|
|
|
memcpy (&(ss.s4.sin_addr), &ss.s6.sin6_addr.s6_addr[12],
|
|
|
|
sizeof (ss.s4.sin_addr));
|
2010-12-30 09:46:44 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 09:07:41 +00:00
|
|
|
if (getnameinfo (&ss.sa, SS_LEN(&ss.ss), s, sizeof (s),
|
2010-12-30 09:58:49 +00:00
|
|
|
NULL, 0, NI_NUMERICHOST)) strcpy (s, "<invalid>");
|
2001-02-19 21:15:25 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
NET_StringToAdr
|
|
|
|
|
|
|
|
idnewt
|
|
|
|
idnewt:28000
|
|
|
|
192.246.40.70
|
|
|
|
192.246.40.70:28000
|
|
|
|
*/
|
|
|
|
qboolean
|
2001-08-24 04:09:56 +00:00
|
|
|
NET_StringToAdr (const char *s, netadr_t *a)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2003-05-08 23:24:02 +00:00
|
|
|
static dstring_t *copy;
|
2001-08-29 02:12:57 +00:00
|
|
|
char *addrs, *space;
|
|
|
|
char *ports = NULL;
|
2001-02-19 21:15:25 +00:00
|
|
|
int err;
|
2001-08-29 02:12:57 +00:00
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *resultp;
|
2010-12-29 12:47:15 +00:00
|
|
|
AF_address_t addr;
|
|
|
|
AF_address_t resadr;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-05-08 23:24:02 +00:00
|
|
|
if (!copy)
|
|
|
|
copy = dstring_new ();
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
memset (&hints, 0, sizeof (hints));
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
[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 (strchr (net_family, '6')) {
|
2011-09-03 04:04:47 +00:00
|
|
|
hints.ai_family = AF_INET6;
|
[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
|
|
|
} else if (strchr (net_family, '4')) {
|
2011-09-03 04:04:47 +00:00
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
} else {
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-05-08 23:24:02 +00:00
|
|
|
dstring_copystr (copy, s);
|
|
|
|
addrs = space = copy->str;
|
2001-02-19 21:15:25 +00:00
|
|
|
if (*addrs == '[') {
|
|
|
|
addrs++;
|
|
|
|
for (; *space && *space != ']'; space++);
|
|
|
|
if (!*space) {
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("NET_StringToAdr: invalid IPv6 address %s\n", s);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*space++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; *space; space++) {
|
|
|
|
if (*space == ':') {
|
|
|
|
*space = '\0';
|
|
|
|
ports = space + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-30 09:58:49 +00:00
|
|
|
if ((err = getaddrinfo (addrs, ports, &hints, &resultp))) { // Error
|
|
|
|
Sys_Printf ("NET_StringToAdr: string %s:\n%s\n", s, gai_strerror (err));
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (resultp->ai_family) {
|
|
|
|
case AF_INET:
|
|
|
|
// convert to ipv6 addr
|
2010-12-29 12:47:15 +00:00
|
|
|
memset (&addr, 0, sizeof (addr));
|
|
|
|
memset (&resadr, 0, sizeof (resadr));
|
|
|
|
memcpy (&resadr.s4, resultp->ai_addr, sizeof (resadr.s4));
|
2010-12-30 09:58:49 +00:00
|
|
|
|
2010-12-29 12:47:15 +00:00
|
|
|
addr.ss.ss_family = AF_INET6;
|
|
|
|
addr.s6.sin6_addr.s6_addr[10] = addr.s6.sin6_addr.s6_addr[11] = 0xff;
|
|
|
|
memcpy (&(addr.s6.sin6_addr.s6_addr[12]), &resadr.s4.sin_addr,
|
|
|
|
sizeof (resadr.s4.sin_addr));
|
|
|
|
addr.s6.sin6_port = resadr.s4.sin_port;
|
2001-02-19 21:15:25 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2010-12-29 12:47:15 +00:00
|
|
|
memcpy (&addr, resultp->ai_addr, sizeof (addr.s6));
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("NET_StringToAdr: string %s:\nprotocol family %d not "
|
2002-07-02 21:37:22 +00:00
|
|
|
"supported\n", s, resultp->ai_family);
|
|
|
|
return 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2010-12-30 09:34:15 +00:00
|
|
|
freeaddrinfo (resultp);
|
2002-07-02 21:37:22 +00:00
|
|
|
|
2010-12-29 12:47:15 +00:00
|
|
|
SockadrToNetadr (&addr, a);
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_net, "Raw address: %s\n", NET_BaseAdrToString (*a));
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean
|
|
|
|
NET_GetPacket (void)
|
|
|
|
{
|
2001-08-29 02:12:57 +00:00
|
|
|
int ret;
|
2001-02-19 21:15:25 +00:00
|
|
|
unsigned int fromlen;
|
2010-12-29 12:47:15 +00:00
|
|
|
AF_address_t from;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
fromlen = sizeof (from);
|
2010-12-30 11:38:20 +00:00
|
|
|
memset (&from, 0, sizeof (from));
|
2011-07-23 05:58:45 +00:00
|
|
|
ret = recvfrom (net_socket, (void *) net_message_buffer,
|
2010-12-30 09:58:49 +00:00
|
|
|
sizeof (net_message_buffer), 0, &from.sa, &fromlen);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (ret == -1) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
int err = WSAGetLastError ();
|
|
|
|
|
|
|
|
if (err == WSAEMSGSIZE) {
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("Warning: Oversize packet from %s\n",
|
2001-02-19 21:15:25 +00:00
|
|
|
NET_AdrToString (net_from));
|
|
|
|
return false;
|
|
|
|
}
|
2002-07-02 21:37:22 +00:00
|
|
|
#else // _WIN32
|
2001-02-19 21:15:25 +00:00
|
|
|
int err = errno;
|
|
|
|
|
|
|
|
if (err == ECONNREFUSED)
|
|
|
|
return false;
|
2002-07-02 21:37:22 +00:00
|
|
|
#endif // _WIN32
|
2001-02-19 21:15:25 +00:00
|
|
|
if (err == EWOULDBLOCK)
|
|
|
|
return false;
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("NET_GetPacket: %s\n", strerror (err));
|
2001-02-19 21:15:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-12-30 11:38:20 +00:00
|
|
|
SockadrToNetadr (&from, &net_from);
|
|
|
|
|
2001-02-23 23:16:13 +00:00
|
|
|
_net_message_message.cursize = ret;
|
2001-02-19 21:15:25 +00:00
|
|
|
if (ret == sizeof (net_message_buffer)) {
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("Oversize packet from %s\n", NET_AdrToString (net_from));
|
2001-02-19 21:15:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-09 14:47:15 +00:00
|
|
|
NET_SendPacket (int length, const void *data, netadr_t to)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2010-12-29 12:47:15 +00:00
|
|
|
AF_address_t addr;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
NetadrToSockadr (&to, &addr);
|
|
|
|
|
2011-08-31 09:07:41 +00:00
|
|
|
ret = sendto (net_socket, data, length, 0, &addr.sa, SA_LEN (&addr.sa));
|
2001-02-19 21:15:25 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
int err = WSAGetLastError ();
|
|
|
|
|
|
|
|
if (err == WSAEADDRNOTAVAIL)
|
2010-03-12 10:51:07 +00:00
|
|
|
Sys_Printf ("NET_SendPacket Warning: %i\n", err);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else /* _WIN32 */
|
|
|
|
int err = errno;
|
|
|
|
|
|
|
|
if (err == ECONNREFUSED)
|
|
|
|
return;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
if (err == EWOULDBLOCK)
|
|
|
|
return;
|
|
|
|
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("NET_SendPacket: %s\n", strerror (err));
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-11 05:59:08 +00:00
|
|
|
static int
|
2001-02-19 21:15:25 +00:00
|
|
|
UDP_OpenSocket (int port)
|
|
|
|
{
|
2003-02-11 05:59:08 +00:00
|
|
|
char Buf[BUFSIZ];
|
|
|
|
const char *Host, *Service;
|
2001-08-29 02:12:57 +00:00
|
|
|
int newsocket, Error;
|
2001-02-19 21:15:25 +00:00
|
|
|
struct sockaddr_in6 address;
|
|
|
|
struct addrinfo hints, *res;
|
|
|
|
|
2010-12-30 08:42:22 +00:00
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
int off = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define ioctl ioctlsocket
|
|
|
|
unsigned long _true = true;
|
|
|
|
#else
|
|
|
|
int _true = 1;
|
|
|
|
#endif
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((newsocket = socket (PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1)
|
|
|
|
Sys_Error ("UDP_OpenSocket: socket: %s", strerror (errno));
|
|
|
|
if (ioctl (newsocket, FIONBIO, &_true) == -1)
|
|
|
|
Sys_Error ("UDP_OpenSocket: ioctl FIONBIO: %s", strerror (errno));
|
|
|
|
memset (&address, 0, sizeof (address));
|
|
|
|
address.sin6_family = AF_INET6;
|
|
|
|
|
|
|
|
memset (&hints, 0, sizeof (hints));
|
[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 (strchr (net_family, '6')) {
|
2011-09-03 07:28:00 +00:00
|
|
|
hints.ai_family = AF_INET6;
|
[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
|
|
|
} else if (strchr (net_family, '4')) {
|
2011-09-03 07:28:00 +00:00
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
} else {
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
hints.ai_protocol = IPPROTO_UDP;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
|
2010-12-30 08:43:46 +00:00
|
|
|
// ZOID -- check for interface binding option
|
|
|
|
if ((i = COM_CheckParm ("-ip")) && i < com_argc) {
|
2001-02-19 21:15:25 +00:00
|
|
|
Host = com_argv[i + 1];
|
|
|
|
} else {
|
2010-12-30 09:58:49 +00:00
|
|
|
Host = "::0";
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_net, "Binding to IP address [%s]\n", Host);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (port == PORT_ANY)
|
|
|
|
Service = NULL;
|
|
|
|
else {
|
|
|
|
sprintf (Buf, "%5d", port);
|
|
|
|
Service = Buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((Error = getaddrinfo (Host, Service, &hints, &res)))
|
|
|
|
Sys_Error ("UDP_OpenSocket: getaddrinfo: %s", gai_strerror (Error));
|
|
|
|
|
2010-12-30 09:58:49 +00:00
|
|
|
if ((newsocket = socket (res->ai_family,
|
2011-07-23 05:58:45 +00:00
|
|
|
res->ai_socktype,
|
2010-12-30 09:58:49 +00:00
|
|
|
res->ai_protocol)) == -1)
|
2001-02-19 21:15:25 +00:00
|
|
|
Sys_Error ("UDP_OpenSocket: socket: %s", strerror (errno));
|
2010-12-30 09:58:49 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
// FIONBIO sets non-blocking IO for this socket
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (ioctl (newsocket, FIONBIO, &_true) == -1)
|
|
|
|
#else /* _WIN32 */
|
|
|
|
if (ioctl (newsocket, FIONBIO, (char *) &_true) == -1)
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
Sys_Error ("UDP_OpenSocket: ioctl FIONBIO: %s", strerror (errno));
|
|
|
|
|
2010-12-30 08:42:22 +00:00
|
|
|
// don't care about the result code
|
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
setsockopt (newsocket, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof (off));
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (bind (newsocket, res->ai_addr, res->ai_addrlen) < 0)
|
|
|
|
Sys_Error ("UDP_OpenSocket: bind: %s", strerror (errno));
|
|
|
|
|
|
|
|
freeaddrinfo (res);
|
|
|
|
|
|
|
|
return newsocket;
|
|
|
|
}
|
|
|
|
|
2003-02-11 05:59:08 +00:00
|
|
|
static void
|
2001-02-19 21:15:25 +00:00
|
|
|
NET_GetLocalAddress (void)
|
|
|
|
{
|
|
|
|
char buff[MAXHOSTNAMELEN];
|
2011-07-23 05:58:45 +00:00
|
|
|
socklen_t namelen;
|
2010-12-29 11:58:14 +00:00
|
|
|
AF_address_t address;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (gethostname (buff, MAXHOSTNAMELEN) == -1)
|
|
|
|
Sys_Error ("Net_GetLocalAddress: gethostname: %s", strerror (errno));
|
|
|
|
buff[MAXHOSTNAMELEN - 1] = 0;
|
|
|
|
|
|
|
|
NET_StringToAdr (buff, &net_local_adr);
|
|
|
|
|
|
|
|
namelen = sizeof (address);
|
|
|
|
if (getsockname (net_socket, (struct sockaddr *) &address, &namelen) == -1)
|
|
|
|
Sys_Error ("NET_GetLocalAddress: getsockname: %s", strerror (errno));
|
2010-12-29 11:58:14 +00:00
|
|
|
net_local_adr.port = address.s6.sin6_port;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("IP address %s\n", NET_AdrToString (net_local_adr));
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NET_Init (int port)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
int r;
|
2001-08-29 02:12:57 +00:00
|
|
|
WORD wVersionRequested;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
wVersionRequested = MAKEWORD (1, 1);
|
|
|
|
|
|
|
|
r = WSAStartup (MAKEWORD (1, 1), &winsockdata);
|
|
|
|
if (r)
|
|
|
|
Sys_Error ("Winsock initialization failed.");
|
|
|
|
#endif /* _WIN32 */
|
[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_Register (&net_family_cvar, 0, 0);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// open the single socket to be used for all communications
|
|
|
|
net_socket = UDP_OpenSocket (port);
|
|
|
|
|
|
|
|
// init the message buffer
|
2001-02-23 23:16:13 +00:00
|
|
|
_net_message_message.maxsize = sizeof (net_message_buffer);
|
|
|
|
_net_message_message.data = net_message_buffer;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// determine my name & address
|
|
|
|
NET_GetLocalAddress ();
|
|
|
|
|
2004-04-12 23:38:09 +00:00
|
|
|
net_loopback_adr.ip[15] = 1;
|
|
|
|
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("UDP (IPv6) Initialized\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 14:15:26 +00:00
|
|
|
static void
|
|
|
|
NET_shutdown (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
closesocket (net_socket);
|
|
|
|
WSACleanup ();
|
|
|
|
#else
|
|
|
|
close (net_socket);
|
|
|
|
#endif
|
|
|
|
}
|