gaddress.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
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 // ===
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 
31 namespace GNet
32 {
33  class Address ;
34  class AddressStorage ;
35  class AddressImp ;
36  class AddressStorageImp ;
37 }
38 
49 {
50 public:
51  G_EXCEPTION( Error , "address error" ) ;
52  G_EXCEPTION( BadFamily , "unsupported address family" ) ;
53  G_EXCEPTION( BadString , "invalid ip address string" ) ;
55  class Localhost
56  {} ;
58  class Broadcast
59  {} ;
60 
61  static int defaultDomain() ;
63 
64  Address( const Address & addr ) ;
66 
67  explicit Address( const AddressStorage & ) ;
69 
70  Address( const sockaddr * addr , socklen_t len ) ;
79 
80  Address( const hostent & h , unsigned int port ) ;
85 
86  Address( const hostent & h , const servent & s ) ;
91 
92  explicit Address( const servent & s ) ;
95 
96  explicit Address( unsigned int port ) ;
100 
101  Address( unsigned int port , Localhost ) ;
105 
106  Address( unsigned int port , Broadcast ) ;
110 
111  explicit Address( const std::string & display_string ) ;
118 
119  Address( const std::string & ip , unsigned int port ) ;
124 
125  ~Address() ;
127 
128  static Address invalidAddress() ;
131 
132  static Address broadcastAddress( unsigned int port ) ;
135 
136  static Address localhost( unsigned int port = 0U ) ;
140 
141  void operator=( const Address &addr ) ;
143 
144  const sockaddr * address() const ;
147 
148  sockaddr * address() ;
151 
152  socklen_t length() const;
155 
156  std::string displayString( bool with_port = true , bool with_scope_id = false ) const ;
159 
160  std::string hostString() const ;
163 
164  unsigned int port() const;
166 
167  int domain() const ;
169 
170  unsigned long scopeId( unsigned long default_ = 0UL ) const ;
174 
175  static bool validPort( unsigned int n ) ;
179 
180  static bool validString( const std::string & display_string , std::string * reason = NULL ) ;
184 
185  bool sameHost( const Address &other ) const ;
190 
191  bool isLocal( std::string & reason ) const ;
195 
196  bool isLocal( std::string & reason , const Address & local_hint ) const ;
199 
200  void setPort( unsigned int port ) ;
203 
204  G::Strings wildcards() const ;
210 
211  bool operator==( const Address & ) const ;
213 
214 private:
215  void setHost( const hostent & ) ;
216 
217 private:
218  AddressImp * m_imp ;
219 } ;
220 
226 {
227 public:
228  AddressStorage() ;
230 
231  ~AddressStorage() ;
233 
234  sockaddr * p1() ;
237 
238  socklen_t * p2() ;
241 
242  const sockaddr * p() const ;
244 
245  socklen_t n() const ;
247 
248 private:
249  AddressStorage( const AddressStorage & ) ;
250  void operator=( const AddressStorage & ) ;
251 
252 private:
253  AddressStorageImp * m_imp ;
254 } ;
255 
256 #endif
257 
G::Strings wildcards() const
Returns an ordered list of wildcard strings that match this address.
A pimple-pattern implementation class for GNet::Address.
static Address broadcastAddress(unsigned int port)
Returns a broadcast address.
static int defaultDomain()
Returns the default address 'domain', eg. PF_INET.
static Address localhost(unsigned int port=0U)
Returns a localhost ("loopback") address.
Network classes.
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
A helper class for calling getsockname() and getpeername() and hiding the definition of sockaddr_stor...
Definition: gaddress.h:225
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
socklen_t * p2()
Returns the length pointer for getsockname()/getpeername() to write into.
sockaddr * p1()
Returns the sockaddr pointer for getsockname()/getpeername() to write into.
static Address invalidAddress()
Returns an invalid address.
A pimple-pattern implementation class for GNet::AddressStorage.
const sockaddr * address() const
Returns the sockaddr address.
std::string hostString() const
Returns a string which represents the host part of the address for debugging and diagnostics purposes...
unsigned int port() const
Returns port part of address.
bool operator==(const Address &) const
Comparison operator.
const sockaddr * p() const
Returns the pointer.
Address(const Address &addr)
Copy constructor.
AddressStorage()
Default constructor.
std::string displayString(bool with_port=true, bool with_scope_id=false) const
Returns a string which represents the address for debugging and diagnostics purposes.
socklen_t length() const
Returns the size of the sockaddr address.
bool sameHost(const Address &other) const
Returns true if the two addresses have the same host part (ie.
unsigned long scopeId(unsigned long default_=0UL) const
Returns the scope-id.
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.
bool isLocal(std::string &reason) const
Returns true if the address is definitely local.
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
An overload discriminator class for GNet::Address.
Definition: gaddress.h:58
An overload discriminator class for GNet::Address.
Definition: gaddress.h:55
void operator=(const Address &addr)
Assignment operator.
static bool validString(const std::string &display_string, std::string *reason=NULL)
Returns true if the display string is valid.
void setPort(unsigned int port)
Sets the port number.
socklen_t n() const
Returns the length.