- MKQUERY_ADDW(id);
- MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */
- MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */
- MKQUERY_ADDW(1); /* QDCOUNT=1 */
- MKQUERY_ADDW(0); /* ANCOUNT=0 */
- MKQUERY_ADDW(0); /* NSCOUNT=0 */
- MKQUERY_ADDW(0); /* ARCOUNT=0 */
- p= owner; pe= owner+ol;
- nlabs= 0;
- if (!*p) return adns_s_invaliddomain;
- do {
- ll= 0;
- while (p!=pe && (c= *p++)!='.') {
- if (c=='\\') {
- if (!(flags & adns_f_anyquote)) return adns_s_invaliddomain;
- if (ctype_digit(p[0])) {
- if (ctype_digit(p[1]) && ctype_digit(p[2])) {
- c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0');
- if (c >= 256) return adns_s_invaliddomain;
- } else {
- return adns_s_invaliddomain;
- }
- } else if (!(c= *p++)) {
- return adns_s_invaliddomain;
- }
- }
- if (!(flags & adns_f_anyquote)) {
- if (ctype_digit(c) || c == '-') {
- if (!ll) return adns_s_invaliddomain;
- } else if ((c < 'a' || c > 'z') && (c < 'A' && c > 'Z')) {
- return adns_s_invaliddomain;
- }
- }
- if (ll == sizeof(label)) return adns_s_invaliddomain;
- label[ll++]= c;
+ qu= malloc(sizeof(*qu)); if (!qu) return 0;
+ qu->answer= malloc(sizeof(*qu->answer));
+ if (!qu->answer) { free(qu); return 0; }
+
+ qu->ads= ads;
+ qu->state= query_tosend;
+ qu->back= qu->next= qu->parent= 0;
+ LIST_INIT(qu->children);
+ LINK_INIT(qu->siblings);
+ LIST_INIT(qu->allocations);
+ qu->interim_allocd= 0;
+ qu->preserved_allocd= 0;
+ qu->final_allocspace= 0;
+
+ qu->typei= typei;
+ qu->query_dgram= 0;
+ qu->query_dglen= 0;
+ adns__vbuf_init(&qu->vb);
+
+ qu->cname_dgram= 0;
+ qu->cname_dglen= qu->cname_begin= 0;
+
+ adns__vbuf_init(&qu->search_vb);
+ qu->search_origlen= qu->search_pos= qu->search_doneabs= 0;
+
+ qu->id= -2; /* will be overwritten with real id before we leave adns */
+ qu->flags= flags;
+ qu->retries= 0;
+ qu->udpnextserver= 0;
+ qu->udpsent= 0;
+ timerclear(&qu->timeout);
+ qu->expires= now.tv_sec + MAXTTLBELIEVE;
+
+ memset(&qu->ctx,0,sizeof(qu->ctx));
+
+ qu->answer->status= adns_s_ok;
+ qu->answer->cname= qu->answer->owner= 0;
+ qu->answer->type= type;
+ qu->answer->expires= -1;
+ qu->answer->nrrs= 0;
+ qu->answer->rrs.untyped= 0;
+ qu->answer->rrsz= typei->getrrsz(typei,type);
+
+ return qu;
+}
+
+static void query_submit(adns_state ads, adns_query qu,
+ const typeinfo *typei, vbuf *qumsg_vb, int id,
+ adns_queryflags flags, struct timeval now) {
+ /* Fills in the query message in for a previously-allocated query,
+ * and submits it. Cannot fail. Takes over the memory for qumsg_vb.
+ */
+
+ qu->vb= *qumsg_vb;
+ adns__vbuf_init(qumsg_vb);
+
+ qu->query_dgram= malloc(qu->vb.used);
+ if (!qu->query_dgram) { adns__query_fail(qu,adns_s_nomemory); return; }
+
+ qu->id= id;
+ qu->query_dglen= qu->vb.used;
+ memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used);
+
+ adns__query_send(qu,now);
+}
+
+adns_status adns__ckl_hostname(adns_state ads, adns_queryflags flags,
+ union checklabel_state *cls,
+ qcontext *ctx, int labnum,
+ const char *label, int lablen)
+{
+ int i, c;
+
+ if (flags & adns_qf_quoteok_query) return adns_s_ok;
+ for (i=0; i<lablen; i++) {
+ c= label[i];
+ if (c == '-') {
+ if (!i) return adns_s_querydomaininvalid;
+ } else if (!ctype_alpha(c) && !ctype_digit(c)) {
+ return adns_s_querydomaininvalid;