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.

Files at this revision

API Documentation at this revision

Comitter:
tass picotcp@tass.be
Date:
Thu Jan 16 10:13:37 2014 +0100
Parent:
132:40ba3014da35
Child:
134:cc4e6d2654d9
Commit message:
Update from master branch

Changed in this revision

include/pico_arp.h Show annotated file Show diff for this revision Revisions of this file
include/pico_constants.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_slaacv4.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_tcp.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_tcp.h Show annotated file Show diff for this revision Revisions of this file
stack/pico_arp.c Show annotated file Show diff for this revision Revisions of this file
stack/pico_device.c Show annotated file Show diff for this revision Revisions of this file
stack/pico_protocol.c Show annotated file Show diff for this revision Revisions of this file
stack/pico_socket.c Show annotated file Show diff for this revision Revisions of this file
stack/pico_stack.c Show annotated file Show diff for this revision Revisions of this file
--- a/include/pico_arp.h	Tue Jan 07 13:55:29 2014 +0100
+++ b/include/pico_arp.h	Thu Jan 16 10:13:37 2014 +0100
@@ -27,4 +27,5 @@
 int pico_arp_create_entry(uint8_t*hwaddr, struct pico_ip4 ipv4, struct pico_device*dev);
 int pico_arp_get_neighbors(struct pico_device *dev, struct pico_ip4 *neighbors, int maxlen);
 void pico_arp_register_ipconflict(struct pico_ip4 *ip, struct pico_eth *mac, void (*cb)(void));
+void pico_arp_init(void);
 #endif
--- a/include/pico_constants.h	Tue Jan 07 13:55:29 2014 +0100
+++ b/include/pico_constants.h	Thu Jan 16 10:13:37 2014 +0100
@@ -112,6 +112,13 @@
 }
 #endif
 
+/*** *** *** *** *** *** ***
+ ***     ARP CONFIG      ***
+ *** *** *** *** *** *** ***/
+/* Maximum amount of accepted ARP requests per burst interval */
+#define PICO_ARP_MAX_RATE 1
+/* Duration of the burst interval in milliseconds */
+#define PICO_ARP_INTERVAL 1000
 
 /* Add well-known host numbers here. (bigendian constants only beyond this point) */
 #define PICO_IP4_ANY (0x00000000U)
@@ -122,12 +129,13 @@
 extern const uint8_t PICO_IPV6_ANY[PICO_SIZE_IP6];
 #endif
 
-static inline uint32_t pico_hash(const char *name)
+static inline uint32_t pico_hash(const void *buf, uint32_t size)
 {
     uint32_t hash = 5381;
-    uint8_t c;
-    while ((c = (uint8_t)*name++))
-        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
+    uint32_t i;
+    const uint8_t *ptr= (const uint8_t *)buf;
+    for(i = 0; i < size; i++)
+        hash = ((hash << 5) + hash) + ptr[i]; /* hash * 33 + char */
     return hash;
 }
 
--- a/modules/pico_slaacv4.c	Tue Jan 07 13:55:29 2014 +0100
+++ b/modules/pico_slaacv4.c	Thu Jan 16 10:13:37 2014 +0100
@@ -55,7 +55,7 @@
     uint32_t seed = 0;
     if (dev->eth != NULL)
     {
-        seed = pico_hash((const char *)dev->eth->mac.addr);
+        seed = pico_hash((const uint8_t *)dev->eth->mac.addr, PICO_SIZE_ETH);
     }
 
     if (rand)
