Address.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 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 
204  char* toStr(char *buf, const unsigned int blen, int force = AF_UNSPEC) const;
205 
214  char* toUrl(char *buf, unsigned int len) const;
215 
224  unsigned int toHostStr(char *buf, const unsigned int len) const;
225 
230  bool fromHost(const char *hostWithoutPort);
231 
241  bool getReverseString(char buf[MAX_IPSTRLEN], int show_type = AF_UNSPEC) const;
242 
248  int matchIPAddr(const Address &rhs) const;
249 
255  int compareWhole(const Ip::Address &rhs) const;
256 
273  void getAddrInfo(struct addrinfo *&ai, int force = AF_UNSPEC) const;
274 
278  static void FreeAddr(struct addrinfo *&ai);
279 
286  static void InitAddr(struct addrinfo *&ai);
287 
294  bool GetHostByName(const char *s);
295 
296 public:
297  /* FIXME: When C => C++ conversion is done will be fully private.
298  * Legacy Transition Methods.
299  * These are here solely to simplify the transition
300  * when moving from converted code to unconverted
301  * these functions can be used to convert this object
302  * and pull out the data needed by the unconverted code
303  * they are intentionaly hard to use, use getAddrInfo() instead.
304  * these functiosn WILL NOT be in the final public API after transition.
305  */
306 
307  void getSockAddr(struct sockaddr_storage &addr, const int family) const;
308  void getSockAddr(struct sockaddr_in &) const;
309  bool getInAddr(struct in_addr &) const; /* false if could not convert IPv6 down to IPv4 */
310  void getSockAddr(struct sockaddr_in6 &) const;
311  void getInAddr(struct in6_addr &) const;
312 
313 private:
314  /* Conversion for dual-type internals */
315 
316  bool getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const;
317 
318  bool getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const;
319 
320  void map4to6(const struct in_addr &src, struct in6_addr &dest) const;
321 
322  void map6to4(const struct in6_addr &src, struct in_addr &dest) const;
323 
324  // Worker behind GetHostName and char* converters
325  bool lookupHostIP(const char *s, bool nodns);
326 
327  /* variables */
328  struct sockaddr_in6 mSocketAddr_;
329 
330 private:
331  /* Internally used constants */
332  static const unsigned int STRLEN_IP4A = 16; // aaa.bbb.ccc.ddd\0
333  static const unsigned int STRLEN_IP4R = 28; // ddd.ccc.bbb.aaa.in-addr.arpa.\0
334  static const unsigned int STRLEN_IP4S = 21; // ddd.ccc.bbb.aaa:ppppp\0
335  static const unsigned int MAX_IP4_STRLEN = STRLEN_IP4R;
336  static const unsigned int STRLEN_IP6A = 42; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]/0
337  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
338  static const unsigned int STRLEN_IP6S = 48; // [ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:00000/0
339  static const unsigned int MAX_IP6_STRLEN = STRLEN_IP6R;
340  static const struct in6_addr v4_localhost;
341  static const struct in6_addr v4_anyaddr;
342  static const struct in6_addr v4_noaddr;
343  static const struct in6_addr v6_noaddr;
344 };
345 
346 inline std::ostream &
347 operator << (std::ostream &os, const Address &ipa)
348 {
349  char buf[MAX_IPSTRLEN];
350  os << ipa.toUrl(buf,MAX_IPSTRLEN);
351  return os;
352 }
353 
354 // WAS _sockaddr_in_list in an earlier incarnation
356 {
357 public:
358  Address_list() { next = NULL; };
359  ~Address_list() { if (next) delete next; next = NULL; };
360 
361  Address s;
363 };
364 
365 } // namespace Ip
366 
368 
369 #endif /* _SQUID_SRC_IP_ADDRESS_H */
370 
bool setIPv4()
Definition: Address.cc:217
int cidr() const
Definition: Address.cc:44
~Address()
Definition: Address.h:54
unsigned short port() const
Definition: Address.cc:771
Address & operator=(struct sockaddr_in const &s)
Definition: Address.cc:423
Definition: client_db.h:18
bool operator!=(Address const &s) const
Definition: Address.cc:729
static const unsigned int MAX_IP4_STRLEN
Definition: Address.h:335
bool getInAddr(struct in_addr &) const
Definition: Address.cc:1023
static void FreeAddr(struct addrinfo *&ai)
Definition: Address.cc:679
void setLocalhost()
Definition: Address.cc:248
bool isNoAddr() const
Definition: Address.cc:277
bool isLocalhost() const
Definition: Address.cc:242
Address_list * next
Definition: Address.h:362
bool getReverseString(char buf[MAX_IPSTRLEN], int show_type=AF_UNSPEC) const
Definition: Address.cc:331
bool isSiteLocalAuto() const
Definition: Address.cc:270
int applyMask(const Address &mask)
Definition: Address.cc:87
struct sockaddr_in6 mSocketAddr_
Definition: Address.h:328
bool operator>=(Address const &rhs) const
Definition: Address.cc:744
static const struct in6_addr v4_noaddr
Definition: Address.h:342
char * toUrl(char *buf, unsigned int len) const
Definition: Address.cc:877
bool operator==(Address const &s) const
Definition: Address.cc:723
bool getReverseString6(char buf[MAX_IPSTRLEN], const struct in6_addr &dat) const
Definition: Address.cc:292
bool operator<=(Address const &rhs) const
Definition: Address.cc:735
bool isIPv4() const
Definition: Address.cc:151
void setNoAddr()
Definition: Address.cc:285
static const unsigned int STRLEN_IP6A
Definition: Address.h:336
char * toStr(char *buf, const unsigned int blen, int force=AF_UNSPEC) const
Definition: Address.cc:795
bool lookupHostIP(const char *s, bool nodns)
Definition: Address.cc:371
static const struct in6_addr v4_localhost
Definition: Address.h:340
static const unsigned int MAX_IP6_STRLEN
Definition: Address.h:339
static void InitAddr(struct addrinfo *&ai)
Definition: Address.cc:661
static const unsigned int STRLEN_IP4S
Definition: Address.h:334
void * addr
Definition: membanger.c:46
bool isIPv6() const
Definition: Address.cc:157
bool isSiteLocal6() const
Definition: Address.cc:260
void setEmpty()
Fast reset of the stored content to what would be after default constructor.
Definition: Address.cc:177
bool operator>(Address const &rhs) const
Definition: Address.cc:753
bool isAnyAddr() const
Definition: Address.cc:163
void getAddrInfo(struct addrinfo *&ai, int force=AF_UNSPEC) const
Definition: Address.cc:592
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:337
bool isSockAddr() const
Definition: Address.cc:145
static const struct in6_addr v6_noaddr
Definition: Address.h:343
static const unsigned int STRLEN_IP4A
Definition: Address.h:332
bool GetHostByName(const char *s)
Definition: Address.cc:365
bool fromHost(const char *hostWithoutPort)
Definition: Address.cc:901
std::ostream & operator<<(std::ostream &os, const Address &ipa)
Definition: Address.h:347
void setAnyAddr()
NOTE: Does NOT clear the Port stored. Ony the Address and Type.
Definition: Address.cc:170
void getSockAddr(struct sockaddr_storage &addr, const int family) const
Definition: Address.cc:927
int compareWhole(const Ip::Address &rhs) const
Definition: Address.cc:717
#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:1003
int matchIPAddr(const Address &rhs) const
Definition: Address.cc:696
void parse_IpAddress_list_token(Ip::Address_list **, char *)
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition: Address.cc:845
bool operator<(Address const &rhs) const
Definition: Address.cc:762
bool getReverseString4(char buf[MAX_IPSTRLEN], const struct in_addr &dat) const
Definition: Address.cc:319
static const struct in6_addr v4_anyaddr
Definition: Address.h:341
#define NULL
Definition: types.h:166
void map4to6(const struct in_addr &src, struct in6_addr &dest) const
Definition: Address.cc:982
static const unsigned int STRLEN_IP6S
Definition: Address.h:338
static const unsigned int STRLEN_IP4R
Definition: Address.h:333

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors