Lab File of Ad Hoc and Sensor Networks Lab (ETEC - 458) : Department of Information Technology
Lab File of Ad Hoc and Sensor Networks Lab (ETEC - 458) : Department of Information Technology
Of
AD HOC AND SENSOR NETWORKS LAB
(ETEC – 458)
Submitted To : Submitted By :
Ms. Monica Swati
Assistant Professor, IT 03615603117
IT – F9
Swati IT - F9 ROLL NO. : 03615603117
INDEX
S No. NAME OF PARTICULARS SIGN
2
Swati IT - F9 ROLL NO. : 03615603117
PROGRAM NO-1
Theory:
A mobile ad hoc network (MANET) is a continuously self-configuring, infrastructure-less network
of mobile devices connected wirelessly.
Each device in a MANET is free to move independently in any direction, and will therefore change
its links to other devices frequently. Each must forward traffic unrelated to its own use, and
therefore be a router. The primary challenge in building a MANET is equipping each device to
continuously maintain the information required to properly route traffic. Such networks may
operate by themselves or may be connected to the larger Internet. They may contain one or multiple
and different transceivers between nodes. This results in a highly dynamic, autonomous topology
MANETs are a kind of wireless ad hoc network (WANET) that usually has a routable networking
environment on top of a Link Layer ad hoc network. MANETs consist of a peer-to-peer, self-
forming, self-healing network. MANETs circa 2000-2015 typically communicate at radio
frequencies (30 MHz - 5 GHz)
The growth of laptops and 802.11/Wi-Fi wireless networking have made MANETs a popular
research topic since the mid-1990s. Many academic papers evaluate protocols and their abilities,
assuming varying degrees of mobility within a bounded space, usually with all nodes within a few
hops of each other. Different protocols are then evaluated based on measures such as the packet
drop rate, the overhead introduced by the routing protocol, end-to-end packet delays, network
throughput, ability to scale, etc.
Types:
1. Vehicular ad hoc networks (VANETs) are used for communication between vehicles and
roadside equipment. Intelligent vehicular ad hoc networks (InVANETs) are a kind of artificial
intelligence that helps vehicles to behave in intelligent manners during vehicle-to-vehicle collisions,
accidents.
2. Smart phone ad hoc networks (SPANs) leverage the existing hardware (primarily Bluetooth
and Wi-Fi) in commercially available smart phones to create peer-to-peer networks without relying
on cellular carrier networks, wireless access points, or traditional network infrastructure. SPANs
differ from traditional hub and spoke networks, such as Wi-Fi Direct, in that they support multi-hop
3
Swati IT - F9 ROLL NO. : 03615603117
relays and there is no notion of a group leader so peers can join and leave at will without destroying
the network.
3. Internet-based mobile ad-hoc networks (iMANETs) are ad hoc networks that link mobile
nodes and fixed Internet-gateway nodes. For example, multiple sub-MANETs may be connected in
a classic Hub-Spoke VPN to create a geographically distributed MANET. In such type of networks
normal ad hoc routing algorithms don't apply directly. One implementation of this is Persistent
System's CloudRelay.
Military or tactical MANETs are used by military units with emphasis on security, range, and
integration with existing systems. Common waveforms include the US Army's SRW.
4
Swati IT - F9 ROLL NO. : 03615603117
PROGRAM NO-2
Theory:
Wireless sensor networks (WSN), sometimes called wireless sensor and actuator
networks (WSAN), are spatially distributed autonomous sensors to monitor physical or
environmental conditions, such as temperature, sound, pressure, etc. and to cooperatively pass their
data through the network to a main location. The more modern networks are bi-directional, also
enabling control of sensor activity. The development of wireless sensor networks was motivated by
military applications such as battlefield surveillance; today such networks are used in many
industrial and consumer applications, such as industrial process monitoring and control, machine
health monitoring, and so on.
In this wireless sensor network setup each wireless node has its own default address which is
shown in the figure that is
● 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 and 19 are the leaf node addresses.
When leaf node sends sensed data to the cluster node, cluster node does the average of the leaf
node data and will send it to the sink node. In this experiment the remote user can give three inputs
which are listed below
1. Network type: Remote user has the option to select any one of single hop network or
hierarchical network.
Single hop network: In this network, leaf nodes 3, 4, 5, 6 and 7 can be connected to the sink node.
5
Swati IT - F9 ROLL NO. : 03615603117
Hierarchical network: In this network, leaf nodes 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
and 19 can be connected to the cluster nodes 2, 8 and 14 and these cluster nodes are connected to
the sink node.
2. Number of nodes: In a single hop network, the minimum number of nodes are 2 and the
maximum number of nodes selected is 6. If the user selects number of nodes as 4, then leaf nodes 3,
4 and 5 are connected to the sink node and thus the total number of nodes in the network is 4.
In a hierarchical network, minimum number of nodes are 3 and maximum number of nodes selected
is 19. If the user selects number of nodes as 10, then the leaf nodes 3, 4, 5, 6 and 7 gets connected to
the cluster node 2. Leaf nodes 9, 10 gets connected to the cluster node 8. The cluster nodes 2 and 8
are connected to the sink node 1. Thus the total number of nodes in the network is 10.
3. Power level: User can operate the wireless sensor network in different power levels.
Procedure
1.2. Identify the components which are providing the required interfaces
2. Implement the module file :This is the file, where we are implementing the actual task. Here
the tasks include Implementing two types of network, i.e centralized network and hierarchical
network. For that, we need 4sets on nodes(Gateway node, Sink node, Cluster head and leaf node)
6
Swati IT - F9 ROLL NO. : 03615603117
implementation
/*Radio related */
leafnodeM.CC2420Control -> CC2420RadioC.CC2420Control;
leafnodeM.SendMsg -> GenericComm.SendMsg[AM_INTMSG];
leafnodeM.ReceiveMsg -> GenericComm.ReceiveMsg[AM_INTMSG];
includes IntMsg;
module
provides
interface StdControl;
uses
{
interface Timer as Timer1; //Idle timer to reset
interface Timer as Timer2; //Sensing timer
interface Timer as Timer3;
interface Leds;
interface SendMsg;
7
Swati IT - F9 ROLL NO. : 03615603117
interface ReceiveMsg;
interface CC2420Control; //TxPower set
interface ADC as ReadTemp;
interface ADC as ReadHum;
interface SplitControl as SensControl;
8
Swati IT - F9 ROLL NO. : 03615603117
Cluster head:
The components used are
implementation
{
includes IntMsg;
module
9
Swati IT - F9 ROLL NO. : 03615603117
interface SendMsg;
interface ReceiveMsg;
interface CC2420Control;
}
}
implementation
{
Sink node
components used are
10
Swati IT - F9 ROLL NO. : 03615603117
implementation
module SinknodeM
{
provides
interface StdControl;
uses
{
interface Leds;
interface Timer as Timer1;
interface Timer as Timer2;
interface SendMsg;
interface ReceiveMsg;
interface CC2420Control;
}
implementation
{
11
Swati IT - F9 ROLL NO. : 03615603117
implementation
{
In module file:
includes IntMsg;
module
{
provides
interface StdControl;
uses
{
interface Leds;
interface SendMsg;
interface HPLUART;
interface ReceiveMsg;
interface Timer as UARTTimer;
}
}
12
Swati IT - F9 ROLL NO. : 03615603117
implementation
{
13
Swati IT - F9 ROLL NO. : 03615603117
PROGRAM NO-3
Aim: Implementation of AODV routing protocol in ns2
14
Swati IT - F9 ROLL NO. : 03615603117
Theory:
The reactive on demand routing protocols establish the route to a particular destination only if it is
needed. Adhoc on-demand Distance Vector (AODV) is one of the commonly used reactive on
demand routing protocols in mobile ad hoc network (MANET). AODV is a reactive enhancement
of the DSDV protocol. The route discovery process involves ROUTE REQUEST (RREQ) and
ROUTE REPLY (RREP) packets. The source node initiates the route requested through the route
discovery process using RREQ packets. The generated route request is forwarded to the neighbors
of the source node and this process is repeated till it reaches the destination. On receiving a RREQ
packet, an intermediate node with route to destination, it generates a RREP containing the number
of hops required to reach the destination. All intermediate nodes that participates in relaying this
reply to the source node creates a forward route to destination. AODV minimizes the number of
packets involved in route discovery by establishing routes on-demand. The sample15.tcl shows a
node configuration for a wireless mobile node that runs AODV as its adhoc routing protocol. Prior
to the establishment of communication between the source and receiver node, the routing protocol
should be mentioned to find the route between them. Data Transmission is established between
nodes using UDP agent and CBR traffic.
Procedure:
15
Swati IT - F9 ROLL NO. : 03615603117
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
# Node Creation
}
#******************************Defining Communication Between node0 and all nodes
****************************8
16
Swati IT - F9 ROLL NO. : 03615603117
OUTPUT:
17
Swati IT - F9 ROLL NO. : 03615603117
18
Swati IT - F9 ROLL NO. : 03615603117
PROGRAM NO-4
Aim: Implementation of DSR routing protocol in ns2
Theory:
The reactive on demand routing protocols establish the route to a particular destination only if it is
needed. Dynamic Source Routing (DSR) is one of the commonly used reactive on demand routing
protocols in mobile ad hoc network (MANET). DSR is a loop-free, source based, on-demand
routing protocol in which each node maintains a route cache consisting of source routes known by
the node. A source node initiates a fresh route discovery process only if it does not already have a
valid route to the destination in its route cache. The entries in the route cache are regularly updated
as new routes are known. The sample16.tcl shows a node configuration for a wireless mobile node
that runs DSR as its adhoc routing protocol. Prior to the establishment of communication between
the source and receiver node, the routing protocol should be mentioned to find the route between
them. Data Transmission is established between nodes using UDP agent and CBR traffic.
Procedure:
19
Swati IT - F9 ROLL NO. : 03615603117
# Node Creation
}
Defining Communication between node0 and all nodes ****************************
20
Swati IT - F9 ROLL NO. : 03615603117
21
Swati IT - F9 ROLL NO. : 03615603117
OUTPUT:
22
Swati IT - F9 ROLL NO. : 03615603117
PROGRAM NO - 5
Aim: Implementation of DSDV routing protocol in ns2
Theory:
Proactive routing protocol maintains constant and updated routing information for each pair of
networking nodes by propagating route updates proactively at fixed interval of time. The periodic
and event-driven messages are responsible for route establishment and route maintenance. The
Destination-Sequenced Distance Vector (DSDV) protocol is the commonly used proactive routing
protocol in mobile ad hoc network (MANET). In DSDV, each node maintains a routing table with
one route entry for each destination in which the shortest path is recorded. It uses a destination
sequence number to avoid routing loops. The sample17.tcl shows a node configuration for a
wireless mobile node that runs DSDV as its adhoc routing protocol. Prior to the establishment of
communication between the source and receiver node, the routing protocol should be mentioned to
find the route between them. Data Transmission is established between nodes using UDP agent and
CBR traffic. Routing process follows DSDV routing protocol.
Procedure:
23
Swati IT - F9 ROLL NO. : 03615603117
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
# Node Creation
24
Swati IT - F9 ROLL NO. : 03615603117
25
Swati IT - F9 ROLL NO. : 03615603117
OUTPUT:
26
Swati IT - F9 ROLL NO. : 03615603117
PROGRAM NO - 6
Aim: Creating Wireless Sensor Network (WSN) in ns2
Theory:
A wireless sensor network (WSN) consists of a large number of small sensor nodes that are
deployed in the area in which a factor is to be monitored. In wireless sensor network, energy model
is one of the optional attributes of a node. The energy model denotes the level of energy in a mobile
node. The components required for designing energy model includes initialEnergy, txPower,
rxPower, and idlePower. The “initialEnergy” represents the level of energy the node has at the
initial stage of simulation. “txPower” and “rxPower” denotes the energy consumed for transmitting
and receiving the packets. If the node is a sensor, the energy model should include a special
component called “sensePower”. It denotes the energy consumed during the sensing operation.
Apart from these components, it is important to specify the communication range (RXThresh_) and
sensing range of a node (CSThresh_). The sample 18.tcl designs a WSN in which sensor nodes are
configured with different communication and sensing range. Base Station is configured with highest
communication range. Data Transmission is established between nodes using UDP agent and CBR
traffic.
Procedure:
27
Swati IT - F9 ROLL NO. : 03615603117
Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 1.5
Antenna/OmniAntenna set Gt_ 1.0
Antenna/OmniAntenna set Gr_ 1.0
28
Swati IT - F9 ROLL NO. : 03615603117
-channelType $val(chan) \
-topoInstance $topo \
-energyModel $val(energymodel) \
-initialEnergy 10 \
-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3 \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
# Node Creation
29
Swati IT - F9 ROLL NO. : 03615603117
-rxPower 0.5 \
-txPower 1.0 \
-idlePower 0.0 \
-sensePower 0.3
set node_($i) [$ns node]
$node_($i) color black
30
Swati IT - F9 ROLL NO. : 03615603117
OUTPUT:
31
Swati IT - F9 ROLL NO. : 03615603117
PROGRAM NO-7
Aim: Creating shortest path for mobile sink in ns2
Theory:
In the sensor network after clusterhead selection, cluster member sends data to clusterhead.
Clusterhead aggregates the received data from cluster member. In mobile sink network, only the
sink moves to collect data from clusterhead whereas in static sink case, data is sent in multiple hops
to sink from clusterhead. So sink mobility scheme achieves energy optimization with certain delay
due to the time taken for the arrival sink nearer to clusterhead. Hence the sink moves along the path
as per the shortest path algorithm. The code given below provides the logic for shortest path in grid
based clustering.
Procedure:
32
Swati IT - F9 ROLL NO. : 03615603117
OUTPUT:
Sink node starting position is (0, 0).
33
Swati IT - F9 ROLL NO. : 03615603117
PROGRAM NO-8
Aim: Creating Blackhole attack in MANET using ns2
Theory:
A Mobile Ad hoc Network (MANET) comprises of mobile nodes that moves independently in an
open environment. Communication between the nodes in a MANET is enabled with the aid of
intermediate routers. The nature of MANET such as open medium, dynamic network topology, lack
of centralized monitoring, and lack of clear defense mechanisms makes it vulnerable to several
routing attacks. In MANET routing, there is a high probability for intermediate nodes to be
malicious that might be a threat to the security. Blackhole is the common attack in ad hoc routing in
which the malicious node uses the process of routing to state itself of being the shortest path to the
destination. Once it receives the data packets, it drops the data packets instead of forwarding them
to its neighbors. In sample20.tcl, the blackhole attacker does not obey the communication model.
Data Transmission is established between nodes using UDP agent and CBR traffic. Sender sends
the data via attacker. Source node transfers data to attacker that does not have shortest route to
Destination. Attacker does not forward data to its neighbours.
Procedure:
$ns at 3.1 "$ns trace-annotate \" Data transmission from source node to destination node\""
34
Swati IT - F9 ROLL NO. : 03615603117
$ns at 5.1 "$ns trace-annotate \" Node_5 is malicious node which node drops the packet \""
35
Swati IT - F9 ROLL NO. : 03615603117
OUTPUT:
36
Swati IT - F9 ROLL NO. : 03615603117
PROGRAM NO-9
Aim: Dynamic Nodes generation and traffic flow in ns2
#Create a simulator object
set ns [new Simulator]
37
Swati IT - F9 ROLL NO. : 03615603117
#Schedule events for the CBR agent and the network dynamics
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
38
Swati IT - F9 ROLL NO. : 03615603117
Output:
Then, down the links and see how dynamically traffic flow by following the other
path
39