14d7100d |
1 | /* -*-c-*- |
2 | * |
1dc93196 |
3 | * $Id$ |
14d7100d |
4 | * |
5 | * Background reverse name resolution (ADNS version) |
6 | * |
7 | * (c) 2003 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of the mLib utilities library. |
13 | * |
14 | * mLib is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * mLib is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with mLib; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | * |
29 | * |
30 | * HOWEVER, since GNU adns is covered by the full GNU General Public |
31 | * License, this file (only) is also covered by the full GNU GPL, and |
32 | * you may NOT take advantage of the more liberal conditions of the |
33 | * LGPL when modifying or redistributing this file. This doesn't mean |
34 | * that a program which uses the interface provided by this file is |
35 | * covered by the GPL, since @bres.c@ provides the same interface and |
36 | * is LGPL. However, it does mean that if your program depends, for |
37 | * some reason (e.g., to meet particular performance criteria), on |
38 | * this adns-based implementation of mLib's background resolver then it |
39 | * must be licensed under the full GPL. |
40 | */ |
41 | |
14d7100d |
42 | #ifndef HAVE_ADNS |
43 | # error "You need the ADNS library to compile this file." |
44 | #endif |
45 | |
46 | /*----- Header files ------------------------------------------------------*/ |
47 | |
48 | #include <assert.h> |
49 | #include <errno.h> |
50 | #include <signal.h> |
51 | #include <stdio.h> |
52 | #include <stdlib.h> |
53 | #include <string.h> |
54 | |
55 | #include <sys/types.h> |
56 | #include <sys/time.h> |
57 | #include <unistd.h> |
58 | #include <sys/wait.h> |
59 | |
60 | #include <sys/socket.h> |
61 | #include <netinet/in.h> |
62 | #include <arpa/inet.h> |
63 | #include <netdb.h> |
64 | |
65 | #include <adns.h> |
66 | |
67 | #include "alloc.h" |
68 | #include "bres.h" |
69 | #include "macros.h" |
70 | #include "report.h" |
71 | #include "sel.h" |
72 | |
73 | /*----- Static variables --------------------------------------------------*/ |
74 | |
75 | static adns_state ads; |
76 | static sel_state *sel; |
77 | static sel_hook selhook; |
78 | |
79 | /*----- Main code ---------------------------------------------------------*/ |
80 | |
81 | /* --- @bres_abort@ --- * |
82 | * |
83 | * Arguments: @bres_client *rc@ = pointer to client block |
84 | * |
85 | * Returns: --- |
86 | * |
87 | * Use: Removes a queued job. |
88 | */ |
89 | |
90 | void bres_abort(bres_client *rc) |
91 | { |
92 | if (rc->q == adns_r_addr) xfree(rc->u.name); |
93 | if (rc->a) free(rc->a); |
94 | adns_cancel(rc->aq); |
95 | } |
96 | |
97 | /* --- @bres_byaddr@ --- * |
98 | * |
99 | * Arguments: @bres_client *rc@ = pointer to client block |
100 | * @struct in_addr addr@ = address to resolve |
101 | * @void (*func)(struct hostent *h, void *p)@ = handler function |
102 | * @void *p@ = argument for handler function |
103 | * |
104 | * Returns: --- |
105 | * |
106 | * Use: Adds an address lookup job to the queue. The job will be |
107 | * processed when there's a spare resolver process to deal with |
108 | * it. |
109 | */ |
110 | |
111 | void bres_byaddr(bres_client *rc, struct in_addr addr, |
112 | void (*func)(struct hostent */*h*/, void */*p*/), |
113 | void *p) |
114 | { |
115 | int e; |
116 | struct sockaddr_in sin; |
117 | |
118 | if (!ads) goto fail; |
119 | sin.sin_family = AF_INET; |
120 | sin.sin_addr = addr; |
121 | sin.sin_port = 0; |
122 | if ((e = adns_submit_reverse(ads, (struct sockaddr *)&sin, adns_r_ptr, |
123 | 0, rc, &rc->aq)) != 0) |
124 | goto fail; |
125 | rc->a = 0; |
126 | rc->q = adns_r_ptr; |
127 | rc->u.addr = addr; |
128 | rc->func = func; |
129 | rc->p = p; |
130 | return; |
131 | |
132 | fail: |
133 | func(0, p); |
134 | } |
135 | |
136 | /* --- @bres_byname@ --- * |
137 | * |
138 | * Arguments: @bres_client *rc@ = pointer to client block |
139 | * @const char *name@ = name to resolve |
140 | * @void (*func)(struct hostent *h, void *p)@ = handler function |
141 | * @void *p@ = argument for handler function |
142 | * |
143 | * Returns: --- |
144 | * |
145 | * Use: Adds a name lookup job to the queue. The job will be |
146 | * processed when there's a spare resolver process to deal with |
147 | * it. |
148 | */ |
149 | |
150 | void bres_byname(bres_client *rc, const char *name, |
151 | void (*func)(struct hostent */*h*/, void */*p*/), |
152 | void *p) |
153 | { |
154 | int e; |
155 | |
156 | if (!ads) goto fail; |
157 | if ((e = adns_submit(ads, name, adns_r_addr, |
4f6d400b |
158 | adns_qf_search | adns_qf_owner, rc, &rc->aq)) != 0) |
14d7100d |
159 | goto fail; |
160 | rc->a = 0; |
161 | rc->q = adns_r_addr; |
162 | rc->u.name = xstrdup(name); |
163 | rc->func = func; |
164 | rc->p = p; |
165 | return; |
166 | |
167 | fail: |
168 | func(0, p); |
169 | } |
170 | |
171 | /* --- @bres_exec@ --- * |
172 | * |
173 | * Arguments: @const char *file@ = file containing server code or null |
174 | * |
175 | * Returns: --- |
176 | * |
177 | * Use: Makes `bres' use a standalone server rather than copies of |
178 | * the current process. This can reduce memory consumption for |
179 | * large processes, at the expense of startup time (which |
180 | * shouldn't be too bad anyway, because of the resolver design). |
181 | * If the filename is null, a default set up at install time is |
182 | * used. It's probably a good idea to leave it alone. |
183 | */ |
184 | |
185 | void bres_exec(const char *file) |
186 | { |
187 | /* Nothin' doin' */ |
188 | } |
189 | |
190 | /* --- @report@ --- * |
191 | * |
192 | * Arguments: @bres_client *c@ = client descriptor block |
193 | * @adns_answer *a@ = A-record answer from resolver library |
194 | * @adns_rr_addr *av@ = vector of address records |
195 | * @int an@ = number of address records (must be positive) |
196 | * @char **nv@ = vector of name strings |
197 | * @int nn@ = number of name strings (must be positive) |
198 | * |
199 | * Returns: --- |
200 | * |
201 | * Use: Formats the given answer into a @struct hostent@ and reports |
202 | * it to the waiting client application. |
203 | */ |
204 | |
205 | static void report(bres_client *rc, adns_answer *a, |
206 | adns_rr_addr *av, int an, |
207 | char **nv, int nn) |
208 | { |
209 | struct hostent h; |
210 | char *n[16]; |
211 | char *aa[16]; |
212 | int i, j; |
213 | |
214 | j = 0; |
215 | if (a->cname) n[j++] = a->cname; |
216 | else { n[j++] = *nv; nv++; nn--; } |
217 | for (i = 0; i < nn && j < N(n) - 1; i++) |
218 | if (strcmp(n[0], nv[i]) != 0) n[j++] = nv[i]; |
219 | n[j++] = 0; |
220 | for (i = j = 0; i < an && j < N(av) - 1; i++) { |
221 | if (av[i].addr.sa.sa_family == AF_INET) |
222 | aa[j++] = (char *)&av[i].addr.inet.sin_addr; |
223 | } |
224 | aa[j++] = 0; |
225 | h.h_name = n[0]; |
226 | h.h_aliases = n + 1; |
227 | h.h_addrtype = AF_INET; |
228 | h.h_length = sizeof(struct in_addr); |
229 | h.h_addr_list = aa; |
230 | rc->func(&h, rc->p); |
231 | } |
232 | |
233 | /* --- @beforehook@, @afterhook@ --- * |
234 | * |
235 | * Arguments: @sel_state *s@ = select state |
236 | * @sel_args *sa@ = argument block |
237 | * @void *p@ = uninteresting pointer |
238 | * |
239 | * Returns: --- |
240 | * |
241 | * Use: Processes the selector's arguments before @select@ is |
242 | * called, to allow ADNS to do its thing. |
243 | */ |
244 | |
245 | static void beforehook(sel_state *s, sel_args *sa, void *p) |
246 | { |
247 | adns_beforeselect(ads, &sa->maxfd, |
248 | &sa->fd[SEL_READ], &sa->fd[SEL_WRITE], &sa->fd[SEL_EXC], |
249 | &sa->tvp, &sa->tv, &sa->now); |
250 | } |
251 | |
252 | static void afterhook(sel_state *s, sel_args *sa, void *p) |
253 | { |
254 | void *c; |
255 | bres_client *rc; |
256 | adns_query q; |
257 | adns_answer *a; |
258 | int e; |
259 | int i; |
260 | |
261 | adns_afterselect(ads, sa->maxfd, |
262 | &sa->fd[SEL_READ], &sa->fd[SEL_WRITE], &sa->fd[SEL_EXC], |
263 | &sa->now); |
264 | while (q = 0, (e = adns_check(ads, &q, &a, &c)) == 0) { |
265 | rc = c; |
266 | if (a->status != 0) |
267 | goto fail; |
268 | else switch (rc->q) { |
269 | case adns_r_addr: |
270 | assert(a->type == adns_r_addr); |
4f6d400b |
271 | report(rc, a, a->rrs.addr, a->nrrs, &a->owner, 1); |
1dc93196 |
272 | xfree(rc->u.name); |
14d7100d |
273 | free(a); |
274 | break; |
275 | case adns_r_ptr: |
276 | if (a->type == adns_r_ptr) { |
277 | rc->a = a; |
278 | if ((e = adns_submit(ads, a->rrs.str[0], adns_r_addr, |
279 | 0, rc, &q)) != 0) |
280 | goto fail; |
281 | rc->aq = q; |
282 | } else { |
283 | assert(a->type == adns_r_addr); |
284 | for (i = 0; i < a->nrrs; i++) { |
285 | if (a->rrs.addr[i].addr.sa.sa_family == AF_INET && |
286 | a->rrs.addr[i].addr.inet.sin_addr.s_addr == |
287 | rc->u.addr.s_addr) |
288 | goto match; |
289 | } |
290 | goto fail; |
291 | match: |
292 | report(rc, a, &a->rrs.addr[i], 1, rc->a->rrs.str, rc->a->nrrs); |
293 | free(rc->a); |
294 | free(a); |
295 | } |
296 | break; |
297 | default: |
298 | abort(); |
299 | } |
300 | continue; |
301 | |
302 | fail: |
303 | rc->func(0, rc->p); |
304 | if (rc->q == adns_r_addr) xfree(rc->u.name); |
305 | if (rc->a) free(rc->a); |
306 | free(a); |
307 | } |
308 | } |
309 | |
310 | /* --- @bres_init@ --- * |
311 | * |
312 | * Arguments: @sel_state *s@ = pointer to select multiplexor |
313 | * |
314 | * Returns: --- |
315 | * |
316 | * Use: Initializes the background resolver for use. |
317 | */ |
318 | |
319 | void bres_init(sel_state *s) |
320 | { |
321 | int e; |
322 | |
d46d1a2f |
323 | if ((e = adns_init(&ads, adns_if_noautosys, 0)) != 0) { |
14d7100d |
324 | moan("adns_init failed: resolver won't work"); |
325 | return; |
326 | } |
327 | sel_addhook(s, &selhook, beforehook, afterhook, 0); |
328 | sel = s; |
329 | } |
330 | |
331 | /*----- That's all, folks -------------------------------------------------*/ |