A Discrete-Event Network Simulator
API
tcp-option-ts.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-ts.h"
22 #include "ns3/log.h"
23 
24 namespace ns3 {
25 
26 NS_LOG_COMPONENT_DEFINE ("TcpOptionTS");
27 
28 NS_OBJECT_ENSURE_REGISTERED (TcpOptionTS);
29 
31  : TcpOption (),
32  m_timestamp (0),
33  m_echo (0)
34 {
35 }
36 
38 {
39 }
40 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::TcpOptionTS")
45  .SetParent<TcpOption> ()
46  .SetGroupName ("Internet")
47  .AddConstructor<TcpOptionTS> ()
48  ;
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  }
98  m_timestamp = i.ReadNtohU32 ();
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
128 TcpOptionTS::SetEcho (uint32_t ts)
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
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
static Time ElapsedTimeFromTsValue(uint32_t echoTime)
Estimate the Time elapsed from a TS echo value.
def start()
Definition: core.py:1482
virtual uint8_t GetKind(void) const
Get the `kind' (as in RFC 793) of this option.
void SetTimestamp(uint32_t ts)
Set the timestamp stored in the Option.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
virtual ~TcpOptionTS()
static TypeId GetTypeId(void)
Get the type ID.
void SetEcho(uint32_t ts)
Set the timestamp echo stored in the Option.
uint32_t ReadNtohU32(void)
Definition: buffer.h:969
iterator in a Buffer instance
Definition: buffer.h:98
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
virtual TypeId GetInstanceTypeId(void) const
Implement the GetInstanceTypeId method defined in ObjectBase.
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
void WriteHtonU32(uint32_t data)
Definition: buffer.h:923
static uint32_t NowToTsValue()
Return an uint32_t value which represent "now".
Defines the TCP option of kind 8 (timestamp option) as in RFC 1323
Definition: tcp-option-ts.h:35
virtual void Print(std::ostream &os) const
Print the Option contents.
void WriteU8(uint8_t data)
Definition: buffer.h:868
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
uint8_t ReadU8(void)
Definition: buffer.h:1020
Base class for all kinds of TCP options.
Definition: tcp-option.h:36
uint32_t m_timestamp
local timestamp
a unique identifier for an interface.
Definition: type-id.h:58
int64_t GetMilliSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:345
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
uint32_t GetTimestamp(void) const
Get the timestamp stored in the Option.
uint32_t m_echo
echo timestamp
uint32_t GetEcho(void) const
Get the timestamp echo stored in the Option.