#include "icrp.h" #include "cache.h" extern class CACHE queryCache; extern class DDCA machine; extern class ROUTER ra; class ICRP icrp; int ICRP::initiateQuery(unsigned int ipAddr) { //read from file char buff[1000]; //buffer to put the query in struct in_addr ip_addr; //to read the gateways struct ddca_header *dh = (struct ddca_header *)(buff + 20);//well you know dh->proto = PKT_ROUTING; struct query_body *qbody = (struct query_body *)(buff + sizeof(struct ddca_header) + 20); //body, you know qbody->type = RREQ; qbody->srcNID = machine.getNid(); //TODO you have to get your CID from the proc maybe qbody->srcCID = get_cid (); //qbody->destNID = ? //qbody->destCID = ? qbody->targNID = ipAddr; qbody->origNID = machine.getNid (); //call system call to get the cid //put a sequence number this->seqNum++; qbody->seqNo = this->seqNum; rewind (gwFile); while(fscanf(gwFile, "%u", &ip_addr.s_addr) != EOF) { //send requests to all gateways //then they will all broadcast it to the other clusters printf("Sending to GWs: %s\n", inet_ntoa(ip_addr)); //if I am a gateway, I will need to broadcast the query if(ip_addr.s_addr == machine.getNid()) { machine.broadcastMsg(buff, sizeof(struct ddca_header) + sizeof (struct query_body)); } machine.unicastMsg(buff, sizeof(struct ddca_header) + sizeof (struct query_body), ip_addr.s_addr); } //reset the file pointer //rewind(gwFile); //for logging struct in_addr ipadr; logFile <<"************ Begin initiate Query ******************" << endl; ipadr.s_addr = qbody->srcNID; logFile <<"qbody->srcNID: " << inet_ntoa(ipadr) <srcCID; logFile <<"qbody->srcCID: " << inet_ntoa(ipadr) <destNID; logFile <<"qbody->dstNID: " << inet_ntoa(ipadr) <destCID; logFile <<"qbody->dstNID: " << inet_ntoa(ipadr) <targNID; logFile <<"qbody->trgNID: " << inet_ntoa(ipadr) <origNID; logFile <<"qbody->orgNID: " << inet_ntoa(ipadr) <type) { case RREQ: //because of broadcasts, we might here our own query, discard it if(qb->origNID == machine.getNid()) { return -1; } logFile << "Received a Query" << endl; //have we already got this query? //if so discard query //for logging all this logFile <<"************ Begin Processing Query ******************" << endl; ipadr.s_addr = qb->srcNID; logFile <<"qb->srcNID: " << inet_ntoa(ipadr) <srcCID; logFile <<"qb->srcCID: " << inet_ntoa(ipadr) <destNID; logFile <<"qb->dstNID" << inet_ntoa(ipadr) <destCID; logFile <<"qb->dstNID" << inet_ntoa(ipadr) <targNID; logFile <<"qb->trgNID: " << inet_ntoa(ipadr) <origNID; logFile <<"qb->orgNID: " << inet_ntoa(ipadr) <targNID == machine.getNid()) { //TODO this dest is reachable through whosoever we rcvd the pkt from ra.addRoute(qb->origNID, qb->srcNID); add_routable(qb->origNID); //call replyQuery this->replyQuery(dh, qb); return 0; } //else notify the parent, //wait for parent's response as to whether we are allowed //to forward this //if allowed forwardQuery this->forwardQuery(dh, qb); break; case RREP: //if we are the originator of a RREP, we must not respond to it if(qb->origNID == machine.getNid()) { return -1; } logFile << "Received a Query Reply" << endl;; //for logging all this logFile <<"************ Begin Processing Query ******************" << endl; ipadr.s_addr = qb->srcNID; logFile <<"qb->srcNID: " << inet_ntoa(ipadr) <srcCID; logFile <<"qb->srcCID: " << inet_ntoa(ipadr) <destNID; logFile <<"qb->dstNID" << inet_ntoa(ipadr) <destCID; logFile <<"qb->dstNID" << inet_ntoa(ipadr) <targNID; logFile <<"qb->trgNID: " << inet_ntoa(ipadr) <origNID; logFile <<"qb->orgNID: " << inet_ntoa(ipadr) <targNID == machine.getNid()) { //07.23.02 //temporary hack, to ward off temporary connection with the //orphan and a not reachable node in A-B-C configuration if(qb->origNID == qb->srcNID) { return -1; } //insert the routing entry in our table ra.addRoute(qb->origNID, qb->srcNID); add_routable(qb->origNID); return 0; } this->forwardReply(dh, qb); return 0; default: printf("Error: Received a malformed Query message\n"); return -1; } return 0; } int ICRP::forwardQuery(struct ddca_header *dh_1, struct query_body *qb_1) { //should we do the above (?) //if it is in our routables //unicast directly //else //forward to all our gateways //keep this in our cache, that we saw this query //broadcast it again char buff[1000]; //buffer to put the query in struct in_addr ip_addr; //to read the gateways struct ddca_header *dh = (struct ddca_header *)(buff + 20);//well you know dh->proto = PKT_ROUTING; struct query_body *qbody = (struct query_body *)(buff + sizeof(struct ddca_header) + 20); //body, you know qbody->type = RREQ; qbody->srcNID = machine.getNid(); //TODO you have to get your CID from the proc maybe qbody->srcCID = get_cid (); //qbody->destNID = ? //qbody->destCID = ? qbody->targNID = qb_1->targNID; qbody->origNID = qb_1->origNID; //call system call to get the cid qbody->seqNo = qb_1->seqNo; rewind (gwFile); while(fscanf(gwFile, "%u", &ip_addr.s_addr) != EOF) { //send requests to all gateways //then they will all broadcast it to the other clusters printf("Sending to GWs: %s\n", inet_ntoa(ip_addr)); //if I am a gateway, I will need to broadcast the query if(ip_addr.s_addr == machine.getNid()) { machine.broadcastMsg(buff, sizeof(struct ddca_header) + sizeof (struct query_body)); } machine.unicastMsg(buff, sizeof(struct ddca_header) + sizeof (struct query_body), ip_addr.s_addr); } //ok now that the query has been forwarded, put it in our query cache C_ENT tmp; tmp.rcvd_from = qb_1->srcNID; //whosoever forwarded the query is our link tmp.target = qbody->targNID; tmp.source = qbody->origNID; queryCache.insertEntry(tmp); //for logging all this struct in_addr ipadr; logFile <<"************ Begin Forward Query ******************" << endl; ipadr.s_addr = qbody->srcNID; logFile <<"qb->srcNID" << inet_ntoa(ipadr) <srcCID; logFile <<"qb->srcCID" << inet_ntoa(ipadr) <destNID; logFile <<"qb->dstNID" << inet_ntoa(ipadr) <destCID; logFile <<"qb->dstNID" << inet_ntoa(ipadr) <targNID; logFile <<"qb->trgNID" << inet_ntoa(ipadr) <origNID; logFile <<"qb->orgNID" << inet_ntoa(ipadr) <proto = PKT_ROUTING; struct query_body *qbody = (struct query_body *)(buff + sizeof(struct ddca_header) + 20); //body, you know qbody->type = RREP; qbody->srcNID = machine.getNid(); //TODO you have to get your CID from the proc maybe qbody->srcCID = get_cid (); //qbody->destNID = ? //qbody->destCID = ? qbody->targNID = qb_1->origNID; //get the originator from the packet qbody->origNID = machine.getNid(); //put a sequence number TODO DO we even need a seqNum in reply this->seqNum++; qbody->seqNo = this->seqNum; machine.unicastMsg(buff, sizeof(struct ddca_header) + sizeof (struct query_body), qb_1->srcNID); //for logging all this struct in_addr ipadr; logFile <<"************ Begin Reply Query ******************" << endl; ipadr.s_addr = qbody->srcNID; logFile <<"qb->srcNID" << inet_ntoa(ipadr) <srcCID; logFile <<"qb->srcCID" << inet_ntoa(ipadr) <destNID; logFile <<"qb->dstNID" << inet_ntoa(ipadr) <destCID; logFile <<"qb->dstNID" << inet_ntoa(ipadr) <targNID; logFile <<"qb->trgNID" << inet_ntoa(ipadr) <origNID; logFile <<"qb->orgNID" << inet_ntoa(ipadr) <proto = PKT_ROUTING; struct query_body *qbody = (struct query_body *)(buff + sizeof(struct ddca_header) + 20); //body, you know qbody->type = RREP; qbody->srcNID = machine.getNid(); //TODO you have to get your CID from the proc maybe qbody->srcCID = get_cid (); //qbody->destNID = ? //qbody->destCID = ? qbody->targNID = qb_1->targNID; //get the originator from the packet qbody->origNID = qb_1->origNID; qbody->seqNo = qb_1->seqNo; //copy the sequence number // change broadcast to unicast with caching //find who we had gotten query for this and send it to them //note that the src and dest have been reversed C_ENT tmp_rec = queryCache.getEntry(queryCache.searchEntry(qbody->targNID, qbody->origNID)); machine.unicastMsg(buff, sizeof(struct ddca_header) + sizeof (struct query_body), tmp_rec.rcvd_from); //ra.addRoute(); struct in_addr ipadr; ipadr.s_addr = tmp_rec.rcvd_from; logFile <<"FORWARDED REPLY: for, from, to" << qbody->targNID << " " << qbody->origNID << " " << inet_ntoa(ipadr) << endl; /* machine.broadcastMsg(buff, sizeof(struct ddca_header) + sizeof (struct query_body)); */ //for logging all this logFile <<"************ Begin FFD Reply Query ******************" << endl; ipadr.s_addr = qbody->srcNID; logFile <<"qb->srcNID" << inet_ntoa(ipadr) <srcCID; logFile <<"qb->srcCID" << inet_ntoa(ipadr) <destNID; logFile <<"qb->dstNID" << inet_ntoa(ipadr) <destCID; logFile <<"qb->dstNID" << inet_ntoa(ipadr) <targNID; logFile <<"qb->trgNID" << inet_ntoa(ipadr) <origNID; logFile <<"qb->orgNID" << inet_ntoa(ipadr) <