Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement Session Initiation Protocol in ns3

To implement the Session Initiation Protocol (SIP) in ns3, we have to create SIP client and server applications, define the protocol logic, and integrate it with ns3’s existing network simulation framework. Here are the steps to implement SIP in ns3.

Prerequisites

  • ns3 installation :

make sure that ns3 is installed on your computer.

  • C++ Programming :

You should have knowledge in C++.

  • Understanding of ns3 :

You should be familiar with ns3 modules and basic simulation scripts.

Steps to implement SIP in ns3

  1. Set up your environment

Make sure that ns3 is installed in the computer. If not, install it from the official ns3 website.

  1. Create a new SIP module

Go to the src directory in ns3 and create a new directory for your SIP module.

  1. Define SIP classes and header files

In the model directory, create header and implementation files for SIP.

sip-server.h

#ifndef SIP_SERVER_H

#define SIP_SERVER_H

#include “ns3/application.h”

#include “ns3/address.h”

#include “ns3/ptr.h”

#include “ns3/socket.h”

#include “ns3/traced-callback.h”

namespace ns3 {

class SipServer : public Application {

public:

static TypeId GetTypeId (void);

SipServer ();

virtual ~SipServer ();

protected:

virtual void StartApplication (void);

virtual void StopApplication (void);

private:

void HandleRead (Ptr<Socket> socket);

void SendResponse (Ptr<Socket> socket, const std::string &response);

Ptr<Socket> m_socket;

Address m_local;

};

} // namespace ns3

#endif // SIP_SERVER_H

sip-server.cc

#include “sip-server.h”

#include “ns3/log.h”

#include “ns3/simulator.h”

#include “ns3/inet-socket-address.h”

#include “ns3/uinteger.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“SipServer”);

NS_OBJECT_ENSURE_REGISTERED (SipServer);

TypeId SipServer::GetTypeId (void) {

static TypeId tid = TypeId (“ns3::SipServer”)

.SetParent<Application> ()

.SetGroupName (“Applications”)

.AddConstructor<SipServer> ()

.AddAttribute (“Local”, “The Address on which to Bind the rx socket.”,

AddressValue (),

MakeAddressAccessor (&SipServer::m_local),

MakeAddressChecker ());

return tid;

}

SipServer::SipServer () {

NS_LOG_FUNCTION (this);

}

SipServer::~SipServer () {

NS_LOG_FUNCTION (this);

}

void SipServer::StartApplication () {

NS_LOG_FUNCTION (this);

if (m_socket == 0) {

m_socket = Socket::CreateSocket (GetNode (), UdpSocketFactory::GetTypeId ());

m_socket->Bind (m_local);

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

}

}

void SipServer::StopApplication () {

NS_LOG_FUNCTION (this);

if (m_socket) {

m_socket->Close ();

m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());

}

}

void SipServer::HandleRead (Ptr<Socket> socket) {

NS_LOG_FUNCTION (this << socket);

Ptr<Packet> packet;

Address from;

while ((packet = socket->RecvFrom (from))) {

NS_LOG_INFO (“Received request from ” << InetSocketAddress::ConvertFrom (from).GetIpv4 ());

// Process SIP INVITE command and send responses

std::string response = “SIP/2.0 200 OK\r\n”;

SendResponse (socket, response);

}

}

void SipServer::SendResponse (Ptr<Socket> socket, const std::string &response) {

NS_LOG_FUNCTION (this << socket << response);

Ptr<Packet> packet = Create<Packet> ((const uint8_t *)response.c_str (), response.size ());

socket->Send (packet);

NS_LOG_INFO (“Sent response: ” << response);

}

} // namespace ns3

sip-client.h

#ifndef SIP_CLIENT_H

#define SIP_CLIENT_H

#include “ns3/application.h”

#include “ns3/address.h”

#include “ns3/ptr.h”

#include “ns3/socket.h”

#include “ns3/traced-callback.h”

namespace ns3 {

class SipClient : public Application {

public:

static TypeId GetTypeId (void);

SipClient ();

virtual ~SipClient ();

protected:

virtual void StartApplication (void);

virtual void StopApplication (void);

private:

void SendRequest ();

void HandleRead (Ptr<Socket> socket);

Ptr<Socket> m_socket;

Address m_peer;

EventId m_sendEvent;

};

} // namespace ns3

#endif // SIP_CLIENT_H

sip-client.cc

#include “sip-client.h”

#include “ns3/log.h”

#include “ns3/simulator.h”

#include “ns3/inet-socket-address.h”

#include “ns3/uinteger.h”

