A Discrete-Event Network Simulator
API
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
107void
108Mac64Address::CopyFrom(const uint8_t buffer[8])
109{
110 NS_LOG_FUNCTION(this << &buffer);
111 std::memcpy(m_address, buffer, 8);
112}
113
114void
115Mac64Address::CopyTo(uint8_t buffer[8]) const
116{
117 NS_LOG_FUNCTION(this << &buffer);
118 std::memcpy(buffer, m_address, 8);
119}
120
121bool
123{
125 return address.CheckCompatible(GetType(), 8);
126}
127
128Mac64Address::operator Address() const
129{
130 return ConvertTo();
131}
132
135{
137 NS_ASSERT(address.CheckCompatible(GetType(), 8));
138 Mac64Address retval;
139 address.CopyTo(retval.m_address);
140 return retval;
141}
142
145{
146 NS_LOG_FUNCTION(this);
147 return Address(GetType(), m_address, 8);
148}
149
152{
154
155 if (m_allocationIndex == 0)
156 {
158 }
159
162 address.m_address[0] = (m_allocationIndex >> 56) & 0xff;
163 address.m_address[1] = (m_allocationIndex >> 48) & 0xff;
164 address.m_address[2] = (m_allocationIndex >> 40) & 0xff;
165 address.m_address[3] = (m_allocationIndex >> 32) & 0xff;
166 address.m_address[4] = (m_allocationIndex >> 24) & 0xff;
167 address.m_address[5] = (m_allocationIndex >> 16) & 0xff;
168 address.m_address[6] = (m_allocationIndex >> 8) & 0xff;
169 address.m_address[7] = m_allocationIndex & 0xff;
170 return address;
171}
172
173void
175{
178}
179
180uint8_t
182{
184 static uint8_t type = Address::Register();
185 return type;
186}
187
188std::ostream&
189operator<<(std::ostream& os, const Mac64Address& address)
190{
191 uint8_t ad[8];
192 address.CopyTo(ad);
193
194 os.setf(std::ios::hex, std::ios::basefield);
195 os.fill('0');
196 for (uint8_t i = 0; i < 7; i++)
197 {
198 os << std::setw(2) << (uint32_t)ad[i] << ":";
199 }
200 // Final byte not suffixed by ":"
201 os << std::setw(2) << (uint32_t)ad[7];
202 os.setf(std::ios::dec, std::ios::basefield);
203 os.fill(' ');
204 return os;
205}
206
207std::istream&
208operator>>(std::istream& is, Mac64Address& address)
209{
210 std::string v;
211 is >> v;
212
213 std::string::size_type col = 0;
214 for (uint8_t i = 0; i < 8; ++i)
215 {
216 std::string tmp;
217 std::string::size_type next;
218 next = v.find(':', col);
219 if (next == std::string::npos)
220 {
221 tmp = v.substr(col, v.size() - col);
222 address.m_address[i] = std::stoul(tmp, nullptr, 16);
223 break;
224 }
225 else
226 {
227 tmp = v.substr(col, next - col);
228 address.m_address[i] = std::stoul(tmp, nullptr, 16);
229 col = next + 1;
230 }
231 }
232 return is;
233}
234
235} // namespace ns3
a polymophic address class
Definition: address.h:92
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
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 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 ",...
#define ASCII_Z
#define ASCII_A
#define ASCII_z
#define ASCII_COLON
#define ASCII_ZERO
#define ASCII_a
address
Definition: first.py:40
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
ATTRIBUTE_HELPER_CPP(Length)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
static char AsciiToLowCase(char c)
Converts a char to lower case.