* net_udp.c: Some cleanups & tidy-ups.

* net_bsd.c: Added from original Quake source.


git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@187 af15c1b1-3010-417e-b628-4374ebc0bcbd
This commit is contained in:
Ozkan Sezer 2010-06-19 16:37:28 +00:00
parent 48f7e6ba51
commit 573e555f67
2 changed files with 128 additions and 65 deletions

93
Quake/net_bsd.c Normal file
View file

@ -0,0 +1,93 @@
/*
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 the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "quakedef.h"
#include "net_loop.h"
#include "net_dgrm.h"
net_driver_t net_drivers[MAX_NET_DRIVERS] =
{
{
"Loopback",
false,
Loop_Init,
Loop_Listen,
Loop_SearchForHosts,
Loop_Connect,
Loop_CheckNewConnections,
Loop_GetMessage,
Loop_SendMessage,
Loop_SendUnreliableMessage,
Loop_CanSendMessage,
Loop_CanSendUnreliableMessage,
Loop_Close,
Loop_Shutdown
}
,
{
"Datagram",
false,
Datagram_Init,
Datagram_Listen,
Datagram_SearchForHosts,
Datagram_Connect,
Datagram_CheckNewConnections,
Datagram_GetMessage,
Datagram_SendMessage,
Datagram_SendUnreliableMessage,
Datagram_CanSendMessage,
Datagram_CanSendUnreliableMessage,
Datagram_Close,
Datagram_Shutdown
}
};
int net_numdrivers = 2;
#include "net_udp.h"
net_landriver_t net_landrivers[MAX_NET_DRIVERS] =
{
{
"UDP",
false,
0,
UDP_Init,
UDP_Shutdown,
UDP_Listen,
UDP_OpenSocket,
UDP_CloseSocket,
UDP_Connect,
UDP_CheckNewConnections,
UDP_Read,
UDP_Write,
UDP_Broadcast,
UDP_AddrToString,
UDP_StringToAddr,
UDP_GetSocketAddr,
UDP_GetNameFromAddr,
UDP_GetAddrFromName,
UDP_AddrCompare,
UDP_GetSocketPort,
UDP_SetSocketPort
}
};
int net_numlandrivers = 1;

View file

@ -23,29 +23,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "quakedef.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <errno.h>
#ifdef __sun__
#include <sys/param.h>
#include <sys/ioctl.h>
#include <unistd.h>
#if defined(__sun) || defined(sun)
#include <sys/filio.h>
#endif
#endif /* __sunos__ */
#if defined(NeXT)
#include <libc.h>
#endif
#include <sys/socket.h>
#if defined (__APPLE__) || defined (MACOSX)
#include <netinet/in.h>
#include <arpa/inet.h>
#endif /* __APPLE__ || MACOSX */
extern int gethostname (char *, int);
extern int close (int);
extern cvar_t hostname;
#include <netdb.h>
static int net_acceptsocket = -1; // socket for fielding new connections
static int net_controlsocket;
@ -69,27 +61,18 @@ int UDP_Init (void)
return -1;
// determine my name & address
#if defined (__APPLE__) || defined (MACOSX)
if (gethostname(buff, MAXHOSTNAMELEN) != 0)
{
Con_Printf ("UDP init failed. Disabling UDP...\n");
return (-1);
{
Con_Printf ("UDP init failed. Disabling UDP...\n");
return -1;
}
local = gethostbyname(buff);
if (local == NULL)
{
Con_Printf ("UDP init failed. Disabling UDP...\n");
return (-1);
}
#else
gethostname(buff, MAXHOSTNAMELEN);
local = gethostbyname(buff);
#endif /* __APPLE__ ||ÊMACOSX */
if (local == NULL)
{
Con_Printf ("UDP init failed. Disabling UDP...\n");
return -1;
}
myAddr = *(int *)local->h_addr_list[0];
@ -107,20 +90,12 @@ int UDP_Init (void)
((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
((struct sockaddr_in *)&broadcastaddr)->sin_port = htons(net_hostport);
#if defined (__APPLE__) || defined (MACOSX)
if (UDP_GetSocketAddr (net_controlsocket, &addr) != 0)
{
Con_Printf ("UDP init failed. Disabling UDP...\n");
return (-1);
}
{
Con_Printf ("UDP init failed. Disabling UDP...\n");
return -1;
}
#else
UDP_GetSocketAddr (net_controlsocket, &addr);
#endif /* __APPLE__ || MACOSX */
Q_strcpy(my_tcpip_address, UDP_AddrToString (&addr));
colon = Q_strrchr (my_tcpip_address, ':');
if (colon)
@ -267,6 +242,9 @@ int UDP_Connect (int socket, struct qsockaddr *addr)
int UDP_CheckNewConnections (void)
{
unsigned long available;
struct sockaddr_in from;
socklen_t fromlen;
char buff[1];
if (net_acceptsocket == -1)
return -1;
@ -275,6 +253,8 @@ int UDP_CheckNewConnections (void)
Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
if (available)
return net_acceptsocket;
// quietly absorb empty packets
recvfrom (net_acceptsocket, buff, 0, 0, (struct sockaddr *) &from, &fromlen);
return -1;
}
@ -282,13 +262,13 @@ int UDP_CheckNewConnections (void)
void get_qsockaddr(struct sockaddr *saddr, struct qsockaddr *qaddr)
{
qaddr->sa_family = saddr->sa_family;
memcpy(&(qaddr->sa_data), &(saddr->sa_data), sizeof(qaddr->sa_data));
qaddr->sa_family = saddr->sa_family;
memcpy(&(qaddr->sa_data), &(saddr->sa_data), sizeof(qaddr->sa_data));
}
int UDP_Read (int socket, byte *buf, int len, struct qsockaddr *addr)
{
static struct sockaddr saddr;
static struct sockaddr saddr;
unsigned int addrlen = sizeof (struct sockaddr);
int ret;
@ -296,8 +276,8 @@ int UDP_Read (int socket, byte *buf, int len, struct qsockaddr *addr)
if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED))
return 0;
get_qsockaddr(&saddr, addr);
get_qsockaddr(&saddr, addr);
return ret;
}
@ -357,11 +337,9 @@ char *UDP_AddrToString (struct qsockaddr *addr)
int haddr;
haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
#if defined (__APPLE__) || defined (MACOSX)
snprintf(buffer, 22, "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff, (haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff, ntohs(((struct sockaddr_in *)addr)->sin_port));
#else
sprintf(buffer, "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff, (haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff, ntohs(((struct sockaddr_in *)addr)->sin_port));
#endif /* __APPLE__ || MACOSX */
snprintf (buffer, sizeof(buffer), "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff,
(haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff,
ntohs(((struct sockaddr_in *)addr)->sin_port));
return buffer;
}
@ -390,16 +368,8 @@ int UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
Q_memset(addr, 0, sizeof(struct qsockaddr));
#if defined (__APPLE__) || defined (MACOSX)
if (getsockname(socket, (struct sockaddr *)addr, &addrlen) != 0)
return (-1);
#else
getsockname(socket, (struct sockaddr *)addr, &addrlen);
#endif /* __APPLE__ || MACOSX */
return -1;
a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
if (a == 0 || a == inet_addr("127.0.0.1"))