icmp.cc

Go to the documentation of this file.
00001 /*    
00002  * Copyright (c) 1998 Regents of the University of California.
00003  * 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  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. All advertising materials mentioning features or use of this software
00014  *    must display the following acknowledgement:
00015  *      This product includes software developed by the Network Research
00016  *      Group at Lawrence Berkeley National Laboratory.
00017  * 4. Neither the name of the University nor of the Laboratory may be used
00018  *    to endorse or promote products derived from this software without
00019  *    specific prior written permission.
00020  *   
00021  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00022  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00025  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00027  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00028  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00030  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00031  * SUCH DAMAGE.
00032  */  
00033 
00034 #ifndef lint
00035 static const char rcsid[] =
00036     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/emulate/icmp.cc,v 1.5 2000/09/01 03:04:10 haoboy Exp $";
00037 #endif
00038 
00039 #include <stdio.h>
00040 #include <sys/types.h>
00041 #include <netinet/in.h>
00042 #include <netinet/in_systm.h>
00043 #include <netinet/ip.h>
00044 #include <netinet/ip_icmp.h>
00045 #include <netinet/ip_icmp.h>
00046 #include <arpa/inet.h>
00047 
00048 #include "agent.h"
00049 #include "scheduler.h"
00050 #include "packet.h"
00051 #include "emulate/net.h"
00052 #include "emulate/internet.h"
00053 
00054 #ifndef IPPROTO_GGP
00055 #define IPPROTO_GGP 3
00056 #endif // IPPROTO_GGP
00057 
00058 //
00059 // icmp.cc -- a very limited-functionality set of icmp routines
00060 // 
00061 
00062 class IcmpAgent : public Agent {
00063 public:
00064     IcmpAgent();
00065     void recv(Packet*, Handler*) { abort(); }
00066 protected:
00067     void    sendredirect(in_addr& me, in_addr& target, in_addr& dest, in_addr& gw);
00068     int command(int argc, const char*const* argv);
00069 
00070     int ttl_;
00071 };
00072 
00073 static class IcmpAgentClass : public TclClass { 
00074 public:
00075         IcmpAgentClass() : TclClass("Agent/IcmpAgent") {}
00076         TclObject* create(int , const char*const*) {
00077                 return (new IcmpAgent());
00078         } 
00079 } class_icmpsagent;
00080 
00081 IcmpAgent::IcmpAgent() : Agent(PT_LIVE)
00082 {
00083     bind("ttl_", &ttl_);
00084 }
00085 
00086 /*
00087  * sendredirect -- send a packet to "target" containing a redirect
00088  * for the network specified by "dst", so that the gateway "gw" is used
00089  * also, forge the source address so as to appear to come from "me"
00090  */
00091 
00092 void
00093 IcmpAgent::sendredirect(in_addr& me, in_addr& target, in_addr& dst, in_addr& gw)
00094 {
00095     // make a simulator packet to hold the IP packet, which in turn
00096     // holds: ip header, icmp header, embedded ip header, plus 64 bits
00097     // data
00098     int iplen = sizeof(ip) + 8 + sizeof(ip) + 8;
00099         Packet* p = allocpkt(iplen);
00100     hdr_cmn* hc = HDR_CMN(p);
00101     ip* iph = (ip*) p->accessdata();
00102     hc->size() = iplen;
00103 
00104     // make an IP packet ready to send to target
00105     // size will be min icmp + a dummy'd-up IP header
00106     Internet::makeip(iph, iplen, ttl_, IPPROTO_ICMP, me, target);
00107 
00108     // make an ICMP host redirect, set the gwaddr field
00109     icmp* icp = (icmp*) (iph + 1);
00110     icp->icmp_gwaddr = gw;
00111 
00112     // make a dummy IP packet to go in the ICMP data, which will
00113     // be used to indicate to the end host which routing table
00114     // entry to update
00115 
00116     ip* dummyhdr = (ip*)((u_char*)icp + 8); // past icmp hdr
00117         // deprecated protocol inside
00118     Internet::makeip(dummyhdr, 20, 254, IPPROTO_GGP, target, dst);
00119     u_short *port = (u_short*) (dummyhdr + 1);  // past ip hdr
00120     *port++ = htons(9); // discard port
00121     *port = htons(9);   // discard port
00122     icp->icmp_cksum = 0;
00123     icp->icmp_type = ICMP_REDIRECT;
00124     icp->icmp_code = ICMP_REDIRECT_HOST;
00125     icp->icmp_cksum = Internet::in_cksum((u_short*)icp,
00126         8 + sizeof(ip) + 8);
00127 
00128     send(p, 0);
00129     return;
00130 }
00131 
00132 int
00133 IcmpAgent::command(int argc, const char*const* argv)
00134 {
00135     if (argc > 5) {
00136         // $obj send name src dst [...stuff...]
00137         if (strcmp(argv[1], "send") == 0) {
00138             if (strcmp(argv[2], "redirect") == 0 &&
00139                 argc == 7) {
00140                 // $obj send redirect src target dst gwaddr
00141                 // as src, send to targ, so that it changes
00142                 // its route to dst to use gwaddr
00143                 u_long s, t, d, g;
00144                 s = inet_addr(argv[3]);
00145                 t = inet_addr(argv[4]);
00146                 d = inet_addr(argv[5]);
00147                 g = inet_addr(argv[6]);
00148                 in_addr src, targ, dst, gw;
00149                 src.s_addr = s;
00150                 targ.s_addr = t;
00151                 dst.s_addr = d;
00152                 gw.s_addr = g;
00153                 sendredirect(src, targ, dst, gw);
00154                 return (TCL_OK);
00155             }
00156         }
00157     }
00158     return (Agent::command(argc, argv));
00159 }

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