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_Windows_Linux.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_WINDOWS_LINUX_CPP
21 #define RAKNETSOCKET2_WINDOWS_LINUX_CPP
22
23 #if !defined(WINDOWS_STORE_RT) && !defined(__native_client__)
24
25 #if RAKNET_SUPPORT_IPV6==1
26
27 void PrepareAddrInfoHints2(addrinfo *hints)
28 {
29 memset(hints, 0, sizeof (addrinfo)); // make sure the struct is empty
30 hints->ai_socktype = SOCK_DGRAM; // UDP sockets
31 hints->ai_flags = AI_PASSIVE; // fill in my IP for me
32 }
33
34 void GetMyIP_Windows_Linux_IPV4And6( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
35 {
36 int idx=0;
37 char ac[ 80 ];
38 int err = gethostname( ac, sizeof( ac ) );
39 RakAssert(err != -1);
40
41 struct addrinfo hints;
42 struct addrinfo *servinfo=0, *aip; // will point to the results
43 PrepareAddrInfoHints2(&hints);
44 getaddrinfo(ac, "", &hints, &servinfo);
45
46 for (idx=0, aip = servinfo; aip != NULL && idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS; aip = aip->ai_next, idx++)
47 {
48 if (aip->ai_family == AF_INET)
49 {
50 struct sockaddr_in *ipv4 = (struct sockaddr_in *)aip->ai_addr;
51 memcpy(&addresses[idx].address.addr4,ipv4,sizeof(sockaddr_in));
52 }
53 else
54 {
55 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)aip->ai_addr;
56 memcpy(&addresses[idx].address.addr4,ipv6,sizeof(sockaddr_in6));
57 }
58
59 }
60
61 freeaddrinfo(servinfo); // free the linked-list
62
63 while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS)
64 {
65 addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS;
66 idx++;
67 }
68 }
69
70 #else
71
72 #if (defined(__GNUC__) || defined(__GCCXML__)) && !defined(__WIN32__)
73 #include <netdb.h>
74 #endif
75 void GetMyIP_Windows_Linux_IPV4( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
76 {
77
78
79
80 int idx=0;
81 char ac[ 80 ];
82 int err = gethostname( ac, sizeof( ac ) );
83 (void) err;
84 RakAssert(err != -1);
85
86 struct addrinfo *curAddress = NULL;
87 err = getaddrinfo(ac, NULL, NULL, &curAddress);
88
89 if ( err != 0 || curAddress == 0 )
90 {
91 RakAssert(false);
92 return ;
93 }
94 while (curAddress != NULL && idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS)
95 {
96 if (curAddress->ai_family == AF_INET) {
97 addresses[idx].address.addr4 = *((struct sockaddr_in *)curAddress->ai_addr);
98 ++idx;
99 }
100 curAddress = curAddress->ai_next;
101 }
102
103 while (idx < MAXIMUM_NUMBER_OF_INTERNAL_IDS)
104 {
105 addresses[idx]=UNASSIGNED_SYSTEM_ADDRESS;
106 idx++;
107 }
108
109 }
110
111 #endif // RAKNET_SUPPORT_IPV6==1
112
113
114 void GetMyIP_Windows_Linux( SystemAddress addresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS] )
115 {
116 #if RAKNET_SUPPORT_IPV6==1
117 GetMyIP_Windows_Linux_IPV4And6(addresses);
118 #else
119 GetMyIP_Windows_Linux_IPV4(addresses);
120 #endif
121 }
122
123
124 #endif // Windows and Linux
125
126 #endif // file header
127
128 #endif // #ifdef RAKNET_SOCKET_2_INLINE_FUNCTIONS