lwip-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [lwip-devel] byte order, packing, optimizations


From: Stéphane Lesage
Subject: RE: [lwip-devel] byte order, packing, optimizations
Date: Sun, 14 Feb 2010 17:07:29 +0100
User-agent: Thunderbird 2.0.0.23 (Windows/20090812)



> Could you explain that? I'd like to make sure it's OK before
> you actually do the work to prepare a patch.
> > - other macros for reading/writing unaligned words for such
> protocols
> > > Hmm, I'm not sure I understand you correctly, but we already
> discussed such macros (for processors/compilers that can't do
> struct packing) and decided we don't want to support this...

I've already did the work. So here's the patch.

get_ns/get_nl = load&convert from a lwip packed struct
set_ns/set_nl = convert&store to a lwip packed struct

for people who want to optimize on little endian plaforms,
it's more efficient to combine both operations.

unpack_ns/unpack_nl/pack_ns/pack_nl:
same but for complety unaligned protocols such as DNS queries/answers after hostname.

> > I removed:
> > - the broadcast with 0.0.0.0 "old style comment", is it
> really used ?
> It is! DHCP/Bootp is allowed to use this.

ok, then what about this:

#define ip_addr_isbroadcast(addr1, netif) ((addr1)->addr == IPADDR_BROADCAST || \
                                          (addr1)->addr == 0 || \
((netif->flags & NETIF_FLAG_BROADCAST) && \ (addr1)->addr == (netif->ip_addr.addr|(~netif->netmask.addr)) ) )

Index: core/dhcp.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/dhcp.c,v
retrieving revision 1.122
diff -u -r1.122 dhcp.c
--- core/dhcp.c 14 Feb 2010 12:42:49 -0000      1.122
+++ core/dhcp.c 14 Feb 2010 15:27:16 -0000
@@ -1548,9 +1548,9 @@
     }
   }
   /* match transaction ID against what we expected */
-  if (ntohl(reply_msg->xid) != dhcp->xid) {
+  if (get_nl(reply_msg->xid) != dhcp->xid) {
     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
-      ("transaction id mismatch 
reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n",ntohl(reply_msg->xid),dhcp->xid));
+      ("transaction id mismatch 
reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n",get_nl(reply_msg->xid),dhcp->xid));
     goto free_pbuf_and_return;
   }
   /* option fields could be unfold? */
@@ -1658,7 +1658,7 @@
   dhcp->msg_out->htype = DHCP_HTYPE_ETH;
   dhcp->msg_out->hlen = netif->hwaddr_len;
   dhcp->msg_out->hops = 0;
-  dhcp->msg_out->xid = htonl(dhcp->xid);
+  set_nl(dhcp->msg_out->xid, dhcp->xid);
   dhcp->msg_out->secs = 0;
   /* we don't need the broadcast flag since we can receive unicast traffic
      before being fully configured! */
Index: core/dns.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/dns.c,v
retrieving revision 1.40
diff -u -r1.40 dns.c
--- core/dns.c  14 Feb 2010 14:02:05 -0000      1.40
+++ core/dns.c  14 Feb 2010 15:27:31 -0000
@@ -565,7 +565,6 @@
 {
   err_t err;
   struct dns_hdr *hdr;
-  struct dns_query qry;
   struct pbuf *p;
   char *query, *nptr;
   const char *pHostname;
@@ -584,7 +583,7 @@
     /* fill dns header */
     hdr = (struct dns_hdr*)p->payload;
     memset(hdr, 0, SIZEOF_DNS_HDR);
-    hdr->id = htons(id);
+    set_ns(hdr->id, id);
     hdr->flags1 = DNS_FLAG1_RD;
     hdr->numquestions = htons(1);
     query = (char*)hdr + SIZEOF_DNS_HDR;
@@ -606,9 +605,8 @@
     *query++='\0';
 
     /* fill dns query */
-    qry.type = htons(DNS_RRTYPE_A);
-    qry.cls = htons(DNS_RRCLASS_IN);
-    SMEMCPY(query, &qry, SIZEOF_DNS_QUERY);
+    pack_ns(query, DNS_RRTYPE_A);
+    pack_ns(query+2, DNS_RRCLASS_IN);
 
     /* resize pbuf to the exact dns query */
     pbuf_realloc(p, (u16_t)((query + SIZEOF_DNS_QUERY) - 
((char*)(p->payload))));
@@ -772,7 +770,7 @@
   if (pbuf_copy_partial(p, dns_payload, p->tot_len, 0) == p->tot_len) {
     /* The ID in the DNS header should be our entry into the name table. */
     hdr = (struct dns_hdr*)dns_payload;
-    i = htons(hdr->id);
+    i = get_ns(hdr->id);
     if (i < DNS_TABLE_SIZE) {
       pEntry = &dns_table[i];
       if(pEntry->state == DNS_STATE_ASKING) {
@@ -782,8 +780,8 @@
 
         /* We only care about the question(s) and the answers. The authrr
            and the extrarr are simply discarded. */
-        nquestions = htons(hdr->numquestions);
-        nanswers   = htons(hdr->numanswers);
+        nquestions = get_ns(hdr->numquestions);
+        nanswers   = get_ns(hdr->numanswers);
 
         /* Check for error. If so, call callback to inform. */
         if (((hdr->flags1 & DNS_FLAG1_RESPONSE) == 0) || (pEntry->err != 0) || 
(nquestions != 1)) {
Index: core/tcp.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/tcp.c,v
retrieving revision 1.117
diff -u -r1.117 tcp.c
--- core/tcp.c  14 Feb 2010 12:42:49 -0000      1.117
+++ core/tcp.c  14 Feb 2010 15:27:49 -0000
@@ -1352,13 +1352,13 @@
   LWIP_DEBUGF(TCP_DEBUG, ("TCP header:\n"));
   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
   LWIP_DEBUGF(TCP_DEBUG, ("|    %5"U16_F"      |    %5"U16_F"      | (src 
port, dest port)\n",
-         ntohs(tcphdr->src), ntohs(tcphdr->dest)));
+         get_ns(tcphdr->src), get_ns(tcphdr->dest)));
   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
   LWIP_DEBUGF(TCP_DEBUG, ("|           %010"U32_F"          | (seq no)\n",
-          ntohl(tcphdr->seqno)));
+         get_nl(tcphdr->seqno)));
   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
   LWIP_DEBUGF(TCP_DEBUG, ("|           %010"U32_F"          | (ack no)\n",
-         ntohl(tcphdr->ackno)));
+         get_nl(tcphdr->ackno)));
   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
   LWIP_DEBUGF(TCP_DEBUG, ("| %2"U16_F" |   
|%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"%"U16_F"|     %5"U16_F"     | (hdrlen, 
flags (",
        TCPH_HDRLEN(tcphdr),
@@ -1368,12 +1368,12 @@
          TCPH_FLAGS(tcphdr) >> 2 & 1,
          TCPH_FLAGS(tcphdr) >> 1 & 1,
          TCPH_FLAGS(tcphdr) & 1,
-         ntohs(tcphdr->wnd)));
+         get_ns(tcphdr->wnd)));
   tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
   LWIP_DEBUGF(TCP_DEBUG, ("), win)\n"));
   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
   LWIP_DEBUGF(TCP_DEBUG, ("|    0x%04"X16_F"     |     %5"U16_F"     | 
(chksum, urgp)\n",
-         ntohs(tcphdr->chksum), ntohs(tcphdr->urgp)));
+         get_ns(tcphdr->chksum), get_ns(tcphdr->urgp)));
   LWIP_DEBUGF(TCP_DEBUG, ("+-------------------------------+\n"));
 }
 
Index: core/tcp_in.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/tcp_in.c,v
retrieving revision 1.135
diff -u -r1.135 tcp_in.c
--- core/tcp_in.c       14 Feb 2010 12:42:49 -0000      1.135
+++ core/tcp_in.c       14 Feb 2010 15:28:28 -0000
@@ -161,11 +161,11 @@
   }
 
   /* Convert fields in TCP header to host byte order. */
-  tcphdr->src = ntohs(tcphdr->src);
-  tcphdr->dest = ntohs(tcphdr->dest);
-  seqno = tcphdr->seqno = ntohl(tcphdr->seqno);
-  ackno = tcphdr->ackno = ntohl(tcphdr->ackno);
-  tcphdr->wnd = ntohs(tcphdr->wnd);
+  tcphdr->src = get_ns(tcphdr->src);
+  tcphdr->dest = get_ns(tcphdr->dest);
+  seqno = tcphdr->seqno = get_nl(tcphdr->seqno);
+  ackno = tcphdr->ackno = get_nl(tcphdr->ackno);
+  tcphdr->wnd = get_ns(tcphdr->wnd);
 
   flags = TCPH_FLAGS(tcphdr);
   tcplen = p->tot_len + ((flags & (TCP_FIN | TCP_SYN)) ? 1 : 0);
@@ -586,10 +586,10 @@
   switch (pcb->state) {
   case SYN_SENT:
     LWIP_DEBUGF(TCP_INPUT_DEBUG, ("SYN-SENT: ackno %"U32_F" pcb->snd_nxt 
%"U32_F" unacked %"U32_F"\n", ackno,
-     pcb->snd_nxt, ntohl(pcb->unacked->tcphdr->seqno)));
+     pcb->snd_nxt, get_nl(pcb->unacked->tcphdr->seqno)));
     /* received SYN ACK with expected sequence number? */
     if ((flags & TCP_ACK) && (flags & TCP_SYN)
-        && ackno == ntohl(pcb->unacked->tcphdr->seqno) + 1) {
+        && ackno == get_nl(pcb->unacked->tcphdr->seqno) + 1) {
       pcb->snd_buf++;
       pcb->rcv_nxt = seqno + 1;
       pcb->rcv_ann_right_edge = pcb->rcv_nxt;
@@ -941,18 +941,18 @@
       LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: ACK for %"U32_F", 
unacked->seqno %"U32_F":%"U32_F"\n",
                                     ackno,
                                     pcb->unacked != NULL?
-                                    ntohl(pcb->unacked->tcphdr->seqno): 0,
+                                    get_nl(pcb->unacked->tcphdr->seqno): 0,
                                     pcb->unacked != NULL?
-                                    ntohl(pcb->unacked->tcphdr->seqno) + 
TCP_TCPLEN(pcb->unacked): 0));
+                                    get_nl(pcb->unacked->tcphdr->seqno) + 
TCP_TCPLEN(pcb->unacked): 0));
 
       /* Remove segment from the unacknowledged list if the incoming
          ACK acknowlegdes them. */
       while (pcb->unacked != NULL &&
-             TCP_SEQ_LEQ(ntohl(pcb->unacked->tcphdr->seqno) +
+             TCP_SEQ_LEQ(get_nl(pcb->unacked->tcphdr->seqno) +
                          TCP_TCPLEN(pcb->unacked), ackno)) {
         LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %"U32_F":%"U32_F" 
from pcb->unacked\n",
-                                      ntohl(pcb->unacked->tcphdr->seqno),
-                                      ntohl(pcb->unacked->tcphdr->seqno) +
+                                      get_nl(pcb->unacked->tcphdr->seqno),
+                                      get_nl(pcb->unacked->tcphdr->seqno) +
                                       TCP_TCPLEN(pcb->unacked)));
 
         next = pcb->unacked;
@@ -995,10 +995,10 @@
        ->unsent list after a retransmission, so these segments may
        in fact have been sent once. */
     while (pcb->unsent != NULL &&
-           TCP_SEQ_BETWEEN(ackno, ntohl(pcb->unsent->tcphdr->seqno) + 
+           TCP_SEQ_BETWEEN(ackno, get_nl(pcb->unsent->tcphdr->seqno) + 
                            TCP_TCPLEN(pcb->unsent), pcb->snd_nxt)) {
       LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %"U32_F":%"U32_F" 
from pcb->unsent\n",
-                                    ntohl(pcb->unsent->tcphdr->seqno), 
ntohl(pcb->unsent->tcphdr->seqno) +
+                                    get_nl(pcb->unsent->tcphdr->seqno), 
get_nl(pcb->unsent->tcphdr->seqno) +
                                     TCP_TCPLEN(pcb->unsent)));
 
       next = pcb->unsent;
Index: core/tcp_out.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/tcp_out.c,v
retrieving revision 1.97
diff -u -r1.97 tcp_out.c
--- core/tcp_out.c      14 Feb 2010 12:42:49 -0000      1.97
+++ core/tcp_out.c      14 Feb 2010 15:29:24 -0000
@@ -63,12 +63,12 @@
                       u32_t seqno_be /* already in network byte order */)
 {
   struct tcp_hdr *tcphdr = (struct tcp_hdr *)p->payload;
-  tcphdr->src = htons(pcb->local_port);
-  tcphdr->dest = htons(pcb->remote_port);
+  set_ns(tcphdr->src, pcb->local_port);
+  set_ns(tcphdr->dest, pcb->remote_port);
   tcphdr->seqno = seqno_be;
-  tcphdr->ackno = htonl(pcb->rcv_nxt);
+  set_nl(tcphdr->ackno, pcb->rcv_nxt);
   TCPH_FLAGS_SET(tcphdr, TCP_ACK);
-  tcphdr->wnd = htons(pcb->rcv_ann_wnd);
+  set_ns(tcphdr->wnd, pcb->rcv_ann_wnd);
   tcphdr->urgp = 0;
   TCPH_HDRLEN_SET(tcphdr, (5 + optlen / 4));
   tcphdr->chksum = 0;
@@ -308,9 +308,9 @@
       goto memerr;
     }
     seg->tcphdr = (struct tcp_hdr *)seg->p->payload;
-    seg->tcphdr->src = htons(pcb->local_port);
-    seg->tcphdr->dest = htons(pcb->remote_port);
-    seg->tcphdr->seqno = htonl(seqno);
+    set_ns(seg->tcphdr->src, pcb->local_port);
+    set_ns(seg->tcphdr->dest, pcb->remote_port);
+    set_nl(seg->tcphdr->seqno, seqno);
     seg->tcphdr->urgp = 0;
     TCPH_FLAGS_SET(seg->tcphdr, flags);
     /* don't fill in tcphdr->ackno and tcphdr->wnd until later */
@@ -320,8 +320,8 @@
     /* Set the length of the header */
     TCPH_HDRLEN_SET(seg->tcphdr, (5 + optlen / 4));
     LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_enqueue: queueing 
%"U32_F":%"U32_F" (0x%"X16_F")\n",
-      ntohl(seg->tcphdr->seqno),
-      ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
+      get_nl(seg->tcphdr->seqno),
+      get_nl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
       (u16_t)flags));
 
     left -= seglen;
@@ -351,7 +351,7 @@
     /* only concatenate segments with the same options */
     (useg->flags == queue->flags) &&
     /* segments are consecutive */
-    (ntohl(useg->tcphdr->seqno) + useg->len == ntohl(queue->tcphdr->seqno)) ) {
+    (get_nl(useg->tcphdr->seqno) + useg->len == get_nl(queue->tcphdr->seqno)) 
) {
     /* Remove TCP header from first segment of our to-be-queued list */
     if(pbuf_header(queue->p, -(TCP_HLEN + optlen))) {
       /* Can we cope with this failing?  Just assert for now */
@@ -448,8 +448,8 @@
 {
   /* Pad with two NOP options to make everything nicely aligned */
   opts[0] = htonl(0x0101080A);
-  opts[1] = htonl(sys_now());
-  opts[2] = htonl(pcb->ts_recent);
+  set_nl(opts[1], sys_now());
+  set_nl(opts[2], pcb->ts_recent);
 }
 #endif
 
@@ -542,7 +542,7 @@
    */
   if (pcb->flags & TF_ACK_NOW &&
      (seg == NULL ||
-      ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
+      get_nl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
      return tcp_send_empty_ack(pcb);
   }
 
@@ -569,13 +569,13 @@
                 ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F
                  ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
                  pcb->snd_wnd, pcb->cwnd, wnd,
-                 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
-                 ntohl(seg->tcphdr->seqno), pcb->lastack));
+                 get_nl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
+                 get_nl(seg->tcphdr->seqno), pcb->lastack));
   }
 #endif /* TCP_CWND_DEBUG */
   /* data available and window allows it to be sent? */
   while (seg != NULL &&
-         ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
+         get_nl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
     LWIP_ASSERT("RST not expected here!", 
                 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
     /* Stop sending if the nagle algorithm would prevent it
@@ -592,9 +592,9 @@
 #if TCP_CWND_DEBUG
     LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", 
wnd %"U32_F", effwnd %"U32_F", seq %"U32_F", ack %"U32_F", i %"S16_F"\n",
                             pcb->snd_wnd, pcb->cwnd, wnd,
-                            ntohl(seg->tcphdr->seqno) + seg->len -
+                            get_nl(seg->tcphdr->seqno) + seg->len -
                             pcb->lastack,
-                            ntohl(seg->tcphdr->seqno), pcb->lastack, i));
+                            get_nl(seg->tcphdr->seqno), pcb->lastack, i));
     ++i;
 #endif /* TCP_CWND_DEBUG */
 
@@ -606,7 +606,7 @@
     }
 
     tcp_output_segment(seg, pcb);
-    snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
+    snd_nxt = get_nl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
     if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
       pcb->snd_nxt = snd_nxt;
     }
@@ -622,11 +622,11 @@
         /* In the case of fast retransmit, the packet should not go to the tail
          * of the unacked queue, but rather somewhere before it. We need to 
check for
          * this case. -STJ Jul 27, 2004 */
-        if (TCP_SEQ_LT(ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))){
+        if (TCP_SEQ_LT(get_nl(seg->tcphdr->seqno), 
get_nl(useg->tcphdr->seqno))){
           /* add segment to before tail of unacked list, keeping the list 
sorted */
           struct tcp_seg **cur_seg = &(pcb->unacked);
           while (*cur_seg &&
-            TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), 
ntohl(seg->tcphdr->seqno))) {
+            TCP_SEQ_LT(get_nl((*cur_seg)->tcphdr->seqno), 
get_nl(seg->tcphdr->seqno))) {
               cur_seg = &((*cur_seg)->next );
           }
           seg->next = (*cur_seg);
@@ -645,7 +645,7 @@
   }
 
   if (seg != NULL && pcb->persist_backoff == 0 && 
-      ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {
+      get_nl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {
     /* prepare for persist timer */
     pcb->persist_cnt = 0;
     pcb->persist_backoff = 1;
@@ -673,10 +673,10 @@
 
   /* The TCP header has already been constructed, but the ackno and
    wnd fields remain. */
-  seg->tcphdr->ackno = htonl(pcb->rcv_nxt);
+  set_nl(seg->tcphdr->ackno, pcb->rcv_nxt);
 
   /* advertise our receive window size in this TCP segment */
-  seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd);
+  set_ns(seg->tcphdr->wnd, pcb->rcv_ann_wnd);
 
   pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
 
@@ -712,12 +712,12 @@
 
   if (pcb->rttest == 0) {
     pcb->rttest = tcp_ticks;
-    pcb->rtseq = ntohl(seg->tcphdr->seqno);
+    pcb->rtseq = get_nl(seg->tcphdr->seqno);
 
     LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", 
pcb->rtseq));
   }
   LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
-          htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) +
+          get_nl(seg->tcphdr->seqno), get_nl(seg->tcphdr->seqno) +
           seg->len));
 
   len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
@@ -781,10 +781,10 @@
               (p->len >= sizeof(struct tcp_hdr)));
 
   tcphdr = (struct tcp_hdr *)p->payload;
-  tcphdr->src = htons(local_port);
-  tcphdr->dest = htons(remote_port);
-  tcphdr->seqno = htonl(seqno);
-  tcphdr->ackno = htonl(ackno);
+  set_ns(tcphdr->src, local_port);
+  set_ns(tcphdr->dest, remote_port);
+  set_nl(tcphdr->seqno, seqno);
+  set_nl(tcphdr->ackno, ackno);
   TCPH_FLAGS_SET(tcphdr, TCP_RST | TCP_ACK);
   tcphdr->wnd = htons(TCP_WND);
   tcphdr->urgp = 0;
@@ -862,7 +862,7 @@
 
   cur_seg = &(pcb->unsent);
   while (*cur_seg &&
-    TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
+    TCP_SEQ_LT(get_nl((*cur_seg)->tcphdr->seqno), get_nl(seg->tcphdr->seqno))) 
{
       cur_seg = &((*cur_seg)->next );
   }
   seg->next = *cur_seg;
