00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ns3/assert.h"
00022 #include "address.h"
00023 #include <string.h>
00024 #include <iostream>
00025 #include <iomanip>
00026
00027 namespace ns3 {
00028
00029 Address::Address ()
00030 : m_type (0),
00031 m_len (0)
00032 {
00033
00034 }
00035
00036 Address::Address (uint8_t type, const uint8_t *buffer, uint8_t len)
00037 : m_type (type),
00038 m_len (len)
00039 {
00040 NS_ASSERT (m_len <= MAX_SIZE);
00041 memcpy (m_data, buffer, m_len);
00042 }
00043 Address::Address (const Address & address)
00044 : m_type (address.m_type),
00045 m_len (address.m_len)
00046 {
00047 NS_ASSERT (m_len <= MAX_SIZE);
00048 memcpy (m_data, address.m_data, m_len);
00049 }
00050 Address &
00051 Address::operator = (const Address &address)
00052 {
00053 NS_ASSERT (m_len <= MAX_SIZE);
00054 m_type = address.m_type;
00055 m_len = address.m_len;
00056 NS_ASSERT (m_len <= MAX_SIZE);
00057 memcpy (m_data, address.m_data, m_len);
00058 return *this;
00059 }
00060
00061 bool
00062 Address::IsInvalid (void) const
00063 {
00064 return m_len == 0 && m_type == 0;
00065 }
00066
00067 uint8_t
00068 Address::GetLength (void) const
00069 {
00070 NS_ASSERT (m_len <= MAX_SIZE);
00071 return m_len;
00072 }
00073 uint32_t
00074 Address::CopyTo (uint8_t buffer[MAX_SIZE]) const
00075 {
00076 NS_ASSERT (m_len <= MAX_SIZE);
00077 memcpy (buffer, m_data, m_len);
00078 return m_len;
00079 }
00080 uint32_t
00081 Address::CopyAllTo (uint8_t *buffer, uint8_t len) const
00082 {
00083 NS_ASSERT (len >= m_len + 2);
00084 buffer[0] = m_type;
00085 buffer[1] = m_len;
00086 memcpy (buffer + 2, m_data, m_len);
00087 return m_len + 2;
00088 }
00089
00090 uint32_t
00091 Address::CopyFrom (const uint8_t *buffer, uint8_t len)
00092 {
00093 NS_ASSERT (len <= MAX_SIZE);
00094 memcpy (m_data, buffer, len);
00095 m_len = len;
00096 return m_len;
00097 }
00098 uint32_t
00099 Address::CopyAllFrom (const uint8_t *buffer, uint8_t len)
00100 {
00101 NS_ASSERT (len >= 2);
00102 m_type = buffer[0];
00103 m_len = buffer[1];
00104
00105 NS_ASSERT (len >= m_len + 2);
00106 memcpy (m_data, buffer + 2, m_len);
00107 return m_len + 2;
00108 }
00109 bool
00110 Address::CheckCompatible (uint8_t type, uint8_t len) const
00111 {
00112 NS_ASSERT (len <= MAX_SIZE);
00113 return m_len == len && (m_type == type || m_type == 0);
00114 }
00115 bool
00116 Address::IsMatchingType (uint8_t type) const
00117 {
00118 return m_type == type;
00119 }
00120
00121 uint8_t
00122 Address::Register (void)
00123 {
00124 static uint8_t type = 1;
00125 type++;
00126 return type;
00127 }
00128
00129 uint32_t
00130 Address::GetSerializedSize (void) const
00131 {
00132 return 1 + 1 + m_len;
00133 }
00134
00135 void
00136 Address::Serialize (TagBuffer buffer) const
00137 {
00138 buffer.WriteU8 (m_type);
00139 buffer.WriteU8 (m_len);
00140 buffer.Write (m_data, m_len);
00141 }
00142
00143 void
00144 Address::Deserialize (TagBuffer buffer)
00145 {
00146 m_type = buffer.ReadU8 ();
00147 m_len = buffer.ReadU8 ();
00148 NS_ASSERT (m_len <= MAX_SIZE);
00149 buffer.Read (m_data, m_len);
00150 }
00151
00152 ATTRIBUTE_HELPER_CPP (Address);
00153
00154
00155 bool operator == (const Address &a, const Address &b)
00156 {
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 if (a.m_type != b.m_type &&
00167 a.m_type != 0 &&
00168 b.m_type != 0)
00169 {
00170 return false;
00171 }
00172 if (a.m_len != b.m_len)
00173 {
00174 return false;
00175 }
00176 return memcmp (a.m_data, b.m_data, a.m_len) == 0;
00177 }
00178 bool operator != (const Address &a, const Address &b)
00179 {
00180 return !(a == b);
00181 }
00182 bool operator < (const Address &a, const Address &b)
00183 {
00184 if (a.m_type < b.m_type)
00185 {
00186 return true;
00187 }
00188 else if (a.m_type > b.m_type)
00189 {
00190 return false;
00191 }
00192 if (a.m_len < b.m_len)
00193 {
00194 return true;
00195 }
00196 else if (a.m_len > b.m_len)
00197 {
00198 return false;
00199 }
00200 NS_ASSERT (a.GetLength() == b.GetLength());
00201 for (uint8_t i = 0; i < a.GetLength(); i++)
00202 {
00203 if (a.m_data[i] < b.m_data[i])
00204 {
00205 return true;
00206 }
00207 else if (a.m_data[i] > b.m_data[i])
00208 {
00209 return false;
00210 }
00211 }
00212 return false;
00213 }
00214
00215 std::ostream& operator<< (std::ostream& os, const Address & address)
00216 {
00217 os.setf (std::ios::hex, std::ios::basefield);
00218 os.fill('0');
00219 os << std::setw(2) << (uint32_t) address.m_type << "-" << std::setw(2) << (uint32_t) address.m_len << "-";
00220 for (uint8_t i = 0; i < (address.m_len-1); ++i)
00221 {
00222 os << std::setw(2) << (uint32_t)address.m_data[i] << ":";
00223 }
00224
00225 os << std::setw(2) << (uint32_t) address.m_data[address.m_len-1];
00226 os.setf (std::ios::dec, std::ios::basefield);
00227 os.fill(' ');
00228 return os;
00229 }
00230
00231 static uint8_t
00232 AsInt (std::string v)
00233 {
00234 std::istringstream iss;
00235 iss.str (v);
00236 uint32_t retval;
00237 iss >> std::hex >> retval >> std::dec;
00238 return retval;
00239 }
00240
00241 std::istream& operator>> (std::istream& is, Address & address)
00242 {
00243 std::string v;
00244 is >> v;
00245 std::string::size_type firstDash, secondDash;
00246 firstDash = v.find ("-");
00247 secondDash = v.find ("-", firstDash+1);
00248 std::string type = v.substr (0, firstDash-0);
00249 std::string len = v.substr (firstDash+1, secondDash-(firstDash+1));
00250
00251 address.m_type = AsInt (type);
00252 address.m_len = AsInt (len);
00253 NS_ASSERT (address.m_len <= Address::MAX_SIZE);
00254
00255 std::string::size_type col = secondDash + 1;
00256 for (uint8_t i = 0; i < address.m_len; ++i)
00257 {
00258 std::string tmp;
00259 std::string::size_type next;
00260 next = v.find (":", col);
00261 if (next == std::string::npos)
00262 {
00263 tmp = v.substr (col, v.size ()-col);
00264 address.m_data[i] = AsInt (tmp);
00265 break;
00266 }
00267 else
00268 {
00269 tmp = v.substr (col, next-col);
00270 address.m_data[i] = AsInt (tmp);
00271 col = next + 1;
00272 }
00273 }
00274 return is;
00275 }
00276
00277
00278
00279 }