author | Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
Mon, 08 Jun 2009 16:45:13 +0200 | |
changeset 4521 | 981e2f0b696b |
parent 3820 | c04ecfdce1ef |
child 4575 | ee3191a0b560 |
permissions | -rw-r--r-- |
3820 | 1 |
#include "icmpv4.h" |
2 |
#include "ns3/packet.h" |
|
3 |
||
4 |
namespace ns3 { |
|
5 |
||
6 |
/******************************************************** |
|
7 |
* Icmpv4Header |
|
8 |
********************************************************/ |
|
9 |
||
10 |
TypeId |
|
11 |
Icmpv4Header::GetTypeId (void) |
|
12 |
{ |
|
13 |
static TypeId tid = TypeId ("ns3::Icmpv4Header") |
|
14 |
.SetParent<Header> () |
|
15 |
.AddConstructor<Icmpv4Header> () |
|
16 |
; |
|
17 |
return tid; |
|
18 |
} |
|
19 |
Icmpv4Header::Icmpv4Header () |
|
20 |
: m_type (0), |
|
21 |
m_code (0), |
|
22 |
m_calcChecksum (false) |
|
23 |
{} |
|
24 |
Icmpv4Header::~Icmpv4Header () |
|
25 |
{} |
|
26 |
void |
|
27 |
Icmpv4Header::EnableChecksum (void) |
|
28 |
{ |
|
29 |
m_calcChecksum = true; |
|
30 |
} |
|
31 |
TypeId |
|
32 |
Icmpv4Header::GetInstanceTypeId (void) const |
|
33 |
{ |
|
34 |
return GetTypeId (); |
|
35 |
} |
|
36 |
uint32_t |
|
37 |
Icmpv4Header::GetSerializedSize (void) const |
|
38 |
{ |
|
39 |
return 4; |
|
40 |
} |
|
41 |
void |
|
42 |
Icmpv4Header::Serialize (Buffer::Iterator start) const |
|
43 |
{ |
|
44 |
Buffer::Iterator i = start; |
|
45 |
i.WriteU8 (m_type); |
|
46 |
i.WriteU8 (m_code); |
|
47 |
i.WriteHtonU16 (0); |
|
48 |
if (m_calcChecksum) |
|
49 |
{ |
|
50 |
i = start; |
|
51 |
uint16_t checksum = i.CalculateIpChecksum (i.GetSize ()); |
|
52 |
i = start; |
|
53 |
i.Next (2); |
|
54 |
i.WriteU16 (checksum); |
|
55 |
} |
|
56 |
||
57 |
} |
|
58 |
uint32_t |
|
59 |
Icmpv4Header::Deserialize (Buffer::Iterator start) |
|
60 |
{ |
|
61 |
m_type = start.ReadU8 (); |
|
62 |
m_code = start.ReadU8 (); |
|
63 |
uint16_t checksum; |
|
64 |
checksum = start.ReadNtohU16 (); |
|
65 |
return 4; |
|
66 |
} |
|
67 |
void |
|
68 |
Icmpv4Header::Print (std::ostream &os) const |
|
69 |
{ |
|
70 |
os << "type=" << (uint32_t)m_type << ", code=" << (uint32_t)m_code; |
|
71 |
} |
|
72 |
||
73 |
void |
|
74 |
Icmpv4Header::SetType (uint8_t type) |
|
75 |
{ |
|
76 |
m_type = type; |
|
77 |
} |
|
78 |
void |
|
79 |
Icmpv4Header::SetCode (uint8_t code) |
|
80 |
{ |
|
81 |
m_code = code; |
|
82 |
} |
|
83 |
uint8_t |
|
84 |
Icmpv4Header::GetType (void) const |
|
85 |
{ |
|
86 |
return m_type; |
|
87 |
} |
|
88 |
uint8_t |
|
89 |
Icmpv4Header::GetCode (void) const |
|
90 |
{ |
|
91 |
return m_code; |
|
92 |
} |
|
93 |
||
94 |
/******************************************************** |
|
95 |
* Icmpv4Echo |
|
96 |
********************************************************/ |
|
97 |
||
98 |
void |
|
99 |
Icmpv4Echo::SetIdentifier (uint16_t id) |
|
100 |
{ |
|
101 |
m_identifier = id; |
|
102 |
} |
|
103 |
void |
|
104 |
Icmpv4Echo::SetSequenceNumber (uint16_t seq) |
|
105 |
{ |
|
106 |
m_sequence = seq; |
|
107 |
} |
|
108 |
void |
|
109 |
Icmpv4Echo::SetData (Ptr<const Packet> data) |
|
110 |
{ |
|
4521
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
111 |
uint32_t size = (data->GetSize ()>16)?16:data->GetSize(); |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
112 |
data->CopyData (m_data, size); |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
113 |
m_dataSize = size; |
3820 | 114 |
} |
115 |
uint16_t |
|
116 |
Icmpv4Echo::GetIdentifier (void) const |
|
117 |
{ |
|
118 |
return m_identifier; |
|
119 |
} |
|
120 |
uint16_t |
|
121 |
Icmpv4Echo::GetSequenceNumber (void) const |
|
122 |
{ |
|
123 |
return m_sequence; |
|
124 |
} |
|
4521
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
125 |
uint32_t |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
126 |
Icmpv4Echo::GetData (uint8_t data[16]) const |
3820 | 127 |
{ |
4521
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
128 |
memcpy (data, m_data, m_dataSize); |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
129 |
return m_dataSize; |
3820 | 130 |
} |
131 |
||
132 |
||
133 |
TypeId |
|
134 |
Icmpv4Echo::GetTypeId (void) |
|
135 |
{ |
|
136 |
static TypeId tid = TypeId ("ns3::Icmpv4Echo") |
|
137 |
.SetParent<Header> () |
|
138 |
.AddConstructor<Icmpv4Echo> () |
|
139 |
; |
|
140 |
return tid; |
|
141 |
} |
|
142 |
Icmpv4Echo::Icmpv4Echo () |
|
143 |
: m_identifier (0), |
|
144 |
m_sequence (0), |
|
4521
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
145 |
m_dataSize (0) |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
146 |
{ |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
147 |
// make sure that thing is initialized to get initialized bytes |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
148 |
for (uint8_t j = 0; j < 16; j++) |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
149 |
{ |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
150 |
m_data[j] = 0; |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
151 |
} |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
152 |
} |
3820 | 153 |
Icmpv4Echo::~Icmpv4Echo () |
154 |
{} |
|
155 |
TypeId |
|
156 |
Icmpv4Echo::GetInstanceTypeId (void) const |
|
157 |
{ |
|
158 |
return GetTypeId (); |
|
159 |
} |
|
160 |
uint32_t |
|
161 |
Icmpv4Echo::GetSerializedSize (void) const |
|
162 |
{ |
|
4521
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
163 |
return 4 + m_dataSize; |
3820 | 164 |
} |
165 |
void |
|
166 |
Icmpv4Echo::Serialize (Buffer::Iterator start) const |
|
167 |
{ |
|
168 |
start.WriteHtonU16 (m_identifier); |
|
169 |
start.WriteHtonU16 (m_sequence); |
|
4521
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
170 |
start.Write (m_data, m_dataSize); |
3820 | 171 |
} |
172 |
uint32_t |
|
173 |
Icmpv4Echo::Deserialize (Buffer::Iterator start) |
|
174 |
{ |
|
175 |
m_identifier = start.ReadNtohU16 (); |
|
176 |
m_sequence = start.ReadNtohU16 (); |
|
177 |
NS_ASSERT (start.GetSize () >= 4); |
|
4521
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
178 |
m_dataSize = start.GetSize () - 4; |
981e2f0b696b
no need to keep around a Ptr<Packet>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3820
diff
changeset
|
179 |
start.Read (m_data, m_dataSize); |
3820 | 180 |
return start.GetSize (); |
181 |
} |
|
182 |
void |
|
183 |
Icmpv4Echo::Print (std::ostream &os) const |
|
184 |
{ |
|
185 |
os << "identifier=" << m_identifier << ", sequence=" << m_sequence; |
|
186 |
} |
|
187 |
||
188 |
||
189 |
/******************************************************** |
|
190 |
* Icmpv4DestinationUnreachable |
|
191 |
********************************************************/ |
|
192 |
||
193 |
TypeId |
|
194 |
Icmpv4DestinationUnreachable::GetTypeId (void) |
|
195 |
{ |
|
196 |
static TypeId tid = TypeId ("ns3::Icmpv4DestinationUnreachable") |
|
197 |
.SetParent<Header> () |
|
198 |
.AddConstructor<Icmpv4DestinationUnreachable> () |
|
199 |
; |
|
200 |
return tid; |
|
201 |
} |
|
202 |
Icmpv4DestinationUnreachable::Icmpv4DestinationUnreachable () |
|
203 |
{ |
|
204 |
// make sure that thing is initialized to get initialized bytes |
|
205 |
// when the ip payload's size is smaller than 8 bytes. |
|
206 |
for (uint8_t j = 0; j < 8; j++) |
|
207 |
{ |
|
208 |
m_data[j] = 0; |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
void |
|
213 |
Icmpv4DestinationUnreachable::SetNextHopMtu (uint16_t mtu) |
|
214 |
{ |
|
215 |
m_nextHopMtu = mtu; |
|
216 |
} |
|
217 |
uint16_t |
|
218 |
Icmpv4DestinationUnreachable::GetNextHopMtu (void) const |
|
219 |
{ |
|
220 |
return m_nextHopMtu; |
|
221 |
} |
|
222 |
||
223 |
void |
|
224 |
Icmpv4DestinationUnreachable::SetData (Ptr<const Packet> data) |
|
225 |
{ |
|
226 |
data->CopyData (m_data, 8); |
|
227 |
} |
|
228 |
void |
|
229 |
Icmpv4DestinationUnreachable::SetHeader (Ipv4Header header) |
|
230 |
{ |
|
231 |
m_header = header; |
|
232 |
} |
|
233 |
void |
|
234 |
Icmpv4DestinationUnreachable::GetData (uint8_t payload[8]) const |
|
235 |
{ |
|
236 |
memcpy (payload, m_data, 8); |
|
237 |
} |
|
238 |
Ipv4Header |
|
239 |
Icmpv4DestinationUnreachable::GetHeader (void) const |
|
240 |
{ |
|
241 |
return m_header; |
|
242 |
} |
|
243 |
||
244 |
||
245 |
Icmpv4DestinationUnreachable::~Icmpv4DestinationUnreachable () |
|
246 |
{} |
|
247 |
TypeId |
|
248 |
Icmpv4DestinationUnreachable::GetInstanceTypeId (void) const |
|
249 |
{ |
|
250 |
return GetTypeId (); |
|
251 |
} |
|
252 |
uint32_t |
|
253 |
Icmpv4DestinationUnreachable::GetSerializedSize (void) const |
|
254 |
{ |
|
255 |
return 4 + m_header.GetSerializedSize () + 8; |
|
256 |
} |
|
257 |
void |
|
258 |
Icmpv4DestinationUnreachable::Serialize (Buffer::Iterator start) const |
|
259 |
{ |
|
260 |
start.WriteU16 (0); |
|
261 |
start.WriteHtonU16 (m_nextHopMtu); |
|
262 |
uint32_t size = m_header.GetSerializedSize (); |
|
263 |
m_header.Serialize (start); |
|
264 |
start.Next (size); |
|
265 |
start.Write (m_data, 8); |
|
266 |
} |
|
267 |
||
268 |
uint32_t |
|
269 |
Icmpv4DestinationUnreachable::Deserialize (Buffer::Iterator start) |
|
270 |
{ |
|
271 |
Buffer::Iterator i = start; |
|
272 |
i.Next (2); |
|
273 |
m_nextHopMtu = i.ReadNtohU16 (); |
|
274 |
uint32_t read = m_header.Deserialize (i); |
|
275 |
i.Next (read); |
|
276 |
for (uint8_t j = 0; j < 8; j++) |
|
277 |
{ |
|
278 |
m_data[j] = i.ReadU8 (); |
|
279 |
} |
|
280 |
return i.GetDistanceFrom (start); |
|
281 |
} |
|
282 |
void |
|
283 |
Icmpv4DestinationUnreachable::Print (std::ostream &os) const |
|
284 |
{ |
|
285 |
m_header.Print (os); |
|
286 |
os << " org data="; |
|
287 |
for (uint8_t i = 0; i < 8; i++) |
|
288 |
{ |
|
289 |
os << (uint32_t) m_data[i]; |
|
290 |
if (i != 8) |
|
291 |
{ |
|
292 |
os << " "; |
|
293 |
} |
|
294 |
} |
|
295 |
} |
|
296 |
||
297 |
/******************************************************** |
|
298 |
* Icmpv4TimeExceeded |
|
299 |
********************************************************/ |
|
300 |
||
301 |
TypeId |
|
302 |
Icmpv4TimeExceeded::GetTypeId (void) |
|
303 |
{ |
|
304 |
static TypeId tid = TypeId ("ns3::Icmpv4TimeExceeded") |
|
305 |
.SetParent<Header> () |
|
306 |
.AddConstructor<Icmpv4TimeExceeded> () |
|
307 |
; |
|
308 |
return tid; |
|
309 |
} |
|
310 |
Icmpv4TimeExceeded::Icmpv4TimeExceeded () |
|
311 |
{ |
|
312 |
// make sure that thing is initialized to get initialized bytes |
|
313 |
// when the ip payload's size is smaller than 8 bytes. |
|
314 |
for (uint8_t j = 0; j < 8; j++) |
|
315 |
{ |
|
316 |
m_data[j] = 0; |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
||
321 |
void |
|
322 |
Icmpv4TimeExceeded::SetData (Ptr<const Packet> data) |
|
323 |
{ |
|
324 |
data->CopyData (m_data, 8); |
|
325 |
} |
|
326 |
void |
|
327 |
Icmpv4TimeExceeded::SetHeader (Ipv4Header header) |
|
328 |
{ |
|
329 |
m_header = header; |
|
330 |
} |
|
331 |
void |
|
332 |
Icmpv4TimeExceeded::GetData (uint8_t payload[8]) const |
|
333 |
{ |
|
334 |
memcpy (payload, m_data, 8); |
|
335 |
} |
|
336 |
Ipv4Header |
|
337 |
Icmpv4TimeExceeded::GetHeader (void) const |
|
338 |
{ |
|
339 |
return m_header; |
|
340 |
} |
|
341 |
||
342 |
||
343 |
Icmpv4TimeExceeded::~Icmpv4TimeExceeded () |
|
344 |
{} |
|
345 |
TypeId |
|
346 |
Icmpv4TimeExceeded::GetInstanceTypeId (void) const |
|
347 |
{ |
|
348 |
return GetTypeId (); |
|
349 |
} |
|
350 |
uint32_t |
|
351 |
Icmpv4TimeExceeded::GetSerializedSize (void) const |
|
352 |
{ |
|
353 |
return 4 + m_header.GetSerializedSize () + 8; |
|
354 |
} |
|
355 |
void |
|
356 |
Icmpv4TimeExceeded::Serialize (Buffer::Iterator start) const |
|
357 |
{ |
|
358 |
start.WriteU32 (0); |
|
359 |
uint32_t size = m_header.GetSerializedSize (); |
|
360 |
m_header.Serialize (start); |
|
361 |
start.Next (size); |
|
362 |
start.Write (m_data, 8); |
|
363 |
} |
|
364 |
||
365 |
uint32_t |
|
366 |
Icmpv4TimeExceeded::Deserialize (Buffer::Iterator start) |
|
367 |
{ |
|
368 |
Buffer::Iterator i = start; |
|
369 |
i.Next (4); |
|
370 |
uint32_t read = m_header.Deserialize (i); |
|
371 |
i.Next (read); |
|
372 |
for (uint8_t j = 0; j < 8; j++) |
|
373 |
{ |
|
374 |
m_data[j] = i.ReadU8 (); |
|
375 |
} |
|
376 |
return i.GetDistanceFrom (start); |
|
377 |
} |
|
378 |
void |
|
379 |
Icmpv4TimeExceeded::Print (std::ostream &os) const |
|
380 |
{ |
|
381 |
m_header.Print (os); |
|
382 |
os << " org data="; |
|
383 |
for (uint8_t i = 0; i < 8; i++) |
|
384 |
{ |
|
385 |
os << (uint32_t) m_data[i]; |
|
386 |
if (i != 8) |
|
387 |
{ |
|
388 |
os << " "; |
|
389 |
} |
|
390 |
} |
|
391 |
} |
|
392 |
||
393 |
} // namespace ns3 |