Address.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 14 IP Storage and Handling */
10
11#ifndef _SQUID_SRC_IP_ADDRESS_H
12#define _SQUID_SRC_IP_ADDRESS_H
13
14#include "ip/forward.h"
15
16#include <iosfwd>
17#include <ostream>
18#if HAVE_SYS_SOCKET_H
19#include <sys/socket.h>
20#endif
21#if HAVE_NETINET_IN_H
22#include <netinet/in.h>
23#endif
24#if HAVE_NETINET_IP_H
25#include <netinet/ip.h>
26#endif
27#if HAVE_WS2TCPIP_H
28#include <ws2tcpip.h>
29#endif
30#if HAVE_NETDB_H
31#include <netdb.h>
32#endif
33
34namespace Ip
35{
36
41{
42
43public:
47 Address(const struct in_addr &);
48 Address(const struct sockaddr_in &);
49 Address(const struct in6_addr &);
50 Address(const struct sockaddr_in6 &);
51 Address(const struct hostent &);
52 Address(const struct addrinfo &);
53 Address(const char*);
58 Address& operator =(struct sockaddr_in const &s);
59 Address& operator =(struct sockaddr_storage const &s);
60 Address& operator =(struct in_addr const &s);
61 Address& operator =(struct in6_addr const &s);
62 Address& operator =(struct sockaddr_in6 const &s);
63 bool operator =(const struct hostent &s);
64 bool operator =(const struct addrinfo &s);
65 bool operator =(const char *s);
70 bool operator ==(Address const &s) const;
71 bool operator !=(Address const &s) const;
72 bool operator >=(Address const &rhs) const;
73 bool operator <=(Address const &rhs) const;
74 bool operator >(Address const &rhs) const;
75 bool operator <(Address const &rhs) const;
76
77public:
78 /* methods */
79
84 bool isIPv4() const;
85
90 bool isIPv6() const;
91
97 bool isSockAddr() const;
98
105 bool isAnyAddr() const;
106
112 bool isNoAddr() const;
113
120 bool isLocalhost() const;
121
127 bool isSiteLocal6() const;
128
134 bool isSiteLocalAuto() const;
135
142 unsigned short port() const;
143
150 unsigned short port(unsigned short port);
151
154 void setAnyAddr();
155
158 void setNoAddr();
159
162 void setLocalhost();
163
165 void setEmpty();
166
172 bool setIPv4();
173
178 int cidr() const;
179
183 int applyMask(const Address &mask);
184
190 bool applyMask(const unsigned int cidr, int mtype);
191
195 void applyClientMask(const Address &mask);
196
208 char* toStr(char *buf, const unsigned int blen, int force = AF_UNSPEC) const;
209
218 char* toUrl(char *buf, unsigned int len) const;
219
228 unsigned int toHostStr(char *buf, const unsigned int len) const;
229
234 bool fromHost(const char *hostWithoutPort);
235
245 bool getReverseString(char buf[MAX_IPSTRLEN], int show_type = AF_UNSPEC) const;
246
252 int matchIPAddr(const Address &rhs) const;
253
259 int compareWhole(const Ip::Address &rhs) const;
260
277 void getAddrInfo(struct addrinfo *&ai, int force = AF_UNSPEC) const;
278
282 static void FreeAddr(struct addrinfo *&ai);
283
290 static void InitAddr(struct addrinfo *&ai);
291
298 bool GetHostByName(const char *s);
299
302 static const Address &NoAddr() { static const Address noAddr(v6_noaddr); return noAddr; }
303
304public:
305 /* XXX: When C => C++ conversion is done will be fully private.
306 * Legacy Transition Methods.
307 * These are here solely to simplify the transition
308 * when moving from converted code to unconverted
309 * these functions can be used to convert this object
310 * and pull out the data needed by the unconverted code
311 * they are intentionaly hard to use, use getAddrInfo() instead.
312 * these functions WILL NOT be in the final public API after transition.
313 */
314
315 void getSockAddr(struct sockaddr_storage &addr, const int family) const;
316 void getSockAddr(struct sockaddr_in &) const;
317 bool getInAddr(struct in_addr &) const; /* false if could not convert IPv6 down to IPv4 */
318 void getSockAddr(struct sockaddr_in6 &) const;
319 void getInAddr(struct in6_addr &) const;
320
321private:
322 /* Conversion for dual-type internals */
323
324 bool getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const;
325
326 bool getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const;
327
328 void map4to6(const struct in_addr &src, struct in6_addr &dest) const;
329
330 void map6to4(const struct in6_addr &src, struct in_addr &dest) const;
331
332 // Worker behind GetHostName and char* converters
333 bool lookupHostIP(const char *s, bool nodns);
334
335 /* variables */
336 struct sockaddr_in6 mSocketAddr_;
337
338private:
339 /* Internally used constants */
340 static const unsigned int STRLEN_IP4A = 16; // aaa.bbb.ccc.ddd\0
341 static const unsigned int STRLEN_IP4R = 28; // ddd.ccc.bbb.aaa.in-addr.arpa.\0
342 static const unsigned int STRLEN_IP4S = 21; // ddd.ccc.bbb.aaa:ppppp\0
343 static const unsigned int MAX_IP4_STRLEN = STRLEN_IP4R;
344 static const unsigned int STRLEN_IP6A = 42; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]/0
345 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
346 static const unsigned int STRLEN_IP6S = 48; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:00000/0
347 static const unsigned int MAX_IP6_STRLEN = STRLEN_IP6R;
348 static const struct in6_addr v4_localhost;
349 static const struct in6_addr v4_anyaddr;
350 static const struct in6_addr v4_noaddr;
351 static const struct in6_addr v6_noaddr;
352};
353
354inline std::ostream &
355operator << (std::ostream &os, const Address &ipa)
356{
357 char buf[MAX_IPSTRLEN];
358 os << ipa.toUrl(buf,MAX_IPSTRLEN);
359 return os;
360}
361
362// WAS _sockaddr_in_list in an earlier incarnation
364{
365public:
366 Address_list() { next = nullptr; };
367 ~Address_list() { if (next) delete next; next = nullptr; };
368
371};
372
373} // namespace Ip
374
376
377#endif /* _SQUID_SRC_IP_ADDRESS_H */
378
Address_list * next
Definition: Address.h:370
char * toStr(char *buf, const unsigned int blen, int force=AF_UNSPEC) const
Definition: Address.cc:792
void map4to6(const struct in_addr &src, struct in6_addr &dest) const
Definition: Address.cc:979
bool isSiteLocal6() const
Definition: Address.cc:267
void setEmpty()
Fast reset of the stored content to what would be after default constructor.
Definition: Address.cc:184
static void InitAddr(struct addrinfo *&ai)
Definition: Address.cc:668
bool setIPv4()
Definition: Address.cc:224
void getSockAddr(struct sockaddr_storage &addr, const int family) const
Definition: Address.cc:924
bool operator==(Address const &s) const
Definition: Address.cc:730
static const unsigned int STRLEN_IP4A
Definition: Address.h:340
void setLocalhost()
Definition: Address.cc:255
bool isLocalhost() const
Definition: Address.cc:249
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition: Address.cc:842
bool operator>(Address const &rhs) const
Definition: Address.cc:760
bool isSockAddr() const
Definition: Address.cc:152
int compareWhole(const Ip::Address &rhs) const
Definition: Address.cc:724
static void FreeAddr(struct addrinfo *&ai)
Definition: Address.cc:686
bool lookupHostIP(const char *s, bool nodns)
Definition: Address.cc:378
bool getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const
Definition: Address.cc:299
void getAddrInfo(struct addrinfo *&ai, int force=AF_UNSPEC) const
Definition: Address.cc:599
bool operator>=(Address const &rhs) const
Definition: Address.cc:751
bool GetHostByName(const char *s)
Definition: Address.cc:372
void applyClientMask(const Address &mask)
Definition: Address.cc:105
static const unsigned int MAX_IP4_STRLEN
Definition: Address.h:343
bool isIPv4() const
Definition: Address.cc:158
bool getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const
Definition: Address.cc:326
static const Address & NoAddr()
Definition: Address.h:302
bool fromHost(const char *hostWithoutPort)
Definition: Address.cc:898
static const unsigned int MAX_IP6_STRLEN
Definition: Address.h:347
int matchIPAddr(const Address &rhs) const
Definition: Address.cc:703
struct sockaddr_in6 mSocketAddr_
Definition: Address.h:336
static const struct in6_addr v6_noaddr
Definition: Address.h:351
bool isSiteLocalAuto() const
Definition: Address.cc:277
bool operator<(Address const &rhs) const
Definition: Address.cc:769
bool operator!=(Address const &s) const
Definition: Address.cc:736
bool isNoAddr() const
Definition: Address.cc:284
static const struct in6_addr v4_noaddr
Definition: Address.h:350
static const unsigned int STRLEN_IP4S
Definition: Address.h:342
bool isAnyAddr() const
Definition: Address.cc:170
bool isIPv6() const
Definition: Address.cc:164
bool getReverseString(char buf[MAX_IPSTRLEN], int show_type=AF_UNSPEC) const
Definition: Address.cc:338
void setNoAddr()
Definition: Address.cc:292
Address & operator=(struct sockaddr_in const &s)
Definition: Address.cc:430
char * toUrl(char *buf, unsigned int len) const
Definition: Address.cc:874
bool getInAddr(struct in_addr &) const
Definition: Address.cc:1020
int applyMask(const Address &mask)
Definition: Address.cc:87
int cidr() const
Definition: Address.cc:44
void setAnyAddr()
NOTE: Does NOT clear the Port stored. Only the Address and Type.
Definition: Address.cc:177
static const unsigned int STRLEN_IP6S
Definition: Address.h:346
static const unsigned int STRLEN_IP6R
Definition: Address.h:345
bool operator<=(Address const &rhs) const
Definition: Address.cc:742
static const struct in6_addr v4_localhost
Definition: Address.h:348
static const unsigned int STRLEN_IP6A
Definition: Address.h:344
unsigned short port() const
Definition: Address.cc:778
static const struct in6_addr v4_anyaddr
Definition: Address.h:349
void map6to4(const struct in6_addr &src, struct in_addr &dest) const
Definition: Address.cc:1000
static const unsigned int STRLEN_IP4R
Definition: Address.h:341
void parse_IpAddress_list_token(Ip::Address_list **, char *)
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:25
Definition: Xaction.cc:139
std::ostream & operator<<(std::ostream &os, const std::optional< Address > &optional)
Definition: Xaction.cc:142

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors