#include #include #include "icrp.h" extern IPQ_LIST global_ipqlist; IPQ_HANDLER ipqhnd; extern class DDCA machine; extern class ROUTER ra; extern class ICRP icrp; // starting code for timers in the IPQ_HANDLER class void update_action (int signo, siginfo_t *info, void *context) { cerr << " update_action invoked" << endl; // call the function to update the ipq entries } void IPQ_HANDLER::initTimer () { updateEvent.sigev_notify = SIGEV_SIGNAL; updateEvent.sigev_signo = UPDATE_SIGNAL; updateEvent.sigev_value.sival_ptr = &updateTimer; updateSpec.it_value.tv_sec = UPDATE_EXPIRE_SEC; updateSpec.it_value.tv_nsec = UPDATE_EXPIRE_NSEC; updateSpec.it_interval.tv_sec = UPDATE_EXPIRE_SEC; updateSpec.it_interval.tv_nsec = UPDATE_EXPIRE_NSEC; sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = update_action; sigemptyset (&sa.sa_mask); if (sigaction (UPDATE_SIGNAL, &sa, NULL)) { perror ("sigaction failed in ipq"); exit (1); } if (timer_create (CLOCK_REALTIME, &updateEvent, &updateTimer)) { perror ("updateTimer creation in ipq"); exit (1); } sigaddset (&set, UPDATE_SIGNAL); } void IPQ_HANDLER::setTimer () { if (timer_settime (updateTimer, 0, &updateSpec, NULL)) { perror ("settimer for updateTimer failed"); exit (1); } } void IPQ_HANDLER::cancelTimer () { struct itimerspec dummy; //used to zero out the timer dummy.it_value.tv_sec = 0L; dummy.it_value.tv_nsec = 0L; dummy.it_interval.tv_sec = 0L; dummy.it_interval.tv_nsec = 0L; if (timer_settime (updateTimer, 0, &dummy, NULL)) { perror ("cancel for updateTimer failed"); exit (1); } } void IPQ_HANDLER::deleteTimer () { if (timer_delete (updateTimer)) { perror ("timer_delete of updateTimer failed"); exit (1); } } int IPQ_HANDLER::initHandler() { /* Init first (flags are not implemented; yet) */ if ((qh = ipq_create_handle(0, PF_INET)) == NULL) { /* Run away... */ ipq_perror("create_handle()"); exit(1); } /* We would like to receive not only metadata... */ if (ipq_set_mode(qh, IPQ_COPY_PACKET, MAX_PACKET_LEN) < 0) { ipq_perror("set_mode()"); ipq_destroy_handle(qh); exit(1); /* No point going further*/ } cout << "passed init" << endl; return 1; } void IPQ_HANDLER::readPacket() { len = ipq_read(qh, buff, sizeof buff, 0); fprintf(stderr, "Length read %d\n", len); if (len == 0) { fprintf(stderr, "read(): Zero length packet; ingnoring\n"); return; } if (len < 0) { ipq_perror("read()"); return; } qpkt = ipq_get_packet(this->buff); iph = (struct iphdr *)qpkt->payload; /* If packet type from kernel is not known to us, * just ignore it (but notify user) */ if ((type = ipq_message_type(buff)) != IPQM_PACKET) { fprintf(stderr, "read(): Unknown message type %d\n", type); //continue; } if ((type = ipq_message_type(buff)) == NLMSG_ERROR) { fprintf(stderr, "read(): had an error returned NLMSG_ERROR%u\n", type); } #ifdef AT_MESSAGES struct in_addr i; i.s_addr = iph->daddr; printf ("readPacket(): rcvd for %s\n", inet_ntoa(i)); #endif this->handlePacket(); } int IPQ_HANDLER::handlePacket() { //if IP=192.168.1.1 //message for us, a route has been found, accept this list TODO if(iph->daddr == inet_addr("192.168.1.1")) { //printf("Got a message for 192.168.1.1\n"); struct ddca_header *dh = (struct ddca_header *)(buff + sizeof( ipq_packet_msg_t)); global_ipqlist.nf_acceptList (dh->nid); return 0; } //well this packet came to us in the first place because we did not have //a path for this right(?). Maybe, maybe not... int ind = -1; if ((ind = global_ipqlist.isDestSeen (iph->daddr)) >= 0) { if (global_ipqlist.getTimerValue (ind) >= MIN_GRANULARITY) { global_ipqlist.insertInList (iph->daddr, qpkt->packet_id); } else { global_ipqlist.nf_dropList (iph->daddr); } } else { // send out a query and queue the packet icrp.initiateQuery(iph->daddr); global_ipqlist.insertInList (iph->daddr, qpkt->packet_id); } /* // this destination seen already & timer not expired if (0) { //queue it back -- insert it into list using } else if(0)//destination seen and timer expired { //reject it -- drop from list using nf_dropList } else //destination not seen { //send out a query & queue the packet } */ /* //TODO remove this DEFAULT ACCEPT POLICY if (ipq_set_verdict(qh, qpkt->packet_id, NF_ACCEPT, 0, NULL) < 0) { ipq_perror("set_verdict(): Oops!"); exit(1); //maybe we should not do this - sanjeev } */ return 0; } void startICRP() { ipqhnd.initHandler(); ipqhnd.initTimer (); // initializing the timer ipqhnd.setTimer(); // setting the timer while(1) { cout << "hihi--entered the forked guy" << endl; ipqhnd.readPacket(); } } /* //this is just test that broadcastMsgRT works void startICRP() { char buff[1000]; struct ddca_header *dh = (struct ddca_header *)(buff + 20); dh->proto = PKT_ROUTING; struct query_body *qbody = (struct query_body *)(buff + sizeof(struct ddca_header) + 20); while(1) { machine.unicastMsg(buff, sizeof(struct ddca_header) + sizeof (struct query_body), inet_addr("136.142.79.154")); sleep(1); } } */ /*int main() { startICRP(); return 0; } */