@@ -894,7 +894,7 @@
                 ("tcp_receive: dupacks %"U16_F" (%"U32_F
                  "), fast retransmit %"U32_F"\n",
                  (u16_t)pcb->dupacks, pcb->lastack,
-                 ntohl(pcb->unacked->tcphdr->seqno)));
+                 get_nl(pcb->unacked->tcphdr->seqno)));
     tcp_rexmit(pcb);
 
     /* Set ssthresh to half of the minimum of the current
Index: core/udp.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/udp.c,v
retrieving revision 1.120
diff -u -r1.120 udp.c
--- core/udp.c  12 Feb 2010 16:30:58 -0000      1.120
+++ core/udp.c  14 Feb 2010 15:29:50 -0000
@@ -118,8 +118,8 @@
   LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", 
p->tot_len));
 
   /* convert src and dest ports to host byte order */
-  src = ntohs(udphdr->src);
-  dest = ntohs(udphdr->dest);
+  src = get_ns(udphdr->src);
+  dest = get_ns(udphdr->dest);
 
   udp_debug_print(udphdr);
 
@@ -128,9 +128,9 @@
               ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "
                "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
                ip4_addr1_16(&iphdr->dest), ip4_addr2_16(&iphdr->dest),
-               ip4_addr3_16(&iphdr->dest), ip4_addr4_16(&iphdr->dest), 
ntohs(udphdr->dest),
+               ip4_addr3_16(&iphdr->dest), ip4_addr4_16(&iphdr->dest), 
get_ns(udphdr->dest),
                ip4_addr1_16(&iphdr->src), ip4_addr2_16(&iphdr->src),
-               ip4_addr3_16(&iphdr->src), ip4_addr4_16(&iphdr->src), 
ntohs(udphdr->src)));
+               ip4_addr3_16(&iphdr->src), ip4_addr4_16(&iphdr->src), 
get_ns(udphdr->src)));
 
 #if LWIP_DHCP
   pcb = NULL;
@@ -221,7 +221,7 @@
     if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
       /* Do the UDP Lite checksum */
 #if CHECKSUM_CHECK_UDP
-      u16_t chklen = ntohs(udphdr->len);
+      u16_t chklen = get_ns(udphdr->len);
       if (chklen < sizeof(struct udp_hdr)) {
         if (chklen == 0) {
           /* For UDP-Lite, checksum length of 0 means checksum
@@ -449,8 +449,8 @@
               (q->len >= sizeof(struct udp_hdr)));
   /* q now represents the packet to be sent */
   udphdr = (struct udp_hdr *)q->payload;
-  udphdr->src = htons(pcb->local_port);
-  udphdr->dest = htons(dst_port);
+  set_ns(udphdr->src, pcb->local_port);
+  set_ns(udphdr->dest, dst_port);
   /* in UDP, 0 checksum means 'no checksum' */
   udphdr->chksum = 0x0000; 
 
@@ -497,7 +497,7 @@
       chklen_hdr = 0;
       chklen = q->tot_len;
     }
-    udphdr->len = htons(chklen_hdr);
+    set_ns(udphdr->len, chklen_hdr);
     /* calculate checksum */
 #if CHECKSUM_GEN_UDP
     udphdr->chksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip,
@@ -520,7 +520,7 @@
 #endif /* LWIP_UDPLITE */
   {      /* UDP */
     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", 
q->tot_len));
-    udphdr->len = htons(q->tot_len);
+    set_ns(udphdr->len, q->tot_len);
     /* calculate checksum */
 #if CHECKSUM_GEN_UDP
     if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
@@ -830,10 +830,10 @@
   LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
   LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     %5"U16_F"     | (src 
port, dest port)\n",
-                          ntohs(udphdr->src), ntohs(udphdr->dest)));
+                          get_ns(udphdr->src), get_ns(udphdr->dest)));
   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
   LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     0x%04"X16_F"    | (len, 
chksum)\n",
-                          ntohs(udphdr->len), ntohs(udphdr->chksum)));
+                          get_ns(udphdr->len), get_ns(udphdr->chksum)));
   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
 }
 #endif /* UDP_DEBUG */
Index: core/ipv4/ip.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/ipv4/ip.c,v
retrieving revision 1.85
diff -u -r1.85 ip.c
--- core/ipv4/ip.c      14 Feb 2010 12:42:50 -0000      1.85
+++ core/ipv4/ip.c      14 Feb 2010 15:30:43 -0000
@@ -218,7 +218,7 @@
   /* calculate IP header length in bytes */
   iphdr_hlen *= 4;
   /* obtain ip length in bytes */
