E-MailRelay

gaddresslocal.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 gaddresslocal.h
19///
20
21#ifndef G_NET_ADDRESSLOCAL_H
22#define G_NET_ADDRESSLOCAL_H
23
24#include "gdef.h"
25#include "gaddress.h"
26#include "gstringview.h"
27#include "gstringarray.h"
28#include <string>
29#if GCONFIG_HAVE_UDS
30#include <sys/types.h>
31#include <sys/un.h>
32#else
33#ifdef G_DOXYGEN
34 struct sockaddr_un ;
35#else
36 struct sockaddr_un {} ;
37#endif
38#endif
39
40 namespace GNet
41{
42 class AddressLocal ;
43}
44
45//| \class GNet::AddressLocal
46/// A 'sockaddr' wrapper class for local-domain addresses.
47///
48/// Use "netcat -U" or "socat" to connect to local-domain
49/// sockets, eg:
50/// \code
51/// $ nc -U -C /run/cmd.s # nc.openbsd, not nc.traditional
52/// $ socat -d tcp-listen:8080,fork unix:/run/cmd.s
53/// \endcode
54///
55 class GNet::AddressLocal
56{
57 public:
58 using sockaddr_type = sockaddr_un ;
59
60 explicit AddressLocal( unsigned int ) ;
61 explicit AddressLocal( std::string_view ) ;
62 AddressLocal( unsigned int port , int /*for overload resolution*/ ) ;
63 AddressLocal( const sockaddr * addr , socklen_t len ) ;
64
65 static int domain() noexcept ;
66 static unsigned short af() noexcept ;
67 const sockaddr * address() const ;
68 sockaddr * address() ;
69 socklen_t length() const noexcept ;
70 unsigned long scopeId( unsigned long default_ = 0UL ) const ;
71 unsigned int port() const ;
72 void setPort( unsigned int port ) ;
73 bool setZone( std::string_view ipv6_zone_name_or_scope_id ) ;
74 void setScopeId( unsigned long ipv6_scope_id ) ;
75 static bool validString( std::string_view , std::string * = nullptr ) ;
76 static bool validStrings( std::string_view , std::string_view , std::string * = nullptr ) ;
77 static bool validPort( unsigned int port ) ;
78 static bool validData( const sockaddr * addr , socklen_t len ) ;
79
80 bool same( const AddressLocal & other , bool ipv6_compare_with_scope = false ) const ;
81 bool sameHostPart( const AddressLocal & other ) const ;
82 bool isLoopback() const ;
83 bool isLocal( std::string & ) const ;
84 bool isLinkLocal() const ;
85 bool isUniqueLocal() const ;
86 bool isMulticast() const ;
87 bool isAny() const ;
88 unsigned int bits() const ;
89 std::string displayString( bool ipv6_with_scope = false ) const ;
90 std::string hostPartString() const ;
91 std::string queryString() const ;
92 G::StringArray wildcards() const ;
93 static bool format( const std::string & ) ;
94
95 private:
96 explicit AddressLocal( std::nullptr_t ) ;
97 std::string path() const ;
98
99 private:
100 sockaddr_type m_local ;
101 std::size_t m_size ;
102} ;
103
104#endif
GNet::AddressLocal
A 'sockaddr' wrapper class for local-domain addresses.
Definition: gaddresslocal.h:56
gaddress.h
gstringarray.h
gstringview.h
GNet
Network classes.
Definition: gdef.h:1243
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30