00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
00073
00074
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
00106
00107
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 }