mirror of
https://github.com/JGRennison/OpenTTD-patches.git
synced 2024-11-16 00:12:51 +00:00
(svn r15969) -Codechange: make the list of broadcast addresses virtually unlimited.
This commit is contained in:
parent
fdddfd6269
commit
82f8badc07
@ -10,6 +10,10 @@
|
|||||||
#include "os_abstraction.h"
|
#include "os_abstraction.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "../../string_func.h"
|
#include "../../string_func.h"
|
||||||
|
#include "../../core/smallvec_type.hpp"
|
||||||
|
|
||||||
|
class NetworkAddress;
|
||||||
|
typedef SmallVector<NetworkAddress, 4> NetworkAddressList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for (un)resolved network addresses; there's no reason to transform
|
* Wrapper for (un)resolved network addresses; there's no reason to transform
|
||||||
|
@ -15,14 +15,12 @@
|
|||||||
* Internal implementation for finding the broadcast IPs.
|
* Internal implementation for finding the broadcast IPs.
|
||||||
* This function is implemented multiple times for multiple targets.
|
* This function is implemented multiple times for multiple targets.
|
||||||
* @param broadcast the list of broadcasts to write into.
|
* @param broadcast the list of broadcasts to write into.
|
||||||
* @param limit the maximum number of items to add.
|
|
||||||
*/
|
*/
|
||||||
static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit);
|
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast);
|
||||||
|
|
||||||
#if defined(PSP)
|
#if defined(PSP)
|
||||||
static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // PSP implementation
|
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // PSP implementation
|
||||||
{
|
{
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined(BEOS_NET_SERVER) /* doesn't have neither getifaddrs or net/if.h */
|
#elif defined(BEOS_NET_SERVER) /* doesn't have neither getifaddrs or net/if.h */
|
||||||
@ -39,27 +37,25 @@ int seek_past_header(char **pos, const char *header)
|
|||||||
return B_OK;
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // BEOS implementation
|
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // BEOS implementation
|
||||||
{
|
{
|
||||||
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
if (sock < 0) {
|
if (sock < 0) {
|
||||||
DEBUG(net, 0, "[core] error creating socket");
|
DEBUG(net, 0, "[core] error creating socket");
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *output_pointer = NULL;
|
char *output_pointer = NULL;
|
||||||
int output_length = _netstat(sock, &output_pointer, 1);
|
int output_length = _netstat(sock, &output_pointer, 1);
|
||||||
if (output_length < 0) {
|
if (output_length < 0) {
|
||||||
DEBUG(net, 0, "[core] error running _netstat");
|
DEBUG(net, 0, "[core] error running _netstat");
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int index;
|
|
||||||
char **output = &output_pointer;
|
char **output = &output_pointer;
|
||||||
if (seek_past_header(output, "IP Interfaces:") == B_OK) {
|
if (seek_past_header(output, "IP Interfaces:") == B_OK) {
|
||||||
while (index != limit) {
|
for (;;) {
|
||||||
|
|
||||||
uint32 n, fields, read;
|
uint32 n, fields, read;
|
||||||
uint8 i1, i2, i3, i4, j1, j2, j3, j4;
|
uint8 i1, i2, i3, i4, j1, j2, j3, j4;
|
||||||
struct in_addr inaddr;
|
struct in_addr inaddr;
|
||||||
@ -80,7 +76,7 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit)
|
|||||||
sockaddr_storage address;
|
sockaddr_storage address;
|
||||||
memset(&address, 0, sizeof(address));
|
memset(&address, 0, sizeof(address));
|
||||||
((sockaddr_in*)&address)->sin_addr.s_addr = htonl(ip | ~netmask);
|
((sockaddr_in*)&address)->sin_addr.s_addr = htonl(ip | ~netmask);
|
||||||
broadcast[index] = NetworkAddress(address, sizeof(sockaddr));
|
*broadcast->Append() = NetworkAddress(address, sizeof(sockaddr));
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
if (read < 0) {
|
if (read < 0) {
|
||||||
@ -90,47 +86,40 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit)
|
|||||||
}
|
}
|
||||||
closesocket(sock);
|
closesocket(sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
return index;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined(HAVE_GETIFADDRS)
|
#elif defined(HAVE_GETIFADDRS)
|
||||||
static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // GETIFADDRS implementation
|
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // GETIFADDRS implementation
|
||||||
{
|
{
|
||||||
struct ifaddrs *ifap, *ifa;
|
struct ifaddrs *ifap, *ifa;
|
||||||
|
|
||||||
if (getifaddrs(&ifap) != 0) return 0;
|
if (getifaddrs(&ifap) != 0) return;
|
||||||
|
|
||||||
int index = 0;
|
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
|
||||||
for (ifa = ifap; ifa != NULL && index != limit; ifa = ifa->ifa_next) {
|
|
||||||
if (!(ifa->ifa_flags & IFF_BROADCAST)) continue;
|
if (!(ifa->ifa_flags & IFF_BROADCAST)) continue;
|
||||||
if (ifa->ifa_broadaddr == NULL) continue;
|
if (ifa->ifa_broadaddr == NULL) continue;
|
||||||
if (ifa->ifa_broadaddr->sa_family != AF_INET) continue;
|
if (ifa->ifa_broadaddr->sa_family != AF_INET) continue;
|
||||||
|
|
||||||
broadcast[index] = NetworkAddress(ifa->ifa_broadaddr, sizeof(sockaddr));
|
*broadcast->Append() = NetworkAddress(ifa->ifa_broadaddr, sizeof(sockaddr));
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
freeifaddrs(ifap);
|
freeifaddrs(ifap);
|
||||||
|
|
||||||
return index;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined(WIN32)
|
#elif defined(WIN32)
|
||||||
static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // Win32 implementation
|
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // Win32 implementation
|
||||||
{
|
{
|
||||||
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
|
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (sock == INVALID_SOCKET) return 0;
|
if (sock == INVALID_SOCKET) return;
|
||||||
|
|
||||||
DWORD len = 0;
|
DWORD len = 0;
|
||||||
INTERFACE_INFO *ifo = AllocaM(INTERFACE_INFO, limit);
|
INTERFACE_INFO *ifo = AllocaM(INTERFACE_INFO, limit);
|
||||||
memset(ifo, 0, limit * sizeof(*ifo));
|
memset(ifo, 0, limit * sizeof(*ifo));
|
||||||
if ((WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, &ifo[0], limit * sizeof(*ifo), &len, NULL, NULL)) != 0) {
|
if ((WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, &ifo[0], limit * sizeof(*ifo), &len, NULL, NULL)) != 0) {
|
||||||
closesocket(sock);
|
closesocket(sock);
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = 0;
|
for (uint j = 0; j < len / sizeof(*ifo); j++) {
|
||||||
for (uint j = 0; j < len / sizeof(*ifo) && index != limit; j++) {
|
|
||||||
if (ifo[j].iiFlags & IFF_LOOPBACK) continue;
|
if (ifo[j].iiFlags & IFF_LOOPBACK) continue;
|
||||||
if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue;
|
if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue;
|
||||||
|
|
||||||
@ -139,22 +128,20 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit)
|
|||||||
/* iiBroadcast is unusable, because it always seems to be set to 255.255.255.255. */
|
/* iiBroadcast is unusable, because it always seems to be set to 255.255.255.255. */
|
||||||
memcpy(&address, &ifo[j].iiAddress.Address, sizeof(sockaddr));
|
memcpy(&address, &ifo[j].iiAddress.Address, sizeof(sockaddr));
|
||||||
((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
|
((sockaddr_in*)&address)->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));
|
*broadcast->Append() = NetworkAddress(address, sizeof(sockaddr));
|
||||||
index++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
closesocket(sock);
|
closesocket(sock);
|
||||||
return index;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#else /* not HAVE_GETIFADDRS */
|
#else /* not HAVE_GETIFADDRS */
|
||||||
|
|
||||||
#include "../../string_func.h"
|
#include "../../string_func.h"
|
||||||
|
|
||||||
static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit) // !GETIFADDRS implementation
|
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // !GETIFADDRS implementation
|
||||||
{
|
{
|
||||||
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
|
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (sock == INVALID_SOCKET) return 0;
|
if (sock == INVALID_SOCKET) return;
|
||||||
|
|
||||||
char buf[4 * 1024]; // Arbitrary buffer size
|
char buf[4 * 1024]; // Arbitrary buffer size
|
||||||
struct ifconf ifconf;
|
struct ifconf ifconf;
|
||||||
@ -163,12 +150,11 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit)
|
|||||||
ifconf.ifc_buf = buf;
|
ifconf.ifc_buf = buf;
|
||||||
if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
|
if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
|
||||||
closesocket(sock);
|
closesocket(sock);
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *buf_end = buf + ifconf.ifc_len;
|
const char *buf_end = buf + ifconf.ifc_len;
|
||||||
int index = 0;
|
for (const char *p = buf; p < buf_end;) {
|
||||||
for (const char *p = buf; p < buf_end && index != limit;) {
|
|
||||||
const struct ifreq *req = (const struct ifreq*)p;
|
const struct ifreq *req = (const struct ifreq*)p;
|
||||||
|
|
||||||
if (req->ifr_addr.sa_family == AF_INET) {
|
if (req->ifr_addr.sa_family == AF_INET) {
|
||||||
@ -178,7 +164,7 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit)
|
|||||||
if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
|
if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
|
||||||
r.ifr_flags & IFF_BROADCAST &&
|
r.ifr_flags & IFF_BROADCAST &&
|
||||||
ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
|
ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
|
||||||
broadcast[index++] = NetworkAddress(&r.ifr_broadaddr, sizeof(sockaddr));
|
*broadcast->Append() = NetworkAddress(&r.ifr_broadaddr, sizeof(sockaddr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,8 +175,6 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit)
|
|||||||
}
|
}
|
||||||
|
|
||||||
closesocket(sock);
|
closesocket(sock);
|
||||||
|
|
||||||
return index;
|
|
||||||
}
|
}
|
||||||
#endif /* all NetworkFindBroadcastIPsInternals */
|
#endif /* all NetworkFindBroadcastIPsInternals */
|
||||||
|
|
||||||
@ -198,19 +182,17 @@ static int NetworkFindBroadcastIPsInternal(NetworkAddress *broadcast, int limit)
|
|||||||
* Find the IPv4 broadcast addresses; IPv6 uses a completely different
|
* Find the IPv4 broadcast addresses; IPv6 uses a completely different
|
||||||
* strategy for broadcasting.
|
* strategy for broadcasting.
|
||||||
* @param broadcast the list of broadcasts to write into.
|
* @param broadcast the list of broadcasts to write into.
|
||||||
* @param limit the maximum number of items to add.
|
|
||||||
*/
|
*/
|
||||||
void NetworkFindBroadcastIPs(NetworkAddress *broadcast, int limit)
|
void NetworkFindBroadcastIPs(NetworkAddressList *broadcast)
|
||||||
{
|
{
|
||||||
int count = NetworkFindBroadcastIPsInternal(broadcast, limit);
|
NetworkFindBroadcastIPsInternal(broadcast);
|
||||||
|
|
||||||
/* Make sure the list is terminated. */
|
|
||||||
broadcast[count] = NetworkAddress("");
|
|
||||||
|
|
||||||
/* Now display to the debug all the detected ips */
|
/* Now display to the debug all the detected ips */
|
||||||
DEBUG(net, 3, "Detected broadcast addresses:");
|
DEBUG(net, 3, "Detected broadcast addresses:");
|
||||||
for (int i = 0; !StrEmpty(broadcast[i].GetHostname()); i++) {
|
int i = 0;
|
||||||
DEBUG(net, 3, "%d) %s", i, broadcast[i].GetHostname());
|
for (NetworkAddress *addr = broadcast->Begin(); addr != broadcast->End(); addr++) {
|
||||||
|
addr->SetPort(NETWORK_DEFAULT_PORT);
|
||||||
|
DEBUG(net, 3, "%d) %s", i++, addr->GetHostname());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,6 @@
|
|||||||
#ifndef NETWORK_CORE_HOST_H
|
#ifndef NETWORK_CORE_HOST_H
|
||||||
#define NETWORK_CORE_HOST_H
|
#define NETWORK_CORE_HOST_H
|
||||||
|
|
||||||
void NetworkFindBroadcastIPs(NetworkAddress *broadcast, int limit);
|
void NetworkFindBroadcastIPs(NetworkAddressList *broadcast);
|
||||||
|
|
||||||
#endif /* NETWORK_CORE_HOST_H */
|
#endif /* NETWORK_CORE_HOST_H */
|
||||||
|
@ -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_max; // To where we may go with our clients
|
||||||
uint32 _frame_counter;
|
uint32 _frame_counter;
|
||||||
uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
|
uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
|
||||||
NetworkAddress _broadcast_list[MAX_INTERFACES + 1];
|
NetworkAddressList _broadcast_list;
|
||||||
uint32 _sync_seed_1, _sync_seed_2;
|
uint32 _sync_seed_1, _sync_seed_2;
|
||||||
uint32 _sync_frame;
|
uint32 _sync_frame;
|
||||||
bool _network_first_time;
|
bool _network_first_time;
|
||||||
@ -1073,7 +1073,7 @@ void NetworkStartUp()
|
|||||||
NetworkUDPInitialize();
|
NetworkUDPInitialize();
|
||||||
NetworkInitialize();
|
NetworkInitialize();
|
||||||
DEBUG(net, 3, "[core] network online, multiplayer available");
|
DEBUG(net, 3, "[core] network online, multiplayer available");
|
||||||
NetworkFindBroadcastIPs(_broadcast_list, MAX_INTERFACES);
|
NetworkFindBroadcastIPs(&_broadcast_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** This shuts the network down */
|
/** This shuts the network down */
|
||||||
|
@ -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.
|
extern uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
|
||||||
|
|
||||||
/* networking settings */
|
/* networking settings */
|
||||||
extern NetworkAddress _broadcast_list[MAX_INTERFACES + 1];
|
extern NetworkAddressList _broadcast_list;
|
||||||
|
|
||||||
extern uint32 _sync_seed_1, _sync_seed_2;
|
extern uint32 _sync_seed_1, _sync_seed_2;
|
||||||
extern uint32 _sync_frame;
|
extern uint32 _sync_frame;
|
||||||
|
@ -26,9 +26,6 @@ enum {
|
|||||||
*/
|
*/
|
||||||
MAX_CLIENT_SLOTS = 256,
|
MAX_CLIENT_SLOTS = 256,
|
||||||
|
|
||||||
/** Maximum number of internet interfaces supported. */
|
|
||||||
MAX_INTERFACES = 9,
|
|
||||||
|
|
||||||
/** How many vehicle/station types we put over the network */
|
/** How many vehicle/station types we put over the network */
|
||||||
NETWORK_VEHICLE_TYPES = 5,
|
NETWORK_VEHICLE_TYPES = 5,
|
||||||
NETWORK_STATION_TYPES = 5,
|
NETWORK_STATION_TYPES = 5,
|
||||||
|
@ -371,14 +371,12 @@ void NetworkUDPCloseAll()
|
|||||||
/* Broadcast to all ips */
|
/* Broadcast to all ips */
|
||||||
static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
|
static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
|
||||||
{
|
{
|
||||||
uint i;
|
for (NetworkAddress *addr = _broadcast_list.Begin(); addr != _broadcast_list.End(); addr++) {
|
||||||
|
|
||||||
for (i = 0; !StrEmpty(_broadcast_list[i].GetHostname()); i++) {
|
|
||||||
Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
|
Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
|
||||||
|
|
||||||
DEBUG(net, 4, "[udp] broadcasting to %s", _broadcast_list[i].GetHostname());
|
DEBUG(net, 4, "[udp] broadcasting to %s", addr->GetHostname());
|
||||||
|
|
||||||
socket->SendPacket(&p, &_broadcast_list[i]);
|
socket->SendPacket(&p, addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user