E-MailRelay

ginterfaces_common.cpp
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 ginterfaces_common.cpp
19///
20
21#include "gdef.h"
22#include "ginterfaces.h"
23#include "gstr.h"
24#include "gtest.h"
25#include <algorithm>
26
27#ifndef G_LIB_SMALL
28 GNet::Interfaces::Interfaces( EventState es ) :
29 m_es(es)
30{
31}
32#endif
33
34 GNet::Interfaces::Interfaces( EventState es , InterfacesHandler & handler ) :
35 m_es(es) ,
36 m_handler(&handler)
37{
38}
39
40 GNet::Interfaces::~Interfaces()
41= default;
42
43 void GNet::Interfaces::load()
44{
45 std::vector<Item> new_list ;
46 loadImp( m_es , new_list ) ;
47 m_loaded = true ;
48 using std::swap ;
49 swap( m_list , new_list ) ;
50}
51
52#ifndef G_LIB_SMALL
53 bool GNet::Interfaces::supported()
54{
55 return true ;
56}
57#endif
58
59 bool GNet::Interfaces::loaded() const
60{
61 return m_loaded ;
62}
63
64#ifndef G_LIB_SMALL
65std::vector<GNet::Address> GNet::Interfaces::addresses( const std::string & name , unsigned int port , int af ) const
66{
67 std::vector<GNet::Address> result ;
68 addresses( result , name , port , af ) ;
69 return result ;
70}
71#endif
72
73std::size_t GNet::Interfaces::addresses( std::vector<Address> & out , const std::string & name , unsigned int port , int af ) const
74{
75 if( !loaded() )
76 const_cast<Interfaces*>(this)->load() ;
77
78 std::size_t count = 0U ;
79 for( const auto & item : m_list )
80 {
81 if( !name.empty() && ( item.name == name || item.altname == name ) && item.up && item.valid_address )
82 {
83 if( af == AF_UNSPEC ||
84 ( af == AF_INET6 && item.address.is6() ) ||
85 ( af == AF_INET && item.address.is4() ) )
86 {
87 count++ ;
88 out.push_back( item.address ) ;
89 out.back().setPort( port ) ;
90 }
91 }
92 }
93 return count ;
94}
95
96#ifndef G_LIB_SMALL
97 G::StringArray GNet::Interfaces::names( bool all ) const
98{
99 G::StringArray list ;
100 for( const auto & iface : *this )
101 {
102 if( all || iface.up )
103 list.push_back( iface.name ) ;
104 }
105 std::sort( list.begin() , list.end() ) ;
106 list.erase( std::unique(list.begin(),list.end()) , list.end() ) ;
107 return list ;
108}
109#endif
110
111GNet::Interfaces::const_iterator GNet::Interfaces::begin() const
112{
113 return m_list.begin() ;
114}
115
116GNet::Interfaces::const_iterator GNet::Interfaces::end() const
117{
118 return m_list.end() ;
119}
120
121 void GNet::Interfaces::readEvent()
122{
123 if( m_notifier )
124 {
125 std::string s = m_notifier->readEvent() ;
126 if( m_handler && !s.empty() )
127 m_handler->onInterfaceEvent( s ) ;
128 }
129}
130
131 void GNet::Interfaces::onFutureEvent()
132{
133 if( m_notifier )
134 {
135 std::string s = m_notifier->onFutureEvent() ;
136 if( m_handler && !s.empty() )
137 m_handler->onInterfaceEvent( s ) ;
138 }
139}
140
141// ==
142
143GNet::Interfaces::Item::Item() :
144 address(Address::defaultAddress())
145{
146}
147
GNet::EventState
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
GNet::InterfacesHandler
An interface for receiving notification of network changes.
Definition: ginterfaces.h:137
GNet::Interfaces
A class for getting a list of network interfaces and their addresses.
Definition: ginterfaces.h:48
GNet::Interfaces::Interfaces
Interfaces(EventState)
Constructor resulting in an empty list.
Definition: ginterfaces_common.cpp:28
GNet::Interfaces::addresses
std::vector< Address > addresses(const std::string &name, unsigned int port, int af=AF_UNSPEC) const
Returns addresses bound to the given interface.
Definition: ginterfaces_common.cpp:65
GNet::Interfaces::load
void load()
Loads or reloads the list.
Definition: ginterfaces_common.cpp:43
GNet::Interfaces::loaded
bool loaded() const
Returns true if load()ed.
Definition: ginterfaces_common.cpp:59
GNet::Interfaces::~Interfaces
~Interfaces() override
Destructor.
GNet::Interfaces::supported
static bool supported()
Returns false if a stubbed-out implementation.
Definition: ginterfaces_common.cpp:53
GNet::Interfaces::begin
const_iterator begin() const
Returns a begin iterator.
Definition: ginterfaces_common.cpp:111
GNet::Interfaces::end
const_iterator end() const
Returns a one-off-the-end iterator.
Definition: ginterfaces_common.cpp:116
GNet::Interfaces::names
G::StringArray names(bool all=false) const
Returns the interface names, optionally including interfaces that are not up.
Definition: ginterfaces_common.cpp:97
ginterfaces.h
gstr.h
gtest.h
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30