Address.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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 namespace Ip
35 {
36 
40 class Address
41 {
42 
43 public:
46  Address() { setEmpty(); }
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*);
54  ~Address() {}
59  Address& operator =(struct sockaddr_in const &s);
60  Address& operator =(struct sockaddr_storage const &s);
61  Address& operator =(struct in_addr const &s);
62  Address& operator =(struct in6_addr const &s);
63  Address& operator =(struct sockaddr_in6 const &s);
64  bool operator =(const struct hostent &s);
65  bool operator =(const struct addrinfo &s);
66  bool operator =(const char *s);
71  bool operator ==(Address const &s) const;
72  bool operator !=(Address const &s) const;
73  bool operator >=(Address const &rhs) const;
74  bool operator <=(Address const &rhs) const;
75  bool operator >(Address const &rhs) const;
76  bool operator <(Address const &rhs) const;
77 
78 public:
79  /* methods */
80 
85  bool isIPv4() const;
86 
91  bool isIPv6() const;
92 
98  bool isSockAddr() const;
99 
106  bool isAnyAddr() const;
107 
113  bool isNoAddr() const;
114 
121  bool isLocalhost() const;
122 
128  bool isSiteLocal6() const;
129 
135  bool isSiteLocalAuto() const;
136 
143  unsigned short port() const;
144 
151  unsigned short port(unsigned short port);
152 
155  void setAnyAddr();
156 
159  void setNoAddr();
160 
163  void setLocalhost();
164 
166  void setEmpty();
167 
173  bool setIPv4();
174 
179  int cidr() const;
180 
184  int applyMask(const Address &mask);
185 
191  bool applyMask(const unsigned int cidr, int mtype);
192 
196  void applyClientMask(const Address &mask);
197 
209  char* toStr(char *buf, const unsigned int blen, int force = AF_UNSPEC) const;
210 
219  char* toUrl(char *buf, unsigned int len) const;
220 
229  unsigned int toHostStr(char *buf, const unsigned int len) const;
230 
235  bool fromHost(const char *hostWithoutPort);
236 
246  bool getReverseString(char buf[MAX_IPSTRLEN], int show_type = AF_UNSPEC) const;
247 
253  int matchIPAddr(const Address &rhs) const;
254 
260  int compareWhole(const Ip::Address &rhs) const;
261 
278  void getAddrInfo(struct addrinfo *&ai, int force = AF_UNSPEC) const;
279 
283  static void FreeAddr(struct addrinfo *&ai);
284 
291  static void InitAddr(struct addrinfo *&ai);
292 
299  bool GetHostByName(const char *s);
300 
301 public:
302  /* FIXME: When C => C++ conversion is done will be fully private.
303  * Legacy Transition Methods.
304  * These are here solely to simplify the transition
305  * when moving from converted code to unconverted
306  * these functions can be used to convert this object
307  * and pull out the data needed by the unconverted code
308  * they are intentionaly hard to use, use getAddrInfo() instead.
309  * these functiosn WILL NOT be in the final public API after transition.
310  */
311 
312  void getSockAddr(struct sockaddr_storage &addr, const int family) const;
313  void getSockAddr(struct sockaddr_in &) const;
314  bool getInAddr(struct in_addr &) const; /* false if could not convert IPv6 down to IPv4 */
315  void getSockAddr(struct sockaddr_in6 &) const;
316  void getInAddr(struct in6_addr &) const;
317 
318 private:
319  /* Conversion for dual-type internals */
320 
321  bool getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const;
322 
323  bool getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const;
324 
325  void map4to6(const struct in_addr &src, struct in6_addr &dest) const;
326 
327  void map6to4(const struct in6_addr &src, struct in_addr &dest) const;
328 
329  // Worker behind GetHostName and char* converters
330  bool lookupHostIP(const char *s, bool nodns);
331 
332  /* variables */
333  struct sockaddr_in6 mSocketAddr_;
334 
335 private:
336  /* Internally used constants */
337  static const unsigned int STRLEN_IP4A = 16; // aaa.bbb.ccc.ddd\0
338  static const unsigned int STRLEN_IP4R = 28; // ddd.ccc.bbb.aaa.in-addr.arpa.\0
339  static const unsigned int STRLEN_IP4S = 21; // ddd.ccc.bbb.aaa:ppppp\0
340  static const unsigned int MAX_IP4_STRLEN = STRLEN_IP4R;
341  static const unsigned int STRLEN_IP6A = 42; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]/0
342  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
343  static const unsigned int STRLEN_IP6S = 48; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:00000/0
344  static const unsigned int MAX_IP6_STRLEN = STRLEN_IP6R;
345  static const struct in6_addr v4_localhost;
346  static const struct in6_addr v4_anyaddr;
347  static const struct in6_addr v4_noaddr;
348  static const struct in6_addr v6_noaddr;
349 };
350 
351 inline std::ostream &
352 operator << (std::ostream &os, const Address &ipa)
353 {
354  char buf[MAX_IPSTRLEN];
355  os << ipa.toUrl(buf,MAX_IPSTRLEN);
356  return os;
357 }
358 
359 // WAS _sockaddr_in_list in an earlier incarnation
361 {
362 public:
363  Address_list() { next = NULL; };
364  ~Address_list() { if (next) delete next; next = NULL; };
365 
366  Address s;
368 };
369 
370 } // namespace Ip
371 
373 
374 #endif /* _SQUID_SRC_IP_ADDRESS_H */
375 
bool setIPv4()
Definition: Address.cc:224
int cidr() const
Definition: Address.cc:44
~Address()
Definition: Address.h:54
unsigned short port() const
Definition: Address.cc:778
Address & operator=(struct sockaddr_in const &s)
Definition: Address.cc:430
Definition: client_db.h:22
bool operator!=(Address const &s) const
Definition: Address.cc:736
static const unsigned int MAX_IP4_STRLEN
Definition: Address.h:340
bool getInAddr(struct in_addr &) const
Definition: Address.cc:1030
static void FreeAddr(struct addrinfo *&ai)
Definition: Address.cc:686
void setLocalhost()
Definition: Address.cc:255
bool isNoAddr() const
Definition: Address.cc:284
bool isLocalhost() const
Definition: Address.cc:249
Address_list * next
Definition: Address.h:367
bool getReverseString(char buf[MAX_IPSTRLEN], int show_type=AF_UNSPEC) const
Definition: Address.cc:338
bool isSiteLocalAuto() const
Definition: Address.cc:277
int applyMask(const Address &mask)
Definition: Address.cc:87
struct sockaddr_in6 mSocketAddr_
Definition: Address.h:333
void applyClientMask(const Address &mask)
Definition: Address.cc:105
bool operator>=(Address const &rhs) const
Definition: Address.cc:751
static const struct in6_addr v4_noaddr
Definition: Address.h:347
char * toUrl(char *buf, unsigned int len) const
Definition: Address.cc:884
bool operator==(Address const &s) const
Definition: Address.cc:730
bool getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const
Definition: Address.cc:299
bool operator<=(Address const &rhs) const
Definition: Address.cc:742
bool isIPv4() const
Definition: Address.cc:158
void setNoAddr()
Definition: Address.cc:292
static const unsigned int STRLEN_IP6A
Definition: Address.h:341
char * toStr(char *buf, const unsigned int blen, int force=AF_UNSPEC) const
Definition: Address.cc:802
bool lookupHostIP(const char *s, bool nodns)
Definition: Address.cc:378
static const struct in6_addr v4_localhost
Definition: Address.h:345
static const unsigned int MAX_IP6_STRLEN
Definition: Address.h:344
static void InitAddr(struct addrinfo *&ai)
Definition: Address.cc:668
static const unsigned int STRLEN_IP4S
Definition: Address.h:339
void * addr
Definition: membanger.c:46
bool isIPv6() const
Definition: Address.cc:164
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
bool operator>(Address const &rhs) const
Definition: Address.cc:760
bool isAnyAddr() const
Definition: Address.cc:170
void getAddrInfo(struct addrinfo *&ai, int force=AF_UNSPEC) const
Definition: Address.cc:599
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
void const char * buf
Definition: stub_helper.cc:16
static const unsigned int STRLEN_IP6R
Definition: Address.h:342
bool isSockAddr() const
Definition: Address.cc:152
static const struct in6_addr v6_noaddr
Definition: Address.h:348
static const unsigned int STRLEN_IP4A
Definition: Address.h:337
bool GetHostByName(const char *s)
Definition: Address.cc:372
bool fromHost(const char *hostWithoutPort)
Definition: Address.cc:908
std::ostream & operator<<(std::ostream &os, const Address &ipa)
Definition: Address.h:352
void setAnyAddr()
NOTE: Does NOT clear the Port stored. Ony the Address and Type.
Definition: Address.cc:177
void getSockAddr(struct sockaddr_storage &addr, const int family) const
Definition: Address.cc:934
int compareWhole(const Ip::Address &rhs) const
Definition: Address.cc:724
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:23
void map6to4(const struct in6_addr &src, struct in_addr &dest) const
Definition: Address.cc:1010
int matchIPAddr(const Address &rhs) const
Definition: Address.cc:703
void parse_IpAddress_list_token(Ip::Address_list **, char *)
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition: Address.cc:852
bool operator<(Address const &rhs) const
Definition: Address.cc:769
bool getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const
Definition: Address.cc:326
static const struct in6_addr v4_anyaddr
Definition: Address.h:346
#define NULL
Definition: types.h:166
void map4to6(const struct in_addr &src, struct in6_addr &dest) const
Definition: Address.cc:989
static const unsigned int STRLEN_IP6S
Definition: Address.h:343
static const unsigned int STRLEN_IP4R
Definition: Address.h:338

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors