E-MailRelay
gaddress4.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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
21#ifndef G_NET_ADDRESS4_H
22#define G_NET_ADDRESS4_H
23
24#include "gdef.h"
25#include "gaddress.h"
26#include <string>
27
28namespace GNet
29{
30 class Address4 ;
31}
32
33//| \class GNet::Address4
34/// A 'sockaddr' wrapper class for IPv4 addresses.
35///
37{
38public:
39 using sockaddr_type = sockaddr_in ;
40
41 explicit Address4( unsigned int ) ;
42 explicit Address4( const std::string & ) ;
43 Address4( const std::string & , const std::string & ) ;
44 Address4( unsigned int port , int /*loopback_overload*/ ) ; // canonical loopback address
45 Address4( const sockaddr * addr , socklen_t len ) ;
46
47 static int domain() noexcept ;
48 static unsigned short af() noexcept ;
49 const sockaddr * address() const ;
50 sockaddr * address() ;
51 static socklen_t length() noexcept ;
52 unsigned long scopeId( unsigned long default_ = 0UL ) const ;
53 unsigned int port() const ;
54 void setPort( unsigned int port ) ;
55 bool setZone( const std::string & ipv6_zone_name_or_scope_id ) ;
56 void setScopeId( unsigned long ipv6_scope_id ) ;
57 static bool validString( const std::string & , std::string * = nullptr ) ;
58 static bool validStrings( const std::string & , const std::string & , std::string * = nullptr ) ;
59 static bool validPort( unsigned int port ) ;
60 static bool validData( const sockaddr * addr , socklen_t len ) ;
61
62 bool same( const Address4 & other , bool ipv6_compare_with_scope = false ) const ;
63 bool sameHostPart( const Address4 & other ) const ;
64 bool isLoopback() const ;
65 bool isLocal( std::string & ) const ;
66 bool isLinkLocal() const ;
67 bool isUniqueLocal() const ;
68 bool isMulticast() const ;
69 bool isAny() const ;
70 unsigned int bits() const ;
71 std::string displayString( bool ipv6_with_scope = false ) const ;
72 std::string hostPartString() const ;
73 std::string queryString() const ;
74 G::StringArray wildcards() const ;
75 static bool format( G::string_view ) ;
76
77private:
78 explicit Address4( std::nullptr_t ) ;
79 static const char * setAddress( sockaddr_type & , G::string_view ) ;
80 static const char * setHostAddress( sockaddr_type & , G::string_view ) ;
81 static const char * setPort( sockaddr_type & , unsigned int ) ;
82 static const char * setPort( sockaddr_type & , G::string_view ) ;
83 static bool sameAddr( const ::in_addr & a , const ::in_addr & b ) ;
84 static void add( G::StringArray & , G::string_view , unsigned int , const char * ) ;
85 static void add( G::StringArray & , unsigned int , const char * ) ;
86 static void add( G::StringArray & , G::string_view , const char * ) ;
87 static void add( G::StringArray & , const char * ) ;
88
89private:
90 sockaddr_type m_inet ;
91} ;
92
93#endif
A 'sockaddr' wrapper class for IPv4 addresses.
Definition: gaddress4.h:37
A class like c++17's std::string_view.
Definition: gstringview.h:51
Network classes.
Definition: gdef.h:1144
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30