A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
mac16-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
* Copyright (c) 2011 The Boeing Company
5
*
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License version 2 as
8
* published by the Free Software Foundation;
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
*
19
*/
20
21
#include "
mac16-address.h
"
22
#include "ns3/address.h"
23
#include "ns3/assert.h"
24
#include "ns3/log.h"
25
#include <iomanip>
26
#include <iostream>
27
#include <cstring>
28
29
NS_LOG_COMPONENT_DEFINE
(
"Mac16Address"
);
30
31
namespace
ns3 {
32
33
ATTRIBUTE_HELPER_CPP
(Mac16Address);
34
35
#define ASCII_a (0x41)
36
#define ASCII_z (0x5a)
37
#define ASCII_A (0x61)
38
#define ASCII_Z (0x7a)
39
#define ASCII_COLON (0x3a)
40
#define ASCII_ZERO (0x30)
41
42
static
char
43
AsciiToLowCase
(
char
c)
44
{
45
if
(c >=
ASCII_a
&& c <=
ASCII_z
) {
46
return
c;
47
}
else
if
(c >=
ASCII_A
&& c <=
ASCII_Z
) {
48
return
c + (
ASCII_a
-
ASCII_A
);
49
}
else
{
50
return
c;
51
}
52
}
53
54
55
Mac16Address::Mac16Address
()
56
{
57
NS_LOG_FUNCTION
(
this
);
58
memset (
m_address
, 0, 2);
59
}
60
61
Mac16Address::Mac16Address
(
const
char
*str)
62
{
63
NS_LOG_FUNCTION
(
this
<< str);
64
int
i = 0;
65
while
(*str != 0 && i < 2)
66
{
67
uint8_t byte = 0;
68
while
(*str !=
ASCII_COLON
&& *str != 0)
69
{
70
byte <<= 4;
71
char
low =
AsciiToLowCase
(*str);
72
if
(low >=
ASCII_a
)
73
{
74
byte |= low -
ASCII_a
+ 10;
75
}
76
else
77
{
78
byte |= low -
ASCII_ZERO
;
79
}
80
str++;
81
}
82
m_address
[i] = byte;
83
i++;
84
if
(*str == 0)
85
{
86
break
;
87
}
88
str++;
89
}
90
NS_ASSERT
(i == 2);
91
}
92
93
void
94
Mac16Address::CopyFrom
(
const
uint8_t buffer[2])
95
{
96
NS_LOG_FUNCTION
(
this
<< &buffer);
97
memcpy (
m_address
, buffer, 2);
98
}
99
void
100
Mac16Address::CopyTo
(uint8_t buffer[2])
const
101
{
102
NS_LOG_FUNCTION
(
this
<< &buffer);
103
memcpy (buffer,
m_address
, 2);
104
}
105
106
bool
107
Mac16Address::IsMatchingType
(
const
Address
&
address
)
108
{
109
NS_LOG_FUNCTION
(&address);
110
return
address.
CheckCompatible
(
GetType
(), 2);
111
}
112
113
Mac16Address::operator
Address
()
const
114
{
115
return
ConvertTo ();
116
}
117
118
Mac16Address
119
Mac16Address::ConvertFrom
(
const
Address
&
address
)
120
{
121
NS_LOG_FUNCTION
(address);
122
NS_ASSERT
(address.
CheckCompatible
(
GetType
(), 2));
123
Mac16Address
retval;
124
address.
CopyTo
(retval.m_address);
125
return
retval;
126
}
127
Address
128
Mac16Address::ConvertTo
(
void
)
const
129
{
130
NS_LOG_FUNCTION
(
this
);
131
return
Address
(
GetType
(),
m_address
, 2);
132
}
133
134
Mac16Address
135
Mac16Address::Allocate
(
void
)
136
{
137
NS_LOG_FUNCTION_NOARGS
();
138
static
uint64_t
id
= 0;
139
id
++;
140
Mac16Address
address
;
141
address.
m_address
[0] = (
id
>> 8) & 0xff;
142
address.
m_address
[1] = (
id
>> 0) & 0xff;
143
return
address
;
144
}
145
146
uint8_t
147
Mac16Address::GetType
(
void
)
148
{
149
NS_LOG_FUNCTION_NOARGS
();
150
static
uint8_t type =
Address::Register
();
151
return
type;
152
}
153
154
std::ostream &
operator<<
(std::ostream& os,
const
Mac16Address
&
address
)
155
{
156
uint8_t ad[2];
157
address.
CopyTo
(ad);
158
159
os.setf (std::ios::hex, std::ios::basefield);
160
os.fill (
'0'
);
161
for
(uint8_t i = 0; i < 1; i++)
162
{
163
os << std::setw (2) << (uint32_t)ad[i] <<
":"
;
164
}
165
// Final byte not suffixed by ":"
166
os << std::setw (2) << (uint32_t)ad[1];
167
os.setf (std::ios::dec, std::ios::basefield);
168
os.fill (
' '
);
169
return
os;
170
}
171
172
static
uint8_t
173
AsInt
(std::string v)
174
{
175
NS_LOG_FUNCTION
(v);
176
std::istringstream iss;
177
iss.str (v);
178
uint32_t retval;
179
iss >> std::hex >> retval >> std::dec;
180
return
retval;
181
}
182
183
std::istream&
operator>>
(std::istream& is,
Mac16Address
&
address
)
184
{
185
std::string v;
186
is >> v;
187
188
std::string::size_type col = 0;
189
for
(uint8_t i = 0; i < 2; ++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] =
AsInt
(tmp);
198
break
;
199
}
200
else
201
{
202
tmp = v.substr (col, next-col);
203
address.
m_address
[i] =
AsInt
(tmp);
204
col = next + 1;
205
}
206
}
207
return
is;
208
}
209
210
}
src
network
utils
mac16-address.cc
Generated on Fri Aug 30 2013 01:43:00 for ns-3 by
1.8.1.2