Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(731)

Side by Side Diff: src/applications/v4ping/v4ping.cc

Issue 115075: AODV model for review (Closed)
Patch Set: Updated after Tom review Created 14 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/applications/v4ping/v4ping.h ('k') | src/contrib/average.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /* 2 /*
3 * This program is free software; you can redistribute it and/or modify 3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as 4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation; 5 * published by the Free Software Foundation;
6 * 6 *
7 * This program is distributed in the hope that it will be useful, 7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details. 10 * GNU General Public License for more details.
11 * 11 *
12 * You should have received a copy of the GNU General Public License 12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software 13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */ 15 */
16 #include "v4ping.h" 16 #include "v4ping.h"
17 #include "ns3/icmpv4.h" 17 #include "ns3/icmpv4.h"
18 #include "ns3/assert.h" 18 #include "ns3/assert.h"
19 #include "ns3/log.h" 19 #include "ns3/log.h"
20 #include "ns3/ipv4-address.h" 20 #include "ns3/ipv4-address.h"
21 #include "ns3/socket.h" 21 #include "ns3/socket.h"
22 #include "ns3/uinteger.h" 22 #include "ns3/uinteger.h"
23 #include "ns3/boolean.h"
23 #include "ns3/inet-socket-address.h" 24 #include "ns3/inet-socket-address.h"
24 #include "ns3/packet.h" 25 #include "ns3/packet.h"
25 #include "ns3/trace-source-accessor.h" 26 #include "ns3/trace-source-accessor.h"
26 #include "ns3/simulator.h"
27 27
28 namespace ns3 { 28 namespace ns3 {
29 29
30 NS_LOG_COMPONENT_DEFINE ("V4Ping"); 30 NS_LOG_COMPONENT_DEFINE ("V4Ping");
31 NS_OBJECT_ENSURE_REGISTERED (V4Ping); 31 NS_OBJECT_ENSURE_REGISTERED (V4Ping);
32 32
33 TypeId· 33 TypeId·
34 V4Ping::GetTypeId (void) 34 V4Ping::GetTypeId (void)
35 { 35 {
36 static TypeId tid = TypeId ("ns3::V4Ping") 36 static TypeId tid = TypeId ("ns3::V4Ping")
37 .SetParent<Application> () 37 .SetParent<Application> ()
38 .AddConstructor<V4Ping> () 38 .AddConstructor<V4Ping> ()
39 .AddAttribute ("Remote",· 39 .AddAttribute ("Remote",·
40 "The address of the machine we want to ping.", 40 "The address of the machine we want to ping.",
41 Ipv4AddressValue (), 41 Ipv4AddressValue (),
42 MakeIpv4AddressAccessor (&V4Ping::m_remote), 42 MakeIpv4AddressAccessor (&V4Ping::m_remote),
43 MakeIpv4AddressChecker ()) 43 MakeIpv4AddressChecker ())
44 .AddAttribute ("Verbose",
45 "Produce usual output.",
46 BooleanValue (false),
47 MakeBooleanAccessor (&V4Ping::m_verbose),
48 MakeBooleanChecker ())
49 .AddAttribute ("Interval", "Wait interval seconds between sending each pac ket.",
50 TimeValue (Seconds (1)),
51 MakeTimeAccessor (&V4Ping::m_interval),
52 MakeTimeChecker ())
53 .AddAttribute ("Size", "The number of data bytes to be sent, real packet wil l be 8 (ICMP) + 20 (IP) bytes longer.",
54 UintegerValue (56),
55 MakeUintegerAccessor (&V4Ping::m_size),
56 MakeUintegerChecker<uint32_t> (16))
44 .AddTraceSource ("Rtt", 57 .AddTraceSource ("Rtt",
45 "The rtt calculated by the ping.", 58 "The rtt calculated by the ping.",
46 MakeTraceSourceAccessor (&V4Ping::m_traceRtt)); 59 MakeTraceSourceAccessor (&V4Ping::m_traceRtt));
47 ; 60 ;
48 return tid; 61 return tid;
49 } 62 }
50 63
51 V4Ping::V4Ping () 64 V4Ping::V4Ping ()
52 : m_socket (0), 65 : m_interval (Seconds (1)),
53 m_seq (0) 66 m_size (56),
54 {} 67 m_socket (0),
68 m_seq (0),
69 m_verbose (false),
70 m_recv (0)
71 {
72 }
55 V4Ping::~V4Ping () 73 V4Ping::~V4Ping ()
56 {} 74 {}
57 75
58 void 76 void
59 V4Ping::DoDispose (void) 77 V4Ping::DoDispose (void)
60 { 78 {
61 NS_LOG_FUNCTION (this); 79 NS_LOG_FUNCTION (this);
62 m_socket = 0; 80 m_socket = 0;
63 Application::DoDispose (); 81 Application::DoDispose ();
64 } 82 }
(...skipping 20 matching lines...) Expand all
85 while (m_socket->GetRxAvailable () > 0) 103 while (m_socket->GetRxAvailable () > 0)
86 { 104 {
87 Address from; 105 Address from;
88 Ptr<Packet> p = m_socket->RecvFrom (0xffffffff, 0, from); 106 Ptr<Packet> p = m_socket->RecvFrom (0xffffffff, 0, from);
89 NS_LOG_DEBUG ("recv " << p->GetSize () << " bytes"); 107 NS_LOG_DEBUG ("recv " << p->GetSize () << " bytes");
90 NS_ASSERT (InetSocketAddress::IsMatchingType (from)); 108 NS_ASSERT (InetSocketAddress::IsMatchingType (from));
91 InetSocketAddress realFrom = InetSocketAddress::ConvertFrom (from); 109 InetSocketAddress realFrom = InetSocketAddress::ConvertFrom (from);
92 NS_ASSERT (realFrom.GetPort () == 1); // protocol should be icmp. 110 NS_ASSERT (realFrom.GetPort () == 1); // protocol should be icmp.
93 Ipv4Header ipv4; 111 Ipv4Header ipv4;
94 p->RemoveHeader (ipv4); 112 p->RemoveHeader (ipv4);
113 uint32_t recvSize = p->GetSize ();
95 NS_ASSERT (ipv4.GetProtocol () == 1); // protocol should be icmp. 114 NS_ASSERT (ipv4.GetProtocol () == 1); // protocol should be icmp.
96 Icmpv4Header icmp; 115 Icmpv4Header icmp;
97 p->RemoveHeader (icmp); 116 p->RemoveHeader (icmp);
98 if (icmp.GetType () == Icmpv4Header::ECHO_REPLY) 117 if (icmp.GetType () == Icmpv4Header::ECHO_REPLY)
99 { 118 {
100 Icmpv4Echo echo; 119 Icmpv4Echo echo;
101 p->RemoveHeader (echo); 120 p->RemoveHeader (echo);
102 » if (echo.GetSequenceNumber () == (m_seq - 1) && 121 » std::map<uint16_t, Time>::iterator i = m_sent.find(echo.GetSequenceNum ber());
103 » echo.GetIdentifier () == 0) 122 »·········
123 » if (i != m_sent.end () && echo.GetIdentifier () == 0)
104 { 124 {
105 uint32_t buf[4]; 125 uint32_t buf[m_size / 4];
106 uint32_t dataSize = echo.GetDataSize (); 126 uint32_t dataSize = echo.GetDataSize ();
107 if (dataSize == sizeof(buf)) 127 if (dataSize == sizeof(buf))
108 { 128 {
109 echo.GetData ((uint8_t *)buf); 129 echo.GetData ((uint8_t *)buf);
110 130
111 if (buf[0] == GetNode ()->GetId () && 131 if (buf[0] == GetNode ()->GetId () &&
112 buf[1] == GetApplicationId ()) 132 buf[1] == GetApplicationId ())
113 { 133 {
114 » » int64_t ts = buf[3]; 134 » » Time sendTime = i->second;
115 » » ts <<= 32;
116 » » ts |= buf[2];
117 » » Time sendTime = TimeStep (ts);
118 NS_ASSERT (Simulator::Now () > sendTime); 135 NS_ASSERT (Simulator::Now () > sendTime);
119 Time delta = Simulator::Now () - sendTime; 136 Time delta = Simulator::Now () - sendTime;
137 ······················
138 m_sent.erase (i);
139 m_avgRtt.Update (delta.GetMilliSeconds());
140 m_recv++;
120 m_traceRtt (delta); 141 m_traceRtt (delta);
142 ······················
143 if (m_verbose)
144 {
145 std::cout << recvSize << " bytes from " << realFrom.Ge tIpv4() << ":"
146 << " icmp_seq=" << echo.GetSequenceNumber ()
147 << " ttl=" << (unsigned)ipv4.GetTtl ()
148 << " time=" << delta.GetMilliSeconds() << " ms\n";
149 }
121 } 150 }
122 } 151 }
123 } 152 }
124 } 153 }
125 } 154 }
126 } 155 }
127 156
128 void 157 void
129 V4Ping::Write32 (uint8_t *buffer, uint32_t data) 158 V4Ping::Write32 (uint8_t *buffer, uint32_t data)
130 { 159 {
131 buffer[0] = (data >> 0) & 0xff; 160 buffer[0] = (data >> 0) & 0xff;
132 buffer[1] = (data >> 8) & 0xff; 161 buffer[1] = (data >> 8) & 0xff;
133 buffer[2] = (data >> 16) & 0xff; 162 buffer[2] = (data >> 16) & 0xff;
134 buffer[3] = (data >> 24) & 0xff; 163 buffer[3] = (data >> 24) & 0xff;
135 } 164 }
136 165
137 void· 166 void·
138 V4Ping::StartApplication (void) 167 V4Ping::Send ()
139 { 168 {
140 NS_LOG_FUNCTION (this);
141 m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::Ipv4R awSocketFactory"));
142 NS_ASSERT (m_socket != 0);
143 m_socket->SetAttribute ("Protocol", UintegerValue (1)); // icmp
144 m_socket->SetRecvCallback (MakeCallback (&V4Ping::Receive, this));
145 InetSocketAddress src = InetSocketAddress (Ipv4Address::GetAny (), 0);
146 int status;
147 status = m_socket->Bind (src);
148 NS_ASSERT (status != -1);
149 InetSocketAddress dst = InetSocketAddress (m_remote, 0);
150 status = m_socket->Connect (dst);
151 NS_ASSERT (status != -1);
152 Ptr<Packet> p = Create<Packet> (); 169 Ptr<Packet> p = Create<Packet> ();
153 Icmpv4Echo echo; 170 Icmpv4Echo echo;
154 echo.SetSequenceNumber (m_seq); 171 echo.SetSequenceNumber (m_seq);
155 m_seq++; 172 m_seq++;
156 echo.SetIdentifier (0); 173 echo.SetIdentifier (0);
157 174
158 // 175 //
159 // We must write quantities out in some form of network order. Since there 176 // We must write quantities out in some form of network order. Since there
160 // isn't an htonl to work with we just follow the convention in pcap traces 177 // isn't an htonl to work with we just follow the convention in pcap traces
161 // (where any difference would show up anyway) and borrow that code. Don't 178 // (where any difference would show up anyway) and borrow that code. Don't
162 // be too surprised when you see that this is a little endian convention. 179 // be too surprised when you see that this is a little endian convention.
163 // 180 //
164 uint8_t data[4 * sizeof(uint32_t)]; 181 uint8_t data[m_size];
182 for (uint32_t i = 0; i < m_size; ++i) data[i] = 0;
183 NS_ASSERT (m_size >= 16);
184 ··
165 uint32_t tmp = GetNode ()->GetId (); 185 uint32_t tmp = GetNode ()->GetId ();
166 Write32 (&data[0 * sizeof(uint32_t)], tmp); 186 Write32 (&data[0 * sizeof(uint32_t)], tmp);
167 187
168 tmp = GetApplicationId (); 188 tmp = GetApplicationId ();
169 Write32 (&data[1 * sizeof(uint32_t)], tmp); 189 Write32 (&data[1 * sizeof(uint32_t)], tmp);
170 190
171 int64_t now = Simulator::Now ().GetTimeStep (); 191 Ptr<Packet> dataPacket = Create<Packet> ((uint8_t *) &data, m_size);
172 tmp = now & 0xffffffff;
173 Write32 (&data[2 * sizeof(uint32_t)], tmp);
174
175 now >>= 32;
176 tmp = now & 0xffffffff;
177 Write32 (&data[3 * sizeof(uint32_t)], tmp);
178
179 Ptr<Packet> dataPacket = Create<Packet> ((uint8_t *) &data, 16);
180 echo.SetData (dataPacket); 192 echo.SetData (dataPacket);
181 p->AddHeader (echo); 193 p->AddHeader (echo);
182 Icmpv4Header header; 194 Icmpv4Header header;
183 header.SetType (Icmpv4Header::ECHO); 195 header.SetType (Icmpv4Header::ECHO);
184 header.SetCode (0); 196 header.SetCode (0);
185 p->AddHeader (header); 197 p->AddHeader (header);
186 m_socket->Send (p, 0); 198 m_socket->Send (p, 0);
199 m_sent.insert (std::make_pair (m_seq - 1, Simulator::Now()));··
200 m_next = Simulator::Schedule (m_interval, & V4Ping::Send, this);
201 }
202
203 void·
204 V4Ping::StartApplication (void)
205 {
206 NS_LOG_FUNCTION (this);
187 ·· 207 ··
208 m_started = Simulator::Now ();
209 if (m_verbose)
210 {
211 std::cout << "PING " << m_remote << " 56(84) bytes of data.\n";
212 }
213 ··
214 m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::Ipv4R awSocketFactory"));
215 NS_ASSERT (m_socket != 0);
216 m_socket->SetAttribute ("Protocol", UintegerValue (1)); // icmp
217 m_socket->SetRecvCallback (MakeCallback (&V4Ping::Receive, this));
218 InetSocketAddress src = InetSocketAddress (Ipv4Address::GetAny (), 0);
219 int status;
220 status = m_socket->Bind (src);
221 NS_ASSERT (status != -1);
222 InetSocketAddress dst = InetSocketAddress (m_remote, 0);
223 status = m_socket->Connect (dst);
224 NS_ASSERT (status != -1);
225 ··
226 Send ();
188 } 227 }
189 void· 228 void·
190 V4Ping::StopApplication (void) 229 V4Ping::StopApplication (void)
191 { 230 {
192 NS_LOG_FUNCTION (this); 231 NS_LOG_FUNCTION (this);
232 m_next.Cancel();
193 m_socket->Close (); 233 m_socket->Close ();
234 ··
235 if (m_verbose)
236 {
237 std::ostringstream os;
238 os.precision (4);
239 os << "--- " << m_remote << " ping statistics ---\n"·
240 << m_seq << " packets transmitted, " << m_recv << " received, "·
241 << ((m_seq - m_recv) * 100 / m_seq) << "% packet loss, "
242 << "time " << (Simulator::Now () - m_started).GetMilliSeconds () << "ms\n";
243 ······
244 if (m_avgRtt.Count () > 0)
245 os << "rtt min/avg/max/mdev = " << m_avgRtt.Min() << "/" << m_avgRtt.Avg () << "/"
246 << m_avgRtt.Max() << "/" << m_avg Rtt.Err()
247 << " ms\n";
248 std::cout << os.str();
249 }
194 } 250 }
195 251
196 252
197 } // namespace ns3 253 } // namespace ns3
OLDNEW
« no previous file with comments | « src/applications/v4ping/v4ping.h ('k') | src/contrib/average.h » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b