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