Mercurial > hg > openttd
changeset 11596:6904b4e22518 draft
(svn r15969) -Codechange: make the list of broadcast addresses virtually unlimited.
author | rubidium <rubidium@openttd.org> |
---|---|
date | Tue, 07 Apr 2009 19:06:36 +0000 |
parents | 3256656698f2 |
children | 0afd6a87b321 |
files | src/network/core/address.h src/network/core/host.cpp src/network/core/host.h src/network/network.cpp src/network/network_internal.h src/network/network_type.h src/network/network_udp.cpp |
diffstat | 7 files changed, 38 insertions(+), 57 deletions(-) [+] |
line wrap: on
line diff
--- a/src/network/core/address.h +++ b/src/network/core/address.h @@ -10,6 +10,10 @@ #include "os_abstraction.h" #include "config.h" #include "../../string_func.h" +#include "../../core/smallvec_type.hpp" + +class NetworkAddress; +typedef SmallVector<NetworkAddress, 4> NetworkAddressList; /** * Wrapper for (un)resolved network addresses; there's no reason to transform
--- a/src/network/core/host.cpp +++ b/src/network/core/host.cpp @@ -15,14 +15,12 @@ * Internal implementation for finding the broadcast IPs. * This function is implemented multiple times for multiple targets. * @param broadcast the list of broadcasts to write into. - * @param limit the maximum number of items to add. */ -static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit); +static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast); #if defined(PSP) -static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // PSP implementation +static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // PSP implementation { - return 0; } #elif defined(BEOS_NET_SERVER) /* doesn't have neither getifaddrs or net/if.h */ @@ -39,27 +37,25 @@ return B_OK; } -static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // BEOS implementation +static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // BEOS implementation { int sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { DEBUG(net, 0, "[core] error creating socket"); - return 0; + return; } char *output_pointer = NULL; int output_length = _netstat(sock, &output_pointer, 1); if (output_length < 0) { DEBUG(net, 0, "[core] error running _netstat"); - return 0; + return; } - int index; char **output = &output_pointer; if (seek_past_header(output, "IP Interfaces:") == B_OK) { - while (index != limit) { - + for (;;) { uint32 n, fields, read; uint8 i1, i2, i3, i4, j1, j2, j3, j4; struct in_addr inaddr; @@ -80,7 +76,7 @@ sockaddr_storage address; memset(&address, 0, sizeof(address)); ((sockaddr_in*)&address)->sin_addr.s_addr = htonl(ip | ~netmask); - broadcast[index] = NetworkAddress(address, sizeof(sockaddr)); + *broadcast->Append() = NetworkAddress(address, sizeof(sockaddr)); index++; } if (read < 0) { @@ -90,47 +86,40 @@ } closesocket(sock); } - - return index; } #elif defined(HAVE_GETIFADDRS) -static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // GETIFADDRS implementation +static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // GETIFADDRS implementation { struct ifaddrs *ifap, *ifa; - if (getifaddrs(&ifap) != 0) return 0; + if (getifaddrs(&ifap) != 0) return; - int index = 0; - for (ifa = ifap; ifa != NULL && index != limit; ifa = ifa->ifa_next) { + for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { if (!(ifa->ifa_flags & IFF_BROADCAST)) continue; if (ifa->ifa_broadaddr == NULL) continue; if (ifa->ifa_broadaddr->sa_family != AF_INET) continue; - broadcast[index] = NetworkAddress(ifa->ifa_broadaddr, sizeof(sockaddr)); - index++; + *broadcast->Append() = NetworkAddress(ifa->ifa_broadaddr, sizeof(sockaddr)); } freeifaddrs(ifap); - - return index; } #elif defined(WIN32) -static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // Win32 implementation +static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // Win32 implementation { SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock == INVALID_SOCKET) return 0; + if (sock == INVALID_SOCKET) return; DWORD len = 0; INTERFACE_INFO *ifo = AllocaM(INTERFACE_INFO, limit); memset(ifo, 0, limit * sizeof(*ifo)); if ((WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, &ifo[0], limit * sizeof(*ifo), &len, NULL, NULL)) != 0) { closesocket(sock); - return 0; + return; } - int index = 0; - for (uint j = 0; j < len / sizeof(*ifo) && index != limit; j++) { + for (uint j = 0; j < len / sizeof(*ifo); j++) { if (ifo[j].iiFlags & IFF_LOOPBACK) continue; if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue; @@ -139,22 +128,20 @@ /* iiBroadcast is unusable, because it always seems to be set to 255.255.255.255. */ memcpy(&address, &ifo[j].iiAddress.Address, sizeof(sockaddr)); ((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr; - broadcast[index] = NetworkAddress(address, sizeof(sockaddr)); - index++; + *broadcast->Append() = NetworkAddress(address, sizeof(sockaddr)); } closesocket(sock); - return index; } #else /* not HAVE_GETIFADDRS */ #include "../../string_func.h" -static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // !GETIFADDRS implementation +static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // !GETIFADDRS implementation { SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock == INVALID_SOCKET) return 0; + if (sock == INVALID_SOCKET) return; char buf[4 * 1024]; // Arbitrary buffer size struct ifconf ifconf; @@ -163,12 +150,11 @@ ifconf.ifc_buf = buf; if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) { closesocket(sock); - return 0; + return; } const char *buf_end = buf + ifconf.ifc_len; - int index = 0; - for (const char *p = buf; p < buf_end && index != limit;) { + for (const char *p = buf; p < buf_end;) { const struct ifreq *req = (const struct ifreq*)p; if (req->ifr_addr.sa_family == AF_INET) { @@ -178,7 +164,7 @@ if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 && r.ifr_flags & IFF_BROADCAST && ioctl(sock, SIOCGIFBRDADDR, &r) != -1) { - broadcast[index++] = NetworkAddress(&r.ifr_broadaddr, sizeof(sockaddr)); + *broadcast->Append() = NetworkAddress(&r.ifr_broadaddr, sizeof(sockaddr)); } } @@ -189,8 +175,6 @@ } closesocket(sock); - - return index; } #endif /* all NetworkFindBroadcastIPsInternals */ @@ -198,19 +182,17 @@ * Find the IPv4 broadcast addresses; IPv6 uses a completely different * strategy for broadcasting. * @param broadcast the list of broadcasts to write into. - * @param limit the maximum number of items to add. */ -void NetworkFindBroadcastIPs(NetworkAddress *broadcast, int limit) +void NetworkFindBroadcastIPs(NetworkAddressList *broadcast) { - int count = NetworkFindBroadcastIPsInternal(broadcast, limit); - - /* Make sure the list is terminated. */ - broadcast[count] = NetworkAddress(""); + NetworkFindBroadcastIPsInternal(broadcast); /* Now display to the debug all the detected ips */ DEBUG(net, 3, "Detected broadcast addresses:"); - for (int i = 0; !StrEmpty(broadcast[i].GetHostname()); i++) { - DEBUG(net, 3, "%d) %s", i, broadcast[i].GetHostname()); + int i = 0; + for (NetworkAddress *addr = broadcast->Begin(); addr != broadcast->End(); addr++) { + addr->SetPort(NETWORK_DEFAULT_PORT); + DEBUG(net, 3, "%d) %s", i++, addr->GetHostname()); } }
--- a/src/network/core/host.h +++ b/src/network/core/host.h @@ -7,6 +7,6 @@ #ifndef NETWORK_CORE_HOST_H #define NETWORK_CORE_HOST_H -void NetworkFindBroadcastIPs(NetworkAddress *broadcast, int limit); +void NetworkFindBroadcastIPs(NetworkAddressList *broadcast); #endif /* NETWORK_CORE_HOST_H */
--- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -61,7 +61,7 @@ uint32 _frame_counter_max; // To where we may go with our clients uint32 _frame_counter; uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients. -NetworkAddress _broadcast_list[MAX_INTERFACES + 1]; +NetworkAddressList _broadcast_list; uint32 _sync_seed_1, _sync_seed_2; uint32 _sync_frame; bool _network_first_time; @@ -1073,7 +1073,7 @@ NetworkUDPInitialize(); NetworkInitialize(); DEBUG(net, 3, "[core] network online, multiplayer available"); - NetworkFindBroadcastIPs(_broadcast_list, MAX_INTERFACES); + NetworkFindBroadcastIPs(&_broadcast_list); } /** This shuts the network down */
--- a/src/network/network_internal.h +++ b/src/network/network_internal.h @@ -101,7 +101,7 @@ extern uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients. /* networking settings */ -extern NetworkAddress _broadcast_list[MAX_INTERFACES + 1]; +extern NetworkAddressList _broadcast_list; extern uint32 _sync_seed_1, _sync_seed_2; extern uint32 _sync_frame;
--- a/src/network/network_type.h +++ b/src/network/network_type.h @@ -26,9 +26,6 @@ */ MAX_CLIENT_SLOTS = 256, - /** Maximum number of internet interfaces supported. */ - MAX_INTERFACES = 9, - /** How many vehicle/station types we put over the network */ NETWORK_VEHICLE_TYPES = 5, NETWORK_STATION_TYPES = 5,
--- a/src/network/network_udp.cpp +++ b/src/network/network_udp.cpp @@ -371,14 +371,12 @@ /* Broadcast to all ips */ static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket) { - uint i; - - for (i = 0; !StrEmpty(_broadcast_list[i].GetHostname()); i++) { + for (NetworkAddress *addr = _broadcast_list.Begin(); addr != _broadcast_list.End(); addr++) { Packet p(PACKET_UDP_CLIENT_FIND_SERVER); - DEBUG(net, 4, "[udp] broadcasting to %s", _broadcast_list[i].GetHostname()); + DEBUG(net, 4, "[udp] broadcasting to %s", addr->GetHostname()); - socket->SendPacket(&p, &_broadcast_list[i]); + socket->SendPacket(&p, addr); } }