Debian Codegraph @

drivers/unix/ip_unix.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* ip_unix.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #if defined(UNIX_ENABLED) && !defined(UNIX_SOCKET_UNAVAILABLE)
32
33 #include "ip_unix.h"
34
35 #include <netdb.h>
36
37 #ifdef ANDROID_ENABLED
38 // We could drop this file once we up our API level to 24,
39 // where the NDK's ifaddrs.h supports to needed getifaddrs.
40 #include "thirdparty/misc/ifaddrs-android.h"
41 #else
42 #ifdef __FreeBSD__
43 #include <sys/types.h>
44 #endif
45 #include <ifaddrs.h>
46 #endif
47
48 #include <arpa/inet.h>
49 #include <sys/socket.h>
50
51 #ifdef __FreeBSD__
52 #include <netinet/in.h>
53 #endif
54
55 #include <net/if.h> // Order is important on OpenBSD, leave as last.
56
57 static IPAddress _sockaddr2ip(struct sockaddr *p_addr) {
58 IPAddress ip;
59
60 if (p_addr->sa_family == AF_INET) {
61 struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
62 ip.set_ipv4((uint8_t *)&(addr->sin_addr));
63 } else if (p_addr->sa_family == AF_INET6) {
64 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
65 ip.set_ipv6(addr6->sin6_addr.s6_addr);
66 }
67
68 return ip;
69 }
70
71 void IPUnix::_resolve_hostname(List<IPAddress> &r_addresses, const String &p_hostname, Type p_type) const {
72 struct addrinfo hints;
73 struct addrinfo *result = nullptr;
74
75 memset(&hints, 0, sizeof(struct addrinfo));
76 if (p_type == TYPE_IPV4) {
77 hints.ai_family = AF_INET;
78 } else if (p_type == TYPE_IPV6) {
79 hints.ai_family = AF_INET6;
80 hints.ai_flags = 0;
81 } else {
82 hints.ai_family = AF_UNSPEC;
83 hints.ai_flags = AI_ADDRCONFIG;
84 }
85 hints.ai_flags &= ~AI_NUMERICHOST;
86
87 int s = getaddrinfo(p_hostname.utf8().get_data(), nullptr, &hints, &result);
88 if (s != 0) {
89 print_verbose("getaddrinfo failed! Cannot resolve hostname.");
90 return;
91 }
92
93 if (result == nullptr || result->ai_addr == nullptr) {
94 print_verbose("Invalid response from getaddrinfo.");
95 if (result) {
96 freeaddrinfo(result);
97 }
98 return;
99 }
100
101 struct addrinfo *next = result;
102
103 do {
104 if (next->ai_addr == nullptr) {
105 next = next->ai_next;
106 continue;
107 }
108 IPAddress ip = _sockaddr2ip(next->ai_addr);
109 if (ip.is_valid() && !r_addresses.find(ip)) {
110 r_addresses.push_back(ip);
111 }
112 next = next->ai_next;
113 } while (next);
114
115 freeaddrinfo(result);
116 }
117
118 void IPUnix::get_local_interfaces(HashMap<String, Interface_Info> *r_interfaces) const {
119 struct ifaddrs *ifAddrStruct = nullptr;
120 struct ifaddrs *ifa = nullptr;
121 int family;
122
123 getifaddrs(&ifAddrStruct);
124
125 for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
126 if (!ifa->ifa_addr) {
127 continue;
128 }
129
130 family = ifa->ifa_addr->sa_family;
131
132 if (family != AF_INET && family != AF_INET6) {
133 continue;
134 }
135
136 HashMap<String, Interface_Info>::Iterator E = r_interfaces->find(ifa->ifa_name);
137 if (!E) {
138 Interface_Info info;
139 info.name = ifa->ifa_name;
140 info.name_friendly = ifa->ifa_name;
141 info.index = String::num_uint64(if_nametoindex(ifa->ifa_name));
142 E = r_interfaces->insert(ifa->ifa_name, info);
143 ERR_CONTINUE(!E);
144 }
145
146 Interface_Info &info = E->value;
147 info.ip_addresses.push_front(_sockaddr2ip(ifa->ifa_addr));
148 }
149
150 if (ifAddrStruct != nullptr) {
151 freeifaddrs(ifAddrStruct);
152 }
153 }
154
155 void IPUnix::make_default() {
156 _create = _create_unix;
157 }
158
159 IP *IPUnix::_create_unix() {
160 return memnew(IPUnix);
161 }
162
163 IPUnix::IPUnix() {
164 }
165
166 #endif // UNIX_ENABLED