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