d1cff511 |
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-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
9 | * |
10 | * It is part of adns, which is |
11 | * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
12 | * Copyright (C) 1999 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 | |
e3f575ff |
29 | #include "adnshost.h" |
30 | |
31 | adns_state ads; |
57d68ed1 |
32 | struct outstanding_list outstanding; |
e3f575ff |
33 | |
57d68ed1 |
34 | static unsigned long idcounter; |
35 | |
36 | #define STATUSTYPEMAX(v) { adns_s_max_##v, #v } |
37 | static const struct statustypemax { |
38 | adns_status smax; |
39 | const char *abbrev; |
40 | } statustypemaxes[]= { |
41 | { adns_s_ok, "ok" }, |
42 | STATUSTYPEMAX(localfail), |
43 | STATUSTYPEMAX(remotefail), |
44 | STATUSTYPEMAX(tempfail), |
45 | STATUSTYPEMAX(misconfig), |
46 | STATUSTYPEMAX(misquery), |
47 | STATUSTYPEMAX(permfail), |
d1cff511 |
48 | }; |
49 | |
57d68ed1 |
50 | void ensure_adns_init(void) { |
51 | int r; |
52 | |
53 | if (ads) return; |
d1cff511 |
54 | |
57d68ed1 |
55 | if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno); |
56 | r= adns_init(&ads, |
57 | adns_if_noautosys|adns_if_nosigpipe | |
58 | (ov_env ? 0 : adns_if_noenv) | |
59 | ov_verbose, |
60 | 0); |
61 | if (r) sysfail("adns_init",r); |
62 | } |
d1cff511 |
63 | |
57d68ed1 |
64 | void query_do(const char *domain) { |
d1cff511 |
65 | struct query_node *qun; |
66 | char idbuf[20]; |
e3f575ff |
67 | int r; |
d1cff511 |
68 | |
d1cff511 |
69 | qun= malloc(sizeof(*qun)); |
70 | qun->pqfr= ov_pqfr; |
71 | if (ov_id) { |
72 | qun->id= xstrsave(ov_id); |
73 | } else { |
74 | sprintf(idbuf,"%lu",idcounter++); |
75 | idcounter &= 0x0fffffffflu; |
76 | qun->id= xstrsave(idbuf); |
77 | } |
78 | |
e3f575ff |
79 | r= adns_submit(ads, domain, |
80 | ov_type == adns_r_none ? adns_r_addr : ov_type, |
d1cff511 |
81 | (ov_search ? adns_qf_search : 0) | |
82 | (ov_tcp ? adns_qf_usevc : 0) | |
83 | (ov_pqfr.show_owner ? adns_qf_owner : 0) | |
84 | (ov_qc_query ? adns_qf_quoteok_query : 0) | |
85 | (ov_qc_anshost ? adns_qf_quoteok_anshost : 0) | |
86 | (ov_qc_cname ? 0 : adns_qf_quoteok_cname) | |
87 | ov_cname, |
88 | qun, |
89 | &qun->qu); |
90 | if (r) sysfail("adns_submit",r); |
91 | |
e3f575ff |
92 | LIST_LINK_TAIL(outstanding,qun); |
d1cff511 |
93 | } |
e3f575ff |
94 | |
57d68ed1 |
95 | static void print_withspace(const char *str) { |
96 | if (printf("%s ", str) == EOF) outerr(); |
97 | } |
98 | |
99 | static void print_ttl(struct query_node *qun, adns_answer *answer) { |
100 | unsigned long ttl; |
101 | time_t now; |
102 | |
103 | switch (qun->pqfr.ttl) { |
104 | case tm_none: |
105 | return; |
106 | case tm_rel: |
107 | if (time(&now) == (time_t)-1) sysfail("get current time",errno); |
108 | ttl= answer->expires < now ? 0 : answer->expires - now; |
109 | break; |
110 | case tm_abs: |
111 | ttl= answer->expires; |
112 | break; |
113 | default: |
114 | abort(); |
115 | } |
116 | if (printf("%lu ",ttl) == EOF) outerr(); |
117 | } |
118 | |
119 | static void print_owner_ttl(struct query_node *qun, adns_answer *answer) { |
120 | if (qun->pqfr.show_owner) print_withspace(answer->owner); |
121 | print_ttl(qun,answer); |
122 | } |
123 | |
124 | static void print_status(adns_status st, struct query_node *qun, adns_answer *answer) { |
125 | int stnmin, stnmax, stn; |
126 | const char *statusabbrev, *statusstring; |
127 | |
128 | stnmin= 0; |
129 | stnmax= sizeof(statustypemaxes)/sizeof(statustypemaxes[0]); |
130 | while (stnmin < stnmax) { |
131 | stn= (stnmin+stnmax)>>1; |
132 | if (st > statustypemaxes[stn].smax) stnmin= stn+1; else stnmax= stn; |
133 | } |
134 | stn= stnmin; |
135 | assert(statustypemaxes[stn].smax >= st); |
136 | |
137 | if (rcode < stn) rcode= stn; |
138 | |
139 | statusabbrev= adns_errabbrev(st); |
140 | statusstring= adns_strerror(st); |
141 | assert(!strchr(statusstring,'"')); |
142 | |
143 | if (printf("%s %d %s ", statustypemaxes[stn].abbrev, st, statusabbrev) |
144 | == EOF) outerr(); |
145 | print_owner_ttl(qun,answer); |
146 | if (qun->pqfr.show_cname) |
147 | print_withspace(answer->cname ? answer->cname : "$"); |
148 | if (printf("\"%s\"\n", statusstring) == EOF) outerr(); |
149 | } |
150 | |
151 | void query_done(struct query_node *qun, adns_answer *answer) { |
152 | adns_status st, ist; |
153 | int rrn, nrrs; |
154 | const char *rrp, *realowner, *typename; |
155 | char *datastr; |
156 | |
157 | if (ov_pipe) setnonblock(1,0); |
158 | |
159 | st= answer->status; |
160 | nrrs= answer->nrrs; |
161 | if (ov_asynch) { |
0b6f56cc |
162 | if (printf("%s %d ", qun->id, nrrs) == EOF) outerr(); |
57d68ed1 |
163 | print_status(st,qun,answer); |
164 | } else { |
165 | if (st) { |
0b6f56cc |
166 | if (fputs("; failed ",stdout) == EOF) outerr(); |
57d68ed1 |
167 | print_status(st,qun,answer); |
168 | } else if (answer->cname) { |
169 | print_owner_ttl(qun,answer); |
170 | if (printf("CNAME %s\n",answer->cname) == EOF) outerr(); |
171 | } |
172 | } |
173 | if (qun->pqfr.show_owner) { |
174 | realowner= answer->cname ? answer->cname : answer->owner;; |
175 | assert(realowner); |
176 | } else { |
177 | realowner= 0; |
178 | } |
179 | if (nrrs) { |
180 | for (rrn=0, rrp = answer->rrs.untyped; |
181 | rrn < nrrs; |
182 | rrn++, rrp += answer->rrsz) { |
183 | if (realowner) print_withspace(realowner); |
184 | print_ttl(qun,answer); |
185 | ist= adns_rr_info(answer->type, &typename, 0, 0, rrp, &datastr); |
186 | if (ist == adns_s_nomemory) sysfail("adns_rr_info failed",ENOMEM); |
187 | assert(!ist); |
188 | if (qun->pqfr.show_type) print_withspace(typename); |
189 | if (printf("%s\n",datastr) == EOF) outerr(); |
190 | free(datastr); |
191 | } |
192 | } |
193 | if (fflush(stdout)) outerr(); |
194 | LIST_UNLINK(outstanding,qun); |
195 | free(answer); |
196 | free(qun->id); |
197 | free(qun); |
198 | } |
199 | |
200 | void of_asynch_id(const struct optioninfo *oi, const char *arg) { |
201 | free(ov_id); |
202 | ov_id= xstrsave(arg); |
203 | } |
204 | |
e3f575ff |
205 | void of_cancel_id(const struct optioninfo *oi, const char *arg) { abort(); } |
57d68ed1 |
206 | |