2010-02-15 23:26:55 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 1996-2001 Id Software, Inc.
|
|
|
|
Copyright (C) 2002-2005 John Fitzgibbons and others
|
|
|
|
Copyright (C) 2007-2008 Kristian Duske
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
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: http://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
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
static sys_socket_t net_acceptsocket = INVALID_SOCKET; // socket for fielding new connections
|
|
|
|
static sys_socket_t net_controlsocket;
|
|
|
|
static sys_socket_t net_broadcastsocket = 0;
|
2010-06-21 09:00:56 +00:00
|
|
|
static struct sockaddr_in broadcastaddr;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-20 13:10:52 +00:00
|
|
|
static in_addr_t myAddr;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
#include "net_udp.h"
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
sys_socket_t UDP_Init (void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-22 18:11:00 +00:00
|
|
|
int err;
|
2010-06-21 15:24:40 +00:00
|
|
|
char *colon;
|
2010-02-15 23:26:55 +00:00
|
|
|
char buff[MAXHOSTNAMELEN];
|
2010-06-21 15:24:40 +00:00
|
|
|
struct hostent *local;
|
|
|
|
struct qsockaddr addr;
|
|
|
|
|
2010-02-15 23:26:55 +00:00
|
|
|
if (COM_CheckParm ("-noudp"))
|
2010-06-21 15:24:40 +00:00
|
|
|
return INVALID_SOCKET;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
// determine my name & address
|
2010-06-21 15:24:40 +00:00
|
|
|
myAddr = htonl(INADDR_LOOPBACK);
|
2010-02-15 23:26:55 +00:00
|
|
|
if (gethostname(buff, MAXHOSTNAMELEN) != 0)
|
2010-06-19 16:37:28 +00:00
|
|
|
{
|
2010-06-22 18:11:00 +00:00
|
|
|
err = SOCKETERRNO;
|
|
|
|
Con_SafePrintf("UDP_Init: gethostname failed (%s)\n",
|
|
|
|
socketerror(err));
|
2010-06-19 17:04:04 +00:00
|
|
|
}
|
2010-06-21 15:24:40 +00:00
|
|
|
else
|
2010-06-19 16:37:28 +00:00
|
|
|
{
|
2010-06-21 15:24:40 +00:00
|
|
|
buff[MAXHOSTNAMELEN - 1] = 0;
|
|
|
|
local = gethostbyname(buff);
|
|
|
|
if (local == NULL)
|
|
|
|
{
|
2010-06-22 18:11:00 +00:00
|
|
|
Con_SafePrintf("UDP_Init: gethostbyname failed (%s)\n",
|
|
|
|
hstrerror(h_errno));
|
2010-06-21 15:24:40 +00:00
|
|
|
}
|
2011-06-27 13:10:19 +00:00
|
|
|
else if (local->h_addrtype != AF_INET)
|
|
|
|
{
|
|
|
|
Con_SafePrintf("UDP_Init: address from gethostbyname not IPv4\n");
|
|
|
|
}
|
2010-06-21 15:24:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
myAddr = *(in_addr_t *)local->h_addr_list[0];
|
|
|
|
}
|
2010-06-19 16:37:28 +00:00
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
if ((net_controlsocket = UDP_OpenSocket(0)) == INVALID_SOCKET)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-21 16:17:17 +00:00
|
|
|
Con_SafePrintf("UDP_Init: Unable to open control socket, UDP disabled\n");
|
2010-06-21 15:24:40 +00:00
|
|
|
return INVALID_SOCKET;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
2010-06-21 09:00:56 +00:00
|
|
|
broadcastaddr.sin_family = AF_INET;
|
|
|
|
broadcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
|
|
|
|
broadcastaddr.sin_port = htons((unsigned short)net_hostport);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
UDP_GetSocketAddr (net_controlsocket, &addr);
|
2011-06-27 13:10:19 +00:00
|
|
|
strcpy(my_tcpip_address, UDP_AddrToString (&addr));
|
|
|
|
colon = strrchr (my_tcpip_address, ':');
|
2010-02-15 23:26:55 +00:00
|
|
|
if (colon)
|
|
|
|
*colon = 0;
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
Con_SafePrintf("UDP Initialized\n");
|
2010-02-15 23:26:55 +00:00
|
|
|
tcpipAvailable = true;
|
|
|
|
|
|
|
|
return net_controlsocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void UDP_Shutdown (void)
|
|
|
|
{
|
|
|
|
UDP_Listen (false);
|
|
|
|
UDP_CloseSocket (net_controlsocket);
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void UDP_Listen (qboolean state)
|
|
|
|
{
|
|
|
|
// enable listening
|
|
|
|
if (state)
|
|
|
|
{
|
2010-06-21 15:24:40 +00:00
|
|
|
if (net_acceptsocket != INVALID_SOCKET)
|
2010-02-15 23:26:55 +00:00
|
|
|
return;
|
2010-06-21 15:24:40 +00:00
|
|
|
if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == INVALID_SOCKET)
|
|
|
|
Sys_Error ("UDP_Listen: Unable to open accept socket");
|
2010-02-15 23:26:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable listening
|
2010-06-21 15:24:40 +00:00
|
|
|
if (net_acceptsocket == INVALID_SOCKET)
|
2010-02-15 23:26:55 +00:00
|
|
|
return;
|
|
|
|
UDP_CloseSocket (net_acceptsocket);
|
2010-06-21 15:24:40 +00:00
|
|
|
net_acceptsocket = INVALID_SOCKET;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
sys_socket_t UDP_OpenSocket (int port)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-21 15:24:40 +00:00
|
|
|
sys_socket_t newsocket;
|
2010-02-15 23:26:55 +00:00
|
|
|
struct sockaddr_in address;
|
2010-06-19 18:20:22 +00:00
|
|
|
int _true = 1;
|
2010-06-22 18:11:00 +00:00
|
|
|
int err;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
|
2010-06-22 18:11:00 +00:00
|
|
|
{
|
|
|
|
err = SOCKETERRNO;
|
|
|
|
Con_SafePrintf("UDP_OpenSocket: %s\n", socketerror(err));
|
2010-06-21 15:24:40 +00:00
|
|
|
return INVALID_SOCKET;
|
2010-06-22 18:11:00 +00:00
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
if (ioctlsocket (newsocket, FIONBIO, &_true) == SOCKET_ERROR)
|
2010-02-15 23:26:55 +00:00
|
|
|
goto ErrorReturn;
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
memset(&address, 0, sizeof(struct sockaddr_in));
|
2010-02-15 23:26:55 +00:00
|
|
|
address.sin_family = AF_INET;
|
|
|
|
address.sin_addr.s_addr = INADDR_ANY;
|
2010-06-21 15:24:40 +00:00
|
|
|
address.sin_port = htons((unsigned short)port);
|
|
|
|
if (bind (newsocket, (struct sockaddr *)&address, sizeof(address)) == 0)
|
|
|
|
return newsocket;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
ErrorReturn:
|
2010-06-22 18:11:00 +00:00
|
|
|
err = SOCKETERRNO;
|
|
|
|
Con_SafePrintf("UDP_OpenSocket: %s\n", socketerror(err));
|
2010-06-21 15:24:40 +00:00
|
|
|
UDP_CloseSocket (newsocket);
|
|
|
|
return INVALID_SOCKET;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
int UDP_CloseSocket (sys_socket_t socketid)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-19 18:20:22 +00:00
|
|
|
if (socketid == net_broadcastsocket)
|
2010-02-15 23:26:55 +00:00
|
|
|
net_broadcastsocket = 0;
|
2010-06-21 15:24:40 +00:00
|
|
|
return closesocket (socketid);
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
2010-06-21 15:24:40 +00:00
|
|
|
|
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
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
char buff[256];
|
|
|
|
char *b;
|
|
|
|
int addr, mask, num, port, run;
|
|
|
|
|
2010-02-15 23:26:55 +00:00
|
|
|
buff[0] = '.';
|
|
|
|
b = buff;
|
|
|
|
strcpy(buff+1, in);
|
|
|
|
if (buff[1] == '.')
|
|
|
|
b++;
|
|
|
|
|
|
|
|
addr = 0;
|
2010-06-20 10:03:05 +00:00
|
|
|
mask = -1;
|
2010-02-15 23:26:55 +00:00
|
|
|
while (*b == '.')
|
|
|
|
{
|
|
|
|
b++;
|
|
|
|
num = 0;
|
|
|
|
run = 0;
|
|
|
|
while (!( *b < '0' || *b > '9'))
|
|
|
|
{
|
2010-06-20 10:03:05 +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;
|
2010-06-20 10:03:05 +00:00
|
|
|
mask <<= 8;
|
2010-02-15 23:26:55 +00:00
|
|
|
addr = (addr<<8) + num;
|
|
|
|
}
|
2010-06-20 10:03:05 +00:00
|
|
|
|
2010-02-15 23:26:55 +00:00
|
|
|
if (*b++ == ':')
|
2011-06-27 13:10:19 +00:00
|
|
|
port = atoi(b);
|
2010-02-15 23:26:55 +00:00
|
|
|
else
|
|
|
|
port = net_hostport;
|
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
hostaddr->qsa_family = AF_INET;
|
2010-06-20 10:03:05 +00:00
|
|
|
((struct sockaddr_in *)hostaddr)->sin_port = htons((unsigned short)port);
|
|
|
|
((struct sockaddr_in *)hostaddr)->sin_addr.s_addr =
|
|
|
|
(myAddr & htonl(mask)) | htonl(addr);
|
|
|
|
|
2010-02-15 23:26:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-06-27 13:10:19 +00:00
|
|
|
|
2010-02-15 23:26:55 +00:00
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
int UDP_Connect (sys_socket_t socketid, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
sys_socket_t UDP_CheckNewConnections (void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2011-11-12 18:03:49 +00:00
|
|
|
int available;
|
2010-06-19 16:37:28 +00:00
|
|
|
struct sockaddr_in from;
|
|
|
|
socklen_t fromlen;
|
|
|
|
char buff[1];
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
if (net_acceptsocket == INVALID_SOCKET)
|
|
|
|
return INVALID_SOCKET;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
if (ioctl (net_acceptsocket, FIONREAD, &available) == -1)
|
2010-06-21 15:24:40 +00:00
|
|
|
{
|
2010-06-22 18:11:00 +00:00
|
|
|
int err = SOCKETERRNO;
|
|
|
|
Sys_Error ("UDP: ioctlsocket (FIONREAD) failed (%s)", socketerror(err));
|
2010-06-21 15:24:40 +00:00
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
if (available)
|
|
|
|
return net_acceptsocket;
|
2010-06-19 16:37:28 +00:00
|
|
|
// quietly absorb empty packets
|
|
|
|
recvfrom (net_acceptsocket, buff, 0, 0, (struct sockaddr *) &from, &fromlen);
|
2010-06-21 15:24:40 +00:00
|
|
|
return INVALID_SOCKET;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
int UDP_Read (sys_socket_t socketid, byte *buf, int len, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-21 16:23:06 +00:00
|
|
|
socklen_t addrlen = sizeof(struct qsockaddr);
|
2010-06-19 17:04:04 +00:00
|
|
|
int ret;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 16:23:06 +00:00
|
|
|
ret = recvfrom (socketid, buf, len, 0, (struct sockaddr *)addr, &addrlen);
|
2010-06-21 15:24:40 +00:00
|
|
|
if (ret == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
int err = SOCKETERRNO;
|
2011-01-12 20:10:34 +00:00
|
|
|
if (err == NET_EWOULDBLOCK || err == NET_ECONNREFUSED)
|
2010-06-21 15:24:40 +00:00
|
|
|
return 0;
|
2010-06-22 18:11:00 +00:00
|
|
|
Con_SafePrintf ("UDP_Read, recvfrom: %s\n", socketerror(err));
|
2010-06-21 15:24:40 +00:00
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
static int UDP_MakeSocketBroadcastCapable (sys_socket_t socketid)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
int i = 1;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
// make this socket broadcast capable
|
2010-06-21 15:24:40 +00:00
|
|
|
if (setsockopt(socketid, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i))
|
|
|
|
== SOCKET_ERROR)
|
|
|
|
{
|
2010-06-22 18:11:00 +00:00
|
|
|
int err = SOCKETERRNO;
|
|
|
|
Con_SafePrintf ("UDP, setsockopt: %s\n", socketerror(err));
|
2010-02-15 23:26:55 +00:00
|
|
|
return -1;
|
2010-06-21 15:24:40 +00:00
|
|
|
}
|
2010-06-19 18:20:22 +00:00
|
|
|
net_broadcastsocket = socketid;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
int UDP_Broadcast (sys_socket_t socketid, byte *buf, int len)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
int ret;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-19 18:20:22 +00:00
|
|
|
if (socketid != net_broadcastsocket)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
if (net_broadcastsocket != 0)
|
2010-06-20 10:03:05 +00:00
|
|
|
Sys_Error("Attempted to use multiple broadcasts sockets");
|
2010-06-19 18:20:22 +00:00
|
|
|
ret = UDP_MakeSocketBroadcastCapable (socketid);
|
2010-02-15 23:26:55 +00:00
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
Con_Printf("Unable to make socket broadcast capable\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-21 09:00:56 +00:00
|
|
|
return UDP_Write (socketid, buf, len, (struct qsockaddr *)&broadcastaddr);
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
int UDP_Write (sys_socket_t socketid, byte *buf, int len, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
int ret;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
ret = sendto (socketid, buf, len, 0, (struct sockaddr *)addr,
|
|
|
|
sizeof(struct qsockaddr));
|
|
|
|
if (ret == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
int err = SOCKETERRNO;
|
2011-01-12 20:10:34 +00:00
|
|
|
if (err == NET_EWOULDBLOCK)
|
2010-06-21 15:24:40 +00:00
|
|
|
return 0;
|
2010-06-22 18:11:00 +00:00
|
|
|
Con_SafePrintf ("UDP_Write, sendto: %s\n", socketerror(err));
|
2010-06-21 15:24:40 +00:00
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-08-29 02:22:55 +00:00
|
|
|
const char *UDP_AddrToString (struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
static char buffer[22];
|
2010-06-20 10:03:05 +00:00
|
|
|
int haddr;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
|
2010-08-20 02:25:22 +00:00
|
|
|
q_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));
|
2010-02-15 23:26:55 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-08-29 02:22:55 +00:00
|
|
|
int UDP_StringToAddr (const char *string, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
int ha1, ha2, ha3, ha4, hp, ipaddr;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
|
|
|
|
ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
|
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
addr->qsa_family = AF_INET;
|
2010-02-15 23:26:55 +00:00
|
|
|
((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
|
2010-06-20 10:03:05 +00:00
|
|
|
((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp);
|
2010-02-15 23:26:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
int UDP_GetSocketAddr (sys_socket_t socketid, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-19 17:04:04 +00:00
|
|
|
socklen_t addrlen = sizeof(struct qsockaddr);
|
2010-06-21 15:24:40 +00:00
|
|
|
in_addr_t a;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-21 15:24:40 +00:00
|
|
|
memset(addr, 0, sizeof(struct qsockaddr));
|
2010-06-19 18:20:22 +00:00
|
|
|
if (getsockname(socketid, (struct sockaddr *)addr, &addrlen) != 0)
|
2010-06-19 16:37:28 +00:00
|
|
|
return -1;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
|
2010-06-19 18:20:22 +00:00
|
|
|
if (a == 0 || a == htonl(INADDR_LOOPBACK))
|
2010-02-15 23:26:55 +00:00
|
|
|
((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int UDP_GetNameFromAddr (struct qsockaddr *addr, char *name)
|
|
|
|
{
|
|
|
|
struct hostent *hostentry;
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr,
|
|
|
|
sizeof(struct in_addr), AF_INET);
|
2010-02-15 23:26:55 +00:00
|
|
|
if (hostentry)
|
|
|
|
{
|
2011-06-27 13:10:19 +00:00
|
|
|
strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
|
2010-02-15 23:26:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-27 13:10:19 +00:00
|
|
|
strcpy (name, UDP_AddrToString (addr));
|
2010-02-15 23:26:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-08-29 02:22:55 +00:00
|
|
|
int UDP_GetAddrFromName (const char *name, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
struct hostent *hostentry;
|
|
|
|
|
|
|
|
if (name[0] >= '0' && name[0] <= '9')
|
|
|
|
return PartialIPAddress (name, addr);
|
|
|
|
|
|
|
|
hostentry = gethostbyname (name);
|
|
|
|
if (!hostentry)
|
|
|
|
return -1;
|
|
|
|
|
2010-06-21 09:20:32 +00:00
|
|
|
addr->qsa_family = AF_INET;
|
2010-06-20 10:03:05 +00:00
|
|
|
((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport);
|
|
|
|
((struct sockaddr_in *)addr)->sin_addr.s_addr =
|
|
|
|
*(in_addr_t *)hostentry->h_addr_list[0];
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int UDP_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
|
|
|
|
{
|
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-20 10:03:05 +00:00
|
|
|
if (((struct sockaddr_in *)addr1)->sin_addr.s_addr !=
|
|
|
|
((struct sockaddr_in *)addr2)->sin_addr.s_addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
if (((struct sockaddr_in *)addr1)->sin_port !=
|
|
|
|
((struct sockaddr_in *)addr2)->sin_port)
|
2010-02-15 23:26:55 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int UDP_GetSocketPort (struct qsockaddr *addr)
|
|
|
|
{
|
|
|
|
return ntohs(((struct sockaddr_in *)addr)->sin_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int UDP_SetSocketPort (struct qsockaddr *addr, int port)
|
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port);
|
2010-02-15 23:26:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
2010-06-19 18:20:22 +00:00
|
|
|
|