E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gaddress_ipv4.cpp
1 //
2 // Copyright (C) 2001-2018 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 // ===
17 //
18 // gaddress_ipv4.cpp
19 //
20
21 #include "gdef.h"
22 #include "gaddress.h"
23 #include "gaddress4.h"
24 #include <algorithm>
25 #include <cstring>
26
27 namespace
28 {
29 void check( GNet::Address::Family f )
30 {
31 if( !GNet::Address::supports(f) )
32 throw GNet::Address::BadFamily() ;
33 }
34 }
35
36 bool GNet::Address::supports( Family f )
37 {
38 return f == Family::ipv4() ;
39 }
40
41 GNet::Address GNet::Address::defaultAddress()
42 {
43 return Address( Family::ipv4() , 0U ) ;
44 }
45
46 GNet::Address::Address( Family f , unsigned int port ) :
47 m_4imp( new Address4(port) )
48 {
49 check( f ) ;
50 }
51
52 GNet::Address::Address( const AddressStorage & storage ) :
53 m_4imp( new Address4(storage.p(),storage.n()) )
54 {
55 }
56
57 GNet::Address::Address( const sockaddr * addr , socklen_t len ) :
58 m_4imp( new Address4(addr,len) )
59 {
60 }
61
62 GNet::Address::Address( const std::string & s ) :
63 m_4imp( new Address4(s) )
64 {
65 }
66
67 GNet::Address::Address( const std::string & s , unsigned int port ) :
68 m_4imp( new Address4(s,port) )
69 {
70 }
71
72 GNet::Address::Address( const Address & other ) :
73 m_4imp( new Address4(*other.m_4imp) )
74 {
75 }
76
77 GNet::Address::Address( Family f , unsigned int port , int loopback_overload ) :
78 m_4imp( new Address4(port,loopback_overload) )
79 {
80 check( f ) ;
81 }
82
83 GNet::Address GNet::Address::loopback( Family f , unsigned int port )
84 {
85 return Address( f , port , 1 ) ;
86 }
87
88 void GNet::Address::operator=( const Address & addr )
89 {
90 Address temp( addr ) ;
91 std::swap( m_4imp , temp.m_4imp ) ;
92 }
93
94 GNet::Address::~Address()
95 {
96 delete m_4imp ;
97 }
98
99 void GNet::Address::setPort( unsigned int port )
100 {
101 m_4imp->setPort( port ) ;
102 }
103
104 bool GNet::Address::isLoopback() const
105 {
106 return m_4imp->isLoopback() ;
107 }
108
109 bool GNet::Address::isLocal( std::string & reason ) const
110 {
111 return m_4imp->isLocal( reason ) ;
112 }
113
114 bool GNet::Address::operator==( const Address & other ) const
115 {
116 return m_4imp->same(*other.m_4imp) ;
117 }
118
119 bool GNet::Address::operator!=( const Address & other ) const
120 {
121 return !( *this == other ) ;
122 }
123
124 bool GNet::Address::sameHostPart( const Address & other ) const
125 {
126 return m_4imp->sameHostPart(*other.m_4imp) ;
127 }
128
129 std::string GNet::Address::displayString() const
130 {
131 return m_4imp->displayString() ;
132 }
133
134 std::string GNet::Address::hostPartString() const
135 {
136 return m_4imp->hostPartString() ;
137 }
138
139 bool GNet::Address::validString( const std::string & s , std::string * reason_p )
140 {
141 return Address4::validString( s , reason_p ) ;
142 }
143
144 bool GNet::Address::validStrings( const std::string & s1 , const std::string & s2 , std::string * reason_p )
145 {
146 return Address4::validStrings( s1 , s2 , reason_p ) ;
147 }
148
149 sockaddr * GNet::Address::address()
150 {
151 return m_4imp->address() ;
152 }
153
154 const sockaddr * GNet::Address::address() const
155 {
156 return m_4imp->address() ;
157 }
158
159 socklen_t GNet::Address::length() const
160 {
161 return Address4::length() ;
162 }
163
164 unsigned int GNet::Address::port() const
165 {
166 return m_4imp->port() ;
167 }
168
169 unsigned long GNet::Address::scopeId( unsigned long default_ ) const
170 {
171 return default_ ;
172 }
173
174 bool GNet::Address::validPort( unsigned int port )
175 {
176 return Address4::validPort( port ) ;
177 }
178
179 bool GNet::Address::validData( const sockaddr * addr , socklen_t len )
180 {
181 return Address4::validData( addr , len ) ;
182 }
183
184 int GNet::Address::domain() const
185 {
186 return Address4::domain() ;
187 }
188
189 GNet::Address::Family GNet::Address::family() const
190 {
191 return Family::ipv4() ;
192 }
193
194 G::StringArray GNet::Address::wildcards() const
195 {
196 return m_4imp->wildcards() ;
197 }
198
199 // ==
200
201 /// \class GNet::AddressStorageImp
202 /// A pimple-pattern implementation class used by GNet::AddressStorage.
203 ///
204 class GNet::AddressStorageImp
205 {
206 public:
207 Address4::union_type u ;
208 socklen_t n ;
209 } ;
210
211 // ==
212
213 GNet::AddressStorage::AddressStorage() :
214 m_imp(new AddressStorageImp)
215 {
216 m_imp->n = sizeof(Address4::union_type) ;
217 }
218
219 GNet::AddressStorage::~AddressStorage()
220 {
221 delete m_imp ;
222 }
223
224 sockaddr * GNet::AddressStorage::p1()
225 {
226 return &(m_imp->u.general) ;
227 }
228
229 socklen_t * GNet::AddressStorage::p2()
230 {
231 return &m_imp->n ;
232 }
233
234 const sockaddr * GNet::AddressStorage::p() const
235 {
236 return &m_imp->u.general ;
237 }
238
239 socklen_t GNet::AddressStorage::n() const
240 {
241 return m_imp->n ;
242 }
243
244 // ==
245
246 #if ! GCONFIG_HAVE_INET_PTON
247 // fallback implementation for inet_pton() -- see gdef.h
248 int GNet::inet_pton_imp( int f , const char * p , void * result )
249 {
250 if( p == nullptr || result == nullptr )
251 {
252 return 0 ; // just in case
253 }
254 else if( f == AF_INET )
255 {
256 static sockaddr_in sa_zero ;
257 sockaddr_in sa = sa_zero ;
258 sa.sin_family = AF_INET ;
259 sa.sin_addr.s_addr = inet_addr( p ) ;
260 *reinterpret_cast<struct in_addr*>(result) = sa.sin_addr ;
261 return 1 ;
262 }
263 else
264 {
265 return -1 ;
266 }
267 }
268 #endif
269
270 #if ! GCONFIG_HAVE_INET_NTOP
271 // fallback implementation for inet_ntop() -- see gdef.h
272 const char * GNet::inet_ntop_imp( int f , void * ap , char * buffer , size_t n )
273 {
274 if( f == AF_INET )
275 {
276 std::ostringstream ss ;
277 struct in_addr a = *reinterpret_cast<struct in_addr*>(ap) ;
278 ss << inet_ntoa( a ) ; // ignore warnings - this is not used if inet_ntop is available
279 if( n <= ss.str().length() ) return nullptr ;
280 std::strncpy( buffer , ss.str().c_str() , n ) ;
281 return buffer ;
282 }
283 else
284 {
285 return nullptr ;
286 }
287 }
288 #endif
289
GNet::Address::loopback
static Address loopback(Family, unsigned int port=0U)
Returns a loopback address.
Definition: gaddress_ipv4.cpp:83
GNet::Address4::union_type
Used by GNet::Address4 to cast between sockaddr and sockaddr_in.
Definition: gaddress4.h:43
GNet::Address::family
Family family() const
Returns the address family.
Definition: gaddress_ipv4.cpp:189
GNet::Address::validData
static bool validData(const sockaddr *, socklen_t len)
Returns true if the sockaddr data is valid.
Definition: gaddress_ipv4.cpp:179
GNet::AddressStorage
A helper class for calling accept(), getsockname() and getpeername() and hiding the definition of soc...
Definition: gaddress.h:201
GNet::Address
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:54
GNet::Address::isLoopback
bool isLoopback() const
Returns true if this is a loopback address.
Definition: gaddress_ipv4.cpp:104
GNet::Address::validStrings
static bool validStrings(const std::string &ip, const std::string &port_string, std::string *reason=nullptr)
Returns true if the combined network-address string and port string is valid.
Definition: gaddress_ipv4.cpp:144
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
GNet::AddressStorage::p2
socklen_t * p2()
Returns the length pointer for accept()/getsockname()/getpeername() to write into.
Definition: gaddress_ipv4.cpp:229
GNet::AddressStorage::p1
sockaddr * p1()
Returns the sockaddr pointer for accept()/getsockname()/getpeername() to write into.
Definition: gaddress_ipv4.cpp:224
GNet::AddressStorageImp
A pimple-pattern implementation class used by GNet::AddressStorage.
Definition: gaddress_ipv4.cpp:204
GNet::Address::address
const sockaddr * address() const
Returns the sockaddr address.
Definition: gaddress_ipv4.cpp:154
GNet::Address::sameHostPart
bool sameHostPart(const Address &other) const
Returns true if the two addresses have the same host part (ie.
Definition: gaddress_ipv4.cpp:124
GNet::Address::port
unsigned int port() const
Returns port part of the address.
Definition: gaddress_ipv4.cpp:164
GNet::Address::operator==
bool operator==(const Address &) const
Comparison operator.
Definition: gaddress_ipv4.cpp:114
GNet::AddressStorage::p
const sockaddr * p() const
Returns the pointer.
Definition: gaddress_ipv4.cpp:234
GNet::Address4
A 'sockaddr' wrapper class for IPv4 addresses.
Definition: gaddress4.h:37
GNet::AddressStorage::AddressStorage
AddressStorage()
Default constructor.
Definition: gaddress_ipv4.cpp:213
GNet::Address::defaultAddress
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
Definition: gaddress_ipv4.cpp:41
GNet::Address::wildcards
G::StringArray wildcards() const
Returns an ordered list of wildcard strings that match this address.
Definition: gaddress_ipv4.cpp:194
GNet::Address::length
socklen_t length() const
Returns the size of the sockaddr address. See address().
Definition: gaddress_ipv4.cpp:159
GNet::Address::hostPartString
std::string hostPartString() const
Returns a string which represents the network address for debugging and diagnostics purposes...
Definition: gaddress_ipv4.cpp:134
GNet::Address::scopeId
unsigned long scopeId(unsigned long default_=0UL) const
Returns the scope-id.
Definition: gaddress_ipv4.cpp:169
GNet::Address::validString
static bool validString(const std::string &display_string, std::string *reason=nullptr)
Returns true if the transport-address display string is valid.
Definition: gaddress_ipv4.cpp:139
GNet::Address::domain
int domain() const
Returns the address 'domain', eg. PF_INET.
Definition: gaddress_ipv4.cpp:184
GNet::Address::~Address
~Address()
Destructor.
Definition: gaddress_ipv4.cpp:94
GNet::AddressStorage::~AddressStorage
~AddressStorage()
Destructor.
Definition: gaddress_ipv4.cpp:219
GNet::Address::validPort
static bool validPort(unsigned int n)
Returns true if the port number is within the valid range.
Definition: gaddress_ipv4.cpp:174
GNet::Address::Family
A type-safe enumerator for IP address family.
Definition: gaddress.h:57
GNet::Address::isLocal
bool isLocal(std::string &reason) const
Returns true if this seems to be a local address (for some definition of 'local').
Definition: gaddress_ipv4.cpp:109
GNet::Address::operator=
void operator=(const Address &addr)
Assignment operator.
Definition: gaddress_ipv4.cpp:88
GNet::Address::Address
Address(const Address &)
Copy constructor.
Definition: gaddress_ipv4.cpp:72
gaddress4.h
This file is formatted for side-by-side comparison with gaddress6.h.
gaddress.h
GNet::Address::setPort
void setPort(unsigned int port)
Sets the port number.
Definition: gaddress_ipv4.cpp:99
GNet::AddressStorage::n
socklen_t n() const
Returns the length.
Definition: gaddress_ipv4.cpp:239
GNet::Address::supports
static bool supports(Family)
Returns true if the implementation supports the given address family.
Definition: gaddress_ipv4.cpp:36
GNet::Address::operator!=
bool operator!=(const Address &) const
Comparison operator.
Definition: gaddress_ipv4.cpp:119
GNet::Address::displayString
std::string displayString() const
Returns a string which represents the transport address for debugging and diagnostics purposes...
Definition: gaddress_ipv4.cpp:129