]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/IcmpPinger.cc
2 * Copyright (C) 1996-2020 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
;
59 WSAStartup(2, &wsaData
);
60 atexit(Win32SockCleanup
);
63 _db_init(NULL
, "ALL,1");
66 x
= read(0, buf
, sizeof(wpi
));
68 if (x
< (int)sizeof(wpi
)) {
71 debugs(42, DBG_CRITICAL
, MYNAME
<< " read: FD 0: " << xstrerr(xerrno
));
76 memcpy(&wpi
, buf
, sizeof(wpi
));
79 x
= read(0, buf
, sizeof(PS
));
81 if (x
< (int)sizeof(PS
)) {
84 debugs(42, DBG_CRITICAL
, MYNAME
<< " read: FD 0: " << xstrerr(xerrno
));
89 memcpy(&PS
, buf
, sizeof(PS
));
91 icmp_sock
= WSASocket(FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
, &wpi
, 0, 0);
93 if (icmp_sock
== -1) {
96 debugs(42, DBG_CRITICAL
, MYNAME
<< "WSASocket: " << xstrerr(xerrno
));
101 x
= connect(icmp_sock
, (struct sockaddr
*) &PS
, sizeof(PS
));
103 if (SOCKET_ERROR
== x
) {
106 debugs(42, DBG_CRITICAL
, MYNAME
<< "connect: " << xstrerr(xerrno
));
107 write(1, "ERR\n", 4);
112 memset(buf
, 0, sizeof(buf
));
113 x
= recv(icmp_sock
, (void *) buf
, sizeof(buf
), 0);
117 debugs(42, DBG_CRITICAL
, MYNAME
<< "recv: " << xstrerr(xerrno
));
121 x
= send(icmp_sock
, (const void *) buf
, strlen(buf
), 0);
124 if (x
< 3 || strncmp("OK\n", buf
, 3)) {
125 debugs(42, DBG_CRITICAL
, MYNAME
<< "recv: " << xstrerr(xerrno
));
130 debugs(42, DBG_IMPORTANT
, "pinger: Squid socket opened");
132 /* windows uses a socket stream as a dual-direction channel */
133 socket_to_squid
= icmp_sock
;
134 socket_from_squid
= icmp_sock
;
138 #else /* !_SQUID_WINDOWS_ */
140 /* non-windows apps use stdin/out pipes as the squid channel(s) */
141 socket_from_squid
= 0; // use STDIN macro ??
142 socket_to_squid
= 1; // use STDOUT macro ??
143 return socket_to_squid
;
148 IcmpPinger::Close(void)
152 shutdown(icmp_sock
, SD_BOTH
);
157 /* also shutdown the helper engines */
163 IcmpPinger::Recv(void)
165 static pingerEchoData pecho
;
169 pecho
= pingerEchoData();
170 n
= recv(socket_from_squid
, &pecho
, sizeof(pecho
), 0);
173 debugs(42, DBG_IMPORTANT
, "Pinger exiting.");
180 debugs(42, DBG_CRITICAL
, "EOF encountered. Pinger exiting.");
186 guess_size
= n
- (sizeof(pingerEchoData
) - PINGER_PAYLOAD_SZ
);
188 if (guess_size
!= pecho
.psize
) {
189 debugs(42, 2, HERE
<< "size mismatch, guess=" << guess_size
<< ", psize=" << pecho
.psize
);
190 /* don't process this message, but keep running */
194 /* pass request for ICMPv6 handing */
195 if (pecho
.to
.isIPv6()) {
196 debugs(42, 2, HERE
<< " Pass " << pecho
.to
<< " off to ICMPv6 module.");
197 icmp6
.SendEcho(pecho
.to
,
203 /* pass the packet for ICMP handling */
204 else if (pecho
.to
.isIPv4()) {
205 debugs(42, 2, HERE
<< " Pass " << pecho
.to
<< " off to ICMPv4 module.");
206 icmp4
.SendEcho(pecho
.to
,
211 debugs(42, DBG_IMPORTANT
, HERE
<< " IP has unknown Type. " << pecho
.to
);
216 IcmpPinger::SendResult(pingerReplyData
&preply
, int len
)
218 debugs(42, 2, HERE
<< "return result to squid. len=" << len
);
220 if (send(socket_to_squid
, &preply
, len
, 0) < 0) {
222 debugs(42, DBG_CRITICAL
, "pinger: FATAL error on send: " << xstrerr(xerrno
));
228 #endif /* USE_ICMP */