E-MailRelay

gmxlookup.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 gmxlookup.h
19///
20
21#ifndef G_MX_LOOKUP_H
22#define G_MX_LOOKUP_H
23
24#include "gdef.h"
25#include "gmessagestore.h"
26#include "gaddress.h"
27#include "gsocket.h"
28#include "geventhandler.h"
29#include "gdatetime.h"
30#include "gtimer.h"
31#include "gslot.h"
32#include <string>
33#include <vector>
34#include <memory>
35
36 namespace GFilters
37{
38 class MxLookup ;
39}
40
41//| \class GFilters::MxLookup
42/// A DNS MX lookup client.
43///
44/// Each nameserver is queried in turn with a 'ns_timeout' interval.
45/// After the final nameserver has been queried there is a
46/// 'restart_timeout' before the sequence starts again. There is no
47/// overall timeout.
48///
49 class GFilters::MxLookup : private GNet::EventHandler
50{
51 public:
52 struct Config /// A configuration structure for GFilters::MxLookup
53 {
54 Config() ;
55 G::TimeInterval ns_timeout {1U,0} ;
56 G::TimeInterval restart_timeout {15U,0} ;
57 } ;
58
59 static bool enabled() ;
60 ///< Returns true if implemented.
61
62 explicit MxLookup( GNet::EventState , Config = {} ) ;
63 ///< Constructor.
64
65 explicit MxLookup( GNet::EventState , Config , const std::vector<GNet::Address> & ns ) ;
66 ///< Constructor taking a list of nameservers.
67 /// \see GNet::nameservers()
68
69 void start( const GStore::MessageId & , const std::string & question_domain , unsigned int port ) ;
70 ///< Starts the lookup.
71
72 G::Slot::Signal<GStore::MessageId,std::string,std::string> & doneSignal() noexcept ;
73 ///< Returns a reference to the completion signal. The signal
74 ///< parameters are (1) the original message id, (2) the answer
75 ///< port-25 transport address (if successful), and (3) the
76 ///< error reason (if not).
77
78 void cancel() ;
79 ///< Cancels the lookup so the doneSignal() is not emitted.
80
81private: // overrides
82 void readEvent() override ; // GNet::EventHandler
83
84private:
85 void startTimer() ;
86 void onTimeout() ;
87 void sendMxQuestion( std::size_t , const std::string & ) ;
88 void sendHostQuestion( std::size_t , const std::string & ) ;
89 void fail( const std::string & ) ;
90 void succeed( const std::string & ) ;
91 void dropReadHandlers() ;
92 GNet::DatagramSocket & socket( std::size_t ) ;
93 void process( const char * , std::size_t ) ;
94 void disable( std::size_t , const std::string & ) ;
95
96private:
97 GNet::EventState m_es ;
98 Config m_config ;
99 GStore::MessageId m_message_id ;
100 std::string m_question ;
101 unsigned int m_port {0U} ;
102 std::string m_error ;
103 std::size_t m_ns_index ;
104 std::size_t m_ns_failures ;
105 std::vector<GNet::Address> m_nameservers ;
106 GNet::Timer<MxLookup> m_timer ;
107 std::unique_ptr<GNet::DatagramSocket> m_socket4 ;
108 std::unique_ptr<GNet::DatagramSocket> m_socket6 ;
109 G::Slot::Signal<GStore::MessageId,std::string,std::string> m_done_signal ;
110} ;
111
112#endif
GFilters::MxLookup
A DNS MX lookup client.
Definition: gmxlookup.h:50
GFilters::MxLookup::MxLookup
MxLookup(GNet::EventState, Config={})
Constructor.
Definition: gmxlookup.cpp:48
GFilters::MxLookup::doneSignal
G::Slot::Signal< GStore::MessageId, std::string, std::string > & doneSignal() noexcept
Returns a reference to the completion signal.
Definition: gmxlookup.cpp:304
GFilters::MxLookup::enabled
static bool enabled()
Returns true if implemented.
Definition: gmxlookup.cpp:42
GFilters::MxLookup::start
void start(const GStore::MessageId &, const std::string &question_domain, unsigned int port)
Starts the lookup.
Definition: gmxlookup.cpp:89
GFilters::MxLookup::cancel
void cancel()
Cancels the lookup so the doneSignal() is not emitted.
Definition: gmxlookup.cpp:247
GNet::EventHandler
A base class for classes that have a file descriptor and handle asynchronous events from the event lo...
Definition: geventhandler.h:48
GNet::EventState
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
GNet::Timer
A timer class template in which the timeout is delivered to the specified method.
Definition: gtimer.h:141
GStore::MessageId
A somewhat opaque identifer for a GStore::MessageStore message id.
Definition: gmessagestore.h:43
G::TimeInterval
An interval between two G::SystemTime values or two G::TimerTime values.
Definition: gdatetime.h:305
gaddress.h
gdatetime.h
geventhandler.h
gmessagestore.h
gslot.h
gsocket.h
gtimer.h
GFilters
Message filter classes.
Definition: gcopyfilter.h:31
GNet
Network classes.
Definition: gdef.h:1243
GStore
Message store classes.
Definition: genvelope.cpp:30
std
STL namespace.
GFilters::MxLookup::Config
A configuration structure for GFilters::MxLookup.
Definition: gmxlookup.h:53
G::Slot::Signal< GStore::MessageId, std::string, std::string >