[go: up one dir, main page]

File: resolver.h

package info (click to toggle)
dibbler 1.0.1-1
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 13,308 kB
  • ctags: 10,470
  • sloc: cpp: 60,323; ansic: 12,233; sh: 11,951; yacc: 3,418; lex: 969; makefile: 940; perl: 319; xml: 116; python: 74
file content (350 lines) | stat: -rw-r--r-- 13,108 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
    Posadis - A DNS Server
    Dns Resolver API
    Copyright (C) 2002  Meilof Veeningen <meilof@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef __POSLIB_RESOLVER_H
#define __POSLIB_RESOLVER_H

#include "sysstl.h"

#include "socket.h"
#include "dnsmessage.h"

/*! \file poslib/resolver.h
 * \brief Posadis resolver functionality
 *
 * This file contains the Posadis resolver implementation. It contains the base
 * pos_resolver class, which does not contain the implementation for the
 * resolver. There are two resolver implementations: pos_cliresolver for client
 * programs and pos_srvresolver for server programs.
 */

/*!
 * \brief default flags for the pos_resolver::query function
 *
 * This define contains the default flags for the 'flag' parameter of the
 * pos_resolver::query function. This means trying UDP at first, and if the
 * answer is truncated, try TCP next.
 */
#define Q_DFL 0

/*!
 * \brief 'no-tcp' flag for pos_resolver::query
 *
 * This flag instructs the pos_resolver::query function not to retry the query
 * using TCP if the UDP answer is truncated. This means the answer returned by
 * the function might be truncated, which can be checked by looking at the
 * DnsMessage::TC flag.
 */
#define Q_NOTCP 1

class WaitAnswerData {
 public:
  WaitAnswerData(u_int16 _r_id, _addr& _from);
  u_int16 r_id;
  _addr from;
};

/*!
 * \brief Posadis abstract resolver class
 *
 * This is the abstract base class for the Posadis resolver functionality. The
 * pos_resolver class offers functions for both UDP and TCP resolving. For UDP
 * resolving, use the high-level query() functions. sendmessage() and
 * waitanswer() are low-level functions that applications would not normally
 * want to call. The same goes for TCP: tcpconnect() tcpdisconnect() and
 * tcpquery() are the functions it's all about.
 *
 * You can modify some parameters by setting member functions of the class, and
 * by specifying custom flags for the query functions.
 *
 * Like we mentioned, this is an abstract class that should _not_ be used
 * directly. For client applications, use the pos_cliresolver class, and for
 * server applications, use pos_srvresolver.
 *
 * Note that pos_resolver actually _does_ contain a complete TCP
 * implementation, so, theoretically it could be instantiated directly for use
 * as a TCP resolver. This is however not encouraged.
 */
class pos_resolver {
  public:
  /*!
   * \brief default constructor
   *
   * This constructor intializes the resolver, and sets the udp_tries,
   * n_udp_tries and tcp_timeout values to their defaults.
   */
  pos_resolver();

  /*!
   * \brief destructor
   *
   * This function clears all memory associated with the resolver object.
   */
  virtual ~pos_resolver();

  /*!
   * \brief number of UDP attempts
   *
   * Sets the number of times Posadis attempts to retry querying using UDP if
   * a previous attempt did not return an answer. If you change this value,
   * you should also change #udp_tries to have at least as many items as this
   * value.
   */
  int n_udp_tries;

  /*!
   * \brief UDP timeout values
   *
   * This is an array of UDP retry values, in milliseconds. This array can be
   * changed by hand. If you want to change the dimensions, a mere realloc()
   * is enough. Just make sure it has at least as many items as the
   * #n_udp_tries value, otherwise your application might crash :(
   */
  int *udp_tries;

  /*!
   * \brief TCP timeout value
   *
   * This is the time, in milliseconds, we're willing to wait for TCP
   * transmission. Because there are four operations involved in a TCP query
   * (two send operations and two read operations), each operation gets 25%
   * of this timeout to complete. Since TCP is a connection-based protocol,
   * we're not responsible for retransmission, thus we only attempt queries
   * once.
   */
  int tcp_timeout;

