d1cff511 |
1 | /* |
2 | * adh-main.c |
3 | * - useful general-purpose resolver client program |
4 | * main program and useful subroutines |
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" |
d1cff511 |
30 | |
31 | void sysfail(const char *what, int errnoval) { |
32 | fprintf(stderr,"adnshost failed: %s: %s\n",what,strerror(errnoval)); |
33 | exit(10); |
34 | } |
35 | |
e3f575ff |
36 | void usageerr(const char *fmt, ...) { |
37 | va_list al; |
38 | fputs("adnshost usage error: ",stderr); |
39 | va_start(al,fmt); |
40 | vfprintf(stderr,fmt,al); |
41 | va_end(al); |
42 | putc('\n',stderr); |
43 | exit(11); |
44 | } |
45 | |
57d68ed1 |
46 | void outerr(void) { |
47 | sysfail("write to stdout",errno); |
48 | } |
49 | |
9fa14499 |
50 | static void domain_do(const char *domain) { |
51 | if (ov_pipe && !ads) usageerr("-f/--pipe not consistent with domains on command line"); |
57d68ed1 |
52 | ensure_adns_init(); |
53 | query_do(domain); |
e3f575ff |
54 | } |
55 | |
56 | void *xmalloc(size_t sz) { |
57 | void *p; |
58 | |
59 | p= malloc(sz); if (!p) sysfail("malloc",sz); |
60 | return p; |
61 | } |
62 | |
63 | char *xstrsave(const char *str) { |
64 | char *p; |
65 | |
66 | p= xmalloc(strlen(str)+1); |
67 | strcpy(p,str); |
68 | return p; |
d1cff511 |
69 | } |
70 | |
9fa14499 |
71 | void of_type(const struct optioninfo *oi, const char *arg) { |
72 | static const struct typename { |
73 | adns_rrtype type; |
74 | const char *desc; |
75 | } typenames[]= { |
76 | /* enhanced versions */ |
77 | { adns_r_ns, "ns" }, |
78 | { adns_r_soa, "soa" }, |
79 | { adns_r_ptr, "ptr" }, |
80 | { adns_r_mx, "mx" }, |
81 | { adns_r_rp, "rp" }, |
82 | { adns_r_addr, "addr" }, |
83 | |
84 | /* types with only one version */ |
85 | { adns_r_cname, "cname" }, |
86 | { adns_r_hinfo, "hinfo" }, |
87 | { adns_r_txt, "txt" }, |
88 | |
89 | /* raw versions */ |
90 | { adns_r_a, "a" }, |
91 | { adns_r_ns_raw, "ns-" }, |
92 | { adns_r_soa_raw, "soa-" }, |
93 | { adns_r_ptr_raw, "ptr-" }, |
94 | { adns_r_mx_raw, "mx-" }, |
95 | { adns_r_rp_raw, "rp-" }, |
d1cff511 |
96 | |
9fa14499 |
97 | { adns_r_none, 0 } |
98 | }; |
57d68ed1 |
99 | |
9fa14499 |
100 | const struct typename *tnp; |
57d68ed1 |
101 | |
9fa14499 |
102 | for (tnp=typenames; |
103 | tnp->type && strcmp(arg,tnp->desc); |
104 | tnp++); |
105 | if (!tnp->type) usageerr("unknown RR type %s",arg); |
106 | ov_type= tnp->type; |
107 | } |
57d68ed1 |
108 | |
9fa14499 |
109 | int rcode; |
110 | |
111 | static void process_optarg(const char *arg, |
112 | const char *const **argv_p, |
113 | const char *value) { |
e3f575ff |
114 | const struct optioninfo *oip; |
9fa14499 |
115 | int invert; |
116 | |
117 | if (arg[0] == '-' || arg[0] == '+') { |
118 | if (arg[0] == '-' && arg[1] == '-') { |
119 | if (!strncmp(arg,"--no-",5)) { |
120 | invert= 1; |
121 | oip= opt_findl(arg+5); |
122 | } else { |
123 | invert= 0; |
124 | oip= opt_findl(arg+2); |
125 | } |
126 | if (oip->type == ot_funcarg) { |
127 | arg= argv_p ? *++(*argv_p) : value; |
128 | if (!arg) usageerr("option --%s requires a value argument",oip->lopt); |
129 | } else { |
130 | if (value) usageerr("option --%s does not take a value",oip->lopt); |
131 | arg= 0; |
132 | } |
133 | opt_do(oip,arg,invert); |
134 | } else if (arg[0] == '-' && arg[1] == 0) { |
135 | arg= argv_p ? *++(*argv_p) : value; |
136 | if (!arg) usageerr("option `-' must be followed by a domain"); |
137 | domain_do(arg); |
138 | } else { /* arg[1] != '-', != '\0' */ |
139 | invert= (arg[0] == '+'); |
140 | ++arg; |
141 | while (*arg) { |
142 | oip= opt_finds(&arg); |
d1cff511 |
143 | if (oip->type == ot_funcarg) { |
9fa14499 |
144 | if (!*arg) { |
145 | arg= argv_p ? *++(*argv_p) : value; |
146 | if (!arg) usageerr("option -%s requires a value argument",oip->sopt); |
d1cff511 |
147 | } else { |
9fa14499 |
148 | if (value) usageerr("two values for option -%s given !",oip->sopt); |
d1cff511 |
149 | } |
9fa14499 |
150 | opt_do(oip,arg,invert); |
151 | arg= ""; |
152 | } else { |
153 | if (value) usageerr("option -%s does not take a value",oip->sopt); |
154 | opt_do(oip,0,invert); |
d1cff511 |
155 | } |
156 | } |
d1cff511 |
157 | } |
9fa14499 |
158 | } else { /* arg[0] != '-' */ |
159 | domain_do(arg); |
d1cff511 |
160 | } |
9fa14499 |
161 | } |
162 | |
163 | static void read_stdin(void) { |
164 | static int used, avail; |
165 | static char *buf; |
166 | |
167 | int anydone, r; |
168 | char *newline, *space; |
169 | |
170 | anydone= 0; |
171 | while (!anydone || used) { |
172 | while (!(newline= memchr(buf,'\n',used))) { |
173 | if (used == avail) { |
174 | avail += 20; avail <<= 1; |
175 | buf= realloc(buf,avail); |
176 | if (!buf) sysfail("realloc stdin buffer",errno); |
177 | } |
178 | do { |
179 | r= read(0,buf+used,avail-used); |
180 | } while (r < 0 && errno == EINTR); |
181 | if (r == 0) { |
182 | if (used) { |
183 | /* fake up final newline */ |
184 | buf[used++]= '\n'; |
185 | r= 1; |
186 | } else { |
187 | ov_pipe= 0; |
188 | return; |
189 | } |
190 | } |
191 | if (r < 0) sysfail("read stdin",errno); |
192 | used += r; |
193 | } |
194 | *newline++= 0; |
195 | space= strchr(buf,' '); |
196 | if (space) *space++= 0; |
197 | process_optarg(buf,0,space); |
198 | used -= (newline-buf); |
199 | memmove(buf,newline,used); |
200 | anydone= 1; |
201 | } |
202 | } |
203 | |
204 | int main(int argc, const char *const *argv) { |
205 | struct timeval *tv, tvbuf; |
206 | adns_query qu; |
207 | void *qun_v; |
208 | adns_answer *answer; |
209 | int r, maxfd; |
210 | fd_set readfds, writefds, exceptfds; |
211 | const char *arg; |
212 | |
213 | while ((arg= *++argv)) process_optarg(arg,&argv,0); |
57d68ed1 |
214 | |
94be415a |
215 | if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help"); |
57d68ed1 |
216 | |
9fa14499 |
217 | ensure_adns_init(); |
218 | |
57d68ed1 |
219 | for (;;) { |
220 | for (;;) { |
221 | qu= ov_asynch ? 0 : outstanding.head ? outstanding.head->qu : 0; |
222 | r= adns_check(ads,&qu,&answer,&qun_v); |
223 | if (r == EAGAIN) break; |
224 | if (r == ESRCH) { if (!ov_pipe) goto x_quit; else break; } |
225 | assert(!r); |
226 | query_done(qun_v,answer); |
227 | } |
228 | maxfd= 0; |
229 | FD_ZERO(&readfds); |
230 | FD_ZERO(&writefds); |
231 | FD_ZERO(&exceptfds); |
232 | if (ov_pipe) { |
233 | maxfd= 1; |
234 | FD_SET(0,&readfds); |
235 | } |
236 | tv= 0; |
237 | adns_beforeselect(ads, &maxfd, &readfds,&writefds,&exceptfds, &tv,&tvbuf,0); |
238 | r= select(maxfd, &readfds,&writefds,&exceptfds, tv); |
239 | if (r == -1) { |
240 | if (errno == EINTR) continue; |
241 | sysfail("select",errno); |
242 | } |
243 | adns_afterselect(ads, maxfd, &readfds,&writefds,&exceptfds, 0); |
9fa14499 |
244 | if (ov_pipe && FD_ISSET(0,&readfds)) read_stdin(); |
57d68ed1 |
245 | } |
246 | x_quit: |
247 | if (fclose(stdout)) outerr(); |
248 | exit(rcode); |
d1cff511 |
249 | } |