NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Revision:
5:dd63a1e02b1b
Parent:
0:632c9925f013
--- a/lwip/core/ipv4/ip_addr.c	Fri Jul 09 14:46:47 2010 +0000
+++ b/lwip/core/ipv4/ip_addr.c	Tue Jul 27 15:59:42 2010 +0000
@@ -51,32 +51,62 @@
  * @param netif the network interface against which the address is checked
  * @return returns non-zero if the address is a broadcast address
  */
-u8_t ip_addr_isbroadcast(ip_addr_t *addr, struct netif *netif)
+u8_t
+ip4_addr_isbroadcast(u32_t addr, const struct netif *netif)
 {
-  u32_t addr2test;
+  ip_addr_t ipaddr;
+  ip4_addr_set_u32(&ipaddr, addr);
 
-  addr2test = ip4_addr_get_u32(addr);
   /* all ones (broadcast) or all zeroes (old skool broadcast) */
-  if ((~addr2test == IPADDR_ANY) ||
-      (addr2test == IPADDR_ANY))
+  if ((~addr == IPADDR_ANY) ||
+      (addr == IPADDR_ANY)) {
     return 1;
   /* no broadcast support on this network interface? */
-  else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0)
+  } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) {
     /* the given address cannot be a broadcast address
      * nor can we check against any broadcast addresses */
     return 0;
   /* address matches network interface address exactly? => no broadcast */
-  else if (addr2test == ip4_addr_get_u32(&netif->ip_addr))
+  } else if (addr == ip4_addr_get_u32(&netif->ip_addr)) {
     return 0;
   /*  on the same (sub) network... */
-  else if (ip_addr_netcmp(addr, &(netif->ip_addr), &(netif->netmask))
+  } else if (ip_addr_netcmp(&ipaddr, &(netif->ip_addr), &(netif->netmask))
          /* ...and host identifier bits are all ones? =>... */
-          && ((addr2test & ~ip4_addr_get_u32(&netif->netmask)) ==
-           (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask))))
+          && ((addr & ~ip4_addr_get_u32(&netif->netmask)) ==
+           (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask)))) {
     /* => network broadcast address */
     return 1;
-  else
+  } else {
     return 0;
+  }
+}
+
+/** Checks if a netmask is valid (starting with ones, then only zeros)
+ *
+ * @param netmask the IPv4 netmask to check (in network byte order!)
+ * @return 1 if the netmask is valid, 0 if it is not
+ */
+u8_t
+ip4_addr_netmask_valid(u32_t netmask)
+{
+  u32_t mask;
+  u32_t nm_hostorder = lwip_htonl(netmask);
+
+  /* first, check for the first zero */
+  for (mask = 1U << 31 ; mask != 0; mask >>= 1) {
+    if ((nm_hostorder & mask) == 0) {
+      break;
+    }
+  }
+  /* then check that there is no one */
+  for (; mask != 0; mask >>= 1) {
+    if ((nm_hostorder & mask) != 0) {
+      /* there is a one after the first zero -> invalid */
+      return 0;
+    }
+  }
+  /* no one after the first zero -> valid */
+  return 1;
 }
 
 /* Here for now until needed in other places in lwIP */
@@ -87,8 +117,8 @@
 #define isxdigit(c)          (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
 #define islower(c)           in_range(c, 'a', 'z')
 #define isspace(c)           (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
-#endif    
-    
+#endif
+
 /**
  * Ascii internet address interpretation routine.
  * The value returned is in network order.
@@ -163,8 +193,9 @@
        *  a.b.c   (with c treated as 16 bits)
        *  a.b (with b treated as 24 bits)
        */
-      if (pp >= parts + 3)
+      if (pp >= parts + 3) {
         return (0);
+      }
       *pp++ = val;
       c = *++cp;
     } else
@@ -173,8 +204,9 @@
   /*
    * Check for trailing characters.
    */
-  if (c != '\0' && !isspace(c))
+  if (c != '\0' && !isspace(c)) {
     return (0);
+  }
   /*
    * Concoct the address according to
    * the number of parts specified.
@@ -188,22 +220,28 @@
     break;
 
   case 2:             /* a.b -- 8.24 bits */
-    if (val > 0xffffffUL)
+    if (val > 0xffffffUL) {
       return (0);
+    }
     val |= parts[0] << 24;
     break;
 
   case 3:             /* a.b.c -- 8.8.16 bits */
-    if (val > 0xffff)
+    if (val > 0xffff) {
       return (0);
+    }
     val |= (parts[0] << 24) | (parts[1] << 16);
     break;
 
   case 4:             /* a.b.c.d -- 8.8.8.8 bits */
-    if (val > 0xff)
+    if (val > 0xff) {
       return (0);
+    }
     val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
     break;
+  default:
+    LWIP_ASSERT("unhandled", 0);
+    break;
   }
   if (addr) {
     ip4_addr_set_u32(addr, htonl(val));
@@ -220,7 +258,7 @@
  *         represenation of addr
  */
 char *
-ipaddr_ntoa(ip_addr_t *addr)
+ipaddr_ntoa(const ip_addr_t *addr)
 {
   static char str[16];
   return ipaddr_ntoa_r(addr, str, 16);
@@ -235,7 +273,7 @@
  * @return either pointer to buf which now holds the ASCII
  *         representation of addr or NULL if buf was too small
  */
-char *ipaddr_ntoa_r(ip_addr_t *addr, char *buf, int buflen)
+char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
 {
   u32_t s_addr;
   char inv[3];