- if (qu->cnameoff<0 &&
- (qu->typei->type & adns__rrt_typemask) != adns_r_cname &&
- rrtype == adns_r_cname) { /* Ignore second and subsequent CNAMEs */
- st= adns__get_domain_perm(ads,qu,serv, dgram,dglen,
- &rdstart,rdstart+rdlength,&qu->cnameoff);
- if (st) { adns__query_fail(ads,qu,st); return; }
- if (qu->cnameoff==-1) goto x_truncated;
- /* If we find the answer section truncated after this point we restart
- * the query at the CNAME; if beforehand then we obviously have to use
- * TCP. If there is no truncation we can use the whole answer if
- * it contains the relevant info.
- */
+ if (rrtype == adns_r_cname &&
+ (qu->typei->type & adns__rrt_typemask) != adns_r_cname) {
+ if (qu->flags & adns_qf_cname_forbid) {
+ adns__query_fail(qu,adns_s_prohibitedcname);
+ return;
+ } else if (!qu->cname_dgram) { /* Ignore second and subsequent CNAMEs */
+ qu->cname_begin= rdstart;
+ qu->cname_dglen= dglen;
+ st= adns__parse_domain(ads,serv,qu, &qu->vb,
+ qu->flags & adns_qf_quoteok_cname ? pdf_quoteok : 0,
+ dgram,dglen, &rdstart,rdstart+rdlength);
+ if (!qu->vb.used) goto x_truncated;
+ if (st) { adns__query_fail(qu,st); return; }
+ l= strlen(qu->vb.buf)+1;
+ qu->answer->cname= adns__alloc_preserved(qu,l);
+ if (!qu->answer->cname) { adns__query_fail(qu,adns_s_nomemory); return; }
+
+ qu->cname_dgram= adns__alloc_mine(qu,dglen);
+ memcpy(qu->cname_dgram,dgram,dglen);
+
+ memcpy(qu->answer->cname,qu->vb.buf,l);
+ cname_here= 1;
+ adns__update_expires(qu,ttl,now);
+ /* If we find the answer section truncated after this point we restart
+ * the query at the CNAME; if beforehand then we obviously have to use
+ * TCP. If there is no truncation we can use the whole answer if
+ * it contains the relevant info.
+ */
+ } else {
+ adns__debug(ads,serv,qu,"ignoring duplicate CNAME (%s, as well as %s)",
+ adns__diag_domain(ads,serv,qu, &qu->vb, dgram,dglen,rdstart),
+ qu->answer->cname);
+ }