chiark / gitweb /
Cleanups, development.
[adns.git] / src / adns.c
1 /**/
2
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <assert.h>
9 #include <unistd.h>
10
11 #include <netdb.h>
12 #include <arpa/nameser.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16
17 #include "adns-internal.h"
18
19 #define LIST_UNLINK_PART(list,node,part) \
20   do { \
21     if ((node)->back) (node)->back->part next= (node)->part next; \
22       else                        (list).head= (node)->part next; \
23     if ((node)->next) (node)->next->part back= (node)->part back; \
24       else                        (list).tail= (node)->part back; \
25   } while(0)
26
27 #define LIST_LINK_TAIL_PART(list,node,part) \
28   do { \
29     (node)->part back= 0; \
30     (node)->part next= (list).tail; \
31     if ((list).tail) (list).tail->part back= (node); else (list).part head= (node); \
32     (list).tail= (node); \
33   } while(0)
34
35 #define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
36 #define LIST_LINK_TAIL_PART(list,node) LIST_LINK_TAIL(list,node,)
37
38 static void vdebug(adns_state ads, const char *fmt, va_list al) {
39   if (!(ads->iflags & adns_if_debug)) return;
40   fputs("adns debug: ",stderr);
41   vfprintf(stderr,fmt,al);
42   fputc('\n',stderr);
43 }
44
45 static void debug(adns_state ads, const char *fmt, ...) {
46   va_list al;
47
48   va_start(al,fmt);
49   vdebug(ads,fmt,al);
50   va_end(al);
51 }
52
53 static void vdiag(adns_state ads, const char *fmt, va_list al) {
54   if (ads->iflags & adns_if_noerrprint) return;
55   fputs("adns: ",stderr);
56   vfprintf(stderr,fmt,al);
57   fputc('\n',stderr);
58 }
59
60 static void diag(adns_state ads, const char *fmt, ...) {
61   va_list al;
62
63   va_start(al,fmt);
64   vdiag(ads,fmt,al);
65   va_end(al);
66 }
67
68 static void addserver(adns_state ads, struct in_addr addr) {
69   int i;
70   struct server *ss;
71   
72   for (i=0; i<ads->nservers; i++) {
73     if (ads->servers[i].addr.s_addr == addr.s_addr) {
74       debug(ads,"duplicate nameserver %s ignored",inet_ntoa(addr));
75       return;
76     }
77   }
78   
79   if (ads->nservers>=MAXSERVERS) {
80     diag(ads,"too many nameservers, ignoring %s",inet_ntoa(addr));
81     return;
82   }
83
84   ss= ads->servers+ads->nservers;
85   ss->addr= addr;
86   ss->state= server_disc;
87   ss->connw.head= ss->connw.tail= 0;
88   ads->nservers++;
89 }
90
91 static void configparseerr(adns_state ads, const char *fn, int lno,
92                            const char *fmt, ...) {
93   va_list al;
94   
95   if (ads->iflags & adns_if_noerrprint) return;
96   if (lno==-1) fprintf(stderr,"adns: %s: ",fn);
97   else fprintf(stderr,"adns: %s:%d: ",fn,lno);
98   va_start(al,fmt);
99   vfprintf(stderr,fmt,al);
100   va_end(al);
101   fputc('\n',stderr);
102 }
103
104 static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *buf) {
105   struct in_addr ia;
106   
107   if (!inet_aton(buf,&ia)) {
108     configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf);
109     return;
110   }
111   debug(ads,"using nameserver %s",inet_ntoa(ia));
112   addserver(ads,ia);
113 }
114
115 static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) {
116   if (!buf) return;
117   diag(ads,"warning - `search' ignored FIXME");
118 }
119
120 static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) {
121   diag(ads,"warning - `sortlist' ignored FIXME");
122 }
123
124 static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) {
125   if (!buf) return;
126   diag(ads,"warning - `options' ignored FIXME");
127 }
128
129 static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) {
130   ads->nservers= 0;
131 }
132
133 static const struct configcommandinfo {
134   const char *name;
135   void (*fn)(adns_state ads, const char *fn, int lno, const char *buf);
136 } configcommandinfos[]= {
137   { "nameserver",        ccf_nameserver  },
138   { "domain",            ccf_search      },
139   { "search",            ccf_search      },
140   { "sortlist",          ccf_sortlist    },
141   { "options",           ccf_options     },
142   { "clearnameservers",  ccf_clearnss    },
143   {  0                                   }
144 };
145
146 static int ctype_whitespace(int c) { return c==' ' || c=='\n' || c=='\t'; }
147 static int ctype_digit(int c) { return c>='0' && c<='9'; }
148
149 static void readconfig(adns_state ads, const char *filename) {
150   char linebuf[2000], *p, *q;
151   FILE *file;
152   int lno, l, c;
153   const struct configcommandinfo *ccip;
154
155   file= fopen(filename,"r");
156   if (!file) {
157     if (errno == ENOENT) {
158       debug(ads,"configuration file `%s' does not exist",filename);
159       return;
160     }
161     diag(ads,"cannot open configuration file `%s': %s",filename,strerror(errno));
162     return;
163   }
164
165   for (lno=1; fgets(linebuf,sizeof(linebuf),file); lno++) {
166     l= strlen(linebuf);
167     if (!l) continue;
168     if (linebuf[l-1] != '\n' && !feof(file)) {
169       diag(ads,"%s:%d: line too long",filename,lno);
170       while ((c= getc(file)) != EOF && c != '\n') { }
171       if (c == EOF) break;
172       continue;
173     }
174     while (l>0 && ctype_whitespace(linebuf[l-1])) l--;
175     linebuf[l]= 0;
176     p= linebuf;
177     while (ctype_whitespace(*p)) p++;
178     if (*p == '#' || *p == '\n') continue;
179     q= p;
180     while (*q && !ctype_whitespace(*q)) q++;
181     for (ccip=configcommandinfos;
182          ccip->name && strncmp(ccip->name,p,q-p);
183          ccip++);
184     if (!ccip->name) {
185       diag(ads,"%s:%d: unknown configuration directive `%.*s'",filename,lno,q-p,p);
186       continue;
187     }
188     while (ctype_whitespace(*q)) q++;
189     ccip->fn(ads,filename,lno,q);
190   }
191   if (ferror(file)) {
192     diag(ads,"%s:%d: read error: %s",filename,lno,strerror(errno));
193   }
194   fclose(file);
195 }
196
197 static const char *instrum_getenv(adns_state ads, const char *envvar) {
198   const char *value;
199
200   value= getenv(envvar);
201   if (!value) debug(ads,"environment variable %s not set",envvar);
202   else debug(ads,"environment variable %s set to `%s'",envvar,value);
203   return value;
204 }
205
206 static void readconfigenv(adns_state ads, const char *envvar) {
207   const char *filename;
208
209   if (ads->iflags & adns_if_noenv) {
210     debug(ads,"not checking environment variable `%s'",envvar);
211     return;
212   }
213   filename= instrum_getenv(ads,envvar);
214   if (filename) readconfig(ads,filename);
215 }
216   
217 int adns_init(adns_state *ads_r, adns_initflags flags) {
218   adns_state ads;
219   const char *res_options, *adns_res_options;
220   struct protoent *proto;
221   int r;
222   
223   ads= malloc(sizeof(*ads)); if (!ads) return errno;
224   ads->tosend.head= ads->tosend.tail= 0;
225   ads->timew.head= ads->timew.tail= 0;
226   ads->childw.head= ads->childw.tail= 0;
227   ads->output.head= ads->output.tail= 0;
228   ads->nextid= 0x311f;
229   ads->udpsocket= -1;
230   ads->qbufavail= 0;
231   ads->qbuf= 0;
232   ads->tcpbufavail= ads->tcpbufused= ads->tcpbufdone= 0;
233   ads->tcpbuf= 0;
234   ads->iflags= flags;
235   ads->nservers= 0;
236   ads->iflags= flags;
237
238   res_options= instrum_getenv(ads,"RES_OPTIONS");
239   adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS");
240   ccf_options(ads,"RES_OPTIONS",-1,res_options);
241   ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
242
243   readconfig(ads,"/etc/resolv.conf");
244   readconfigenv(ads,"RES_CONF");
245   readconfigenv(ads,"ADNS_RES_CONF");
246
247   ccf_options(ads,"RES_OPTIONS",-1,res_options);
248   ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options);
249
250   ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN"));
251   ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN"));
252
253   if (!ads->nservers) {
254     struct in_addr ia;
255     if (ads->iflags & adns_if_debug)
256       fprintf(stderr,"adns: no nameservers, using localhost\n");
257     ia.s_addr= INADDR_LOOPBACK;
258     addserver(ads,ia);
259   }
260
261   proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
262   ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto);
263   if (!ads->udpsocket) { r= errno; goto x_closeudp; }
264   
265   *ads_r= ads;
266   return 0;
267
268  x_closeudp:
269   close(ads->udpsocket);
270  x_free:
271   free(ads);
272   return r;
273 }
274
275 static void query_fail(adns_state ads, adns_query qu, adns_status stat) {
276   adns_answer *ans;
277   
278   ans= qu->answer;
279   if (!ans) ans= malloc(sizeof(*qu->answer));
280   if (ans) {
281     ans->status= stat;
282     ans->cname= 0;
283     ans->type= qu->type;
284     ans->nrrs= 0;
285   }
286   qu->answer= ans;
287   qu->id= -1;
288   LIST_LINK_TAIL(ads->output,qu);
289 }
290
291 int adns_finish(adns_state ads) {
292   abort(); /* FIXME */
293 }
294
295 static void autosys(adns_state ads, struct timeval now) {
296   if (ads->iflags & adns_if_noautosys) return;
297   adns_callback(ads,-1,0,0,0);
298 }
299
300 void adns_cancel(adns_state ads, adns_query query) {
301   abort(); /* FIXME */
302 }
303
304 int adns_callback(adns_state ads, int maxfd,
305                   const fd_set *readfds, const fd_set *writefds,
306                   const fd_set *exceptfds) {
307   abort(); /* FIXME */
308 }
309
310 static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
311                         struct timeval maxto) {
312   struct timeval rbuf;
313
314   rbuf= *tv_io;
315   if (!rbuf) { *tvbuf= maxto; *tv_io= tvbuf; return; }
316   if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
317 }
318
319 static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
320                            struct timeval now, struct timeval maxtime) {
321   ldiv_t dr;
322   
323   maxtime.tv_sec -= (now.tv_sec-1);
324   maxtime.tv_usec += (1000-now.tv_usec);
325   dr= ldiv(maxtime.tv_usec,1000);
326   maxtime.tv_sec += dr.quot;
327   maxtime.tv_usec -= dr.rem;
328   inter_maxto(tv_io,tvbuf,maxtime);
329 }
330
331 static void localresourcerr(struct timeval **tv_io, struct timeval *tvbuf,
332                             const char *syscall) {
333   struct timeval tvto_lr;
334   
335   diag(ads,"local system resources scarce (during %s): %s",syscall,strerror(errno));
336   timerclear(&tvto_lr); timevaladd(&tvto_lr,LOCALRESOURCEMS);
337   inter_maxto(tv_io, tvbuf, tvto_lr);
338   return;
339 }
340
341 static inline void timevaladd(struct timeval *tv_io, long ms) {
342   struct timeval tmp;
343   assert(ms>=0);
344   tmp= *tv_io;
345   tmp.tv_usec += (ms%1000)*1000;
346   tmp.tv_sec += ms/1000;
347   if (tmp.tv_usec >= 1000) { tmp.tv_sec++; tmp.tv_usec -= 1000; }
348   *tv_io= tmp;
349 }    
350
351 void adns_interest(adns_state ads, int *maxfd,
352                    fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
353                    struct timeval **tv_io, struct timeval *tvbuf) {
354   struct timeval now;
355   adns_query qu;
356   int r;
357   
358   r= gettimeofday(&now,0);
359   if (r) { localresourcerr(tv_io,tvbuf,"gettimeofday"); return; }
360
361   for (qu= ads->timew; qu; qu= nqu) {
362     nqu= qu->next;
363     if (timercmp(&now,qu->timeout,>)) {
364       DLIST_UNLINK(ads->timew,qu);
365       if (qu->nextudpserver == -1) {
366         query_fail(ads,qu,adns_s_notresponding);
367       } else {
368         DLIST_LINKTAIL(ads->tosend,qu);
369       }
370     } else {
371       inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
372     }
373   }
374   
375   for (qu= ads->tosend; qu; qu= nqu) {
376     nqu= qu->next;
377     quproc_tosend(ads,qu,now);
378   }
379
380   for (qu= ads->timew; qu; qu= qu->next) {
381     if (qu->sentudp) {
382       inter_addfd(maxfd,readfds,ads->udpsocket);
383       break;
384     }
385   }
386   switch (ads->tcpstate) {
387   case server_disc:
388     break;
389   case server_connecting:
390     inter_addfd(maxfd,readfds,ads->tcpsocket);
391     inter_addfd(maxfd,writefds,ads->tcpsocket);
392     inter_addfd(maxfd,exceptfds,ads->tcpsocket);
393     break;
394   case server_connected:
395     inter_addfd(maxfd,readfds,ads->tcpsocket);
396     inter_addfd(maxfd,exceptfds,ads->tcpsocket);
397     if (ads->opbufused) inter_addfd(maxfd,writefds,ads->tcpsocket);
398   default:
399     abort();
400   }
401   
402 }
403
404 static int internal_check(adns_state ads,
405                           adns_query *query_io,
406                           adns_answer **answer,
407                           void **context_r) {
408   adns_query qu;
409
410   qu= *query_io;
411   if (!qu) {
412     if (!ads->output.head) return EWOULDBLOCK;
413     qu= ads->output.head;
414   } else {
415     if (qu->id>=0) return EWOULDBLOCK;
416   }
417   LIST_UNLINK(ads->output,qu);
418   *answer= qu->answer;
419   if (context_r) *context_r= qu->context;
420   free(qu);
421   return 0;
422 }
423
424 int adns_wait(adns_state ads,
425               adns_query *query_io,
426               adns_answer **answer_r,
427               void **context_r) {
428   int r, maxfd, rsel, rcb;
429   fd_set readfds, writefds, exceptfds;
430   struct timeval tvbuf, *tvp;
431   
432   for (;;) {
433     r= internal_check(ads,query_io,answer_r,context_r);
434     if (r && r != EWOULDBLOCK) return r;
435     FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
436     maxfd= 0; tvp= 0;
437     adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf);
438     rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
439     if (rsel==-1) return r;
440     rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds);
441     assert(rcb==rsel);
442   }
443 }
444
445 int adns_check(adns_state ads,
446                adns_query *query_io,
447                adns_answer **answer_r,
448                void **context_r) {
449   autosys(ads);
450   return internal_check(ads,query_io,answer_r,context_r);
451 }
452
453 int adns_synchronous(adns_state ads,
454                      const char *owner,
455                      adns_rrtype type,
456                      adns_queryflags flags,
457                      adns_answer **answer_r) {
458   adns_query qu;
459   int r;
460   
461   r= adns_submit(ads,owner,type,flags,0,&qu);
462   if (r) return r;
463
464   do {
465     r= adns_wait(ads,&qu,answer_r,0);
466   } while (r==EINTR);
467   if (r) adns_cancel(ads,qu);
468   return r;
469 }
470
471 static adns_status mkquery(adns_state ads, const char *owner, int ol, int id,
472                            adns_rrtype type, adns_queryflags flags, int *qml_r) {
473   int ll, c, nlabs, qbufreq;
474   unsigned char label[255], *nqbuf;
475   const char *p, *pe;
476
477 #define MKQUERY_ADDB(b) *nqbuf++= (b)
478 #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
479
480   qbufreq= 12+strlen(owner)+3;
481   if (ads->qbufavail < qbufreq) {
482     nqbuf= realloc(ads->qbuf,qbufreq);
483     if (!nqbuf) return adns_s_nolocalmem;
484     ads->qbuf= nqbuf; ads->qbufavail= qbufreq;
485   }
486   nqbuf= ads->qbuf;
487   
488   MKQUERY_ADDW(id);
489   MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */
490   MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */
491   MKQUERY_ADDW(1); /* QDCOUNT=1 */
492   MKQUERY_ADDW(0); /* ANCOUNT=0 */
493   MKQUERY_ADDW(0); /* NSCOUNT=0 */
494   MKQUERY_ADDW(0); /* ARCOUNT=0 */
495   p= owner; pe= owner+ol;
496   nlabs= 0;
497   if (!*p) return adns_s_invaliddomain;
498   do {
499     ll= 0;
500     while (p!=pe && (c= *p++)!='.') {
501       if (c=='\\') {
502         if (!(flags & adns_f_anyquote)) return adns_s_invaliddomain;
503         if (ctype_digit(p[0])) {
504           if (ctype_digit(p[1]) && ctype_digit(p[2])) {
505             c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0');
506             if (c >= 256) return adns_s_invaliddomain;
507           } else {
508             return adns_s_invaliddomain;
509           }
510         } else if (!(c= *p++)) {
511           return adns_s_invaliddomain;
512         }
513       }
514       if (!(flags & adns_f_anyquote)) {
515         if ((c >= '0' && c <= '9') || c == '-') {
516           if (!ll) return adns_s_invaliddomain;
517         } else if ((c < 'a' || c > 'z') && (c < 'A' && c > 'Z')) {
518           return adns_s_invaliddomain;
519         }
520       }
521       if (ll == sizeof(label)) return adns_s_invaliddomain;
522       label[ll++]= c;
523     }
524     if (!ll) return adns_s_invaliddomain;
525     if (nlabs++ > 63) return adns_s_invaliddomain;
526     MKQUERY_ADDB(ll);
527     memcpy(nqbuf,label,ll); nqbuf+= ll;
528   } while (p!=pe);
529
530   MKQUERY_ADDB(0);
531   MKQUERY_ADDW(type & adns__rrt_typemask); /* QTYPE */
532   MKQUERY_ADDW(1); /* QCLASS=IN */
533
534   *qml_r= nqbuf - ads->qbuf;
535   
536   return adns_s_ok;
537 }
538
539 static adns_query allocquery(adns_state ads, const char *owner, int ol,
540                              int qml, int id, adns_rrtype type,
541                              adns_queryflags flags, void *context) {
542   adns_query qu;
543   unsigned char *qm;
544   
545   qu= malloc(sizeof(*qu)+ol+1+qml); if (!qu) return 0;
546   qu->next= qu->back= qu->parent= 0;
547   qu->children.head= qu->children.tail= 0;
548   qu->siblings.next= qu->siblings.back= 0;
549   qu->id= id;
550   qu->type= type;
551   qu->answer= 0;
552   qu->flags= flags;
553   qu->context= context;
554   qu->udpretries= 0;
555   qu->sentudp= qu->senttcp= 0;
556   qu->nextserver= 0;
557   memcpy(qu->owner,owner,ol); qu->owner[ol]= 0;
558   qu->querymsg= qm= qu->owner+ol+1;
559   memcpy(qm,ads->qbuf,qml);
560   qu->querylen= qml;
561   return qu;
562 }
563
564 static int failsubmit(adns_state ads, void *context, adns_query *query_r,
565                       adns_rrtype type, adns_queryflags flags,
566                       int id, adns_status stat) {
567   adns_query qu;
568
569   qu= allocquery(ads,0,0,0,id,type,flags,context); if (!qu) return errno;
570   query_fail(ads,qu,stat);
571   *query_r= qu;
572   return 0;
573 }
574
575 static void quproc_tosend(adns_state ads, adns_query qu, struct timeval now) {
576   /* Query must be on the `tosend' queue, and guarantees to remove it. */
577   struct sockaddr_in servaddr;
578   int serv;
579
580   if (qu->nextudpserver != -1) {
581     if (qu->udpretries >= UDPMAXRETRIES) {
582       DLIST_UNLINK(ads->tosend,qu);
583       query_fail(ads,qu,adns_s_notresponding);
584       return;
585     }
586     serv= qu->nextudpserver;
587     memset(&servaddr,0,sizeof(servaddr));
588     servaddr.sin_family= AF_INET;
589     servaddr.sin_addr= ads->servers[serv].addr;
590     servaddr.sin_port= htons(53);
591     r= sendto(ads->udpsocket,qu->querymsg,qu->querylen,0,&servaddr,sizeof(servaddr));
592     if (r<0 && errno == EMSGSIZE) {
593       qu->nextudpserver= -1;
594     } else {
595       if (r<0) {
596         diag("sendto %s failed: %s",inet_ntoa(servaddr.sin_addr),strerror(errno));
597       }
598       DLIST_UNLINK(ads->tosend,qu);
599       timevaladd(&now,UDPRETRYMS);
600       qu->timeout= now;
601       qu->sentudp |= (1<<serv);
602       qu->nextudpserver= (serv+1)%ads->nservers;
603       qu->udpretries++;
604       DLIST_LINKTAIL(ads->timew,qu);
605       return;
606     }
607   }
608
609   for (;;) {
610     serv= tcpserver_get(ads);
611     if (serv<0) { r=0; break; }
612     if (ads->opbufused) { r=0; break; }
613     r= write(ads->tcpsocket,qu->querymsg,qu->querylen);
614     if (r >= 0) break;
615     if (errno == EAGAIN || errno == EINTR || errno == ENOSPC ||
616         errno == ENOBUFS || errno == ENOMEM) {
617       r= 0; break;
618     }
619     tcpserver_broken(serv);
620   }
621   if (r < qu->querylen) {
622     newopbufused= qu->opbufused + (qu->querylen-r);
623     if (newopbufused > ads->opbufavail) {
624       newopbufavail= ads->newopbufused<<1;
625       newopbuf= realloc(newopbufavail);
626       if (!newopbuf) {
627         DLIST_UNLINK(ads->tosend,qu);
628         query_fail(ads,qu,adns_s_nolocalmem);
629         return;
630       }
631       ads->opbuf= newopbuf;
632       ads->opbufavail= newopbufavail;
633     }
634     memcpy(ads->opbuf+ads->opbufused,qu->querymsg+r,qu->querylen-r);
635     ads->opbufused= newopbufused;
636   }
637   DLIST_UNLINK(ads->tosend,qu);
638   timevaladd(&now,TCPMS);
639   qu->timeout= now;
640   qu->senttcp |= (1<<qu->nextserver);
641   DLIST_LINKTAIL(ads->timew,qu);
642 }
643
644 int adns_submit(adns_state ads,
645                 const char *owner,
646                 adns_rrtype type,
647                 adns_queryflags flags,
648                 void *context,
649                 adns_query *query_r) {
650   adns_query qu;
651   adns_status stat;
652   int ol, id, qml;
653
654   id= ads->nextid++;
655
656   ol= strlen(owner);
657   if (ol<=1 || ol>MAXDNAME+1)
658     return failsubmit(ads,context,query_r,type,flags,id,adns_s_invaliddomain);
659   if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_f_search; ol--; }
660
661   stat= mkquery(ads,owner,ol,id,type,flags,&qml);
662   if (stat) return failsubmit(ads,context,query_r,type,flags,id,stat);
663
664   qu= allocquery(ads,owner,ol,qml,id,type,flags,context); if (!qu) return errno;
665   if (qu->flags & adns_f_usevc) qu->udpretries= -1;
666   LIST_LINK_TAIL(ads->tosend,qu);
667     
668   r= gettimeofday(&now,0); if (r) return;
669   quproc_tosend(ads,qu,now);
670   autosys(ads,now);
671
672   *query_r= qu;
673   return 0;
674 }