Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

IpAddress.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00002 
00009 
00010 IpAddress::IpAddress() {
00011     setVersion(IPv4);
00012     setIpAddress((unsigned long)0);
00013 } // ctor
00014 
00015 
00016 IpAddress::IpAddress(unsigned long value) {
00017     setVersion(IPv4);
00018     setIpAddress(value);
00019 } // ctor
00020 
00021 
00022 IpAddress::IpAddress(const char* value) {
00023     setVersion(IPv4);
00024     setIpAddress(value);
00025 } // ctor
00026 
00027 
00028 IpAddress::IpAddress(string value) {
00029     setVersion(IPv4);
00030     setIpAddress(value);
00031 } // ctor
00032 
00033 
00034 IpAddress::IpAddress(const struct in_addr value) {
00035     setVersion(IPv4);
00036     setIpAddress(value);
00037 } // ctor
00038 
00039 
00040 IpAddress::IpAddress(const IpAddress& value) {
00041   setIpAddress(value);
00042 } // ctor
00043 
00044 
00045 IpAddress::~IpAddress() {
00046     m_ipAddr = 0;
00047 } // dtor
00048 
00049 
00050 unsigned long 
00051 IpAddress::getUnsignedLongForm() {
00052     return m_ipAddr;
00053 } // fn getUnsignedLongForm
00054 
00055 
00056 string
00057 IpAddress::getHostname() {
00058     const char* ipStr = toCStr();
00059     struct hostent hostEntry;
00060     threadsafe_gethostbyaddr(&hostEntry, ipStr, strlen(ipStr), AF_INET);
00061     m_hostname = hostEntry.h_name;
00062     return m_hostname;
00063 } // fn getHostname
00064 
00065 
00066 void 
00067 IpAddress::setIpAddress(unsigned long value) {
00068     m_ipAddr = value;
00069 #ifdef DEBUG_ON
00070     toCStr();
00071 #endif
00072 } // fn setIpAddress
00073 
00074 
00081 void 
00082 IpAddress::setIpAddress(const char* value) {
00083     // figure out if it is a dotted decimal or a name
00084     if (inet_addr(value) != INADDR_NONE) {
00085                 m_ipAddr = inet_addr(value);
00086 #ifdef DEBUG_ON
00087         toCStr();
00088 #endif
00089         return;
00090     }
00091         
00092     // it is probably a name then...
00093         struct in_addr tmpInAddr;
00094         struct hostent hostEntry;
00095     if (threadsafe_gethostbyname(&hostEntry, value)) {
00096                 memcpy(&tmpInAddr.s_addr, hostEntry.h_addr, hostEntry.h_length);
00097         m_ipAddr = tmpInAddr.s_addr;
00098         }
00099         else {
00100                 m_ipAddr = 0;
00101         }
00102 
00103 #ifdef DEBUG_ON
00104     toCStr();
00105 #endif
00106 } // fn setIpAddress
00107 
00108 
00109 void 
00110 IpAddress::setIpAddress(string value) {
00111     setIpAddress(value.c_str());
00112 } // fn setIpAddress
00113 
00114 
00115 void 
00116 IpAddress::setIpAddress(struct in_addr value) {
00117 #ifdef WIN32
00118     m_ipAddr = value.S_un.S_addr;
00119 #else
00120     m_ipAddr = value.s_addr;
00121 #endif
00122 
00123 #ifdef DEBUG_ON
00124     toCStr();
00125 #endif
00126 } // fn setIpAddress
00127 
00128 
00129 void
00130 IpAddress::setIpAddress(const IpAddress& value) {
00131     m_ipAddr = value.m_ipAddr;
00132     m_version = value.m_version;
00133 #ifdef DEBUG_ON
00134     toCStr(); // for debugging
00135 #endif
00136 } // fn setIpAddress
00137 
00138 
00139 IpAddress::IpVersion
00140 IpAddress::getVersion() {
00141     return m_version;
00142 } // fn getVersion
00143 
00144 
00145 void 
00146 IpAddress::setVersion(IpVersion version) {
00147     m_version = version;
00148 } // fn setVersion
00149 
00150 
00151 bool 
00152 IpAddress::equals(IpAddress value) {
00153     return (value.m_ipAddr == m_ipAddr);
00154 } // fn equals
00155 
00156 
00157 bool 
00158 IpAddress::equals(unsigned long value) {
00159     return (m_ipAddr == value);
00160 } // fn equals
00161 
00162 
00170 bool 
00171 IpAddress::equals(const char* value) {
00172     IpAddress tmpIp(value);
00173     return (m_ipAddr == tmpIp.m_ipAddr);
00174 } // fn equals
00175 
00176 
00177 bool 
00178 IpAddress::equals(string value) {
00179     IpAddress tmpIp(value);
00180     return (m_ipAddr == tmpIp.m_ipAddr);
00181 } // fn equals
00182 
00183 
00184 bool
00185 IpAddress::equals(const struct in_addr value) {
00186 #ifdef WIN32
00187     return (m_ipAddr == value.S_un.S_addr);
00188 #else
00189     return (m_ipAddr == value.s_addr);
00190 #endif
00191 } // fn equals
00192 
00193 
00203 bool
00204 IpAddress::isLanIp() {
00205     stringstream s;
00206     s << *this;
00207 
00208 #ifdef BROKEN_STL
00209 /* jules:
00210  * this is necassary for some non stl complaint (gcc 2.95 and gcc 3!)
00211  * compilers that implements a old version of the STL :(
00212  * 
00213  */
00214     if (s.str().compare("192.168.", 0, 8) == 0) {
00215         return true;
00216     }
00217     else if (s.str().compare("172.16.", 0, 7) == 0) {
00218         return true;
00219     }
00220     else if (s.str().compare("10.", 0, 3) == 0) {
00221         return true;
00222     }
00223 #else
00224     if (s.str().compare(0, 8, "192.168.") == 0) {
00225         return true;
00226     }
00227     else if (s.str().compare(0, 7, "172.16.") == 0) {
00228         return true;
00229     }
00230     else if (s.str().compare(0, 3, "10.") == 0) {
00231         return true;
00232     }
00233 #endif
00234 
00235     return false;
00236 } // fn isLanIp
00237 
00238 
00242 bool
00243 IpAddress::isLoopback() {
00244     stringstream s;
00245     s << *this;
00246 #ifdef BROKEN_STL
00247     if (s.str().compare("127.0.0.1",0,9) == 0) {
00248 #else
00249     if (s.str().compare(0, 9, "127.0.0.1") == 0) {
00250 #endif
00251         return true;
00252     }
00253     return false;
00254 } // fn isLoopback
00255 
00256 
00260 bool
00261 IpAddress::isZero() {
00262     return (m_ipAddr == 0);
00263 } // fn isZero
00264 
00265 
00269 bool
00270 IpAddress::isInAddrAny() {
00271     return (m_ipAddr == INADDR_ANY);
00272 } // fn isInAddrAny
00273 
00274 
00278 bool
00279 IpAddress::read(std::istream& in) {
00280     char buf[MAX_LINE_LENGTH];
00281     in >> buf;
00282     setIpAddress(buf);
00283     return (!in.fail() && !in.eof());
00284 }
00285 
00286 
00290 void 
00291 IpAddress::toStream(std::ostream& out) {
00292     out << toCStr();
00293 } // fn toStream
00294 
00295 
00296 /*
00297  * Converts the IP address to a C string.  The memory used is internal to this
00298  * object - you may not modify the return value.  The value returned is the 
00299  * dotted decimal form of the IP address.
00300  *
00301  * @return A pointer to the C string.
00302  */
00303 const char* 
00304 IpAddress::toCStr() {
00305     unsigned long tmpIpAddr = htonl(m_ipAddr);
00306     unsigned char* c = (unsigned char *)&tmpIpAddr;
00307         OS_SPEC_SNPRINTF(m_cstr, MAX_IP_ADDR_STR, "%u.%u.%u.%u", c[3], c[2], c[1], c[0]);
00308     return m_cstr;
00309 } // fn toCStr
00310 
00311 

Generated at Thu Jul 11 13:31:50 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001