protoname.cc
上传用户:juan65231
上传日期:2014-02-17
资源大小:194k
文件大小:8k
- #include "protoname.h"
- #include "protoname_pkt.h"
- #include <random.h>
- #include <cmu-trace.h>
- #include <iostream>
- 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("accesible_var_", &accesible_var_);
- ra_addr_ = id;
- node_ = (MobileNode*)Node::get_node_by_address(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;
- }
// to generate the pairwise keys , nodes only broadcast the packet
else if (strcasecmp(argv[1], "gen-pair-keys") == 0) {
- send_protoname_pairkey();
- return TCL_OK;
- }
else if (strcasecmp(argv[1], "initilize") == 0) {
- init();
- 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;
- }
- else if (strcmp(argv[1], "send-pkt-to") == 0) {
- send_protoname_pkt(nsaddr_t(atoi(argv[2])));
- return TCL_OK;
}
else if (strcmp(argv[1], "send-to") == 0) {
- send_protoname_pkt(nsaddr_t(atoi(argv[2])));
- 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);
- }
- }
int a(int m,int e) {
int c=1; //c是结果
- int arr[20]; //存放指数转换为二进制的中间值
- int i=0;
- int temp;
- while(e!=0)
- {
- temp=e%2;
- arr[i++]=temp;
- e=e/2;
- }
- i=i-1;
- while(i>=0)
- {
- c=(c*c)%71;
- if(arr[i--]==1)
- c=(c*(m%71))%71;
- }
return c;
}
- 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);
//ph->pkt_len() = 1;
- /* ... processing of protoname packet ... */
- //forward_data(p);
if(ph->pkt_app() == 0) {
// to add sth here
int ka,kb;
ka = key_element;
kb = ph->pkt_ran();
pkt_pairkeys_[index] = a(kb,ka);
pkt_neighbours_[index] = ph->pkt_src();
printf("host is %d his neighbour is %d their pairwise key is %dn",ra_addr(),pkt_neighbours_[index],pkt_pairkeys_[index]);
index++;
}
- // Release resources
- // Packet::free(p);
- }
-
- void
- Protoname::send_protoname_pkt(nsaddr_t dest) {
- 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_dst() = dest;
- ph->pkt_len() = 32;
- ph->pkt_seq_num() = seq_num_++;
ph->pkt_app() = 1;
u_int16_t t1;
//srand(time(0));
t1 = rand()%71;
t1 = a(7,t1);
- ph->pkt_ran() = t1;
- 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::send_protoname_pairkey() {
- 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_dst() = dest;
- ph->pkt_len() = 32;
- ph->pkt_seq_num() = seq_num_++;
ph->pkt_app() = 0; // 0 stand for the application of generating the pairwise keys
/*
u_int16_t t1;
//srand(time(0));
t1 = rand()%71;
//key_element = t1;
//ph->pkt_len() = key_element;
t1 = a(7,t1);
- ph->pkt_ran() = t1;
key_element = t1; // the key_element store the local element
*/
ph->pkt_ran() = a(7,key_element);
ph->pkt_temp() = key_element;
- 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::init() {
index = 0;
for (int i = 0;i<10;i++)
{
pkt_pairkeys_[i] = 0;
pkt_neighbours_[i] = NULL;
}
//printf("afeirgoo");
u_int16_t t1;
//srand(time(0));
t1 = rand()%71;
//t1 = a(7,t1);
- //ph->pkt_ran() = t1;
key_element = t1; // the key_element store the local element
}
- //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);
- struct hdr_protoname_pkt* ph = HDR_PROTONAME_PKT(p);
-
- if (ch->direction() == hdr_cmn::UP &&
- ((u_int32_t)ih->daddr() == IP_BROADCAST && ph->pkt_dst() == ra_addr())) {
- // dmux_->recv(p, 0);
//Packet::free(p);
- drop(p, DROP_RTR_ROUTE_LOOP);
- return;
- }
- else {
- ch->direction() = hdr_cmn::DOWN;
- ch->addr_type() = NS_AF_INET;
- ch->next_hop() = IP_BROADCAST;
-
- Scheduler::instance().schedule(target_, p, 0.0);
- }
- }