SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RakNetSocket2_Berkley_NativeClient.cpp
Go to the documentation of this file.
1 /*
2 * Original work: 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 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2016-2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
16 #include "slikenet/EmptyHeader.h"
17
18 #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS
19
20 #ifndef RAKNETSOCKET2_BERKLEY_NATIVE_CLIENT_CPP
21 #define RAKNETSOCKET2_BERKLEY_NATIVE_CLIENT_CPP
22
23 // Every platform except windows store 8 and native client supports Berkley sockets
24 #if !defined(WINDOWS_STORE_RT)
25
26 #include "slikenet/Itoa.h"
27 #include "slikenet/WSAStartupSingleton.h" // used for WSAStartupSingleton
28
29 // Shared on most platforms, but excluded from the listed
30
31 // #low - maybe change to char (&ip)[65] - then we can also use sizeof again
32 void DomainNameToIP_Berkley_IPV4And6( const char *domainName, char ip[65] )
33 {
34 #if RAKNET_SUPPORT_IPV6==1
35 struct addrinfo hints, *res, *p;
36 int status;
37 memset(&hints, 0, sizeof hints);
38 hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
39 hints.ai_socktype = SOCK_DGRAM;
40
41 if ((status = getaddrinfo(domainName, NULL, &hints, &res)) != 0) {
42 // #high - review/update callers - some unnecessarily initialize ip unnecessarily
43 ip[0] = '\0';
44 return;
45 }
46
47 p=res;
48 // for(p = res;p != NULL; p = p->ai_next) {
49 void *addr;
50 // char *ipver;
51
52 // get the pointer to the address itself,
53 // different fields in IPv4 and IPv6:
54 if (p->ai_family == AF_INET)
55 {
56 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
57 addr = &(ipv4->sin_addr);
58 inet_ntop(AF_INET, &ipv4->sin_addr, ip, 65);
59 }
60 else
61 {
62 // TODO - test
63 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
64 addr = &(ipv6->sin6_addr);
65 // inet_ntop function does not exist on windows
66 // http://www.mail-archive.com/users@ipv6.org/msg02107.html
67 getnameinfo((struct sockaddr *)ipv6, sizeof(struct sockaddr_in6), ip, 1, NULL, 0, NI_NUMERICHOST);
68 }
69 freeaddrinfo(res); // free the linked list
70 // }
71 #else
72 (void) domainName;
73 (void) ip;
74 #endif // #if RAKNET_SUPPORT_IPV6==1
75 }
76
77
78 void DomainNameToIP_Berkley_IPV4( const char *domainName, char ip[65] )
79 {
80 // Use inet_addr instead? What is the difference?
81 struct addrinfo *addressinfo = NULL;
82 // needed for getaddrinfo
83 WSAStartupSingleton::AddRef();
84 int error = getaddrinfo(domainName, NULL, NULL, &addressinfo);
85 WSAStartupSingleton::Deref();
86
87 if ( error != 0 || addressinfo == 0 )
88 {
89 //cerr << "Yow! Bad host lookup." << endl;
90 // #high - review/update callers - some unnecessarily initialize ip unnecessarily
91 ip[0] = '\0';
92 return;
93 }
94
95 // get the (first) IPv4 address
96 while (addressinfo != NULL) {
97 if (addressinfo->ai_family == AF_INET) {
98 break; // found an IPv4 address
99 }
100 addressinfo = addressinfo->ai_next;
101 }
102
103 if (addressinfo == NULL) {
104 return;
105 }
106
107 struct sockaddr_in *sockaddr_ipv4 = (struct sockaddr_in *) addressinfo->ai_addr;
108
109 inet_ntop(AF_INET, &sockaddr_ipv4->sin_addr, ip, 65);
110 }
111
112
113
114 void DomainNameToIP_Berkley( const char *domainName, char ip[65] )
115 {
116 #if RAKNET_SUPPORT_IPV6==1
117 return DomainNameToIP_Berkley_IPV4And6(domainName, ip);
118 #else
119 return DomainNameToIP_Berkley_IPV4(domainName, ip);
120 #endif
121 }
122
123
124
125
126 #endif // !defined(WINDOWS_STORE_RT) && !defined(__native_client__)
127
128 #endif // file header
129
130 #endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS