address.h

Go to the documentation of this file.
00001 /* $Id: address.h 22695 2011-07-30 10:28:52Z 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   bool resolved;                          
00037 
00043   typedef SOCKET (*LoopProc)(addrinfo *runp);
00044 
00045   SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func);
00046 public:
00052   NetworkAddress(struct sockaddr_storage &address, int address_length) :
00053     address_length(address_length),
00054     address(address),
00055     resolved(address_length != 0)
00056   {
00057     *this->hostname = '\0';
00058   }
00059 
00065   NetworkAddress(sockaddr *address, int address_length) :
00066     address_length(address_length),
00067     resolved(address_length != 0)
00068   {
00069     *this->hostname = '\0';
00070     memset(&this->address, 0, sizeof(this->address));
00071     memcpy(&this->address, address, address_length);
00072   }
00073 
00080   NetworkAddress(const char *hostname = "", uint16 port = 0, int family = AF_UNSPEC) :
00081     address_length(0),
00082     resolved(false)
00083   {
00084     /* Also handle IPv6 bracket enclosed hostnames */
00085     if (StrEmpty(hostname)) hostname = "";
00086     if (*hostname == '[') hostname++;
00087     strecpy(this->hostname, StrEmpty(hostname) ? "" : hostname, lastof(this->hostname));
00088     char *tmp = strrchr(this->hostname, ']');
00089     if (tmp != NULL) *tmp = '\0';
00090 
00091     memset(&this->address, 0, sizeof(this->address));
00092     this->address.ss_family = family;
00093     this->SetPort(port);
00094   }
00095 
00100   NetworkAddress(const NetworkAddress &address)
00101   {
00102     memcpy(this, &address, sizeof(*this));
00103   }
00104 
00105   const char *GetHostname();
00106   void GetAddressAsString(char *buffer, const char *last, bool with_family = true);
00107   const char *GetAddressAsString(bool with_family = true);
00108   const sockaddr_storage *GetAddress();
00109 
00114   int GetAddressLength()
00115   {
00116     /* Resolve it if we didn't do it already */
00117     if (!this->IsResolved()) this->GetAddress();
00118     return this->address_length;
00119   }
00120 
00121   uint16 GetPort() const;
00122   void SetPort(uint16 port);
00123 
00128   bool IsResolved() const
00129   {
00130     return this->resolved;
00131   }
00132 
00133   bool IsFamily(int family);
00134   bool IsInNetmask(char *netmask);
00135 
00141   int CompareTo(NetworkAddress &address)
00142   {
00143     int r = this->GetAddressLength() - address.GetAddressLength();
00144     if (r == 0) r = this->address.ss_family - address.address.ss_family;
00145     if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
00146     if (r == 0) r = this->GetPort() - address.GetPort();
00147     return r;
00148   }
00149 
00155   bool operator == (NetworkAddress &address)
00156   {
00157     return this->CompareTo(address) == 0;
00158   }
00159 
00165   bool operator == (NetworkAddress &address) const
00166   {
00167     return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
00168   }
00174   bool operator != (NetworkAddress address) const
00175   {
00176     return const_cast<NetworkAddress*>(this)->CompareTo(address) != 0;
00177   }
00178 
00183   bool operator < (NetworkAddress &address)
00184   {
00185     return this->CompareTo(address) < 0;
00186   }
00187 
00188   SOCKET Connect();
00189   void Listen(int socktype, SocketList *sockets);
00190 
00191   static const char *SocketTypeAsString(int socktype);
00192   static const char *AddressFamilyAsString(int family);
00193 };
00194 
00195 #endif /* ENABLE_NETWORK */
00196 #endif /* NETWORK_CORE_ADDRESS_H */