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

How to Implement Data Transmission Privacy in ns3

To implement data transmission privacy in ns3, by using various encryption and security mechanisms. This mechanism helps in ensuring that while transmitting the data over the network is safe and secure, and cannot be easily decrypted, intercepted or tampered. Here the step given below will guide on how to implement a basic data transmission privacy mechanism in ns3.

Step-by-step to implement in ns3

Step 1: Set Up the Simulation Environment

Make sure ns3 is installed on the system.

Step 2: Create the Network Topology

Create a network topology using ns3 with multiple nodes. For this example, we will simulate a simple scenario where nodes communicate securely using encryption.

Step 3: Define the Encryption and Decryption Functions

Create custom applications to simulate the encryption and decryption of data during transmission.

Step 4: Write the Script

An example given based on creating and configuring data transmission privacy in ns3:

  1. Create a New File:
    • Save the following script as data-transmission-privacy.cc in the scratch directory of your ns-3 installation.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

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

#include “ns3/applications-module.h”

#include <openssl/aes.h>

#include <openssl/rand.h>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“DataTransmissionPrivacyExample”);

class SecureApplication : public Application

{

public:

SecureApplication ();

virtual ~SecureApplication ();

void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, Time interPacketInterval, const std::string &key);

protected:

virtual void StartApplication (void);

virtual void StopApplication (void);

private:

void SendPacket ();

void ReceivePacket (Ptr<Socket> socket);

std::vector<uint8_t> Encrypt (const std::vector<uint8_t> &plaintext);

std::vector<uint8_t> Decrypt (const std::vector<uint8_t> &ciphertext);

Ptr<Socket> m_socket;

Address m_peer;

uint32_t m_packetSize;

uint32_t m_nPackets;

Time m_interval;

uint32_t m_sent;

std::string m_key;

EventId m_sendEvent;

};

SecureApplication::SecureApplication ()

: m_socket (0),

m_packetSize (0),

m_nPackets (0),

m_sent (0)

{

}

SecureApplication::~SecureApplication ()

{

m_socket = 0;

}

void

SecureApplication::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, Time interPacketInterval, const std::string &key)

{

m_socket = socket;

m_peer = address;

m_packetSize = packetSize;

m_nPackets = nPackets;

m_interval = interPacketInterval;

m_key = key;

}

void

SecureApplication::StartApplication (void)

{

m_socket->Bind ();

m_socket->Connect (m_peer);

m_socket->SetRecvCallback (MakeCallback (&SecureApplication::ReceivePacket, this));

SendPacket ();

}

void

SecureApplication::StopApplication (void)

{

if (m_socket)

{

m_socket->Close ();

}

Simulator::Cancel (m_sendEvent);

}

 

void

SecureApplication::SendPacket ()

{

std::vector<uint8_t> plaintext(m_packetSize, ‘A’); // Example data

std::vector<uint8_t> ciphertext = Encrypt (plaintext);

Ptr<Packet> packet = Create<Packet> (ciphertext.data (), ciphertext.size ());

m_socket->Send (packet);

NS_LOG_UNCOND (“Sent encrypted packet of size ” << ciphertext.size ());

if (++m_sent < m_nPackets)

{

m_sendEvent = Simulator::Schedule (m_interval, &SecureApplication::SendPacket, this);

}

}

void

SecureApplication::ReceivePacket (Ptr<Socket> socket)

{

Ptr<Packet> packet = socket->Recv ();

std::vector<uint8_t> ciphertext(packet->GetSize ());

packet->CopyData (ciphertext.data (), packet->GetSize ());

std::vector<uint8_t> plaintext = Decrypt (ciphertext);

NS_LOG_UNCOND (“Received encrypted packet of size ” << ciphertext.size () << ” and decrypted data: ” << std::string (plaintext.begin (), plaintext.end ()));

}

 

std::vector<uint8_t>

SecureApplication::Encrypt (const std::vector<uint8_t> &plaintext)

{

AES_KEY encryptKey;

AES_set_encrypt_key (reinterpret_cast<const uint8_t *>(m_key.data ()), 128, &encryptKey);

std::vector<uint8_t> ciphertext (plaintext.size () + AES_BLOCK_SIZE);

uint8_t iv[AES_BLOCK_SIZE];

RAND_bytes (iv, AES_BLOCK_SIZE);

int outlen;

AES_cfb128_encrypt (plaintext.data (), ciphertext.data (), plaintext.size (), &encryptKey, iv, &outlen, AES_ENCRYPT);

ciphertext.resize (outlen);

ciphertext.insert (ciphertext.begin (), iv, iv + AES_BLOCK_SIZE); // Prepend IV

return ciphertext;

}

