1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 /* 3 * Copyright (c) 2008 University of Washington 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation; 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * 18 */ 19 20 #ifndef IPV4_INTERFACE_ADDRESS_H 21 #define IPV4_INTERFACE_ADDRESS_H 22 23 #include <stdint.h> 24 #include <ostream> 25 #include "ns3/ipv4-address.h" 26 27 namespace ns3 { 28 29 /** 30 * \ingroup address 31 * \ingroup ipv4 32 * 33 * \brief a class to store IPv4 address information on an interface 34 * 35 * Corresponds to Linux struct in_ifaddr. A list of these addresses 36 * is stored in Ipv4Interface. This class is modelled after how current 37 * Linux handles IP aliasing for IPv4. Notably, aliasing of IPv4 38 * interfaces (e.g., "eth0:1") is not used, and instead an interface 39 * is assigned possibly multiple addresses, with each address being 40 * classified as being primary and secondary. See the iproute2 41 * documentation for this distinction. 42 */ 43 class Ipv4InterfaceAddress 44 { 45 public: 46 /** 47 * \enum InterfaceAddressScope_e 48 * \brief Address scope. 49 */ 50 enum InterfaceAddressScope_e { 51 HOST, 52 LINK, 53 GLOBAL 54 }; 55 56 Ipv4InterfaceAddress (); 57 58 /** 59 * \brief Configure local address, mask and broadcast address 60 * \param local the local address 61 * \param mask the network mask 62 */ 63 Ipv4InterfaceAddress (Ipv4Address local, Ipv4Mask mask); 64 /** 65 * Copy constructor 66 * \param o the object to copy 67 */ 68 Ipv4InterfaceAddress (const Ipv4InterfaceAddress &o); 69 70 /** 71 * \brief Set local address 72 * \param local the address 73 * 74 * \note Functionally identical to `Ipv4InterfaceAddress::SetAddress`. 75 * The method corresponds to the linux variable in_ifaddr.ifa_local 76 * `Ipv4InterfaceAddress::SetAddress` is to be preferred. 77 */ 78 void SetLocal (Ipv4Address local); 79 80 /** 81 * \brief Set local address 82 * \param address the address 83 * 84 * \note Functially identical to `Ipv4InterfaceAddress::SetLocal`. 85 * This function is consistent with `Ipv6InterfaceAddress::SetAddress`. 86 */ 87 void SetAddress (Ipv4Address address); 88 89 /** 90 * \brief Get the local address 91 * \returns the local address 92 * 93 * \note Functionally identical to `Ipv4InterfaceAddress::GetAddress`. 94 * The method corresponds to the linux variable in_ifaddr.ifa_local 95 * `Ipv4InterfaceAddress::GetAddress` is to be preferred. 96 */ 97 Ipv4Address GetLocal (void) const; 98 99 /** 100 * \brief Get the local address 101 * \returns the local address 102 * 103 * \note Functially identical to `Ipv4InterfaceAddress::GetLocal`. 104 * This function is consistent with `Ipv6InterfaceAddress::GetAddress`. 105 */ 106 Ipv4Address GetAddress (void) const; 107 108 /** 109 * \brief Set the network mask 110 * \param mask the network mask 111 */ 112 void SetMask (Ipv4Mask mask); 113 /** 114 * \brief Get the network mask 115 * \returns the network mask 116 */ 117 Ipv4Mask GetMask (void) const; 118 /** 119 * \brief Set the broadcast address 120 * \param broadcast the broadcast address 121 */ 122 void SetBroadcast (Ipv4Address broadcast); 123 /** 124 * \brief Get the broadcast address 125 * \returns the broadcast address 126 */ 127 Ipv4Address GetBroadcast (void) const; 128 129 /** 130 * \brief Set the scope. 131 * \param scope the scope of address 132 */ 133 void SetScope (Ipv4InterfaceAddress::InterfaceAddressScope_e scope); 134 135 /** 136 * \brief Get address scope. 137 * \return scope 138 */ 139 Ipv4InterfaceAddress::InterfaceAddressScope_e GetScope (void) const; 140 141 /** 142 * \brief Checks if the address is in the same subnet. 143 * \param b the address to check 144 * \return true if the address is in the same subnet. 145 */ 146 bool IsInSameSubnet (const Ipv4Address b) const; 147 148 /** 149 * \brief Check if the address is a secondary address 150 * 151 * Secondary address is used for multihoming 152 * \returns true if the address is secondary 153 */ 154 bool IsSecondary (void) const; 155 156 /** 157 * \brief Make the address secondary (used for multihoming) 158 */ 159 void SetSecondary (void); 160 /** 161 * \brief Make the address primary 162 */ 163 void SetPrimary (void); 164 165 private: 166 167 Ipv4Address m_local; //!< Interface address 168 // Note: m_peer may be added in future when necessary 169 // Ipv4Address m_peer; // Peer destination address (in Linux: m_address) 170 Ipv4Mask m_mask; //!< Network mask 171 Ipv4Address m_broadcast; //!< Broadcast address 172 173 InterfaceAddressScope_e m_scope; //!< Address scope 174 bool m_secondary; //!< For use in multihoming 175 176 /** 177 * \brief Equal to operator. 178 * 179 * \param a the first operand 180 * \param b the first operand 181 * \returns true if the operands are equal 182 */ 183 friend bool operator == (Ipv4InterfaceAddress const &a, Ipv4InterfaceAddress const &b); 184 185 /** 186 * \brief Not equal to operator. 187 * 188 * \param a the first operand 189 * \param b the first operand 190 * \returns true if the operands are not equal 191 */ 192 friend bool operator != (Ipv4InterfaceAddress const &a, Ipv4InterfaceAddress const &b); 193 }; 194 195 /** 196 * \brief Stream insertion operator. 197 * 198 * \param os the reference to the output stream 199 * \param addr the Ipv4InterfaceAddress 200 * \returns the reference to the output stream 201 */ 202 std::ostream& operator<< (std::ostream& os, const Ipv4InterfaceAddress &addr); 203 204 inline bool operator == (const Ipv4InterfaceAddress &a, const Ipv4InterfaceAddress &b) 205 { 206 return (a.m_local == b.m_local && a.m_mask == b.m_mask && 207 a.m_broadcast == b.m_broadcast && a.m_scope == b.m_scope && a.m_secondary == b.m_secondary); 208 } 209 inline bool operator != (const Ipv4InterfaceAddress &a, const Ipv4InterfaceAddress &b) 210 { 211 return (a.m_local != b.m_local || a.m_mask != b.m_mask || 212 a.m_broadcast != b.m_broadcast || a.m_scope != b.m_scope || a.m_secondary != b.m_secondary); 213 } 214 215 216 } // namespace ns3 217 218 #endif /* IPV4_ADDRESS_H */ 219