A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
mac16-address.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 * Copyright (c) 2011 The Boeing Company
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9#include "mac16-address.h"
10
11#include "ns3/address.h"
12#include "ns3/assert.h"
13#include "ns3/log.h"
14#include "ns3/simulator.h"
15
16#include <cstring>
17#include <iomanip>
18#include <iostream>
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("Mac16Address");
24
26
28
30{
31 NS_LOG_FUNCTION(this << str);
32 NS_ASSERT_MSG(strlen(str) <= 5, "Mac16Address: illegal string (too long) " << str);
33
34 unsigned int bytes[2];
35 int charsRead = 0;
36
37 int i = sscanf(str, "%02x:%02x%n", bytes, bytes + 1, &charsRead);
38 NS_ASSERT_MSG(i == 2 && !str[charsRead], "Mac16Address: illegal string " << str);
39
40 std::copy(std::begin(bytes), std::end(bytes), std::begin(m_address));
41}
42
44{
45 NS_LOG_FUNCTION(this);
46 m_address[1] = addr & 0xFF;
47 m_address[0] = (addr >> 8) & 0xFF;
48}
49
50void
51Mac16Address::CopyFrom(const uint8_t buffer[2])
52{
53 NS_LOG_FUNCTION(this << &buffer);
54 memcpy(m_address, buffer, 2);
55}
56
57void
58Mac16Address::CopyTo(uint8_t buffer[2]) const
59{
60 NS_LOG_FUNCTION(this << &buffer);
61 memcpy(buffer, m_address, 2);
62}
63
64bool
66{
67 NS_LOG_FUNCTION(&address);
68 return address.CheckCompatible(GetType(), 2);
69}
70
71Mac16Address::operator Address() const
72{
73 return ConvertTo();
74}
75
78{
79 NS_LOG_FUNCTION(address);
80 NS_ASSERT(address.CheckCompatible(GetType(), 2));
81 Mac16Address retval;
82 address.CopyTo(retval.m_address);
83 return retval;
84}
85
88{
89 NS_LOG_FUNCTION(this);
90 return Address(GetType(), m_address, 2);
91}
92
93uint16_t
95{
96 uint16_t addr = m_address[1] & (0xFF);
97 addr |= (m_address[0] << 8) & (0xFF << 8);
98
99 return addr;
100}
101
104{
106
107 if (m_allocationIndex == 0)
108 {
110 }
111
113 Mac16Address address;
114 address.m_address[0] = (m_allocationIndex >> 8) & 0xff;
115 address.m_address[1] = m_allocationIndex & 0xff;
116 return address;
117}
118
119void
125
126uint8_t
128{
130
131 static uint8_t type = Address::Register();
132 return type;
133}
134
137{
139
140 static Mac16Address broadcast("ff:ff");
141 return broadcast;
142}
143
146{
147 NS_LOG_FUNCTION(address);
148
149 uint8_t ipv6AddrBuf[16];
150 address.GetBytes(ipv6AddrBuf);
151
152 uint8_t addrBuf[2];
153
154 addrBuf[0] = 0x80 | (ipv6AddrBuf[14] & 0x1F);
155 addrBuf[1] = ipv6AddrBuf[15];
156
157 Mac16Address multicastAddr;
158 multicastAddr.CopyFrom(addrBuf);
159
160 return multicastAddr;
161}
162
163bool
165{
166 NS_LOG_FUNCTION(this);
167 return m_address[0] == 0xff && m_address[1] == 0xff;
168}
169
170bool
172{
173 NS_LOG_FUNCTION(this);
174 uint8_t val = m_address[0];
175 val >>= 5;
176 return val == 0x4;
177}
178
179std::ostream&
180operator<<(std::ostream& os, const Mac16Address& address)
181{
182 uint8_t ad[2];
183 address.CopyTo(ad);
184
185 os.setf(std::ios::hex, std::ios::basefield);
186 os.fill('0');
187 for (uint8_t i = 0; i < 1; i++)
188 {
189 os << std::setw(2) << (uint32_t)ad[i] << ":";
190 }
191 // Final byte not suffixed by ":"
192 os << std::setw(2) << (uint32_t)ad[1];
193 os.setf(std::ios::dec, std::ios::basefield);
194 os.fill(' ');
195 return os;
196}
197
198std::istream&
199operator>>(std::istream& is, Mac16Address& address)
200{
201 std::string v;
202 is >> v;
203
204 std::string::size_type col = 0;
205 for (uint8_t i = 0; i < 2; ++i)
206 {
207 std::string tmp;
208 std::string::size_type next;
209 next = v.find(':', col);
210 if (next == std::string::npos)
211 {
212 tmp = v.substr(col, v.size() - col);
213 address.m_address[i] = std::stoul(tmp, nullptr, 16);
214 break;
215 }
216 else
217 {
218 tmp = v.substr(col, next - col);
219 address.m_address[i] = std::stoul(tmp, nullptr, 16);
220 col = next + 1;
221 }
222 }
223 return is;
224}
225
226} // 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
Describes an IPv6 address.
This class can contain 16 bit addresses.
static Mac16Address GetMulticast(Ipv6Address address)
Returns the multicast address associated with an IPv6 address according to RFC 4944 Section 9.
static bool IsMatchingType(const Address &address)
static uint64_t m_allocationIndex
Address allocation index.
static Mac16Address ConvertFrom(const Address &address)
Mac16Address()=default
uint16_t ConvertToInt() const
void CopyTo(uint8_t buffer[2]) const
static Mac16Address Allocate()
Allocate a new Mac16Address.
Address ConvertTo() const
void CopyFrom(const uint8_t buffer[2])
bool IsMulticast() const
Checks if the address is a multicast address according to RFC 4944 Section 9 (i.e....
uint8_t m_address[2]
Address value.
static void ResetAllocationIndex()
Reset the Mac16Address allocation index.
static Mac16Address GetBroadcast()
bool IsBroadcast() const
Checks if the address is a broadcast address according to 802.15.4 scheme (i.e., 0xFFFF).
static uint8_t GetType()
Return the Type of address.
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