BUG/MEDIUM: dns: fix alignment issue when building DNS queries

On some architectures, unaligned access is not authorized. On most
architectures, it is just slower. Therefore, we have to use memcpy()
when an unaligned access is needed, specifically when writing the qinfo.

Also remove the unaligned access when reading answer count when reading
the answer. It's likely that this instruction was optimized away by the
compiler since it is unneeded. Add a comment to explain why we use 7 as
an offset instead of 6. Not an unaligned offset since "resp" is
"unsigned char", then promoted to int.
This commit is contained in:
Vincent Bernat 2016-04-08 22:17:45 +02:00 committed by Willy Tarreau
parent e2f8497716
commit 9b7125cde9

View File

@ -619,8 +619,7 @@ int dns_get_ip_from_response(unsigned char *resp, unsigned char *resp_end,
cname = *newip = newip4 = newip6 = NULL;
cnamelen = currentip_found = 0;
*newip_sin_family = AF_UNSPEC;
ancount = (((struct dns_header *)resp)->ancount);
ancount = *(resp + 7);
ancount = *(resp + 7); /* Assume no more than 256 answers */
/* bypass DNS response header */
reader = resp + sizeof(struct dns_header);
@ -975,7 +974,7 @@ int dns_init_resolvers(void)
int dns_build_query(int query_id, int query_type, char *hostname_dn, int hostname_dn_len, char *buf, int bufsize)
{
struct dns_header *dns;
struct dns_question *qinfo;
struct dns_question qinfo;
char *ptr, *bufend;
memset(buf, '\0', bufsize);
@ -1021,9 +1020,9 @@ int dns_build_query(int query_id, int query_type, char *hostname_dn, int hostnam
return -1;
/* set up query info (type and class) */
qinfo = (struct dns_question *)ptr;
qinfo->qtype = htons(query_type);
qinfo->qclass = htons(DNS_RCLASS_IN);
qinfo.qtype = htons(query_type);
qinfo.qclass = htons(DNS_RCLASS_IN);
memcpy(ptr, &qinfo, sizeof(qinfo));
ptr += sizeof(struct dns_question);