# address.h

Go to the documentation of this file.

    00001 /* $Id: address.h 17699 2009-10-04 20:00:56Z 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_ADDRESS_H
    00013 #define NETWORK_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
    00053   SOCKET Resolve(int family, int socktype, int flags, SocketList *sockets, LoopProc func);
    00054 public:
    00059   NetworkAddress(struct sockaddr_storage &address, int address_length) :
    00060     address_length(address_length),
    00061     address(address)
    00062   {
    00063     *this->hostname = '\0';
    00064   }
    00065
    00070   NetworkAddress(sockaddr *address, int address_length) :
    00071     address_length(address_length)
    00072   {
    00073     *this->hostname = '\0';
    00074     memset(&this->address, 0, sizeof(this->address));
    00075     memcpy(&this->address, address, address_length);
    00076   }
    00077
    00084   NetworkAddress(const char *hostname = "", uint16 port = 0, int family = AF_UNSPEC) :
    00085     address_length(0)
    00086   {
    00087     /* Also handle IPv6 bracket enclosed hostnames */
    00088     if (StrEmpty(hostname)) hostname = "";
    00089     if (*hostname == '[') hostname++;
    00090     strecpy(this->hostname, StrEmpty(hostname) ? "" : hostname, lastof(this->hostname));
    00091     char *tmp = strrchr(this->hostname, ']');
    00092     if (tmp != NULL) *tmp = '\0';
    00093
    00094     memset(&this->address, 0, sizeof(this->address));
    00095     this->address.ss_family = family;
    00096     this->SetPort(port);
    00097   }
    00098
    00103   NetworkAddress(const NetworkAddress &address)
    00104   {
    00105     memcpy(this, &address, sizeof(*this));
    00106   }
    00107
    00113   const char *GetHostname();
    00114
    00121   void GetAddressAsString(char *buffer, const char *last, bool with_family = true);
    00122
    00129   const char *GetAddressAsString(bool with_family = true);
    00130
    00135   const sockaddr_storage *GetAddress();
    00136
    00141   int GetAddressLength()
    00142   {
    00143     /* Resolve it if we didn't do it already */
    00144     if (!this->IsResolved()) this->GetAddress();
    00145     return this->address_length;
    00146   }
    00147
    00152   uint16 GetPort() const;
    00153
    00158   void SetPort(uint16 port);
    00159
    00164   bool IsResolved() const
    00165   {
    00166     return this->address_length != 0;
    00167   }
    00168
    00174   bool IsFamily(int family);
    00175
    00182   bool IsInNetmask(char *netmask);
    00183
    00189   int CompareTo(NetworkAddress &address)
    00190   {
    00191     int r = this->GetAddressLength() - address.GetAddressLength();
    00192     if (r == 0) r = this->address.ss_family - address.address.ss_family;
    00193     if (r == 0) r = memcmp(&this->address, &address.address, this->address_length);
    00194     if (r == 0) r = this->GetPort() - address.GetPort();
    00195     return r;
    00196   }
    00197
    00203   bool operator == (NetworkAddress &address)
    00204   {
    00205     return this->CompareTo(address) == 0;
    00206   }
    00207
    00213   bool operator == (NetworkAddress &address) const
    00214   {
    00215     return const_cast<NetworkAddress*>(this)->CompareTo(address) == 0;
    00216   }
    00222   bool operator != (NetworkAddress address) const
    00223   {
    00224     return const_cast<NetworkAddress*>(this)->CompareTo(address) != 0;
    00225   }
    00226
    00231   bool operator < (NetworkAddress &address)
    00232   {
    00233     return this->CompareTo(address) < 0;
    00234   }
    00235
    00240   SOCKET Connect();
    00241
    00247   void Listen(int socktype, SocketList *sockets);
    00248
    00255   static const char *SocketTypeAsString(int socktype);
    00256
    00263   static const char *AddressFamilyAsString(int family);
    00264 };
    00265
    00266 #endif /* ENABLE_NETWORK */
    00267 #endif /* NETWORK_ADDRESS_H */