  /*!
   * \brief high-level query function
   *
   * This function will query the given DNS server for the information
   * identified by the DNS query message q. If it succeeds, it will return and
   * put the answer from the server in a, which need not be initialized
   * previously (in fact, this will result in a memory leak). If not, it will
   * raise a PException.
   *
   * If the query() function does not receive an answer in time, it will retry
   * for #n_udp_tries times, using the timeout values from the #udp_tries
   * array. If the answer it receives is truncated, it will retry using TCP,
   * unless instructed not to by the flags parameter.
   *
   * The behavior of the query function can be changed by the flags parameter.
   * Currently, this can only be Q_DFL (default flags) or Q_NOTCP (do not retry
   * using UDP).
   * \param q The DNS query message
   * \param a Variable to put the answer in
   * \param server The server to query
   * \param flags Flags controlling query behavior.
   */
  virtual void query(DnsMessage *q, DnsMessage*& a, _addr *server, int flags = Q_DFL) = 0;

  /*!
   * \brief high-level query function using multiple servers
   *
   * This function generally behaves the same as the query() function, except
   * it takes a list of servers instead of one. The query algorithm differs in
   * that for each timeout value from #udp_tries, all servers will be queried.
   * Also, if the answer is truncated, _only_ the server that returned the
   * truncated answer will be tried using TCP. This function will start
   * querying at a random place in the servers list; after that, it will run
   * through all servers listed in the order in which you specify them.
   *
   * \param q 
   * \param a likely an answer
   * \param servers List of servers to query
   * \param flags
   *
   * \return The address of the server that returned this answer
   * \sa query()
   */
  virtual _addr query(DnsMessage *q, DnsMessage*& a, stl_slist(_addr) &servers, int flags = Q_DFL) = 0;

  /*!
   * \brief low-level resolver function for sending a message
   *
   * This function sends a DNS message to a specified server using UDP.
   * \param msg The DNS message to send
   * \param res The host to send the message to
   * \param sockid Implementation-dependent argument.
   */
  virtual void sendmessage(DnsMessage *msg, _addr *res, int sockid = -1) = 0;

  /*!
   * \brief low-level resolver function for waiting for an answer
   *
   * This function waits for at most the amount of milliseconds specified
   * by timeout until an answer to our query arrives. Since multiple
   * messages for the same query might have been sent out, it asks for a list
   * of sent queries.
   *
   * If no answer is received in time, this function will raise an exception.
   * \param ans If an answer is received, it will be put in this variable. This
   *            should already be a valid DNS message, presumably initialized
   *            with q->initialize_answer()
   * \param wait List of sent queries we might get an answer to
   * \param timeout Number of milliseconds to wait at most
   * \param it If an answer is received, this iterator will point to the
   *           message this was an answer to.
   * \param sockid Implementation-dependent argument.
   */
  virtual bool waitanswer(DnsMessage*& ans, stl_slist(WaitAnswerData)& wait, int timeout, stl_slist(WaitAnswerData)::iterator& it, int sockid = -1) = 0;

  /*!
   * \brief establishes a TCP connection to a DNS server
   *
   * This function will try to connect to the given address using TCP.
   * \param res The server to connect to
   * \return A socket identifier to use for other tcp resolver functions
   * \sa tcpdisconnect()
   */
  virtual int tcpconnect(_addr *res);

  /*!
   * \brief disconnects from a DNS server
   *
   * This function will disconnect from a server we connected to earlier using
   * tcpconnect().
   * \sa tcpconnect()
   */
  virtual void tcpdisconnect(int sockid);

