- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
48 class GNet::Address
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
225 class GNet::AddressStorage
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
GNet::Address::wildcards
G::Strings wildcards() const
Returns an ordered list of wildcard strings that match this address.
Definition: gaddress_ipv4.cpp:636
GNet::AddressImp
A pimple-pattern implementation class for GNet::Address.
Definition: gaddress_ipv4.cpp:36
GNet::Address::broadcastAddress
static Address broadcastAddress(unsigned int port)
Returns a broadcast address.
Definition: gaddress_ipv4.cpp:468
GNet::Address::defaultDomain
static int defaultDomain()
Returns the default address 'domain', eg. PF_INET.
Definition: gaddress_ipv4.cpp:626
gstrings.h
GNet::Address::localhost
static Address localhost(unsigned int port=0U)
Returns a localhost ("loopback") address.
Definition: gaddress_ipv4.cpp:463
GNet
Network classes.
G::Strings
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
GNet::AddressStorage
A helper class for calling getsockname() and getpeername() and hiding the definition of sockaddr_stor...
Definition: gaddress.h:225
GNet::Address
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
GNet::AddressStorage::p2
socklen_t * p2()
Returns the length pointer for getsockname()/getpeername() to write into.
Definition: gaddress_ipv4.cpp:659
GNet::AddressStorage::p1
sockaddr * p1()
Returns the sockaddr pointer for getsockname()/getpeername() to write into.
Definition: gaddress_ipv4.cpp:654
GNet::Address::invalidAddress
static Address invalidAddress()
Returns an invalid address.
Definition: gaddress_ipv4.cpp:458
GNet::AddressStorageImp
A pimple-pattern implementation class for GNet::AddressStorage.
Definition: gaddress_ipv4.cpp:96
GNet::Address::address
const sockaddr * address() const
Returns the sockaddr address.
Definition: gaddress_ipv4.cpp:601
GNet::Address::hostString
std::string hostString() const
Returns a string which represents the host part of the address for debugging and diagnostics purposes...
Definition: gaddress_ipv4.cpp:586
GNet::Address::port
unsigned int port() const
Returns port part of address.
Definition: gaddress_ipv4.cpp:611
GNet::Address::operator==
bool operator==(const Address &) const
Comparison operator.
Definition: gaddress_ipv4.cpp:544
GNet::AddressStorage::p
const sockaddr * p() const
Returns the pointer.
Definition: gaddress_ipv4.cpp:664
GNet::Address::Address
Address(const Address &addr)
Copy constructor.
Definition: gaddress_ipv4.cpp:523
GNet::AddressStorage::AddressStorage
AddressStorage()
Default constructor.
Definition: gaddress_ipv4.cpp:643
GNet::Address::displayString
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.
Definition: gaddress_ipv4.cpp:581
GNet::Address::length
socklen_t length() const
Returns the size of the sockaddr address.
Definition: gaddress_ipv4.cpp:606
GNet::Address::sameHost
bool sameHost(const Address &other) const
Returns true if the two addresses have the same host part (ie.
Definition: gaddress_ipv4.cpp:576
GNet::Address::scopeId
unsigned long scopeId(unsigned long default_=0UL) const
Returns the scope-id.
Definition: gaddress_ipv4.cpp:616
GNet::Address::domain
int domain() const
Returns the address 'domain', eg. PF_INET.
Definition: gaddress_ipv4.cpp:631
GNet::Address::~Address
~Address()
Destructor.
Definition: gaddress_ipv4.cpp:534
GNet::AddressStorage::~AddressStorage
~AddressStorage()
Destructor.
Definition: gaddress_ipv4.cpp:649
GNet::Address::validPort
static bool validPort(unsigned int n)
Returns true if the port number is within the valid range.
Definition: gaddress_ipv4.cpp:621
GNet::Address::isLocal
bool isLocal(std::string &reason) const
Returns true if the address is definitely local.
Definition: gaddress_ipv4.cpp:549
gexception.h
G_EXCEPTION
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
GNet::Address::Broadcast
An overload discriminator class for GNet::Address.
Definition: gaddress.h:58
GNet::Address::Localhost
An overload discriminator class for GNet::Address.
Definition: gaddress.h:55
GNet::Address::operator=
void operator=(const Address &addr)
Assignment operator.
Definition: gaddress_ipv4.cpp:528
GNet::Address::validString
static bool validString(const std::string &display_string, std::string *reason=NULL)
Returns true if the display string is valid.
Definition: gaddress_ipv4.cpp:591
GNet::Address::setPort
void setPort(unsigned int port)
Sets the port number.
Definition: gaddress_ipv4.cpp:539
GNet::AddressStorage::n
socklen_t n() const
Returns the length.
Definition: gaddress_ipv4.cpp:669