mip.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) Sun Microsystems, Inc. 1998 All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  *
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  *
00016  * 3. All advertising materials mentioning features or use of this software
00017  *    must display the following acknowledgement:
00018  *      This product includes software developed by Sun Microsystems, Inc.
00019  *
00020  * 4. The name of the Sun Microsystems, Inc nor may not be used to endorse or
00021  *      promote products derived from this software without specific prior
00022  *      written permission.
00023  *
00024  * SUN MICROSYSTEMS MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS
00025  * SOFTWARE FOR ANY PARTICULAR PURPOSE.  The software is provided "as is"
00026  * without express or implied warranty of any kind.
00027  *
00028  * These notices must be retained in any copies of any part of this software.
00029  */
00030 
00031 // #ident "@(#)mip.cc  1.4     98/08/21 SMI"
00032 
00033 #include <address.h>
00034 #include "mip.h"
00035 
00036 
00037 #define IP_HEADER_SIZE  20
00038 
00039 int hdr_ipinip::offset_;
00040 static class IPinIPHeaderClass : public PacketHeaderClass {
00041 public:
00042         IPinIPHeaderClass() : PacketHeaderClass("PacketHeader/IPinIP",
00043                         sizeof(hdr_ipinip*)) {
00044         bind_offset(&hdr_ipinip::offset_);
00045     }
00046 } class_ipiniphdr;
00047 
00048 static class MIPEncapsulatorClass : public TclClass {
00049 public:
00050     MIPEncapsulatorClass() : TclClass("MIPEncapsulator") {}
00051     TclObject* create(int, const char*const*) {
00052         return (new MIPEncapsulator());
00053     }
00054 } class_mipencapsulator;
00055 
00056 MIPEncapsulator::MIPEncapsulator() : Connector(), mask_(0xffffffff), 
00057     shift_(8), defttl_(32)
00058 {
00059     bind("addr_", (int*)&(here_.addr_));
00060     bind("port_", (int*)&(here_.port_));
00061     bind("shift_", &shift_);
00062     bind("mask_", &mask_);
00063     bind("ttl_", &defttl_);
00064 }
00065 
00066 void MIPEncapsulator::recv(Packet* p, Handler *h)
00067 {
00068     Tcl& tcl = Tcl::instance();
00069 
00070     hdr_ip* hdr = hdr_ip::access(p);
00071     hdr_ipinip **ppinhdr = (hdr_ipinip**)hdr_ipinip::access(p);
00072     if (--hdr->ttl_ <= 0) {
00073         /*
00074          * XXX this should be "dropped" somehow.  Right now,
00075          * these events aren't traced.
00076          */
00077         hdr_ipinip *ptr = *ppinhdr, *temp;
00078         while (ptr != NULL) {
00079             temp = ptr;
00080             ptr = ptr->next_;
00081             delete temp;
00082         }
00083         *ppinhdr = NULL;
00084         Packet::free(p);
00085         return;
00086     }
00087     hdr_ipinip *inhdr = new hdr_ipinip;
00088     //int dst = ((hdr->dst() >> shift_) & mask_);
00089     int dst = Address::instance().get_nodeaddr(hdr->daddr());
00090     tcl.evalf("%s tunnel-exit %d", name_, dst);
00091     int te = atoi(tcl.result());
00092 
00093     inhdr->next_ = *ppinhdr;
00094     *ppinhdr = inhdr;
00095     inhdr->hdr_ = *hdr;
00096 
00097     hdr->saddr() = here_.addr_;
00098     hdr->sport() = here_.port_;
00099     //hdr->dst() = addr_ & ~(~(nsaddr_t)0 << shift_) | (te & mask_) << shift_;;
00100     hdr->daddr() = te;
00101     hdr->dport() = 1;
00102     hdr->ttl() = defttl_;
00103     hdr_cmn::access(p)->size() += IP_HEADER_SIZE;
00104 
00105     target_->recv(p,h);
00106 }
00107 
00108 static class MIPDecapsulatorClass : public TclClass {
00109 public:
00110     MIPDecapsulatorClass() : TclClass("Classifier/Addr/MIPDecapsulator") {}
00111     TclObject* create(int, const char*const*) {
00112         return (new MIPDecapsulator());
00113     }
00114 } class_mipdecapsulator;
00115 
00116 MIPDecapsulator::MIPDecapsulator() : AddressClassifier()
00117 {
00118 }
00119 
00120 void MIPDecapsulator::recv(Packet* p, Handler *h)
00121 {
00122     hdr_ipinip **ppinhdr = (hdr_ipinip **)hdr_ipinip::access(p);
00123     // restore inner header
00124     hdr_ip *pouthdr = hdr_ip::access(p);
00125     assert(ppinhdr);
00126     hdr_ip *pinhdr = &(*ppinhdr)->hdr_;
00127     *ppinhdr = (*ppinhdr)->next_;
00128     *pouthdr = *pinhdr;
00129 
00130     NsObject* link = find(p);
00131     // for mobilenodes use default-target which is probably the 
00132     // RA. cannot use node_entry point instead, as hier address
00133     // of MH point to HA. hence hand decapsulated pkt directly 
00134     // to RA.
00135 
00136     if (link == NULL || pinhdr->ttl_ <= 0) {
00137         /*
00138          * XXX this should be "dropped" somehow.  Right now,
00139          * these events aren't traced.
00140          */
00141         hdr_ipinip *ptr = *ppinhdr, *temp;
00142         while (ptr != NULL) {
00143             temp = ptr;
00144             ptr = ptr->next_;
00145             delete temp;
00146         }
00147         *ppinhdr = NULL;
00148         delete pinhdr;
00149         Packet::free(p);
00150         return;
00151     }
00152     delete pinhdr;
00153 
00154     hdr_cmn::access(p)->size() -= IP_HEADER_SIZE;
00155 
00156     link->recv(p,h);
00157 }
00158 
00159 
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167 

Generated on Tue Mar 6 16:47:47 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6