2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
net_udp.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Copyright (C) 2000 Marcus Sundberg [mackan@stacken.kth.se]
|
|
|
|
|
|
|
|
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
|
2003-01-15 15:31:36 +00:00
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
# include <sys/param.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
|
2002-08-09 02:44:23 +00:00
|
|
|
# define model_t sun_model_t
|
2001-02-19 21:15:25 +00:00
|
|
|
# include <netinet/in.h>
|
2002-08-09 02:44:23 +00:00
|
|
|
# undef model_t
|
2001-02-19 21:15:25 +00:00
|
|
|
#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-08-29 02:12:57 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/console.h"
|
2001-05-31 02:43:48 +00:00
|
|
|
#include "QF/cvar.h"
|
2003-05-08 23:24:02 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/msg.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/qargs.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-06-15 18:08:26 +00:00
|
|
|
#include "compat.h"
|
2003-02-11 22:48:57 +00:00
|
|
|
#include "netchan.h"
|
2001-05-31 02:43:48 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
# undef EWOULDBLOCK
|
|
|
|
# define EWOULDBLOCK WSAEWOULDBLOCK
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAXHOSTNAMELEN
|
|
|
|
# define MAXHOSTNAMELEN 512
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_SOCKLEN_T
|
|
|
|
# ifdef HAVE_SIZE
|
|
|
|
typedef size_t socklen_t;
|
|
|
|
# else
|
|
|
|
typedef unsigned int socklen_t;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2001-08-29 02:12:57 +00:00
|
|
|
int net_socket;
|
2001-02-19 21:15:25 +00:00
|
|
|
netadr_t net_local_adr;
|
2004-04-12 23:38:09 +00:00
|
|
|
netadr_t net_loopback_adr;
|
2001-02-19 21:15:25 +00:00
|
|
|
netadr_t net_from;
|
|
|
|
|
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
|
|
|
|
|
2001-08-29 02:12:57 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-02-19 21:15:25 +00:00
|
|
|
NetadrToSockadr (netadr_t *a, struct sockaddr_in *s)
|
|
|
|
{
|
|
|
|
memset (s, 0, sizeof (*s));
|
|
|
|
s->sin_family = AF_INET;
|
|
|
|
|
2001-11-17 04:30:51 +00:00
|
|
|
memcpy (&s->sin_addr, &a->ip, 4);
|
2001-02-19 21:15:25 +00:00
|
|
|
s->sin_port = a->port;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-02-19 21:15:25 +00:00
|
|
|
SockadrToNetadr (struct sockaddr_in *s, netadr_t *a)
|
|
|
|
{
|
2001-11-18 23:40:01 +00:00
|
|
|
memcpy (&a->ip, &s->sin_addr, 4);
|
2001-02-19 21:15:25 +00:00
|
|
|
a->port = s->sin_port;
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean
|
|
|
|
NET_CompareBaseAdr (netadr_t a, netadr_t b)
|
|
|
|
{
|
|
|
|
if (a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1] && a.ip[2] == b.ip[2]
|
|
|
|
&& a.ip[3] == b.ip[3])
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean
|
|
|
|
NET_CompareAdr (netadr_t a, netadr_t b)
|
|
|
|
{
|
|
|
|
if (a.ip[0] == b.ip[0] && a.ip[1] == b.ip[1] && a.ip[2] == b.ip[2]
|
|
|
|
&& a.ip[3] == b.ip[3] && a.port == b.port)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-07-15 07:04:17 +00:00
|
|
|
const char *
|
2001-02-19 21:15:25 +00:00
|
|
|
NET_AdrToString (netadr_t a)
|
|
|
|
{
|
|
|
|
static char s[64];
|
|
|
|
|
|
|
|
snprintf (s, sizeof (s), "%i.%i.%i.%i:%i", a.ip[0], a.ip[1], a.ip[2],
|
|
|
|
a.ip[3], ntohs (a.port));
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2001-07-15 07:04:17 +00:00
|
|
|
const char *
|
2001-02-19 21:15:25 +00:00
|
|
|
NET_BaseAdrToString (netadr_t a)
|
|
|
|
{
|
|
|
|
static char s[64];
|
|
|
|
|
|
|
|
snprintf (s, sizeof (s), "%i.%i.%i.%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3]);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
NET_StringToAdr
|
|
|
|
|
|
|
|
idnewt
|
|
|
|
idnewt:28000
|
|
|
|
192.246.40.70
|
|
|
|
192.246.40.70:28000
|
|
|
|
*/
|
|
|
|
qboolean
|
2001-07-15 07:04:17 +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 *colon;
|
2001-02-19 21:15:25 +00:00
|
|
|
struct hostent *h;
|
|
|
|
struct sockaddr_in sadr;
|
|
|
|
|
2003-05-08 23:24:02 +00:00
|
|
|
if (!copy)
|
|
|
|
copy = dstring_new ();
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
memset (&sadr, 0, sizeof (sadr));
|
|
|
|
sadr.sin_family = AF_INET;
|
|
|
|
|
|
|
|
sadr.sin_port = 0;
|
|
|
|
|
2003-05-08 23:24:02 +00:00
|
|
|
dstring_copystr (copy, s);
|
2001-02-19 21:15:25 +00:00
|
|
|
// strip off a trailing :port if present
|
2003-05-08 23:24:02 +00:00
|
|
|
for (colon = copy->str; *colon; colon++)
|
2001-02-19 21:15:25 +00:00
|
|
|
if (*colon == ':') {
|
|
|
|
*colon = 0;
|
|
|
|
sadr.sin_port = htons ((unsigned short) atoi (colon + 1));
|
|
|
|
}
|
|
|
|
|
2003-05-08 23:24:02 +00:00
|
|
|
if (copy->str[0] >= '0' && copy->str[0] <= '9') {
|
|
|
|
int addr = inet_addr (copy->str);
|
2001-11-18 23:40:01 +00:00
|
|
|
memcpy (&sadr.sin_addr, &addr, 4);
|
2001-02-19 21:15:25 +00:00
|
|
|
} else {
|
2003-05-08 23:24:02 +00:00
|
|
|
if (!(h = gethostbyname (copy->str)))
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
2001-11-18 23:40:01 +00:00
|
|
|
memcpy (&sadr.sin_addr, h->h_addr_list[0], 4);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SockadrToNetadr (&sadr, a);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if we can't bind the address locally--in other words,
|
|
|
|
// the IP is NOT one of our interfaces.
|
|
|
|
qboolean
|
|
|
|
NET_IsClientLegal (netadr_t *adr)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
int newsocket;
|
2001-08-29 02:12:57 +00:00
|
|
|
struct sockaddr_in sadr;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (adr->ip[0] == 127)
|
|
|
|
return false; // no local connections period
|
|
|
|
|
|
|
|
NetadrToSockadr (adr, &sadr);
|
|
|
|
|
|
|
|
if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
|
|
|
|
Sys_Error ("NET_IsClientLegal: socket:", strerror (errno));
|
|
|
|
|
|
|
|
sadr.sin_port = 0;
|
|
|
|
|
|
|
|
if (bind (newsocket, (void *) &sadr, sizeof (sadr)) == -1) {
|
|
|
|
// It is not a local address
|
|
|
|
close (newsocket);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
close (newsocket);
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean
|
|
|
|
NET_GetPacket (void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
socklen_t fromlen;
|
2001-08-29 02:12:57 +00:00
|
|
|
struct sockaddr_in from;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
fromlen = sizeof (from);
|
|
|
|
ret =
|
|
|
|
recvfrom (net_socket, (void *) net_message_buffer,
|
|
|
|
sizeof (net_message_buffer), 0, (struct sockaddr *) &from,
|
|
|
|
&fromlen);
|
|
|
|
|
|
|
|
SockadrToNetadr (&from, &net_from);
|
|
|
|
|
|
|
|
|
|
|
|
if (ret == -1) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
int err = WSAGetLastError ();
|
|
|
|
|
|
|
|
if (err == WSAEMSGSIZE) {
|
|
|
|
Con_Printf ("Warning: Oversize packet from %s\n",
|
|
|
|
NET_AdrToString (net_from));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (err == 10054) {
|
|
|
|
Con_DPrintf ("error 10054 from %s\n", 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;
|
2001-11-09 07:22:34 +00:00
|
|
|
Con_Printf ("NET_GetPacket: %d: %s\n", err, strerror (err));
|
2001-02-19 21:15:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-08-29 02:12:57 +00:00
|
|
|
// Check for malformed packets
|
2003-08-27 17:47:53 +00:00
|
|
|
if (ntohs(net_from.port)<1024) {
|
2001-02-19 21:15:25 +00:00
|
|
|
Con_Printf ("Warning: Packet from %s dropped: Bad port\n",
|
2001-11-17 04:30:51 +00:00
|
|
|
NET_AdrToString (net_from));
|
2001-02-19 21:15:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-08-29 02:12:57 +00:00
|
|
|
if (from.sin_addr.s_addr==INADDR_ANY || from.sin_addr.s_addr ==
|
|
|
|
INADDR_BROADCAST) {
|
2001-02-19 21:15:25 +00:00
|
|
|
Con_Printf ("Warning: Packet dropped - bad address\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
Con_Printf ("Oversize packet from %s\n", NET_AdrToString (net_from));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NET_SendPacket (int length, void *data, netadr_t to)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
|
|
|
|
NetadrToSockadr (&to, &addr);
|
|
|
|
|
2002-07-02 21:37:22 +00:00
|
|
|
ret = sendto (net_socket, data, length, 0, (struct sockaddr *) &addr,
|
|
|
|
sizeof (addr));
|
2001-02-19 21:15:25 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
int err = WSAGetLastError ();
|
|
|
|
|
2004-02-19 22:29:50 +00:00
|
|
|
if (err == WSAEADDRNOTAVAIL)
|
|
|
|
Con_DPrintf ("NET_SendPacket Warning: %i\n", err);
|
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;
|
2002-07-02 21:37:22 +00:00
|
|
|
#endif // _WIN32
|
2001-02-19 21:15:25 +00:00
|
|
|
if (err == EWOULDBLOCK)
|
|
|
|
return;
|
|
|
|
|
2001-09-21 03:01:14 +00:00
|
|
|
Con_Printf ("NET_SendPacket: %s\n", strerror (errno));
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2001-02-19 21:15:25 +00:00
|
|
|
UDP_OpenSocket (int port)
|
|
|
|
{
|
2001-08-29 02:12:57 +00:00
|
|
|
int newsocket, i;
|
2001-02-19 21:15:25 +00:00
|
|
|
struct sockaddr_in address;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define ioctl ioctlsocket
|
|
|
|
unsigned long _true = true;
|
|
|
|
#else
|
|
|
|
int _true = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset (&address, 0, sizeof(address));
|
|
|
|
|
|
|
|
if ((newsocket = socket (PF_INET, 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));
|
|
|
|
address.sin_family = AF_INET;
|
2001-08-29 02:12:57 +00:00
|
|
|
// ZOID -- check for interface binding option
|
2001-02-19 21:15:25 +00:00
|
|
|
if ((i = COM_CheckParm ("-ip")) != 0 && i < com_argc) {
|
|
|
|
address.sin_addr.s_addr = inet_addr (com_argv[i + 1]);
|
|
|
|
Con_Printf ("Binding to IP Interface Address of %s\n",
|
|
|
|
inet_ntoa (address.sin_addr));
|
|
|
|
} else
|
|
|
|
address.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
if (port == PORT_ANY)
|
|
|
|
address.sin_port = 0;
|
|
|
|
else
|
|
|
|
address.sin_port = htons ((short) port);
|
|
|
|
if (bind (newsocket, (void *) &address, sizeof (address)) == -1)
|
|
|
|
Sys_Error ("UDP_OpenSocket: bind: %s", strerror (errno));
|
|
|
|
|
|
|
|
return newsocket;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-02-19 21:15:25 +00:00
|
|
|
NET_GetLocalAddress (void)
|
|
|
|
{
|
|
|
|
char buff[MAXHOSTNAMELEN];
|
|
|
|
socklen_t namelen;
|
2001-08-29 02:12:57 +00:00
|
|
|
struct sockaddr_in address;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
gethostname (buff, MAXHOSTNAMELEN);
|
|
|
|
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_Init: getsockname: %s", strerror (errno));
|
|
|
|
net_local_adr.port = address.sin_port;
|
|
|
|
|
|
|
|
Con_Printf ("IP address %s\n", NET_AdrToString (net_local_adr));
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
// 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[0] = 127;
|
|
|
|
net_loopback_adr.ip[3] = 1;
|
|
|
|
|
2003-06-26 20:24:47 +00:00
|
|
|
Con_Printf ("UDP (IPv4) Initialized\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NET_Shutdown (void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
closesocket (net_socket);
|
|
|
|
WSACleanup ();
|
|
|
|
#else
|
|
|
|
close (net_socket);
|
|
|
|
#endif
|
|
|
|
}
|