--------------------- PatchSet 4101 Date: 2007/03/18 02:56:32 Author: amosjeffries Branch: squid3-ipv6 Tag: (none) Log: FIX: Segmentation Fault when acl_ip_data allocated. FIX: Segmentation Fault when CIDR mask applied to stored address. Members: src/IPAddress.cc:1.1.2.10->1.1.2.11 Index: squid3/src/IPAddress.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Attic/IPAddress.cc,v retrieving revision 1.1.2.10 retrieving revision 1.1.2.11 diff -u -r1.1.2.10 -r1.1.2.11 --- squid3/src/IPAddress.cc 12 Mar 2007 11:04:46 -0000 1.1.2.10 +++ squid3/src/IPAddress.cc 18 Mar 2007 02:56:32 -0000 1.1.2.11 @@ -1,5 +1,5 @@ /* - * $Id: IPAddress.cc,v 1.1.2.10 2007/03/12 11:04:46 amosjeffries Exp $ + * $Id: IPAddress.cc,v 1.1.2.11 2007/03/18 02:56:32 amosjeffries Exp $ */ #include "IPAddress.h" @@ -48,17 +48,21 @@ IPAddress::IPAddress() { memset(this,0,sizeof(IPAddress)); +#ifdef INET6 m_Type = IPv64; +#else + m_Type = IPv4; +#endif } IPAddress::~IPAddress() { memset(this,0,sizeof(IPAddress)); } -bool IPAddress::ApplyMask(const IPAddress &mask_addr) +bool IPAddress::ApplyMask(IPAddress const &mask_addr) { uint32_t *p1 = (uint32_t*)(&m_SocketAddr.sin6_addr); - const uint32_t *p2 = (const uint32_t*)(&mask_addr.m_SocketAddr.sin6_addr); + uint32_t const *p2 = (uint32_t const *)(&mask_addr.m_SocketAddr.sin6_addr); unsigned int blen = sizeof(m_SocketAddr.sin6_addr)/sizeof(uint32_t); for (unsigned int i = 0; i < blen; i++) { @@ -66,32 +70,28 @@ } return true; } -bool IPAddress::ApplyMask(const unsigned int cidr, IPAddressType mtype) +bool IPAddress::ApplyMask(const unsigned int icidr, IPAddressType mtype) { - uint8_t clearbits; - uint8_t* p; + uint8_t clearbits = 0; + uint8_t* p = NULL; + unsigned int cidr = icidr; #ifdef INET6 /* IPv6 has IPv4 as Mapped-address */ - if(mtype & IPv4) - { - cidr += 96; // keep the 96 bits added during the IPv4 mapping process. - } - if (cidr > 128) return false; #else - /* FIXME: is it worth trying to convert IPv6 CIDR to IPv4 - * it would mean accepting CIDR from /96 to /128 - * as in /0 to /32 range and mapping the rest to /0 - */ - if (cidr > 32) return false; #endif if(cidr < 0) return true; // any err like this can be assumed /0 - clearbits = (m_Type&IPv6?128:32) - cidr; - p = (uint8_t*)(&m_SocketAddr.sin6_addr + sizeof(m_SocketAddr.sin6_addr)); + clearbits = (uint8_t)((m_Type&IPv6?128:32)-cidr); - for (; clearbits >0; clearbits-=8, p++ ) { +#ifdef INET6 + p = (uint8_t*)(&m_SocketAddr.sin6_addr) + 15; +#else + p = (uint8_t*)(&m_SocketAddr.sin6_addr) + 3; +#endif + + for (; clearbits>0 && p >= (uint8_t*)&m_SocketAddr.sin6_addr ; clearbits-=8, p-- ) { *p &= ((0xFF << clearbits) & 0xFF); } @@ -109,11 +109,15 @@ void IPAddress::SetAnyAddr() { memset(&(m_SocketAddr.sin6_addr),0, sizeof(m_SocketAddr.sin6_addr)); +#ifdef INET6 m_Type = IPv64; +#else + m_Type=IPv4; +#endif } void IPAddress::SetEmpty() { - memset(&(m_SocketAddr.sin6_addr),0, sizeof(m_SocketAddr.sin6_addr)); + memset(&(m_SocketAddr.sin6_addr),0x0, sizeof(m_SocketAddr.sin6_addr)); } bool IPAddress::IsNoAddr() const @@ -123,8 +127,12 @@ } void IPAddress::SetNoAddr() { - memset(&(m_SocketAddr.sin6_addr),255,sizeof(m_SocketAddr.sin6_addr)); + memset(&(m_SocketAddr.sin6_addr),0xFFFFFFFF,sizeof(m_SocketAddr.sin6_addr)); +#ifdef INET6 m_Type = IPv64; +#else + m_Type=IPv4; +#endif } #ifdef INET6 @@ -477,6 +485,8 @@ char* IPAddress::NtoA(char* buf, unsigned int blen) const { + char t[MAX_IPSTRLEN]; + // Ensure we have a buffer. if(buf == NULL) { @@ -484,9 +494,10 @@ return NULL; } + memset(t,0,MAX_IPSTRLEN); + if(m_SocketAddr.sin6_port > 0) { - char t[MAX_IPSTRLEN]; if(inet_ntop((m_Type&IPv6?AF_INET6:AF_INET), &m_SocketAddr.sin6_addr, t, MAX_IPSTRLEN )) #ifdef INET6 snprintf(buf,blen,"[%s]:%d",t, ntohs(m_SocketAddr.sin6_port) );