OpenTTD
host.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "../../stdafx.h"
13 #include "../../debug.h"
14 #include "address.h"
15 
16 #include "../../safeguards.h"
17 
24 
25 #if defined(__HAIKU__) /* doesn't have neither getifaddrs or net/if.h */
26 /* Based on Andrew Bachmann's netstat+.c. Big thanks to him! */
27 extern "C" int _netstat(int fd, char **output, int verbose);
28 
29 int seek_past_header(char **pos, const char *header)
30 {
31  char *new_pos = strstr(*pos, header);
32  if (new_pos == 0) {
33  return B_ERROR;
34  }
35  *pos += strlen(header) + new_pos - *pos + 1;
36  return B_OK;
37 }
38 
39 static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // BEOS implementation
40 {
41  int sock = socket(AF_INET, SOCK_DGRAM, 0);
42 
43  if (sock < 0) {
44  DEBUG(net, 0, "[core] error creating socket");
45  return;
46  }
47 
48  char *output_pointer = nullptr;
49  int output_length = _netstat(sock, &output_pointer, 1);
50  if (output_length < 0) {
51  DEBUG(net, 0, "[core] error running _netstat");
52  return;
53  }
54 
55  char **output = &output_pointer;
56  if (seek_past_header(output, "IP Interfaces:") == B_OK) {
57  for (;;) {
58  uint32 n;
59  int fields, read;
60  uint8 i1, i2, i3, i4, j1, j2, j3, j4;
61  uint32 ip;
62  uint32 netmask;
63 
64  fields = sscanf(*output, "%u: %hhu.%hhu.%hhu.%hhu, netmask %hhu.%hhu.%hhu.%hhu%n",
65  &n, &i1, &i2, &i3, &i4, &j1, &j2, &j3, &j4, &read);
66  read += 1;
67  if (fields != 9) {
68  break;
69  }
70 
71  ip = (uint32)i1 << 24 | (uint32)i2 << 16 | (uint32)i3 << 8 | (uint32)i4;
72  netmask = (uint32)j1 << 24 | (uint32)j2 << 16 | (uint32)j3 << 8 | (uint32)j4;
73 
74  if (ip != INADDR_LOOPBACK && ip != INADDR_ANY) {
75  sockaddr_storage address;
76  memset(&address, 0, sizeof(address));
77  ((sockaddr_in*)&address)->sin_addr.s_addr = htonl(ip | ~netmask);
78  NetworkAddress addr(address, sizeof(sockaddr));
79  if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
80  }
81  if (read < 0) {
82  break;
83  }
84  *output += read;
85  }
86  closesocket(sock);
87  }
88 }
89 
90 #elif defined(HAVE_GETIFADDRS)
91 static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // GETIFADDRS implementation
92 {
93  struct ifaddrs *ifap, *ifa;
94 
95  if (getifaddrs(&ifap) != 0) return;
96 
97  for (ifa = ifap; ifa != nullptr; ifa = ifa->ifa_next) {
98  if (!(ifa->ifa_flags & IFF_BROADCAST)) continue;
99  if (ifa->ifa_broadaddr == nullptr) continue;
100  if (ifa->ifa_broadaddr->sa_family != AF_INET) continue;
101 
102  NetworkAddress addr(ifa->ifa_broadaddr, sizeof(sockaddr));
103  if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
104  }
105  freeifaddrs(ifap);
106 }
107 
108 #elif defined(_WIN32)
109 static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // Win32 implementation
110 {
111  SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
112  if (sock == INVALID_SOCKET) return;
113 
114  DWORD len = 0;
115  int num = 2;
116  INTERFACE_INFO *ifo = CallocT<INTERFACE_INFO>(num);
117 
118  for (;;) {
119  if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, nullptr, 0, ifo, num * sizeof(*ifo), &len, nullptr, nullptr) == 0) break;
120  free(ifo);
121  if (WSAGetLastError() != WSAEFAULT) {
122  closesocket(sock);
123  return;
124  }
125  num *= 2;
126  ifo = CallocT<INTERFACE_INFO>(num);
127  }
128 
129  for (uint j = 0; j < len / sizeof(*ifo); j++) {
130  if (ifo[j].iiFlags & IFF_LOOPBACK) continue;
131  if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue;
132 
133  sockaddr_storage address;
134  memset(&address, 0, sizeof(address));
135  /* iiBroadcast is unusable, because it always seems to be set to 255.255.255.255. */
136  memcpy(&address, &ifo[j].iiAddress.Address, sizeof(sockaddr));
137  ((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
138  NetworkAddress addr(address, sizeof(sockaddr));
139  if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
140  }
141 
142  free(ifo);
143  closesocket(sock);
144 }
145 
146 #else /* not HAVE_GETIFADDRS */
147 
148 #include "../../string_func.h"
149 
150 static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // !GETIFADDRS implementation
151 {
152  SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
153  if (sock == INVALID_SOCKET) return;
154 
155  char buf[4 * 1024]; // Arbitrary buffer size
156  struct ifconf ifconf;
157 
158  ifconf.ifc_len = sizeof(buf);
159  ifconf.ifc_buf = buf;
160  if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
161  closesocket(sock);
162  return;
163  }
164 
165  const char *buf_end = buf + ifconf.ifc_len;
166  for (const char *p = buf; p < buf_end;) {
167  const struct ifreq *req = (const struct ifreq*)p;
168 
169  if (req->ifr_addr.sa_family == AF_INET) {
170  struct ifreq r;
171 
172  strecpy(r.ifr_name, req->ifr_name, lastof(r.ifr_name));
173  if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
174  (r.ifr_flags & IFF_BROADCAST) &&
175  ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
176  NetworkAddress addr(&r.ifr_broadaddr, sizeof(sockaddr));
177  if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
178  }
179  }
180 
181  p += sizeof(struct ifreq);
182 #if defined(AF_LINK) && !defined(SUNOS)
183  p += req->ifr_addr.sa_len - sizeof(struct sockaddr);
184 #endif
185  }
186 
187  closesocket(sock);
188 }
189 #endif /* all NetworkFindBroadcastIPsInternals */
190 
197 {
199 
200  /* Now display to the debug all the detected ips */
201  DEBUG(net, 3, "Detected broadcast addresses:");
202  int i = 0;
203  for (NetworkAddress &addr : *broadcast) {
204  addr.SetPort(NETWORK_DEFAULT_PORT);
205  DEBUG(net, 3, "%d) %s", i++, addr.GetHostname());
206  }
207 }
Wrapper for (un)resolved network addresses; there&#39;s no reason to transform a numeric IP to a string a...
Definition: address.h:29
void NetworkFindBroadcastIPs(NetworkAddressList *broadcast)
Find the IPv4 broadcast addresses; IPv6 uses a completely different strategy for broadcasting.
Definition: host.cpp:196
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:20
Wrapper for network addresses.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
static const uint16 NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition: config.h:31
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:131
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast)
Internal implementation for finding the broadcast IPs.
Definition: host.cpp:150