1
0
Fork 0

(svn r15915) -Codechange: let the udp code use NetworkAddress.

release/1.0
rubidium 2009-04-02 19:21:26 +00:00
parent 804370d964
commit c0c6e07081
5 changed files with 90 additions and 78 deletions

View File

@ -11,25 +11,37 @@
#include "host.h" #include "host.h"
#include "../../string_func.h" #include "../../string_func.h"
const char *NetworkAddress::GetHostname() const const char *NetworkAddress::GetHostname()
{ {
if (this->hostname != NULL) return this->hostname; if (this->hostname == NULL) {
this->hostname = strdup(inet_ntoa(((struct sockaddr_in *)&this->address)->sin_addr));
in_addr addr; }
addr.s_addr = this->ip; return this->hostname;
return inet_ntoa(addr);
} }
uint32 NetworkAddress::GetIP() uint32 NetworkAddress::GetIP()
{ {
assert(this->address.ss_family == AF_INET);
if (!this->resolved) { if (!this->resolved) {
this->ip = NetworkResolveHost(this->hostname); ((struct sockaddr_in *)&this->address)->sin_addr.s_addr = NetworkResolveHost(this->hostname);
this->resolved = true; this->resolved = true;
} }
return this->ip; return ((struct sockaddr_in *)&this->address)->sin_addr.s_addr;
} }
const char *NetworkAddress::GetAddressAsString() const uint16 NetworkAddress::GetPort() const
{
switch (this->address.ss_family) {
case AF_INET:
return ntohs(((struct sockaddr_in *)&this->address)->sin_port);
default:
NOT_REACHED();
}
}
const char *NetworkAddress::GetAddressAsString()
{ {
/* 6 = for the : and 5 for the decimal port number */ /* 6 = for the : and 5 for the decimal port number */
static char buf[NETWORK_HOSTNAME_LENGTH + 6]; static char buf[NETWORK_HOSTNAME_LENGTH + 6];
@ -38,4 +50,10 @@ const char *NetworkAddress::GetAddressAsString() const
return buf; return buf;
} }
const sockaddr_storage *NetworkAddress::GetAddress()
{
if (!this->resolved) this->GetIP();
return &this->address;
}
#endif /* ENABLE_NETWORK */ #endif /* ENABLE_NETWORK */

View File

