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
Date:
Thu Dec 05 08:31:32 2013 +0000
Parent:
127:476fed453d4d
Child:
129:dbf9eddc9109
Commit message:
updated repo to work with uint64 tick.

Changed in this revision

include/arch/pico_mbed.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
include/pico_frame.h Show annotated file Show diff for this revision Revisions of this file
include/pico_socket.h Show annotated file Show diff for this revision Revisions of this file
include/pico_stack.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_dhcp_client.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_dns_client.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_icmp4.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_icmp4.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_igmp.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_ipfilter.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_ipfilter.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_mbed.cpp Show annotated file Show diff for this revision Revisions of this file
modules/pico_nat.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_olsr.c 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
stack/pico_arp.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/arch/pico_mbed.h	Wed Dec 04 08:39:16 2013 +0000
+++ b/include/arch/pico_mbed.h	Thu Dec 05 08:31:32 2013 +0000
@@ -80,30 +80,35 @@
 extern void pico_mutex_unlock(void*);
 
 extern uint32_t os_time;
+extern uint64_t local_time;
+extern uint32_t last_os_time;
 
 #ifdef TIME_PRESCALE
 extern int32_t prescale_time;
 #endif
 extern uint32_t os_time;
 
+#define UPDATE_LOCAL_TIME() do{local_time=local_time+(os_time-last_os_time);last_os_time=os_time;}while(0)
 
-static inline unsigned long PICO_TIME(void)
+static inline uint64_t PICO_TIME(void)
 {
+  UPDATE_LOCAL_TIME();
   #ifdef TIME_PRESCALE
-  return (prescale_time < 0) ? (unsigned long)(os_time / 1000 << (-prescale_time)) : \
-                                (unsigned long)(os_time / 1000 >> prescale_time);
+  return (prescale_time < 0) ? (uint64_t)(local_time / 1000 << (-prescale_time)) : \
+                                (uint64_t)(local_time / 1000 >> prescale_time);
   #else
-  return (unsigned long)(os_time / 1000);
+  return (uint64_t)(local_time / 1000);
   #endif
 }
 
-static inline unsigned long PICO_TIME_MS(void)
+static inline uint64_t PICO_TIME_MS(void)
 {
+  UPDATE_LOCAL_TIME();
   #ifdef TIME_PRESCALE
-  return (prescale_time < 0) ? (unsigned long)(os_time << (-prescale_time)) : \
-                                (unsigned long)(os_time >> prescale_time);
+  return (prescale_time < 0) ? (uint64_t)(local_time << (-prescale_time)) : \
+                                (uint64_t)(local_time >> prescale_time);
   #else
-  return (unsigned long)os_time;
+  return (uint64_t)local_time;
   #endif
 }
 
--- a/include/pico_constants.h	Wed Dec 04 08:39:16 2013 +0000
+++ b/include/pico_constants.h	Thu Dec 05 08:31:32 2013 +0000
@@ -7,8 +7,8 @@
 #define _INCLUDE_PICO_CONST
 /* Included from pico_config.h */
 /** Endian-dependant constants **/
-
-extern volatile uint32_t pico_tick;
+typedef uint64_t pico_time;
+extern volatile uint64_t pico_tick;
 
 #ifdef PICO_BIGENDIAN
 
--- a/include/pico_frame.h	Wed Dec 04 08:39:16 2013 +0000
+++ b/include/pico_frame.h	Thu Dec 05 08:31:32 2013 +0000
@@ -47,7 +47,7 @@
    */
   struct pico_device *dev;
 
-  uint32_t timestamp;
+  pico_time timestamp;
 
   /* Failures due to bad datalink addressing. */
   uint16_t failure_count;
--- a/include/pico_socket.h	Wed Dec 04 08:39:16 2013 +0000
+++ b/include/pico_socket.h	Thu Dec 05 08:31:32 2013 +0000
@@ -65,7 +65,7 @@
   int id;
   uint16_t state;
   uint16_t opt_flags;
-  uint32_t timestamp;
+  pico_time timestamp;
   void *priv;
 };
 
--- a/include/pico_stack.h	Wed Dec 04 08:39:16 2013 +0000
+++ b/include/pico_stack.h	Thu Dec 05 08:31:32 2013 +0000
@@ -62,7 +62,7 @@
 int pico_source_is_local(struct pico_frame *f);
 int pico_destination_is_local(struct pico_frame *f);
 void pico_store_network_origin(void *src, struct pico_frame *f);
-struct pico_timer *pico_timer_add(uint32_t expire, void (*timer)(uint32_t, void *), void *arg);
+struct pico_timer *pico_timer_add(pico_time expire, void (*timer)(pico_time, void *), void *arg);
 void pico_timer_cancel(struct pico_timer *t);
 uint32_t pico_rand(void);
 void pico_rand_feed(uint32_t feed);
--- a/modules/pico_dhcp_client.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_dhcp_client.c	Thu Dec 05 08:31:32 2013 +0000
@@ -67,7 +67,7 @@
   uint32_t *uid;
   enum dhcp_client_state state;
   void (*cb)(void* dhcpc, int code);
-  uint32_t init_timestamp;
+  pico_time init_timestamp;
   struct pico_socket *s;
   struct pico_ip4 address;
   struct pico_ip4 netmask;
@@ -156,7 +156,7 @@
     return NULL;
 }
 
-static void pico_dhcp_client_init_timer(uint32_t __attribute__((unused)) now, void *arg)
+static void pico_dhcp_client_init_timer(pico_time __attribute__((unused)) now, void *arg)
 {
   struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
@@ -178,7 +178,7 @@
   return;
 }
 
-static void pico_dhcp_client_requesting_timer(uint32_t __attribute__((unused)) now, void *arg)
+static void pico_dhcp_client_requesting_timer(pico_time __attribute__((unused)) now, void *arg)
 {
   struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
@@ -198,7 +198,7 @@
   return;
 }
 
-static void pico_dhcp_client_renewing_timer(uint32_t __attribute__((unused)) now, void *arg)
+static void pico_dhcp_client_renewing_timer(pico_time __attribute__((unused)) now, void *arg)
 {
   struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
@@ -213,7 +213,7 @@
   return;
 }
 
-static void pico_dhcp_client_rebinding_timer(uint32_t __attribute__((unused)) now, void *arg)
+static void pico_dhcp_client_rebinding_timer(pico_time __attribute__((unused)) now, void *arg)
 {
   struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
@@ -228,7 +228,7 @@
   return;
 }
 
-static void pico_dhcp_client_T1_timer(uint32_t __attribute__((unused)) now, void *arg)
+static void pico_dhcp_client_T1_timer(pico_time __attribute__((unused)) now, void *arg)
 {
   struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
@@ -242,7 +242,7 @@
   return;
 }
 
-static void pico_dhcp_client_T2_timer(uint32_t __attribute__((unused)) now, void *arg)
+static void pico_dhcp_client_T2_timer(pico_time __attribute__((unused)) now, void *arg)
 {
   struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
@@ -257,7 +257,7 @@
   return;
 }
 
-static void pico_dhcp_client_lease_timer(uint32_t __attribute__((unused)) now, void *arg)
+static void pico_dhcp_client_lease_timer(pico_time __attribute__((unused)) now, void *arg)
 {
   struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
@@ -272,7 +272,7 @@
   return;
 }
 
-static void pico_dhcp_client_reinit(uint32_t __attribute__((unused)) now, void *arg)
+static void pico_dhcp_client_reinit(pico_time __attribute__((unused)) now, void *arg)
 {
   struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
@@ -925,6 +925,7 @@
   return ((struct pico_dhcp_client_cookie*)dhcpc)->netmask;
 }
 
+
 struct pico_ip4 pico_dhcp_get_nameserver(void* dhcpc)
 {
   return ((struct pico_dhcp_client_cookie*)dhcpc)->nameserver;
--- a/modules/pico_dns_client.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_dns_client.c	Thu Dec 05 08:31:32 2013 +0000
@@ -73,7 +73,7 @@
 #define PICO_DNS_IPV4_ADDR_LEN 16
 
 static void pico_dns_client_callback(uint16_t ev, struct pico_socket *s);
-static void pico_dns_client_retransmission(uint32_t now, void *arg);
+static void pico_dns_client_retransmission(pico_time now, void *arg);
 
 /* RFC 1035 section 4. MESSAGES */
 struct __attribute__((packed)) pico_dns_name
@@ -520,7 +520,7 @@
   return -1;
 }
 
