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