A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("TcpOptionTS");
25 namespace ns3 {
26 
27 NS_OBJECT_ENSURE_REGISTERED (TcpOptionTS);
28 
30  : TcpOption (),
31  m_timestamp (0),
32  m_echo (0)
33 {
34 }
35 
37 {
38 }
39 
40 TypeId
42 {
43  static TypeId tid = TypeId ("ns3::TcpOptionTS")
44  .SetParent<TcpOption> ()
45  .AddConstructor<TcpOptionTS> ()
46  ;
47  return tid;
48 }
49 
50 TypeId
52 {
53  return GetTypeId ();
54 }
55 
56 void
57 TcpOptionTS::Print (std::ostream &os) const
58 {
59  os << m_timestamp << ";" << m_echo;
60 }
61 
62 uint32_t
64 {
65  return 10;
66 }
67 
68 void
70 {
72  i.WriteU8 (GetKind ()); // Kind
73  i.WriteU8 (10); // Length
74  i.WriteHtonU32 (m_timestamp); // Local timestamp
75  i.WriteHtonU32 (m_echo); // Echo timestamp
76 }
77 
78 uint32_t
80 {
82 
83  uint8_t readKind = i.ReadU8 ();
84  if (readKind != GetKind ())
85  {
86  NS_LOG_WARN ("Malformed Timestamp option");
87  return 0;
88  }
89 
90  uint8_t size = i.ReadU8 ();
91  if (size != 10)
92  {
93  NS_LOG_WARN ("Malformed Timestamp option");
94  return 0;
95  }
96  m_timestamp = i.ReadNtohU32 ();
97  m_echo = i.ReadNtohU32 ();
98  return GetSerializedSize ();
99 }
100 
101 uint8_t
103 {
104  return TcpOption::TS;
105 }
106 
107 uint32_t
109 {
110  return m_timestamp;
111 }
112 
113 uint32_t
115 {
116  return m_echo;
117 }
118 
119 void
121 {
122  m_timestamp = ts;
123 }
124 
125 void
126 TcpOptionTS::SetEcho (uint32_t ts)
127 {
128  m_echo = ts;
129 }
130 
131 uint32_t
133 {
134  uint64_t now = (uint64_t) Simulator::Now ().GetMilliSeconds ();
135 
136  // high: (now & 0xFFFFFFFF00000000ULL) >> 32;
137  // low: now & 0xFFFFFFFF
138  return (now & 0xFFFFFFFF);
139 }
140 
141 Time
143 {
144  uint64_t now64 = (uint64_t) Simulator::Now ().GetMilliSeconds ();
145  uint32_t now32 = now64 & 0xFFFFFFFF;
146 
147  Time ret = Seconds (0.0);
148  if (now32 > echoTime)
149  {
150  ret = MilliSeconds (now32 - echoTime);
151  }
152 
153  return ret;
154 }
155 
156 } // 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 the class in the ns-3 factory.
Definition: object-base.h:38
static Time ElapsedTimeFromTsValue(uint32_t echoTime)
Estimate the Time elapsed from a TS echo value.
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:170
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:853
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
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
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:203
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:845
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:49
int64_t GetMilliSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:326
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
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.