-static void pico_dns_client_retransmission(uint32_t now, void *arg)
+static void pico_dns_client_retransmission(pico_time now, void *arg)
 {
   struct pico_dns_query *q = NULL;
   struct pico_dns_query dummy;
--- a/modules/pico_icmp4.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_icmp4.c	Thu Dec 05 08:31:32 2013 +0000
@@ -158,7 +158,7 @@
   uint16_t seq;
   uint16_t size;
   int count;
-  uint32_t timestamp;
+  pico_time timestamp;
   int interval;
   int timeout;
   void (*cb)(struct pico_icmp4_stats*);
@@ -198,7 +198,7 @@
 }
 
 
-static void ping_timeout(uint32_t now, void *arg)
+static void ping_timeout(pico_time now, void *arg)
 {
   struct pico_icmp4_ping_cookie *cookie = (struct pico_icmp4_ping_cookie *)arg;
   IGNORE_PARAMETER(now);
@@ -220,7 +220,7 @@
   }
 }
 
-static void next_ping(uint32_t now, void *arg);
+static void next_ping(pico_time now, void *arg);
 static inline void send_ping(struct pico_icmp4_ping_cookie *cookie)
 {
   pico_icmp4_send_echo(cookie);
@@ -230,7 +230,7 @@
     pico_timer_add((uint32_t)cookie->interval, next_ping, cookie);
 }
 
-static void next_ping(uint32_t now, void *arg)
+static void next_ping(pico_time now, void *arg)
 {
   struct pico_icmp4_ping_cookie *newcookie, *cookie = (struct pico_icmp4_ping_cookie *)arg;
   IGNORE_PARAMETER(now);
--- a/modules/pico_icmp4.h	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_icmp4.h	Thu Dec 05 08:31:32 2013 +0000
@@ -129,7 +129,7 @@
   struct pico_ip4 dst;
   unsigned long size;
   unsigned long seq;
-  uint32_t time;
+  pico_time time;
   unsigned long ttl;
   int err;
 };
--- a/modules/pico_igmp.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_igmp.c	Thu Dec 05 08:31:32 2013 +0000
@@ -129,8 +129,8 @@
 struct igmp_timer {
   uint8_t type;
   uint8_t stopped;
-  uint32_t start;
-  uint32_t delay;
+  pico_time start;
+  pico_time delay;
   struct pico_ip4 mcast_link;
   struct pico_ip4 mcast_group;
   struct pico_frame *f;
@@ -212,7 +212,7 @@
   return 0;
 }
 
