packaging: Switching on -fcommon for Adress Sanitizer
[platform/upstream/adns.git] / 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 part of adns, which is
8  *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
9  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
10  *    Copyright (C) 1991 Massachusetts Institute of Technology
11  *  (See the file INSTALL for full details.)
12  *  
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; either version 2, or (at your option)
16  *  any later version.
17  *  
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *  
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software Foundation,
25  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
26  */
27
28 #include "adnshost.h"
29
30 int rcode;
31 const char *config_text;
32
33 static int used, avail;
34 static char *buf;
35
36 void quitnow(int rc) {
37   if (ads) adns_finish(ads);
38   free(buf);
39   free(ov_id);
40   exit(rc);
41 }
42
43 void sysfail(const char *what, int errnoval) {
44   fprintf(stderr,"adnshost failed: %s: %s\n",what,strerror(errnoval));
45   quitnow(10);
46 }
47
48 void usageerr(const char *fmt, ...) {
49   va_list al;
50   fputs("adnshost usage error: ",stderr);
51   va_start(al,fmt);
52   vfprintf(stderr,fmt,al);
53   va_end(al);
54   putc('\n',stderr);
55   quitnow(11);
56 }
57
58 void outerr(void) {
59   sysfail("write to stdout",errno);
60 }
61
62 void *xmalloc(size_t sz) {
63   void *p;
64
65   p= malloc(sz); if (!p) sysfail("malloc",sz);
66   return p;
67 }
68
69 char *xstrsave(const char *str) {
70   char *p;
71   
72   p= xmalloc(strlen(str)+1);
73   strcpy(p,str);
74   return p;
75 }
76
77 void of_config(const struct optioninfo *oi, const char *arg, const char *arg2) {
78   config_text= arg;
79 }
80
81 void of_type(const struct optioninfo *oi, const char *arg, const char *arg2) {
82   static const struct typename {
83     adns_rrtype type;
84     const char *desc;
85   } typenames[]= {
86     /* enhanced versions */
87     { adns_r_ns,     "ns"     },
88     { adns_r_soa,    "soa"    },
89     { adns_r_ptr,    "ptr"    },
90     { adns_r_mx,     "mx"     },
91     { adns_r_rp,     "rp"     },
92     { adns_r_srv,    "srv"    },
93     { adns_r_addr,   "addr"   },
94     { adns_r_srv,    "srv"    },
95     
96     /* types with only one version */
97     { adns_r_cname,  "cname"  },
98     { adns_r_hinfo,  "hinfo"  },
99     { adns_r_txt,    "txt"    },
100     
101     /* raw versions */
102     { adns_r_a,        "a"    },
103     { adns_r_aaaa,     "aaaa" },
104     { adns_r_ns_raw,   "ns-"  },
105     { adns_r_soa_raw,  "soa-" },
106     { adns_r_ptr_raw,  "ptr-" },
107     { adns_r_mx_raw,   "mx-"  },
108     { adns_r_rp_raw,   "rp-"  },
109     { adns_r_srv_raw,  "srv-" },
110
111     { adns_r_none, 0 }
112   };
113
114   const struct typename *tnp;
115   unsigned long unknowntype;
116   char *ep;
117
118   if (strlen(arg) > 4 && !memcmp(arg,"type",4) &&
119       (unknowntype= strtoul(arg+4, &ep, 10), !*ep) && unknowntype < 65536) {
120     ov_type= unknowntype | adns_r_unknown;
121     return;
122   }
123
124   for (tnp=typenames;
125        tnp->type && strcmp(arg,tnp->desc);
126        tnp++);
127   if (!tnp->type) usageerr("unknown RR type %s",arg);
128   ov_type= tnp->type;
129 }
130
131 static void process_optarg(const char *arg,
132                            const char *const **argv_p,
133                            const char *value) {
134   const struct optioninfo *oip;
135   const char *arg2;
136   int invert;
137
138   if (arg[0] == '-' || arg[0] == '+') {
139     if (arg[0] == '-' && arg[1] == '-') {
140       if (!strncmp(arg,"--no-",5)) {
141         invert= 1;
142         oip= opt_findl(arg+5);
143       } else {
144         invert= 0;
145         oip= opt_findl(arg+2);
146       }
147       if (oip->type == ot_funcarg) {
148         arg= argv_p ? *++(*argv_p) : value;
149         if (!arg) usageerr("option --%s requires a value argument",oip->lopt);
150         arg2= 0;
151       } else if (oip->type == ot_funcarg2) {
152         assert(argv_p);
153         arg= *++(*argv_p);
154         arg2= arg ? *++(*argv_p) : 0;
155         if (!arg || !arg2)
156           usageerr("option --%s requires two more arguments", oip->lopt);
157       } else {
158         if (value) usageerr("option --%s does not take a value",oip->lopt);
159         arg= 0;
160         arg2= 0;
161       }
162       opt_do(oip,invert,arg,arg2);
163     } else if (arg[0] == '-' && arg[1] == 0) {
164       arg= argv_p ? *++(*argv_p) : value;
165       if (!arg) usageerr("option `-' must be followed by a domain");
166       query_do(arg);
167     } else { /* arg[1] != '-', != '\0' */
168       invert= (arg[0] == '+');
169       ++arg;
170       while (*arg) {
171         oip= opt_finds(&arg);
172         if (oip->type == ot_funcarg) {
173           if (!*arg) {
174             arg= argv_p ? *++(*argv_p) : value;
175             if (!arg) usageerr("option -%s requires a value argument",oip->sopt);
176           } else {
177             if (value) usageerr("two values for option -%s given !",oip->sopt);
178           }
179           opt_do(oip,invert,arg,0);
180           arg= "";
181         } else {
182           if (value) usageerr("option -%s does not take a value",oip->sopt);
183           opt_do(oip,invert,0,0);
184         }
185       }
186     }
187   } else { /* arg[0] != '-' */
188     query_do(arg);
189   }
190 }
191     
192 static void read_stdin(void) {
193   int anydone, r;
194   char *newline, *space;
195
196   anydone= 0;
197   while (!anydone || used) {
198     while (!(newline= memchr(buf,'\n',used))) {
199       if (used == avail) {
200         avail += 20; avail <<= 1;
201         buf= realloc(buf,avail);
202         if (!buf) sysfail("realloc stdin buffer",errno);
203       }
204       do {
205         r= read(0,buf+used,avail-used);
206       } while (r < 0 && errno == EINTR);
207       if (r == 0) {
208         if (used) {
209           /* fake up final newline */
210           buf[used++]= '\n';
211           r= 1;
212         } else {
213           ov_pipe= 0;
214           return;
215         }
216       }
217       if (r < 0) sysfail("read stdin",errno);
218       used += r;
219     }
220     *newline++= 0;
221     space= strchr(buf,' ');
222     if (space) *space++= 0;
223     process_optarg(buf,0,space);
224     used -= (newline-buf);
225     memmove(buf,newline,used);
226     anydone= 1;
227   }
228 }
229
230 int main(int argc, const char *const *argv) {
231   struct timeval *tv, tvbuf;
232   adns_query qu;
233   void *qun_v;
234   adns_answer *answer;
235   int r, maxfd;
236   fd_set readfds, writefds, exceptfds;
237   const char *arg;
238   
239   while ((arg= *++argv)) process_optarg(arg,&argv,0);
240
241   if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help");
242
243   ensure_adns_init();
244
245   for (;;) {
246     for (;;) {
247       qu= ov_asynch ? 0 : outstanding.head ? outstanding.head->qu : 0;
248       r= adns_check(ads,&qu,&answer,&qun_v);
249       if (r == EAGAIN) break;
250       if (r == ESRCH) { if (!ov_pipe) goto x_quit; else break; }
251       assert(!r);
252       query_done(qun_v,answer);
253     }
254     maxfd= 0;
255     FD_ZERO(&readfds);
256     FD_ZERO(&writefds);
257     FD_ZERO(&exceptfds);
258     if (ov_pipe) {
259       maxfd= 1;
260       FD_SET(0,&readfds);
261     }
262     tv= 0;
263     adns_beforeselect(ads, &maxfd, &readfds,&writefds,&exceptfds, &tv,&tvbuf,0);
264     r= select(maxfd, &readfds,&writefds,&exceptfds, tv);
265     if (r == -1) {
266       if (errno == EINTR) continue;
267       sysfail("select",errno);
268     }
269     adns_afterselect(ads, maxfd, &readfds,&writefds,&exceptfds, 0);
270     if (ov_pipe && FD_ISSET(0,&readfds)) read_stdin();
271   }
272 x_quit:
273   if (fclose(stdout)) outerr();
274   quitnow(rcode);
275 }