1.1 --- a/src/internet-stack/tcp-socket-impl.cc Thu Aug 14 13:04:18 2008 -0400
1.2 +++ b/src/internet-stack/tcp-socket-impl.cc Thu Aug 14 17:39:43 2008 -0400
1.3 @@ -872,11 +872,11 @@
1.4 case SERV_NOTIFY:
1.5 NS_LOG_LOGIC ("TcpSocketImpl " << this <<" Action SERV_NOTIFY");
1.6 NS_LOG_LOGIC ("TcpSocketImpl " << this << " Connected!");
1.7 - NotifyNewConnectionCreated (this, fromAddress);
1.8 m_connected = true; // ! This is bogus; fix when we clone the tcp
1.9 m_endPoint->SetPeer (m_remoteAddress, m_remotePort);
1.10 //treat the connection orientation final ack as a newack
1.11 CommonNewAck (tcpHeader.GetAckNumber (), true);
1.12 + NotifyNewConnectionCreated (this, fromAddress);
1.13 break;
1.14 default:
1.15 break;
1.16 @@ -1552,7 +1552,7 @@
1.17
1.18 #include "ns3/test.h"
1.19 #include "ns3/socket-factory.h"
1.20 -#include "ns3/udp-socket-factory.h"
1.21 +#include "ns3/tcp-socket-factory.h"
1.22 #include "ns3/simulator.h"
1.23 #include "ns3/simple-channel.h"
1.24 #include "ns3/simple-net-device.h"
1.25 @@ -1567,18 +1567,163 @@
1.26 public:
1.27 TcpSocketImplTest ();
1.28 virtual bool RunTests (void);
1.29 + private:
1.30 + void Test1 (void); //send string "Hello world" server->client
1.31 + Ptr<Node> CreateInternetNode ();
1.32 + Ptr<SimpleNetDevice> AddSimpleNetDevice (Ptr<Node>,const char*,const char*);
1.33 +
1.34 + void HandleConnectionCreated (Ptr<Socket>, const Address &);
1.35 + void HandleRecv (Ptr<Socket> sock);
1.36 +
1.37 + void Reset ();
1.38 +
1.39 + Ptr<Node> node0;
1.40 + Ptr<Node> node1;
1.41 + Ptr<SimpleNetDevice> dev0;
1.42 + Ptr<SimpleNetDevice> dev1;
1.43 + Ptr<SimpleChannel> channel;
1.44 + Ptr<Socket> listeningSock;
1.45 + Ptr<Socket> sock0;
1.46 + Ptr<Socket> sock1;
1.47 + uint32_t rxBytes0;
1.48 + uint32_t rxBytes1;
1.49 +
1.50 + uint8_t* rxPayload;
1.51 +
1.52 + bool result;
1.53 };
1.54
1.55 TcpSocketImplTest::TcpSocketImplTest ()
1.56 - : Test ("TcpSocketImpl")
1.57 + : Test ("TcpSocketImpl"),
1.58 + rxBytes0 (0),
1.59 + rxBytes1 (0),
1.60 + rxPayload (0),
1.61 + result (true)
1.62 {
1.63 }
1.64
1.65 bool
1.66 TcpSocketImplTest::RunTests (void)
1.67 {
1.68 - bool result = true;
1.69 - return result;
1.70 + Test1();
1.71 + return result;
1.72 +}
1.73 +
1.74 +void
1.75 +TcpSocketImplTest::Test1 ()
1.76 +{
1.77 + const char* netmask = "255.255.255.0";
1.78 + const char* ipaddr0 = "192.168.1.1";
1.79 + const char* ipaddr1 = "192.168.1.2";
1.80 + node0 = CreateInternetNode ();
1.81 + node1 = CreateInternetNode ();
1.82 + dev0 = AddSimpleNetDevice (node0, ipaddr0, netmask);
1.83 + dev1 = AddSimpleNetDevice (node1, ipaddr1, netmask);
1.84 +
1.85 + channel = CreateObject<SimpleChannel> ();
1.86 + dev0->SetChannel (channel);
1.87 + dev1->SetChannel (channel);
1.88 +
1.89 + Ptr<SocketFactory> sockFactory0 = node0->GetObject<TcpSocketFactory> ();
1.90 + Ptr<SocketFactory> sockFactory1 = node1->GetObject<TcpSocketFactory> ();
1.91 +
1.92 + listeningSock = sockFactory0->CreateSocket();
1.93 + sock1 = sockFactory1->CreateSocket();
1.94 +
1.95 + uint16_t port = 50000;
1.96 + InetSocketAddress serverlocaladdr (Ipv4Address::GetAny(), port);
1.97 + InetSocketAddress serverremoteaddr (Ipv4Address(ipaddr0), port);
1.98 +
1.99 + listeningSock->Bind(serverlocaladdr);
1.100 + listeningSock->Listen (0);
1.101 + listeningSock->SetAcceptCallback
1.102 + (MakeNullCallback<bool, Ptr< Socket >, const Address &> (),
1.103 + MakeCallback(&TcpSocketImplTest::HandleConnectionCreated,this));
1.104 +
1.105 + sock1->Connect(serverremoteaddr);
1.106 + sock1->SetRecvCallback (MakeCallback(&TcpSocketImplTest::HandleRecv, this));
1.107 +
1.108 + Simulator::Run ();
1.109 + Simulator::Destroy ();
1.110 +
1.111 + result = result && (rxBytes1 == 13);
1.112 + result = result && (strcmp((const char*) rxPayload,"Hello World!") == 0);
1.113 +
1.114 + Reset ();
1.115 +}
1.116 +
1.117 +Ptr<Node>
1.118 +TcpSocketImplTest::CreateInternetNode ()
1.119 +{
1.120 + Ptr<Node> node = CreateObject<Node> ();
1.121 + AddInternetStack (node);
1.122 + return node;
1.123 +}
1.124 +
1.125 +Ptr<SimpleNetDevice>
1.126 +TcpSocketImplTest::AddSimpleNetDevice (Ptr<Node> node, const char* ipaddr, const char* netmask)
1.127 +{
1.128 + Ptr<SimpleNetDevice> dev = CreateObject<SimpleNetDevice> ();
1.129 + dev->SetAddress (Mac48Address::Allocate ());
1.130 + node->AddDevice (dev);
1.131 + Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
1.132 + uint32_t ndid = ipv4->AddInterface (dev);
1.133 + ipv4->SetAddress (ndid, Ipv4Address (ipaddr));
1.134 + ipv4->SetNetworkMask (ndid, Ipv4Mask (netmask));
1.135 + ipv4->SetUp (ndid);
1.136 + return dev;
1.137 +}
1.138 +
1.139 +void
1.140 +TcpSocketImplTest::HandleConnectionCreated (Ptr<Socket> s, const Address & addr)
1.141 +{
1.142 + NS_ASSERT(s != listeningSock);
1.143 + NS_ASSERT(sock0 == 0);
1.144 + sock0 = s;
1.145 + const uint8_t* hello = (uint8_t*)"Hello World!";
1.146 + Ptr<Packet> p = Create<Packet> (hello, 13);
1.147 + sock0->Send(p);
1.148 +
1.149 + sock0->SetRecvCallback (MakeCallback(&TcpSocketImplTest::HandleRecv, this));
1.150 +}
1.151 +
1.152 +void
1.153 +TcpSocketImplTest::HandleRecv (Ptr<Socket> sock)
1.154 +{
1.155 + NS_ASSERT (sock == sock0 | sock == sock1);
1.156 + Ptr<Packet> p = sock->Recv();
1.157 + uint32_t sz = p->GetSize();
1.158 + if (sock == sock0)
1.159 + {
1.160 + rxBytes0 += sz;
1.161 + }
1.162 + else if (sock == sock1)
1.163 + {
1.164 + rxBytes1 += sz;
1.165 + rxPayload = new uint8_t[sz];
1.166 + memcpy (rxPayload, p->PeekData(), sz);
1.167 + }
1.168 + else
1.169 + {
1.170 + NS_FATAL_ERROR ("Recv from unknown socket "<<sock);
1.171 + }
1.172 +}
1.173 +
1.174 +void
1.175 +TcpSocketImplTest::Reset ()
1.176 +{
1.177 + node0 = 0;
1.178 + node1 = 0;
1.179 + dev0 = 0;
1.180 + dev1 = 0;
1.181 + channel = 0;
1.182 + listeningSock = 0;
1.183 + sock0 = 0;
1.184 + sock1 = 0;
1.185 + rxBytes0 = 0;
1.186 + rxBytes1 = 0;
1.187 + delete[] rxPayload;
1.188 + rxPayload = 0;
1.189 }
1.190
1.191 static TcpSocketImplTest gTcpSocketImplTest;