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  .AddConstructor<TcpOptionTS> ()
47  ;
48  return tid;
49 }
50 
51 TypeId
53 {
54  return GetTypeId ();
55 }
56 
57 void
58 TcpOptionTS::Print (std::ostream &os) const
59 {
60  os << m_timestamp << ";" << m_echo;
61 }
62 
63 uint32_t
65 {
66  return 10;
67 }
68 
69 void
71 {
73  i.WriteU8 (GetKind ()); // Kind
74  i.WriteU8 (10); // Length
75  i.WriteHtonU32 (m_timestamp); // Local timestamp
76  i.WriteHtonU32 (m_echo); // Echo timestamp
77 }
78 
79 uint32_t
81 {
83 
84  uint8_t readKind = i.ReadU8 ();
85  if (readKind != GetKind ())
86  {
87  NS_LOG_WARN ("Malformed Timestamp option");
88  return 0;
89  }
90 
91  uint8_t size = i.ReadU8 ();
92  if (size != 10)
93  {
94  NS_LOG_WARN ("Malformed Timestamp option");
95  return 0;
96  }
97  m_timestamp = i.ReadNtohU32 ();
98  m_echo = i.ReadNtohU32 ();
99  return GetSerializedSize ();
100 }
101 
102 uint8_t
104 {
105  return TcpOption::TS;
106 }
107 
108 uint32_t
110 {
111  return m_timestamp;
112 }
113 
114 uint32_t
116 {
117  return m_echo;
118 }
119 
120 void
122 {
123  m_timestamp = ts;
124 }
125 
126 void
127 TcpOptionTS::SetEcho (uint32_t ts)
128 {
129  m_echo = ts;
130 }
131 
132 uint32_t
134 {
135  uint64_t now = (uint64_t) Simulator::Now ().GetMilliSeconds ();
136 
137  // high: (now & 0xFFFFFFFF00000000ULL) >> 32;
138  // low: now & 0xFFFFFFFF
139  return (now & 0xFFFFFFFF);
140 }
141 
142 Time
144 {
145  uint64_t now64 = (uint64_t) Simulator::Now ().GetMilliSeconds ();
146  uint32_t now32 = now64 & 0xFFFFFFFF;
147 
148  Time ret = Seconds (0.0);
149  if (now32 > echoTime)
150  {
151  ret = MilliSeconds (now32 - echoTime);
152  }
153 
154  return ret;
155 }
156 
157 } // 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:95
#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:867
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:977
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:223
void WriteHtonU32(uint32_t data)
Definition: buffer.h:931
static uint32_t NowToTsValue()
Return an uint32_t value which represent "now".
virtual void Print(std::ostream &os) const
Print the Option contents.
void WriteU8(uint8_t data)
Definition: buffer.h:876
#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:859
uint8_t ReadU8(void)
Definition: buffer.h:1028
Base class for all kinds of TCP options.
Definition: tcp-option.h:34
uint32_t m_timestamp
local timestamp
a unique identifier for an interface.
Definition: type-id.h:51
int64_t GetMilliSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:331
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
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.