ReactOS 0.4.16-dev-334-g4d9f67c
adh-query.c
Go to the documentation of this file.
1/*
2 * adh-query.c
3 * - useful general-purpose resolver client program
4 * make queries and print answers
5 */
6/*
7 * This file is
8 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
9 *
10 * It is part of adns, which is
11 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
12 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * This program 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 General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29#ifdef ADNS_JGAA_WIN32
30# include "adns_win32.h"
31#endif
32
33#include "adnshost.h"
34
37
38static unsigned long idcounter;
39
40void ensure_adns_init(void) {
42 int r;
43
44 if (ads) return;
45
46#ifdef SIGPIPE
47 if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno);
48#endif
49
52
53 if (config_text) {
55 } else {
56 r= adns_init(&ads, initflags, 0);
57 }
58 if (r) sysfail("adns_init",r);
59
62}
63
64static void prep_query(struct query_node **qun_r, int *quflags_r) {
65 struct query_node *qun;
66 char idbuf[20];
67
68 if (ov_pipe && !ads) usageerr("-f/--pipe not consistent with domains on command line");
70
71 qun= malloc(sizeof(*qun));
72 qun->pqfr= ov_pqfr;
73 if (ov_id) {
74 qun->id= xstrsave(ov_id);
75 } else {
76 sprintf(idbuf,"%lu",idcounter++);
77 idcounter &= 0x0fffffffflu;
78 qun->id= xstrsave(idbuf);
79 }
80
81 *quflags_r=
83 (ov_tcp ? adns_qf_usevc : 0) |
84 ((ov_pqfr.show_owner || ov_format == fmt_simple) ? adns_qf_owner : 0) |
89
90 *qun_r= qun;
91}
92
93void of_ptr(const struct optioninfo *oi, const char *arg, const char *arg2) {
94 struct query_node *qun;
95 int quflags, r;
96 struct sockaddr_in sa;
97
98 memset(&sa,0,sizeof(sa));
99 sa.sin_family= AF_INET;
100 if (!inet_aton(arg,&sa.sin_addr)) usageerr("invalid IP address %s",arg);
101
102 prep_query(&qun,&quflags);
103 qun->owner= xstrsave(arg);
105 (struct sockaddr*)&sa,
107 quflags,
108 qun,
109 &qun->qu);
110 if (r) sysfail("adns_submit_reverse",r);
111
113}
114
115void of_reverse(const struct optioninfo *oi, const char *arg, const char *arg2) {
116 struct query_node *qun;
117 int quflags, r;
118 struct sockaddr_in sa;
119
120 memset(&sa,0,sizeof(sa));
121 sa.sin_family= AF_INET;
122 if (!inet_aton(arg,&sa.sin_addr)) usageerr("invalid IP address %s",arg);
123
124 prep_query(&qun,&quflags);
125 qun->owner= xmalloc(strlen(arg) + strlen(arg2) + 2);
126 sprintf(qun->owner, "%s %s", arg,arg2);
128 (struct sockaddr*)&sa, arg2,
130 quflags,
131 qun,
132 &qun->qu);
133 if (r) sysfail("adns_submit_reverse",r);
134
136}
137
138void query_do(const char *domain) {
139 struct query_node *qun;
140 int quflags, r;
141
142 prep_query(&qun,&quflags);
143 qun->owner= xstrsave(domain);
146 quflags,
147 qun,
148 &qun->qu);
149 if (r) sysfail("adns_submit",r);
150
152}
153
154static void dequeue_query(struct query_node *qun) {
156 free(qun->id);
157 free(qun->owner);
158 free(qun);
159}
160
161static void print_withspace(const char *str) {
162 if (printf("%s ", str) == EOF) outerr();
163}
164
165static void print_ttl(struct query_node *qun, adns_answer *answer) {
166 unsigned long ttl;
167 time_t now;
168
169 switch (qun->pqfr.ttl) {
170 case tm_none:
171 return;
172 case tm_rel:
173 if (time(&now) == (time_t)-1) sysfail("get current time",errno);
174 ttl= answer->expires < now ? 0 : answer->expires - now;
175 break;
176 case tm_abs:
177 ttl= answer->expires;
178 break;
179 default:
180 abort();
181 }
182 if (printf("%lu ",ttl) == EOF) outerr();
183}
184
185static const char *owner_show(struct query_node *qun, adns_answer *answer) {
186 return answer->owner ? answer->owner : qun->owner;
187}
188
189static void print_owner_ttl(struct query_node *qun, adns_answer *answer) {
190 if (qun->pqfr.show_owner) print_withspace(owner_show(qun,answer));
191 print_ttl(qun,answer);
192}
193
194static void check_status(adns_status st) {
195 static const adns_status statuspoints[]= {
196 adns_s_ok,
199 };
200
201 const adns_status *spp;
202 int minrcode;
203
204 for (minrcode=0, spp=statuspoints;
205 spp < statuspoints + (sizeof(statuspoints)/sizeof(statuspoints[0]));
206 spp++)
207 if (st > *spp) minrcode++;
208 if (rcode < minrcode) rcode= minrcode;
209}
210
211static void print_status(adns_status st, struct query_node *qun, adns_answer *answer) {
212 const char *statustypeabbrev, *statusabbrev, *statusstring;
213
214 statustypeabbrev= adns_errtypeabbrev(st);
215 statusabbrev= adns_errabbrev(st);
216 statusstring= adns_strerror(st);
217 assert(!strchr(statusstring,'"'));
218
219 if (printf("%s %d %s ", statustypeabbrev, st, statusabbrev)
220 == EOF) outerr();
221 print_owner_ttl(qun,answer);
222 if (qun->pqfr.show_cname)
223 print_withspace(answer->cname ? answer->cname : "$");
224 if (printf("\"%s\"\n", statusstring) == EOF) outerr();
225}
226
227static void print_dnsfail(adns_status st, struct query_node *qun, adns_answer *answer) {
228 int r;
229 const char *typename, *statusstring;
230 adns_status ist;
231
232 if (ov_format == fmt_inline) {
233 if (fputs("; failed ",stdout) == EOF) outerr();
234 print_status(st,qun,answer);
235 return;
236 }
238 if (st == adns_s_nxdomain) {
239 r= fprintf(stderr,"%s does not exist\n", owner_show(qun,answer));
240 } else {
241 ist= adns_rr_info(answer->type, &typename, 0,0,0,0);
242 if (st == adns_s_nodata) {
243 r= fprintf(stderr,"%s has no %s record\n", owner_show(qun,answer), typename);
244 } else {
245 statusstring= adns_strerror(st);
246 r= fprintf(stderr,"Error during DNS %s lookup for %s: %s\n",
247 typename, owner_show(qun,answer), statusstring);
248 }
249 }
250 if (r == EOF) sysfail("write error message to stderr",errno);
251}
252
253void query_done(struct query_node *qun, adns_answer *answer) {
254 adns_status st, ist;
255 int rrn, nrrs;
256 const char *rrp, *realowner, *typename;
257 char *datastr;
258
259 st= answer->status;
260 nrrs= answer->nrrs;
261 if (ov_format == fmt_asynch) {
262 check_status(st);
263 if (printf("%s %d ", qun->id, nrrs) == EOF) outerr();
264 print_status(st,qun,answer);
265 } else {
266 if (qun->pqfr.show_cname && answer->cname) {
267 print_owner_ttl(qun,answer);
268 if (qun->pqfr.show_type) print_withspace("CNAME");
269 if (printf("%s\n", answer->cname) == EOF) outerr();
270 }
271 if (st) {
272 check_status(st);
273 print_dnsfail(st,qun,answer);
274 }
275 }
276 if (qun->pqfr.show_owner) {
277 realowner= answer->cname ? answer->cname : owner_show(qun,answer);
278 assert(realowner);
279 } else {
280 realowner= 0;
281 }
282 if (nrrs) {
283 for (rrn=0, rrp = answer->rrs.untyped;
284 rrn < nrrs;
285 rrn++, rrp += answer->rrsz) {
286 if (realowner) print_withspace(realowner);
287 print_ttl(qun,answer);
288 ist= adns_rr_info(answer->type, &typename, 0, 0, rrp, &datastr);
289 if (ist == adns_s_nomemory) sysfail("adns_rr_info failed",ENOMEM);
290 assert(!ist);
291 if (qun->pqfr.show_type) print_withspace(typename);
292 if (printf("%s\n",datastr) == EOF) outerr();
293 free(datastr);
294 }
295 }
296 if (fflush(stdout)) outerr();
297 free(answer);
298 dequeue_query(qun);
299}
300
301void of_asynch_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
302 free(ov_id);
304}
305
306void of_cancel_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
307 struct query_node *qun;
308
309 for (qun= outstanding.head;
310 qun && strcmp(qun->id,arg);
311 qun= qun->next);
312 if (!qun) return;
313 adns_cancel(qun->qu);
314 dequeue_query(qun);
315}
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#define ENOMEM
Definition: acclib.h:84
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * strchr(const char *String, int ch)
Definition: utclib.c:501
void usageerr(const char *fmt,...)
Definition: adh-main.c:54
int rcode
Definition: adh-main.c:36
const char * config_text
Definition: adh-main.c:37
void sysfail(const char *what, int errnoval)
Definition: adh-main.c:49
void outerr(void)
Definition: adh-main.c:64
int ov_format
Definition: adh-opts.c:39
int ov_qc_anshost
Definition: adh-opts.c:38
char * ov_id
Definition: adh-opts.c:40
int ov_qc_cname
Definition: adh-opts.c:38
adns_rrtype ov_type
Definition: adh-opts.c:37
int ov_verbose
Definition: adh-opts.c:36
struct perqueryflags_remember ov_pqfr
Definition: adh-opts.c:41
int ov_env
Definition: adh-opts.c:35
int ov_cname
Definition: adh-opts.c:39
int ov_asynch
Definition: adh-opts.c:35
int ov_qc_query
Definition: adh-opts.c:38
int ov_pipe
Definition: adh-opts.c:35
int ov_search
Definition: adh-opts.c:38
int ov_tcp
Definition: adh-opts.c:39
static void prep_query(struct query_node **qun_r, int *quflags_r)
Definition: adh-query.c:64
static void print_status(adns_status st, struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:211
static unsigned long idcounter
Definition: adh-query.c:38
static void print_dnsfail(adns_status st, struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:227
struct outstanding_list outstanding
Definition: adh-query.c:36
static const char * owner_show(struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:185
void ensure_adns_init(void)
Definition: adh-query.c:40
static void print_ttl(struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:165
void query_done(struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:253
static void print_withspace(const char *str)
Definition: adh-query.c:161
static void check_status(adns_status st)
Definition: adh-query.c:194
adns_state ads
Definition: adh-query.c:35
static void print_owner_ttl(struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:189
static void dequeue_query(struct query_node *qun)
Definition: adh-query.c:154
void query_do(const char *domain)
Definition: adh-query.c:138
@ adns_r_addr
Definition: adns.h:143
@ adns_r_none
Definition: adns.h:118
@ adns_r_ptr
Definition: adns.h:131
@ adns_r_txt
Definition: adns.h:138
ADNS_API const char * adns_errtypeabbrev(adns_status st)
Definition: general.c:311
ADNS_API int adns_submit_reverse_any(adns_state ads, const struct sockaddr *addr, const char *rzone, adns_rrtype type, adns_queryflags flags, void *context, adns_query *query_r)
Definition: query.c:271
ADNS_API const char * adns_strerror(adns_status st)
Definition: general.c:268
ADNS_API adns_status adns_rr_info(adns_rrtype type, const char **rrtname_r, const char **fmtname_r, int *len_r, const void *datap, char **data_r)
Definition: general.c:186
ADNS_API int adns_init(adns_state *newstate_r, adns_initflags flags, FILE *diagfile)
Definition: setup.c:568
adns_initflags
Definition: adns.h:87
@ adns_if_noautosys
Definition: adns.h:93
@ adns_if_noenv
Definition: adns.h:88
@ adns_if_nosigpipe
Definition: adns.h:95
adns_status
Definition: adns.h:215
@ adns_s_max_misconfig
Definition: adns.h:250
@ adns_s_max_localfail
Definition: adns.h:223
@ adns_s_ok
Definition: adns.h:216
@ adns_s_nodata
Definition: adns.h:261
@ adns_s_max_misquery
Definition: adns.h:257
@ adns_s_max_tempfail
Definition: adns.h:241
@ adns_s_max_remotefail
Definition: adns.h:232
@ adns_s_nomemory
Definition: adns.h:219
@ adns_s_nxdomain
Definition: adns.h:260
ADNS_API int adns_submit_reverse(adns_state ads, const struct sockaddr *addr, adns_rrtype type, adns_queryflags flags, void *context, adns_query *query_r)
Definition: query.c:304
ADNS_API const char * adns_errabbrev(adns_status st)
Definition: general.c:275
ADNS_API int adns_submit(adns_state ads, const char *owner, adns_rrtype type, adns_queryflags flags, void *context, adns_query *query_r)
Definition: query.c:204
ADNS_API int adns_init_strcfg(adns_state *newstate_r, adns_initflags flags, FILE *diagfile, const char *configtext)
Definition: setup.c:629
ADNS_API void adns_cancel(adns_query query)
Definition: query.c:430
@ adns_qf_quoteok_cname
Definition: adns.h:105
@ adns_qf_usevc
Definition: adns.h:102
@ adns_qf_quoteok_query
Definition: adns.h:104
@ adns_qf_quoteok_anshost
Definition: adns.h:106
@ adns_qf_search
Definition: adns.h:101
@ adns_qf_owner
Definition: adns.h:103
#define inet_aton(ap, inp)
Definition: adns_win32.h:161
@ fmt_simple
Definition: adnshost.h:75
@ fmt_asynch
Definition: adnshost.h:75
@ fmt_default
Definition: adnshost.h:75
@ fmt_inline
Definition: adnshost.h:75
@ tm_rel
Definition: adnshost.h:74
@ tm_none
Definition: adnshost.h:74
@ tm_abs
Definition: adnshost.h:74
optfunc of_reverse
Definition: adnshost.h:90
optfunc of_asynch_id
optfunc of_ptr
Definition: adnshost.h:90
optfunc of_cancel_id
Definition: adnshost.h:91
static adns_initflags initflags
Definition: adnsresfilter.c:62
static struct sockaddr_in sa
Definition: adnsresfilter.c:69
void * xmalloc(int size)
Definition: uimain.c:747
#define SIG_ERR
Definition: signal.h:52
#define SIGPIPE
Definition: signal.h:35
#define SIG_IGN
Definition: signal.h:48
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
WCHAR * xstrsave(const WCHAR *str)
Definition: query.c:544
#define assert(x)
Definition: debug.h:53
__kernel_time_t time_t
Definition: linux.h:252
#define AF_INET
Definition: tcpip.h:117
time_t now
Definition: finger.c:65
#define printf
Definition: freeldr.h:97
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLuint GLuint GLuint GLuint GLuint GLuint GLuint arg2
Definition: glext.h:9514
#define abort()
Definition: i386-dis.c:34
#define stdout
Definition: stdio.h:99
#define EOF
Definition: stdio.h:24
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
__u16 time
Definition: mkdosfs.c:8
#define sprintf(buf, format,...)
Definition: sprintf.c:55
const WCHAR * str
#define errno
Definition: errno.h:18
#define LIST_UNLINK(list, node)
Definition: dlist.h:50
#define LIST_LINK_TAIL(list, node)
Definition: dlist.h:51
int signal
Definition: except.c:84
#define memset(x, y, z)
Definition: compat.h:39
int nrrs
Definition: adns.h:316
time_t expires
Definition: adns.h:315
int rrsz
Definition: adns.h:316
union adns_answer::@4227 rrs
adns_status status
Definition: adns.h:311
adns_rrtype type
Definition: adns.h:314
char * cname
Definition: adns.h:312
char * owner
Definition: adns.h:313
void * untyped
Definition: adns.h:318
Definition: cookie.c:42
char * owner
Definition: adnshost.h:102
struct query_node * next
Definition: adnshost.h:100
char * id
Definition: adnshost.h:102
adns_query qu
Definition: adnshost.h:103
struct perqueryflags_remember pqfr
Definition: adnshost.h:101