RakNetSocket2_Windows_Linux.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "EmptyHeader.h"
  11. #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS
  12. #ifndef RAKNETSOCKET2_WINDOWS_LINUX_CPP
  13. #define RAKNETSOCKET2_WINDOWS_LINUX_CPP
  14. #if !defined(WINDOWS_STORE_RT) && !defined(__native_client__)
  15. #if RAKNET_SUPPORT_IPV6==1
  16. void PrepareAddrInfoHints2(addrinfo *hints)
  17. {
  18. memset(hints, 0, sizeof (addrinfo)); // make sure the struct is empty
  19. hints->ai_socktype = SOCK_DGRAM; // UDP sockets
  20. hints->ai_flags = AI_PASSIVE; // fill in my IP for me
  21. }
  22. void GetMyIP_Windows_Linux_IPV4And6( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
  23. {
  24. int idx=0;
  25. char ac[ 80 ];
  26. int err = gethostname( ac, sizeof( ac ) );
  27. RakAssert(err != -1);
  28. struct addrinfo hints;
  29. struct addrinfo *servinfo=0, *aip; // will point to the results
  30. PrepareAddrInfoHints2(&hints);
  31. getaddrinfo(ac, "", &hints, &servinfo);
  32. for (idx=0, aip = servinfo; aip != NULL && idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; aip = aip->ai_next, idx++)
  33. {
  34. if (aip->ai_family == AF_INET)
  35. {
  36. struct sockaddr_in *ipv4 = (struct sockaddr_in *)aip->ai_addr;
  37. memcpy(&addresses[idx].address.addr4,ipv4,sizeof(sockaddr_in));
  38. }
  39. else
  40. {
  41. struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)aip->ai_addr;
  42. memcpy(&addresses[idx].address.addr4,ipv6,sizeof(sockaddr_in6));
  43. }
  44. }
  45. freeaddrinfo(servinfo); // free the linked-list
  46. while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS)
  47. {
  48. addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS;
  49. idx++;
  50. }
  51. }
  52. #else
  53. #if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(__WIN32__)
  54. #include <netdb.h>
  55. #endif
  56. void GetMyIP_Windows_Linux_IPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
  57. {
  58. int idx=0;
  59. char ac[ 80 ];
  60. int err = gethostname( ac, sizeof( ac ) );
  61. (void) err;
  62. RakAssert(err != -1);
  63. struct hostent *phe = gethostbyname( ac );
  64. if ( phe == 0 )
  65. {
  66. RakAssert(phe!=0);
  67. return ;
  68. }
  69. for ( idx = 0; idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; ++idx )
  70. {
  71. if (phe->h_addr_list[ idx ] == 0)
  72. break;
  73. memcpy(&addresses[idx].address.addr4.sin_addr,phe->h_addr_list[ idx ],sizeof(struct in_addr));
  74. }
  75. while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS)
  76. {
  77. addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS;
  78. idx++;
  79. }
  80. }
  81. #endif // RAKNET_SUPPORT_IPV6==1
  82. void GetMyIP_Windows_Linux( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
  83. {
  84. #if RAKNET_SUPPORT_IPV6==1
  85. GetMyIP_Windows_Linux_IPV4And6(addresses);
  86. #else
  87. GetMyIP_Windows_Linux_IPV4(addresses);
  88. #endif
  89. }
  90. #endif // Windows and Linux
  91. #endif // file header
  92. #endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS
粤ICP备19079148号