E-MailRelay
gaddress4.h
Go to the documentation of this file.
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 /// \file gaddress4.h
19 ///
20 /// This file is formatted for side-by-side comparison with gaddress6.h.
21 
22 #ifndef G_NET_ADDRESS4__H
23 #define G_NET_ADDRESS4__H
24 
25 #include "gdef.h"
26 #include "gaddress.h"
27 #include <string>
28 
29 namespace GNet
30 {
31  class Address4 ;
32 }
33 
34 /// \class GNet::Address4
35 /// A 'sockaddr' wrapper class for IPv4 addresses.
36 ///
38 {
39 public:
40  typedef sockaddr general_type ;
41  typedef sockaddr_in specific_type ;
42  typedef sockaddr storage_type ;
43  union union_type /// Used by GNet::Address4 to cast between sockaddr and sockaddr_in.
44  { specific_type specific ; general_type general ; storage_type storage ; } ;
45 
46  explicit Address4( unsigned int ) ;
47  explicit Address4( const std::string & ) ;
48  Address4( const std::string & , const std::string & ) ;
49  Address4( const std::string & , unsigned int ) ;
50  Address4( unsigned int port , int /*for overload resolution*/ ) ; // canonical loopback address
51  Address4( const sockaddr * addr , socklen_t len ) ;
52  Address4( const Address4 & other ) ;
53 
54  static int domain() ;
55  static unsigned short family() ;
56  const sockaddr * address() const ;
57  sockaddr * address() ;
58  static socklen_t length() ;
59 
60  unsigned int port() const ;
61  void setPort( unsigned int port ) ;
62 
63  static bool validString( const std::string & , std::string * = nullptr ) ;
64  static bool validStrings( const std::string & , const std::string & , std::string * = nullptr ) ;
65  static bool validPort( unsigned int port ) ;
66  static bool validData( const sockaddr * addr , socklen_t len ) ;
67 
68  bool same( const Address4 & other ) const ;
69  bool sameHostPart( const Address4 & other ) const ;
70  bool isLoopback() const ;
71  bool isLocal( std::string & ) const ;
72 
73  std::string displayString() const ;
74  std::string hostPartString() const ;
75  G::StringArray wildcards() const ;
76  static bool format( std::string ) ;
77 
78 private:
79  void init() ;
80  static const char * setAddress( union_type & , const std::string & ) ;
81  static const char * setHostAddress( union_type & , const std::string & ) ;
82  static const char * setPort( union_type & , unsigned int ) ;
83  static const char * setPort( union_type & , const std::string & ) ;
84  static bool sameAddr( const ::in_addr & a , const ::in_addr & b ) ;
85  static void add( G::StringArray & , const std::string & , unsigned int , const char * ) ;
86  static void add( G::StringArray & , const std::string & , const char * ) ;
87 
88 private:
89  union_type m_inet ;
90 } ;
91 
92 #endif
Used by GNet::Address4 to cast between sockaddr and sockaddr_in.
Definition: gaddress4.h:43
Network classes.
Definition: gaddress.h:29
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
A 'sockaddr' wrapper class for IPv4 addresses.
Definition: gaddress4.h:37