This is the 5th of some splitted native Windows patches grouped by functionality.
Native Windows port enhancements:
- Added native Windows and Cygwin support to pinger.cc
- Introduced the use of IPPROTO_TCP and IPPROTO_UDP defines instead of '0' on comm_open, needed by Winsocket. See this old squid-dev thread about:
http://www.squid-cache.org/mail-archive/squid-dev/200108/0162.html
Regards
Guido
-
=======================================================
Serassio Guido
Via Albenga, 11/4 10134 - Torino - ITALY
E-mail: [EMAIL PROTECTED]
WWW: http://www.serassio.it
Index: pinger.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/pinger.cc,v
retrieving revision 1.5
diff -u -p -r1.5 pinger.cc
--- pinger.cc 18 Nov 2002 03:12:57 -0000 1.5
+++ pinger.cc 5 Feb 2003 08:53:22 -0000
@@ -37,19 +37,92 @@
#if USE_ICMP
+/* Native Windows port doesn't have netinet support, so we emulate it.
+ At this time, Cygwin lacks icmp support in its include files, so we need
+ to use the native Windows port definitions.
+ */
+
+#if !defined(_SQUID_MSWIN_) && !defined(_SQUID_CYGWIN_)
+
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
+#define PINGER_TIMEOUT 10
+
+static int socket_from_squid = 0;
+static int socket_to_squid = 1;
+
+#else /* _SQUID_MSWIN_ or _SQUID_CYGWIN_ */
+
+#ifdef _SQUID_MSWIN_
+
+#include <winsock2.h>
+#include <process.h>
+
+#define PINGER_TIMEOUT 5
+
+static int socket_to_squid = -1;
+#define socket_from_squid socket_to_squid
+
+#else /* _SQUID_CYGWIN_ */
+
+#include <netinet/in_systm.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
+#include <netinet/ip_icmp.h>
+
+#define PINGER_TIMEOUT 10
+
+static int socket_from_squid = 0;
+static int socket_to_squid = 1;
+
+#endif
+
+#define ICMP_ECHO 8
+#define ICMP_ECHOREPLY 0
+
+typedef struct iphdr
+{
+ u_int8_t ip_vhl:4; /* Length of the header in dwords */
+ u_int8_t version:4; /* Version of IP */
+ u_int8_t tos; /* Type of service */
+ u_int16_t total_len; /* Length of the packet in dwords */
+ u_int16_t ident; /* unique identifier */
+ u_int16_t flags; /* Flags */
+ u_int8_t ip_ttl; /* Time to live */
+ u_int8_t proto; /* Protocol number (TCP, UDP etc) */
+ u_int16_t checksum; /* IP checksum */
+ u_int32_t source_ip;
+ u_int32_t dest_ip;
+}
+iphdr;
+
+/* ICMP header */
+typedef struct icmphdr
+{
+ u_int8_t icmp_type; /* ICMP packet type */
+ u_int8_t icmp_code; /* Type sub code */
+ u_int16_t icmp_cksum;
+ u_int16_t icmp_id;
+ u_int16_t icmp_seq;
+ u_int32_t timestamp; /* not part of ICMP, but we need it */
+}
+icmphdr;
+
+#endif /* _SQUID_MSWIN_ */
+
#ifndef _SQUID_LINUX_
#ifndef _SQUID_CYGWIN_
+#ifndef _SQUID_MSWIN_
#define icmphdr icmp
#define iphdr ip
#endif
#endif
+#endif
-#if defined (_SQUID_LINUX_) || defined (_SQUID_CYGWIN_)
+#if defined (_SQUID_LINUX_)
#ifdef icmp_id
#undef icmp_id
#endif
@@ -118,11 +191,45 @@ static void pingerRecv(void);
static void pingerLog(struct icmphdr *, struct in_addr, int, int);
static int ipHops(int ttl);
static void pingerSendtoSquid(pingerReplyData * preply);
+static void pingerOpen(void);
+static void pingerClose(void);
void
pingerOpen(void)
{
struct protoent *proto = NULL;
+#ifdef _SQUID_MSWIN_
+ WSADATA wsaData;
+ WSAPROTOCOL_INFO wpi;
+ char buf[sizeof(wpi)];
+ int x;
+ struct sockaddr_in PS;
+
+ WSAStartup(2, &wsaData);
+
+ getCurrentTime();
+ _db_init(NULL, "ALL,1");
+ setmode(0, O_BINARY);
+ setmode(1, O_BINARY);
+ x = read(0, buf, sizeof(wpi));
+ if (x < (int)sizeof(wpi)) {
+ getCurrentTime();
+ debug(42, 0) ("pingerOpen: read: FD 0: %s\n", xstrerror());
+ write(1, "ERR\n", 4);
+ exit(1);
+ }
+ xmemcpy(&wpi, buf, sizeof(wpi));
+
+ write(1, "OK\n", 3);
+ x = read(0, buf, sizeof(PS));
+ if (x < (int)sizeof(PS)) {
+ getCurrentTime();
+ debug(42, 0) ("pingerOpen: read: FD 0: %s\n", xstrerror());
+ write(1, "ERR\n", 4);
+ exit(1);
+ }
+ xmemcpy(&PS, buf, sizeof(PS));
+#endif
if ((proto = getprotobyname("icmp")) == 0) {
debug(42, 0) ("pingerOpen: unknown protocol: icmp\n");
exit(1);
@@ -134,12 +241,50 @@ pingerOpen(void)
}
icmp_ident = getpid() & 0xffff;
debug(42, 0) ("pinger: ICMP socket opened\n");
+#ifdef _SQUID_MSWIN_
+ socket_to_squid =
+ WSASocket(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
+ &wpi, 0, 0);
+ if (socket_to_squid == -1) {
+ getCurrentTime();
+ debug(42, 0) ("pingerOpen: WSASocket: %s\n", xstrerror());
+ write(1, "ERR\n", 4);
+ exit(1);
+ }
+ x = connect(socket_to_squid, (struct sockaddr *) &PS, sizeof(PS));
+ if (SOCKET_ERROR == x) {
+ getCurrentTime();
+ debug(42, 0) ("pingerOpen: connect: %s\n", xstrerror());
+ write(1, "ERR\n", 4);
+ exit(1);
+ }
+ write(1, "OK\n", 3);
+ memset(buf, 0, sizeof(buf));
+ x = recv(socket_to_squid, buf, sizeof(buf), 0);
+ if (x < 3) {
+ debug(42, 0) ("icmpOpen: recv: %s\n", xstrerror());
+ exit(1);
+ }
+ x = send(socket_to_squid, buf, strlen(buf), 0);
+ if (x < 3 || strncmp("OK\n", buf, 3)) {
+ debug(42, 0) ("icmpOpen: recv: %s\n", xstrerror());
+ exit(1);
+ }
+
+ getCurrentTime();
+ debug(42, 0) ("pinger: Squid socket opened\n");
+#endif
}
void
pingerClose(void)
{
close(icmp_sock);
+#ifdef _SQUID_MSWIN_
+ shutdown(socket_to_squid, SD_BOTH);
+ close(socket_to_squid);
+ socket_to_squid = -1;
+#endif
icmp_sock = -1;
icmp_ident = 0;
}
@@ -267,7 +412,7 @@ in_cksum(unsigned short *ptr, int size)
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
- answer = ~sum;
+ answer = (unsigned short) ~sum;
return (answer);
}
@@ -307,7 +452,7 @@ pingerReadRequest(void)
int n;
int guess_size;
memset(&pecho, '\0', sizeof(pecho));
- n = recv(0, (char *) &pecho, sizeof(pecho), 0);
+ n = recv(socket_from_squid, (char *) &pecho, sizeof(pecho), 0);
if (n < 0)
return n;
if (0 == n) {
@@ -334,8 +479,9 @@ static void
pingerSendtoSquid(pingerReplyData * preply)
{
int len = sizeof(pingerReplyData) - MAX_PKT_SZ + preply->psize;
- if (send(1, (char *) preply, len, 0) < 0) {
+ if (send(socket_to_squid, (char *) preply, len, 0) < 0) {
debug(50, 0) ("pinger: send: %s\n", xstrerror());
+ pingerClose();
exit(1);
}
}
@@ -376,29 +522,35 @@ main(int argc, char *argv[])
_db_init(NULL, debug_args);
for (;;) {
- tv.tv_sec = 10;
+ tv.tv_sec = PINGER_TIMEOUT;
tv.tv_usec = 0;
FD_ZERO(&R);
- FD_SET(0, &R);
+ FD_SET(socket_from_squid, &R);
FD_SET(icmp_sock, &R);
x = select(icmp_sock + 1, &R, NULL, NULL, &tv);
getCurrentTime();
- if (x < 0)
+ if (x < 0) {
+ pingerClose();
exit(1);
- if (FD_ISSET(0, &R))
+ }
+ if (FD_ISSET(socket_from_squid, &R))
if (pingerReadRequest() < 0) {
debug(42, 0) ("Pinger exiting.\n");
+ pingerClose();
exit(1);
}
if (FD_ISSET(icmp_sock, &R))
pingerRecv();
- if (10 + last_check_time < squid_curtime) {
- if (send(1, (char *) &tv, 0, 0) < 0)
+ if (PINGER_TIMEOUT + last_check_time < squid_curtime) {
+ if (send(socket_to_squid, (char *) &tv, 0, 0) < 0) {
+ pingerClose();
exit(1);
+ }
last_check_time = squid_curtime;
}
}
/* NOTREACHED */
+ return 0;
}
#else
Index: access_log.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/access_log.cc,v
retrieving revision 1.4
diff -u -p -r1.4 access_log.cc
--- access_log.cc 6 Dec 2002 23:58:51 -0000 1.4
+++ access_log.cc 5 Feb 2003 08:47:52 -0000
@@ -406,7 +406,7 @@ accessLogInit(void)
mcast_miss_to.sin_port = htons(Config.mcast_miss.port);
mcast_miss_to.sin_addr.s_addr = Config.mcast_miss.addr.s_addr;
mcast_miss_fd = comm_open(SOCK_DGRAM,
- 0,
+ IPPROTO_UDP,
Config.Addrs.udp_incoming,
Config.mcast_miss.port,
COMM_NONBLOCKING,
Index: client_side.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/client_side.cc,v
retrieving revision 1.18
diff -u -p -r1.18 client_side.cc
--- client_side.cc 29 Jan 2003 03:13:28 -0000 1.18
+++ client_side.cc 5 Feb 2003 08:47:53 -0000
@@ -2388,7 +2388,7 @@ clientHttpConnectionsOpen(void)
}
enter_suid();
fd = comm_open(SOCK_STREAM,
- 0,
+ IPPROTO_TCP,
s->s.sin_addr,
ntohs(s->s.sin_port), COMM_NONBLOCKING, "HTTP Socket");
leave_suid();
@@ -2422,7 +2422,7 @@ clientHttpsConnectionsOpen(void)
}
enter_suid();
fd = comm_open(SOCK_STREAM,
- 0,
+ IPPROTO_TCP,
s->s.sin_addr,
ntohs(s->s.sin_port), COMM_NONBLOCKING, "HTTPS Socket");
leave_suid();
Index: dns_internal.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/dns_internal.cc,v
retrieving revision 1.5
diff -u -p -r1.5 dns_internal.cc
--- dns_internal.cc 18 Jan 2003 03:14:47 -0000 1.5
+++ dns_internal.cc 5 Feb 2003 08:47:54 -0000
@@ -626,7 +626,7 @@ idnsInit(void)
else
addr = Config.Addrs.udp_incoming;
DnsSocket = comm_open(SOCK_DGRAM,
- 0,
+ IPPROTO_UDP,
addr,
0,
COMM_NONBLOCKING,
Index: forward.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/forward.cc,v
retrieving revision 1.9
diff -u -p -r1.9 forward.cc
--- forward.cc 28 Jan 2003 03:13:08 -0000 1.9
+++ forward.cc 5 Feb 2003 08:47:54 -0000
@@ -457,7 +457,7 @@ fwdConnectStart(void *data)
debug(17, 3) ("fwdConnectStart: got addr %s, tos %d\n",
inet_ntoa(outgoing), tos);
fd = comm_openex(SOCK_STREAM,
- 0,
+ IPPROTO_TCP,
outgoing,
0,
COMM_NONBLOCKING,
Index: ftp.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/ftp.cc,v
retrieving revision 1.8
diff -u -p -r1.8 ftp.cc
--- ftp.cc 2 Feb 2003 03:12:31 -0000 1.8
+++ ftp.cc 5 Feb 2003 08:47:56 -0000
@@ -1695,7 +1695,7 @@ ftpSendPasv(FtpStateData * ftpState)
}
/* Open data channel with the same local address as control channel */
fd = comm_open(SOCK_STREAM,
- 0,
+ IPPROTO_TCP,
addr.sin_addr,
0,
COMM_NONBLOCKING,
@@ -1843,7 +1843,7 @@ ftpOpenListenSocket(FtpStateData * ftpSt
port = ntohs(addr.sin_port);
}
fd = comm_open(SOCK_STREAM,
- 0,
+ IPPROTO_TCP,
addr.sin_addr,
port,
COMM_NONBLOCKING | (fallback ? COMM_REUSEADDR : 0),
Index: htcp.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/htcp.cc,v
retrieving revision 1.8
diff -u -p -r1.8 htcp.cc
--- htcp.cc 23 Jan 2003 03:14:42 -0000 1.8
+++ htcp.cc 5 Feb 2003 08:47:56 -0000
@@ -919,7 +919,7 @@ htcpInit(void)
}
enter_suid();
htcpInSocket = comm_open(SOCK_DGRAM,
- 0,
+ IPPROTO_UDP,
Config.Addrs.udp_incoming,
Config.Port.htcp,
COMM_NONBLOCKING,
@@ -933,7 +933,7 @@ htcpInit(void)
if (Config.Addrs.udp_outgoing.s_addr != no_addr.s_addr) {
enter_suid();
htcpOutSocket = comm_open(SOCK_DGRAM,
- 0,
+ IPPROTO_UDP,
Config.Addrs.udp_outgoing,
Config.Port.htcp,
COMM_NONBLOCKING,
Index: icp_v2.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/icp_v2.cc,v
retrieving revision 1.8
diff -u -p -r1.8 icp_v2.cc
--- icp_v2.cc 28 Jan 2003 03:13:09 -0000 1.8
+++ icp_v2.cc 5 Feb 2003 08:47:57 -0000
@@ -554,7 +554,7 @@ icpConnectionsOpen(void)
return;
enter_suid();
theInIcpConnection = comm_open(SOCK_DGRAM,
- 0,
+ IPPROTO_UDP,
Config.Addrs.udp_incoming,
port,
COMM_NONBLOCKING,
@@ -575,7 +575,7 @@ icpConnectionsOpen(void)
if ((addr = Config.Addrs.udp_outgoing).s_addr != no_addr.s_addr) {
enter_suid();
theOutIcpConnection = comm_open(SOCK_DGRAM,
- 0,
+ IPPROTO_UDP,
addr,
port,
COMM_NONBLOCKING,
Index: ident.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/ident.cc,v
retrieving revision 1.6
diff -u -p -r1.6 ident.cc
--- ident.cc 18 Nov 2002 03:12:57 -0000 1.6
+++ ident.cc 5 Feb 2003 08:47:57 -0000
@@ -202,7 +202,7 @@ identStart(struct sockaddr_in *me, struc
return;
}
fd = comm_open(SOCK_STREAM,
- 0,
+ IPPROTO_TCP,
me->sin_addr,
0,
COMM_NONBLOCKING,
Index: neighbors.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/neighbors.cc,v
retrieving revision 1.7
diff -u -p -r1.7 neighbors.cc
--- neighbors.cc 28 Jan 2003 03:13:09 -0000 1.7
+++ neighbors.cc 5 Feb 2003 08:47:57 -0000
@@ -1121,7 +1121,7 @@ peerProbeConnect(peer * p)
return; /* probe already running */
if (squid_curtime - p->stats.last_connect_probe < Config.Timeout.connect)
return; /* don't probe to often */
- fd = comm_open(SOCK_STREAM, 0, getOutgoingAddr(NULL),
+ fd = comm_open(SOCK_STREAM, IPPROTO_TCP, getOutgoingAddr(NULL),
0, COMM_NONBLOCKING, p->host);
if (fd < 0)
return;
Index: snmp_core.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/snmp_core.cc,v
retrieving revision 1.6
diff -u -p -r1.6 snmp_core.cc
--- snmp_core.cc 28 Jan 2003 03:13:09 -0000 1.6
+++ snmp_core.cc 5 Feb 2003 08:47:58 -0000
@@ -357,7 +357,7 @@ snmpConnectionOpen(void)
if ((port = Config.Port.snmp) > (u_short) 0) {
enter_suid();
theInSnmpConnection = comm_open(SOCK_DGRAM,
- 0,
+ IPPROTO_UDP,
Config.Addrs.snmp_incoming,
port,
COMM_NONBLOCKING,
@@ -371,7 +371,7 @@ snmpConnectionOpen(void)
if (Config.Addrs.snmp_outgoing.s_addr != no_addr.s_addr) {
enter_suid();
theOutSnmpConnection = comm_open(SOCK_DGRAM,
- 0,
+ IPPROTO_UDP,
Config.Addrs.snmp_outgoing,
port,
COMM_NONBLOCKING,
Index: squid.h
===================================================================
RCS file: /cvsroot/squid/squid3/src/squid.h,v
retrieving revision 1.5
diff -u -p -r1.5 squid.h
--- squid.h 28 Dec 2002 03:12:34 -0000 1.5
+++ squid.h 5 Feb 2003 08:47:58 -0000
@@ -417,4 +417,12 @@ extern "C" {
#define FD_READ_METHOD(fd, buf, len) (*fd_table[fd].read_method)(fd, buf, len)
#define FD_WRITE_METHOD(fd, buf, len) (*fd_table[fd].write_method)(fd, buf, len)
+#ifndef IPPROTO_UDP
+#define IPPROTO_UDP 0
+#endif
+
+#ifndef IPPROTO_TCP
+#define IPPROTO_TCP 0
+#endif
+
#endif /* SQUID_H */
Index: ssl.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/ssl.cc,v
retrieving revision 1.8
diff -u -p -r1.8 ssl.cc
--- ssl.cc 28 Jan 2003 03:13:09 -0000 1.8
+++ ssl.cc 5 Feb 2003 08:47:58 -0000
@@ -465,7 +465,7 @@ sslStart(clientHttpRequest * http, size_
statCounter.server.other.requests++;
/* Create socket. */
sock = comm_openex(SOCK_STREAM,
- 0,
+ IPPROTO_TCP,
getOutgoingAddr(request),
0,
COMM_NONBLOCKING,
Index: wccp.cc
===================================================================
RCS file: /cvsroot/squid/squid3/src/wccp.cc,v
retrieving revision 1.5
diff -u -p -r1.5 wccp.cc
--- wccp.cc 18 Nov 2002 03:12:57 -0000 1.5
+++ wccp.cc 5 Feb 2003 08:47:59 -0000
@@ -129,7 +129,7 @@ wccpConnectionOpen(void)
return;
}
theInWccpConnection = comm_open(SOCK_DGRAM,
- 0,
+ IPPROTO_UDP,
Config.Wccp.incoming,
port,
COMM_NONBLOCKING,
@@ -145,7 +145,7 @@ wccpConnectionOpen(void)
(int) port, theInWccpConnection);
if (Config.Wccp.outgoing.s_addr != no_addr.s_addr) {
theOutWccpConnection = comm_open(SOCK_DGRAM,
- 0,
+ IPPROTO_UDP,
Config.Wccp.outgoing,
port,
COMM_NONBLOCKING,
