E-MailRelay
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 
37 {
38  return f == Family::ipv4() ;
39 }
40 
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 
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 
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 
95 {
96  delete m_4imp ;
97 }
98 
99 void GNet::Address::setPort( unsigned int port )
100 {
101  m_4imp->setPort( port ) ;
102 }
103 
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 
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 
185 {
186  return Address4::domain() ;
187 }
188 
190 {
191  return Family::ipv4() ;
192 }
193 
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 ///
205 {
206 public:
208  socklen_t n ;
209 } ;
210 
211 // ==
212 
214  m_imp(new AddressStorageImp)
215 {
216  m_imp->n = sizeof(Address4::union_type) ;
217 }
218 
220 {
221  delete m_imp ;
222 }
223 
225 {
226  return &(m_imp->u.general) ;
227 }
228 
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 
static Address loopback(Family, unsigned int port=0U)
Returns a loopback address.
Used by GNet::Address4 to cast between sockaddr and sockaddr_in.
Definition: gaddress4.h:43
Family family() const
Returns the address family.
static bool validData(const sockaddr *, socklen_t len)
Returns true if the sockaddr data is valid.
A helper class for calling accept(), getsockname() and getpeername() and hiding the definition of soc...
Definition: gaddress.h:201
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:54
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 network-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:37
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 transport-address 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:57
bool isLocal(std::string &reason) const
Returns true if this seems to be a local address (for some definition of 'local').
void operator=(const Address &addr)
Assignment operator.
Address(const Address &)
Copy constructor.
This file is formatted for side-by-side comparison with gaddress6.h.
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...