VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gaddress.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gaddress.h
19 ///
20 
21 #ifndef G_ADDRESS_H
22 #define G_ADDRESS_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gstrings.h"
27 #include "gexception.h"
28 #include <string>
29 
30 namespace GNet
31 {
32  class Address ;
33  class Address4 ;
34  class Address6 ;
35  class AddressStorage ;
36  class AddressStorageImp ;
37  int inet_pton_imp( int f , const char * p , void * result ) ;
38  const char * inet_ntop_imp( int f , void * ap , char * buffer , size_t n ) ;
39 }
40 
41 /// \class GNet::Address
42 /// The GNet::Address class encapsulates a TCP/UDP transport address. The
43 /// address is exposed as a 'sockaddr' structure for low-level socket
44 /// operations.
45 ///
46 /// A double pimple pattern is used for the implementation; this class
47 /// instantiates either a GNet::Address4 implementation sub-object or a
48 /// GNet::Address6 sub-object depending on the address family, and forwards
49 /// its method calls directly to the one instantiated sub-object. In an
50 /// IPv4-only build the GNet::Address6 is forward-declared but not defined
51 /// and all methods are forwarded to the GNet::Address4 sub-object.
52 ///
53 /// \see GNet::Resolver
54 ///
56 {
57 public:
58  class Family /// A type-safe enumerator for IP address family.
59  {
60  public:
61  static Family ipv4() ;
62  static Family ipv6() ;
63  bool operator==( const Family & other ) const ;
64  bool operator!=( const Family & other ) const ;
65  private:
66  explicit Family( bool is4_ ) ;
67  bool is4 ;
68  } ;
69 
70  G_EXCEPTION( Error , "address error" ) ;
71  G_EXCEPTION( BadFamily , "unsupported address family" ) ;
72  G_EXCEPTION( BadString , "invalid address" ) ;
73 
74  static bool supports( Family ) ;
75  ///< Returns true if the implementation supports the given address family.
76 
77  Address( const Address & ) ;
78  ///< Copy constructor.
79 
80  explicit Address( const AddressStorage & ) ;
81  ///< Constructor taking a storage object.
82 
83  Address( const sockaddr * addr , socklen_t len ) ;
84  ///< Constructor using a given sockaddr. Throws an exception if an
85  ///< invalid structure. See also: validData()
86 
87  Address( Family , unsigned int port ) ;
88  ///< Constructor for a wildcard INADDR_ANY address with the given port
89  ///< number. Throws an exception if an invalid port number.
90  /// \see validPort()
91 
92  explicit Address( const std::string & display_string ) ;
93  ///< Constructor taking a string originally obtained from displayString().
94  ///< Throws an exception if an invalid string. See also validString().
95 
96  Address( const std::string & ip , unsigned int port ) ;
97  ///< Constructor taking an ip-address and a port number. Throws an
98  ///< exception if an invalid string.
99 
100  ~Address() ;
101  ///< Destructor.
102 
103  static Address defaultAddress() ;
104  ///< Returns a default address, being the IPv4 wildcard address
105  ///< with a zero port number.
106 
107  static Address loopback( Family , unsigned int port = 0U ) ;
108  ///< Returns a loopback address.
109 
110  void operator=( const Address & addr ) ;
111  ///< Assignment operator.
112 
113  const sockaddr * address() const ;
114  ///< Returns the sockaddr address. Typically used when making socket
115  ///< system calls. Never returns nullptr.
116 
117  sockaddr * address() ;
118  ///< This is a non-const version of address() for compiling on systems
119  ///< which are not properly const-clean.
120 
121  socklen_t length() const;
122  ///< Returns the size of the sockaddr address. See address().
123 
124  std::string displayString() const ;
125  ///< Returns a string which represents the transport address for
126  ///< debugging and diagnostics purposes.
127 
128  std::string hostPartString() const ;
129  ///< Returns a string which represents the network address for
130  ///< debugging and diagnostics purposes.
131 
132  unsigned int port() const;
133  ///< Returns port part of the address.
134 
135  int domain() const ;
136  ///< Returns the address 'domain', eg. PF_INET.
137 
138  unsigned long scopeId( unsigned long default_ = 0UL ) const ;
139  ///< Returns the scope-id. Returns the default if scope-ids are not
140  ///< supported by the underlying address type.
141 
142  static bool validPort( unsigned int n ) ;
143  ///< Returns true if the port number is within the valid range. This
144  ///< can be used to avoid exceptions from the relevant constructors.
145 
146  static bool validString( const std::string & display_string , std::string * reason = nullptr ) ;
147  ///< Returns true if the display string is valid. This can be used
148  ///< to avoid exceptions from the relevant constructor.
149 
150  static bool validStrings( const std::string & ip , const std::string & port_string , std::string * reason = nullptr ) ;
151  ///< Returns true if the combined ip address string and port string
152  ///< is valid. This can be used to avoid exceptions from the relevant
153  ///< constructor.
154 
155  static bool validData( const sockaddr * , socklen_t len ) ;
156  ///< Returns true if the sockaddr data is valid. This can be used
157  ///< to avoid exceptions from the relevant constructor.
158 
159  bool sameHostPart( const Address & other ) const ;
160  ///< Returns true if the two addresses have the same host part
161  ///< (ie. the network address, ignoring the port number).
162 
163  void setPort( unsigned int port ) ;
164  ///< Sets the port number. Throws an exception if an invalid
165  ///< port number (ie. too big).
166 
167  G::StringArray wildcards() const ;
168  ///< Returns an ordered list of wildcard strings that match this
169  ///< address. The fully-address-specific string (eg. "192.168.0.1")
170  ///< comes first, and the most general match-all wildcard like
171  ///< "*.*.*.*" or "128.0.0.0/1" comes last.
172 
173  bool isLoopback() const ;
174  ///< Returns true if this is a loopback address.
175 
176  bool isLocal( std::string & reason ) const ;
177  ///< Returns true if this seems to be a local address.
178  ///< Returns an explanation by reference otherwise.
179 
180  Family family() const ;
181  ///< Returns the address family.
182 
183  bool operator==( const Address & ) const ;
184  ///< Comparison operator.
185 
186  bool operator!=( const Address & ) const ;
187  ///< Comparison operator.
188 
189 private:
190  Address( Family , unsigned int , int ) ; // loopback()
191 
192 private:
193  Address4 * m_4imp ;
194  Address6 * m_6imp ;
195 } ;
196 
197 /// \class GNet::AddressStorage
198 /// and hiding the definition of sockaddr_storage.
199 ///
201 {
202 public:
203  AddressStorage() ;
204  ///< Default constructor.
205 
206  ~AddressStorage() ;
207  ///< Destructor.
208 
209  sockaddr * p1() ;
210  ///< Returns the sockaddr pointer for accept()/getsockname()/getpeername()
211  ///< to write into.
212 
213  socklen_t * p2() ;
214  ///< Returns the length pointer for accept()/getsockname()/getpeername()
215  ///< to write into.
216 
217  const sockaddr * p() const ;
218  ///< Returns the pointer.
219 
220  socklen_t n() const ;
221  ///< Returns the length.
222 
223 private:
224  AddressStorage( const AddressStorage & ) ;
225  void operator=( const AddressStorage & ) ;
226 
227 private:
228  AddressStorageImp * m_imp ;
229 } ;
230 
231 inline GNet::Address::Family GNet::Address::Family::ipv4() { return Family(true) ; }
232 inline GNet::Address::Family GNet::Address::Family::ipv6() { return Family(false) ; }
233 inline bool GNet::Address::Family::operator==( const Family & other ) const { return is4 == other.is4 ; }
234 inline bool GNet::Address::Family::operator!=( const Family & other ) const { return is4 != other.is4 ; }
235 inline GNet::Address::Family::Family( bool is4_ ) : is4(is4_) {}
236 
237 #endif
static Address loopback(Family, unsigned int port=0U)
Returns a loopback address.
Family family() const
Returns the address family.
static bool validData(const sockaddr *, socklen_t len)
Returns true if the sockaddr data is valid.
and hiding the definition of sockaddr_storage.
Definition: gaddress.h:200
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:55
bool isLoopback() const
Returns true if this is a loopback address.
static bool validStrings(const std::string &ip, const std::string &port_string, std::string *reason=nullptr)
Returns true if the combined ip address string and port string is valid.
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
socklen_t * p2()
Returns the length pointer for accept()/getsockname()/getpeername() to write into.
sockaddr * p1()
Returns the sockaddr pointer for accept()/getsockname()/getpeername() to write into.
A pimple-pattern implementation class used by GNet::AddressStorage.
const sockaddr * address() const
Returns the sockaddr address.
bool sameHostPart(const Address &other) const
Returns true if the two addresses have the same host part (ie.
unsigned int port() const
Returns port part of the address.
bool operator==(const Address &) const
Comparison operator.
const sockaddr * p() const
Returns the pointer.
A 'sockaddr' wrapper class for IPv4 addresses.
Definition: gaddress4.h:38
AddressStorage()
Default constructor.
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
G::StringArray wildcards() const
Returns an ordered list of wildcard strings that match this address.
socklen_t length() const
Returns the size of the sockaddr address. See address().
std::string hostPartString() const
Returns a string which represents the network address for debugging and diagnostics purposes...
unsigned long scopeId(unsigned long default_=0UL) const
Returns the scope-id.
static bool validString(const std::string &display_string, std::string *reason=nullptr)
Returns true if the display string is valid.
int domain() const
Returns the address 'domain', eg. PF_INET.
~Address()
Destructor.
~AddressStorage()
Destructor.
static bool validPort(unsigned int n)
Returns true if the port number is within the valid range.
A type-safe enumerator for IP address family.
Definition: gaddress.h:58
bool isLocal(std::string &reason) const
Returns true if this seems to be a local address.
void operator=(const Address &addr)
Assignment operator.
Address(const Address &)
Copy constructor.
void setPort(unsigned int port)
Sets the port number.
socklen_t n() const
Returns the length.
static bool supports(Family)
Returns true if the implementation supports the given address family.
bool operator!=(const Address &) const
Comparison operator.
std::string displayString() const
Returns a string which represents the transport address for debugging and diagnostics purposes...
A 'sockaddr' wrapper class for IPv6 addresses.
Definition: gaddress6.h:38