diff options
author | rubidium <rubidium@openttd.org> | 2009-04-07 19:06:36 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-04-07 19:06:36 +0000 |
commit | 82f8badc074d24b4df618c16b23c3854c3369711 (patch) | |
tree | 0ef655d1bf99df921f95015642ba96de2eae500e /src/network | |
parent | fdddfd6269ec4f5c770c288f05927ab8b0a60142 (diff) | |
download | openttd-82f8badc074d24b4df618c16b23c3854c3369711.tar.xz |
(svn r15969) -Codechange: make the list of broadcast addresses virtually unlimited.
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/core/address.h | 4 | ||||
-rw-r--r-- | src/network/core/host.cpp | 72 | ||||
-rw-r--r-- | src/network/core/host.h | 2 | ||||
-rw-r--r-- | src/network/network.cpp | 4 | ||||
-rw-r--r-- | src/network/network_internal.h | 2 | ||||
-rw-r--r-- | src/network/network_type.h | 3 | ||||
-rw-r--r-- | src/network/network_udp.cpp | 8 |
7 files changed, 38 insertions, 57 deletions
diff --git a/src/network/core/address.h b/src/network/core/address.h index f2bc00f74..d19e7b376 100644 --- 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 diff --git a/src/network/core/host.cpp b/src/network/core/host.cpp index 88a662208..14fe4f303 100644 --- 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 @@ int seek_past_header(char **pos, const char *header) 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 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) 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 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) } 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 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) /* 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 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) 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 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) 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 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) } closesocket(sock); - - return index; } #endif /* all NetworkFindBroadcastIPsInternals */ @@ -198,19 +182,17 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) * 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()); } } diff --git a/src/network/core/host.h b/src/network/core/host.h index 81cb0ff51..67ce5caac 100644 --- 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 */ diff --git a/src/network/network.cpp b/src/network/network.cpp index 0e241f1a6..60c163c37 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -61,7 +61,7 @@ uint32 _frame_counter_server; // The frame_counter of the server, if in network- 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 @@ void NetworkStartUp() NetworkUDPInitialize(); NetworkInitialize(); DEBUG(net, 3, "[core] network online, multiplayer available"); - NetworkFindBroadcastIPs(_broadcast_list, MAX_INTERFACES); + NetworkFindBroadcastIPs(&_broadcast_list); } /** This shuts the network down */ diff --git a/src/network/network_internal.h b/src/network/network_internal.h index f411c3630..c813e316a 100644 --- a/src/network/network_internal.h +++ b/src/network/network_internal.h @@ -101,7 +101,7 @@ extern uint32 _frame_counter; 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; diff --git a/src/network/network_type.h b/src/network/network_type.h index f2208b8c7..7e27d398c 100644 --- a/src/network/network_type.h +++ b/src/network/network_type.h @@ -26,9 +26,6 @@ enum { */ 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, diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp index c1a775d46..44d0ea414 100644 --- a/src/network/network_udp.cpp +++ b/src/network/network_udp.cpp @@ -371,14 +371,12 @@ void NetworkUDPCloseAll() /* 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); } } |