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

How to Implement Simple Network Management Protocol in ns3

To implement Simple Network Management Protocol (SNMP) in ns3, we have to create modules for SNMP agents and managers, define the protocol logic, and integrate it with ns3’s existing network simulation framework. Here are the steps for implementing SNMP 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 SNMP 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 SNMP module

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

cd ns-3.XX/src

mkdir snmp

cd snmp

mkdir model helper test

 

  1. Define SNMP classes and header files

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

snmp.h

#ifndef SNMP_H

#define SNMP_H

#include “ns3/application.h”

#include “ns3/address.h”

#include “ns3/ptr.h”

#include “ns3/socket.h”

namespace ns3 {

class SnmpAgent : public Application {

public:

static TypeId GetTypeId (void);

SnmpAgent ();

virtual ~SnmpAgent ();

protected:

virtual void StartApplication (void);

virtual void StopApplication (void);

private:

void HandleRead (Ptr<Socket> socket);

Ptr<Socket> m_socket;

Address m_local;

};

class SnmpManager : public Application {

public:

static TypeId GetTypeId (void);

SnmpManager ();

virtual ~SnmpManager ();

protected:

virtual void StartApplication (void);

virtual void StopApplication (void);

private:

void SendQuery ();

Ptr<Socket> m_socket;

Address m_peer;

EventId m_sendEvent;

};

} // namespace ns3

#endif // SNMP_H

snmp.cc

#include “snmp.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 (“SnmpAgent”);

NS_LOG_COMPONENT_DEFINE (“SnmpManager”);

NS_OBJECT_ENSURE_REGISTERED (SnmpAgent);

NS_OBJECT_ENSURE_REGISTERED (SnmpManager);

TypeId SnmpAgent::GetTypeId (void) {

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

.SetParent<Application> ()

.SetGroupName (“Applications”)

.AddConstructor<SnmpAgent> ()

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

AddressValue (),

MakeAddressAccessor (&SnmpAgent::m_local),

MakeAddressChecker ());

return tid;

}

SnmpAgent::SnmpAgent () {

NS_LOG_FUNCTION (this);

}

SnmpAgent::~SnmpAgent () {

NS_LOG_FUNCTION (this);

}

void SnmpAgent::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 (&SnmpAgent::HandleRead, this));

}

}

void SnmpAgent::StopApplication () {

NS_LOG_FUNCTION (this);

if (m_socket) {

m_socket->Close ();

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

}

}

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

NS_LOG_FUNCTION (this << socket);

Ptr<Packet> packet;

Address from;

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

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

// Handle SNMP packet here

}

}

TypeId SnmpManager::GetTypeId (void) {

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

.SetParent<Application> ()

.SetGroupName (“Applications”)

.AddConstructor<SnmpManager> ()

.AddAttribute (“Remote”, “The Address of the peer.”,

AddressValue (),

MakeAddressAccessor (&SnmpManager::m_peer),

MakeAddressChecker ());

return tid;

}

SnmpManager::SnmpManager () {

NS_LOG_FUNCTION (this);

}

SnmpManager::~SnmpManager () {

NS_LOG_FUNCTION (this);

}

void SnmpManager::StartApplication () {

NS_LOG_FUNCTION (this);

if (m_socket == 0) {

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

m_socket->Connect (m_peer);

m_sendEvent = Simulator::Schedule (Seconds (1.0), &SnmpManager::SendQuery, this);

}

}

void SnmpManager::StopApplication () {

NS_LOG_FUNCTION (this);

if (m_socket) {

m_socket->Close ();

}

Simulator::Cancel (m_sendEvent);

}

void SnmpManager::SendQuery () {

NS_LOG_FUNCTION (this);

Ptr<Packet> packet = Create<Packet> (100); // Create a dummy SNMP query packet

m_socket->Send (packet);

NS_LOG_INFO (“Sent SNMP query to ” << InetSocketAddress::ConvertFrom (m_peer).GetIpv4 ());

m_sendEvent = Simulator::Schedule (Seconds (5.0), &SnmpManager::SendQuery, this);

}

} // namespace ns3

 

  1. Create SNMP helper class

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

snmp-helper.h

#ifndef SNMP_HELPER_H

#define SNMP_HELPER_H

#include “ns3/application-container.h”

#include “ns3/node-container.h”

#include “ns3/snmp.h”

namespace ns3 {

class SnmpAgentHelper {

public:

SnmpAgentHelper (Address address);

ApplicationContainer Install (NodeContainer c);

private:

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

Address m_address;

};

class SnmpManagerHelper {

public:

SnmpManagerHelper (Address address);

ApplicationContainer Install (NodeContainer c);

private:

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

Address m_address;

};

} // namespace ns3

#endif // SNMP_HELPER_H

snmp-helper.cc

#include “snmp-helper.h”

#include “ns3/uinteger.h”

#include “ns3/names.h”

namespace ns3 {

SnmpAgentHelper::SnmpAgentHelper (Address address)

: m_address (address) {

}

ApplicationContainer SnmpAgentHelper::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> SnmpAgentHelper::InstallPriv (Ptr<Node> node) {

Ptr<SnmpAgent> agent = CreateObject<SnmpAgent> ();

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

node->AddApplication (agent);

return agent;

}

SnmpManagerHelper::SnmpManagerHelper (Address address)

: m_address (address) {

}

ApplicationContainer SnmpManagerHelper::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> SnmpManagerHelper::InstallPriv (Ptr<Node> node) {

Ptr<SnmpManager> manager = CreateObject<SnmpManager> ();

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

node->AddApplication (manager);

return manager;

}

} // namespace ns3

 

  1. Integrate SNMP with ns3 build system

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

src/snmp/wscript

def build(bld):

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

module.source = [

‘model/snmp.cc’,

‘helper/snmp-helper.cc’,

]

headers = bld(features=’ns3header’)

headers.module = ‘snmp’

headers.source = [

‘model/snmp.h’,

‘helper/snmp-helper.h’,

]

 

  1. Build and test

Rebuild ns3 to include your new SNMP module.

./waf configure

./waf build

 

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

scratch/test-snmp.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/snmp-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);

SnmpAgentHelper snmpAgent (InetSocketAddress (Ipv4Address::GetAny (), 161));

ApplicationContainer agentApps = snmpAgent.Install (nodes.Get (1));

agentApps.Start (Seconds (1.0));

agentApps.Stop (Seconds (10.0));

SnmpManagerHelper snmpManager (InetSocketAddress (interfaces.GetAddress (1), 161));

ApplicationContainer managerApps = snmpManager.Install (nodes.Get (0));

managerApps.Start (Seconds (2.0));

managerApps.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-snmp

Overall, we had successfully implemented the Simple Network Management Protocol (SNMP) in ns3 by creating modules for SNMP agents and managers, defining the protocol logic, and integrating it with ns3’s existing network simulation framework.

We work all modules on Simple Network Management Protocol (SNMP)  approach ns3simulation.com to get your task done.