  /*!
   * \brief TCP query function
   *
   * This is the high-level TCP query function. It will query the server we
   * connected to, and put the answer to the query q in a. It will wait at
   * most #tcp_timeout milliseconds before it receives an answer.
   *
   * If the function receives a message which does not answer our query, it
   * will stop waiting and raise an exception.
   * \param q The DNS query message
   * \param a Variable to put the answer in
   * \param sockid The TCP connection (as returned by tcpconnect())
   */
  virtual void tcpquery(DnsMessage *q, DnsMessage*& a, int sockid);

  /*!
   * \brief TCP low-level function for sending a message
   *
   * This function sends a DNS message over a TCP connection.
   * \param msg The message to send
   * \param sockid The TCP connection (as returned by tcpconnect())
   */
  virtual void tcpsendmessage(DnsMessage *msg, int sockid);

  /*!
   * \brief TCP low-level function for waiting for an answer
   *
   * This function waits for at most #tcp_timeout milliseconds until it
   * receives an answer from the server on the other end of the line. Unlike
   * its UDP conterpart, this function does not check whether the server
   * actually answered our query.
   * \param ans Variable to put the received message in. This should already
   *            point to an existing DNS message, which should be initialized
   *            by calling q->initialize_answer(); even if an error occurs,
   *            this may be non-NULL.
   * \param sockid The TCP connection (as returned by tcpconnect())
   */
  virtual void tcpwaitanswer(DnsMessage*& ans, int sockid);
};

/*!
 * \brief resolver for client applications
 *
 * This is an implementation of the pos_resolver class meant for client
 * applications. It does not maintain a centralized query database like
 * pos_srvresolver. Instead, it will open up a new socket for each query it
 * attempts. The advantage is that it does not require the multi-thread
 * architecture pos_srvresolver depends on.
 *
 * pos_cliresolver implements the pos_resolver query(), sendmessage() and
 * waitanswer() functions.
 */

class pos_cliresolver : public pos_resolver {
  public:
  /*!
   * \brief resolver constructor
   *
   * Resolver for the client resolver.
   */
  pos_cliresolver();

  /*!
   * \brief destructor
   *
   * Destructor for the client resolver
   */
  virtual ~pos_cliresolver();
  
  void query(DnsMessage *q, DnsMessage*& a, _addr *server, int flags = Q_DFL);

  _addr query(DnsMessage *q, DnsMessage*& a, stl_slist(_addr) &servers, int flags = Q_DFL);

  /*!
   * \brief low-level resolver function for sending a message
   *
   * This function sends a DNS message to a specified server using UDP.
   * \param msg The DNS message to send
   * \param res The host to send the message to
   * \param sockid The socket to use
   */
  void sendmessage(DnsMessage *msg, _addr *res, int sockid = -1);
  /*!
   * \brief low-level resolver function for waiting for an answer
   *
   * This function waits for at most the amount of milliseconds specified
   * by timeout until an answer to our query arrives. Since multiple
   * messages for the same query might have been sent out, it asks for a list
   * of sent queries.
   *
   * If no answer is received in time, this function will raise an exception.
   * \param ans If an answer is received, it ill be put in this variable. This
   *            should already be a valid DNS message, presumably initialized
   *            with q->initialize_answer()
   * \param wait List of sent queries we might get an answer to
   * \param timeout Number of milliseconds to wait at most
   * \param it If an answer is received, this iterator will point to the
   *           message this was an answer to.
   * \param sockid The socket id the answers will come from.
   */
  bool waitanswer(DnsMessage*& ans, stl_slist(WaitAnswerData)& wait, int timeout, stl_slist(WaitAnswerData)::iterator& it, int sockid = -1);

  /*!
   * \brief stops the resolving process asap
   *
   * This function will try to stop the resolving process as soon as possible.
   * Thus, it will need to be called asynchronously (since the query functions
   * block), either from another thread or from a signal handler. It will tease
   * the query functions a bit by closing their sockets, and urge them to
   * quit.
   */
  void stop();
 private:
  void clrstop();
  int sockid;
  bool quit_flag;
  bool is_tcp;
#ifndef _WIN32
  int clipipes[2];
#endif
};

#endif /* __POSLIB_RESOLVER_H */