-  iphdr_len = ntohs(IPH_LEN(iphdr));
+  iphdr_len = get_ns(IPH_LEN(iphdr));
 
   /* header length exceeds first pbuf length, or ip length exceeds total pbuf 
length? */
   if ((iphdr_hlen > p->len) || (iphdr_len > p->tot_len)) {
@@ -315,8 +315,8 @@
     /* remote port is DHCP server? */
     if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
       LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: UDP packet to DHCP 
client port %"U16_F"\n",
-        ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest)));
-      if (ntohs(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest) == 
DHCP_CLIENT_PORT) {
+        get_ns(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest)));
+      if (get_ns(((struct udp_hdr *)((u8_t *)iphdr + iphdr_hlen))->dest) == 
DHCP_CLIENT_PORT) {
         LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_TRACE, ("ip_input: DHCP packet 
accepted.\n"));
         netif = inp;
         check_ip_src = 0;
@@ -365,7 +365,7 @@
   if ((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {
 #if IP_REASSEMBLY /* packet fragment reassembly code present? */
     LWIP_DEBUGF(IP_DEBUG, ("IP packet is a fragment (id=0x%04"X16_F" 
tot_len=%"U16_F" len=%"U16_F" MF=%"U16_F" offset=%"U16_F"), calling 
ip_reass()\n",
-      ntohs(IPH_ID(iphdr)), p->tot_len, ntohs(IPH_LEN(iphdr)), 
!!(IPH_OFFSET(iphdr) & htons(IP_MF)), (ntohs(IPH_OFFSET(iphdr)) & 
IP_OFFMASK)*8));
+      get_ns(IPH_ID(iphdr)), p->tot_len, get_ns(IPH_LEN(iphdr)), 
!!(IPH_OFFSET(iphdr) & htons(IP_MF)), (get_ns(IPH_OFFSET(iphdr)) & 
IP_OFFMASK)*8));
     /* reassemble the packet*/
     p = ip_reass(p);
     /* packet not fully reassembled yet? */
@@ -376,7 +376,7 @@
 #else /* IP_REASSEMBLY == 0, no packet fragment reassembly code present */
     pbuf_free(p);
     LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("IP packet dropped since 
it was fragmented (0x%"X16_F") (while IP_REASSEMBLY == 0).\n",
-      ntohs(IPH_OFFSET(iphdr))));
+      get_ns(IPH_OFFSET(iphdr))));
     IP_STATS_INC(ip.opterr);
     IP_STATS_INC(ip.drop);
     /* unsupported protocol feature */
@@ -699,19 +699,19 @@
                     IPH_V(iphdr),
                     IPH_HL(iphdr),
                     IPH_TOS(iphdr),
-                    ntohs(IPH_LEN(iphdr))));
+                    get_ns(IPH_LEN(iphdr))));
   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
   LWIP_DEBUGF(IP_DEBUG, ("|    %5"U16_F"      |%"U16_F"%"U16_F"%"U16_F"|    
%4"U16_F"   | (id, flags, offset)\n",
-                    ntohs(IPH_ID(iphdr)),
-                    ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
-                    ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
-                    ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
-                    ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));
+                    get_ns(IPH_ID(iphdr)),
+                    get_ns(IPH_OFFSET(iphdr)) >> 15 & 1,
+                    get_ns(IPH_OFFSET(iphdr)) >> 14 & 1,
+                    get_ns(IPH_OFFSET(iphdr)) >> 13 & 1,
+                    get_ns(IPH_OFFSET(iphdr)) & IP_OFFMASK));
   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |    0x%04"X16_F"     | 
(ttl, proto, chksum)\n",
                     IPH_TTL(iphdr),
                     IPH_PROTO(iphdr),
