A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mac48-address.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA
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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "mac48-address.h"
21 #include "ns3/address.h"
22 #include "ns3/assert.h"
23 #include "ns3/log.h"
24 #include <iomanip>
25 #include <iostream>
26 #include <cstring>
27 
28 NS_LOG_COMPONENT_DEFINE ("Mac48Address");
29 
30 namespace ns3 {
31 
32 ATTRIBUTE_HELPER_CPP (Mac48Address);
33 
34 #define ASCII_a (0x41)
35 #define ASCII_z (0x5a)
36 #define ASCII_A (0x61)
37 #define ASCII_Z (0x7a)
38 #define ASCII_COLON (0x3a)
39 #define ASCII_ZERO (0x30)
40 
41 static char
43 {
44  NS_LOG_FUNCTION (c);
45  if (c >= ASCII_a && c <= ASCII_z) {
46  return c;
47  } else if (c >= ASCII_A && c <= ASCII_Z) {
48  return c + (ASCII_a - ASCII_A);
49  } else {
50  return c;
51  }
52 }
53 
54 
56 {
57  NS_LOG_FUNCTION (this);
58  std::memset (m_address, 0, 6);
59 }
60 Mac48Address::Mac48Address (const char *str)
61 {
62  NS_LOG_FUNCTION (this << str);
63  int i = 0;
64  while (*str != 0 && i < 6)
65  {
66  uint8_t byte = 0;
67  while (*str != ASCII_COLON && *str != 0)
68  {
69  byte <<= 4;
70  char low = AsciiToLowCase (*str);
71  if (low >= ASCII_a)
72  {
73  byte |= low - ASCII_a + 10;
74  }
75  else
76  {
77  byte |= low - ASCII_ZERO;
78  }
79  str++;
80  }
81  m_address[i] = byte;
82  i++;
83  if (*str == 0)
84  {
85  break;
86  }
87  str++;
88  }
89  NS_ASSERT (i == 6);
90 }
91 void
92 Mac48Address::CopyFrom (const uint8_t buffer[6])
93 {
94  NS_LOG_FUNCTION (this << &buffer);
95  std::memcpy (m_address, buffer, 6);
96 }
97 void
98 Mac48Address::CopyTo (uint8_t buffer[6]) const
99 {
100  NS_LOG_FUNCTION (this << &buffer);
101  std::memcpy (buffer, m_address, 6);
102 }
103 
104 bool
106 {
107  NS_LOG_FUNCTION (&address);
108  return address.CheckCompatible (GetType (), 6);
109 }
110 Mac48Address::operator Address () const
111 {
112  return ConvertTo ();
113 }
114 Address
116 {
117  NS_LOG_FUNCTION (this);
118  return Address (GetType (), m_address, 6);
119 }
122 {
123  NS_LOG_FUNCTION (&address);
124  NS_ASSERT (address.CheckCompatible (GetType (), 6));
125  Mac48Address retval;
126  address.CopyTo (retval.m_address);
127  return retval;
128 }
131 {
133  static uint64_t id = 0;
134  id++;
136  address.m_address[0] = (id >> 40) & 0xff;
137  address.m_address[1] = (id >> 32) & 0xff;
138  address.m_address[2] = (id >> 24) & 0xff;
139  address.m_address[3] = (id >> 16) & 0xff;
140  address.m_address[4] = (id >> 8) & 0xff;
141  address.m_address[5] = (id >> 0) & 0xff;
142  return address;
143 }
144 uint8_t
146 {
148  static uint8_t type = Address::Register ();
149  return type;
150 }
151 
152 bool
154 {
155  NS_LOG_FUNCTION (this);
156  return *this == GetBroadcast ();
157 }
158 bool
160 {
161  NS_LOG_FUNCTION (this);
162  return (m_address[0] & 0x01) == 0x01;
163 }
166 {
168  static Mac48Address broadcast = Mac48Address ("ff:ff:ff:ff:ff:ff");
169  return broadcast;
170 }
173 {
175  static Mac48Address multicast = Mac48Address ("01:00:5e:00:00:00");
176  return multicast;
177 }
180 {
182  static Mac48Address multicast = Mac48Address ("33:33:00:00:00:00");
183  return multicast;
184 }
187 {
188  NS_LOG_FUNCTION (multicastGroup);
190  //
191  // We now have the multicast address in an abstract 48-bit container. We
192  // need to pull it out so we can play with it. When we're done, we have the
193  // high order bits in etherBuffer[0], etc.
194  //
195  uint8_t etherBuffer[6];
196  etherAddr.CopyTo (etherBuffer);
197 
198  //
199  // Now we need to pull the raw bits out of the Ipv4 destination address.
200  //
201  uint8_t ipBuffer[4];
202  multicastGroup.Serialize (ipBuffer);
203 
204  //
205  // RFC 1112 says that an Ipv4 host group address is mapped to an EUI-48
206  // multicast address by placing the low-order 23-bits of the IP address into
207  // the low-order 23 bits of the Ethernet multicast address
208  // 01-00-5E-00-00-00 (hex).
209  //
210  etherBuffer[3] |= ipBuffer[1] & 0x7f;
211  etherBuffer[4] = ipBuffer[2];
212  etherBuffer[5] = ipBuffer[3];
213 
214  //
215  // Now, etherBuffer has the desired ethernet multicast address. We have to
216  // suck these bits back into the Mac48Address,
217  //
218  Mac48Address result;
219  result.CopyFrom (etherBuffer);
220  return result;
221 }
223 {
224  NS_LOG_FUNCTION (addr);
226  uint8_t etherBuffer[6];
227  uint8_t ipBuffer[16];
228 
229  /* a MAC multicast IPv6 address is like 33:33 and the four low bytes */
230  /* for 2001:db8::2fff:fe11:ac10 => 33:33:FE:11:AC:10 */
231  etherAddr.CopyTo (etherBuffer);
232  addr.Serialize (ipBuffer);
233 
234  etherBuffer[2] = ipBuffer[12];
235  etherBuffer[3] = ipBuffer[13];
236  etherBuffer[4] = ipBuffer[14];
237  etherBuffer[5] = ipBuffer[15];
238 
239  etherAddr.CopyFrom (etherBuffer);
240 
241  return etherAddr;
242 }
243 
244 std::ostream& operator<< (std::ostream& os, const Mac48Address & address)
245 {
246  uint8_t ad[6];
247  address.CopyTo (ad);
248 
249  os.setf (std::ios::hex, std::ios::basefield);
250  os.fill ('0');
251  for (uint8_t i=0; i < 5; i++)
252  {
253  os << std::setw (2) << (uint32_t)ad[i] << ":";
254  }
255  // Final byte not suffixed by ":"
256  os << std::setw (2) << (uint32_t)ad[5];
257  os.setf (std::ios::dec, std::ios::basefield);
258  os.fill (' ');
259  return os;
260 }
261 
262 static uint8_t
263 AsInt (std::string v)
264 {
265  NS_LOG_FUNCTION (v);
266  std::istringstream iss;
267  iss.str (v);
268  uint8_t retval;
269  iss >> std::hex >> retval >> std::dec;
270  return retval;
271 }
272 
273 std::istream& operator>> (std::istream& is, Mac48Address & address)
274 {
275  std::string v;
276  is >> v;
277 
278  std::string::size_type col = 0;
279  for (uint8_t i = 0; i < 6; ++i)
280  {
281  std::string tmp;
282  std::string::size_type next;
283  next = v.find (":", col);
284  if (next == std::string::npos)
285  {
286  tmp = v.substr (col, v.size ()-col);
287  address.m_address[i] = AsInt (tmp);
288  break;
289  }
290  else
291  {
292  tmp = v.substr (col, next-col);
293  address.m_address[i] = AsInt (tmp);
294  col = next + 1;
295  }
296  }
297  return is;
298 }
299 
300 
301 } // namespace ns3
uint8_t m_address[6]
static bool IsMatchingType(const Address &address)
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:49
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
static Mac48Address GetMulticastPrefix(void)
#define NS_ASSERT(condition)
Definition: assert.h:64
bool IsBroadcast(void) const
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
static uint8_t AsInt(std::string v)
Definition: address.cc:252
#define ASCII_A
a polymophic address class
Definition: address.h:86
#define ASCII_Z
bool CheckCompatible(uint8_t type, uint8_t len) const
Definition: address.cc:122
static char AsciiToLowCase(char c)
void CopyTo(uint8_t buffer[6]) const
static Mac48Address Allocate(void)
Allocate a new Mac48Address.
static Mac48Address GetMulticast(Ipv4Address address)
#define ASCII_COLON
static Mac48Address GetBroadcast(void)
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
static Mac48Address ConvertFrom(const Address &address)
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
static uint8_t GetType(void)
#define ASCII_z
bool IsGroup(void) const
an EUI-48 address
Definition: mac48-address.h:41
void CopyFrom(const uint8_t buffer[6])
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Definition: address.cc:82
#define ASCII_a
Macro to make help make class an ns-3 attribute.
tuple address
Definition: first.py:37
ATTRIBUTE_HELPER_CPP(ObjectFactory)
static Mac48Address GetMulticast6Prefix(void)
Get the multicast prefix for IPv6 (33:33:00:00:00:00).
Address ConvertTo(void) const
NS_LOG_COMPONENT_DEFINE("Mac48Address")
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138
#define ASCII_ZERO
void Serialize(uint8_t buf[16]) const
Serialize this address to a 16-byte buffer.