std::vector<uint8_t>

SecureApplication::Decrypt (const std::vector<uint8_t> &ciphertext)

{

AES_KEY decryptKey;

AES_set_decrypt_key (reinterpret_cast<const uint8_t *>(m_key.data ()), 128, &decryptKey);

 

std::vector<uint8_t> plaintext (ciphertext.size ());

uint8_t iv[AES_BLOCK_SIZE];

std::copy (ciphertext.begin (), ciphertext.begin () + AES_BLOCK_SIZE, iv);

int outlen;

AES_cfb128_encrypt (ciphertext.data () + AES_BLOCK_SIZE, plaintext.data (), ciphertext.size () – AES_BLOCK_SIZE, &decryptKey, iv, &outlen, AES_DECRYPT);

plaintext.resize (outlen);

return plaintext;

}

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

{

CommandLine cmd;

cmd.Parse (argc, argv);

NodeContainer nodes;

nodes.Create (2); // Two nodes for secure communication

// Set up point-to-point links between nodes

PointToPointHelper pointToPoint;

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

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

NetDeviceContainer devices = pointToPoint.Install (nodes);

// Install the Internet stack on the nodes

InternetStackHelper stack;

stack.Install (nodes);

 

// Assign IP addresses to the devices

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer interfaces = address.Assign (devices);

// Set up secure applications

uint32_t packetSize = 1024;

uint32_t nPackets = 10;

Time interPacketInterval = Seconds (1.0);

std::string key = “0123456789abcdef”; // Example key

// Node A

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

Ptr<SecureApplication> appA = CreateObject<SecureApplication> ();

appA->Setup (sourceSocketA, InetSocketAddress (interfaces.GetAddress (1), 9), packetSize, nPackets, interPacketInterval, key);

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

appA->SetStartTime (Seconds (2.0));

appA->SetStopTime (Seconds (10.0));

// Node B

Ptr<Socket>sinkSocketB=Socket::CreateSocket(nodes.Get (1), UdpSocketFactory::GetTypeId ());

InetSocketAddress localB = InetSocketAddress (Ipv4Address::GetAny (), 9);

sinkSocketB->Bind (localB);

Ptr<SecureApplication> appB = CreateObject<SecureApplication> ();

nodes.Get (1)->AddApplication (appB);

sinkSocketB->SetRecvCallback (MakeCallback (&SecureApplication::ReceivePacket, appB));

// Enable packet capturing

pointToPoint.EnablePcapAll (“data-transmission-privacy”);

// Run simulation

Simulator::Run ();

Simulator::Destroy ();

return 0;

}

Explanation:

  1. Create Nodes and Network:
    • Create two nodes and connect them using point-to-point links.
    • Install the Internet stack on the nodes.
    • Assign IP addresses to the devices.
  2. Define Secure Application:
    • Create a SecureApplication class that performs encryption and decryption of data packets.
    • The Encrypt function encrypts the data using AES encryption.
    • The Decrypt function decrypts the data using AES decryption.
    • The SendPacket function creates and sends encrypted packets.
    • The ReceivePacket function receives and decrypts packets.
  3. Install and Configure the Application:
    • Create sockets for sending and receiving.
    • Install the SecureApplication on the nodes.
    • Configure the application with the necessary parameters, such as packet size, number of packets, inter-packet interval, and encryption key.
  4. Run the Simulation:
    • Schedule the start and stop times for the applications.
    • Run the simulation and log the results.

Step 4: Compile and Run the Script

  1. Save the script as data-transmission-privacy.cc in the scratch directory of your ns-3 installation.
  2. Compile the script using the following commands:

./waf configure

./waf build

./waf –run data-transmission-privacy

Step 5: Analyze the Results

We can analyze the results by looking at the logs and the pcap files generated by the packet capturing to verify the behavior of the data transmission privacy application right after running the simulation.

From the above given steps we can completely understand the applications and mechanism involved in implementing the data transmission privacy for the secure and safety of the data while transmitting it over the network.

If you’re working on implementing Data Transmission Privacy in ns3, you can get assistance from ns3simulation.com. Just share your project details with us and we’ll provide you with project ideas and coding support.