How to Implement Network IP Addressing in NS2
To implement the IP addressing in the NS2 (Network Simulator 2) that is a necessary portion of the simulating network communication, like this network permits the nodes to communicate including each other using the one and only identifiers, To execute it that contains allocating IP addresses to the nodes and set up the routing and communication among this nodes. While NS2 does not explicitly use the real-world concept of the IP addressing such as in actual network stacks, IP addresses are implicitly manages via the agents and routing procedure such as AODV, DSDV, and DSR. Given below is a vital approaches to execute it in the NS2:
Key Concepts for Network IP Addressing in NS2:
- Node Configuration: These nodes are made and set up including the agents which replicate the network layers with addressing.
- Agent Setup: The IP addressing is replicated using the agents TCP or UDP connected to nodes, then these agents manage the data transmission.
- Routing Protocols: This protocols such as AODV, DSDV, or DSR are manage the emulation of routing packets among the nodes that same to how routing is completed with IP addresses in the real time networks.
Steps to Implement IP Addressing in NS2
- Node Creation: Make a nodes within NS2 which will mimic network devices with implicit IP addresses.
- Agent Attachment: Connect the TCP/UDP agents to the nodes to replicate the communication among them.
- Routing Protocol: We can use a routing protocol such as AODV, DSDV to mimic the packet forwarding among these nodes.
- Traffic Generation: Configure the traffic among the nodes to replicate data communication, like if they had IP addresses.
- Visualization: We can use the NAM (Network Animator) to envision communication among the nodes.
Example: Simulating IP Addressing in NS2
Given instances is replicates a basic network in which two nodes communicate using TCP/IP. We will use the AODV as the routing protocol to manage the packet routing among the nodes.
Step 1: Define Nodes and Attach Agents
# Define the simulator object
set ns [new Simulator]
# Define trace and nam files for output
set tracefile [open out.tr w]
set namfile [open out.nam w]
$ns trace-all $tracefile
$ns namtrace-all $namfile
# Define a ‘finish’ procedure to end the simulation and visualize in NAM
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
exit 0
}
# Set up the topography for the simulation area
set topo [new Topography]
$topo load_flatgrid 500 500
# Define the wireless channel
set chan [new Channel/WirelessChannel]
# Configure the nodes for ad-hoc communication using AODV routing protocol
$ns node-config -adhocRouting AODV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channel $chan
# Create two nodes (node0 and node1)
set node0 [$ns node]
set node1 [$ns node]
# Set node positions for NAM visualization
$node0 set X_ 100; $node0 set Y_ 100; $node0 set Z_ 0
$node1 set X_ 400; $node1 set Y_ 400; $node1 set Z_ 0
# Step 2: Attach TCP Agents to Simulate IP Communication
# Create a TCP agent at node0 (sender)
set tcp0 [new Agent/TCP]
$ns attach-agent $node0 $tcp0
# Create a TCP sink at node1 (receiver)
set sink1 [new Agent/TCPSink]
$ns attach-agent $node1 $sink1
# Connect the TCP agent (node0) to the TCP sink (node1)
$ns connect $tcp0 $sink1
# Step 3: Set up FTP traffic on top of the TCP agent
set ftp [new Application/FTP]
$ftp attach-agent $tcp0
$ftp start 1.0 ;# Start FTP traffic at 1 second
$ftp stop 5.0 ;# Stop FTP traffic at 5 seconds
# Step 4: Schedule the end of the simulation
$ns at 6.0 “finish”
# Run the simulation
$ns run
Step 2: Explanation of the Script
- Node Creation: These two nodes like node0 and node1 are made and located in simulation space.
- Agent Attachment: A TCP agent is attached to node0, and a TCPSink agent is attached to node1. It replicates a TCP/IP connection among the two nodes.
- Routing Protocol (AODV): We use the routing protocol AODV to manage the packet routing among the nodes. This protocol implicitly manages the addressing and forwarding in the simulation.
- Traffic Generation: An FTP application is made on top of the TCP agent at node0 to transfer the data to node1 across the TCP/IP connection.
- Simulation End: The simulation concludes at 6 seconds.
Step 3: Run the Simulation
We can save the script as ip_addressing.tcl then run it using:
ns ip_addressing.tcl
It will produce a trace file (out.tr) and a NAM file (out.nam). The trace file records the packet transmissions also the NAM file permits to envision the communication among the nodes.
Step 4: Visualize the Simulation in NAM
To envision the simulation in NAM, use the below command:
nam out.nam
In the NAM, we will monitor the nodes are communicating using the TCP/IP stack. Even though IP addresses are not explicitly mentioned in the simulation NS2 and the agents replicate the IP-like communication, including routing protocols such as AODV handling the packet forwarding.
Advanced Features for IP Addressing in NS2
- Multiple Nodes and Dynamic Topology: To replicate a more difficult network with several nodes and active topology changes (e.g., node mobility).
Example:
set node2 [$ns node]
$node2 set X_ 200; $node2 set Y_ 200; $node2 set Z_ 0
$ns at 0.0 “$node2 setdest 300 300 10.0”
- UDP Communication: We can use the UDP agents to mimic the IP communication across the UDP rather than TCP.
Example:
set udp0 [new Agent/UDP]
set null1 [new Agent/Null]
$ns attach-agent $node0 $udp0
$ns attach-agent $node1 $null1
$ns connect $udp0 $null1
- Multiple Traffic Types: Configure several kinds of traffic (e.g., FTP, CBR) among the various nodes.
- IP Addressing for Wired Networks: To replicate wired networks in which nodes communicate across a fixed topology using links and duplex-links.
Example:
$ns duplex-link $node0 $node1 1Mb 10ms DropTail
- Custom Routing Protocols: Execute the custom routing algorithms in which IP-like addresses are explicitly stated in the protocol logic.
In this step-by-step procedure, we had completely delivered the instructions with samples snippets to implement the Network IP addressing in the simulation platform NS2. Further concepts and ideas will be shared another material, if you required.
For additional assistance with Network IP Addressing projects using the ns2 tool, please feel free to contact us. We provide exemplary implementation guidance and also support you with network performance analysis for your project.