Mercurial > hg > openttd
changeset 11560:34af2eeea95b draft
(svn r15926) -Codechange: make the broadcast IP list less AF dependent.
author | rubidium <rubidium@openttd.org> |
---|---|
date | Fri, 03 Apr 2009 11:02:41 +0000 |
parents | 61f34aed5412 |
children | 0fcdcc728f43 |
files | src/network/core/address.h src/network/core/host.cpp src/network/core/host.h src/network/network.cpp src/network/network_gamelist.cpp src/network/network_internal.h src/network/network_udp.cpp |
diffstat | 7 files changed, 43 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/src/network/core/address.h +++ b/src/network/core/address.h @@ -64,6 +64,18 @@ } /** + * Create a network address based on a resolved IP and port + * @param address the IP address with port + */ + NetworkAddress(sockaddr *address, size_t address_length) : + hostname(NULL), + address_length(address_length) + { + memset(&this->address, 0, sizeof(this->address)); + memcpy(&this->address, address, address_length); + } + + /** * Create a network address based on a unresolved host and port * @param ip the unresolved hostname * @param port the port
--- a/src/network/core/host.cpp +++ b/src/network/core/host.cpp @@ -7,7 +7,9 @@ #include "../../stdafx.h" #include "../../debug.h" #include "os_abstraction.h" +#include "address.h" #include "../../core/alloc_func.hpp" +#include "../../string_func.h" /** * Internal implementation for finding the broadcast IPs. @@ -15,10 +17,10 @@ * @param broadcast the list of broadcasts to write into. * @param limit the maximum number of items to add. */ -static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit); +static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit); #if defined(PSP) -static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // PSP implementation +static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // PSP implementation { return 0; } @@ -37,7 +39,7 @@ return B_OK; } -static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // BEOS implementation +static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // BEOS implementation { int sock = socket(AF_INET, SOCK_DGRAM, 0); @@ -75,8 +77,10 @@ netmask = (uint32)j1 << 24 | (uint32)j2 << 16 | (uint32)j3 << 8 | (uint32)j4; if (ip != INADDR_LOOPBACK && ip != INADDR_ANY) { - inaddr.s_addr = htonl(ip | ~netmask); - broadcast[index] = inaddr.s_addr; + sockaddr_storage address; + memset(&address, 0, sizeof(address)); + ((sockaddr_in*)&storage)->sin_addr.s_addr = htonl(ip | ~netmask); + broadcast[index] = NetworkAddress(address, sizeof(sockaddr)); index++; } if (read < 0) { @@ -91,7 +95,7 @@ } #elif defined(HAVE_GETIFADDRS) -static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // GETIFADDRS implementation +static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // GETIFADDRS implementation { struct ifaddrs *ifap, *ifa; @@ -103,7 +107,7 @@ if (ifa->ifa_broadaddr == NULL) continue; if (ifa->ifa_broadaddr->sa_family != AF_INET) continue; - broadcast[index] = ((struct sockaddr_in*)ifa->ifa_broadaddr)->sin_addr.s_addr; + broadcast[index] = NetworkAddress(ifa->ifa_broadaddr, sizeof(sockaddr)); index++; } freeifaddrs(ifap); @@ -112,7 +116,7 @@ } #elif defined(WIN32) -static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // Win32 implementation +static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // Win32 implementation { SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == INVALID_SOCKET) return 0; @@ -130,8 +134,11 @@ if (ifo[j].iiFlags & IFF_LOOPBACK) continue; if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue; + sockaddr_storage address; + memset(&address, 0, sizeof(address)); /* iiBroadcast is unusable, because it always seems to be set to 255.255.255.255. */ - broadcast[index++] = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr; + ((sockaddr_in*)&storage)->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)); } closesocket(sock); @@ -142,7 +149,7 @@ #include "../../string_func.h" -static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // !GETIFADDRS implementation +static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // !GETIFADDRS implementation { SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock == INVALID_SOCKET) return 0; @@ -169,7 +176,7 @@ if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 && r.ifr_flags & IFF_BROADCAST && ioctl(sock, SIOCGIFBRDADDR, &r) != -1) { - broadcast[index++] = ((struct sockaddr_in*)&r.ifr_broadaddr)->sin_addr.s_addr; + broadcast[index++] = NetworkAddress(&r.ifr_broadaddr, sizeof(sockaddr)); } } @@ -186,21 +193,22 @@ #endif /* all NetworkFindBroadcastIPsInternals */ /** - * Find the IPs to broadcast. + * 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(uint32 *broadcast, int limit) +void NetworkFindBroadcastIPs(NetworkAddress *broadcast, int limit) { int count = NetworkFindBroadcastIPsInternal(broadcast, limit); /* Make sure the list is terminated. */ - broadcast[count] = 0; + broadcast[count] = NetworkAddress(""); /* Now display to the debug all the detected ips */ DEBUG(net, 3, "Detected broadcast addresses:"); - for (int i = 0; broadcast[i] != 0; i++) { - DEBUG(net, 3, "%d) %s", i, inet_ntoa(*(struct in_addr *)&broadcast[i])); // inet_ntoa(inaddr)); + for (int i = 0; !StrEmpty(broadcast[i].GetHostname()); i++) { + DEBUG(net, 3, "%d) %s", i, broadcast[i].GetHostname()); } }
--- a/src/network/core/host.h +++ b/src/network/core/host.h @@ -6,6 +6,6 @@ #ifndef NETWORK_CORE_HOST_H -void NetworkFindBroadcastIPs(uint32 *broadcast, int limit); +void NetworkFindBroadcastIPs(NetworkAddress *broadcast, int limit); #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. -uint32 _broadcast_list[MAX_INTERFACES + 1]; +NetworkAddress _broadcast_list[MAX_INTERFACES + 1]; uint32 _sync_seed_1, _sync_seed_2; uint32 _sync_frame; bool _network_first_time;
--- a/src/network/network_gamelist.cpp +++ b/src/network/network_gamelist.cpp @@ -60,7 +60,7 @@ /** Add a new item to the linked gamelist. If the IP and Port match * return the existing item instead of adding it again - * @param ip the IP-address (inet_addr) of the to-be added item + * @param address the address of the to-be added item * @param port the port the server is running on * @return a point to the newly added or already existing item */ NetworkGameList *NetworkGameListAddItem(NetworkAddress address)
--- 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 uint32 _broadcast_list[MAX_INTERFACES + 1]; +extern NetworkAddress _broadcast_list[MAX_INTERFACES + 1]; extern uint32 _sync_seed_1, _sync_seed_2; extern uint32 _sync_frame;
--- a/src/network/network_udp.cpp +++ b/src/network/network_udp.cpp @@ -363,13 +363,12 @@ { uint i; - for (i = 0; _broadcast_list[i] != 0; i++) { + for (i = 0; !StrEmpty(_broadcast_list[i].GetHostname()); i++) { Packet p(PACKET_UDP_CLIENT_FIND_SERVER); - NetworkAddress out_addr(_broadcast_list[i], _settings_client.network.server_port); - DEBUG(net, 4, "[udp] broadcasting to %s", out_addr.GetHostname()); + DEBUG(net, 4, "[udp] broadcasting to %s", _broadcast_list[i].GetHostname()); - socket->SendPacket(&p, &out_addr); + socket->SendPacket(&p, &_broadcast_list[i]); } }