--------------------- PatchSet 4557 Date: 2007/05/15 22:56:56 Author: amosjeffries Branch: squid3-ipv6 Tag: (none) Log: Roll IPAddress into NAT/IPInterception. Members: src/IPInterception.cc:1.4.4.4->1.4.4.5 src/IPInterception.h:1.2.10.2->1.2.10.3 src/client_side.cc:1.68.2.22->1.68.2.23 Index: squid3/src/IPInterception.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/IPInterception.cc,v retrieving revision 1.4.4.4 retrieving revision 1.4.4.5 diff -u -r1.4.4.4 -r1.4.4.5 --- squid3/src/IPInterception.cc 29 Apr 2007 03:01:36 -0000 1.4.4.4 +++ squid3/src/IPInterception.cc 15 May 2007 22:56:56 -0000 1.4.4.5 @@ -1,6 +1,6 @@ /* - * $Id: IPInterception.cc,v 1.4.4.4 2007/04/29 03:01:36 amosjeffries Exp $ + * $Id: IPInterception.cc,v 1.4.4.5 2007/05/15 22:56:56 amosjeffries Exp $ * * DEBUG: section 89 NAT / IP Interception * AUTHOR: Robert Collins @@ -87,8 +87,11 @@ #if IPF_TRANSPARENT int -clientNatLookup(int fd, struct sockaddr_in me, struct sockaddr_in peer, struct sockaddr_in *dst) +clientNatLookup(int fd, const IPAddress &me, const IPAddress &peer, IPAddress &dst) { + dst = me; + if( !me.IsIPv4() ) return -1; + if( !peer.IsIPv4() ) return -1; #if defined(IPFILTER_VERSION) && (IPFILTER_VERSION >= 4000027) @@ -113,10 +116,10 @@ obj.ipfo_offset = 0; #endif - natLookup.nl_inport = me.sin_port; - natLookup.nl_outport = peer.sin_port; - natLookup.nl_inip = me.sin_addr; - natLookup.nl_outip = peer.sin_addr; + natLookup.nl_inport = htons(me.GetPort()); + natLookup.nl_outport = htons(peer.GetPort()); + me.GetInAddr(natLookup.nl_inip); + peer.GetInAddr(natLookup.nl_outip); natLookup.nl_flags = IPN_TCP; if (natfd < 0) @@ -182,12 +185,12 @@ return -1; } else { - if (me.sin_addr.s_addr != natLookup.nl_realip.s_addr) - dst->sin_family = AF_INET; + if (me != natLookup.nl_realip) { + dst = natLookup.nl_realip; - dst->sin_port = natLookup.nl_realport; - - dst->sin_addr = natLookup.nl_realip; + dst.SetPort(ntohs(natLookup.nl_realport)); + } + // else. we already copied it. return 0; } @@ -196,13 +199,19 @@ #elif LINUX_NETFILTER int -clientNatLookup(int fd, struct sockaddr_in me, struct sockaddr_in peer, struct sockaddr_in *dst) +clientNatLookup(int fd, const IPAddress &me, const IPAddress &peer, IPAddress &dst) { + dst = me; + if( !me.IsIPv4() ) return -1; + if( !peer.IsIPv4() ) return -1; + static time_t last_reported = 0; - socklen_t sock_sz = sizeof(*dst); - memcpy(dst, &me, sizeof(*dst)); + socklen_t sock_sz = sizeof(struct sockaddr_in); + struct sockaddr_in lookup; + + dst.GetSockAddr(lookup); - if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, dst, &sock_sz) != 0) + if (getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, &lookup, &sock_sz) != 0) { if (squid_curtime - last_reported > 60) { debugs(89, 1, "clientNatLookup: NF getsockopt(SO_ORIGINAL_DST) failed: " << xstrerror()); @@ -211,10 +220,11 @@ return -1; } + dst = lookup; - debugs(89, 5, "clientNatLookup: addr = " << inet_ntoa(dst->sin_addr) << ""); + debugs(89, 5, "clientNatLookup: addr = " << dst << ""); - if (me.sin_addr.s_addr != dst->sin_addr.s_addr) + if (me != dst) return 0; else return -1; @@ -223,13 +233,16 @@ #elif PF_TRANSPARENT int -clientNatLookup(int fd, struct sockaddr_in me, struct sockaddr_in peer, struct sockaddr_in *dst) +clientNatLookup(int fd, const IPAddress &me, const IPAddress &peer, IPAddress dst) { struct pfioc_natlook nl; static int pffd = -1; static time_t last_reported = 0; + if( !me.IsIPv4() ) return -1; + if( !peer.IsIPv4() ) return -1; + if (pffd < 0) pffd = open("/dev/pf", O_RDWR); @@ -244,13 +257,15 @@ } - memset(dst, 0, sizeof(*dst)); + dst.SetEmpty(); memset(&nl, 0, sizeof(struct pfioc_natlook)); - nl.saddr.v4.s_addr = peer.sin_addr.s_addr; - nl.sport = peer.sin_port; - nl.daddr.v4.s_addr = me.sin_addr.s_addr; - nl.dport = me.sin_port; + peer.GetInAddr(nl.saddr.v4); + nl.sport = htons(peer.GetPort()); + + me.GetINAddr(nl.daddr.v4); + nl.dport = htons(me.GetPort()); + nl.af = AF_INET; nl.proto = IPPROTO_TCP; nl.direction = PF_OUT; @@ -270,10 +285,9 @@ return -1; } else { - int natted = me.sin_addr.s_addr != nl.rdaddr.v4.s_addr; - dst->sin_family = AF_INET; - dst->sin_port = nl.rdport; - dst->sin_addr = nl.rdaddr.v4; + int natted = (me != nl.rdaddr.v4); + dst = nl.rdaddr.v4; + dst.SetPort(ntohs(nl.rdport)); if (natted) return 0; @@ -285,8 +299,9 @@ #else int -clientNatLookup(int fd, struct sockaddr_in me, struct sockaddr_in peer, struct sockaddr_in *dst) +clientNatLookup(int fd, const IPAddress &me, const IPAddress &peer, IPAddress &dst) { + dst = me; debugs(89, 1, "WARNING: transparent proxying not supported"); return -1; } Index: squid3/src/IPInterception.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/IPInterception.h,v retrieving revision 1.2.10.2 retrieving revision 1.2.10.3 diff -u -r1.2.10.2 -r1.2.10.3 --- squid3/src/IPInterception.h 22 Apr 2007 07:48:21 -0000 1.2.10.2 +++ squid3/src/IPInterception.h 15 May 2007 22:56:56 -0000 1.2.10.3 @@ -1,6 +1,6 @@ /* - * $Id: IPInterception.h,v 1.2.10.2 2007/04/22 07:48:21 amosjeffries Exp $ + * $Id: IPInterception.h,v 1.2.10.3 2007/05/15 22:56:56 amosjeffries Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -34,8 +34,9 @@ #ifndef SQUID_IPINTERCEPTION_H #define SQUID_IPINTERCEPTION_H -SQUIDCEXTERN int +#include "IPAddress.h" -clientNatLookup(int fd, struct sockaddr_in me, struct sockaddr_in peer, struct sockaddr_in *dst); +SQUIDCEXTERN int +clientNatLookup(int fd, const IPAddress &me, const IPAddress &peer, IPAddress &dst); #endif /* SQUID_IPINTERCEPTION_H */ Index: squid3/src/client_side.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/client_side.cc,v retrieving revision 1.68.2.22 retrieving revision 1.68.2.23 diff -u -r1.68.2.22 -r1.68.2.23 --- squid3/src/client_side.cc 15 May 2007 13:42:41 -0000 1.68.2.22 +++ squid3/src/client_side.cc 15 May 2007 22:56:56 -0000 1.68.2.23 @@ -1,6 +1,6 @@ /* - * $Id: client_side.cc,v 1.68.2.22 2007/05/15 13:42:41 amosjeffries Exp $ + * $Id: client_side.cc,v 1.68.2.23 2007/05/15 22:56:56 amosjeffries Exp $ * * DEBUG: section 33 Client-side Routines * AUTHOR: Duane Wessels @@ -2675,10 +2675,11 @@ if (port->transparent) { - struct sockaddr_in dst; + IPAddress dst; - if (clientNatLookup(fd, *me, *peer, &dst) == 0) { - result->me = dst; /* XXX This should be moved to another field */ +/* FIXME INET6 : drop temp conversion */ IPAddress tmpme(*me); IPAddress tmppeer(*peer); + if (clientNatLookup(fd, tmpme, tmppeer, dst) == 0) { + dst.GetSockAddr(result->me); /* XXX This should be moved to another field */ result->transparent(true); } }