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