A Discrete-Event Network Simulator
API
tcp-option.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 #include "tcp-option.h"
21 
22 #include "tcp-option-rfc793.h"
24 #include "tcp-option-sack.h"
25 #include "tcp-option-ts.h"
26 #include "tcp-option-winscale.h"
27 
28 #include "ns3/log.h"
29 #include "ns3/type-id.h"
30 
31 #include <vector>
32 
33 namespace ns3
34 {
35 
36 NS_LOG_COMPONENT_DEFINE("TcpOption");
37 
39 
41 {
42 }
43 
45 {
46 }
47 
48 TypeId
50 {
51  static TypeId tid = TypeId("ns3::TcpOption").SetParent<Object>().SetGroupName("Internet");
52  return tid;
53 }
54 
55 TypeId
57 {
58  return GetTypeId();
59 }
60 
63 {
64  struct KindToTid
65  {
66  TcpOption::Kind kind;
67  TypeId tid;
68  };
69 
70  static ObjectFactory objectFactory;
71  static KindToTid toTid[] = {
80  };
81 
82  for (unsigned int i = 0; i < sizeof(toTid) / sizeof(KindToTid); ++i)
83  {
84  if (toTid[i].kind == kind)
85  {
86  objectFactory.SetTypeId(toTid[i].tid);
87  return objectFactory.Create<TcpOption>();
88  }
89  }
90 
91  return CreateObject<TcpOptionUnknown>();
92 }
93 
94 bool
96 {
97  switch (kind)
98  {
99  case END:
100  case NOP:
101  case MSS:
102  case WINSCALE:
103  case SACKPERMITTED:
104  case SACK:
105  case TS:
106  // Do not add UNKNOWN here
107  return true;
108  }
109 
110  return false;
111 }
112 
114 
116  : TcpOption()
117 {
118  m_kind = 0xFF;
119  m_size = 0;
120 }
121 
123 {
124 }
125 
126 TypeId
128 {
129  static TypeId tid = TypeId("ns3::TcpOptionUnknown")
130  .SetParent<TcpOption>()
131  .SetGroupName("Internet")
132  .AddConstructor<TcpOptionUnknown>();
133  return tid;
134 }
135 
136 TypeId
138 {
139  return GetTypeId();
140 }
141 
142 void
143 TcpOptionUnknown::Print(std::ostream& os) const
144 {
145  os << "Unknown option";
146 }
147 
148 uint32_t
150 {
151  return m_size;
152 }
153 
154 void
156 {
157  if (m_size == 0)
158  {
159  NS_LOG_WARN("Can't Serialize an Unknown Tcp Option");
160  return;
161  }
162 
163  i.WriteU8(GetKind());
164  i.WriteU8(static_cast<uint8_t>(GetSerializedSize()));
165  i.Write(m_content, m_size - 2);
166 }
167 
168 uint32_t
170 {
172 
173  m_kind = i.ReadU8();
174  NS_LOG_WARN("Trying to Deserialize an Unknown Option of Kind " << int(m_kind));
175 
176  m_size = i.ReadU8();
177  if (m_size < 2 || m_size > 40)
178  {
179  NS_LOG_WARN("Unable to parse an unknown option of kind "
180  << int(m_kind) << " with apparent size " << int(m_size));
181  return 0;
182  }
183 
184  i.Read(m_content, m_size - 2);
185 
186  return m_size;
187 }
188 
189 uint8_t
191 {
192  return m_kind;
193 }
194 
195 } // 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 Write(const uint8_t *buffer, uint32_t size)
Definition: buffer.cc:948
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1125
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static TypeId GetTypeId()
Get the type ID.
Base class for all kinds of TCP options.
Definition: tcp-option.h:38
static Ptr< TcpOption > CreateOption(uint8_t kind)
Creates an option.
Definition: tcp-option.cc:62
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition: tcp-option.cc:56
~TcpOption() override
Definition: tcp-option.cc:44
static bool IsKindKnown(uint8_t kind)
Check if the option is implemented.
Definition: tcp-option.cc:95
Kind
The option Kind, as defined in the respective RFCs.
Definition: tcp-option.h:55
@ UNKNOWN
not a standardized value; for unknown recv'd options
Definition: tcp-option.h:65
@ SACKPERMITTED
SACKPERMITTED.
Definition: tcp-option.h:62
@ WINSCALE
WINSCALE.
Definition: tcp-option.h:61
static TypeId GetTypeId()
Get the type ID.
Definition: tcp-option.cc:49
static TypeId GetTypeId()
Get the type ID.
static TypeId GetTypeId()
Get the type ID.
static TypeId GetTypeId()
Get the type ID.
static TypeId GetTypeId()
Get the type ID.
static TypeId GetTypeId()
Get the type ID.
An unknown TCP option.
Definition: tcp-option.h:124
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the Option from a buffer iterator.
Definition: tcp-option.cc:169
void Serialize(Buffer::Iterator start) const override
Serialize the Option to a buffer iterator.
Definition: tcp-option.cc:155
uint8_t GetKind() const override
Get the ‘kind’ (as in RFC 793) of this option.
Definition: tcp-option.cc:190
uint8_t m_content[40]
The option data.
Definition: tcp-option.h:146
void Print(std::ostream &os) const override
Print the Option contents.
Definition: tcp-option.cc:143
static TypeId GetTypeId()
Get the type ID.
Definition: tcp-option.cc:127
uint32_t GetSerializedSize() const override
Returns number of bytes required for Option serialization.
Definition: tcp-option.cc:149
uint32_t m_size
The unknown option size.
Definition: tcp-option.h:145
~TcpOptionUnknown() override
Definition: tcp-option.cc:122
uint8_t m_kind
The unknown option kind.
Definition: tcp-option.h:144
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition: tcp-option.cc:137
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_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.