A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
mac64-address.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2007 INRIA
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
*/
20
#include "
mac64-address.h
"
21
#include "ns3/address.h"
22
#include "ns3/assert.h"
23
#include "ns3/log.h"
24
#include <iomanip>
25
#include <iostream>
26
#include <cstring>
27
28
namespace
ns3
{
29
30
NS_LOG_COMPONENT_DEFINE
(
"Mac64Address"
);
31
32
ATTRIBUTE_HELPER_CPP
(
Mac64Address
);
33
34
#define ASCII_a (0x41)
35
#define ASCII_z (0x5a)
36
#define ASCII_A (0x61)
37
#define ASCII_Z (0x7a)
38
#define ASCII_COLON (0x3a)
39
#define ASCII_ZERO (0x30)
40
46
static
char
47
AsciiToLowCase
(
char
c)
48
{
49
NS_LOG_FUNCTION
(c);
50
if
(c >=
ASCII_a
&& c <=
ASCII_z
) {
51
return
c;
52
}
else
if
(c >=
ASCII_A
&& c <=
ASCII_Z
) {
53
return
c + (
ASCII_a
-
ASCII_A
);
54
}
else
{
55
return
c;
56
}
57
}
58
59
60
Mac64Address::Mac64Address
()
61
{
62
NS_LOG_FUNCTION
(
this
);
63
std::memset (
m_address
, 0, 8);
64
}
65
Mac64Address::Mac64Address
(
const
char
*str)
66
{
67
NS_LOG_FUNCTION
(
this
<< str);
68
int
i = 0;
69
while
(*str != 0 && i < 8)
70
{
71
uint8_t
byte
= 0;
72
while
(*str !=
ASCII_COLON
&& *str != 0)
73
{
74
byte
<<= 4;
75
char
low =
AsciiToLowCase
(*str);
76
if
(low >=
ASCII_a
)
77
{
78
byte
|= low -
ASCII_a
+ 10;
79
}
80
else
81
{
82
byte
|= low -
ASCII_ZERO
;
83
}
84
str++;
85
}
86
m_address
[i] = byte;
87
i++;
88
if
(*str == 0)
89
{
90
break
;
91
}
92
str++;
93
}
94
NS_ASSERT
(i == 8);
95
}
96
void
97
Mac64Address::CopyFrom
(
const
uint8_t buffer[8])
98
{
99
NS_LOG_FUNCTION
(
this
<< &buffer);
100
std::memcpy (
m_address
, buffer, 8);
101
}
102
void
103
Mac64Address::CopyTo
(uint8_t buffer[8])
const
104
{
105
NS_LOG_FUNCTION
(
this
<< &buffer);
106
std::memcpy (buffer,
m_address
, 8);
107
}
108
109
bool
110
Mac64Address::IsMatchingType
(
const
Address
&
address
)
111
{
112
NS_LOG_FUNCTION
(&
address
);
113
return
address
.CheckCompatible (
GetType
(), 8);
114
}
115
Mac64Address::operator
Address
()
const
116
{
117
return
ConvertTo ();
118
}
119
Mac64Address
120
Mac64Address::ConvertFrom
(
const
Address
&
address
)
121
{
122
NS_LOG_FUNCTION
(
address
);
123
NS_ASSERT
(
address
.CheckCompatible (
GetType
(), 8));
124
Mac64Address
retval;
125
address
.CopyTo (retval.
m_address
);
126
return
retval;
127
}
128
129
Address
130
Mac64Address::ConvertTo
(
void
)
const
131
{
132
NS_LOG_FUNCTION
(
this
);
133
return
Address
(
GetType
(),
m_address
, 8);
134
}
135
136
Mac64Address
137
Mac64Address::Allocate
(
void
)
138
{
139
NS_LOG_FUNCTION_NOARGS
();
140
static
uint64_t
id
= 0;
141
id
++;
142
Mac64Address
address
;
143
address
.m_address[0] = (
id
>> 56) & 0xff;
144
address
.m_address[1] = (
id
>> 48) & 0xff;
145
address
.m_address[2] = (
id
>> 40) & 0xff;
146
address
.m_address[3] = (
id
>> 32) & 0xff;
147
address
.m_address[4] = (
id
>> 24) & 0xff;
148
address
.m_address[5] = (
id
>> 16) & 0xff;
149
address
.m_address[6] = (
id
>> 8) & 0xff;
150
address
.m_address[7] = (
id
>> 0) & 0xff;
151
return
address
;
152
}
153
uint8_t
154
Mac64Address::GetType
(
void
)
155
{
156
NS_LOG_FUNCTION_NOARGS
();
157
static
uint8_t type =
Address::Register
();
158
return
type;
159
}
160
161
std::ostream&
operator<<
(std::ostream& os,
const
Mac64Address
&
address
)
162
{
163
uint8_t ad[8];
164
address
.CopyTo (ad);
165
166
os.setf (std::ios::hex, std::ios::basefield);
167
os.fill (
'0'
);
168
for
(uint8_t i=0; i < 7; i++)
169
{
170
os << std::setw (2) << (uint32_t)ad[i] <<
":"
;
171
}
172
// Final byte not suffixed by ":"
173
os << std::setw (2) << (uint32_t)ad[7];
174
os.setf (std::ios::dec, std::ios::basefield);
175
os.fill (
' '
);
176
return
os;
177
}
178
179
std::istream&
operator>>
(std::istream& is,
Mac64Address
&
address
)
180
{
181
std::string v;
182
is >> v;
183
184
std::string::size_type col = 0;
185
for
(uint8_t i = 0; i < 8; ++i)
186
{
187
std::string tmp;
188
std::string::size_type next;
189
next = v.find (
":"
, col);
190
if
(next == std::string::npos)
191
{
192
tmp = v.substr (col, v.size ()-col);
193
address
.m_address[i] = strtoul (tmp.c_str(), 0, 16);
194
break
;
195
}
196
else
197
{
198
tmp = v.substr (col, next-col);
199
address
.m_address[i] = strtoul (tmp.c_str(), 0, 16);
200
col = next + 1;
201
}
202
}
203
return
is;
204
}
205
206
}
// namespace ns3
ASCII_ZERO
#define ASCII_ZERO
Definition:
mac64-address.cc:39
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:205
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition:
assert.h:67
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Mac64Address::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition:
mac64-address.cc:110
ns3::Mac64Address::ConvertFrom
static Mac64Address ConvertFrom(const Address &address)
Definition:
mac64-address.cc:120
mac64-address.h
ns3::Address::Register
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition:
address.cc:138
ns3::Mac64Address::CopyTo
void CopyTo(uint8_t buffer[8]) const
Definition:
mac64-address.cc:103
ns3::Mac64Address
an EUI-64 address
Definition:
mac64-address.h:44
ASCII_A
#define ASCII_A
Definition:
mac64-address.cc:36
ns3::Address
a polymophic address class
Definition:
address.h:91
ATTRIBUTE_HELPER_CPP
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
Definition:
attribute-helper.h:412
first.address
address
Definition:
first.py:44
ns3::Mac64Address::ConvertTo
Address ConvertTo(void) const
Definition:
mac64-address.cc:130
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition:
log-macros-enabled.h:209
ns3::Mac64Address::Allocate
static Mac64Address Allocate(void)
Allocate a new Mac64Address.
Definition:
mac64-address.cc:137
ASCII_a
#define ASCII_a
Definition:
mac64-address.cc:34
ns3::Mac64Address::m_address
uint8_t m_address[8]
address value
Definition:
mac64-address.h:150
ns3::AsciiToLowCase
static char AsciiToLowCase(char c)
Converts a char to lower case.
Definition:
mac16-address.cc:48
ns3::Mac64Address::Mac64Address
Mac64Address()
Definition:
mac64-address.cc:60
ns3::Mac64Address::GetType
static uint8_t GetType(void)
Return the Type of address.
Definition:
mac64-address.cc:154
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition:
log-macros-enabled.h:244
ASCII_COLON
#define ASCII_COLON
Definition:
mac64-address.cc:38
ASCII_Z
#define ASCII_Z
Definition:
mac64-address.cc:37
ASCII_z
#define ASCII_z
Definition:
mac64-address.cc:35
ns3::operator<<
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition:
angles.cc:137
ns3::Mac64Address::CopyFrom
void CopyFrom(const uint8_t buffer[8])
Definition:
mac64-address.cc:97
ns3::operator>>
std::istream & operator>>(std::istream &is, Angles &a)
Definition:
angles.cc:160
src
network
utils
mac64-address.cc
Generated on Fri Oct 1 2021 17:03:30 for ns-3 by
1.8.20