2010-02-15 23:26:55 +00:00
|
|
|
/*
|
|
|
|
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
|
2010-06-19 18:20:22 +00:00
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-08-14 20:55:39 +00:00
|
|
|
#include "q_stdinc.h"
|
* Makefile, Makefile.darwin, Makefile.w32, Makefile.w64: Build changes:
The SDL_net driver is now disabled by default and platform-specific network
drivers will be used. To compile for SDL_net, a command like "make SDLNET=1"
must be used, in which case a new preprocessor macro _USE_SDLNET will be
defined in the CFLAGS. For windows targets when not using SDL_net, WINSOCK2
is added as another option: A command line like "make WINSOCK2=1" will enable
WinSock2 api and a new preprocessor macro _USE_WINSOCK2 will be defined in
the CFLAGS. Or, a command line like "make WINSOCK2=0" will disable WinSock2
api and the old WinSock 1.1 api will be used instead. For Win64, WinSock2 is
enabled by default. For Win32, WinSock 1.1 is the default api.
* net_bsd.c, net_dgrm.c, net_loop.c, net_main.c, net_sdl.c, net_sdlnet.c,
net_udp.c, net_win.c, net_wins.c, net_wipx.c: Use the newly added net_sys.h
header. The sys_socket_t type is not in use, yet.
git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@215 af15c1b1-3010-417e-b628-4374ebc0bcbd
2010-06-21 11:10:38 +00:00
|
|
|
#include "arch_def.h"
|
|
|
|
#include "net_sys.h"
|
2010-02-15 23:26:55 +00:00
|
|
|
#include "quakedef.h"
|
2010-06-20 17:21:10 +00:00
|
|
|
#include "net_defs.h"
|
2010-02-15 23:26:55 +00:00
|
|
|
#include "net_sdlnet.h"
|
2010-06-19 22:50:48 +00:00
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_net.h"
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
#define MAX_SOCKETS 32
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
static int net_controlsocket;
|
|
|
|
static int net_broadcastsocket = 0;
|
|
|
|
static int net_acceptsocket = -1;
|
|
|
|
static struct qsockaddr broadcastaddr;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
SDLNet_SocketSet acceptsocket_set;
|
|
|
|
IPaddress myaddr;
|
2010-02-15 23:26:55 +00:00
|
|
|
// contains a map of socket numbers to SDL_net UDP sockets
|
2010-02-17 23:32:04 +00:00
|
|
|
UDPsocket net_sockets[MAX_SOCKETS];
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-19 18:20:22 +00:00
|
|
|
static int socket_id (UDPsocket socket_p)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-20 11:05:28 +00:00
|
|
|
int idx;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-20 11:05:28 +00:00
|
|
|
for (idx = 0; idx < MAX_SOCKETS; idx++)
|
2010-02-17 23:32:04 +00:00
|
|
|
{
|
2010-06-20 11:05:28 +00:00
|
|
|
if (net_sockets[idx] == socket_p)
|
|
|
|
return idx;
|
2010-02-17 23:32:04 +00:00
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-20 11:05:28 +00:00
|
|
|
for (idx = 0; idx < MAX_SOCKETS; idx++)
|
2010-02-17 23:32:04 +00:00
|
|
|
{
|
2010-06-20 11:05:28 +00:00
|
|
|
if (net_sockets[idx] == NULL)
|
|
|
|
break;
|
2010-02-17 23:32:04 +00:00
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-20 11:05:28 +00:00
|
|
|
if (idx == MAX_SOCKETS)
|
|
|
|
Sys_Error("net_sdlnet: No free sockets.");
|
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
net_sockets[idx] = socket_p;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return idx;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-08-29 02:22:55 +00:00
|
|
|
static const char *_AddrToString (int ip, int port)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
static char buffer[22];
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-20 12:50:51 +00:00
|
|
|
sprintf(buffer, "%d.%d.%d.%d:%d", (ip >> 24) & 0xff, (ip >> 16) & 0xff,
|
|
|
|
(ip >> 8) & 0xff, ip & 0xff, port);
|
2010-02-17 23:32:04 +00:00
|
|
|
return buffer;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-08-29 02:22:55 +00:00
|
|
|
static const char *_IPAddrToString (IPaddress *address)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
int ip;
|
|
|
|
int port;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
ip = SDLNet_Read32(&address->host);
|
|
|
|
port = SDLNet_Read16(&address->port);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return _AddrToString(ip, port);
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SDLN_Init (void)
|
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
int i;
|
|
|
|
IPaddress *ipaddress;
|
|
|
|
|
|
|
|
// init SDL
|
|
|
|
if (SDLNet_Init() == -1)
|
|
|
|
{
|
|
|
|
Con_SafePrintf ("SDL_net initialization failed.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate a socket set for the accept socket
|
|
|
|
acceptsocket_set = SDLNet_AllocSocketSet(1);
|
|
|
|
if (acceptsocket_set == NULL)
|
|
|
|
{
|
2010-02-15 23:26:55 +00:00
|
|
|
Con_DPrintf ("SDL_net initialization failed: Could not create socket set.\n");
|
2010-02-17 23:32:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
// set my IP address
|
2010-02-15 23:26:55 +00:00
|
|
|
i = COM_CheckParm ("-ip");
|
|
|
|
if (i)
|
|
|
|
{
|
|
|
|
if (i < com_argc-1)
|
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
SDLNet_ResolveHost(&myaddr, com_argv[i+1], 0);
|
2010-02-15 23:26:55 +00:00
|
|
|
if (myaddr.host == INADDR_NONE)
|
|
|
|
Sys_Error ("%s is not a valid IP address", com_argv[i+1]);
|
|
|
|
strcpy(my_tcpip_address, com_argv[i+1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Sys_Error ("NET_Init: you must specify an IP address after -ip");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-20 12:50:51 +00:00
|
|
|
/* with SDL_net, there is no way of doing an
|
2010-06-21 13:27:30 +00:00
|
|
|
equivalent of gethostaddr() / gethostbyname(). */
|
|
|
|
SDLNet_ResolveHost(&myaddr, NULL, 0);
|
|
|
|
strcpy(my_tcpip_address, "INADDR_ANY");
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
// open the control socket
|
2010-06-20 12:50:51 +00:00
|
|
|
if ((net_controlsocket = SDLN_OpenSocket(0)) == -1)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
Con_Printf("SDLN_Init: Unable to open control socket\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
broadcastaddr.qsa_family = AF_INET;
|
|
|
|
ipaddress = (IPaddress *)&(broadcastaddr.qsa_data);
|
2010-02-17 23:32:04 +00:00
|
|
|
SDLNet_Write32(INADDR_BROADCAST, &ipaddress->host);
|
|
|
|
SDLNet_Write16(net_hostport, &ipaddress->port);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
Con_Printf("SDL_net TCP/IP initialized\n");
|
|
|
|
tcpipAvailable = true;
|
|
|
|
|
|
|
|
return net_controlsocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLN_Shutdown (void)
|
|
|
|
{
|
|
|
|
SDLN_Listen (false);
|
|
|
|
SDLN_CloseSocket (net_controlsocket);
|
|
|
|
}
|
|
|
|
|
2010-06-19 18:20:22 +00:00
|
|
|
void SDLN_GetLocalAddress (void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
if (myaddr.host != INADDR_ANY)
|
|
|
|
return;
|
|
|
|
|
2010-06-21 13:27:30 +00:00
|
|
|
SDLNet_ResolveHost(&myaddr, NULL, 0);
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SDLN_Listen (qboolean state)
|
|
|
|
{
|
|
|
|
// enable listening
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
if (net_acceptsocket != -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SDLN_GetLocalAddress();
|
|
|
|
if ((net_acceptsocket = SDLN_OpenSocket (net_hostport)) == -1)
|
|
|
|
Sys_Error ("SDLN_Listen: Unable to open accept socket\n");
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
SDLNet_UDP_AddSocket(acceptsocket_set, net_sockets[net_acceptsocket]);
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
2010-02-17 23:32:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// disable listening
|
|
|
|
if (net_acceptsocket == -1)
|
|
|
|
return;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
SDLNet_UDP_DelSocket(acceptsocket_set, net_sockets[net_acceptsocket]);
|
|
|
|
SDLN_CloseSocket(net_acceptsocket);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
net_acceptsocket = -1;
|
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_OpenSocket (int port)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
UDPsocket newsocket;
|
|
|
|
static IPaddress address;
|
2010-02-18 21:56:10 +00:00
|
|
|
Uint16 _port = port;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
if ((newsocket = SDLNet_UDP_Open(_port)) == NULL)
|
2010-02-17 23:32:04 +00:00
|
|
|
return -1;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
address.host = myaddr.host;
|
2010-02-18 21:56:10 +00:00
|
|
|
address.port = SDLNet_Read16(&_port);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
if (SDLNet_UDP_Bind(newsocket, 0, &address) != -1)
|
|
|
|
return socket_id(newsocket);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
Sys_Error ("Unable to bind to %s", _IPAddrToString(&address));
|
|
|
|
|
|
|
|
SDLNet_UDP_Close(newsocket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_CloseSocket (int socketid)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-18 21:56:10 +00:00
|
|
|
UDPsocket socket_p;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
if (socketid == net_broadcastsocket)
|
|
|
|
net_broadcastsocket = -1;
|
2010-06-21 13:27:30 +00:00
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
socket_p = net_sockets[socketid];
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
if (socket_p == NULL)
|
2010-02-17 23:32:04 +00:00
|
|
|
return -1;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
SDLNet_UDP_Close(socket_p);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
net_sockets[socketid] = NULL;
|
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
int SDLN_Connect (int socketid, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_CheckNewConnections (void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
if (net_acceptsocket == -1)
|
|
|
|
return -1;
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
if (SDLNet_CheckSockets(acceptsocket_set, 0) > 0)
|
|
|
|
return net_acceptsocket;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return -1;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 18:20:22 +00:00
|
|
|
static UDPpacket *init_packet(UDPpacket *packet, int len)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
if (packet == NULL)
|
|
|
|
return SDLNet_AllocPacket(len);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
if (packet->maxlen < len)
|
|
|
|
SDLNet_ResizePacket(packet, len);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return packet;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_Read (int socketid, byte *buf, int len, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
int numrecv;
|
|
|
|
static UDPpacket *packet;
|
|
|
|
IPaddress *ipaddress;
|
2010-02-18 21:56:10 +00:00
|
|
|
UDPsocket socket_p;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
socket_p = net_sockets[socketid];
|
|
|
|
if (socket_p == NULL)
|
2010-02-17 23:32:04 +00:00
|
|
|
return -1;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
packet = init_packet(packet, len);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
numrecv = SDLNet_UDP_Recv(socket_p, packet);
|
2010-02-17 23:32:04 +00:00
|
|
|
if (numrecv == 1)
|
|
|
|
{
|
|
|
|
memcpy(buf, packet->data, packet->len);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
addr->qsa_family = AF_INET;
|
|
|
|
ipaddress = (IPaddress *)&(addr->qsa_data);
|
2010-02-17 23:32:04 +00:00
|
|
|
ipaddress->host = packet->address.host;
|
|
|
|
ipaddress->port = packet->address.port;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return packet->len;
|
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return numrecv;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_Write (int socketid, byte *buf, int len, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
int numsent;
|
|
|
|
static UDPpacket *packet;
|
2010-02-18 21:56:10 +00:00
|
|
|
UDPsocket socket_p;
|
2010-02-17 23:32:04 +00:00
|
|
|
IPaddress *ipaddress;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
socket_p = net_sockets[socketid];
|
|
|
|
if (socket_p == NULL)
|
2010-02-17 23:32:04 +00:00
|
|
|
return -1;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
packet = init_packet(packet, len);
|
|
|
|
memcpy(packet->data, buf, len);
|
|
|
|
packet->len = len;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
ipaddress = (IPaddress *)&(addr->qsa_data);
|
2010-02-17 23:32:04 +00:00
|
|
|
packet->address.host = ipaddress->host;
|
|
|
|
packet->address.port = ipaddress->port;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
numsent = SDLNet_UDP_Send(socket_p, -1, packet);
|
2010-02-17 23:32:04 +00:00
|
|
|
if (numsent == 0)
|
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return len;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_Broadcast (int socketid, byte *buf, int len)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
if (socketid != net_broadcastsocket)
|
|
|
|
{
|
|
|
|
if (net_broadcastsocket != 0)
|
|
|
|
Sys_Error("Attempted to use multiple broadcast sockets\n");
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
// todo make socket broadcast capable
|
|
|
|
Sys_Error("Unable to make socket broadcast capable\n");
|
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return SDLN_Write(socketid, buf, len, &broadcastaddr);
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-08-29 02:22:55 +00:00
|
|
|
const char *SDLN_AddrToString (struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
int ip;
|
|
|
|
int port;
|
|
|
|
IPaddress *ipaddress;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
ipaddress = (IPaddress *)&(addr->qsa_data);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
ip = SDLNet_Read32(&ipaddress->host);
|
|
|
|
port = SDLNet_Read16(&ipaddress->port);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return _AddrToString(ip, port);
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-08-29 02:22:55 +00:00
|
|
|
int SDLN_StringToAddr (const char *string, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
int ha1, ha2, ha3, ha4, hp;
|
|
|
|
int hostaddr;
|
2010-02-17 23:32:04 +00:00
|
|
|
IPaddress *ipaddress;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
|
|
|
|
hostaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
|
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
addr->qsa_family = AF_INET;
|
|
|
|
ipaddress = (IPaddress *)&(addr->qsa_data);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
SDLNet_Write32(hostaddr, &ipaddress->host);
|
|
|
|
SDLNet_Write16(hp, &ipaddress->port);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_GetSocketAddr (int socketid, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-18 21:56:10 +00:00
|
|
|
static UDPsocket socket_p;
|
2010-02-17 23:32:04 +00:00
|
|
|
IPaddress *peeraddress;
|
|
|
|
IPaddress *ipaddress;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
Q_memset(addr, 0, sizeof(struct qsockaddr));
|
2010-06-21 13:27:30 +00:00
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
socket_p = net_sockets[socketid];
|
|
|
|
if (socket_p == NULL)
|
2010-02-17 23:32:04 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-02-18 21:56:10 +00:00
|
|
|
peeraddress = SDLNet_UDP_GetPeerAddress(socket_p, -1);
|
2010-02-17 23:32:04 +00:00
|
|
|
if (peeraddress == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
addr->qsa_family = AF_INET;
|
|
|
|
ipaddress = (IPaddress *) addr->qsa_data;
|
2010-02-18 21:56:10 +00:00
|
|
|
if (peeraddress->host == 0 ||
|
|
|
|
peeraddress->host == SDL_SwapBE32(INADDR_LOOPBACK) /* inet_addr ("127.0.0.1") */)
|
2010-02-17 23:32:04 +00:00
|
|
|
{
|
|
|
|
ipaddress->host = myaddr.host;
|
|
|
|
ipaddress->port = myaddr.port;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ipaddress->host = peeraddress->host;
|
|
|
|
ipaddress->port = peeraddress->port;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_GetNameFromAddr (struct qsockaddr *addr, char *name)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-08-29 02:22:55 +00:00
|
|
|
const char *buf;
|
2010-02-17 23:32:04 +00:00
|
|
|
IPaddress *ipaddress;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
ipaddress = (IPaddress *)&(addr->qsa_data);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-08-29 02:22:55 +00:00
|
|
|
buf = SDLNet_ResolveIP(ipaddress);
|
2010-02-17 23:32:04 +00:00
|
|
|
if (buf != NULL)
|
|
|
|
{
|
|
|
|
Q_strncpy(name, buf, NET_NAMELEN - 1);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
Q_strcpy(name, SDLN_AddrToString(addr));
|
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
PartialIPAddress
|
|
|
|
|
|
|
|
this lets you type only as much of the net address as required, using
|
|
|
|
the local network components to fill in the rest
|
|
|
|
============
|
|
|
|
*/
|
2010-08-29 02:22:55 +00:00
|
|
|
static int PartialIPAddress (const char *in, struct qsockaddr *hostaddr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
char buff[256];
|
|
|
|
char *b;
|
|
|
|
int addr;
|
|
|
|
int num;
|
|
|
|
int mask;
|
2010-02-17 23:32:04 +00:00
|
|
|
int tmp;
|
2010-02-15 23:26:55 +00:00
|
|
|
int run;
|
|
|
|
int port;
|
|
|
|
IPaddress *ipaddress;
|
|
|
|
|
|
|
|
buff[0] = '.';
|
|
|
|
b = buff;
|
|
|
|
strcpy(buff+1, in);
|
|
|
|
if (buff[1] == '.')
|
|
|
|
b++;
|
|
|
|
|
|
|
|
addr = 0;
|
|
|
|
mask=-1;
|
|
|
|
while (*b == '.')
|
|
|
|
{
|
|
|
|
b++;
|
|
|
|
num = 0;
|
|
|
|
run = 0;
|
|
|
|
while (!( *b < '0' || *b > '9'))
|
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
num = num*10 + *b++ - '0';
|
|
|
|
if (++run > 3)
|
|
|
|
return -1;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
|
|
|
|
return -1;
|
|
|
|
if (num < 0 || num > 255)
|
|
|
|
return -1;
|
|
|
|
mask<<=8;
|
|
|
|
addr = (addr<<8) + num;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*b++ == ':')
|
|
|
|
port = Q_atoi(b);
|
|
|
|
else
|
|
|
|
port = net_hostport;
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
tmp = SDLNet_Read32(&myaddr.host);
|
|
|
|
tmp = (tmp & mask) | addr;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
hostaddr->qsa_family = AF_INET;
|
|
|
|
ipaddress = (IPaddress *)&(hostaddr->qsa_data);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
SDLNet_Write32(tmp, &ipaddress->host);
|
|
|
|
SDLNet_Write16(port, &ipaddress->port);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-08-29 02:22:55 +00:00
|
|
|
int SDLN_GetAddrFromName (const char *name, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
IPaddress *ipaddress;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
if (name[0] >= '0' && name[0] <= '9')
|
2010-02-17 23:32:04 +00:00
|
|
|
return PartialIPAddress (name, addr);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
ipaddress = (IPaddress *)&(addr->qsa_data);
|
|
|
|
if (SDLNet_ResolveHost((IPaddress *)(&addr->qsa_data), name, net_hostport) == -1)
|
2010-02-17 23:32:04 +00:00
|
|
|
return -1;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
addr->qsa_family = AF_INET;
|
2010-02-17 23:32:04 +00:00
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
IPaddress *ipaddr1;
|
|
|
|
IPaddress *ipaddr2;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
if (addr1->qsa_family != addr2->qsa_family)
|
2010-02-15 23:26:55 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
ipaddr1 = (IPaddress *)&(addr1->qsa_data);
|
|
|
|
ipaddr2 = (IPaddress *)&(addr2->qsa_data);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
if (ipaddr1->host != ipaddr2->host)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (ipaddr1->port != ipaddr2->port)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_GetSocketPort (struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
IPaddress *ipaddress;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
ipaddress = (IPaddress *)&(addr->qsa_data);
|
2010-02-17 23:32:04 +00:00
|
|
|
return SDLNet_Read16(&ipaddress->port);
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
int SDLN_SetSocketPort (struct qsockaddr *addr, int port)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-02-17 23:32:04 +00:00
|
|
|
IPaddress *ipaddress;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
ipaddress = (IPaddress *)&(addr->qsa_data);
|
2010-02-17 23:32:04 +00:00
|
|
|
SDLNet_Write16(port, &ipaddress->port);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-02-17 23:32:04 +00:00
|
|
|
return 0;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|