Make netmain use netadr_t, same as netchan.

Having sockaddr and friends in a header file was bad news for portability
(especially windows).
This commit is contained in:
Bill Currie 2011-09-04 09:00:42 +09:00
parent 5ee01879ec
commit e50f0e18ba
5 changed files with 160 additions and 115 deletions

View file

@ -80,7 +80,7 @@ int UDP_CloseSocket (int socket);
\param addr The address to which very little will be done.
\return 0
*/
int UDP_Connect (int socket, AF_address_t *addr);
int UDP_Connect (int socket, netadr_t *addr);
/** Check for incoming packets on the accept socket.
@ -96,7 +96,7 @@ int UDP_CheckNewConnections (void);
\param[out] addr The address from which the packet originated.
\return The number of bytes read or -1 on error.
*/
int UDP_Read (int socket, byte *buf, int len, AF_address_t *addr);
int UDP_Read (int socket, byte *buf, int len, netadr_t *addr);
/** Send a packet via the specified socket to the specified address.
@ -106,7 +106,7 @@ int UDP_Read (int socket, byte *buf, int len, AF_address_t *addr);
\param addr The addres to which the packet will be sent.
\return The number of bytes sent or -1 on error.
*/
int UDP_Write (int socket, byte *buf, int len, AF_address_t *addr);
int UDP_Write (int socket, byte *buf, int len, netadr_t *addr);
/** Broadcast a packet via the specified socket.
@ -130,7 +130,7 @@ int UDP_Broadcast (int socket, byte *buf, int len);
\param addr The address to convert.
\return The address in human readable form.
*/
const char *UDP_AddrToString (AF_address_t *addr);
const char *UDP_AddrToString (netadr_t *addr);
/** Retrieve the address to which the socket is bound.
@ -138,7 +138,7 @@ const char *UDP_AddrToString (AF_address_t *addr);
\param[out] addr The address to which the socket is bound.
\return 0
*/
int UDP_GetSocketAddr (int socket, AF_address_t *addr);
int UDP_GetSocketAddr (int socket, netadr_t *addr);
/** Convert an address to a hostname.
@ -147,7 +147,7 @@ int UDP_GetSocketAddr (int socket, AF_address_t *addr);
\bug No checking is done on the size of the buffer, and uses strcpy.
*/
int UDP_GetNameFromAddr (AF_address_t *addr, char *name);
int UDP_GetNameFromAddr (netadr_t *addr, char *name);
/** Convert a human readable address to a quake address.
@ -162,7 +162,7 @@ int UDP_GetNameFromAddr (AF_address_t *addr, char *name);
\param addr The resulting address of the conversion.
\return 0 if the conversion is successful, otherwise -1.
*/
int UDP_GetAddrFromName (const char *name, AF_address_t *addr);
int UDP_GetAddrFromName (const char *name, netadr_t *addr);
/** Compare two network addresses.
@ -177,14 +177,14 @@ int UDP_GetAddrFromName (const char *name, AF_address_t *addr);
is different.
\return 0 if everything is the same.
*/
int UDP_AddrCompare (AF_address_t *addr1, AF_address_t *addr2);
int UDP_AddrCompare (netadr_t *addr1, netadr_t *addr2);
/** Get the port number from the socket address.
\param addr The socket address from which to retrieve the port number.
\return The port number.
*/
int UDP_GetSocketPort (AF_address_t *addr);
int UDP_GetSocketPort (netadr_t *addr);
/** Set the port number of the socket address.
@ -192,7 +192,7 @@ int UDP_GetSocketPort (AF_address_t *addr);
\param port The port number to which the socket address will be set.
\return 0
*/
int UDP_SetSocketPort (AF_address_t *addr, int port);
int UDP_SetSocketPort (netadr_t *addr, int port);
//@}

View file

@ -49,9 +49,9 @@
typedef struct
{
#ifdef HAVE_IPV6
byte ip[16];
byte ip[16];
#else
byte ip[4];
byte ip[4];
#endif
unsigned short port;
unsigned short family;

View file

@ -29,16 +29,6 @@
#ifndef __net_h
#define __net_h
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
# define model_t sun_model_t
# include <netinet/in.h>
# undef model_t
#endif
#include "QF/quakeio.h"
#include "QF/sizebuf.h"
@ -47,15 +37,17 @@
*/
//@{
//FIXME our code is not yet ready for anything but ipv4 addresses
typedef union address {
// struct sockaddr_storage ss;
struct sockaddr sa;
struct sockaddr_in s4;
#ifdef HAVE_IPV6
// struct sockaddr_in6 s6;
#endif
} AF_address_t;
typedef struct
{
//FIXME not yet ready for ipv6
//#ifdef HAVE_IPV6
// byte ip[16];
//#else
byte ip[4];
//#endif
unsigned short port;
unsigned short family;
} netadr_t;
#define NET_NAMELEN 64
@ -207,7 +199,7 @@ typedef struct qsocket_s {
/// \name socket address
//@{
AF_address_t addr;
netadr_t addr;
char address[NET_NAMELEN]; ///< Human readable form.
//@}
} qsocket_t;
@ -268,7 +260,7 @@ typedef struct {
int maxusers;
int driver;
int ldriver;
AF_address_t addr;
netadr_t addr;
} hostcache_t;
extern int hostCacheCount;
@ -415,18 +407,18 @@ typedef struct {
void (*Listen) (qboolean state);
int (*OpenSocket) (int port);
int (*CloseSocket) (int socket);
int (*Connect) (int socket, AF_address_t *addr);
int (*Connect) (int socket, netadr_t *addr);
int (*CheckNewConnections) (void);
int (*Read) (int socket, byte *buf, int len, AF_address_t *addr);
int (*Write) (int socket, byte *buf, int len, AF_address_t *addr);
int (*Read) (int socket, byte *buf, int len, netadr_t *addr);
int (*Write) (int socket, byte *buf, int len, netadr_t *addr);
int (*Broadcast) (int socket, byte *buf, int len);
const char * (*AddrToString) (AF_address_t *addr);
int (*GetSocketAddr) (int socket, AF_address_t *addr);
int (*GetNameFromAddr) (AF_address_t *addr, char *name);
int (*GetAddrFromName) (const char *name, AF_address_t *addr);
int (*AddrCompare) (AF_address_t *addr1, AF_address_t *addr2);
int (*GetSocketPort) (AF_address_t *addr);
int (*SetSocketPort) (AF_address_t *addr, int port);
const char * (*AddrToString) (netadr_t *addr);
int (*GetSocketAddr) (int socket, netadr_t *addr);
int (*GetNameFromAddr) (netadr_t *addr, char *name);
int (*GetAddrFromName) (const char *name, netadr_t *addr);
int (*AddrCompare) (netadr_t *addr1, netadr_t *addr2);
int (*GetSocketPort) (netadr_t *addr);
int (*SetSocketPort) (netadr_t *addr, int port);
} net_landriver_t;
extern int net_numlandrivers;

View file

@ -299,7 +299,7 @@ Datagram_GetMessage (qsocket_t *sock)
unsigned int length;
unsigned int flags;
int ret = 0;
AF_address_t readaddr;
netadr_t readaddr;
unsigned int sequence;
unsigned int count;
@ -491,7 +491,7 @@ PollProcedure testPollProcedure = { NULL, 0.0, Test_Poll };
static void
Test_Poll (void *unused)
{
AF_address_t clientaddr;
netadr_t clientaddr;
int control;
int len;
char name[32]; //FIXME: overflow
@ -552,7 +552,7 @@ Test_f (void)
const char *host;
int n;
int max = MAX_SCOREBOARD;
AF_address_t sendaddr;
netadr_t sendaddr;
if (testInProgress)
return;
@ -568,7 +568,7 @@ Test_f (void)
max = hostcache[n].maxusers;
memcpy (&sendaddr, &hostcache[n].addr,
sizeof (AF_address_t));
sizeof (netadr_t));
break;
}
if (n < hostCacheCount)
@ -623,7 +623,7 @@ PollProcedure test2PollProcedure = { NULL, 0.0, Test2_Poll };
static void
Test2_Poll (void *unused)
{
AF_address_t clientaddr;
netadr_t clientaddr;
int control;
int len;
char name[256]; //FIXME: overflow
@ -689,7 +689,7 @@ Test2_f (void)
{
const char *host;
int n;
AF_address_t sendaddr;
netadr_t sendaddr;
if (test2InProgress)
return;
@ -704,7 +704,7 @@ Test2_f (void)
net_landriverlevel = hostcache[n].ldriver;
memcpy (&sendaddr, &hostcache[n].addr,
sizeof (AF_address_t));
sizeof (netadr_t));
break;
}
if (n < hostCacheCount)
@ -814,8 +814,8 @@ Datagram_Listen (qboolean state)
static qsocket_t *
_Datagram_CheckNewConnections (void)
{
AF_address_t clientaddr;
AF_address_t newaddr;
netadr_t clientaddr;
netadr_t newaddr;
int newsock;
int acceptsock;
qsocket_t *sock;
@ -977,10 +977,10 @@ _Datagram_CheckNewConnections (void)
}
#ifdef BAN_TEST
// check for a ban
if (clientaddr.sa.sa_family == AF_INET) {
if (clientaddr.family == AF_INET) {
unsigned testAddr;
testAddr = clientaddr.s4.sin_addr.s_addr;
memcpy (&testAddr, clientaddr.ip, 4);
if ((testAddr & banMask) == banAddr) {
SZ_Clear (net_message->message);
// save space for the header, filled in later
@ -1108,8 +1108,8 @@ _Datagram_SearchForHosts (qboolean xmit)
int ret;
int n;
int i;
AF_address_t readaddr;
AF_address_t myaddr;
netadr_t readaddr;
netadr_t myaddr;
int control;
dfunc.GetSocketAddr (dfunc.controlSock, &myaddr);
@ -1180,7 +1180,7 @@ _Datagram_SearchForHosts (qboolean xmit)
strcpy (hostcache[n].name, "*");
strcat (hostcache[n].name, hostcache[n].cname);
}
memcpy (&hostcache[n].addr, &readaddr, sizeof (AF_address_t));
memcpy (&hostcache[n].addr, &readaddr, sizeof (netadr_t));
hostcache[n].driver = net_driverlevel;
hostcache[n].ldriver = net_landriverlevel;
@ -1219,8 +1219,8 @@ Datagram_SearchForHosts (qboolean xmit)
static qsocket_t *
_Datagram_Connect (const char *host)
{
AF_address_t sendaddr;
AF_address_t readaddr;
netadr_t sendaddr;
netadr_t readaddr;
qsocket_t *sock;
int newsock;
int ret;
@ -1275,9 +1275,9 @@ _Datagram_Connect (const char *host)
if (sfunc.AddrCompare (&readaddr, &sendaddr) != 0) {
Sys_MaskPrintf (SYS_NET, "%2d ",
sfunc.AddrCompare (&readaddr, &sendaddr));
Sys_MaskPrintf (SYS_NET, "%d %s ", readaddr.sa.sa_family,
Sys_MaskPrintf (SYS_NET, "%d %s ", readaddr.family,
sfunc.AddrToString (&readaddr));
Sys_MaskPrintf (SYS_NET, "%d %s\n", sendaddr.sa.sa_family,
Sys_MaskPrintf (SYS_NET, "%d %s\n", sendaddr.family,
sfunc.AddrToString (&sendaddr));
ret = 0;
continue;
@ -1338,7 +1338,7 @@ _Datagram_Connect (const char *host)
}
if (ret == CCREP_ACCEPT) {
memcpy (&sock->addr, &sendaddr, sizeof (AF_address_t));
memcpy (&sock->addr, &sendaddr, sizeof (netadr_t));
dfunc.SetSocketPort (&sock->addr, MSG_ReadLong (net_message));
} else {

View file

@ -112,11 +112,57 @@ static __attribute__ ((used)) const char rcsid[] =
# endif
#endif
#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) (((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
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;
}
static int net_acceptsocket = -1; // socket for fielding new
// connections
static int net_controlsocket;
static int net_broadcastsocket = 0;
static AF_address_t broadcastaddr;
static netadr_t broadcastaddr;
static uint32_t myAddr;
@ -176,7 +222,7 @@ UDP_Init (void)
{
struct hostent *local;
char buff[MAXHOSTNAMELEN];
AF_address_t addr;
netadr_t addr;
char *colon;
#ifdef _WIN32
WSADATA winsockdata;
@ -209,12 +255,11 @@ UDP_Init (void)
get_iface_list (net_controlsocket);
{
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));
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);
}
UDP_GetSocketAddr (net_controlsocket, &addr);
@ -317,7 +362,7 @@ UDP_CloseSocket (int socket)
the local network components to fill in the rest
*/
static int
PartialIPAddress (const char *in, AF_address_t *hostaddr)
PartialIPAddress (const char *in, netadr_t *hostaddr)
{
char *buff;
char *b;
@ -352,9 +397,13 @@ PartialIPAddress (const char *in, AF_address_t *hostaddr)
else
port = net_hostport;
hostaddr->sa.sa_family = AF_INET;
hostaddr->s4.sin_port = htons ((short) port);
hostaddr->s4.sin_addr.s_addr = (myAddr & htonl (mask)) | htonl (addr);
hostaddr->family = AF_INET;
hostaddr->port = htons ((short) port);
{
int32_t t = myAddr & htonl (mask);
t |= htonl (addr);
memcpy (hostaddr->ip, &t, ADDR_SIZE);
}
free (buff);
return 0;
@ -364,7 +413,7 @@ error:
}
int
UDP_Connect (int socket, AF_address_t *addr)
UDP_Connect (int socket, netadr_t *addr)
{
return 0;
}
@ -388,20 +437,20 @@ UDP_CheckNewConnections (void)
// 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.
// we don't care about the interface on which the packet arrived
recvfrom (net_acceptsocket, buff, 0, 0, (struct sockaddr *) &from,
&fromlen);
recvfrom (net_acceptsocket, buff, 0, 0, &from.sa, &fromlen);
return -1;
}
int
UDP_Read (int socket, byte *buf, int len, AF_address_t *addr)
UDP_Read (int socket, byte *buf, int len, netadr_t *from)
{
int ret;
AF_address_t addr;
#ifdef HAVE_IN_PKTINFO
char ancillary[CMSG_SPACE (sizeof (struct in_pktinfo))];
struct msghdr msghdr = {
addr,
sizeof (*addr),
&addr,
sizeof (addr),
0, 0,
ancillary,
sizeof (ancillary),
@ -411,6 +460,7 @@ UDP_Read (int socket, byte *buf, int len, AF_address_t *addr)
struct cmsghdr *cmsg;
struct in_pktinfo *info = 0;
memset (&addr, 0, sizeof (addr));
msghdr.msg_iov = &iovec;
msghdr.msg_iovlen = 1;
ret = recvmsg (socket, &msghdr, 0);
@ -431,17 +481,20 @@ UDP_Read (int socket, byte *buf, int len, AF_address_t *addr)
*/
last_iface = &ifaces[info->ipi_ifindex - 1];
}
SockadrToNetadr (&addr, from);
Sys_MaskPrintf (SYS_NET, "got %d bytes from %s on iface %d (%s)\n", ret,
UDP_AddrToString (addr), info ? info->ipi_ifindex - 1 : -1,
UDP_AddrToString (from), info ? info->ipi_ifindex - 1 : -1,
last_iface ? inet_ntoa (info->ipi_addr) : "?");
#else
socklen_t addrlen = sizeof (AF_address_t);
ret = recvfrom (socket, buf, len, 0, (struct sockaddr *) addr, &addrlen);
memset (&addr, 0, sizeof (addr));
ret = recvfrom (socket, buf, len, 0, &addr, &addrlen);
if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED))
return 0;
SockadrToNetadr (&addr, from);
Sys_MaskPrintf (SYS_NET, "got %d bytes from %s\n", ret,
UDP_AddrToString (addr));
UDP_AddrToString (from));
last_iface = default_iface;
#endif
return ret;
@ -480,60 +533,61 @@ UDP_Broadcast (int socket, byte *buf, int len)
}
int
UDP_Write (int socket, byte *buf, int len, AF_address_t *addr)
UDP_Write (int socket, byte *buf, int len, netadr_t *to)
{
int ret;
AF_address_t addr;
ret = sendto (socket, buf, len, 0, (struct sockaddr *) addr,
sizeof (AF_address_t));
NetadrToSockadr (to, &addr);
ret = sendto (socket, buf, len, 0, &addr.sa, SA_LEN (&addr.sa));
if (ret == -1 && errno == EWOULDBLOCK)
return 0;
Sys_MaskPrintf (SYS_NET, "sent %d bytes to %s\n", ret, UDP_AddrToString (addr));
Sys_MaskPrintf (SYS_NET, "sent %d bytes to %s\n", ret,
UDP_AddrToString (to));
return ret;
}
const char *
UDP_AddrToString (AF_address_t *addr)
UDP_AddrToString (netadr_t *addr)
{
static dstring_t *buffer;
int haddr;
if (!buffer)
buffer = dstring_new ();
haddr = ntohl (addr->s4.sin_addr.s_addr);
dsprintf (buffer, "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff,
(haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff,
ntohs (addr->s4.sin_port));
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;
}
int
UDP_GetSocketAddr (int socket, AF_address_t *addr)
UDP_GetSocketAddr (int socket, netadr_t *na)
{
unsigned int a;
socklen_t addrlen = sizeof (AF_address_t);
AF_address_t addr;
memset (addr, 0, sizeof (AF_address_t));
memset (&addr, 0, sizeof (AF_address_t));
getsockname (socket, (struct sockaddr *) addr, &addrlen);
a = addr->s4.sin_addr.s_addr;
getsockname (socket, &addr.sa, &addrlen);
SockadrToNetadr (&addr, na);
memcpy (&a, na->ip, ADDR_SIZE);
if (a == 0 || a == inet_addr ("127.0.0.1")) {
addr->s4.sin_addr.s_addr = *default_iface;
memcpy (na->ip, default_iface, ADDR_SIZE);
if (last_iface)
addr->s4.sin_addr.s_addr = *last_iface;
memcpy (na->ip, last_iface, ADDR_SIZE);
}
return 0;
}
int
UDP_GetNameFromAddr (AF_address_t *addr, char *name)
UDP_GetNameFromAddr (netadr_t *addr, char *name)
{
struct hostent *hostentry;
hostentry = gethostbyaddr ((char *) &addr->s4.sin_addr,
sizeof (struct in_addr), AF_INET);
hostentry = gethostbyaddr (&addr->ip, ADDR_SIZE, AF_INET);
if (hostentry) {
strncpy (name, (char *) hostentry->h_name, NET_NAMELEN - 1);
@ -545,7 +599,7 @@ UDP_GetNameFromAddr (AF_address_t *addr, char *name)
}
int
UDP_GetAddrFromName (const char *name, AF_address_t *addr)
UDP_GetAddrFromName (const char *name, netadr_t *addr)
{
struct hostent *hostentry;
@ -556,38 +610,37 @@ UDP_GetAddrFromName (const char *name, AF_address_t *addr)
if (!hostentry)
return -1;
addr->sa.sa_family = AF_INET;
addr->s4.sin_port = htons (net_hostport);
addr->s4.sin_addr.s_addr = *(uint32_t *) hostentry->h_addr_list[0];
addr->family = AF_INET;
addr->port = htons (net_hostport);
memcpy (addr->ip, hostentry->h_addr_list[0], ADDR_SIZE);
return 0;
}
int
UDP_AddrCompare (AF_address_t *addr1, AF_address_t *addr2)
UDP_AddrCompare (netadr_t *addr1, netadr_t *addr2)
{
if (addr1->sa.sa_family != addr2->sa.sa_family)
if (addr1->family != addr2->family)
return -2;
if (addr1->s4.sin_addr.s_addr != addr2->s4.sin_addr.s_addr)
if (memcmp (addr1->ip, addr2->ip, ADDR_SIZE))
return -1;
if (addr1->s4.sin_port != addr2->s4.sin_port)
if (addr1->port != addr2->port)
return 1;
return 0;
}
int
UDP_GetSocketPort (AF_address_t *addr)
UDP_GetSocketPort (netadr_t *addr)
{
return ntohs (addr->s4.sin_port);
return ntohs (addr->port);
}
int
UDP_SetSocketPort (AF_address_t *addr, int port)
UDP_SetSocketPort (netadr_t *addr, int port)
{
addr->s4.sin_port = htons (port);
addr->port = htons (port);
return 0;
}