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 #ifndef lint
00035 static const char rcsid[] =
00036 "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/emulate/ping_responder.cc,v 1.8 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 "emulate/internet.h"
00051
00052
00053
00054
00055
00056
00057
00058 class PingResponder : public Agent {
00059 public:
00060 PingResponder() : Agent(PT_LIVE) { }
00061 void recv(Packet*, Handler*);
00062 protected:
00063 icmp* validate(int, ip*);
00064 void reflect(ip*);
00065 };
00066
00067 static class PingResponderClass : public TclClass {
00068 public:
00069 PingResponderClass() : TclClass("Agent/PingResponder") {}
00070 TclObject* create(int , const char*const*) {
00071 return (new PingResponder());
00072 }
00073 } class_pingresponder;
00074
00075
00076
00077
00078
00079
00080
00081 void
00082 PingResponder::recv(Packet* pkt, Handler*)
00083 {
00084 hdr_cmn *ch = HDR_CMN(pkt);
00085 int psize = ch->size();
00086 u_char* payload = pkt->accessdata();
00087
00088 if (payload == NULL) {
00089 fprintf(stderr, "%f: PingResponder(%s): recv NULL data area\n",
00090 Scheduler::instance().clock(), name());
00091 Packet::free(pkt);
00092 return;
00093 }
00094
00095
00096
00097
00098
00099 icmp* icmph;
00100 if ((icmph = validate(psize, (ip*) payload)) == NULL) {
00101 Internet::print_ip((ip*) payload);
00102 Packet::free(pkt);
00103 return;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 icmph->icmp_type = ICMP_ECHOREPLY;
00113 reflect((ip*) payload);
00114
00115
00116
00117
00118
00119 Agent::initpkt(pkt);
00120 ch->size() = psize;
00121
00122 target_->recv(pkt);
00123 return;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 icmp*
00133 PingResponder::validate(int sz, ip* iph)
00134 {
00135 if (sz < 20) {
00136 fprintf(stderr, "%f: PingResponder(%s): sim pkt too small for base IP header(%d)\n",
00137 Scheduler::instance().clock(), name(), sz);
00138 return (NULL);
00139 }
00140
00141 int ipver = (*((char*)iph) & 0xf0) >> 4;
00142 if (ipver != 4) {
00143 fprintf(stderr, "%f: PingResponder(%s): IP bad ver (%d)\n",
00144 Scheduler::instance().clock(), name(), ipver);
00145 return (NULL);
00146 }
00147
00148 int iplen = ntohs(iph->ip_len);
00149 int iphlen = (*((char*)iph) & 0x0f) << 2;
00150 if (iplen < (iphlen + 8)) {
00151 fprintf(stderr, "%f: PingResponder(%s): IP dgram not long enough (len: %d)\n",
00152 Scheduler::instance().clock(), name(), iplen);
00153 return (NULL);
00154 }
00155
00156 if (sz < iplen) {
00157 fprintf(stderr, "%f: PingResponder(%s): IP dgram not long enough (len: %d)\n",
00158 Scheduler::instance().clock(), name(), iplen);
00159 return (NULL);
00160 }
00161
00162 if (iphlen != 20) {
00163 fprintf(stderr, "%f: PingResponder(%s): IP bad hlen (%d)\n",
00164 Scheduler::instance().clock(), name(), iphlen);
00165 return (NULL);
00166 }
00167
00168 if (Internet::in_cksum((u_short*) iph, iphlen)) {
00169 fprintf(stderr, "%f: PingResponder(%s): IP bad cksum\n",
00170 Scheduler::instance().clock(), name());
00171 return (NULL);
00172 }
00173
00174 if (iph->ip_p != IPPROTO_ICMP) {
00175 fprintf(stderr, "%f: PingResponder(%s): not ICMP (proto: %d)\n",
00176 Scheduler::instance().clock(), name(), iph->ip_p);
00177 return (NULL);
00178 }
00179
00180
00181 if (iph->ip_off != 0) {
00182 fprintf(stderr, "%f: PingResponder(%s): fragment! (off: 0x%x)\n",
00183 Scheduler::instance().clock(), name(), ntohs(iph->ip_off));
00184 return (NULL);
00185 }
00186
00187 if (iph->ip_src.s_addr == 0xffffffff || iph->ip_src.s_addr == 0) {
00188 fprintf(stderr, "%f: PingResponder(%s): bad src addr (%s)\n",
00189 Scheduler::instance().clock(), name(),
00190 inet_ntoa(iph->ip_src));
00191 return (NULL);
00192 }
00193
00194 if (IN_MULTICAST(ntohl(iph->ip_src.s_addr))) {
00195 fprintf(stderr, "%f: PingResponder(%s): mcast src addr (%s)\n",
00196 Scheduler::instance().clock(), name(),
00197 inet_ntoa(iph->ip_src));
00198 return (NULL);
00199 }
00200 icmp* icp = (icmp*) (iph + 1);
00201 if (Internet::in_cksum((u_short*) icp, iplen - iphlen) != 0) {
00202 fprintf(stderr,
00203 "%f: PingResponder(%s): bad ICMP cksum\n",
00204 Scheduler::instance().clock(), name());
00205 return (NULL);
00206 }
00207 if (icp->icmp_type != ICMP_ECHO) {
00208 fprintf(stderr, "%f: PingResponder(%s): not echo request (%d)\n",
00209 Scheduler::instance().clock(), name(),
00210 icp->icmp_type);
00211 return (NULL);
00212 }
00213
00214 if (icp->icmp_code != 0) {
00215 fprintf(stderr, "%f: PingResponder(%s): bad code (%d)\n",
00216 Scheduler::instance().clock(), name(),
00217 icp->icmp_code);
00218 return (NULL);
00219 }
00220 return (icp);
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230 void
00231 PingResponder::reflect(ip* iph)
00232 {
00233 in_addr daddr = iph->ip_dst;
00234 int iplen = ntohs(iph->ip_len);
00235 int iphlen = (*((char*)iph) & 0x0f) << 2;
00236
00237
00238 iph->ip_dst = iph->ip_src;
00239 iph->ip_src = daddr;
00240 iph->ip_sum = 0;
00241 iph->ip_sum = Internet::in_cksum((u_short*) iph, iphlen);
00242
00243
00244 icmp* icp = (icmp*)(iph + 1);
00245 icp->icmp_cksum = 0;
00246 icp->icmp_cksum = Internet::in_cksum((u_short*)icp, iplen - iphlen);
00247 }