]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/pinger.cc
2 * Copyright (C) 1996-2016 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
14 \defgroup pinger pinger
15 \ingroup ExternalPrograms
17 * Although it would be possible for Squid to send and receive
18 * ICMP messages directly, we use an external process for
19 * two important reasons:
21 \li Because squid handles many filedescriptors simultaneously,
22 * we get much more accurate RTT measurements when ICMP is
23 * handled by a separate process.
25 \li Superuser privileges are required to send and receive ICMP.
26 * Rather than require Squid to be started as root, we prefer
27 * to have the smaller and simpler pinger program installed
28 * with setuid permissions.
31 * If you want to use Squid's ICMP features (highly recommended!)
32 * When USE_ICMP is defined, Squid will send ICMP pings
33 * to origin server sites.
34 * This information is used in numerous ways:
35 \li - Sent in ICP replies so neighbor caches know how close
36 * you are to the source.
37 \li - For finding the closest instance of a URN.
38 \li - With the 'test_reachability' option. Squid will return
39 * ICP_OP_MISS_NOFETCH for sites which it cannot ping.
44 #include "SquidTime.h"
50 #include "IcmpPinger.h"
63 #define PINGER_TIMEOUT 5
65 /* windows uses the control socket for feedback to squid */
66 #define LINK_TO_SQUID squid_link
68 // windows still requires WSAFD but there are too many dependancy problems
69 // to just link to win32.cc where it is normally defined.
72 Win32__WSAFDIsSet(int fd
, fd_set FAR
* set
)
74 fde
*F
= &fd_table
[fd
];
75 SOCKET s
= F
->win32
.handle
;
77 return __WSAFDIsSet(s
, set
);
82 #define PINGER_TIMEOUT 10
84 /* non-windows use STDOUT for feedback to squid */
85 #define LINK_TO_SQUID 1
87 #endif /* _SQUID_WINDOWS_ */
89 // ICMP Engines are declared global here so they can call each other easily.
94 int icmp_pkts_sent
= 0;
98 \par This is the pinger external process.
104 main(int argc
, char *argv
[])
111 const char *debug_args
= "ALL,10";
113 time_t last_check_time
= 0;
116 * cevans - do this first. It grabs a raw socket. After this we can
119 int icmp4_worker
= -1;
120 int icmp6_worker
= -1;
123 /** start by initializing the pinger debug cache.log-pinger. */
124 if ((t
= getenv("SQUID_DEBUG")))
125 debug_args
= xstrdup(t
);
129 // determine IPv4 or IPv6 capabilities before using sockets.
130 Ip::ProbeTransport();
132 _db_init(NULL
, debug_args
);
134 debugs(42, DBG_CRITICAL
, "pinger: Initialising ICMP pinger ...");
136 icmp4_worker
= icmp4
.Open();
137 if (icmp4_worker
< 0) {
138 debugs(42, DBG_CRITICAL
, "pinger: Unable to start ICMP pinger.");
140 max_fd
= max(max_fd
, icmp4_worker
);
143 icmp6_worker
= icmp6
.Open();
144 if (icmp6_worker
<0 ) {
145 debugs(42, DBG_CRITICAL
, "pinger: Unable to start ICMPv6 pinger.");
147 max_fd
= max(max_fd
, icmp6_worker
);
150 /** abort if neither worker could open a socket. */
151 if (icmp4_worker
< 0 && icmp6_worker
< 0) {
152 debugs(42, DBG_CRITICAL
, "FATAL: pinger: Unable to open any ICMP sockets.");
156 if ( (squid_link
= control
.Open()) < 0) {
157 debugs(42, DBG_CRITICAL
, "FATAL: pinger: Unable to setup Pinger control sockets.");
160 exit(1); // fatal error if the control channel fails.
162 max_fd
= max(max_fd
, squid_link
);
164 if (setgid(getgid()) < 0) {
165 debugs(42, DBG_CRITICAL
, "FATAL: pinger: setgid(" << getgid() << ") failed: " << xstrerror());
170 if (setuid(getuid()) < 0) {
171 debugs(42, DBG_CRITICAL
, "FATAL: pinger: setuid(" << getuid() << ") failed: " << xstrerror());
177 last_check_time
= squid_curtime
;
180 tv
.tv_sec
= PINGER_TIMEOUT
;
183 if (icmp4_worker
>= 0) {
184 FD_SET(icmp4_worker
, &R
);
186 if (icmp6_worker
>= 0) {
187 FD_SET(icmp6_worker
, &R
);
190 FD_SET(squid_link
, &R
);
191 x
= select(10, &R
, NULL
, NULL
, &tv
);
195 debugs(42, DBG_CRITICAL
, HERE
<< " FATAL Shutdown. select()==" << x
<< ", ERR: " << xstrerror());
200 if (FD_ISSET(squid_link
, &R
)) {
204 if (icmp6_worker
>= 0 && FD_ISSET(icmp6_worker
, &R
)) {
207 if (icmp4_worker
>= 0 && FD_ISSET(icmp4_worker
, &R
)) {
211 if (PINGER_TIMEOUT
+ last_check_time
< squid_curtime
) {
212 if (send(LINK_TO_SQUID
, &tv
, 0, 0) < 0) {
213 debugs(42, DBG_CRITICAL
, "pinger: Closing. No requests in last " << PINGER_TIMEOUT
<< " seconds.");
218 last_check_time
= squid_curtime
;
226 #else /* !USE_ICMP */
230 main(int argc
, char *argv
[])
232 std::cerr
<< argv
[0] << ": ICMP support not compiled in." << std::endl
;
236 #endif /* USE_ICMP */