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