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::
72operator Address() const
73{
74 return ConvertTo();
75}
76
79{
80 NS_LOG_FUNCTION(address);
81 NS_ASSERT(address.CheckCompatible(GetType(), 2));
82 Mac16Address retval;
83 address.CopyTo(retval.m_address);
84 return retval;
85}
86
89{
90 NS_LOG_FUNCTION(this);
91 return Address(GetType(), m_address, 2);
92}
93
94uint16_t
96{
97 uint16_t addr = m_address[1] & (0xFF);
98 addr |= (m_address[0] << 8) & (0xFF << 8);
99
100 return addr;
101}
102
105{
107
108 if (m_allocationIndex == 0)
109 {
111 }
112
114 Mac16Address address;
115 address.m_address[0] = (m_allocationIndex >> 8) & 0xff;
116 address.m_address[1] = m_allocationIndex & 0xff;
117 return address;
118}
119
120void
126
127uint8_t
129{
131
132 static uint8_t type = Address::Register();
133 return type;
134}
135
138{
140
141 static Mac16Address broadcast("ff:ff");
142 return broadcast;
143}
144
147{
148 NS_LOG_FUNCTION(address);
149
150 uint8_t ipv6AddrBuf[16];
151 address.GetBytes(ipv6AddrBuf);
152
153 uint8_t addrBuf[2];
154
155 addrBuf[0] = 0x80 | (ipv6AddrBuf[14] & 0x1F);
156 addrBuf[1] = ipv6AddrBuf[15];
157
158 Mac16Address multicastAddr;
159 multicastAddr.CopyFrom(addrBuf);
160
161 return multicastAddr;
162}
163
164bool
166{
167 NS_LOG_FUNCTION(this);
168 return m_address[0] == 0xff && m_address[1] == 0xff;
169}
170
171bool
173{
174 NS_LOG_FUNCTION(this);
175 uint8_t val = m_address[0];
176 val >>= 5;
177 return val == 0x4;
178}
179
180std::ostream&
181operator<<(std::ostream& os, const Mac16Address& address)
182{
183 uint8_t ad[2];
184 address.CopyTo(ad);
185
186 os.setf(std::ios::hex, std::ios::basefield);
187 os.fill('0');
188 for (uint8_t i = 0; i < 1; i++)
189 {
190 os << std::setw(2) << (uint32_t)ad[i] << ":";
191 }
192 // Final byte not suffixed by ":"
193 os << std::setw(2) << (uint32_t)ad[1];
194 os.setf(std::ios::dec, std::ios::basefield);
195 os.fill(' ');
196 return os;
197}
198
199std::istream&
200operator>>(std::istream& is, Mac16Address& address)
201{
202 std::string v;
203 is >> v;
204
205 std::string::size_type col = 0;
206 for (uint8_t i = 0; i < 2; ++i)
207 {
208 std::string tmp;
209 std::string::size_type next;
210 next = v.find(':', col);
211 if (next == std::string::npos)
212 {
213 tmp = v.substr(col, v.size() - col);
214 address.m_address[i] = std::stoul(tmp, nullptr, 16);
215 break;
216 }
217 else
218 {
219 tmp = v.substr(col, next - col);
220 address.m_address[i] = std::stoul(tmp, nullptr, 16);
221 col = next + 1;
222 }
223 }
224 return is;
225}
226
227} // 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: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