Free (GPLv2) TCP/IP stack developed by TASS Belgium

Dependents:   lpc1768-picotcp-demo ZeroMQ_PicoTCP_Publisher_demo TCPSocket_HelloWorld_PicoTCP Pico_TCP_UDP_Test ... more

PicoTCP. Copyright (c) 2013 TASS Belgium NV.

Released under the GNU General Public License, version 2.

Different licensing models may exist, at the sole discretion of the Copyright holders.

Official homepage: http://www.picotcp.com

Bug tracker: https://github.com/tass-belgium/picotcp/issues

Development steps:

  • initial integration with mbed RTOS
  • generic mbed Ethernet driver
  • high performance NXP LPC1768 specific Ethernet driver
  • Multi-threading support for mbed RTOS
  • Berkeley sockets and integration with the New Socket API
  • Fork of the apps running on top of the New Socket API
  • Scheduling optimizations
  • Debugging/benchmarking/testing

Demo application (measuring TCP sender performance):

Import programlpc1768-picotcp-demo

A PicoTCP demo app testing the ethernet throughput on the lpc1768 mbed board.

Revision:
133:5b075f5e141a
Parent:
132:40ba3014da35
Child:
134:cc4e6d2654d9
--- a/stack/pico_arp.c	Tue Jan 07 13:55:29 2014 +0100
+++ b/stack/pico_arp.c	Thu Jan 16 10:13:37 2014 +0100
@@ -29,6 +29,7 @@
 
 static struct pico_queue pending;
 static int pending_timer_on = 0;
+static int max_arp_reqs = PICO_ARP_MAX_RATE;
 
 void check_pending(pico_time now, void *_unused)
 {
@@ -46,6 +47,20 @@
     pico_timer_add(PICO_ARP_RETRY, &check_pending, NULL);
 }
 
+static void update_max_arp_reqs(pico_time now, void *unused)
+{
+    IGNORE_PARAMETER(now);
+    IGNORE_PARAMETER(unused);
+    if (max_arp_reqs < PICO_ARP_MAX_RATE)
+        max_arp_reqs++;
+
+    pico_timer_add(PICO_ARP_INTERVAL / PICO_ARP_MAX_RATE, &update_max_arp_reqs, NULL);
+}
+
+void pico_arp_init()
+{
+    pico_timer_add(PICO_ARP_INTERVAL / PICO_ARP_MAX_RATE, &update_max_arp_reqs, NULL);
+}
 
 struct
 __attribute__ ((__packed__))
@@ -81,6 +96,7 @@
     int arp_status;
     pico_time timestamp;
     struct pico_device *dev;
+    struct pico_timer *timer;
 };
 
 
@@ -229,16 +245,36 @@
 {
     struct pico_arp_hdr *hdr;
     struct pico_arp search, *found, *new = NULL;
+    struct pico_ip4 me;
+    struct pico_device *link_dev;
+    struct pico_eth_hdr *eh;
     int ret = -1;
     hdr = (struct pico_arp_hdr *) f->net_hdr;
+    me.addr = hdr->dst.addr;
+    eh = (struct pico_eth_hdr *)f->datalink_hdr;
 
     if (!hdr)
         goto end;
 
     /* Validate the incoming arp packet */
+
+    /* Check the hardware type and protocol */
     if ((hdr->htype != PICO_ARP_HTYPE_ETH) || (hdr->ptype != PICO_IDETH_IPV4))
         goto end;
 
+    /* The source mac address must not be a multicast or broadcast address */
+    if (hdr->s_mac[0] & 0x01)
+        goto end;
+
+    /* Prevent ARP flooding */
+    link_dev = pico_ipv4_link_find(&me);
+    if ((link_dev == f->dev) && (hdr->opcode == PICO_ARP_REQUEST)) {
+        if (max_arp_reqs == 0)
+            goto end;
+        else
+            max_arp_reqs--;
+    }
+
     if (conflict_ipv4.conflict != NULL)
     {
         if ((conflict_ipv4.ip.addr == hdr->src.addr) && (memcmp(hdr->s_mac, conflict_ipv4.mac.addr, 6) != 0))
@@ -250,40 +286,47 @@
     memcpy(search.eth.addr, hdr->s_mac, PICO_SIZE_ETH);
 
     /* Search for already existing entry */
+    found = pico_tree_findKey(&arp_tree, &search);
+    if (found) {
+        if (found->arp_status == PICO_ARP_STATUS_STALE) {
+            /* Replace if stale */
+            new = found;
 
-    found = pico_tree_findKey(&arp_tree, &search);
+            pico_tree_delete(&arp_tree, new);
+        }
+        else {
+            /* Update mac address */
+            memcpy(found->eth.addr, hdr->s_mac, PICO_SIZE_ETH);
+
+            /* Refresh timeout & update timestamp*/
+            pico_timer_cancel(found->timer);
+            found->timer = pico_timer_add(PICO_ARP_TIMEOUT, arp_expire, found);
+            found->timestamp = PICO_TIME();
+        }
+    }
+
+    /* Check if we are the target IP address */
+    if (link_dev != f->dev)
+        goto end;
+
+    /* If no existing entry was found, create a new entry */
     if (!found) {
         new = pico_zalloc(sizeof(struct pico_arp));
         if (!new)
             goto end;
 
         new->ipv4.addr = hdr->src.addr;
+        memcpy(new->eth.addr, hdr->s_mac, PICO_SIZE_ETH);
+        new->dev = f->dev;
     }
-    else if (found->arp_status == PICO_ARP_STATUS_STALE) {
-        /* Replace if stale */
-        new = found;
 
-        pico_tree_delete(&arp_tree, new);
-    }
+    if (new)
+        pico_arp_add_entry(new);
 
     ret = 0;
 
-    if (new) {
-        memcpy(new->eth.addr, hdr->s_mac, PICO_SIZE_ETH);
-        new->dev = f->dev;
-        pico_arp_add_entry(new);
-    }
-
+    /* If the packet is a request, send a reply */
     if (hdr->opcode == PICO_ARP_REQUEST) {
-        struct pico_ip4 me;
-        struct pico_eth_hdr *eh = (struct pico_eth_hdr *)f->datalink_hdr;
-        struct pico_device *link_dev;
-        me.addr = hdr->dst.addr;
-
-        link_dev = pico_ipv4_link_find(&me);
-        if (link_dev != f->dev)
-            goto end;
-
         hdr->opcode = PICO_ARP_REPLY;
         memcpy(hdr->d_mac, hdr->s_mac, PICO_SIZE_ETH);
         memcpy(hdr->s_mac, f->dev->eth->mac.addr, PICO_SIZE_ETH);