View | Details | Raw Unified | Return to bug 454
Collapse All | Expand All

(-)a/examples/tcp/tcp-echo-example.cc (+173 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2012
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
 * Authors: Frank Helbert <frank@ime.usp.br>,
19
 *          Luiz Arthur Feitosa dos Santos <luizsan@ime.usp.br> and
20
 *          Rodrigo Campiolo <campiolo@ime.usp.br>
21
 */
22
23
#include <iostream>
24
#include <fstream>
25
26
#include "ns3/core-module.h"
27
#include "ns3/network-module.h"
28
#include "ns3/internet-module.h"
29
#include "ns3/point-to-point-module.h"
30
#include "ns3/applications-module.h"
31
32
using namespace ns3;
33
34
NS_LOG_COMPONENT_DEFINE ("TcpEchoExample");
35
36
// ===========================================================================
37
//
38
//         node 0                 node 1
39
//   +----------------+    +----------------+
40
//   |    ns-3 TCP    |    |    ns-3 TCP    |
41
//   +----------------+    +----------------+
42
//   |    10.1.1.1    |    |    10.1.1.2    |
43
//   +----------------+    +----------------+
44
//   | point-to-point |    | point-to-point |
45
//   +----------------+    +----------------+
46
//           |                     |
47
//           +---------------------+
48
//                100 Mbps, 1 ms
49
//
50
// This is a simple test of TCP connection. We create two nodes,
51
// one will be a TCP client echo and other the TCP server echo, 
52
// the nodes transmit data in the simple point-to-point channel. 
53
// In this scenario the node 0 (client) send one packet TCP to 
54
// the node 1 (server), then server receive this packet, return 
55
// this back to the node 0 (client) and finish TCP connection.
56
//
57
// ===========================================================================
58
//
59
60
int
61
main (int argc, char *argv[])
62
{
63
  
64
  //
65
  // The three lines below enable debugging mode. Comment these three lines for disable.
66
  //
67
  LogComponentEnable ("TcpEchoExample", LOG_LEVEL_INFO);
68
  LogComponentEnable ("TcpEchoClientApplication", LOG_LEVEL_ALL);
69
  LogComponentEnable ("TcpEchoServerApplication", LOG_LEVEL_ALL);
70
71
  //
72
  // Allow the user to override any of the defaults and the above Bind() at
73
  // run-time, via command-line arguments
74
  //
75
  bool useV6 = false;
76
  Address serverAddress;
77
78
  CommandLine cmd;
79
  cmd.AddValue ("useIpv6", "Use Ipv6", useV6);
80
  cmd.Parse (argc, argv);
81
  
82
  //
83
  // Create two nodes required by the topology (point-to-point).
84
  //
85
  NS_LOG_INFO ("Create nodes.");
86
  NodeContainer nodes;
87
  nodes.Create (2);
88
89
  //
90
  // Create and configure channel for the communication.
91
  //
92
  NS_LOG_INFO("Create and configuring channels.");
93
  PointToPointHelper pointToPoint;
94
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("100Mbps"));
95
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("1ms"));
96
97
  NetDeviceContainer devices;
98
  devices = pointToPoint.Install (nodes);
99
100
  InternetStackHelper stack;
101
  stack.Install (nodes);
102
  
103
  //
104
  // Now, add IP address in the nodes.
105
  //
106
107
  NS_LOG_INFO("Assign IP Address.");
108
  if (useV6 == false)
109
    {
110
      Ipv4AddressHelper ipv4;
111
      ipv4.SetBase ("10.1.1.0", "255.255.255.0");
112
      Ipv4InterfaceContainer i4 = ipv4.Assign (devices);
113
      // for IPv4 GetAddress (1) is equivalent to GetAddress (1,0)
114
      serverAddress = Address(i4.GetAddress (1));
115
    }
116
  else
117
    {
118
      Ipv6AddressHelper ipv6;
119
      ipv6.NewNetwork ("2001:0000:f00d:cafe::", 64);
120
      Ipv6InterfaceContainer i6 = ipv6.Assign (devices);
121
      // Notice the difference between IPv4 and IPv6.
122
      // In both cases GetAddress (0,0) is the localhost (127.0.0.1 or ::1)
123
      // However in IPv6 GetAddress (1,0) is the Link-Local address
124
      // and GetAddress (1,1) is the Global address.
125
      serverAddress = Address(i6.GetAddress (1,1));
126
    }
127
  
128
  //
129
  // Create a TcpEchoServer on node 1.
130
  //
131
  NS_LOG_INFO("Create Server Application.");
132
  uint16_t port = 7; // well-known echo port number.
133
  TcpEchoServerHelper echoServer (port);
134
  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
135
  serverApps.Start (Seconds (1.0));
136
  serverApps.Stop (Seconds (11.0));
137
  
138
  // Create a TcpEchoClient application to send TCP packet to server.
139
  NS_LOG_INFO("Create Client Application.");
140
  TcpEchoClientHelper echoClient (serverAddress, port);
141
  echoClient.SetAttribute ("MaxPackets", UintegerValue (10));
142
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
143
  echoClient.SetAttribute ("PacketSize", UintegerValue (183));
144
  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
145
  clientApps.Start (Seconds (2.0));
146
  clientApps.Stop (Seconds (10.0));
147
148
#if 0
149
//
150
// Users may find it convenient to initialize echo packets with actual data;
151
// the below lines suggest how to do this
152
//
153
  echoClient.SetFill (clientApps.Get (0), "Hello World");
154
155
  echoClient.SetFill (clientApps.Get (0), 0xa5, 1024);
156
157
  uint8_t fill[] = { 0, 1, 2, 3, 4, 5, 6};
158
  echoClient.SetFill (clientApps.Get (0), fill, sizeof(fill), 1024);
159
#endif
160
161
  //
162
  // Enable packet trace in pcap format and save on file tcp_echo_example.pcap.
163
  // Comment the two lines below to disable this.
164
  AsciiTraceHelper ascii;
165
  pointToPoint.EnablePcapAll("tcp_echo_example");
166
167
  // Start the simulation.
168
  NS_LOG_INFO("Start Simulation.");
169
  Simulator::Run ();
170
  Simulator::Destroy ();
171
  NS_LOG_INFO("Simulation finished.");
172
  return 0;
173
}
(-)a/examples/tcp/wscript (+4 lines)
 Lines 24-26    Link Here 
24
    obj = bld.create_ns3_program('tcp-bulk-send',
24
    obj = bld.create_ns3_program('tcp-bulk-send',
25
                                 ['point-to-point', 'applications', 'internet'])
25
                                 ['point-to-point', 'applications', 'internet'])
26
    obj.source = 'tcp-bulk-send.cc'
26
    obj.source = 'tcp-bulk-send.cc'
27
    
