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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "mac64-address.h"
9
10#include "ns3/address.h"
11#include "ns3/assert.h"
12#include "ns3/log.h"
13#include "ns3/simulator.h"
14
15#include <cstring>
16#include <iomanip>
17#include <iostream>
18
19namespace ns3
20{
21
22NS_LOG_COMPONENT_DEFINE("Mac64Address");
23
25
27
29{
30 NS_LOG_FUNCTION(this << str);
31 NS_ASSERT_MSG(strlen(str) <= 23, "Mac64Address: illegal string (too long) " << str);
32
33 unsigned int bytes[8];
34 int charsRead = 0;
35
36 int i = sscanf(str,
37 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x%n",
38 bytes,
39 bytes + 1,
40 bytes + 2,
41 bytes + 3,
42 bytes + 4,
43 bytes + 5,
44 bytes + 6,
45 bytes + 7,
46 &charsRead);
47 NS_ASSERT_MSG(i == 8 && !str[charsRead], "Mac64Address: illegal string " << str);
48
49 std::copy(std::begin(bytes), std::end(bytes), std::begin(m_address));
50}
51
53{
54 NS_LOG_FUNCTION(this);
55 m_address[7] = addr & 0xFF;
56 m_address[6] = (addr >> 8) & 0xFF;
57 m_address[5] = (addr >> 16) & 0xFF;
58 m_address[4] = (addr >> 24) & 0xFF;
59 m_address[3] = (addr >> 32) & 0xFF;
60 m_address[2] = (addr >> 40) & 0xFF;
61 m_address[1] = (addr >> 48) & 0xFF;
62 m_address[0] = (addr >> 56) & 0xFF;
63}
64
65void
66Mac64Address::CopyFrom(const uint8_t buffer[8])
67{
68 NS_LOG_FUNCTION(this << &buffer);
69 std::memcpy(m_address, buffer, 8);
70}
71
72void
73Mac64Address::CopyTo(uint8_t buffer[8]) const
74{
75 NS_LOG_FUNCTION(this << &buffer);
76 std::memcpy(buffer, m_address, 8);
77}
78
79bool
81{
82 NS_LOG_FUNCTION(&address);
83 return address.CheckCompatible(GetType(), 8);
84}
85
86Mac64Address::
87operator Address() const
88{
89 return ConvertTo();
90}
91
94{
95 NS_LOG_FUNCTION(address);
96 NS_ASSERT(address.CheckCompatible(GetType(), 8));
97 Mac64Address retval;
98 address.CopyTo(retval.m_address);
99 return retval;
100}
101
104{
105 NS_LOG_FUNCTION(this);
106 return Address(GetType(), m_address, 8);
107}
108
109uint64_t
111{
112 uint64_t shift = 0xFF;
113 uint64_t addr = static_cast<uint64_t>(m_address[7]) & (shift);
114 addr |= (static_cast<uint64_t>(m_address[6]) << 8) & (shift << 8);
115 addr |= (static_cast<uint64_t>(m_address[5]) << 16) & (shift << 16);
116 addr |= (static_cast<uint64_t>(m_address[4]) << 24) & (shift << 24);
117
118 addr |= (static_cast<uint64_t>(m_address[3]) << 32) & (shift << 32);
119 addr |= (static_cast<uint64_t>(m_address[2]) << 40) & (shift << 40);
120 addr |= (static_cast<uint64_t>(m_address[1]) << 48) & (shift << 48);
121 addr |= (static_cast<uint64_t>(m_address[0]) << 56) & (shift << 56);
122
123 return addr;
124}
125
128{
130
131 if (m_allocationIndex == 0)
132 {
134 }
135
137 Mac64Address address;
138 address.m_address[0] = (m_allocationIndex >> 56) & 0xff;
139 address.m_address[1] = (m_allocationIndex >> 48) & 0xff;
140 address.m_address[2] = (m_allocationIndex >> 40) & 0xff;
141 address.m_address[3] = (m_allocationIndex >> 32) & 0xff;
142 address.m_address[4] = (m_allocationIndex >> 24) & 0xff;
143 address.m_address[5] = (m_allocationIndex >> 16) & 0xff;
144 address.m_address[6] = (m_allocationIndex >> 8) & 0xff;
145 address.m_address[7] = m_allocationIndex & 0xff;
146 return address;
147}
148
149void
155
156uint8_t
158{
160 static uint8_t type = Address::Register();
161 return type;
162}
163
164std::ostream&
165operator<<(std::ostream& os, const Mac64Address& address)
166{
167 uint8_t ad[8];
168 address.CopyTo(ad);
169
170 os.setf(std::ios::hex, std::ios::basefield);
171 os.fill('0');
172 for (uint8_t i = 0; i < 7; i++)
173 {
174 os << std::setw(2) << (uint32_t)ad[i] << ":";
175 }
176 // Final byte not suffixed by ":"
177 os << std::setw(2) << (uint32_t)ad[7];
178 os.setf(std::ios::dec, std::ios::basefield);
179 os.fill(' ');
180 return os;
181}
182
183std::istream&
184operator>>(std::istream& is, Mac64Address& address)
185{
186 std::string v;
187 is >> v;
188
189 std::string::size_type col = 0;
190 for (uint8_t i = 0; i < 8; ++i)
191 {
192 std::string tmp;
193 std::string::size_type next;
194 next = v.find(':', col);
195 if (next == std::string::npos)
196 {
197 tmp = v.substr(col, v.size() - col);
198 address.m_address[i] = std::stoul(tmp, nullptr, 16);
199 break;
200 }
201 else
202 {
203 tmp = v.substr(col, next - col);
204 address.m_address[i] = std::stoul(tmp, nullptr, 16);
205 col = next + 1;
206 }
207 }
208 return is;
209}
210
211} // namespace ns3
a polymophic address class
Definition address.h:90
static uint8_t Register()
Allocate a new type id for a new type of address.
Definition address.cc:135
an EUI-64 address
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.
Mac64Address()=default
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:612
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
#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:191
#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:148
std::istream & operator>>(std::istream &is, Angles &a)
Definition angles.cc:172