-static void pico_igmp_timer_expired(uint32_t now, void *arg)
+static void pico_igmp_timer_expired(pico_time now, void *arg)
 {
   struct igmp_timer *t = NULL, *timer = NULL, test = {0};
 
@@ -1076,7 +1076,7 @@
   if (!t)
     return -1;
 
-  time_to_run = t->start + t->delay - PICO_TIME_MS();
+  time_to_run = (uint32_t)(t->start + t->delay - PICO_TIME_MS());
   if ((p->max_resp_time * 100) < time_to_run) { /* max_resp_time in units of 1/10 seconds */
     t->delay = pico_rand() % (p->max_resp_time * 100u);
     pico_igmp_timer_reset(t);
--- a/modules/pico_ipfilter.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_ipfilter.c	Thu Dec 05 08:31:32 2013 +0000
@@ -70,11 +70,11 @@
 	if(temp->filter_id && filter->filter_id == temp->filter_id)
 		return 0;
 
-	ipf_dbg("filter ->> %x %x %x %x %d %d\n",filter->in_addr,filter->in_addr_netmask,filter->out_addr,filter->out_addr_netmask,filter->in_port,filter->out_port);
-
+	ipf_dbg("\nfilter ->> %x %x %x %x %d %d %d\n",filter->in_addr,filter->in_addr_netmask,filter->out_addr,filter->out_addr_netmask,filter->in_port,filter->out_port,filter->proto);
+	ipf_dbg("\ntemp ->> %x %x %x %x %d %d %d\n",temp->in_addr,temp->in_addr_netmask,temp->out_addr,temp->out_addr_netmask,temp->in_port,temp->out_port,filter->proto);
 	CHECK_AND_RETURN(filter->fdev,temp->fdev);
 	CHECK_AND_RETURN((filter->in_addr & filter->in_addr_netmask),(temp->in_addr & filter->in_addr_netmask));
-	CHECK_AND_RETURN((filter->out_addr & filter->out_addr_netmask),(temp->out_addr & filter->in_addr_netmask));
+	CHECK_AND_RETURN((filter->out_addr & filter->out_addr_netmask),(temp->out_addr & filter->out_addr_netmask));
 	CHECK_AND_RETURN(filter->in_port,temp->in_port);
 	CHECK_AND_RETURN(filter->out_port,temp->out_port);
 	CHECK_AND_RETURN(filter->priority,temp->priority);
@@ -148,7 +148,7 @@
   if(filter_id == 0)
 	  filter_id = 1;
 
-  new_filter->filter_id = filter_id;
+  new_filter->filter_id = filter_id++;
 
   /*Define filterType_functionPointer here instead of in ipfilter-function, to prevent running multiple times through switch*/
   switch (action) {
@@ -203,6 +203,7 @@
   temp.fdev = f->dev;
   temp.out_addr = ipv4_hdr->dst.addr;
   temp.in_addr = ipv4_hdr->src.addr;
+
   if (ipv4_hdr->proto == PICO_PROTO_TCP ) {
       tcp_hdr = (struct pico_tcp_hdr *) f->transport_hdr;
       temp.out_port = short_be(tcp_hdr->trans.dport);
@@ -217,7 +218,7 @@
 	  if(ipv4_hdr->proto == PICO_PROTO_ICMP4)
 	  {
 		  icmp_hdr = (struct pico_icmp4_hdr *) f->transport_hdr;
-		  if(icmp_hdr->type == PICO_ICMP_UNREACH && icmp_hdr->code == PICO_ICMP_UNREACH_FILTER_PROHIB)
+		  if(icmp_hdr->type == PICO_ICMP_UNREACH && icmp_hdr->type == PICO_ICMP_UNREACH_FILTER_PROHIB)
 			  return 0;
 	  }
 	  temp.out_port = temp.in_port = 0;
--- a/modules/pico_ipfilter.h	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_ipfilter.h	Thu Dec 05 08:31:32 2013 +0000
@@ -10,7 +10,7 @@
 #include "pico_device.h"
 
 enum filter_action {
-  FILTER_PRIORITY,
+  FILTER_PRIORITY=0,
   FILTER_REJECT,
   FILTER_DROP,
   FILTER_COUNT
--- a/modules/pico_mbed.cpp	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_mbed.cpp	Thu Dec 05 08:31:32 2013 +0000
@@ -3,6 +3,10 @@
 
 extern "C" {
 #include "pico_config.h"
+
+    uint64_t local_time;
+    uint32_t last_os_time;
+
     void *pico_mutex_init(void)
     {
         return new Mutex();
--- a/modules/pico_nat.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_nat.c	Thu Dec 05 08:31:32 2013 +0000
@@ -268,7 +268,7 @@
   return 0;
 }
 
-static void pico_ipv4_nat_table_cleanup(uint32_t now, void *_unused)
+static void pico_ipv4_nat_table_cleanup(pico_time now, void *_unused)
 {
   struct pico_tree_node *index = NULL, *_tmp = NULL;
   struct pico_nat_tuple *t = NULL;
--- a/modules/pico_olsr.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_olsr.c	Thu Dec 05 08:31:32 2013 +0000
@@ -368,7 +368,7 @@
 
 static uint32_t buffer_mem_used = 0U;
 
-void olsr_process_out(uint32_t now, void *arg)
+void olsr_process_out(pico_time now, void *arg)
 {
   struct olsr_fwd_pkt *p = (struct olsr_fwd_pkt *)arg;
   struct pico_ip4 bcast;
@@ -924,7 +924,7 @@
   pico_free(recvbuf);
 }
 
-static void olsr_hello_tick(uint32_t when, void *unused)
+static void olsr_hello_tick(pico_time when, void *unused)
 {
   struct olsr_dev_entry *d;
   (void)when;
@@ -939,7 +939,7 @@
   pico_timer_add(OLSR_HELLO_INTERVAL, &olsr_hello_tick, NULL);
 }
 
-static void olsr_tc_tick(uint32_t when, void *unused)
+static void olsr_tc_tick(pico_time when, void *unused)
 {
   struct olsr_dev_entry *d;
   (void)when;
--- a/modules/pico_slaacv4.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_slaacv4.c	Thu Dec 05 08:31:32 2013 +0000
@@ -83,7 +83,7 @@
   tmp->timer = NULL;
 }
 
-static void pico_slaacv4_send_announce_timer(uint32_t __attribute__((unused)) now, void *arg)
+static void pico_slaacv4_send_announce_timer(pico_time __attribute__((unused)) now, void *arg)
 {
   struct slaacv4_cookie *tmp = (struct slaacv4_cookie *)arg;
   struct pico_ip4 netmask = {.addr = 0x0000FFFF};
@@ -103,7 +103,7 @@
   }
 }
 
-static void pico_slaacv4_send_probe_timer(uint32_t __attribute__((unused)) now, void *arg)
+static void pico_slaacv4_send_probe_timer(pico_time __attribute__((unused)) now, void *arg)
 {
 
   struct slaacv4_cookie *tmp = (struct slaacv4_cookie *)arg;
--- a/modules/pico_tcp.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/modules/pico_tcp.c	Thu Dec 05 08:31:32 2013 +0000
@@ -150,8 +150,8 @@
 	}
 	else
 	{
-	  struct tcp_input_segment dummy={.seq=long_be(seq)};
-	  // TODO: Check if long_be(seq) is needed
+	  struct tcp_input_segment dummy={.seq=seq};
+
 	  return pico_tree_findKey(&tq->pool,&dummy);
 	}
 
@@ -336,7 +336,7 @@
   return ret;
 }
 
-static int release_all_until(struct pico_tcp_queue *q, uint32_t seq,uint32_t * timestamp)
+static int release_all_until(struct pico_tcp_queue *q, uint32_t seq,pico_time * timestamp)
 {
   void *f = NULL, *tmp __attribute__((unused));
   struct pico_tree_node * idx, * temp;
@@ -440,7 +440,7 @@
 
 static void tcp_add_options(struct pico_socket_tcp *ts, struct pico_frame *f, uint16_t flags, uint16_t optsiz)
 {
-  uint32_t tsval = long_be(pico_tick);
+  uint32_t tsval = long_be((uint32_t)pico_tick);
   uint32_t tsecr = long_be(ts->ts_nxt);
   uint32_t i = 0;
   f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
@@ -460,7 +460,7 @@
   f->start[i++] = PICO_TCPOPTLEN_WS;
   f->start[i++] = (uint8_t)(ts->wnd_scale);
 
-  if (optsiz >= 12) {
+  if ((flags & PICO_TCP_SYN) || ts->ts_ok) {
     f->start[i++] = PICO_TCP_OPTION_TIMESTAMP;
     f->start[i++] = PICO_TCPOPTLEN_TIMESTAMP;
     memcpy(f->start + i, &tsval, 4);
@@ -854,7 +854,7 @@
 }
 
 int pico_tcp_initconn(struct pico_socket *s);
-static void initconn_retry(uint32_t when, void *arg)
+static void initconn_retry(pico_time when, void *arg)
 {
   struct pico_socket_tcp *t = (struct pico_socket_tcp *)arg;
   IGNORE_PARAMETER(when);
@@ -1209,7 +1209,7 @@
       pkt = next_segment(&t->tcpq_in, pkt);
       continue;
     }
-    if(pkt->seq == (right + 1)) {
+    if(pkt->seq == right) {
       right += pkt->payload_len;
       pkt = next_segment(&t->tcpq_in, pkt);
       continue;
@@ -1256,20 +1256,25 @@
         nxt = peek_segment(&t->tcpq_in, t->rcv_nxt);
         while(nxt) {
           tcp_dbg("scrolling rcv_nxt...%08x\n", t->rcv_nxt);
-          t->rcv_nxt += f->payload_len;
+          t->rcv_nxt += nxt->payload_len;
           nxt = peek_segment(&t->tcpq_in, t->rcv_nxt);
         }
         t->sock.ev_pending |= PICO_SOCK_EV_RD;
-        t->rcv_nxt = SEQN(f) + f->payload_len;
       } else {
         tcp_dbg("TCP> lo segment. Uninteresting retransmission. (exp: %x got: %x)\n", t->rcv_nxt, SEQN(f));
       }
     } else {
       tcp_dbg("TCP> hi segment. Possible packet loss. I'll dupack this. (exp: %x got: %x)\n", t->rcv_nxt, SEQN(f));
       if (t->sack_ok) {
-        tcp_sack_prepare(t);
+    	struct tcp_input_segment * input = segment_from_frame(f);
+    	if(input && pico_enqueue_segment(&t->tcpq_in,input) <= 0){
+    		// failed to enqueue, destroy segment
+    		pico_free(input->payload);
+    		pico_free(input);
+    	}
+    	tcp_sack_prepare(t);
       }
-    }
+   }
     /* In either case, ack til recv_nxt. */
     if ( ((t->sock.state & PICO_SOCKET_STATE_TCP) != PICO_SOCKET_STATE_TCP_CLOSE_WAIT) && ((t->sock.state & PICO_SOCKET_STATE_TCP) != PICO_SOCKET_STATE_TCP_SYN_SENT) && ((t->sock.state & PICO_SOCKET_STATE_TCP) != PICO_SOCKET_STATE_TCP_SYN_RECV)) {
       //tcp_dbg("SENDACK CALLED FROM OUTSIDE tcp_synack, state %x\n",t->sock.state);
@@ -1284,7 +1289,7 @@
   }
 }
 
-static int tcp_ack_advance_una(struct pico_socket_tcp *t, struct pico_frame *f, uint32_t * timestamp)
+static int tcp_ack_advance_una(struct pico_socket_tcp *t, struct pico_frame *f, pico_time * timestamp)
 {
   int ret =  release_all_until(&t->tcpq_out, ACKN(f),timestamp);
   if (ret > 0)
@@ -1292,7 +1297,7 @@
   return ret;
 }
 
-static uint16_t time_diff(uint32_t a, uint32_t b)
+static uint16_t time_diff(pico_time a, pico_time b)
 {
   if (a >= b)
     return (uint16_t)(a - b);
@@ -1363,12 +1368,12 @@
   tcp_dbg("TCP_CWND, %lu, %u, %u, %u\n", pico_tick, t->cwnd, t->ssthresh, t->in_flight);
 }
 
-static void add_retransmission_timer(struct pico_socket_tcp *t, uint32_t next_ts);
-static void tcp_retrans_timeout(uint32_t val, void *sock)
+static void add_retransmission_timer(struct pico_socket_tcp *t, pico_time next_ts);
+static void tcp_retrans_timeout(pico_time val, void *sock)
 {
   struct pico_socket_tcp *t = (struct pico_socket_tcp *) sock;
   struct pico_frame *f = NULL;
-  uint32_t limit = val - t->rto;
+  pico_time limit = val - t->rto;
   if( t->sock.net && ((t->sock.state & 0xFF00) == PICO_SOCKET_STATE_TCP_ESTABLISHED
   		|| (t->sock.state & 0xFF00) == PICO_SOCKET_STATE_TCP_CLOSE_WAIT) && t->backoff < PICO_TCP_MAX_RETRANS)
   {
@@ -1423,7 +1428,7 @@
 	}
 }
 
-static void add_retransmission_timer(struct pico_socket_tcp *t, uint32_t next_ts)
+static void add_retransmission_timer(struct pico_socket_tcp *t, pico_time next_ts)
 {
   struct pico_tree_node * index;
 
@@ -1525,7 +1530,7 @@
   struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *) f->transport_hdr;
   uint32_t rtt = 0;
   uint16_t acked = 0;
-  uint32_t acked_timestamp = 0;
+  pico_time acked_timestamp = 0;
   uint8_t restart_tmr = 0;
 
   struct pico_frame *una = NULL;
@@ -1695,7 +1700,7 @@
   return 0;
 }
 
-static void tcp_deltcb(uint32_t when, void *arg)
+static void tcp_deltcb(pico_time when, void *arg)
 {
   struct pico_socket_tcp *t = (struct pico_socket_tcp *)arg;
   IGNORE_PARAMETER(when);
@@ -2140,7 +2145,7 @@
   return ret;
 }
 
-static void tcp_send_keepalive(uint32_t when, void *_t)
+static void tcp_send_keepalive(pico_time when, void *_t)
 {
   struct pico_socket_tcp *t = (struct pico_socket_tcp *)_t;
   IGNORE_PARAMETER(when);
--- a/stack/pico_arp.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/stack/pico_arp.c	Thu Dec 05 08:31:32 2013 +0000
@@ -28,7 +28,7 @@
 static struct pico_queue pending;
 static int pending_timer_on = 0;
 
-void check_pending(uint32_t now, void *_unused)
+void check_pending(pico_time now, void *_unused)
 {
   struct pico_frame *f = pico_dequeue(&pending);
   IGNORE_PARAMETER(now);
@@ -75,7 +75,7 @@
   struct pico_eth eth;
   struct pico_ip4 ipv4;
   int    arp_status;
-  uint32_t timestamp;
+  pico_time timestamp;
   struct pico_device *dev;
 };
 
@@ -179,7 +179,7 @@
 }
 #endif
 
-void arp_expire(uint32_t now, void *_stale)
+void arp_expire(pico_time now, void *_stale)
 {
   struct pico_arp *stale = (struct pico_arp *) _stale;
   IGNORE_PARAMETER(now);
--- a/stack/pico_socket.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/stack/pico_socket.c	Thu Dec 05 08:31:32 2013 +0000
@@ -612,10 +612,14 @@
     pico_tcp_cleanup_queues(sock);
 }
 
-static void socket_garbage_collect(uint32_t now, void *arg)
+static void socket_garbage_collect(pico_time now, void *arg)
 {
   struct pico_socket *s = (struct pico_socket *) arg;
   IGNORE_PARAMETER(now);
+
+  if(s->parent)
+	  s->parent->number_of_pending_conn--;
+
   socket_clean_queues(s);
   pico_free(s);
 }
@@ -2100,11 +2104,11 @@
 
 // checking for pending connections
   if(TCP_STATE(s) == PICO_SOCKET_STATE_TCP_SYN_RECV)
-    if((uint32_t)(PICO_TIME_MS() - s->timestamp) >= PICO_SOCKET_BOUND_TIMEOUT){
+    if((pico_time)(PICO_TIME_MS() - s->timestamp) >= PICO_SOCKET_BOUND_TIMEOUT){
 	  s->parent->number_of_pending_conn--;
 	  return -1;
 	}
-  if((uint32_t)(PICO_TIME_MS() - s->timestamp) >= PICO_SOCKET_TIMEOUT) {
+  if((pico_time)(PICO_TIME_MS() - s->timestamp) >= PICO_SOCKET_TIMEOUT) {
 	// checking for hanging sockets
 	if( (TCP_STATE(s) != PICO_SOCKET_STATE_TCP_LISTEN) && (TCP_STATE(s) != PICO_SOCKET_STATE_TCP_ESTABLISHED ) )
 	  return -1;
--- a/stack/pico_stack.c	Wed Dec 04 08:39:16 2013 +0000
+++ b/stack/pico_stack.c	Thu Dec 05 08:31:32 2013 +0000
@@ -34,7 +34,7 @@
   const uint8_t PICO_ETHADDR_MCAST[6] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
 #endif
 
-volatile uint32_t pico_tick;
+volatile pico_time pico_tick;
 volatile pico_err_t pico_err;
 
 static uint32_t _rand_seed;
@@ -466,12 +466,12 @@
 {
   uint32_t expire;
   void *arg;
-  void (*timer)(uint32_t timestamp, void *arg);
+  void (*timer)(pico_time timestamp, void *arg);
 };
 
 struct pico_timer_ref
 {
-  uint32_t expire;
+  pico_time expire;
   struct pico_timer *tmr;
 };
 
@@ -706,7 +706,7 @@
   }
 }
 
-struct pico_timer *pico_timer_add(uint32_t expire, void (*timer)(uint32_t, void *), void *arg)
+struct pico_timer *pico_timer_add(pico_time expire, void (*timer)(pico_time, void *), void *arg)
 {
   struct pico_timer *t = pico_zalloc(sizeof(struct pico_timer));
   struct pico_timer_ref tref;