2 * DEBUG: section 14 IP Storage and Handling
3 * AUTHOR: Amos Jeffries
5 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from the
9 * Internet community. Development is led by Duane Wessels of the
10 * National Laboratory for Applied Network Research and funded by the
11 * National Science Foundation. Squid is Copyrighted (C) 1998 by
12 * the Regents of the University of California. Please see the
13 * COPYRIGHT file for full details. Squid incorporates software
14 * developed and/or copyrighted by other sources. Please see the
15 * CREDITS file for full details.
17 * This Ip::Address code is copyright (C) 2007 by Treehouse Networks Ltd
18 * of New Zealand. It is published and Lisenced as an extension of
19 * squid under the same conditions as the main squid application.
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 #ifndef _SQUID_IP_IPADDRESS_H
37 #define _SQUID_IP_IPADDRESS_H
42 #include <sys/socket.h>
45 #include <netinet/in.h>
64 /// Length of buffer that needs to be allocated to old a null-terminated IP-string
65 // Yuck. But there are still structures that need it to be an 'integer constant'.
66 #define MAX_IPSTRLEN 75
69 * Holds and manipulates IPv4, IPv6, and Socket Addresses.
75 /** @name Constructors and Destructor */
78 Address(const Address
&);
81 * This constructor takes its own copy of the object pointed to for memory-safe usage later.
82 * The caller must itself perform and ptr memory-management needed.
84 \deprecated Use of pointers can be nasty. Consider this a last-resort.
85 * Prefer the by-reference (&) version instead.
89 Address(const struct in_addr
&);
90 Address(const struct sockaddr_in
&);
91 Address(const struct in6_addr
&);
92 Address(const struct sockaddr_in6
&);
93 Address(const struct hostent
&);
94 Address(const struct addrinfo
&);
96 /// Default destructor.
100 /** @name Assignment Operators */
102 Address
& operator =(const Address
&s
);
103 Address
& operator =(struct sockaddr_in
const &s
);
104 Address
& operator =(struct sockaddr_storage
const &s
);
105 Address
& operator =(struct in_addr
const &s
);
106 Address
& operator =(struct in6_addr
const &s
);
107 Address
& operator =(struct sockaddr_in6
const &s
);
108 bool operator =(const struct hostent
&s
);
109 bool operator =(const struct addrinfo
&s
);
110 bool operator =(const char *s
);
113 /** @name Boolean Operators */
115 bool operator ==(Address
const &s
) const;
116 bool operator !=(Address
const &s
) const;
117 bool operator >=(Address
const &rhs
) const;
118 bool operator <=(Address
const &rhs
) const;
119 bool operator >(Address
const &rhs
) const;
120 bool operator <(Address
const &rhs
) const;
125 /** Test whether content can be used as an IPv4 address
126 \retval true if content was received as an IPv4 address
127 \retval true if content was received as an IPv4-Mapped address
128 \retval false if content was received as a non-mapped IPv6 native address.
132 /** Test whether content can be used as an IPv6 address.
133 \retval true if --enable-ipv6 has been compiled.
134 \retval false if --disable-ipv6 has been compiled.
135 \retval false if --with-ipv6-split-stack has been compiled AND content is IPv4-mapped.
139 /** Test whether content can be used as a Socket address.
140 \retval true if address AND port are both set
141 \retval true if content was received as a Socket address with port
142 \retval false if port in unset (zero)
144 bool IsSockAddr() const;
146 /** Content-neutral test for whether the specific IP case ANY_ADDR is stored.
147 * This is the default content of a new undefined Ip::Address object.
148 \retval true IPv4 0.0.0.0
150 \retval false anything else.
152 bool IsAnyAddr() const;
154 /** Content-neutral test for whether the specific IP case NO_ADDR is stored.
155 \retval true IPv4 255.255.255.255
156 \retval true IPv6 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
157 \retval false anything else.
159 bool IsNoAddr() const;
161 /** Content-neutral test for whether the specific IP case LOCALHOST is stored.
162 * This is the default content of a new undefined Ip::Address object.
163 \retval true IPv4 127.0.0.1
164 \retval true IPv6 ::1
165 \retval false anything else.
167 bool IsLocalhost() const;
169 /** Test whether content is an IPv6 Site-Local address.
170 \retval true if address begins with fe80::/10.
171 \retval false if --disable-ipv6 has been compiled.
172 \retval false if address does not match fe80::/10
174 bool IsSiteLocal6() const;
176 /** Test whether content is an IPv6 address with SLAAC EUI-64 embeded.
177 \retval true if address matches ::ff:fe00:0
178 \retval false if --disable-ipv6 has been compiled.
179 \retval false if address does not match ::ff:fe00:0
181 bool IsSlaac() const;
185 /** Retrieve the Port if stored.
186 \retval 0 Port is unset or an error occured.
187 \retval n Port associated with this address in host native -endian.
189 u_short
GetPort() const;
191 /** Set the Port value for an address.
192 * Replaces any previously existing Port value.
193 \param port Port being assigned in host native -endian.
194 \retval 0 Port is unset or an error occured.
195 \retval n Port associated with this address in host native -endian.
197 u_short
SetPort(u_short port
);
199 /// Set object to contain the specific IP case ANY_ADDR (format-neutral).
200 /// see IsAnyAddr() for more detail.
203 /// Set object to contain the specific IP case NO_ADDR (format-neutral).
204 /// see IsNoAddr() for more detail.
207 /// Set object to contain the specific IP case LOCALHOST (format-neutral).
208 /// see IsLocalhost() for more detail.
211 /// Fast reset of the stored content to what would be after default constructor.
214 /** Require an IPv4-only address for this usage.
215 * Converts the object to prefer only IPv4 output.
216 \retval true Content can be IPv4
217 \retval false Content CANNOT be IPv4
222 * Valid results IF and only IF the stored IP address is actually a network bitmask
223 \retval N number of bits which are set in the bitmask stored.
227 /** Apply a mask to the stored address.
228 \param mask Netmask format to be bit-mask-AND'd over the stored address.
230 const int ApplyMask(const Address
&mask
);
232 /** Apply a mask to the stored address.
233 * CIDR will be converted appropriate to map the stored content.
234 \param cidr CIDR Mask being applied. As an integer in host format.
235 \param mtype Type of CIDR mask being applied (AF_INET or AF_INET6)
237 bool ApplyMask(const unsigned int cidr
, int mtype
);
240 /** Return the ASCII equivalent of the address
241 * Semantically equivalent to the IPv4 inet_ntoa()
242 * eg. 127.0.0.1 (IPv4) or ::1 (IPv6)
243 * But for memory safety it requires a buffer as input
244 * instead of producing one magically.
245 * If buffer is not large enough the data is truncated silently.
246 \param buf Allocated buffer to write address to
247 \param len byte length of buffer available for writing.
248 \param force (optional) require the IPA in a specific format.
249 \return pointer to buffer received.
251 char* NtoA(char *buf
, unsigned int len
, int force
= AF_UNSPEC
) const;
253 /** Return the ASCII equivalent of the address:port combination
254 * Provides a URL formatted version of the content.
255 * If buffer is not large enough the data is truncated silently.
256 * eg. 127.0.0.1:80 (IPv4) or [::1]:80 (IPv6)
257 \param buf Allocated buffer to write address:port to
258 \param len byte length of buffer available for writing.
259 \return pointer to buffer received.
261 char* ToURL(char *buf
, unsigned int len
) const;
263 /** Return a properly hostname formatted copy of the address
264 * Provides a URL formatted version of the content.
265 * If buffer is not large enough the data is truncated silently.
266 * eg. 127.0.0.1 (IPv4) or [::1] (IPv6)
267 \param buf Allocated buffer to write address to
268 \param len byte length of buffer available for writing.
269 \return pointer to buffer received.
271 unsigned int ToHostname(char *buf
, const unsigned int len
) const;
274 * Convert the content into a Reverse-DNS string.
275 * The buffer sent MUST be allocated large enough to hold the resulting string.
276 * Name truncation will occur if buf does not have enough space.
277 * The constant MAX_IPSTRLEN is defined to provide for sizing arrays correctly.
278 \param show_type may be one of: AF_INET, AF_INET6 for the format of rDNS string wanted.
279 * AF_UNSPEC the default displays the IP in its most advanced native form.
280 \param buf buffer to receive the text string output.
282 bool GetReverseString(char buf
[MAX_IPSTRLEN
], int show_type
= AF_UNSPEC
) const;
284 /** Test how two IP relate to each other.
285 \retval 0 IP are equal
286 \retval 1 IP rhs is greater (numerically) than that stored.
287 \retval -1 IP rhs is less (numerically) than that stored.
289 int matchIPAddr(const Address
&rhs
) const;
291 /** Compare taking IP, port, protocol, etc. into account. Returns an
292 integer less than, equal to, or greater than zero if the object
293 is found, respectively, to be less than, to match, or to be greater
294 than rhs. The exact ordering algorithm is not specified and may change.
296 int compareWhole(const Ip::Address
&rhs
) const;
299 * Get RFC 3493 addrinfo structure from the Ip::Address data
300 * for protocol-neutral socket operations.
301 * Should be passed a NULL pointer of type struct addrinfo* it will
302 * allocate memory for the structures involved. (see FreeAddrInfo to clear).
303 * Defaults to a TCP streaming socket, if other values (such as UDP) are needed
304 * the caller MUST override these default settings.
305 * Some situations may also require an actual call to the system getaddrinfo()
306 * to pull relevant OS details for the socket.
308 * Ip::Address allocated objects MUST be destructed by Ip::Address::FreeAddrInfo
309 * System getaddrinfo() allocated objects MUST be freed with system freeaddrinfo()
311 \param ai structure to be filled out.
312 \param force a specific sockaddr type is needed. default: don't care.
314 void GetAddrInfo(struct addrinfo
*&ai
, int force
= AF_UNSPEC
) const;
317 * Equivalent to the sysem call freeaddrinfo() but for Ip::Address allocated data
319 static void FreeAddrInfo(struct addrinfo
*&ai
);
322 * Initializes an empty addrinfo properly for use.
323 * It is intended for use in cases such as getsockopt() where the addrinfo is
324 * about to be changed and the stored details may not match the new ones coming.
325 \param ai addrinfo struct to be initialized as AF_UNSPEC with large address buffer
327 static void InitAddrInfo(struct addrinfo
*&ai
);
330 * Lookup a Host by Name. Equivalent to system call gethostbyname(char*)
331 \param s The textual FQDN of the host being located.
332 \retval true lookup was successful and an IPA was located.
333 \retval false lookup failed or FQDN has no IP associated.
335 bool GetHostByName(const char *s
);
338 /* FIXME: When C => C++ conversion is done will be fully private.
339 * Legacy Transition Methods.
340 * These are here solely to simplify the transition
341 * when moving from converted code to unconverted
342 * these functions can be used to convert this object
343 * and pull out the data needed by the unconverted code
344 * they are intentionaly hard to use, use GetAddrInfo() instead.
345 * these functiosn WILL NOT be in the final public API after transition.
348 void GetSockAddr(struct sockaddr_storage
&addr
, const int family
) const;
349 void GetSockAddr(struct sockaddr_in
&) const;
350 bool GetInAddr(struct in_addr
&) const; /* false if could not convert IPv6 down to IPv4 */
351 void GetSockAddr(struct sockaddr_in6
&) const;
352 void GetInAddr(struct in6_addr
&) const;
355 /* Conversion for dual-type internals */
357 bool GetReverseString4(char buf
[MAX_IPSTRLEN
], const struct in_addr
&dat
) const;
359 bool GetReverseString6(char buf
[MAX_IPSTRLEN
], const struct in6_addr
&dat
) const;
361 void Map4to6(const struct in_addr
&src
, struct in6_addr
&dest
) const;
363 void Map6to4(const struct in6_addr
&src
, struct in_addr
&dest
) const;
365 // Worker behind GetHostName and char* converters
366 bool LookupHostIP(const char *s
, bool nodns
);
369 struct sockaddr_in6 m_SocketAddr
;
372 /* Internally used constants */
373 static const unsigned int STRLEN_IP4A
= 16; // aaa.bbb.ccc.ddd\0
374 static const unsigned int STRLEN_IP4R
= 28; // ddd.ccc.bbb.aaa.in-addr.arpa.\0
375 static const unsigned int STRLEN_IP4S
= 21; // ddd.ccc.bbb.aaa:ppppp\0
376 static const unsigned int MAX_IP4_STRLEN
= STRLEN_IP4R
;
377 static const unsigned int STRLEN_IP6A
= 42; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]/0
378 static const unsigned int STRLEN_IP6R
= 75; // f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f f.f.f.f ipv6.arpa./0
379 static const unsigned int STRLEN_IP6S
= 48; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:00000/0
380 static const unsigned int MAX_IP6_STRLEN
= STRLEN_IP6R
;
381 static const struct in6_addr v4_localhost
;
382 static const struct in6_addr v4_anyaddr
;
383 static const struct in6_addr v4_noaddr
;
384 static const struct in6_addr v6_noaddr
;
388 inline std::ostream
&
389 operator << (std::ostream
&os
, const Address
&ipa
)
391 char buf
[MAX_IPSTRLEN
];
392 os
<< ipa
.ToURL(buf
,MAX_IPSTRLEN
);
396 // WAS _sockaddr_in_list in an earlier incarnation
400 Address_list() { next
= NULL
; };
401 ~Address_list() { if (next
) delete next
; next
= NULL
; };
409 extern void parse_IpAddress_list_token(Ip::Address_list
**, char *);
411 #endif /* _SQUID_IP_IPADDRESS_H */