int d_a_p(dns_pkt_a * dpa, char *buf, int limitlen) { int offset, rdlen; uint16_t u; int i; if ((rdlen = nametolbl(dpa->name, buf)) == -1) return -1; offset = rdlen; if (offset + 10 > limitlen) err_intret(ERR_DNSPLB); buf += offset; u = htons(dpa->type); memcpy(buf, &u, 2); buf += 2; offset += 2; u = htons(dpa->cl); memcpy(buf, &u, 2); buf += 2; offset += 2; i = htonl(dpa->ttl); memcpy(buf, &i, 4); buf += 4; offset += 4; if (dpa->type == T_A) { if (offset + dpa->rdlength > limitlen) err_intret(ERR_DNSPLB); memcpy(buf + 2, dpa->rdata, dpa->rdlength); offset += dpa->rdlength; } else if (dpa->type == T_MX) { memcpy(buf + 2, dpa->rdata, 2); if ((rdlen = nametolbl(dpa->rdata + 2, buf + 4)) == -1) { error(err_str); err_ret(ERR_DNSMDA, -1); } offset += rdlen + 2; if (offset > limitlen) err_ret(ERR_DNSPLB, -1); dpa->rdlength = rdlen + 2; } else { if ((rdlen = nametolbl(dpa->rdata, buf + 2)) == -1) { error(err_str); err_ret(ERR_DNSMDA, -1); } offset += rdlen; if (offset > limitlen) err_ret(ERR_DNSPLB, -1); dpa->rdlength = rdlen; } u = htons(dpa->rdlength); memcpy(buf, &u, 2); offset += 2; return offset; }
/* * Translate a struct dns_pkt_qst in the dns-buffer buf. * Returns: * -1 On error * Bytes writed otherwise. */ int d_qst_p(dns_pkt_qst *dpq,char *buf, int limitlen) { int offset; uint16_t u; if((offset=nametolbl(dpq->qname,buf))==-1) { error(err_str); err_ret(ERR_DNSMDA,-1); } if (offset+4>limitlen) err_ret(ERR_DNSPLB,-1); buf+=offset; u=htons(dpq->qtype); memcpy(buf,&u,2); buf+=2;offset+=2; u=htons(dpq->qclass); memcpy(buf,&u,2); buf+=2;offset+=2; return offset; }