-                    ntohs(IPH_CHKSUM(iphdr))));
+                    get_ns(IPH_CHKSUM(iphdr))));
   LWIP_DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));
   LWIP_DEBUGF(IP_DEBUG, ("|  %3"U16_F"  |  %3"U16_F"  |  %3"U16_F"  |  
%3"U16_F"  | (src)\n",
                     ip4_addr1_16(&iphdr->src),
Index: core/ipv4/ip_frag.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/ipv4/ip_frag.c,v
retrieving revision 1.53
diff -u -r1.53 ip_frag.c
--- core/ipv4/ip_frag.c 12 Feb 2010 09:51:30 -0000      1.53
+++ core/ipv4/ip_frag.c 14 Feb 2010 15:30:56 -0000
@@ -335,8 +335,8 @@
 
   /* Extract length and fragment offset from current fragment */
   fraghdr = (struct ip_hdr*)new_p->payload; 
-  len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
-  offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
+  len = get_ns(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
+  offset = (get_ns(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
 
   /* overwrite the fragment's ip header from the pbuf with our helper struct,
    * and setup the embedded helper structure. */
@@ -491,8 +491,8 @@
     goto nullreturn;
   }
 
-  offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
-  len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
+  offset = (get_ns(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
+  len = get_ns(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
 
   /* Check if we are allowed to enqueue more datagrams. */
   clen = pbuf_clen(p);
@@ -520,7 +520,7 @@
        fragment into the buffer. */
     if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) {
       LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: matching previous fragment 
ID=%"X16_F"\n",
-        ntohs(IPH_ID(fraghdr))));
+        get_ns(IPH_ID(fraghdr))));
       IPFRAG_STATS_INC(ip_frag.cachehit);
       break;
     }
@@ -535,8 +535,8 @@
       goto nullreturn;
     }
   } else {
-    if (((ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) && 
-      ((ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {
+    if (((get_ns(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) && 
+      ((get_ns(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {
       /* ipr->iphdr is not the header from the first fragment, but fraghdr is
        * -> copy fraghdr into ipr->iphdr since we want to have the header
        * of the first fragment (for ICMP time exceeded and later, for copying
@@ -552,7 +552,7 @@
    * to an existing one */
 
   /* check for 'no more fragments', and update queue entry*/
-  if ((ntohs(IPH_OFFSET(fraghdr)) & IP_MF) == 0) {
+  if ((get_ns(IPH_OFFSET(fraghdr)) & IP_MF) == 0) {
     ipr->flags |= IP_REASS_FLAG_LASTFRAG;
     ipr->datagram_len = offset + len;
     LWIP_DEBUGF(IP_REASS_DEBUG,
@@ -674,7 +674,7 @@
 #endif /* IP_FRAG_USES_STATIC_BUF */
 
   /* Save original offset */
-  tmp = ntohs(IPH_OFFSET(iphdr));
+  tmp = get_ns(IPH_OFFSET(iphdr));
   ofo = tmp & IP_OFFMASK;
   omf = tmp & IP_MF;
 
Index: include/ipv4/lwip/ip.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/ipv4/lwip/ip.h,v
retrieving revision 1.38
diff -u -r1.38 ip.h
--- include/ipv4/lwip/ip.h      14 Feb 2010 11:42:08 -0000      1.38
+++ include/ipv4/lwip/ip.h      14 Feb 2010 15:31:25 -0000
@@ -138,9 +138,9 @@
 #  include "arch/epstruct.h"
 #endif
 
-#define IPH_V(hdr)  (ntohs((hdr)->_v_hl_tos) >> 12)
-#define IPH_HL(hdr) ((ntohs((hdr)->_v_hl_tos) >> 8) & 0x0f)
-#define IPH_TOS(hdr) (ntohs((hdr)->_v_hl_tos) & 0xff)
+#define IPH_V(hdr)  (get_ns((hdr)->_v_hl_tos) >> 12)
+#define IPH_HL(hdr) ((get_ns((hdr)->_v_hl_tos) >> 8) & 0x0f)
+#define IPH_TOS(hdr) (get_ns((hdr)->_v_hl_tos) & 0xff)
 #define IPH_LEN(hdr) ((hdr)->_len)
 #define IPH_ID(hdr) ((hdr)->_id)
 #define IPH_OFFSET(hdr) ((hdr)->_offset)
@@ -148,7 +148,7 @@
 #define IPH_PROTO(hdr) ((hdr)->_proto)
 #define IPH_CHKSUM(hdr) ((hdr)->_chksum)
 
-#define IPH_VHLTOS_SET(hdr, v, hl, tos) (hdr)->_v_hl_tos = (htons(((v) << 12) 
| ((hl) << 8) | (tos)))
+#define IPH_VHLTOS_SET(hdr, v, hl, tos) set_ns((hdr)->_v_hl_tos, ((v) << 12) | 
((hl) << 8) | (tos))
 #define IPH_LEN_SET(hdr, len) (hdr)->_len = (len)
 #define IPH_ID_SET(hdr, id) (hdr)->_id = (id)
 #define IPH_OFFSET_SET(hdr, off) (hdr)->_offset = (off)
Index: include/lwip/def.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/def.h,v
retrieving revision 1.9
diff -u -r1.9 def.h
--- include/lwip/def.h  29 Jan 2010 08:20:39 -0000      1.9
+++ include/lwip/def.h  14 Feb 2010 15:43:03 -0000
@@ -65,10 +65,25 @@
 #endif
 
 #if BYTE_ORDER == BIG_ENDIAN
-#define htons(x) (x)
-#define ntohs(x) (x)
-#define htonl(x) (x)
-#define ntohl(x) (x)
+
+#define htons(x)     (x)
+#define ntohs(x)     (x)
+#define htonl(x)     (x)
+#define ntohl(x)     (x)
+
+#ifndef get_ns
+#define get_ns(x)    (x)
+#endif
+#ifndef get_nl
+#define get_nl(x)    (x)
+#endif
+#ifndef set_ns
+#define set_ns(x, v) ((x) = (v))
+#endif
+#ifndef set_nl
+#define set_nl(x, v) ((x) = (v))
+#endif
+
 #else /* BYTE_ORDER != BIG_ENDIAN */
 #ifdef LWIP_PREFIX_BYTEORDER_FUNCS
 /* workaround for naming collisions on some platforms */
@@ -89,8 +104,38 @@
 u32_t ntohl(u32_t x);
 #endif /* LWIP_PLATFORM_BYTESWAP */
 
+/* New macros to convert byte-order when data is stored in a lwip packed 
structure
+   Don't use with constants: let the compiler do its job.
+*/
+#ifndef get_ns
+#define get_ns(x) ntohs(x)
+#endif
+#ifndef get_nl
+#define get_nl(x) ntohl(x)
+#endif
+#ifndef set_ns
+#define set_ns(x, v) ((x) = htons(v))
+#endif
+#ifndef set_nl
+#define set_nl(x, v) ((x) = htonl(v))
+#endif
+
 #endif /* BYTE_ORDER == BIG_ENDIAN */
 
+/* Macros to load/store network byte order short/long in unaligned protocols 
like DNS */
+#ifndef unpack_ns
+#define unpack_ns(p) ( (((u8_t*)(p))[0]<<8) | (((u8_t*)(p))[1]) )
+#endif
+#ifndef unpack_nl
+#define unpack_nl(p) ( (((u8_t*)(p))[0]<<24) | (((u8_t*)(p))[1]<<16) | 
(((u8_t*)(p))[2]<<8) | (((u8_t*)(p))[3]) )
+#endif
+#ifndef pack_ns
+#define pack_ns(p, v) ((u8_t*)(p))[0]=((v)>>8), ((u8_t*)(p))[1]=(v)
+#endif
+#ifndef pack_nl
+#define pack_nl(p, v) ((u8_t*)(p))[0]=((v)>>24), ((u8_t*)(p))[1]=((v)>>16), 
((u8_t*)(p))[2]=((v)>>8), ((u8_t*)(p))[3]=(v)
+#endif
+
 #ifdef __cplusplus
 }
 #endif
Index: include/lwip/tcp.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/tcp.h,v
retrieving revision 1.95
diff -u -r1.95 tcp.h
--- include/lwip/tcp.h  12 Feb 2010 16:32:31 -0000      1.95
+++ include/lwip/tcp.h  14 Feb 2010 15:32:10 -0000
@@ -295,9 +295,9 @@
 #  include "arch/epstruct.h"
 #endif
 
-#define TCPH_OFFSET(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 8)
-#define TCPH_HDRLEN(phdr) (ntohs((phdr)->_hdrlen_rsvd_flags) >> 12)
-#define TCPH_FLAGS(phdr)  (ntohs((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
+#define TCPH_OFFSET(phdr) (get_ns((phdr)->_hdrlen_rsvd_flags) >> 8)
+#define TCPH_HDRLEN(phdr) (get_ns((phdr)->_hdrlen_rsvd_flags) >> 12)
+#define TCPH_FLAGS(phdr)  (get_ns((phdr)->_hdrlen_rsvd_flags) & TCP_FLAGS)
 
 #define TCPH_OFFSET_SET(phdr, offset) (phdr)->_hdrlen_rsvd_flags = 
htons(((offset) << 8) | TCPH_FLAGS(phdr))
 #define TCPH_HDRLEN_SET(phdr, len) (phdr)->_hdrlen_rsvd_flags = htons(((len) 
<< 12) | TCPH_FLAGS(phdr))
Index: netif/etharp.c
===================================================================
RCS file: /sources/lwip/lwip/src/netif/etharp.c,v
retrieving revision 1.160
diff -u -r1.160 etharp.c
--- netif/etharp.c      14 Feb 2010 12:42:50 -0000      1.160
+++ netif/etharp.c      14 Feb 2010 15:35:20 -0000
@@ -83,11 +83,11 @@
 
 #define HWTYPE_ETHERNET 1
 
-#define ARPH_HWLEN(hdr) (ntohs((hdr)->_hwlen_protolen) >> 8)
-#define ARPH_PROTOLEN(hdr) (ntohs((hdr)->_hwlen_protolen) & 0xff)
+#define ARPH_HWLEN(hdr)    (get_ns((hdr)->_hwlen_protolen) >> 8)
+#define ARPH_PROTOLEN(hdr) (get_ns((hdr)->_hwlen_protolen) & 0xff)
 
-#define ARPH_HWLEN_SET(hdr, len) (hdr)->_hwlen_protolen = 
htons(ARPH_PROTOLEN(hdr) | ((len) << 8))
-#define ARPH_PROTOLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons((len) | 
(ARPH_HWLEN(hdr) << 8))
+#define ARPH_HWLEN_SET(hdr, len)    set_ns((hdr)->_hwlen_protolen, 
ARPH_PROTOLEN(hdr) | ((len) << 8))
+#define ARPH_PROTOLEN_SET(hdr, len) set_ns((hdr)->_hwlen_protolen, (len) | 
(ARPH_HWLEN(hdr) << 8))
 
 enum etharp_state {
   ETHARP_STATE_EMPTY = 0,
@@ -707,7 +707,7 @@
   }
 
   /* now act on the message itself */
-  switch (htons(hdr->opcode)) {
+  switch (get_ns(hdr->opcode)) {
   /* ARP request? */
   case ARP_REQUEST:
     /* ARP request. If it asked for our address, we send out a
@@ -1164,9 +1164,9 @@
      (unsigned)ethhdr->dest.addr[3], (unsigned)ethhdr->dest.addr[4], 
(unsigned)ethhdr->dest.addr[5],
      (unsigned)ethhdr->src.addr[0], (unsigned)ethhdr->src.addr[1], 
(unsigned)ethhdr->src.addr[2],
      (unsigned)ethhdr->src.addr[3], (unsigned)ethhdr->src.addr[4], 
(unsigned)ethhdr->src.addr[5],
-     (unsigned)htons(ethhdr->type)));
+     (unsigned)get_ns(ethhdr->type)));
 
-  type = htons(ethhdr->type);
+  type = get_ns(ethhdr->type);
 #if ETHARP_SUPPORT_VLAN
   if (type == ETHTYPE_VLAN) {
     struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr*)(((char*)ethhdr) + 
SIZEOF_ETH_HDR);
@@ -1177,7 +1177,7 @@
       return ERR_OK;
     }
 #endif /* ETHARP_VLAN_CHECK */
-    type = htons(vlan->tpid);
+    type = get_ns(vlan->tpid);
   }
 #endif /* ETHARP_SUPPORT_VLAN */
 
Index: netif/ethernetif.c
===================================================================
RCS file: /sources/lwip/lwip/src/netif/ethernetif.c,v
retrieving revision 1.52
diff -u -r1.52 ethernetif.c
--- netif/ethernetif.c  9 Oct 2009 20:16:26 -0000       1.52
+++ netif/ethernetif.c  14 Feb 2010 15:35:26 -0000
@@ -236,7 +236,7 @@
   /* points to packet payload, which starts with an Ethernet header */
   ethhdr = p->payload;
 
-  switch (htons(ethhdr->type)) {
+  switch (get_ns(ethhdr->type)) {
   /* IP or ARP packet? */
   case ETHTYPE_IP:
   case ETHTYPE_ARP:


reply via email to

[Prev in Thread] Current Thread [Next in Thread]