Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/windows/ip_windows.cpp
9903 views
1
/**************************************************************************/
2
/* ip_windows.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(WINDOWS_ENABLED)
32
33
#include "ip_windows.h"
34
35
#define WIN32_LEAN_AND_MEAN
36
#include <windows.h>
37
#include <winsock2.h>
38
#include <ws2tcpip.h>
39
40
#include <iphlpapi.h>
41
42
#include <cstdio>
43
44
static IPAddress _sockaddr2ip(struct sockaddr *p_addr) {
45
IPAddress ip;
46
47
if (p_addr->sa_family == AF_INET) {
48
struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
49
ip.set_ipv4((uint8_t *)&(addr->sin_addr));
50
} else if (p_addr->sa_family == AF_INET6) {
51
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
52
ip.set_ipv6(addr6->sin6_addr.s6_addr);
53
}
54
55
return ip;
56
}
57
58
void IPWindows::_resolve_hostname(List<IPAddress> &r_addresses, const String &p_hostname, Type p_type) const {
59
struct addrinfo hints;
60
struct addrinfo *result = nullptr;
61
62
memset(&hints, 0, sizeof(struct addrinfo));
63
if (p_type == TYPE_IPV4) {
64
hints.ai_family = AF_INET;
65
} else if (p_type == TYPE_IPV6) {
66
hints.ai_family = AF_INET6;
67
hints.ai_flags = 0;
68
} else {
69
hints.ai_family = AF_UNSPEC;
70
hints.ai_flags = AI_ADDRCONFIG;
71
}
72
hints.ai_flags &= ~AI_NUMERICHOST;
73
74
int s = getaddrinfo(p_hostname.utf8().get_data(), nullptr, &hints, &result);
75
if (s != 0) {
76
print_verbose("getaddrinfo failed! Cannot resolve hostname.");
77
return;
78
}
79
80
if (result == nullptr || result->ai_addr == nullptr) {
81
print_verbose("Invalid response from getaddrinfo.");
82
if (result) {
83
freeaddrinfo(result);
84
}
85
return;
86
}
87
88
struct addrinfo *next = result;
89
90
do {
91
if (next->ai_addr == nullptr) {
92
next = next->ai_next;
93
continue;
94
}
95
IPAddress ip = _sockaddr2ip(next->ai_addr);
96
if (ip.is_valid() && !r_addresses.find(ip)) {
97
r_addresses.push_back(ip);
98
}
99
next = next->ai_next;
100
} while (next);
101
102
freeaddrinfo(result);
103
}
104
105
void IPWindows::get_local_interfaces(HashMap<String, Interface_Info> *r_interfaces) const {
106
ULONG buf_size = 1024;
107
IP_ADAPTER_ADDRESSES *addrs;
108
109
while (true) {
110
addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size);
111
int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
112
nullptr, addrs, &buf_size);
113
if (err == NO_ERROR) {
114
break;
115
}
116
memfree(addrs);
117
if (err == ERROR_BUFFER_OVERFLOW) {
118
continue; // Will go back and alloc the right size.
119
}
120
121
ERR_FAIL_MSG("Call to GetAdaptersAddresses failed with error " + itos(err) + ".");
122
}
123
124
IP_ADAPTER_ADDRESSES *adapter = addrs;
125
126
while (adapter != nullptr) {
127
Interface_Info info;
128
info.name = adapter->AdapterName;
129
info.name_friendly = adapter->FriendlyName;
130
info.index = String::num_uint64(adapter->IfIndex);
131
132
IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress;
133
while (address != nullptr) {
134
int family = address->Address.lpSockaddr->sa_family;
135
if (family != AF_INET && family != AF_INET6) {
136
continue;
137
}
138
info.ip_addresses.push_front(_sockaddr2ip(address->Address.lpSockaddr));
139
address = address->Next;
140
}
141
adapter = adapter->Next;
142
// Only add interface if it has at least one IP.
143
if (info.ip_addresses.size() > 0) {
144
r_interfaces->insert(info.name, info);
145
}
146
}
147
148
memfree(addrs);
149
}
150
151
void IPWindows::make_default() {
152
_create = _create_unix;
153
}
154
155
IP *IPWindows::_create_unix() {
156
return memnew(IPWindows);
157
}
158
159
IPWindows::IPWindows() {
160
}
161
162
#endif // WINDOWS_ENABLED
163
164