Branch data Line data Source code
1 : : // $Id: DNS_Mgr.h 6915 2009-09-22 05:04:17Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #ifndef dnsmgr_h
6 : : #define dnsmgr_h
7 : :
8 : : #include <list>
9 : : #include <map>
10 : : #include <queue>
11 : :
12 : : #include "util.h"
13 : : #include "BroList.h"
14 : : #include "Dict.h"
15 : : #include "EventHandler.h"
16 : : #include "IOSource.h"
17 : :
18 : : class Val;
19 : : class ListVal;
20 : : class TableVal;
21 : : class Func;
22 : : class EventHandler;
23 : : class RecordType;
24 : : class DNS_Mgr_Request;
25 : :
26 : : struct nb_dns_info;
27 : : struct nb_dns_result;
28 : :
29 [ - + ][ # # ]: 5 : declare(PDict,ListVal);
30 : :
31 : : class DNS_Mapping;
32 [ - + ][ # # ]: 181 : declare(PDict,DNS_Mapping);
33 : :
34 : : enum DNS_MgrMode {
35 : : DNS_PRIME, // used to prime the cache
36 : : DNS_FORCE, // internal error if cache miss
37 : : DNS_DEFAULT, // lookup names as they're requested
38 : : DNS_FAKE, // don't look up names, just return dummy results
39 : : };
40 : :
41 : : // Number of seconds we'll wait for a reply.
42 : : #define DNS_TIMEOUT 5
43 : :
44 : : // ### For now, we don't support IPv6 lookups. When we do, this
45 : : // should become addr_type.
46 : : typedef uint32 dns_mgr_addr_type;
47 : :
48 : : class DNS_Mgr : public IOSource {
49 : : public:
50 : : DNS_Mgr(DNS_MgrMode mode);
51 : : virtual ~DNS_Mgr();
52 : :
53 : : bool Init();
54 : :
55 : : // Looks up the address or addresses of the given host, and returns
56 : : // a set of addr.
57 : : TableVal* LookupHost(const char* host);
58 : :
59 : : Val* LookupAddr(uint32 addr);
60 : :
61 : : // Define the directory where to store the data.
62 : 3 : void SetDir(const char* arg_dir) { dir = copy_string(arg_dir); }
63 : :
64 : : void Verify();
65 : : void Resolve();
66 : : int Save();
67 : :
68 : : const char* LookupAddrInCache(dns_mgr_addr_type addr);
69 : : TableVal* LookupNameInCache(string name);
70 : :
71 : : // Support for async lookups.
72 : : class LookupCallback {
73 : : public:
74 : 0 : LookupCallback() { }
75 [ # # ][ # # ]: 0 : virtual ~LookupCallback() { }
[ # # ]
76 : :
77 : 0 : virtual void Resolved(const char* name) { };
78 : 0 : virtual void Resolved(TableVal* addrs) { };
79 : : virtual void Timeout() = 0;
80 : : };
81 : :
82 : : void AsyncLookupAddr(dns_mgr_addr_type host, LookupCallback* callback);
83 : : void AsyncLookupName(string name, LookupCallback* callback);
84 : :
85 : : protected:
86 : : friend class LookupCallback;
87 : : friend class DNS_Mgr_Request;
88 : :
89 : : void Event(EventHandlerPtr e, DNS_Mapping* dm,
90 : : ListVal* l1 = 0, ListVal* l2 = 0);
91 : : void Event(EventHandlerPtr e, DNS_Mapping* old_dm, DNS_Mapping* new_dm);
92 : :
93 : : Val* BuildMappingVal(DNS_Mapping* dm);
94 : :
95 : : void AddResult(DNS_Mgr_Request* dr, struct nb_dns_result* r);
96 : : void CompareMappings(DNS_Mapping* prev_dm, DNS_Mapping* new_dm);
97 : : ListVal* AddrListDelta(ListVal* al1, ListVal* al2);
98 : : void DumpAddrList(FILE* f, ListVal* al);
99 : :
100 : : void LoadCache(FILE* f);
101 : : void Save(FILE* f, PDict(DNS_Mapping)& m);
102 : :
103 : : // Selects on the fd to see if there is an answer available (timeout is
104 : : // secs). Returns 0 on timeout, -1 on EINTR, and 1 if answer is ready.
105 : : int AnswerAvailable(int timeout);
106 : :
107 : : // Issue as many queued async requests as slots are available.
108 : : void IssueAsyncRequests();
109 : :
110 : : // Finish the request if we have a result. If not, time it out if
111 : : // requested.
112 : : void CheckAsyncAddrRequest(dns_mgr_addr_type addr, bool timeout);
113 : : void CheckAsyncHostRequest(const char* host, bool timeout);
114 : :
115 : : // IOSource interface.
116 : : virtual void GetFds(int* read, int* write, int* except);
117 : : virtual double NextTimestamp(double* network_time);
118 : : virtual void Process();
119 : 0 : virtual const char* Tag() { return "DNS_Mgr"; }
120 : :
121 : : DNS_MgrMode mode;
122 : :
123 : : PDict(ListVal) services;
124 : :
125 : : PDict(DNS_Mapping) host_mappings;
126 : : PDict(DNS_Mapping) addr_mappings;
127 : :
128 : : DNS_mgr_request_list requests;
129 : :
130 : : nb_dns_info* nb_dns;
131 : : char* cache_name;
132 : : char* dir; // directory in which cache_name resides
133 : :
134 : : int did_init;
135 : :
136 : : // DNS-related events.
137 : : EventHandlerPtr dns_mapping_valid;
138 : : EventHandlerPtr dns_mapping_unverified;
139 : : EventHandlerPtr dns_mapping_new_name;
140 : : EventHandlerPtr dns_mapping_lost_name;
141 : : EventHandlerPtr dns_mapping_name_changed;
142 : : EventHandlerPtr dns_mapping_altered;
143 : :
144 : : RecordType* dm_rec;
145 : :
146 : : int dns_fake_count; // used to generate unique fake replies
147 : :
148 : : typedef list<LookupCallback*> CallbackList;
149 : :
150 : 0 : struct AsyncRequest {
151 : : double time;
152 : : dns_mgr_addr_type host;
153 : : string name;
154 : : CallbackList callbacks;
155 : :
156 : 0 : bool IsAddrReq() const { return name.length() == 0; }
157 : :
158 : 0 : void Resolved(const char* name)
159 : : {
160 [ # # ]: 0 : for ( CallbackList::iterator i = callbacks.begin();
161 : : i != callbacks.end(); ++i )
162 : : {
163 : 0 : (*i)->Resolved(name);
164 [ # # ]: 0 : delete *i;
165 : : }
166 : 0 : callbacks.clear();
167 : 0 : }
168 : :
169 : 0 : void Resolved(TableVal* addrs)
170 : : {
171 [ # # ]: 0 : for ( CallbackList::iterator i = callbacks.begin();
172 : : i != callbacks.end(); ++i )
173 : : {
174 : 0 : (*i)->Resolved(addrs);
175 [ # # ]: 0 : delete *i;
176 : : }
177 : 0 : callbacks.clear();
178 : 0 : }
179 : :
180 : 0 : void Timeout()
181 : : {
182 [ # # ]: 0 : for ( CallbackList::iterator i = callbacks.begin();
183 : : i != callbacks.end(); ++i )
184 : : {
185 : 0 : (*i)->Timeout();
186 [ # # ]: 0 : delete *i;
187 : : }
188 : 0 : callbacks.clear();
189 : 0 : }
190 : :
191 : : };
192 : :
193 : : typedef map<dns_mgr_addr_type, AsyncRequest*> AsyncRequestAddrMap;
194 : : AsyncRequestAddrMap asyncs_addrs;
195 : :
196 : : typedef map<string, AsyncRequest*> AsyncRequestNameMap;
197 : : AsyncRequestNameMap asyncs_names;
198 : :
199 : : typedef list<AsyncRequest*> QueuedList;
200 : : QueuedList asyncs_queued;
201 : :
202 : : typedef priority_queue<AsyncRequest*> TimeoutQueue;
203 : : TimeoutQueue asyncs_timeouts;
204 : :
205 : : int asyncs_pending;
206 : : };
207 : :
208 : : extern DNS_Mgr* dns_mgr;
209 : :
210 : : #endif
|