2010-02-15 23:26:55 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 1996-2001 Id Software, Inc.
|
|
|
|
Copyright (C) 2002-2009 John Fitzgibbons and others
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
// net_wins.c
|
|
|
|
|
2010-06-20 20:50:27 +00:00
|
|
|
#include "winsock.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
|
|
|
|
|
|
|
#define MAXHOSTNAMELEN 256
|
|
|
|
|
2010-06-20 13:10:52 +00:00
|
|
|
static int net_acceptsocket = -1; // socket for fielding new connections
|
|
|
|
static int net_controlsocket;
|
|
|
|
static int net_broadcastsocket = 0;
|
|
|
|
static struct qsockaddr broadcastaddr;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-20 13:10:52 +00:00
|
|
|
static u_long myAddr;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
#include "net_wins.h"
|
|
|
|
|
|
|
|
int winsock_initialized = 0;
|
|
|
|
WSADATA winsockdata;
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
static double blocktime;
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
static INT_PTR PASCAL FAR BlockingHook (void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
MSG msg;
|
|
|
|
BOOL ret;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
if ((Sys_FloatTime() - blocktime) > 2.0)
|
|
|
|
{
|
|
|
|
WSACancelBlockingCall();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
/* get the next message, if any */
|
|
|
|
ret = (BOOL) PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
/* if we got one, process it */
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
/* TRUE if we got a message */
|
|
|
|
return ret;
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
static void WINS_GetLocalAddress (void)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
struct hostent *local = NULL;
|
2010-06-20 13:10:52 +00:00
|
|
|
char buff[MAXHOSTNAMELEN];
|
|
|
|
u_long addr;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
if (myAddr != INADDR_ANY)
|
|
|
|
return;
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
if (gethostname(buff, MAXHOSTNAMELEN) == SOCKET_ERROR)
|
2010-02-15 23:26:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
blocktime = Sys_FloatTime();
|
|
|
|
WSASetBlockingHook(BlockingHook);
|
2010-06-20 10:03:05 +00:00
|
|
|
local = gethostbyname(buff);
|
2010-02-15 23:26:55 +00:00
|
|
|
WSAUnhookBlockingHook();
|
|
|
|
if (local == NULL)
|
|
|
|
return;
|
|
|
|
|
2010-06-20 13:10:52 +00:00
|
|
|
myAddr = *(u_long *)local->h_addr_list[0];
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
addr = ntohl(myAddr);
|
2010-06-20 20:50:27 +00:00
|
|
|
sprintf(my_tcpip_address, "%ld.%ld.%ld.%ld", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int WINS_Init (void)
|
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
int i, err;
|
2010-02-15 23:26:55 +00:00
|
|
|
char buff[MAXHOSTNAMELEN];
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (COM_CheckParm ("-noudp"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (winsock_initialized == 0)
|
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
err = WSAStartup(MAKEWORD(1,1), &winsockdata);
|
|
|
|
if (err != 0)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
Con_SafePrintf ("Winsock initialization failed.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
winsock_initialized++;
|
|
|
|
|
|
|
|
// determine my name
|
2010-06-20 10:03:05 +00:00
|
|
|
if (gethostname(buff, MAXHOSTNAMELEN) == SOCKET_ERROR)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
Con_DPrintf ("Winsock TCP/IP Initialization failed.\n");
|
|
|
|
if (--winsock_initialized == 0)
|
2010-06-20 10:03:05 +00:00
|
|
|
WSACleanup ();
|
2010-02-15 23:26:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the quake hostname isn't set, set it to the machine name
|
|
|
|
if (Q_strcmp(hostname.string, "UNNAMED") == 0)
|
|
|
|
{
|
|
|
|
// see if it's a text IP address (well, close enough)
|
|
|
|
for (p = buff; *p; p++)
|
|
|
|
if ((*p < '0' || *p > '9') && *p != '.')
|
|
|
|
break;
|
|
|
|
|
|
|
|
// if it is a real name, strip off the domain; we only want the host
|
|
|
|
if (*p)
|
|
|
|
{
|
|
|
|
for (i = 0; i < 15; i++)
|
|
|
|
if (buff[i] == '.')
|
|
|
|
break;
|
|
|
|
buff[i] = 0;
|
|
|
|
}
|
|
|
|
Cvar_Set ("hostname", buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = COM_CheckParm ("-ip");
|
|
|
|
if (i)
|
|
|
|
{
|
|
|
|
if (i < com_argc-1)
|
|
|
|
{
|
|
|
|
myAddr = inet_addr(com_argv[i+1]);
|
|
|
|
if (myAddr == 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
|
|
|
|
{
|
|
|
|
myAddr = INADDR_ANY;
|
|
|
|
strcpy(my_tcpip_address, "INADDR_ANY");
|
|
|
|
}
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
if ((net_controlsocket = WINS_OpenSocket(0)) == -1)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
Con_Printf("WINS_Init: Unable to open control socket\n");
|
|
|
|
if (--winsock_initialized == 0)
|
2010-06-20 10:03:05 +00:00
|
|
|
WSACleanup ();
|
2010-02-15 23:26:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
|
|
|
|
((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
|
|
|
|
((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((unsigned short)net_hostport);
|
|
|
|
|
|
|
|
Con_Printf("Winsock TCP/IP Initialized\n");
|
|
|
|
tcpipAvailable = true;
|
|
|
|
|
|
|
|
return net_controlsocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void WINS_Shutdown (void)
|
|
|
|
{
|
|
|
|
WINS_Listen (false);
|
|
|
|
WINS_CloseSocket (net_controlsocket);
|
|
|
|
if (--winsock_initialized == 0)
|
2010-06-20 10:03:05 +00:00
|
|
|
WSACleanup ();
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void WINS_Listen (qboolean state)
|
|
|
|
{
|
|
|
|
// enable listening
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
if (net_acceptsocket != -1)
|
|
|
|
return;
|
|
|
|
WINS_GetLocalAddress();
|
|
|
|
if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
|
|
|
|
Sys_Error ("WINS_Listen: Unable to open accept socket\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// disable listening
|
|
|
|
if (net_acceptsocket == -1)
|
|
|
|
return;
|
|
|
|
WINS_CloseSocket (net_acceptsocket);
|
|
|
|
net_acceptsocket = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int WINS_OpenSocket (int port)
|
|
|
|
{
|
|
|
|
int newsocket;
|
|
|
|
struct sockaddr_in address;
|
|
|
|
u_long _true = 1;
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
|
2010-02-15 23:26:55 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
if (ioctlsocket (newsocket, FIONBIO, &_true) == -1)
|
2010-02-15 23:26:55 +00:00
|
|
|
goto ErrorReturn;
|
|
|
|
|
|
|
|
address.sin_family = AF_INET;
|
|
|
|
address.sin_addr.s_addr = myAddr;
|
|
|
|
address.sin_port = htons((unsigned short)port);
|
|
|
|
if( bind (newsocket, (void *)&address, sizeof(address)) == 0)
|
|
|
|
return newsocket;
|
|
|
|
|
|
|
|
Sys_Error ("Unable to bind to %s", WINS_AddrToString((struct qsockaddr *)&address));
|
|
|
|
ErrorReturn:
|
2010-06-20 10:03:05 +00:00
|
|
|
closesocket (newsocket);
|
2010-02-15 23:26:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
int WINS_CloseSocket (int socketid)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
if (socketid == net_broadcastsocket)
|
2010-02-15 23:26:55 +00:00
|
|
|
net_broadcastsocket = 0;
|
2010-06-20 10:03:05 +00:00
|
|
|
return closesocket (socketid);
|
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
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
static int PartialIPAddress (char *in, struct qsockaddr *hostaddr)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*b++ == ':')
|
|
|
|
port = Q_atoi(b);
|
|
|
|
else
|
|
|
|
port = net_hostport;
|
|
|
|
|
|
|
|
hostaddr->sa_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;
|
|
|
|
}
|
2010-06-20 10:03:05 +00:00
|
|
|
|
2010-02-15 23:26:55 +00:00
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
int WINS_Connect (int socketid, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int WINS_CheckNewConnections (void)
|
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
char buf[4096];
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
if (net_acceptsocket == -1)
|
|
|
|
return -1;
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
if (recvfrom (net_acceptsocket, buf, sizeof(buf), MSG_PEEK, NULL, NULL)
|
|
|
|
!= SOCKET_ERROR) /* >= 0 */
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
return net_acceptsocket;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
int WINS_Read (int 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 addrlen = sizeof(struct qsockaddr);
|
2010-02-15 23:26:55 +00:00
|
|
|
int ret;
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
ret = recvfrom (socketid, (char *)buf, len, 0, (struct sockaddr *)addr, &addrlen);
|
|
|
|
if (ret == SOCKET_ERROR)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
2010-06-20 10:03:05 +00:00
|
|
|
int err = WSAGetLastError();
|
2010-02-15 23:26:55 +00:00
|
|
|
if (err == WSAEWOULDBLOCK || err == WSAECONNREFUSED)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
int WINS_MakeSocketBroadcastCapable (int socketid)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
// make this socket broadcast capable
|
2010-06-20 10:03:05 +00:00
|
|
|
if (setsockopt(socketid, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i))
|
|
|
|
== SOCKET_ERROR)
|
2010-02-15 23:26:55 +00:00
|
|
|
return -1;
|
2010-06-20 10:03:05 +00:00
|
|
|
net_broadcastsocket = socketid;
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
int WINS_Broadcast (int 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-20 10:03:05 +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-02-15 23:26:55 +00:00
|
|
|
WINS_GetLocalAddress();
|
2010-06-20 10:03:05 +00:00
|
|
|
ret = WINS_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-20 10:03:05 +00:00
|
|
|
return WINS_Write (socketid, buf, len, &broadcastaddr);
|
2010-02-15 23:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
int WINS_Write (int 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-20 10:03:05 +00:00
|
|
|
ret = sendto (socketid, (char *)buf, len, 0, (struct sockaddr *)addr,
|
|
|
|
sizeof(struct qsockaddr));
|
|
|
|
if (ret == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
if (WSAGetLastError() == WSAEWOULDBLOCK)
|
2010-02-15 23:26:55 +00:00
|
|
|
return 0;
|
2010-06-20 10:03:05 +00:00
|
|
|
}
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
char *WINS_AddrToString (struct qsockaddr *addr)
|
|
|
|
{
|
|
|
|
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-06-20 10:03:05 +00:00
|
|
|
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));
|
2010-02-15 23:26:55 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int WINS_StringToAddr (char *string, struct qsockaddr *addr)
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
addr->sa_family = AF_INET;
|
|
|
|
((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
|
|
|
|
((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
int WINS_GetSocketAddr (int socketid, struct qsockaddr *addr)
|
2010-02-15 23:26:55 +00:00
|
|
|
{
|
|
|
|
int addrlen = sizeof(struct qsockaddr);
|
|
|
|
unsigned int a;
|
|
|
|
|
|
|
|
Q_memset(addr, 0, sizeof(struct qsockaddr));
|
2010-06-20 10:03:05 +00:00
|
|
|
getsockname(socketid, (struct sockaddr *)addr, &addrlen);
|
2010-02-15 23:26:55 +00:00
|
|
|
a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
|
|
|
|
if (a == 0 || a == inet_addr("127.0.0.1"))
|
|
|
|
((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int WINS_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)
|
|
|
|
{
|
|
|
|
Q_strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_strcpy (name, WINS_AddrToString (addr));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int WINS_GetAddrFromName(char *name, struct qsockaddr *addr)
|
|
|
|
{
|
|
|
|
struct hostent *hostentry;
|
|
|
|
|
|
|
|
if (name[0] >= '0' && name[0] <= '9')
|
|
|
|
return PartialIPAddress (name, addr);
|
|
|
|
|
2010-06-20 10:03:05 +00:00
|
|
|
hostentry = gethostbyname (name);
|
2010-02-15 23:26:55 +00:00
|
|
|
if (!hostentry)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
addr->sa_family = AF_INET;
|
|
|
|
((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport);
|
2010-06-20 10:03:05 +00:00
|
|
|
((struct sockaddr_in *)addr)->sin_addr.s_addr =
|
|
|
|
*(u_long *)hostentry->h_addr_list[0];
|
2010-02-15 23:26:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
int WINS_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
|
|
|
|
{
|
|
|
|
if (addr1->sa_family != addr2->sa_family)
|
|
|
|
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 WINS_GetSocketPort (struct qsockaddr *addr)
|
|
|
|
{
|
|
|
|
return ntohs(((struct sockaddr_in *)addr)->sin_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int WINS_SetSocketPort (struct qsockaddr *addr, int port)
|
|
|
|
{
|
|
|
|
((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
2010-06-20 10:03:05 +00:00
|
|
|
|