]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/IcmpPinger.cc
2 * DEBUG: section 42 ICMP Pinger program
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 #define SQUID_HELPER 1
39 #include "SquidTime.h"
40 #include "IcmpPinger.h"
49 IcmpPinger::IcmpPinger() : Icmp()
51 // these start invalid. Setup properly in Open()
52 socket_from_squid
= -1;
56 IcmpPinger::~IcmpPinger()
63 Win32SockCleanup(void)
71 IcmpPinger::Open(void)
77 char buf
[sizeof(wpi
)];
80 struct sockaddr_in PS
;
82 WSAStartup(2, &wsaData
);
83 atexit(Win32SockCleanup
);
86 _db_init(NULL
, "ALL,1");
89 x
= read(0, buf
, sizeof(wpi
));
91 if (x
< (int)sizeof(wpi
)) {
93 debugs(42, DBG_CRITICAL
, HERE
<< "read: FD 0: " << xstrerror());
98 memcpy(&wpi
, buf
, sizeof(wpi
));
101 x
= read(0, buf
, sizeof(PS
));
103 if (x
< (int)sizeof(PS
)) {
105 debugs(42, DBG_CRITICAL
, HERE
<< "read: FD 0: " << xstrerror());
106 write(1, "ERR\n", 4);
110 memcpy(&PS
, buf
, sizeof(PS
));
112 icmp_sock
= WSASocket(FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
, &wpi
, 0, 0);
114 if (icmp_sock
== -1) {
116 debugs(42, DBG_CRITICAL
, HERE
<< "WSASocket: " << xstrerror());
117 write(1, "ERR\n", 4);
121 x
= connect(icmp_sock
, (struct sockaddr
*) &PS
, sizeof(PS
));
123 if (SOCKET_ERROR
== x
) {
125 debugs(42, DBG_CRITICAL
, HERE
<< "connect: " << xstrerror());
126 write(1, "ERR\n", 4);
131 memset(buf
, 0, sizeof(buf
));
132 x
= recv(icmp_sock
, (void *) buf
, sizeof(buf
), 0);
135 debugs(42, DBG_CRITICAL
, HERE
<< "recv: " << xstrerror());
139 x
= send(icmp_sock
, (const void *) buf
, strlen(buf
), 0);
141 if (x
< 3 || strncmp("OK\n", buf
, 3)) {
142 debugs(42, DBG_CRITICAL
, HERE
<< "recv: " << xstrerror());
147 debugs(42, DBG_IMPORTANT
, "pinger: Squid socket opened");
149 /* windows uses a socket stream as a dual-direction channel */
150 socket_to_squid
= icmp_sock
;
151 socket_from_squid
= icmp_sock
;
155 #else /* !_SQUID_WINDOWS_ */
157 /* non-windows apps use stdin/out pipes as the squid channel(s) */
158 socket_from_squid
= 0; // use STDIN macro ??
159 socket_to_squid
= 1; // use STDOUT macro ??
160 return socket_to_squid
;
165 IcmpPinger::Close(void)
169 shutdown(icmp_sock
, SD_BOTH
);
174 /* also shutdown the helper engines */
180 IcmpPinger::Recv(void)
182 static pingerEchoData pecho
;
186 memset(&pecho
, '\0', sizeof(pecho
));
187 n
= recv(socket_from_squid
, &pecho
, sizeof(pecho
), 0);
190 debugs(42, DBG_IMPORTANT
, "Pinger exiting.");
197 debugs(42, DBG_CRITICAL
, HERE
<< "EOF encountered. Pinger exiting.\n");
203 guess_size
= n
- (sizeof(pingerEchoData
) - PINGER_PAYLOAD_SZ
);
205 if (guess_size
!= pecho
.psize
) {
206 debugs(42, 2, HERE
<< "size mismatch, guess=" << guess_size
<< ", psize=" << pecho
.psize
);
207 /* don't process this message, but keep running */
211 /* pass request for ICMPv6 handing */
212 if (pecho
.to
.IsIPv6()) {
213 debugs(42, 2, HERE
<< " Pass " << pecho
.to
<< " off to ICMPv6 module.");
214 icmp6
.SendEcho(pecho
.to
,
220 /* pass the packet for ICMP handling */
221 else if (pecho
.to
.IsIPv4()) {
222 debugs(42, 2, HERE
<< " Pass " << pecho
.to
<< " off to ICMPv4 module.");
223 icmp4
.SendEcho(pecho
.to
,
228 debugs(42, DBG_IMPORTANT
, HERE
<< " IP has unknown Type. " << pecho
.to
);
233 IcmpPinger::SendResult(pingerReplyData
&preply
, int len
)
235 debugs(42, 2, HERE
<< "return result to squid. len=" << len
);
237 if (send(socket_to_squid
, &preply
, len
, 0) < 0) {
238 debugs(42, DBG_CRITICAL
, "pinger: FATAL error on send: " << xstrerror());
244 #endif /* USE_ICMP */