ReactOS  0.4.12-dev-919-gfc3b8d5
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 
38 static unsigned long idcounter;
39 
40 void 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 
60  if (ov_format == fmt_default)
62 }
63 
64 static 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=
82  (ov_search ? adns_qf_search : 0) |
83  (ov_tcp ? adns_qf_usevc : 0) |
84  ((ov_pqfr.show_owner || ov_format == fmt_simple) ? adns_qf_owner : 0) |
88  ov_cname,
89 
90  *qun_r= qun;
91 }
92 
93 void 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 
115 void 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 
138 void 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);
144  r= adns_submit(ads, domain,
146  quflags,
147  qun,
148  &qun->qu);
149  if (r) sysfail("adns_submit",r);
150 
152 }
153 
154 static void dequeue_query(struct query_node *qun) {
156  free(qun->id);
157  free(qun->owner);
158  free(qun);
159 }
160 
161 static void print_withspace(const char *str) {
162  if (printf("%s ", str) == EOF) outerr();
163 }
164 
165 static 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 
185 static const char *owner_show(struct query_node *qun, adns_answer *answer) {
186  return answer->owner ? answer->owner : qun->owner;
187 }
188 
189 static 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 
194 static 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 
211 static 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 
227 static 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 
253 void 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 
301 void of_asynch_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
302  free(ov_id);
303  ov_id= xstrsave(arg);
304 }
305 
306 void 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 }
adns_initflags
Definition: adns.h:87
void query_done(struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:253
ADNS_API const char * adns_errtypeabbrev(adns_status st)
Definition: general.c:311
adns_state ads
Definition: adh-query.c:35
adns_rrtype type
Definition: adns.h:314
int ov_cname
Definition: adh-opts.c:39
int ov_format
Definition: adh-opts.c:39
char * id
Definition: adnshost.h:102
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
void of_asynch_id(const struct optioninfo *oi, const char *arg, const char *arg2)
Definition: adh-query.c:301
int rcode
Definition: adh-main.c:36
struct query_node * next
Definition: adnshost.h:100
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
const char * config_text
Definition: adh-main.c:37
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
#define free
Definition: debug_ros.c:5
char * owner
Definition: adnshost.h:102
char * ov_id
Definition: adh-opts.c:40
static void check_status(adns_status st)
Definition: adh-query.c:194
adns_rrtype ov_type
Definition: adh-opts.c:37
#define assert(x)
Definition: debug.h:53
int ov_pipe
Definition: adh-opts.c:35
static void print_owner_ttl(struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:189
#define SIG_ERR
Definition: signal.h:52
int errno
__u16 time
Definition: mkdosfs.c:366
ADNS_API const char * adns_errabbrev(adns_status st)
Definition: general.c:275
void query_do(const char *domain)
Definition: adh-query.c:138
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
Definition: arc.h:48
WCHAR * xstrsave(const WCHAR *str)
Definition: query.c:212
FILE * stdout
static void prep_query(struct query_node **qun_r, int *quflags_r)
Definition: adh-query.c:64
GLuint GLuint GLuint GLuint GLuint GLuint GLuint arg2
Definition: glext.h:9514
#define sprintf(buf, format,...)
Definition: sprintf.c:55
int rrsz
Definition: adns.h:316
#define LIST_LINK_TAIL(list, node)
Definition: dlist.h:51
int ov_verbose
Definition: adh-opts.c:36
void * xmalloc(int size)
Definition: uimain.c:747
static unsigned long idcounter
Definition: adh-query.c:38
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
adns_status
Definition: adns.h:215
time_t now
Definition: finger.c:65
struct outstanding_list outstanding
Definition: adh-query.c:36
const WCHAR * str
#define LIST_UNLINK(list, node)
Definition: dlist.h:50
int ov_search
Definition: adh-opts.c:38
ADNS_API int adns_init_strcfg(adns_state *newstate_r, adns_initflags flags, FILE *diagfile, const char *configtext)
Definition: setup.c:629
int ov_qc_anshost
Definition: adh-opts.c:38
int ov_asynch
Definition: adh-opts.c:35
static void print_status(adns_status st, struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:211
static void print_dnsfail(adns_status st, struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:227
ADNS_API void adns_cancel(adns_query query)
Definition: query.c:430
int ov_env
Definition: adh-opts.c:35
int ov_qc_query
Definition: adh-opts.c:38
struct perqueryflags_remember ov_pqfr
Definition: adh-opts.c:41
void * untyped
Definition: adns.h:318
#define inet_aton(ap, inp)
Definition: adns_win32.h:161
void usageerr(const char *fmt,...)
Definition: adh-main.c:54
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
time_t expires
Definition: adns.h:315
void sysfail(const char *what, int errnoval)
Definition: adh-main.c:49
adns_status status
Definition: adns.h:311
#define SIGPIPE
Definition: signal.h:35
int signal
Definition: xcptfil.c:12
int ov_tcp
Definition: adh-opts.c:39
void of_ptr(const struct optioninfo *oi, const char *arg, const char *arg2)
Definition: adh-query.c:93
char * cname
Definition: adns.h:312
static const char * owner_show(struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:185
adns_query qu
Definition: adnshost.h:103
union adns_answer::@3821 rrs
int ov_qc_cname
Definition: adh-opts.c:38
void ensure_adns_init(void)
Definition: adh-query.c:40
void of_cancel_id(const struct optioninfo *oi, const char *arg, const char *arg2)
Definition: adh-query.c:306
int nrrs
Definition: adns.h:316
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
__kernel_time_t time_t
Definition: linux.h:252
ADNS_API int adns_init(adns_state *newstate_r, adns_initflags flags, FILE *diagfile)
Definition: setup.c:568
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 const char * adns_strerror(adns_status st)
Definition: general.c:268
#define abort()
Definition: i386-dis.c:35
struct perqueryflags_remember pqfr
Definition: adnshost.h:101
static adns_initflags initflags
Definition: adnsresfilter.c:62
static void dequeue_query(struct query_node *qun)
Definition: adh-query.c:154
char * strchr(const char *String, int ch)
Definition: utclib.c:501
#define EOF
Definition: stdio.h:24
static void print_withspace(const char *str)
Definition: adh-query.c:161
FILE * stderr
#define AF_INET
Definition: tcpip.h:117
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
void of_reverse(const struct optioninfo *oi, const char *arg, const char *arg2)
Definition: adh-query.c:115
#define malloc
Definition: debug_ros.c:4
static void print_ttl(struct query_node *qun, adns_answer *answer)
Definition: adh-query.c:165
void outerr(void)
Definition: adh-main.c:64
char * owner
Definition: adns.h:313
#define SIG_IGN
Definition: signal.h:48
#define memset(x, y, z)
Definition: compat.h:39
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)
static struct sockaddr_in sa
Definition: adnsresfilter.c:69
#define printf
Definition: config.h:203