| 1 |
/********************************************************************* |
| 2 |
* |
| 3 |
* AUTHORIZATION TO USE AND DISTRIBUTE |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that: |
| 7 |
* |
| 8 |
* (1) source code distributions retain this paragraph in its entirety, |
| 9 |
* |
| 10 |
* (2) distributions including binary code include this paragraph in |
| 11 |
* its entirety in the documentation or other materials provided |
| 12 |
* with the distribution, and |
| 13 |
* |
| 14 |
* (3) all advertising materials mentioning features or use of this |
| 15 |
* software display the following acknowledgment: |
| 16 |
* |
| 17 |
* "This product includes software written and developed |
| 18 |
* by Brian Adamson, Joe Macker and Justin Dean of the |
| 19 |
* Naval Research Laboratory (NRL)." |
| 20 |
* |
| 21 |
* The name of NRL, the name(s) of NRL employee(s), or any entity |
| 22 |
* of the United States Government may not be used to endorse or |
| 23 |
* promote products derived from this software, nor does the |
| 24 |
* inclusion of the NRL written and developed software directly or |
| 25 |
* indirectly suggest NRL or United States Government endorsement |
| 26 |
* of this product. |
| 27 |
* |
| 28 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 29 |
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 30 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 31 |
********************************************************************/ |
| 32 |
|
| 33 |
// New router template for adding a protocol to CMU extensions of ns2 |
| 34 |
|
| 35 |
// IT changed absolute references to header files - 7/17/2008 - don't know why they |
| 36 |
// would need to include protolib/ns ... |
| 37 |
#include <nsProtoManetKernel.h> |
| 38 |
#include <nsProtoManetKernel_packet.h> |
| 39 |
|
| 40 |
/* |
| 41 |
TCL Hooks |
| 42 |
*/ |
| 43 |
int hdr_ProtolibMK::offset_; |
| 44 |
|
| 45 |
static class ProtolibMKHeaderClass : public PacketHeaderClass { |
| 46 |
public: |
| 47 |
|
| 48 |
ProtolibMKHeaderClass() : PacketHeaderClass("PacketHeader/ProtolibMK",//0){ |
| 49 |
sizeof(hdr_ProtolibMK)) { //this is might not be correct... |
| 50 |
bind_offset(&hdr_ProtolibMK::offset_); |
| 51 |
} |
| 52 |
} class_rtProtoProtolibMK_hdr; |
| 53 |
|
| 54 |
|
| 55 |
static class ProtolibMKclass : public TclClass { |
| 56 |
public: |
| 57 |
ProtolibMKclass() : TclClass("Agent/ProtolibMK") {} |
| 58 |
TclObject* create(int argc, const char*const* argv) { |
| 59 |
assert(argc == 5); |
| 60 |
return (new ProtolibMK((nsaddr_t) Address::instance().str2addr(argv[4]))); |
| 61 |
} |
| 62 |
} class_rtProtoProtolibMK; |
| 63 |
|
| 64 |
// interface with tcl scripts can change valuse on the fly by getting the class instance handle in tcl |
| 65 |
int |
| 66 |
ProtolibMK::command(int argc, const char*const* argv) { |
| 67 |
if(argc == 2) { |
| 68 |
Tcl& tcl = Tcl::instance(); |
| 69 |
|
| 70 |
if(strncasecmp(argv[1], "id", 2) == 0) { |
| 71 |
tcl.resultf("%d", index); |
| 72 |
return TCL_OK; |
| 73 |
} |
| 74 |
|
| 75 |
if(strncasecmp(argv[1], "start", 2) == 0) { |
| 76 |
return TCL_OK; |
| 77 |
} |
| 78 |
} |
| 79 |
else if(argc == 3) { |
| 80 |
if(strcmp(argv[1], "port-dmux") == 0) { |
| 81 |
dmux_ = (PortClassifier *)TclObject::lookup(argv[2]); |
| 82 |
if (dmux_ == 0) { |
| 83 |
fprintf(stderr,"protolibManetKernel::command: %s lookup of %s failed\n",argv[1],argv[2]); |
| 84 |
return TCL_ERROR; |
| 85 |
} |
| 86 |
return TCL_OK; |
| 87 |
} |
| 88 |
else if(strcmp(argv[1], "attach-manet") == 0) { |
| 89 |
manetpointer = (Agent*)TclObject::lookup(argv[2]); |
| 90 |
return TCL_OK; |
| 91 |
} |
| 92 |
else if(strcmp(argv[1], "log-target" ) == 0 || strcmp(argv[1], "tracetarget") == 0) { |
| 93 |
logtarget = (Trace*) TclObject::lookup(argv[2]); |
| 94 |
if(logtarget == 0) |
| 95 |
return TCL_ERROR; |
| 96 |
return TCL_OK; |
| 97 |
} |
| 98 |
else if(strcmp(argv[1], "if-queue") == 0) { |
| 99 |
ifqueue = (PriQueue*) TclObject::lookup(argv[2]); |
| 100 |
if(ifqueue == 0) |
| 101 |
return TCL_ERROR; |
| 102 |
return TCL_OK; |
| 103 |
} |
| 104 |
else if(strcmp(argv[1], "target") == 0) { |
| 105 |
Tcl& tcl = Tcl::instance(); |
| 106 |
tcl.evalf("%s set node_", this->name()); |
| 107 |
tcl.evalf("%s set ifq_(0)",tcl.result()); |
| 108 |
ifqueue = (PriQueue*) TclObject::lookup(tcl.result()); |
| 109 |
return Agent::command(argc, argv); |
| 110 |
} |
| 111 |
} |
| 112 |
return Agent::command(argc, argv); |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
Constructor |
| 117 |
*/ |
| 118 |
|
| 119 |
ProtolibMK::ProtolibMK(nsaddr_t id) : Agent(PT_PROTOLIBMK) { |
| 120 |
srand(10); |
| 121 |
index = id; |
| 122 |
logtarget = 0; |
| 123 |
numberofforwards = 0; |
| 124 |
numberofrecv = 0; |
| 125 |
idnumber = 0; |
| 126 |
} |
| 127 |
|
| 128 |
void |
| 129 |
ProtolibMK::recv(Packet *p, Handler*) { |
| 130 |
struct hdr_cmn *ch = HDR_CMN(p); |
| 131 |
struct hdr_ip *ih = HDR_IP(p); |
| 132 |
assert(initialized()); |
| 133 |
/* fprintf(stdout,"\nAddress %d at time %f\n ",(int)here_.addr_,Scheduler::instance().clock()); |
| 134 |
fprintf(stdout,"Pmanet cm header %d type, %d size, %d uid, %d errorf",ch->ptype_,ch->size_,ch->uid_,ch->error_); |
| 135 |
fprintf(stdout,", %d errbit, %d fecsize, %f ts, %d iface",ch->errbitcnt_,ch->fecsize_,ch->ts_,ch->iface_); |
| 136 |
if(ch->direction_==-1) |
| 137 |
fprintf(stdout,", DOWN\n"); |
| 138 |
else if(ch->direction_==0) |
| 139 |
fprintf(stdout,", NONE\n"); |
| 140 |
else |
| 141 |
fprintf(stdout,", UP\n"); |
| 142 |
fprintf(stdout,"Pmanet cmd contu %d prev, %d next, %d last, %d num forwards\n",ch->prev_hop_,ch->next_hop_,ch->last_hop_,ch->num_forwards());*/ |
| 143 |
//if(here_.addr_==1 && ih->dport()!=698){ |
| 144 |
// fprintf(stdout,"Pmanet ip header %d src %d dst %d ttl %d sport %d dport %d fid %d prio\n",(int)ih->src_.addr_,(int)ih->dst_.addr_,ih->ttl_,ih->sport(),ih->dport(),ih->fid_,ih->prio_); |
| 145 |
// fflush(stdout); |
| 146 |
// } |
| 147 |
|
| 148 |
if(ch->ptype() == PT_PROTOLIBMK) { |
| 149 |
ih->ttl_ -= 1; |
| 150 |
// fprintf(stdout,"sending packet to manetpointer recv and freeing up packet\n"); |
| 151 |
manetpointer->recv(p,NULL); |
| 152 |
return; |
| 153 |
} |
| 154 |
if((ih->saddr() == index) && (ch->num_forwards() == 0)) { |
| 155 |
// if I'm originating a (nonOLSR) packet... |
| 156 |
//ch->size() += IP_HDR_LEN; //I don't think I need to add the ip header length after all |
| 157 |
if(ih->ttl_ == 0) |
| 158 |
ih->ttl_ = 255; // default ttl value to 255 |
| 159 |
ch->uid_ = idnumber++; //replacing global id number with local node id number |
| 160 |
//ih->fid_ = idnumber++; //attempting to use the ip flow id number instead of the unique id number for duplicate detection. |
| 161 |
if(ih->dport()==698){ |
| 162 |
ch->ptype() = PT_PROTOLIBMK; |
| 163 |
ih->daddr() = IP_BROADCAST; |
| 164 |
// fprintf(stdout,"setting type to pt_pmanet %d==%d?\n",ch->ptype(),PT_PROTOLIBMK); |
| 165 |
} |
| 166 |
if(ih->daddr()== (int)IP_BROADCAST){ // if its broadcast message it need more configuration |
| 167 |
//ih->ttl_ = 1; //32 seems reasonable |
| 168 |
} |
| 169 |
} else if(ih->saddr() == index) { |
| 170 |
// fprintf(stdout,"dropping packet because of a route loop?"); |
| 171 |
//drop(p, DROP_RTR_ROUTE_LOOP); |
| 172 |
Packet::free(p); |
| 173 |
return; |
| 174 |
} else { |
| 175 |
if(--ih->ttl_ == 0) { |
| 176 |
// fprintf(stdout,"dropping packet because of ttl value\n"); |
| 177 |
drop(p, DROP_RTR_TTL); |
| 178 |
return; |
| 179 |
} |
| 180 |
} |
| 181 |
if(ih->daddr()== (int)IP_BROADCAST){ |
| 182 |
//fprintf(stdout,"at time %f %d is sending bcast packet to %d %d==%d? on port %d invalid %d\n",Scheduler::instance().clock(),here_.addr_,ih->daddr(),PT_PROTOLIBMK,ch->ptype(),ih->dport(),INVALID); |
| 183 |
if(ih->dport()!=-1){ |
| 184 |
if(ih->dport()!=698){ //use forwarding engine to forward all broadcast packets |
| 185 |
//if(here_.addr_==1){ |
| 186 |
// fprintf(stdout,"ProtolibMK accepting bcast packet %d,%d at time %f\n",++numberofrecv,ih->fid_,Scheduler::instance().clock()); |
| 187 |
//} |
| 188 |
//We need to do Classifier::find stuff but without the tcl code |
| 189 |
//NsObject* node = dmux_->find(p); |
| 190 |
NsObject* node = ProtolibMKFind(p);//does same stuff as classifier::find but without tcl error |
| 191 |
if (node) { //packet should be accpeted at this node |
| 192 |
// fprintf(stdout,"sending copy up to dmux in protomanetKernel\n"); |
| 193 |
Packet *copy_p=p->copy(); //send copy up to agent so that we can attempt to forward packet. |
| 194 |
if(!copy_p) { |
| 195 |
fprintf(stderr,"ProtoManetKernel::recv error copying packet returning without recving packet\n"); |
| 196 |
return; |
| 197 |
} |
| 198 |
dmux_->recv(copy_p,NULL); |
| 199 |
} |
| 200 |
//attempt to send packet back out interface |
| 201 |
manetpointer->recv(p,this); //recv with non null is really a multicast forwarder and will check to see if it should forward the packetit uses bcastforward to actually send the packet back out. |
| 202 |
|
| 203 |
} else { //end only non router packets |
| 204 |
forward(p,(nsaddr_t)ih->daddr());//send packet to bcast address |
| 205 |
} |
| 206 |
} else { |
| 207 |
// fprintf(stdout,"dropping packet because ih->daddr() is broadcast but dport is equal to -1\n"); |
| 208 |
drop(p, DROP_RTR_NO_ROUTE); |
| 209 |
} |
| 210 |
} else { |
| 211 |
// fprintf(stdout,"%d is sending packet up to manetpointer\n",here_.addr_); |
| 212 |
manetpointer->recv(p,NULL); //manet will figure out where udp packet goes and then call our forward method |
| 213 |
} |
| 214 |
return; |
| 215 |
} |
| 216 |
NsObject* ProtolibMK::ProtolibMKFind(Packet* p){ |
| 217 |
NsObject* returnnode = NULL; |
| 218 |
int cl = ((Classifier*)dmux_)->classify(p); |
| 219 |
if ((returnnode = dmux_->slot(cl)) == 0) { |
| 220 |
// if (dmux_->default_target_) |
| 221 |
// return dmux_->default_target_; |
| 222 |
} |
| 223 |
return returnnode; |
| 224 |
} |
| 225 |
|
| 226 |
// will get called when link layer detects error |
| 227 |
void ProtolibMK::rt_failed(Packet *p) { |
| 228 |
drop(p,DROP_RTR_NO_ROUTE); // this line took me a week to find arggg! |
| 229 |
} |
| 230 |
|
| 231 |
void ProtolibMK::forward(Packet *p, nsaddr_t nexthop) { |
| 232 |
struct hdr_cmn *ch = HDR_CMN(p); |
| 233 |
struct hdr_ip *ih = HDR_IP(p); |
| 234 |
|
| 235 |
/* check ttl */ |
| 236 |
if (ih->ttl_ == 0){ |
| 237 |
// fprintf(stdout,"ProtolibMK dropping packet in forward because ttl == 0\n"); |
| 238 |
drop(p, DROP_RTR_TTL); |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
//check to see if the packet is meant for myself |
| 243 |
if (nexthop == here_.addr_) { |
| 244 |
ch->next_hop_ = nexthop; |
| 245 |
ch->addr_type() = NS_AF_INET; |
| 246 |
ch->direction() = hdr_cmn::UP; |
| 247 |
} else { |
| 248 |
// ch->num_forwards_++; |
| 249 |
ch->prev_hop_ = here_.addr_; |
| 250 |
ch->next_hop_ = nexthop; |
| 251 |
ch->addr_type() = NS_AF_INET; |
| 252 |
ch->direction() = hdr_cmn::DOWN; |
| 253 |
// fprintf(stdout,"ProtolibMK at time %f %d is attempting to foward packet to %d\n",Scheduler::instance().clock(),here_.addr_,nexthop); |
| 254 |
} |
| 255 |
// Send the packet |
| 256 |
|
| 257 |
Scheduler::instance().schedule(target_, p, 0.); |
| 258 |
} |
| 259 |
|
| 260 |
bool ProtolibMK::bcastforward(Packet *p) { //right now this just does what forward does but returns true will add smarts later |
| 261 |
struct hdr_cmn *ch = HDR_CMN(p); |
| 262 |
struct hdr_ip *ih = HDR_IP(p); |
| 263 |
double jitter; |
| 264 |
|
| 265 |
/* check ttl */ |
| 266 |
if (ih->ttl_ == 0){ |
| 267 |
//DMSG(0,"ProtolibMK dropping packet in bcastforward because ttl == 0\n"); |
| 268 |
drop(p, DROP_RTR_TTL); |
| 269 |
return false; |
| 270 |
} |
| 271 |
// if(here_.addr_==1){ |
| 272 |
//DMSG(2, "ProtolibMK forwarding bcast packet %d,%d at time %f\n",++numberofforwards,ih->fid_,Scheduler::instance().clock()); |
| 273 |
// } |
| 274 |
// ch->num_forwards_++; |
| 275 |
ch->prev_hop_ = here_.addr_; |
| 276 |
ch->next_hop_ = IP_BROADCAST; |
| 277 |
ch->addr_type() = NS_AF_INET; |
| 278 |
ch->direction() = hdr_cmn::DOWN; |
| 279 |
// fprintf(stdout,"ProtolibMK at time %f %d is attempting to bcastfoward packet to %d\n",Scheduler::instance().clock(),here_.addr_,nexthop); |
| 280 |
// Send the packet |
| 281 |
jitter = 0.00 * (double)rand() / (double)RAND_MAX; |
| 282 |
Scheduler::instance().schedule(target_, p, jitter); |
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|