2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
net_udp.c
|
|
|
|
|
2011-07-26 05:15:41 +00:00
|
|
|
UDP 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
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2001-09-07 04:41:13 +00:00
|
|
|
#include <sys/types.h>
|
2001-05-09 05:41:34 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2001-04-10 23:17:47 +00:00
|
|
|
#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-04-10 23:17:47 +00:00
|
|
|
# include <netinet/in.h>
|
2002-08-09 02:44:23 +00:00
|
|
|
# undef model_t
|
2001-04-10 23:17:47 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
# include <netdb.h>
|
|
|
|
#endif
|
2011-08-11 09:32:39 +00:00
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
# include <ifaddrs.h>
|
|
|
|
#endif
|
2001-04-10 23:17:47 +00:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
# include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_WINSOCK_H
|
|
|
|
# include <winsock.h>
|
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2001-08-27 07:13:32 +00:00
|
|
|
# include <unistd.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __sun__
|
2001-08-27 07:13:32 +00:00
|
|
|
# include <sys/filio.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
#ifdef NeXT
|
2001-08-27 07:13:32 +00:00
|
|
|
# include <libc.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
|
|
|
|
2001-08-27 07:13:32 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2005-04-27 03:34:38 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
2001-08-27 07:13:32 +00:00
|
|
|
|
2001-05-31 03:41:35 +00:00
|
|
|
#include "QF/cvar.h"
|
2003-07-08 22:39:45 +00:00
|
|
|
#include "QF/dstring.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/sys.h"
|
2003-07-08 22:39:45 +00:00
|
|
|
#include "QF/va.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-27 07:13:32 +00:00
|
|
|
#include "compat.h"
|
2003-02-11 22:48:57 +00:00
|
|
|
#include "netmain.h"
|
2001-05-31 03:41:35 +00:00
|
|
|
|
2001-04-10 23:17:47 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# undef EWOULDBLOCK
|
|
|
|
# define EWOULDBLOCK WSAEWOULDBLOCK
|
|
|
|
# undef ECONNREFUSED
|
|
|
|
# define ECONNREFUSED WSAECONNREFUSED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAXHOSTNAMELEN
|
|
|
|
# define MAXHOSTNAMELEN 512
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_SOCKLEN_T
|
|
|
|
# ifdef HAVE_SIZE
|
2001-08-27 07:13:32 +00:00
|
|
|
typedef size_t socklen_t;
|
2001-04-10 23:17:47 +00:00
|
|
|
# else
|
2001-08-27 07:13:32 +00:00
|
|
|
typedef unsigned int socklen_t;
|
2001-04-10 23:17:47 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
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;
|
2001-02-19 21:15:25 +00:00
|
|
|
static struct qsockaddr broadcastaddr;
|
|
|
|
|
2011-08-11 09:32:39 +00:00
|
|
|
static uint32_t myAddr;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#include "net_udp.h"
|
|
|
|
|
2011-08-06 09:57:53 +00:00
|
|
|
static int num_ifaces;
|
|
|
|
uint32_t *ifaces;
|
|
|
|
uint32_t *default_iface;
|
2011-08-06 11:30:56 +00:00
|
|
|
uint32_t *last_iface;
|
2011-08-06 09:57:53 +00:00
|
|
|
|
2005-04-27 03:34:38 +00:00
|
|
|
static int
|
2011-08-06 09:57:53 +00:00
|
|
|
get_iface_list (int sock)
|
2005-04-27 03:34:38 +00:00
|
|
|
{
|
2011-08-11 09:32:39 +00:00
|
|
|
#ifdef HAVE_GETIFADDRS
|
|
|
|
struct ifaddrs *ifa_head;
|
|
|
|
struct ifaddrs *ifa;
|
|
|
|
struct ifreq ifreq;
|
|
|
|
|
|
|
|
if (getifaddrs (&ifa_head) < 0)
|
|
|
|
goto no_ifaddrs;
|
|
|
|
for (ifa = ifa_head; ifa; ifa = ifa->ifa_next) {
|
|
|
|
if (!ifa->ifa_flags & IFF_UP)
|
|
|
|
continue;
|
|
|
|
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
strncpy (ifreq.ifr_name, ifa->ifa_name, IFNAMSIZ);
|
|
|
|
ifreq.ifr_name[IFNAMSIZ - 1] = 0;
|
|
|
|
ioctl (sock, SIOCGIFINDEX, (char*) &ifreq);
|
|
|
|
if (ifreq.ifr_ifindex > num_ifaces)
|
|
|
|
num_ifaces = ifreq.ifr_ifindex;
|
|
|
|
}
|
|
|
|
ifaces = malloc (num_ifaces * sizeof (uint32_t));
|
|
|
|
Sys_MaskPrintf (SYS_NET, "%d interfaces\n", num_ifaces);
|
|
|
|
for (ifa = ifa_head; ifa; ifa = ifa->ifa_next) {
|
|
|
|
int index;
|
|
|
|
struct sockaddr_in *sa;
|
|
|
|
|
|
|
|
if (!ifa->ifa_flags & IFF_UP)
|
|
|
|
continue;
|
|
|
|
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
strncpy (ifreq.ifr_name, ifa->ifa_name, IFNAMSIZ);
|
|
|
|
ifreq.ifr_name[IFNAMSIZ - 1] = 0;
|
|
|
|
ioctl (sock, SIOCGIFINDEX, (char*) &ifreq);
|
|
|
|
index = ifreq.ifr_ifindex - 1;
|
|
|
|
sa = (struct sockaddr_in *) ifa->ifa_addr;
|
|
|
|
memcpy (&ifaces[index], &sa->sin_addr, sizeof (uint32_t));
|
|
|
|
Sys_MaskPrintf (SYS_NET, " %-10s %s\n", ifa->ifa_name,
|
|
|
|
inet_ntoa (sa->sin_addr));
|
|
|
|
if (!default_iface && ifaces[index] != htonl (0x7f000001))
|
|
|
|
default_iface = &ifaces[index];
|
2005-04-27 03:34:38 +00:00
|
|
|
}
|
2011-08-11 09:32:39 +00:00
|
|
|
freeifaddrs (ifa_head);
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
no_ifaddrs:
|
|
|
|
ifaces = &myAddr;
|
|
|
|
default_iface = &ifaces[0];
|
|
|
|
num_ifaces = 1;
|
|
|
|
return 0;
|
2005-04-27 03:34:38 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
UDP_Init (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
struct hostent *local;
|
2001-02-26 06:48:02 +00:00
|
|
|
char buff[MAXHOSTNAMELEN];
|
2001-02-19 21:15:25 +00:00
|
|
|
struct qsockaddr addr;
|
2001-02-26 06:48:02 +00:00
|
|
|
char *colon;
|
2001-10-21 21:18:31 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA winsockdata;
|
|
|
|
int r;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-10-21 21:18:31 +00:00
|
|
|
r = WSAStartup (MAKEWORD (1, 1), &winsockdata);
|
|
|
|
if (r)
|
|
|
|
Sys_Error ("Winsock initialization failed.");
|
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
if (COM_CheckParm ("-noudp"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// determine my name & address
|
2001-02-26 06:48:02 +00:00
|
|
|
gethostname (buff, MAXHOSTNAMELEN);
|
|
|
|
local = gethostbyname (buff);
|
2001-07-27 23:17:40 +00:00
|
|
|
if (local)
|
2011-08-11 09:32:39 +00:00
|
|
|
myAddr = *(uint32_t *) local->h_addr_list[0];
|
2001-07-27 23:17:40 +00:00
|
|
|
else
|
|
|
|
myAddr = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
// if the quake hostname isn't set, set it to the machine name
|
2001-02-26 06:48:02 +00:00
|
|
|
if (strcmp (hostname->string, "UNNAMED") == 0) {
|
2001-02-19 21:15:25 +00:00
|
|
|
buff[15] = 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
Cvar_Set (hostname, buff);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("UDP_Init: Unable to open control socket");
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2011-08-06 09:57:53 +00:00
|
|
|
get_iface_list (net_controlsocket);
|
2005-04-27 03:34:38 +00:00
|
|
|
|
2009-12-19 10:54:23 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in t;
|
|
|
|
memcpy (&t, &broadcastaddr, sizeof (t));
|
|
|
|
t.sin_family = AF_INET;
|
|
|
|
t.sin_addr.s_addr = INADDR_BROADCAST;
|
|
|
|
t.sin_port = htons (net_hostport);
|
|
|
|
memcpy (&broadcastaddr, &t, sizeof (broadcastaddr));
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
UDP_GetSocketAddr (net_controlsocket, &addr);
|
2001-02-26 06:48:02 +00:00
|
|
|
strcpy (my_tcpip_address, UDP_AddrToString (&addr));
|
2001-02-19 21:15:25 +00:00
|
|
|
colon = strrchr (my_tcpip_address, ':');
|
|
|
|
if (colon)
|
|
|
|
*colon = 0;
|
|
|
|
|
2007-11-06 10:17:14 +00:00
|
|
|
Sys_Printf ("UDP (IPv4) Initialized\n");
|
2001-02-19 21:15:25 +00:00
|
|
|
tcpipAvailable = true;
|
|
|
|
|
|
|
|
return net_controlsocket;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
UDP_Shutdown (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
UDP_Listen (false);
|
2001-10-21 21:18:31 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
UDP_CloseSocket (net_controlsocket);
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
UDP_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;
|
|
|
|
if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == -1)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("UDP_Listen: Unable to open accept socket");
|
2001-02-19 21:15:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// disable listening
|
|
|
|
if (net_acceptsocket == -1)
|
|
|
|
return;
|
|
|
|
UDP_CloseSocket (net_acceptsocket);
|
|
|
|
net_acceptsocket = -1;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
UDP_OpenSocket (int port)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int newsocket;
|
2001-02-19 21:15:25 +00:00
|
|
|
struct sockaddr_in address;
|
2001-04-10 23:17:47 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define ioctl ioctlsocket
|
|
|
|
unsigned long _true = true;
|
|
|
|
#else
|
2011-08-06 11:30:56 +00:00
|
|
|
int _true = true;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_IN_PKTINFO
|
|
|
|
int ip_pktinfo = 1;
|
2001-04-10 23:17:47 +00:00
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
|
|
|
|
return -1;
|
|
|
|
|
2001-04-10 23:17:47 +00:00
|
|
|
if (ioctl (newsocket, FIONBIO, &_true) == -1)
|
2001-02-19 21:15:25 +00:00
|
|
|
goto ErrorReturn;
|
2011-08-06 11:30:56 +00:00
|
|
|
#ifdef HAVE_IN_PKTINFO
|
|
|
|
if (setsockopt (newsocket, SOL_IP, IP_PKTINFO, &ip_pktinfo,
|
|
|
|
sizeof (ip_pktinfo)) == -1) {
|
|
|
|
close (newsocket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
address.sin_family = AF_INET;
|
|
|
|
address.sin_addr.s_addr = INADDR_ANY;
|
2001-02-26 06:48:02 +00:00
|
|
|
address.sin_port = htons (port);
|
|
|
|
if (bind (newsocket, (void *) &address, sizeof (address)) == -1)
|
2001-02-19 21:15:25 +00:00
|
|
|
goto ErrorReturn;
|
|
|
|
|
|
|
|
return newsocket;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
ErrorReturn:
|
2002-08-20 19:16:11 +00:00
|
|
|
UDP_CloseSocket (newsocket);
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
UDP_CloseSocket (int socket)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (socket == net_broadcastsocket)
|
|
|
|
net_broadcastsocket = 0;
|
2001-10-21 21:18:31 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
closesocket (socket);
|
|
|
|
WSACleanup ();
|
|
|
|
return 0;
|
|
|
|
#else
|
2001-02-19 21:15:25 +00:00
|
|
|
return close (socket);
|
2001-10-21 21:18:31 +00:00
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-08-27 07:13:32 +00:00
|
|
|
PartialIPAddress
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-27 07:13:32 +00:00
|
|
|
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-19 21:15:25 +00:00
|
|
|
*/
|
2001-02-26 06:48:02 +00:00
|
|
|
static int
|
2001-07-15 07:04:17 +00:00
|
|
|
PartialIPAddress (const char *in, struct qsockaddr *hostaddr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2003-07-08 22:39:45 +00:00
|
|
|
char *buff;
|
2001-02-26 06:48:02 +00:00
|
|
|
char *b;
|
2001-08-27 07:13:32 +00:00
|
|
|
int addr, mask, num, port, run;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2003-07-08 22:39:45 +00:00
|
|
|
buff = nva (".%s", in);
|
2001-02-19 21:15:25 +00:00
|
|
|
b = buff;
|
|
|
|
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)
|
2003-07-08 22:39:45 +00:00
|
|
|
goto error;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
|
2003-07-08 22:39:45 +00:00
|
|
|
goto error;
|
2001-02-19 21:15:25 +00:00
|
|
|
if (num < 0 || num > 255)
|
2003-07-08 22:39:45 +00:00
|
|
|
goto error;
|
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-02-26 06:48:02 +00:00
|
|
|
port = atoi (b);
|
2001-02-19 21:15:25 +00:00
|
|
|
else
|
|
|
|
port = net_hostport;
|
|
|
|
|
2002-09-18 15:31:44 +00:00
|
|
|
hostaddr->qsa_family = AF_INET;
|
2001-02-26 06:48:02 +00:00
|
|
|
((struct sockaddr_in *) hostaddr)->sin_port = htons ((short) port);
|
|
|
|
|
|
|
|
((struct sockaddr_in *) hostaddr)->sin_addr.s_addr =
|
|
|
|
(myAddr & htonl (mask)) | htonl (addr);
|
|
|
|
|
2003-07-08 22:39:45 +00:00
|
|
|
free (buff);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
2003-07-08 22:39:45 +00:00
|
|
|
error:
|
|
|
|
free (buff);
|
|
|
|
return -1;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
UDP_Connect (int socket, struct qsockaddr *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
UDP_CheckNewConnections (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
unsigned long available;
|
|
|
|
struct sockaddr_in from;
|
|
|
|
socklen_t fromlen;
|
|
|
|
char buff[1];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (net_acceptsocket == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (ioctl (net_acceptsocket, FIONREAD, &available) == -1)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("UDP: ioctlsocket (FIONREAD) failed");
|
2001-02-19 21:15:25 +00:00
|
|
|
if (available)
|
|
|
|
return net_acceptsocket;
|
2003-07-08 20:11:10 +00:00
|
|
|
// quietly absorb empty packets
|
2011-08-06 09:57:53 +00:00
|
|
|
// there is no way to tell between an empty packet and no packets, but
|
|
|
|
// as non-blocking io is used, this is not a problem.
|
2011-08-06 11:30:56 +00:00
|
|
|
// we don't care about the interface on which the packet arrived
|
2001-02-26 06:48:02 +00:00
|
|
|
recvfrom (net_acceptsocket, buff, 0, 0, (struct sockaddr *) &from,
|
|
|
|
&fromlen);
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-07-29 04:51:58 +00:00
|
|
|
UDP_Read (int socket, byte *buf, int len, struct qsockaddr *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int ret;
|
2011-08-06 11:30:56 +00:00
|
|
|
#ifdef HAVE_IN_PKTINFO
|
|
|
|
char ancillary[CMSG_SPACE (sizeof (struct in_pktinfo))];
|
|
|
|
struct msghdr msghdr = {
|
|
|
|
addr,
|
|
|
|
sizeof (*addr),
|
|
|
|
0, 0,
|
|
|
|
ancillary,
|
|
|
|
sizeof (ancillary),
|
|
|
|
0
|
|
|
|
};
|
|
|
|
struct iovec iovec = {buf, len};
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
struct in_pktinfo *info = 0;
|
|
|
|
|
|
|
|
msghdr.msg_iov = &iovec;
|
|
|
|
msghdr.msg_iovlen = 1;
|
|
|
|
ret = recvmsg (socket, &msghdr, 0);
|
|
|
|
if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED))
|
|
|
|
return 0;
|
|
|
|
for (cmsg = CMSG_FIRSTHDR (&msghdr); cmsg;
|
|
|
|
cmsg = CMSG_NXTHDR (&msghdr, cmsg)) {
|
|
|
|
Sys_MaskPrintf (SYS_NET, "%d\n", cmsg->cmsg_type);
|
|
|
|
if (cmsg->cmsg_type == IP_PKTINFO) {
|
|
|
|
info = (struct in_pktinfo *) CMSG_DATA (cmsg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_iface = 0;
|
|
|
|
if (info) {
|
|
|
|
/* Our iterface list is 0 based, but the pktinfo interface index is 1
|
|
|
|
* based.
|
|
|
|
*/
|
|
|
|
last_iface = &ifaces[info->ipi_ifindex - 1];
|
|
|
|
}
|
|
|
|
Sys_MaskPrintf (SYS_NET, "got %d bytes from %s on iface %d (%s)\n", ret,
|
|
|
|
UDP_AddrToString (addr), info ? info->ipi_ifindex - 1 : -1,
|
|
|
|
last_iface ? inet_ntoa (info->ipi_addr) : "?");
|
|
|
|
#else
|
|
|
|
socklen_t addrlen = sizeof (struct qsockaddr);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
ret = recvfrom (socket, buf, len, 0, (struct sockaddr *) addr, &addrlen);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED))
|
|
|
|
return 0;
|
2011-08-06 11:30:56 +00:00
|
|
|
Sys_MaskPrintf (SYS_NET, "got %d bytes from %s\n", ret,
|
|
|
|
UDP_AddrToString (addr));
|
|
|
|
last_iface = default_iface;
|
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2001-02-26 06:48:02 +00:00
|
|
|
UDP_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-08-27 07:13:32 +00:00
|
|
|
if (setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) &i,
|
|
|
|
sizeof (i)) < 0)
|
|
|
|
return -1;
|
2001-02-19 21:15:25 +00:00
|
|
|
net_broadcastsocket = socket;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-07-29 04:51:58 +00:00
|
|
|
UDP_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-19 21:15:25 +00:00
|
|
|
ret = UDP_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 UDP_Write (socket, buf, len, &broadcastaddr);
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2011-07-29 04:51:58 +00:00
|
|
|
UDP_Write (int socket, byte *buf, int len, struct qsockaddr *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int ret;
|
|
|
|
|
2001-08-27 07:13:32 +00:00
|
|
|
ret = sendto (socket, buf, len, 0, (struct sockaddr *) addr,
|
|
|
|
sizeof (struct qsockaddr));
|
2001-02-19 21:15:25 +00:00
|
|
|
if (ret == -1 && errno == EWOULDBLOCK)
|
|
|
|
return 0;
|
2011-08-02 06:22:57 +00:00
|
|
|
Sys_MaskPrintf (SYS_NET, "sent %d bytes to %s\n", ret, UDP_AddrToString (addr));
|
2001-02-19 21:15:25 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-07-08 22:39:45 +00:00
|
|
|
const char *
|
2001-02-26 06:48:02 +00:00
|
|
|
UDP_AddrToString (struct qsockaddr *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2003-07-08 22:39:45 +00:00
|
|
|
static dstring_t *buffer;
|
2001-02-26 06:48:02 +00:00
|
|
|
int haddr;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-07-08 22:39:45 +00:00
|
|
|
if (!buffer)
|
|
|
|
buffer = dstring_new ();
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
haddr = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
|
2003-07-08 22:39:45 +00:00
|
|
|
dsprintf (buffer, "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff,
|
2001-02-26 06:48:02 +00:00
|
|
|
(haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff,
|
|
|
|
ntohs (((struct sockaddr_in *) addr)->sin_port));
|
2003-07-08 22:39:45 +00:00
|
|
|
return buffer->str;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
unsigned int a;
|
2005-06-08 08:49:54 +00:00
|
|
|
socklen_t addrlen = sizeof (struct qsockaddr);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
memset (addr, 0, sizeof (struct qsockaddr));
|
|
|
|
|
|
|
|
getsockname (socket, (struct sockaddr *) addr, &addrlen);
|
|
|
|
a = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
|
2011-08-06 12:47:57 +00:00
|
|
|
if (a == 0 || a == inet_addr ("127.0.0.1")) {
|
|
|
|
((struct sockaddr_in *) addr)->sin_addr.s_addr = *default_iface;
|
|
|
|
if (last_iface)
|
|
|
|
((struct sockaddr_in *) addr)->sin_addr.s_addr = *last_iface;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
UDP_GetNameFromAddr (struct qsockaddr *addr, char *name)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
struct hostent *hostentry;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
hostentry =
|
|
|
|
gethostbyaddr ((char *) &((struct sockaddr_in *) addr)->sin_addr,
|
|
|
|
sizeof (struct in_addr), AF_INET);
|
|
|
|
|
|
|
|
if (hostentry) {
|
|
|
|
strncpy (name, (char *) hostentry->h_name, NET_NAMELEN - 1);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy (name, UDP_AddrToString (addr));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
2001-07-15 07:04:17 +00:00
|
|
|
UDP_GetAddrFromName (const char *name, struct qsockaddr *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 = gethostbyname (name);
|
|
|
|
if (!hostentry)
|
|
|
|
return -1;
|
|
|
|
|
2002-09-18 15:31:44 +00:00
|
|
|
addr->qsa_family = AF_INET;
|
2001-02-26 06:48:02 +00:00
|
|
|
((struct sockaddr_in *) addr)->sin_port = htons (net_hostport);
|
|
|
|
|
|
|
|
((struct sockaddr_in *) addr)->sin_addr.s_addr =
|
|
|
|
*(int *) hostentry->h_addr_list[0];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
UDP_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2002-09-18 15:31:44 +00:00
|
|
|
if (addr1->qsa_family != addr2->qsa_family)
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (((struct sockaddr_in *) addr1)->sin_addr.s_addr !=
|
|
|
|
((struct sockaddr_in *) addr2)->sin_addr.s_addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
return -1;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (((struct sockaddr_in *) addr1)->sin_port !=
|
|
|
|
((struct sockaddr_in *) addr2)->sin_port)
|
2001-02-19 21:15:25 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
UDP_GetSocketPort (struct qsockaddr *addr)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
return ntohs (((struct sockaddr_in *) addr)->sin_port);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int
|
|
|
|
UDP_SetSocketPort (struct qsockaddr *addr, int port)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
((struct sockaddr_in *) addr)->sin_port = htons (port);
|
2001-02-19 21:15:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|