mirror of
https://github.com/u-boot/u-boot.git
synced 2025-05-09 03:21:51 +00:00
net/dns.c: Fix endian conversion for big-endian in dns command
net/dns.c used endian conversion macros wrongly (shorts in reply were put swapped into CPU, and then ntohs() was used to swap it back, which broke on big-endian). Fix this by using the correct linux conversion macro for reading a unaligned short in network byte order: get_unaligned_be16() Thanks to Mike Frysinger pointing at the best macro to use. Tested on big and little endian qemu boards (mips and versatile) Signed-off-by: Bernhard Kaindl <bernhard.kaindl@thalesgroup.com> Cc: Pieter Voorthuijsen <pieter.voorthuijsen@prodrive.nl> Cc: Robin Getz <rgetz@blackfin.uclinux.org> Acked-by: Mike Frysinger <vapier@gentoo.org>
This commit is contained in:
parent
f5ca20c6b6
commit
6dc809f407
1 changed files with 8 additions and 12 deletions
20
net/dns.c
20
net/dns.c
|
@ -25,6 +25,7 @@
|
||||||
#include <common.h>
|
#include <common.h>
|
||||||
#include <command.h>
|
#include <command.h>
|
||||||
#include <net.h>
|
#include <net.h>
|
||||||
|
#include <asm/unaligned.h>
|
||||||
|
|
||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
|
|
||||||
|
@ -109,7 +110,6 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
|
||||||
int found, stop, dlen;
|
int found, stop, dlen;
|
||||||
char IPStr[22];
|
char IPStr[22];
|
||||||
IPaddr_t IPAddress;
|
IPaddr_t IPAddress;
|
||||||
short tmp;
|
|
||||||
|
|
||||||
|
|
||||||
debug("%s\n", __func__);
|
debug("%s\n", __func__);
|
||||||
|
@ -120,14 +120,14 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
|
||||||
debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
|
debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
|
||||||
pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
|
pkt+i, pkt[i], pkt[i+1], pkt[i+2], pkt[i+3]);
|
||||||
|
|
||||||
/* We sent 1 query. We want to see more that 1 answer. */
|
/* We sent one query. We want to have a single answer: */
|
||||||
header = (struct header *) pkt;
|
header = (struct header *) pkt;
|
||||||
if (ntohs(header->nqueries) != 1)
|
if (ntohs(header->nqueries) != 1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Received 0 answers */
|
/* Received 0 answers */
|
||||||
if (header->nanswers == 0) {
|
if (header->nanswers == 0) {
|
||||||
puts("DNS server returned no answers\n");
|
puts("DNS: host not found\n");
|
||||||
NetState = NETLOOP_SUCCESS;
|
NetState = NETLOOP_SUCCESS;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -139,9 +139,8 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* We sent query class 1, query type 1 */
|
/* We sent query class 1, query type 1 */
|
||||||
tmp = p[1] | (p[2] << 8);
|
if (&p[5] > e || get_unaligned_be16(p+1) != DNS_A_RECORD) {
|
||||||
if (&p[5] > e || ntohs(tmp) != DNS_A_RECORD) {
|
puts("DNS: response was not an A record\n");
|
||||||
puts("DNS response was not A record\n");
|
|
||||||
NetState = NETLOOP_SUCCESS;
|
NetState = NETLOOP_SUCCESS;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -160,14 +159,12 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
|
||||||
}
|
}
|
||||||
debug("Name (Offset in header): %d\n", p[1]);
|
debug("Name (Offset in header): %d\n", p[1]);
|
||||||
|
|
||||||
tmp = p[2] | (p[3] << 8);
|
type = get_unaligned_be16(p+2);
|
||||||
type = ntohs(tmp);
|
|
||||||
debug("type = %d\n", type);
|
debug("type = %d\n", type);
|
||||||
if (type == DNS_CNAME_RECORD) {
|
if (type == DNS_CNAME_RECORD) {
|
||||||
/* CNAME answer. shift to the next section */
|
/* CNAME answer. shift to the next section */
|
||||||
debug("Found canonical name\n");
|
debug("Found canonical name\n");
|
||||||
tmp = p[10] | (p[11] << 8);
|
dlen = get_unaligned_be16(p+10);
|
||||||
dlen = ntohs(tmp);
|
|
||||||
debug("dlen = %d\n", dlen);
|
debug("dlen = %d\n", dlen);
|
||||||
p += 12 + dlen;
|
p += 12 + dlen;
|
||||||
} else if (type == DNS_A_RECORD) {
|
} else if (type == DNS_A_RECORD) {
|
||||||
|
@ -181,8 +178,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len)
|
||||||
|
|
||||||
if (found && &p[12] < e) {
|
if (found && &p[12] < e) {
|
||||||
|
|
||||||
tmp = p[10] | (p[11] << 8);
|
dlen = get_unaligned_be16(p+10);
|
||||||
dlen = ntohs(tmp);
|
|
||||||
p += 12;
|
p += 12;
|
||||||
memcpy(&IPAddress, p, 4);
|
memcpy(&IPAddress, p, 4);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue