• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

SocketLayer.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #include "SocketLayer.h"
00010 #include "RakAssert.h"
00011 #include "RakNetTypes.h"
00012 #include "CCRakNetUDT.h"
00013 #include "GetTime.h"
00014 
00015 #ifdef _WIN32
00016 #elif !defined(_PS3) && !defined(__PS3__) && !defined(SN_TARGET_PS3)
00017 #include <string.h> // memcpy
00018 #include <unistd.h>
00019 #include <fcntl.h>
00020 #include <arpa/inet.h>
00021 #include <errno.h>  // error numbers
00022 #include <stdio.h> // RAKNET_DEBUG_PRINTF
00023 #include <ifaddrs.h>
00024 #include <netinet/in.h>
00025 #include <net/if.h>
00026 #include <sys/types.h>
00027 #include <sys/socket.h>
00028 #include <sys/ioctl.h>
00029 
00030 #endif
00031 
00032 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00033                                                                
00034 #endif
00035 
00036 #if defined(_XBOX) || defined(X360)
00037                                                           
00038 #elif defined(_WIN32)
00039 #include "WSAStartupSingleton.h"
00040 #include <ws2tcpip.h> // 'IP_DONTFRAGMENT' 'IP_TTL'
00041 #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00042                                
00043 #else
00044 #define closesocket close
00045 #include <unistd.h>
00046 #endif
00047 
00048 #include "RakSleep.h"
00049 #include <stdio.h>
00050 
00051 #include "ExtendedOverlappedPool.h"
00052 
00053 #ifdef _MSC_VER
00054 #pragma warning( push )
00055 #endif
00056 
00057 SocketLayer SocketLayer::I;
00058 
00059 extern void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetSmartPtr<RakNetSocket> rakNetSocket, RakNetTimeUS timeRead );
00060 extern void ProcessNetworkPacket( const SystemAddress systemAddress, const char *data, const int length, RakPeer *rakPeer, RakNetTimeUS timeRead );
00061 extern void ProcessPortUnreachable( const unsigned int binaryAddress, const unsigned short port, RakPeer *rakPeer );
00062 
00063 #ifdef _DEBUG
00064 #include <stdio.h>
00065 #endif
00066 
00067 SocketLayer::SocketLayer()
00068 {
00069 #ifdef _WIN32
00070         WSAStartupSingleton::AddRef();
00071 #endif
00072         slo=0;
00073 }
00074 
00075 SocketLayer::~SocketLayer()
00076 {
00077 #ifdef _WIN32
00078         WSAStartupSingleton::Deref();
00079 #endif
00080 }
00081 
00082 SOCKET SocketLayer::Connect( SOCKET writeSocket, unsigned int binaryAddress, unsigned short port )
00083 {
00084         RakAssert( writeSocket != (SOCKET) -1 );
00085         sockaddr_in connectSocketAddress;
00086         memset(&connectSocketAddress,0,sizeof(sockaddr_in));
00087 
00088         connectSocketAddress.sin_family = AF_INET;
00089         connectSocketAddress.sin_port = htons( port );
00090         connectSocketAddress.sin_addr.s_addr = binaryAddress;
00091 
00092         if ( connect( writeSocket, ( struct sockaddr * ) & connectSocketAddress, sizeof( struct sockaddr ) ) != 0 )
00093         {
00094 #if defined(_WIN32) && !defined(_XBOX) && defined(_DEBUG) && !defined(X360)
00095                 DWORD dwIOError = GetLastError();
00096                 LPVOID messageBuffer;
00097                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00098                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00099                         ( LPTSTR ) &messageBuffer, 0, NULL );
00100                 // something has gone wrong here...
00101                 RAKNET_DEBUG_PRINTF( "WSAConnect failed:Error code - %d\n%s", dwIOError, messageBuffer );
00102                 //Free the buffer.
00103                 LocalFree( messageBuffer );
00104 #endif
00105         }
00106 
00107         return writeSocket;
00108 }
00109 bool SocketLayer::IsPortInUse(unsigned short port, const char *hostAddress)
00110 {
00111         SOCKET listenSocket;
00112         sockaddr_in listenerSocketAddress;
00113         memset(&listenerSocketAddress,0,sizeof(sockaddr_in));
00114         // Listen on our designated Port#
00115         listenerSocketAddress.sin_port = htons( port );
00116         listenSocket = socket( AF_INET, SOCK_DGRAM, 0 );
00117         if ( listenSocket == (SOCKET) -1 )
00118                 return true;
00119         // bind our name to the socket
00120         // Fill in the rest of the address structure
00121         listenerSocketAddress.sin_family = AF_INET;
00122         if ( hostAddress && hostAddress[0] )
00123                 listenerSocketAddress.sin_addr.s_addr = inet_addr( hostAddress );
00124         else
00125                 listenerSocketAddress.sin_addr.s_addr = INADDR_ANY;
00126         int ret = bind( listenSocket, ( struct sockaddr * ) & listenerSocketAddress, sizeof( listenerSocketAddress ) );
00127         closesocket(listenSocket);
00128 
00129 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00130                                   
00131 #else
00132         return ret <= -1;
00133 #endif
00134 }
00135 void SocketLayer::SetDoNotFragment( SOCKET listenSocket, int opt )
00136 {
00137 
00138 #if defined(IP_DONTFRAGMENT )
00139 
00140 #if defined(_WIN32) && !defined(_XBOX) && defined(_DEBUG) && !defined(X360)
00141         // If this assert hit you improperly linked against WSock32.h
00142         RakAssert(IP_DONTFRAGMENT==14);
00143 #endif
00144 
00145         if ( setsockopt( listenSocket, IPPROTO_IP, IP_DONTFRAGMENT, ( char * ) & opt, sizeof ( opt ) ) == -1 )
00146         {
00147 #if defined(_WIN32) && defined(_DEBUG)
00148                 DWORD dwIOError = GetLastError();
00149                 LPVOID messageBuffer;
00150                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00151                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00152                         ( LPTSTR ) & messageBuffer, 0, NULL );
00153                 RAKNET_DEBUG_PRINTF( "setsockopt(IP_DONTFRAGMENT) failed:Error code - %d\n%s", dwIOError, messageBuffer );
00154                 LocalFree( messageBuffer );
00155 #endif
00156         }
00157 #endif
00158 
00159 }
00160 
00161 void SocketLayer::SetNonBlocking( SOCKET listenSocket)
00162 {
00163 #ifdef _WIN32
00164         unsigned long nonBlocking = 1;
00165         ioctlsocket( listenSocket, FIONBIO, &nonBlocking );
00166 #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00167                                                                                                               
00168 #else
00169         int flags = fcntl(listenSocket, F_GETFL, 0);
00170         fcntl(listenSocket, F_SETFL, flags | O_NONBLOCK);
00171 #endif
00172 }
00173 
00174 void SocketLayer::SetSocketOptions( SOCKET listenSocket)
00175 {
00176         int sock_opt = 1;
00177         // // On Vista, can get WSAEACCESS (10013)
00178         /*
00179         if ( setsockopt( listenSocket, SOL_SOCKET, SO_REUSEADDR, ( char * ) & sock_opt, sizeof ( sock_opt ) ) == -1 )
00180         {
00181         #if defined(_WIN32) && !defined(_XBOX) && defined(_DEBUG) && !defined(X360)
00182         DWORD dwIOError = GetLastError();
00183         LPVOID messageBuffer;
00184         FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00185         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00186         ( LPTSTR ) & messageBuffer, 0, NULL );
00187         // something has gone wrong here...
00188         RAKNET_DEBUG_PRINTF( "setsockopt(SO_REUSEADDR) failed:Error code - %d\n%s", dwIOError, messageBuffer );
00189         //Free the buffer.
00190         LocalFree( messageBuffer );
00191         #endif
00192         }
00193         */
00194 
00195         // This doubles the max throughput rate
00196         sock_opt=1024*256;
00197         setsockopt(listenSocket, SOL_SOCKET, SO_RCVBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) );
00198 
00199         // Immediate hard close. Don't linger the socket, or recreating the socket quickly on Vista fails.
00200         sock_opt=0;
00201         setsockopt(listenSocket, SOL_SOCKET, SO_LINGER, ( char * ) & sock_opt, sizeof ( sock_opt ) );
00202 
00203 #if !defined(_PS3) && !defined(__PS3__) && !defined(SN_TARGET_PS3)
00204         // This doesn't make much difference: 10% maybe
00205         // Not supported on console 2
00206         sock_opt=1024*16;
00207         setsockopt(listenSocket, SOL_SOCKET, SO_SNDBUF, ( char * ) & sock_opt, sizeof ( sock_opt ) );
00208 #endif
00209 
00210         /*
00211         #ifdef _WIN32
00212                 unsigned long nonblocking = 1;
00213                 ioctlsocket( listenSocket, FIONBIO, &nonblocking );
00214         #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00215                                                                                                             
00216         #else
00217                 fcntl( listenSocket, F_SETFL, O_NONBLOCK );
00218         #endif
00219         */
00220 
00221         // Set broadcast capable
00222         sock_opt=1;
00223         if ( setsockopt( listenSocket, SOL_SOCKET, SO_BROADCAST, ( char * ) & sock_opt, sizeof( sock_opt ) ) == -1 )
00224                 {
00225 #if defined(_WIN32) && defined(_DEBUG)
00226                 DWORD dwIOError = GetLastError();
00227                 // On Vista, can get WSAEACCESS (10013)
00228                 // See http://support.microsoft.com/kb/819124
00229                 // http://blogs.msdn.com/wndp/archive/2007/03/19/winsock-so-exclusiveaddruse-on-vista.aspx
00230                 // http://msdn.microsoft.com/en-us/library/ms740621(VS.85).aspx
00231 #if !defined(_XBOX) && !defined(X360)
00232                 LPVOID messageBuffer;
00233                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00234                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00235                         ( LPTSTR ) & messageBuffer, 0, NULL );
00236                 // something has gone wrong here...
00237                 RAKNET_DEBUG_PRINTF( "setsockopt(SO_BROADCAST) failed:Error code - %d\n%s", dwIOError, messageBuffer );
00238                 //Free the buffer.
00239                 LocalFree( messageBuffer );
00240 #endif
00241 #endif
00242 
00243                 }
00244 }
00245 SOCKET SocketLayer::CreateBoundSocket_PS3Lobby( unsigned short port, bool blockingSocket, const char *forceHostAddress )
00246 {
00247         (void) port;
00248         (void) blockingSocket;
00249         (void) forceHostAddress;
00250 
00251 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00252                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
00253 #else
00254         return 0;
00255 #endif
00256 }
00257 SOCKET SocketLayer::CreateBoundSocket( unsigned short port, bool blockingSocket, const char *forceHostAddress, unsigned int sleepOn10048 )
00258 {
00259         (void) blockingSocket;
00260 
00261         int ret;
00262         SOCKET listenSocket;
00263         sockaddr_in listenerSocketAddress;
00264         memset(&listenerSocketAddress,0,sizeof(sockaddr_in));
00265         // Listen on our designated Port#
00266         listenerSocketAddress.sin_port = htons( port );
00267 #if (defined(_XBOX) || defined(_X360)) && defined(RAKNET_USE_VDP)
00268         listenSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_VDP );
00269 #else
00270         listenSocket = socket( AF_INET, SOCK_DGRAM, 0 );
00271 #endif
00272 
00273         if ( listenSocket == (SOCKET) -1 )
00274         {
00275 #if defined(_WIN32) && !defined(_XBOX) && defined(_DEBUG)
00276                 DWORD dwIOError = GetLastError();
00277                 LPVOID messageBuffer;
00278                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00279                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00280                         ( LPTSTR ) & messageBuffer, 0, NULL );
00281                 // something has gone wrong here...
00282                 RAKNET_DEBUG_PRINTF( "socket(...) failed:Error code - %d\n%s", dwIOError, messageBuffer );
00283                 //Free the buffer.
00284                 LocalFree( messageBuffer );
00285 #endif
00286 
00287                 return (SOCKET) -1;
00288         }
00289 
00290         SetSocketOptions(listenSocket);
00291 
00292         // Fill in the rest of the address structure
00293         listenerSocketAddress.sin_family = AF_INET;
00294 
00295         if (forceHostAddress && forceHostAddress[0])
00296         {
00297 //              printf("Force binding %s:%i\n", forceHostAddress, port);
00298                 listenerSocketAddress.sin_addr.s_addr = inet_addr( forceHostAddress );
00299         }
00300         else
00301         {
00302 //              printf("Binding any on port %i\n", port);
00303                 listenerSocketAddress.sin_addr.s_addr = INADDR_ANY;
00304         }
00305 
00306         // bind our name to the socket
00307         ret = bind( listenSocket, ( struct sockaddr * ) & listenerSocketAddress, sizeof( listenerSocketAddress ) );
00308 
00309         if ( ret <= -1 )
00310         {
00311 #if defined(_WIN32) && !defined(_XBOX) && !defined(X360)
00312                 DWORD dwIOError = GetLastError();
00313                 if (dwIOError==10048)
00314                 {
00315                         if (sleepOn10048==0)
00316                                 return (SOCKET) -1;
00317                         // Vista has a bug where it returns WSAEADDRINUSE (10048) if you create, shutdown, then rebind the socket port unless you wait a while first.
00318                         // Wait, then rebind
00319                         RakSleep(100);
00320 
00321                         closesocket(listenSocket);
00322                         listenerSocketAddress.sin_port = htons( port );
00323                         listenSocket = socket( AF_INET, SOCK_DGRAM, 0 );
00324                         if ( listenSocket == (SOCKET) -1 )
00325                                 return false;
00326                         SetSocketOptions(listenSocket);
00327 
00328                         // Fill in the rest of the address structure
00329                         listenerSocketAddress.sin_family = AF_INET;
00330                         if (forceHostAddress && forceHostAddress[0])
00331                                 listenerSocketAddress.sin_addr.s_addr = inet_addr( forceHostAddress );
00332                         else
00333                                 listenerSocketAddress.sin_addr.s_addr = INADDR_ANY;
00334 
00335                         // bind our name to the socket
00336                         ret = bind( listenSocket, ( struct sockaddr * ) & listenerSocketAddress, sizeof( listenerSocketAddress ) );
00337 
00338                         if ( ret >= 0 )
00339                                 return listenSocket;
00340                 }
00341                 dwIOError = GetLastError();
00342                 LPVOID messageBuffer;
00343                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00344                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00345                         ( LPTSTR ) & messageBuffer, 0, NULL );
00346                 // something has gone wrong here...
00347                 RAKNET_DEBUG_PRINTF( "bind(...) failed:Error code - %d\n%s", (unsigned int) dwIOError, (char*) messageBuffer );
00348                 //Free the buffer.
00349                 LocalFree( messageBuffer );
00350 #elif (defined(__GNUC__)  || defined(__GCCXML__) || defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)) && !defined(__WIN32)
00351                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
00352 #endif
00353 
00354                 return (SOCKET) -1;
00355         }
00356 
00357         return listenSocket;
00358 }
00359 
00360 const char* SocketLayer::DomainNameToIP( const char *domainName )
00361 {
00362         struct in_addr addr;
00363 
00364 #if defined(_XBOX) || defined(X360)
00365                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
00366 #else
00367         struct hostent * phe = gethostbyname( domainName );
00368 
00369         if ( phe == 0 || phe->h_addr_list[ 0 ] == 0 )
00370         {
00371                 //cerr << "Yow! Bad host lookup." << endl;
00372                 return 0;
00373         }
00374 
00375         if (phe->h_addr_list[ 0 ]==0)
00376                 return 0;
00377 
00378         memcpy( &addr, phe->h_addr_list[ 0 ], sizeof( struct in_addr ) );
00379         return inet_ntoa( addr );
00380 #endif
00381 
00382 //      return "";
00383 }
00384 
00385 
00386 void SocketLayer::Write( const SOCKET writeSocket, const char* data, const int length )
00387 {
00388 #ifdef _DEBUG
00389         RakAssert( writeSocket != (SOCKET) -1 );
00390 #endif
00391 
00392         send( writeSocket, data, length, 0 );
00393 }
00394 int SocketLayer::RecvFrom( const SOCKET s, RakPeer *rakPeer, int *errorCode, RakNetSmartPtr<RakNetSocket> rakNetSocket, unsigned short remotePortRakNetWasStartedOn_PS3 )
00395 {
00396         int len=0;
00397 #if (defined(_XBOX) || defined(_X360)) && defined(RAKNET_USE_VDP)
00398         char dataAndVoice[ MAXIMUM_MTU_SIZE*2 ];
00399         char *data=&dataAndVoice[sizeof(unsigned short)]; // 2 bytes in
00400 #else
00401         char data[ MAXIMUM_MTU_SIZE ];
00402 #endif
00403 
00404         if (slo)
00405         {
00406                 SystemAddress sender;
00407                 len = slo->RakNetRecvFrom(s,rakPeer,data,&sender,true);
00408                 if (len>0)
00409                 {
00410                         ProcessNetworkPacket( sender, data, len, rakPeer, rakNetSocket, RakNet::GetTimeUS() );
00411                         return 1;
00412                 }
00413         }
00414 
00415         if ( s == (SOCKET) -1 )
00416         {
00417                 *errorCode = -1;
00418                 return -1;
00419         }
00420 
00421 #if defined (_WIN32) || !defined(MSG_DONTWAIT)
00422         const int flag=0;
00423 #else
00424         const int flag=MSG_DONTWAIT;
00425 #endif
00426 
00427         sockaddr_in sa;
00428         memset(&sa,0,sizeof(sockaddr_in));
00429         socklen_t len2;
00430         unsigned short portnum=0;
00431         if (remotePortRakNetWasStartedOn_PS3!=0)
00432         {
00433 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00434                                                                                                                                                                                                                                                                            
00435 #endif
00436         }
00437         else
00438         {
00439                 len2 = sizeof( sa );
00440                 sa.sin_family = AF_INET;
00441                 sa.sin_port=0;
00442 
00443 #if (defined(_XBOX) || defined(_X360)) && defined(RAKNET_USE_VDP)
00444 
00445                 /*
00446                 DWORD zero=0;
00447                 WSABUF wsaBuf;
00448                 DWORD lenDword=0;
00449                 wsaBuf.buf=dataAndVoice;
00450                 wsaBuf.len=sizeof(dataAndVoice);
00451                 int result = WSARecvFrom( s, 
00452                         &wsaBuf,
00453                         1,
00454                         &lenDword,
00455                         &zero,
00456                         ( sockaddr* ) & sa, ( socklen_t* ) & len2,
00457                         0,0     );
00458                 len=lenDword;
00459                 */
00460 
00461                 len = recvfrom( s, dataAndVoice, sizeof(dataAndVoice), flag, ( sockaddr* ) & sa, ( socklen_t* ) & len2 );
00462                 if (len>2)
00463                 {
00464                         // Skip first two bytes
00465                         len-=2;
00466                 }
00467 #else
00468                 len = recvfrom( s, data, MAXIMUM_MTU_SIZE, flag, ( sockaddr* ) & sa, ( socklen_t* ) & len2 );
00469 #endif
00470 
00471                 portnum = ntohs( sa.sin_port );
00472         }
00473 
00474         if ( len == 0 )
00475         {
00476 #ifdef _DEBUG
00477                 RAKNET_DEBUG_PRINTF( "Error: recvfrom returned 0 on a connectionless blocking call\non port %i.  This is a bug with Zone Alarm.  Please turn off Zone Alarm.\n", portnum );
00478                 RakAssert( 0 );
00479 #endif
00480 
00481                 // 4/13/09 Changed from returning -1 to 0, to prevent attackers from sending 0 byte messages to shutdown the server
00482                 *errorCode = 0;
00483                 return 0;
00484         }
00485 
00486         if ( len > 0 )
00487         {
00488                 ProcessNetworkPacket( SystemAddress(sa.sin_addr.s_addr, portnum), data, len, rakPeer, rakNetSocket, RakNet::GetTimeUS() );
00489 
00490                 return 1;
00491         }
00492         else
00493         {
00494                 *errorCode = 0;
00495 
00496 
00497 #if defined(_WIN32) && defined(_DEBUG)
00498 
00499                 DWORD dwIOError = WSAGetLastError();
00500 
00501                 if ( dwIOError == WSAEWOULDBLOCK )
00502                 {
00503                         return SOCKET_ERROR;
00504                 }
00505                 if ( dwIOError == WSAECONNRESET )
00506                 {
00507 #if defined(_DEBUG)
00508 //                      RAKNET_DEBUG_PRINTF( "A previous send operation resulted in an ICMP Port Unreachable message.\n" );
00509 #endif
00510 
00511 
00512                         unsigned short portnum=0;
00513                         ProcessPortUnreachable(sa.sin_addr.s_addr, portnum, rakPeer);
00514                         // *errorCode = dwIOError;
00515                         return -1;
00516                 }
00517                 else
00518                 {
00519 #if defined(_DEBUG) && !defined(_XBOX) && !defined(X360)
00520                         if ( dwIOError != WSAEINTR && dwIOError != WSAETIMEDOUT)
00521                         {
00522                                 LPVOID messageBuffer;
00523                                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00524                                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00525                                         ( LPTSTR ) & messageBuffer, 0, NULL );
00526                                 // something has gone wrong here...
00527                                 RAKNET_DEBUG_PRINTF( "recvfrom failed:Error code - %d\n%s", dwIOError, messageBuffer );
00528 
00529                                 //Free the buffer.
00530                                 LocalFree( messageBuffer );
00531                         }
00532 #endif
00533                 }
00534 #endif
00535         }
00536 
00537         return 0; // no data
00538 }
00539 void SocketLayer::RecvFromBlocking( const SOCKET s, RakPeer *rakPeer, unsigned short remotePortRakNetWasStartedOn_PS3, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNetTimeUS *timeRead )
00540 {
00541         (void) rakPeer;
00542         // Randomly crashes, slo is 0, yet gets inside loop.
00543         /*
00544         if (SocketLayer::Instance()->slo)
00545         {
00546                 SystemAddress sender;
00547                 *bytesReadOut = SocketLayer::Instance()->slo->RakNetRecvFrom(s,rakPeer,dataOut,systemAddressOut,false);
00548                 if (*bytesReadOut>0)
00549                 {
00550                         *timeRead=RakNet::GetTimeUS();
00551                         return;
00552                 }
00553                 else if (*bytesReadOut==0)
00554                 {
00555                         return;
00556                 }
00557                 // Negative, process as normal
00558         }
00559         */
00560 
00561 
00562         sockaddr* sockAddrPtr;
00563         socklen_t sockLen;
00564         socklen_t* socketlenPtr=(socklen_t*) &sockLen;
00565         sockaddr_in sa;
00566         memset(&sa,0,sizeof(sockaddr_in));
00567         char *dataOutModified;
00568         int dataOutSize;
00569         const int flag=0;
00570 
00571         (void) remotePortRakNetWasStartedOn_PS3;
00572 
00573 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00574                                                                                                                                                                                
00575 #endif
00576         {
00577                 sockLen=sizeof(sa);
00578                 sa.sin_family = AF_INET;
00579                 sa.sin_port=0;
00580                 sockAddrPtr=(sockaddr*) &sa;
00581         }
00582 
00583 #if (defined(_XBOX) || defined(_X360)) && defined(RAKNET_USE_VDP)
00584         dataOutModified=dataOut+sizeof(uint16_t);
00585         dataOutSize=MAXIMUM_MTU_SIZE*2;
00586 #else
00587         dataOutModified=dataOut;
00588         dataOutSize=MAXIMUM_MTU_SIZE;
00589 #endif
00590         *bytesReadOut = recvfrom( s, dataOutModified, dataOutSize, flag, sockAddrPtr, socketlenPtr );
00591         if (*bytesReadOut<=0)
00592         {
00593                 /*
00594 #if defined(_WIN32) && !defined(_XBOX) && !defined(X360) && defined(_DEBUG)
00595                 DWORD dwIOError = GetLastError();
00596                 LPVOID messageBuffer;
00597                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00598                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00599                         ( LPTSTR ) & messageBuffer, 0, NULL );
00600                 // something has gone wrong here...
00601                 RAKNET_DEBUG_PRINTF( "sendto failed:Error code - %d\n%s", dwIOError, messageBuffer );
00602 
00603                 //Free the buffer.
00604                 LocalFree( messageBuffer );
00605 #endif
00606                 */
00607                 return;
00608         }
00609         *timeRead=RakNet::GetTimeUS();
00610         
00611 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00612                                                                                                                                                                          
00613 #endif
00614         {
00615                 systemAddressOut->port=ntohs( sa.sin_port );
00616                 systemAddressOut->binaryAddress=sa.sin_addr.s_addr;
00617         }
00618 }
00619 
00620 void SocketLayer::RawRecvFromNonBlocking( const SOCKET s, unsigned short remotePortRakNetWasStartedOn_PS3, char *dataOut, int *bytesReadOut, SystemAddress *systemAddressOut, RakNetTimeUS *timeRead )
00621 {
00622         
00623         sockaddr* sockAddrPtr;
00624         socklen_t sockLen;
00625         socklen_t* socketlenPtr=(socklen_t*) &sockLen;
00626         sockaddr_in sa;
00627         memset(&sa,0,sizeof(sockaddr_in));
00628         char *dataOutModified;
00629         int dataOutSize;
00630         const int flag=0;
00631 
00632         (void) remotePortRakNetWasStartedOn_PS3;
00633 
00634 // This is the wrong place for this - call on the socket before calling the function
00635 //      #if defined(_WIN32)
00636 //      u_long val = 1;
00637 //      ioctlsocket (s,FIONBIO,&val);//non block
00638 //      #else
00639 //      int flags = fcntl(s, F_GETFL, 0);
00640 //      fcntl(s, F_SETFL, flags | O_NONBLOCK);
00641 //      #endif
00642 
00643 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00644                                                                                                                                                                                
00645 #endif
00646         {
00647                 sockLen=sizeof(sa);
00648                 sa.sin_family = AF_INET;
00649                 sa.sin_port=0;
00650                 sockAddrPtr=(sockaddr*) &sa;
00651         }
00652 
00653 #if (defined(_XBOX) || defined(_X360)) && defined(RAKNET_USE_VDP)
00654         dataOutModified=dataOut+sizeof(uint16_t);
00655         dataOutSize=MAXIMUM_MTU_SIZE*2;
00656 #else
00657         dataOutModified=dataOut;
00658         dataOutSize=MAXIMUM_MTU_SIZE;
00659 #endif
00660 
00661         *bytesReadOut = recvfrom( s, dataOutModified, dataOutSize, flag, sockAddrPtr, socketlenPtr );
00662         if (*bytesReadOut<=0)
00663         {
00664                 return;
00665         }
00666         *timeRead=RakNet::GetTimeUS();
00667         
00668 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00669                                                                                                                                                                          
00670 #endif
00671         {
00672                 systemAddressOut->port=ntohs( sa.sin_port );
00673                 systemAddressOut->binaryAddress=sa.sin_addr.s_addr;
00674         }
00675 }
00676 
00677 int SocketLayer::SendTo_PS3Lobby( SOCKET s, const char *data, int length, unsigned int binaryAddress, unsigned short port, unsigned short remotePortRakNetWasStartedOn_PS3 )
00678 {
00679         (void) s;
00680         (void) data;
00681         (void) length;
00682         (void) binaryAddress;
00683         (void) port;
00684         (void) remotePortRakNetWasStartedOn_PS3;
00685 
00686         int len=0;
00687 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00688                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
00689 #endif
00690         return len;
00691 }
00692 int SocketLayer::SendTo_360( SOCKET s, const char *data, int length, const char *voiceData, int voiceLength, unsigned int binaryAddress, unsigned short port )
00693 {
00694         (void) s;
00695         (void) data;
00696         (void) length;
00697         (void) voiceData;
00698         (void) voiceLength;
00699         (void) binaryAddress;
00700         (void) port;
00701 
00702         int len=0;
00703 #if (defined(_XBOX) || defined(_X360)) && defined(RAKNET_USE_VDP)
00704         unsigned short payloadLength=length;
00705         WSABUF buffers[3];
00706         buffers[0].buf=(char*) &payloadLength;
00707         buffers[0].len=sizeof(payloadLength);
00708         buffers[1].buf=(char*) data;
00709         buffers[1].len=length;
00710         buffers[2].buf=(char*) voiceData;
00711         buffers[2].len=voiceLength;
00712         DWORD size = buffers[0].len + buffers[1].len + buffers[2].len;
00713 
00714         sockaddr_in sa;
00715         memset(&sa,0,sizeof(sockaddr_in));
00716         sa.sin_port = htons( port ); // User port
00717         sa.sin_addr.s_addr = binaryAddress;
00718         sa.sin_family = AF_INET;
00719 
00720         int result = WSASendTo(s, (LPWSABUF)buffers, 3, &size, 0, ( const sockaddr* ) & sa, sizeof( sa ), NULL, NULL);
00721         if (result==-1)
00722         {
00723                 DWORD dwIOError = GetLastError();
00724                 int a=5;
00725         }
00726         len=size;
00727 
00728 #endif
00729         return len;
00730 }
00731 int SocketLayer::SendTo_PC( SOCKET s, const char *data, int length, unsigned int binaryAddress, unsigned short port )
00732 {
00733         sockaddr_in sa;
00734         memset(&sa,0,sizeof(sockaddr_in));
00735         sa.sin_port = htons( port ); // User port
00736         sa.sin_addr.s_addr = binaryAddress;
00737         sa.sin_family = AF_INET;
00738         int len=0;
00739         do
00740         {
00741                 len = sendto( s, data, length, 0, ( const sockaddr* ) & sa, sizeof( sa ) );
00742                 if (len<0)
00743                 {
00744 
00745 #if defined(_WIN32) && !defined(_XBOX) && !defined(X360)
00746                         DWORD dwIOError = GetLastError();
00747                         if (dwIOError!= 10040 && dwIOError != WSAEADDRNOTAVAIL)
00748                         {
00749         #if defined(_DEBUG)
00750                                 LPVOID messageBuffer;
00751                                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00752                                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00753                                         ( LPTSTR ) &messageBuffer, 0, NULL );
00754                                 // something has gone wrong here...
00755                                 RAKNET_DEBUG_PRINTF( "SendTo_PC failed:Error code - %d\n%s", dwIOError, messageBuffer );
00756                                 //Free the buffer.
00757                                 LocalFree( messageBuffer );
00758         #endif
00759                         }
00760                         else
00761                         {
00762                                 // buffer size exceeded
00763                                 return -10040;
00764                         }
00765 #endif
00766 
00767                         printf("sendto failed with code %i for char %i and length %i.\n", len, data[0], length);
00768                 }
00769         }
00770         while ( len == 0 );
00771         return len;
00772 }
00773 
00774 #ifdef _MSC_VER
00775 #pragma warning( disable : 4702 ) // warning C4702: unreachable code
00776 #endif
00777 int SocketLayer::SendTo( SOCKET s, const char *data, int length, unsigned int binaryAddress, unsigned short port, unsigned short remotePortRakNetWasStartedOn_PS3 )
00778 {
00779         RakAssert(length<=MAXIMUM_MTU_SIZE-UDP_HEADER_SIZE);
00780         RakAssert(port!=0);
00781         if (slo)
00782         {
00783                 SystemAddress sa(binaryAddress,port);
00784                 return slo->RakNetSendTo(s,data,length,sa);
00785         }
00786 
00787         if ( s == (SOCKET) -1 )
00788         {
00789                 return -1;
00790         }
00791 
00792         int len=0;
00793 
00794         if (remotePortRakNetWasStartedOn_PS3!=0)
00795         {
00796                 len = SendTo_PS3Lobby(s,data,length,binaryAddress,port, remotePortRakNetWasStartedOn_PS3);
00797         }
00798         else
00799         {
00800 
00801 #if (defined(_XBOX) || defined(_X360)) && defined(RAKNET_USE_VDP)
00802                 len = SendTo_360(s,data,length,0,0,binaryAddress,port);
00803 #else
00804                 len = SendTo_PC(s,data,length,binaryAddress,port);
00805 #endif
00806         }
00807 
00808         if ( len != -1 )
00809                 return 0;
00810 
00811 #if defined(_WIN32) && !defined(_WIN32_WCE)
00812 
00813         DWORD dwIOError = WSAGetLastError();
00814 
00815         if ( dwIOError == WSAECONNRESET )
00816         {
00817 #if defined(_DEBUG)
00818                 RAKNET_DEBUG_PRINTF( "A previous send operation resulted in an ICMP Port Unreachable message.\n" );
00819 #endif
00820 
00821         }
00822         else if ( dwIOError != WSAEWOULDBLOCK && dwIOError != WSAEADDRNOTAVAIL)
00823         {
00824 #if defined(_WIN32) && !defined(_XBOX) && !defined(X360) && defined(_DEBUG)
00825                 LPVOID messageBuffer;
00826                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00827                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00828                         ( LPTSTR ) & messageBuffer, 0, NULL );
00829                 // something has gone wrong here...
00830                 RAKNET_DEBUG_PRINTF( "sendto failed:Error code - %d\n%s", dwIOError, messageBuffer );
00831 
00832                 //Free the buffer.
00833                 LocalFree( messageBuffer );
00834 #endif
00835 
00836         }
00837 
00838         return dwIOError;
00839 #endif
00840 
00841         return 1; // error
00842 }
00843 int SocketLayer::SendTo( SOCKET s, const char *data, int length, const char ip[ 16 ], unsigned short port, unsigned short remotePortRakNetWasStartedOn_PS3 )
00844 {
00845         unsigned int binaryAddress;
00846         binaryAddress = inet_addr( ip );
00847         return SendTo( s, data, length, binaryAddress, port,remotePortRakNetWasStartedOn_PS3 );
00848 }
00849 int SocketLayer::SendToTTL( SOCKET s, const char *data, int length, const char ip[ 16 ], unsigned short port, int ttl )
00850 {
00851         unsigned int binaryAddress;
00852         binaryAddress = inet_addr( ip );
00853         SystemAddress sa(binaryAddress,port);
00854 
00855         if (slo)
00856                 return slo->RakNetSendTo(s,data,length,sa);
00857 
00858 #if !defined(_XBOX) && !defined(X360)
00859         int oldTTL;
00860         socklen_t opLen=sizeof(oldTTL);
00861         // Get the current TTL
00862         if (getsockopt(s, IPPROTO_IP, IP_TTL, ( char * ) & oldTTL, &opLen ) == -1)
00863         {
00864 #if defined(_WIN32) && defined(_DEBUG)
00865                 DWORD dwIOError = GetLastError();
00866                 LPVOID messageBuffer;
00867                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00868                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00869                         ( LPTSTR ) & messageBuffer, 0, NULL );
00870                 // something has gone wrong here...
00871                 RAKNET_DEBUG_PRINTF( "getsockopt(IPPROTO_IP,IP_TTL) failed:Error code - %d\n%s", dwIOError, messageBuffer );
00872                 //Free the buffer.
00873                 LocalFree( messageBuffer );
00874 #endif
00875         }
00876 
00877         // Set to TTL
00878         int newTTL=ttl;
00879         if (setsockopt(s, IPPROTO_IP, IP_TTL, ( char * ) & newTTL, sizeof ( newTTL ) ) == -1)
00880         {
00881 
00882 #if defined(_WIN32) && defined(_DEBUG)
00883                 DWORD dwIOError = GetLastError();
00884                 LPVOID messageBuffer;
00885                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00886                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
00887                         ( LPTSTR ) & messageBuffer, 0, NULL );
00888                 // something has gone wrong here...
00889                 RAKNET_DEBUG_PRINTF( "setsockopt(IPPROTO_IP,IP_TTL) failed:Error code - %d\n%s", dwIOError, messageBuffer );
00890                 //Free the buffer.
00891                 LocalFree( messageBuffer );
00892 #endif
00893         }
00894 
00895         // Send
00896         int res = SendTo(s,data,length,ip,port,false);
00897 
00898         // Restore the old TTL
00899         setsockopt(s, IPPROTO_IP, IP_TTL, ( char * ) & oldTTL, opLen );
00900 
00901         return res;
00902 #else
00903         return 0;
00904 #endif
00905 }
00906 
00907 
00908 RakNet::RakString SocketLayer::GetSubNetForSocketAndIp(SOCKET inSock, RakNet::RakString inIpString)
00909 {
00910         RakNet::RakString netMaskString;
00911         RakNet::RakString ipString;
00912 
00913 #if defined(_XBOX) || defined(X360)
00914            
00915 #elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00916            
00917 #elif defined(_WIN32)
00918         INTERFACE_INFO InterfaceList[20];
00919         unsigned long nBytesReturned;
00920         if (WSAIoctl(inSock, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList,
00921                 sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR) {
00922                         return "";
00923         }
00924 
00925         int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
00926 
00927         for (int i = 0; i < nNumInterfaces; ++i)
00928         {
00929                 sockaddr_in *pAddress;
00930                 pAddress = (sockaddr_in *) & (InterfaceList[i].iiAddress);
00931                 ipString=inet_ntoa(pAddress->sin_addr);
00932 
00933                 if (inIpString==ipString)
00934                 {
00935                         pAddress = (sockaddr_in *) & (InterfaceList[i].iiNetmask);
00936                         netMaskString=inet_ntoa(pAddress->sin_addr);
00937                         return netMaskString;
00938                 }
00939         }
00940         return "";
00941 #else
00942 
00943         int fd,fd2;
00944         fd2 = socket(AF_INET, SOCK_DGRAM, 0);
00945 
00946         if(fd2 < 0)
00947         {
00948                 return "";
00949         }
00950 
00951         struct ifconf ifc;
00952         char          buf[1999];
00953         ifc.ifc_len = sizeof(buf);
00954         ifc.ifc_buf = buf;
00955         if(ioctl(fd2, SIOCGIFCONF, &ifc) < 0)
00956         {
00957                 return "";
00958         }
00959 
00960         struct ifreq *ifr;
00961         ifr         = ifc.ifc_req;
00962         int intNum = ifc.ifc_len / sizeof(struct ifreq);
00963         for(int i = 0; i < intNum; i++)
00964         {
00965                 ipString=inet_ntoa(((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr);
00966 
00967                 if (inIpString==ipString)
00968                 {
00969                         struct ifreq ifr2;
00970                         fd = socket(AF_INET, SOCK_DGRAM, 0);
00971                         if(fd < 0)
00972                         {
00973                                 return "";
00974                         }
00975                         ifr2.ifr_addr.sa_family = AF_INET;
00976 
00977                         strncpy(ifr2.ifr_name, ifr[i].ifr_name, IFNAMSIZ-1);
00978 
00979                         ioctl(fd, SIOCGIFNETMASK, &ifr2);
00980 
00981                         close(fd);
00982                         close(fd2);
00983                         netMaskString=inet_ntoa(((struct sockaddr_in *)&ifr2.ifr_addr)->sin_addr);
00984 
00985                         return netMaskString;
00986                 }
00987         }
00988 
00989         close(fd2);
00990         return "";
00991 
00992 #endif
00993 
00994 }
00995 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
00996                                                                                                                                                                                                                                                                                                                                               
00997 #elif defined(_WIN32)
00998 void GetMyIP_Win32( char ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ][ 16 ], unsigned int binaryAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
00999 {
01000         char ac[ 80 ];
01001         if ( gethostname( ac, sizeof( ac ) ) == -1 )
01002         {
01003                 DWORD dwIOError = GetLastError();
01004                 LPVOID messageBuffer;
01005                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
01006                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
01007                         ( LPTSTR ) & messageBuffer, 0, NULL );
01008                 // something has gone wrong here...
01009                 RAKNET_DEBUG_PRINTF( "gethostname failed:Error code - %d\n%s", dwIOError, messageBuffer );
01010                 //Free the buffer.
01011                 LocalFree( messageBuffer );
01012                 return ;
01013         }
01014 
01015         struct hostent *phe = gethostbyname( ac );
01016 
01017         if ( phe == 0 )
01018         {
01019                 DWORD dwIOError = GetLastError();
01020                 LPVOID messageBuffer;
01021                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
01022                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
01023                         ( LPTSTR ) & messageBuffer, 0, NULL );
01024                 // something has gone wrong here...
01025                 RAKNET_DEBUG_PRINTF( "gethostbyname failed:Error code - %d\n%s", dwIOError, messageBuffer );
01026 
01027                 //Free the buffer.
01028                 LocalFree( messageBuffer );
01029                 return ;
01030         }
01031 
01032         struct in_addr addr[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ];
01033         int idx;
01034         for ( idx = 0; idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++idx )
01035         {
01036                 if (phe->h_addr_list[ idx ] == 0)
01037                         break;
01038 
01039                 memcpy( &addr[idx], phe->h_addr_list[ idx ], sizeof( struct in_addr ) );
01040                 binaryAddresses[idx]=addr[idx].S_un.S_addr;
01041                 strcpy( ipList[ idx ], inet_ntoa( addr[idx] ) );
01042 
01043         }
01044 
01045         for ( ; idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++idx )
01046         {
01047                 ipList[idx][0]=0;
01048         }
01049 }
01050 #elif !defined(_XBOX) && !defined(X360)
01051 void GetMyIP_Linux( char ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ][ 16 ], unsigned int binaryAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
01052 {
01053         struct ifaddrs *ifaddr, *ifa;
01054         int family, s;
01055         char host[NI_MAXHOST];
01056         struct in_addr linux_in_addr;
01057 
01058         if (getifaddrs(&ifaddr) == -1) {
01059                 printf( "Error getting interface list\n");
01060         }
01061 
01062         int idx = 0;
01063         for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
01064                 if (!ifa->ifa_addr) continue;
01065                 family = ifa->ifa_addr->sa_family;
01066 
01067                 if (family == AF_INET) {
01068                         s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
01069                         if (s != 0) {
01070                                 printf ("getnameinfo() failed: %s\n", gai_strerror(s));
01071                         }
01072                         printf ("IP address: %s\n", host);
01073                         strcpy( ipList[ idx ], host );
01074                         if (inet_aton(host, &linux_in_addr) == 0) {
01075                                 perror("inet_aton");
01076                         }
01077                         else {
01078                                 binaryAddresses[idx]=linux_in_addr.s_addr;
01079                         }
01080                         idx++;
01081                 }
01082         }
01083 
01084         for ( ; idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++idx )
01085         {
01086                 ipList[idx][0]=0;
01087         }
01088 
01089         freeifaddrs(ifaddr);
01090 }
01091 #endif
01092 
01093 #if !defined(_XBOX) && !defined(X360)
01094 void SocketLayer::GetMyIP( char ipList[ MAXIMUM_NUMBER_OF_INTERNAL_IDS ][ 16 ], unsigned int binaryAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
01095 {
01096 #if defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
01097                                       
01098 #elif defined(_WIN32)
01099         GetMyIP_Win32(ipList, binaryAddresses);
01100 #else
01101         GetMyIP_Linux(ipList, binaryAddresses);
01102 #endif
01103 }
01104 #endif
01105 
01106 unsigned short SocketLayer::GetLocalPort ( SOCKET s )
01107 {
01108         sockaddr_in sa;
01109         memset(&sa,0,sizeof(sockaddr_in));
01110         socklen_t len = sizeof(sa);
01111         if (getsockname(s, (sockaddr*)&sa, &len)!=0)
01112         {
01113 #if defined(_WIN32) && !defined(_XBOX) && !defined(X360) && defined(_DEBUG)
01114                 DWORD dwIOError = GetLastError();
01115                 LPVOID messageBuffer;
01116                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
01117                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
01118                         ( LPTSTR ) & messageBuffer, 0, NULL );
01119                 // something has gone wrong here...
01120                 RAKNET_DEBUG_PRINTF( "getsockname failed:Error code - %d\n%s", dwIOError, messageBuffer );
01121 
01122                 //Free the buffer.
01123                 LocalFree( messageBuffer );
01124 #endif
01125                 return 0;
01126         }
01127         return ntohs(sa.sin_port);
01128 }
01129 
01130 SystemAddress SocketLayer::GetSystemAddress ( SOCKET s )
01131 {
01132         sockaddr_in sa;
01133         memset(&sa,0,sizeof(sockaddr_in));
01134         socklen_t len = sizeof(sa);
01135         if (getsockname(s, (sockaddr*)&sa, &len)!=0)
01136         {
01137 #if defined(_WIN32) && !defined(_XBOX) && !defined(X360) && defined(_DEBUG)
01138                 DWORD dwIOError = GetLastError();
01139                 LPVOID messageBuffer;
01140                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
01141                         NULL, dwIOError, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),  // Default language
01142                         ( LPTSTR ) & messageBuffer, 0, NULL );
01143                 // something has gone wrong here...
01144                 RAKNET_DEBUG_PRINTF( "getsockname failed:Error code - %d\n%s", dwIOError, messageBuffer );
01145 
01146                 //Free the buffer.
01147                 LocalFree( messageBuffer );
01148 #endif
01149                 return UNASSIGNED_SYSTEM_ADDRESS;
01150         }
01151 
01152         SystemAddress out;
01153         out.port=ntohs(sa.sin_port);
01154         out.binaryAddress=sa.sin_addr.s_addr;
01155         return out;
01156 }
01157 
01158 void SocketLayer::SetSocketLayerOverride(SocketLayerOverride *_slo)
01159 {
01160         slo=_slo;
01161 }
01162 
01163 #ifdef _MSC_VER
01164 #pragma warning( pop )
01165 #endif

Generated on Thu Sep 30 2010 01:27:28 for RakNet by  doxygen 1.7.1