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

How to Implement File Transfer Protocol in ns3

To implement the File transfer protocol (FTP) in ns3 consists to make modules for FTP client and server applications that describes the logic of protocol and incorporated it with ns3 existing network simulation model.

The given below are the detailed procedure that we follow to  implement the FTP in ns3 environment.

Prerequisites

  1. Ns3 Installation: make certain ns3 is installed in the computer.
  2. C++ Programming: understanding of C++ is required.
  3. Understanding of ns-3: knowledge with ns-3 modules and basic simulation scripts.

Steps to Implement FTP in ns-3

  1. Set Up the Development Environment

Make sure that you have installed ns3. Download and install ns3 from official website.

  1. Create a New FTP Module

Navigate to the src directory in ns-3 and create a new directory for your FTP module.

cd ns-3.XX/src

mkdir ftp

cd ftp

mkdir model helper test

3.      Define FTP Classes and Headers

Create header and implementation files for FTP in the model directory.

ftp-server.h

#ifndef FTP_SERVER_H

#define FTP_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 FtpServer : public Application {

public:

static TypeId GetTypeId (void);

FtpServer ();

virtual ~FtpServer ();

protected:

virtual void StartApplication (void);

virtual void StopApplication (void);

private:

void HandleRead (Ptr<Socket> socket);

void SendFile (Ptr<Socket> socket);

Ptr<Socket> m_socket;

Address m_local;

};

} // namespace ns3

#endif // FTP_SERVER_H

ftp-server.cc

#include “ftp-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 (“FtpServer”);

NS_OBJECT_ENSURE_REGISTERED (FtpServer);

TypeId FtpServer::GetTypeId (void) {

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

.SetParent<Application> ()

.SetGroupName (“Applications”)

.AddConstructor<FtpServer> ()

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

AddressValue (),

MakeAddressAccessor (&FtpServer::m_local),

MakeAddressChecker ());

return tid;

}

FtpServer::FtpServer () {

NS_LOG_FUNCTION (this);

}

FtpServer::~FtpServer () {

NS_LOG_FUNCTION (this);

}

void FtpServer::StartApplication () {

NS_LOG_FUNCTION (this);

if (m_socket == 0) {

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

m_socket->Bind (m_local);

m_socket->Listen ();

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

}

}

void FtpServer::StopApplication () {

NS_LOG_FUNCTION (this);

if (m_socket) {

m_socket->Close ();

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

}

}

void FtpServer::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 ());

SendFile (socket);

}

}

void FtpServer::SendFile (Ptr<Socket> socket) {

NS_LOG_FUNCTION (this << socket);

Ptr<Packet> packet = Create<Packet> (1024); // Simulate sending a 1024-byte file

socket->Send (packet);

NS_LOG_INFO (“Sent file to client”);

}

} // namespace ns3

ftp-client.h

#ifndef FTP_CLIENT_H

#define FTP_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 FtpClient : public Application {

public:

static TypeId GetTypeId (void);

FtpClient ();

virtual ~FtpClient ();

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 // FTP_CLIENT_H

ftp-client.cc

#include “ftp-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 (“FtpClient”);

NS_OBJECT_ENSURE_REGISTERED (FtpClient);

TypeId FtpClient::GetTypeId (void) {

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

.SetParent<Application> ()

.SetGroupName (“Applications”)

.AddConstructor<FtpClient> ()

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

AddressValue (),

MakeAddressAccessor (&FtpClient::m_peer),

MakeAddressChecker ());

return tid;

}

FtpClient::FtpClient () {

NS_LOG_FUNCTION (this);

}

FtpClient::~FtpClient () {

NS_LOG_FUNCTION (this);

}

void FtpClient::StartApplication () {

NS_LOG_FUNCTION (this);

if (m_socket == 0) {

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

m_socket->Connect (m_peer);

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

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

}

}

void FtpClient::StopApplication () {

NS_LOG_FUNCTION (this);

if (m_socket) {

m_socket->Close ();

}

Simulator::Cancel (m_sendEvent);

}

void FtpClient::SendRequest () {

NS_LOG_FUNCTION (this);

Ptr<Packet> packet = Create<Packet> (10); // Simulate sending a request packet

m_socket->Send (packet);

NS_LOG_INFO (“Sent request to server”);

}

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

NS_LOG_FUNCTION (this << socket);

Ptr<Packet> packet;

Address from;

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

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

// Handle the received file here

}

}

} // namespace ns3

4.      Create FTP Helper Classes

Create helper classes to facilitate the use and testing of FTP.

ftp-helper.h

#ifndef FTP_HELPER_H

#define FTP_HELPER_H

#include “ns3/application-container.h”

#include “ns3/node-container.h”

#include “ns3/ftp-server.h”

#include “ns3/ftp-client.h”

namespace ns3 {

class FtpServerHelper {

public:

FtpServerHelper (Address address);

ApplicationContainer Install (NodeContainer c);

private:

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

Address m_address;

};

class FtpClientHelper {

public:

FtpClientHelper (Address address);

ApplicationContainer Install (NodeContainer c);

private:

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

Address m_address;

};

} // namespace ns3

#endif // FTP_HELPER_H

ftp-helper.cc

#include “ftp-helper.h”

#include “ns3/uinteger.h”

#include “ns3/names.h”

namespace ns3 {

FtpServerHelper::FtpServerHelper (Address address)

: m_address (address) {

}

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

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

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

node->AddApplication (server);

return server;

}

FtpClientHelper::FtpClientHelper (Address address)

: m_address (address) {

}

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

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

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

node->AddApplication (client);

return client;

}

} // namespace ns3

5.      Integrate FTP with ns-3 Build System

Update the wscript file in the src directory to include your new FTP module.

src/ftp/wscript

def build(bld):

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

module.source = [

‘model/ftp-server.cc’,

‘model/ftp-client.cc’,

‘helper/ftp-helper.cc’,

]

headers = bld(features=’ns3header’)

headers.module = ‘ftp’

headers.source = [

‘model/ftp-server.h’,

‘model/ftp-client.h’,

‘helper/ftp-helper.h’,

]

  1. Build and Test
  • Rebuild ns-3 to include your new FTP module.

./waf configure

./waf build

  • Create test scripts to verify the implementation of your FTP protocol.

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

FtpServerHelper ftpServer (InetSocketAddress (Ipv4Address::GetAny (), 21));

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

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (10.0));

FtpClientHelper ftpClient (InetSocketAddress (interfaces.GetAddress (1), 21));

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

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (10.0));

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

7.      Run and Debug

Run your test script and debug any issues that arise.

./waf –run scratch/test-ftp

Overall, we had learned how to implement the File Transfer Protocol in ns3 environment by creating a classes and headers to make a client server application.

Comparative Analysis based on File transfer protocol (FTP) in ns3 are assisted by ns3simulation.com. We also offer further information about File transfer protocol related to your work.