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

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

Return to bug 454