00001 #ifndef DV_INET_ADDR_H 00002 #define DV_INET_ADDR_H 00003 00004 // $Id: inetaddress.h,v 1.27 2008/03/12 19:43:27 dvermeir Exp $ 00005 00006 #include <string> 00007 #include <stdexcept> 00008 #include <dvutil/shared_ptr.h> 00009 00010 /** @file 00011 * The Dv::Net::InetAddress class represents a valid internet host. 00012 */ 00013 namespace Dv { 00014 namespace Net { 00015 00016 /** A Dv::Net::InetAddress object represents a valid internet host. */ 00017 class InetAddress { 00018 public: 00019 ~InetAddress(); 00020 /** 00021 * Factory method: return ref<InetAddress> corresponding with host or 0. 00022 * @param host string representation of host, of the form 00023 * "tinf2.vub.ac.be" or "134.184.65.2". "localhost" is also a valid argument. 00024 * @return reference to InetAddress representing the host with given 00025 * name or 0 00026 * 00027 * Example usage: 00028 * @code 00029 * Dv::Util::ref<InetAddress> address(InetAddress::Net::by_name("tinf2.vub.ac.be")); 00030 * if (address) 00031 * cout << address->dot_address() << endl; 00032 * @endcode 00033 */ 00034 static Dv::shared_ptr<InetAddress> by_name(const std::string& host); 00035 /** Factory method: return ref<InetAddress> corresponding with addr or 0. 00036 * Note that this function does not automatically fill in the 00037 * alphabetic host, in order not to do a possibly superfluous 00038 * DNS query. 00039 * @param addr numeric internet address 00040 * @return reference to InetAddress representing the host with @a addr or 0. 00041 * @sa Dv::Net::InetAddress::host 00042 */ 00043 static Dv::shared_ptr<InetAddress> by_address(unsigned long addr); 00044 00045 /** @return true iff the numeric addresses are the same. */ 00046 bool operator==(const InetAddress&) const; 00047 00048 /** Find alphabetic hostname of this address. Note that, 00049 * if the Dv::Net::InetAddress object was created using 00050 * Dv::Net::InetAddress::by_address, this call may 00051 * result in a DNS query. This is because the (alphabetic) 00052 * host is only obtained ``on demand'', i.e. by calling 00053 * this function. This feature was introduced to avoid 00054 * unnecessary DNS queries. 00055 * @return 0 if host name cannot be obtained using gethostbyaddr. 00056 * @return string representation of host, e.g. "tinf2.vub.ac.be." 00057 * @sa Dv::Net::InetAddress::by_address 00058 */ 00059 const char* host() const; 00060 /** @return numeric address of host (in network order). */ 00061 unsigned long address() const { return address_; } 00062 /** @return dot address of host, e.g. "134.184.65.2". */ 00063 const std::string& dot_address() const { return dot_address_; } 00064 /** @return name of local host or "localhost" if not found. */ 00065 static std::string local_host(); 00066 private: 00067 mutable const char* host_; // star2.vub.ac.be 00068 unsigned long address_; // in network order 00069 std::string dot_address_; // 134.184.65.2 00070 00071 /** Private constructor. */ 00072 InetAddress(const char*, unsigned long, const char *); 00073 00074 /** Use gethostbyaddr to find the hostname. 00075 * @return 0 if not found 00076 * @warning the return value has been allocated using strdup 00077 */ 00078 static const char* host_name_from_address(unsigned long addr); 00079 }; 00080 00081 /** Parse a string of the form "host:port" into its constitiuents. 00082 * @param host_port a string of the form "host:port", e.g. 00083 * "tinf2.vub.ac.be:80" 00084 * @param host the host part of the @a host_port parameter. 00085 * @param port the port number from the @a host_port parameter 00086 * @exception std::runtime_error if @a host_port is malformed 00087 */ 00088 void parse_host_port(const std::string& host_port, std::string& host, int& port) 00089 throw (std::runtime_error); 00090 } 00091 } 00092 00093 #endif 00094
dvnet-0.9.24 | [ 5 December, 2009] |