54e6708cf3b515fa4465d547034fae30a2412611
3 * $Id: bres-adns.c,v 1.1 2003/12/13 20:37:59 mdw Exp $
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.
42 /*----- Revision history --------------------------------------------------*
44 * $Log: bres-adns.c,v $
45 * Revision 1.1 2003/12/13 20:37:59 mdw
46 * Add adns support in background resolver.
51 # error "You need the ADNS library to compile this file."
54 /*----- Header files ------------------------------------------------------*/
63 #include <sys/types.h>
68 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
81 /*----- Static variables --------------------------------------------------*/
83 static adns_state ads
;
84 static sel_state
*sel
;
85 static sel_hook selhook
;
87 /*----- Main code ---------------------------------------------------------*/
89 /* --- @bres_abort@ --- *
91 * Arguments: @bres_client *rc@ = pointer to client block
95 * Use: Removes a queued job.
98 void bres_abort(bres_client
*rc
)
100 if (rc
->q
== adns_r_addr
) xfree(rc
->u
.name
);
101 if (rc
->a
) free(rc
->a
);
105 /* --- @bres_byaddr@ --- *
107 * Arguments: @bres_client *rc@ = pointer to client block
108 * @struct in_addr addr@ = address to resolve
109 * @void (*func)(struct hostent *h, void *p)@ = handler function
110 * @void *p@ = argument for handler function
114 * Use: Adds an address lookup job to the queue. The job will be
115 * processed when there's a spare resolver process to deal with
119 void bres_byaddr(bres_client
*rc
, struct in_addr addr
,
120 void (*func
)(struct hostent */
*h*/
, void */
*p*/
),
124 struct sockaddr_in sin
;
127 sin
.sin_family
= AF_INET
;
130 if ((e
= adns_submit_reverse(ads
, (struct sockaddr
*)&sin
, adns_r_ptr
,
131 0, rc
, &rc
->aq
)) != 0)
144 /* --- @bres_byname@ --- *
146 * Arguments: @bres_client *rc@ = pointer to client block
147 * @const char *name@ = name to resolve
148 * @void (*func)(struct hostent *h, void *p)@ = handler function
149 * @void *p@ = argument for handler function
153 * Use: Adds a name lookup job to the queue. The job will be
154 * processed when there's a spare resolver process to deal with
158 void bres_byname(bres_client
*rc
, const char *name
,
159 void (*func
)(struct hostent */
*h*/
, void */
*p*/
),
165 if ((e
= adns_submit(ads
, name
, adns_r_addr
,
166 adns_qf_search
, rc
, &rc
->aq
)) != 0)
170 rc
->u
.name
= xstrdup(name
);
179 /* --- @bres_exec@ --- *
181 * Arguments: @const char *file@ = file containing server code or null
185 * Use: Makes `bres' use a standalone server rather than copies of
186 * the current process. This can reduce memory consumption for
187 * large processes, at the expense of startup time (which
188 * shouldn't be too bad anyway, because of the resolver design).
189 * If the filename is null, a default set up at install time is
190 * used. It's probably a good idea to leave it alone.
193 void bres_exec(const char *file
)
198 /* --- @report@ --- *
200 * Arguments: @bres_client *c@ = client descriptor block
201 * @adns_answer *a@ = A-record answer from resolver library
202 * @adns_rr_addr *av@ = vector of address records
203 * @int an@ = number of address records (must be positive)
204 * @char **nv@ = vector of name strings
205 * @int nn@ = number of name strings (must be positive)
209 * Use: Formats the given answer into a @struct hostent@ and reports
210 * it to the waiting client application.
213 static void report(bres_client
*rc
, adns_answer
*a
,
214 adns_rr_addr
*av
, int an
,
223 if (a
->cname
) n
[j
++] = a
->cname
;
224 else { n
[j
++] = *nv
; nv
++; nn
--; }
225 for (i
= 0; i
< nn
&& j
< N(n
) - 1; i
++)
226 if (strcmp(n
[0], nv
[i
]) != 0) n
[j
++] = nv
[i
];
228 for (i
= j
= 0; i
< an
&& j
< N(av
) - 1; i
++) {
229 if (av
[i
].addr
.sa
.sa_family
== AF_INET
)
230 aa
[j
++] = (char *)&av
[i
].addr
.inet
.sin_addr
;
235 h
.h_addrtype
= AF_INET
;
236 h
.h_length
= sizeof(struct in_addr
);
241 /* --- @beforehook@, @afterhook@ --- *
243 * Arguments: @sel_state *s@ = select state
244 * @sel_args *sa@ = argument block
245 * @void *p@ = uninteresting pointer
249 * Use: Processes the selector's arguments before @select@ is
250 * called, to allow ADNS to do its thing.
253 static void beforehook(sel_state
*s
, sel_args
*sa
, void *p
)
255 adns_beforeselect(ads
, &sa
->maxfd
,
256 &sa
->fd
[SEL_READ
], &sa
->fd
[SEL_WRITE
], &sa
->fd
[SEL_EXC
],
257 &sa
->tvp
, &sa
->tv
, &sa
->now
);
260 static void afterhook(sel_state
*s
, sel_args
*sa
, void *p
)
269 adns_afterselect(ads
, sa
->maxfd
,
270 &sa
->fd
[SEL_READ
], &sa
->fd
[SEL_WRITE
], &sa
->fd
[SEL_EXC
],
272 while (q
= 0, (e
= adns_check(ads
, &q
, &a
, &c
)) == 0) {
276 else switch (rc
->q
) {
278 assert(a
->type
== adns_r_addr
);
279 report(rc
, a
, a
->rrs
.addr
, a
->nrrs
, &rc
->u
.name
, 1);
284 if (a
->type
== adns_r_ptr
) {
286 if ((e
= adns_submit(ads
, a
->rrs
.str
[0], adns_r_addr
,
291 assert(a
->type
== adns_r_addr
);
292 for (i
= 0; i
< a
->nrrs
; i
++) {
293 if (a
->rrs
.addr
[i
].addr
.sa
.sa_family
== AF_INET
&&
294 a
->rrs
.addr
[i
].addr
.inet
.sin_addr
.s_addr
==
300 report(rc
, a
, &a
->rrs
.addr
[i
], 1, rc
->a
->rrs
.str
, rc
->a
->nrrs
);
312 if (rc
->q
== adns_r_addr
) xfree(rc
->u
.name
);
313 if (rc
->a
) free(rc
->a
);
318 /* --- @bres_init@ --- *
320 * Arguments: @sel_state *s@ = pointer to select multiplexor
324 * Use: Initializes the background resolver for use.
327 void bres_init(sel_state
*s
)
331 if ((e
= adns_init(&ads
, 0, 0)) != 0) {
332 moan("adns_init failed: resolver won't work");
335 sel_addhook(s
, &selhook
, beforehook
, afterhook
, 0);
339 /*----- That's all, folks -------------------------------------------------*/