summaryrefslogtreecommitdiff
path: root/indra/llmessage/llares.h
blob: 78febcd560fdb2af07c4d053b1245fb1b0d810f8 (plain)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/** 
 * @file llares.h
 * @author Bryan O'Sullivan
 * @date 2007-08-15
 * @brief Wrapper for asynchronous DNS lookups.
 *
 * $LicenseInfo:firstyear=2007&license=viewergpl$
 * 
 * Copyright (c) 2007-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#ifndef LL_LLARES_H
#define LL_LLARES_H

#ifdef LL_WINDOWS
// ares.h is broken on windows in that it depends on types defined in ws2tcpip.h
// we need to include them first to work around it, but the headers issue warnings
# pragma warning(push)
# pragma warning(disable:4996)
# include <winsock2.h>
# include <ws2tcpip.h>
# pragma warning(pop)
#endif

#ifdef LL_STANDALONE
# include <ares.h>
#else
# include <ares/ares.h>
#endif

#include "llpointer.h"
#include "llrefcount.h"
#include "lluri.h"

#include <boost/shared_ptr.hpp>

class LLQueryResponder;
class LLAresListener;

/**
 * @brief Supported DNS RR types.
 */
enum LLResType
{
	RES_INVALID = 0,			/**< Cookie. */
	RES_A = 1,					/**< "A" record. IPv4 address. */
	RES_NS = 2,					/**< "NS" record. Authoritative server. */
	RES_CNAME = 5,				/**< "CNAME" record. Canonical name. */
	RES_PTR = 12,				/**< "PTR" record. Domain name pointer. */
	RES_AAAA = 28,				/**< "AAAA" record. IPv6 Address. */
	RES_SRV = 33,				/**< "SRV" record. Server Selection. */
	RES_MAX = 65536				/**< Sentinel; RR types are 16 bits wide. */
};

/**
 * @class LLDnsRecord
 * @brief Base class for all DNS RR types.
 */
class LLDnsRecord : public LLRefCount
{
protected:
	friend class LLQueryResponder;

	LLResType mType;
	std::string mName;
	unsigned mTTL;
	
	virtual int parse(const char *buf, size_t len, const char *pos,
					  size_t rrlen) = 0;

	LLDnsRecord(LLResType type, const std::string &name, unsigned ttl);
	
public:
	/**
	 * @brief Record name.
	 */
	const std::string &name() const { return mName; }

	/**
	 * @brief Time-to-live value, in seconds.
	 */
	unsigned ttl() const { return mTTL; }

	/**
	 * @brief RR type.
	 */
	LLResType type() const { return mType; }
};

/**
 * @class LLAddrRecord
 * @brief Base class for address-related RRs.
 */
class LLAddrRecord : public LLDnsRecord
{
protected:
	friend class LLQueryResponder;

	LLAddrRecord(LLResType type, const std::string &name, unsigned ttl);

	union 
	{
		sockaddr sa;
		sockaddr_in sin;
		sockaddr_in6 sin6;
	} mSA;

	socklen_t mSize;

public:
	/**
	 * @brief Generic socket address.
	 */
	const sockaddr &addr() const { return mSA.sa; }

	/**
	 * @brief Size of the socket structure.
	 */
	socklen_t size() const { return mSize; }
};

/**
 * @class LLARecord
 * @brief A RR, for IPv4 addresses.
 */
class LLARecord : public LLAddrRecord
{
protected:
	friend class LLQueryResponder;

	LLARecord(const std::string &name, unsigned ttl);

	int parse(const char *buf, size_t len, const char *pos, size_t rrlen);

public:
	/**
	 * @brief Socket address.
	 */
	const sockaddr_in &addr_in() const { return mSA.sin; }
};

/**
 * @class LLAaaaRecord
 * @brief AAAA RR, for IPv6 addresses.
 */
class LLAaaaRecord : public LLAddrRecord
{
protected:
	friend class LLQueryResponder;

	LLAaaaRecord(const std::string &name, unsigned ttl);

	int parse(const char *buf, size_t len, const char *pos, size_t rrlen);

public:
	/**
	 * @brief Socket address.
	 */
	const sockaddr_in6 &addr_in6() const { return mSA.sin6; }
};
	
/**
 * @class LLHostRecord
 * @brief Base class for host-related RRs.
 */
class LLHostRecord : public LLDnsRecord
{
protected:
	LLHostRecord(LLResType type, const std::string &name, unsigned ttl);

	int parse(const char *buf, size_t len, const char *pos, size_t rrlen);

	std::string mHost;

public:
	/**
	 * @brief Host name.
	 */
	const std::string &host() const { return mHost; }
};
	
/**
 * @class LLCnameRecord
 * @brief CNAME RR.
 */
class LLCnameRecord : public LLHostRecord
{
protected:
	friend class LLQueryResponder;

