mirror of
https://github.com/Shpoike/Quakespasm.git
synced 2025-02-10 01:31:14 +00:00
* 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:
parent
48f7e6ba51
commit
573e555f67
2 changed files with 128 additions and 65 deletions
93
Quake/net_bsd.c
Normal file
93
Quake/net_bsd.c
Normal 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;
|
|
@ -23,29 +23,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
#include "quakedef.h"
|
#include "quakedef.h"
|
||||||
|
|
||||||
#include <sys/types.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>
|
#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>
|
#include <sys/filio.h>
|
||||||
#endif
|
#endif /* __sunos__ */
|
||||||
|
|
||||||
#if defined(NeXT)
|
#include <sys/socket.h>
|
||||||
#include <libc.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined (__APPLE__) || defined (MACOSX)
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#endif /* __APPLE__ || MACOSX */
|
#include <netdb.h>
|
||||||
|
|
||||||
extern int gethostname (char *, int);
|
|
||||||
extern int close (int);
|
|
||||||
|
|
||||||
extern cvar_t hostname;
|
|
||||||
|
|
||||||
static int net_acceptsocket = -1; // socket for fielding new connections
|
static int net_acceptsocket = -1; // socket for fielding new connections
|
||||||
static int net_controlsocket;
|
static int net_controlsocket;
|
||||||
|
@ -69,28 +61,19 @@ int UDP_Init (void)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
// determine my name & address
|
// determine my name & address
|
||||||
#if defined (__APPLE__) || defined (MACOSX)
|
|
||||||
|
|
||||||
if (gethostname(buff, MAXHOSTNAMELEN) != 0)
|
if (gethostname(buff, MAXHOSTNAMELEN) != 0)
|
||||||
{
|
{
|
||||||
Con_Printf ("UDP init failed. Disabling UDP...\n");
|
Con_Printf ("UDP init failed. Disabling UDP...\n");
|
||||||
return (-1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
local = gethostbyname(buff);
|
local = gethostbyname(buff);
|
||||||
if (local == NULL)
|
if (local == NULL)
|
||||||
{
|
{
|
||||||
Con_Printf ("UDP init failed. Disabling UDP...\n");
|
Con_Printf ("UDP init failed. Disabling UDP...\n");
|
||||||
return (-1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
gethostname(buff, MAXHOSTNAMELEN);
|
|
||||||
local = gethostbyname(buff);
|
|
||||||
|
|
||||||
#endif /* __APPLE__ ||ÊMACOSX */
|
|
||||||
|
|
||||||
myAddr = *(int *)local->h_addr_list[0];
|
myAddr = *(int *)local->h_addr_list[0];
|
||||||
|
|
||||||
// if the quake hostname isn't set, set it to the machine name
|
// if the quake hostname isn't set, set it to the machine name
|
||||||
|
@ -107,20 +90,12 @@ int UDP_Init (void)
|
||||||
((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
|
((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
|
||||||
((struct sockaddr_in *)&broadcastaddr)->sin_port = htons(net_hostport);
|
((struct sockaddr_in *)&broadcastaddr)->sin_port = htons(net_hostport);
|
||||||
|
|
||||||
#if defined (__APPLE__) || defined (MACOSX)
|
|
||||||
|
|
||||||
if (UDP_GetSocketAddr (net_controlsocket, &addr) != 0)
|
if (UDP_GetSocketAddr (net_controlsocket, &addr) != 0)
|
||||||
{
|
{
|
||||||
Con_Printf ("UDP init failed. Disabling UDP...\n");
|
Con_Printf ("UDP init failed. Disabling UDP...\n");
|
||||||
return (-1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
UDP_GetSocketAddr (net_controlsocket, &addr);
|
|
||||||
|
|
||||||
#endif /* __APPLE__ || MACOSX */
|
|
||||||
|
|
||||||
Q_strcpy(my_tcpip_address, UDP_AddrToString (&addr));
|
Q_strcpy(my_tcpip_address, UDP_AddrToString (&addr));
|
||||||
colon = Q_strrchr (my_tcpip_address, ':');
|
colon = Q_strrchr (my_tcpip_address, ':');
|
||||||
if (colon)
|
if (colon)
|
||||||
|
@ -267,6 +242,9 @@ int UDP_Connect (int socket, struct qsockaddr *addr)
|
||||||
int UDP_CheckNewConnections (void)
|
int UDP_CheckNewConnections (void)
|
||||||
{
|
{
|
||||||
unsigned long available;
|
unsigned long available;
|
||||||
|
struct sockaddr_in from;
|
||||||
|
socklen_t fromlen;
|
||||||
|
char buff[1];
|
||||||
|
|
||||||
if (net_acceptsocket == -1)
|
if (net_acceptsocket == -1)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -275,6 +253,8 @@ int UDP_CheckNewConnections (void)
|
||||||
Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
|
Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
|
||||||
if (available)
|
if (available)
|
||||||
return net_acceptsocket;
|
return net_acceptsocket;
|
||||||
|
// quietly absorb empty packets
|
||||||
|
recvfrom (net_acceptsocket, buff, 0, 0, (struct sockaddr *) &from, &fromlen);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,11 +337,9 @@ char *UDP_AddrToString (struct qsockaddr *addr)
|
||||||
int haddr;
|
int haddr;
|
||||||
|
|
||||||
haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
|
haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
|
||||||
#if defined (__APPLE__) || defined (MACOSX)
|
snprintf (buffer, sizeof(buffer), "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff,
|
||||||
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));
|
(haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff,
|
||||||
#else
|
ntohs(((struct sockaddr_in *)addr)->sin_port));
|
||||||
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 */
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -390,16 +368,8 @@ int UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
|
||||||
|
|
||||||
Q_memset(addr, 0, sizeof(struct qsockaddr));
|
Q_memset(addr, 0, sizeof(struct qsockaddr));
|
||||||
|
|
||||||
#if defined (__APPLE__) || defined (MACOSX)
|
|
||||||
|
|
||||||
if (getsockname(socket, (struct sockaddr *)addr, &addrlen) != 0)
|
if (getsockname(socket, (struct sockaddr *)addr, &addrlen) != 0)
|
||||||
return (-1);
|
return -1;
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
getsockname(socket, (struct sockaddr *)addr, &addrlen);
|
|
||||||
|
|
||||||
#endif /* __APPLE__ || MACOSX */
|
|
||||||
|
|
||||||
a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
|
a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
|
||||||
if (a == 0 || a == inet_addr("127.0.0.1"))
|
if (a == 0 || a == inet_addr("127.0.0.1"))
|
||||||
|
|
Loading…
Reference in a new issue