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"
25 
26 #include "ns3/type-id.h"
27 #include "ns3/log.h"
28 
29 #include <vector>
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("TcpOption");
34 
35 NS_OBJECT_ENSURE_REGISTERED (TcpOption);
36 
37 
39 {
40 }
41 
43 {
44 }
45 
46 TypeId
48 {
49  static TypeId tid = TypeId ("ns3::TcpOption")
50  .SetParent<Object> ()
51  .SetGroupName ("Internet")
52  ;
53  return tid;
54 }
55 
56 TypeId
58 {
59  return GetTypeId ();
60 }
61 
64 {
65  struct kindToTid
66  {
67  TcpOption::Kind kind;
68  TypeId tid;
69  };
70 
71  static ObjectFactory objectFactory;
72  static kindToTid toTid[] =
73  {
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
95 TcpOption::IsKindKnown (uint8_t kind)
96 {
97  switch (kind)
98  {
99  case END:
100  case NOP:
101  case MSS:
102  case WINSCALE:
103  case TS:
104  // Do not add UNKNOWN here
105  return true;
106  }
107 
108  return false;
109 }
110 
112 
114  : TcpOption ()
115 {
116  m_kind = 0xFF;
117  m_size = 0;
118 }
119 
121 {
122 }
123 
124 TypeId
126 {
127  static TypeId tid = TypeId ("ns3::TcpOptionUnknown")
128  .SetParent<TcpOption> ()
129  .SetGroupName ("Internet")
130  .AddConstructor<TcpOptionUnknown> ()
131  ;
132  return tid;
133 }
134 
135 TypeId
137 {
138  return GetTypeId ();
139 }
140 
141 void
142 TcpOptionUnknown::Print (std::ostream &os) const
143 {
144  os << "Unknown option";
145 }
146 
147 uint32_t
149 {
150  return m_size;
151 }
152 
153 void
155 {
156  if (m_size == 0)
157  {
158  NS_LOG_WARN ("Can't Serialize an Unknown Tcp Option");
159  return;
160  }
161 
162  i.WriteU8 (GetKind ());
163  i.WriteU8 (GetSerializedSize ());
164  i.Write (m_content, m_size-2);
165 }
166 
167 uint32_t
169 {
171 
172  m_kind = i.ReadU8 ();
173  NS_LOG_WARN ("Trying to Deserialize an Unknown Option of Kind " << int (m_kind));
174 
175  m_size = i.ReadU8 ();
176  if (m_size < 2 || m_size > 40)
177  {
178  NS_LOG_WARN ("Unable to parse an unknown option of kind " << int (m_kind) << " with apparent size " << int (m_size));
179  return 0;
180  }
181 
182  i.Read (m_content, m_size-2);
183 
184  return m_size;
185 }
186 
187 uint8_t
189 {
190  return m_kind;
191 }
192 
193 } // namespace ns3
virtual uint8_t GetKind(void) const
Get the `kind' (as in RFC 793) of this option.
Definition: tcp-option.cc:188
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
def start()
Definition: core.py:1482
not a standardized value; for unknown recv'd options
Definition: tcp-option.h:62
virtual ~TcpOption()
Definition: tcp-option.cc:42
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
static TypeId GetTypeId(void)
Get the type ID.
iterator in a Buffer instance
Definition: buffer.h:98
virtual void Print(std::ostream &os) const
Print the Option contents.
Definition: tcp-option.cc:142
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
virtual ~TcpOptionUnknown()
Definition: tcp-option.cc:120
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
Definition: tcp-option.cc:154
An unknown TCP option.
Definition: tcp-option.h:120
Kind
The option Kind, as defined in the respective RFCs.
Definition: tcp-option.h:53
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:168
uint8_t m_content[40]
The option data.
Definition: tcp-option.h:143
uint32_t m_size
The unknown option size.
Definition: tcp-option.h:142
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
Definition: tcp-option.cc:148
Instantiate subclasses of ns3::Object.
static TypeId GetTypeId(void)
Get the type ID.
void WriteU8(uint8_t data)
Definition: buffer.h:868
static bool IsKindKnown(uint8_t kind)
Check if the option is implemented.
Definition: tcp-option.cc:95
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-option.cc:47
static TypeId GetTypeId(void)
Get the type ID.
uint8_t m_kind
The unknown option kind.
Definition: tcp-option.h:141
uint8_t ReadU8(void)
Definition: buffer.h:1020
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
A base class which provides memory management and object aggregation.
Definition: object.h:87
virtual TypeId GetInstanceTypeId(void) const
Implement the GetInstanceTypeId method defined in ObjectBase.
Definition: tcp-option.cc:57
virtual TypeId GetInstanceTypeId(void) const
Implement the GetInstanceTypeId method defined in ObjectBase.
Definition: tcp-option.cc:136
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:63
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-option.cc:125