	LLCnameRecord(const std::string &name, unsigned ttl);
};
	
/**
 * @class LLPtrRecord
 * @brief PTR RR.
 */
class LLPtrRecord : public LLHostRecord
{
protected:
	friend class LLQueryResponder;

	LLPtrRecord(const std::string &name, unsigned ttl);
};

/**
 * @class LLSrvRecord
 * @brief SRV RR.
 */
class LLSrvRecord : public LLHostRecord
{
protected:
	U16 mPriority;
	U16 mWeight;
	U16 mPort;

	int parse(const char *buf, size_t len, const char *pos, size_t rrlen);

public:
	LLSrvRecord(const std::string &name, unsigned ttl);

	/**
	 * @brief Service priority.
	 */
	U16 priority() const { return mPriority; }

	/**
	 * @brief Service weight.
	 */
	U16 weight() const { return mWeight; }

	/**
	 * @brief Port number of service.
	 */
	U16 port() const { return mPort; }

	/**
	 * @brief Functor for sorting SRV records by priority.
	 */
	struct ComparePriorityLowest
	{
		bool operator()(const LLSrvRecord& lhs, const LLSrvRecord& rhs)
		{
			return lhs.mPriority < rhs.mPriority;
		}
	};
};
	
/**
 * @class LLNsRecord
 * @brief NS RR.
 */
class LLNsRecord : public LLHostRecord
{
public:
	LLNsRecord(const std::string &name, unsigned ttl);
};

class LLQueryResponder;

/**
 * @class LLAres
 * @brief Asynchronous address resolver.
 */
class LLAres
{
public:
    /**
	 * @class HostResponder
	 * @brief Base class for responding to hostname lookups.
	 * @see LLAres::getHostByName
	 */
	class HostResponder : public LLRefCount
	{
	public:
		virtual ~HostResponder();

		virtual void hostResult(const hostent *ent);
		virtual void hostError(int code);
	};

    /**
	 * @class NameInfoResponder
	 * @brief Base class for responding to address lookups.
	 * @see LLAres::getNameInfo
	 */
	class NameInfoResponder : public LLRefCount
	{
	public:
		virtual ~NameInfoResponder();

		virtual void nameInfoResult(const char *node, const char *service);
		virtual void nameInfoError(int code);
	};

    /**
	 * @class QueryResponder
	 * @brief Base class for responding to custom searches.
	 * @see LLAres::search
	 */
	class QueryResponder : public LLRefCount
	{
	public:
		virtual ~QueryResponder();

		virtual void queryResult(const char *buf, size_t len);
		virtual void queryError(int code);
	};

	class SrvResponder;
	class UriRewriteResponder;
		
	LLAres();

	~LLAres();

	/**
	 * Cancel all outstanding requests.  The error methods of the
	 * corresponding responders will be called, with ARES_ETIMEOUT.
	 */
	void cancel();
	
	/**
	 * Look up the address of a host.
	 *
	 * @param name name of host to look up
	 * @param resp responder to call with result
	 * @param family AF_INET for IPv4 addresses, AF_INET6 for IPv6
	 */
	void getHostByName(const std::string &name, HostResponder *resp,
					   int family = AF_INET) {
		getHostByName(name.c_str(), resp, family);
	}

	/**
	 * Look up the address of a host.
	 *
	 * @param name name of host to look up
	 * @param resp responder to call with result
	 * @param family AF_INET for IPv4 addresses, AF_INET6 for IPv6
	 */
	void getHostByName(const char *name, HostResponder *resp,
					   int family = PF_INET);

	/**
	 * Look up the name associated with a socket address.
	 *
	 * @param sa socket address to look up
	 * @param salen size of socket address
	 * @param flags flags to use
	 * @param resp responder to call with result
	 */
	void getNameInfo(const struct sockaddr &sa, socklen_t salen, int flags,
					 NameInfoResponder *resp);

	/**
	 * Look up SRV (service location) records for a service name.
	 *
	 * @param name service name (e.g. "_https._tcp.login.agni.lindenlab.com")
	 * @param resp responder to call with result
	 */
	void getSrvRecords(const std::string &name, SrvResponder *resp);

	/**
	 * Rewrite a URI, using SRV (service location) records for its
	 * protocol if available.  If no SRV records are published, the
	 * existing URI is handed to the responder.
	 *
	 * @param uri URI to rewrite
	 * @param resp responder to call with result
	 */
	void rewriteURI(const std::string &uri,
					UriRewriteResponder *resp);

	/**
	 * Start a custom search.
	 *
	 * @param query query to make
	 * @param type type of query to perform
	 * @param resp responder to call with result
	 */
	void search(const std::string &query, LLResType type,
				QueryResponder *resp);

