A Discrete-Event Network Simulator
API
tcp-option-rfc793.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Adrian Sai-wah Tam
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Adrian Sai-wah Tam <adrian.sw.tam@gmail.com>
18  */
19 
20 // TCP options that are specified in RFC 793 (kinds 0, 1, and 2)
21 
22 #include "tcp-option-rfc793.h"
23 
24 #include "ns3/log.h"
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("TcpOptionRfc793");
30 
31 NS_OBJECT_ENSURE_REGISTERED(TcpOptionEnd);
32 
34  : TcpOption()
35 {
36 }
37 
39 {
40 }
41 
42 TypeId
44 {
45  static TypeId tid = TypeId("ns3::TcpOptionEnd")
47  .SetGroupName("Internet")
48  .AddConstructor<TcpOptionEnd>();
49  return tid;
50 }
51 
52 TypeId
54 {
55  return GetTypeId();
56 }
57 
58 void
59 TcpOptionEnd::Print(std::ostream& os) const
60 {
61  os << "EOL";
62 }
63 
64 uint32_t
66 {
67  return 1;
68 }
69 
70 void
72 {
74  i.WriteU8(GetKind());
75 }
76 
77 uint32_t
79 {
81 
82  uint8_t readKind = i.ReadU8();
83 
84  if (readKind != GetKind())
85  {
86  NS_LOG_WARN("Malformed END option");
87  return 0;
88  }
89 
90  return GetSerializedSize();
91 }
92 
93 uint8_t
95 {
96  return TcpOption::END;
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  .SetGroupName("Internet")
118  .AddConstructor<TcpOptionNOP>();
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  .SetGroupName("Internet")
188  .AddConstructor<TcpOptionMSS>();
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_ABORT_IF(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
iterator in a Buffer instance
Definition: buffer.h:100
uint8_t ReadU8()
Definition: buffer.h:1027
void WriteU8(uint8_t data)
Definition: buffer.h:881
void WriteHtonU16(uint16_t data)
Definition: buffer.h:915
uint16_t ReadNtohU16()
Definition: buffer.h:954
Defines the TCP option of kind 0 (end of option list) as in RFC 793
void Serialize(Buffer::Iterator start) const override
Serialize the Option to a buffer iterator.
uint32_t GetSerializedSize() const override
Returns number of bytes required for Option serialization.
static TypeId GetTypeId()
Get the type ID.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the Option from a buffer iterator.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint8_t GetKind() const override
Get the ‘kind’ (as in RFC 793) of this option.
void Print(std::ostream &os) const override
Print the Option contents.
Base class for all kinds of TCP options.
Definition: tcp-option.h:38
Defines the TCP option of kind 2 (maximum segment size) as in RFC 793
uint16_t GetMSS() const
Get the Maximum Segment Size stored in the Option.
uint16_t m_mss
maximum segment size
void SetMSS(uint16_t mss)
Set the Maximum Segment Size stored in the Option.
static TypeId GetTypeId()
Get the type ID.
uint8_t GetKind() const override
Get the ‘kind’ (as in RFC 793) of this option.
void Print(std::ostream &os) const override
Print the Option contents.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint32_t GetSerializedSize() const override
Returns number of bytes required for Option serialization.
void Serialize(Buffer::Iterator start) const override
Serialize the Option to a buffer iterator.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the Option from a buffer iterator.
Defines the TCP option of kind 1 (no operation) as in RFC 793
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the Option from a buffer iterator.
void Serialize(Buffer::Iterator start) const override
Serialize the Option to a buffer iterator.
uint32_t GetSerializedSize() const override
Returns number of bytes required for Option serialization.
uint8_t GetKind() const override
Get the ‘kind’ (as in RFC 793) of this option.
void Print(std::ostream &os) const override
Print the Option contents.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
static TypeId GetTypeId()
Get the type ID.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:76
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.