--------------------- PatchSet 4903 Date: 2007/06/30 12:01:39 Author: amosjeffries Branch: squid3-ipv6 Tag: (none) Log: Applied astyle Members: lib/IPAddress.cc:1.1.2.52->1.1.2.53 Index: squid3/lib/IPAddress.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/lib/Attic/IPAddress.cc,v retrieving revision 1.1.2.52 retrieving revision 1.1.2.53 diff -u -r1.1.2.52 -r1.1.2.53 --- squid3/lib/IPAddress.cc 30 Jun 2007 08:04:10 -0000 1.1.2.52 +++ squid3/lib/IPAddress.cc 30 Jun 2007 12:01:39 -0000 1.1.2.53 @@ -1,6 +1,6 @@ /* - * $Id: IPAddress.cc,v 1.1.2.52 2007/06/30 08:04:10 amosjeffries Exp $ + * $Id: IPAddress.cc,v 1.1.2.53 2007/06/30 12:01:39 amosjeffries Exp $ * * DEBUG: section 14 IP Storage and Handling * AUTHOR: Amos Jeffries @@ -183,6 +183,7 @@ if(mtype == None) mtype = IPv4; + #else if(mtype == None) mtype = IPv6; @@ -192,7 +193,7 @@ // validation and short-cuts. if (cidr > 128) return false; - + if (cidr > 32 && mtype == IPv4) return false; @@ -423,8 +424,11 @@ bool IPAddress::operator =(const char* s) { int err = 0; + struct addrinfo *res = NULL; + struct addrinfo want; + memset(&want, 0, sizeof(struct addrinfo)); want.ai_flags = AI_NUMERICHOST; // prevent actual DNS lookups! #if !USE_IPV6 @@ -439,14 +443,13 @@ return false; } -/* - * NP: =(sockaddr_*) sets SockAddr flag. - * we MUST NOT set that flag from only an IPA assignment - */ + /* + * NP: =(sockaddr_*) sets SockAddr flag. + * we MUST NOT set that flag from only an IPA assignment + */ if( IsSockAddr() ) { // does not matter. operator=(*res); - } - else { + } else { // prevent SockAddr being set by the other operator. operator=(*res); // TODO FIX: really need to get a non-magic way of doing this mask. SockAddr == 0x01 -> 0xe in last bits. @@ -455,7 +458,9 @@ /* free the memory getaddrinfo dynamically allocated. */ freeaddrinfo(res); + res = NULL; + return true; } @@ -669,6 +674,7 @@ bool IPAddress::operator =(const struct addrinfo &s) { + struct sockaddr_in* ipv4 = NULL; struct sockaddr_in6* ipv6 = NULL; @@ -703,15 +709,17 @@ debugs(14,1, HERE << "Discarded IPv6 Address. Protocol disabled."); // see if there is another address in the list that might be usable ?? + if (s.ai_next) return operator=(*s.ai_next); else return false; + #endif break; default: - return false; + return false; } return true; @@ -719,7 +727,8 @@ void IPAddress::GetAddrInfo(struct addrinfo *&dst, int force) const { - if(dst == NULL) { + if(dst == NULL) + { dst = new addrinfo; } @@ -727,50 +736,65 @@ // set defaults dst->ai_flags = AI_NUMERICHOST; + if(dst->ai_socktype == 0) - dst->ai_socktype = SOCK_STREAM; + dst->ai_socktype = SOCK_STREAM; if(dst->ai_socktype == SOCK_STREAM // implies TCP - && dst->ai_protocol == 0) + && dst->ai_protocol == 0) dst->ai_protocol = IPPROTO_TCP; if(dst->ai_socktype == SOCK_DGRAM // implies UDP - && dst->ai_protocol == 0) + && dst->ai_protocol == 0) dst->ai_protocol = IPPROTO_UDP; #if USE_IPV6 - if( force==AF_INET6 || force == AF_UNSPEC && IsIPv6() ) { + if( force==AF_INET6 || force == AF_UNSPEC && IsIPv6() ) + { + dst->ai_addr = (struct sockaddr*)new sockaddr_in6; + memset(dst->ai_addr,0,sizeof(struct sockaddr_in6)); + GetSockAddr(*((struct sockaddr_in6*)dst->ai_addr)); + dst->ai_addrlen = sizeof(struct sockaddr_in6); dst->ai_family = ((struct sockaddr_in6*)dst->ai_addr)->sin6_family; dst->ai_protocol = IPPROTO_IPV6; - } - else + } else #endif - if( force == AF_INET || force == AF_UNSPEC && IsIPv4() ) { - dst->ai_addr = (struct sockaddr*)new sockaddr_in; - memset(dst->ai_addr,0,sizeof(struct sockaddr_in)); - GetSockAddr(*((struct sockaddr_in*)dst->ai_addr)); - dst->ai_addrlen = sizeof(struct sockaddr_in); + if( force == AF_INET || force == AF_UNSPEC && IsIPv4() ) + { - dst->ai_family = ((struct sockaddr_in*)dst->ai_addr)->sin_family; - } - else { - assert(false); - } + dst->ai_addr = (struct sockaddr*)new sockaddr_in; + + memset(dst->ai_addr,0,sizeof(struct sockaddr_in)); + + GetSockAddr(*((struct sockaddr_in*)dst->ai_addr)); + + dst->ai_addrlen = sizeof(struct sockaddr_in); + + dst->ai_family = ((struct sockaddr_in*)dst->ai_addr)->sin_family; + } else + { + assert(false); + } } void IPAddress::FreeAddrInfo(struct addrinfo *&ai) const { if(ai == NULL) return; + if(ai->ai_addr) delete ai->ai_addr; + ai->ai_addr = NULL; + ai->ai_addrlen = 0; + // NP: name fields are NOT allocated at present. delete ai; + ai = NULL; } @@ -785,6 +809,7 @@ #endif // loop a byte-wise compare + for(int i=sizeof(m_SocketAddr.sin6_addr); i>0 ; i--) { if(l[i] < r[i]) @@ -890,24 +915,25 @@ if( (force & IPv64) && !(force & m_Type) ) { if( IsIPv6() ) memcpy(buf, "{IPv6}", 6); + if( IsIPv4() ) memcpy(buf, "{IPv4}", 6); + return buf; } if( !(force & IPv4) && IsIPv6() ) { inet_ntop(AF_INET6, &m_SocketAddr.sin6_addr, buf, blen); - } - else if ( !(force & IPv6) && IsIPv4() ) { + } else if ( !(force & IPv6) && IsIPv4() ) { #if USE_IPV6 + struct in_addr tmp; GetInAddr(tmp); inet_ntop(AF_INET, &tmp, buf, blen); #else inet_ntop(AF_INET, &m_SocketAddr.sin_addr, buf, blen); #endif - } - else { + } else { debugs(14,0,"WARNING: Corrupt IP Address details found!"); fprintf(stderr,"WARNING: Corrupt IP Address details found!"); memcpy(buf,"dead:beef::",12); @@ -972,22 +998,28 @@ { #if USE_IPV6 - if(m_Type & IPv4) { + if(m_Type & IPv4) + { buf.sin_family = AF_INET; buf.sin_port = m_SocketAddr.sin6_port; Map6to4( m_SocketAddr.sin6_addr, buf.sin_addr); - } - else { + } else + { debugs(14,1, HERE << "IPAddress::GetSockAddr : Cannot convert non-IPv4 to IPv4."); + memset(&buf,0xFFFFFFFF,sizeof(struct sockaddr_in)); assert(false); } + #else + memcpy(&buf, &m_SocketAddr, sizeof(struct sockaddr_in)); - if(buf.sin_family == 0) { + if(buf.sin_family == 0) + { buf.sin_family = AF_INET; } + #endif } @@ -1016,6 +1048,7 @@ } else if( in.s_addr == 0xFFFFFFFF) { /* NOADDR */ + out.s6_addr32[0] = 0xFFFFFFFF; out.s6_addr32[1] = 0xFFFFFFFF; out.s6_addr32[2] = 0xFFFFFFFF; @@ -1072,6 +1105,7 @@ #if USE_IPV6 case IPv4: + case IPv64: // IPv4-Compatible IPv6 Address Map6to4((const in6_addr)m_SocketAddr.sin6_addr, buf); return true; @@ -1087,6 +1121,7 @@ default: debugs(14,1, HERE << "IPAddress::GetInAddr : Cannot convert non-IPv4 to IPv4. IPA=" << *this); + memset(&buf,0xFFFFFFFF,sizeof(struct in_addr)); assert(false); return false;