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
TcpOptionEnd()33 TcpOptionEnd::TcpOptionEnd () : TcpOption ()
34 {
35 }
36
~TcpOptionEnd()37 TcpOptionEnd::~TcpOptionEnd ()
38 {
39 }
40
41 TypeId
GetTypeId(void)42 TcpOptionEnd::GetTypeId (void)
43 {
44 static TypeId tid = TypeId ("ns3::TcpOptionEnd")
45 .SetParent<TcpOption> ()
46 .SetGroupName ("Internet")
47 .AddConstructor<TcpOptionEnd> ()
48 ;
49 return tid;
50 }
51
52 TypeId
GetInstanceTypeId(void) const53 TcpOptionEnd::GetInstanceTypeId (void) const
54 {
55 return GetTypeId ();
56 }
57
58 void
Print(std::ostream & os) const59 TcpOptionEnd::Print (std::ostream &os) const
60 {
61 os << "EOL";
62 }
63
64 uint32_t
GetSerializedSize(void) const65 TcpOptionEnd::GetSerializedSize (void) const
66 {
67 return 1;
68 }
69
70 void
Serialize(Buffer::Iterator start) const71 TcpOptionEnd::Serialize (Buffer::Iterator start) const
72 {
73 Buffer::Iterator i = start;
74 i.WriteU8 (GetKind ());
75 }
76
77 uint32_t
Deserialize(Buffer::Iterator start)78 TcpOptionEnd::Deserialize (Buffer::Iterator start)
79 {
80 Buffer::Iterator i = start;
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
GetKind(void) const94 TcpOptionEnd::GetKind (void) const
95 {
96 return TcpOption::END;
97 }
98
99
100 // Tcp Option NOP
101
102 NS_OBJECT_ENSURE_REGISTERED (TcpOptionNOP);
103
TcpOptionNOP()104 TcpOptionNOP::TcpOptionNOP ()
105 : TcpOption ()
106 {
107 }
108
~TcpOptionNOP()109 TcpOptionNOP::~TcpOptionNOP ()
110 {
111 }
112
113 TypeId
GetTypeId(void)114 TcpOptionNOP::GetTypeId (void)
115 {
116 static TypeId tid = TypeId ("ns3::TcpOptionNOP")
117 .SetParent<TcpOption> ()
118 .SetGroupName ("Internet")
119 .AddConstructor<TcpOptionNOP> ()
120 ;
121 return tid;
122 }
123
124 TypeId
GetInstanceTypeId(void) const125 TcpOptionNOP::GetInstanceTypeId (void) const
126 {
127 return GetTypeId ();
128 }
129
130 void
Print(std::ostream & os) const131 TcpOptionNOP::Print (std::ostream &os) const
132 {
133 os << "NOP";
134 }
135
136 uint32_t
GetSerializedSize(void) const137 TcpOptionNOP::GetSerializedSize (void) const
138 {
139 return 1;
140 }
141
142 void
Serialize(Buffer::Iterator start) const143 TcpOptionNOP::Serialize (Buffer::Iterator start) const
144 {
145 Buffer::Iterator i = start;
146 i.WriteU8 (GetKind ());
147 }
148
149 uint32_t
Deserialize(Buffer::Iterator start)150 TcpOptionNOP::Deserialize (Buffer::Iterator start)
151 {
152 Buffer::Iterator i = start;
153
154 uint8_t readKind = i.ReadU8 ();
155 if (readKind != GetKind ())
156 {
157 NS_LOG_WARN ("Malformed NOP option");
158 return 0;
159 }
160
161 return GetSerializedSize ();
162 }
163
164 uint8_t
GetKind(void) const165 TcpOptionNOP::GetKind (void) const
166 {
167 return TcpOption::NOP;
168 }
169
170 // Tcp Option MSS
171
172 NS_OBJECT_ENSURE_REGISTERED (TcpOptionMSS);
173
TcpOptionMSS()174 TcpOptionMSS::TcpOptionMSS ()
175 : TcpOption (),
176 m_mss (1460)
177 {
178 }
179
~TcpOptionMSS()180 TcpOptionMSS::~TcpOptionMSS ()
181 {
182 }
183
184 TypeId
GetTypeId(void)185 TcpOptionMSS::GetTypeId (void)
186 {
187 static TypeId tid = TypeId ("ns3::TcpOptionMSS")
188 .SetParent<TcpOption> ()
189 .SetGroupName ("Internet")
190 .AddConstructor<TcpOptionMSS> ()
191 ;
192 return tid;
193 }
194
195 TypeId
GetInstanceTypeId(void) const196 TcpOptionMSS::GetInstanceTypeId (void) const
197 {
198 return GetTypeId ();
199 }
200
201 void
Print(std::ostream & os) const202 TcpOptionMSS::Print (std::ostream &os) const
203 {
204 os << "MSS:" << m_mss;
205 }
206
207 uint32_t
GetSerializedSize(void) const208 TcpOptionMSS::GetSerializedSize (void) const
209 {
210 return 4;
211 }
212
213 void
Serialize(Buffer::Iterator start) const214 TcpOptionMSS::Serialize (Buffer::Iterator start) const
215 {
216 Buffer::Iterator i = start;
217 i.WriteU8 (GetKind ()); // Kind
218 i.WriteU8 (4); // Length
219 i.WriteHtonU16 (m_mss); // Max segment size
220 }
221
222 uint32_t
Deserialize(Buffer::Iterator start)223 TcpOptionMSS::Deserialize (Buffer::Iterator start)
224 {
225 Buffer::Iterator i = start;
226
227 uint8_t readKind = i.ReadU8 ();
228 if (readKind != GetKind ())
229 {
230 NS_LOG_WARN ("Malformed MSS option");
231 return 0;
232 }
233
234 uint8_t size = i.ReadU8 ();
235
236 NS_ABORT_IF (size != 4);
237 m_mss = i.ReadNtohU16 ();
238
239 return GetSerializedSize ();
240 }
241
242 uint8_t
GetKind(void) const243 TcpOptionMSS::GetKind (void) const
244 {
245 return TcpOption::MSS;
246 }
247
248 uint16_t
GetMSS(void) const249 TcpOptionMSS::GetMSS (void) const
250 {
251 return m_mss;
252 }
253
254 void
SetMSS(uint16_t mss)255 TcpOptionMSS::SetMSS (uint16_t mss)
256 {
257 m_mss = mss;
258 }
259
260 } // namespace ns3
261