📄 routemanager.java
字号:
rerr = new RERR(cfgInfo, curInfo, true, cfgInfo.ipAddressMulticastVal, (short) 1, false, (byte) unreachableIPList.size(), adrList, seqList); pktSender.sendMessage(rerr); } } } /*--------------------------------------------------------------------------------------*/ // Route deleter interface // ----------------------- // if thread = route deleter AND route status = invalid // delete route from internal table // active minder = null; public synchronized int checkDeleteRouteLifetime(InetAddress da, DeleteMinder delMinder) throws Exception { RouteEntry destRte; int lifetime; destRte = rtList.get(da); if(destRte == null) return 0; // if for some reason the route has got some status other than invalid // OR delMinder is not the current thread that is managing the route, // then stop the thread if((destRte.routeStatusFlag != RouteEntry.ROUTE_STATUS_FLAG_INVALID) || (destRte.activeMinder != delMinder)) { return 0; } lifetime = (int) (destRte.expiryTime - (new Date()).getTime()); if(lifetime <= 0) { // if lifetime expired, delete the route from list rtList.remove(da); return 0; } else return lifetime; } /*--------------------------------------------------------------------------------------*/ // RREQID minder interface // ----------------------- public synchronized RREQIDEntry getFirstRREQID() { return idList.getFirst(); } public synchronized RREQIDEntry removeRREQID(InetAddress adr, int id) { return idList.remove(adr, id); } /*--------------------------------------------------------------------------------------*/ // Hello minder interface // ---------------------- /** * Method called by the hello minder to send a hello message. Sends * it only if there are unexpired routes. * * @param HELLO hm - the hello message to send */ public synchronized boolean sendHello(HELLO hm) throws Exception { // check if route manager is active if(!rtMgrActive) { return false; } // send hello only if any active unexpired routes exist if(doUnexpiredRoutesExist()) { pktSender.sendMessage(hm); } return true; } /*--------------------------------------------------------------------------------------*/ /** * This method handles a RREQ messge received by the protocol handle. In * summary, either it will send a RREP or propogate the RREQ. Following * text describes the * * create or update a route to the prev hop increase lifetime * by ACTIVE_ROUTE_TIMEOUT (without a valid seq num, i.e. validDestSeqNumFlag = invalid * check RREQ prevously recvd (check from RREQ ID + orig ip list), if so drop * * in RREQ increment hop count * serach a route to the originator of RREQ, then add or update route * (use orig seq num in RREQ to update, see FORMULA * set validDestSeqNumFlag = valid) * * route lifetime to originator should be updated using FORMULA * * check if i am the destnation, then RREP generated * check if route to dest available AND active AND D flag is not set AND my dest seq num is valid * AND my dest seq num >= to dest seq num in RREQ, then RREP generated * if RREP generated * if destination * seq num FORMULA * hop count = 0, lifetime = MY_ROUTE_TIME, prefix = 0, R flag 0, * A flag from parametrs, rest from RREQ * unicast send, to sender of RREQ (prev hop) * * if not destination (intermediate) * seq num = what is route entry * hop count = what is route entry * lifetime = what is route entry (route time - curr time) * rest from RREQ * unicast send, to sender of RREQ (prev hop) * if G flag set in RREP (send RREP to destination) * hop count = from route to originator * dest ip adddress = originate of RREQ * dest seq num = originator seq num of RREQ * originator ip address = dest ip address * lifetime = (route time - curr time) to originator * unicast send to next hop to destination (from route) * * if no RREP generated then * check the TTL, should be > 1 else drop packet * reduce TTL by 1 * place highest of dest seq considering RREQ and route to dest in my route list * put RREQ ID + originator ip in list for RREQ minder with PATH_DISCOVERY_TIME * propogate RREQ * * @param RREQ rreq - the RREQ to process * @exception Exception - exceptions thrown when error */ public synchronized void processAODVMsgRREQ(RREQ rreq) throws Exception { RouteEntry prevHopRte, origRte, destRte; boolean mf, rf, af; InetAddress sendto; short ttl; byte ps, hc; InetAddress da, oa; int lt, dsn; boolean jf, gf, df, usnf; int ri, osn; boolean generateRREP; long minimalLifetime; RREP rrep; RREQ newRREQ; // log curInfo.log.write(Logging.INFO_LOGGING, "Route Manager - RREQ Received "+ rreq.toString()); // create or update a route to the prev hop increase lifetime // by ACTIVE_ROUTE_TIMEOUT (without a valid seq num, i.e. validDestSeqNumFlag = invalid prevHopRte = rtList.get(rreq.fromIPAddr); // if no route entry available if(prevHopRte == null) { // create entry prevHopRte = new RouteEntry(cfgInfo, curInfo); prevHopRte.destIPAddr = rreq.fromIPAddr; prevHopRte.destSeqNum = 0; prevHopRte.validDestSeqNumFlag = RouteEntry.DEST_SEQ_FLAG_INVALID; prevHopRte.routeStatusFlag = RouteEntry.ROUTE_STATUS_FLAG_VALID; prevHopRte.ifaceName = rreq.ifaceName ; prevHopRte.hopCount = 1; prevHopRte.nextHopIPAddr = rreq.fromIPAddr; prevHopRte.precursorList = new LinkedList(); prevHopRte.expiryTime = (new Date()).getTime() + cfgInfo.activeRouteTimeoutVal; prevHopRte.activeMinder = new RouteMinder(cfgInfo, curInfo, this, rreq.fromIPAddr, cfgInfo.activeRouteTimeoutVal); prevHopRte.activeMinder.start(); // if available and not expired } else if(prevHopRte.routeStatusFlag == RouteEntry.ROUTE_STATUS_FLAG_VALID) { // route is active, only extend lifetime prevHopRte.expiryTime = (new Date()).getTime() + cfgInfo.activeRouteTimeoutVal; // if available but expired } else { // set kernel route, start the minder and extend lifetime prevHopRte.hopCount = 1; prevHopRte.nextHopIPAddr = rreq.fromIPAddr; prevHopRte.expiryTime = (new Date()).getTime() + cfgInfo.activeRouteTimeoutVal; prevHopRte.routeStatusFlag = RouteEntry.ROUTE_STATUS_FLAG_VALID; prevHopRte.activeMinder = new RouteMinder(cfgInfo, curInfo, this, rreq.fromIPAddr, cfgInfo.activeRouteTimeoutVal); prevHopRte.activeMinder.start(); } rtList.update(rreq.fromIPAddr, prevHopRte); // check RREQ prevously recvd (check from RREQ ID + orig IP in list), if so drop if(idList.exist(rreq.origIPAddr, rreq.RREQID)) { // log curInfo.log.write(Logging.INFO_LOGGING, "Route Manager - RREQ disregarded as previously processed"); return; } // in RREQ, increment hop count rreq.hopCount++; // serach a route to the originator of RREQ, then add or update route // (use orig seq num in RREQ to update, see FORMULA // set validDestSeqNumFlag = valid) origRte = rtList.get(rreq.origIPAddr); // if route not available if(origRte == null) { origRte = new RouteEntry(cfgInfo, curInfo); origRte.destIPAddr = rreq.origIPAddr; origRte.destSeqNum = rreq.origSeqNum; origRte.validDestSeqNumFlag = RouteEntry.DEST_SEQ_FLAG_VALID; origRte.routeStatusFlag = RouteEntry.ROUTE_STATUS_FLAG_VALID; origRte.ifaceName = rreq.ifaceName ; origRte.hopCount = (byte) rreq.hopCount; origRte.nextHopIPAddr = rreq.fromIPAddr; origRte.precursorList = new LinkedList(); origRte.expiryTime = (new Date()).getTime(); // if available and not expired } else if(origRte.routeStatusFlag == RouteEntry.ROUTE_STATUS_FLAG_VALID) { // only lifetime need extended, done later // if available but expired } else { // create whole route origRte.destSeqNum = rreq.origSeqNum; origRte.validDestSeqNumFlag = RouteEntry.DEST_SEQ_FLAG_VALID; origRte.routeStatusFlag = RouteEntry.ROUTE_STATUS_FLAG_VALID; origRte.ifaceName = rreq.ifaceName ; origRte.hopCount = (byte) rreq.hopCount; origRte.nextHopIPAddr = rreq.fromIPAddr; origRte.precursorList = new LinkedList(); origRte.expiryTime = (new Date()).getTime(); } // update lifetime // route lifetime to originator should be updated using FORMULA // maximum of (ExistingLifetime, MinimalLifetime) // MinimalLifetime = (current time + 2*NET_TRAVERSAL_TIME - // 2*HopCount*NODE_TRAVERSAL_TIME). minimalLifetime = (2 * cfgInfo.netTraversalTimeVal) - (2 * origRte.hopCount * cfgInfo.nodeTraversalTimeVal) + (new Date()).getTime(); if(minimalLifetime > origRte.expiryTime) origRte.expiryTime = minimalLifetime; origRte.activeMinder = new RouteMinder(cfgInfo, curInfo, this, rreq.origIPAddr, (int) (origRte.expiryTime - (new Date()).getTime())); origRte.activeMinder.start(); rtList.update(rreq.origIPAddr, origRte); // check if i am the destnation, then RREP generated if(cfgInfo.ipAddressVal.equals(rreq.destIPAddr)) { generateRREP = true; destRte = null; // check if route to dest available AND active AND D flag is not set AND my dest seq num is valid // AND my dest seq num >= to dest seq num in RREQ, then RREP generated } else { destRte = rtList.get(rreq.destIPAddr); if(destRte != null && (destRte.routeStatusFlag == RouteEntry.ROUTE_STATUS_FLAG_VALID) && !rreq.destOnlyFlag && destRte.validDestSeqNumFlag == RouteEntry.DEST_SEQ_FLAG_VALID && (curInfo.destSeqCompare(destRte.destSeqNum, rreq.destSeqNum) == curInfo.GREATER || curInfo.destSeqCompare(destRte.destSeqNum, rreq.destSeqNum) == curInfo.EQUAL)) { generateRREP = true; // if none of above, propogate the RREQ } else { generateRREP = false; destRte = null; } } // if RREP generated if(generateRREP) { // if i am destination if(cfgInfo.ipAddressVal.equals(rreq.destIPAddr)) { // seq num see FORMULA // hop count = 0, lifetime = MY_ROUTE_TIME, prefix = 0, R flag 0, // A flag from parametrs, rest from RREQ // unicast send, to sender of RREQ (prev hop) mf = false; sendto = prevHopRte.destIPAddr; ttl = 255; rf = false; af = cfgInfo.RREPAckRequiredVal; ps = 0; hc = 0; da = cfgInfo.ipAddressVal; //rreq.destSeqNum++; if(curInfo.destSeqCompare(rreq.destSeqNum, curInfo.lastSeqNum) == curInfo.GREATER) { curInfo.lastSeqNum = rreq.destSeqNum; } else if(curInfo.destSeqCompare(rreq.destSeqNum, curInfo.lastSeqNum) == curInfo.EQUAL) { curInfo.incrementOwnSeqNum(); } else { // use existing value } dsn = curInfo.lastSeqNum; oa = origRte.destIPAddr; lt = cfgInfo.myRouteTimeoutVal; rrep = new RREP(cfgInfo, curInfo, mf, sendto, ttl, rf, af, ps, hc, da, dsn, oa, lt); pktSender.sendMessage(rrep); // if not destination (intermediate node) } else { // seq num = what is route entry // hop count = what is route entry // lifetime = what is route entry (route time - curr time) // rest from RREQ // unicast send, to sender of RREQ (prev hop) mf = false; sendto = prevHopRte.destIPAddr; ttl = 255; rf = false; af = cfgInfo.RREPAckRequiredVal; ps = 0; hc = (byte) destRte.hopCount; da = destRte.destIPAddr; dsn = destRte.destSeqNum; oa = rreq.origIPAddr; lt = (int) (destRte.expiryTime - (new Date()).getTime()); rrep = new RREP(cfgInfo, curInfo, mf, sendto, ttl, rf, af, ps, hc, da, dsn, oa, lt); pktSender.sendMessage(rrep); // if G flag set in RREQ (send RREP to destination) if(rreq.gratRREPFlag) { // hop count = from route to originator // dest ip adddress = originate of RREQ // dest seq num = originator seq num of RREQ // originator ip address = dest ip address // lifetime = (route time - curr time) to originator // unicast send to next hop to destination (from route) mf = false; sendto = destRte.nextHopIPAddr; ttl = 225; rf = false; af = cfgInfo.RREPAckRequiredVal; ps = 0; hc = (byte) origRte.hopCount; da = origRte.destIPAddr; dsn = origRte.destSeqNum; oa = destRte.destIPAddr; lt = (int) (origRte.expiryTime - (new Date()).getTime()); rrep = new RREP(cfgInfo, curInfo, mf, sendto, ttl, rf, af, ps, hc, da, dsn, oa, lt); pktSender.sendMessage(rrep); } } // if no RREP generated, then propogate RREQ } else { // check the TTL, should be > 1 else drop packet if(rreq.ttlValue > 1) { // reduce TTL by 1 // place highest of dest seq considering RREQ and route to dest in my route list // put RREQ ID + originator ip in list for RREQ minder with PATH_DISCOVERY_TIME // propogate RREQ rreq.ttlValue--; if(destRte != null) { if(!rreq.unknownSeqNumFlag && curInfo.destSeqCompare(rreq.destSeqNum, destRte.destSeqNum) == CurrentInfo.GREATER) { destRte.destSeqNum = rreq.destSeqNum; rtList.update(rreq.destIPAddr, destRte); rreq.unknownSeqNumFlag = false; } else { rreq.destSeqNum = destRte.destSeqNum; } } sendto = cfgInfo.ipAddressMulticastVal; ttl = rreq.ttlValue; jf = rreq.joinFlag; rf = rreq.repairFlag; gf = rreq.gratRREPFlag; df = rreq.destOnlyFlag; usnf = rreq.unknownSeqNumFlag; hc = rreq.hopCount; ri = rreq.RREQID; da = rreq.destIPAddr; dsn = rreq.destSeqNum; oa = rreq.origIPAddr; osn = rreq.origSeqNum; newRREQ = new RREQ(cfgInfo, curInfo, true, sendto, ttl, jf, rf, gf, df, usnf, hc, ri, da, dsn, oa, osn); idList.add(oa, ri); pktSender.sendMessage(newRREQ); } } } /** * This method is responsible for handling RREP messages recived by the * node. The following is the procedure, * * find route to prev hop (who sent RREP, i.e dest=prev hop) * if not, create route without a valid seq num * increment hop count in RREP * * find route to dest * if route found, compare dest seq num * if seq num invalid in route * OR (dest seq in RREP > what is in route (2s comp) AND dest seq valid) * OR (seq == seq AND route is inactive route) * OR (seq num == seq num AND active route AND hop count in RREP is < hop count in route) * update route * do as (100) * if route not found * (100) create route - route flag = active, dest seq flag = valid, * next hop = src ip in RREP, hop count = hop count in RREP * expiry time = current time + lifetime in RREP * dest seq num = dest seq num of RREP * dest ip = dest ip in RREP * iface = iface from which RREP recvd * * * if i am not originator of RREP * find route to originator * update lifetime of route to max of (existing lifetime, currtime + ACTIVE_ROUTE_TIMEOUT) * update precursor list - using from the src ip from whom the RREP was * recvd (i.e. next hop) * * find route to dest * update precuror list to dest (i.e. next hop to dest) - put ip of next hop to which RREP is * forwarded (not ncessarily originator) * send RREP to next hop to originator * * if i am originator * unicast RREP-ACK to dest * * @param RREP rrep - the RREP recived * @exception Exception - thrown for any error occured
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -