sr_forwarder.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:6k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * sr_forwarder.cc
  3.  * Copyright (C) 2000 by the University of Southern California
  4.  * $Id: sr_forwarder.cc,v 1.2 2005/08/25 18:58:05 johnh Exp $
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License,
  8.  * version 2, as published by the Free Software Foundation.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18.  *
  19.  *
  20.  * The copyright of this module includes the following
  21.  * linking-with-specific-other-licenses addition:
  22.  *
  23.  * In addition, as a special exception, the copyright holders of
  24.  * this module give you permission to combine (via static or
  25.  * dynamic linking) this module with free software programs or
  26.  * libraries that are released under the GNU LGPL and with code
  27.  * included in the standard release of ns-2 under the Apache 2.0
  28.  * license or under otherwise-compatible licenses with advertising
  29.  * requirements (or modified versions of such code, with unchanged
  30.  * license).  You may copy and distribute such a system following the
  31.  * terms of the GNU GPL for this module and the licenses of the
  32.  * other code concerned, provided that you include the source code of
  33.  * that other code when and as the GNU GPL requires distribution of
  34.  * source code.
  35.  *
  36.  * Note that people who make modified versions of this module
  37.  * are not obligated to grant this special exception for their
  38.  * modified versions; it is their choice whether to do so.  The GNU
  39.  * General Public License gives permission to release a modified
  40.  * version without this exception; this exception also makes it
  41.  * possible to release a modified version which carries forward this
  42.  * exception.
  43.  *
  44.  */
  45. //
  46. // Other copyrights might apply to parts of this software and are so
  47. // noted when applicable.
  48. //
  49. // Ported from CMU/Monarch's code, appropriate copyright applies.  
  50. /* -*- c++ -*-
  51.    
  52.    sr_forwarder.cc
  53.    source route forwarder
  54.    */
  55. #include <object.h>
  56. #include <packet.h>
  57. #include <ip.h>
  58. #include <mac.h>
  59. #include "hdr_sr.h"
  60. #include "dsr_proto.h"
  61. #include "routecache.h"
  62. #include "requesttable.h"
  63. #include "sr_forwarder.h"
  64. /*===========================================================================
  65.   static classes for OTcl
  66. ---------------------------------------------------------------------------*/
  67. static class SRForwarderClass : public TclClass {
  68. public:
  69.   SRForwarderClass() : TclClass("SRForwarder") {}
  70.   TclObject* create(int, const char*const*) {
  71.     return (new SRForwarder);
  72.   }
  73. } class_srforwarder;
  74. /*===========================================================================
  75.   Member functions
  76. ---------------------------------------------------------------------------*/
  77. SRForwarder::SRForwarder()
  78. {
  79.   target_ = 0;
  80.   route_agent_ = 0;
  81.   tracetarget = 0;
  82.   
  83.   bind("off_sr_", &off_sr_);
  84.   bind("off_ll_", &off_ll_);
  85.   bind("off_mac_", &off_mac_);
  86.   bind("off_ip_", &off_ip_);
  87. }
  88. int
  89. SRForwarder::command(int argc, const char*const* argv)
  90. {
  91.   Tcl& tcl = Tcl::instance();
  92.   if (argc == 2) 
  93.     {
  94.       if (strcmp(argv[1], "target") == 0) {
  95. if (target_ != 0)
  96.   tcl.result(target_->name());
  97. return (TCL_OK);
  98.       }
  99.       if (strcmp(argv[1], "routeagent") == 0) {
  100. if (route_agent_ != 0)
  101.   tcl.result(route_agent_->name());
  102. return (TCL_OK);
  103.       }
  104.     }
  105.   else if(argc == 3) 
  106.     {
  107.       TclObject *obj;
  108.       
  109.       if( (obj = TclObject::lookup(argv[2])) == 0) {
  110. fprintf(stderr, "SRForwarder: %s lookup of %s failedn", argv[1],
  111. argv[2]);
  112. return TCL_ERROR;
  113.       }
  114.       if (strcasecmp(argv[1], "tracetarget") == 0) 
  115. {
  116.   tracetarget = (Trace*) obj;
  117.   return TCL_OK;
  118. }
  119.       else if (strcasecmp(argv[1], "routecache") == 0) 
  120. {
  121.   routecache_ = (RouteCache*) obj;
  122.   return TCL_OK;
  123. }
  124.       else if (strcasecmp(argv[1], "target") == 0) 
  125. {
  126.   target_ = (NsObject*) obj;
  127.   return TCL_OK;
  128. }
  129.       else if (strcasecmp(argv[1], "routeagent") == 0) 
  130. {
  131.   route_agent_ = (DSRProto*) obj;
  132.   return TCL_OK;
  133. }
  134.     }
  135.   
  136.   return NsObject::command(argc, argv);
  137. }
  138. void
  139. SRForwarder::handlePktWithoutSR(Packet *p)
  140. {
  141.   hdr_sr *srh = hdr_sr::access(p);
  142.   hdr_ip *iph = hdr_ip::access(p);
  143.   //hdr_cmn *cmh =  hdr_cmn::access(p);
  144.   Path path;
  145.   /* check route cache for source route for this packet */
  146.   //if (routecache_->findRoute(ID(iph->daddr()>>8,::IP),path))
  147.   if (routecache_->findRoute(ID(iph->daddr(),::IP),path,0))
  148.     {
  149.       srh->init();
  150.       path.fillSR(srh);
  151.       sprintf(tracetarget->pt_->buffer(),
  152.       "$hit %g originating 0x%x -> 0x%x %s", 
  153.       Scheduler::instance().clock(),
  154.       iph->saddr(), iph->daddr(), srh->dump());
  155.       tracetarget->pt_->dump();
  156.       this->recv(p,0); // send back to forwarder to send this out
  157.       return;
  158.     }
  159.   
  160.   /* if not found, give packet to routing protocol */
  161.   sprintf(tracetarget->pt_->buffer(),
  162.   "dfu %g srforwarder: %s no route to 0x%x", 
  163.   Scheduler::instance().clock(),
  164.   routecache_->net_id.dump(), iph->daddr());
  165.   tracetarget->pt_->dump();
  166.   route_agent_->noRouteForPacket(p);
  167. }
  168. void
  169. SRForwarder::recv(Packet* p, Handler* callback)
  170. {
  171.   hdr_sr *srh =  (hdr_sr*)p->access(off_sr_);
  172.   assert(tracetarget != 0);
  173.   if (!srh->valid())
  174.     {
  175.       handlePktWithoutSR(p);
  176.       return;
  177.     }
  178.   /* forward as per source route */
  179.   hdr_ip *iph = hdr_ip::access(p);
  180.   hdr_cmn *cmnh =  hdr_cmn::access(p);
  181.   
  182.   if (srh->route_request())
  183.     { /* just broadcast */
  184.       cmnh->next_hop() = MAC_BROADCAST;
  185.       cmnh->addr_type() = NS_AF_ILINK;
  186.       target_->recv(p, (Handler*)0);
  187.       return;   
  188.     }
  189.   
  190.   /* it must be a normal data packet with a source route */
  191.   if (tracetarget)
  192.     {
  193.       sprintf(tracetarget->pt_->buffer(),
  194.       "F %g 0x%x -> 0x%x via 0x%x %s", 
  195.       Scheduler::instance().clock(),
  196.       iph->saddr(), iph->daddr(), srh->get_next_addr(), srh->dump());
  197.       tracetarget->pt_->dump();
  198.     }
  199.   
  200.   cmnh->next_hop() = srh->get_next_addr();
  201.   cmnh->addr_type() = NS_AF_INET;
  202.   srh->cur_addr() = srh->cur_addr() + 1;
  203.   target_->recv(p, (Handler*)0);
  204.   return;
  205. }