(svn r15926) -Codechange: make the broadcast IP list less AF dependent.

This commit is contained in:
rubidium 2009-04-03 11:02:41 +00:00
parent 47602d7b60
commit 11723c40b2
7 changed files with 43 additions and 24 deletions

View File

@ -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

View File

@ -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());
}
}

View File

@ -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 */

View File

@ -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;

View File

@ -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)

View File

@ -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;

View File

@ -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]);
}
}