modules/up/src/Core/network/Ping.cc
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- pingTimerHandler
- Ping
- Ping
- send
- handleTimer
- receive
1 //
2 // $Id: Ping.cc,v 1.1.1.1 2000/03/10 16:32:19 engin Exp $
3 //
4 // Author(s): Ramesh Govindan
5
6 #ifdef HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9
10 #include "util/Types.hh"
11 #include "util/Trail.hh"
12 #include "util/Handler.hh"
13 #include "util/Buffer.hh"
14 #include "sys/File.hh"
15 #include "sys/Pipe.hh"
16 #include "sys/Time.hh"
17 #include "sched/Timer.hh"
18 #include "sched/Dispatcher.hh"
19
20 #include "network/Ping.hh"
21 #include "network/Headers.hh"
22 #include "network/Network.hh"
23
24 // Locals
25 static TraceCode tracePing("network");
26 static TimeShort maxWait(10, 0);
27 static unsigned int probeNumber = 1;
28
29
30 static void
31 pingTimerHandler(void* ptr,
/* [<][>][^][v][top][bottom][index][help] */
32 void*)
33 {
34 ((Ping*) ptr)->handleTimer();
35 }
36
37 Ping::Ping(Address* a,
/* [<][>][^][v][top][bottom][index][help] */
38 Handler& cb)
39 : ListNode()
40 {
41 done = cb;
42 addr = *a;
43 timer = NULL;
44 network->pendingPings.append(this);
45 }
46
47 Ping::~Ping()
/* [<][>][^][v][top][bottom][index][help] */
48 {
49 if (timer) {
50 delete timer;
51 }
52 network->pendingPings.remove(this);
53 }
54
55 void
56 Ping::send()
/* [<][>][^][v][top][bottom][index][help] */
57 {
58 Handler th(pingTimerHandler, this);
59 Handler nh(NULL, NULL);
60 Buffer* buf;
61 IP* ip;
62 ICMP* icmp;
63 RawSocket* raw;
64 unsigned long* t;
65 TimeLong at;
66
67 raw = new RawSocket(nh, nh, RawSocketICMP);
68 buf = new Buffer(sizeof(IP) + sizeof(ICMP) + sizeof(unsigned long long));
69 buf->zeroFill();
70
71 ip = (IP*) buf->contents;
72 ip->headerLength = sizeof(IP) >> 2;
73 ip->version = 4;
74 ip->typeOfService = 0;
75 ip->totalLength = sizeof(IP) + sizeof(ICMP) + sizeof(unsigned long long);
76 ip->identifier = 0;
77 ip->fragmentOffset = 0;
78 ip->timeToLive = 0xff;
79 ip->protocol = IPPROTO_ICMP;
80 ip->source = (network->interfaces.head())->address.get();
81 ip->destination = addr.get();
82 ip->hton();
83 ip->checksum = 0;
84 ip->checksum = htons(network->cksum((U16*) ip, sizeof(IP)));
85
86 // We encode the sequence number and ident in the probe
87 sequence = probeNumber++;
88 icmp = (ICMP*) (ip + 1);
89 icmp->type = ICMPTypeEcho;
90 icmp->code = 0;
91 icmp->gateway = sequence;
92 icmp->hton();
93
94 dispatcher.systemClock.sync();
95 sent = dispatcher.systemClock;
96
97 icmp->checksum = 0;
98 icmp->checksum = network->cksum((U16*) icmp,
99 sizeof(ICMP) + sizeof(unsigned long long));
100
101 raw->sendTo(buf->contents, buf->size, addr, 0);
102
103 TRACE(tracePing, "ping sent to %s\n", addr.name());
104
105 delete raw;
106 delete buf;
107
108 if (timer) {
109 delete timer;
110 }
111 at = dispatcher.systemClock;
112 at = at + maxWait;
113 timer = new Timer(th, at);
114
115 return;
116 }
117
118 void
119 Ping::handleTimer()
/* [<][>][^][v][top][bottom][index][help] */
120 {
121 TRACE(tracePing, "ping failed to %s\n", addr.name());
122
123 timer = NULL;
124 lastRtt = InfiniteInterval;
125 done.callBack((void*) this);
126 return;
127 }
128
129 Boolean
130 Ping::receive(IP* ip)
/* [<][>][^][v][top][bottom][index][help] */
131 {
132 ICMP* icmp;
133 unsigned long* t;
134
135 icmp = (struct ICMP*) ((char*) ip + (ip->headerLength << 2));
136 // Verify signature to see if it is for us
137 if (icmp->type == ICMPTypeEchoReply) {
138 if (ip->source != addr.get()) {
139 return false;
140 }
141 if (icmp->gateway != sequence) {
142 return false;
143 }
144
145 TRACE(tracePing, "ping received from %s\n", addr.name());
146
147 t = (unsigned long *) (icmp+1);
148
149 dispatcher.systemClock.sync();
150 lastRtt = dispatcher.systemClock - sent;
151 done.callBack((void*) this);
152 return true;
153 }
154 return false;
155 }
156
157 // Copyright (c) 1994 by the University of Southern California.
158 // All rights reserved.
159 //
160 // Permission to use, copy, modify, and distribute this software and
161 // its documentation in source and binary forms for lawful
162 // non-commercial purposes and without fee is hereby granted, provided
163 // that the above copyright notice appear in all copies and that both
164 // the copyright notice and this permission notice appear in supporting
165 // documentation, and that any documentation, advertising materials,
166 // and other materials related to such distribution and use acknowledge
167 // that the software was developed by the University of Southern
168 // California and/or Information Sciences Institute.
169 // The name of the University of Southern California may not
170 // be used to endorse or promote products derived from this software
171 // without specific prior written permission.
172 //
173 // THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
174 // ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE. THIS SOFTWARE IS
175 // PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
176 // INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
177 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
178 // NON-INFRINGEMENT.
179 //
180 // IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
181 // SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
182 // TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
183 // THE USE OR PERFORMANCE OF THIS SOFTWARE.
184 //
185 // Questions concerning this software should be directed to
186 // scan@isi.edu.
187 //
188
189