address.h

Go to the documentation of this file.
00001 /* $Id$ */
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:
00051   NetworkAddress(struct sockaddr_storage &address, int address_length) :
00052     address_length(address_length),
00053     address(address)
00054   {
00055     *this->hostname = '\0';
00056   }
00057 
00063   NetworkAddress(sockaddr *address, int address_length) :
00064     address_length(address_length)
00065   {
00066     *this->hostname = '\0';
00067     memset(&this->address, 0, sizeof(this->address));
00068     memcpy(&this->address, address, address_length);
00069   }
00070 
00077   NetworkAddress(const char *hostname = "", uint16 port = 0, int family = AF_UNSPEC) :
00078     address_length(0)
00079   {
00080     /* Also handle IPv6 bracket enclosed hostnames */
00081     if (StrEmpty(hostname)) hostname = "";
00082     if (*hostname == '[') hostname++;
00083     strecpy(this->hostname, StrEmpty(hostname) ? "" : hostname, lastof(this->hostname));
00084     char *tmp = strrchr(this->hostname, ']');
00085     if (tmp != NULL) *tmp = '\0';
00086 
00087     memset(&this->address, 0, sizeof(this->address));
00088     this->address.ss_family = family;
00089     this->SetPort(port);
00090   }
00091 
00096   NetworkAddress(const NetworkAddress &address)
00097   {
00098     memcpy(this, &address, sizeof(*this));
00099   }
00100 
00101   const char *GetHostname();
00102   void GetAddressAsString(char *buffer, const char *last, bool with_family = true);
00103   const char *GetAddressAsString(bool with_family = true);
00104   const sockaddr_storage *GetAddress();
00105 
00110   int GetAddressLength()
00111   {
00112     /* Resolve it if we didn't do it already */
00113     if (!this->IsResolved()) this->GetAddress();
00114     return this->address_length;
00115   }
00116 
00117   uint16 GetPort() const;
00118   void SetPort(uint16 port);
00119 
00124   bool IsResolved() const
00125   {
00126     return this->address_length != 0;
00127   }
00128 
00129   bool IsFamily(int family);
00130   bool IsInNetmask(char *netmask);
00131 
00137   int CompareTo(NetworkAddress &address)
00138   {
00139     int r = this->GetAddressLength() - address.GetAddressLength();
00140     if (r == 0) r = this->address.ss_family - address.address.ss_family;
00141     if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
00142     if (r == 0) r = this->GetPort() - address.GetPort();
00143     return r;
00144   }
00145 
00151   bool operator == (NetworkAddress &address)
00152   {
00153     return this->CompareTo(address) == 0;
00154   }
00155 
00161   bool operator == (NetworkAddress &address) const
00162   {
00163     return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
00164   }
00170   bool operator != (NetworkAddress address) const
00171   {
00172     return const_cast<NetworkAddress*>(this)->CompareTo(address) != 0;
00173   }
00174 
00179   bool operator < (NetworkAddress &address)
00180   {
00181     return this->CompareTo(address) < 0;
00182   }
00183 
00184   SOCKET Connect();
00185   void Listen(int socktype, SocketList *sockets);
00186 
00187   static const char *SocketTypeAsString(int socktype);
00188   static const char *AddressFamilyAsString(int family);
00189 };
00190 
00191 #endif /* ENABLE_NETWORK */
00192 #endif /* NETWORK_CORE_ADDRESS_H */

Generated on Fri May 27 04:19:43 2011 for OpenTTD by  doxygen 1.6.1