Forum Replies Created
Hi all,
does anyone, having tryed to implement the code of this article (“Implementing a New Manet Unicast Routing Protocol in NS2”), have any .tcl simulation code to test it??? I need it to know well how the algorithm works…
Thks
Maurizio
This is true, but I don’t know why. Firstly I tried with only one Protoname agent and the other was a sink. It worked…The Protoname generated flooding packets and sink received them. But when I put two Protoname agents exchanging flooding packet each other, the program went out with a segmentation fault. I tried to debug it and find that probably the program was blocked on DSDV.cc. I can’t understand the reason why it’s not possible for me to have more Protoname agents flooding the network.
Plz every suggestion would be welcome
Thank you
Best Regards
Maurizio
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 */