uIP 1.0 based webserver for LPC1114 + ENC28J60

Dependencies:   mbed TMP102

Revision:
3:a2715e9c7737
Parent:
0:685224d2f66d
--- a/uip/uip-fw.c	Sat Jun 21 11:54:24 2014 +0000
+++ b/uip/uip-fw.c	Mon Jun 30 16:00:08 2014 +0000
@@ -26,11 +26,10 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * This file is part of the uIP TCP/IP stack
+ * This file is part of the Contiki operating system.
  *
  * Author: Adam Dunkels <adam@sics.se>
  *
- * $Id: uip-fw.c,v 1.2 2006/06/12 08:00:30 adam Exp $
  */
 /**
  * \addtogroup uip
@@ -53,11 +52,16 @@
  *
  */
 
+#include <string.h>
+
+#include "uip-conf.h"
+
 #include "uip.h"
 #include "uip_arch.h"
 #include "uip-fw.h"
-
-#include <string.h> /* for memcpy() */
+#ifdef AODV_COMPLIANCE
+#include "uaodv-def.h"
+#endif
 
 /*
  * The list of registered network interfaces.
@@ -71,47 +75,45 @@
 
 struct tcpip_hdr {
   /* IP header. */
-  u8_t vhl,
+  uint8_t vhl,
     tos;
-  u16_t len,
+  uint16_t len,
     ipid,
     ipoffset;
-  u8_t ttl,
+  uint8_t ttl,
     proto;
-  u16_t ipchksum;
-  u16_t srcipaddr[2],
-    destipaddr[2];
+  uint16_t ipchksum;
+  uip_ipaddr_t srcipaddr, destipaddr;
   
   /* TCP header. */
-  u16_t srcport,
+  uint16_t srcport,
     destport;
-  u8_t seqno[4],
+  uint8_t seqno[4],
     ackno[4],
     tcpoffset,
     flags,
     wnd[2];
-  u16_t tcpchksum;
-  u8_t urgp[2];
-  u8_t optdata[4];
+  uint16_t tcpchksum;
+  uint8_t urgp[2];
+  uint8_t optdata[4];
 };
 
 struct icmpip_hdr {
   /* IP header. */
-  u8_t vhl,
+  uint8_t vhl,
     tos,
     len[2],
     ipid[2],
     ipoffset[2],
     ttl,
     proto;
-  u16_t ipchksum;
-  u16_t srcipaddr[2],
-    destipaddr[2];
+  uint16_t ipchksum;
+  uip_ipaddr_t srcipaddr, destipaddr;
   /* ICMP (echo) header. */
-  u8_t type, icode;
-  u16_t icmpchksum;
-  u16_t id, seqno;
-  u8_t payload[1];
+  uint8_t type, icode;
+  uint16_t icmpchksum;
+  uint16_t id, seqno;
+  uint8_t payload[1];
 };
 
 /* ICMP ECHO. */
@@ -135,20 +137,20 @@
  * duplicate packets.
  */
 struct fwcache_entry {
-  u16_t timer;
+  uint16_t timer;
   
-  u16_t srcipaddr[2];
-  u16_t destipaddr[2];
-  u16_t ipid;
-  u8_t proto;
-  u8_t unused;
+  uip_ipaddr_t srcipaddr;
+  uip_ipaddr_t destipaddr;
+  uint16_t ipid;
+  uint8_t proto;
+  uint8_t unused;
 
 #if notdef
-  u16_t payload[2];
+  uint16_t payload[2];
 #endif
 
 #if UIP_REASSEMBLY > 0
-  u16_t len, offset;
+  uint16_t len, offset;
 #endif
 };
 
@@ -204,10 +206,12 @@
  */
 /*------------------------------------------------------------------------------*/
 static unsigned char
