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::operator Address() const
87{
88 return ConvertTo();
89}
90
93{
94 NS_LOG_FUNCTION(address);
95 NS_ASSERT(address.CheckCompatible(GetType(), 8));
96 Mac64Address retval;
97 address.CopyTo(retval.m_address);
98 return retval;
99}
100
103{
104 NS_LOG_FUNCTION(this);
105 return Address(GetType(), m_address, 8);
106}
107
108uint64_t
110{
111 uint64_t shift = 0xFF;
112 uint64_t addr = static_cast<uint64_t>(m_address[7]) & (shift);
113 addr |= (static_cast<uint64_t>(m_address[6]) << 8) & (shift << 8);
114 addr |= (static_cast<uint64_t>(m_address[5]) << 16) & (shift << 16);
115 addr |= (static_cast<uint64_t>(m_address[4]) << 24) & (shift << 24);
116
117 addr |= (static_cast<uint64_t>(m_address[3]) << 32) & (shift << 32);
118 addr |= (static_cast<uint64_t>(m_address[2]) << 40) & (shift << 40);
119 addr |= (static_cast<uint64_t>(m_address[1]) << 48) & (shift << 48);
120 addr |= (static_cast<uint64_t>(m_address[0]) << 56) & (shift << 56);
121
122 return addr;
123}
124
127{
129
130 if (m_allocationIndex == 0)
131 {
133 }
134
136 Mac64Address address;
137 address.m_address[0] = (m_allocationIndex >> 56) & 0xff;
138 address.m_address[1] = (m_allocationIndex >> 48) & 0xff;
139 address.m_address[2] = (m_allocationIndex >> 40) & 0xff;
140 address.m_address[3] = (m_allocationIndex >> 32) & 0xff;
141 address.m_address[4] = (m_allocationIndex >> 24) & 0xff;
142 address.m_address[5] = (m_allocationIndex >> 16) & 0xff;
143 address.m_address[6] = (m_allocationIndex >> 8) & 0xff;
144 address.m_address[7] = m_allocationIndex & 0xff;
145 return address;
146}
147
148void
154
155uint8_t
157{
159 static uint8_t type = Address::Register();
160 return type;
161}
162
163std::ostream&
164operator<<(std::ostream& os, const Mac64Address& address)
165{
166 uint8_t ad[8];
167 address.CopyTo(ad);
168
169 os.setf(std::ios::hex, std::ios::basefield);
170 os.fill('0');
171 for (uint8_t i = 0; i < 7; i++)
172 {
173 os << std::setw(2) << (uint32_t)ad[i] << ":";
174 }
175 // Final byte not suffixed by ":"
176 os << std::setw(2) << (uint32_t)ad[7];
177 os.setf(std::ios::dec, std::ios::basefield);
178 os.fill(' ');
179 return os;
180}
181
182std::istream&
183operator>>(std::istream& is, Mac64Address& address)
184{
185 std::string v;
186 is >> v;
187
188 std::string::size_type col = 0;
189 for (uint8_t i = 0; i < 8; ++i)
190 {
191 std::string tmp;
192 std::string::size_type next;
193 next = v.find(':', col);
194 if (next == std::string::npos)
195 {
196 tmp = v.substr(col, v.size() - col);
197 address.m_address[i] = std::stoul(tmp, nullptr, 16);
198 break;
199 }
200 else
201 {
202 tmp = v.substr(col, next - col);
203 address.m_address[i] = std::stoul(tmp, nullptr, 16);
204 col = next + 1;
205 }
206 }
207 return is;
208}
209
210} // 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:611
#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