ttl.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) 1996-1997 Regents of the University of California.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the MASH Research
00017  *  Group at the University of California Berkeley.
00018  * 4. Neither the name of the University nor of the Research Group may be
00019  *    used to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 
00035 #ifndef lint
00036 static const char rcsid[] =
00037     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/common/ttl.cc,v 1.11 1998/08/12 23:41:24 gnguyen Exp $";
00038 #endif
00039 
00040 #include "packet.h"
00041 #include "ip.h"
00042 #include "connector.h"
00043 
00044 class TTLChecker : public Connector {
00045 public:
00046     TTLChecker() : noWarn_(1), tick_(1) {}
00047     int command(int argc, const char*const* argv) {
00048         if (argc == 3) {
00049             if (strcmp(argv[1], "warning") == 0) {
00050                 noWarn_ = ! atoi(argv[2]);
00051                 return TCL_OK;
00052             }
00053             if (strcmp(argv[1], "tick") == 0) {
00054                 int tick = atoi(argv[2]);
00055                 if (tick > 0) {
00056                     tick_ = tick;
00057                     return TCL_OK;
00058                 } else {
00059                     Tcl& tcl = Tcl::instance();
00060                     tcl.resultf("%s: TTL must be positive (specified = %d)\n",
00061                             name(), tick);
00062                     return TCL_ERROR;
00063                 }
00064             }
00065         }
00066         return Connector::command(argc, argv);
00067     }
00068     void recv(Packet* p, Handler* h) {
00069         hdr_ip* iph = hdr_ip::access(p);
00070         int ttl = iph->ttl() - tick_;
00071         if (ttl <= 0) {
00072             /* XXX should send to a drop object.*/
00073             // Yes, and now it does...
00074             // Packet::free(p);
00075             if (! noWarn_)
00076                 printf("ttl exceeded\n");
00077             drop(p);
00078             return;
00079         }
00080         iph->ttl() = ttl;
00081         send(p, h);
00082     }
00083 protected:
00084     int noWarn_;
00085     int tick_;
00086 };
00087 
00088 static class TTLCheckerClass : public TclClass {
00089 public:
00090     TTLCheckerClass() : TclClass("TTLChecker") {}
00091     TclObject* create(int, const char*const*) {
00092         return (new TTLChecker);
00093     }
00094 } ttl_checker_class;
00095 
00096 
00097 class SessionTTLChecker : public Connector {
00098 public:
00099     SessionTTLChecker() {}
00100     int command(int argc, const char*const* argv);
00101     void recv(Packet* p, Handler* h) {
00102         hdr_ip* iph = hdr_ip::access(p);
00103         int ttl = iph->ttl() - tick_;
00104         if (ttl <= 0) {
00105             /* XXX should send to a drop object.*/
00106             // Yes, and now it does...
00107             // Packet::free(p);
00108             printf("ttl exceeded\n");
00109             drop(p);
00110             return;
00111         }
00112         iph->ttl() = ttl;
00113         send(p, h);
00114     }
00115 protected:
00116     int tick_;
00117 };
00118 
00119 static class SessionTTLCheckerClass : public TclClass {
00120 public:
00121     SessionTTLCheckerClass() : TclClass("TTLChecker/Session") {}
00122     TclObject* create(int, const char*const*) {
00123         return (new SessionTTLChecker);
00124     }
00125 } session_ttl_checker_class;
00126 
00127 int SessionTTLChecker::command(int argc, const char*const* argv)
00128 {
00129     if (argc == 3) {
00130         if (strcmp(argv[1], "tick") == 0) {
00131             tick_ = atoi(argv[2]);
00132             return (TCL_OK);
00133         }
00134     }
00135     return (Connector::command(argc, argv));
00136 }

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