~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to src/icmp/pinger.cc

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2009-09-24 14:51:06 UTC
  • mfrom: (1.1.12 upstream)
  • mto: (20.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20090924145106-38jgrzmj0d73pha5
Tags: 3.1.0.13-1
* Upload to experimental

* New upstream release
  - Fixes Follow-X-Forwarded-For support (Closes: #523943)
  - Adds IPv6 support (Closes: #432351)

* debian/rules
  - Removed obsolete configuration options
  - Enable db and radius basic authentication modules

* debian/patches/01-cf.data.debian
  - Adapted to new upstream version

* debian/patches/02-makefile-defaults
  - Adapted to new upstream version

* debian/{squid.postinst,squid.rc,README.Debian,watch}
  - Updated references to squid 3.1

* debian/squid3.install
  - Install CSS file for error pages
  - Install manual pages for new authentication modules

* debian/squid3-common.install
  - Install documented version of configuration file in /usr/share/doc/squid3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id$
 
3
 *
 
4
 * DEBUG: section 42    ICMP Pinger program
 
5
 * AUTHOR: Duane Wessels
 
6
 *
 
7
 * SQUID Web Proxy Cache          http://www.squid-cache.org/
 
8
 * ----------------------------------------------------------
 
9
 *
 
10
 *  Squid is the result of efforts by numerous individuals from
 
11
 *  the Internet community; see the CONTRIBUTORS file for full
 
12
 *  details.   Many organizations have provided support for Squid's
 
13
 *  development; see the SPONSORS file for full details.  Squid is
 
14
 *  Copyrighted (C) 2001 by the Regents of the University of
 
15
 *  California; see the COPYRIGHT file for full details.  Squid
 
16
 *  incorporates software developed and/or copyrighted by other
 
17
 *  sources; see the CREDITS file for full details.
 
18
 *
 
19
 *  This program is free software; you can redistribute it and/or modify
 
20
 *  it under the terms of the GNU General Public License as published by
 
21
 *  the Free Software Foundation; either version 2 of the License, or
 
22
 *  (at your option) any later version.
 
23
 *
 
24
 *  This program is distributed in the hope that it will be useful,
 
25
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
26
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
27
 *  GNU General Public License for more details.
 
28
 *
 
29
 *  You should have received a copy of the GNU General Public License
 
30
 *  along with this program; if not, write to the Free Software
 
31
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 
32
 *
 
33
 */
 
34
 
 
35
#define SQUID_HELPER 1
 
36
 
 
37
/**
 
38
 \defgroup pinger pinger
 
39
 \ingroup ExternalPrograms
 
40
 \par
 
41
 *   Although it would be possible for Squid to send and receive
 
42
 *   ICMP messages directly, we use an external process for
 
43
 *   two important reasons:
 
44
 *
 
45
 \li Because squid handles many filedescriptors simultaneously,
 
46
 *   we get much more accurate RTT measurements when ICMP is
 
47
 *   handled by a separate process.
 
48
 *
 
49
 \li Superuser privileges are required to send and receive ICMP.
 
50
 *   Rather than require Squid to be started as root, we prefer
 
51
 *   to have the smaller and simpler pinger program installed
 
52
 *   with setuid permissions.
 
53
 *
 
54
 \par
 
55
 *   If you want to use Squid's ICMP features (highly recommended!)
 
56
 *   When USE_ICMP is defined, Squid will send ICMP pings
 
57
 *   to origin server sites.
 
58
 *   This information is used in numerous ways:
 
59
 \li  - Sent in ICP replies so neighbor caches know how close
 
60
 *      you are to the source.
 
61
 \li  - For finding the closest instance of a URN.
 
62
 \li  - With the 'test_reachability' option.  Squid will return
 
63
 *      ICP_OP_MISS_NOFETCH for sites which it cannot ping.
 
64
 */
 
65
 
 
66
#include "squid.h"
 
67
#include "SquidTime.h"
 
68
 
 
69
#if USE_ICMP
 
70
 
 
71
#include "Icmp4.h"
 
72
#include "Icmp6.h"
 
73
#include "IcmpPinger.h"
 
74
 
 
75
#ifdef _SQUID_MSWIN_
 
76
 
 
77
#if HAVE_WINSOCK2_H
 
78
#include <winsock2.h>
 
79
#endif
 
80
#include <process.h>
 
81
#include "fde.h"
 
82
 
 
83
#define PINGER_TIMEOUT 5
 
84
 
 
85
/* windows uses the control socket for feedback to squid */
 
86
#define LINK_TO_SQUID squid_link
 
87
 
 
88
// windows still requires WSAFD but there are too many dependancy problems
 
89
// to just link to win32.cc where it is normally defined.
 
90
 
 
91
int
 
92
Win32__WSAFDIsSet(int fd, fd_set FAR * set)
 
93
{
 
94
    fde *F = &fd_table[fd];
 
95
    SOCKET s = F->win32.handle;
 
96
 
 
97
    return __WSAFDIsSet(s, set);
 
98
}
 
99
 
 
100
#else
 
101
 
 
102
#define PINGER_TIMEOUT 10
 
103
 
 
104
/* non-windows use STDOUT for feedback to squid */
 
105
#define LINK_TO_SQUID   1
 
106
 
 
107
#endif  /* _SQUID_MSWIN_ */
 
108
 
 
109
// ICMP Engines are declared global here so they can call each other easily.
 
110
IcmpPinger control;
 
111
Icmp4 icmp4;
 
112
#if USE_IPV6
 
113
Icmp6 icmp6;
 
114
#endif
 
115
 
 
116
int icmp_pkts_sent = 0;
 
117
 
 
118
/**
 
119
 \ingroup pinger
 
120
 \par This is the pinger external process.
 
121
 *
 
122
 \param argc Ignored.
 
123
 \param argv Ignored.
 
124
 */
 
125
int
 
126
main(int argc, char *argv[])
 
127
{
 
128
    fd_set R;
 
129
    int x;
 
130
    int max_fd = 0;
 
131
 
 
132
    struct timeval tv;
 
133
    const char *debug_args = "ALL,10";
 
134
    char *t;
 
135
    time_t last_check_time = 0;
 
136
 
 
137
    /*
 
138
     * cevans - do this first. It grabs a raw socket. After this we can
 
139
     * drop privs
 
140
     */
 
141
    int icmp4_worker = -1;
 
142
#if USE_IPV6
 
143
    int icmp6_worker = -1;
 
144
#endif
 
145
    int squid_link = -1;
 
146
 
 
147
    /** start by initializing the pinger debug cache.log-pinger. */
 
148
    if ((t = getenv("SQUID_DEBUG")))
 
149
        debug_args = xstrdup(t);
 
150
 
 
151
    getCurrentTime();
 
152
 
 
153
    _db_init(NULL, debug_args);
 
154
 
 
155
    debugs(42, 0, "pinger: Initialising ICMP pinger ...");
 
156
 
 
157
    icmp4_worker = icmp4.Open();
 
158
    if (icmp4_worker < 0) {
 
159
        debugs(42, 0, "pinger: Unable to start ICMP pinger.");
 
160
    }
 
161
    max_fd = max(max_fd, icmp4_worker);
 
162
 
 
163
#if USE_IPV6
 
164
    icmp6_worker = icmp6.Open();
 
165
    if (icmp6_worker <0 ) {
 
166
        debugs(42, 0, "pinger: Unable to start ICMPv6 pinger.");
 
167
    }
 
168
    max_fd = max(max_fd, icmp6_worker);
 
169
#endif
 
170
 
 
171
    /** abort if neither worker could open a socket. */
 
172
    if (icmp4_worker == -1) {
 
173
#if USE_IPV6
 
174
        if (icmp6_worker == -1)
 
175
#endif
 
176
        {
 
177
            debugs(42, 0, "FATAL: pinger: Unable to open any ICMP sockets.");
 
178
            exit(1);
 
179
        }
 
180
    }
 
181
 
 
182
    if ( (squid_link = control.Open()) < 0) {
 
183
        debugs(42, 0, "FATAL: pinger: Unable to setup Pinger control sockets.");
 
184
        icmp4.Close();
 
185
#if USE_IPV6
 
186
        icmp6.Close();
 
187
#endif
 
188
        exit(1); // fatal error if the control channel fails.
 
189
    }
 
190
    max_fd = max(max_fd, squid_link);
 
191
 
 
192
    setgid(getgid());
 
193
    setuid(getuid());
 
194
 
 
195
    last_check_time = squid_curtime;
 
196
 
 
197
    for (;;) {
 
198
        tv.tv_sec = PINGER_TIMEOUT;
 
199
        tv.tv_usec = 0;
 
200
        FD_ZERO(&R);
 
201
        if (icmp4_worker >= 0) {
 
202
            FD_SET(icmp4_worker, &R);
 
203
        }
 
204
#if USE_IPV6
 
205
 
 
206
        if (icmp6_worker >= 0) {
 
207
            FD_SET(icmp6_worker, &R);
 
208
        }
 
209
#endif
 
210
        FD_SET(squid_link, &R);
 
211
        x = select(10, &R, NULL, NULL, &tv);
 
212
        getCurrentTime();
 
213
 
 
214
        if (x < 0) {
 
215
            debugs(42, 0, HERE << " FATAL Shutdown. select()==" << x << ", ERR: " << xstrerror());
 
216
            control.Close();
 
217
            exit(1);
 
218
        }
 
219
 
 
220
        if (FD_ISSET(squid_link, &R)) {
 
221
            control.Recv();
 
222
        }
 
223
 
 
224
#if USE_IPV6
 
225
        if (icmp6_worker >= 0 && FD_ISSET(icmp6_worker, &R)) {
 
226
            icmp6.Recv();
 
227
        }
 
228
#endif
 
229
 
 
230
        if (icmp4_worker >= 0 && FD_ISSET(icmp4_worker, &R)) {
 
231
            icmp4.Recv();
 
232
        }
 
233
 
 
234
        if (PINGER_TIMEOUT + last_check_time < squid_curtime) {
 
235
            if (send(LINK_TO_SQUID, &tv, 0, 0) < 0) {
 
236
                debugs(42, 0, "pinger: Closing. No requests in last " << PINGER_TIMEOUT << " seconds.");
 
237
                control.Close();
 
238
                exit(1);
 
239
            }
 
240
 
 
241
            last_check_time = squid_curtime;
 
242
        }
 
243
    }
 
244
 
 
245
    /* NOTREACHED */
 
246
    return 0;
 
247
}
 
248
 
 
249
#else
 
250
#include <stdio.h>
 
251
int
 
252
main(int argc, char *argv[])
 
253
{
 
254
    fprintf(stderr, "%s: ICMP support not compiled in.\n", argv[0]);
 
255
    return 1;
 
256
}
 
257
 
 
258
#endif /* USE_ICMP */