user2225868
user2225868

Reputation: 33

NS-3 Socket to connect to external program

I am trying for a way to write a socket class to connect my NS-3 simulation to an outside program. So what I want to do is to create packets in NS-3 and send them through this socket to an outside tool, do some simple manipulations on the packet in that tool, and then send it back to NS-3. I don't think the built in NS-3 socket can be used for this purpose.

Has anyone come across something like this before or has any suggestions?

Your help is very much appreciated!

Upvotes: 1

Views: 1592

Answers (1)

jangelfdez
jangelfdez

Reputation: 389

I'm using a TCP socket to connect an external Python TCP Socket using NS-3, here is the code:

/*
 * Create a NS-3 Application that opens a TCP Socket and 
 * waits for incoming connections
 *
 */
#include "ns3/icmpv4.h"
#include "ns3/assert.h"
#include "ns3/log.h"
#include "ns3/ipv4-address.h"
#include "ns3/socket.h"
#include "ns3/integer.h"
#include "ns3/boolean.h"
#include "ns3/inet-socket-address.h"
#include "ns3/packet.h"
#include "ns3/trace-source-accessor.h"
#include "ns3/config.h"
#include "ns3/tos-device.h"
#include "ns3/names.h"
#include "ns3/string.h"
#include "ns3/object.h"


namespace ns3 {

IOProxyServer::IOProxyServer ()
{
    m_socket = 0;
}

TypeId IOProxyServer::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::IoProxyServer")
    .SetParent<Application> ()
    .AddConstructor<IOProxyServer> ()
    .AddAttribute("RemotePortNumber",
                        "Remote port listening for connections",
                        IntegerValue(9999),
                        MakeIntegerAccessor(&IOProxyServer::m_remotePortNumber),
                        MakeIntegerChecker<int64_t>())
    .AddAttribute("RemoteIp",
                        "Remote IP listening for connections",
                        StringValue("127.0.0.1"),
                        MakeStringAccessor(&IOProxyServer::m_remoteIp),
                        MakeStringChecker())
    .AddAttribute("LocalPortNumber",
                        "Local port for incoming connections",
                        IntegerValue(3333),
                        MakeIntegerAccessor(&IOProxyServer::m_localPortNumber),
                        MakeIntegerChecker<int64_t>())
    .AddAttribute("LocalIp",
                        "Local IP for incoming connections",
                        StringValue("127.0.0.1"),
                        MakeStringAccessor(&IOProxyServer::m_localIp),
                        MakeStringChecker());
  return tid;
}


void IOProxyServer::StartApplication (void)
{
  NS_LOG_FUNCTION (this);

  m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::TcpSocketFactory"));
  NS_ASSERT_MSG (m_socket != 0, "An error has happened when trying to create the socket");


  InetSocketAddress src = InetSocketAddress (Ipv4Address::GetAny(), m_localPortNumber );
  InetSocketAddress dest = InetSocketAddress(Ipv4Address(m_remoteIp.c_str()), m_remotePortNumber);

  int status;
  status = m_socket->Bind (src);
  NS_ASSERT_MSG (status != -1, "An error has happened when trying to bind to local end point");

  status = m_socket->Connect(dest);
  NS_ASSERT_MSG (status != -1, "An error has happened when trying to connect to remote end point");

  // Configures the callbacks for the different events related with the connection

  //m_socket->SetConnectCallback


  m_socket->SetAcceptCallback (
    MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
    MakeCallback (&IOProxyServer::HandleAccept, this));

  m_socket->SetRecvCallback (
    MakeCallback (&IOProxyServer::HandleRead, this));

  m_socket->SetDataSentCallback (
    MakeCallback (&IOProxyServer::HandleSend,this));

  //m_socket->SetSendCallback

  m_socket->SetCloseCallbacks (
    MakeCallback (&IOProxyServer::HandlePeerClose, this),
    MakeCallback (&IOProxyServer::HandlePeerError, this));

  // If we need to configure a reception only socket or a sending only socket
  // we need to call one of the following methods:
  // m_socket->ShutdownSend();
  // m_socket->ShutdownRecv();
}

void IOProxyServer::StopApplication (void)
{
  NS_LOG_FUNCTION (this);
  m_socket->Close();
}

void IOProxyServer::HandlePeerClose (Ptr<Socket> socket)
{
  NS_LOG_FUNCTION (this << socket);
}

void IOProxyServer::HandlePeerError (Ptr<Socket> socket)
{
  NS_LOG_FUNCTION (this << socket);
}

void IOProxyServer::HandleSend (Ptr<Socket> socket, uint32_t dataSent)
{
  NS_LOG_FUNCTION (this << socket);
}

void IOProxyServer::HandleAccept (Ptr<Socket> s, const Address& from)
{
  NS_LOG_FUNCTION (this << s << from);
  s->SetRecvCallback (MakeCallback (&IOProxyServer::HandleRead, this));
}

void IOProxyServer::HandleRead (Ptr<Socket> socket)
{
  NS_LOG_FUNCTION (this << socket);
  Ptr<Packet> packet;

  while ((packet = socket->RecvFrom (from)))
    {
      if (packet->GetSize () == 0)
        { //EOF
          break;
        }

      if (InetSocketAddress::IsMatchingType (from))
        {
           //Do whatever you need with the incoming info 
        }

    }
}

void IOProxyServer::SendData()
{
    //Do whatever you need for creating your packet and send it using the socket

    //Ptr<Packet> packet = Create<Packet>(pointer, sizeof(pointer));
    //m_socket->Send(packet, 0, from);
}

IOProxyServer::~IOProxyServer ()
{

}

void IOProxyServer::DoDispose (void)
{
  NS_LOG_FUNCTION (this);
  m_socket = 0;
  Application::DoDispose ();
}

} // namespace ns3

Upvotes: 1

Related Questions