A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
mac64-address.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#include "mac64-address.h"
20
21#include "ns3/address.h"
22#include "ns3/assert.h"
23#include "ns3/log.h"
24#include "ns3/simulator.h"
25
26#include <cstring>
27#include <iomanip>
28#include <iostream>
29
30namespace ns3
31{
32
33NS_LOG_COMPONENT_DEFINE("Mac64Address");
34
36
37#define ASCII_a (0x41)
38#define ASCII_z (0x5a)
39#define ASCII_A (0x61)
40#define ASCII_Z (0x7a)
41#define ASCII_COLON (0x3a)
42#define ASCII_ZERO (0x30)
43
49static char
51{
53 if (c >= ASCII_a && c <= ASCII_z)
54 {
55 return c;
56 }
57 else if (c >= ASCII_A && c <= ASCII_Z)
58 {
59 return c + (ASCII_a - ASCII_A);
60 }
61 else
62 {
63 return c;
64 }
65}
66
68
70{
71 NS_LOG_FUNCTION(this);
72 std::memset(m_address, 0, 8);
73}
74
76{
77 NS_LOG_FUNCTION(this << str);
78 int i = 0;
79 while (*str != 0 && i < 8)
80 {
81 uint8_t byte = 0;
82 while (*str != ASCII_COLON && *str != 0)
83 {
84 byte <<= 4;
85 char low = AsciiToLowCase(*str);
86 if (low >= ASCII_a)
87 {
88 byte |= low - ASCII_a + 10;
89 }
90 else
91 {
92 byte |= low - ASCII_ZERO;
93 }
94 str++;
95 }
96 m_address[i] = byte;
97 i++;
98 if (*str == 0)
99 {
100 break;
101 }
102 str++;
103 }
104 NS_ASSERT(i == 8);
105}
106
108{
109 NS_LOG_FUNCTION(this);
110 m_address[7] = addr & 0xFF;
111 m_address[6] = (addr >> 8) & 0xFF;
112 m_address[5] = (addr >> 16) & 0xFF;
113 m_address[4] = (addr >> 24) & 0xFF;
114 m_address[3] = (addr >> 32) & 0xFF;
115 m_address[2] = (addr >> 40) & 0xFF;
116 m_address[1] = (addr >> 48) & 0xFF;
117 m_address[0] = (addr >> 56) & 0xFF;
118}
119
120void
121Mac64Address::CopyFrom(const uint8_t buffer[8])
122{
123 NS_LOG_FUNCTION(this << &buffer);
124 std::memcpy(m_address, buffer, 8);
125}
126
127void
128Mac64Address::CopyTo(uint8_t buffer[8]) const
129{
130 NS_LOG_FUNCTION(this << &buffer);
131 std::memcpy(buffer, m_address, 8);
132}
133
134bool
136{
137 NS_LOG_FUNCTION(&address);
138 return address.CheckCompatible(GetType(), 8);
139}
140
141Mac64Address::operator Address() const
142{
143 return ConvertTo();
144}
145
148{
149 NS_LOG_FUNCTION(address);
150 NS_ASSERT(address.CheckCompatible(GetType(), 8));
151 Mac64Address retval;
152 address.CopyTo(retval.m_address);
153 return retval;
154}
155
158{
159 NS_LOG_FUNCTION(this);
160 return Address(GetType(), m_address, 8);
161}
162
163uint64_t
165{
166 uint64_t shift = 0xFF;
167 uint64_t addr = static_cast<uint64_t>(m_address[7]) & (shift);
168 addr |= (static_cast<uint64_t>(m_address[6]) << 8) & (shift << 8);
169 addr |= (static_cast<uint64_t>(m_address[5]) << 16) & (shift << 16);
170 addr |= (static_cast<uint64_t>(m_address[4]) << 24) & (shift << 24);
171
172 addr |= (static_cast<uint64_t>(m_address[3]) << 32) & (shift << 32);
173 addr |= (static_cast<uint64_t>(m_address[2]) << 40) & (shift << 40);
174 addr |= (static_cast<uint64_t>(m_address[1]) << 48) & (shift << 48);
175 addr |= (static_cast<uint64_t>(m_address[0]) << 56) & (shift << 56);
176
177 return addr;
178}
179
182{
184
185 if (m_allocationIndex == 0)
186 {
188 }
189
191 Mac64Address address;
192 address.m_address[0] = (m_allocationIndex >> 56) & 0xff;
193 address.m_address[1] = (m_allocationIndex >> 48) & 0xff;
194 address.m_address[2] = (m_allocationIndex >> 40) & 0xff;
195 address.m_address[3] = (m_allocationIndex >> 32) & 0xff;
196 address.m_address[4] = (m_allocationIndex >> 24) & 0xff;
197 address.m_address[5] = (m_allocationIndex >> 16) & 0xff;
198 address.m_address[6] = (m_allocationIndex >> 8) & 0xff;
199 address.m_address[7] = m_allocationIndex & 0xff;
200 return address;
201}
202
203void
205{
208}
209
210uint8_t
212{
214 static uint8_t type = Address::Register();
215 return type;
216}
217
218std::ostream&
219operator<<(std::ostream& os, const Mac64Address& address)
220{
221 uint8_t ad[8];
222 address.CopyTo(ad);
223
224 os.setf(std::ios::hex, std::ios::basefield);
225 os.fill('0');
226 for (uint8_t i = 0; i < 7; i++)
227 {
228 os << std::setw(2) << (uint32_t)ad[i] << ":";
229 }
230 // Final byte not suffixed by ":"
231 os << std::setw(2) << (uint32_t)ad[7];
232 os.setf(std::ios::dec, std::ios::basefield);
233 os.fill(' ');
234 return os;
235}
236
237std::istream&
238operator>>(std::istream& is, Mac64Address& address)
239{
240 std::string v;
241 is >> v;
242
243 std::string::size_type col = 0;
244 for (uint8_t i = 0; i < 8; ++i)
245 {
246 std::string tmp;
247 std::string::size_type next;
248 next = v.find(':', col);
249 if (next == std::string::npos)
250 {
251 tmp = v.substr(col, v.size() - col);
252 address.m_address[i] = std::stoul(tmp, nullptr, 16);
253 break;
254 }
255 else
256 {
257 tmp = v.substr(col, next - col);
258 address.m_address[i] = std::stoul(tmp, nullptr, 16);
259 col = next + 1;
260 }
261 }
262 return is;
263}
264
265} // namespace ns3
a polymophic address class
Definition: address.h:100
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition: address.cc:146
an EUI-64 address
Definition: mac64-address.h:46
uint8_t m_address[8]
address value
Address ConvertTo() const
uint64_t ConvertToInt() const
static bool IsMatchingType(const Address &address)
static Mac64Address Allocate()
Allocate a new Mac64Address.
void CopyFrom(const uint8_t buffer[8])
void CopyTo(uint8_t buffer[8]) const
static Mac64Address ConvertFrom(const Address &address)
static void ResetAllocationIndex()
Reset the Mac64Address allocation index.
static uint8_t GetType()
Return the Type of address.
static uint64_t m_allocationIndex
Address allocation index.
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition: simulator.h:625
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
static char AsciiToLowCase(char c)
Converts a char to lower case.
#define ASCII_Z
#define ASCII_A
#define ASCII_z
#define ASCII_COLON
#define ASCII_ZERO
#define ASCII_a