--- a/modules/pico_tcp.c	Tue Jan 07 13:55:29 2014 +0100
+++ b/modules/pico_tcp.c	Thu Jan 16 10:13:37 2014 +0100
@@ -2159,7 +2159,6 @@
            a reset is valid if its sequence number is in the window */
         if ((long_be(hdr->seq) >= t->rcv_ackd) && (long_be(hdr->seq) <= ((uint32_t)(short_be(hdr->rwnd) << (t->wnd_scale)) + t->rcv_ackd))) {
             if ((s->state & PICO_SOCKET_STATE_TCP) == PICO_SOCKET_STATE_TCP_SYN_RECV) {
-                s->parent->number_of_pending_conn--;
                 tcp_force_closed(s);
                 pico_err = PICO_ERR_ECONNRESET;
                 tcp_wakeup_pending(s, PICO_SOCK_EV_ERR);
@@ -2202,7 +2201,6 @@
         /* set SHUT_LOCAL */
         s->state |= PICO_SOCKET_STATE_SHUT_LOCAL;
         pico_socket_close(s);
-        s->parent->number_of_pending_conn--;
         return 1;
     }
 
@@ -2247,7 +2245,7 @@
         { /* PICO_SOCKET_STATE_TCP_UNDEF      */ },
         { /* PICO_SOCKET_STATE_TCP_CLOSED     */ },
         { /* PICO_SOCKET_STATE_TCP_LISTEN     */ PICO_TCP_SYN },
-        { /* PICO_SOCKET_STATE_TCP_SYN_SENT   */ PICO_TCP_SYNACK, PICO_TCP_RST},
+        { /* PICO_SOCKET_STATE_TCP_SYN_SENT   */ PICO_TCP_SYNACK, PICO_TCP_RST, PICO_TCP_RSTACK},
         { /* PICO_SOCKET_STATE_TCP_SYN_RECV   */ PICO_TCP_ACK, PICO_TCP_PSH, PICO_TCP_PSHACK, PICO_TCP_FINACK, PICO_TCP_FINPSHACK, PICO_TCP_RST},
         { /* PICO_SOCKET_STATE_TCP_ESTABLISHED*/ PICO_TCP_SYN, PICO_TCP_SYNACK, PICO_TCP_ACK, PICO_TCP_PSH, PICO_TCP_PSHACK, PICO_TCP_FIN, PICO_TCP_FINACK, PICO_TCP_FINPSHACK, PICO_TCP_RST},
         { /* PICO_SOCKET_STATE_TCP_CLOSE_WAIT */ PICO_TCP_SYNACK, PICO_TCP_ACK, PICO_TCP_PSH, PICO_TCP_PSHACK, PICO_TCP_FIN, PICO_TCP_FINACK, PICO_TCP_FINPSHACK, PICO_TCP_RST},
--- a/modules/pico_tcp.h	Tue Jan 07 13:55:29 2014 +0100
+++ b/modules/pico_tcp.h	Thu Jan 16 10:13:37 2014 +0100
@@ -71,6 +71,7 @@
 #define PICO_TCP_PSHACK    (PICO_TCP_PSH | PICO_TCP_ACK)
 #define PICO_TCP_FINACK    (PICO_TCP_FIN | PICO_TCP_ACK)
 #define PICO_TCP_FINPSHACK (PICO_TCP_FIN | PICO_TCP_PSH | PICO_TCP_ACK)
+#define PICO_TCP_RSTACK    (PICO_TCP_RST | PICO_TCP_ACK)
 
 
 struct __attribute__((packed)) pico_tcp_option
--- 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);
--- a/stack/pico_device.c	Tue Jan 07 13:55:29 2014 +0100
+++ b/stack/pico_device.c	Thu Jan 16 10:13:37 2014 +0100
@@ -36,7 +36,7 @@
         len = MAX_DEVICE_NAME;
 
     memcpy(dev->name, name, len);
-    dev->hash = pico_hash(dev->name);
+    dev->hash = pico_hash(dev->name, len);
 
     pico_tree_insert(&Device_tree, dev);
     dev->q_in = pico_zalloc(sizeof(struct pico_queue));
--- a/stack/pico_protocol.c	Tue Jan 07 13:55:29 2014 +0100
+++ b/stack/pico_protocol.c	Thu Jan 16 10:13:37 2014 +0100
@@ -335,7 +335,7 @@
     if (!p)
         return;
 
-    p->hash = pico_hash(p->name);
+    p->hash = pico_hash(p->name, (uint32_t)strlen(p->name));
     switch (p->layer) {
     case PICO_LAYER_DATALINK:
         pico_tree_insert(&Datalink_proto_tree, p);
--- a/stack/pico_socket.c	Tue Jan 07 13:55:29 2014 +0100
+++ b/stack/pico_socket.c	Thu Jan 16 10:13:37 2014 +0100
@@ -642,12 +642,6 @@
     struct pico_socket *s = (struct pico_socket *) arg;
     IGNORE_PARAMETER(now);
 
-#ifdef PICO_SUPPORT_TCP
-    if(s->parent)
-        s->parent->number_of_pending_conn--;
-
-#endif
-
     socket_clean_queues(s);
     pico_free(s);
 }
@@ -711,6 +705,11 @@
     } while (0);
 #endif
 
+#ifdef PICO_SUPPORT_TCP
+    if(s->parent)
+        s->parent->number_of_pending_conn--;
+#endif
+
     s->state = PICO_SOCKET_STATE_CLOSED;
     pico_timer_add(3000, socket_garbage_collect, s);
     UNLOCK(Mutex);
@@ -2227,11 +2226,10 @@
 {
 
 /* checking for pending connections */
-    if(TCP_STATE(s) == PICO_SOCKET_STATE_TCP_SYN_RECV)
-        if((pico_time)(PICO_TIME_MS() - s->timestamp) >= PICO_SOCKET_BOUND_TIMEOUT) {
-            s->parent->number_of_pending_conn--;
+    if(TCP_STATE(s) == PICO_SOCKET_STATE_TCP_SYN_RECV) {
+        if((pico_time)(PICO_TIME_MS() - s->timestamp) >= PICO_SOCKET_BOUND_TIMEOUT)
             return -1;
-        }
+    }
 
     if((pico_time)(PICO_TIME_MS() - s->timestamp) >= PICO_SOCKET_TIMEOUT) {
         /* checking for hanging sockets */
--- a/stack/pico_stack.c	Tue Jan 07 13:55:29 2014 +0100
+++ b/stack/pico_stack.c	Thu Jan 16 10:13:37 2014 +0100
@@ -801,6 +801,7 @@
 
     /* Initialize timer heap */
     Timers = heap_init();
+    pico_arp_init();
     pico_stack_tick();
     pico_stack_tick();
     pico_stack_tick();