From e05e1822d61e8147889167896e97dd8687c10f69 Mon Sep 17 00:00:00 2001 From: rubidium Date: Fri, 3 Apr 2009 15:42:41 +0000 Subject: (svn r15935) -Codechange: function to compare IP addresses (to sort them) --- src/network/core/address.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src') diff --git a/src/network/core/address.h b/src/network/core/address.h index 30e79f7a4..9515de976 100644 --- a/src/network/core/address.h +++ b/src/network/core/address.h @@ -167,6 +167,23 @@ public: return this->GetPort() == address.GetPort() && strcmp(this->GetHostname(), address.GetHostname()) == 0; } + /** + * Compare the address of this class with the address of another. + * @param address the other address. + */ + bool operator < (NetworkAddress &address) + { + int r = this->address.ss_family - address.address.ss_family; + if (r == 0 && this->IsResolved() && address.IsResolved()) { + r = this->address_length - address.address_length; + if (r == 0) r = memcmp(&this->address, &address.address, this->address_length) == 0; + } else { + r = strcmp(this->GetHostname(), address.GetHostname()); + } + if (r == 0) r = this->GetPort() - address.GetPort(); + return r < 0; + } + /** * Assign another address to ourself * @param other obviously the address to assign to us -- cgit v1.2.3-70-g09d2