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