1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
/*
GameSpy GT2 SDK
Dan "Mr. Pants" Schoenblum
dan@gamespy.com
Copyright 2002 GameSpy Industries, Inc
devsupport@gamespy.com
*/
#include "gt2Main.h"
#include "gt2Utility.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
#include <sys/types.h>
#include <sys/timeb.h>
#endif
#define GTI2_STACK_HOSTLEN_MAX 256
/*************************
** BYTE ORDER FUNCTIONS **
*************************/
unsigned int gt2NetworkToHostInt(unsigned int i)
{
return (unsigned int)ntohl(i);
}
unsigned int gt2HostToNetworkInt(unsigned int i)
{
return (unsigned int)htonl(i);
}
unsigned short gt2NetworkToHostShort(unsigned short s)
{
return ntohs(s);
}
unsigned short gt2HostToNetworkShort(unsigned short s)
{
return htons(s);
}
/*******************************
** INTERNET ADDRESS FUNCTIONS **
*******************************/
const char * gt2AddressToString(unsigned int ip, unsigned short port, char string[22])
{
static char strAddressArray[2][22];
static int nIndex;
char * strAddress;
if(string)
strAddress = string;
else
{
nIndex ^= 1;
strAddress = strAddressArray[nIndex];
}
if(ip)
{
IN_ADDR inAddr;
inAddr.s_addr = ip;
if(port)
sprintf(strAddress, "%s:%d", inet_ntoa(inAddr), port);
else
sprintf(strAddress, "%s", inet_ntoa(inAddr));
}
else if(port)
sprintf(strAddress, ":%d", port);
else
strAddress[0] = '\0';
return strAddress;
}
GT2Bool gt2StringToAddress(const char * string, unsigned int * ip, unsigned short * port)
{
unsigned int srcIP = 0; // avoid use uninit condition
unsigned short srcPort = 0;
if(!string || !string[0])
{
srcIP = 0;
srcPort = 0;
}
else
{
char stackHost[GTI2_STACK_HOSTLEN_MAX + 1];
const char * colon;
const char * host;
// Is there a port?
colon = strchr(string, ':');
if(!colon)
{
// The string is the host.
host = string;
// No port.
srcPort = 0;
}
else
{
int len;
const char * check;
int temp;
// Is it just a port?
if(colon == string)
{
host = NULL;
srcIP = 0;
}
else
{
// Copy the host portion into the array on the stack.
len = (colon - string);
assert(len < GTI2_STACK_HOSTLEN_MAX);
memcpy(stackHost, string, (unsigned int)len);
stackHost[len] = '\0';
host = stackHost;
}
// Check the port.
for(check = (colon + 1) ; *check ; check++)
if(!isdigit(*check))
return GT2False;
// Get the port.
temp = atoi(colon + 1);
if((temp < 0) || (temp > 0xFFFF))
return GT2False;
srcPort = (unsigned short)temp;
}
// Is there a host?
if(host)
{
// Try dotted IP.
/////////////////
srcIP = inet_addr(host);
if(srcIP == INADDR_NONE)
{
#if defined(_XBOX)
return GT2False;
#else
HOSTENT * hostent;
hostent = gethostbyname(host);
if(hostent == NULL)
return GT2False;
srcIP = *(unsigned int *)hostent->h_addr_list[0];
#endif
}
}
}
if(ip)
*ip = srcIP;
if(port)
*port = srcPort;
return GT2True;
}
#ifdef GSI_ADHOC
gsi_u32 gti2MacToIp(const char *mac)
// change mac ethernet to IP address
{
// Mac (48 bit)<---> IP (32 bit) convertion.
// horrible hack. But the chances of a conflict are less then getting struck by lightning
// but in order to translate back, we will need to keep a table
// stick real value in table.
GTI2MacEntry *entry;
int i;
gsi_u32 ip;
memcpy(&ip,mac,4); // store rest in table
// find match in table
for (i=0;i< MAC_TABLE_SIZE;i++)
{
if(MacTable[i].ip == ip)
{
return ip; // already there, don't add to the table.
}
}
// if not found match, add it, overwriting oldest entry
entry = &MacTable[lastmactableentry];
lastmactableentry = (lastmactableentry +1 ) & (MAC_TABLE_SIZE-1);
entry->ip = ip;
memcpy(entry->mac,mac,6);
return ip;
}
void gti2IpToMac(gsi_u32 ip,char *mac)
// change IP address to mac ethernet
{
int i;
// find match in table
for (i=0;i< MAC_TABLE_SIZE;i++)
{
if(MacTable[i].ip == ip)
{
memcpy(mac,MacTable[i].mac,6);
return;
}
}
// error, can not find mac address
memset(mac,0,6);
GS_FAIL();
}
#endif
#if defined(_XBOX)
// XBox doesn't support the address functions
static const char * gti2HandleHostInfo(HOSTENT * host, char *** aliases, unsigned int *** ips) { return NULL; }
const char * gt2IPToHostInfo(unsigned int ip, char *** aliases, unsigned int *** ips) { return NULL; }
const char * gt2StringToHostInfo(const char * string, char *** aliases, unsigned int *** ips) { return NULL; }
unsigned int gt2XnAddrToIP(XNADDR theAddr, XNKID theKeyId)
{
// String to fit ip address + : + port
IN_ADDR anAddr;
if (XNetXnAddrToInAddr(&theAddr, &theKeyId, &anAddr)== 0)
return anAddr.s_addr;
return 0;
}
GT2Bool gt2IPToXnAddr(int ip, XNADDR *theAddr, XNKID *theKeyId)
{
IN_ADDR anAddr;
anAddr.s_addr = ip;
if (XNetInAddrToXnAddr(anAddr, theAddr, theKeyId) == 0)
return GT2True;
return GT2False;
}
#else
static const char * gti2HandleHostInfo(HOSTENT * host, char *** aliases, unsigned int *** ips)
{
if(!host || (host->h_addrtype != AF_INET) || (host->h_length != 4))
return NULL;
if(aliases)
*aliases = host->h_aliases;
if(ips)
*ips = (unsigned int **)host->h_addr_list;
return host->h_name;
}
const char * gt2IPToHostInfo(unsigned int ip, char *** aliases, unsigned int *** ips)
{
#ifdef _PSP
return NULL;
#else
HOSTENT * host;
host = gethostbyaddr((const char *)&ip, 4, AF_INET);
GSI_UNUSED(ip);
return gti2HandleHostInfo(host, aliases, ips);
#endif
}
const char * gt2StringToHostInfo(const char * string, char *** aliases, unsigned int *** ips)
{
HOSTENT * host;
unsigned int ip;
if(!string || !string[0])
return NULL;
// Is the string actually a dotted IP?
ip = inet_addr(string);
if(ip != INADDR_NONE)
return gt2IPToHostInfo(ip, aliases, ips);
host = gethostbyname(string);
return gti2HandleHostInfo(host, aliases, ips);
}
#endif
const char * gt2IPToHostname(unsigned int ip)
{
return gt2IPToHostInfo(ip, NULL, NULL);
}
const char * gt2StringToHostname(const char * string)
{
return gt2StringToHostInfo(string, NULL, NULL);
}
char ** gt2IPToAliases(unsigned int ip)
{
char ** aliases;
if(!gt2IPToHostInfo(ip, &aliases, NULL))
return NULL;
return aliases;
}
char ** gt2StringToAliases(const char * string)
{
char ** aliases;
if(!gt2StringToHostInfo(string, &aliases, NULL))
return NULL;
return aliases;
}
unsigned int ** gt2IPToIPs(unsigned int ip)
{
unsigned int ** ips;
if(!gt2IPToHostInfo(ip, NULL, &ips))
return NULL;
return ips;
}
unsigned int ** gt2StringToIPs(const char * string)
{
unsigned int ** ips;
if(!gt2StringToHostInfo(string, NULL, &ips))
return NULL;
return ips;
}
/***********************
** INTERNAL FUNCTIONS **
***********************/
#ifdef __MWERKS__ // CodeWarrior will warn if not prototyped
void gti2MessageCheck(const GT2Byte ** message, int * len);
#endif
// Used from gt2main.c
void gti2MessageCheck(const GT2Byte ** message, int * len)
{
// check for an empty message
if(!*message)
{
*message = (const GT2Byte *)"";
*len = 0;
}
// check for calculating the message length
else if(*len == -1)
{
*len = (int)(strlen((const char *)*message) + 1);
}
}
#ifdef RECV_LOG
void gti2LogMessage
(
unsigned int fromIP, unsigned short fromPort,
unsigned int toIP, unsigned short toPort,
const GT2Byte * message, int len
)
{
FILE * file;
IN_ADDR ip;
int i;
#ifdef WIN32
struct _timeb utcTime;
struct tm * now;
#endif
file = fopen("recv.log", "at");
if(!file)
return;
// date-time
#ifdef WIN32
_ftime(&utcTime);
now = localtime(&utcTime.time);
fprintf(file, "%02d.%02d.%02d %02d:%02d:%02d.%03d\n",
now->tm_year - 100, now->tm_mon + 1, now->tm_mday,
now->tm_hour, now->tm_min, now->tm_sec, utcTime.millitm);
#endif
// from
ip.s_addr = fromIP;
fprintf(file, "%s:%d -> ", inet_ntoa(ip), fromPort);
// to
ip.s_addr = toIP;
fprintf(file, "%s:%d\n", inet_ntoa(ip), toPort);
// data
fprintf(file, "%d: ", len);
for(i = 0 ; i < len ; i++)
fprintf(file, "%02X ", message[i]);
fprintf(file, "\n\n");
fclose(file);
}
#endif
|