- Home Page of ProjectGuideline.com ›
- Forums ›
- Get Help to do your B.E., and M.E., M.Tech., & M.S., Project in Ns2 ›
- Implementing a New Manet Unicast Routing Protocol in NS2
Hi all,
I’m trying to simulate a MANET in order to test an efficient flooding technique (broadcast). I took the code in this article, without using the routing part, because I don’t need. I want only to flood the network with control information. I wrote a little simulation to test the algorithm. Two nodes flooding packets. The problem is I get a segmentation fault and I don’t know why.
I attach the simulation file.
Thank you a lot
Best Regards
Maurizio
# simple-wireless.tcl
# A simple example for wireless simulation
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 2 ;# number of mobilenodes
set val(rp) AODV ;# routing protocol
# Main Program
#
# Initialize Global Variables
#
set ns_ [new Simulator]
set tracefd [open simple.tr w]
$ns_ trace-all $tracefd
# set up topography object
set topo [new Topography]
$topo load_flatgrid 500 500
#
# Create God
#
create-god $val(nn)
#
# Create the specified number of mobilenodes [$val(nn)] and "attach"
# them to the channel.
# Here two nodes are created : node(0) and node(1)
# configure node
$ns_ node-config -adhocRouting $val(rp)
-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 OFF
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) random-motion 0
}
#
# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
#
$node_(0) set X_ 5.0
$node_(0) set Y_ 2.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 390.0
$node_(1) set Y_ 385.0
$node_(1) set Z_ 0.0
#
# Now produce some simple node movements
# Node_(1) starts to move towards node_(0)
#
$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"
# Node_(1) then starts to move away from node_(0)
$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0"
# Setup traffic flow between nodes
# TCP connections between node_(0) and node_(1)
set proto0 [new Agent/Protoname]
set proto1 [new Agent/Protoname]
$ns_ attach-agent $node_(0) $proto0
$ns_ attach-agent $node_(1) $proto1
$ns_ connect $proto0 $proto1
#set ftp [new Application/FTP]
#$ftp attach-agent $tcp
$ns_ at 5.0 "$proto0 start"
$ns_ at 5.0 "$proto1 start"
#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 150.0 "$node_($i) reset";
}
$ns_ at 150.0 "stop"
$ns_ at 150.01 "puts "NS EXITING..." ; $ns_ halt"
proc stop {} {
global ns_ tracefd
$ns_ flush-trace
close $tracefd
}
puts "Starting Simulation..."
$ns_ run
From which article you took the code?
How you implemented Agent/Protoname?
and what it will do?
Hi,
I modified a code found on the article “Implementing a New Manet Unicast Routing Protocol in NS2”. I attach the files… some parts are commented because I don’t want to use the routing table only want to test broadcast traffic.
protoname/protoname.h
/*
* File: protoname.h
* Author: Maurizio Bellemo
*
* Created on August 18, 2008, 6:48 PM
*/
#ifndef _PROTONAME_H
#define _PROTONAME_H
#include “protoname_pkt.h”
#include
#include
#include
#include
#include
#include
#define CURRENT_TIME Scheduler::instance().clock()
#define JITTER (Random::uniform()*0.5)
class Protoname; // forward declaration
/* Timers */
class Protoname_PktTimer : public TimerHandler {
public:
Protoname_PktTimer(Protoname* agent) : TimerHandler() {
agent_ = agent;
}
protected:
Protoname* agent_;
virtual void expire(Event* e);
};
/* Agent */
class Protoname : public Agent {
/* Friends */
friend class Protoname_PktTimer;
/* Private members */
nsaddr_t ra_addr_;
/*protoname_state*/
int state_;
int accessible_var_;
u_int8_t seq_num_;
protected:
PortClassifier* dmux_; // For passing up to agents
Trace* logtarget_; // For logging
Protoname_PktTimer pkt_timer_; // Timer for sending packets
inline nsaddr_t& ra_addr() { return ra_addr_; }
inline int& state() { return state_; }
inline int& accessible_var() { return accessible_var_; }
void forward_data(Packet*);
void recv_protoname_pkt(Packet*);
void send_protoname_pkt();
void reset_protoname_pkt_timer();
public:
Protoname(nsaddr_t);
int command(int, const char*const*);
void recv(Packet*, Handler*);
};
#endif /* _PROTONAME_H */
protoname/protoname.cc
/*
* File: protoname.cc
* Author: Maurizio Bellemo
*
* Created on August 18, 2008, 6:27 PM
*/
#include “protoname.h”
//#include “protoname_pkt.h”
int hdr_protoname_pkt::offset_;
static class ProtonameHeaderClass : public PacketHeaderClass {
public:
ProtonameHeaderClass() : PacketHeaderClass(“PacketHeader/Protoname”, sizeof(hdr_protoname_pkt)) {
bind_offset(&hdr_protoname_pkt::offset_);
}
} class_rtProtoProtoname_hdr;
static class ProtonameClass : public TclClass {
public:
ProtonameClass() : TclClass(“Agent/Protoname”) {}
TclObject* create(int argc, const char*const* argv) {
assert(argc == 5);
return(new Protoname((nsaddr_t)Address::instance().str2addr(argv[4])));
}
} class_rtProtoProtoname;
void
Protoname_PktTimer::expire(Event* e) {
agent_->send_protoname_pkt();
agent_->reset_protoname_pkt_timer();
}
Protoname::Protoname(nsaddr_t id) : Agent(PT_PROTONAME), pkt_timer_(this) {
bind_bool(“accessible_var_”, &accessible_var_);
ra_addr_ = id;
}
int
Protoname::command(int argc, const char*const* argv) {
if (argc == 2) {
if (strcasecmp(argv[1], “start”) == 0) {
pkt_timer_.resched(0.0);
return TCL_OK;
}
else if (strcasecmp(argv[1], “print_rtable”) == 0) {
if (logtarget_ != 0) {
sprintf(logtarget_->pt_->buffer(), “P %f _%d_ Routing Table”, CURRENT_TIME, ra_addr());
logtarget_->pt_->dump();
//rtable_.print(logtarget_);
}
else {
fprintf(stdout, “%f _%d_ If you want to print this routing table you must create a trace file in your tcl script”, CURRENT_TIME, ra_addr());
}
return TCL_OK;
}
}
else if (argc == 3) {
// Obtains corresponding dmux to carry packets to upper layers
if (strcmp(argv[1], “port-dmux”) == 0) {
dmux_ = (PortClassifier*)TclObject::lookup(argv[2]);
if (dmux_ == 0) {
fprintf(stderr, “%s: %s lookup of %s failedn”, __FILE__, argv[1], argv[2]);
return TCL_ERROR;
}
return TCL_OK;
}
// Obtains corresponding tracer
else if (strcmp(argv[1], “log-target”) == 0 || strcmp(argv[1], “tracetarget”) == 0) {
logtarget_ = (Trace*)TclObject::lookup(argv[2]);
if (logtarget_ == 0)
return TCL_ERROR;
return TCL_OK;
}
}
// Pass the command to the base class
return Agent::command(argc, argv);
}
void
Protoname::recv(Packet *p, Handler* h) {
struct hdr_cmn* ch = HDR_CMN(p);
struct hdr_ip* ih = HDR_IP(p);
if(ih->saddr() == ra_addr()) {
// If there exists a loop, must drop the packet
if(ch->num_forwards() > 0) {
drop(p, DROP_RTR_ROUTE_LOOP);
return;
}
// else if this is a packet I am originating, must add IP header
else if (ch->num_forwards() == 0)
ch->size() += IP_HDR_LEN;
}
// If it is a protoname packet, must process it
if(ch->ptype() == PT_PROTONAME)
recv_protoname_pkt(p);
// Otherwise, must forward the packet (unless TTL has reached zero)
else {
ih->ttl_–;
if(ih->ttl_ == 0) {
drop(p, DROP_RTR_TTL);
return;
}
forward_data(p);
}
}
void
Protoname::recv_protoname_pkt(Packet* p) {
struct hdr_ip* ih = HDR_IP(p);
struct hdr_protoname_pkt* ph = HDR_PROTONAME_PKT(p);
// All routing messages are sent from and to port RT_PORT, so we check it
assert(ih->sport() == RT_PORT);
assert(ih->dport() == RT_PORT);
// processing of protoname packet
// Release resources
Packet::free(p);
}
void
Protoname::send_protoname_pkt() {
Packet* p = allocpkt();
struct hdr_cmn* ch = HDR_CMN(p);
struct hdr_ip* ih = HDR_IP(p);
struct hdr_protoname_pkt* ph = HDR_PROTONAME_PKT(p);
ph->pkt_src() = ra_addr();
ph->pkt_len() = 7;
ph->pkt_seq_num() = seq_num_++;
ch->ptype() = PT_PROTONAME;
ch->direction() = hdr_cmn::DOWN;
ch->size() = IP_HDR_LEN + ph->pkt_len();
ch->error() = 0;
ch->next_hop() = IP_BROADCAST;
ch->addr_type() = NS_AF_INET;
ih->saddr() = ra_addr();
ih->daddr() = IP_BROADCAST;
ih->sport() = RT_PORT;
ih->dport() = RT_PORT;
ih->ttl() = IP_DEF_TTL;
Scheduler::instance().schedule(target_, p, JITTER);
}
void
Protoname::reset_protoname_pkt_timer() {
pkt_timer_.resched((double)5.0);
}
void
Protoname::forward_data(Packet *p) {
struct hdr_cmn* ch = HDR_CMN(p);
struct hdr_ip* ih = HDR_IP(p);
if(ch->direction() == hdr_cmn::UP && ((u_int32_t)ih->daddr() == IP_BROADCAST || ih->daddr() == ra_addr())) {
dmux_->recv(p, NULL);
return;
}
else {
ch->direction() = hdr_cmn::DOWN;
ch->addr_type() = NS_AF_INET;
if((u_int32_t)ih->daddr() == IP_BROADCAST)
ch->next_hop() = IP_BROADCAST;
/*else {
nsaddr_t next_hop = rtable_.lookup(ih->daddr());
if(next_hop == IP_BROADCAST) {
debug(“%f: Agent %d can not forward a packet destined to %dn”, CURRENT_TIME, ra_addr(), ih->daddr());
drop(p, DROP_RTR_NO_ROUTE);
return;
}
else
ch->next_hop() = next_hop;
}*/
Scheduler::instance().schedule(target_, p, 0.0);
}
}
protoname/protoname_pkt.h
/*
* File: protoname_pkt.h
* Author: Maurizio Bellemo
*
* Created on August 18, 2008, 6:08 PM
*/
#ifndef _PROTONAME_PKT_H
#define _PROTONAME_PKT_H
#include
#define HDR_PROTONAME_PKT(p) hdr_protoname_pkt::access(p)
struct hdr_protoname_pkt {
nsaddr_t pkt_src_; // Node which originated this packet
u_int16_t pkt_len_; // Packet length (in bytes)
u_int8_t pkt_seq_num_; // Packet sequence number
inline nsaddr_t& pkt_src() { return pkt_src_; }
inline u_int16_t& pkt_len() { return pkt_len_; }
inline u_int8_t& pkt_seq_num() { return pkt_seq_num_; }
static int offset_;
inline static int& offset() { return offset_; }
inline static hdr_protoname_pkt* access(const Packet* p) {
return (hdr_protoname_pkt*)p->access(offset_);
}
};
#endif /* _PROTONAME_PKT_H */