From patchwork Wed Oct 12 21:23:20 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernhard Kaindl X-Patchwork-Id: 119304 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id E5620B6F7F for ; Thu, 13 Oct 2011 08:23:07 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 48FB228A4B; Wed, 12 Oct 2011 23:23:06 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HCq64v1YciQl; Wed, 12 Oct 2011 23:23:06 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id DF9192895E; Wed, 12 Oct 2011 23:23:03 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A49F52895E for ; Wed, 12 Oct 2011 23:23:01 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id wZyvm8JOW7qh for ; Wed, 12 Oct 2011 23:23:00 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-ey0-f172.google.com (mail-ey0-f172.google.com [209.85.215.172]) by theia.denx.de (Postfix) with ESMTPS id A4FF12890A for ; Wed, 12 Oct 2011 23:22:59 +0200 (CEST) Received: by eyg24 with SMTP id 24so1316522eyg.3 for ; Wed, 12 Oct 2011 14:22:58 -0700 (PDT) Received: by 10.223.62.19 with SMTP id v19mr1174144fah.27.1318454578742; Wed, 12 Oct 2011 14:22:58 -0700 (PDT) Received: from localhost.localdomain ([178.190.66.149]) by mx.google.com with ESMTPS id k26sm5860437fab.12.2011.10.12.14.22.56 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 12 Oct 2011 14:22:58 -0700 (PDT) From: Bernhard Kaindl To: u-boot@lists.denx.de Date: Wed, 12 Oct 2011 23:23:20 +0200 Message-Id: <1318454600-18349-1-git-send-email-bernhard.kaindl@gmx.net> X-Mailer: git-send-email 1.6.5.2 Cc: Pieter Voorthuijsen , Bernhard Kaindl , Bernhard Kaindl , Robin Getz Subject: [U-Boot] [PATCH] net/dns.c: Fix broken endian handling in dns command X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de From: Bernhard Kaindl The U-Boot dns command only worked in little-endian CPUs so far because it was based on an antique version of the TADNS source which was using a broken macro to read the shorts found in DNS reply messages by shifting the LSB from the message into the CPU's MSB of a short int and the MSB from the stream into the LSB part of the CPU's short int. So far, so twisted. To correct the twisted bytes, the code used ntohs() as a byte-swapping function to swap the MSB back where it belongs and vice versa. This works fine, except that ntohs() naturally does nothing on big-endian CPUs. So on big-endian CPUs, the MSB from the message stayed in the LSB of the CPU and vice versa. Ditch this brain-deadness by just shifting the MSB from the network byte stream of the reply message into the right (MSB) location of a short int and putting the LSB from the network byte stream as the lower byte of it, and we are done with reading the short from the network stream for both endianesses, no ntohs() or such! The current TADNS source uses the same macro meanwhile. Signed-off-by: Bernhard Kaindl Cc: Pieter Voorthuijsen Cc: Robin Getz --- net/dns.c | 22 ++++++++++------------ 1 files changed, 10 insertions(+), 12 deletions(-) diff --git a/net/dns.c b/net/dns.c index b51d1bd..7034183 100644 --- a/net/dns.c +++ b/net/dns.c @@ -28,6 +28,9 @@ #include "dns.h" +/* p is a char *, we shift the MSB up in our CPU endianness -> no ntohs used! */ +#define netbytes2hs(p) ((p)[0] << 8 | (p)[1]) + char *NetDNSResolve; /* The host to resolve */ char *NetDNSenvvar; /* The envvar to store the answer in */ @@ -109,7 +112,6 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) int found, stop, dlen; char IPStr[22]; IPaddr_t IPAddress; - short tmp; debug("%s\n", __func__); @@ -120,14 +122,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", 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; if (ntohs(header->nqueries) != 1) return; /* Received 0 answers */ if (header->nanswers == 0) { - puts("DNS server returned no answers\n"); + puts("DNS: host not found\n"); NetState = NETLOOP_SUCCESS; return; } @@ -139,9 +141,8 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) continue; /* We sent query class 1, query type 1 */ - tmp = p[1] | (p[2] << 8); - if (&p[5] > e || ntohs(tmp) != DNS_A_RECORD) { - puts("DNS response was not A record\n"); + if (&p[5] > e || netbytes2hs(p+1) != DNS_A_RECORD) { + puts("DNS: response not an A record\n"); NetState = NETLOOP_SUCCESS; return; } @@ -160,14 +161,12 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) } debug("Name (Offset in header): %d\n", p[1]); - tmp = p[2] | (p[3] << 8); - type = ntohs(tmp); + type = netbytes2hs(p+2); debug("type = %d\n", type); if (type == DNS_CNAME_RECORD) { /* CNAME answer. shift to the next section */ debug("Found canonical name\n"); - tmp = p[10] | (p[11] << 8); - dlen = ntohs(tmp); + dlen = netbytes2hs(p+10); debug("dlen = %d\n", dlen); p += 12 + dlen; } else if (type == DNS_A_RECORD) { @@ -181,8 +180,7 @@ DnsHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) if (found && &p[12] < e) { - tmp = p[10] | (p[11] << 8); - dlen = ntohs(tmp); + dlen = netbytes2hs(p+10); p += 12; memcpy(&IPAddress, p, 4);