1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
8 /** @file host.cpp Functions related to getting host specific data (IPs). */
9 
10 #include "../../stdafx.h"
11 #include "../../debug.h"
12 #include "address.h"
13 
14 #include "../../safeguards.h"
15 
16 /**
17  * Internal implementation for finding the broadcast IPs.
18  * This function is implemented multiple times for multiple targets.
19  * @param broadcast the list of broadcasts to write into.
20  */
21 static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast);
22 
23 #if defined(HAVE_GETIFADDRS)
NetworkFindBroadcastIPsInternal(NetworkAddressList * broadcast)24 static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // GETIFADDRS implementation
25 {
26 	struct ifaddrs *ifap, *ifa;
27 
28 	if (getifaddrs(&ifap) != 0) return;
29 
30 	for (ifa = ifap; ifa != nullptr; ifa = ifa->ifa_next) {
31 		if (!(ifa->ifa_flags & IFF_BROADCAST)) continue;
32 		if (ifa->ifa_broadaddr == nullptr) continue;
33 		if (ifa->ifa_broadaddr->sa_family != AF_INET) continue;
34 
35 		NetworkAddress addr(ifa->ifa_broadaddr, sizeof(sockaddr));
36 		if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
37 	}
38 	freeifaddrs(ifap);
39 }
40 
41 #elif defined(_WIN32)
NetworkFindBroadcastIPsInternal(NetworkAddressList * broadcast)42 static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // Win32 implementation
43 {
44 	SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
45 	if (sock == INVALID_SOCKET) return;
46 
47 	DWORD len = 0;
48 	int num = 2;
49 	INTERFACE_INFO *ifo = CallocT<INTERFACE_INFO>(num);
50 
51 	for (;;) {
52 		if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, nullptr, 0, ifo, num * sizeof(*ifo), &len, nullptr, nullptr) == 0) break;
53 		free(ifo);
54 		if (WSAGetLastError() != WSAEFAULT) {
55 			closesocket(sock);
56 			return;
57 		}
58 		num *= 2;
59 		ifo = CallocT<INTERFACE_INFO>(num);
60 	}
61 
62 	for (uint j = 0; j < len / sizeof(*ifo); j++) {
63 		if (ifo[j].iiFlags & IFF_LOOPBACK) continue;
64 		if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue;
65 
66 		sockaddr_storage address;
67 		memset(&address, 0, sizeof(address));
68 		/* iiBroadcast is unusable, because it always seems to be set to 255.255.255.255. */
69 		memcpy(&address, &ifo[j].iiAddress.Address, sizeof(sockaddr));
70 		((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
71 		NetworkAddress addr(address, sizeof(sockaddr));
72 		if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
73 	}
74 
75 	free(ifo);
76 	closesocket(sock);
77 }
78 
79 #else /* not HAVE_GETIFADDRS */
80 
81 #include "../../string_func.h"
82 
NetworkFindBroadcastIPsInternal(NetworkAddressList * broadcast)83 static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // !GETIFADDRS implementation
84 {
85 	SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
86 	if (sock == INVALID_SOCKET) return;
87 
88 	char buf[4 * 1024]; // Arbitrary buffer size
89 	struct ifconf ifconf;
90 
91 	ifconf.ifc_len = sizeof(buf);
92 	ifconf.ifc_buf = buf;
93 	if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
94 		closesocket(sock);
95 		return;
96 	}
97 
98 	const char *buf_end = buf + ifconf.ifc_len;
99 	for (const char *p = buf; p < buf_end;) {
100 		const struct ifreq *req = (const struct ifreq*)p;
101 
102 		if (req->ifr_addr.sa_family == AF_INET) {
103 			struct ifreq r;
104 
105 			strecpy(r.ifr_name, req->ifr_name, lastof(r.ifr_name));
106 			if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
107 					(r.ifr_flags & IFF_BROADCAST) &&
108 					ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
109 				NetworkAddress addr(&r.ifr_broadaddr, sizeof(sockaddr));
110 				if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
111 			}
112 		}
113 
114 		p += sizeof(struct ifreq);
115 #if defined(AF_LINK) && !defined(SUNOS)
116 		p += req->ifr_addr.sa_len - sizeof(struct sockaddr);
117 #endif
118 	}
119 
120 	closesocket(sock);
121 }
122 #endif /* all NetworkFindBroadcastIPsInternals */
123 
124 /**
125  * Find the IPv4 broadcast addresses; IPv6 uses a completely different
126  * strategy for broadcasting.
127  * @param broadcast the list of broadcasts to write into.
128  */
NetworkFindBroadcastIPs(NetworkAddressList * broadcast)129 void NetworkFindBroadcastIPs(NetworkAddressList *broadcast)
130 {
131 	NetworkFindBroadcastIPsInternal(broadcast);
132 
133 	/* Now display to the debug all the detected ips */
134 	Debug(net, 3, "Detected broadcast addresses:");
135 	int i = 0;
136 	for (NetworkAddress &addr : *broadcast) {
137 		addr.SetPort(NETWORK_DEFAULT_PORT);
138 		Debug(net, 3, "  {}) {}", i++, addr.GetHostname());
139 	}
140 }
141