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
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: tcp-option.cc:59
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
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
def start()
Definition: core.py:1858
virtual uint8_t GetKind(void) const
Get the ‘kind’ (as in RFC 793) of this option.
Definition: tcp-option.cc:194
virtual ~TcpOption()
Definition: tcp-option.cc:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
static TypeId GetTypeId(void)
Get the type ID.
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
Definition: tcp-option.cc:160
iterator in a Buffer instance
Definition: buffer.h:98
virtual void Print(std::ostream &os) const
Print the Option contents.
Definition: tcp-option.cc:148
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
virtual ~TcpOptionUnknown()
Definition: tcp-option.cc:126
An unknown TCP option.
Definition: tcp-option.h:122
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TypeId GetTypeId(void)
Get the type ID.
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1123
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
Definition: tcp-option.cc:174
uint8_t m_content[40]
The option data.
Definition: tcp-option.h:145
uint32_t m_size
The unknown option size.
Definition: tcp-option.h:144
not a standardized value; for unknown recv&#39;d options
Definition: tcp-option.h:64
static TypeId GetTypeId(void)
Get the type ID.
Instantiate subclasses of ns3::Object.
static TypeId GetTypeId(void)
Get the type ID.
void WriteU8(uint8_t data)
Definition: buffer.h:869
static bool IsKindKnown(uint8_t kind)
Check if the option is implemented.
Definition: tcp-option.cc:99
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:264
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-option.cc:49
static TypeId GetTypeId(void)
Get the type ID.
uint8_t m_kind
The unknown option kind.
Definition: tcp-option.h:143
Kind
The option Kind, as defined in the respective RFCs.
Definition: tcp-option.h:53
uint8_t ReadU8(void)
Definition: buffer.h:1021
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:953
Base class for all kinds of TCP options.
Definition: tcp-option.h:36
static TypeId GetTypeId(void)
Get the type ID.
A base class which provides memory management and object aggregation.
Definition: object.h:87
a unique identifier for an interface.
Definition: type-id.h:58
static TypeId GetTypeId(void)
Get the type ID.
static Ptr< TcpOption > CreateOption(uint8_t kind)
Creates an option.
Definition: tcp-option.cc:65
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-option.cc:131