--------------------- PatchSet 4115 Date: 2007/03/23 15:42:43 Author: amosjeffries Branch: squid3-ipv6 Tag: (none) Log: small alteration of IPv6 content test for dual/split stack systems. Documentation. Members: include/IPAddress.h:1.1.2.3->1.1.2.4 Index: squid3/include/IPAddress.h =================================================================== RCS file: /cvsroot/squid-sf//squid3/include/Attic/IPAddress.h,v retrieving revision 1.1.2.3 retrieving revision 1.1.2.4 diff -u -r1.1.2.3 -r1.1.2.4 --- squid3/include/IPAddress.h 22 Mar 2007 12:34:25 -0000 1.1.2.3 +++ squid3/include/IPAddress.h 23 Mar 2007 15:42:43 -0000 1.1.2.4 @@ -1,5 +1,5 @@ /* - * $Id: IPAddress.h,v 1.1.2.3 2007/03/22 12:34:25 amosjeffries Exp $ + * $Id: IPAddress.h,v 1.1.2.4 2007/03/23 15:42:43 amosjeffries Exp $ */ #ifndef _INC_IPADDRESS_H #define _INC_IPADDRESS_H @@ -14,11 +14,11 @@ /// Specify the type of Address being or to be handled. enum IPAddressType { - None =0, /// Nothing Secial. Equates to default if used as a parameter. - SockAddr =1, /// Full SocketAddr Details Stored. - IPv4 =2, /// Pure IPv4 address stored (conversion up must be done explicitly) - IPv6 =4, /// Pure IPv6 address Stored (no conversion to IPv4 possible) - IPv64 =6 /// Dual-Address stored (can return either IPv6 OR IPv4) + None =0, ///< Nothing Special. Equates to default if used as a parameter. + SockAddr =1, ///< Full SocketAddr Details Stored. Port and Address can be used together or seperately. + IPv4 =2, ///< Pure IPv4 address stored (conversion up must be done explicitly) + IPv6 =4, ///< Pure IPv6 address Stored (no conversion to IPv4 possible) + IPv64 =6 ///< Dual-Address stored (can return either IPv6 OR IPv4) }; /// Length of buffer that needs to be allocated to old a null-terminated IP-string @@ -30,6 +30,9 @@ class IPAddress { public: + /** @name Constructors and Destructor */ + /*@{*/ + // default constructor. IPAddress(); IPAddress(const IPAddress &); IPAddress(const hostent *); @@ -40,9 +43,12 @@ IPAddress(const struct in6_addr &); IPAddress(const struct sockaddr_in6 &); #endif + /// Default destructor. ~IPAddress(); + /*@}*/ - /** Assignment Operators **/ + /** @name Assignment Operators */ + /*@{*/ IPAddress& operator =(const IPAddress &s); IPAddress& operator =(const struct hostent *s); IPAddress& operator =(const char *s); @@ -52,48 +58,134 @@ IPAddress& operator =(struct in6_addr const &s); IPAddress& operator =(struct sockaddr_in6 const &s); #endif + /*@}*/ - /* Boolean Operators */ + /** @name Boolean Operators */ + /*@{*/ bool operator ==(IPAddress const &s) const; bool operator >=(IPAddress const &rhs) const; bool operator <=(IPAddress const &rhs) const; public: /* methods */ - inline bool IsIPv4() const { return (m_Type & IPv4); }; /// test whether content can be used as an IPv4. - inline bool IsIPv6() const { return (m_Type & IPv6); }; /// test whether content can be used as an IPv6. - inline bool IsSockAddr() const { return (m_Type & SockAddr); }; /// test whether content can be used as a sockaddr (Address and Port combo). - bool IsNoAddr() const; /// Content-neutral test for whether specific IP case ANY_ADDR is stored. - bool IsAnyAddr() const; /// Content-neutral test for whether specific IP case NO_ADDR is stored. + /** Test whether content can be used as an IPv4 address + * \retval true if content was received as an IPv4 address + * \retval true if content was received as an IPv4-Mapped address + * \retval false if content was received as a non-mapped IPv6 native address. + */ + inline bool IsIPv4() const { return (m_Type & IPv4); }; + + /** Test whether content can be used as an IPv6 address. + * \retval true if --enable-ipv6 has been compiled. + * \retval false if --disable-ipv6 has been compiled. + * \retval false if --with-ipv6-split-stack has been compiled AND content is IPv4-mapped. + */ + inline bool IsIPv6() const { + return (m_Type & IPv6) +#ifdef USE_IPV6_SPLITSTACK + && !(m_Type & IPv4) +#endif + ; + }; + + /** Test whether content can be used as a Socket address. + * \retval true if address AND port are both set + * \retval true if content was received as a Socket address + * \retval false if port in unset (zero) + */ + inline bool IsSockAddr() const { return (m_Type & SockAddr); }; + + /** Content-neutral test for whether the specific IP case ANY_ADDR is stored. + * This is the default content of a new undefined IPAddress object. + * \retval true IPv4 0.0.0.0 + * \retval true IPv6 :: + * \retval false anything else. + */ + bool IsAnyAddr() const; + /** Content-neutral test for whether the specific IP case NO_ADDR is stored. + * \retval true IPv4 255.255.255.255 + * \retval true IPv6 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff + * \retval false anything else. + */ + bool IsNoAddr() const; + /*@}*/ + + /** Retrieve the Port if stored. + * \retval 0 Port is unset or an error occured. + * \retval n Port associated with this address in host native -endian. + */ + u_short GetPort() const; + /** Set the Port value for an address. + * Replaces any previously existing Port value. + * \param port Port being assigned in host native -endian. + * \retval 0 Port is unset or an error occured. + * \retval n Port associated with this address in host native -endian. + */ + u_short SetPort(u_short port); + + /// Set object to contain the specific IP case ANY_ADDR (format-neutral). + /// see IsAnyAddr() for more detail. + void SetAnyAddr(); + + /// Set object to contain the specific IP case NO_ADDR (format-neutral). + /// see \link IsNoAddr() for more detail. + void SetNoAddr(); + + /// Fast reset of the stored content to what would be after default constructor. + void SetEmpty(); + + /** Apply a mask to the stored address. + * \param mask Netmask format to be bit-mask-AND'd over the stored address. + */ + const int ApplyMask(const IPAddress &mask); + + /** Apply a mask to the stored address. + * CIDR will be converted appropriate to map the stored content. + * \param cidr CIDR Mask being applied. As an integer in host format. + * \param mtype Type of CIDR mask being applied (IPv4,IPv6, or None for default) + */ + bool ApplyMask(const unsigned int cidr, IPAddressType mtype = None); + + + /** Return the ASCII equivalent of the address + * Semantically equivalent to the IPv4 inet_ntoa() + * But for memory safety it requires a buffer as input + * instead of producing one magically. + * If bufer is not large enough fo writing data is truncated silently. + * \param buf Allocated buffer to write address:port to + * \param len byte length of buffer available for writing. + * \return pointer to buffer received. + */ + char* NtoA(char *buf, unsigned int len) const; + + /** \fn bool GetReverseString(char buf[], IPAddressType show_format) + * Convert the content into a Reverse-DNS string. + * The buffer sent MUST be allocated large enough to hold the resulting string. + * Name truncation will occur if buf does not have enough space. + * The constant MAX_IPSTRLEN is defined to provide for sizing arrays correctly. + * \param show_format may be one of: IPv4,IPv6 for the format of rDNS string wanted. + * \param buf buffer to receive the text string output. + */ #ifdef USE_IPV6 bool GetReverseString(char buf[], IPAddressType show_format = IPv6) const; #else bool GetReverseString(char buf[], IPAddressType show_format = IPv4) const; #endif - u_short GetPort() const; - u_short SetPort(u_short port); - void SetAnyAddr(); /// Set object to contain the specific IP case ANY_ADDR (format-neutral). - void SetNoAddr(); /// Set object to contain the specific IP case NO_ADDR (format-neutral). - void SetEmpty(); /// Fast reset of the stored content to what would be after default constructor. std::ostream& operator<<(std::ostream& os) const; - char* NtoA(char *buf, unsigned int len) const; - const int ApplyMask(const IPAddress &); - bool ApplyMask(const unsigned int cidr, IPAddressType mtype = None); - - /* variables */ public: - /* FIXME: When C => C++ conversion is done will be fully private. */ - /* Legacy Transition Methods */ - /* These are here solely to simplify the transition */ - /* when moving from converted code to unconverted */ - /* these functions can be used to convert this object */ - /* and pull out the data needed by the unconverted code */ - /* they are intentionaly hard to use, to encourage less use. */ + /* FIXME: When C => C++ conversion is done will be fully private. + * Legacy Transition Methods + * These are here solely to simplify the transition + * when moving from converted code to unconverted + * these functions can be used to convert this object + * and pull out the data needed by the unconverted code + * they are intentionaly hard to use, to encourage less use. + */ void GetSockAddr(struct sockaddr_in &) const; bool GetInAddr(struct in_addr &) const; /* false if could not convert IPv6 down to IPv4 */ @@ -103,15 +195,14 @@ #endif private: - /* methods */ int matchIPAddr(const IPAddress &rhs) const; /* Conversion for dual-type internals */ - bool GetReverseString4(char buf[], struct in_addr &) const; + bool GetReverseString4(char buf[], struct in_addr &) const; #ifdef USE_IPV6 - void check4Mapped(); - bool GetReverseString6(char buf[], struct in6_addr &) const; - void Map4to6(const struct in_addr &src, struct in6_addr &dest) const; - void Map6to4(const struct in6_addr &src, struct in_addr &dest) const; + void check4Mapped(); + bool GetReverseString6(char buf[], struct in6_addr &) const; + void Map4to6(const struct in_addr &src, struct in6_addr &dest) const; + void Map6to4(const struct in6_addr &src, struct in_addr &dest) const; #endif /* variables */