reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
DnsResult.hxx
Go to the documentation of this file.
1#if !defined(RESIP_DNSRESULT_HXX)
2#define RESIP_DNSRESULT_HXX
3
4#ifdef HAVE_CONFIG_H
5#include "config.h"
6#endif
7
8
9#include <iosfwd>
10#include <set>
11#include <vector>
12#include <deque>
13#include <map>
14#include <stack>
15
16#include "resip/stack/Tuple.hxx"
18#include "resip/stack/Uri.hxx"
20#include "rutil/Condition.hxx"
22#include "rutil/dns/RRVip.hxx"
23#include "rutil/dns/DnsStub.hxx"
24
25#ifdef WIN32
26#include <Ws2tcpip.h>
27#endif
28
29struct hostent;
30
31namespace resip
32{
33class DnsInterface;
34class DnsAAAARecord;
35class DnsHandler;
37
39{
40 public:
41 virtual void onEnumResult(const DNSResult<DnsNaptrRecord>& result, int order) = 0;
42};
43
45{
46 public:
47 EnumResult(EnumResultSink& resultSink, int order);
49
50 // DnsResultSink
56
59
60 private:
61 EnumResultSink& mResultSink;
62 int mOrder;
63};
64
66{
67 public:
69 DnsResult(DnsInterface& interfaceObj, DnsStub& dns, RRVip& vip, DnsHandler* handler);
70 virtual ~DnsResult();
71
72 typedef enum
73 {
74 Available, // A result is available now
75 Pending, // More results may be pending
76 Finished, // No more results available and none pending
77 Destroyed // the associated transaction has been deleted
78 // (ie, this DnsResult will delete itself as soon as it
79 // gets a new result)
81
82 typedef std::vector<Data> DataVector;
83
91 void lookup(const Uri& uri);
92
101 bool blacklistLast(uint64_t expiry);
102
113 bool greylistLast(uint64_t expiry);
114
125 Type available();
126
137 Tuple next();
138
153 void whitelistLast();
154
155 // return the target of associated query
156 // Safe to call after DnsHandler::handle callback has been called.
157 Data target() const { return mTarget; }
158 unsigned int getSRVResultsSize() const {return (unsigned int)mSRVResults.size();}
159
160 // Will delete this DnsResult if no pending queries are out there or wait
161 // until the pending queries get responses and then delete
162 void destroy();
163
164 // Used to store a NAPTR result
165 class NAPTR
166 {
167 public:
168 NAPTR();
169 // As defined by RFC3263
170 bool operator<(const NAPTR& rhs) const;
171
172 Data key; // NAPTR record key
173
174 int order;
175 int pref;
180 };
181
182 class SRV
183 {
184 public:
185 SRV();
186 // As defined by RFC3263, RFC2782
187 bool operator<(const SRV& rhs) const;
188
189 Data key; // SRV record key
190
195 int port;
197 };
198
199 private:
200
201 /*
202 The following command is used to ensure that all DnsInterface mRRVip and
203 mTupleMarkManager, and enum setting accesses are done from the DnsThread.
204 This gets the initial call * lookupInternalWithEnum to occur on the
205 DnsThread (using the DnsStub command fifo).
206 */
207 class LookupCommand : public DnsStub::Command
208 {
209 public:
210 LookupCommand(DnsResult& dnsResult, const Uri& uri) : mDnsResult(dnsResult), mUri(uri) {}
211 virtual ~LookupCommand() {}
212 virtual void execute()
213 {
214 mDnsResult.lookupInternalWithEnum(mUri);
215 }
216 private:
217 DnsResult& mDnsResult;
218 Uri mUri;
219 };
220 friend class LookupCommand;
221 void lookupInternalWithEnum(const Uri& uri);
222 void lookupInternal(const Uri& uri);
223
224 // Given a transport and port from uri, return the default port to use
225 int getDefaultPort(TransportType transport, int port);
226
227 void lookupHost(const Data& target);
228
229 // compute the cumulative weights for the SRV entries with the lowest
230 // priority, then randomly pick according to RFC2782 from the entries with
231 // the lowest priority based on weights. When all entries at the lowest
232 // priority are chosen, pick the next lowest priority and repeat. After an
233 // SRV entry is selected, remove it from mSRVResults
234 SRV retrieveSRV();
235
236 // Will retrieve the next SRV record and compute the prime the mResults
237 // with the appropriate Tuples.
238 void primeResults();
239
240 private:
241 DnsInterface& mInterface;
242 DnsStub& mDnsStub;
243 RRVip& mVip;
244 DnsHandler* mHandler;
245 int mSRVCount;
246 Uri mInputUri;
247 int mDoingEnum;
248 std::map<int,Uri> mEnumDestinations;
249 resip::Mutex mEnumDestinationsMutex;
250
251 bool mSips;
252 Data mTarget;
253 Data mSrvKey;
254 TransportType mTransport; // current
255 int mPort; // current
256
268 bool mHaveChosenTransport;
269
277 Type mType;
278
279 //Ugly hack
280 Data mPassHostFromAAAAtoA;
281
282 void transition(Type t);
283
284 // This is where the current pending (ordered) results are stored. As they
285 // are retrieved by calling next(), they are popped from the front of the list
286 std::deque<Tuple> mResults;
287 std::vector<Tuple> mGreylistedTuples;
288
289 // Map of NAPTR records by replacement (ie. SRV lookup key)
290 std::map<Data, NAPTR> mTopOrderedNAPTRs;
291
292 // used in determining the next SRV record to use as per rfc2782
293 int mCumulativeWeight; // for current priority
294
295 // All SRV records sorted in order of preference
296 std::vector<SRV> mSRVResults;
297
298 friend class DnsInterface;
302
303 // DnsResultSink
304 void onDnsResult(const DNSResult<DnsHostRecord>&);
305 void onDnsResult(const DNSResult<DnsAAAARecord>&);
306 void onDnsResult(const DNSResult<DnsSrvRecord>&);
307 void onDnsResult(const DNSResult<DnsNaptrRecord>&);
308 void onDnsResult(const DNSResult<DnsCnameRecord>&);
309
310 void onEnumResult(const DNSResult<DnsNaptrRecord>& result, int order);
311 void onNaptrResult(const DNSResult<DnsNaptrRecord>& result);
312
313 typedef struct
314 {
315 Data domain;
316 int rrType;
317 Data value; // stores ip for A/AAAA, target host:port for SRV, and replacement for NAPTR.
318 } Item;
319
326 std::vector<Item> mLastReturnedPath;
327
335 std::vector<Item> mCurrentPath;
336 bool mHaveReturnedResults;
337 void clearCurrPath();
338 Tuple mLastResult;
339
340 /*
341 The following two commands are to ensure that all DnsInterface mVip
342 and mTupleMarkManager calls are done from the DnsThread (using the
343 DnsStub command fifo).
344 */
345 class WhitelistCommand : public DnsStub::Command
346 {
347 public:
348 WhitelistCommand(RRVip& vip, TupleMarkManager& markManager, std::vector<Item>& path, Tuple& result) : mVip(vip), mMarkManager(markManager), mPath(path), mResult(result) {}
349 virtual ~WhitelistCommand() {}
350 virtual void execute();
351 private:
352 RRVip& mVip;
353 TupleMarkManager& mMarkManager;
354 std::vector<Item> mPath;
355 Tuple mResult;
356 };
357
358 class GreyOrBlacklistCommand : public DnsStub::Command
359 {
360 public:
361 GreyOrBlacklistCommand(RRVip& vip, TupleMarkManager& markManager, Item& pathTop, Tuple& result, uint64_t expiry, TupleMarkManager::MarkType markType) :
362 mVip(vip), mMarkManager(markManager), mPathTop(pathTop), mResult(result), mExpiry(expiry), mMarkType(markType) {}
363 virtual ~GreyOrBlacklistCommand() {}
364 virtual void execute();
365 private:
366 RRVip& mVip;
367 TupleMarkManager& mMarkManager;
368 Item mPathTop;
369 Tuple mResult;
370 uint64_t mExpiry;
372 };
373};
374
375EncodeStream& operator<<(EncodeStream& strm, const DnsResult&);
376EncodeStream& operator<<(EncodeStream& strm, const DnsResult::SRV&);
377EncodeStream& operator<<(EncodeStream& strm, const DnsResult::NAPTR&);
378
379}
380
381#endif
382
383// Copyright (c) 2003, Jason Fischl
384/* ====================================================================
385 * The Vovida Software License, Version 1.0
386 *
387 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
388 *
389 * Redistribution and use in source and binary forms, with or without
390 * modification, are permitted provided that the following conditions
391 * are met:
392 *
393 * 1. Redistributions of source code must retain the above copyright
394 * notice, this list of conditions and the following disclaimer.
395 *
396 * 2. Redistributions in binary form must reproduce the above copyright
397 * notice, this list of conditions and the following disclaimer in
398 * the documentation and/or other materials provided with the
399 * distribution.
400 *
401 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
402 * and "Vovida Open Communication Application Library (VOCAL)" must
403 * not be used to endorse or promote products derived from this
404 * software without prior written permission. For written
405 * permission, please contact vocal@vovida.org.
406 *
407 * 4. Products derived from this software may not be called "VOCAL", nor
408 * may "VOCAL" appear in their name, without prior written
409 * permission of Vovida Networks, Inc.
410 *
411 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
412 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
413 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
414 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
415 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
416 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
417 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
418 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
419 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
420 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
421 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
422 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
423 * DAMAGE.
424 *
425 * ====================================================================
426 *
427 * This software consists of voluntary contributions made by Vovida
428 * Networks, Inc. and many individuals on behalf of Vovida Networks,
429 * Inc. For more information on Vovida Networks, Inc., please see
430 * <http://www.vovida.org/>.
431 *
432 */
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
DnsNaptrRecord::RegExp regex
bool operator<(const NAPTR &rhs) const
TransportType transport
bool operator<(const SRV &rhs) const
void lookup(const Uri &uri)
bool greylistLast(uint64_t expiry)
RESIP_HeapCount(DnsResult)
friend EncodeStream & operator<<(EncodeStream &strm, const DnsResult::SRV &)
unsigned int getSRVResultsSize() const
std::vector< Data > DataVector
Definition DnsResult.hxx:82
friend EncodeStream & operator<<(EncodeStream &strm, const DnsResult &)
friend class LookupCommand
bool blacklistLast(uint64_t expiry)
Data target() const
friend EncodeStream & operator<<(EncodeStream &strm, const DnsResult::NAPTR &)
virtual ~DnsResult()
virtual void onEnumResult(const DNSResult< DnsNaptrRecord > &result, int order)=0
void onEnumResult(const DNSResult< DnsNaptrRecord > &result)
void onDnsResult(const DNSResult< DnsHostRecord > &)
Definition DnsResult.cxx:60
void onNaptrResult(const DNSResult< DnsNaptrRecord > &result)
Represents a network address.
Definition Tuple.hxx:59
Represents the "SIP-URI" and "SIPS-URI" elements in the RFC 3261 grammar.
Definition Uri.hxx:22
dcm! – add UnusedChecking(_enum) below;
std::mutex Mutex
A semaphore that can be locked by only one thread at a time.
Definition Mutex.hxx:37
unsigned long long uint64_t
Definition Stun.hxx:39
TransportType
An enumeration of transport protocols.
EncodeStream & operator<<(EncodeStream &strm, const BaseException &e)
#define EncodeStream
unsigned __int64 uint64_t
Definition stdint.h:95
void list()
Definition userAdmin.cxx:66