]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/icmp/pinger.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
36 \defgroup pinger pinger
37 \ingroup ExternalPrograms
39 * Although it would be possible for Squid to send and receive
40 * ICMP messages directly, we use an external process for
41 * two important reasons:
43 \li Because squid handles many filedescriptors simultaneously,
44 * we get much more accurate RTT measurements when ICMP is
45 * handled by a separate process.
47 \li Superuser privileges are required to send and receive ICMP.
48 * Rather than require Squid to be started as root, we prefer
49 * to have the smaller and simpler pinger program installed
50 * with setuid permissions.
53 * If you want to use Squid's ICMP features (highly recommended!)
54 * When USE_ICMP is defined, Squid will send ICMP pings
55 * to origin server sites.
56 * This information is used in numerous ways:
57 \li - Sent in ICP replies so neighbor caches know how close
58 * you are to the source.
59 \li - For finding the closest instance of a URN.
60 \li - With the 'test_reachability' option. Squid will return
61 * ICP_OP_MISS_NOFETCH for sites which it cannot ping.
66 #include "SquidTime.h"
72 #include "IcmpPinger.h"
84 #define PINGER_TIMEOUT 5
86 /* windows uses the control socket for feedback to squid */
87 #define LINK_TO_SQUID squid_link
89 // windows still requires WSAFD but there are too many dependancy problems
90 // to just link to win32.cc where it is normally defined.
93 Win32__WSAFDIsSet(int fd
, fd_set FAR
* set
)
95 fde
*F
= &fd_table
[fd
];
96 SOCKET s
= F
->win32
.handle
;
98 return __WSAFDIsSet(s
, set
);
103 #define PINGER_TIMEOUT 10
105 /* non-windows use STDOUT for feedback to squid */
106 #define LINK_TO_SQUID 1
108 #endif /* _SQUID_MSWIN_ */
110 // ICMP Engines are declared global here so they can call each other easily.
115 int icmp_pkts_sent
= 0;
119 \par This is the pinger external process.
125 main(int argc
, char *argv
[])
132 const char *debug_args
= "ALL,10";
134 time_t last_check_time
= 0;
137 * cevans - do this first. It grabs a raw socket. After this we can
140 int icmp4_worker
= -1;
141 int icmp6_worker
= -1;
144 /** start by initializing the pinger debug cache.log-pinger. */
145 if ((t
= getenv("SQUID_DEBUG")))
146 debug_args
= xstrdup(t
);
150 _db_init(NULL
, debug_args
);
152 debugs(42, DBG_CRITICAL
, "pinger: Initialising ICMP pinger ...");
154 icmp4_worker
= icmp4
.Open();
155 if (icmp4_worker
< 0) {
156 debugs(42, DBG_CRITICAL
, "pinger: Unable to start ICMP pinger.");
158 max_fd
= max(max_fd
, icmp4_worker
);
161 icmp6_worker
= icmp6
.Open();
162 if (icmp6_worker
<0 ) {
163 debugs(42, DBG_CRITICAL
, "pinger: Unable to start ICMPv6 pinger.");
165 max_fd
= max(max_fd
, icmp6_worker
);
168 /** abort if neither worker could open a socket. */
169 if (icmp4_worker
< 0 && icmp6_worker
< 0) {
170 debugs(42, DBG_CRITICAL
, "FATAL: pinger: Unable to open any ICMP sockets.");
174 if ( (squid_link
= control
.Open()) < 0) {
175 debugs(42, DBG_CRITICAL
, "FATAL: pinger: Unable to setup Pinger control sockets.");
178 exit(1); // fatal error if the control channel fails.
180 max_fd
= max(max_fd
, squid_link
);
185 last_check_time
= squid_curtime
;
188 tv
.tv_sec
= PINGER_TIMEOUT
;
191 if (icmp4_worker
>= 0) {
192 FD_SET(icmp4_worker
, &R
);
194 if (icmp6_worker
>= 0) {
195 FD_SET(icmp6_worker
, &R
);
198 FD_SET(squid_link
, &R
);
199 x
= select(10, &R
, NULL
, NULL
, &tv
);
203 debugs(42, DBG_CRITICAL
, HERE
<< " FATAL Shutdown. select()==" << x
<< ", ERR: " << xstrerror());
208 if (FD_ISSET(squid_link
, &R
)) {
212 if (icmp6_worker
>= 0 && FD_ISSET(icmp6_worker
, &R
)) {
215 if (icmp4_worker
>= 0 && FD_ISSET(icmp4_worker
, &R
)) {
219 if (PINGER_TIMEOUT
+ last_check_time
< squid_curtime
) {
220 if (send(LINK_TO_SQUID
, &tv
, 0, 0) < 0) {
221 debugs(42, DBG_CRITICAL
, "pinger: Closing. No requests in last " << PINGER_TIMEOUT
<< " seconds.");
226 last_check_time
= squid_curtime
;
237 main(int argc
, char *argv
[])
239 fprintf(stderr
, "%s: ICMP support not compiled in.\n", argv
[0]);
243 #endif /* USE_ICMP */