]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/IcmpPinger.cc
Removed squid-old.h
[thirdparty/squid.git] / src / icmp / IcmpPinger.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 42 ICMP Pinger program
5 * AUTHOR: Duane Wessels
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35 #define SQUID_HELPER 1
36
37 #include "squid.h"
38
39 #if USE_ICMP
40
41 #include "SquidTime.h"
42 #include "IcmpPinger.h"
43 #include "Icmp4.h"
44 #include "Icmp6.h"
45 #include "Debug.h"
46
47 IcmpPinger::IcmpPinger() : Icmp()
48 {
49 // these start invalid. Setup properly in Open()
50 socket_from_squid = -1;
51 socket_to_squid = -1;
52 }
53
54 IcmpPinger::~IcmpPinger()
55 {
56 Close();
57 }
58
59 #if _SQUID_MSWIN_
60 void
61 Win32SockCleanup(void)
62 {
63 WSACleanup();
64 return;
65 }
66 #endif
67
68 int
69 IcmpPinger::Open(void)
70 {
71 #if _SQUID_MSWIN_
72
73 WSADATA wsaData;
74 WSAPROTOCOL_INFO wpi;
75 char buf[sizeof(wpi)];
76 int x;
77
78 struct sockaddr_in PS;
79
80 WSAStartup(2, &wsaData);
81 atexit(Win32SockCleanup);
82
83 getCurrentTime();
84 _db_init(NULL, "ALL,1");
85 setmode(0, O_BINARY);
86 setmode(1, O_BINARY);
87 x = read(0, buf, sizeof(wpi));
88
89 if (x < (int)sizeof(wpi)) {
90 getCurrentTime();
91 debugs(42, DBG_CRITICAL, HERE << "read: FD 0: " << xstrerror());
92 write(1, "ERR\n", 4);
93 return -1;
94 }
95
96 memcpy(&wpi, buf, sizeof(wpi));
97
98 write(1, "OK\n", 3);
99 x = read(0, buf, sizeof(PS));
100
101 if (x < (int)sizeof(PS)) {
102 getCurrentTime();
103 debugs(42, DBG_CRITICAL, HERE << "read: FD 0: " << xstrerror());
104 write(1, "ERR\n", 4);
105 return -1;
106 }
107
108 memcpy(&PS, buf, sizeof(PS));
109
110 icmp_sock = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, &wpi, 0, 0);
111
112 if (icmp_sock == -1) {
113 getCurrentTime();
114 debugs(42, DBG_CRITICAL, HERE << "WSASocket: " << xstrerror());
115 write(1, "ERR\n", 4);
116 return -1;
117 }
118
119 x = connect(icmp_sock, (struct sockaddr *) &PS, sizeof(PS));
120
121 if (SOCKET_ERROR == x) {
122 getCurrentTime();
123 debugs(42, DBG_CRITICAL, HERE << "connect: " << xstrerror());
124 write(1, "ERR\n", 4);
125 return -1;
126 }
127
128 write(1, "OK\n", 3);
129 memset(buf, 0, sizeof(buf));
130 x = recv(icmp_sock, (void *) buf, sizeof(buf), 0);
131
132 if (x < 3) {
133 debugs(42, DBG_CRITICAL, HERE << "recv: " << xstrerror());
134 return -1;
135 }
136
137 x = send(icmp_sock, (const void *) buf, strlen(buf), 0);
138
139 if (x < 3 || strncmp("OK\n", buf, 3)) {
140 debugs(42, DBG_CRITICAL, HERE << "recv: " << xstrerror());
141 return -1;
142 }
143
144 getCurrentTime();
145 debugs(42, DBG_IMPORTANT, "pinger: Squid socket opened");
146
147 /* windows uses a socket stream as a dual-direction channel */
148 socket_to_squid = icmp_sock;
149 socket_from_squid = icmp_sock;
150
151 return icmp_sock;
152
153 #else /* !_SQUID_MSWIN_ */
154
155 /* non-windows apps use stdin/out pipes as the squid channel(s) */
156 socket_from_squid = 0; // use STDIN macro ??
157 socket_to_squid = 1; // use STDOUT macro ??
158 return socket_to_squid;
159 #endif
160 }
161
162 void
163 IcmpPinger::Close(void)
164 {
165 #if _SQUID_MSWIN_
166
167 shutdown(icmp_sock, SD_BOTH);
168 close(icmp_sock);
169 icmp_sock = -1;
170 #endif
171
172 /* also shutdown the helper engines */
173 icmp4.Close();
174 icmp6.Close();
175 }
176
177 void
178 IcmpPinger::Recv(void)
179 {
180 static pingerEchoData pecho;
181 int n;
182 int guess_size;
183
184 memset(&pecho, '\0', sizeof(pecho));
185 n = recv(socket_from_squid, &pecho, sizeof(pecho), 0);
186
187 if (n < 0) {
188 debugs(42, DBG_IMPORTANT, "Pinger exiting.");
189 Close();
190 exit(1);
191 }
192
193 if (0 == n) {
194 /* EOF indicator */
195 debugs(42, DBG_CRITICAL, HERE << "EOF encountered. Pinger exiting.\n");
196 errno = 0;
197 Close();
198 exit(1);
199 }
200
201 guess_size = n - (sizeof(pingerEchoData) - PINGER_PAYLOAD_SZ);
202
203 if (guess_size != pecho.psize) {
204 debugs(42, 2, HERE << "size mismatch, guess=" << guess_size << ", psize=" << pecho.psize);
205 /* don't process this message, but keep running */
206 return;
207 }
208
209 /* pass request for ICMPv6 handing */
210 if (pecho.to.IsIPv6()) {
211 debugs(42, 2, HERE << " Pass " << pecho.to << " off to ICMPv6 module.");
212 icmp6.SendEcho(pecho.to,
213 pecho.opcode,
214 pecho.payload,
215 pecho.psize);
216 }
217
218 /* pass the packet for ICMP handling */
219 else if (pecho.to.IsIPv4()) {
220 debugs(42, 2, HERE << " Pass " << pecho.to << " off to ICMPv4 module.");
221 icmp4.SendEcho(pecho.to,
222 pecho.opcode,
223 pecho.payload,
224 pecho.psize);
225 } else {
226 debugs(42, DBG_IMPORTANT, HERE << " IP has unknown Type. " << pecho.to );
227 }
228 }
229
230 void
231 IcmpPinger::SendResult(pingerReplyData &preply, int len)
232 {
233 debugs(42, 2, HERE << "return result to squid. len=" << len);
234
235 if (send(socket_to_squid, &preply, len, 0) < 0) {
236 debugs(42, DBG_CRITICAL, "pinger: FATAL error on send: " << xstrerror());
237 Close();
238 exit(1);
239 }
240 }
241
242 #endif /* USE_ICMP */