namespace ns3 {

NS_LOG_COMPONENT_DEFINE (“SipClient”);

NS_OBJECT_ENSURE_REGISTERED (SipClient);

TypeId SipClient::GetTypeId (void) {

static TypeId tid = TypeId (“ns3::SipClient”)

.SetParent<Application> ()

.SetGroupName (“Applications”)

.AddConstructor<SipClient> ()

.AddAttribute (“Remote”, “The Address of the SIP server.”,

AddressValue (),

MakeAddressAccessor (&SipClient::m_peer),

MakeAddressChecker ());

return tid;

}

SipClient::SipClient () {

NS_LOG_FUNCTION (this);

}

SipClient::~SipClient () {

NS_LOG_FUNCTION (this);

}

void SipClient::StartApplication () {

NS_LOG_FUNCTION (this);

if (m_socket == 0) {

m_socket = Socket::CreateSocket (GetNode (), UdpSocketFactory::GetTypeId ());

m_socket->Connect (m_peer);

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

m_sendEvent = Simulator::Schedule (Seconds (1.0), &SipClient::SendRequest, this);

}

}

void SipClient::StopApplication () {

NS_LOG_FUNCTION (this);

if (m_socket) {

m_socket->Close ();

}

Simulator::Cancel (m_sendEvent);

}

void SipClient::SendRequest () {

NS_LOG_FUNCTION (this);

std::string request = “INVITE sip:user@server.com SIP/2.0\r\n”;

Ptr<Packet> packet = Create<Packet> ((const uint8_t *)request.c_str (), request.size ());

m_socket->Send (packet);

NS_LOG_INFO (“Sent request to server: ” << request);

}

void SipClient::HandleRead (Ptr<Socket> socket) {

NS_LOG_FUNCTION (this << socket);

Ptr<Packet> packet;

Address from;

while ((packet = socket->RecvFrom (from))) {

uint8_t *buffer = new uint8_t[packet->GetSize ()];

packet->CopyData (buffer, packet->GetSize ());

std::string response = std::string ((char *)buffer, packet->GetSize ());

NS_LOG_INFO (“Received response from server: ” << response);

delete[] buffer;

}

}

} // namespace ns3

  1. Create SIP helper class

To facilitate the use and to test SIP, create helper classes.

sip-helper.h

#ifndef SIP_HELPER_H

#define SIP_HELPER_H

#include “ns3/application-container.h”

#include “ns3/node-container.h”

#include “ns3/sip-server.h”

#include “ns3/sip-client.h”

namespace ns3 {

class SipServerHelper {

public:

SipServerHelper (Address address);

ApplicationContainer Install (NodeContainer c);

private:

Ptr<Application> InstallPriv (Ptr<Node> node);

 

Address m_address;

};

class SipClientHelper {

public:

SipClientHelper (Address address);

ApplicationContainer Install (NodeContainer c);

private:

Ptr<Application> InstallPriv (Ptr<Node> node);

Address m_address;

};

} // namespace ns3

#endif // SIP_HELPER_H

sip-helper.cc

#include “sip-helper.h”

#include “ns3/uinteger.h”

#include “ns3/names.h”

namespace ns3 {

SipServerHelper::SipServerHelper (Address address)

: m_address (address) {

}

ApplicationContainer SipServerHelper::Install (NodeContainer c) {

ApplicationContainer apps;

for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i) {

Ptr<Node> node = *i;

Ptr<Application> app = InstallPriv (node);

apps.Add (app);

}

return apps;

}

Ptr<Application> SipServerHelper::InstallPriv (Ptr<Node> node) {

Ptr<SipServer> server = CreateObject<SipServer> ();

server->SetAttribute (“Local”, AddressValue (m_address));

node->AddApplication (server);

return server;

}

SipClientHelper::SipClientHelper (Address address)

: m_address (address) {

}

ApplicationContainer SipClientHelper::Install (NodeContainer c) {

ApplicationContainer apps;

for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i) {

Ptr<Node> node = *i;

Ptr<Application> app = InstallPriv (node);

apps.Add (app);

}

return apps;

}

Ptr<Application> SipClientHelper::InstallPriv (Ptr<Node> node) {

Ptr<SipClient> client = CreateObject<SipClient> ();

client->SetAttribute (“Remote”, AddressValue (m_address));

node->AddApplication (client);

return client;

}

} // namespace ns3

 

  1. Integrate SIP with ns3 build system

To include your new SIP module, update the wscript file in the src directory.

src/sip/wscript

def build(bld):

module = bld.create_ns3_module(‘sip’, [‘core’, ‘network’, ‘internet’, ‘applications’])

module.source = [

‘model/sip-server.cc’,

‘model/sip-client.cc’,

‘helper/sip-helper.cc’,

]

headers = bld(features=’ns3header’)

headers.module = ‘sip’

headers.source = [

‘model/sip-server.h’,

‘model/sip-client.h’,

‘helper/sip-helper.h’,

]

 

  1. Build and run the simulation

Rebuild ns3 to include your new SIP module.

 

./waf configure

./waf build

Create test scripts to verify the implementation of your SIP protocol.

scratch/test-sip.cc

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/sip-helper.h”

using namespace ns3;

int main (int argc, char *argv[])

{

CommandLine cmd;

cmd.Parse (argc, argv);

NodeContainer nodes;

nodes.Create (2);

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));

pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

NetDeviceContainer devices;

devices = pointToPoint.Install (nodes);

InternetStackHelper stack;

stack.Install (nodes);

Ipv4AddressHelper address;

address.SetBase (“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces = address.Assign (devices);

SipServerHelper sipServer (InetSocketAddress (Ipv4Address::GetAny (), 5060));

ApplicationContainer serverApps = sipServer.Install (nodes.Get (1));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

SipClientHelper sipClient (InetSocketAddress (interfaces.GetAddress (1), 5060));

ApplicationContainer clientApps = sipClient.Install (nodes.Get (0));

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

  1. Run and debug

Run your test script and debug any issues that arise.

./waf –run scratch/test-sip

Overall, we had successfully learned on implementing Session Initiation Protocol (SIP) in ns3 by creating SIP client and server applications, defining the protocol logic, and integrating it with ns3’s existing network simulation framework.

Best programming results can be got from ns3simulaion.com for your Session Initiation Protocol (SIP) in ns3.