How to Begin Implement File Transfer Protocol in NS3

To start executing the File Transfer Protocol (FTP) using NS3, we’ll need to replicate the FTP’s behavior to transmit files through the network. FTP normally functions on the Application Layer of the OSI model and for reliable data transport it utilizes TCP. We want to construct an FTP application, which interacts with the fundamental TCP stack for replicating the file transfers in NS3.

Below is an ordered instruction to get started with executing the FTP in NS3:

Steps to Begin Implement File Transfer Protocol in NS3

Step 1: Understand FTP Requirements

  1. Protocol Behavior:
    • FTP utilizes two connections like:
      • Control Connection: IT supports to handle the commands and responses which are usually port 21.
      • Data Connection: Transmits file data (dynamic ports).
    • FTP functions have file upload, download, and directory listing.
  2. Simplification for Simulation:
    • We address the data connection and mimic file transfers through TCP for simplicity.

Step 2: Familiarize Yourself with NS3

  1. Existing Application Models:
    • Focus on BulkSendApplication for TCP-based data transfer in src/applications/.
    • Learn PacketSinkApplication for inheriting information.
  2. Understanding TCP:
    • While NS3’s TCP stack manages the reliable data transfer, FTP will be utilized TCP sockets for interaction.

Step 3: Create the FTP Application

  1. Set Up Your Directory:
    • Make a new directory in src/applications/, for FTP such as ftp/.
  2. Add Required Files:
    • ftp-application.h: Integrate the header file for the FTP application.
    • ftp-application.cc: Execution file.

Step 4: Define FTP Application

Describe the FTP application to prolong ns3::Application.

Header File (ftp-application.h)

#include “ns3/application.h”

#include “ns3/socket.h”

class FtpApplication : public ns3::Application {

public:

static ns3::TypeId GetTypeId (void);

FtpApplication ();

virtual ~FtpApplication ();

void Setup (ns3::Ptr<ns3::Socket> socket, ns3::Address address, uint32_t fileSize, uint32_t packetSize);

protected:

virtual void StartApplication (void);

virtual void StopApplication (void);

private:

void SendData ();

ns3::Ptr<ns3::Socket> m_socket;       // Socket for data transfer

ns3::Address m_peerAddress;          // Peer address

uint32_t m_fileSize;                 // Total size of the file to transfer

uint32_t m_packetSize;               // Size of each data packet

uint32_t m_bytesSent;                // Bytes sent so far

ns3::EventId m_sendEvent;            // Event to schedule sending

};

Implementation File (ftp-application.cc)

#include “ftp-application.h”

#include “ns3/log.h”

#include “ns3/simulator.h”

NS_LOG_COMPONENT_DEFINE (“FtpApplication”);

NS_OBJECT_ENSURE_REGISTERED (FtpApplication);

ns3::TypeId FtpApplication::GetTypeId (void) {

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

.SetParent<ns3::Application> ()

.SetGroupName (“Applications”)

.AddConstructor<FtpApplication> ();

return tid;

}

FtpApplication::FtpApplication ()

: m_socket (0),

m_fileSize (0),

m_packetSize (0),

m_bytesSent (0) {}

 

FtpApplication::~FtpApplication () {

m_socket = nullptr;

}

void FtpApplication::Setup (ns3::Ptr<ns3::Socket> socket, ns3::Address address, uint32_t fileSize, uint32_t packetSize) {

m_socket = socket;

m_peerAddress = address;

m_fileSize = fileSize;

m_packetSize = packetSize;

}

void FtpApplication::StartApplication (void) {

NS_LOG_INFO (“Starting FTP Application”);

m_socket->Bind ();

m_socket->Connect (m_peerAddress);

SendData ();

}

void FtpApplication::StopApplication (void) {

NS_LOG_INFO (“Stopping FTP Application”);

if (m_socket) {

m_socket->Close ();

}

Simulator::Cancel (m_sendEvent);

}

void FtpApplication::SendData () {

if (m_bytesSent < m_fileSize) {

uint32_t toSend = std::min (m_packetSize, m_fileSize – m_bytesSent);

ns3::Ptr<ns3::Packet> packet = ns3::Create<ns3::Packet> (toSend);

m_socket->Send (packet);

m_bytesSent += toSend;

NS_LOG_INFO (“Sent ” << m_bytesSent << ” bytes out of ” << m_fileSize);

// Schedule the next packet transmission

m_sendEvent = Simulator::Schedule (ns3::MilliSeconds (1), &FtpApplication::SendData, this);

}

}

Step 5: Write Simulation Script

Make a simulation script to experiment the FTP application.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/applications-module.h”

#include “ftp-application.h”

using namespace ns3;

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

NodeContainer nodes;

nodes.Create (2);

InternetStackHelper stack;

stack.Install (nodes);

// Configure point-to-point channel

PointToPointHelper pointToPoint;

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

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

NetDeviceContainer devices;

devices = pointToPoint.Install (nodes);

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign (devices);

// Configure FTP sender

Ptr<Socket> ftpSocket = Socket::CreateSocket (nodes.Get (0), TcpSocketFactory::GetTypeId ());

Ptr<FtpApplication> ftpApp = CreateObject<FtpApplication> ();

ftpApp->Setup (ftpSocket, InetSocketAddress (interfaces.GetAddress (1), 8080), 1024 * 1024, 1024); // 1 MB file, 1 KB packets

nodes.Get (0)->AddApplication (ftpApp);

ftpApp->SetStartTime (Seconds (1.0));

ftpApp->SetStopTime (Seconds (10.0));

// Configure data receiver

PacketSinkHelper sinkHelper (“ns3::TcpSocketFactory”, InetSocketAddress (Ipv4Address::GetAny (), 8080));

ApplicationContainer sinkApp = sinkHelper.Install (nodes.Get (1));

sinkApp.Start (Seconds (0.0));

sinkApp.Stop (Seconds (10.0));

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Step 6: Compile and Run

  1. Build the Code:

./waf configure

./waf build

  1. Run the Simulation:

./waf –run your-script

Step 7: Validate and Extend

  1. Validation:
    • Seizure and examine the traffic leveraging the NS3 tracing tools such as AsciiTraceHelper, PcapHelper.
    • Confirm the metrics like throughput and latency for validation.
  2. Extensions:
    • Integrate functionality of control connection for commands such as file listing.
    • Replicate the FTP through diverse network topologies and link conditions.
    • Execute the multi-threaded transfers or parallel sessions.

Through NS3 based implementation, we had showed a detailed approach on how to execute and extend the File Transfer Protocol in this manual. We are ready to extend the results with extra data if required.