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

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * Encapsulator.cc
  3.  * Copyright (C) 2000 by the University of Southern California
  4.  * $Id: Encapsulator.cc,v 1.6 2005/08/25 18:58:02 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. // $Header: /cvsroot/nsnam/ns-2/common/Encapsulator.cc,v 1.6 2005/08/25 18:58:02 johnh Exp $
  47. #include "packet.h"
  48. #include "ip.h"
  49. #include "Encapsulator.h"
  50. #include "encap.h"
  51. static class EncapsulatorClass : public TclClass {
  52. public:
  53. EncapsulatorClass() : TclClass("Agent/Encapsulator") {}
  54. TclObject* create(int, const char*const*) {
  55. return (new Encapsulator());
  56. }
  57. } class_encapsulator;
  58. Encapsulator::Encapsulator() : 
  59. Agent(PT_ENCAPSULATED), 
  60. d_target_(0)
  61. {
  62. bind("status_", &status_);
  63. bind("overhead_", &overhead_);
  64. }
  65. int Encapsulator::command(int argc, const char*const* argv)
  66. {
  67. Tcl& tcl = Tcl::instance();
  68. if (argc == 2) {
  69. if (strcmp(argv[1], "decap-target") == 0) {
  70. if (d_target_ != 0) tcl.result(d_target_->name());
  71. return (TCL_OK);
  72. }
  73. }
  74. else if (argc == 3) {
  75. if (strcmp(argv[1], "decap-target") == 0) {
  76. d_target_ = (NsObject*)TclObject::lookup(argv[2]);
  77. // even if it's zero, it's OK, we'll just not send to such
  78. // a target then.
  79. return (TCL_OK);
  80. }
  81. }
  82. return (Agent::command(argc, argv));
  83. }
  84. void Encapsulator::recv(Packet* p, Handler* h)
  85. {
  86. if (d_target_) {
  87. Packet *copy_p= p->copy();
  88. d_target_->recv(copy_p, h);
  89. }
  90. if (status_) {
  91. Packet* ep= allocpkt(); //sizeof(Packet*));
  92. hdr_encap* eh= hdr_encap::access(ep);
  93. eh->encap(p);
  94. //Packet** pp= (Packet**) encap_p->accessdata();
  95. //*pp= p;
  96. hdr_cmn* ch_e= hdr_cmn::access(ep);
  97. hdr_cmn* ch_p= hdr_cmn::access(p);
  98.   
  99. ch_e->ptype()= PT_ENCAPSULATED;
  100.   ch_e->size()= ch_p->size() + overhead_;
  101. ch_e->timestamp()= ch_p->timestamp();
  102. send(ep, h);
  103. }
  104. else send(p, h); //forward the packet as it is
  105. }