1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2007 INRIA
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;
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.
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
18 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20 #include "mac48-address.h"
22 #include "ns3/assert.h"
29 ATTRIBUTE_HELPER_CPP (Mac48Address);
31 #define ASCII_a (0x41)
32 #define ASCII_z (0x5a)
33 #define ASCII_A (0x61)
34 #define ASCII_Z (0x7a)
35 #define ASCII_COLON (0x3a)
36 #define ASCII_ZERO (0x30)
39 AsciiToLowCase (char c)
41 if (c >= ASCII_a && c <= ASCII_z) {
43 } else if (c >= ASCII_A && c <= ASCII_Z) {
44 return c + (ASCII_a - ASCII_A);
51 Mac48Address::Mac48Address ()
53 memset (m_address, 0, 6);
55 Mac48Address::Mac48Address (const char *str)
58 while (*str != 0 && i < 6)
61 while (*str != ASCII_COLON && *str != 0)
64 char low = AsciiToLowCase (*str);
67 byte |= low - ASCII_a + 10;
71 byte |= low - ASCII_ZERO;
86 Mac48Address::CopyFrom (const uint8_t buffer[6])
88 memcpy (m_address, buffer, 6);
91 Mac48Address::CopyTo (uint8_t buffer[6]) const
93 memcpy (buffer, m_address, 6);
97 Mac48Address::IsMatchingType (const Address &address)
99 return address.CheckCompatible (GetType (), 6);
101 Mac48Address::operator Address () const
106 Mac48Address::ConvertTo (void) const
108 return Address (GetType (), m_address, 6);
111 Mac48Address::ConvertFrom (const Address &address)
113 NS_ASSERT (address.CheckCompatible (GetType (), 6));
115 address.CopyTo (retval.m_address);
119 Mac48Address::Allocate (void)
121 static uint64_t id = 0;
123 Mac48Address address;
124 address.m_address[0] = (id >> 40) & 0xff;
125 address.m_address[1] = (id >> 32) & 0xff;
126 address.m_address[2] = (id >> 24) & 0xff;
127 address.m_address[3] = (id >> 16) & 0xff;
128 address.m_address[4] = (id >> 8) & 0xff;
129 address.m_address[5] = (id >> 0) & 0xff;
133 Mac48Address::GetType (void)
135 static uint8_t type = Address::Register ();
140 Mac48Address::IsBroadcast (void) const
142 return *this == GetBroadcast ();
145 Mac48Address::IsMulticast (void) const
153 prefix.CopyFrom (mcBuf);
154 return prefix == Mac48Address::GetMulticastPrefix ();
157 Mac48Address::IsGroup (void) const
159 return (m_address[0] & 0x01) == 0x01;
162 Mac48Address::GetBroadcast (void)
164 static Mac48Address broadcast = Mac48Address ("ff:ff:ff:ff:ff:ff");
168 Mac48Address::GetMulticastPrefix (void)
170 static Mac48Address multicast = Mac48Address ("01:00:5e:00:00:00");
174 Mac48Address::GetMulticast6Prefix (void)
176 static Mac48Address multicast = Mac48Address ("33:33:00:00:00:00");
180 Mac48Address::GetMulticast (Ipv4Address multicastGroup)
182 Mac48Address etherAddr = Mac48Address::GetMulticastPrefix ();
184 // We now have the multicast address in an abstract 48-bit container. We
185 // need to pull it out so we can play with it. When we're done, we have the
186 // high order bits in etherBuffer[0], etc.
188 uint8_t etherBuffer[6];
189 etherAddr.CopyTo (etherBuffer);
192 // Now we need to pull the raw bits out of the Ipv4 destination address.
195 multicastGroup.Serialize (ipBuffer);
198 // RFC 1112 says that an Ipv4 host group address is mapped to an EUI-48
199 // multicast address by placing the low-order 23-bits of the IP address into
200 // the low-order 23 bits of the Ethernet multicast address
201 // 01-00-5E-00-00-00 (hex).
203 etherBuffer[3] |= ipBuffer[1] & 0x7f;
204 etherBuffer[4] = ipBuffer[2];
205 etherBuffer[5] = ipBuffer[3];
208 // Now, etherBuffer has the desired ethernet multicast address. We have to
209 // suck these bits back into the Mac48Address,
212 result.CopyFrom (etherBuffer);
215 Mac48Address Mac48Address::GetMulticast(Ipv6Address addr)
217 Mac48Address etherAddr = Mac48Address::GetMulticast6Prefix();
218 uint8_t etherBuffer[6];
219 uint8_t ipBuffer[16];
221 /* a MAC multicast IPv6 address is like 33:33 and the four low bytes */
222 /* for 2001:db8::2fff:fe11:ac10 => 33:33:FE:11:AC:10 */
223 etherAddr.CopyTo (etherBuffer);
224 addr.Serialize (ipBuffer);
226 etherBuffer[2] = ipBuffer[12];
227 etherBuffer[3] = ipBuffer[13];
228 etherBuffer[4] = ipBuffer[14];
229 etherBuffer[5] = ipBuffer[15];
231 etherAddr.CopyFrom (etherBuffer);
236 bool operator == (const Mac48Address &a, const Mac48Address &b)
238 return memcmp (a.m_address, b.m_address, 6) == 0;
240 bool operator != (const Mac48Address &a, const Mac48Address &b)
245 bool operator < (const Mac48Address &a, const Mac48Address &b)
251 for (uint8_t i = 0; i < 6; i++)
253 if (a.m_address[i] < b.m_address[i])
257 else if (a.m_address[i] > b.m_address[i])
266 std::ostream& operator<< (std::ostream& os, const Mac48Address & address)
271 os.setf (std::ios::hex, std::ios::basefield);
273 for (uint8_t i=0; i < 5; i++)
275 os << std::setw(2) << (uint32_t)ad[i] << ":";
277 // Final byte not suffixed by ":"
278 os << std::setw(2) << (uint32_t)ad[5];
279 os.setf (std::ios::dec, std::ios::basefield);
285 AsInt (std::string v)
287 std::istringstream iss;
290 iss >> std::hex >> retval >> std::dec;
294 std::istream& operator>> (std::istream& is, Mac48Address & address)
299 std::string::size_type col = 0;
300 for (uint8_t i = 0; i < 6; ++i)
303 std::string::size_type next;
304 next = v.find (":", col);
305 if (next == std::string::npos)
307 tmp = v.substr (col, v.size ()-col);
308 address.m_address[i] = AsInt (tmp);
313 tmp = v.substr (col, next-col);
314 address.m_address[i] = AsInt (tmp);