28
    obj = bld.create_ns3_program('tcp-echo-example', 
29
                                 ['point-to-point','applications'])
30
    obj.source = 'tcp-echo-example.cc'
(-)a/src/applications/helper/tcp-echo-helper.cc (+158 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2012
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
 * Authors: Frank Helbert <frank@ime.usp.br>,
19
 *          Luiz Arthur Feitosa dos Santos <luizsan@ime.usp.br> and
20
 *          Rodrigo Campiolo <campiolo@ime.usp.br>
21
 */
22
23
#include "tcp-echo-helper.h"
24
#include "ns3/tcp-echo-server.h"
25
#include "ns3/tcp-echo-client.h"
26
#include "ns3/uinteger.h"
27
#include "ns3/names.h"
28
29
namespace ns3 {
30
31
  
32
TcpEchoServerHelper::TcpEchoServerHelper (uint16_t port)
33
{
34
  m_factory.SetTypeId (TcpEchoServer::GetTypeId ());
35
  SetAttribute ("Port", UintegerValue (port));
36
}
37
38
void
39
TcpEchoServerHelper::SetAttribute (
40
  std::string name,
41
  const AttributeValue &value)
42
{
43
  m_factory.Set (name, value);
44
}
45
46
ApplicationContainer
47
TcpEchoServerHelper::Install (Ptr<Node> node) const
48
{
49
  return ApplicationContainer (InstallPriv (node));
50
}
51
52
ApplicationContainer
53
TcpEchoServerHelper::Install (std::string nodeName) const
54
{
55
  Ptr<Node> node = Names::Find<Node> (nodeName);
56
  return ApplicationContainer (InstallPriv (node));
57
}
58
59
ApplicationContainer
60
TcpEchoServerHelper::Install (NodeContainer c) const
61
{
62
  ApplicationContainer apps;
63
  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
64
    {
65
      apps.Add (InstallPriv (*i));
66
    }
67
68
  return apps;
69
}
70
71
Ptr<Application>
72
TcpEchoServerHelper::InstallPriv (Ptr<Node> node) const
73
{
74
  Ptr<Application> app = m_factory.Create<TcpEchoServer> ();
75
  node->AddApplication (app);
76
77
  return app;
78
}
79
80
TcpEchoClientHelper::TcpEchoClientHelper (Address address, uint16_t port)
81
{
82
  m_factory.SetTypeId (TcpEchoClient::GetTypeId ());
83
  SetAttribute ("RemoteAddress", AddressValue (address));
84
  SetAttribute ("RemotePort", UintegerValue (port));
85
}
86
87
void
88
TcpEchoClientHelper::SetAttribute (
89
  std::string name,
90
  const AttributeValue &value)
91
{
92
  m_factory.Set (name, value);
93
}
94
95
void
96
TcpEchoClientHelper::SetFill (Ptr<Application> app, std::string fill)
97
{
98
  app->GetObject<TcpEchoClient>()->SetFill (fill);
99
}
100
101
void
102
TcpEchoClientHelper::SetFill (Ptr<Application> app, uint8_t fill, uint32_t dataLength)
103
{
104
  app->GetObject<TcpEchoClient>()->SetFill (fill, dataLength);
105
}
106
107
void
108
TcpEchoClientHelper::SetFill (Ptr<Application> app, uint8_t *fill, uint32_t fillLength, uint32_t dataLength)
109
{
110
  app->GetObject<TcpEchoClient>()->SetFill (fill, fillLength, dataLength);
111
}
112
113
ApplicationContainer
114
TcpEchoClientHelper::Install (Ptr<Node> node)
115
{
116
  return ApplicationContainer (InstallPriv (node));
117
}
118
119
ApplicationContainer
120
TcpEchoClientHelper::Install (std::string nodeName)
121
{
122
  Ptr<Node> node = Names::Find<Node> (nodeName);
123
  return ApplicationContainer (InstallPriv (node));
124
}
125
126
ApplicationContainer
127
TcpEchoClientHelper::Install (NodeContainer c)
128
{
129
  ApplicationContainer apps;
130
  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
131
    {
132
      Ptr<Node> node = *i;
133
134
      m_client = m_factory.Create<TcpEchoClient> ();
135
      node->AddApplication (m_client);
136
      apps.Add (m_client);
137
138
    }
139
140
  return apps;
141
}
142
143
Ptr<Application>
144
TcpEchoClientHelper::InstallPriv (Ptr<Node> node)
145
{
146
  m_client = m_factory.Create<TcpEchoClient> ();
147
  node->AddApplication (m_client);
148
149
  return m_client;
150
}
151
152
Ptr<TcpEchoClient>
153
TcpEchoClientHelper::GetClient (void)
154
{
155
  return m_client;
156
}
157
158
} // namespace ns3
(-)a/src/applications/helper/tcp-echo-helper.h (+217 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2012
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
 * Authors: Frank Helbert <frank@ime.usp.br>,
19
 *          Luiz Arthur Feitosa dos Santos <luizsan@ime.usp.br> and
20
 *          Rodrigo Campiolo <campiolo@ime.usp.br>
21
 */
22
23
#ifndef TCP_ECHO_HELPER
24
#define TCP_ECHO_HELPER
25
26
#include <stdint.h>
27
#include "ns3/application-container.h"
28
#include "ns3/node-container.h"
29
#include "ns3/object-factory.h"
30
#include "ns3/ipv4-address.h"
31
32
namespace ns3 {
33
34
class TcpEchoClient;
35
36
/**
37
 * \brief Create a server application which waits for TCP connections. 
38
 * This server receive TCP packets and send back to the client.
39
 * 
40
 */
41
42
class TcpEchoServerHelper
43
{
44
public:
45
  /**
46
   * Create TcpEchoServerHelper which will make life easier for people trying
47
   * to set up simulations with TCP echos.
48
   *
49
   * \param port The port the server will wait on for incoming packets
50
   */
51
  TcpEchoServerHelper (uint16_t port);
52
53
  /**
54
   * Record an attribute to be set in each Application after it is is created.
55
   *
56
   * \param name the name of the attribute to set
57
   * \param value the value of the attribute to set
58
   */
59
  void SetAttribute (std::string name, const AttributeValue &value);
60
61
  /**
62
   * Create a TcpEchoServerApplication on the specified Node.
63
   *
64
   * \param node The node on which to create the Application.  The node is
65
   *             specified by a Ptr<Node>.
66
   *
67
   * \returns An ApplicationContainer holding the Application created,
68
   */
69
  ApplicationContainer Install (Ptr<Node> node) const;
70
71
  /**
72
   * Create a TcpEchoServerApplication on specified node
73
   *
74
   * \param nodeName The node on which to create the application.  The node
75
   *                 is specified by a node name previously registered with
76
   *                 the Object Name Service.
77
   *
78
   * \returns An ApplicationContainer holding the Application created.
79
   */
80
  ApplicationContainer Install (std::string nodeName) const;
81
82
  /**
83
   * \param c The nodes on which to create the Applications.  The nodes
84
   *          are specified by a NodeContainer.
85
   *
86
   * Create one TCP echo server application on each of the Nodes in the
87
   * NodeContainer.
88
   *
89
   * \returns The applications created, one Application per Node in the
90
   *          NodeContainer.
91
   */
92
  ApplicationContainer Install (NodeContainer c) const;
93
94
private:
95
  /**
96
   * \internal
97
   */
98
  Ptr<Application> InstallPriv (Ptr<Node> node) const;
99
100
  ObjectFactory m_factory;
101
};
102
103
/**
104
 * \brief create an application which sends a TCP packet and waits for an echo of this packet
105
 */
106
class TcpEchoClientHelper
107
{
108
public:
109
  /**
110
   * Create TcpEchoClientHelper which will make life easier for people trying
111
   * to set up simulations with TCP echos.
112
   *
113
   * \param ip The IP address of the remote TCP echo server
114
   * \param port The port number of the remote TCP echo server
115
   */
116
  TcpEchoClientHelper (Address ip, uint16_t port);
117
118
  /**
119
   * Record an attribute to be set in each Application after it is created.
120
   *
121
   * \param name the name of the attribute to set
122
   * \param value the value of the attribute to set
123
   */
124
  void SetAttribute (std::string name, const AttributeValue &value);
125
126
  /**
127
   * Given a pointer to a TcpEchoClient application, set the data fill of the TCP
128
   * packet (what is sent as data to the server) to the contents of the fill
129
   * string (including the trailing zero terminator).
130
   *
131
   * \warning The size of resulting echo packets will be automatically adjusted
132
   * to reflect the size of the fill string -- this means that the PacketSize
133
   * attribute may be changed as a result of this call.
134
   *
135
   * \param app Smart pointer to the application (real type must be TcpEchoClient).
136
   * \param fill The string to use as the actual echo data bytes.
137
   */
138
  void SetFill (Ptr<Application> app, std::string fill);
139
140
  /**
141
   * Given a pointer to a TcpEchoClient application, set the data fill of the
142
   * packet (what is sent as data to the server) to the contents of the fill
143
   * byte.
144
   *
145
   * The fill byte will be used to initialize the contents of the data packet.
146
   *
147
   * \warning The size of resulting echo packets will be automatically adjusted
148
   * to reflect the dataLength parameter -- this means that the PacketSize
149
   * attribute may be changed as a result of this call.
150
   *
151
   * \param app Smart pointer to the application (real type must be TcpEchoClient).
152
   * \param fill The byte to be repeated in constructing the packet data.
153
   * \param dataLength The desired length of the resulting echo packet data.
154
   */
155
  void SetFill (Ptr<Application> app, uint8_t fill, uint32_t dataLength);
156
157
  /**
158
   * Given a pointer to a TcpEchoClient application, set the data fill of the
159
   * packet (what is sent as data to the server) to the contents of the fill
160
   * buffer, repeated as many times as is required.
161
   *
162
   * Initializing the fill to the contents of a single buffer is accomplished
163
   * by providing a complete buffer with fillLength set to your desired
164
   * dataLength
165
   *
166
   * \warning The size of resulting echo packets will be automatically adjusted
167
   * to reflect the dataLength parameter -- this means that the PacketSize
168
   * attribute of the Application may be changed as a result of this call.
169
   *
170
   * \param app Smart pointer to the application (real type must be TcpEchoClient).
171
   * \param fill The fill pattern to use when constructing packets.
172
   * \param fillLength The number of bytes in the provided fill pattern.
173
   * \param dataLength The desired length of the final echo data.
174
   */
175
  void SetFill (Ptr<Application> app, uint8_t *fill, uint32_t fillLength, uint32_t dataLength);
176
177
  /**
178
   * Create a TCP echo client application on the specified node.  The Node
179
   * is provided as a Ptr<Node>.
180
   *
181
   * \param node The Ptr<Node> on which to create the TcpEchoClientApplication.
182
   *
183
   * \returns An ApplicationContainer that holds a Ptr<Application> to the
184
   *          application created
185
   */
186
  ApplicationContainer Install (Ptr<Node> node) ;
187
188
  /**
189
   * Create a TCP echo client application on the specified node.  The Node
190
   * is provided as a string name of a Node that has been previously
191
   * associated using the Object Name Service.
192
   *
193
   * \param nodeName The name of the node on which to create the TcpEchoClientApplication
194
   *
195
   * \returns An ApplicationContainer that holds a Ptr<Application> to the
196
   *          application created
197
   */
198
  ApplicationContainer Install (std::string nodeName) ;
199
200
  /**
201
   * \param c the nodes
202
   *
203
   * Create one TCP echo client application on each of the input nodes
204
   *
205
   * \returns the applications created, one application per input node.
206
   */
207
  ApplicationContainer Install (NodeContainer c);
208
  Ptr<TcpEchoClient> GetClient (void);
209
private:
210
  Ptr<Application> InstallPriv (Ptr<Node> node);
211
  ObjectFactory m_factory;
212
  Ptr<TcpEchoClient> m_client;
213
};
214
215
} // namespace ns3
216
217
#endif /* TCP_ECHO_HELPER */
(-)a/src/applications/model/tcp-echo-client.cc (+354 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2012
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
 * Authors: Frank Helbert <frank@ime.usp.br>,
19
 *          Luiz Arthur Feitosa dos Santos <luizsan@ime.usp.br> and
20
 *          Rodrigo Campiolo <campiolo@ime.usp.br>
21
 */
22
23
#include "ns3/log.h"
24
#include "ns3/ipv4-address.h"
25
#include "ns3/ipv6-address.h"
26
#include "ns3/address-utils.h"
27
#include "ns3/nstime.h"
28
#include "ns3/inet-socket-address.h"
29
#include "ns3/inet6-socket-address.h"
30
#include "ns3/socket.h"
31
#include "ns3/simulator.h"
32
#include "ns3/socket-factory.h"
33
#include "ns3/packet.h"
34
#include "ns3/uinteger.h"
35
#include "ns3/trace-source-accessor.h"
36
#include "tcp-echo-client.h"
37
38
namespace ns3 {
39
40
NS_LOG_COMPONENT_DEFINE ("TcpEchoClientApplication");
41
NS_OBJECT_ENSURE_REGISTERED (TcpEchoClient);
42
43
TypeId
44
TcpEchoClient::GetTypeId (void)
45
{
46
  static TypeId tid = TypeId ("ns3::TcpEchoClient")
47
    .SetParent<Application> ()
48
    .AddConstructor<TcpEchoClient> ()
49
    .AddAttribute ("MaxPackets",
50
                   "The maximum number of packets the application will send",
51
                   UintegerValue (100),
52
                   MakeUintegerAccessor (&TcpEchoClient::m_count),
53
                   MakeUintegerChecker<uint32_t> ())
54
    .AddAttribute ("Interval",
55
                   "The time to wait between packets",
56
                   TimeValue (Seconds (1.0)),
57
                   MakeTimeAccessor (&TcpEchoClient::m_interval),
58
                   MakeTimeChecker ())
59
    .AddAttribute ("RemoteAddress",
60
                   "The destination address of the outbound packets",
61
                   AddressValue (),
62
                   MakeAddressAccessor (&TcpEchoClient::m_peerAddress),
63
                   MakeAddressChecker ())
64
    .AddAttribute ("RemotePort",
65
                   "The destination port of the outbound packets",
66
                   UintegerValue (0),
67
                   MakeUintegerAccessor (&TcpEchoClient::m_peerPort),
68
                   MakeUintegerChecker<uint16_t> ())
69
    .AddAttribute ("PacketSize", "Size of echo data in outbound packets",
70
                   UintegerValue (100),
71
                   MakeUintegerAccessor (&TcpEchoClient::SetDataSize,
72
                                         &TcpEchoClient::GetDataSize),
73
                   MakeUintegerChecker<uint32_t> ())
74
    .AddTraceSource ("Tx", "A new packet is created and is sent",
75
                     MakeTraceSourceAccessor (&TcpEchoClient::m_txTrace))
76
  ;
77
  return tid;
78
}
79
80
TcpEchoClient::TcpEchoClient ()
81
{
82
  NS_LOG_FUNCTION_NOARGS ();
83
  m_sent = 0;
84
  m_bytesSent = 0;
85
  m_recvBack = 0;
86
  m_bytesRecvBack = 0;
87
  m_socket = 0;
88
  m_sendEvent = EventId ();
89
  m_data = 0;
90
  m_dataSize = 0;
91
}
92
93
TcpEchoClient::~TcpEchoClient()
94
{
95
  NS_LOG_FUNCTION_NOARGS ();
96
  m_socket = 0;
97
98
  delete [] m_data;
99
  m_data = 0;
100
  m_dataSize = 0;
101
}
102
103
void
104
TcpEchoClient::SetRemote (Address ip, uint16_t port)
105
{
106
  m_peerAddress = ip;
107
  m_peerPort = port;
108
}
109
110
void
111
TcpEchoClient::DoDispose (void)
112
{
113
  NS_LOG_FUNCTION_NOARGS ();
114
  Application::DoDispose ();
115
}
116
117
void
118
TcpEchoClient::StartApplication (void)
119
{
120
  NS_LOG_FUNCTION_NOARGS ();
121
122
  if (m_socket == 0)
123
    {
124
      TypeId tid = TypeId::LookupByName ("ns3::TcpSocketFactory");
125
      m_socket = Socket::CreateSocket (GetNode (), tid);
126
      if (Ipv4Address::IsMatchingType(m_peerAddress) == true)
127
        {
128
          m_socket->Bind();
129
          m_socket->Connect (InetSocketAddress (Ipv4Address::ConvertFrom(m_peerAddress), m_peerPort));
130
        }
131
      else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
132
        {
133
          m_socket->Bind6();
134
          m_socket->Connect (Inet6SocketAddress (Ipv6Address::ConvertFrom(m_peerAddress), m_peerPort));
135
        }
136
    }
137
138
  m_socket->SetRecvCallback (MakeCallback (&TcpEchoClient::ReceivePacket, this));
139
140
  ScheduleTransmit (Seconds (0.));
141
}
142
143
void
144
TcpEchoClient::StopApplication ()
145
{
146
  NS_LOG_FUNCTION_NOARGS ();
147
148
  if (m_socket != 0)
149
    {
150
      m_socket->Close ();
151
      m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
152
      m_socket = 0;
153
    }
154
155
  Simulator::Cancel (m_sendEvent);
156
}
157
158
void
159
TcpEchoClient::SetDataSize (uint32_t dataSize)
160
{
161
  NS_LOG_FUNCTION (dataSize);
162
163
  //
164
  // If the client is setting the echo packet data size this way, we infer
165
  // that she doesn't care about the contents of the packet at all, so
166
  // neither will we.
167
  //
168
  delete [] m_data;
169
  m_data = 0;
170
  m_dataSize = 0;
171
  m_size = dataSize;
172
}
173
174
uint32_t
175
TcpEchoClient::GetDataSize (void) const
176
{
177
  NS_LOG_FUNCTION_NOARGS ();
178
  return m_size;
179
}
180
181
void
182
TcpEchoClient::SetFill (std::string fill)
183
{
184
  NS_LOG_FUNCTION (fill);
185
186
  uint32_t dataSize = fill.size () + 1;
187
188
  if (dataSize != m_dataSize)
189
    {
190
      delete [] m_data;
191
      m_data = new uint8_t [dataSize];
192
      m_dataSize = dataSize;
193
    }
194
195
  memcpy (m_data, fill.c_str (), dataSize);
196
197
  //
198
  // Overwrite packet size attribute.
199
  //
200
  m_size = dataSize;
201
}
202
203
void
204
TcpEchoClient::SetFill (uint8_t fill, uint32_t dataSize)
205
{
206
  if (dataSize != m_dataSize)
207
    {
208
      delete [] m_data;
209
      m_data = new uint8_t [dataSize];
210
      m_dataSize = dataSize;
211
    }
212
213
  memset (m_data, fill, dataSize);
214
215
  //
216
  // Overwrite packet size attribute.
217
  //
218
  m_size = dataSize;
219
}
220
221
void
222
TcpEchoClient::SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize)
223
{
224
  if (dataSize != m_dataSize)
225
    {
226
      delete [] m_data;
227
      m_data = new uint8_t [dataSize];
228
      m_dataSize = dataSize;
229
    }
230
231
  if (fillSize >= dataSize)
232
    {
233
      memcpy (m_data, fill, dataSize);
234
      return;
235
    }
236
237
  //
238
  // Do all but the final fill.
239
  //
240
  uint32_t filled = 0;
241
  while (filled + fillSize < dataSize)
242
    {
243
      memcpy (&m_data[filled], fill, fillSize);
244
      filled += fillSize;
245
    }
246
247
  //
248
  // Last fill may be partial
249
  //
250
  memcpy (&m_data[filled], fill, dataSize - filled);
251
252
  //
253
  // Overwrite packet size attribute.
254
  //
255
  m_size = dataSize;
256
}
257
258
void
259
TcpEchoClient::ScheduleTransmit (Time dt)
260
{
261
  NS_LOG_FUNCTION_NOARGS ();
262
  m_sendEvent = Simulator::Schedule (dt, &TcpEchoClient::Send, this);
263
}
264
265
void
266
TcpEchoClient::Send (void)
267
{
268
  NS_LOG_FUNCTION_NOARGS ();
269
270
  NS_ASSERT (m_sendEvent.IsExpired ());
271
272
  Ptr<Packet> p;
273
  if (m_dataSize)
274
    {
275
      //
276
      // If m_dataSize is non-zero, we have a data buffer of the same size that we
277
      // are expected to copy and send.  This state of affairs is created if one of
278
      // the Fill functions is called.  In this case, m_size must have been set
279
      // to agree with m_dataSize
280
      //
281
      NS_ASSERT_MSG (m_dataSize == m_size, "TcpEchoClient::Send(): m_size and m_dataSize inconsistent");
282
      NS_ASSERT_MSG (m_data, "TcpEchoClient::Send(): m_dataSize but no m_data");
283
      p = Create<Packet> (m_data, m_dataSize);
284
      m_bytesSent += m_dataSize;
285
    }
286
  else
287
    {
288
      //
289
      // If m_dataSize is zero, the client has indicated that she doesn't care
290
      // about the data itself either by specifying the data size by setting
291
      // the corresponding atribute or by not calling a SetFill function.  In
292
      // this case, we don't worry about it either.  But we do allow m_size
293
      // to have a value different from the (zero) m_dataSize.
294
      //
295
      p = Create<Packet> (m_size);
296
      m_bytesSent += m_size;
297
    }
298
  // call to the trace sinks before the packet is actually sent,
299
  // so that tags added to the packet can be sent as well
300
  m_txTrace (p);
301
  m_socket->Send (p);
302
303
  ++m_sent;
304
305
  if (InetSocketAddress::IsMatchingType (m_peerAddress))
306
    {
307
      NS_LOG_INFO ("Sent " << m_size << " bytes to " <<
308
                   InetSocketAddress::ConvertFrom (m_peerAddress));
309
    }
310
  else if (Inet6SocketAddress::IsMatchingType (m_peerAddress))
311
    {
312
      NS_LOG_INFO ("Sent " << m_size << " bytes to " <<
313
                   Inet6SocketAddress::ConvertFrom (m_peerAddress));
314
    }
315
316
  if (m_sent < m_count)
317
    {
318
      ScheduleTransmit (m_interval);
319
    }
320
}
321
322
void
323
TcpEchoClient::ReceivePacket (Ptr<Socket> socket)
324
{
325
  NS_LOG_FUNCTION (this << socket);
326
  Ptr<Packet> packet;
327
  Address from;
328
  while ((packet = socket->RecvFrom (from)))
329
    {
330
      if (InetSocketAddress::IsMatchingType (from))
331
        {
332
          NS_LOG_INFO ("Received " << packet->GetSize () << " bytes from " <<
333
                       InetSocketAddress::ConvertFrom (from).GetIpv4 ());
334
        }
335
      else if (Inet6SocketAddress::IsMatchingType (from))
336
        {
337
          NS_LOG_INFO ("Received " << packet->GetSize () << " bytes from " <<
338
                       Inet6SocketAddress::ConvertFrom (from).GetIpv6 ());
339
        }
340
341
      // don't check if data returned is the same data sent earlier
342
      m_recvBack++;
343
      m_bytesRecvBack += packet->GetSize ();
344
    }
345
346
  if (m_count == m_recvBack)
347
    {
348
	  socket->Close();
349
	  m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
350
	  socket = 0;
351
    }
352
}
353
354
} // Namespace ns3
(-)a/src/applications/model/tcp-echo-client.h (+170 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2012
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
 * Authors: Frank Helbert <frank@ime.usp.br>,
19
 *          Luiz Arthur Feitosa dos Santos <luizsan@ime.usp.br> and
20
 *          Rodrigo Campiolo <campiolo@ime.usp.br>
21
 */
22
23
#ifndef TCP_ECHO_CLIENT
24
#define TCP_ECHO_CLIENT
25
26
27
#include "ns3/application.h"
28
#include "ns3/event-id.h"
29
#include "ns3/ptr.h"
30
#include "ns3/ipv4-address.h"
31
#include "ns3/ipv6-address.h"
32
#include "ns3/traced-callback.h"
33
34
namespace ns3 {
35
36
class Socket;
37
class Packet;
38
39
/**
40
 * \ingroup tcpecho
41
 * \brief A Tcp Echo client
42
 *
43
 * Every packet sent should be returned by the server and received here.
44
 */
45
class TcpEchoClient : public Application
46
{
47
public:
48
  static TypeId GetTypeId (void);
49
50
  TcpEchoClient ();
51
52
  virtual ~TcpEchoClient ();
53
54
  /**
55
   * \param ip destination address
56
   * \param port destination port
57
   */
58
  void SetRemote (Address ip, uint16_t port);
59
60
  /**
61
   * Set the data size of the packet (the number of bytes that are sent as data
62
   * to the server).  The contents of the data are set to unspecified (don't
63
   * care) by this call.
64
   *
65
   * \warning If you have set the fill data for the echo client using one of the
66
   * SetFill calls, this will undo those effects.
67
   *
68
   * \param dataSize The size of the echo data you want to sent.
69
   */
70
  void SetDataSize (uint32_t dataSize);
71
72
  /**
73
   * Get the number of data bytes that will be sent to the server.
74
   *
75
   * \warning The number of bytes may be modified by calling any one of the
76
   * SetFill methods.  If you have called SetFill, then the number of
77
   * data bytes will correspond to the size of an initialized data buffer.
78
   * If you have not called a SetFill method, the number of data bytes will
79
   * correspond to the number of don't care bytes that will be sent.
80
   *
81
   * \returns The number of data bytes.
82
   */
83
  uint32_t GetDataSize (void) const;
84
85
  /**
86
   * Set the data fill of the packet (what is sent as data to the server) to
87
   * the zero-terminated contents of the fill string string.
88
   *
89
   * \warning The size of resulting echo packets will be automatically adjusted
90
   * to reflect the size of the fill string -- this means that the PacketSize
91
   * attribute may be changed as a result of this call.
92
   *
93
   * \param fill The string to use as the actual echo data bytes.
94
   */
95
  void SetFill (std::string fill);
96
97
  /**
98
   * Set the data fill of the packet (what is sent as data to the server) to
99
   * the repeated contents of the fill byte.  i.e., the fill byte will be
100
   * used to initialize the contents of the data packet.
101
   *
102
   * \warning The size of resulting echo packets will be automatically adjusted
103
   * to reflect the dataSize parameter -- this means that the PacketSize
104
   * attribute may be changed as a result of this call.
105
   *
106
   * \param fill The byte to be repeated in constructing the packet data..
107
   * \param dataSize The desired size of the resulting echo packet data.
108
   */
109
  void SetFill (uint8_t fill, uint32_t dataSize);
110
111
  /**
112
   * Set the data fill of the packet (what is sent as data to the server) to
113
   * the contents of the fill buffer, repeated as many times as is required.
114
   *
115
   * Initializing the packet to the contents of a provided single buffer is
116
   * accomplished by setting the fillSize set to your desired dataSize
117
   * (and providing an appropriate buffer).
118
   *
119
   * \warning The size of resulting echo packets will be automatically adjusted
120
   * to reflect the dataSize parameter -- this means that the PacketSize
121
   * attribute of the Application may be changed as a result of this call.
122
   *
123
   * \param fill The fill pattern to use when constructing packets.
124
   * \param fillSize The number of bytes in the provided fill pattern.
125
   * \param dataSize The desired size of the final echo data.
126
   */
127
  void SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize);
128
129
  // mainly to the automated test
130
  uint32_t GetPacketsSent (void) { return m_sent; };
131
  uint64_t GetBytesSent (void) { return m_bytesSent; };
132
  uint32_t GetPacketsReceivedBack (void) { return m_recvBack; };
133
  uint64_t GetBytesReceivedBack (void) { return m_bytesRecvBack; };
134
135
protected:
136
  virtual void DoDispose (void);
137
138
private:
139
140
  virtual void StartApplication (void);
141
  virtual void StopApplication (void);
142
143
  void ScheduleTransmit (Time dt);
144
  void Send (void);
145
146
  void ReceivePacket (Ptr<Socket> socket);
147
148
  uint32_t m_count;
149
  Time m_interval;
150
  uint32_t m_size;
151
152
  uint32_t m_dataSize;
153
  uint8_t *m_data;
154
155
  uint32_t m_sent;
156
  uint64_t m_bytesSent;
157
  uint32_t m_recvBack;
158
  uint64_t m_bytesRecvBack;
159
  Ptr<Socket> m_socket;
160
  Address m_peerAddress;
161
  uint16_t m_peerPort;
162
  EventId m_sendEvent;
163
  /// Callbacks for tracing the packet Tx events
164
  TracedCallback<Ptr<const Packet> > m_txTrace;
165
};
166
167
} // namespace ns3
168
169
170
#endif /* TCP_ECHO_CLIENT */
(-)a/src/applications/model/tcp-echo-server.cc (+183 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2012
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
 * Authors: Frank Helbert <frank@ime.usp.br>,
19
 *          Luiz Arthur Feitosa dos Santos <luizsan@ime.usp.br> and
20
 *          Rodrigo Campiolo <campiolo@ime.usp.br>
21
 */
22
23
#include "ns3/log.h"
24
#include "ns3/ipv4-address.h"
25
#include "ns3/ipv6-address.h"
26
#include "ns3/address-utils.h"
27
#include "ns3/nstime.h"
28
#include "ns3/inet-socket-address.h"
29
#include "ns3/inet6-socket-address.h"
30
#include "ns3/socket.h"
31
#include "ns3/simulator.h"
32
#include "ns3/socket-factory.h"
33
#include "ns3/packet.h"
34
#include "ns3/uinteger.h"
35
36
#include "tcp-echo-server.h"
37
38
namespace ns3 {
39
40
NS_LOG_COMPONENT_DEFINE ("TcpEchoServerApplication");
41
NS_OBJECT_ENSURE_REGISTERED (TcpEchoServer);
42
43
TypeId
44
TcpEchoServer::GetTypeId (void)
45
{
46
  static TypeId tid = TypeId ("ns3::TcpEchoServer")
47
    .SetParent<Application> ()
48
    .AddConstructor<TcpEchoServer> ()
49
    .AddAttribute ("Port", "Port on which we listen for incoming connections.",
50
                   UintegerValue (7),
51
                   MakeUintegerAccessor (&TcpEchoServer::m_port),
52
                   MakeUintegerChecker<uint16_t> ())
53
  ;
54
  return tid;
55
}
56
57
TcpEchoServer::TcpEchoServer ()
58
{
59
  NS_LOG_FUNCTION_NOARGS ();
60
  m_socket = 0;
61
  m_socket6 = 0;
62
  m_running = false;
63
}
64
65
TcpEchoServer::~TcpEchoServer()
66
{
67
  NS_LOG_FUNCTION_NOARGS ();
68
  m_socket = 0;
69
  m_socket6 = 0;
70
}
71
72
void
73
TcpEchoServer::DoDispose (void)
74
{
75
  NS_LOG_FUNCTION_NOARGS ();
76
  Application::DoDispose ();
77
}
78
79
void
80
TcpEchoServer::StartApplication (void)
81
{
82
  NS_LOG_FUNCTION_NOARGS ();
83
84
  m_running = true;
85
86
  if (m_socket == 0)
87
    {
88
      TypeId tid = TypeId::LookupByName ("ns3::TcpSocketFactory");
89
      m_socket = Socket::CreateSocket (GetNode (), tid);
90
      InetSocketAddress listenAddress = InetSocketAddress (Ipv4Address::GetAny (), m_port);
91
      m_socket->Bind (listenAddress);
92
      m_socket->Listen();
93
    }
94
  if (m_socket6 == 0)
95
    {
96
      TypeId tid = TypeId::LookupByName ("ns3::TcpSocketFactory");
97
      m_socket6 = Socket::CreateSocket (GetNode (), tid);
98
      Inet6SocketAddress listenAddress = Inet6SocketAddress (Ipv6Address::GetAny (), m_port);
99
      m_socket6->Bind (listenAddress);
100
      m_socket6->Listen();
101
    }
102
103
  m_socket->SetAcceptCallback (
104
        MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
105
        MakeCallback (&TcpEchoServer::HandleAccept, this));
106
  m_socket6->SetAcceptCallback (
107
        MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
108
        MakeCallback (&TcpEchoServer::HandleAccept, this));
109
}
110
111
void
112
TcpEchoServer::StopApplication ()
113
{
114
  NS_LOG_FUNCTION_NOARGS ();
115
116
  m_running = false;
117
118
  if (m_socket != 0)
119
    {
120
      m_socket->Close ();
121
      m_socket->SetAcceptCallback (
122
            MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
123
            MakeNullCallback<void, Ptr<Socket>, const Address &> () );
124
    }
125
  if (m_socket6 != 0)
126
    {
127
      m_socket6->Close ();
128
      m_socket6->SetAcceptCallback (
129
            MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
130
            MakeNullCallback<void, Ptr<Socket>, const Address &> () );
131
    }
132
}
133
134
void
135
TcpEchoServer::ReceivePacket (Ptr<Socket> s)
136
{
137
  NS_LOG_FUNCTION (this << s);
138
139
  Ptr<Packet> packet;
140
  Address from;
141
  while ((packet = s->RecvFrom (from)))
142
    {
143
      if (packet->GetSize () > 0)
144
        {
145
          if (InetSocketAddress::IsMatchingType (from))
146
            {
147
              NS_LOG_INFO ("Server Received " << packet->GetSize () << " bytes from " <<
148
                           InetSocketAddress::ConvertFrom (from).GetIpv4 ());
149
            }
150
          else if (Inet6SocketAddress::IsMatchingType (from))
151
            {
152
              NS_LOG_INFO ("Server Received " << packet->GetSize () << " bytes from " <<
153
                           Inet6SocketAddress::ConvertFrom (from).GetIpv6 ());
154
            }
155
156
          packet->RemoveAllPacketTags ();
157
          packet->RemoveAllByteTags ();
158
159
          NS_LOG_LOGIC ("Echoing packet");
160
          s->Send (packet);
161
        }
162
    }
163
}
164
165
void TcpEchoServer::HandleAccept (Ptr<Socket> s, const Address& from)
166
{
167
  NS_LOG_FUNCTION (this << s << from);
168
  s->SetRecvCallback (MakeCallback (&TcpEchoServer::ReceivePacket, this));
169
  s->SetCloseCallbacks(MakeCallback (&TcpEchoServer::HandleSuccessClose, this),
170
    MakeNullCallback<void, Ptr<Socket> > () );
171
}
172
173
void TcpEchoServer::HandleSuccessClose(Ptr<Socket> s)
174
{
175
  NS_LOG_FUNCTION (this << s);
176
  NS_LOG_LOGIC ("Client close received");
177
  s->Close();
178
  s->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > () );
179
  s->SetCloseCallbacks(MakeNullCallback<void, Ptr<Socket> > (),
180
      MakeNullCallback<void, Ptr<Socket> > () );
181
}
182
183
} // Namespace ns3
(-)a/src/applications/model/tcp-echo-server.h (+101 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2012
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
 * Authors: Frank Helbert <frank@ime.usp.br>,
19
 *          Luiz Arthur Feitosa dos Santos <luizsan@ime.usp.br> and
20
 *          Rodrigo Campiolo <campiolo@ime.usp.br>
21
 */
22
23
#ifndef TCP_ECHO_SERVER
24
#define TCP_ECHO_SERVER
25
26
#include "ns3/application.h"
27
#include "ns3/event-id.h"
28
#include "ns3/ptr.h"
29
#include "ns3/address.h"
30
31
namespace ns3 {
32
33
class Socket;
34
class Packet;
35
36
/**
37
 * \ingroup applications
38
 * \defgroup TcpEcho 
39
 */
40
41
/**
42
 * \ingroup tcpecho
43
 * \brief A Tcp Echo server
44
 *
45
 * Every packet received is sent back to the client.
46
 */
47
class TcpEchoServer : public Application
48
{
49
public:
50
  static TypeId GetTypeId (void);
51
  TcpEchoServer ();
52
  virtual ~TcpEchoServer ();
53
54
  /**
55
   *
56
   * Receive the packet from client echo on socket level (layer 4), 
57
   * handle the packet and return to the client.
58
   *
59
   * \param socket TCP socket.
60
   *
61
   */
62
  void ReceivePacket(Ptr<Socket> socket);
63
  
64
  /**
65
  *
66
  * Handle packet from accept connections.
67
  *
68
  * \parm s TCP socket.
69
  * \parm from Address from client echo.
70
  */
71
  void HandleAccept (Ptr<Socket> s, const Address& from);
72
  
73
  /**
74
  *
75
  * Handle successful closing connections.
76
  *
77
  * \parm s TCP socket.
78
  *
79
  */
80
  void HandleSuccessClose(Ptr<Socket> s);
81
82
protected:
83
  virtual void DoDispose (void);
84
85
private:
86
87
  virtual void StartApplication (void);
88
  virtual void StopApplication (void);
89
90
  void HandleRead (Ptr<Socket> socket);
91
92
  Ptr<Socket> m_socket;
93
  Ptr<Socket> m_socket6;
94
  uint16_t m_port;
95
  bool m_running;
96
};
97
98
} // namespace ns3
99
100
#endif /* TCP_ECHO_SERVER */
101
(-)a/src/applications/test/tcp-echo-test-suite.cc (+204 lines)
Line 0    Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * Copyright (c) 2012
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
 * Authors: Frank Helbert <frank@ime.usp.br>,
19
 *          Luiz Arthur Feitosa dos Santos <luizsan@ime.usp.br> and
20
 *          Rodrigo Campiolo <campiolo@ime.usp.br>
21
 */
22
23
#include <fstream>
24
#include "ns3/log.h"
25
#include "ns3/abort.h"
26
#include "ns3/config.h"
27
#include "ns3/string.h"
28
#include "ns3/uinteger.h"
29
#include "ns3/address.h"
30
#include "ns3/inet-socket-address.h"
31
#include "ns3/inet6-socket-address.h"
32
#include "ns3/internet-stack-helper.h"
33
#include "ns3/ipv4-address-helper.h"
34
#include "ns3/ipv6-address-helper.h"
35
#include "ns3/tcp-echo-server.h"
36
#include "ns3/tcp-echo-client.h"
37
#include "ns3/tcp-echo-helper.h"
38
#include "ns3/simple-net-device.h"
39
#include "ns3/simple-channel.h"
40
#include "ns3/test.h"
41
#include "ns3/simulator.h"
42
43
using namespace ns3;
44
45
/**
46
 * Test if all tcp packets generated by a tcpEchoClient application are
47
 * correctly received by an tcpEchoServer application.
48
 * 
49
 */
50
class TcpEchoTestCase : public TestCase
51
{
52
public:
53
  TcpEchoTestCase ();
54
  virtual ~TcpEchoTestCase ();
55
56
private:
57
  virtual void DoRun (void);
58
};
59
60
TcpEchoTestCase::TcpEchoTestCase ()
61
  : TestCase ("Tcp-echo test if data sent by an client returns back to server")
62
{
63
}
64
65
TcpEchoTestCase::~TcpEchoTestCase ()
66
{
67
}
68
69
void
70
TcpEchoTestCase::DoRun (void)
71
{
72
  NodeContainer n;
73
  n.Create (2);
74
75
  InternetStackHelper internet;
76
  internet.Install (n);
77
78
  // link the two nodes
79
  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice> ();
80
  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice> ();
81
  n.Get (0)->AddDevice (txDev);
82
  n.Get (1)->AddDevice (rxDev);
83
  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
84
  rxDev->SetChannel (channel1);
85
  txDev->SetChannel (channel1);
86
  NetDeviceContainer d;
87
  d.Add (txDev);
88
  d.Add (rxDev);
89
90
  Ipv4AddressHelper ipv4;
91
92
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
93
  Ipv4InterfaceContainer i = ipv4.Assign (d);
94
95
  uint16_t port = 4000;
96
  TcpEchoServerHelper server (port);
97
  ApplicationContainer apps = server.Install (n.Get (1));
98
  apps.Start (Seconds (1.0));
99
  apps.Stop (Seconds (10.0));
100
101
  uint32_t MaxPacketSize = 183;
102
  Time interPacketInterval = Seconds (1.);
103
  uint32_t maxPacketCount = 5;
104
  TcpEchoClientHelper client (i.GetAddress (1), port);
105
  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
106
  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
107
  client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
108
  apps = client.Install (n.Get (0));
109
  apps.Start (Seconds (2.0));
110
  apps.Stop (Seconds (10.0));
111
112
  Simulator::Run ();
113
  Simulator::Destroy ();
114
115
  NS_TEST_ASSERT_MSG_EQ (client.GetClient ()->GetBytesSent (), client.GetClient ()->GetBytesReceivedBack (), "Bytes received differ from bytes sent !");
116
}
117
118
/**
119
 * Test if all tcp packets generated by a tcpEchoClient application are
120
 * correctly received by an tcpEchoServer apllication - IPv6 version.
121
 *
122
 */
123
class TcpEchoTestCase6 : public TestCase
124
{
125
public:
126
  TcpEchoTestCase6 ();
127
  virtual ~TcpEchoTestCase6 ();
128
129
private:
130
  virtual void DoRun (void);
131
};
132
133
TcpEchoTestCase6::TcpEchoTestCase6 ()
134
  : TestCase ("Tcp-echo test if data sent by an client returns back to server - IPv6")
135
{
136
}
137
138
TcpEchoTestCase6::~TcpEchoTestCase6 ()
139
{
140
}
141
142
void
143
TcpEchoTestCase6::DoRun (void)
144
{
145
  NodeContainer n;
146
  n.Create (2);
147
148
  InternetStackHelper internet;
149
  internet.Install (n);
150
151
  // link the two nodes
152
  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice> ();
153
  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice> ();
154
  n.Get (0)->AddDevice (txDev);
155
  n.Get (1)->AddDevice (rxDev);
156
  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
157
  rxDev->SetChannel (channel1);
158
  txDev->SetChannel (channel1);
159
  NetDeviceContainer d;
160
  d.Add (txDev);
161
  d.Add (rxDev);
162
163
  Ipv6AddressHelper ipv6;
164
  ipv6.NewNetwork ("2001:0000:f00d:cafe::", 64);
165
  Ipv6InterfaceContainer i6 = ipv6.Assign (d);
166
167
  uint16_t port = 4000;
168
  TcpEchoServerHelper server (port);
169
  ApplicationContainer apps = server.Install (n.Get (1));
170
  apps.Start (Seconds (1.0));
171
  apps.Stop (Seconds (11.0));
172
173
  uint32_t MaxPacketSize = 183;
174
  Time interPacketInterval = Seconds (1.);
175
  uint32_t maxPacketCount = 5;
176
  TcpEchoClientHelper client (i6.GetAddress (1,1), port);
177
  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
178
  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
179
  client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
180
  apps = client.Install (n.Get (0));
181
  apps.Start (Seconds (2.0));
182
  apps.Stop (Seconds (10.0));
183
184
  Simulator::Run ();
185
  Simulator::Destroy ();
186
187
  NS_TEST_ASSERT_MSG_EQ (client.GetClient ()->GetBytesSent (), client.GetClient ()->GetBytesReceivedBack (), "Bytes received differ from bytes sent !");
188
}
189
190
class TcpEchoTestSuite : public TestSuite
191
{
192
public:
193
  TcpEchoTestSuite ();
194
};
195
196
TcpEchoTestSuite::TcpEchoTestSuite ()
197
  : TestSuite ("tcp-echo", UNIT)
198
{
199
  AddTestCase (new TcpEchoTestCase);
200
  AddTestCase (new TcpEchoTestCase6);
201
}
202
203
static TcpEchoTestSuite tcpEchoTestSuite;
204
(-)a/src/applications/wscript (+7 lines)
 Lines 18-23    Link Here 
18
        'model/udp-echo-client.cc',
18
        'model/udp-echo-client.cc',
19
        'model/udp-echo-server.cc',
19
        'model/udp-echo-server.cc',
20
        'model/v4ping.cc',
20
        'model/v4ping.cc',
21
        'model/tcp-echo-client.cc',
22
        'model/tcp-echo-server.cc',
21
        'helper/bulk-send-helper.cc',
23
        'helper/bulk-send-helper.cc',
22
        'helper/on-off-helper.cc',
24
        'helper/on-off-helper.cc',
23
        'helper/packet-sink-helper.cc',
25
        'helper/packet-sink-helper.cc',
 Lines 25-35    Link Here 
25
        'helper/udp-client-server-helper.cc',
27
        'helper/udp-client-server-helper.cc',
26
        'helper/udp-echo-helper.cc',
28
        'helper/udp-echo-helper.cc',
27
        'helper/v4ping-helper.cc',
29
        'helper/v4ping-helper.cc',
30
        'helper/tcp-echo-helper.cc',
28
        ]
31
        ]
29
32
30
    applications_test = bld.create_ns3_module_test_library('applications')
33
    applications_test = bld.create_ns3_module_test_library('applications')
31
    applications_test.source = [
34
    applications_test.source = [
32
        'test/udp-client-server-test.cc',
35
        'test/udp-client-server-test.cc',
36
        'test/tcp-echo-test-suite.cc',
33
        ]
37
        ]
34
38
35
    headers = bld.new_task_gen(features=['ns3header'])
39
    headers = bld.new_task_gen(features=['ns3header'])
 Lines 50-55    Link Here 
50
        'model/udp-echo-client.h',
54
        'model/udp-echo-client.h',
51
        'model/udp-echo-server.h',
55
        'model/udp-echo-server.h',
52
        'model/v4ping.h',
56
        'model/v4ping.h',
57
        'model/tcp-echo-client.h',
58
        'model/tcp-echo-server.h',
53
        'helper/bulk-send-helper.h',
59
        'helper/bulk-send-helper.h',
54
        'helper/on-off-helper.h',
60
        'helper/on-off-helper.h',
55
        'helper/packet-sink-helper.h',
61
        'helper/packet-sink-helper.h',
 Lines 57-62    Link Here 
57
        'helper/udp-client-server-helper.h',
63
        'helper/udp-client-server-helper.h',
58
        'helper/udp-echo-helper.h',
64
        'helper/udp-echo-helper.h',
59
        'helper/v4ping-helper.h',
65
        'helper/v4ping-helper.h',
66
        'helper/tcp-echo-helper.h',
60
        ]
67
        ]
61
68
62
    bld.ns3_python_bindings()
69
    bld.ns3_python_bindings()

Return to bug 454