1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007-2009 Strasbourg University
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: David Gross <gdavid.devel@gmail.com>
19  */
20 
21 #include "ns3/assert.h"
22 #include "ns3/log.h"
23 #include "ns3/header.h"
24 #include "ipv6-option-header.h"
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE ("Ipv6OptionHeader");
30 
31 NS_OBJECT_ENSURE_REGISTERED (Ipv6OptionHeader);
32 
GetTypeId()33 TypeId Ipv6OptionHeader::GetTypeId ()
34 {
35   static TypeId tid = TypeId ("ns3::Ipv6OptionHeader")
36     .AddConstructor<Ipv6OptionHeader> ()
37     .SetParent<Header> ()
38     .SetGroupName ("Internet")
39   ;
40   return tid;
41 }
42 
GetInstanceTypeId() const43 TypeId Ipv6OptionHeader::GetInstanceTypeId () const
44 {
45   return GetTypeId ();
46 }
47 
Ipv6OptionHeader()48 Ipv6OptionHeader::Ipv6OptionHeader ()
49   : m_type (0),
50     m_length (0)
51 {
52 }
53 
~Ipv6OptionHeader()54 Ipv6OptionHeader::~Ipv6OptionHeader ()
55 {
56 }
57 
SetType(uint8_t type)58 void Ipv6OptionHeader::SetType (uint8_t type)
59 {
60   m_type = type;
61 }
62 
GetType() const63 uint8_t Ipv6OptionHeader::GetType () const
64 {
65   return m_type;
66 }
67 
SetLength(uint8_t length)68 void Ipv6OptionHeader::SetLength (uint8_t length)
69 {
70   m_length = length;
71 }
72 
GetLength() const73 uint8_t Ipv6OptionHeader::GetLength () const
74 {
75   return m_length;
76 }
77 
Print(std::ostream & os) const78 void Ipv6OptionHeader::Print (std::ostream &os) const
79 {
80   os << "( type = " << (uint32_t)m_type << " )";
81 }
82 
GetSerializedSize() const83 uint32_t Ipv6OptionHeader::GetSerializedSize () const
84 {
85   return m_length + 2;
86 }
87 
Serialize(Buffer::Iterator start) const88 void Ipv6OptionHeader::Serialize (Buffer::Iterator start) const
89 {
90   Buffer::Iterator i = start;
91 
92   i.WriteU8 (m_type);
93   i.WriteU8 (m_length);
94 
95   i.Write (m_data.Begin (), m_data.End ());
96 }
97 
Deserialize(Buffer::Iterator start)98 uint32_t Ipv6OptionHeader::Deserialize (Buffer::Iterator start)
99 {
100   Buffer::Iterator i = start;
101 
102   m_type = i.ReadU8 ();
103   m_length = i.ReadU8 ();
104 
105   m_data = Buffer ();
106   m_data.AddAtEnd (m_length);
107   Buffer::Iterator dataStart = i;
108   i.Next (m_length);
109   Buffer::Iterator dataEnd = i;
110   m_data.Begin ().Write (dataStart, dataEnd);
111 
112   return GetSerializedSize ();
113 }
114 
GetAlignment() const115 Ipv6OptionHeader::Alignment Ipv6OptionHeader::GetAlignment () const
116 {
117   return (Alignment){ 1,0};
118 }
119 
120 NS_OBJECT_ENSURE_REGISTERED (Ipv6OptionPad1Header);
121 
GetTypeId()122 TypeId Ipv6OptionPad1Header::GetTypeId ()
123 {
124   static TypeId tid = TypeId ("ns3::Ipv6OptionPad1Header")
125     .AddConstructor<Ipv6OptionPad1Header> ()
126     .SetParent<Ipv6OptionHeader> ()
127     .SetGroupName ("Internet")
128   ;
129   return tid;
130 }
131 
GetInstanceTypeId() const132 TypeId Ipv6OptionPad1Header::GetInstanceTypeId () const
133 {
134   return GetTypeId ();
135 }
136 
Ipv6OptionPad1Header()137 Ipv6OptionPad1Header::Ipv6OptionPad1Header ()
138 {
139   SetType (0);
140 }
141 
~Ipv6OptionPad1Header()142 Ipv6OptionPad1Header::~Ipv6OptionPad1Header ()
143 {
144 }
145 
Print(std::ostream & os) const146 void Ipv6OptionPad1Header::Print (std::ostream &os) const
147 {
148   os << "( type = " << (uint32_t)GetType () << " )";
149 }
150 
GetSerializedSize() const151 uint32_t Ipv6OptionPad1Header::GetSerializedSize () const
152 {
153   return 1;
154 }
155 
Serialize(Buffer::Iterator start) const156 void Ipv6OptionPad1Header::Serialize (Buffer::Iterator start) const
157 {
158   Buffer::Iterator i = start;
159 
160   i.WriteU8 (GetType ());
161 }
162 
Deserialize(Buffer::Iterator start)163 uint32_t Ipv6OptionPad1Header::Deserialize (Buffer::Iterator start)
164 {
165   Buffer::Iterator i = start;
166 
167   SetType (i.ReadU8 ());
168 
169   return GetSerializedSize ();
170 }
171 
172 NS_OBJECT_ENSURE_REGISTERED (Ipv6OptionPadnHeader);
173 
GetTypeId()174 TypeId Ipv6OptionPadnHeader::GetTypeId ()
175 {
176   static TypeId tid = TypeId ("ns3::Ipv6OptionPadnHeader")
177     .AddConstructor<Ipv6OptionPadnHeader> ()
178     .SetParent<Ipv6OptionHeader> ()
179     .SetGroupName ("Internet")
180   ;
181   return tid;
182 }
183 
GetInstanceTypeId() const184 TypeId Ipv6OptionPadnHeader::GetInstanceTypeId () const
185 {
186   return GetTypeId ();
187 }
188 
Ipv6OptionPadnHeader(uint32_t pad)189 Ipv6OptionPadnHeader::Ipv6OptionPadnHeader (uint32_t pad)
190 {
191   SetType (1);
192   NS_ASSERT_MSG (pad >= 2, "PadN must be at least 2 bytes long");
193   SetLength (pad - 2);
194 }
195 
~Ipv6OptionPadnHeader()196 Ipv6OptionPadnHeader::~Ipv6OptionPadnHeader ()
197 {
198 }
199 
Print(std::ostream & os) const200 void Ipv6OptionPadnHeader::Print (std::ostream &os) const
201 {
202   os << "( type = " << (uint32_t)GetType () << " length = " << (uint32_t)GetLength () << " )";
203 }
204 
GetSerializedSize() const205 uint32_t Ipv6OptionPadnHeader::GetSerializedSize () const
206 {
207   return GetLength () + 2;
208 }
209 
Serialize(Buffer::Iterator start) const210 void Ipv6OptionPadnHeader::Serialize (Buffer::Iterator start) const
211 {
212   Buffer::Iterator i = start;
213 
214   i.WriteU8 (GetType ());
215   i.WriteU8 (GetLength ());
216 
217   for (int padding = 0; padding < GetLength (); padding++)
218     {
219       i.WriteU8 (0);
220     }
221 }
222 
Deserialize(Buffer::Iterator start)223 uint32_t Ipv6OptionPadnHeader::Deserialize (Buffer::Iterator start)
224 {
225   Buffer::Iterator i = start;
226 
227   SetType (i.ReadU8 ());
228   SetLength (i.ReadU8 ());
229 
230   return GetSerializedSize ();
231 }
232 
233 NS_OBJECT_ENSURE_REGISTERED (Ipv6OptionJumbogramHeader);
234 
GetTypeId()235 TypeId Ipv6OptionJumbogramHeader::GetTypeId ()
236 {
237   static TypeId tid = TypeId ("ns3::Ipv6OptionJumbogramHeader")
238     .AddConstructor<Ipv6OptionJumbogramHeader> ()
239     .SetParent<Ipv6OptionHeader> ()
240     .SetGroupName ("Internet")
241   ;
242   return tid;
243 }
244 
GetInstanceTypeId() const245 TypeId Ipv6OptionJumbogramHeader::GetInstanceTypeId () const
246 {
247   return GetTypeId ();
248 }
249 
Ipv6OptionJumbogramHeader()250 Ipv6OptionJumbogramHeader::Ipv6OptionJumbogramHeader ()
251 {
252   SetType (0xC2);
253   SetLength (4);
254   m_dataLength = 0;
255 }
256 
~Ipv6OptionJumbogramHeader()257 Ipv6OptionJumbogramHeader::~Ipv6OptionJumbogramHeader ()
258 {
259 }
260 
SetDataLength(uint32_t dataLength)261 void Ipv6OptionJumbogramHeader::SetDataLength (uint32_t dataLength)
262 {
263   m_dataLength = dataLength;
264 }
265 
GetDataLength() const266 uint32_t Ipv6OptionJumbogramHeader::GetDataLength () const
267 {
268   return m_dataLength;
269 }
270 
Print(std::ostream & os) const271 void Ipv6OptionJumbogramHeader::Print (std::ostream &os) const
272 {
273   os << "( type = " << (uint32_t)GetType () << " length = " << (uint32_t)GetLength () << " data length = " << (uint32_t)m_dataLength << " )";
274 }
275 
GetSerializedSize() const276 uint32_t Ipv6OptionJumbogramHeader::GetSerializedSize () const
277 {
278   return GetLength () + 2;
279 }
280 
Serialize(Buffer::Iterator start) const281 void Ipv6OptionJumbogramHeader::Serialize (Buffer::Iterator start) const
282 {
283   Buffer::Iterator i = start;
284 
285   i.WriteU8 (GetType ());
286   i.WriteU8 (GetLength ());
287   i.WriteHtonU32 (m_dataLength);
288 }
289 
Deserialize(Buffer::Iterator start)290 uint32_t Ipv6OptionJumbogramHeader::Deserialize (Buffer::Iterator start)
291 {
292   Buffer::Iterator i = start;
293 
294   SetType (i.ReadU8 ());
295   SetLength (i.ReadU8 ());
296   m_dataLength = i.ReadNtohU16 ();
297 
298   return GetSerializedSize ();
299 }
300 
GetAlignment() const301 Ipv6OptionHeader::Alignment Ipv6OptionJumbogramHeader::GetAlignment () const
302 {
303   return (Alignment){ 4,2}; //4n+2
304 }
305 
306 NS_OBJECT_ENSURE_REGISTERED (Ipv6OptionRouterAlertHeader);
307 
GetTypeId()308 TypeId Ipv6OptionRouterAlertHeader::GetTypeId ()
309 {
310   static TypeId tid = TypeId ("ns3::Ipv6OptionRouterAlertHeader")
311     .AddConstructor<Ipv6OptionRouterAlertHeader> ()
312     .SetParent<Ipv6OptionHeader> ()
313     .SetGroupName ("Internet")
314   ;
315   return tid;
316 }
317 
GetInstanceTypeId() const318 TypeId Ipv6OptionRouterAlertHeader::GetInstanceTypeId () const
319 {
320   return GetTypeId ();
321 }
322 
Ipv6OptionRouterAlertHeader()323 Ipv6OptionRouterAlertHeader::Ipv6OptionRouterAlertHeader ()
324   : m_value (0)
325 {
326   SetType (5);
327   SetLength (2);
328 }
329 
~Ipv6OptionRouterAlertHeader()330 Ipv6OptionRouterAlertHeader::~Ipv6OptionRouterAlertHeader ()
331 {
332 }
333 
SetValue(uint16_t value)334 void Ipv6OptionRouterAlertHeader::SetValue (uint16_t value)
335 {
336   m_value = value;
337 }
338 
GetValue() const339 uint16_t Ipv6OptionRouterAlertHeader::GetValue () const
340 {
341   return m_value;
342 }
343 
Print(std::ostream & os) const344 void Ipv6OptionRouterAlertHeader::Print (std::ostream &os) const
345 {
346   os << "( type = " << (uint32_t)GetType () << " length = " << (uint32_t)GetLength ()   << " value = " << (uint32_t)m_value << " )";
347 }
348 
GetSerializedSize() const349 uint32_t Ipv6OptionRouterAlertHeader::GetSerializedSize () const
350 {
351   return GetLength () + 2;
352 }
353 
Serialize(Buffer::Iterator start) const354 void Ipv6OptionRouterAlertHeader::Serialize (Buffer::Iterator start) const
355 {
356   Buffer::Iterator i = start;
357 
358   i.WriteU8 (GetType ());
359   i.WriteU8 (GetLength ());
360   i.WriteHtonU16 (m_value);
361 }
362 
Deserialize(Buffer::Iterator start)363 uint32_t Ipv6OptionRouterAlertHeader::Deserialize (Buffer::Iterator start)
364 {
365   Buffer::Iterator i = start;
366 
367   SetType (i.ReadU8 ());
368   SetLength (i.ReadU8 ());
369   m_value = i.ReadNtohU16 ();
370 
371   return GetSerializedSize ();
372 }
373 
GetAlignment() const374 Ipv6OptionHeader::Alignment Ipv6OptionRouterAlertHeader::GetAlignment () const
375 {
376   return (Alignment){ 2,0}; //2n+0
377 }
378 
379 } /* namespace ns3 */
380 
381