-ipaddr_maskcmp(u16_t *ipaddr, u16_t *netipaddr, u16_t *netmask)
+ipaddr_maskcmp(uip_ipaddr_t *ipaddr,
+	       uip_ipaddr_t *netipaddr,
+	       uip_ipaddr_t *netmask)
 {
-  return (ipaddr[0] & netmask [0]) == (netipaddr[0] & netmask[0]) &&
-    (ipaddr[1] & netmask[1]) == (netipaddr[1] & netmask[1]);
+  return (ipaddr->u16[0] & netmask->u16[0]) == (netipaddr->u16[0] & netmask->u16[0]) &&
+    (ipaddr->u16[1] & netmask->u16[1]) == (netipaddr->u16[1] & netmask->u16[1]);
 }
 /*------------------------------------------------------------------------------*/
 /**
@@ -221,15 +225,15 @@
 static void
 time_exceeded(void)
 {
-  u16_t tmp16;
 
-  /* We don't send out ICMP errors for ICMP messages. */
-  if(ICMPBUF->proto == UIP_PROTO_ICMP) {
+  /* We don't send out ICMP errors for ICMP messages (unless they are pings). */
+  if(ICMPBUF->proto == UIP_PROTO_ICMP &&
+     ICMPBUF->type != ICMP_ECHO) {
     uip_len = 0;
     return;
   }
   /* Copy fields from packet header into payload of this ICMP packet. */
-  memcpy(&(ICMPBUF->payload[0]), ICMPBUF, 28);
+  memcpy(&(ICMPBUF->payload[0]), ICMPBUF, UIP_IPH_LEN + 8);
 
   /* Set the ICMP type and code. */
   ICMPBUF->type = ICMP_TE;
@@ -237,26 +241,20 @@
 
   /* Calculate the ICMP checksum. */
   ICMPBUF->icmpchksum = 0;
-  ICMPBUF->icmpchksum = ~uip_chksum((u16_t *)&(ICMPBUF->type), 36);
+  ICMPBUF->icmpchksum = ~uip_chksum((uint16_t *)&(ICMPBUF->type), 36);
 
   /* Set the IP destination address to be the source address of the
      original packet. */
-  tmp16= BUF->destipaddr[0];
-  BUF->destipaddr[0] = BUF->srcipaddr[0];
-  BUF->srcipaddr[0] = tmp16;
-  tmp16 = BUF->destipaddr[1];
-  BUF->destipaddr[1] = BUF->srcipaddr[1];
-  BUF->srcipaddr[1] = tmp16;
+  uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr);
 
   /* Set our IP address as the source address. */
-  BUF->srcipaddr[0] = uip_hostaddr[0];
-  BUF->srcipaddr[1] = uip_hostaddr[1];
+  uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr);
 
   /* The size of the ICMP time exceeded packet is 36 + the size of the
      IP header (20) = 56. */
   uip_len = 56;
   ICMPBUF->len[0] = 0;
-  ICMPBUF->len[1] = uip_len;
+  ICMPBUF->len[1] = (uint8_t)uip_len;
 
   /* Fill in the other fields in the IP header. */
   ICMPBUF->vhl = 0x45;
@@ -300,10 +298,8 @@
 
   fw->timer = FW_TIME;
   fw->ipid = BUF->ipid;
-  fw->srcipaddr[0] = BUF->srcipaddr[0];
-  fw->srcipaddr[1] = BUF->srcipaddr[1];
-  fw->destipaddr[0] = BUF->destipaddr[0];
-  fw->destipaddr[1] = BUF->destipaddr[1];
+  uip_ipaddr_copy(&fw->srcipaddr, &BUF->srcipaddr);
+  uip_ipaddr_copy(&fw->destipaddr, &BUF->destipaddr);
   fw->proto = BUF->proto;
 #if notdef
   fw->payload[0] = BUF->srcport;