@ -16,10 +16,9 @@
*/ */
class NetworkAddress { class NetworkAddress {
private: private:
bool resolved; ///< Has the IP address been resolved bool resolved; ///< Has the IP address been resolved
char *hostname; ///< The hostname, NULL if there isn't one char *hostname; ///< The hostname, NULL if there isn't one
uint32 ip; ///< The resolved IP address sockaddr_storage address; ///< The resolved address
uint16 port; ///< The port associated with the address
public: public:
/** /**
@ -28,10 +27,23 @@ public:
* @param port the port * @param port the port
*/ */
NetworkAddress(in_addr_t ip, uint16 port) : NetworkAddress(in_addr_t ip, uint16 port) :
resolved(true),
hostname(NULL)
{
memset(&this->address, 0, sizeof(this->address));
this->address.ss_family = AF_INET;
((struct sockaddr_in*)&this->address)->sin_addr.s_addr = ip;
((struct sockaddr_in*)&this->address)->sin_port = htons(port);
}
/**
* Create a network address based on a resolved IP and port
* @param address the IP address with port
*/
NetworkAddress(struct sockaddr_storage &address) :
resolved(true), resolved(true),
hostname(NULL), hostname(NULL),
ip(ip), address(address)
port(port)
{ {
} }
@ -42,10 +54,11 @@ public:
*/ */
NetworkAddress(const char *hostname = NULL, uint16 port = 0) : NetworkAddress(const char *hostname = NULL, uint16 port = 0) :
resolved(false), resolved(false),
hostname(hostname == NULL ? NULL : strdup(hostname)), hostname(hostname == NULL ? NULL : strdup(hostname))
ip(0),
port(port)
{ {
memset(&this->address, 0, sizeof(this->address));
this->address.ss_family = AF_INET;
((struct sockaddr_in*)&this->address)->sin_port = htons(port);
} }
/** /**
@ -55,8 +68,7 @@ public:
NetworkAddress(const NetworkAddress &address) : NetworkAddress(const NetworkAddress &address) :
resolved(address.resolved), resolved(address.resolved),
hostname(address.hostname == NULL ? NULL : strdup(address.hostname)), hostname(address.hostname == NULL ? NULL : strdup(address.hostname)),
ip(address.ip), address(address.address)
port(address.port)
{ {
} }
@ -71,13 +83,19 @@ public:
* IPv4 dotted representation is given. * IPv4 dotted representation is given.
* @return the hostname * @return the hostname
*/ */
const char *GetHostname() const; const char *GetHostname();
/** /**
* Get the address as a string, e.g. 127.0.0.1:12345. * Get the address as a string, e.g. 127.0.0.1:12345.
* @return the address * @return the address
*/ */
const char *GetAddressAsString() const; const char *GetAddressAsString();
/**
* Get the address in it's internal representation.
* @return the address
*/
const sockaddr_storage *GetAddress();
/** /**
* Get the IP address. If the IP has not been resolved yet this will resolve * Get the IP address. If the IP has not been resolved yet this will resolve
@ -90,10 +108,7 @@ public:
* Get the port * Get the port
* @return the port * @return the port
*/ */
uint16 GetPort() const uint16 GetPort() const;
{
return this->port;
}
/** /**
* Check whether the IP address has been resolved already * Check whether the IP address has been resolved already

View File

@ -82,14 +82,14 @@ NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection()
* @param p the packet to send * @param p the packet to send
* @param recv the receiver (target) of the packet * @param recv the receiver (target) of the packet
*/ */
void NetworkUDPSocketHandler::SendPacket(Packet *p, const struct sockaddr_in *recv) void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv)
{ {
int res; int res;
p->PrepareToSend(); p->PrepareToSend();
/* Send the buffer */ /* Send the buffer */
res = sendto(this->sock, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv)); res = sendto(this->sock, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv->GetAddress(), sizeof(*recv->GetAddress()));
/* Check for any errors, but ignore it otherwise */ /* Check for any errors, but ignore it otherwise */
if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR()); if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR());
@ -100,7 +100,7 @@ void NetworkUDPSocketHandler::SendPacket(Packet *p, const struct sockaddr_in *re
*/ */
void NetworkUDPSocketHandler::ReceivePackets() void NetworkUDPSocketHandler::ReceivePackets()
{ {
struct sockaddr_in client_addr; struct sockaddr_storage client_addr;
socklen_t client_len; socklen_t client_len;
int nbytes; int nbytes;
Packet p(this); Packet p(this);
@ -117,19 +117,18 @@ void NetworkUDPSocketHandler::ReceivePackets()
/* We got some bytes for the base header of the packet. */ /* We got some bytes for the base header of the packet. */
if (nbytes > 2) { if (nbytes > 2) {
NetworkAddress address(client_addr);
p.PrepareToRead(); p.PrepareToRead();
/* If the size does not match the packet must be corrupted. /* If the size does not match the packet must be corrupted.
* Otherwise it will be marked as corrupted later on. */ * Otherwise it will be marked as corrupted later on. */
if (nbytes != p.size) { if (nbytes != p.size) {
DEBUG(net, 1, "received a packet with mismatching size from %s:%d", DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString());
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
return; return;
} }
/* Handle the packet */ /* Handle the packet */
this->HandleUDPPacket(&p, &client_addr); this->HandleUDPPacket(&p, &address);
} }
} }
@ -277,7 +276,7 @@ void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *i
* @param p the received packet * @param p the received packet
* @param client_addr the sender of the packet * @param client_addr the sender of the packet
*/ */
void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_in *client_addr) void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, NetworkAddress *client_addr)
{ {
PacketUDPType type; PacketUDPType type;
@ -301,9 +300,9 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_i
default: default:
if (this->HasClientQuit()) { if (this->HasClientQuit()) {
DEBUG(net, 0, "[udp] received invalid packet type %d from %s:%d", type, inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); DEBUG(net, 0, "[udp] received invalid packet type %d from %s", type, client_addr->GetAddressAsString());
} else { } else {
DEBUG(net, 0, "[udp] received illegal packet from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); DEBUG(net, 0, "[udp] received illegal packet from %s", client_addr->GetAddressAsString());
} }
break; break;
} }
@ -317,9 +316,9 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_i
*/ */
#define DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(type) \ #define DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(type) \
void NetworkUDPSocketHandler::NetworkPacketReceive_## type ##_command(\ void NetworkUDPSocketHandler::NetworkPacketReceive_## type ##_command(\
Packet *p, const struct sockaddr_in *client_addr) { \ Packet *p, NetworkAddress *client_addr) { \
DEBUG(net, 0, "[udp] received packet type %d on wrong port from %s:%d", \ DEBUG(net, 0, "[udp] received packet type %d on wrong port from %s", \
type, inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); \ type, client_addr->GetAddressAsString()); \
} }
DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER); DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);

