From 11723c40b2af93baab87868853f42ce0f17c364b Mon Sep 17 00:00:00 2001 From: rubidium Date: Fri, 3 Apr 2009 11:02:41 +0000 Subject: (svn r15926) -Codechange: make the broadcast IP list less AF dependent. --- src/network/core/address.h | 12 ++++++++++++ src/network/core/host.cpp | 40 ++++++++++++++++++++++++---------------- src/network/core/host.h | 2 +- 3 files changed, 37 insertions(+), 17 deletions(-) (limited to 'src/network/core') diff --git a/src/network/core/address.h b/src/network/core/address.h index eea66cabb..30e79f7a4 100644 --- a/src/network/core/address.h +++ b/src/network/core/address.h @@ -63,6 +63,18 @@ public: { } + /** + * 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 diff --git a/src/network/core/host.cpp b/src/network/core/host.cpp index 4f83c1b37..e85bb3b3e 100644 --- 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 @@ int seek_past_header(char **pos, const char *header) 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 @@ static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // BEOS 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 @@ static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // BEOS } #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 @@ static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // GETI 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 @@ static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // GETI } #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 @@ static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // Win3 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 @@ static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // Win3 #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 @@ static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // !GET 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 @@ static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // !GET #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()); } } diff --git a/src/network/core/host.h b/src/network/core/host.h index b54819912..32fb7fc5c 100644 --- 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 */ -- cgit v1.2.3-70-g09d2