summaryrefslogtreecommitdiff
path: root/src/network/core/address.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-04-07 19:04:37 +0000
committerrubidium <rubidium@openttd.org>2009-04-07 19:04:37 +0000
commitfdddfd6269ec4f5c770c288f05927ab8b0a60142 (patch)
treeb25fdd92d40db23425b5054e4630acdadcdd27c4 /src/network/core/address.h
parent22d93068890b758ac7d54256fdc24e0c3169504e (diff)
downloadopenttd-fdddfd6269ec4f5c770c288f05927ab8b0a60142.tar.xz
(svn r15968) -Codechange: do not allocate a buffer for NetworkAddresses so passing it around is easier.
Diffstat (limited to 'src/network/core/address.h')
-rw-r--r--src/network/core/address.h48
1 files changed, 19 insertions, 29 deletions
diff --git a/src/network/core/address.h b/src/network/core/address.h
index ecb3a8f22..f2bc00f74 100644
--- a/src/network/core/address.h
+++ b/src/network/core/address.h
@@ -8,6 +8,8 @@
#ifdef ENABLE_NETWORK
#include "os_abstraction.h"
+#include "config.h"
+#include "../../string_func.h"
/**
* Wrapper for (un)resolved network addresses; there's no reason to transform
@@ -16,9 +18,9 @@
*/
class NetworkAddress {
private:
- char *hostname; ///< The hostname, NULL if there isn't one
- size_t address_length; ///< The length of the resolved address
- sockaddr_storage address; ///< The resolved address
+ char hostname[NETWORK_HOSTNAME_LENGTH]; ///< The hostname
+ size_t address_length; ///< The length of the resolved address
+ sockaddr_storage address; ///< The resolved address
/**
* Helper function to resolve something to a socket.
@@ -43,9 +45,9 @@ public:
* @param port the port
*/
NetworkAddress(in_addr_t ip, uint16 port) :
- hostname(NULL),
address_length(sizeof(sockaddr))
{
+ *this->hostname = '\0';
memset(&this->address, 0, sizeof(this->address));
this->address.ss_family = AF_INET;
((struct sockaddr_in*)&this->address)->sin_addr.s_addr = ip;
@@ -57,10 +59,10 @@ public:
* @param address the IP address with port
*/
NetworkAddress(struct sockaddr_storage &address, size_t address_length) :
- hostname(NULL),
address_length(address_length),
address(address)
{
+ *this->hostname = '\0';
}
/**
@@ -68,9 +70,9 @@ public:
* @param address the IP address with port
*/
NetworkAddress(sockaddr *address, size_t address_length) :
- hostname(NULL),
address_length(address_length)
{
+ *this->hostname = '\0';
memset(&this->address, 0, sizeof(this->address));
memcpy(&this->address, address, address_length);
}
@@ -82,9 +84,9 @@ public:
* @param family the address family
*/
NetworkAddress(const char *hostname = "0.0.0.0", uint16 port = 0, int family = AF_INET) :
- hostname(strdup(hostname)),
address_length(0)
{
+ strecpy(this->hostname, StrEmpty(hostname) ? "" : hostname, lastof(this->hostname));
memset(&this->address, 0, sizeof(this->address));
this->address.ss_family = family;
this->SetPort(port);
@@ -94,17 +96,9 @@ public:
* Make a clone of another address
* @param address the address to clone
*/
- NetworkAddress(const NetworkAddress &address) :
- hostname(address.hostname == NULL ? NULL : strdup(address.hostname)),
- address_length(address.address_length),
- address(address.address)
+ NetworkAddress(const NetworkAddress &address)
{
- }
-
- /** Clean up our mess */
- ~NetworkAddress()
- {
- free(hostname);
+ memcpy(this, &address, sizeof(*this));
}
/**
@@ -183,6 +177,7 @@ public:
/**
* Compare the address of this class with the address of another.
* @param address the other address.
+ * @return true if both match.
*/
bool operator == (NetworkAddress address)
{
@@ -192,25 +187,20 @@ public:
/**
* Compare the address of this class with the address of another.
* @param address the other address.
+ * @return true if both match.
*/
- bool operator < (NetworkAddress address)
+ bool operator == (NetworkAddress address) const
{
- return this->CompareTo(address) < 0;
+ return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
}
/**
- * Assign another address to ourself
- * @param other obviously the address to assign to us
- * @return 'this'
+ * Compare the address of this class with the address of another.
+ * @param address the other address.
*/
- NetworkAddress& operator = (const NetworkAddress &other)
+ bool operator < (NetworkAddress address)
{
- if (this != &other) { // protect against invalid self-assignment
- free(this->hostname);
- memcpy(this, &other, sizeof(*this));
- if (other.hostname != NULL) this->hostname = strdup(other.hostname);
- }
- return *this;
+ return this->CompareTo(address) < 0;
}
/**