5 * Background reverse name resolution (ADNS version)
7 * (c) 2003 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
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.
45 # error "You need the ADNS library to compile this file."
48 /*----- Header files ------------------------------------------------------*/
57 #include <sys/types.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
75 /*----- Static variables --------------------------------------------------*/
77 static adns_state ads;
78 static sel_state *sel;
79 static sel_hook selhook;
81 /*----- Main code ---------------------------------------------------------*/
83 /* --- @bres_abort@ --- *
85 * Arguments: @bres_client *rc@ = pointer to client block
89 * Use: Removes a queued job.
92 void bres_abort(bres_client *rc)
94 if (rc->q == adns_r_addr) xfree(rc->u.name);
95 if (rc->a) free(rc->a);
99 /* --- @bres_byaddr@ --- *
101 * Arguments: @bres_client *rc@ = pointer to client block
102 * @struct in_addr addr@ = address to resolve
103 * @void (*func)(struct hostent *h, void *p)@ = handler function
104 * @void *p@ = argument for handler function
108 * Use: Adds an address lookup job to the queue. The job will be
109 * processed when there's a spare resolver process to deal with
113 void bres_byaddr(bres_client *rc, struct in_addr addr,
114 void (*func)(struct hostent */*h*/, void */*p*/),
118 struct sockaddr_in sin;
121 sin.sin_family = AF_INET;
124 if ((e = adns_submit_reverse(ads, (struct sockaddr *)&sin, adns_r_ptr,
125 0, rc, &rc->aq)) != 0)
138 /* --- @bres_byname@ --- *
140 * Arguments: @bres_client *rc@ = pointer to client block
141 * @const char *name@ = name to resolve
142 * @void (*func)(struct hostent *h, void *p)@ = handler function
143 * @void *p@ = argument for handler function
147 * Use: Adds a name lookup job to the queue. The job will be
148 * processed when there's a spare resolver process to deal with
152 void bres_byname(bres_client *rc, const char *name,
153 void (*func)(struct hostent */*h*/, void */*p*/),
159 if ((e = adns_submit(ads, name, adns_r_addr,
160 adns_qf_search | adns_qf_owner, rc, &rc->aq)) != 0)
164 rc->u.name = xstrdup(name);
173 /* --- @bres_exec@ --- *
175 * Arguments: @const char *file@ = file containing server code or null
179 * Use: Makes `bres' use a standalone server rather than copies of
180 * the current process. This can reduce memory consumption for
181 * large processes, at the expense of startup time (which
182 * shouldn't be too bad anyway, because of the resolver design).
183 * If the filename is null, a default set up at install time is
184 * used. It's probably a good idea to leave it alone.
187 void bres_exec(const char *file)
192 /* --- @report@ --- *
194 * Arguments: @bres_client *c@ = client descriptor block
195 * @adns_answer *a@ = A-record answer from resolver library
196 * @adns_rr_addr *av@ = vector of address records
197 * @int an@ = number of address records (must be positive)
198 * @char **nv@ = vector of name strings
199 * @int nn@ = number of name strings (must be positive)
203 * Use: Formats the given answer into a @struct hostent@ and reports
204 * it to the waiting client application.
207 static void report(bres_client *rc, adns_answer *a,
208 adns_rr_addr *av, int an,
217 if (a->cname) n[j++] = a->cname;
218 else { n[j++] = *nv; nv++; nn--; }
219 for (i = 0; i < nn && j < N(n) - 1; i++)
220 if (strcmp(n[0], nv[i]) != 0) n[j++] = nv[i];
222 for (i = j = 0; i < an && j < N(av) - 1; i++) {
223 if (av[i].addr.sa.sa_family == AF_INET)
224 aa[j++] = (char *)&av[i].addr.inet.sin_addr;
229 h.h_addrtype = AF_INET;
230 h.h_length = sizeof(struct in_addr);
235 /* --- @beforehook@, @afterhook@ --- *
237 * Arguments: @sel_state *s@ = select state
238 * @sel_args *sa@ = argument block
239 * @void *p@ = uninteresting pointer
243 * Use: Processes the selector's arguments before @select@ is
244 * called, to allow ADNS to do its thing.
247 static void beforehook(sel_state *s, sel_args *sa, void *p)
249 adns_beforeselect(ads, &sa->maxfd,
250 &sa->fd[SEL_READ], &sa->fd[SEL_WRITE], &sa->fd[SEL_EXC],
251 &sa->tvp, &sa->tv, &sa->now);
254 static void afterhook(sel_state *s, sel_args *sa, void *p)
263 adns_afterselect(ads, sa->maxfd,
264 &sa->fd[SEL_READ], &sa->fd[SEL_WRITE], &sa->fd[SEL_EXC],
266 while (q = 0, (e = adns_check(ads, &q, &a, &c)) == 0) {
270 else switch (rc->q) {
272 assert(a->type == adns_r_addr);
274 report(rc, a, a->rrs.addr, a->nrrs, &a->owner, 1);
278 if (a->type == adns_r_ptr) {
280 if ((e = adns_submit(ads, a->rrs.str[0], adns_r_addr,
285 assert(a->type == adns_r_addr);
286 for (i = 0; i < a->nrrs; i++) {
287 if (a->rrs.addr[i].addr.sa.sa_family == AF_INET &&
288 a->rrs.addr[i].addr.inet.sin_addr.s_addr ==
295 report(rc, a, &a->rrs.addr[i], 1, aa->rrs.str, aa->nrrs);
306 if (rc->q == adns_r_addr) xfree(rc->u.name);
307 if (rc->a) free(rc->a);
313 /* --- @bres_init@ --- *
315 * Arguments: @sel_state *s@ = pointer to select multiplexor
319 * Use: Initializes the background resolver for use.
322 void bres_init(sel_state *s)
326 if ((e = adns_init(&ads, adns_if_noautosys, 0)) != 0) {
327 moan("adns_init failed: resolver won't work");
330 sel_addhook(s, &selhook, beforehook, afterhook, 0);
334 /*----- That's all, folks -------------------------------------------------*/