summaryrefslogtreecommitdiff
path: root/src/network
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
parent47602d7b60e40821c364dbd07161caaf30803d52 (diff)
downloadopenttd-11723c40b2af93baab87868853f42ce0f17c364b.tar.xz
(svn r15926) -Codechange: make the broadcast IP list less AF dependent.
Diffstat (limited to 'src/network')
-rw-r--r--src/network/core/address.h12
-rw-r--r--src/network/core/host.cpp40
-rw-r--r--src/network/core/host.h2
-rw-r--r--src/network/network.cpp2
-rw-r--r--src/network/network_gamelist.cpp2
-rw-r--r--src/network/network_internal.h2
-rw-r--r--src/network/network_udp.cpp7
7 files changed, 43 insertions, 24 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 */
diff --git a/src/network/network.cpp b/src/network/network.cpp
index 8bf97e873..3bbe7b898 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.
-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;
diff --git a/src/network/network_gamelist.cpp b/src/network/network_gamelist.cpp
index 2dd36fa51..3f1db60b3 100644
--- a/src/network/network_gamelist.cpp
+++ b/src/network/network_gamelist.cpp
@@ -60,7 +60,7 @@ static void NetworkGameListHandleDelayedInsert()
/** 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)
diff --git a/src/network/network_internal.h b/src/network/network_internal.h
index c2ed87543..f411c3630 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 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;
diff --git a/src/network/network_udp.cpp b/src/network/network_udp.cpp
index 01dad9cbc..886639fe4 100644
--- a/src/network/network_udp.cpp
+++ b/src/network/network_udp.cpp
@@ -363,13 +363,12 @@ static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
{
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]);
}
}