address.h

Go to the documentation of this file.
00001 /* $Id: address.h 21886 2011-01-22 09:53:15Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef NETWORK_CORE_ADDRESS_H
00013 #define NETWORK_CORE_ADDRESS_H
00014 
00015 #include "os_abstraction.h"
00016 #include "config.h"
00017 #include "../../string_func.h"
00018 #include "../../core/smallmap_type.hpp"
00019 
00020 #ifdef ENABLE_NETWORK
00021 
00022 class NetworkAddress;
00023 typedef SmallVector<NetworkAddress, 4> NetworkAddressList;
00024 typedef SmallMap<NetworkAddress, SOCKET, 4> SocketList;
00025 
00031 class NetworkAddress {
00032 private:
00033   char hostname[NETWORK_HOSTNAME_LENGTH]; 
00034   int address_length;                     
00035   sockaddr_storage address;               
00036 
00042   typedef SOCKET (*LoopProc)(addrinfo *runp);
00043 
00044   SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func);
00045 public:
00050   NetworkAddress(struct sockaddr_storage &address, int address_length) :
00051     address_length(address_length),
00052     address(address)
00053   {
00054     *this->hostname = '\0';
00055   }
00056 
00061   NetworkAddress(sockaddr *address, int address_length) :
00062     address_length(address_length)
00063   {
00064     *this->hostname = '\0';
00065     memset(&this->address, 0, sizeof(this->address));
00066     memcpy(&this->address, address, address_length);
00067   }
00068 
00075   NetworkAddress(const char *hostname = "", uint16 port = 0, int family = AF_UNSPEC) :
00076     address_length(0)
00077   {
00078     /* Also handle IPv6 bracket enclosed hostnames */
00079     if (StrEmpty(hostname)) hostname = "";
00080     if (*hostname == '[') hostname++;
00081     strecpy(this->hostname, StrEmpty(hostname) ? "" : hostname, lastof(this->hostname));
00082     char *tmp = strrchr(this->hostname, ']');
00083     if (tmp != NULL) *tmp = '\0';
00084 
00085     memset(&this->address, 0, sizeof(this->address));
00086     this->address.ss_family = family;
00087     this->SetPort(port);
00088   }
00089 
00094   NetworkAddress(const NetworkAddress &address)
00095   {
00096     memcpy(this, &address, sizeof(*this));
00097   }
00098 
00099   const char *GetHostname();
00100   void GetAddressAsString(char *buffer, const char *last, bool with_family = true);
00101   const char *GetAddressAsString(bool with_family = true);
00102   const sockaddr_storage *GetAddress();
00103 
00108   int GetAddressLength()
00109   {
00110     /* Resolve it if we didn't do it already */
00111     if (!this->IsResolved()) this->GetAddress();
00112     return this->address_length;
00113   }
00114 
00115   uint16 GetPort() const;
00116   void SetPort(uint16 port);
00117 
00122   bool IsResolved() const
00123   {
00124     return this->address_length != 0;
00125   }
00126 
00127   bool IsFamily(int family);
00128   bool IsInNetmask(char *netmask);
00129 
00135   int CompareTo(NetworkAddress &address)
00136   {
00137     int r = this->GetAddressLength() - address.GetAddressLength();
00138     if (r == 0) r = this->address.ss_family - address.address.ss_family;
00139     if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
00140     if (r == 0) r = this->GetPort() - address.GetPort();
00141     return r;
00142   }
00143 
00149   bool operator == (NetworkAddress &address)
00150   {
00151     return this->CompareTo(address) == 0;
00152   }
00153 
00159   bool operator == (NetworkAddress &address) const
00160   {
00161     return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
00162   }
00168   bool operator != (NetworkAddress address) const
00169   {
00170     return const_cast<NetworkAddress*>(this)->CompareTo(address) != 0;
00171   }
00172 
00177   bool operator < (NetworkAddress &address)
00178   {
00179     return this->CompareTo(address) < 0;
00180   }
00181 
00182   SOCKET Connect();
00183   void Listen(int socktype, SocketList *sockets);
00184 
00185   static const char *SocketTypeAsString(int socktype);
00186   static const char *AddressFamilyAsString(int family);
00187 };
00188 
00189 #endif /* ENABLE_NETWORK */
00190 #endif /* NETWORK_CORE_ADDRESS_H */

Generated on Fri Mar 4 21:37:00 2011 for OpenTTD by  doxygen 1.6.1