ENUt
|
00001 /* 00002 00003 This file is a part of ENUt, a library containing network 00004 programming utilities. 00005 00006 Copyright (C) 2006-2008 Hasselt University - Expertise Centre for 00007 Digital Media (EDM) (http://www.edm.uhasselt.be) 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 00022 USA 00023 00024 */ 00025 00030 #ifndef NUT_IPV4ADDRESS_H 00031 00032 #define NUT_IPV4ADDRESS_H 00033 00034 #include "nutconfig.h" 00035 #include "networklayeraddress.h" 00036 #include "nuttypes.h" 00037 00038 #if !(defined(WIN32) || defined(_WIN32_WCE)) 00039 #include <sys/socket.h> 00040 #include <netinet/in.h> 00041 #include <arpa/inet.h> 00042 #endif // !(WIN32 || _WIN32_WCE) 00043 00044 namespace nut 00045 { 00046 00048 class ENUT_IMPORTEXPORT IPv4Address : public NetworkLayerAddress 00049 { 00050 public: 00052 IPv4Address(uint8_t ip[4]) : NetworkLayerAddress(IPv4) { m_ip = ((uint32_t)ip[3]) | (((uint32_t)ip[2]) << 8) | (((uint32_t)ip[1]) << 16) | (((uint32_t)ip[0]) << 24); } 00053 00055 IPv4Address(uint8_t ip0, uint8_t ip1, uint8_t ip2, uint8_t ip3) : NetworkLayerAddress(IPv4) 00056 { m_ip = ((uint32_t)ip3) | (((uint32_t)ip2) << 8) | (((uint32_t)ip1) << 16) | (((uint32_t)ip0) << 24); } 00057 00061 IPv4Address(uint32_t ip = 0, bool hostByteOrder = true) : NetworkLayerAddress(IPv4) 00062 { if (hostByteOrder) m_ip = ip; else m_ip = ntohl(ip); } 00063 ~IPv4Address() { } 00064 00066 uint32_t getAddress() const { return m_ip; } 00067 00069 uint32_t getAddressNBO() const { return htonl(m_ip); } 00070 00075 bool setAddress(const std::string &addressString); 00076 00077 NetworkLayerAddress *createCopy() const { return new IPv4Address(m_ip); } 00078 bool isSameAddress(const NetworkLayerAddress &address) const; 00079 std::string getAddressString() const; 00080 private: 00081 uint32_t m_ip; 00082 }; 00083 00084 inline bool IPv4Address::isSameAddress(const NetworkLayerAddress &address) const 00085 { 00086 if (address.getProtocol() != IPv4) 00087 return false; 00088 00089 const IPv4Address &addr = (const IPv4Address &)address; 00090 00091 if (addr.m_ip != m_ip) 00092 return false; 00093 return true; 00094 } 00095 00096 inline bool IPv4Address::setAddress(const std::string &addressString) 00097 { 00098 uint32_t ip; 00099 00100 ip = inet_addr(addressString.c_str()); 00101 if (ip == INADDR_NONE) 00102 return false; 00103 m_ip = ntohl(ip); 00104 return true; 00105 } 00106 00107 } // end namespace 00108 00109 #endif // NUT_IPV4ADDRESS_H 00110