A Discrete-Event Network Simulator
API
tcp-option-rfc793.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Adrian Sai-wah Tam
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: Adrian Sai-wah Tam <adrian.sw.tam@gmail.com>
19  */
20 
21 // TCP options that are specified in RFC 793 (kinds 0, 1, and 2)
22 
23 #include "tcp-option-rfc793.h"
24 
25 #include "ns3/log.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("TcpOptionRfc793");
30 
31 NS_OBJECT_ENSURE_REGISTERED (TcpOptionEnd);
32 
34 {
35 }
36 
38 {
39 }
40 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::TcpOptionEnd")
45  .SetParent<TcpOption> ()
46  .AddConstructor<TcpOptionEnd> ()
47  ;
48  return tid;
49 }
50 
51 TypeId
53 {
54  return GetTypeId ();
55 }
56 
57 void
58 TcpOptionEnd::Print (std::ostream &os) const
59 {
60  os << "EOL";
61 }
62 
63 uint32_t
65 {
66  return 1;
67 }
68 
69 void
71 {
73  i.WriteU8 (GetKind ());
74 }
75 
76 uint32_t
78 {
80 
81  uint8_t readKind = i.ReadU8 ();
82 
83  if (readKind != GetKind ())
84  {
85  NS_LOG_WARN ("Malformed END option");
86  return 0;
87  }
88 
89  return GetSerializedSize ();
90 }
91 
92 uint8_t
94 {
95  return TcpOption::END;
96 }
97 
98 
99 // Tcp Option NOP
100 
102 
104  : TcpOption ()
105 {
106 }
107 
109 {
110 }
111 
112 TypeId
114 {
115  static TypeId tid = TypeId ("ns3::TcpOptionNOP")
116  .SetParent<TcpOption> ()
117  .AddConstructor<TcpOptionNOP> ()
118  ;
119  return tid;
120 }
121 
122 TypeId
124 {
125  return GetTypeId ();
126 }
127 
128 void
129 TcpOptionNOP::Print (std::ostream &os) const
130 {
131  os << "NOP";
132 }
133 
134 uint32_t
136 {
137  return 1;
138 }
139 
140 void
142 {
144  i.WriteU8 (GetKind ());
145 }
146 
147 uint32_t
149 {
151 
152  uint8_t readKind = i.ReadU8 ();
153  if (readKind != GetKind ())
154  {
155  NS_LOG_WARN ("Malformed NOP option");
156  return 0;
157  }
158 
159  return GetSerializedSize ();
160 }
161 
162 uint8_t
164 {
165  return TcpOption::NOP;
166 }
167 
168 // Tcp Option MSS
169 
171 
173  : TcpOption (),
174  m_mss (1460)
175 {
176 }
177 
179 {
180 }
181 
182 TypeId
184 {
185  static TypeId tid = TypeId ("ns3::TcpOptionMSS")
186  .SetParent<TcpOption> ()
187  .AddConstructor<TcpOptionMSS> ()
188  ;
189  return tid;
190 }
191 
192 TypeId
194 {
195  return GetTypeId ();
196 }
197 
198 void
199 TcpOptionMSS::Print (std::ostream &os) const
200 {
201  os << "MSS:" << m_mss;
202 }
203 
204 uint32_t
206 {
207  return 4;
208 }
209 
210 void
212 {
214  i.WriteU8 (GetKind ()); // Kind
215  i.WriteU8 (4); // Length
216  i.WriteHtonU16 (m_mss); // Max segment size
217 }
218 
219 uint32_t
221 {
223 
224  uint8_t readKind = i.ReadU8 ();
225  if (readKind != GetKind ())
226  {
227  NS_LOG_WARN ("Malformed MSS option");
228  return 0;
229  }
230 
231  uint8_t size = i.ReadU8 ();
232 
233  NS_ASSERT (size == 4);
234  m_mss = i.ReadNtohU16 ();
235 
236  return GetSerializedSize ();
237 }
238 
239 uint8_t
241 {
242  return TcpOption::MSS;
243 }
244 
245 uint16_t
247 {
248  return m_mss;
249 }
250 
251 void
252 TcpOptionMSS::SetMSS (uint16_t mss)
253 {
254  m_mss = mss;
255 }
256 
257 } // namespace ns3
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
void SetMSS(uint16_t mss)
Set the Maximum Segment Size stored in the Option.
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
Defines the TCP option of kind 1 (no operation) as in RFC 793
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
def start()
Definition: core.py:1482
virtual uint8_t GetKind(void) const
Get the `kind' (as in RFC 793) of this option.
virtual void Print(std::ostream &os) const
Print the Option contents.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
virtual uint8_t GetKind(void) const
Get the `kind' (as in RFC 793) of this option.
iterator in a Buffer instance
Definition: buffer.h:98
uint16_t GetMSS(void) const
Get the Maximum Segment Size stored in the Option.
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
virtual TypeId GetInstanceTypeId(void) const
Implement the GetInstanceTypeId method defined in ObjectBase.
void WriteHtonU16(uint16_t data)
Definition: buffer.h:912
uint16_t m_mss
maximum segment size
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TypeId GetTypeId(void)
Get the type ID.
virtual TypeId GetInstanceTypeId(void) const
Implement the GetInstanceTypeId method defined in ObjectBase.
virtual void Print(std::ostream &os) const
Print the Option contents.
virtual uint8_t GetKind(void) const
Get the `kind' (as in RFC 793) of this option.
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
static TypeId GetTypeId(void)
Get the type ID.
void WriteU8(uint8_t data)
Definition: buffer.h:876
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
static TypeId GetTypeId(void)
Get the type ID.
Defines the TCP option of kind 2 (maximum segment size) as in RFC 793
uint8_t ReadU8(void)
Definition: buffer.h:1028
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
Base class for all kinds of TCP options.
Definition: tcp-option.h:34
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
virtual TypeId GetInstanceTypeId(void) const
Implement the GetInstanceTypeId method defined in ObjectBase.
uint16_t ReadNtohU16(void)
Definition: buffer.h:953
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
virtual void Print(std::ostream &os) const
Print the Option contents.