]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ICMPPinger.cc
Summary: Synced with libecap, adopted pass-all-changes-through transactions
[thirdparty/squid.git] / src / ICMPPinger.cc
1 /*
2 * $Id: ICMPPinger.cc,v 1.1 2007/12/14 23:11:45 amosjeffries Exp $
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 #include "squid.h"
38
39 #if USE_ICMP
40
41 #include "SquidTime.h"
42 #include "ICMPPinger.h"
43 #include "ICMPv4.h"
44 #include "ICMPv6.h"
45 #include "Debug.h"
46
47 ICMPPinger::ICMPPinger() : ICMP()
48 {
49 // these start invalid. Setup properly in Open()
50 socket_from_squid = -1;
51 socket_to_squid = -1;
52 }
53
54 ICMPPinger::~ICMPPinger()
55 {
56 Close();
57 }
58
59 int
60 ICMPPinger::Open(void)
61 {
62 #ifdef _SQUID_MSWIN_
63
64 WSADATA wsaData;
65 WSAPROTOCOL_INFO wpi;
66 char buf[sizeof(wpi)];
67 int x;
68
69 struct sockaddr_in PS;
70
71 WSAStartup(2, &wsaData);
72
73 getCurrentTime();
74 _db_init(NULL, "ALL,1");
75 setmode(0, O_BINARY);
76 setmode(1, O_BINARY);
77 x = read(0, buf, sizeof(wpi));
78
79 if (x < (int)sizeof(wpi)) {
80 getCurrentTime();
81 debugs(42, 0, HERE << "read: FD 0: " << xstrerror());
82 write(1, "ERR\n", 4);
83 return -1;
84 }
85
86 xmemcpy(&wpi, buf, sizeof(wpi));
87
88 write(1, "OK\n", 3);
89 x = read(0, buf, sizeof(PS));
90
91 if (x < (int)sizeof(PS)) {
92 getCurrentTime();
93 debugs(42, 0, HERE << "read: FD 0: " << xstrerror());
94 write(1, "ERR\n", 4);
95 return -1;
96 }
97
98 xmemcpy(&PS, buf, sizeof(PS));
99
100 icmp_sock = WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, &wpi, 0, 0);
101
102 if (icmp_sock == -1) {
103 getCurrentTime();
104 debugs(42, 0, HERE << "WSASocket: " << xstrerror());
105 write(1, "ERR\n", 4);
106 return -1;
107 }
108
109 x = connect(icmp_sock, (struct sockaddr *) &PS, sizeof(PS));
110
111 if (SOCKET_ERROR == x) {
112 getCurrentTime();
113 debugs(42, 0, HERE << "connect: " << xstrerror());
114 write(1, "ERR\n", 4);
115 return -1;
116 }
117
118 write(1, "OK\n", 3);
119 memset(buf, 0, sizeof(buf));
120 x = recv(icmp_sock, (void *) buf, sizeof(buf), 0);
121
122 if (x < 3) {
123 debugs(42, 0, HERE << "recv: " << xstrerror());
124 return -1;
125 }
126
127 x = send(icmp_sock, (const void *) buf, strlen(buf), 0);
128
129 if (x < 3 || strncmp("OK\n", buf, 3)) {
130 debugs(42, 0, HERE << "recv: " << xstrerror());
131 return -1;
132 }
133
134 getCurrentTime();
135 debugs(42, 1, "pinger: Squid socket opened");
136
137 /* windows uses a socket stream as a dual-direction channel */
138 socket_to_squid = icmp_sock;
139 socket_from_squid = icmp_sock;
140
141 return icmp_sock;
142
143 #else /* !_SQUID_MSWIN_ */
144
145 /* non-windows apps use stdin/out pipes as the squid channel(s) */
146 socket_from_squid = 0; // use STDIN macro ??
147 socket_to_squid = 1; // use STDOUT macro ??
148 return socket_to_squid;
149 #endif
150 }
151
152 void
153 ICMPPinger::Close(void)
154 {
155 #ifdef _SQUID_MSWIN_
156
157 shutdown(icmp_sock, SD_BOTH);
158 close(icmp_sock);
159 icmp_sock = -1;
160 #endif
161
162 /* also shutdown the helper engines */
163 icmp4.Close();
164 #if USE_IPV6
165 icmp6.Close();
166 #endif
167 }
168
169 void
170 ICMPPinger::Recv(void)
171 {
172 static pingerEchoData pecho;
173 int n;
174 int guess_size;
175
176 memset(&pecho, '\0', sizeof(pecho));
177 n = recv(socket_from_squid, &pecho, sizeof(pecho), 0);
178
179 if (n < 0) {
180 debugs(42, 1, "Pinger exiting.");
181 Close();
182 exit(1);
183 }
184
185 if (0 == n) {
186 /* EOF indicator */
187 debugs(42, 0, HERE << "EOF encountered. Pinger exiting.\n");
188 errno = 0;
189 Close();
190 exit(1);
191 }
192
193 guess_size = n - (sizeof(pingerEchoData) - PINGER_PAYLOAD_SZ);
194
195 if (guess_size != pecho.psize) {
196 debugs(42, 2, HERE << "size mismatch, guess=" << guess_size << ", psize=" << pecho.psize);
197 /* don't process this message, but keep running */
198 return;
199 }
200
201 #if USE_IPV6
202 /* pass request for ICMPv6 handing */
203 if(pecho.to.IsIPv6()) {
204 debugs(42, 2, HERE << " Pass " << pecho.to << " off to ICMPv6 module.");
205 icmp6.SendEcho(pecho.to,
206 pecho.opcode,
207 pecho.payload,
208 pecho.psize);
209 }
210 #endif
211
212 /* pass the packet for ICMP handling */
213 else if(pecho.to.IsIPv4()) {
214 debugs(42, 2, HERE << " Pass " << pecho.to << " off to ICMPv4 module.");
215 icmp4.SendEcho(pecho.to,
216 pecho.opcode,
217 pecho.payload,
218 pecho.psize);
219 }
220 else {
221 debugs(42, 1, HERE << " IP has unknown Type. " << pecho.to );
222 }
223 }
224
225 void
226 ICMPPinger::SendResult(pingerReplyData &preply, int len)
227 {
228 debugs(42, 2, HERE << "return result to squid. len=" << len);
229
230 if (send(socket_to_squid, &preply, len, 0) < 0) {
231 debugs(42, 0, "pinger: FATAL error on send: " << xstrerror());
232 Close();
233 exit(1);
234 }
235 }
236
237 #endif /* USE_ICMP */