From c0c6e07081fb55d1ec5c46cc3606185062cfe45c Mon Sep 17 00:00:00 2001 From: rubidium Date: Thu, 2 Apr 2009 19:21:26 +0000 Subject: (svn r15915) -Codechange: let the udp code use NetworkAddress. --- src/network/core/address.cpp | 36 ++++++++++++++++++++++++-------- src/network/core/address.h | 49 +++++++++++++++++++++++++++++--------------- src/network/core/udp.cpp | 25 +++++++++++----------- src/network/core/udp.h | 9 ++++---- 4 files changed, 76 insertions(+), 43 deletions(-) (limited to 'src/network/core') diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index cbb9defb0..bd7f569ae 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -11,25 +11,37 @@ #include "host.h" #include "../../string_func.h" -const char *NetworkAddress::GetHostname() const +const char *NetworkAddress::GetHostname() { - if (this->hostname != NULL) return this->hostname; - - in_addr addr; - addr.s_addr = this->ip; - return inet_ntoa(addr); + if (this->hostname == NULL) { + this->hostname = strdup(inet_ntoa(((struct sockaddr_in *)&this->address)->sin_addr)); + } + return this->hostname; } uint32 NetworkAddress::GetIP() { + assert(this->address.ss_family == AF_INET); + if (!this->resolved) { - this->ip = NetworkResolveHost(this->hostname); + ((struct sockaddr_in *)&this->address)->sin_addr.s_addr = NetworkResolveHost(this->hostname); 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 */ static char buf[NETWORK_HOSTNAME_LENGTH + 6]; @@ -38,4 +50,10 @@ const char *NetworkAddress::GetAddressAsString() const return buf; } +const sockaddr_storage *NetworkAddress::GetAddress() +{ + if (!this->resolved) this->GetIP(); + return &this->address; +} + #endif /* ENABLE_NETWORK */ diff --git a/src/network/core/address.h b/src/network/core/address.h index 30d86b5e8..7488aad3f 100644 --- a/src/network/core/address.h +++ b/src/network/core/address.h @@ -16,10 +16,9 @@ */ class NetworkAddress { private: - bool resolved; ///< Has the IP address been resolved - char *hostname; ///< The hostname, NULL if there isn't one - uint32 ip; ///< The resolved IP address - uint16 port; ///< The port associated with the address + bool resolved; ///< Has the IP address been resolved + char *hostname; ///< The hostname, NULL if there isn't one + sockaddr_storage address; ///< The resolved address public: /** @@ -28,10 +27,23 @@ public: * @param port the 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), hostname(NULL), - ip(ip), - port(port) + address(address) { } @@ -42,10 +54,11 @@ public: */ NetworkAddress(const char *hostname = NULL, uint16 port = 0) : resolved(false), - hostname(hostname == NULL ? NULL : strdup(hostname)), - ip(0), - port(port) + hostname(hostname == NULL ? NULL : strdup(hostname)) { + 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) : resolved(address.resolved), hostname(address.hostname == NULL ? NULL : strdup(address.hostname)), - ip(address.ip), - port(address.port) + address(address.address) { } @@ -71,13 +83,19 @@ public: * IPv4 dotted representation is given. * @return the hostname */ - const char *GetHostname() const; + const char *GetHostname(); /** * Get the address as a string, e.g. 127.0.0.1:12345. * @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 @@ -90,10 +108,7 @@ public: * Get the port * @return the port */ - uint16 GetPort() const - { - return this->port; - } + uint16 GetPort() const; /** * Check whether the IP address has been resolved already diff --git a/src/network/core/udp.cpp b/src/network/core/udp.cpp index 6c921ae0a..31ae8a446 100644 --- a/src/network/core/udp.cpp +++ b/src/network/core/udp.cpp @@ -82,14 +82,14 @@ NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection() * @param p the packet to send * @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; p->PrepareToSend(); /* 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 */ 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() { - struct sockaddr_in client_addr; + struct sockaddr_storage client_addr; socklen_t client_len; int nbytes; Packet p(this); @@ -117,19 +117,18 @@ void NetworkUDPSocketHandler::ReceivePackets() /* We got some bytes for the base header of the packet. */ if (nbytes > 2) { + NetworkAddress address(client_addr); p.PrepareToRead(); /* If the size does not match the packet must be corrupted. * Otherwise it will be marked as corrupted later on. */ if (nbytes != p.size) { - DEBUG(net, 1, "received a packet with mismatching size from %s:%d", - inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); - + DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString()); return; } /* 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 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; @@ -301,9 +300,9 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_i default: 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 { - 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; } @@ -317,9 +316,9 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_i */ #define DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(type) \ void NetworkUDPSocketHandler::NetworkPacketReceive_## type ##_command(\ - Packet *p, const struct sockaddr_in *client_addr) { \ - DEBUG(net, 0, "[udp] received packet type %d on wrong port from %s:%d", \ - type, inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); \ + Packet *p, NetworkAddress *client_addr) { \ + DEBUG(net, 0, "[udp] received packet type %d on wrong port from %s", \ + type, client_addr->GetAddressAsString()); \ } DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER); diff --git a/src/network/core/udp.h b/src/network/core/udp.h index 3696bada4..dd019b3bb 100644 --- a/src/network/core/udp.h +++ b/src/network/core/udp.h @@ -67,6 +67,7 @@ #ifdef ENABLE_NETWORK #include "os_abstraction.h" +#include "address.h" #include "core.h" #include "game.h" #include "packet.h" @@ -88,8 +89,8 @@ enum PacketUDPType { 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 DEF_UDP_RECEIVE_COMMAND(cls, type) void cls ##NetworkUDPSocketHandler::NetworkPacketReceive_ ## type ## _command(Packet *p, const struct sockaddr_in *client_addr) +#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, NetworkAddress *client_addr) /** Base socket handler for all UDP sockets */ class NetworkUDPSocketHandler : public NetworkSocketHandler { @@ -110,7 +111,7 @@ protected: DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_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 @@ -127,7 +128,7 @@ public: bool Listen(uint32 host, uint16 port, bool broadcast); void Close(); - void SendPacket(Packet *p, const struct sockaddr_in *recv); + void SendPacket(Packet *p, NetworkAddress *recv); void ReceivePackets(); void Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info); -- cgit v1.2.3-54-g00ecf