00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2005 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 00021 #ifndef IPV4_ADDRESS_H 00022 #define IPV4_ADDRESS_H 00023 00024 #include <stdint.h> 00025 #include <ostream> 00026 #include "address.h" 00027 #include "ns3/attribute-helper.h" 00028 00029 namespace ns3 { 00030 00031 class Ipv4Mask; 00032 00033 /** 00034 * \ingroup address 00035 * 00036 * \brief Ipv4 addresses are stored in host order in this class. 00037 */ 00038 class Ipv4Address { 00039 public: 00040 Ipv4Address (); 00041 /** 00042 * input address is in host order. 00043 * \param address The host order 32-bit address 00044 */ 00045 explicit Ipv4Address (uint32_t address); 00046 /** 00047 * \brief Constructs an Ipv4Address by parsing a the input C-string 00048 * 00049 * Input address is in format: 00050 * hhh.xxx.xxx.lll 00051 * where h is the high byte and l the 00052 * low byte 00053 * \param address C-string containing the address as described above 00054 */ 00055 Ipv4Address (char const *address); 00056 /** 00057 * Get the host-order 32-bit IP address 00058 * \return the host-order 32-bit IP address 00059 */ 00060 uint32_t Get (void) const; 00061 /** 00062 * input address is in host order. 00063 * \param address The host order 32-bit address 00064 */ 00065 void Set (uint32_t address); 00066 /** 00067 * \brief Sets an Ipv4Address by parsing a the input C-string 00068 * 00069 * Input address is in format: 00070 * hhh.xxx.xxx.lll 00071 * where h is the high byte and l the 00072 * low byte 00073 * \param address C-string containing the address as described above 00074 */ 00075 void Set (char const *address); 00076 /** 00077 * \brief Comparison operation between two Ipv4Addresses 00078 * \param other address to which to compare this address 00079 * \return True if the addresses are equal. False otherwise. 00080 */ 00081 bool IsEqual (const Ipv4Address &other) const 00082 { 00083 return m_address == other.m_address; 00084 } 00085 /** 00086 * Serialize this address to a 4-byte buffer 00087 * 00088 * \param buf output buffer to which this address gets overwritten with this 00089 * Ipv4Address 00090 */ 00091 void Serialize (uint8_t buf[4]) const; 00092 /** 00093 * \param buf buffer to read address from 00094 * \return an Ipv4Address 00095 * 00096 * The input address is expected to be in network byte order format. 00097 */ 00098 static Ipv4Address Deserialize (const uint8_t buf[4]); 00099 /** 00100 * \brief Print this address to the given output stream 00101 * 00102 * The print format is in the typical "192.168.1.1" 00103 * \param os The output stream to which this Ipv4Address is printed 00104 */ 00105 void Print (std::ostream &os) const; 00106 /** 00107 * \return true if address is 255.255.255.255; false otherwise 00108 */ 00109 bool IsBroadcast (void) const; 00110 /** 00111 * \return true only if address is in the range 224.0.0.0 - 239.255.255.255 00112 */ 00113 bool IsMulticast (void) const; 00114 /** 00115 * \brief Combine this address with a network mask 00116 * 00117 * This method returns an IPv4 address that is this address combined 00118 * (bitwise and) with a network mask, yielding an IPv4 network 00119 * address. 00120 * 00121 * \param mask a network mask 00122 */ 00123 Ipv4Address CombineMask (Ipv4Mask const &mask) const; 00124 /** 00125 * \brief Generate subnet-directed broadcast address corresponding to mask 00126 * 00127 * The subnet-directed broadcast address has the host bits set to all 00128 * ones. If this method is called with a mask of 255.255.255.255, 00129 * (i.e., the address is a /32 address), the program will assert, since 00130 * there is no subnet associated with a /32 address. 00131 * 00132 * \param mask a network mask 00133 */ 00134 Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const; 00135 /** 00136 * \brief Generate subnet-directed broadcast address corresponding to mask 00137 * 00138 * The subnet-directed broadcast address has the host bits set to all 00139 * ones. If this method is called with a mask of 255.255.255.255, 00140 * (i.e., the address is a /32 address), the program will assert, since 00141 * there is no subnet associated with a /32 address. 00142 * 00143 * \param mask a network mask 00144 * \return true if the address, when combined with the input mask, has all 00145 * of its host bits set to one 00146 */ 00147 bool IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const; 00148 /** 00149 * \param address an address to compare type with 00150 * 00151 * \return true if the type of the address stored internally 00152 * is compatible with the type of the input address, false otherwise. 00153 */ 00154 static bool IsMatchingType (const Address &address); 00155 /** 00156 * Convert an instance of this class to a polymorphic Address instance. 00157 * 00158 * \return a new Address instance 00159 */ 00160 operator Address () const; 00161 /** 00162 * \param address a polymorphic address 00163 * \return a new Ipv4Address from the polymorphic address 00164 * 00165 * This function performs a type check and asserts if the 00166 * type of the input address is not compatible with an 00167 * Ipv4Address. 00168 */ 00169 static Ipv4Address ConvertFrom (const Address &address); 00170 /** 00171 * \return the 0.0.0.0 address 00172 */ 00173 static Ipv4Address GetZero (void); 00174 /** 00175 * \return the 0.0.0.0 address 00176 */ 00177 static Ipv4Address GetAny (void); 00178 /** 00179 * \return the 255.255.255.255 address 00180 */ 00181 static Ipv4Address GetBroadcast (void); 00182 /** 00183 * \return the 127.0.0.1 address 00184 */ 00185 static Ipv4Address GetLoopback (void); 00186 00187 private: 00188 Address ConvertTo (void) const; 00189 static uint8_t GetType (void); 00190 uint32_t m_address; 00191 00192 friend bool operator == (Ipv4Address const &a, Ipv4Address const &b); 00193 friend bool operator != (Ipv4Address const &a, Ipv4Address const &b); 00194 friend bool operator < (Ipv4Address const &addrA, Ipv4Address const &addrB); 00195 }; 00196 00197 /** 00198 * \ingroup address 00199 * 00200 * \brief a class to represent an Ipv4 address mask 00201 */ 00202 class Ipv4Mask { 00203 public: 00204 Ipv4Mask (); 00205 Ipv4Mask (uint32_t mask); 00206 Ipv4Mask (char const *mask); 00207 /** 00208 * \param a first address to compare 00209 * \param b second address to compare 00210 * \return true if both addresses are equal in their masked bits, 00211 * corresponding to this mask 00212 */ 00213 bool IsMatch (Ipv4Address a, Ipv4Address b) const; 00214 /** 00215 * \param other a mask to compare 00216 * \return true if the mask equals the mask passed as input parameter 00217 */ 00218 bool IsEqual (Ipv4Mask other) const; 00219 /** 00220 * Get the host-order 32-bit IP mask 00221 * \return the host-order 32-bit IP mask 00222 */ 00223 uint32_t Get (void) const; 00224 /** 00225 * input mask is in host order. 00226 * \param mask The host order 32-bit mask 00227 */ 00228 void Set (uint32_t mask); 00229 /** 00230 * \brief Return the inverse mask in host order. 00231 */ 00232 uint32_t GetInverse (void) const; 00233 /** 00234 * \brief Print this mask to the given output stream 00235 * 00236 * The print format is in the typical "255.255.255.0" 00237 * \param os The output stream to which this Ipv4Address is printed 00238 */ 00239 void Print (std::ostream &os) const; 00240 /** 00241 * \return the 255.0.0.0 mask corresponding to a typical loopback address 00242 */ 00243 static Ipv4Mask GetLoopback (void); 00244 /** 00245 * \return the 0.0.0.0 mask 00246 */ 00247 static Ipv4Mask GetZero (void); 00248 /** 00249 * \return the 255.255.255.255 mask 00250 */ 00251 static Ipv4Mask GetOnes (void); 00252 00253 private: 00254 uint32_t m_mask; 00255 }; 00256 00257 /** 00258 * \class ns3::Ipv4AddressValue 00259 * \brief hold objects of type ns3::Ipv4Address 00260 */ 00261 /** 00262 * \class ns3::Ipv4MaskValue 00263 * \brief hold objects of type ns3::Ipv4Mask 00264 */ 00265 00266 ATTRIBUTE_HELPER_HEADER (Ipv4Address); 00267 ATTRIBUTE_HELPER_HEADER (Ipv4Mask); 00268 00269 std::ostream& operator<< (std::ostream& os, Ipv4Address const& address); 00270 std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask); 00271 std::istream & operator >> (std::istream &is, Ipv4Address &address); 00272 std::istream & operator >> (std::istream &is, Ipv4Mask &mask); 00273 00274 inline bool operator == (const Ipv4Address &a, const Ipv4Address &b) 00275 { 00276 return (a.m_address == b.m_address); 00277 } 00278 inline bool operator != (const Ipv4Address &a, const Ipv4Address &b) 00279 { 00280 return (a.m_address != b.m_address); 00281 } 00282 inline bool operator < (const Ipv4Address &a, const Ipv4Address &b) 00283 { 00284 return (a.m_address < b.m_address); 00285 } 00286 00287 00288 class Ipv4AddressHash : public std::unary_function<Ipv4Address, size_t> { 00289 public: 00290 size_t operator()(Ipv4Address const &x) const; 00291 }; 00292 00293 bool operator == (Ipv4Mask const &a, Ipv4Mask const &b); 00294 bool operator != (Ipv4Mask const &a, Ipv4Mask const &b); 00295 00296 } // namespace ns3 00297 00298 #endif /* IPV4_ADDRESS_H */