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
ipv4-address.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2005 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
21
#include <cstdlib>
22
#include "ns3/log.h"
23
#include "
ipv4-address.h
"
24
#include "ns3/assert.h"
25
26
NS_LOG_COMPONENT_DEFINE
(
"Ipv4Address"
);
27
28
namespace
ns3 {
29
30
#define ASCII_DOT (0x2e)
31
#define ASCII_ZERO (0x30)
32
#define ASCII_SLASH (0x2f)
33
34
static
uint32_t
35
AsciiToIpv4Host
(
char
const
*address)
36
{
37
NS_LOG_FUNCTION
(&address);
38
uint32_t host = 0;
39
while
(
true
)
40
{
41
uint8_t byte = 0;
42
while
(*address !=
ASCII_DOT
&& *address != 0)
43
{
44
byte *= 10;
45
byte += *address -
ASCII_ZERO
;
46
address++;
47
}
48
host <<= 8;
49
host |= byte;
50
if
(*address == 0)
51
{
52
break
;
53
}
54
address++;
55
}
56
return
host;
57
}
58
59
}
// namespace ns3
60
61
namespace
ns3 {
62
63
64
Ipv4Mask::Ipv4Mask
()
65
: m_mask (0x66666666)
66
{
67
NS_LOG_FUNCTION
(
this
);
68
}
69
70
Ipv4Mask::Ipv4Mask
(uint32_t mask)
71
: m_mask (mask)
72
{
73
NS_LOG_FUNCTION
(
this
<< mask);
74
}
75
76
Ipv4Mask::Ipv4Mask
(
char
const
*mask)
77
{
78
NS_LOG_FUNCTION
(
this
<< mask);
79
if
(*mask ==
ASCII_SLASH
)
80
{
81
uint32_t plen =
static_cast<
uint32_t
>
(std::atoi (++mask));
82
NS_ASSERT
(plen <= 32);
83
if
(plen > 0)
84
{
85
m_mask
= 0xffffffff << (32 - plen);
86
}
87
else
88
{
89
m_mask
= 0;
90
}
91
}
92
else
93
{
94
m_mask
=
AsciiToIpv4Host
(mask);
95
}
96
}
97
98
bool
99
Ipv4Mask::IsEqual
(
Ipv4Mask
other)
const
100
{
101
NS_LOG_FUNCTION
(
this
<< other);
102
if
(other.
m_mask
==
m_mask
) {
103
return
true
;
104
}
else
{
105
return
false
;
106
}
107
}
108
109
bool
110
Ipv4Mask::IsMatch
(
Ipv4Address
a,
Ipv4Address
b)
const
111
{
112
NS_LOG_FUNCTION
(
this
<< a << b);
113
if
((a.
Get
() &
m_mask
) == (b.
Get
() &
m_mask
)) {
114
return
true
;
115
}
else
{
116
return
false
;
117
}
118
}
119
120
uint32_t
121
Ipv4Mask::Get
(
void
)
const
122
{
123
NS_LOG_FUNCTION
(
this
);
124
return
m_mask
;
125
}
126
void
127
Ipv4Mask::Set
(uint32_t mask)
128
{
129
NS_LOG_FUNCTION
(
this
<< mask);
130
m_mask
= mask;
131
}
132
uint32_t
133
Ipv4Mask::GetInverse
(
void
)
const
134
{
135
NS_LOG_FUNCTION
(
this
);
136
return
~
m_mask
;
137
}
138
139
void
140
Ipv4Mask::Print
(std::ostream &os)
const
141
{
142
NS_LOG_FUNCTION
(
this
<< &os);
143
os << ((m_mask >> 24) & 0xff) <<
"."
144
<< ((
m_mask
>> 16) & 0xff) <<
"."
145
<< ((
m_mask
>> 8) & 0xff) <<
"."
146
<< ((
m_mask
>> 0) & 0xff);
147
}
148
149
150
Ipv4Mask
151
Ipv4Mask::GetLoopback
(
void
)
152
{
153
NS_LOG_FUNCTION_NOARGS
();
154
static
Ipv4Mask
loopback =
Ipv4Mask
(
"255.0.0.0"
);
155
return
loopback;
156
}
157
Ipv4Mask
158
Ipv4Mask::GetZero
(
void
)
159
{
160
NS_LOG_FUNCTION_NOARGS
();
161
static
Ipv4Mask
zero
=
Ipv4Mask
(
"0.0.0.0"
);
162
return
zero
;
163
}
164
Ipv4Mask
165
Ipv4Mask::GetOnes
(
void
)
166
{
167
NS_LOG_FUNCTION_NOARGS
();
168
static
Ipv4Mask
ones =
Ipv4Mask
(
"255.255.255.255"
);
169
return
ones;
170
}
171
172
uint16_t
173
Ipv4Mask::GetPrefixLength
(
void
)
const
174
{
175
NS_LOG_FUNCTION
(
this
);
176
uint16_t tmp = 0;
177
uint32_t mask =
m_mask
;
178
while
(mask != 0 )
179
{
180
mask = mask << 1;
181
tmp++;
182
}
183
return
tmp;
184
}
185
186
187
Ipv4Address::Ipv4Address
()
188
: m_address (0x66666666)
189
{
190
NS_LOG_FUNCTION
(
this
);
191
}
192
Ipv4Address::Ipv4Address
(uint32_t address)
193
{
194
NS_LOG_FUNCTION
(
this
<< address);
195
m_address
= address;
196
}
197
Ipv4Address::Ipv4Address
(
char
const
*address)
198
{
199
NS_LOG_FUNCTION
(
this
<< address);
200
m_address
=
AsciiToIpv4Host
(address);
201
}
202
203
uint32_t
204
Ipv4Address::Get
(
void
)
const
205
{
206
NS_LOG_FUNCTION
(
this
);
207
return
m_address
;
208
}
209
void
210
Ipv4Address::Set
(uint32_t address)
211
{
212
NS_LOG_FUNCTION
(
this
<< address);
213
m_address
= address;
214
}
215
void
216
Ipv4Address::Set
(
char
const
*address)
217
{
218
NS_LOG_FUNCTION
(
this
<< address);
219
m_address
=
AsciiToIpv4Host
(address);
220
}
221
222
Ipv4Address
223
Ipv4Address::CombineMask
(
Ipv4Mask
const
&mask)
const
224
{
225
NS_LOG_FUNCTION
(
this
<< mask);
226
return
Ipv4Address
(
Get
() & mask.
Get
());
227
}
228
229
Ipv4Address
230
Ipv4Address::GetSubnetDirectedBroadcast
(
Ipv4Mask
const
&mask)
const
231
{
232
NS_LOG_FUNCTION
(
this
<< mask);
233
if
(mask ==
Ipv4Mask::GetOnes
())
234
{
235
NS_ASSERT_MSG
(
false
,
"Trying to get subnet-directed broadcast address with an all-ones netmask"
);
236
}
237
return
Ipv4Address
(
Get
() | mask.
GetInverse
());
238
}
239
240
bool
241
Ipv4Address::IsSubnetDirectedBroadcast
(
Ipv4Mask
const
&mask)
const
242
{
243
NS_LOG_FUNCTION
(
this
<< mask);
244
if
(mask ==
Ipv4Mask::GetOnes
())
245
{
246
// If the mask is 255.255.255.255, there is no subnet directed
247
// broadcast for this address.
248
return
false
;
249
}
250
return
( (
Get
() | mask.
GetInverse
()) ==
Get
() );
251
}
252
253
bool
254
Ipv4Address::IsBroadcast
(
void
)
const
255
{
256
NS_LOG_FUNCTION
(
this
);
257
return
(
m_address
== 0xffffffffU);
258
}
259
260
bool
261
Ipv4Address::IsMulticast
(
void
)
const
262
{
263
//
264
// Multicast addresses are defined as ranging from 224.0.0.0 through
265
// 239.255.255.255 (which is E0000000 through EFFFFFFF in hex).
266
//
267
NS_LOG_FUNCTION
(
this
);
268
return
(
m_address
>= 0xe0000000 &&
m_address
<= 0xefffffff);
269
}
270
271
bool
272
Ipv4Address::IsLocalMulticast
(
void
)
const
273
{
274
NS_LOG_FUNCTION
(
this
);
275
// Link-Local multicast address is 224.0.0.0/24
276
return
(
m_address
& 0xffffff00) == 0xe0000000;
277
}
278
279
void
280
Ipv4Address::Serialize
(uint8_t buf[4])
const
281
{
282
NS_LOG_FUNCTION
(
this
<< &buf);
283
buf[0] = (
m_address
>> 24) & 0xff;
284
buf[1] = (
m_address
>> 16) & 0xff;
285
buf[2] = (
m_address
>> 8) & 0xff;
286
buf[3] = (
m_address
>> 0) & 0xff;
287
}
288
Ipv4Address
289
Ipv4Address::Deserialize
(
const
uint8_t buf[4])
290
{
291
NS_LOG_FUNCTION
(&buf);
292
Ipv4Address
ipv4;
293
ipv4.
m_address
= 0;
294
ipv4.
m_address
|= buf[0];
295
ipv4.
m_address
<<= 8;
296
ipv4.
m_address
|= buf[1];
297
ipv4.
m_address
<<= 8;
298
ipv4.
m_address
|= buf[2];
299
ipv4.
m_address
<<= 8;
300
ipv4.
m_address
|= buf[3];
301
return
ipv4;
302
}
303
304
void
305
Ipv4Address::Print
(std::ostream &os)
const
306
{
307
NS_LOG_FUNCTION
(
this
);
308
os << ((m_address >> 24) & 0xff) <<
"."
309
<< ((
m_address
>> 16) & 0xff) <<
"."
310
<< ((
m_address
>> 8) & 0xff) <<
"."
311
<< ((
m_address
>> 0) & 0xff);
312
}
313
314
bool
315
Ipv4Address::IsMatchingType
(
const
Address
&address)
316
{
317
NS_LOG_FUNCTION
(&address);
318
return
address.
CheckCompatible
(
GetType
(), 4);
319
}
320
Ipv4Address::operator
Address
()
const
321
{
322
return
ConvertTo ();
323
}
324
325
Address
326
Ipv4Address::ConvertTo
(
void
)
const
327
{
328
NS_LOG_FUNCTION
(
this
);
329
uint8_t buf[4];
330
Serialize
(buf);
331
return
Address
(
GetType
(), buf, 4);
332
}
333
334
Ipv4Address
335
Ipv4Address::ConvertFrom
(
const
Address
&address)
336
{
337
NS_LOG_FUNCTION
(&address);
338
NS_ASSERT
(address.
CheckCompatible
(
GetType
(), 4));
339
uint8_t buf[4];
340
address.
CopyTo
(buf);
341
return
Deserialize
(buf);
342
}
343
344
uint8_t
345
Ipv4Address::GetType
(
void
)
346
{
347
NS_LOG_FUNCTION_NOARGS
();
348
static
uint8_t type =
Address::Register
();
349
return
type;
350
}
351
352
Ipv4Address
353
Ipv4Address::GetZero
(
void
)
354
{
355
NS_LOG_FUNCTION_NOARGS
();
356
static
Ipv4Address
zero
(
"0.0.0.0"
);
357
return
zero
;
358
}
359
Ipv4Address
360
Ipv4Address::GetAny
(
void
)
361
{
362
NS_LOG_FUNCTION_NOARGS
();
363
static
Ipv4Address
any (
"0.0.0.0"
);
364
return
any;
365
}
366
Ipv4Address
367
Ipv4Address::GetBroadcast
(
void
)
368
{
369
NS_LOG_FUNCTION_NOARGS
();
370
static
Ipv4Address
broadcast (
"255.255.255.255"
);
371
return
broadcast;
372
}
373
Ipv4Address
374
Ipv4Address::GetLoopback
(
void
)
375
{
376
NS_LOG_FUNCTION_NOARGS
();
377
Ipv4Address
loopback (
"127.0.0.1"
);
378
return
loopback;
379
}
380
381
size_t
Ipv4AddressHash::operator()
(
Ipv4Address
const
&
x
)
const
382
{
383
return
x.
Get
();
384
}
385
386
std::ostream&
operator<<
(std::ostream& os,
Ipv4Address
const
& address)
387
{
388
address.
Print
(os);
389
return
os;
390
}
391
std::ostream&
operator<<
(std::ostream& os,
Ipv4Mask
const
& mask)
392
{
393
mask.
Print
(os);
394
return
os;
395
}
396
std::istream &
operator >>
(std::istream &is,
Ipv4Address
&address)
397
{
398
std::string str;
399
is >> str;
400
address =
Ipv4Address
(str.c_str ());
401
return
is;
402
}
403
std::istream &
operator >>
(std::istream &is,
Ipv4Mask
&mask)
404
{
405
std::string str;
406
is >> str;
407
mask =
Ipv4Mask
(str.c_str ());
408
return
is;
409
}
410
411
bool
operator ==
(
Ipv4Mask
const
&a,
Ipv4Mask
const
&b)
412
{
413
return
a.
IsEqual
(b);
414
}
415
bool
operator !=
(
Ipv4Mask
const
&a,
Ipv4Mask
const
&b)
416
{
417
return
!a.
IsEqual
(b);
418
}
419
420
ATTRIBUTE_HELPER_CPP
(Ipv4Address);
421
ATTRIBUTE_HELPER_CPP
(Ipv4Mask);
422
423
}
// namespace ns3
src
network
utils
ipv4-address.cc
Generated on Tue May 14 2013 11:08:31 for ns-3 by
1.8.1.2