RakNetSocket2_Berkley_NativeClient.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_BERKLEY_NATIVE_CLIENT_CPP
  13. #define RAKNETSOCKET2_BERKLEY_NATIVE_CLIENT_CPP
  14. // Every platform except windows store 8 and native client supports Berkley sockets
  15. #if !defined(WINDOWS_STORE_RT)
  16. #include "Itoa.h"
  17. // Shared on most platforms, but excluded from the listed
  18. void DomainNameToIP_Berkley_IPV4And6( const char *domainName, char ip[65] )
  19. {
  20. #if RAKNET_SUPPORT_IPV6==1
  21. struct addrinfo hints, *res, *p;
  22. int status;
  23. memset(&hints, 0, sizeof hints);
  24. hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
  25. hints.ai_socktype = SOCK_DGRAM;
  26. if ((status = getaddrinfo(domainName, NULL, &hints, &res)) != 0) {
  27. memset(ip, 0, sizeof(ip));
  28. return;
  29. }
  30. p=res;
  31. // for(p = res;p != NULL; p = p->ai_next) {
  32. void *addr;
  33. // char *ipver;
  34. // get the pointer to the address itself,
  35. // different fields in IPv4 and IPv6:
  36. if (p->ai_family == AF_INET)
  37. {
  38. struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
  39. addr = &(ipv4->sin_addr);
  40. strcpy(ip, inet_ntoa( ipv4->sin_addr ));
  41. }
  42. else
  43. {
  44. // TODO - test
  45. struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
  46. addr = &(ipv6->sin6_addr);
  47. // inet_ntop function does not exist on windows
  48. // http://www.mail-archive.com/users@ipv6.org/msg02107.html
  49. getnameinfo((struct sockaddr *)ipv6, sizeof(struct sockaddr_in6), ip, 1, NULL, 0, NI_NUMERICHOST);
  50. }
  51. freeaddrinfo(res); // free the linked list
  52. // }
  53. #else
  54. (void) domainName;
  55. (void) ip;
  56. #endif // #if RAKNET_SUPPORT_IPV6==1
  57. }
  58. void DomainNameToIP_Berkley_IPV4( const char *domainName, char ip[65] )
  59. {
  60. static struct in_addr addr;
  61. memset(&addr,0,sizeof(in_addr));
  62. // Use inet_addr instead? What is the difference?
  63. struct hostent * phe = gethostbyname( domainName );
  64. if ( phe == 0 || phe->h_addr_list[ 0 ] == 0 )
  65. {
  66. //cerr << "Yow! Bad host lookup." << endl;
  67. memset(ip,0,65*sizeof(char));
  68. return;
  69. }
  70. if (phe->h_addr_list[ 0 ]==0)
  71. {
  72. memset(ip,0,65*sizeof(char));
  73. return;
  74. }
  75. memcpy( &addr, phe->h_addr_list[ 0 ], sizeof( struct in_addr ) );
  76. strcpy(ip, inet_ntoa( addr ));
  77. }
  78. void DomainNameToIP_Berkley( const char *domainName, char ip[65] )
  79. {
  80. #if RAKNET_SUPPORT_IPV6==1
  81. return DomainNameToIP_Berkley_IPV4And6(domainName, ip);
  82. #else
  83. return DomainNameToIP_Berkley_IPV4(domainName, ip);
  84. #endif
  85. }
  86. #endif // !defined(WINDOWS_STORE_RT) && !defined(__native_client__)
  87. #endif // file header
  88. #endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS
粤ICP备19079148号