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 
34 #include <optional>
35 
36 namespace Ip
37 {
38 
42 class Address
43 {
44 
45 public:
52  static std::optional<Address> Parse(const char *);
53 
56  Address() { setEmpty(); }
57  Address(const struct in_addr &);
58  Address(const struct sockaddr_in &);
59  Address(const struct in6_addr &);
60  Address(const struct sockaddr_in6 &);
61  Address(const struct hostent &);
62  Address(const struct addrinfo &);
63  Address(const char*);
68  Address& operator =(struct sockaddr_in const &s);
69  Address& operator =(struct sockaddr_storage const &s);
70  Address& operator =(struct in_addr const &s);
71  Address& operator =(struct in6_addr const &s);
72  Address& operator =(struct sockaddr_in6 const &s);
73  bool operator =(const struct hostent &s);
74  bool operator =(const struct addrinfo &s);
75 
80  bool operator =(const char *s);
85  bool operator ==(Address const &s) const;
86  bool operator !=(Address const &s) const;
87  bool operator >=(Address const &rhs) const;
88  bool operator <=(Address const &rhs) const;
89  bool operator >(Address const &rhs) const;
90  bool operator <(Address const &rhs) const;
91 
92 public:
93  /* methods */
94 
99  bool isIPv4() const;
100 
105  bool isIPv6() const;
106 
112  bool isSockAddr() const;
113 
120  bool isAnyAddr() const;
121 
127  bool isNoAddr() const;
128 
135  bool isLocalhost() const;
136 
142  bool isSiteLocal6() const;
143 
149  bool isSiteLocalAuto() const;
150 
157  unsigned short port() const;
158 
165  unsigned short port(unsigned short port);
166 
169  void setAnyAddr();
170 
173  void setNoAddr();
174 
177  void setLocalhost();
178 
180  void setEmpty();
181 
187  bool setIPv4();
188 
193  int cidr() const;
194 
198  int applyMask(const Address &mask);
199 
205  bool applyMask(const unsigned int cidr, int mtype);
206 
210  void applyClientMask(const Address &mask);
211 
215  void turnMaskedBitsOn(const Address &mask);
216 
228  char* toStr(char *buf, const unsigned int blen, int force = AF_UNSPEC) const;
229 
238  char* toUrl(char *buf, unsigned int len) const;
239 
248  unsigned int toHostStr(char *buf, const unsigned int len) const;
249 
253  bool fromHost(const char *hostWithoutPort);
254 
264  bool getReverseString(char buf[MAX_IPSTRLEN], int show_type = AF_UNSPEC) const;
265 
271  int matchIPAddr(const Address &rhs) const;
272 
278  int compareWhole(const Ip::Address &rhs) const;
279 
296  void getAddrInfo(struct addrinfo *&ai, int force = AF_UNSPEC) const;
297 
301  static void FreeAddr(struct addrinfo *&ai);
302 
309  static void InitAddr(struct addrinfo *&ai);
310 
317  bool GetHostByName(const char *s);
318 
321  static const Address &NoAddr() { static const Address noAddr(v6_noaddr); return noAddr; }
322 
323 public:
324  /* XXX: When C => C++ conversion is done will be fully private.
325  * Legacy Transition Methods.
326  * These are here solely to simplify the transition
327  * when moving from converted code to unconverted
328  * these functions can be used to convert this object
329  * and pull out the data needed by the unconverted code
330  * they are intentionaly hard to use, use getAddrInfo() instead.
331  * these functions WILL NOT be in the final public API after transition.
332  */
333 
334  void getSockAddr(struct sockaddr_storage &addr, const int family) const;
335  void getSockAddr(struct sockaddr_in &) const;
336  bool getInAddr(struct in_addr &) const; /* false if could not convert IPv6 down to IPv4 */
337  void getSockAddr(struct sockaddr_in6 &) const;
338  void getInAddr(struct in6_addr &) const;
339 
340 private:
341  /* Conversion for dual-type internals */
342 
343  bool getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const;
344 
345  bool getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const;
346 
347  void map4to6(const struct in_addr &src, struct in6_addr &dest) const;
348 
349  void map6to4(const struct in6_addr &src, struct in_addr &dest) const;
350 
351  // Worker behind GetHostName and char* converters
352  bool lookupHostIP(const char *s, bool nodns);
353 
354  /* variables */
355  struct sockaddr_in6 mSocketAddr_;
356 
357 private:
358  /* Internally used constants */
359  static const unsigned int STRLEN_IP4A = 16; // aaa.bbb.ccc.ddd\0
360  static const unsigned int STRLEN_IP4R = 28; // ddd.ccc.bbb.aaa.in-addr.arpa.\0
361  static const unsigned int STRLEN_IP4S = 21; // ddd.ccc.bbb.aaa:ppppp\0
362  static const unsigned int MAX_IP4_STRLEN = STRLEN_IP4R;
363  static const unsigned int STRLEN_IP6A = 42; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]/0
364  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
365  static const unsigned int STRLEN_IP6S = 48; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:00000/0
366  static const unsigned int MAX_IP6_STRLEN = STRLEN_IP6R;
367  static const struct in6_addr v4_localhost;
368  static const struct in6_addr v4_anyaddr;
369  static const struct in6_addr v4_noaddr;
370  static const struct in6_addr v6_noaddr;
371 };
372 
373 inline std::ostream &
374 operator << (std::ostream &os, const Address &ipa)
375 {
376  char buf[MAX_IPSTRLEN];
377  os << ipa.toUrl(buf,MAX_IPSTRLEN);
378  return os;
379 }
380 
381 // WAS _sockaddr_in_list in an earlier incarnation
383 {
384 public:
385  Address_list() { next = nullptr; };
386  ~Address_list() { if (next) delete next; next = nullptr; };
387 
388  Address s;
390 };
391 
392 } // namespace Ip
393 
395 
396 #endif /* SQUID_SRC_IP_ADDRESS_H */
397 
void turnMaskedBitsOn(const Address &mask)
Definition: Address.cc:115
bool getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const
Definition: Address.cc:346
void setLocalhost()
Definition: Address.cc:275
bool isAnyAddr() const
Definition: Address.cc:190
static const unsigned int STRLEN_IP4A
Definition: Address.h:359
bool GetHostByName(const char *s)
Definition: Address.cc:392
Address_list * next
Definition: Address.h:389
bool operator==(Address const &s) const
Definition: Address.cc:750
bool getInAddr(struct in_addr &) const
Definition: Address.cc:1040
bool fromHost(const char *hostWithoutPort)
Definition: Address.cc:918
static void FreeAddr(struct addrinfo *&ai)
Definition: Address.cc:706
bool operator>=(Address const &rhs) const
Definition: Address.cc:771
int compareWhole(const Ip::Address &rhs) const
Definition: Address.cc:744
static const unsigned int STRLEN_IP6A
Definition: Address.h:363
char * toUrl(char *buf, unsigned int len) const
Definition: Address.cc:894
bool getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const
Definition: Address.cc:319
bool isIPv4() const
Definition: Address.cc:178
int matchIPAddr(const Address &rhs) const
Definition: Address.cc:723
static std::optional< Address > Parse(const char *)
Definition: Address.cc:44
char * toStr(char *buf, const unsigned int blen, int force=AF_UNSPEC) const
Definition: Address.cc:812
static const struct in6_addr v4_anyaddr
Definition: Address.h:368
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:25
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition: Address.cc:862
void applyClientMask(const Address &mask)
Definition: Address.cc:125
bool operator<=(Address const &rhs) const
Definition: Address.cc:762
static const unsigned int STRLEN_IP4S
Definition: Address.h:361
bool isSiteLocal6() const
Definition: Address.cc:287
void setNoAddr()
Definition: Address.cc:312
static const unsigned int STRLEN_IP6S
Definition: Address.h:365
unsigned short port() const
Definition: Address.cc:798
bool isIPv6() const
Definition: Address.cc:184
static const unsigned int STRLEN_IP4R
Definition: Address.h:360
static const Address & NoAddr()
Definition: Address.h:321
void parse_IpAddress_list_token(Ip::Address_list **, char *)
static const unsigned int STRLEN_IP6R
Definition: Address.h:364
bool setIPv4()
Definition: Address.cc:244
int cidr() const
Definition: Address.cc:54
std::ostream & operator<<(std::ostream &os, const std::optional< Address > &optional)
Definition: Xaction.cc:140
static const struct in6_addr v6_noaddr
Definition: Address.h:370
static const unsigned int MAX_IP4_STRLEN
Definition: Address.h:362
Address & operator=(struct sockaddr_in const &s)
Definition: Address.cc:450
void setEmpty()
Fast reset of the stored content to what would be after default constructor.
Definition: Address.cc:204
bool isNoAddr() const
Definition: Address.cc:304
bool getReverseString(char buf[MAX_IPSTRLEN], int show_type=AF_UNSPEC) const
Definition: Address.cc:358
Definition: Xaction.cc:137
bool operator!=(Address const &s) const
Definition: Address.cc:756
int applyMask(const Address &mask)
Definition: Address.cc:97
bool operator>(Address const &rhs) const
Definition: Address.cc:780
bool isSiteLocalAuto() const
Definition: Address.cc:297
bool isSockAddr() const
Definition: Address.cc:172
void getAddrInfo(struct addrinfo *&ai, int force=AF_UNSPEC) const
Definition: Address.cc:619
bool isLocalhost() const
Definition: Address.cc:269
void map6to4(const struct in6_addr &src, struct in_addr &dest) const
Definition: Address.cc:1020
void setAnyAddr()
NOTE: Does NOT clear the Port stored. Only the Address and Type.
Definition: Address.cc:197
struct sockaddr_in6 mSocketAddr_
Definition: Address.h:355
void map4to6(const struct in_addr &src, struct in6_addr &dest) const
Definition: Address.cc:999
bool operator<(Address const &rhs) const
Definition: Address.cc:789
static const struct in6_addr v4_localhost
Definition: Address.h:367
static const struct in6_addr v4_noaddr
Definition: Address.h:369
void getSockAddr(struct sockaddr_storage &addr, const int family) const
Definition: Address.cc:944
static const unsigned int MAX_IP6_STRLEN
Definition: Address.h:366
bool lookupHostIP(const char *s, bool nodns)
Definition: Address.cc:398
static void InitAddr(struct addrinfo *&ai)
Definition: Address.cc:688

 

Introduction

Documentation

Support

Miscellaneous