From 0d5bebabe235f900c7b2049432b43ce9c4c6439b Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 24 Aug 2011 09:14:02 +0900 Subject: [PATCH] Replace qsockaddr with AF_address_t. It seems qsockaddr's assumptions aren't necessarily portable, as OpenBSD seems to be doing weird things with qsa_family. Even if that's not the case, this is cleaner. --- include/net_udp.h | 20 +++++----- include/netmain.h | 48 +++++++++++++++--------- libs/net/nm/net_dgrm.c | 38 +++++++++---------- libs/net/nm/net_udp.c | 83 +++++++++++++++++++----------------------- nq/source/sys_sdl.c | 4 +- nq/source/sys_unix.c | 4 +- nq/source/sys_unixd.c | 4 +- nq/source/sys_win.c | 4 +- nq/source/sys_wind.c | 4 +- 9 files changed, 107 insertions(+), 102 deletions(-) diff --git a/include/net_udp.h b/include/net_udp.h index b85d69000..0931f38bb 100644 --- a/include/net_udp.h +++ b/include/net_udp.h @@ -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, struct qsockaddr *addr); +int UDP_Connect (int socket, AF_address_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, struct qsockaddr *addr); +int UDP_Read (int socket, byte *buf, int len, AF_address_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, struct qsockaddr *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, struct qsockaddr *addr); +int UDP_Write (int socket, byte *buf, int len, AF_address_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 (struct qsockaddr *addr); +const char *UDP_AddrToString (AF_address_t *addr); /** Retrieve the address to which the socket is bound. @@ -138,7 +138,7 @@ const char *UDP_AddrToString (struct qsockaddr *addr); \param[out] addr The address to which the socket is bound. \return 0 */ -int UDP_GetSocketAddr (int socket, struct qsockaddr *addr); +int UDP_GetSocketAddr (int socket, AF_address_t *addr); /** Convert an address to a hostname. @@ -147,7 +147,7 @@ int UDP_GetSocketAddr (int socket, struct qsockaddr *addr); \bug No checking is done on the size of the buffer, and uses strcpy. */ -int UDP_GetNameFromAddr (struct qsockaddr *addr, char *name); +int UDP_GetNameFromAddr (AF_address_t *addr, char *name); /** Convert a human readable address to a quake address. @@ -162,7 +162,7 @@ int UDP_GetNameFromAddr (struct qsockaddr *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, struct qsockaddr *addr); +int UDP_GetAddrFromName (const char *name, AF_address_t *addr); /** Compare two network addresses. @@ -177,14 +177,14 @@ int UDP_GetAddrFromName (const char *name, struct qsockaddr *addr); is different. \return 0 if everything is the same. */ -int UDP_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2); +int UDP_AddrCompare (AF_address_t *addr1, AF_address_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 (struct qsockaddr *addr); +int UDP_GetSocketPort (AF_address_t *addr); /** Set the port number of the socket address. @@ -192,7 +192,7 @@ int UDP_GetSocketPort (struct qsockaddr *addr); \param port The port number to which the socket address will be set. \return 0 */ -int UDP_SetSocketPort (struct qsockaddr *addr, int port); +int UDP_SetSocketPort (AF_address_t *addr, int port); //@} diff --git a/include/netmain.h b/include/netmain.h index 57d989a4a..7c0df05b9 100644 --- a/include/netmain.h +++ b/include/netmain.h @@ -29,6 +29,15 @@ #ifndef __net_h #define __net_h +#ifdef HAVE_SYS_SOCKET_H +# include +#endif +#ifdef HAVE_NETINET_IN_H +# define model_t sun_model_t +# include +# undef model_t +#endif + #include "QF/quakeio.h" #include "QF/sizebuf.h" @@ -37,11 +46,14 @@ */ //@{ -struct qsockaddr { - short qsa_family; - unsigned char qsa_data[14]; -}; - +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; #define NET_NAMELEN 64 @@ -193,8 +205,8 @@ typedef struct qsocket_s { /// \name socket address //@{ - struct qsockaddr addr; - char address[NET_NAMELEN]; ///< Human readable form. + AF_address_t addr; + char address[NET_NAMELEN]; ///< Human readable form. //@} } qsocket_t; @@ -254,7 +266,7 @@ typedef struct { int maxusers; int driver; int ldriver; - struct qsockaddr addr; + AF_address_t addr; } hostcache_t; extern int hostCacheCount; @@ -401,18 +413,18 @@ typedef struct { void (*Listen) (qboolean state); int (*OpenSocket) (int port); int (*CloseSocket) (int socket); - int (*Connect) (int socket, struct qsockaddr *addr); + int (*Connect) (int socket, AF_address_t *addr); int (*CheckNewConnections) (void); - int (*Read) (int socket, byte *buf, int len, struct qsockaddr *addr); - int (*Write) (int socket, byte *buf, int len, struct qsockaddr *addr); + 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 (*Broadcast) (int socket, byte *buf, int len); - const char * (*AddrToString) (struct qsockaddr *addr); - int (*GetSocketAddr) (int socket, struct qsockaddr *addr); - int (*GetNameFromAddr) (struct qsockaddr *addr, char *name); - int (*GetAddrFromName) (const char *name, struct qsockaddr *addr); - int (*AddrCompare) (struct qsockaddr *addr1, struct qsockaddr *addr2); - int (*GetSocketPort) (struct qsockaddr *addr); - int (*SetSocketPort) (struct qsockaddr *addr, int port); + 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); } net_landriver_t; extern int net_numlandrivers; diff --git a/libs/net/nm/net_dgrm.c b/libs/net/nm/net_dgrm.c index ac0e87c03..8897febd3 100644 --- a/libs/net/nm/net_dgrm.c +++ b/libs/net/nm/net_dgrm.c @@ -299,7 +299,7 @@ Datagram_GetMessage (qsocket_t *sock) unsigned int length; unsigned int flags; int ret = 0; - struct qsockaddr readaddr; + AF_address_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) { - struct qsockaddr clientaddr; + AF_address_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; - struct qsockaddr sendaddr; + AF_address_t sendaddr; if (testInProgress) return; @@ -568,7 +568,7 @@ Test_f (void) max = hostcache[n].maxusers; memcpy (&sendaddr, &hostcache[n].addr, - sizeof (struct qsockaddr)); + sizeof (AF_address_t)); break; } if (n < hostCacheCount) @@ -623,7 +623,7 @@ PollProcedure test2PollProcedure = { NULL, 0.0, Test2_Poll }; static void Test2_Poll (void *unused) { - struct qsockaddr clientaddr; + AF_address_t clientaddr; int control; int len; char name[256]; //FIXME: overflow @@ -689,7 +689,7 @@ Test2_f (void) { const char *host; int n; - struct qsockaddr sendaddr; + AF_address_t sendaddr; if (test2InProgress) return; @@ -704,7 +704,7 @@ Test2_f (void) net_landriverlevel = hostcache[n].ldriver; memcpy (&sendaddr, &hostcache[n].addr, - sizeof (struct qsockaddr)); + sizeof (AF_address_t)); break; } if (n < hostCacheCount) @@ -814,8 +814,8 @@ Datagram_Listen (qboolean state) static qsocket_t * _Datagram_CheckNewConnections (void) { - struct qsockaddr clientaddr; - struct qsockaddr newaddr; + AF_address_t clientaddr; + AF_address_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.qsa_family == AF_INET) { + if (clientaddr.sa.sa_family == AF_INET) { unsigned testAddr; - testAddr = ((struct sockaddr_in *) &clientaddr)->sin_addr.s_addr; + testAddr = clientaddr.s4.sin_addr.s_addr; 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; - struct qsockaddr readaddr; - struct qsockaddr myaddr; + AF_address_t readaddr; + AF_address_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 (struct qsockaddr)); + memcpy (&hostcache[n].addr, &readaddr, sizeof (AF_address_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) { - struct qsockaddr sendaddr; - struct qsockaddr readaddr; + AF_address_t sendaddr; + AF_address_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.qsa_family, + Sys_MaskPrintf (SYS_NET, "%d %s ", readaddr.sa.sa_family, sfunc.AddrToString (&readaddr)); - Sys_MaskPrintf (SYS_NET, "%d %s\n", sendaddr.qsa_family, + Sys_MaskPrintf (SYS_NET, "%d %s\n", sendaddr.sa.sa_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 (struct qsockaddr)); + memcpy (&sock->addr, &sendaddr, sizeof (AF_address_t)); dfunc.SetSocketPort (&sock->addr, MSG_ReadLong (net_message)); } else { diff --git a/libs/net/nm/net_udp.c b/libs/net/nm/net_udp.c index 8166b33b5..ae57dd393 100644 --- a/libs/net/nm/net_udp.c +++ b/libs/net/nm/net_udp.c @@ -91,6 +91,7 @@ static __attribute__ ((used)) const char rcsid[] = #include "compat.h" #include "netmain.h" +#include "net_udp.h" #ifdef _WIN32 # undef EWOULDBLOCK @@ -115,12 +116,10 @@ static int net_acceptsocket = -1; // socket for fielding new // connections static int net_controlsocket; static int net_broadcastsocket = 0; -static struct qsockaddr broadcastaddr; +static AF_address_t broadcastaddr; static uint32_t myAddr; -#include "net_udp.h" - static int num_ifaces; uint32_t *ifaces; uint32_t *default_iface; @@ -177,7 +176,7 @@ UDP_Init (void) { struct hostent *local; char buff[MAXHOSTNAMELEN]; - struct qsockaddr addr; + AF_address_t addr; char *colon; #ifdef _WIN32 WSADATA winsockdata; @@ -318,7 +317,7 @@ UDP_CloseSocket (int socket) the local network components to fill in the rest */ static int -PartialIPAddress (const char *in, struct qsockaddr *hostaddr) +PartialIPAddress (const char *in, AF_address_t *hostaddr) { char *buff; char *b; @@ -353,11 +352,9 @@ PartialIPAddress (const char *in, struct qsockaddr *hostaddr) else port = net_hostport; - hostaddr->qsa_family = AF_INET; - ((struct sockaddr_in *) hostaddr)->sin_port = htons ((short) port); - - ((struct sockaddr_in *) hostaddr)->sin_addr.s_addr = - (myAddr & htonl (mask)) | htonl (addr); + hostaddr->sa.sa_family = AF_INET; + hostaddr->s4.sin_port = htons ((short) port); + hostaddr->s4.sin_addr.s_addr = (myAddr & htonl (mask)) | htonl (addr); free (buff); return 0; @@ -367,7 +364,7 @@ error: } int -UDP_Connect (int socket, struct qsockaddr *addr) +UDP_Connect (int socket, AF_address_t *addr) { return 0; } @@ -376,8 +373,8 @@ int UDP_CheckNewConnections (void) { unsigned long available; - struct sockaddr_in from; - socklen_t fromlen; + AF_address_t from; + socklen_t fromlen = sizeof (from); char buff[1]; if (net_acceptsocket == -1) @@ -397,7 +394,7 @@ UDP_CheckNewConnections (void) } int -UDP_Read (int socket, byte *buf, int len, struct qsockaddr *addr) +UDP_Read (int socket, byte *buf, int len, AF_address_t *addr) { int ret; #ifdef HAVE_IN_PKTINFO @@ -438,7 +435,7 @@ UDP_Read (int socket, byte *buf, int len, struct qsockaddr *addr) UDP_AddrToString (addr), info ? info->ipi_ifindex - 1 : -1, last_iface ? inet_ntoa (info->ipi_addr) : "?"); #else - socklen_t addrlen = sizeof (struct qsockaddr); + socklen_t addrlen = sizeof (AF_address_t); ret = recvfrom (socket, buf, len, 0, (struct sockaddr *) addr, &addrlen); if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED)) @@ -483,12 +480,12 @@ UDP_Broadcast (int socket, byte *buf, int len) } int -UDP_Write (int socket, byte *buf, int len, struct qsockaddr *addr) +UDP_Write (int socket, byte *buf, int len, AF_address_t *addr) { int ret; ret = sendto (socket, buf, len, 0, (struct sockaddr *) addr, - sizeof (struct qsockaddr)); + sizeof (AF_address_t)); if (ret == -1 && errno == EWOULDBLOCK) return 0; Sys_MaskPrintf (SYS_NET, "sent %d bytes to %s\n", ret, UDP_AddrToString (addr)); @@ -496,7 +493,7 @@ UDP_Write (int socket, byte *buf, int len, struct qsockaddr *addr) } const char * -UDP_AddrToString (struct qsockaddr *addr) +UDP_AddrToString (AF_address_t *addr) { static dstring_t *buffer; int haddr; @@ -504,40 +501,39 @@ UDP_AddrToString (struct qsockaddr *addr) if (!buffer) buffer = dstring_new (); - haddr = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr); + 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 (((struct sockaddr_in *) addr)->sin_port)); + ntohs (addr->s4.sin_port)); return buffer->str; } int -UDP_GetSocketAddr (int socket, struct qsockaddr *addr) +UDP_GetSocketAddr (int socket, AF_address_t *addr) { unsigned int a; - socklen_t addrlen = sizeof (struct qsockaddr); + socklen_t addrlen = sizeof (AF_address_t); - memset (addr, 0, sizeof (struct qsockaddr)); + memset (addr, 0, sizeof (AF_address_t)); getsockname (socket, (struct sockaddr *) addr, &addrlen); - a = ((struct sockaddr_in *) addr)->sin_addr.s_addr; + a = addr->s4.sin_addr.s_addr; if (a == 0 || a == inet_addr ("127.0.0.1")) { - ((struct sockaddr_in *) addr)->sin_addr.s_addr = *default_iface; + addr->s4.sin_addr.s_addr = *default_iface; if (last_iface) - ((struct sockaddr_in *) addr)->sin_addr.s_addr = *last_iface; + addr->s4.sin_addr.s_addr = *last_iface; } return 0; } int -UDP_GetNameFromAddr (struct qsockaddr *addr, char *name) +UDP_GetNameFromAddr (AF_address_t *addr, char *name) { struct hostent *hostentry; - hostentry = - gethostbyaddr ((char *) &((struct sockaddr_in *) addr)->sin_addr, - sizeof (struct in_addr), AF_INET); + hostentry = gethostbyaddr ((char *) &addr->s4.sin_addr, + sizeof (struct in_addr), AF_INET); if (hostentry) { strncpy (name, (char *) hostentry->h_name, NET_NAMELEN - 1); @@ -549,7 +545,7 @@ UDP_GetNameFromAddr (struct qsockaddr *addr, char *name) } int -UDP_GetAddrFromName (const char *name, struct qsockaddr *addr) +UDP_GetAddrFromName (const char *name, AF_address_t *addr) { struct hostent *hostentry; @@ -560,41 +556,38 @@ UDP_GetAddrFromName (const char *name, struct qsockaddr *addr) if (!hostentry) return -1; - addr->qsa_family = AF_INET; - ((struct sockaddr_in *) addr)->sin_port = htons (net_hostport); + addr->sa.sa_family = AF_INET; + addr->s4.sin_port = htons (net_hostport); - ((struct sockaddr_in *) addr)->sin_addr.s_addr = - *(int *) hostentry->h_addr_list[0]; + addr->s4.sin_addr.s_addr = *(uint32_t *) hostentry->h_addr_list[0]; return 0; } int -UDP_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2) +UDP_AddrCompare (AF_address_t *addr1, AF_address_t *addr2) { - if (addr1->qsa_family != addr2->qsa_family) + if (addr1->sa.sa_family != addr2->sa.sa_family) return -2; - if (((struct sockaddr_in *) addr1)->sin_addr.s_addr != - ((struct sockaddr_in *) addr2)->sin_addr.s_addr) + if (addr1->s4.sin_addr.s_addr != addr2->s4.sin_addr.s_addr) return -1; - if (((struct sockaddr_in *) addr1)->sin_port != - ((struct sockaddr_in *) addr2)->sin_port) + if (addr1->s4.sin_port != addr2->s4.sin_port) return 1; return 0; } int -UDP_GetSocketPort (struct qsockaddr *addr) +UDP_GetSocketPort (AF_address_t *addr) { - return ntohs (((struct sockaddr_in *) addr)->sin_port); + return ntohs (addr->s4.sin_port); } int -UDP_SetSocketPort (struct qsockaddr *addr, int port) +UDP_SetSocketPort (AF_address_t *addr, int port) { - ((struct sockaddr_in *) addr)->sin_port = htons (port); + addr->s4.sin_port = htons (port); return 0; } diff --git a/nq/source/sys_sdl.c b/nq/source/sys_sdl.c index e6543b3ee..fe9c38cab 100644 --- a/nq/source/sys_sdl.c +++ b/nq/source/sys_sdl.c @@ -100,7 +100,7 @@ startup (void) } static void -shutdown (void) +shutdown_f (void) { #ifndef _WIN32 fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK); @@ -131,7 +131,7 @@ SDL_main (int c, char **v) #endif Sys_RegisterShutdown (Host_Shutdown); - Sys_RegisterShutdown (shutdown); + Sys_RegisterShutdown (shutdown_f); Host_Init (); diff --git a/nq/source/sys_unix.c b/nq/source/sys_unix.c index 5ed1bfbd0..895c6aba1 100644 --- a/nq/source/sys_unix.c +++ b/nq/source/sys_unix.c @@ -62,7 +62,7 @@ static __attribute__ ((used)) const char rcsid[] = qboolean isDedicated = false; static void -shutdown (void) +shutdown_f (void) { // change stdin to blocking fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK); @@ -85,7 +85,7 @@ main (int c, const char *v[]) fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) | O_NONBLOCK); Sys_RegisterShutdown (Host_Shutdown); - Sys_RegisterShutdown (shutdown); + Sys_RegisterShutdown (shutdown_f); Host_Init (); diff --git a/nq/source/sys_unixd.c b/nq/source/sys_unixd.c index 62f8f8357..9a19e326a 100644 --- a/nq/source/sys_unixd.c +++ b/nq/source/sys_unixd.c @@ -60,7 +60,7 @@ qboolean isDedicated = true; int nostdout = 0; static void -shutdown (void) +shutdown_f (void) { fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK); fflush (stdout); @@ -95,7 +95,7 @@ main (int argc, const char **argv) fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) | O_NONBLOCK); Sys_RegisterShutdown (Host_Shutdown); - Sys_RegisterShutdown (shutdown); + Sys_RegisterShutdown (shutdown_f); Host_Init (); diff --git a/nq/source/sys_win.c b/nq/source/sys_win.c index 0ae97bbf6..d9660d6c1 100644 --- a/nq/source/sys_win.c +++ b/nq/source/sys_win.c @@ -112,7 +112,7 @@ startup (void) } static void -shutdown (void) +shutdown_f (void) { if (tevent) CloseHandle (tevent); @@ -214,7 +214,7 @@ WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, Host_Init (); Sys_RegisterShutdown (Host_Shutdown); - Sys_RegisterShutdown (shutdown); + Sys_RegisterShutdown (shutdown_f); oldtime = Sys_DoubleTime (); diff --git a/nq/source/sys_wind.c b/nq/source/sys_wind.c index ea8c53780..c85ee0076 100644 --- a/nq/source/sys_wind.c +++ b/nq/source/sys_wind.c @@ -48,7 +48,7 @@ static __attribute__ ((used)) const char rcsid[] = qboolean isDedicated = true; static void -shutdown (void) +shutdown_f (void) { } @@ -84,7 +84,7 @@ main (int argc, const char **argv) host_parms.argv = com_argv; Sys_RegisterShutdown (Host_Shutdown); - Sys_RegisterShutdown (shutdown); + Sys_RegisterShutdown (shutdown_f); Host_Init ();