A Discrete-Event Network Simulator
API
tcp-option-ts.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-ts.h"
21 
22 #include "ns3/log.h"
23 
24 namespace ns3
25 {
26 
27 NS_LOG_COMPONENT_DEFINE("TcpOptionTS");
28 
29 NS_OBJECT_ENSURE_REGISTERED(TcpOptionTS);
30 
32  : TcpOption(),
33  m_timestamp(0),
34  m_echo(0)
35 {
36 }
37 
39 {
40 }
41 
42 TypeId
44 {
45  static TypeId tid = TypeId("ns3::TcpOptionTS")
47  .SetGroupName("Internet")
48  .AddConstructor<TcpOptionTS>();
49  return tid;
50 }
51 
52 TypeId
54 {
55  return GetTypeId();
56 }
57 
58 void
59 TcpOptionTS::Print(std::ostream& os) const
60 {
61  os << m_timestamp << ";" << m_echo;
62 }
63 
64 uint32_t
66 {
67  return 10;
68 }
69 
70 void
72 {
74  i.WriteU8(GetKind()); // Kind
75  i.WriteU8(10); // Length
76  i.WriteHtonU32(m_timestamp); // Local timestamp
77  i.WriteHtonU32(m_echo); // Echo timestamp
78 }
79 
80 uint32_t
82 {
84 
85  uint8_t readKind = i.ReadU8();
86  if (readKind != GetKind())
87  {
88  NS_LOG_WARN("Malformed Timestamp option");
89  return 0;
90  }
91 
92  uint8_t size = i.ReadU8();
93  if (size != 10)
94  {
95  NS_LOG_WARN("Malformed Timestamp option");
96  return 0;
97  }
99  m_echo = i.ReadNtohU32();
100  return GetSerializedSize();
101 }
102 
103 uint8_t
105 {
106  return TcpOption::TS;
107 }
108 
109 uint32_t
111 {
112  return m_timestamp;
113 }
114 
115 uint32_t
117 {
118  return m_echo;
119 }
120 
121 void
123 {
124  m_timestamp = ts;
125 }
126 
127 void
129 {
130  m_echo = ts;
131 }
132 
133 uint32_t
135 {
136  uint64_t now = (uint64_t)Simulator::Now().GetMilliSeconds();
137 
138  // high: (now & 0xFFFFFFFF00000000ULL) >> 32;
139  // low: now & 0xFFFFFFFF
140  return (now & 0xFFFFFFFF);
141 }
142 
143 Time
145 {
146  uint64_t now64 = (uint64_t)Simulator::Now().GetMilliSeconds();
147  uint32_t now32 = now64 & 0xFFFFFFFF;
148 
149  Time ret = Seconds(0.0);
150  if (now32 > echoTime)
151  {
152  ret = MilliSeconds(now32 - echoTime);
153  }
154 
155  return ret;
156 }
157 
158 } // 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
uint32_t ReadNtohU32()
Definition: buffer.h:978
void WriteHtonU32(uint32_t data)
Definition: buffer.h:933
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Base class for all kinds of TCP options.
Definition: tcp-option.h:38
Defines the TCP option of kind 8 (timestamp option) as in RFC 1323
Definition: tcp-option-ts.h:37
uint8_t GetKind() const override
Get the ‘kind’ (as in RFC 793) of this option.
void SetTimestamp(uint32_t ts)
Set the timestamp stored in the Option.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
static Time ElapsedTimeFromTsValue(uint32_t echoTime)
Estimate the Time elapsed from a TS echo value.
uint32_t GetSerializedSize() const override
Returns number of bytes required for Option serialization.
static TypeId GetTypeId()
Get the type ID.
uint32_t GetTimestamp() const
Get the timestamp stored in the Option.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the Option from a buffer iterator.
uint32_t m_timestamp
local timestamp
~TcpOptionTS() override
uint32_t GetEcho() const
Get the timestamp echo stored in the Option.
static uint32_t NowToTsValue()
Return an uint32_t value which represent "now".
void Serialize(Buffer::Iterator start) const override
Serialize the Option to a buffer iterator.
uint32_t m_echo
echo timestamp
void SetEcho(uint32_t ts)
Set the timestamp echo stored in the Option.
void Print(std::ostream &os) const override
Print the Option contents.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetMilliSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:408
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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Every class exported by the ns3 library is enclosed in the ns3 namespace.