summaryrefslogtreecommitdiff
path: root/src/network/core
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-04-03 11:02:41 +0000
committerrubidium <rubidium@openttd.org>2009-04-03 11:02:41 +0000
commit11723c40b2af93baab87868853f42ce0f17c364b (patch)
tree4d97eb5554d0691ee6509cefa6c03808fea399e5 /src/network/core
parent47602d7b60e40821c364dbd07161caaf30803d52 (diff)
downloadopenttd-11723c40b2af93baab87868853f42ce0f17c364b.tar.xz
(svn r15926) -Codechange: make the broadcast IP list less AF dependent.
Diffstat (limited to 'src/network/core')
-rw-r--r--src/network/core/address.h12
-rw-r--r--src/network/core/host.cpp40
-rw-r--r--src/network/core/host.h2
3 files changed, 37 insertions, 17 deletions
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
@@ -64,6 +64,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
* @param port the port
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 */