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