00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2007-2008 Louis Pasteur University 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: Sebastien Vincent <vincent@clarinet.u-strasbg.fr> 00019 */ 00020 00021 #ifndef IPV6_ADDRESS_H 00022 #define IPV6_ADDRESS_H 00023 00024 #include <stdint.h> 00025 #include <string.h> 00026 00027 #include <ostream> 00028 00029 #include "address.h" 00030 #include "ns3/attribute-helper.h" 00031 00032 namespace ns3 { 00033 00034 class Ipv6Prefix; 00035 class Mac48Address; 00036 00037 /** 00038 * \class Ipv6Address 00039 * \brief Describes an IPv6 address. 00040 * \see Ipv6Prefix 00041 */ 00042 class Ipv6Address 00043 { 00044 public : 00045 /** 00046 * \brief Default constructor. 00047 */ 00048 Ipv6Address (); 00049 00050 /** 00051 * \brief Constructs an Ipv6Address by parsing the input C-string. 00052 * \param address the C-string containing the IPv6 address (e.g. 2001:660:4701::1). 00053 */ 00054 Ipv6Address (char const* address); 00055 00056 /** 00057 * \brief Constructs an Ipv6Address by using the input 16 bytes. 00058 * \param address the 128-bit address 00059 * \warning the parameter must point on a 16 bytes integer array! 00060 */ 00061 Ipv6Address (uint8_t address[16]); 00062 00063 /** 00064 * \brief Copy constructor. 00065 * \param addr Ipv6Address object 00066 */ 00067 Ipv6Address (Ipv6Address const & addr); 00068 00069 /** 00070 * \brief Copy constructor. 00071 * \param addr Ipv6Address pointer 00072 */ 00073 Ipv6Address (Ipv6Address const* addr); 00074 00075 /** 00076 * \brief Destructor. 00077 */ 00078 ~Ipv6Address (); 00079 00080 /** 00081 * \brief Sets an Ipv6Address by parsing the input C-string. 00082 * \param address the C-string containing the IPv6 address (e.g. 2001:660:4701::1). 00083 */ 00084 void Set (char const* address); 00085 00086 /** 00087 * \brief Set an Ipv6Address by using the input 16 bytes. 00088 * 00089 * \param address the 128-bit address 00090 * \warning the parameter must point on a 16 bytes integer array! 00091 */ 00092 void Set (uint8_t address[16]); 00093 00094 /** 00095 * \brief Comparison operation between two Ipv6Addresses. 00096 * 00097 * \param other the IPv6 address to which to compare thisaddress 00098 * \return true if the addresses are equal, false otherwise 00099 */ 00100 bool IsEqual (const Ipv6Address& other) const; 00101 00102 /** 00103 * \brief Serialize this address to a 16-byte buffer. 00104 * \param buf the output buffer to which this address gets overwritter with this 00105 * Ipv6Address 00106 */ 00107 void Serialize (uint8_t buf[16]) const; 00108 00109 /** 00110 * \brief Deserialize this address. 00111 * \param buf buffer to read address from 00112 * \return an Ipv6Address 00113 */ 00114 static Ipv6Address Deserialize (const uint8_t buf[16]); 00115 00116 /** 00117 * \brief Make the solicited IPv6 address. 00118 * \param addr the IPv6 address 00119 * \return Solicited IPv6 address 00120 */ 00121 static Ipv6Address MakeSolicitedAddress (Ipv6Address addr); 00122 00123 /** 00124 * \brief Make the autoconfigured IPv6 address with Mac48Address. 00125 * \param addr the MAC address (48 bits). 00126 * \param prefix the IPv6 prefix 00127 * \return autoconfigured IPv6 address 00128 */ 00129 static Ipv6Address MakeAutoconfiguredAddress (Mac48Address addr, Ipv6Address prefix); 00130 00131 /** 00132 * \brief Make the autoconfigured link-local IPv6 address with Mac48Address. 00133 * \param mac the MAC address (48 bits). 00134 * \return autoconfigured link-local IPv6 address 00135 */ 00136 static Ipv6Address MakeAutoconfiguredLinkLocalAddress (Mac48Address mac); 00137 00138 /** 00139 * \brief Print this address to the given output stream. 00140 * 00141 * The print format is in the typical "2001:660:4701::1". 00142 * \param os the output stream to which this Ipv6Address is printed 00143 */ 00144 void Print (std::ostream& os) const; 00145 00146 /** 00147 * \brief If the IPv6 address is localhost (::1). 00148 * \return true if localhost, false otherwise 00149 */ 00150 bool IsLocalhost () const; 00151 00152 /** 00153 * \brief If the IPv6 address is multicast (ff00::/8). 00154 * \return true if multicast, false otherwise 00155 */ 00156 bool IsMulticast () const; 00157 00158 /** 00159 * \brief If the IPv6 address is "all nodes multicast" (ff02::1/8). 00160 * \return true if "all nodes multicast", false otherwise 00161 */ 00162 bool IsAllNodesMulticast () const; 00163 00164 /** 00165 * \brief If the IPv6 address is "all routers multicast" (ff02::2/8). 00166 * \return true if "all routers multicast", false otherwise 00167 */ 00168 bool IsAllRoutersMulticast () const; 00169 00170 /** 00171 * \brief If the IPv6 address is "all hosts multicast" (ff02::3/8). 00172 * \return true if "all hosts multicast", false otherwise 00173 */ 00174 bool IsAllHostsMulticast () const; 00175 00176 /** 00177 * \brief If the IPv6 address is a link-local address (fe80::/64). 00178 * \return true if the address is link-local, false otherwise 00179 */ 00180 bool IsLinkLocal () const; 00181 00182 /** 00183 * \brief If the IPv6 address is a Solicited multicast address. 00184 * \return true if it is, false otherwise 00185 */ 00186 bool IsSolicitedMulticast () const; 00187 00188 /** 00189 * \brief If the IPv6 address is the "Any" address. 00190 * \return true if it is, false otherwise 00191 */ 00192 bool IsAny () const; 00193 00194 /** 00195 * \brief Combine this address with a prefix. 00196 * \param prefix a IPv6 prefix 00197 * \return an IPv6 address that is this address combined 00198 * (bitwise AND) with a prefix, yielding an IPv6 network address. 00199 */ 00200 Ipv6Address CombinePrefix (Ipv6Prefix const & prefix); 00201 00202 /** 00203 * \brief If the Address matches the type. 00204 * \param address other address 00205 * \return true if the type matches, false otherwise 00206 */ 00207 static bool IsMatchingType (const Address& address); 00208 00209 /** 00210 * \brief Convert to Address object 00211 */ 00212 operator Address () const; 00213 00214 /** 00215 * \brief Convert the Address object into an Ipv6Address one. 00216 * \return an Ipv6Address 00217 */ 00218 static Ipv6Address ConvertFrom (const Address& address); 00219 00220 /** 00221 * \brief Get the 0 (::) Ipv6Address. 00222 * \return the :: Ipv6Address representation 00223 */ 00224 static Ipv6Address GetZero (); 00225 00226 /** 00227 * \brief Get the "any" (::) Ipv6Address. 00228 * \return the "any" (::) Ipv6Address 00229 */ 00230 static Ipv6Address GetAny (); 00231 00232 /** 00233 * \brief Get the "all nodes multicast" address. 00234 * \return the "ff02::2/8" Ipv6Address representation 00235 */ 00236 static Ipv6Address GetAllNodesMulticast (); 00237 00238 /** 00239 * \brief Get the "all routers multicast" address. 00240 * \return the "ff02::2/8" Ipv6Address representation 00241 */ 00242 static Ipv6Address GetAllRoutersMulticast (); 00243 00244 /** 00245 * \brief Get the "all hosts multicast" address. 00246 * \return the "ff02::3/8" Ipv6Address representation 00247 */ 00248 static Ipv6Address GetAllHostsMulticast (); 00249 00250 /** 00251 * \brief Get the loopback address. 00252 * \return the "::1/128" Ipv6Address representation. 00253 */ 00254 static Ipv6Address GetLoopback (); 00255 00256 /** 00257 * \brief Get the bytes corresponding to the address. 00258 * \param buf buffer to store the data 00259 * \return bytes of the address 00260 */ 00261 void GetBytes (uint8_t buf[16]) const; 00262 00263 private: 00264 /** 00265 * \brief convert the IPv6Address object to an Address object. 00266 * \return the Address object corresponding to this object. 00267 */ 00268 Address ConvertTo (void) const; 00269 00270 /** 00271 * \brief Return the Type of address. 00272 * \return type of address 00273 */ 00274 static uint8_t GetType (void); 00275 00276 /** 00277 * \brief The address representation on 128 bits (16 bytes). 00278 */ 00279 uint8_t m_address[16]; 00280 00281 friend bool operator == (Ipv6Address const &a, Ipv6Address const &b); 00282 friend bool operator != (Ipv6Address const &a, Ipv6Address const &b); 00283 friend bool operator < (Ipv6Address const &a, Ipv6Address const &b); 00284 }; 00285 00286 /** 00287 * \class Ipv6Prefix 00288 * \brief Describes an IPv6 prefix. It is just a bitmask like Ipv4Mask. 00289 * \see Ipv6Address 00290 */ 00291 class Ipv6Prefix 00292 { 00293 public: 00294 /** 00295 * \brief Default constructor. 00296 */ 00297 Ipv6Prefix (); 00298 00299 /** 00300 * \brief Constructs an Ipv6Prefix by using the input 16 bytes. 00301 * \param prefix the 128-bit prefix 00302 */ 00303 Ipv6Prefix (uint8_t prefix[16]); 00304 00305 /** 00306 * \brief Constructs an Ipv6Prefix by using the input string. 00307 * \param prefix the 128-bit prefix 00308 */ 00309 Ipv6Prefix (char const* prefix); 00310 00311 /** 00312 * \brief Constructs an Ipv6Prefix by using the input number of bits. 00313 * \param prefix number of bits of the prefix (0 - 128) 00314 * \note A valid number of bits is between 0 and 128). 00315 */ 00316 Ipv6Prefix (uint8_t prefix); 00317 00318 /** 00319 * \brief Copy constructor. 00320 * \param prefix Ipv6Prefix object 00321 */ 00322 Ipv6Prefix (Ipv6Prefix const& prefix); 00323 00324 /** 00325 * \brief Copy constructor. 00326 * \param prefix Ipv6Prefix pointer 00327 */ 00328 Ipv6Prefix (Ipv6Prefix const* prefix); 00329 00330 /** 00331 * \brief Destructor. 00332 */ 00333 ~Ipv6Prefix (); 00334 00335 /** 00336 * \brief If the Address match the type. 00337 * \param a a first address 00338 * \param b a second address 00339 * \return true if the type match, false otherwise 00340 */ 00341 bool IsMatch (Ipv6Address a, Ipv6Address b) const; 00342 00343 /** 00344 * \brief Get the bytes corresponding to the prefix. 00345 * \param buf buffer to store the data 00346 */ 00347 void GetBytes (uint8_t buf[16]) const; 00348 00349 /** 00350 * \brief Comparison operation between two Ipv6Prefix. 00351 * \param other the IPv6 prefix to which to compare this prefix 00352 * \return true if the prefixes are equal, false otherwise 00353 */ 00354 bool IsEqual (const Ipv6Prefix& other) const; 00355 00356 /** 00357 * \brief Print this address to the given output stream. 00358 * 00359 * The print format is in the typicall "2001:660:4701::1". 00360 * \param os the output stream to which this Ipv6Address is printed 00361 */ 00362 void Print (std::ostream &os) const; 00363 00364 /** 00365 * \brief Get the loopback prefix ( /128). 00366 * \return a Ipv6Prefix corresponding to loopback prefix 00367 */ 00368 static Ipv6Prefix GetLoopback (); 00369 00370 /** 00371 * \brief Get the zero prefix ( /0). 00372 * \return an Ipv6Prefix 00373 */ 00374 static Ipv6Prefix GetZero (); 00375 00376 private: 00377 /** 00378 * \brief The prefix representation. 00379 */ 00380 uint8_t m_prefix[16]; 00381 }; 00382 00383 /** 00384 * \class ns3::Ipv6AddressValue 00385 * \brief hold objects of type ns3::Ipv6Address 00386 */ 00387 ATTRIBUTE_HELPER_HEADER (Ipv6Address); 00388 00389 /** 00390 * \class ns3::Ipv6PrefixValue 00391 * \brief hold objects of type ns3::Ipv6Prefix 00392 */ 00393 ATTRIBUTE_HELPER_HEADER (Ipv6Prefix); 00394 00395 std::ostream& operator << (std::ostream& os, Ipv6Address const& address); 00396 std::ostream& operator<< (std::ostream& os, Ipv6Prefix const& prefix); 00397 std::istream & operator >> (std::istream &is, Ipv6Address &address); 00398 std::istream & operator >> (std::istream &is, Ipv6Prefix &prefix); 00399 00400 inline bool operator == (const Ipv6Address& a, const Ipv6Address& b) 00401 { 00402 return (!memcmp (a.m_address, b.m_address, 16)); 00403 } 00404 00405 inline bool operator != (const Ipv6Address& a, const Ipv6Address& b) 00406 { 00407 return memcmp (a.m_address, b.m_address, 16); 00408 } 00409 00410 inline bool operator < (const Ipv6Address& a, const Ipv6Address& b) 00411 { 00412 return (memcmp (a.m_address, b.m_address, 16) < 0); 00413 } 00414 00415 class Ipv6AddressHash : public std::unary_function<Ipv6Address, size_t> 00416 { 00417 public: 00418 size_t operator() (Ipv6Address const &x) const; 00419 }; 00420 00421 bool operator == (Ipv6Prefix const &a, Ipv6Prefix const &b); 00422 bool operator != (Ipv6Prefix const &a, Ipv6Prefix const &b); 00423 00424 } /* namespace ns3 */ 00425 00426 #endif /* IPV6_ADDRESS_H */ 00427