A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("TcpOption");
32 
33 namespace ns3 {
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  ;
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[] =
72  {
79  };
80 
81  for (unsigned int i = 0; i < sizeof (toTid) / sizeof (kindToTid); ++i)
82  {
83  if (toTid[i].kind == kind)
84  {
85  objectFactory.SetTypeId (toTid[i].tid);
86  return objectFactory.Create<TcpOption> ();
87  }
88  }
89 
90  return CreateObject<TcpOptionUnknown> ();
91 }
92 
93 bool
94 TcpOption::IsKindKnown (uint8_t kind)
95 {
96  switch (kind)
97  {
98  case END:
99  case NOP:
100  case MSS:
101  case WINSCALE:
102  case TS:
103  // Do not add UNKNOWN here
104  return true;
105  }
106 
107  return false;
108 }
109 
111 
113  : TcpOption ()
114 {
115  m_kind = 0xFF;
116  m_size = 0;
117 }
118 
120 {
121 }
122 
123 TypeId
125 {
126  static TypeId tid = TypeId ("ns3::TcpOptionUnknown")
127  .SetParent<TcpOption> ()
128  .AddConstructor<TcpOptionUnknown> ()
129  ;
130  return tid;
131 }
132 
133 TypeId
135 {
136  return GetTypeId ();
137 }
138 
139 void
140 TcpOptionUnknown::Print (std::ostream &os) const
141 {
142  os << "Unknown option";
143 }
144 
145 uint32_t
147 {
148  return m_size;
149 }
150 
151 void
153 {
154  if (m_size == 0)
155  {
156  NS_LOG_WARN ("Can't Serialize an Unknown Tcp Option");
157  return;
158  }
159 
160  i.WriteU8 (GetKind ());
161  i.WriteU8 (GetSerializedSize ());
162  i.Write (m_content, m_size-2);
163 }
164 
165 uint32_t
167 {
169 
170  m_kind = i.ReadU8 ();
171  NS_LOG_WARN ("Trying to Deserialize an Unknown Option of Kind " << int (m_kind));
172 
173  m_size = i.ReadU8 ();
174  if (m_size < 2 || m_size > 40)
175  {
176  NS_LOG_WARN ("Unable to parse an unknown option of kind " << int (m_kind) << " with apparent size " << int (m_size));
177  return 0;
178  }
179 
180  i.Read (m_content, m_size-2);
181 
182  return m_size;
183 }
184 
185 uint8_t
187 {
188  return m_kind;
189 }
190 
191 } // namespace ns3
virtual uint8_t GetKind(void) const
Get the `kind' (as in RFC 793) of this option.
Definition: tcp-option.cc:186
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
virtual ~TcpOption()
Definition: tcp-option.cc:42
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
void SetTypeId(TypeId tid)
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:140
Ptr< Object > Create(void) const
virtual ~TcpOptionUnknown()
Definition: tcp-option.cc:119
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
Definition: tcp-option.cc:152
An unknown TCP option.
Definition: tcp-option.h:116
static TypeId GetTypeId(void)
Get the type ID.
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1152
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
Definition: tcp-option.cc:166
uint8_t m_content[40]
The option data.
Definition: tcp-option.h:139
uint32_t m_size
The unknown option size.
Definition: tcp-option.h:138
not a standardized value; for unknown recv'd options
Definition: tcp-option.h:60
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
Definition: tcp-option.cc:146
instantiate subclasses of ns3::Object.
static TypeId GetTypeId(void)
Get the type ID.
void WriteU8(uint8_t data)
Definition: buffer.h:876
static bool IsKindKnown(uint8_t kind)
Check if the option is implemented.
Definition: tcp-option.cc:94
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:203
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:137
Kind
The option Kind, as defined in the respective RFCs.
Definition: tcp-option.h:51
uint8_t ReadU8(void)
Definition: buffer.h:1028
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:982
Base class for all kinds of TCP options.
Definition: tcp-option.h:34
a base class which provides memory management and object aggregation
Definition: object.h:64
virtual TypeId GetInstanceTypeId(void) const
Definition: tcp-option.cc:56
virtual TypeId GetInstanceTypeId(void) const
Definition: tcp-option.cc:134
a unique identifier for an interface.
Definition: type-id.h:49
static TypeId GetTypeId(void)
Get the type ID.
static Ptr< TcpOption > CreateOption(uint8_t kind)
Creates an option.
Definition: tcp-option.cc:62
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-option.cc:124