]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/IcmpPinger.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 42 ICMP Pinger program */
11 #define SQUID_HELPER 1
20 #include "IcmpPinger.h"
21 #include "SquidTime.h"
25 IcmpPinger::IcmpPinger() : Icmp()
27 // these start invalid. Setup properly in Open()
28 socket_from_squid
= -1;
32 IcmpPinger::~IcmpPinger()
39 Win32SockCleanup(void)
47 IcmpPinger::Open(void)
53 char buf
[sizeof(wpi
)];
56 struct sockaddr_in PS
;
58 WSAStartup(2, &wsaData
);
59 atexit(Win32SockCleanup
);
62 _db_init(NULL
, "ALL,1");
65 x
= read(0, buf
, sizeof(wpi
));
67 if (x
< (int)sizeof(wpi
)) {
69 debugs(42, DBG_CRITICAL
, HERE
<< "read: FD 0: " << xstrerror());
74 memcpy(&wpi
, buf
, sizeof(wpi
));
77 x
= read(0, buf
, sizeof(PS
));
79 if (x
< (int)sizeof(PS
)) {
81 debugs(42, DBG_CRITICAL
, HERE
<< "read: FD 0: " << xstrerror());
86 memcpy(&PS
, buf
, sizeof(PS
));
88 icmp_sock
= WSASocket(FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
, &wpi
, 0, 0);
90 if (icmp_sock
== -1) {
92 debugs(42, DBG_CRITICAL
, HERE
<< "WSASocket: " << xstrerror());
97 x
= connect(icmp_sock
, (struct sockaddr
*) &PS
, sizeof(PS
));
99 if (SOCKET_ERROR
== x
) {
101 debugs(42, DBG_CRITICAL
, HERE
<< "connect: " << xstrerror());
102 write(1, "ERR\n", 4);
107 memset(buf
, 0, sizeof(buf
));
108 x
= recv(icmp_sock
, (void *) buf
, sizeof(buf
), 0);
111 debugs(42, DBG_CRITICAL
, HERE
<< "recv: " << xstrerror());
115 x
= send(icmp_sock
, (const void *) buf
, strlen(buf
), 0);
117 if (x
< 3 || strncmp("OK\n", buf
, 3)) {
118 debugs(42, DBG_CRITICAL
, HERE
<< "recv: " << xstrerror());
123 debugs(42, DBG_IMPORTANT
, "pinger: Squid socket opened");
125 /* windows uses a socket stream as a dual-direction channel */
126 socket_to_squid
= icmp_sock
;
127 socket_from_squid
= icmp_sock
;
131 #else /* !_SQUID_WINDOWS_ */
133 /* non-windows apps use stdin/out pipes as the squid channel(s) */
134 socket_from_squid
= 0; // use STDIN macro ??
135 socket_to_squid
= 1; // use STDOUT macro ??
136 return socket_to_squid
;
141 IcmpPinger::Close(void)
145 shutdown(icmp_sock
, SD_BOTH
);
150 /* also shutdown the helper engines */
156 IcmpPinger::Recv(void)
158 static pingerEchoData pecho
;
162 memset(&pecho
, '\0', sizeof(pecho
));
163 n
= recv(socket_from_squid
, &pecho
, sizeof(pecho
), 0);
166 debugs(42, DBG_IMPORTANT
, "Pinger exiting.");
173 debugs(42, DBG_CRITICAL
, HERE
<< "EOF encountered. Pinger exiting.\n");
179 guess_size
= n
- (sizeof(pingerEchoData
) - PINGER_PAYLOAD_SZ
);
181 if (guess_size
!= pecho
.psize
) {
182 debugs(42, 2, HERE
<< "size mismatch, guess=" << guess_size
<< ", psize=" << pecho
.psize
);
183 /* don't process this message, but keep running */
187 /* pass request for ICMPv6 handing */
188 if (pecho
.to
.isIPv6()) {
189 debugs(42, 2, HERE
<< " Pass " << pecho
.to
<< " off to ICMPv6 module.");
190 icmp6
.SendEcho(pecho
.to
,
196 /* pass the packet for ICMP handling */
197 else if (pecho
.to
.isIPv4()) {
198 debugs(42, 2, HERE
<< " Pass " << pecho
.to
<< " off to ICMPv4 module.");
199 icmp4
.SendEcho(pecho
.to
,
204 debugs(42, DBG_IMPORTANT
, HERE
<< " IP has unknown Type. " << pecho
.to
);
209 IcmpPinger::SendResult(pingerReplyData
&preply
, int len
)
211 debugs(42, 2, HERE
<< "return result to squid. len=" << len
);
213 if (send(socket_to_squid
, &preply
, len
, 0) < 0) {
214 debugs(42, DBG_CRITICAL
, "pinger: FATAL error on send: " << xstrerror());
220 #endif /* USE_ICMP */