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
38
40{
41 NS_LOG_FUNCTION(this << str);
42 NS_ASSERT_MSG(strlen(str) <= 23, "Mac64Address: illegal string (too long) " << str);
43
44 unsigned int bytes[8];
45 int charsRead = 0;
46
47 int i = sscanf(str,
48 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x%n",
49 bytes,
50 bytes + 1,
51 bytes + 2,
52 bytes + 3,
53 bytes + 4,
54 bytes + 5,
55 bytes + 6,
56 bytes + 7,
57 &charsRead);
58 NS_ASSERT_MSG(i == 8 && !str[charsRead], "Mac64Address: illegal string " << str);
59
60 std::copy(std::begin(bytes), std::end(bytes), std::begin(m_address));
61}
62
64{
65 NS_LOG_FUNCTION(this);
66 m_address[7] = addr & 0xFF;
67 m_address[6] = (addr >> 8) & 0xFF;
68 m_address[5] = (addr >> 16) & 0xFF;
69 m_address[4] = (addr >> 24) & 0xFF;
70 m_address[3] = (addr >> 32) & 0xFF;
71 m_address[2] = (addr >> 40) & 0xFF;
72 m_address[1] = (addr >> 48) & 0xFF;
73 m_address[0] = (addr >> 56) & 0xFF;
74}
75
76void
77Mac64Address::CopyFrom(const uint8_t buffer[8])
78{
79 NS_LOG_FUNCTION(this << &buffer);
80 std::memcpy(m_address, buffer, 8);
81}
82
83void
84Mac64Address::CopyTo(uint8_t buffer[8]) const
85{
86 NS_LOG_FUNCTION(this << &buffer);
87 std::memcpy(buffer, m_address, 8);
88}
89
90bool
92{
93 NS_LOG_FUNCTION(&address);
94 return address.CheckCompatible(GetType(), 8);
95}
96
97Mac64Address::operator Address() const
98{
99 return ConvertTo();
100}
101
104{
105 NS_LOG_FUNCTION(address);
106 NS_ASSERT(address.CheckCompatible(GetType(), 8));
107 Mac64Address retval;
108 address.CopyTo(retval.m_address);
109 return retval;
110}
111
114{
115 NS_LOG_FUNCTION(this);
116 return Address(GetType(), m_address, 8);
117}
118
119uint64_t
121{
122 uint64_t shift = 0xFF;
123 uint64_t addr = static_cast<uint64_t>(m_address[7]) & (shift);
124 addr |= (static_cast<uint64_t>(m_address[6]) << 8) & (shift << 8);
125 addr |= (static_cast<uint64_t>(m_address[5]) << 16) & (shift << 16);
126 addr |= (static_cast<uint64_t>(m_address[4]) << 24) & (shift << 24);
127
128 addr |= (static_cast<uint64_t>(m_address[3]) << 32) & (shift << 32);
129 addr |= (static_cast<uint64_t>(m_address[2]) << 40) & (shift << 40);
130 addr |= (static_cast<uint64_t>(m_address[1]) << 48) & (shift << 48);
131 addr |= (static_cast<uint64_t>(m_address[0]) << 56) & (shift << 56);
132
133 return addr;
134}
135
138{
140
141 if (m_allocationIndex == 0)
142 {
144 }
145
147 Mac64Address address;
148 address.m_address[0] = (m_allocationIndex >> 56) & 0xff;
149 address.m_address[1] = (m_allocationIndex >> 48) & 0xff;
150 address.m_address[2] = (m_allocationIndex >> 40) & 0xff;
151 address.m_address[3] = (m_allocationIndex >> 32) & 0xff;
152 address.m_address[4] = (m_allocationIndex >> 24) & 0xff;
153 address.m_address[5] = (m_allocationIndex >> 16) & 0xff;
154 address.m_address[6] = (m_allocationIndex >> 8) & 0xff;
155 address.m_address[7] = m_allocationIndex & 0xff;
156 return address;
157}
158
159void
161{
164}
165
166uint8_t
168{
170 static uint8_t type = Address::Register();
171 return type;
172}
173
174std::ostream&
175operator<<(std::ostream& os, const Mac64Address& address)
176{
177 uint8_t ad[8];
178 address.CopyTo(ad);
179
180 os.setf(std::ios::hex, std::ios::basefield);
181 os.fill('0');
182 for (uint8_t i = 0; i < 7; i++)
183 {
184 os << std::setw(2) << (uint32_t)ad[i] << ":";
185 }
186 // Final byte not suffixed by ":"
187 os << std::setw(2) << (uint32_t)ad[7];
188 os.setf(std::ios::dec, std::ios::basefield);
189 os.fill(' ');
190 return os;
191}
192
193std::istream&
194operator>>(std::istream& is, Mac64Address& address)
195{
196 std::string v;
197 is >> v;
198
199 std::string::size_type col = 0;
200 for (uint8_t i = 0; i < 8; ++i)
201 {
202 std::string tmp;
203 std::string::size_type next;
204 next = v.find(':', col);
205 if (next == std::string::npos)
206 {
207 tmp = v.substr(col, v.size() - col);
208 address.m_address[i] = std::stoul(tmp, nullptr, 16);
209 break;
210 }
211 else
212 {
213 tmp = v.substr(col, next - col);
214 address.m_address[i] = std::stoul(tmp, nullptr, 16);
215 col = next + 1;
216 }
217 }
218 return is;
219}
220
221} // namespace ns3
a polymophic address class
Definition: address.h:101
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.
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:622
#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_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:86
#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:159
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183