View File

@ -67,6 +67,7 @@
#ifdef ENABLE_NETWORK #ifdef ENABLE_NETWORK
#include "os_abstraction.h" #include "os_abstraction.h"
#include "address.h"
#include "core.h" #include "core.h"
#include "game.h" #include "game.h"
#include "packet.h" #include "packet.h"
@ -88,8 +89,8 @@ enum PacketUDPType {
PACKET_UDP_END ///< Must ALWAYS be on the end of this list!! (period) PACKET_UDP_END ///< Must ALWAYS be on the end of this list!! (period)
}; };
#define DECLARE_UDP_RECEIVE_COMMAND(type) virtual void NetworkPacketReceive_## type ##_command(Packet *p, const struct sockaddr_in *) #define DECLARE_UDP_RECEIVE_COMMAND(type) virtual void NetworkPacketReceive_## type ##_command(Packet *p, NetworkAddress *client_addr)
#define DEF_UDP_RECEIVE_COMMAND(cls, type) void cls ##NetworkUDPSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p, const struct sockaddr_in *client_addr) #define DEF_UDP_RECEIVE_COMMAND(cls, type) void cls ##NetworkUDPSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p, NetworkAddress *client_addr)
/** Base socket handler for all UDP sockets */ /** Base socket handler for all UDP sockets */
class NetworkUDPSocketHandler : public NetworkSocketHandler { class NetworkUDPSocketHandler : public NetworkSocketHandler {
@ -110,7 +111,7 @@ protected:
DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS); DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS); DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
void HandleUDPPacket(Packet *p, const struct sockaddr_in *client_addr); void HandleUDPPacket(Packet *p, NetworkAddress *client_addr);
/** /**
* Function that is called for every GRFConfig that is read when receiving * Function that is called for every GRFConfig that is read when receiving
@ -127,7 +128,7 @@ public:
bool Listen(uint32 host, uint16 port, bool broadcast); bool Listen(uint32 host, uint16 port, bool broadcast);
void Close(); void Close();
void SendPacket(Packet *p, const struct sockaddr_in *recv); void SendPacket(Packet *p, NetworkAddress *recv);
void ReceivePackets(); void ReceivePackets();
void Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info); void Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info);

View File

@ -104,7 +104,7 @@ DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_FIND_SERVER)
/* Let the client know that we are here */ /* Let the client know that we are here */
this->SendPacket(&packet, client_addr); this->SendPacket(&packet, client_addr);
DEBUG(net, 2, "[udp] queried from '%s'", inet_ntoa(client_addr->sin_addr)); DEBUG(net, 2, "[udp] queried from '%s'", client_addr->GetHostname());
} }
DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_DETAIL_INFO) DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_DETAIL_INFO)
@ -154,7 +154,7 @@ DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_GET_NEWGRFS)
uint8 in_reply_count = 0; uint8 in_reply_count = 0;
size_t packet_len = 0; size_t packet_len = 0;
DEBUG(net, 6, "[udp] newgrf data request from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); DEBUG(net, 6, "[udp] newgrf data request from %s", client_addr->GetAddressAsString());
num_grfs = p->Recv_uint8 (); num_grfs = p->Recv_uint8 ();
if (num_grfs > NETWORK_MAX_GRF_COUNT) return; if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
@ -217,10 +217,10 @@ DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_RESPONSE)
/* Just a fail-safe.. should never happen */ /* Just a fail-safe.. should never happen */
if (_network_udp_server) return; if (_network_udp_server) return;
DEBUG(net, 4, "[udp] server response from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); DEBUG(net, 4, "[udp] server response from %s", client_addr->GetAddressAsString());
/* Find next item */ /* Find next item */
item = NetworkGameListAddItem(inet_addr(inet_ntoa(client_addr->sin_addr)), ntohs(client_addr->sin_port)); item = NetworkGameListAddItem(client_addr->GetIP(), client_addr->GetPort());
this->Recv_NetworkGameInfo(p, &item->info); this->Recv_NetworkGameInfo(p, &item->info);
@ -236,7 +236,6 @@ DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_RESPONSE)
const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT]; const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
const GRFConfig *c; const GRFConfig *c;
uint in_request_count = 0; uint in_request_count = 0;
struct sockaddr_in out_addr;
for (c = item->info.grfconfig; c != NULL; c = c->next) { for (c = item->info.grfconfig; c != NULL; c = c->next) {
if (c->status == GCS_NOT_FOUND) item->info.compatible = false; if (c->status == GCS_NOT_FOUND) item->info.compatible = false;
@ -255,15 +254,13 @@ DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_RESPONSE)
this->Send_GRFIdentifier(&packet, in_request[i]); this->Send_GRFIdentifier(&packet, in_request[i]);
} }
out_addr.sin_family = AF_INET; NetworkAddress out_addr(item->ip, item->port);
out_addr.sin_port = htons(item->port);
out_addr.sin_addr.s_addr = item->ip;
this->SendPacket(&packet, &out_addr); this->SendPacket(&packet, &out_addr);
} }
} }
if (item->info.hostname[0] == '\0') if (item->info.hostname[0] == '\0')
snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", inet_ntoa(client_addr->sin_addr)); snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", client_addr->GetHostname());
/* Check if we are allowed on this server based on the revision-match */ /* Check if we are allowed on this server based on the revision-match */
item->info.version_compatible = IsNetworkCompatibleVersion(item->info.server_revision); item->info.version_compatible = IsNetworkCompatibleVersion(item->info.server_revision);
@ -302,7 +299,7 @@ DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_NEWGRFS)
uint8 num_grfs; uint8 num_grfs;
uint i; uint i;
DEBUG(net, 6, "[udp] newgrf data reply from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); DEBUG(net, 6, "[udp] newgrf data reply from %s", client_addr->GetAddressAsString());
num_grfs = p->Recv_uint8 (); num_grfs = p->Recv_uint8 ();
if (num_grfs > NETWORK_MAX_GRF_COUNT) return; if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
@ -369,13 +366,9 @@ static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
for (i = 0; _broadcast_list[i] != 0; i++) { for (i = 0; _broadcast_list[i] != 0; i++) {
Packet p(PACKET_UDP_CLIENT_FIND_SERVER); Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
struct sockaddr_in out_addr; NetworkAddress out_addr(_broadcast_list[i], _settings_client.network.server_port);
out_addr.sin_family = AF_INET; DEBUG(net, 4, "[udp] broadcasting to %s", out_addr.GetHostname());
out_addr.sin_port = htons(_settings_client.network.server_port);
out_addr.sin_addr.s_addr = _broadcast_list[i];
DEBUG(net, 4, "[udp] broadcasting to %s", inet_ntoa(out_addr.sin_addr));
socket->SendPacket(&p, &out_addr); socket->SendPacket(&p, &out_addr);
} }
@ -385,24 +378,19 @@ static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
/* Request the the server-list from the master server */ /* Request the the server-list from the master server */
void NetworkUDPQueryMasterServer() void NetworkUDPQueryMasterServer()
{ {
struct sockaddr_in out_addr;
if (!_udp_client_socket->IsConnected()) { if (!_udp_client_socket->IsConnected()) {
if (!_udp_client_socket->Listen(0, 0, true)) return; if (!_udp_client_socket->Listen(0, 0, true)) return;
} }
Packet p(PACKET_UDP_CLIENT_GET_LIST); Packet p(PACKET_UDP_CLIENT_GET_LIST);
NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
out_addr.sin_family = AF_INET;
out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
/* packet only contains protocol version */ /* packet only contains protocol version */
p.Send_uint8(NETWORK_MASTER_SERVER_VERSION); p.Send_uint8(NETWORK_MASTER_SERVER_VERSION);
_udp_client_socket->SendPacket(&p, &out_addr); _udp_client_socket->SendPacket(&p, &out_addr);
DEBUG(net, 2, "[udp] master server queried at %s:%d", inet_ntoa(out_addr.sin_addr), ntohs(out_addr.sin_port)); DEBUG(net, 2, "[udp] master server queried at %s", out_addr.GetAddressAsString());
} }
/* Find all servers */ /* Find all servers */
@ -440,10 +428,7 @@ void NetworkUDPQueryServerThread(void *pntr)
{ {
NetworkUDPQueryServerInfo *info = (NetworkUDPQueryServerInfo*)pntr; NetworkUDPQueryServerInfo *info = (NetworkUDPQueryServerInfo*)pntr;
struct sockaddr_in out_addr; NetworkAddress out_addr(info->GetIP(), info->GetPort());
out_addr.sin_family = AF_INET;
out_addr.sin_port = htons(info->GetPort());
out_addr.sin_addr.s_addr = info->GetIP();
/* Clear item in gamelist */ /* Clear item in gamelist */
NetworkGameList *item = CallocT<NetworkGameList>(1); NetworkGameList *item = CallocT<NetworkGameList>(1);
@ -481,10 +466,7 @@ void NetworkUDPRemoveAdvertiseThread(void *pntr)
DEBUG(net, 1, "[udp] removing advertise from master server"); DEBUG(net, 1, "[udp] removing advertise from master server");
/* Find somewhere to send */ /* Find somewhere to send */
struct sockaddr_in out_addr; NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
out_addr.sin_family = AF_INET;
out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
/* Send the packet */ /* Send the packet */
Packet p(PACKET_UDP_SERVER_UNREGISTER); Packet p(PACKET_UDP_SERVER_UNREGISTER);
@ -516,10 +498,7 @@ void NetworkUDPRemoveAdvertise()
void NetworkUDPAdvertiseThread(void *pntr) void NetworkUDPAdvertiseThread(void *pntr)
{ {
/* Find somewhere to send */ /* Find somewhere to send */
struct sockaddr_in out_addr; NetworkAddress out_addr(NETWORK_MASTER_SERVER_HOST, NETWORK_MASTER_SERVER_PORT);
out_addr.sin_family = AF_INET;
out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
DEBUG(net, 1, "[udp] advertising to master server"); DEBUG(net, 1, "[udp] advertising to master server");