mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2025-01-31 05:00:35 +00:00
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:
parent
5ee01879ec
commit
e50f0e18ba
5 changed files with 160 additions and 115 deletions
|
@ -80,7 +80,7 @@ int UDP_CloseSocket (int socket);
|
||||||
\param addr The address to which very little will be done.
|
\param addr The address to which very little will be done.
|
||||||
\return 0
|
\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.
|
/** 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.
|
\param[out] addr The address from which the packet originated.
|
||||||
\return The number of bytes read or -1 on error.
|
\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.
|
/** 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.
|
\param addr The addres to which the packet will be sent.
|
||||||
\return The number of bytes sent or -1 on error.
|
\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.
|
/** 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.
|
\param addr The address to convert.
|
||||||
\return The address in human readable form.
|
\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.
|
/** 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.
|
\param[out] addr The address to which the socket is bound.
|
||||||
\return 0
|
\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.
|
/** 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.
|
\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.
|
/** 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.
|
\param addr The resulting address of the conversion.
|
||||||
\return 0 if the conversion is successful, otherwise -1.
|
\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.
|
/** Compare two network addresses.
|
||||||
|
|
||||||
|
@ -177,14 +177,14 @@ int UDP_GetAddrFromName (const char *name, AF_address_t *addr);
|
||||||
is different.
|
is different.
|
||||||
\return 0 if everything is the same.
|
\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.
|
/** Get the port number from the socket address.
|
||||||
|
|
||||||
\param addr The socket address from which to retrieve the port number.
|
\param addr The socket address from which to retrieve the port number.
|
||||||
\return 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.
|
/** 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.
|
\param port The port number to which the socket address will be set.
|
||||||
\return 0
|
\return 0
|
||||||
*/
|
*/
|
||||||
int UDP_SetSocketPort (AF_address_t *addr, int port);
|
int UDP_SetSocketPort (netadr_t *addr, int port);
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
|
|
@ -49,9 +49,9 @@
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
byte ip[16];
|
byte ip[16];
|
||||||
#else
|
#else
|
||||||
byte ip[4];
|
byte ip[4];
|
||||||
#endif
|
#endif
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
unsigned short family;
|
unsigned short family;
|
||||||
|
|
|
@ -29,16 +29,6 @@
|
||||||
#ifndef __net_h
|
#ifndef __net_h
|
||||||
#define __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/quakeio.h"
|
||||||
#include "QF/sizebuf.h"
|
#include "QF/sizebuf.h"
|
||||||
|
|
||||||
|
@ -47,15 +37,17 @@
|
||||||
*/
|
*/
|
||||||
//@{
|
//@{
|
||||||
|
|
||||||
//FIXME our code is not yet ready for anything but ipv4 addresses
|
typedef struct
|
||||||
typedef union address {
|
{
|
||||||
// struct sockaddr_storage ss;
|
//FIXME not yet ready for ipv6
|
||||||
struct sockaddr sa;
|
//#ifdef HAVE_IPV6
|
||||||
struct sockaddr_in s4;
|
// byte ip[16];
|
||||||
#ifdef HAVE_IPV6
|
//#else
|
||||||
// struct sockaddr_in6 s6;
|
byte ip[4];
|
||||||
#endif
|
//#endif
|
||||||
} AF_address_t;
|
unsigned short port;
|
||||||
|
unsigned short family;
|
||||||
|
} netadr_t;
|
||||||
|
|
||||||
#define NET_NAMELEN 64
|
#define NET_NAMELEN 64
|
||||||
|
|
||||||
|
@ -207,7 +199,7 @@ typedef struct qsocket_s {
|
||||||
|
|
||||||
/// \name socket address
|
/// \name socket address
|
||||||
//@{
|
//@{
|
||||||
AF_address_t addr;
|
netadr_t addr;
|
||||||
char address[NET_NAMELEN]; ///< Human readable form.
|
char address[NET_NAMELEN]; ///< Human readable form.
|
||||||
//@}
|
//@}
|
||||||
} qsocket_t;
|
} qsocket_t;
|
||||||
|
@ -268,7 +260,7 @@ typedef struct {
|
||||||
int maxusers;
|
int maxusers;
|
||||||
int driver;
|
int driver;
|
||||||
int ldriver;
|
int ldriver;
|
||||||
AF_address_t addr;
|
netadr_t addr;
|
||||||
} hostcache_t;
|
} hostcache_t;
|
||||||
|
|
||||||
extern int hostCacheCount;
|
extern int hostCacheCount;
|
||||||
|
@ -415,18 +407,18 @@ typedef struct {
|
||||||
void (*Listen) (qboolean state);
|
void (*Listen) (qboolean state);
|
||||||
int (*OpenSocket) (int port);
|
int (*OpenSocket) (int port);
|
||||||
int (*CloseSocket) (int socket);
|
int (*CloseSocket) (int socket);
|
||||||
int (*Connect) (int socket, AF_address_t *addr);
|
int (*Connect) (int socket, netadr_t *addr);
|
||||||
int (*CheckNewConnections) (void);
|
int (*CheckNewConnections) (void);
|
||||||
int (*Read) (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, AF_address_t *addr);
|
int (*Write) (int socket, byte *buf, int len, netadr_t *addr);
|
||||||
int (*Broadcast) (int socket, byte *buf, int len);
|
int (*Broadcast) (int socket, byte *buf, int len);
|
||||||
const char * (*AddrToString) (AF_address_t *addr);
|
const char * (*AddrToString) (netadr_t *addr);
|
||||||
int (*GetSocketAddr) (int socket, AF_address_t *addr);
|
int (*GetSocketAddr) (int socket, netadr_t *addr);
|
||||||
int (*GetNameFromAddr) (AF_address_t *addr, char *name);
|
int (*GetNameFromAddr) (netadr_t *addr, char *name);
|
||||||
int (*GetAddrFromName) (const char *name, AF_address_t *addr);
|
int (*GetAddrFromName) (const char *name, netadr_t *addr);
|
||||||
int (*AddrCompare) (AF_address_t *addr1, AF_address_t *addr2);
|
int (*AddrCompare) (netadr_t *addr1, netadr_t *addr2);
|
||||||
int (*GetSocketPort) (AF_address_t *addr);
|
int (*GetSocketPort) (netadr_t *addr);
|
||||||
int (*SetSocketPort) (AF_address_t *addr, int port);
|
int (*SetSocketPort) (netadr_t *addr, int port);
|
||||||
} net_landriver_t;
|
} net_landriver_t;
|
||||||
|
|
||||||
extern int net_numlandrivers;
|
extern int net_numlandrivers;
|
||||||
|
|
|
@ -299,7 +299,7 @@ Datagram_GetMessage (qsocket_t *sock)
|
||||||
unsigned int length;
|
unsigned int length;
|
||||||
unsigned int flags;
|
unsigned int flags;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
AF_address_t readaddr;
|
netadr_t readaddr;
|
||||||
unsigned int sequence;
|
unsigned int sequence;
|
||||||
unsigned int count;
|
unsigned int count;
|
||||||
|
|
||||||
|
@ -491,7 +491,7 @@ PollProcedure testPollProcedure = { NULL, 0.0, Test_Poll };
|
||||||
static void
|
static void
|
||||||
Test_Poll (void *unused)
|
Test_Poll (void *unused)
|
||||||
{
|
{
|
||||||
AF_address_t clientaddr;
|
netadr_t clientaddr;
|
||||||
int control;
|
int control;
|
||||||
int len;
|
int len;
|
||||||
char name[32]; //FIXME: overflow
|
char name[32]; //FIXME: overflow
|
||||||
|
@ -552,7 +552,7 @@ Test_f (void)
|
||||||
const char *host;
|
const char *host;
|
||||||
int n;
|
int n;
|
||||||
int max = MAX_SCOREBOARD;
|
int max = MAX_SCOREBOARD;
|
||||||
AF_address_t sendaddr;
|
netadr_t sendaddr;
|
||||||
|
|
||||||
if (testInProgress)
|
if (testInProgress)
|
||||||
return;
|
return;
|
||||||
|
@ -568,7 +568,7 @@ Test_f (void)
|
||||||
max = hostcache[n].maxusers;
|
max = hostcache[n].maxusers;
|
||||||
memcpy (&sendaddr, &hostcache[n].addr,
|
memcpy (&sendaddr, &hostcache[n].addr,
|
||||||
|
|
||||||
sizeof (AF_address_t));
|
sizeof (netadr_t));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (n < hostCacheCount)
|
if (n < hostCacheCount)
|
||||||
|
@ -623,7 +623,7 @@ PollProcedure test2PollProcedure = { NULL, 0.0, Test2_Poll };
|
||||||
static void
|
static void
|
||||||
Test2_Poll (void *unused)
|
Test2_Poll (void *unused)
|
||||||
{
|
{
|
||||||
AF_address_t clientaddr;
|
netadr_t clientaddr;
|
||||||
int control;
|
int control;
|
||||||
int len;
|
int len;
|
||||||
char name[256]; //FIXME: overflow
|
char name[256]; //FIXME: overflow
|
||||||
|
@ -689,7 +689,7 @@ Test2_f (void)
|
||||||
{
|
{
|
||||||
const char *host;
|
const char *host;
|
||||||
int n;
|
int n;
|
||||||
AF_address_t sendaddr;
|
netadr_t sendaddr;
|
||||||
|
|
||||||
if (test2InProgress)
|
if (test2InProgress)
|
||||||
return;
|
return;
|
||||||
|
@ -704,7 +704,7 @@ Test2_f (void)
|
||||||
net_landriverlevel = hostcache[n].ldriver;
|
net_landriverlevel = hostcache[n].ldriver;
|
||||||
memcpy (&sendaddr, &hostcache[n].addr,
|
memcpy (&sendaddr, &hostcache[n].addr,
|
||||||
|
|
||||||
sizeof (AF_address_t));
|
sizeof (netadr_t));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (n < hostCacheCount)
|
if (n < hostCacheCount)
|
||||||
|
@ -814,8 +814,8 @@ Datagram_Listen (qboolean state)
|
||||||
static qsocket_t *
|
static qsocket_t *
|
||||||
_Datagram_CheckNewConnections (void)
|
_Datagram_CheckNewConnections (void)
|
||||||
{
|
{
|
||||||
AF_address_t clientaddr;
|
netadr_t clientaddr;
|
||||||
AF_address_t newaddr;
|
netadr_t newaddr;
|
||||||
int newsock;
|
int newsock;
|
||||||
int acceptsock;
|
int acceptsock;
|
||||||
qsocket_t *sock;
|
qsocket_t *sock;
|
||||||
|
@ -977,10 +977,10 @@ _Datagram_CheckNewConnections (void)
|
||||||
}
|
}
|
||||||
#ifdef BAN_TEST
|
#ifdef BAN_TEST
|
||||||
// check for a ban
|
// check for a ban
|
||||||
if (clientaddr.sa.sa_family == AF_INET) {
|
if (clientaddr.family == AF_INET) {
|
||||||
unsigned testAddr;
|
unsigned testAddr;
|
||||||
|
|
||||||
testAddr = clientaddr.s4.sin_addr.s_addr;
|
memcpy (&testAddr, clientaddr.ip, 4);
|
||||||
if ((testAddr & banMask) == banAddr) {
|
if ((testAddr & banMask) == banAddr) {
|
||||||
SZ_Clear (net_message->message);
|
SZ_Clear (net_message->message);
|
||||||
// save space for the header, filled in later
|
// save space for the header, filled in later
|
||||||
|
@ -1108,8 +1108,8 @@ _Datagram_SearchForHosts (qboolean xmit)
|
||||||
int ret;
|
int ret;
|
||||||
int n;
|
int n;
|
||||||
int i;
|
int i;
|
||||||
AF_address_t readaddr;
|
netadr_t readaddr;
|
||||||
AF_address_t myaddr;
|
netadr_t myaddr;
|
||||||
int control;
|
int control;
|
||||||
|
|
||||||
dfunc.GetSocketAddr (dfunc.controlSock, &myaddr);
|
dfunc.GetSocketAddr (dfunc.controlSock, &myaddr);
|
||||||
|
@ -1180,7 +1180,7 @@ _Datagram_SearchForHosts (qboolean xmit)
|
||||||
strcpy (hostcache[n].name, "*");
|
strcpy (hostcache[n].name, "*");
|
||||||
strcat (hostcache[n].name, hostcache[n].cname);
|
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].driver = net_driverlevel;
|
||||||
hostcache[n].ldriver = net_landriverlevel;
|
hostcache[n].ldriver = net_landriverlevel;
|
||||||
|
@ -1219,8 +1219,8 @@ Datagram_SearchForHosts (qboolean xmit)
|
||||||
static qsocket_t *
|
static qsocket_t *
|
||||||
_Datagram_Connect (const char *host)
|
_Datagram_Connect (const char *host)
|
||||||
{
|
{
|
||||||
AF_address_t sendaddr;
|
netadr_t sendaddr;
|
||||||
AF_address_t readaddr;
|
netadr_t readaddr;
|
||||||
qsocket_t *sock;
|
qsocket_t *sock;
|
||||||
int newsock;
|
int newsock;
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -1275,9 +1275,9 @@ _Datagram_Connect (const char *host)
|
||||||
if (sfunc.AddrCompare (&readaddr, &sendaddr) != 0) {
|
if (sfunc.AddrCompare (&readaddr, &sendaddr) != 0) {
|
||||||
Sys_MaskPrintf (SYS_NET, "%2d ",
|
Sys_MaskPrintf (SYS_NET, "%2d ",
|
||||||
sfunc.AddrCompare (&readaddr, &sendaddr));
|
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));
|
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));
|
sfunc.AddrToString (&sendaddr));
|
||||||
ret = 0;
|
ret = 0;
|
||||||
continue;
|
continue;
|
||||||
|
@ -1338,7 +1338,7 @@ _Datagram_Connect (const char *host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == CCREP_ACCEPT) {
|
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));
|
dfunc.SetSocketPort (&sock->addr, MSG_ReadLong (net_message));
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -112,11 +112,57 @@ static __attribute__ ((used)) const char rcsid[] =
|
||||||
# endif
|
# endif
|
||||||
#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
|
static int net_acceptsocket = -1; // socket for fielding new
|
||||||
// connections
|
// connections
|
||||||
static int net_controlsocket;
|
static int net_controlsocket;
|
||||||
static int net_broadcastsocket = 0;
|
static int net_broadcastsocket = 0;
|
||||||
static AF_address_t broadcastaddr;
|
static netadr_t broadcastaddr;
|
||||||
|
|
||||||
static uint32_t myAddr;
|
static uint32_t myAddr;
|
||||||
|
|
||||||
|
@ -176,7 +222,7 @@ UDP_Init (void)
|
||||||
{
|
{
|
||||||
struct hostent *local;
|
struct hostent *local;
|
||||||
char buff[MAXHOSTNAMELEN];
|
char buff[MAXHOSTNAMELEN];
|
||||||
AF_address_t addr;
|
netadr_t addr;
|
||||||
char *colon;
|
char *colon;
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WSADATA winsockdata;
|
WSADATA winsockdata;
|
||||||
|
@ -209,12 +255,11 @@ UDP_Init (void)
|
||||||
get_iface_list (net_controlsocket);
|
get_iface_list (net_controlsocket);
|
||||||
|
|
||||||
{
|
{
|
||||||
struct sockaddr_in t;
|
AF_address_t t;
|
||||||
memcpy (&t, &broadcastaddr, sizeof (t));
|
t.s4.sin_family = AF_INET;
|
||||||
t.sin_family = AF_INET;
|
t.s4.sin_addr.s_addr = INADDR_BROADCAST;
|
||||||
t.sin_addr.s_addr = INADDR_BROADCAST;
|
t.s4.sin_port = htons (net_hostport);
|
||||||
t.sin_port = htons (net_hostport);
|
SockadrToNetadr (&t, &broadcastaddr);
|
||||||
memcpy (&broadcastaddr, &t, sizeof (broadcastaddr));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UDP_GetSocketAddr (net_controlsocket, &addr);
|
UDP_GetSocketAddr (net_controlsocket, &addr);
|
||||||
|
@ -317,7 +362,7 @@ UDP_CloseSocket (int socket)
|
||||||
the local network components to fill in the rest
|
the local network components to fill in the rest
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
PartialIPAddress (const char *in, AF_address_t *hostaddr)
|
PartialIPAddress (const char *in, netadr_t *hostaddr)
|
||||||
{
|
{
|
||||||
char *buff;
|
char *buff;
|
||||||
char *b;
|
char *b;
|
||||||
|
@ -352,9 +397,13 @@ PartialIPAddress (const char *in, AF_address_t *hostaddr)
|
||||||
else
|
else
|
||||||
port = net_hostport;
|
port = net_hostport;
|
||||||
|
|
||||||
hostaddr->sa.sa_family = AF_INET;
|
hostaddr->family = AF_INET;
|
||||||
hostaddr->s4.sin_port = htons ((short) port);
|
hostaddr->port = htons ((short) port);
|
||||||
hostaddr->s4.sin_addr.s_addr = (myAddr & htonl (mask)) | htonl (addr);
|
{
|
||||||
|
int32_t t = myAddr & htonl (mask);
|
||||||
|
t |= htonl (addr);
|
||||||
|
memcpy (hostaddr->ip, &t, ADDR_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
free (buff);
|
free (buff);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -364,7 +413,7 @@ error:
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
UDP_Connect (int socket, AF_address_t *addr)
|
UDP_Connect (int socket, netadr_t *addr)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -388,20 +437,20 @@ UDP_CheckNewConnections (void)
|
||||||
// there is no way to tell between an empty packet and no packets, but
|
// 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.
|
// as non-blocking io is used, this is not a problem.
|
||||||
// we don't care about the interface on which the packet arrived
|
// we don't care about the interface on which the packet arrived
|
||||||
recvfrom (net_acceptsocket, buff, 0, 0, (struct sockaddr *) &from,
|
recvfrom (net_acceptsocket, buff, 0, 0, &from.sa, &fromlen);
|
||||||
&fromlen);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
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;
|
int ret;
|
||||||
|
AF_address_t addr;
|
||||||
#ifdef HAVE_IN_PKTINFO
|
#ifdef HAVE_IN_PKTINFO
|
||||||
char ancillary[CMSG_SPACE (sizeof (struct in_pktinfo))];
|
char ancillary[CMSG_SPACE (sizeof (struct in_pktinfo))];
|
||||||
struct msghdr msghdr = {
|
struct msghdr msghdr = {
|
||||||
addr,
|
&addr,
|
||||||
sizeof (*addr),
|
sizeof (addr),
|
||||||
0, 0,
|
0, 0,
|
||||||
ancillary,
|
ancillary,
|
||||||
sizeof (ancillary),
|
sizeof (ancillary),
|
||||||
|
@ -411,6 +460,7 @@ UDP_Read (int socket, byte *buf, int len, AF_address_t *addr)
|
||||||
struct cmsghdr *cmsg;
|
struct cmsghdr *cmsg;
|
||||||
struct in_pktinfo *info = 0;
|
struct in_pktinfo *info = 0;
|
||||||
|
|
||||||
|
memset (&addr, 0, sizeof (addr));
|
||||||
msghdr.msg_iov = &iovec;
|
msghdr.msg_iov = &iovec;
|
||||||
msghdr.msg_iovlen = 1;
|
msghdr.msg_iovlen = 1;
|
||||||
ret = recvmsg (socket, &msghdr, 0);
|
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];
|
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,
|
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) : "?");
|
last_iface ? inet_ntoa (info->ipi_addr) : "?");
|
||||||
#else
|
#else
|
||||||
socklen_t addrlen = sizeof (AF_address_t);
|
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))
|
if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED))
|
||||||
return 0;
|
return 0;
|
||||||
|
SockadrToNetadr (&addr, from);
|
||||||
Sys_MaskPrintf (SYS_NET, "got %d bytes from %s\n", ret,
|
Sys_MaskPrintf (SYS_NET, "got %d bytes from %s\n", ret,
|
||||||
UDP_AddrToString (addr));
|
UDP_AddrToString (from));
|
||||||
last_iface = default_iface;
|
last_iface = default_iface;
|
||||||
#endif
|
#endif
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -480,60 +533,61 @@ UDP_Broadcast (int socket, byte *buf, int len)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
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;
|
int ret;
|
||||||
|
AF_address_t addr;
|
||||||
|
|
||||||
ret = sendto (socket, buf, len, 0, (struct sockaddr *) addr,
|
NetadrToSockadr (to, &addr);
|
||||||
sizeof (AF_address_t));
|
ret = sendto (socket, buf, len, 0, &addr.sa, SA_LEN (&addr.sa));
|
||||||
if (ret == -1 && errno == EWOULDBLOCK)
|
if (ret == -1 && errno == EWOULDBLOCK)
|
||||||
return 0;
|
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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
UDP_AddrToString (AF_address_t *addr)
|
UDP_AddrToString (netadr_t *addr)
|
||||||
{
|
{
|
||||||
static dstring_t *buffer;
|
static dstring_t *buffer;
|
||||||
int haddr;
|
|
||||||
|
|
||||||
if (!buffer)
|
if (!buffer)
|
||||||
buffer = dstring_new ();
|
buffer = dstring_new ();
|
||||||
|
|
||||||
haddr = ntohl (addr->s4.sin_addr.s_addr);
|
dsprintf (buffer, "%d.%d.%d.%d:%d", addr->ip[0],
|
||||||
dsprintf (buffer, "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff,
|
addr->ip[1], addr->ip[2], addr->ip[3],
|
||||||
(haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff,
|
ntohs (addr->port));
|
||||||
ntohs (addr->s4.sin_port));
|
|
||||||
return buffer->str;
|
return buffer->str;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
UDP_GetSocketAddr (int socket, AF_address_t *addr)
|
UDP_GetSocketAddr (int socket, netadr_t *na)
|
||||||
{
|
{
|
||||||
unsigned int a;
|
unsigned int a;
|
||||||
socklen_t addrlen = sizeof (AF_address_t);
|
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);
|
getsockname (socket, &addr.sa, &addrlen);
|
||||||
a = addr->s4.sin_addr.s_addr;
|
SockadrToNetadr (&addr, na);
|
||||||
|
memcpy (&a, na->ip, ADDR_SIZE);
|
||||||
if (a == 0 || a == inet_addr ("127.0.0.1")) {
|
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)
|
if (last_iface)
|
||||||
addr->s4.sin_addr.s_addr = *last_iface;
|
memcpy (na->ip, last_iface, ADDR_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
UDP_GetNameFromAddr (AF_address_t *addr, char *name)
|
UDP_GetNameFromAddr (netadr_t *addr, char *name)
|
||||||
{
|
{
|
||||||
struct hostent *hostentry;
|
struct hostent *hostentry;
|
||||||
|
|
||||||
hostentry = gethostbyaddr ((char *) &addr->s4.sin_addr,
|
hostentry = gethostbyaddr (&addr->ip, ADDR_SIZE, AF_INET);
|
||||||
sizeof (struct in_addr), AF_INET);
|
|
||||||
|
|
||||||
if (hostentry) {
|
if (hostentry) {
|
||||||
strncpy (name, (char *) hostentry->h_name, NET_NAMELEN - 1);
|
strncpy (name, (char *) hostentry->h_name, NET_NAMELEN - 1);
|
||||||
|
@ -545,7 +599,7 @@ UDP_GetNameFromAddr (AF_address_t *addr, char *name)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
UDP_GetAddrFromName (const char *name, AF_address_t *addr)
|
UDP_GetAddrFromName (const char *name, netadr_t *addr)
|
||||||
{
|
{
|
||||||
struct hostent *hostentry;
|
struct hostent *hostentry;
|
||||||
|
|
||||||
|
@ -556,38 +610,37 @@ UDP_GetAddrFromName (const char *name, AF_address_t *addr)
|
||||||
if (!hostentry)
|
if (!hostentry)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
addr->sa.sa_family = AF_INET;
|
addr->family = AF_INET;
|
||||||
addr->s4.sin_port = htons (net_hostport);
|
addr->port = htons (net_hostport);
|
||||||
|
memcpy (addr->ip, hostentry->h_addr_list[0], ADDR_SIZE);
|
||||||
addr->s4.sin_addr.s_addr = *(uint32_t *) hostentry->h_addr_list[0];
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
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;
|
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;
|
return -1;
|
||||||
|
|
||||||
if (addr1->s4.sin_port != addr2->s4.sin_port)
|
if (addr1->port != addr2->port)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
UDP_GetSocketPort (AF_address_t *addr)
|
UDP_GetSocketPort (netadr_t *addr)
|
||||||
{
|
{
|
||||||
return ntohs (addr->s4.sin_port);
|
return ntohs (addr->port);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue