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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "gsPlatformSocket.h"
#include "gsPlatformUtil.h"
#include "gsMemory.h"
// mj-ToDo: remove these and include the files int the linker instead.
// removing reference to other platforms.
// remove all plat specific code from here, move it to the platspecific files.
// Include platform separated functions
#if defined(_X360)
#include "x360/gsSocketX360.c"
#elif defined(_XBOX)
#include "xbox/gsSocketXBox.c"
#elif defined(_WIN32)
#include "win32/gsSocketWin32.c"
#elif defined(_LINUX)
//#include "linux/gsSocketLinux.c"
#elif defined(_MACOSX)
//#include "macosx/gsSocketMacOSX.c"
#elif defined(_NITRO)
#include "nitro/gsSocketNitro.c"
#elif defined(_PS2)
#include "ps2/gsSocketPs2.c"
#elif defined(_PS3)
#include "ps3/gsSocketPs3.c"
#include <sys/select.h>
#elif defined(_PSP)
#include "psp/gsSocketPSP.c"
#elif defined(_REVOLUTION)
#include "revolution/gsSocketRevolution.c"
#else
#error "Missing or unsupported platform"
#endif
// Disable compiler warnings for issues that are unavoidable.
/////////////////////////////////////////////////////////////
#if defined(_MSC_VER) // DevStudio
// Level4, "conditional expression is constant".
// Occurs with use of the MS provided macro FD_SET
#pragma warning ( disable: 4127 )
#endif // _MSC_VER
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int SetSockBlocking(SOCKET sock, int isblocking)
{
int rcode;
#if defined(_REVOLUTION)
int val;
val = SOFcntl(sock, SO_F_GETFL, 0);
if(isblocking)
val &= ~SO_O_NONBLOCK;
else
val |= SO_O_NONBLOCK;
rcode = SOFcntl(sock, SO_F_SETFL, val);
#elif defined(_NITRO)
int val;
val = SOC_Fcntl(sock, SOC_F_GETFL, 0);
if(isblocking)
val &= ~SOC_O_NONBLOCK;
else
val |= SOC_O_NONBLOCK;
rcode = SOC_Fcntl(sock, SOC_F_SETFL, val);
#else
#if defined(_PS2) || defined(_PS3)
// EENet requires int
// SNSystems requires int
// Insock requires int
// PS3 requires int
gsi_i32 argp;
#else
unsigned long argp;
#endif
if(isblocking)
argp = 0;
else
argp = 1;
#ifdef _PS2
#ifdef SN_SYSTEMS
rcode = setsockopt(sock, SOL_SOCKET, (isblocking) ? SO_BIO : SO_NBIO, &argp, sizeof(argp));
#endif
#ifdef EENET
rcode = setsockopt(sock, SOL_SOCKET, SO_NBIO, &argp, sizeof(argp));
#endif
#ifdef INSOCK
if (isblocking)
argp = -1;
else
argp = 5; //added longer timeout to 5ms
sceInsockSetRecvTimeout(sock, argp);
sceInsockSetSendTimeout(sock, argp);
sceInsockSetShutdownTimeout(sock, argp);
GSI_UNUSED(sock);
rcode = 0;
#endif
#elif defined(_PSP)
rcode = setsockopt(sock, SCE_NET_INET_SOL_SOCKET, SCE_NET_INET_SO_NBIO, &argp, sizeof(argp));
#elif defined(_PS3)
rcode = setsockopt(sock, SOL_SOCKET, SO_NBIO, &argp, sizeof(argp));
#else
rcode = ioctlsocket(sock, FIONBIO, &argp);
#endif
#endif
if(rcode == 0)
{
gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Network, GSIDebugLevel_Comment,
"SetSockBlocking: Set socket %d to %s\r\n", (unsigned int)sock, isblocking ? "blocking":"non-blocking");
return 1;
}
gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Network, GSIDebugLevel_Comment,
"SetSockBlocking failed: tried to set socket %d to %s\r\n", (unsigned int)sock, isblocking ? "blocking":"non-blocking");
return 0;
}
int SetSockBroadcast(SOCKET sock)
{
#if !defined(INSOCK) && !defined(_NITRO) && !defined(_REVOLUTION)
int optval = 1;
if(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval)) != 0)
return 0;
#else
GSI_UNUSED(sock);
#endif
return 1;
}
int DisableNagle(SOCKET sock)
{
#if defined(_WIN32) || defined(_UNIX)
int rcode;
int noDelay = 1;
rcode = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&noDelay, sizeof(int));
return gsiSocketIsError(rcode);
#else
GSI_UNUSED(sock);
// not supported
return 0;
#endif // moved this to here to silence VC warning
}
#ifndef INSOCK
int SetReceiveBufferSize(SOCKET sock, int size)
{
int rcode;
rcode = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char *)&size, sizeof(int));
return gsiSocketIsNotError(rcode);
}
int SetSendBufferSize(SOCKET sock, int size)
{
int rcode;
rcode = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char *)&size, sizeof(int));
return gsiSocketIsNotError(rcode);
}
int GetReceiveBufferSize(SOCKET sock)
{
int rcode;
int size;
int len;
len = sizeof(size);
rcode = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&size, &len);
if(gsiSocketIsError(rcode))
return -1;
return size;
}
int GetSendBufferSize(SOCKET sock)
{
int rcode;
int size;
int len;
len = sizeof(size);
rcode = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&size, &len);
if(gsiSocketIsError(rcode))
return -1;
return size;
}
// Formerly known as ghiSocketSelect
#ifdef SN_SYSTEMS
#undef FD_SET
#define FD_SET(s,p) ((p)->array[((s) - 1) >> SN_FD_SHR] |= \
(unsigned int)(1 << (((s) - 1) & SN_FD_BITS)) )
#endif
#endif
#if !defined(_NITRO) && !defined(INSOCK) && !defined(_REVOLUTION)
int GSISocketSelect(SOCKET theSocket, int* theReadFlag, int* theWriteFlag, int* theExceptFlag)
{
fd_set aReadSet;
fd_set aWriteSet;
fd_set aExceptSet;
fd_set * aReadFds = NULL;
fd_set * aWriteFds = NULL;
fd_set * aExceptFds = NULL;
int aResult;
// 04-13-2005, Saad Nader
// Added case for SN Systems that would
// handle errors after performing selects.
#ifdef SN_SYSTEMS
int aOut, aOutLen = sizeof(aOut);
#endif
struct timeval aTimeout = { 0, 0 };
if (theSocket == INVALID_SOCKET)
return -1;
// Setup the parameters.
////////////////////////
if(theReadFlag != NULL)
{
FD_ZERO(&aReadSet);
FD_SET(theSocket,&aReadSet);
aReadFds = &aReadSet;
}
if(theWriteFlag != NULL)
{
FD_ZERO(&aWriteSet);
FD_SET(theSocket, &aWriteSet);
aWriteFds = &aWriteSet;
}
if(theExceptFlag != NULL)
{
FD_ZERO(&aExceptSet);
FD_SET(theSocket, &aExceptSet);
aExceptFds = &aExceptSet;
}
#ifdef _PS3
// to do, port what is below in the else
//int socketselect(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);
aResult = socketselect(FD_SETSIZE, aReadFds, aWriteFds, aExceptFds, &aTimeout);
#else
// Perform the select
aResult = select(FD_SETSIZE, aReadFds, aWriteFds, aExceptFds, &aTimeout);
#endif
if(gsiSocketIsError(aResult))
return -1;
// 04-13-2005, Saad Nader
// Added case for SN Systems that would
// handle errors after performing selects.
#ifdef SN_SYSTEMS
getsockopt(theSocket, SOL_SOCKET, SO_ERROR, (char *)&aOut, &aOutLen);
if (aOut != 0)
{
return 0;
}
#endif
// Check results.
/////////////////
if(theReadFlag != NULL)
{
if((aResult > 0) && FD_ISSET(theSocket, aReadFds))
*theReadFlag = 1;
else
*theReadFlag = 0;
}
if(theWriteFlag != NULL)
{
if((aResult > 0) && FD_ISSET(theSocket, aWriteFds))
*theWriteFlag = 1;
else
*theWriteFlag = 0;
}
if(theExceptFlag != NULL)
{
if((aResult > 0) && FD_ISSET(theSocket, aExceptFds))
*theExceptFlag = 1;
else
*theExceptFlag = 0;
}
return aResult; // 0 or 1 at this point
}
#endif // !nitro && !revolution && !insock
// Return 1 for immediate recv, otherwise 0
int CanReceiveOnSocket(SOCKET sock)
{
int aReadFlag = 0;
if (1 == GSISocketSelect(sock, &aReadFlag, NULL, NULL))
return aReadFlag;
// SDKs expect 0 on SOCKET_ERROR
return 0;
}
// Return 1 for immediate send, otherwise 0
int CanSendOnSocket(SOCKET sock)
{
int aWriteFlag = 0;
if (1 == GSISocketSelect(sock, NULL, &aWriteFlag, NULL))
return aWriteFlag;
// SDKs expect 0 on SOCKET_ERROR
return 0;
}
#if defined(_PS3) || defined (_PSP)
#else
HOSTENT * getlocalhost(void)
{
#ifdef EENET
#define MAX_IPS 5
static HOSTENT localhost;
static char * aliases = NULL;
static char * ipPtrs[MAX_IPS + 1];
static unsigned int ips[MAX_IPS];
struct sceEENetIfname * interfaces;
struct sceEENetIfname * interface;
int num;
int i;
int count;
int len;
u_short flags;
IN_ADDR address;
// initialize the host
localhost.h_name = "localhost";
localhost.h_aliases = &aliases;
localhost.h_addrtype = AF_INET;
localhost.h_length = 0;
localhost.h_addr_list = ipPtrs;
// get the local interfaces
sceEENetGetIfnames(NULL, &num);
interfaces = (struct sceEENetIfname *)gsimalloc(num * sizeof(struct sceEENetIfname));
if(!interfaces)
return NULL;
sceEENetGetIfnames(interfaces, &num);
// loop through the interfaces
count = 0;
for(i = 0 ; i < num ; i++)
{
// the next interface
interface = &interfaces[i];
//printf("eenet%d: %s\n", i, interface->ifn_name);
// get the flags
len = sizeof(flags);
if(sceEENetGetIfinfo(interface->ifn_name, sceEENET_IFINFO_IFFLAGS, &flags, &len) != 0)
continue;
//printf("eenet%d flags: 0x%X\n", i, flags);
// check for up, running, and non-loopback
if(!(flags & (IFF_UP|IFF_RUNNING)) || (flags & IFF_LOOPBACK))
continue;
//printf("eenet%d: up and running, non-loopback\n", i);
// get the address
len = sizeof(address);
if(sceEENetGetIfinfo(interface->ifn_name, sceEENET_IFINFO_ADDR, &address, &len) != 0)
continue;
//printf("eenet%d: %s\n", i, inet_ntoa(address));
// add this address
ips[count] = address.s_addr;
ipPtrs[count] = (char *)&ips[count];
count++;
}
// free the interfaces
gsifree(interfaces);
// check that we got at least one IP
if(!count)
return NULL;
// finish filling in the host struct
localhost.h_length = (gsi_u16)sizeof(ips[0]);
ipPtrs[count] = NULL;
return &localhost;
////////////////////
// INSOCK
#elif defined(INSOCK)
// Global storage
#define MAX_IPS sceLIBNET_MAX_INTERFACE
static HOSTENT localhost;
static char * aliases = NULL;
static char * ipPtrs[MAX_IPS + 1];
static unsigned int ips[MAX_IPS];
// Temp storage
int aInterfaceIdArray[MAX_IPS];
int aNumInterfaces = 0;
int aInterfaceNum = 0;
int aCount = 0;
// Get the list of interfaces
aNumInterfaces = sceInetGetInterfaceList(&gGSIInsockClientData,
&gGSIInsockSocketBuffer, aInterfaceIdArray, MAX_IPS);
if (aNumInterfaces < 1)
return NULL;
// initialize the HOSTENT
localhost.h_name = "localhost";
localhost.h_aliases = &aliases;
localhost.h_addrtype = AF_INET;
localhost.h_addr_list = ipPtrs;
// Look up each address and copy into the HOSTENT structure
aCount = 0; // count of valid interfaces
for (aInterfaceNum = 0; aInterfaceNum < aNumInterfaces; aInterfaceNum++)
{
sceInetAddress_t anAddr;
int result = sceInetInterfaceControl(&gGSIInsockClientData, &gGSIInsockSocketBuffer,
aInterfaceIdArray[aInterfaceNum], sceInetCC_GetAddress,
&anAddr, sizeof(anAddr));
if (result == 0)
{
// Add this interface to the array
memcpy(&ips[aCount], anAddr.data, sizeof(ips[aCount]));
ips[aCount] = htonl(ips[aCount]);
ipPtrs[aCount] = (char*)&ips[aCount];
aCount++;
}
}
// Set the final hostent data, then return
localhost.h_length = (gsi_u16)sizeof(ips[0]);
ipPtrs[aCount] = NULL;
return &localhost;
#elif defined(_NITRO)
#define MAX_IPS 5
static HOSTENT localhost;
static char * aliases = NULL;
static char * ipPtrs[MAX_IPS + 1];
static unsigned int ips[MAX_IPS];
int count = 0;
localhost.h_name = "localhost";
localhost.h_aliases = &aliases;
localhost.h_addrtype = AF_INET;
localhost.h_length = 0;
localhost.h_addr_list = (u8 **)ipPtrs;
ips[count] = 0;
IP_GetAddr(NULL, (u8*)&ips[count]);
if(ips[count] == 0)
return NULL;
ipPtrs[count] = (char *)&ips[count];
count++;
localhost.h_length = (gsi_u16)sizeof(ips[0]);
ipPtrs[count] = NULL;
return &localhost;
#elif defined(_REVOLUTION)
#define MAX_IPS 5
static HOSTENT aLocalHost;
static char * aliases = NULL;
int aNumOfIps, i;
int aSizeNumOfIps;
static IPAddrEntry aAddrs[MAX_IPS];
int aAddrsSize, aAddrsSizeInitial;
static u8 * ipPtrs[MAX_IPS + 1];
static unsigned int ips[MAX_IPS];
int ret;
aSizeNumOfIps = sizeof(aNumOfIps);
ret = SOGetInterfaceOpt(NULL, SO_SOL_CONFIG, SO_CONFIG_IP_ADDR_NUMBER, &aNumOfIps, &aSizeNumOfIps);
if (ret != 0)
return NULL;
aAddrsSize = (int)(MAX_IPS * sizeof(IPAddrEntry));
aAddrsSizeInitial = aAddrsSize;
ret = SOGetInterfaceOpt(NULL, SO_SOL_CONFIG, SO_CONFIG_IP_ADDR_TABLE, &aAddrs, &aAddrsSize);
if (ret != 0)
return NULL;
if (aAddrsSize != aAddrsSizeInitial)
{
aNumOfIps = aAddrsSize / (int)sizeof(IPAddrEntry);
}
aLocalHost.h_name = "localhost";
aLocalHost.h_aliases = &aliases;
aLocalHost.h_addrtype = AF_INET;
aLocalHost.h_length = SO_IP4_ALEN;
for (i = 0; i < MAX_IPS; i++)
{
if (i < aNumOfIps)
{
memcpy(&ips[i], &aAddrs[i].addr, sizeof(aAddrs[i].addr));
ipPtrs[i] = (u8 *)&ips[i];
}
else
ipPtrs[i] = NULL;
}
aLocalHost.h_addr_list = ipPtrs;
return &aLocalHost;
#elif defined(_X360)
XNADDR addr;
DWORD rcode;
static HOSTENT localhost;
static char * ipPtrs[2];
static IN_ADDR ip;
while((rcode = XNetGetTitleXnAddr(&addr)) == XNET_GET_XNADDR_PENDING)
msleep(1);
if((rcode == XNET_GET_XNADDR_NONE) || (rcode == XNET_GET_XNADDR_TROUBLESHOOT))
return NULL;
localhost.h_name = "localhost";
localhost.h_aliases = NULL;
localhost.h_addrtype = AF_INET;
localhost.h_length = (gsi_u16)sizeof(IN_ADDR);
localhost.h_addr_list = (gsi_i8 **)ipPtrs;
ip = addr.ina;
ipPtrs[0] = (char *)&ip;
ipPtrs[1] = NULL;
return &localhost;
#elif defined(_XBOX)
return NULL;
#else
char hostname[256] = "";
// get the local host's name
gethostname(hostname, sizeof(hostname));
// return the host for that name
return gethostbyname(hostname);
#endif
}
#endif
int IsPrivateIP(IN_ADDR * addr)
{
int b1;
int b2;
unsigned int ip;
// get the first 2 bytes
ip = ntohl(addr->s_addr);
b1 = (int)((ip >> 24) & 0xFF);
b2 = (int)((ip >> 16) & 0xFF);
// 10.X.X.X
if(b1 == 10)
return 1;
// 172.16-31.X.X
if((b1 == 172) && ((b2 >= 16) && (b2 <= 31)))
return 1;
// 192.168.X.X
if((b1 == 192) && (b2 == 168))
return 1;
return 0;
}
gsi_u32 gsiGetBroadcastIP(void)
{
#if defined(_NITRO)
gsi_u32 ip;
IP_GetBroadcastAddr(NULL, (u8*)&ip);
return ip;
#elif defined(_REVOLUTION)
/*
int length;
gsi_u32 ip;
length = (gsi_u32)sizeof(ip);
// IP_GetBroadcastAddr replaced by SOGetInterfaceOpt
// IP_GetBroadcastAddr(NULL, (u8*)&ip);
SOGetInterfaceOpt(NULL, SO_SOL_IP, SO_INADDR_BROADCAST, (u8*)&ip, &length);
*/
IPAddrEntry* addrtbl;
int addrnum;
int ret;
int length;
gsi_u32 ip;
length = (int)sizeof( addrnum );
ret = SOGetInterfaceOpt(NULL,
SO_SOL_CONFIG,
SO_CONFIG_IP_ADDR_NUMBER,
(u8*)&addrnum, &length);
if( ret >= 0 )
return 0xFFFFFFFF;
length = (int)(sizeof( IPAddrEntry ) * addrnum);
addrtbl = (IPAddrEntry*)gsimalloc( (u32)length );
if( addrtbl == NULL )
return 0xFFFFFFFF;
ret = SOGetInterfaceOpt(NULL,
SO_SOL_CONFIG,
SO_CONFIG_IP_ADDR_TABLE,
(u8*)addrtbl,
&length);
if( ret < 0 )
{
gsifree( addrtbl );
return 0xFFFFFFFF;
}
ip = (u32)(addrtbl->bcastAddr[3]
| (addrtbl->bcastAddr[2] << 8)
| (addrtbl->bcastAddr[1] << 16)
| (addrtbl->bcastAddr[0] << 24));
gsifree( addrtbl );
return ip;
return ip;
#else
return 0xFFFFFFFF;
#endif
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Re-enable previously disabled compiler warnings
///////////////////////////////////////////////////
#if defined(_MSC_VER)
#pragma warning ( default: 4127 )
#endif // _MSC_VER
|