3 * Background reverse name resolution (ADNS version)
5 * (c) 2003 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 * HOWEVER, since GNU adns is covered by the full GNU General Public
29 * License, this file (only) is also covered by the full GNU GPL, and
30 * you may NOT take advantage of the more liberal conditions of the
31 * LGPL when modifying or redistributing this file. This doesn't mean
32 * that a program which uses the interface provided by this file is
33 * covered by the GPL, since @bres.c@ provides the same interface and
34 * is LGPL. However, it does mean that if your program depends, for
35 * some reason (e.g., to meet particular performance criteria), on
36 * this adns-based implementation of mLib's background resolver then it
37 * must be licensed under the full GPL.
43 # error "You need the ADNS library to compile this file."
46 /*----- Header files ------------------------------------------------------*/
55 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
73 /*----- Static variables --------------------------------------------------*/
75 static adns_state ads;
76 static sel_state *sel;
77 static sel_hook selhook;
79 /*----- Main code ---------------------------------------------------------*/
81 /* --- @bres_abort@ --- *
83 * Arguments: @bres_client *rc@ = pointer to client block
87 * Use: Removes a queued job.
90 void bres_abort(bres_client *rc)
92 if (rc->q == adns_r_addr) xfree(rc->u.name);
93 if (rc->a) free(rc->a);
97 /* --- @bres_byaddr@ --- *
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
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
111 void bres_byaddr(bres_client *rc, struct in_addr addr,
112 void (*func)(struct hostent */*h*/, void */*p*/),
116 struct sockaddr_in sin;
119 sin.sin_family = AF_INET;
122 if ((e = adns_submit_reverse(ads, (struct sockaddr *)&sin, adns_r_ptr,
123 0, rc, &rc->aq)) != 0)
136 /* --- @bres_byname@ --- *
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
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
150 void bres_byname(bres_client *rc, const char *name,
151 void (*func)(struct hostent */*h*/, void */*p*/),
157 if ((e = adns_submit(ads, name, adns_r_addr,
158 adns_qf_search | adns_qf_owner, rc, &rc->aq)) != 0)
162 rc->u.name = xstrdup(name);
171 /* --- @bres_exec@ --- *
173 * Arguments: @const char *file@ = file containing server code or null
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.
185 void bres_exec(const char *file)
190 /* --- @report@ --- *
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)
201 * Use: Formats the given answer into a @struct hostent@ and reports
202 * it to the waiting client application.
205 static void report(bres_client *rc, adns_answer *a,
206 adns_rr_addr *av, int an,
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];
220 for (i = j = 0; i < an && j < N(aa) - 1; i++) {
221 if (av[i].addr.sa.sa_family == AF_INET)
222 aa[j++] = (char *)&av[i].addr.inet.sin_addr;
227 h.h_addrtype = AF_INET;
228 h.h_length = sizeof(struct in_addr);
233 /* --- @beforehook@, @afterhook@ --- *
235 * Arguments: @sel_state *s@ = select state
236 * @sel_args *sa@ = argument block
237 * @void *p@ = uninteresting pointer
241 * Use: Processes the selector's arguments before @select@ is
242 * called, to allow ADNS to do its thing.
245 static void beforehook(sel_state *s, sel_args *sa, void *p)
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);
252 static void afterhook(sel_state *s, sel_args *sa, void *p)
261 adns_afterselect(ads, sa->maxfd,
262 &sa->fd[SEL_READ], &sa->fd[SEL_WRITE], &sa->fd[SEL_EXC],
264 while (q = 0, (e = adns_check(ads, &q, &a, &c)) == 0) {
268 else switch (rc->q) {
270 assert(a->type == adns_r_addr);
272 report(rc, a, a->rrs.addr, a->nrrs, &a->owner, 1);
276 if (a->type == adns_r_ptr) {
278 if ((e = adns_submit(ads, a->rrs.str[0], adns_r_addr,
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 ==
293 report(rc, a, &a->rrs.addr[i], 1, aa->rrs.str, aa->nrrs);
304 if (rc->q == adns_r_addr) xfree(rc->u.name);
305 if (rc->a) free(rc->a);
311 /* --- @bres_init@ --- *
313 * Arguments: @sel_state *s@ = pointer to select multiplexor
317 * Use: Initializes the background resolver for use.
320 void bres_init(sel_state *s)
324 if ((e = adns_init(&ads, adns_if_noautosys, 0)) != 0) {
325 moan("adns_init failed: resolver won't work");
328 sel_addhook(s, &selhook, beforehook, afterhook, 0);
332 /*----- That's all, folks -------------------------------------------------*/