@@ -327,8 +323,8 @@
   
   /* Walk through every network interface to check for a match. */
   for(netif = netifs; netif != NULL; netif = netif->next) {
-    if(ipaddr_maskcmp(BUF->destipaddr, netif->ipaddr,
-		      netif->netmask)) {
+    if(ipaddr_maskcmp(&BUF->destipaddr, &netif->ipaddr,
+		      &netif->netmask)) {
       /* If there was a match, we break the loop. */
       return netif;
     }
@@ -354,10 +350,13 @@
  * function is passed unmodified as a return value.
  */
 /*------------------------------------------------------------------------------*/
-u8_t
+uint8_t
 uip_fw_output(void)
 {
   struct uip_fw_netif *netif;
+#if UIP_BROADCAST
+  const struct uip_udpip_hdr *udp = (void *)BUF;
+#endif /* UIP_BROADCAST */
 
   if(uip_len == 0) {
     return UIP_FW_ZEROLEN;
@@ -367,9 +366,7 @@
 
 #if UIP_BROADCAST
   /* Link local broadcasts go out on all interfaces. */
-  if(/*BUF->proto == UIP_PROTO_UDP &&*/
-     BUF->destipaddr[0] == 0xffff &&
-     BUF->destipaddr[1] == 0xffff) {
+  if(uip_ipaddr_cmp(&udp->destipaddr, &uip_broadcast_addr)) {
     if(defaultnetif != NULL) {
       defaultnetif->output();
     }
@@ -402,22 +399,28 @@
  * the packet should be processed locally.
  */
 /*------------------------------------------------------------------------------*/
-u8_t
+uint8_t
 uip_fw_forward(void)
 {
   struct fwcache_entry *fw;
 
   /* First check if the packet is destined for ourselves and return 0
      to indicate that the packet should be processed locally. */
-  if(BUF->destipaddr[0] == uip_hostaddr[0] &&
-     BUF->destipaddr[1] == uip_hostaddr[1]) {
+  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) {
     return UIP_FW_LOCAL;
   }
 
+#ifdef AODV_COMPLIANCE
+#define udp ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
+  if(udp->proto == UIP_PROTO_UDP && udp->destport == UIP_HTONS(UAODV_UDPPORT)) {
+    return UIP_FW_LOCAL;
+  }
+#endif
+
   /* If we use ping IP address configuration, and our IP address is
      not yet configured, we should intercept all ICMP echo packets. */
 #if UIP_PINGADDRCONF
-  if((uip_hostaddr[0] | uip_hostaddr[1]) == 0 &&
+  if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr) &&
      BUF->proto == UIP_PROTO_ICMP &&
      ICMPBUF->type == ICMP_ECHO) {
     return UIP_FW_LOCAL;
@@ -434,10 +437,8 @@
        fw->offset == BUF->ipoffset &&
 #endif
        fw->ipid == BUF->ipid &&
-       fw->srcipaddr[0] == BUF->srcipaddr[0] &&
-       fw->srcipaddr[1] == BUF->srcipaddr[1] &&
-       fw->destipaddr[0] == BUF->destipaddr[0] &&
-       fw->destipaddr[1] == BUF->destipaddr[1] &&
+       uip_ipaddr_cmp(&fw->srcipaddr, &BUF->srcipaddr) &&
+       uip_ipaddr_cmp(&fw->destipaddr, &BUF->destipaddr) &&
 #if notdef
        fw->payload[0] == BUF->srcport &&
        fw->payload[1] == BUF->destport &&
@@ -451,9 +452,10 @@
   /* If the TTL reaches zero we produce an ICMP time exceeded message
      in the uip_buf buffer and forward that packet back to the sender
      of the packet. */
+
   if(BUF->ttl <= 1) {
     /* No time exceeded for broadcasts and multicasts! */
-    if(BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
+    if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
       return UIP_FW_LOCAL;
     }
     time_exceeded();
@@ -463,10 +465,10 @@
   BUF->ttl = BUF->ttl - 1;
   
   /* Update the IP checksum. */
-  if(BUF->ipchksum >= HTONS(0xffff - 0x0100)) {
-    BUF->ipchksum = BUF->ipchksum + HTONS(0x0100) + 1;
+  if(BUF->ipchksum >= UIP_HTONS(0xffff - 0x0100)) {
+    BUF->ipchksum = BUF->ipchksum + UIP_HTONS(0x0100) + 1;
   } else {
-    BUF->ipchksum = BUF->ipchksum + HTONS(0x0100);
+    BUF->ipchksum = BUF->ipchksum + UIP_HTONS(0x0100);
   }
 
   if(uip_len > 0) {
@@ -475,7 +477,7 @@
   }
 
 #if UIP_BROADCAST
-  if(BUF->destipaddr[0] == 0xffff && BUF->destipaddr[1] == 0xffff) {
+  if(uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr)) {
     return UIP_FW_LOCAL;
   }
 #endif /* UIP_BROADCAST */
@@ -530,3 +532,5 @@
   }
 }
 /*------------------------------------------------------------------------------*/
+/** @} */
+/** @} */