	/**
	 * Process any outstanding queries.  This method takes an optional
	 * timeout parameter (specified in microseconds).  If provided, it
	 * will block the calling thread for that length of time to await
	 * possible responses. A timeout of zero will return immediately
	 * if there are no responses or timeouts to process.
	 *
	 * @param timeoutUsecs number of microseconds to block before timing out
	 * @return whether any responses were processed
	 */
	bool process(U64 timeoutUsecs = 0);

	/**
	 * Process all outstanding queries, blocking the calling thread
	 * until all have either been responded to or timed out.
	 *
	 * @return whether any responses were processed
	 */
	bool processAll();
	
	/**
	 * Expand a DNS-encoded compressed string into a normal string.
	 *
	 * @param encoded the encoded name (null-terminated)
	 * @param abuf the response buffer in which the string is embedded
	 * @param alen the length of the response buffer
	 * @param s the string into which to place the result
	 * @return ARES_SUCCESS on success, otherwise an error indicator
	 */
	static int expandName(const char *encoded, const char *abuf, size_t alen,
						  std::string &s) {
		size_t ignore;
		return expandName(encoded, abuf, alen, s, ignore);
	}
	
	static int expandName(const char *encoded, const char *abuf, size_t alen,
						  std::string &s, size_t &enclen);

	/**
	 * Return a string describing an error code.
	 */
	static const char *strerror(int code);

	bool isInitialized(void) { return mInitSuccess; }

protected:
	ares_channel chan_;
	bool mInitSuccess;
    // boost::scoped_ptr would actually fit the requirement better, but it
    // can't handle incomplete types as boost::shared_ptr can.
    boost::shared_ptr<LLAresListener> mListener;
};
	
/**
 * An ordered collection of DNS resource records.
 */
typedef std::vector<LLPointer<LLDnsRecord> > dns_rrs_t;

/**
 * @class LLQueryResponder
 * @brief Base class for friendly handling of DNS query responses.
 *
 * This class parses a DNS response and represents it in a friendly
 * manner.
 *
 * @see LLDnsRecord
 * @see LLARecord
 * @see LLNsRecord
 * @see LLCnameRecord
 * @see LLPtrRecord
 * @see LLAaaaRecord
 * @see LLSrvRecord
 */
class LLQueryResponder : public LLAres::QueryResponder
{
protected:
	int mResult;
	std::string mQuery;
	LLResType mType;
	
	dns_rrs_t mAnswers;
	dns_rrs_t mAuthorities;
	dns_rrs_t mAdditional;

	/**
	 * Parse a single RR.
	 */
	int parseRR(const char *buf, size_t len, const char *&pos,
				LLPointer<LLDnsRecord> &r);
	/**
	 * Parse one section of a response.
	 */
	int parseSection(const char *buf, size_t len,
					 size_t count, const char *& pos, dns_rrs_t &rrs);

	void queryResult(const char *buf, size_t len);
	virtual void querySuccess();

public:
	LLQueryResponder();

	/**
	 * Indicate whether the response could be parsed successfully.
	 */
	bool valid() const { return mResult == ARES_SUCCESS; }

	/**
	 * The more detailed result of parsing the response.
	 */
	int result() const { return mResult; }
	
	/**
	 * Return the query embedded in the response.
	 */
	const std::string &query() const { return mQuery; }

	/**
	 * Return the contents of the "answers" section of the response.
	 */
	const dns_rrs_t &answers() const { return mAnswers; }

	/**
	 * Return the contents of the "authorities" section of the
	 * response.
	 */
	const dns_rrs_t &authorities() const { return mAuthorities; }

	/**
	 * Return the contents of the "additional records" section of the
	 * response.
	 */
	const dns_rrs_t &additional() const { return mAdditional; }
};

/**
 * @class LLAres::SrvResponder
 * @brief Class for handling SRV query responses.
 */
class LLAres::SrvResponder : public LLQueryResponder
{
public:
	friend void LLAres::getSrvRecords(const std::string &name,
									  SrvResponder *resp);
	void querySuccess();
	void queryError(int code);

	virtual void srvResult(const dns_rrs_t &ents);
	virtual void srvError(int code);
};

/**
 * @class LLAres::UriRewriteResponder
 * @brief Class for handling URI rewrites based on SRV records.
 */
class LLAres::UriRewriteResponder : public LLQueryResponder
{
protected:
	LLURI mUri;

public:
	friend void LLAres::rewriteURI(const std::string &uri,
								   UriRewriteResponder *resp);
	void querySuccess();
	void queryError(int code);

	virtual void rewriteResult(const std::vector<std::string> &uris);
};
	
/**
 * Singleton responder.
 */
extern LLAres *gAres;

/**
 * Set up the singleton responder.  It's safe to call this more than
 * once from within a single thread, but this function is not
 * thread safe.
 */
extern LLAres *ll_init_ares();

#endif // LL_LLARES_H