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 Belgium NV
Date:
Mon Dec 16 11:25:54 2013 +0100
Parent:
130:0cee8bdac6d0
Child:
132:40ba3014da35
Commit message:
Syncronized with master branch

Changed in this revision

include/heap.h Show annotated file Show diff for this revision Revisions of this file
include/pico_addressing.h Show annotated file Show diff for this revision Revisions of this file
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
include/pico_device.h Show annotated file Show diff for this revision Revisions of this file
include/pico_eth.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_module_eth.h Show annotated file Show diff for this revision Revisions of this file
include/pico_protocol.h Show annotated file Show diff for this revision Revisions of this file
include/pico_queue.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
include/pico_tree.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_dev_loop.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_dev_loop.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_dhcp_client.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_dhcp_common.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_dhcp_common.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_dhcp_server.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_dhcp_server.h 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_dns_client.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_http_client.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_http_client.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_http_server.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_http_server.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_http_util.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_http_util.h 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_igmp.h 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_ipv4.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_ipv4.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_ipv6.h 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_nat.h 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_olsr.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_posix.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_simple_http.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_simple_http.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_slaacv4.h 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
modules/pico_udp.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_udp.h Show annotated file Show diff for this revision Revisions of this file
modules/pico_zmq.c Show annotated file Show diff for this revision Revisions of this file
modules/pico_zmq.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_frame.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
stack/pico_tree.c Show annotated file Show diff for this revision Revisions of this file
--- a/include/heap.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/heap.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,81 +1,81 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 
 #define DECLARE_HEAP(type, orderby) \
-struct heap_##type {   \
-  uint32_t size;   	\
-  uint32_t n;   	\
-  type *top;    	\
-}; \
-typedef struct heap_##type heap_##type; \
-static inline int heap_insert(struct heap_##type *heap, type *el) \
-{ \
-  uint32_t i; \
-  type * newTop; \
-  if (++heap->n >= heap->size) {    											\
-    newTop = pico_zalloc((heap->n + 1) * sizeof(type)); \
-    if(!newTop) \
-      return -1; \
-    if (heap->top)  {\
-      memcpy(newTop,heap->top,heap->n*sizeof(type)); \
-      pico_free(heap->top); \
+    struct heap_ ## type {   \
+        uint32_t size;    \
+        uint32_t n;       \
+        type *top;        \
+    }; \
+    typedef struct heap_ ## type heap_ ## type; \
+    static inline int heap_insert(struct heap_ ## type *heap, type * el) \
+    { \
+        uint32_t i; \
+        type *newTop; \
+        if (++heap->n >= heap->size) {                                                \
+            newTop = pico_zalloc((heap->n + 1) * sizeof(type)); \
+            if(!newTop) \
+                return -1; \
+            if (heap->top)  { \
+                memcpy(newTop, heap->top, heap->n * sizeof(type)); \
+                pico_free(heap->top); \
+            } \
+            heap->top = newTop;             \
+            heap->size++;                                                               \
+        }                                                                             \
+        if (heap->n == 1) {                                                       \
+            memcpy(&heap->top[1], el, sizeof(type));                                    \
+            return 0;                                                                   \
+        }                                                                             \
+        for (i = heap->n; ((i > 1) && (heap->top[i / 2].orderby > el->orderby)); i /= 2) {        \
+            memcpy(&heap->top[i], &heap->top[i / 2], sizeof(type));                     \
+        }             \
+        memcpy(&heap->top[i], el, sizeof(type));                                      \
+        return 0;                                                                     \
     } \
-    heap->top = newTop;				\
-    heap->size++;																\
-  }  																			\
-  if (heap->n == 1) {  														\
-    memcpy(&heap->top[1], el, sizeof(type));									\
-    return 0;																	\
-  }  																			\
-  for (i = heap->n; ((i > 1) && (heap->top[i / 2].orderby > el->orderby)); i /= 2) {  		\
-    memcpy(&heap->top[i], &heap->top[i / 2], sizeof(type));						\
-  }  			\
-  memcpy(&heap->top[i], el, sizeof(type));  									\
-  return 0;   																	\
-} \
-static inline int heap_peek(struct heap_##type *heap, type *first) \
-{ \
-  type *last;  			\
-  uint32_t i, child;  		\
-  if(heap->n == 0) {  	\
-    return -1; 			\
-  }  					\
-  memcpy(first, &heap->top[1], sizeof(type));  	\
-  last = &heap->top[heap->n--];  				\
-  for(i = 1; (i * 2) <= heap->n; i = child) {  	\
-    child = 2 * i;								\
-    if ((child != heap->n) && 					\
-    	(heap->top[child + 1]).orderby 			\
-    	< (heap->top[child]).orderby)			\
-    	child++;								\
-    if (last->orderby > 						\
-    	heap->top[child].orderby)				\
-    	memcpy(&heap->top[i], &heap->top[child],\
-    			sizeof(type));					\
-    else										\
-    	break;									\
-  }  											\
-  memcpy(&heap->top[i], last, sizeof(type));  	\
-  return 0;  									\
-} \
-static inline type *heap_first(heap_##type *heap)  \
-{ \
-  if (heap->n == 0)  	\
-    return NULL;		\
-  return &heap->top[1];  \
-} \
-static inline heap_##type *heap_init(void) \
-{ \
-  heap_##type *p = (heap_##type *)pico_zalloc(sizeof(heap_##type));  \
-  return p;  	\
-} \
-static inline void heap_destroy(heap_##type *h) \
-{ \
-  pico_free(h->top);   \
-  pico_free(h);   	\
-} \
+    static inline int heap_peek(struct heap_ ## type *heap, type * first) \
+    { \
+        type *last;           \
+        uint32_t i, child;        \
+        if(heap->n == 0) {    \
+            return -1;          \
+        }                     \
+        memcpy(first, &heap->top[1], sizeof(type));   \
+        last = &heap->top[heap->n--];                 \
+        for(i = 1; (i * 2) <= heap->n; i = child) {   \
+            child = 2 * i;                              \
+            if ((child != heap->n) &&                   \
+                (heap->top[child + 1]).orderby          \
+                < (heap->top[child]).orderby)           \
+                child++;                                \
+            if (last->orderby >                         \
+                heap->top[child].orderby)               \
+                memcpy(&heap->top[i], &heap->top[child], \
+                       sizeof(type));                  \
+            else                                        \
+                break;                                  \
+        }                                             \
+        memcpy(&heap->top[i], last, sizeof(type));    \
+        return 0;                                     \
+    } \
+    static inline type *heap_first(heap_ ## type * heap)  \
+    { \
+        if (heap->n == 0)     \
+            return NULL;        \
+        return &heap->top[1];  \
+    } \
+    static inline heap_ ## type *heap_init(void) \
+    { \
+        heap_ ## type * p = (heap_ ## type *)pico_zalloc(sizeof(heap_ ## type));  \
+        return p;     \
+    } \
+    static inline void heap_destroy(heap_ ## type * h) \
+    { \
+        pico_free(h->top);   \
+        pico_free(h);     \
+    } \
 
 
--- a/include/pico_addressing.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_addressing.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,8 +1,8 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_ADDRESSING
 #define _INCLUDE_PICO_ADDRESSING
 #include <stdint.h>
@@ -10,21 +10,21 @@
 
 struct pico_ip4
 {
-  uint32_t     addr;
+    uint32_t addr;
 };
 #define PICO_SIZE_IP4 4
 
 
 struct pico_ip6
 {
-  uint8_t addr[16];
+    uint8_t addr[16];
 };
 #define PICO_SIZE_IP6 16
 
 struct pico_eth
 {
-  uint8_t addr[6];
-  uint8_t padding[2];
+    uint8_t addr[6];
+    uint8_t padding[2];
 };
 #define PICO_SIZE_ETH 6
 
@@ -33,8 +33,8 @@
 
 struct pico_trans
 {
-  uint16_t sport;
-  uint16_t dport;
+    uint16_t sport;
+    uint16_t dport;
 
 };
 #define PICO_SIZE_TRANS 8
--- a/include/pico_arp.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_arp.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,8 +1,8 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_ARP
 #define _INCLUDE_PICO_ARP
 #include "pico_eth.h"
@@ -24,7 +24,7 @@
 
 struct pico_eth *pico_arp_lookup(struct pico_ip4 *dst);
 struct pico_ip4 *pico_arp_reverse_lookup(struct pico_eth *dst);
-int pico_arp_create_entry(uint8_t* hwaddr, struct pico_ip4 ipv4, struct pico_device* dev);
+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_register_ipconflict(struct pico_ip4 *ip, struct pico_eth *mac, void (*cb)(void));
 #endif
--- a/include/pico_constants.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_constants.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,8 +1,8 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_CONST
 #define _INCLUDE_PICO_CONST
 /* Included from pico_config.h */
@@ -26,48 +26,48 @@
 
 static inline uint16_t short_from(void *_p)
 {
-  unsigned char *p = (unsigned char *)_p;
-  uint16_t r, p0, p1;
-  p0 = p[0];
-  p1 = p[1];
-  r = (p0 << 8) + p1;
-  return r;
+    unsigned char *p = (unsigned char *)_p;
+    uint16_t r, p0, p1;
+    p0 = p[0];
+    p1 = p[1];
+    r = (p0 << 8) + p1;
+    return r;
 }
 
 static inline uint32_t long_from(void *_p)
 {
-  unsigned char *p = (unsigned char *)_p;
-  uint32_t r, p0, p1, p2, p3;
-  p0 = p[0];
-  p1 = p[1];
-  p2 = p[2];
-  p3 = p[3];
-  r = (p0 << 24) + (p1 << 16) + (p2 << 8) + p3;
-  return r;
+    unsigned char *p = (unsigned char *)_p;
+    uint32_t r, p0, p1, p2, p3;
+    p0 = p[0];
+    p1 = p[1];
+    p2 = p[2];
+    p3 = p[3];
+    r = (p0 << 24) + (p1 << 16) + (p2 << 8) + p3;
+    return r;
 }
 
 #else
 
 static inline uint16_t short_from(void *_p)
 {
-  unsigned char *p = (unsigned char *)_p;
-  uint16_t r, _p0, _p1;
-  _p0 = p[0];
-  _p1 = p[1];
-  r = (uint16_t)((_p1 << 8u) + _p0);
-  return r;
+    unsigned char *p = (unsigned char *)_p;
+    uint16_t r, _p0, _p1;
+    _p0 = p[0];
+    _p1 = p[1];
+    r = (uint16_t)((_p1 << 8u) + _p0);
+    return r;
 }
 
 static inline uint32_t long_from(void *_p)
 {
-  unsigned char *p = (unsigned char *)_p;
-  uint32_t r, _p0, _p1, _p2, _p3;
-  _p0 = p[0];
-  _p1 = p[1];
-  _p2 = p[2];
-  _p3 = p[3];
-  r = (_p3 << 24) + (_p2 << 16) + (_p1 << 8) + _p0;
-  return r;
+    unsigned char *p = (unsigned char *)_p;
+    uint32_t r, _p0, _p1, _p2, _p3;
+    _p0 = p[0];
+    _p1 = p[1];
+    _p2 = p[2];
+    _p3 = p[3];
+    r = (_p3 << 24) + (_p2 << 16) + (_p1 << 8) + _p0;
+    return r;
 }
 
 
@@ -81,34 +81,34 @@
 
 static inline uint16_t short_be(uint16_t le)
 {
-  return (uint16_t)(((le & 0xFFu) << 8) | ((le >> 8u) & 0xFFu));
+    return (uint16_t)(((le & 0xFFu) << 8) | ((le >> 8u) & 0xFFu));
 }
 
 static inline uint32_t long_be(uint32_t le)
 {
-  uint8_t *b = (uint8_t *)&le;
-  uint32_t be = 0;
-  uint32_t b0, b1, b2;
-  b0 = b[0];
-  b1 = b[1];
-  b2 = b[2];
-  be = b[3] + (b2 << 8) + (b1 << 16) + (b0 << 24);
-  return be;
+    uint8_t *b = (uint8_t *)&le;
+    uint32_t be = 0;
+    uint32_t b0, b1, b2;
+    b0 = b[0];
+    b1 = b[1];
+    b2 = b[2];
+    be = b[3] + (b2 << 8) + (b1 << 16) + (b0 << 24);
+    return be;
 }
 static inline uint64_t long_long_be(uint64_t le)
 {
-  uint8_t *b = (uint8_t *)&le;
-  uint64_t be = 0;
-  uint64_t b0, b1, b2, b3, b4, b5, b6;
-  b0 = b[0];
-  b1 = b[1];
-  b2 = b[2];
-  b3 = b[3];
-  b4 = b[4];
-  b5 = b[5];
-  b6 = b[6];
-  be = b[7] + (b6 << 8) + (b5 << 16) + (b4 << 24) + (b3 << 32) + (b2 << 40) + (b1 << 48) + (b0 << 56);
-  return be;
+    uint8_t *b = (uint8_t *)&le;
+    uint64_t be = 0;
+    uint64_t b0, b1, b2, b3, b4, b5, b6;
+    b0 = b[0];
+    b1 = b[1];
+    b2 = b[2];
+    b3 = b[3];
+    b4 = b[4];
+    b5 = b[5];
+    b6 = b[6];
+    be = b[7] + (b6 << 8) + (b5 << 16) + (b4 << 24) + (b3 << 32) + (b2 << 40) + (b1 << 48) + (b0 << 56);
+    return be;
 }
 #endif
 
@@ -124,14 +124,14 @@
 
 static inline uint32_t pico_hash(const char *name)
 {
-  uint32_t hash = 5381;
-  uint8_t c;
-  while ((c = (uint8_t)*name++))
-    hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
-  return hash;
+    uint32_t hash = 5381;
+    uint8_t c;
+    while ((c = (uint8_t)*name++))
+        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
+    return hash;
 }
 
 /* Debug */
-//#define PICO_SUPPORT_DEBUG_MEMORY
-//#define PICO_SUPPORT_DEBUG_TOOLS
+/* #define PICO_SUPPORT_DEBUG_MEMORY */
+/* #define PICO_SUPPORT_DEBUG_TOOLS */
 #endif
--- a/include/pico_device.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_device.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
-#ifndef _INCLUDE_PICO_DEVICE 
-#define _INCLUDE_PICO_DEVICE 
+ *********************************************************************/
+#ifndef _INCLUDE_PICO_DEVICE
+#define _INCLUDE_PICO_DEVICE
 #include "pico_queue.h"
 #include "pico_frame.h"
 #include "pico_addressing.h"
@@ -13,29 +13,29 @@
 
 
 struct pico_ethdev {
-  struct pico_eth mac;
+    struct pico_eth mac;
 };
 
 struct pico_device {
-  char name[MAX_DEVICE_NAME];
-  uint32_t hash;
-  uint32_t overhead;
-  struct pico_ethdev *eth; /* Null if non-ethernet */
-  struct pico_queue *q_in;
-  struct pico_queue *q_out;
-  int (*send)(struct pico_device *self, void *buf, int len); /* Send function. Return 0 if busy */
-  int (*poll)(struct pico_device *self, int loop_score);
-  void(*destroy)(struct pico_device *self);
-  int (*dsr)(struct pico_device *self, int loop_score);
-  int __serving_interrupt;
-  // used to signal the upper layer the number of events arrived since the last processing
-  volatile int eventCnt;
+    char name[MAX_DEVICE_NAME];
+    uint32_t hash;
+    uint32_t overhead;
+    struct pico_ethdev *eth; /* Null if non-ethernet */
+    struct pico_queue *q_in;
+    struct pico_queue *q_out;
+    int (*send)(struct pico_device *self, void *buf, int len); /* Send function. Return 0 if busy */
+    int (*poll)(struct pico_device *self, int loop_score);
+    void (*destroy)(struct pico_device *self);
+    int (*dsr)(struct pico_device *self, int loop_score);
+    int __serving_interrupt;
+    /* used to signal the upper layer the number of events arrived since the last processing */
+    volatile int eventCnt;
 };
 
 int pico_device_init(struct pico_device *dev, const char *name, uint8_t *mac);
 void pico_device_destroy(struct pico_device *dev);
 int pico_devices_loop(int loop_score, int direction);
-struct pico_device* pico_get_device(const char* name);
-int32_t pico_device_broadcast(struct pico_frame * f);
+struct pico_device*pico_get_device(const char*name);
+int32_t pico_device_broadcast(struct pico_frame *f);
 
 #endif
--- a/include/pico_eth.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_eth.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,8 +1,8 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_ETH
 #define _INCLUDE_PICO_ETH
 #include "pico_addressing.h"
@@ -11,9 +11,9 @@
 
 
 struct __attribute__((packed)) pico_eth_hdr {
-  uint8_t   daddr[6];
-  uint8_t   saddr[6];
-  uint16_t  proto;
+    uint8_t daddr[6];
+    uint8_t saddr[6];
+    uint16_t proto;
 };
 
 #define PICO_SIZE_ETHHDR 14
--- a/include/pico_frame.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_frame.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,8 +1,8 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_FRAME
 #define _INCLUDE_PICO_FRAME
 #include "pico_config.h"
@@ -18,63 +18,64 @@
 
 struct pico_frame {
 
-  /* Connector for queues */
-  struct pico_frame *next;
+    /* Connector for queues */
+    struct pico_frame *next;
 
-  /* Start of the whole buffer, total frame length. */
-  unsigned char *buffer;
-  uint32_t      buffer_len;
+    /* Start of the whole buffer, total frame length. */
+    unsigned char *buffer;
+    uint32_t buffer_len;
 
-  /* For outgoing packets: this is the meaningful buffer. */
-  unsigned char *start;
-  uint32_t      len;
+    /* For outgoing packets: this is the meaningful buffer. */
+    unsigned char *start;
+    uint32_t len;
 
-  /* Pointer to usage counter */
-  uint32_t *usage_count;
+    /* Pointer to usage counter */
+    uint32_t *usage_count;
 
-  /* Pointer to protocol headers */
-  uint8_t *datalink_hdr;
+    /* Pointer to protocol headers */
+    uint8_t *datalink_hdr;
 
-  uint8_t *net_hdr;
-  uint16_t net_len;
-  uint8_t *transport_hdr;
-  uint16_t transport_len;
-  uint8_t *app_hdr;
-  uint16_t app_len;
+    uint8_t *net_hdr;
+    uint16_t net_len;
+    uint8_t *transport_hdr;
+    uint16_t transport_len;
+    uint8_t *app_hdr;
+    uint16_t app_len;
 
-  /* Pointer to the phisical device this packet belongs to.
-   * Should be valid in both routing directions
-   */
-  struct pico_device *dev;
+    /* Pointer to the phisical device this packet belongs to.
+     * Should be valid in both routing directions
+     */
+    struct pico_device *dev;
 
-  pico_time timestamp;
+    pico_time timestamp;
 
-  /* Failures due to bad datalink addressing. */
-  uint16_t failure_count;
+    /* Failures due to bad datalink addressing. */
+    uint16_t failure_count;
 
-  /* Protocol over IP */
-  uint8_t  proto;
+    /* Protocol over IP */
+    uint8_t proto;
 
-  /* PICO_FRAME_FLAG_* */
-  uint8_t flags;
+    /* PICO_FRAME_FLAG_* */
+    uint8_t flags;
 
-  /* Pointer to payload */
-  unsigned char *payload;
-  uint16_t payload_len;
+    /* Pointer to payload */
+    unsigned char *payload;
+    uint16_t payload_len;
 
 #ifdef PICO_SUPPORT_IPFRAG
-  /* Payload fragmentation info (big endian)*/
-  uint16_t frag;
+    /* Payload fragmentation info (big endian)*/
+    uint16_t frag;
 #endif
 
-  /* Pointer to socket */
-  struct pico_socket *sock;
+    /* Pointer to socket */
+    struct pico_socket *sock;
 
-  /* Pointer to transport info, used to store remote UDP duple (IP + port) */
-  void *info;
+    /* Pointer to transport info, used to store remote UDP duple (IP + port) */
+    void *info;
 
-  /*Priority. "best-effort" priority, the default value is 0. Priority can be in between -10 and +10*/
-  int8_t priority;
+    /*Priority. "best-effort" priority, the default value is 0. Priority can be in between -10 and +10*/
+    int8_t priority;
+    uint8_t transport_flags_saved;
 };
 
 /** frame alloc/dealloc/copy **/
@@ -87,9 +88,10 @@
 
 static inline int pico_is_digit(char c)
 {
-  if (c < '0' || c > '9')
-    return 0;
-  return 1;
+    if (c < '0' || c > '9')
+        return 0;
+
+    return 1;
 }
 
 #endif
--- a/include/pico_module_eth.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_module_eth.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,25 +1,25 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _PICO_MODULE_IPV4_H
 #define _PICO_MODULE_IPV4_H
 
 struct pico_arp_entry {
-  struct eth dest;
+    struct eth dest;
 #ifdef PICO_CONFIG_IPV4
-  struct ipv4 addr_ipv4;
+    struct ipv4 addr_ipv4;
 #endif
-  RB_ENTRY(pico_arp_entry) node;
+    RB_ENTRY(pico_arp_entry) node;
 };
 
 /* Configured device */
 struct pico_eth_link {
-  struct pico_device *dev;
-  struct eth address;
-  struct eth netmask;
-  RB_ENTRY(pico_eth_link) node;
+    struct pico_device *dev;
+    struct eth address;
+    struct eth netmask;
+    RB_ENTRY(pico_eth_link) node;
 };
 
 #ifndef IS_MODULE_ETH
--- a/include/pico_protocol.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_protocol.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
-#ifndef _INCLUDE_PICO_PROTOCOL 
-#define _INCLUDE_PICO_PROTOCOL 
+ *********************************************************************/
+#ifndef _INCLUDE_PICO_PROTOCOL
+#define _INCLUDE_PICO_PROTOCOL
 #include <stdint.h>
 #include "pico_queue.h"
 
@@ -12,54 +12,54 @@
 #define PICO_LOOP_DIR_OUT  2
 
 enum pico_layer {
-  PICO_LAYER_DATALINK = 2,  /* Ethernet only. */
-  PICO_LAYER_NETWORK = 3,   /* IPv4, IPv6, ARP. Arp is there because it communicates with L2 */
-  PICO_LAYER_TRANSPORT = 4, /* UDP, TCP, ICMP */
-  PICO_LAYER_SOCKET = 5     /* Socket management */
+    PICO_LAYER_DATALINK = 2, /* Ethernet only. */
+    PICO_LAYER_NETWORK = 3, /* IPv4, IPv6, ARP. Arp is there because it communicates with L2 */
+    PICO_LAYER_TRANSPORT = 4, /* UDP, TCP, ICMP */
+    PICO_LAYER_SOCKET = 5   /* Socket management */
 };
 
 enum pico_err_e {
-  PICO_ERR_NOERR = 0,
-  PICO_ERR_EPERM,
-  PICO_ERR_ENOENT,
-  /* ... */
-  PICO_ERR_EINTR = 4,
-  PICO_ERR_EIO,
-  PICO_ERR_ENXIO, 
-  /* ... */
-  PICO_ERR_EAGAIN = 11,
-  PICO_ERR_ENOMEM,
-  PICO_ERR_EACCESS,
-  PICO_ERR_EFAULT,
-  /* ... */
-  PICO_ERR_EBUSY = 16,
-  PICO_ERR_EEXIST = 17,
-  /* ... */
-  PICO_ERR_EINVAL = 22,
-  /* ... */
-  PICO_ERR_EPROTO = 71,
-  PICO_ERR_ENOPROTOOPT = 92,
-  PICO_ERR_EPROTONOSUPPORT = 93,
+    PICO_ERR_NOERR = 0,
+    PICO_ERR_EPERM,
+    PICO_ERR_ENOENT,
+    /* ... */
+    PICO_ERR_EINTR = 4,
+    PICO_ERR_EIO,
+    PICO_ERR_ENXIO,
+    /* ... */
+    PICO_ERR_EAGAIN = 11,
+    PICO_ERR_ENOMEM,
+    PICO_ERR_EACCESS,
+    PICO_ERR_EFAULT,
+    /* ... */
+    PICO_ERR_EBUSY = 16,
+    PICO_ERR_EEXIST = 17,
+    /* ... */
+    PICO_ERR_EINVAL = 22,
+    /* ... */
+    PICO_ERR_EPROTO = 71,
+    PICO_ERR_ENOPROTOOPT = 92,
+    PICO_ERR_EPROTONOSUPPORT = 93,
 
-  /* ... */
-  PICO_ERR_EADDRINUSE = 98,
-  PICO_ERR_EADDRNOTAVAIL,
-  PICO_ERR_ENETUNREACH,
+    /* ... */
+    PICO_ERR_EADDRINUSE = 98,
+    PICO_ERR_EADDRNOTAVAIL,
+    PICO_ERR_ENETUNREACH,
 
-  /* ... */
-  PICO_ERR_ECONNRESET = 104,
+    /* ... */
+    PICO_ERR_ECONNRESET = 104,
 
-  /* ... */
-  PICO_ERR_EISCONN = 106,
-  PICO_ERR_ENOTCONN,
-  PICO_ERR_ESHUTDOWN,
-  /* ... */
-  PICO_ERR_ETIMEDOUT = 110,
-  PICO_ERR_ECONNREFUSED = 111,
-  PICO_ERR_EHOSTDOWN,
-  PICO_ERR_EHOSTUNREACH,
-  /* ... */
-  PICO_ERR_EOPNOTSUPP = 122,
+    /* ... */
+    PICO_ERR_EISCONN = 106,
+    PICO_ERR_ENOTCONN,
+    PICO_ERR_ESHUTDOWN,
+    /* ... */
+    PICO_ERR_ETIMEDOUT = 110,
+    PICO_ERR_ECONNREFUSED = 111,
+    PICO_ERR_EHOSTDOWN,
+    PICO_ERR_EHOSTUNREACH,
+    /* ... */
+    PICO_ERR_EOPNOTSUPP = 122,
 
 };
 
@@ -72,16 +72,16 @@
 #define MAX_PROTOCOL_NAME 16
 
 struct pico_protocol {
-  const char name[MAX_PROTOCOL_NAME];
-  uint32_t hash;
-  const enum pico_layer layer;
-  const uint16_t proto_number;
-  struct pico_queue * const q_in;
-  struct pico_queue * const q_out;
-  struct pico_frame *(* const alloc)(struct pico_protocol *self, uint16_t size); /* Frame allocation. */
-  int (* const push) (struct pico_protocol *self, struct pico_frame *p);    /* Push function, for active outgoing pkts from above */
-  int (* const process_out)(struct pico_protocol *self, struct pico_frame *p); /* Send loop. */
-  int (* const process_in)(struct pico_protocol *self, struct pico_frame *p); /* Recv loop. */
+    const char name[MAX_PROTOCOL_NAME];
+    uint32_t hash;
+    const enum pico_layer layer;
+    const uint16_t proto_number;
+    struct pico_queue *const q_in;
+    struct pico_queue *const q_out;
+    struct pico_frame *(*const alloc)(struct pico_protocol *self, uint16_t size); /* Frame allocation. */
+    int (*const push) (struct pico_protocol *self, struct pico_frame *p);   /* Push function, for active outgoing pkts from above */
+    int (*const process_out) (struct pico_protocol *self, struct pico_frame *p); /* Send loop. */
+    int (*const process_in) (struct pico_protocol *self, struct pico_frame *p); /* Recv loop. */
 };
 
 int pico_protocols_loop(int loop_score);
--- a/include/pico_queue.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_queue.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,8 +1,8 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_QUEUE
 #define _INCLUDE_PICO_QUEUE
 #include <stdint.h>
@@ -16,123 +16,129 @@
 #endif
 
 struct pico_queue {
-  uint32_t frames;
-  uint32_t size;
-  uint32_t max_frames;
-  uint32_t max_size;
-  struct pico_frame *head;
-  struct pico_frame *tail;
+    uint32_t frames;
+    uint32_t size;
+    uint32_t max_frames;
+    uint32_t max_size;
+    struct pico_frame *head;
+    struct pico_frame *tail;
 #ifdef PICO_SUPPORT_MUTEX
-  void * mutex;
+    void *mutex;
 #endif
-  uint8_t shared;
-  uint16_t overhead;
+    uint8_t shared;
+    uint16_t overhead;
 };
 
 #ifdef PICO_SUPPORT_MUTEX
-#define LOCK(x) {\
-  if (x == NULL) \
-    x = pico_mutex_init(); \
-  pico_mutex_lock(x); \
+#define LOCK(x) { \
+        if (x == NULL) \
+            x = pico_mutex_init(); \
+        pico_mutex_lock(x); \
 }
-#define UNLOCK(x) pico_mutex_unlock(x);
+#define UNLOCK(x) pico_mutex_unlock(x)
 
-#else 
-#define LOCK(x) do{}while(0)
-#define UNLOCK(x) do{}while(0)
+#else
+#define LOCK(x) do {} while(0)
+#define UNLOCK(x) do {} while(0)
 #endif
 
 #ifdef PICO_SUPPORT_DEBUG_TOOLS
 static void debug_q(struct pico_queue *q)
 {
-  struct pico_frame *p = q->head;
-  dbg("%d: ", q->frames);
-  while(p) {
-    dbg("(%p)-->", p);
-    p = p->next;
-  }
-  dbg("X\n");
+    struct pico_frame *p = q->head;
+    dbg("%d: ", q->frames);
+    while(p) {
+        dbg("(%p)-->", p);
+        p = p->next;
+    }
+    dbg("X\n");
 }
 
 #else
 
-#define debug_q(x) do{}while(0)
+#define debug_q(x) do {} while(0)
 #endif
 
 static inline int32_t pico_enqueue(struct pico_queue *q, struct pico_frame *p)
 {
-  if ((q->max_frames) && (q->max_frames <= q->frames))
-    return -1;
-    
-  if ((Q_LIMIT) && (Q_LIMIT < p->buffer_len + q->size))
-    return -1;
-    
-  if ((q->max_size) && (q->max_size < (p->buffer_len + q->size)))
-    return -1;
+    if ((q->max_frames) && (q->max_frames <= q->frames))
+        return -1;
+
+    if ((Q_LIMIT) && (Q_LIMIT < p->buffer_len + q->size))
+        return -1;
 
-  if (q->shared)
-    LOCK(q->mutex);
+    if ((q->max_size) && (q->max_size < (p->buffer_len + q->size)))
+        return -1;
+
+    if (q->shared)
+        LOCK(q->mutex);
 
-  p->next = NULL;
-  if (!q->head) {
-    q->head = p;
-    q->tail = p;
-    q->size = 0;
-    q->frames = 0;
-  } else {
-    q->tail->next = p;
-    q->tail = p;
-  }
-  q->size += p->buffer_len + q->overhead;
-  q->frames++;
-  debug_q(q);
+    p->next = NULL;
+    if (!q->head) {
+        q->head = p;
+        q->tail = p;
+        q->size = 0;
+        q->frames = 0;
+    } else {
+        q->tail->next = p;
+        q->tail = p;
+    }
 
-  if (q->shared)
-    UNLOCK(q->mutex);
-  return (int32_t)q->size;
+    q->size += p->buffer_len + q->overhead;
+    q->frames++;
+    debug_q(q);
+
+    if (q->shared)
+        UNLOCK(q->mutex);
+
+    return (int32_t)q->size;
 }
 
 static inline struct pico_frame *pico_dequeue(struct pico_queue *q)
 {
-  struct pico_frame *p = q->head;
-  if (q->frames < 1)
-    return NULL;
-  if (q->shared)
-    LOCK(q->mutex);
+    struct pico_frame *p = q->head;
+    if (q->frames < 1)
+        return NULL;
+
+    if (q->shared)
+        LOCK(q->mutex);
 
-  q->head = p->next;
-  q->frames--;
-  q->size -= p->buffer_len -q->overhead;
-  if (q->head == NULL)
-    q->tail = NULL;
-  debug_q(q);
-  p->next = NULL;
-  if (q->shared)
-    UNLOCK(q->mutex);
-  return p;
+    q->head = p->next;
+    q->frames--;
+    q->size -= p->buffer_len - q->overhead;
+    if (q->head == NULL)
+        q->tail = NULL;
+
+    debug_q(q);
+    p->next = NULL;
+    if (q->shared)
+        UNLOCK(q->mutex);
+
+    return p;
 }
 
 static inline struct pico_frame *pico_queue_peek(struct pico_queue *q)
 {
-  struct pico_frame *p = q->head;
-  if (q->frames < 1)
-    return NULL;
-  debug_q(q);
-  return p;
+    struct pico_frame *p = q->head;
+    if (q->frames < 1)
+        return NULL;
+
+    debug_q(q);
+    return p;
 }
 
 static inline void pico_queue_empty(struct pico_queue *q)
 {
-  struct pico_frame *p = pico_dequeue(q);
-  while(p) {
-    pico_free(p);
-    p = pico_dequeue(q);
-  }
+    struct pico_frame *p = pico_dequeue(q);
+    while(p) {
+        pico_free(p);
+        p = pico_dequeue(q);
+    }
 }
 
 static inline void pico_queue_protect(struct pico_queue *q)
 {
-  q->shared = 1;
+    q->shared = 1;
 }
 
 #endif
--- a/include/pico_socket.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_socket.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,8 @@
-#define MBED
+/*********************************************************************
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-/*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
-
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_SOCKET
 #define _INCLUDE_PICO_SOCKET
 #include "pico_queue.h"
@@ -12,10 +10,10 @@
 #include "pico_config.h"
 #include "pico_protocol.h"
 
-#ifndef MBED
-	#define PICO_DEFAULT_SOCKETQ (128 * 1024)
+#ifdef __linux__
+    #define PICO_DEFAULT_SOCKETQ (128 * 1024) /* Linux host, so we want full throttle */
 #else
-	#define PICO_DEFAULT_SOCKETQ (4 * 1024)
+    #define PICO_DEFAULT_SOCKETQ (4 * 1024) /* seems like an acceptable default for small embedded systems */
 #endif
 
 #define PICO_SHUT_RD   1
@@ -24,71 +22,71 @@
 
 
 struct pico_socket {
-  struct pico_protocol *proto;
-  struct pico_protocol *net;
+    struct pico_protocol *proto;
+    struct pico_protocol *net;
 
-  union {
-    struct pico_ip4 ip4;
-    struct pico_ip6 ip6;
-  } local_addr;
+    union {
+        struct pico_ip4 ip4;
+        struct pico_ip6 ip6;
+    } local_addr;
 
-  union {
-    struct pico_ip4 ip4;
-    struct pico_ip6 ip6;
-  } remote_addr;
+    union {
+        struct pico_ip4 ip4;
+        struct pico_ip6 ip6;
+    } remote_addr;
 
-  uint16_t local_port;
-  uint16_t remote_port;
+    uint16_t local_port;
+    uint16_t remote_port;
 
-  struct pico_queue q_in;
-  struct pico_queue q_out;
+    struct pico_queue q_in;
+    struct pico_queue q_out;
 
-  void (*wakeup)(uint16_t ev, struct pico_socket *s);
+    void (*wakeup)(uint16_t ev, struct pico_socket *s);
 
 
 #ifdef PICO_SUPPORT_TCP
-  /* For the TCP backlog queue */
-  struct pico_socket *backlog;
-  struct pico_socket *next;
-  struct pico_socket *parent;
-  int max_backlog;
-  uint8_t number_of_pending_conn;
+    /* For the TCP backlog queue */
+    struct pico_socket *backlog;
+    struct pico_socket *next;
+    struct pico_socket *parent;
+    int max_backlog;
+    uint8_t number_of_pending_conn;
 #endif
 #ifdef PICO_SUPPORT_MCAST
-  struct pico_tree *MCASTListen;
+    struct pico_tree *MCASTListen;
 #endif
-  uint16_t ev_pending;
+    uint16_t ev_pending;
 
-  struct pico_device *dev;
+    struct pico_device *dev;
 
-  /* Private field. */
-  int id;
-  uint16_t state;
-  uint16_t opt_flags;
-  pico_time timestamp;
-  void *priv;
+    /* Private field. */
+    int id;
+    uint16_t state;
+    uint16_t opt_flags;
+    pico_time timestamp;
+    void *priv;
 };
 
 struct pico_remote_duple {
-  union {
-    struct pico_ip4 ip4;
-    struct pico_ip6 ip6;
-  } remote_addr;
+    union {
+        struct pico_ip4 ip4;
+        struct pico_ip6 ip6;
+    } remote_addr;
 
-  uint16_t remote_port;
+    uint16_t remote_port;
 };
 
 
 /* request struct for multicast socket opt */
 struct pico_ip_mreq {
-  struct pico_ip4   mcast_group_addr;
-  struct pico_ip4 mcast_link_addr;
+    struct pico_ip4 mcast_group_addr;
+    struct pico_ip4 mcast_link_addr;
 };
 
 struct pico_ip_mreq_source {
-  struct pico_ip4  mcast_group_addr;
-  struct pico_ip4 mcast_source_addr;
-  struct pico_ip4 mcast_link_addr;
+    struct pico_ip4 mcast_group_addr;
+    struct pico_ip4 mcast_source_addr;
+    struct pico_ip4 mcast_link_addr;
 };
 
 #define PICO_SOCKET_STATE_UNDEFINED       0x0000
@@ -135,8 +133,8 @@
 # define PICO_IP_DEFAULT_MULTICAST_TTL        1
 # define PICO_IP_DEFAULT_MULTICAST_LOOP       1
 
-#define PICO_SOCKET_TIMEOUT					  90000u //90 seconds
-#define PICO_SOCKET_BOUND_TIMEOUT             30000u //30 seconds
+#define PICO_SOCKET_TIMEOUT                   90000u /* 90 seconds */
+#define PICO_SOCKET_BOUND_TIMEOUT             30000u /* 30 seconds */
 
 #define PICO_SOCKET_SHUTDOWN_WRITE 0x01
 #define PICO_SOCKET_SHUTDOWN_READ  0x02
@@ -207,7 +205,7 @@
 
 /* Socket loop */
 int pico_sockets_loop(int loop_score);
-struct pico_socket* pico_sockets_find(uint16_t local,uint16_t remote);
+struct pico_socket*pico_sockets_find(uint16_t local, uint16_t remote);
 /* Port check */
 int pico_is_port_free(uint16_t proto, uint16_t port, void *addr, void *net);
 
--- a/include/pico_stack.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_stack.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,8 +1,8 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_STACK
 #define _INCLUDE_PICO_STACK
 #include "pico_config.h"
@@ -23,9 +23,9 @@
 /* interface towards ethernet */
 int32_t pico_network_receive(struct pico_frame *f);
 
-/* The pico_ethernet_receive() function is used by 
- * those devices supporting ETH in order to push packets up 
- * into the stack. 
+/* The pico_ethernet_receive() function is used by
+ * those devices supporting ETH in order to push packets up
+ * into the stack.
  */
 /* DATALINK LEVEL */
 int32_t pico_ethernet_receive(struct pico_frame *f);
--- a/include/pico_tree.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/include/pico_tree.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Author: Andrei Carp <andrei.carp@tass.be>
-*********************************************************************/
+   Author: Andrei Carp <andrei.carp@tass.be>
+ *********************************************************************/
 
 #ifndef __PICO_RBTREE_H__
 #define __PICO_RBTREE_H__
@@ -11,74 +11,74 @@
 #include <stdint.h>
 #include "pico_config.h"
 
-// This is used to declare a new tree, leaf root by default
-#define PICO_TREE_DECLARE(name,compareFunction) \
-	struct pico_tree name =\
-	{ \
-		&LEAF, \
-		compareFunction \
-	}
+/* This is used to declare a new tree, leaf root by default */
+#define PICO_TREE_DECLARE(name, compareFunction) \
+    struct pico_tree name = \
+    { \
+        &LEAF, \
+        compareFunction \
+    }
 
 struct pico_tree_node
 {
-  void* keyValue; // generic key
-  struct pico_tree_node* parent;
-  struct pico_tree_node* leftChild;
-  struct pico_tree_node* rightChild;
-  uint8_t color;
+    void*keyValue; /* generic key */
+    struct pico_tree_node*parent;
+    struct pico_tree_node*leftChild;
+    struct pico_tree_node*rightChild;
+    uint8_t color;
 };
 
 struct pico_tree
 {
-	struct pico_tree_node * root; // root of the tree
+    struct pico_tree_node *root;  /* root of the tree */
 
-	// this function directly provides the keys as parameters not the nodes.
-  int (*compare)(void* keyA, void* keyB);
+    /* this function directly provides the keys as parameters not the nodes. */
+    int (*compare)(void*keyA, void*keyB);
 };
 
-extern struct pico_tree_node LEAF; // generic leaf node
+extern struct pico_tree_node LEAF; /* generic leaf node */
 /*
  * Manipulation functions
  */
-void * pico_tree_insert(struct pico_tree * tree, void * key);
-void * pico_tree_delete(struct pico_tree * tree, void * key);
-void * pico_tree_findKey(struct pico_tree * tree, void * key);
-void 	pico_tree_drop(struct pico_tree * tree);
-int 	pico_tree_empty(struct pico_tree * tree);
-struct pico_tree_node * pico_tree_findNode(struct pico_tree * tree, void * key);
+void *pico_tree_insert(struct pico_tree *tree, void *key);
+void *pico_tree_delete(struct pico_tree *tree, void *key);
+void *pico_tree_findKey(struct pico_tree *tree, void *key);
+void    pico_tree_drop(struct pico_tree *tree);
+int     pico_tree_empty(struct pico_tree *tree);
+struct pico_tree_node *pico_tree_findNode(struct pico_tree *tree, void *key);
 
-void * pico_tree_first(struct pico_tree * tree);
-void * pico_tree_last(struct pico_tree * tree);
+void *pico_tree_first(struct pico_tree *tree);
+void *pico_tree_last(struct pico_tree *tree);
 /*
  * Traverse functions
  */
-struct pico_tree_node * pico_tree_lastNode(struct pico_tree_node * node);
-struct pico_tree_node * pico_tree_firstNode(struct pico_tree_node * node);
-struct pico_tree_node * pico_tree_next(struct pico_tree_node * node);
-struct pico_tree_node * pico_tree_prev(struct pico_tree_node * node);
+struct pico_tree_node *pico_tree_lastNode(struct pico_tree_node *node);
+struct pico_tree_node *pico_tree_firstNode(struct pico_tree_node *node);
+struct pico_tree_node *pico_tree_next(struct pico_tree_node *node);
+struct pico_tree_node *pico_tree_prev(struct pico_tree_node *node);
 
 /*
  * For each macros
  */
 
-#define pico_tree_foreach(idx,tree) \
-		for ((idx) = pico_tree_firstNode((tree)->root); \
-		     (idx) != &LEAF; \
-		     (idx) = pico_tree_next(idx))
+#define pico_tree_foreach(idx, tree) \
+    for ((idx) = pico_tree_firstNode((tree)->root); \
+         (idx) != &LEAF; \
+         (idx) = pico_tree_next(idx))
 
-#define pico_tree_foreach_reverse(idx,tree) \
-		for ((idx) = pico_tree_lastNode((tree)->root); \
-		     (idx) != &LEAF; \
-		     (idx) = pico_tree_prev(idx))
+#define pico_tree_foreach_reverse(idx, tree) \
+    for ((idx) = pico_tree_lastNode((tree)->root); \
+         (idx) != &LEAF; \
+         (idx) = pico_tree_prev(idx))
 
-#define pico_tree_foreach_safe(idx,tree,idx2) \
-		for ((idx) = pico_tree_firstNode((tree)->root);	\
-		     ((idx) != &LEAF) && ((idx2) = pico_tree_next(idx), 1); \
-		      (idx) = (idx2))
+#define pico_tree_foreach_safe(idx, tree, idx2) \
+    for ((idx) = pico_tree_firstNode((tree)->root); \
+         ((idx) != &LEAF) && ((idx2) = pico_tree_next(idx), 1); \
+         (idx) = (idx2))
 
-#define pico_tree_foreach_reverse_safe(idx,tree,idx2) \
-		for ((idx) = pico_tree_lastNode((tree)->root); \
-				((idx) != &LEAF) && ((idx2) = pico_tree_prev(idx), 1); \
-				 (idx) = (idx2))
+#define pico_tree_foreach_reverse_safe(idx, tree, idx2) \
+    for ((idx) = pico_tree_lastNode((tree)->root); \
+         ((idx) != &LEAF) && ((idx2) = pico_tree_prev(idx), 1); \
+         (idx) = (idx2))
 
 #endif
--- a/modules/pico_dev_loop.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_dev_loop.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 
 #include "pico_device.h"
@@ -13,58 +13,61 @@
 
 #define LOOP_MTU 1500
 static uint8_t l_buf[LOOP_MTU];
-static uint32_t l_bufsize = 0;
+static int l_bufsize = 0;
 
 
 static int pico_loop_send(struct pico_device *dev, void *buf, int len)
 {
-	IGNORE_PARAMETER(dev);
-	if (len > LOOP_MTU)
-    return 0;
+    IGNORE_PARAMETER(dev);
+    if (len > LOOP_MTU)
+        return 0;
 
-  if (l_bufsize == 0) {
-    memcpy(l_buf, buf, len);
-    l_bufsize+=len;
-    return len;
-  }
-  return 0;
+    if (l_bufsize == 0) {
+        memcpy(l_buf, buf, (size_t)len);
+        l_bufsize += len;
+        return len;
+    }
+
+    return 0;
 }
 
 static int pico_loop_poll(struct pico_device *dev, int loop_score)
 {
-  if (loop_score <= 0)
-    return 0;
+    if (loop_score <= 0)
+        return 0;
 
-  if (l_bufsize > 0) {
-    pico_stack_recv(dev, l_buf, l_bufsize);
-    l_bufsize = 0;
-    loop_score--;
-  }
-  return loop_score;
+    if (l_bufsize > 0) {
+        pico_stack_recv(dev, l_buf, (uint32_t)l_bufsize);
+        l_bufsize = 0;
+        loop_score--;
+    }
+
+    return loop_score;
 }
 
 /* Public interface: create/destroy. */
 
 void pico_loop_destroy(struct pico_device *dev)
 {
-	IGNORE_PARAMETER(dev);
+    IGNORE_PARAMETER(dev);
 }
 
 struct pico_device *pico_loop_create(void)
 {
-  struct pico_device *loop = pico_zalloc(sizeof(struct pico_device));
-  if (!loop)
-    return NULL;
+    struct pico_device *loop = pico_zalloc(sizeof(struct pico_device));
+    if (!loop)
+        return NULL;
 
-  if( 0 != pico_device_init((struct pico_device *)loop, "loop", NULL)) {
-    dbg ("Loop init failed.\n");
-    pico_loop_destroy((struct pico_device *)loop);
-    return NULL;
-  }
-  loop->send = pico_loop_send;
-  loop->poll = pico_loop_poll;
-  loop->destroy = pico_loop_destroy;
-  dbg("Device %s created.\n", loop->name);
-  return (struct pico_device *)loop;
+    if( 0 != pico_device_init((struct pico_device *)loop, "loop", NULL)) {
+        dbg ("Loop init failed.\n");
+        pico_loop_destroy((struct pico_device *)loop);
+        return NULL;
+    }
+
+    loop->send = pico_loop_send;
+    loop->poll = pico_loop_poll;
+    loop->destroy = pico_loop_destroy;
+    dbg("Device %s created.\n", loop->name);
+    return (struct pico_device *)loop;
 }
 
--- a/modules/pico_dev_loop.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_dev_loop.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,8 +1,8 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_LOOP
 #define _INCLUDE_PICO_LOOP
 #include "pico_config.h"
--- a/modules/pico_dhcp_client.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_dhcp_client.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Authors: Kristof Roelants, Frederik Van Slycken 
-*********************************************************************/
+   Authors: Kristof Roelants, Frederik Van Slycken
+ *********************************************************************/
 
 
 #include "pico_dhcp_client.h"
@@ -15,8 +15,8 @@
 #include "pico_eth.h"
 
 #ifdef PICO_SUPPORT_DHCPC
-#define dhcpc_dbg(...) do{}while(0)
-//#define dhcpc_dbg dbg
+#define dhcpc_dbg(...) do {} while(0)
+/* #define dhcpc_dbg dbg */
 
 /* timer values */
 #define DHCP_CLIENT_REINIT             3000 /* msec */
@@ -39,49 +39,49 @@
 
 /* serialize client negotiations if multiple devices */
 /* NOTE: ONLY initialization is serialized! */
-static uint8_t pico_dhcp_client_mutex = 1; 
+static uint8_t pico_dhcp_client_mutex = 1;
 
 enum dhcp_client_state {
-  DHCP_CLIENT_STATE_INIT_REBOOT = 0,
-  DHCP_CLIENT_STATE_REBOOTING,
-  DHCP_CLIENT_STATE_INIT,
-  DHCP_CLIENT_STATE_SELECTING,
-  DHCP_CLIENT_STATE_REQUESTING,
-  DHCP_CLIENT_STATE_BOUND,
-  DHCP_CLIENT_STATE_RENEWING,
-  DHCP_CLIENT_STATE_REBINDING
+    DHCP_CLIENT_STATE_INIT_REBOOT = 0,
+    DHCP_CLIENT_STATE_REBOOTING,
+    DHCP_CLIENT_STATE_INIT,
+    DHCP_CLIENT_STATE_SELECTING,
+    DHCP_CLIENT_STATE_REQUESTING,
+    DHCP_CLIENT_STATE_BOUND,
+    DHCP_CLIENT_STATE_RENEWING,
+    DHCP_CLIENT_STATE_REBINDING
 };
 
 struct dhcp_client_timer
 {
-  uint8_t state;
-  uint32_t time;
+    uint8_t state;
+    uint32_t time;
 };
 
 struct pico_dhcp_client_cookie
 {
-  uint8_t status;
-  uint8_t event;
-  uint8_t retry;
-  uint32_t xid;
-  uint32_t *uid;
-  enum dhcp_client_state state;
-  void (*cb)(void* dhcpc, int code);
-  pico_time init_timestamp;
-  struct pico_socket *s;
-  struct pico_ip4 address;
-  struct pico_ip4 netmask;
-  struct pico_ip4 gateway;
-  struct pico_ip4 nameserver;
-  struct pico_ip4 server_id;
-  struct pico_device *dev;
-  struct dhcp_client_timer init_timer;
-  struct dhcp_client_timer requesting_timer;
-  struct dhcp_client_timer renewing_timer;
-  struct dhcp_client_timer rebinding_timer;
-  struct dhcp_client_timer T1_timer;
-  struct dhcp_client_timer T2_timer;
-  struct dhcp_client_timer lease_timer;
+    uint8_t status;
+    uint8_t event;
+    uint8_t retry;
+    uint32_t xid;
+    uint32_t *uid;
+    enum dhcp_client_state state;
+    void (*cb)(void*dhcpc, int code);
+    pico_time init_timestamp;
+    struct pico_socket *s;
+    struct pico_ip4 address;
+    struct pico_ip4 netmask;
+    struct pico_ip4 gateway;
+    struct pico_ip4 nameserver;
+    struct pico_ip4 server_id;
+    struct pico_device *dev;
+    struct dhcp_client_timer init_timer;
+    struct dhcp_client_timer requesting_timer;
+    struct dhcp_client_timer renewing_timer;
+    struct dhcp_client_timer rebinding_timer;
+    struct dhcp_client_timer T1_timer;
+    struct dhcp_client_timer T2_timer;
+    struct dhcp_client_timer lease_timer;
 };
 
 static int pico_dhcp_client_init(struct pico_dhcp_client_cookie *dhcpc);
@@ -92,628 +92,668 @@
 
 static int dhcp_cookies_cmp(void *ka, void *kb)
 {
-  struct pico_dhcp_client_cookie *a = ka, *b = kb;
-  if (a->xid == b->xid)
-    return 0;
-  return (a->xid < b->xid) ? -1 : 1;
-} 
+    struct pico_dhcp_client_cookie *a = ka, *b = kb;
+    if (a->xid == b->xid)
+        return 0;
+
+    return (a->xid < b->xid) ? -1 : 1;
+}
 PICO_TREE_DECLARE(DHCPCookies, dhcp_cookies_cmp);
 
 static struct pico_dhcp_client_cookie *pico_dhcp_client_add_cookie(uint32_t xid, struct pico_device *dev, void (*cb)(void *dhcpc, int code), uint32_t *uid)
 {
-  struct pico_dhcp_client_cookie *dhcpc = NULL, *found = NULL, test = {0};
-  
-  test.xid = xid;
-  found = pico_tree_findKey(&DHCPCookies, &test);
-  if (found) {
-    pico_err = PICO_ERR_EAGAIN;
-    return NULL;
-  }
+    struct pico_dhcp_client_cookie *dhcpc = NULL, *found = NULL, test = {
+        0
+    };
+
+    test.xid = xid;
+    found = pico_tree_findKey(&DHCPCookies, &test);
+    if (found) {
+        pico_err = PICO_ERR_EAGAIN;
+        return NULL;
+    }
 
-  dhcpc = pico_zalloc(sizeof(struct pico_dhcp_client_cookie));
-  if (!dhcpc) {
-    pico_err = PICO_ERR_ENOMEM;
-    return NULL;
-  }
+    dhcpc = pico_zalloc(sizeof(struct pico_dhcp_client_cookie));
+    if (!dhcpc) {
+        pico_err = PICO_ERR_ENOMEM;
+        return NULL;
+    }
 
-  dhcpc->state = DHCP_CLIENT_STATE_INIT;
-  dhcpc->status = DHCP_CLIENT_STATUS_INIT;
-  dhcpc->xid = xid;
-  dhcpc->uid = uid;
-  *(dhcpc->uid) = 0;
-  dhcpc->cb = cb;
-  dhcpc->dev = dev;
+    dhcpc->state = DHCP_CLIENT_STATE_INIT;
+    dhcpc->status = DHCP_CLIENT_STATUS_INIT;
+    dhcpc->xid = xid;
+    dhcpc->uid = uid;
+    *(dhcpc->uid) = 0;
+    dhcpc->cb = cb;
+    dhcpc->dev = dev;
 
-  pico_tree_insert(&DHCPCookies, dhcpc);
-  return dhcpc;
+    pico_tree_insert(&DHCPCookies, dhcpc);
+    return dhcpc;
 }
 
 static int pico_dhcp_client_del_cookie(uint32_t xid)
 {
-  struct pico_dhcp_client_cookie test = {0}, *found = NULL;
+    struct pico_dhcp_client_cookie test = {
+        0
+    }, *found = NULL;
 
-  test.xid = xid;
-  found = pico_tree_findKey(&DHCPCookies, &test);
-  if (!found)
-    return -1;
+    test.xid = xid;
+    found = pico_tree_findKey(&DHCPCookies, &test);
+    if (!found)
+        return -1;
 
-  pico_socket_close(found->s);
-  pico_ipv4_link_del(found->dev, found->address);
-  pico_tree_delete(&DHCPCookies, found);
-  pico_free(found);
-  return 0;
+    pico_socket_close(found->s);
+    pico_ipv4_link_del(found->dev, found->address);
+    pico_tree_delete(&DHCPCookies, found);
+    pico_free(found);
+    return 0;
 }
 
 static struct pico_dhcp_client_cookie *pico_dhcp_client_find_cookie(uint32_t xid)
 {
-  struct pico_dhcp_client_cookie test = {0}, *found = NULL;
+    struct pico_dhcp_client_cookie test = {
+        0
+    }, *found = NULL;
 
-  test.xid = xid;
-  found = pico_tree_findKey(&DHCPCookies, &test);
-  if (found)
-    return found;
-  else
-    return NULL;
+    test.xid = xid;
+    found = pico_tree_findKey(&DHCPCookies, &test);
+    if (found)
+        return found;
+    else
+        return NULL;
 }
 
 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;
+    struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
-  if (dhcpc->init_timer.state == DHCP_CLIENT_TIMER_STOPPED)
-    return;
+    if (dhcpc->init_timer.state == DHCP_CLIENT_TIMER_STOPPED)
+        return;
 
-  if (++dhcpc->retry >= DHCP_CLIENT_RETRIES) {
-    pico_err = PICO_ERR_EAGAIN;
-    dhcpc->cb(dhcpc, PICO_DHCP_ERROR);
-    pico_dhcp_client_del_cookie(dhcpc->xid);
-    pico_dhcp_client_mutex++;
+    if (++dhcpc->retry >= DHCP_CLIENT_RETRIES) {
+        pico_err = PICO_ERR_EAGAIN;
+        dhcpc->cb(dhcpc, PICO_DHCP_ERROR);
+        pico_dhcp_client_del_cookie(dhcpc->xid);
+        pico_dhcp_client_mutex++;
+        return;
+    }
+
+    /* init_timer is restarted in retransmit function,
+     * otherwise an old init_timer would go on indefinitely */
+    dhcpc->event = PICO_DHCP_EVENT_RETRANSMIT;
+    pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
     return;
-  }
-
-  /* init_timer is restarted in retransmit function, 
-   * otherwise an old init_timer would go on indefinitely */
-  dhcpc->event = PICO_DHCP_EVENT_RETRANSMIT;
-  pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
-  return;
 }
 
 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;
+    struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
-  if (dhcpc->requesting_timer.state == DHCP_CLIENT_TIMER_STOPPED)
-    return;
+    if (dhcpc->requesting_timer.state == DHCP_CLIENT_TIMER_STOPPED)
+        return;
 
-  if (++dhcpc->retry > DHCP_CLIENT_RETRIES) {
-    pico_dhcp_client_mutex++;
-    reset(dhcpc, NULL);
-    return;
-  }
+    if (++dhcpc->retry > DHCP_CLIENT_RETRIES) {
+        pico_dhcp_client_mutex++;
+        reset(dhcpc, NULL);
+        return;
+    }
 
-  /* requesting_timer is restarted in retransmit function, 
-   * otherwise an old requesting_timer would go on indefinitely */
-  dhcpc->event = PICO_DHCP_EVENT_RETRANSMIT;
-  pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
-  return;
+    /* requesting_timer is restarted in retransmit function,
+     * otherwise an old requesting_timer would go on indefinitely */
+    dhcpc->event = PICO_DHCP_EVENT_RETRANSMIT;
+    pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
+    return;
 }
 
 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;
+    struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
-  if (dhcpc->renewing_timer.state == DHCP_CLIENT_TIMER_STOPPED)
-    return;
+    if (dhcpc->renewing_timer.state == DHCP_CLIENT_TIMER_STOPPED)
+        return;
 
-  /* renewing_timer is restarted in retransmit function, 
-   * otherwise an old renewing_timer would go on indefinitely */
-  dhcpc->retry++;
-  dhcpc->event = PICO_DHCP_EVENT_RETRANSMIT;
-  pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
-  return;
+    /* renewing_timer is restarted in retransmit function,
+     * otherwise an old renewing_timer would go on indefinitely */
+    dhcpc->retry++;
+    dhcpc->event = PICO_DHCP_EVENT_RETRANSMIT;
+    pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
+    return;
 }
 
 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;
+    struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
-  if (dhcpc->rebinding_timer.state == DHCP_CLIENT_TIMER_STOPPED)
-    return;
+    if (dhcpc->rebinding_timer.state == DHCP_CLIENT_TIMER_STOPPED)
+        return;
 
-  /* rebinding_timer is restarted in retransmit function, 
-   * otherwise an old rebinding_timer would go on indefinitely */
-  dhcpc->retry++;
-  dhcpc->event = PICO_DHCP_EVENT_RETRANSMIT;
-  pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
-  return;
+    /* rebinding_timer is restarted in retransmit function,
+     * otherwise an old rebinding_timer would go on indefinitely */
+    dhcpc->retry++;
+    dhcpc->event = PICO_DHCP_EVENT_RETRANSMIT;
+    pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
+    return;
 }
 
 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;
+    struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
-  if (dhcpc->T1_timer.state == DHCP_CLIENT_TIMER_STOPPED)
-    return;
+    if (dhcpc->T1_timer.state == DHCP_CLIENT_TIMER_STOPPED)
+        return;
 
-  /* T1 state is set to stopped in renew function, 
-   * otherwise an old T1 could stop a valid T1 */
-  dhcpc->event = PICO_DHCP_EVENT_T1;
-  pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
-  return;
+    /* T1 state is set to stopped in renew function,
+     * otherwise an old T1 could stop a valid T1 */
+    dhcpc->event = PICO_DHCP_EVENT_T1;
+    pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
+    return;
 }
 
 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;
+    struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
-  if (dhcpc->T2_timer.state == DHCP_CLIENT_TIMER_STOPPED)
-    return;
+    if (dhcpc->T2_timer.state == DHCP_CLIENT_TIMER_STOPPED)
+        return;
 
-  /* T2 state is set to stopped in rebind function, 
-   * otherwise an old T2 could stop a valid T2. 
-   * Likewise for renewing_timer */
-  dhcpc->event = PICO_DHCP_EVENT_T2;
-  pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
-  return;
+    /* T2 state is set to stopped in rebind function,
+     * otherwise an old T2 could stop a valid T2.
+     * Likewise for renewing_timer */
+    dhcpc->event = PICO_DHCP_EVENT_T2;
+    pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
+    return;
 }
 
 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;
+    struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
-  if (dhcpc->lease_timer.state == DHCP_CLIENT_TIMER_STOPPED)
-    return;
+    if (dhcpc->lease_timer.state == DHCP_CLIENT_TIMER_STOPPED)
+        return;
 
-  /* lease state is set to stopped in reset function, 
-   * otherwise an old lease could stop a valid lease. 
-   * Likewise for rebinding_timer */
-  dhcpc->event = PICO_DHCP_EVENT_LEASE;
-  pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
-  return;
+    /* lease state is set to stopped in reset function,
+     * otherwise an old lease could stop a valid lease.
+     * Likewise for rebinding_timer */
+    dhcpc->event = PICO_DHCP_EVENT_LEASE;
+    pico_dhcp_state_machine(dhcpc->event, dhcpc, NULL);
+    return;
 }
 
 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;
+    struct pico_dhcp_client_cookie *dhcpc = (struct pico_dhcp_client_cookie *)arg;
 
-  if (++dhcpc->retry > DHCP_CLIENT_RETRIES) {
-    pico_err = PICO_ERR_EAGAIN;
-    dhcpc->cb(dhcpc, PICO_DHCP_ERROR);
-    pico_dhcp_client_del_cookie(dhcpc->xid);
+    if (++dhcpc->retry > DHCP_CLIENT_RETRIES) {
+        pico_err = PICO_ERR_EAGAIN;
+        dhcpc->cb(dhcpc, PICO_DHCP_ERROR);
+        pico_dhcp_client_del_cookie(dhcpc->xid);
+        return;
+    }
+
+    pico_dhcp_client_init(dhcpc);
     return;
-  }
-  pico_dhcp_client_init(dhcpc);
-  return;
 }
 
 static void pico_dhcp_client_stop_timers(struct pico_dhcp_client_cookie *dhcpc)
 {
-  dhcpc->retry = 0;
-  dhcpc->init_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->requesting_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->renewing_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->rebinding_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->T1_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->T2_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->lease_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->retry = 0;
+    dhcpc->init_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->requesting_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->renewing_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->rebinding_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->T1_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->T2_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->lease_timer.state = DHCP_CLIENT_TIMER_STOPPED;
 
-  return;
+    return;
 }
 
 static void pico_dhcp_client_start_init_timer(struct pico_dhcp_client_cookie *dhcpc)
 {
-  uint32_t time = 0;
+    uint32_t time = 0;
 
-  /* timer value is doubled with every retry (exponential backoff) */
-  dhcpc->init_timer.state = DHCP_CLIENT_TIMER_STARTED;
-  dhcpc->init_timer.time = DHCP_CLIENT_RETRANS;
-  time = dhcpc->init_timer.time << dhcpc->retry;
-  pico_timer_add(time * 1000, pico_dhcp_client_init_timer, dhcpc);
+    /* timer value is doubled with every retry (exponential backoff) */
+    dhcpc->init_timer.state = DHCP_CLIENT_TIMER_STARTED;
+    dhcpc->init_timer.time = DHCP_CLIENT_RETRANS;
+    time = dhcpc->init_timer.time << dhcpc->retry;
+    pico_timer_add(time * 1000, pico_dhcp_client_init_timer, dhcpc);
 
-  return;
+    return;
 }
 
 static void pico_dhcp_client_start_requesting_timer(struct pico_dhcp_client_cookie *dhcpc)
 {
-  uint32_t time = 0;
+    uint32_t time = 0;
 
-  /* timer value is doubled with every retry (exponential backoff) */
-  dhcpc->init_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->requesting_timer.state = DHCP_CLIENT_TIMER_STARTED;
-  dhcpc->requesting_timer.time = DHCP_CLIENT_RETRANS;
-  time = dhcpc->requesting_timer.time << dhcpc->retry;
-  pico_timer_add(time * 1000, pico_dhcp_client_requesting_timer, dhcpc);
+    /* timer value is doubled with every retry (exponential backoff) */
+    dhcpc->init_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->requesting_timer.state = DHCP_CLIENT_TIMER_STARTED;
+    dhcpc->requesting_timer.time = DHCP_CLIENT_RETRANS;
+    time = dhcpc->requesting_timer.time << dhcpc->retry;
+    pico_timer_add(time * 1000, pico_dhcp_client_requesting_timer, dhcpc);
 
-  return;
+    return;
 }
 
 static void pico_dhcp_client_start_renewing_timer(struct pico_dhcp_client_cookie *dhcpc)
 {
-  uint32_t halftime = 0;
+    uint32_t halftime = 0;
 
-  /* wait one-half of the remaining time until T2, down to a minimum of 60 seconds */
-  /* (dhcpc->retry + 1): initial -> divide by 2, 1st retry -> divide by 4, 2nd retry -> divide by 8, etc */
-  dhcpc->T1_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->renewing_timer.state = DHCP_CLIENT_TIMER_STARTED;
-  halftime = dhcpc->renewing_timer.time >> (dhcpc->retry + 1);
-  if (halftime < 60)
-    halftime = 60;
-  pico_timer_add(halftime * 1000, pico_dhcp_client_renewing_timer, dhcpc);
+    /* wait one-half of the remaining time until T2, down to a minimum of 60 seconds */
+    /* (dhcpc->retry + 1): initial -> divide by 2, 1st retry -> divide by 4, 2nd retry -> divide by 8, etc */
+    dhcpc->T1_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->renewing_timer.state = DHCP_CLIENT_TIMER_STARTED;
+    halftime = dhcpc->renewing_timer.time >> (dhcpc->retry + 1);
+    if (halftime < 60)
+        halftime = 60;
 
-  return;
+    pico_timer_add(halftime * 1000, pico_dhcp_client_renewing_timer, dhcpc);
+
+    return;
 }
 
 static void pico_dhcp_client_start_rebinding_timer(struct pico_dhcp_client_cookie *dhcpc)
 {
-  uint32_t halftime = 0;
+    uint32_t halftime = 0;
 
-  /* wait one-half of the remaining time until T2, down to a minimum of 60 seconds */
-  /* (dhcpc->retry + 1): initial -> divide by 2, 1st retry -> divide by 4, 2nd retry -> divide by 8, etc */
-  dhcpc->T2_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->renewing_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->rebinding_timer.state = DHCP_CLIENT_TIMER_STARTED;
-  halftime = dhcpc->rebinding_timer.time >> (dhcpc->retry + 1);
-  if (halftime < 60)
-    halftime = 60;
-  pico_timer_add(halftime * 1000, pico_dhcp_client_rebinding_timer, dhcpc);
+    /* wait one-half of the remaining time until T2, down to a minimum of 60 seconds */
+    /* (dhcpc->retry + 1): initial -> divide by 2, 1st retry -> divide by 4, 2nd retry -> divide by 8, etc */
+    dhcpc->T2_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->renewing_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->rebinding_timer.state = DHCP_CLIENT_TIMER_STARTED;
+    halftime = dhcpc->rebinding_timer.time >> (dhcpc->retry + 1);
+    if (halftime < 60)
+        halftime = 60;
 
-  return;
+    pico_timer_add(halftime * 1000, pico_dhcp_client_rebinding_timer, dhcpc);
+
+    return;
 }
 
 static void pico_dhcp_client_start_reacquisition_timers(struct pico_dhcp_client_cookie *dhcpc)
 {
-  dhcpc->requesting_timer.state = DHCP_CLIENT_TIMER_STOPPED;
-  dhcpc->T1_timer.state = DHCP_CLIENT_TIMER_STARTED;
-  dhcpc->T2_timer.state = DHCP_CLIENT_TIMER_STARTED;
-  dhcpc->lease_timer.state = DHCP_CLIENT_TIMER_STARTED;
-  pico_timer_add(dhcpc->T1_timer.time * 1000, pico_dhcp_client_T1_timer, dhcpc);
-  pico_timer_add(dhcpc->T2_timer.time * 1000, pico_dhcp_client_T2_timer, dhcpc);
-  pico_timer_add(dhcpc->lease_timer.time * 1000, pico_dhcp_client_lease_timer, dhcpc);
+    dhcpc->requesting_timer.state = DHCP_CLIENT_TIMER_STOPPED;
+    dhcpc->T1_timer.state = DHCP_CLIENT_TIMER_STARTED;
+    dhcpc->T2_timer.state = DHCP_CLIENT_TIMER_STARTED;
+    dhcpc->lease_timer.state = DHCP_CLIENT_TIMER_STARTED;
+    pico_timer_add(dhcpc->T1_timer.time * 1000, pico_dhcp_client_T1_timer, dhcpc);
+    pico_timer_add(dhcpc->T2_timer.time * 1000, pico_dhcp_client_T2_timer, dhcpc);
+    pico_timer_add(dhcpc->lease_timer.time * 1000, pico_dhcp_client_lease_timer, dhcpc);
 
-  return;
+    return;
 }
 
 static int pico_dhcp_client_init(struct pico_dhcp_client_cookie *dhcpc)
 {
-  uint16_t port = PICO_DHCP_CLIENT_PORT;
-  struct pico_ip4 inaddr_any = {0}, netmask = {0};
+    uint16_t port = PICO_DHCP_CLIENT_PORT;
+    struct pico_ip4 inaddr_any = {
+        0
+    }, netmask = {
+        0
+    };
 
-  /* serialize client negotations if multiple devices */
-  /* NOTE: ONLY initialization is serialized! */
-  if (!pico_dhcp_client_mutex) {
-    pico_timer_add(DHCP_CLIENT_REINIT, pico_dhcp_client_reinit, dhcpc);
-    return 0;
-  }
-  pico_dhcp_client_mutex--;
+    /* serialize client negotations if multiple devices */
+    /* NOTE: ONLY initialization is serialized! */
+    if (!pico_dhcp_client_mutex) {
+        pico_timer_add(DHCP_CLIENT_REINIT, pico_dhcp_client_reinit, dhcpc);
+        return 0;
+    }
 
-  switch (dhcpc->status)
-  {
+    pico_dhcp_client_mutex--;
+
+    switch (dhcpc->status)
+    {
     case DHCP_CLIENT_STATUS_INIT:
-      dhcpc->s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &pico_dhcp_client_wakeup);
-      if (!dhcpc->s) {
-        pico_dhcp_client_mutex++;
-        pico_timer_add(DHCP_CLIENT_REINIT, pico_dhcp_client_reinit, dhcpc);
-        break;
-      }
-      dhcpc->s->dev = dhcpc->dev;
-      dhcpc->status = DHCP_CLIENT_STATUS_SOCKET;
-      /* fallthrough */
+        dhcpc->s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &pico_dhcp_client_wakeup);
+        if (!dhcpc->s) {
+            pico_dhcp_client_mutex++;
+            pico_timer_add(DHCP_CLIENT_REINIT, pico_dhcp_client_reinit, dhcpc);
+            break;
+        }
+
+        dhcpc->s->dev = dhcpc->dev;
+        dhcpc->status = DHCP_CLIENT_STATUS_SOCKET;
+    /* fallthrough */
 
     case DHCP_CLIENT_STATUS_SOCKET:
-      if (pico_socket_bind(dhcpc->s, &inaddr_any, &port) < 0) {
-        pico_dhcp_client_mutex++;
-        pico_timer_add(DHCP_CLIENT_REINIT, pico_dhcp_client_reinit, dhcpc);
-        break;
-      }
-      dhcpc->status = DHCP_CLIENT_STATUS_BOUND;
-      /* fallthrough */
+        if (pico_socket_bind(dhcpc->s, &inaddr_any, &port) < 0) {
+            pico_dhcp_client_mutex++;
+            pico_timer_add(DHCP_CLIENT_REINIT, pico_dhcp_client_reinit, dhcpc);
+            break;
+        }
+
+        dhcpc->status = DHCP_CLIENT_STATUS_BOUND;
+    /* fallthrough */
 
     case DHCP_CLIENT_STATUS_BOUND:
-      /* adding a link with address 0.0.0.0 and netmask 0.0.0.0, 
-       * automatically adds a route for a global broadcast */
-      if (pico_ipv4_link_add(dhcpc->dev, inaddr_any, netmask) < 0) {
-        pico_dhcp_client_mutex++;
-        pico_timer_add(DHCP_CLIENT_REINIT, pico_dhcp_client_reinit, dhcpc);
-        break;
-      }
-      dhcpc->status = DHCP_CLIENT_STATUS_LINKED;
-      /* fallthrough */
+        /* adding a link with address 0.0.0.0 and netmask 0.0.0.0,
+         * automatically adds a route for a global broadcast */
+        if (pico_ipv4_link_add(dhcpc->dev, inaddr_any, netmask) < 0) {
+            pico_dhcp_client_mutex++;
+            pico_timer_add(DHCP_CLIENT_REINIT, pico_dhcp_client_reinit, dhcpc);
+            break;
+        }
+
+        dhcpc->status = DHCP_CLIENT_STATUS_LINKED;
+    /* fallthrough */
 
     case DHCP_CLIENT_STATUS_LINKED:
-      if (pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_DISCOVER) < 0) {
-        pico_dhcp_client_mutex++;
-        pico_timer_add(DHCP_CLIENT_REINIT, pico_dhcp_client_reinit, dhcpc);
-        break;
-      }
-      dhcpc->status = DHCP_CLIENT_STATUS_TRANSMITTED;
-      /* fallthrough */
+        if (pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_DISCOVER) < 0) {
+            pico_dhcp_client_mutex++;
+            pico_timer_add(DHCP_CLIENT_REINIT, pico_dhcp_client_reinit, dhcpc);
+            break;
+        }
+
+        dhcpc->status = DHCP_CLIENT_STATUS_TRANSMITTED;
+    /* fallthrough */
 
     case DHCP_CLIENT_STATUS_TRANSMITTED:
-      dhcpc->retry = 0;
-      dhcpc->init_timestamp = PICO_TIME_MS();
-      pico_dhcp_client_start_init_timer(dhcpc);
-      break;
+        dhcpc->retry = 0;
+        dhcpc->init_timestamp = PICO_TIME_MS();
+        pico_dhcp_client_start_init_timer(dhcpc);
+        break;
 
     default:
-      return -1;
-  }
-  return 0;
+        return -1;
+    }
+    return 0;
 }
 
 int pico_dhcp_initiate_negotiation(struct pico_device *dev, void (*cb)(void *dhcpc, int code), uint32_t *uid)
 {
-  uint8_t retry = 32;
-  uint32_t xid = 0;
-  struct pico_dhcp_client_cookie *dhcpc = NULL;
+    uint8_t retry = 32;
+    uint32_t xid = 0;
+    struct pico_dhcp_client_cookie *dhcpc = NULL;
 
-  if (!dev || !cb || !uid) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  if (!dev->eth) {
-    pico_err = PICO_ERR_EOPNOTSUPP;
-    return -1;
-  }
+    if (!dev || !cb || !uid) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    if (!dev->eth) {
+        pico_err = PICO_ERR_EOPNOTSUPP;
+        return -1;
+    }
 
-  /* attempt to generate a correct xid, else fail */
-  do {
-    xid = pico_rand();
-  } while (!xid && --retry);
+    /* attempt to generate a correct xid, else fail */
+    do {
+        xid = pico_rand();
+    } while (!xid && --retry);
 
-  if (!xid) {
-    pico_err = PICO_ERR_EAGAIN;
-    return -1;
-  }
+    if (!xid) {
+        pico_err = PICO_ERR_EAGAIN;
+        return -1;
+    }
 
-  dhcpc = pico_dhcp_client_add_cookie(xid, dev, cb, uid);
-  if (!dhcpc)
-    return -1;
+    dhcpc = pico_dhcp_client_add_cookie(xid, dev, cb, uid);
+    if (!dhcpc)
+        return -1;
 
-  dhcpc_dbg("DHCP client: cookie with xid %u\n", dhcpc->xid);
-  return pico_dhcp_client_init(dhcpc);
+    dhcpc_dbg("DHCP client: cookie with xid %u\n", dhcpc->xid);
+    return pico_dhcp_client_init(dhcpc);
 }
 
 static void pico_dhcp_client_recv_params(struct pico_dhcp_client_cookie *dhcpc, struct pico_dhcp_opt *opt)
 {
-  do {
-    switch (opt->code)
-    {
-      case PICO_DHCP_OPT_PAD:
-        break;
+    do {
+        switch (opt->code)
+        {
+        case PICO_DHCP_OPT_PAD:
+            break;
 
-      case PICO_DHCP_OPT_END:
-        break;
+        case PICO_DHCP_OPT_END:
+            break;
 
-      case PICO_DHCP_OPT_MSGTYPE:
-        dhcpc->event = opt->ext.msg_type.type;
-        dhcpc_dbg("DHCP client: message type %u\n", dhcpc->event);
-        break;
+        case PICO_DHCP_OPT_MSGTYPE:
+            dhcpc->event = opt->ext.msg_type.type;
+            dhcpc_dbg("DHCP client: message type %u\n", dhcpc->event);
+            break;
 
-      case PICO_DHCP_OPT_LEASETIME:
-        dhcpc->lease_timer.time = long_be(opt->ext.lease_time.time);
-        dhcpc_dbg("DHCP client: lease time %u\n", dhcpc->lease_timer.time);
-        break;
+        case PICO_DHCP_OPT_LEASETIME:
+            dhcpc->lease_timer.time = long_be(opt->ext.lease_time.time);
+            dhcpc_dbg("DHCP client: lease time %u\n", dhcpc->lease_timer.time);
+            break;
+
+        case PICO_DHCP_OPT_RENEWALTIME:
+            dhcpc->T1_timer.time = long_be(opt->ext.renewal_time.time);
+            dhcpc_dbg("DHCP client: renewal time %u\n", dhcpc->T1_timer.time);
+            break;
 
-      case PICO_DHCP_OPT_RENEWALTIME:
-        dhcpc->T1_timer.time = long_be(opt->ext.renewal_time.time);
-        dhcpc_dbg("DHCP client: renewal time %u\n", dhcpc->T1_timer.time);
-        break;
+        case PICO_DHCP_OPT_REBINDINGTIME:
+            dhcpc->T2_timer.time = long_be(opt->ext.rebinding_time.time);
+            dhcpc_dbg("DHCP client: rebinding time %u\n", dhcpc->T2_timer.time);
+            break;
 
-      case PICO_DHCP_OPT_REBINDINGTIME:
-        dhcpc->T2_timer.time = long_be(opt->ext.rebinding_time.time);
-        dhcpc_dbg("DHCP client: rebinding time %u\n", dhcpc->T2_timer.time);
-        break;
+        case PICO_DHCP_OPT_ROUTER:
+            dhcpc->gateway = opt->ext.router.ip;
+            dhcpc_dbg("DHCP client: router %08X\n", dhcpc->gateway.addr);
+            break;
 
-      case PICO_DHCP_OPT_ROUTER:
-        dhcpc->gateway = opt->ext.router.ip;
-        dhcpc_dbg("DHCP client: router %08X\n", dhcpc->gateway.addr);
-        break;
+        case PICO_DHCP_OPT_DNS:
+            dhcpc->nameserver = opt->ext.dns.ip;
+            dhcpc_dbg("DHCP client: dns %08X\n", dhcpc->nameserver.addr);
+            break;
 
-      case PICO_DHCP_OPT_DNS:
-        dhcpc->nameserver = opt->ext.dns.ip;
-        dhcpc_dbg("DHCP client: dns %08X\n", dhcpc->nameserver.addr);
-        break;
+        case PICO_DHCP_OPT_NETMASK:
+            dhcpc->netmask = opt->ext.netmask.ip;
+            dhcpc_dbg("DHCP client: netmask %08X\n", dhcpc->netmask.addr);
+            break;
 
-      case PICO_DHCP_OPT_NETMASK:
-        dhcpc->netmask = opt->ext.netmask.ip;
-        dhcpc_dbg("DHCP client: netmask %08X\n", dhcpc->netmask.addr);
-        break;
+        case PICO_DHCP_OPT_SERVERID:
+            dhcpc->server_id = opt->ext.server_id.ip;
+            dhcpc_dbg("DHCP client: server ID %08X\n", dhcpc->server_id.addr);
+            break;
 
-      case PICO_DHCP_OPT_SERVERID:
-        dhcpc->server_id = opt->ext.server_id.ip;
-        dhcpc_dbg("DHCP client: server ID %08X\n", dhcpc->server_id.addr);
-        break;
-
-      case PICO_DHCP_OPT_OPTOVERLOAD:
-        dhcpc_dbg("DHCP client: WARNING option overload present (not processed)");
-        break;
+        case PICO_DHCP_OPT_OPTOVERLOAD:
+            dhcpc_dbg("DHCP client: WARNING option overload present (not processed)");
+            break;
 
-      default:
-        dhcpc_dbg("DHCP client: WARNING unsupported option %u\n", opt->code);
-        break;
-    }
-  } while (pico_dhcp_next_option(&opt));
+        default:
+            dhcpc_dbg("DHCP client: WARNING unsupported option %u\n", opt->code);
+            break;
+        }
+    } while (pico_dhcp_next_option(&opt));
 
-  /* default values for T1 and T2 when not provided */
-  if (!dhcpc->T1_timer.time)
-    dhcpc->T1_timer.time = dhcpc->lease_timer.time >> 1;
-  if (!dhcpc->T2_timer.time)
-    dhcpc->T2_timer.time = (dhcpc->lease_timer.time * 875) / 1000;
+    /* default values for T1 and T2 when not provided */
+    if (!dhcpc->T1_timer.time)
+        dhcpc->T1_timer.time = dhcpc->lease_timer.time >> 1;
 
-  return;
+    if (!dhcpc->T2_timer.time)
+        dhcpc->T2_timer.time = (dhcpc->lease_timer.time * 875) / 1000;
+
+    return;
 }
 
 static int recv_offer(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf)
 {
-  struct pico_dhcp_hdr *hdr = (struct pico_dhcp_hdr *)buf;
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)hdr->options;
+    struct pico_dhcp_hdr *hdr = (struct pico_dhcp_hdr *)buf;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)hdr->options;
+
+    pico_dhcp_client_recv_params(dhcpc, opt);
+    if ((dhcpc->event != PICO_DHCP_MSG_OFFER) || !dhcpc->server_id.addr || !dhcpc->netmask.addr || !dhcpc->lease_timer.time)
+        return -1;
 
-  pico_dhcp_client_recv_params(dhcpc, opt);
-  if ((dhcpc->event != PICO_DHCP_MSG_OFFER) || !dhcpc->server_id.addr || !dhcpc->netmask.addr || !dhcpc->lease_timer.time)
-    return -1;
-  dhcpc->address.addr = hdr->yiaddr;
+    dhcpc->address.addr = hdr->yiaddr;
 
-  /* we skip state SELECTING, process first offer received */
-  dhcpc->state = DHCP_CLIENT_STATE_REQUESTING;
-  dhcpc->retry = 0;
-  pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_REQUEST);
-  pico_dhcp_client_start_requesting_timer(dhcpc);
-  return 0;
+    /* we skip state SELECTING, process first offer received */
+    dhcpc->state = DHCP_CLIENT_STATE_REQUESTING;
+    dhcpc->retry = 0;
+    pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_REQUEST);
+    pico_dhcp_client_start_requesting_timer(dhcpc);
+    return 0;
 }
 
 static int recv_ack(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf)
 {
-  struct pico_dhcp_hdr *hdr = (struct pico_dhcp_hdr *)buf;
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)hdr->options;
-  struct pico_ip4 address = {0}, netmask = {0}, bcast = { .addr = 0xFFFFFFFF };
+    struct pico_dhcp_hdr *hdr = (struct pico_dhcp_hdr *)buf;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)hdr->options;
+    struct pico_ip4 address = {
+        0
+    }, netmask = {
+        0
+    }, bcast = {
+        .addr = 0xFFFFFFFF
+    };
 
-  pico_dhcp_client_recv_params(dhcpc, opt);
-  if ((dhcpc->event != PICO_DHCP_MSG_ACK) || !dhcpc->server_id.addr || !dhcpc->netmask.addr || !dhcpc->lease_timer.time)
-    return -1;
+    pico_dhcp_client_recv_params(dhcpc, opt);
+    if ((dhcpc->event != PICO_DHCP_MSG_ACK) || !dhcpc->server_id.addr || !dhcpc->netmask.addr || !dhcpc->lease_timer.time)
+        return -1;
 
-  /* Issue #20 the server can transmit on ACK a different IP than the one in OFFER */
-  /* RFC2131 ch 4.3.2 ... The client SHOULD use the parameters in the DHCPACK message for configuration */
-  if (dhcpc->state == DHCP_CLIENT_STATE_REQUESTING)
-    dhcpc->address.addr = hdr->yiaddr;
+    /* Issue #20 the server can transmit on ACK a different IP than the one in OFFER */
+    /* RFC2131 ch 4.3.2 ... The client SHOULD use the parameters in the DHCPACK message for configuration */
+    if (dhcpc->state == DHCP_CLIENT_STATE_REQUESTING)
+        dhcpc->address.addr = hdr->yiaddr;
 
 
-  /* close the socket used for address (re)acquisition */
-  pico_socket_close(dhcpc->s);
-  /* delete the link with address 0.0.0.0, add new link with acquired address */
-  if (dhcpc->status == DHCP_CLIENT_STATUS_TRANSMITTED) {
-    pico_ipv4_link_del(dhcpc->dev, address);
-    pico_ipv4_link_add(dhcpc->dev, dhcpc->address, dhcpc->netmask);
-    dhcpc->status = DHCP_CLIENT_STATUS_INITIALIZED;
-  }
-  /* delete the default route for our global broadcast messages, otherwise another interface can not rebind */
-  if (dhcpc->state == DHCP_CLIENT_STATE_REBINDING)
-    pico_ipv4_route_del(bcast, netmask, 1);
+    /* close the socket used for address (re)acquisition */
+    pico_socket_close(dhcpc->s);
+    /* delete the link with address 0.0.0.0, add new link with acquired address */
+    if (dhcpc->status == DHCP_CLIENT_STATUS_TRANSMITTED) {
+        pico_ipv4_link_del(dhcpc->dev, address);
+        pico_ipv4_link_add(dhcpc->dev, dhcpc->address, dhcpc->netmask);
+        dhcpc->status = DHCP_CLIENT_STATUS_INITIALIZED;
+    }
+
+    /* delete the default route for our global broadcast messages, otherwise another interface can not rebind */
+    if (dhcpc->state == DHCP_CLIENT_STATE_REBINDING)
+        pico_ipv4_route_del(bcast, netmask, 1);
 
-  dbg("DHCP client: renewal time (T1) %u\n", dhcpc->T1_timer.time);
-  dbg("DHCP client: rebinding time (T2) %u\n", dhcpc->T2_timer.time);
-  dbg("DHCP client: lease time %u\n", dhcpc->lease_timer.time);
+    dbg("DHCP client: renewal time (T1) %u\n", dhcpc->T1_timer.time);
+    dbg("DHCP client: rebinding time (T2) %u\n", dhcpc->T2_timer.time);
+    dbg("DHCP client: lease time %u\n", dhcpc->lease_timer.time);
 
-  dhcpc->retry = 0;
-  dhcpc->renewing_timer.time = dhcpc->T2_timer.time - dhcpc->T1_timer.time;
-  dhcpc->rebinding_timer.time = dhcpc->lease_timer.time - dhcpc->T2_timer.time;
-  pico_dhcp_client_start_reacquisition_timers(dhcpc);
+    dhcpc->retry = 0;
+    dhcpc->renewing_timer.time = dhcpc->T2_timer.time - dhcpc->T1_timer.time;
+    dhcpc->rebinding_timer.time = dhcpc->lease_timer.time - dhcpc->T2_timer.time;
+    pico_dhcp_client_start_reacquisition_timers(dhcpc);
 
-  pico_dhcp_client_mutex++;
-  *(dhcpc->uid) = dhcpc->xid;
-  dhcpc->cb(dhcpc, PICO_DHCP_SUCCESS);
-  dhcpc->state = DHCP_CLIENT_STATE_BOUND;
-  return 0;
+    pico_dhcp_client_mutex++;
+    *(dhcpc->uid) = dhcpc->xid;
+    dhcpc->cb(dhcpc, PICO_DHCP_SUCCESS);
+    dhcpc->state = DHCP_CLIENT_STATE_BOUND;
+    return 0;
 }
 
 static int renew(struct pico_dhcp_client_cookie *dhcpc, uint8_t __attribute__((unused)) *buf)
 {
-  uint16_t port = PICO_DHCP_CLIENT_PORT;
+    uint16_t port = PICO_DHCP_CLIENT_PORT;
+
+    dhcpc->state = DHCP_CLIENT_STATE_RENEWING;
+    dhcpc->s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &pico_dhcp_client_wakeup);
+    if (!dhcpc->s) {
+        dhcpc_dbg("DHCP client ERROR: failure opening socket on renew, aborting DHCP! (%s)\n", strerror(pico_err));
+        dhcpc->cb(dhcpc, PICO_DHCP_ERROR);
+        return -1;
+    }
 
-  dhcpc->state = DHCP_CLIENT_STATE_RENEWING;
-  dhcpc->s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &pico_dhcp_client_wakeup);
-  if (!dhcpc->s) {
-    dhcpc_dbg("DHCP client ERROR: failure opening socket on renew, aborting DHCP! (%s)\n", strerror(pico_err));
-    dhcpc->cb(dhcpc, PICO_DHCP_ERROR);
-    return -1;
-  }
-  if (pico_socket_bind(dhcpc->s, &dhcpc->address, &port) != 0) {
-    dhcpc_dbg("DHCP client ERROR: failure binding socket on renew, aborting DHCP! (%s)\n", strerror(pico_err));
-    pico_socket_close(dhcpc->s);
-    dhcpc->cb(dhcpc, PICO_DHCP_ERROR);
-    return -1;
-  }
+    if (pico_socket_bind(dhcpc->s, &dhcpc->address, &port) != 0) {
+        dhcpc_dbg("DHCP client ERROR: failure binding socket on renew, aborting DHCP! (%s)\n", strerror(pico_err));
+        pico_socket_close(dhcpc->s);
+        dhcpc->cb(dhcpc, PICO_DHCP_ERROR);
+        return -1;
+    }
 
-  dhcpc->retry = 0;
-  pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_REQUEST);
-  pico_dhcp_client_start_renewing_timer(dhcpc);
+    dhcpc->retry = 0;
+    pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_REQUEST);
+    pico_dhcp_client_start_renewing_timer(dhcpc);
 
-  return 0;
+    return 0;
 }
 
 static int rebind(struct pico_dhcp_client_cookie *dhcpc, uint8_t __attribute__((unused)) *buf)
 {
-  struct pico_ip4 bcast = { .addr = 0xFFFFFFFF }, netmask = {0}, inaddr_any = {0};
+    struct pico_ip4 bcast = {
+        .addr = 0xFFFFFFFF
+    }, netmask = {
+        0
+    }, inaddr_any = {
+        0
+    };
 
-  dhcpc->state = DHCP_CLIENT_STATE_REBINDING;
-  dhcpc->retry = 0;
-  /* we need a default route for our global broadcast messages, otherwise they get dropped. */
-  pico_ipv4_route_add(bcast, netmask, inaddr_any, 1, pico_ipv4_link_get(&dhcpc->address));
-  pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_REQUEST);
-  pico_dhcp_client_start_rebinding_timer(dhcpc);
+    dhcpc->state = DHCP_CLIENT_STATE_REBINDING;
+    dhcpc->retry = 0;
+    /* we need a default route for our global broadcast messages, otherwise they get dropped. */
+    pico_ipv4_route_add(bcast, netmask, inaddr_any, 1, pico_ipv4_link_get(&dhcpc->address));
+    pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_REQUEST);
+    pico_dhcp_client_start_rebinding_timer(dhcpc);
 
-  return 0;
+    return 0;
 }
 
 static int reset(struct pico_dhcp_client_cookie *dhcpc, uint8_t __attribute__((unused)) *buf)
 {
-  struct pico_ip4 address = {0};
+    struct pico_ip4 address = {
+        0
+    };
 
-  if (dhcpc->state == DHCP_CLIENT_STATE_REQUESTING)
-    address.addr = PICO_IP4_ANY;
-  else
-    address.addr = dhcpc->address.addr;
+    if (dhcpc->state == DHCP_CLIENT_STATE_REQUESTING)
+        address.addr = PICO_IP4_ANY;
+    else
+        address.addr = dhcpc->address.addr;
 
-  /* close the socket used for address (re)acquisition */
-  pico_socket_close(dhcpc->s);
-  /* delete the link with the currently in use address */
-  pico_ipv4_link_del(dhcpc->dev, address);
+    /* close the socket used for address (re)acquisition */
+    pico_socket_close(dhcpc->s);
+    /* delete the link with the currently in use address */
+    pico_ipv4_link_del(dhcpc->dev, address);
 
-  dhcpc->cb(dhcpc, PICO_DHCP_RESET);
-  if (dhcpc->state < DHCP_CLIENT_STATE_BOUND)
-  {
-    pico_dhcp_client_mutex++;
-  }
-  dhcpc->state = DHCP_CLIENT_STATE_INIT;
-  dhcpc->status = DHCP_CLIENT_STATUS_INIT;
-  pico_dhcp_client_stop_timers(dhcpc);
-  pico_dhcp_client_init(dhcpc);
-  return 0;
+    dhcpc->cb(dhcpc, PICO_DHCP_RESET);
+    if (dhcpc->state < DHCP_CLIENT_STATE_BOUND)
+    {
+        pico_dhcp_client_mutex++;
+    }
+
+    dhcpc->state = DHCP_CLIENT_STATE_INIT;
+    dhcpc->status = DHCP_CLIENT_STATUS_INIT;
+    pico_dhcp_client_stop_timers(dhcpc);
+    pico_dhcp_client_init(dhcpc);
+    return 0;
 }
 
 static int retransmit(struct pico_dhcp_client_cookie *dhcpc, uint8_t __attribute__((unused)) *buf)
 {
-  switch (dhcpc->state)
-  {
+    switch (dhcpc->state)
+    {
     case DHCP_CLIENT_STATE_INIT:
-      pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_DISCOVER);
-      pico_dhcp_client_start_init_timer(dhcpc);
-      break;
+        pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_DISCOVER);
+        pico_dhcp_client_start_init_timer(dhcpc);
+        break;
 
     case DHCP_CLIENT_STATE_REQUESTING:
-      pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_REQUEST);
-      pico_dhcp_client_start_requesting_timer(dhcpc);
-      break;
+        pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_REQUEST);
+        pico_dhcp_client_start_requesting_timer(dhcpc);
+        break;
 
     case DHCP_CLIENT_STATE_RENEWING:
-      pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_REQUEST);
-      pico_dhcp_client_start_renewing_timer(dhcpc);
-      break;
+        pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_REQUEST);
+        pico_dhcp_client_start_renewing_timer(dhcpc);
+        break;
 
     case DHCP_CLIENT_STATE_REBINDING:
-      pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_DISCOVER);
-      pico_dhcp_client_start_rebinding_timer(dhcpc);
-      break;
+        pico_dhcp_client_msg(dhcpc, PICO_DHCP_MSG_DISCOVER);
+        pico_dhcp_client_start_rebinding_timer(dhcpc);
+        break;
 
     default:
-      dhcpc_dbg("DHCP client WARNING: retransmit in incorrect state (%u)!\n", dhcpc->state);
-      return -1;
-  }
-  return 0;
+        dhcpc_dbg("DHCP client WARNING: retransmit in incorrect state (%u)!\n", dhcpc->state);
+        return -1;
+    }
+    return 0;
 }
 
 struct dhcp_action_entry {
-  int (*offer)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
-  int (*ack)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
-  int (*nak)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
-  int (*timer1)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
-  int (*timer2)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
-  int (*timer_lease)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
-  int (*timer_retransmit)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
+    int (*offer)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
+    int (*ack)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
+    int (*nak)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
+    int (*timer1)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
+    int (*timer2)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
+    int (*timer_lease)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
+    int (*timer_retransmit)(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf);
 };
 
-static struct dhcp_action_entry dhcp_fsm[] = 
+static struct dhcp_action_entry dhcp_fsm[] =
 { /* event                |offer      |ack      |nak    |T1    |T2     |lease  |retransmit */
-/* state init-reboot */ { NULL,       NULL,     NULL,   NULL,  NULL,   NULL,  NULL       },
+/* state init-reboot */
+    { NULL,       NULL,     NULL,   NULL,  NULL,   NULL,  NULL       },
 /* state rebooting   */ { NULL,       NULL,     NULL,   NULL,  NULL,   NULL,  NULL       },
 /* state init        */ { recv_offer, NULL,     NULL,   NULL,  NULL,   NULL,  retransmit },
 /* state selecting   */ { NULL,       NULL,     NULL,   NULL,  NULL,   NULL,  NULL       },
@@ -726,208 +766,223 @@
 /* TIMERS REMARK:
  * In state bound we have T1, T2 and the lease timer running. If T1 goes off, we attempt to renew.
  * If the renew succeeds a new T1, T2 and lease timer is started. The former T2 and lease timer is
- * still running though. This poses no concerns as the T2 and lease event in state bound have a NULL 
- * pointer in the fsm. If the former T2 or lease timer goes off, nothing happens. Same situation 
+ * still running though. This poses no concerns as the T2 and lease event in state bound have a NULL
+ * pointer in the fsm. If the former T2 or lease timer goes off, nothing happens. Same situation
  * applies for T2 and a succesfull rebind. */
 
 static void pico_dhcp_state_machine(uint8_t event, struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf)
 {
-  switch (event) 
-  {
+    switch (event)
+    {
     case PICO_DHCP_MSG_OFFER:
-      dhcpc_dbg("DHCP client: received OFFER\n");
-      if (dhcp_fsm[dhcpc->state].offer)
-        dhcp_fsm[dhcpc->state].offer(dhcpc, buf);
-      break;
+        dhcpc_dbg("DHCP client: received OFFER\n");
+        if (dhcp_fsm[dhcpc->state].offer)
+            dhcp_fsm[dhcpc->state].offer(dhcpc, buf);
+
+        break;
 
     case PICO_DHCP_MSG_ACK:
-      dhcpc_dbg("DHCP client: received ACK\n");
-      if (dhcp_fsm[dhcpc->state].ack)
-        dhcp_fsm[dhcpc->state].ack(dhcpc, buf);
-      break;
+        dhcpc_dbg("DHCP client: received ACK\n");
+        if (dhcp_fsm[dhcpc->state].ack)
+            dhcp_fsm[dhcpc->state].ack(dhcpc, buf);
+
+        break;
 
     case PICO_DHCP_MSG_NAK:
-      dhcpc_dbg("DHCP client: received NAK\n");
-      if (dhcp_fsm[dhcpc->state].nak)
-        dhcp_fsm[dhcpc->state].nak(dhcpc, buf);
-      break;
+        dhcpc_dbg("DHCP client: received NAK\n");
+        if (dhcp_fsm[dhcpc->state].nak)
+            dhcp_fsm[dhcpc->state].nak(dhcpc, buf);
+
+        break;
 
     case PICO_DHCP_EVENT_T1:
-      dhcpc_dbg("DHCP client: received T1 timeout\n");
-      if (dhcp_fsm[dhcpc->state].timer1)
-        dhcp_fsm[dhcpc->state].timer1(dhcpc, NULL);
-      break;
+        dhcpc_dbg("DHCP client: received T1 timeout\n");
+        if (dhcp_fsm[dhcpc->state].timer1)
+            dhcp_fsm[dhcpc->state].timer1(dhcpc, NULL);
+
+        break;
 
     case PICO_DHCP_EVENT_T2:
-      dhcpc_dbg("DHCP client: received T2 timeout\n");
-      if (dhcp_fsm[dhcpc->state].timer2)
-        dhcp_fsm[dhcpc->state].timer2(dhcpc, NULL);
-      break;
+        dhcpc_dbg("DHCP client: received T2 timeout\n");
+        if (dhcp_fsm[dhcpc->state].timer2)
+            dhcp_fsm[dhcpc->state].timer2(dhcpc, NULL);
+
+        break;
 
     case PICO_DHCP_EVENT_LEASE:
-      dhcpc_dbg("DHCP client: received LEASE timeout\n");
-      if (dhcp_fsm[dhcpc->state].timer_lease)
-        dhcp_fsm[dhcpc->state].timer_lease(dhcpc, NULL);
-      break;
+        dhcpc_dbg("DHCP client: received LEASE timeout\n");
+        if (dhcp_fsm[dhcpc->state].timer_lease)
+            dhcp_fsm[dhcpc->state].timer_lease(dhcpc, NULL);
+
+        break;
 
     case PICO_DHCP_EVENT_RETRANSMIT:
-      dhcpc_dbg("DHCP client: received RETRANSMIT timeout\n");
-      if (dhcp_fsm[dhcpc->state].timer_retransmit)
-        dhcp_fsm[dhcpc->state].timer_retransmit(dhcpc, NULL);
-      break;
+        dhcpc_dbg("DHCP client: received RETRANSMIT timeout\n");
+        if (dhcp_fsm[dhcpc->state].timer_retransmit)
+            dhcp_fsm[dhcpc->state].timer_retransmit(dhcpc, NULL);
+
+        break;
 
     default:
-      dhcpc_dbg("DHCP client WARNING: unrecognized event (%u)!\n", dhcpc->event);
-      return;
-  }
-  return;
+        dhcpc_dbg("DHCP client WARNING: unrecognized event (%u)!\n", dhcpc->event);
+        return;
+    }
+    return;
 }
 
 static int16_t pico_dhcp_client_opt_parse(void *ptr, uint16_t len)
 {
-  uint32_t optlen = len - (uint32_t)sizeof(struct pico_dhcp_hdr);
-  struct pico_dhcp_hdr *hdr = (struct pico_dhcp_hdr *)ptr;
-  struct pico_dhcp_opt *opt = NULL;
+    uint32_t optlen = len - (uint32_t)sizeof(struct pico_dhcp_hdr);
+    struct pico_dhcp_hdr *hdr = (struct pico_dhcp_hdr *)ptr;
+    struct pico_dhcp_opt *opt = NULL;
+
+    if (hdr->dhcp_magic != PICO_DHCPD_MAGIC_COOKIE)
+        return -1;
 
-  if (hdr->dhcp_magic != PICO_DHCPD_MAGIC_COOKIE)
-    return -1;
-  if (!pico_dhcp_are_options_valid(hdr->options, (int32_t)optlen))
+    if (!pico_dhcp_are_options_valid(hdr->options, (int32_t)optlen))
+        return -1;
+
+    opt = (struct pico_dhcp_opt *)hdr->options;
+    do {
+        if (opt->code == PICO_DHCP_OPT_MSGTYPE)
+            return opt->ext.msg_type.type;
+    } while (pico_dhcp_next_option(&opt));
+
     return -1;
-
-  opt = (struct pico_dhcp_opt *)hdr->options;
-  do {
-    if (opt->code == PICO_DHCP_OPT_MSGTYPE)
-      return opt->ext.msg_type.type;
-  } while (pico_dhcp_next_option(&opt));
-
-  return -1;
 }
 
 static int8_t pico_dhcp_client_msg(struct pico_dhcp_client_cookie *dhcpc, uint8_t msg_type)
 {
-  int32_t r = 0;
-  uint16_t optlen = 0, offset = 0;
-  struct pico_ip4 destination = { .addr = 0xFFFFFFFF};
-  struct pico_dhcp_hdr *hdr = NULL;
+    int32_t r = 0;
+    uint16_t optlen = 0, offset = 0;
+    struct pico_ip4 destination = {
+        .addr = 0xFFFFFFFF
+    };
+    struct pico_dhcp_hdr *hdr = NULL;
 
-  switch (msg_type)
-  {
+    switch (msg_type)
+    {
     case PICO_DHCP_MSG_DISCOVER:
-      dhcpc_dbg("DHCP client: sent DHCPDISCOVER\n");
-      optlen = PICO_DHCP_OPTLEN_MSGTYPE + PICO_DHCP_OPTLEN_MAXMSGSIZE + PICO_DHCP_OPTLEN_PARAMLIST + PICO_DHCP_OPTLEN_END;
-      hdr = pico_zalloc((size_t)(sizeof(struct pico_dhcp_hdr) + optlen));
-      /* specific options */
-      offset = (uint16_t)(offset + pico_dhcp_opt_maxmsgsize(&hdr->options[offset], DHCP_CLIENT_MAXMSGZISE));
-      break;
+        dhcpc_dbg("DHCP client: sent DHCPDISCOVER\n");
+        optlen = PICO_DHCP_OPTLEN_MSGTYPE + PICO_DHCP_OPTLEN_MAXMSGSIZE + PICO_DHCP_OPTLEN_PARAMLIST + PICO_DHCP_OPTLEN_END;
+        hdr = pico_zalloc((size_t)(sizeof(struct pico_dhcp_hdr) + optlen));
+        /* specific options */
+        offset = (uint16_t)(offset + pico_dhcp_opt_maxmsgsize(&hdr->options[offset], DHCP_CLIENT_MAXMSGZISE));
+        break;
 
     case PICO_DHCP_MSG_REQUEST:
-      dhcpc_dbg("DHCP client: sent DHCPREQUEST\n");
-      optlen = PICO_DHCP_OPTLEN_MSGTYPE + PICO_DHCP_OPTLEN_MAXMSGSIZE + PICO_DHCP_OPTLEN_PARAMLIST + PICO_DHCP_OPTLEN_REQIP + PICO_DHCP_OPTLEN_SERVERID 
-              + PICO_DHCP_OPTLEN_END;
-      hdr = pico_zalloc(sizeof(struct pico_dhcp_hdr) + optlen);
-      /* specific options */
-      offset = (uint16_t)(offset+pico_dhcp_opt_maxmsgsize(&hdr->options[offset], DHCP_CLIENT_MAXMSGZISE));
-      if (dhcpc->state == DHCP_CLIENT_STATE_REQUESTING) {
-        offset = (uint16_t)(offset+pico_dhcp_opt_reqip(&hdr->options[offset], &dhcpc->address));
-        offset = (uint16_t)(offset+pico_dhcp_opt_serverid(&hdr->options[offset], &dhcpc->server_id));
-      }
-      break;
+        dhcpc_dbg("DHCP client: sent DHCPREQUEST\n");
+        optlen = PICO_DHCP_OPTLEN_MSGTYPE + PICO_DHCP_OPTLEN_MAXMSGSIZE + PICO_DHCP_OPTLEN_PARAMLIST + PICO_DHCP_OPTLEN_REQIP + PICO_DHCP_OPTLEN_SERVERID
+                 + PICO_DHCP_OPTLEN_END;
+        hdr = pico_zalloc(sizeof(struct pico_dhcp_hdr) + optlen);
+        /* specific options */
+        offset = (uint16_t)(offset + pico_dhcp_opt_maxmsgsize(&hdr->options[offset], DHCP_CLIENT_MAXMSGZISE));
+        if (dhcpc->state == DHCP_CLIENT_STATE_REQUESTING) {
+            offset = (uint16_t)(offset + pico_dhcp_opt_reqip(&hdr->options[offset], &dhcpc->address));
+            offset = (uint16_t)(offset + pico_dhcp_opt_serverid(&hdr->options[offset], &dhcpc->server_id));
+        }
+
+        break;
 
     default:
-      return -1;
-  }
+        return -1;
+    }
 
-  /* common options */
-  offset = (uint16_t)(offset+pico_dhcp_opt_msgtype(&hdr->options[offset], msg_type));
-  offset = (uint16_t)(offset+pico_dhcp_opt_paramlist(&hdr->options[offset]));
-  offset = (uint16_t)(offset+pico_dhcp_opt_end(&hdr->options[offset]));
+    /* common options */
+    offset = (uint16_t)(offset + pico_dhcp_opt_msgtype(&hdr->options[offset], msg_type));
+    offset = (uint16_t)(offset + pico_dhcp_opt_paramlist(&hdr->options[offset]));
+    offset = (uint16_t)(offset + pico_dhcp_opt_end(&hdr->options[offset]));
 
-  switch (dhcpc->state)
-  {
+    switch (dhcpc->state)
+    {
     case DHCP_CLIENT_STATE_BOUND:
-      destination.addr = dhcpc->server_id.addr;
-      hdr->ciaddr = dhcpc->address.addr;
-      break;
+        destination.addr = dhcpc->server_id.addr;
+        hdr->ciaddr = dhcpc->address.addr;
+        break;
 
     case DHCP_CLIENT_STATE_RENEWING:
-      destination.addr = dhcpc->server_id.addr;
-      hdr->ciaddr = dhcpc->address.addr;
-      break;
+        destination.addr = dhcpc->server_id.addr;
+        hdr->ciaddr = dhcpc->address.addr;
+        break;
 
     case DHCP_CLIENT_STATE_REBINDING:
-      hdr->ciaddr = dhcpc->address.addr;
-      break;
+        hdr->ciaddr = dhcpc->address.addr;
+        break;
 
     default:
-      /* do nothing */
-      break;
-  }
+        /* do nothing */
+        break;
+    }
 
-  /* header information */
-  hdr->op = PICO_DHCP_OP_REQUEST;
-  hdr->htype = PICO_DHCP_HTYPE_ETH;
-  hdr->hlen = PICO_SIZE_ETH;
-  hdr->xid = dhcpc->xid;
-  hdr->flags = short_be(PICO_DHCP_FLAG_BROADCAST);
-  hdr->dhcp_magic = PICO_DHCPD_MAGIC_COOKIE;
-  /* copy client hardware address */
-  memcpy(hdr->hwaddr, &dhcpc->dev->eth->mac, PICO_SIZE_ETH);
+    /* header information */
+    hdr->op = PICO_DHCP_OP_REQUEST;
+    hdr->htype = PICO_DHCP_HTYPE_ETH;
+    hdr->hlen = PICO_SIZE_ETH;
+    hdr->xid = dhcpc->xid;
+    hdr->flags = short_be(PICO_DHCP_FLAG_BROADCAST);
+    hdr->dhcp_magic = PICO_DHCPD_MAGIC_COOKIE;
+    /* copy client hardware address */
+    memcpy(hdr->hwaddr, &dhcpc->dev->eth->mac, PICO_SIZE_ETH);
 
-  r = pico_socket_sendto(dhcpc->s, hdr, (int)(sizeof(struct pico_dhcp_hdr) + optlen), &destination, PICO_DHCPD_PORT);
-  pico_free(hdr);
-  if (r < 0)
-    return -1;
+    r = pico_socket_sendto(dhcpc->s, hdr, (int)(sizeof(struct pico_dhcp_hdr) + optlen), &destination, PICO_DHCPD_PORT);
+    pico_free(hdr);
+    if (r < 0)
+        return -1;
 
-  return 0;
+    return 0;
 }
 
 static void pico_dhcp_client_wakeup(uint16_t ev, struct pico_socket *s)
 {
-  uint8_t buf[DHCP_CLIENT_MAXMSGZISE] = {0};
-  int r = 0;
-  struct pico_dhcp_hdr *hdr = NULL;
-  struct pico_dhcp_client_cookie *dhcpc = NULL;
+    uint8_t buf[DHCP_CLIENT_MAXMSGZISE] = {
+        0
+    };
+    int r = 0;
+    struct pico_dhcp_hdr *hdr = NULL;
+    struct pico_dhcp_client_cookie *dhcpc = NULL;
+
+    if (ev != PICO_SOCK_EV_RD)
+        return;
 
-  if (ev != PICO_SOCK_EV_RD)
-    return;
-  r = pico_socket_recvfrom(s, buf, DHCP_CLIENT_MAXMSGZISE, NULL, NULL);
-  if (r < 0)
-    return;
+    r = pico_socket_recvfrom(s, buf, DHCP_CLIENT_MAXMSGZISE, NULL, NULL);
+    if (r < 0)
+        return;
 
-  /* If the 'xid' of an arriving message does not match the 'xid' 
-   * of the most recent transmitted message, the message must be 
-   * silently discarded. */
-  hdr = (struct pico_dhcp_hdr *)buf;
-  dhcpc = pico_dhcp_client_find_cookie(hdr->xid);
-  if (!dhcpc)
-    return;
-  dhcpc->event = (uint8_t)pico_dhcp_client_opt_parse(buf, (uint16_t)r);
-  pico_dhcp_state_machine(dhcpc->event, dhcpc, buf);
+    /* If the 'xid' of an arriving message does not match the 'xid'
+     * of the most recent transmitted message, the message must be
+     * silently discarded. */
+    hdr = (struct pico_dhcp_hdr *)buf;
+    dhcpc = pico_dhcp_client_find_cookie(hdr->xid);
+    if (!dhcpc)
+        return;
+
+    dhcpc->event = (uint8_t)pico_dhcp_client_opt_parse(buf, (uint16_t)r);
+    pico_dhcp_state_machine(dhcpc->event, dhcpc, buf);
 }
 
 void *pico_dhcp_get_identifier(uint32_t xid)
 {
-  return (void *)pico_dhcp_client_find_cookie(xid);
+    return (void *)pico_dhcp_client_find_cookie(xid);
 }
 
-struct pico_ip4 pico_dhcp_get_address(void* dhcpc)
+struct pico_ip4 pico_dhcp_get_address(void*dhcpc)
 {
-  return ((struct pico_dhcp_client_cookie*)dhcpc)->address;
+    return ((struct pico_dhcp_client_cookie*)dhcpc)->address;
 }
 
-struct pico_ip4 pico_dhcp_get_gateway(void* dhcpc)
+struct pico_ip4 pico_dhcp_get_gateway(void*dhcpc)
 {
-  return ((struct pico_dhcp_client_cookie*)dhcpc)->gateway;
+    return ((struct pico_dhcp_client_cookie*)dhcpc)->gateway;
 }
 
 struct pico_ip4 pico_dhcp_get_netmask(void *dhcpc)
 {
-  return ((struct pico_dhcp_client_cookie*)dhcpc)->netmask;
+    return ((struct pico_dhcp_client_cookie*)dhcpc)->netmask;
 }
 
 
-struct pico_ip4 pico_dhcp_get_nameserver(void* dhcpc)
+struct pico_ip4 pico_dhcp_get_nameserver(void*dhcpc)
 {
-  return ((struct pico_dhcp_client_cookie*)dhcpc)->nameserver;
+    return ((struct pico_dhcp_client_cookie*)dhcpc)->nameserver;
 }
 #endif
--- a/modules/pico_dhcp_client.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_dhcp_client.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,23 +1,23 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_DHCP_CLIENT
 #define _INCLUDE_PICO_DHCP_CLIENT
 #include "pico_dhcp_common.h"
 #include "pico_addressing.h"
 #include "pico_protocol.h"
 
-int pico_dhcp_initiate_negotiation(struct pico_device *device, void (*callback)(void* cli, int code), uint32_t *xid);
+int pico_dhcp_initiate_negotiation(struct pico_device *device, void (*callback)(void*cli, int code), uint32_t *xid);
 void pico_dhcp_process_incoming_message(uint8_t *data, int len);
 void *pico_dhcp_get_identifier(uint32_t xid);
 struct pico_ip4 pico_dhcp_get_address(void *cli);
 struct pico_ip4 pico_dhcp_get_gateway(void *cli);
 struct pico_ip4 pico_dhcp_get_netmask(void *cli);
-struct pico_ip4 pico_dhcp_get_nameserver(void* cli);
+struct pico_ip4 pico_dhcp_get_nameserver(void*cli);
 
 /* possible codes for the callback */
 #define PICO_DHCP_SUCCESS 0
--- a/modules/pico_dhcp_common.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_dhcp_common.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-Authors: Frederik Van Slycken
-*********************************************************************/
+   Authors: Frederik Van Slycken
+ *********************************************************************/
 
 #include "pico_config.h"
 #include "pico_stack.h"
@@ -16,170 +16,172 @@
 /* The argument pointer is moved forward to the next option */
 struct pico_dhcp_opt *pico_dhcp_next_option(struct pico_dhcp_opt **ptr)
 {
-  uint8_t **p = (uint8_t **)ptr;
-  struct pico_dhcp_opt *opt = *ptr;
+    uint8_t **p = (uint8_t **)ptr;
+    struct pico_dhcp_opt *opt = *ptr;
+
+    if (opt->code == PICO_DHCP_OPT_END)
+        return NULL;
 
-	if (opt->code == PICO_DHCP_OPT_END)
-    return NULL;
-  if (opt->code == PICO_DHCP_OPT_PAD) {
-    *p += 1; 
-		return *ptr;
-  }
+    if (opt->code == PICO_DHCP_OPT_PAD) {
+        *p += 1;
+        return *ptr;
+    }
 
-  *p += (opt->len + 2); /* (len + 2) to account for code and len octet */
-  return *ptr;
+    *p += (opt->len + 2); /* (len + 2) to account for code and len octet */
+    return *ptr;
 }
 
 uint8_t pico_dhcp_are_options_valid(void *ptr, int32_t len)
 {
-	uint8_t optlen = 0, *p = ptr;
+    uint8_t optlen = 0, *p = ptr;
 
-	while (len > 0) {
-    switch (*p)
-    {
-      case PICO_DHCP_OPT_END:
-        return 1;
+    while (len > 0) {
+        switch (*p)
+        {
+        case PICO_DHCP_OPT_END:
+            return 1;
+
+        case PICO_DHCP_OPT_PAD:
+            p++;
+            len--;
+            break;
 
-      case PICO_DHCP_OPT_PAD:
-        p++;
-        len--;
-        break;
+        default:
+            p++; /* move pointer from code octet to len octet */
+            if ((--len <= 0) || (len - (*p + 1) < 0)) /* (*p + 1) to account for len octet */
+                return 0;
 
-      default:
-        p++; /* move pointer from code octet to len octet */
-        if ((--len <= 0) || (len - (*p + 1) < 0)) /* (*p + 1) to account for len octet */
-          return 0;
-        optlen = *p;
-        p += optlen + 1;
-        len -= optlen;
-        break;
+            optlen = *p;
+            p += optlen + 1;
+            len -= optlen;
+            break;
+        }
     }
-	}
-	return 0;
+    return 0;
 }
 
 uint8_t pico_dhcp_opt_netmask(void *ptr, struct pico_ip4 *ip)
 {
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
 
-  /* option: netmask */
-  opt->code = PICO_DHCP_OPT_NETMASK;
-  opt->len = PICO_DHCP_OPTLEN_NETMASK - PICO_DHCP_OPTLEN_HDR;
-  opt->ext.netmask.ip = *ip;
-  return PICO_DHCP_OPTLEN_NETMASK;
+    /* option: netmask */
+    opt->code = PICO_DHCP_OPT_NETMASK;
+    opt->len = PICO_DHCP_OPTLEN_NETMASK - PICO_DHCP_OPTLEN_HDR;
+    opt->ext.netmask.ip = *ip;
+    return PICO_DHCP_OPTLEN_NETMASK;
 }
 
 uint8_t pico_dhcp_opt_router(void *ptr, struct pico_ip4 *ip)
 {
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
 
-  /* option: router */
-  opt->code = PICO_DHCP_OPT_ROUTER;
-  opt->len = PICO_DHCP_OPTLEN_ROUTER - PICO_DHCP_OPTLEN_HDR;
-  opt->ext.router.ip = *ip;
-  return PICO_DHCP_OPTLEN_ROUTER;
+    /* option: router */
+    opt->code = PICO_DHCP_OPT_ROUTER;
+    opt->len = PICO_DHCP_OPTLEN_ROUTER - PICO_DHCP_OPTLEN_HDR;
+    opt->ext.router.ip = *ip;
+    return PICO_DHCP_OPTLEN_ROUTER;
 }
 
 uint8_t pico_dhcp_opt_dns(void *ptr, struct pico_ip4 *ip)
 {
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
 
-  /* option: dns */
-  opt->code = PICO_DHCP_OPT_DNS;
-  opt->len = PICO_DHCP_OPTLEN_DNS - PICO_DHCP_OPTLEN_HDR;
-  opt->ext.dns.ip = *ip;
-  return PICO_DHCP_OPTLEN_DNS;
+    /* option: dns */
+    opt->code = PICO_DHCP_OPT_DNS;
+    opt->len = PICO_DHCP_OPTLEN_DNS - PICO_DHCP_OPTLEN_HDR;
+    opt->ext.dns.ip = *ip;
+    return PICO_DHCP_OPTLEN_DNS;
 }
 
 uint8_t pico_dhcp_opt_broadcast(void *ptr, struct pico_ip4 *ip)
 {
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
 
-  /* option: broadcast */
-  opt->code = PICO_DHCP_OPT_BROADCAST;
-  opt->len = PICO_DHCP_OPTLEN_BROADCAST - PICO_DHCP_OPTLEN_HDR;
-  opt->ext.broadcast.ip = *ip;
-  return PICO_DHCP_OPTLEN_BROADCAST;
+    /* option: broadcast */
+    opt->code = PICO_DHCP_OPT_BROADCAST;
+    opt->len = PICO_DHCP_OPTLEN_BROADCAST - PICO_DHCP_OPTLEN_HDR;
+    opt->ext.broadcast.ip = *ip;
+    return PICO_DHCP_OPTLEN_BROADCAST;
 }
 
 uint8_t pico_dhcp_opt_reqip(void *ptr, struct pico_ip4 *ip)
 {
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
 
-  /* option: request IP address */
-  opt->code = PICO_DHCP_OPT_REQIP;
-  opt->len = PICO_DHCP_OPTLEN_REQIP - PICO_DHCP_OPTLEN_HDR;
-  opt->ext.req_ip.ip = *ip;
-  return PICO_DHCP_OPTLEN_REQIP;
+    /* option: request IP address */
+    opt->code = PICO_DHCP_OPT_REQIP;
+    opt->len = PICO_DHCP_OPTLEN_REQIP - PICO_DHCP_OPTLEN_HDR;
+    opt->ext.req_ip.ip = *ip;
+    return PICO_DHCP_OPTLEN_REQIP;
 }
 
 uint8_t pico_dhcp_opt_leasetime(void *ptr, uint32_t time)
 {
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
 
-  /* option: lease time */
-  opt->code = PICO_DHCP_OPT_LEASETIME;
-  opt->len = PICO_DHCP_OPTLEN_LEASETIME - PICO_DHCP_OPTLEN_HDR;
-  opt->ext.lease_time.time = time;
-  return PICO_DHCP_OPTLEN_LEASETIME;
+    /* option: lease time */
+    opt->code = PICO_DHCP_OPT_LEASETIME;
+    opt->len = PICO_DHCP_OPTLEN_LEASETIME - PICO_DHCP_OPTLEN_HDR;
+    opt->ext.lease_time.time = time;
+    return PICO_DHCP_OPTLEN_LEASETIME;
 }
 
 uint8_t pico_dhcp_opt_msgtype(void *ptr, uint8_t type)
 {
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
 
-  /* option: message type */
-  opt->code = PICO_DHCP_OPT_MSGTYPE;
-  opt->len = PICO_DHCP_OPTLEN_MSGTYPE - PICO_DHCP_OPTLEN_HDR;
-  opt->ext.msg_type.type = type;
-  return PICO_DHCP_OPTLEN_MSGTYPE;
+    /* option: message type */
+    opt->code = PICO_DHCP_OPT_MSGTYPE;
+    opt->len = PICO_DHCP_OPTLEN_MSGTYPE - PICO_DHCP_OPTLEN_HDR;
+    opt->ext.msg_type.type = type;
+    return PICO_DHCP_OPTLEN_MSGTYPE;
 }
 
 uint8_t pico_dhcp_opt_serverid(void *ptr, struct pico_ip4 *ip)
 {
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
 
-  /* option: server identifier */
-  opt->code = PICO_DHCP_OPT_SERVERID;
-  opt->len = PICO_DHCP_OPTLEN_SERVERID - PICO_DHCP_OPTLEN_HDR;
-  opt->ext.server_id.ip = *ip;
-  return PICO_DHCP_OPTLEN_SERVERID;
+    /* option: server identifier */
+    opt->code = PICO_DHCP_OPT_SERVERID;
+    opt->len = PICO_DHCP_OPTLEN_SERVERID - PICO_DHCP_OPTLEN_HDR;
+    opt->ext.server_id.ip = *ip;
+    return PICO_DHCP_OPTLEN_SERVERID;
 }
 
 uint8_t pico_dhcp_opt_paramlist(void *ptr)
 {
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
 
-  /* option: parameter list */
-  opt->code = PICO_DHCP_OPT_PARAMLIST;
-  opt->len = PICO_DHCP_OPTLEN_PARAMLIST - PICO_DHCP_OPTLEN_HDR;
-  opt->ext.param_list.code[0] = PICO_DHCP_OPT_NETMASK;
-  opt->ext.param_list.code[1] = PICO_DHCP_OPT_TIME;
-  opt->ext.param_list.code[2] = PICO_DHCP_OPT_ROUTER;
-  opt->ext.param_list.code[3] = PICO_DHCP_OPT_HOSTNAME;
-  opt->ext.param_list.code[4] = PICO_DHCP_OPT_RENEWALTIME;
-  opt->ext.param_list.code[5] = PICO_DHCP_OPT_REBINDINGTIME;
-  return PICO_DHCP_OPTLEN_PARAMLIST;
+    /* option: parameter list */
+    opt->code = PICO_DHCP_OPT_PARAMLIST;
+    opt->len = PICO_DHCP_OPTLEN_PARAMLIST - PICO_DHCP_OPTLEN_HDR;
+    opt->ext.param_list.code[0] = PICO_DHCP_OPT_NETMASK;
+    opt->ext.param_list.code[1] = PICO_DHCP_OPT_TIME;
+    opt->ext.param_list.code[2] = PICO_DHCP_OPT_ROUTER;
+    opt->ext.param_list.code[3] = PICO_DHCP_OPT_HOSTNAME;
+    opt->ext.param_list.code[4] = PICO_DHCP_OPT_RENEWALTIME;
+    opt->ext.param_list.code[5] = PICO_DHCP_OPT_REBINDINGTIME;
+    return PICO_DHCP_OPTLEN_PARAMLIST;
 }
 
 uint8_t pico_dhcp_opt_maxmsgsize(void *ptr, uint16_t size)
 {
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)ptr;
 
-  /* option: maximum message size */
-  opt->code = PICO_DHCP_OPT_MAXMSGSIZE;
-  opt->len = PICO_DHCP_OPTLEN_MAXMSGSIZE - PICO_DHCP_OPTLEN_HDR;
-  opt->ext.max_msg_size.size = short_be(size);
-  return PICO_DHCP_OPTLEN_MAXMSGSIZE;
+    /* option: maximum message size */
+    opt->code = PICO_DHCP_OPT_MAXMSGSIZE;
+    opt->len = PICO_DHCP_OPTLEN_MAXMSGSIZE - PICO_DHCP_OPTLEN_HDR;
+    opt->ext.max_msg_size.size = short_be(size);
+    return PICO_DHCP_OPTLEN_MAXMSGSIZE;
 }
 
 uint8_t pico_dhcp_opt_end(void *ptr)
 {
-  uint8_t *opt = (uint8_t *)ptr;
+    uint8_t *opt = (uint8_t *)ptr;
 
-  /* option: end of options */
-  *opt = PICO_DHCP_OPT_END;
-  return PICO_DHCP_OPTLEN_END;
+    /* option: end of options */
+    *opt = PICO_DHCP_OPT_END;
+    return PICO_DHCP_OPTLEN_END;
 }
 
 #endif
--- a/modules/pico_dhcp_common.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_dhcp_common.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_DHCP_COMMON
 #define _INCLUDE_PICO_DHCP_COMMON
 #include "pico_addressing.h"
@@ -84,85 +84,85 @@
 
 struct __attribute__((packed)) pico_dhcp_hdr
 {
-	uint8_t op;
-	uint8_t htype;
-	uint8_t hlen;
-	uint8_t hops; //zero
-	uint32_t xid; //store this in the request
-	uint16_t secs; // ignore
-	uint16_t flags;
-	uint32_t ciaddr; // client address - if asking for renewal
-	uint32_t yiaddr; // your address (client)
-	uint32_t siaddr; // dhcp offered address
-	uint32_t giaddr; // relay agent, bootp.
-	uint8_t hwaddr[6];
-	uint8_t hwaddr_padding[10];
-	char    hostname[64];
-	char    bootp_filename[128];
-	uint32_t dhcp_magic;
-	uint8_t options[];
+    uint8_t op;
+    uint8_t htype;
+    uint8_t hlen;
+    uint8_t hops; /* zero */
+    uint32_t xid; /* store this in the request */
+    uint16_t secs; /* ignore */
+    uint16_t flags;
+    uint32_t ciaddr; /* client address - if asking for renewal */
+    uint32_t yiaddr; /* your address (client) */
+    uint32_t siaddr; /* dhcp offered address */
+    uint32_t giaddr; /* relay agent, bootp. */
+    uint8_t hwaddr[6];
+    uint8_t hwaddr_padding[10];
+    char hostname[64];
+    char bootp_filename[128];
+    uint32_t dhcp_magic;
+    uint8_t options[];
 };
 
 struct __attribute__((packed)) pico_dhcp_opt
 {
-  uint8_t code;
-  uint8_t len;
-  union {
-    struct {
-      struct pico_ip4 ip;
-    } netmask;
-    struct {
-      struct pico_ip4 ip;
-    } router;
-    struct {
-      struct pico_ip4 ip;
-    } dns;
-    struct {
-      struct pico_ip4 ip;
-    } broadcast;
-    struct {
-      struct pico_ip4 ip;
-    } req_ip;
-    struct {
-      uint32_t time;
-    } lease_time;
-    struct {
-      uint8_t value;
-    } opt_overload;
-    struct {
-      char name[1];
-    } tftp_server;
-    struct {
-      char name[1];
-    } bootfile;
-    struct {
-      uint8_t type;
-    } msg_type;
-    struct {
-      struct pico_ip4 ip;
-    } server_id;
-    struct {
-      uint8_t code[1];
-    } param_list;
-    struct {
-      char error[1];
-    } message;
-    struct {
-      uint16_t size;
-    } max_msg_size;
-    struct {
-      uint32_t time;
-    } renewal_time;
-    struct {
-      uint32_t time;
-    } rebinding_time;
-    struct {
-      uint8_t id[1];
-    } vendor_id;
-    struct {
-      uint8_t id[1];
-    } client_id;
-  } ext;
+    uint8_t code;
+    uint8_t len;
+    union {
+        struct {
+            struct pico_ip4 ip;
+        } netmask;
+        struct {
+            struct pico_ip4 ip;
+        } router;
+        struct {
+            struct pico_ip4 ip;
+        } dns;
+        struct {
+            struct pico_ip4 ip;
+        } broadcast;
+        struct {
+            struct pico_ip4 ip;
+        } req_ip;
+        struct {
+            uint32_t time;
+        } lease_time;
+        struct {
+            uint8_t value;
+        } opt_overload;
+        struct {
+            char name[1];
+        } tftp_server;
+        struct {
+            char name[1];
+        } bootfile;
+        struct {
+            uint8_t type;
+        } msg_type;
+        struct {
+            struct pico_ip4 ip;
+        } server_id;
+        struct {
+            uint8_t code[1];
+        } param_list;
+        struct {
+            char error[1];
+        } message;
+        struct {
+            uint16_t size;
+        } max_msg_size;
+        struct {
+            uint32_t time;
+        } renewal_time;
+        struct {
+            uint32_t time;
+        } rebinding_time;
+        struct {
+            uint8_t id[1];
+        } vendor_id;
+        struct {
+            uint8_t id[1];
+        } client_id;
+    } ext;
 };
 
 uint8_t dhcp_get_next_option(uint8_t *begin, uint8_t *data, int *len, uint8_t **nextopt);
--- a/modules/pico_dhcp_server.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_dhcp_server.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
 
-Authors: Frederik Van Slycken, Kristof Roelants
-*********************************************************************/
+   Authors: Frederik Van Slycken, Kristof Roelants
+ *********************************************************************/
 
 #ifdef PICO_SUPPORT_DHCPD
 #include "pico_dhcp_server.h"
@@ -15,10 +15,10 @@
 #include "pico_stack.h"
 #include "pico_arp.h"
 
-#define dhcps_dbg(...) do{}while(0)
-//#define dhcps_dbg dbg
+#define dhcps_dbg(...) do {} while(0)
+/* #define dhcps_dbg dbg */
 
-/* default configurations */ 
+/* default configurations */
 #define DHCP_SERVER_OPENDNS    long_be(0xd043dede) /* OpenDNS DNS server 208.67.222.222 */
 #define DHCP_SERVER_POOL_START long_be(0x00000064)
 #define DHCP_SERVER_POOL_END   long_be(0x000000fe)
@@ -30,288 +30,317 @@
 #define ip_inrange(x) ((long_be(x) >= long_be(dhcpn->dhcps->pool_start)) && (long_be(x) <= long_be(dhcpn->dhcps->pool_end)))
 
 enum dhcp_server_state {
-  PICO_DHCP_STATE_DISCOVER = 0,
-  PICO_DHCP_STATE_OFFER,
-  PICO_DHCP_STATE_REQUEST,
-  PICO_DHCP_STATE_BOUND,
-  PICO_DHCP_STATE_RENEWING
+    PICO_DHCP_STATE_DISCOVER = 0,
+    PICO_DHCP_STATE_OFFER,
+    PICO_DHCP_STATE_REQUEST,
+    PICO_DHCP_STATE_BOUND,
+    PICO_DHCP_STATE_RENEWING
 };
 
 struct pico_dhcp_server_negotiation {
-  uint32_t xid;
-  enum dhcp_server_state state;
-  struct pico_dhcp_server_setting *dhcps;
-  struct pico_ip4 ciaddr;
-  struct pico_eth hwaddr;
+    uint32_t xid;
+    enum dhcp_server_state state;
+    struct pico_dhcp_server_setting *dhcps;
+    struct pico_ip4 ciaddr;
+    struct pico_eth hwaddr;
 };
 
 static void pico_dhcpd_wakeup(uint16_t ev, struct pico_socket *s);
 
 static int dhcp_settings_cmp(void *ka, void *kb)
 {
-  struct pico_dhcp_server_setting *a = ka, *b = kb;
-  if (a->dev == b->dev)
-    return 0;
-  return (a->dev < b->dev) ? -1 : 1;
-} 
+    struct pico_dhcp_server_setting *a = ka, *b = kb;
+    if (a->dev == b->dev)
+        return 0;
+
+    return (a->dev < b->dev) ? -1 : 1;
+}
 PICO_TREE_DECLARE(DHCPSettings, dhcp_settings_cmp);
 
 static int dhcp_negotiations_cmp(void *ka, void *kb)
 {
-  struct pico_dhcp_server_negotiation *a = ka, *b = kb;
-  if (a->xid == b->xid)
-    return 0;
-  return (a->xid < b->xid) ? -1 : 1;
-} 
+    struct pico_dhcp_server_negotiation *a = ka, *b = kb;
+    if (a->xid == b->xid)
+        return 0;
+
+    return (a->xid < b->xid) ? -1 : 1;
+}
 PICO_TREE_DECLARE(DHCPNegotiations, dhcp_negotiations_cmp);
 
 static struct pico_dhcp_server_setting *pico_dhcp_server_add_setting(struct pico_dhcp_server_setting *setting)
 {
-  uint16_t port = PICO_DHCPD_PORT;
-  struct pico_dhcp_server_setting *dhcps = NULL, *found = NULL, test = {0};
-  struct pico_ipv4_link *link = NULL;
-  
-  link = pico_ipv4_link_get(&setting->server_ip);
-  if (!link) {
-    pico_err = PICO_ERR_EINVAL;
-    return NULL;
-  }
-  test.dev = setting->dev;
-  found = pico_tree_findKey(&DHCPSettings, &test);
-  if (found) {
-    pico_err = PICO_ERR_EINVAL;
-    return NULL;
-  }
-  dhcps = pico_zalloc(sizeof(struct pico_dhcp_server_setting));
-  if (!dhcps) {
-    pico_err = PICO_ERR_ENOMEM;
-    return NULL;
-  }
+    uint16_t port = PICO_DHCPD_PORT;
+    struct pico_dhcp_server_setting *dhcps = NULL, *found = NULL, test = {
+        0
+    };
+    struct pico_ipv4_link *link = NULL;
+
+    link = pico_ipv4_link_get(&setting->server_ip);
+    if (!link) {
+        pico_err = PICO_ERR_EINVAL;
+        return NULL;
+    }
+
+    test.dev = setting->dev;
+    found = pico_tree_findKey(&DHCPSettings, &test);
+    if (found) {
+        pico_err = PICO_ERR_EINVAL;
+        return NULL;
+    }
+
+    dhcps = pico_zalloc(sizeof(struct pico_dhcp_server_setting));
+    if (!dhcps) {
+        pico_err = PICO_ERR_ENOMEM;
+        return NULL;
+    }
 
-  dhcps->lease_time = setting->lease_time;
-  dhcps->pool_start = setting->pool_start;
-  dhcps->pool_next = setting->pool_next;
-  dhcps->pool_end = setting->pool_end;
-  dhcps->dev = link->dev;
-  dhcps->server_ip = link->address;
-  dhcps->netmask = link->netmask;
+    dhcps->lease_time = setting->lease_time;
+    dhcps->pool_start = setting->pool_start;
+    dhcps->pool_next = setting->pool_next;
+    dhcps->pool_end = setting->pool_end;
+    dhcps->dev = link->dev;
+    dhcps->server_ip = link->address;
+    dhcps->netmask = link->netmask;
+
+    /* default values if not provided */
+    if (!dhcps->pool_start)
+        dhcps->pool_start = (dhcps->server_ip.addr & dhcps->netmask.addr) | DHCP_SERVER_POOL_START;
+
+    if (!dhcps->pool_end)
+        dhcps->pool_end = (dhcps->server_ip.addr & dhcps->netmask.addr) | DHCP_SERVER_POOL_END;
+
+    if (!dhcps->lease_time)
+        dhcps->lease_time = DHCP_SERVER_LEASE_TIME;
 
-  /* default values if not provided */
-  if (!dhcps->pool_start)
-    dhcps->pool_start = (dhcps->server_ip.addr & dhcps->netmask.addr) | DHCP_SERVER_POOL_START;
-  if (!dhcps->pool_end)
-    dhcps->pool_end = (dhcps->server_ip.addr & dhcps->netmask.addr) | DHCP_SERVER_POOL_END;
-  if (!dhcps->lease_time)
-    dhcps->lease_time = DHCP_SERVER_LEASE_TIME;
-  dhcps->pool_next = dhcps->pool_start;
+    dhcps->pool_next = dhcps->pool_start;
+
+    dhcps->s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &pico_dhcpd_wakeup);
+    if (!dhcps->s) {
+        dhcps_dbg("DHCP server ERROR: failure opening socket (%s)\n", strerror(pico_err));
+        pico_free(dhcps);
+        return NULL;
+    }
 
-  dhcps->s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &pico_dhcpd_wakeup);
-  if (!dhcps->s) {
-    dhcps_dbg("DHCP server ERROR: failure opening socket (%s)\n", strerror(pico_err));
-    pico_free(dhcps);
-    return NULL;
-  }
-  if (pico_socket_bind(dhcps->s, &dhcps->server_ip, &port) < 0) {
-    dhcps_dbg("DHCP server ERROR: failure binding socket (%s)\n", strerror(pico_err));
-    pico_free(dhcps);
-    return NULL;
-  }
-  
-  pico_tree_insert(&DHCPSettings, dhcps);
-  return dhcps;
+    if (pico_socket_bind(dhcps->s, &dhcps->server_ip, &port) < 0) {
+        dhcps_dbg("DHCP server ERROR: failure binding socket (%s)\n", strerror(pico_err));
+        pico_free(dhcps);
+        return NULL;
+    }
+
+    pico_tree_insert(&DHCPSettings, dhcps);
+    return dhcps;
 }
 
 static struct pico_dhcp_server_negotiation *pico_dhcp_server_find_negotiation(uint32_t xid)
 {
-  struct pico_dhcp_server_negotiation test = {0}, *found = NULL;
+    struct pico_dhcp_server_negotiation test = {
+        0
+    }, *found = NULL;
 
-  test.xid = xid;
-  found = pico_tree_findKey(&DHCPNegotiations, &test);
-  if (found)
-    return found;
-  else
-    return NULL;
+    test.xid = xid;
+    found = pico_tree_findKey(&DHCPNegotiations, &test);
+    if (found)
+        return found;
+    else
+        return NULL;
 }
 
 static struct pico_dhcp_server_negotiation *pico_dhcp_server_add_negotiation(struct pico_device *dev, struct pico_dhcp_hdr *hdr)
 {
-  struct pico_dhcp_server_negotiation *dhcpn = NULL;
-  struct pico_dhcp_server_setting test = {0};
-  struct pico_ip4 *ciaddr = NULL;
+    struct pico_dhcp_server_negotiation *dhcpn = NULL;
+    struct pico_dhcp_server_setting test = {
+        0
+    };
+    struct pico_ip4 *ciaddr = NULL;
 
-  if (pico_dhcp_server_find_negotiation(hdr->xid))
-    return NULL;
+    if (pico_dhcp_server_find_negotiation(hdr->xid))
+        return NULL;
 
-  dhcpn = pico_zalloc(sizeof(struct pico_dhcp_server_negotiation));
-  if (!dhcpn) {
-    pico_err = PICO_ERR_ENOMEM;
-    return NULL;
-  }
+    dhcpn = pico_zalloc(sizeof(struct pico_dhcp_server_negotiation));
+    if (!dhcpn) {
+        pico_err = PICO_ERR_ENOMEM;
+        return NULL;
+    }
 
-  dhcpn->xid = hdr->xid;
-  dhcpn->state = PICO_DHCP_STATE_DISCOVER;
-  memcpy(dhcpn->hwaddr.addr, hdr->hwaddr, PICO_SIZE_ETH);
+    dhcpn->xid = hdr->xid;
+    dhcpn->state = PICO_DHCP_STATE_DISCOVER;
+    memcpy(dhcpn->hwaddr.addr, hdr->hwaddr, PICO_SIZE_ETH);
 
-  test.dev = dev;
-  dhcpn->dhcps = pico_tree_findKey(&DHCPSettings, &test);
-  if (!dhcpn->dhcps) {
-    dhcps_dbg("DHCP server WARNING: received DHCP message on unconfigured link %s\n", dev->name);
-    pico_free(dhcpn);
-    return NULL;
-  }
+    test.dev = dev;
+    dhcpn->dhcps = pico_tree_findKey(&DHCPSettings, &test);
+    if (!dhcpn->dhcps) {
+        dhcps_dbg("DHCP server WARNING: received DHCP message on unconfigured link %s\n", dev->name);
+        pico_free(dhcpn);
+        return NULL;
+    }
 
-  ciaddr = pico_arp_reverse_lookup(&dhcpn->hwaddr);
-  if (!ciaddr) {
-    dhcpn->ciaddr.addr = dhcpn->dhcps->pool_next;
-    dhcpn->dhcps->pool_next = long_be(long_be(dhcpn->dhcps->pool_next) + 1);
-    pico_arp_create_entry(dhcpn->hwaddr.addr, dhcpn->ciaddr, dhcpn->dhcps->dev);
-  } else {
-    dhcpn->ciaddr = *ciaddr;
-  }
+    ciaddr = pico_arp_reverse_lookup(&dhcpn->hwaddr);
+    if (!ciaddr) {
+        dhcpn->ciaddr.addr = dhcpn->dhcps->pool_next;
+        dhcpn->dhcps->pool_next = long_be(long_be(dhcpn->dhcps->pool_next) + 1);
+        pico_arp_create_entry(dhcpn->hwaddr.addr, dhcpn->ciaddr, dhcpn->dhcps->dev);
+    } else {
+        dhcpn->ciaddr = *ciaddr;
+    }
 
-  pico_tree_insert(&DHCPNegotiations, dhcpn);
-  return dhcpn; 
+    pico_tree_insert(&DHCPNegotiations, dhcpn);
+    return dhcpn;
 }
 
 static void dhcpd_make_reply(struct pico_dhcp_server_negotiation *dhcpn, uint8_t msg_type)
 {
-  int r = 0, optlen = 0, offset = 0;
-  struct pico_ip4 broadcast = {0}, dns = {0}, destination = { .addr = 0xFFFFFFFF};
-  struct pico_dhcp_hdr *hdr = NULL;
+    int r = 0, optlen = 0, offset = 0;
+    struct pico_ip4 broadcast = {
+        0
+    }, dns = {
+        0
+    }, destination = {
+        .addr = 0xFFFFFFFF
+    };
+    struct pico_dhcp_hdr *hdr = NULL;
 
-  dns.addr = DHCP_SERVER_OPENDNS;
-  broadcast.addr = dhcpn->dhcps->server_ip.addr | ~(dhcpn->dhcps->netmask.addr);
+    dns.addr = DHCP_SERVER_OPENDNS;
+    broadcast.addr = dhcpn->dhcps->server_ip.addr | ~(dhcpn->dhcps->netmask.addr);
 
-  optlen = PICO_DHCP_OPTLEN_MSGTYPE + PICO_DHCP_OPTLEN_SERVERID + PICO_DHCP_OPTLEN_LEASETIME + PICO_DHCP_OPTLEN_NETMASK + PICO_DHCP_OPTLEN_ROUTER 
-          + PICO_DHCP_OPTLEN_BROADCAST + PICO_DHCP_OPTLEN_DNS + PICO_DHCP_OPTLEN_END;
-  hdr = pico_zalloc(sizeof(struct pico_dhcp_hdr) + (uint32_t)optlen);
+    optlen = PICO_DHCP_OPTLEN_MSGTYPE + PICO_DHCP_OPTLEN_SERVERID + PICO_DHCP_OPTLEN_LEASETIME + PICO_DHCP_OPTLEN_NETMASK + PICO_DHCP_OPTLEN_ROUTER
+             + PICO_DHCP_OPTLEN_BROADCAST + PICO_DHCP_OPTLEN_DNS + PICO_DHCP_OPTLEN_END;
+    hdr = pico_zalloc(sizeof(struct pico_dhcp_hdr) + (uint32_t)optlen);
 
-  hdr->op = PICO_DHCP_OP_REPLY;
-  hdr->htype = PICO_DHCP_HTYPE_ETH;
-  hdr->hlen = PICO_SIZE_ETH;
-  hdr->xid = dhcpn->xid;
-  hdr->yiaddr = dhcpn->ciaddr.addr;
-  hdr->siaddr = dhcpn->dhcps->server_ip.addr;
-  hdr->dhcp_magic = PICO_DHCPD_MAGIC_COOKIE;
-  memcpy(hdr->hwaddr, dhcpn->hwaddr.addr, PICO_SIZE_ETH);
+    hdr->op = PICO_DHCP_OP_REPLY;
+    hdr->htype = PICO_DHCP_HTYPE_ETH;
+    hdr->hlen = PICO_SIZE_ETH;
+    hdr->xid = dhcpn->xid;
+    hdr->yiaddr = dhcpn->ciaddr.addr;
+    hdr->siaddr = dhcpn->dhcps->server_ip.addr;
+    hdr->dhcp_magic = PICO_DHCPD_MAGIC_COOKIE;
+    memcpy(hdr->hwaddr, dhcpn->hwaddr.addr, PICO_SIZE_ETH);
 
-  /* options */
-  offset += pico_dhcp_opt_msgtype(&hdr->options[offset], msg_type);
-  offset += pico_dhcp_opt_serverid(&hdr->options[offset], &dhcpn->dhcps->server_ip);
-  offset += pico_dhcp_opt_leasetime(&hdr->options[offset], dhcpn->dhcps->lease_time);
-  offset += pico_dhcp_opt_netmask(&hdr->options[offset], &dhcpn->dhcps->netmask);
-  offset += pico_dhcp_opt_router(&hdr->options[offset], &dhcpn->dhcps->server_ip);
-  offset += pico_dhcp_opt_broadcast(&hdr->options[offset], &broadcast);
-  offset += pico_dhcp_opt_dns(&hdr->options[offset], &dns);
-  offset += pico_dhcp_opt_end(&hdr->options[offset]);
+    /* options */
+    offset += pico_dhcp_opt_msgtype(&hdr->options[offset], msg_type);
+    offset += pico_dhcp_opt_serverid(&hdr->options[offset], &dhcpn->dhcps->server_ip);
+    offset += pico_dhcp_opt_leasetime(&hdr->options[offset], dhcpn->dhcps->lease_time);
+    offset += pico_dhcp_opt_netmask(&hdr->options[offset], &dhcpn->dhcps->netmask);
+    offset += pico_dhcp_opt_router(&hdr->options[offset], &dhcpn->dhcps->server_ip);
+    offset += pico_dhcp_opt_broadcast(&hdr->options[offset], &broadcast);
+    offset += pico_dhcp_opt_dns(&hdr->options[offset], &dns);
+    offset += pico_dhcp_opt_end(&hdr->options[offset]);
 
-  destination.addr = hdr->yiaddr;
-  r = pico_socket_sendto(dhcpn->dhcps->s, hdr, (int)(sizeof(struct pico_dhcp_hdr) + (uint32_t)optlen), &destination, PICO_DHCP_CLIENT_PORT);
-  if (r < 0)
-    dhcps_dbg("DHCP server WARNING: failure sending: %s!\n", strerror(pico_err));
+    destination.addr = hdr->yiaddr;
+    r = pico_socket_sendto(dhcpn->dhcps->s, hdr, (int)(sizeof(struct pico_dhcp_hdr) + (uint32_t)optlen), &destination, PICO_DHCP_CLIENT_PORT);
+    if (r < 0)
+        dhcps_dbg("DHCP server WARNING: failure sending: %s!\n", strerror(pico_err));
 
-  return;
+    return;
 }
 
 static void pico_dhcp_server_recv(struct pico_socket *s, uint8_t *buf, uint32_t len)
 {
-  uint8_t msgtype = 0;
-  int32_t optlen = (int32_t)(len - sizeof(struct pico_dhcp_hdr));
-  struct pico_dhcp_hdr *hdr = (struct pico_dhcp_hdr *)buf;
-  struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)hdr->options;
-  struct pico_dhcp_server_negotiation *dhcpn = NULL;
-  struct pico_ip4 reqip = {0}, server_id = {0};
-  struct pico_device *dev = NULL;
+    uint8_t msgtype = 0;
+    int32_t optlen = (int32_t)(len - sizeof(struct pico_dhcp_hdr));
+    struct pico_dhcp_hdr *hdr = (struct pico_dhcp_hdr *)buf;
+    struct pico_dhcp_opt *opt = (struct pico_dhcp_opt *)hdr->options;
+    struct pico_dhcp_server_negotiation *dhcpn = NULL;
+    struct pico_ip4 reqip = {
+        0
+    }, server_id = {
+        0
+    };
+    struct pico_device *dev = NULL;
 
-  if (!pico_dhcp_are_options_valid(hdr->options, optlen))
-    return;
+    if (!pico_dhcp_are_options_valid(hdr->options, optlen))
+        return;
+
+    dev = pico_ipv4_link_find(&s->local_addr.ip4);
+    dhcpn = pico_dhcp_server_find_negotiation(hdr->xid);
+    if (!dhcpn)
+        dhcpn = pico_dhcp_server_add_negotiation(dev, hdr);
+
+    if (!ip_inrange(dhcpn->ciaddr.addr))
+        return;
+
+    do {
+        switch (opt->code)
+        {
+        case PICO_DHCP_OPT_PAD:
+            break;
 
-  dev = pico_ipv4_link_find(&s->local_addr.ip4);
-  dhcpn = pico_dhcp_server_find_negotiation(hdr->xid);
-  if (!dhcpn)
-    dhcpn = pico_dhcp_server_add_negotiation(dev, hdr);
- 
-  if (!ip_inrange(dhcpn->ciaddr.addr))
-    return;
+        case PICO_DHCP_OPT_END:
+            break;
+
+        case PICO_DHCP_OPT_MSGTYPE:
+            msgtype = opt->ext.msg_type.type;
+            dhcps_dbg("DHCP server: message type %u\n", msgtype);
+            break;
+
+        case PICO_DHCP_OPT_REQIP:
+            reqip = opt->ext.req_ip.ip;
+            dhcps_dbg("DHCP server: requested IP %08X\n", reqip.addr);
+            break;
 
-  do {
-    switch (opt->code)
-    {
-      case PICO_DHCP_OPT_PAD:
-        break;
+        case PICO_DHCP_OPT_SERVERID:
+            server_id = opt->ext.server_id.ip;
+            dhcps_dbg("DHCP server: server ID %08X\n", server_id.addr);
+            break;
 
-      case PICO_DHCP_OPT_END:
-        break;
+        default:
+            dhcps_dbg("DHCP server WARNING: unsupported option %u\n", opt->code);
+            break;
+        }
+    } while (pico_dhcp_next_option(&opt));
 
-      case PICO_DHCP_OPT_MSGTYPE:
-        msgtype = opt->ext.msg_type.type;
-        dhcps_dbg("DHCP server: message type %u\n", msgtype);
+    switch (msgtype)
+    {
+    case PICO_DHCP_MSG_DISCOVER:
+        dhcpd_make_reply(dhcpn, PICO_DHCP_MSG_OFFER);
+        dhcpn->state = PICO_DHCP_STATE_OFFER;
         break;
 
-      case PICO_DHCP_OPT_REQIP:
-        reqip = opt->ext.req_ip.ip;
-        dhcps_dbg("DHCP server: requested IP %08X\n", reqip.addr);
+    case PICO_DHCP_MSG_REQUEST:
+        if ((dhcpn->state == PICO_DHCP_STATE_BOUND) && (!reqip.addr) && (!server_id.addr) && (hdr->ciaddr == dhcpn->ciaddr.addr))
+            dhcpd_make_reply(dhcpn, PICO_DHCP_MSG_ACK);
+
+        if (dhcpn->state == PICO_DHCP_STATE_OFFER) {
+            dhcpn->state = PICO_DHCP_STATE_BOUND;
+            dhcpd_make_reply(dhcpn, PICO_DHCP_MSG_ACK);
+        }
+
         break;
 
-      case PICO_DHCP_OPT_SERVERID:
-        server_id = opt->ext.server_id.ip;
-        dhcps_dbg("DHCP server: server ID %08X\n", server_id.addr);
-        break;        
-
-      default:
-        dhcps_dbg("DHCP server WARNING: unsupported option %u\n", opt->code);
+    default:
+        dhcps_dbg("DHCP server WARNING: unsupported message type %u\n", msgtype);
         break;
     }
-  } while (pico_dhcp_next_option(&opt));
-
-  switch (msgtype)
-  {
-    case PICO_DHCP_MSG_DISCOVER:
-      dhcpd_make_reply(dhcpn, PICO_DHCP_MSG_OFFER);
-      dhcpn->state = PICO_DHCP_STATE_OFFER;
-      break;
-
-    case PICO_DHCP_MSG_REQUEST:
-      if ((dhcpn->state == PICO_DHCP_STATE_BOUND) && (!reqip.addr) && (!server_id.addr) && (hdr->ciaddr == dhcpn->ciaddr.addr))
-        dhcpd_make_reply(dhcpn, PICO_DHCP_MSG_ACK);
-      if (dhcpn->state == PICO_DHCP_STATE_OFFER) {
-        dhcpn->state = PICO_DHCP_STATE_BOUND;
-        dhcpd_make_reply(dhcpn, PICO_DHCP_MSG_ACK);
-      }
-      break;
-
-    default:
-      dhcps_dbg("DHCP server WARNING: unsupported message type %u\n", msgtype);
-      break;
-  }
-  return;
+    return;
 }
 
 static void pico_dhcpd_wakeup(uint16_t ev, struct pico_socket *s)
 {
-  uint8_t buf[DHCP_SERVER_MAXMSGSIZE] = {0};
-  int r = 0;
+    uint8_t buf[DHCP_SERVER_MAXMSGSIZE] = {
+        0
+    };
+    int r = 0;
 
-  if (ev != PICO_SOCK_EV_RD)
+    if (ev != PICO_SOCK_EV_RD)
+        return;
+
+    r = pico_socket_recvfrom(s, buf, DHCP_SERVER_MAXMSGSIZE, NULL, NULL);
+    if (r < 0)
+        return;
+
+    pico_dhcp_server_recv(s, buf, (uint32_t)r);
     return;
-  r = pico_socket_recvfrom(s, buf, DHCP_SERVER_MAXMSGSIZE, NULL, NULL);
-  if (r < 0)
-    return;
-
-  pico_dhcp_server_recv(s, buf, (uint32_t)r);
-  return;
 }
 
 int pico_dhcp_server_initiate(struct pico_dhcp_server_setting *setting)
 {
-  if (!setting || !setting->server_ip.addr) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (!setting || !setting->server_ip.addr) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  if (pico_dhcp_server_add_setting(setting) == NULL)
-    return -1;
+    if (pico_dhcp_server_add_setting(setting) == NULL)
+        return -1;
 
-  return 0;
+    return 0;
 }
 #endif /* PICO_SUPPORT_DHCP */
--- a/modules/pico_dhcp_server.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_dhcp_server.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,8 +1,8 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_DHCP_SERVER
 #define _INCLUDE_PICO_DHCP_SERVER
 
@@ -11,15 +11,15 @@
 
 struct pico_dhcp_server_setting
 {
-  uint32_t pool_start;
-  uint32_t pool_next;
-  uint32_t pool_end;
-  uint32_t lease_time;
-  struct pico_device *dev;
-  struct pico_socket *s;
-  struct pico_ip4 server_ip;
-  struct pico_ip4 netmask;
-  uint8_t flags; /* unused atm */
+    uint32_t pool_start;
+    uint32_t pool_next;
+    uint32_t pool_end;
+    uint32_t lease_time;
+    struct pico_device *dev;
+    struct pico_socket *s;
+    struct pico_ip4 server_ip;
+    struct pico_ip4 netmask;
+    uint8_t flags; /* unused atm */
 };
 
 /* required field: IP address of the interface to serve, only IPs of this network will be served. */
--- a/modules/pico_dns_client.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_dns_client.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
-  
-Authors: Kristof Roelants
-*********************************************************************/
+   .
+
+   Authors: Kristof Roelants
+ *********************************************************************/
 #include "pico_config.h"
 #include "pico_stack.h"
 #include "pico_addressing.h"
@@ -16,8 +16,8 @@
 
 #ifdef PICO_SUPPORT_DNS_CLIENT
 
-#define dns_dbg(...) do{}while(0)
-//#define dns_dbg dbg
+#define dns_dbg(...) do {} while(0)
+/* #define dns_dbg dbg */
 
 /* DNS response length */
 #define PICO_DNS_MAX_RESPONSE_LEN 256
@@ -78,682 +78,714 @@
 /* RFC 1035 section 4. MESSAGES */
 struct __attribute__((packed)) pico_dns_name
 {
-  char name[0];
+    char name[0];
 };
 
 /* prefix = header + name pointer
  * flags splitted in 2x uint8 due to endianness */
 struct __attribute__((packed)) pico_dns_prefix
 {
-  uint16_t id;
-  uint8_t rd:1; /* recursion desired  */
-  uint8_t tc:1; /* truncation  */
-  uint8_t aa:1; /* authoritative answer  */
-  uint8_t opcode:4; /* opcode  */
-  uint8_t qr:1; /* query  */
-  uint8_t rcode:4; /* response code */
-  uint8_t z:3; /* zero */
-  uint8_t ra:1; /* recursion available  */
-  uint16_t qdcount;
-  uint16_t ancount;
-  uint16_t nscount;
-  uint16_t arcount;
-  struct pico_dns_name domain;
+    uint16_t id;
+    uint8_t rd : 1; /* recursion desired  */
+    uint8_t tc : 1; /* truncation  */
+    uint8_t aa : 1; /* authoritative answer  */
+    uint8_t opcode : 4; /* opcode  */
+    uint8_t qr : 1; /* query  */
+    uint8_t rcode : 4; /* response code */
+    uint8_t z : 3; /* zero */
+    uint8_t ra : 1; /* recursion available  */
+    uint16_t qdcount;
+    uint16_t ancount;
+    uint16_t nscount;
+    uint16_t arcount;
+    struct pico_dns_name domain;
 };
 
 struct __attribute__((packed)) pico_dns_query_suffix
 {
-  uint16_t qtype;
-  uint16_t qclass;
+    uint16_t qtype;
+    uint16_t qclass;
 };
 
 struct __attribute__((packed)) pico_dns_answer_suffix
 {
-  uint16_t qtype;
-  uint16_t qclass;
-  uint32_t ttl;
-  uint16_t rdlength;
-  uint8_t rdata[];
+    uint16_t qtype;
+    uint16_t qclass;
+    uint32_t ttl;
+    uint16_t rdlength;
+    uint8_t rdata[];
 };
 
 struct pico_dns_ns
 {
-  struct pico_ip4 ns; /* nameserver */
+    struct pico_ip4 ns; /* nameserver */
 };
 
 static int dns_ns_cmp(void *ka, void *kb)
 {
-	struct pico_dns_ns *a = ka, *b = kb;
-  if (a->ns.addr == b->ns.addr)
-    return 0;
-  return (a->ns.addr < b->ns.addr) ? -1 : 1;
-} 
+    struct pico_dns_ns *a = ka, *b = kb;
+    if (a->ns.addr == b->ns.addr)
+        return 0;
+
+    return (a->ns.addr < b->ns.addr) ? -1 : 1;
+}
 PICO_TREE_DECLARE(NSTable, dns_ns_cmp);
-    
+
 struct pico_dns_query
 {
-  char *query;
-  uint16_t len;
-  uint16_t id;
-  uint16_t qtype;
-  uint16_t qclass;
-  uint8_t retrans;
-  struct pico_dns_ns q_ns;
-  struct pico_socket *s;
-  void (*callback)(char *, void *);
-  void *arg;
+    char *query;
+    uint16_t len;
+    uint16_t id;
+    uint16_t qtype;
+    uint16_t qclass;
+    uint8_t retrans;
+    struct pico_dns_ns q_ns;
+    struct pico_socket *s;
+    void (*callback)(char *, void *);
+    void *arg;
 };
 
 static int dns_query_cmp(void *ka, void *kb)
 {
-	struct pico_dns_query *a = ka,*b = kb;
-  if (a->id == b->id)
-    return 0;
-  return (a->id < b->id) ? -1 : 1;
-} 
+    struct pico_dns_query *a = ka, *b = kb;
+    if (a->id == b->id)
+        return 0;
+
+    return (a->id < b->id) ? -1 : 1;
+}
 PICO_TREE_DECLARE(DNSTable, dns_query_cmp);
 
 static int pico_dns_client_del_ns(struct pico_ip4 *ns_addr)
 {
-  struct pico_dns_ns test = {{0}}, *found = NULL;
+    struct pico_dns_ns test = {{0}}, *found = NULL;
 
-  test.ns = *ns_addr;
-  found = pico_tree_findKey(&NSTable, &test);
-  if (!found)
-    return -1;
+    test.ns = *ns_addr;
+    found = pico_tree_findKey(&NSTable, &test);
+    if (!found)
+        return -1;
 
-  pico_tree_delete(&NSTable, found);
-  pico_free(found);
+    pico_tree_delete(&NSTable, found);
+    pico_free(found);
 
-  /* no NS left, add default NS */
-  if (pico_tree_empty(&NSTable))
-    pico_dns_client_init();
+    /* no NS left, add default NS */
+    if (pico_tree_empty(&NSTable))
+        pico_dns_client_init();
 
-  return 0;
+    return 0;
 }
 
 static struct pico_dns_ns *pico_dns_client_add_ns(struct pico_ip4 *ns_addr)
 {
-  struct pico_dns_ns *dns = NULL, *found = NULL, test = {{0}};
-  
-  dns = pico_zalloc(sizeof(struct pico_dns_ns));
-  if (!dns) {
-    pico_err = PICO_ERR_ENOMEM;
-    return NULL;
-  }
-  dns->ns = *ns_addr;
+    struct pico_dns_ns *dns = NULL, *found = NULL, test = {{0}};
+
+    dns = pico_zalloc(sizeof(struct pico_dns_ns));
+    if (!dns) {
+        pico_err = PICO_ERR_ENOMEM;
+        return NULL;
+    }
+
+    dns->ns = *ns_addr;
 
-  found = pico_tree_insert(&NSTable, dns);
-  if (found) { /* nameserver already present */
-    pico_free(dns);
-    return found;
-  }
+    found = pico_tree_insert(&NSTable, dns);
+    if (found) { /* nameserver already present */
+        pico_free(dns);
+        return found;
+    }
 
-  /* default NS found, remove it */
-  pico_string_to_ipv4(PICO_DNS_NS_GOOGLE, &test.ns.addr);
-  found = pico_tree_findKey(&NSTable, &test);
-  if (found && (found->ns.addr != ns_addr->addr))
-    pico_dns_client_del_ns(&found->ns);
+    /* default NS found, remove it */
+    pico_string_to_ipv4(PICO_DNS_NS_GOOGLE, &test.ns.addr);
+    found = pico_tree_findKey(&NSTable, &test);
+    if (found && (found->ns.addr != ns_addr->addr))
+        pico_dns_client_del_ns(&found->ns);
 
-  return dns;
+    return dns;
 }
 
 static struct pico_dns_ns pico_dns_client_next_ns(struct pico_ip4 *ns_addr)
 {
-  struct pico_dns_ns dns = {{0}}, *nxtdns = NULL;
-  struct pico_tree_node *node = NULL, *nxtnode = NULL;
+    struct pico_dns_ns dns = {{0}}, *nxtdns = NULL;
+    struct pico_tree_node *node = NULL, *nxtnode = NULL;
 
-  dns.ns = *ns_addr;
-  node = pico_tree_findNode(&NSTable, &dns);
-  if (!node)
-    return dns; /* keep using current NS */
+    dns.ns = *ns_addr;
+    node = pico_tree_findNode(&NSTable, &dns);
+    if (!node)
+        return dns; /* keep using current NS */
 
-  nxtnode = pico_tree_next(node);
-  nxtdns = nxtnode->keyValue;
-  if (!nxtdns)
-    nxtdns = (struct pico_dns_ns *)pico_tree_first(&NSTable);
-  return *nxtdns;
+    nxtnode = pico_tree_next(node);
+    nxtdns = nxtnode->keyValue;
+    if (!nxtdns)
+        nxtdns = (struct pico_dns_ns *)pico_tree_first(&NSTable);
+
+    return *nxtdns;
 }
 
-static struct pico_dns_query *pico_dns_client_add_query(struct pico_dns_prefix *hdr, uint16_t len, struct pico_dns_query_suffix *suffix, 
+static struct pico_dns_query *pico_dns_client_add_query(struct pico_dns_prefix *hdr, uint16_t len, struct pico_dns_query_suffix *suffix,
                                                         void (*callback)(char *, void *), void *arg)
 {
-  struct pico_dns_query *q = NULL, *found = NULL;
+    struct pico_dns_query *q = NULL, *found = NULL;
 
-  q = pico_zalloc(sizeof(struct pico_dns_query));
-  if (!q)
-    return NULL;
+    q = pico_zalloc(sizeof(struct pico_dns_query));
+    if (!q)
+        return NULL;
 
-  q->query = (char *)hdr;
-  q->len = len;
-  q->id = short_be(hdr->id);
-  q->qtype = short_be(suffix->qtype);
-  q->qclass = short_be(suffix->qclass);
-  q->retrans = 1;
-  q->q_ns = *((struct pico_dns_ns *)pico_tree_first(&NSTable));
-  q->callback = callback;
-  q->arg = arg;
-  q->s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &pico_dns_client_callback);
-  if (!q->s) {
-    pico_free(q);
-    return NULL;
-  }
+    q->query = (char *)hdr;
+    q->len = len;
+    q->id = short_be(hdr->id);
+    q->qtype = short_be(suffix->qtype);
+    q->qclass = short_be(suffix->qclass);
+    q->retrans = 1;
+    q->q_ns = *((struct pico_dns_ns *)pico_tree_first(&NSTable));
+    q->callback = callback;
+    q->arg = arg;
+    q->s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &pico_dns_client_callback);
+    if (!q->s) {
+        pico_free(q);
+        return NULL;
+    }
 
-  found = pico_tree_insert(&DNSTable, q);
-  if (found) {
-    pico_err = PICO_ERR_EAGAIN;
-    pico_socket_close(q->s);
-    pico_free(q);
-    return NULL;
-  }
+    found = pico_tree_insert(&DNSTable, q);
+    if (found) {
+        pico_err = PICO_ERR_EAGAIN;
+        pico_socket_close(q->s);
+        pico_free(q);
+        return NULL;
+    }
 
-  return q;
+    return q;
 }
 
 static int pico_dns_client_del_query(uint16_t id)
 {
-  struct pico_dns_query test = {0}, *found = NULL;
+    struct pico_dns_query test = {
+        0
+    }, *found = NULL;
 
-  test.id = id;
-  found = pico_tree_findKey(&DNSTable, &test);
-  if (!found)
-    return -1;
+    test.id = id;
+    found = pico_tree_findKey(&DNSTable, &test);
+    if (!found)
+        return -1;
 
-  pico_free(found->query);
-  pico_socket_close(found->s);
-  pico_tree_delete(&DNSTable, found);
-  pico_free(found);
-  return 0;
+    pico_free(found->query);
+    pico_socket_close(found->s);
+    pico_tree_delete(&DNSTable, found);
+    pico_free(found);
+    return 0;
 }
 
 static struct pico_dns_query *pico_dns_client_find_query(uint16_t id)
 {
-  struct pico_dns_query test = {0}, *found = NULL;
+    struct pico_dns_query test = {
+        0
+    }, *found = NULL;
 
-  test.id = id;
-  found = pico_tree_findKey(&DNSTable, &test);
-  if (found)
-    return found;
-  else
-    return NULL;
+    test.id = id;
+    found = pico_tree_findKey(&DNSTable, &test);
+    if (found)
+        return found;
+    else
+        return NULL;
 }
 
 /* determine len of string */
 static uint16_t pico_dns_client_strlen(const char *url)
 {
-  char p = 0;
-  uint16_t len = 0;
+    char p = 0;
+    uint16_t len = 0;
 
-  if (!url)
-    return 0;
+    if (!url)
+        return 0;
 
-  while ((p = *url++) != 0) {
-    len++;
-  }
-  return len;
+    while ((p = *url++) != 0) {
+        len++;
+    }
+    return len;
 }
 
 /* seek end of string */
 static char *pico_dns_client_seek(char *ptr)
 {
-  char p = 0;
+    char p = 0;
 
-  if (!ptr)
-    return NULL;
+    if (!ptr)
+        return NULL;
 
-  while ((p = *ptr++) != 0);
-  return ptr++;
+    while ((p = *ptr++) != 0) ;
+    return ptr++;
 }
 
 /* mirror ip address numbers
  * f.e. 192.168.0.1 => 1.0.168.192 */
 static int8_t pico_dns_client_mirror(char *ptr)
 {
-  const unsigned char *addr = NULL;
-  char *m = ptr;
-  uint32_t ip = 0;
-  int8_t i = 0;
+    const unsigned char *addr = NULL;
+    char *m = ptr;
+    uint32_t ip = 0;
+    int8_t i = 0;
 
-  if (pico_string_to_ipv4(ptr, &ip) < 0)
-    return -1;
+    if (pico_string_to_ipv4(ptr, &ip) < 0)
+        return -1;
 
-  ptr = m;
-  addr = (unsigned char *)&ip;
-  for (i = 3; i >= 0; i--) {
-    if (addr[i] > 99) {
-      *ptr++ = (char)('0' + (addr[i] / 100));
-      *ptr++ = (char)('0' + ((addr[i] % 100) / 10));
-      *ptr++ = (char)('0' + ((addr[i] % 100) % 10));
-    } else if(addr[i] > 9) {
-      *ptr++ = (char)('0' + (addr[i] / 10));
-      *ptr++ = (char)('0' + (addr[i] % 10));
-    } else {
-      *ptr++ = (char)('0' + addr[i]);
+    ptr = m;
+    addr = (unsigned char *)&ip;
+    for (i = 3; i >= 0; i--) {
+        if (addr[i] > 99) {
+            *ptr++ = (char)('0' + (addr[i] / 100));
+            *ptr++ = (char)('0' + ((addr[i] % 100) / 10));
+            *ptr++ = (char)('0' + ((addr[i] % 100) % 10));
+        } else if(addr[i] > 9) {
+            *ptr++ = (char)('0' + (addr[i] / 10));
+            *ptr++ = (char)('0' + (addr[i] % 10));
+        } else {
+            *ptr++ = (char)('0' + addr[i]);
+        }
+
+        if(i > 0)
+            *ptr++ = '.';
     }
-    if(i > 0)
-      *ptr++ = '.';
-  }
-  *ptr = '\0';
+    *ptr = '\0';
 
-  return 0;
+    return 0;
 }
 
 static struct pico_dns_query *pico_dns_client_idcheck(uint16_t id)
 {
-  struct pico_dns_query test = {0};
+    struct pico_dns_query test = {
+        0
+    };
 
-  test.id = id;
-  return pico_tree_findKey(&DNSTable, &test);
+    test.id = id;
+    return pico_tree_findKey(&DNSTable, &test);
 }
 
 static int pico_dns_client_query_prefix(struct pico_dns_prefix *pre)
 {
-  uint16_t id = 0;
-  uint8_t retry = 32;
+    uint16_t id = 0;
+    uint8_t retry = 32;
 
-  do {
-    id = (uint16_t)(pico_rand() & 0xFFFFU);
-    dns_dbg("DNS: generated id %u\n", id);
-  } while (retry-- && pico_dns_client_idcheck(id));
-  if (!retry)
-    return -1;
+    do {
+        id = (uint16_t)(pico_rand() & 0xFFFFU);
+        dns_dbg("DNS: generated id %u\n", id);
+    } while (retry-- && pico_dns_client_idcheck(id));
+    if (!retry)
+        return -1;
 
-  pre->id = short_be(id);
-  pre->qr = PICO_DNS_QR_QUERY; 
-  pre->opcode = PICO_DNS_OPCODE_QUERY; 
-  pre->aa = PICO_DNS_AA_NO_AUTHORITY; 
-  pre->tc = PICO_DNS_TC_NO_TRUNCATION; 
-  pre->rd = PICO_DNS_RD_IS_DESIRED; 
-  pre->ra = PICO_DNS_RA_NO_SUPPORT; 
-  pre->z = 0; 
-  pre->rcode = PICO_DNS_RCODE_NO_ERROR; 
-  pre->qdcount = short_be(1);
-  pre->ancount = short_be(0);
-  pre->nscount = short_be(0);
-  pre->arcount = short_be(0);
+    pre->id = short_be(id);
+    pre->qr = PICO_DNS_QR_QUERY;
+    pre->opcode = PICO_DNS_OPCODE_QUERY;
+    pre->aa = PICO_DNS_AA_NO_AUTHORITY;
+    pre->tc = PICO_DNS_TC_NO_TRUNCATION;
+    pre->rd = PICO_DNS_RD_IS_DESIRED;
+    pre->ra = PICO_DNS_RA_NO_SUPPORT;
+    pre->z = 0;
+    pre->rcode = PICO_DNS_RCODE_NO_ERROR;
+    pre->qdcount = short_be(1);
+    pre->ancount = short_be(0);
+    pre->nscount = short_be(0);
+    pre->arcount = short_be(0);
 
-  return 0;
+    return 0;
 }
 
 static int pico_dns_client_query_suffix(struct pico_dns_query_suffix *suf, uint16_t type, uint16_t class)
 {
-  suf->qtype = short_be(type);
-  suf->qclass = short_be(class);
-  return 0;
+    suf->qtype = short_be(type);
+    suf->qclass = short_be(class);
+    return 0;
 }
 
-/* replace '.' in the domain name by the label length 
+/* replace '.' in the domain name by the label length
  * f.e. www.google.be => 3www6google2be0 */
 static int pico_dns_client_query_domain(char *ptr)
 {
-  char p = 0, *label = NULL;
-  uint8_t len = 0;
+    char p = 0, *label = NULL;
+    uint8_t len = 0;
 
-  if (!ptr)
-    return -1;
+    if (!ptr)
+        return -1;
 
-  label = ptr++;
-  while ((p = *ptr++) != 0){
-    if (p == '.') {
-      *label = (char)len;
-      label = ptr - 1;
-      len = 0;
-    } else {
-      len++;
+    label = ptr++;
+    while ((p = *ptr++) != 0) {
+        if (p == '.') {
+            *label = (char)len;
+            label = ptr - 1;
+            len = 0;
+        } else {
+            len++;
+        }
     }
-  }
-  *label = (char)len;
-  return 0;
+    *label = (char)len;
+    return 0;
 }
 
-/* replace the label length in the domain name by '.' 
+/* replace the label length in the domain name by '.'
  * f.e. 3www6google2be0 => .www.google.be */
 static int pico_dns_client_answer_domain(char *ptr)
 {
-  char p = 0, *label = NULL;
+    char p = 0, *label = NULL;
 
-  if (!ptr)
-    return -1;
+    if (!ptr)
+        return -1;
 
-  label = ptr;
-  while ((p = *ptr++) != 0){
-    ptr += p;
-    *label = '.';
     label = ptr;
-  }
-  return 0;
+    while ((p = *ptr++) != 0) {
+        ptr += p;
+        *label = '.';
+        label = ptr;
+    }
+    return 0;
 }
 
 static int pico_dns_client_check_prefix(struct pico_dns_prefix *pre)
 {
-  if (pre->qr != PICO_DNS_QR_RESPONSE || pre->opcode != PICO_DNS_OPCODE_QUERY || pre->rcode != PICO_DNS_RCODE_NO_ERROR) {
-    dns_dbg("DNS ERROR: OPCODE %d | TC %d | RCODE %d\n", pre->opcode, pre->tc, pre->rcode);
-    return -1;
-  }
-  if (short_be(pre->ancount) < 1) {
-    dns_dbg("DNS ERROR: ancount < 1\n");
-    return -1;
-  }
-  return 0;
+    if (pre->qr != PICO_DNS_QR_RESPONSE || pre->opcode != PICO_DNS_OPCODE_QUERY || pre->rcode != PICO_DNS_RCODE_NO_ERROR) {
+        dns_dbg("DNS ERROR: OPCODE %d | TC %d | RCODE %d\n", pre->opcode, pre->tc, pre->rcode);
+        return -1;
+    }
+
+    if (short_be(pre->ancount) < 1) {
+        dns_dbg("DNS ERROR: ancount < 1\n");
+        return -1;
+    }
+
+    return 0;
 }
 
 static int pico_dns_client_check_qsuffix(struct pico_dns_query_suffix *suf, struct pico_dns_query *q)
 {
-  if (short_be(suf->qtype) != q->qtype || short_be(suf->qclass) != q->qclass) {
-    dns_dbg("DNS ERROR: received qtype (%u) or qclass (%u) incorrect\n", short_be(suf->qtype), short_be(suf->qclass));
-    return -1;
-  }
-  return 0;
+    if (short_be(suf->qtype) != q->qtype || short_be(suf->qclass) != q->qclass) {
+        dns_dbg("DNS ERROR: received qtype (%u) or qclass (%u) incorrect\n", short_be(suf->qtype), short_be(suf->qclass));
+        return -1;
+    }
+
+    return 0;
 }
 
 static int pico_dns_client_check_asuffix(struct pico_dns_answer_suffix *suf, struct pico_dns_query *q)
 {
-  if (short_be(suf->qtype) != q->qtype || short_be(suf->qclass) != q->qclass) {
-    dns_dbg("DNS WARNING: received qtype (%u) or qclass (%u) incorrect\n", short_be(suf->qtype), short_be(suf->qclass));
-    return -1;
-  }
-  if (long_be(suf->ttl) > PICO_DNS_MAX_TTL) {
-    dns_dbg("DNS WARNING: received TTL (%u) > MAX (%u)\n", short_be(suf->ttl), PICO_DNS_MAX_TTL);
-    return -1;
-  }
-  return 0;
+    if (short_be(suf->qtype) != q->qtype || short_be(suf->qclass) != q->qclass) {
+        dns_dbg("DNS WARNING: received qtype (%u) or qclass (%u) incorrect\n", short_be(suf->qtype), short_be(suf->qclass));
+        return -1;
+    }
+
+    if (long_be(suf->ttl) > PICO_DNS_MAX_TTL) {
+        dns_dbg("DNS WARNING: received TTL (%u) > MAX (%u)\n", short_be(suf->ttl), PICO_DNS_MAX_TTL);
+        return -1;
+    }
+
+    return 0;
 }
 
 static char *pico_dns_client_seek_suffix(char *suf, struct pico_dns_prefix *pre, struct pico_dns_query *q)
 {
-  struct pico_dns_answer_suffix *asuffix = NULL;
-  uint16_t comp = 0, compression = 0;
-  uint16_t i = 0;
-  char *psuffix = suf;
+    struct pico_dns_answer_suffix *asuffix = NULL;
+    uint16_t comp = 0, compression = 0;
+    uint16_t i = 0;
+    char *psuffix = suf;
 
-  while (i++ < short_be(pre->ancount)) {
-    comp = short_from(psuffix);
-    compression = short_be(comp);
-    switch (compression >> 14)
-    {
-      case PICO_DNS_POINTER:
-        while (compression >> 14 == PICO_DNS_POINTER) {
-          dns_dbg("DNS: pointer\n");
-          psuffix += sizeof(uint16_t);
-          comp = short_from(psuffix);
-          compression = short_be(comp);
-        }
-        break;
+    while (i++ < short_be(pre->ancount)) {
+        comp = short_from(psuffix);
+        compression = short_be(comp);
+        switch (compression >> 14)
+        {
+        case PICO_DNS_POINTER:
+            while (compression >> 14 == PICO_DNS_POINTER) {
+                dns_dbg("DNS: pointer\n");
+                psuffix += sizeof(uint16_t);
+                comp = short_from(psuffix);
+                compression = short_be(comp);
+            }
+            break;
 
-      case PICO_DNS_LABEL:
-        dns_dbg("DNS: label\n");
-        psuffix = pico_dns_client_seek(psuffix);
-        break;
+        case PICO_DNS_LABEL:
+            dns_dbg("DNS: label\n");
+            psuffix = pico_dns_client_seek(psuffix);
+            break;
 
-      default:
-        dns_dbg("DNS ERROR: incorrect compression (%u) value\n", compression);
-        return NULL;
+        default:
+            dns_dbg("DNS ERROR: incorrect compression (%u) value\n", compression);
+            return NULL;
+        }
+
+        asuffix = (struct pico_dns_answer_suffix *)psuffix;
+        if (pico_dns_client_check_asuffix(asuffix, q) < 0) {
+            psuffix += (sizeof(struct pico_dns_answer_suffix) + short_be(asuffix->rdlength));
+            continue;
+        }
+
+        return psuffix;
     }
-
-    asuffix = (struct pico_dns_answer_suffix *)psuffix;
-    if (pico_dns_client_check_asuffix(asuffix, q) < 0) {
-      psuffix += (sizeof(struct pico_dns_answer_suffix) + short_be(asuffix->rdlength));
-      continue;
-    }
-    return psuffix;
-  }
-  return NULL;
+    return NULL;
 }
 
 static int pico_dns_client_send(struct pico_dns_query *q)
 {
-  uint16_t * paramID = pico_zalloc(sizeof(uint16_t));
-  dns_dbg("DNS: sending query to %08X\n", q->q_ns.ns.addr);
-  if (!q->s)
-    goto failure;
-  if (pico_socket_connect(q->s, &q->q_ns.ns, short_be(PICO_DNS_NS_PORT)) < 0)
-    goto failure;
+    uint16_t *paramID = pico_zalloc(sizeof(uint16_t));
+    dns_dbg("DNS: sending query to %08X\n", q->q_ns.ns.addr);
+    if (!q->s)
+        goto failure;
 
-  pico_socket_send(q->s, q->query, q->len);
-  *paramID = q->id;
-  pico_timer_add(PICO_DNS_CLIENT_RETRANS, pico_dns_client_retransmission, paramID);
+    if (pico_socket_connect(q->s, &q->q_ns.ns, short_be(PICO_DNS_NS_PORT)) < 0)
+        goto failure;
 
-  return 0;
+    pico_socket_send(q->s, q->query, q->len);
+    *paramID = q->id;
+    pico_timer_add(PICO_DNS_CLIENT_RETRANS, pico_dns_client_retransmission, paramID);
+
+    return 0;
 
 failure:
-  pico_free(paramID);
-  return -1;
+    pico_free(paramID);
+    return -1;
 }
 
 static void pico_dns_client_retransmission(pico_time now, void *arg)
 {
-  struct pico_dns_query *q = NULL;
-  struct pico_dns_query dummy;
-  IGNORE_PARAMETER(now);
+    struct pico_dns_query *q = NULL;
+    struct pico_dns_query dummy;
+    IGNORE_PARAMETER(now);
 
-  if(!arg)
-	  return;
-  // search for the dns query and free used space
-  dummy.id = *(uint16_t *)arg;
-  q = (struct pico_dns_query *)pico_tree_findKey(&DNSTable,&dummy);
-  pico_free(arg);
+    if(!arg)
+        return;
+
+    /* search for the dns query and free used space */
+    dummy.id = *(uint16_t *)arg;
+    q = (struct pico_dns_query *)pico_tree_findKey(&DNSTable, &dummy);
+    pico_free(arg);
 
-  /* dns query successful? */
-  if (!q) {
-    return;
-  }
+    /* dns query successful? */
+    if (!q) {
+        return;
+    }
 
-  if (q->retrans++ <= PICO_DNS_CLIENT_MAX_RETRANS) {
-    q->q_ns = pico_dns_client_next_ns(&q->q_ns.ns); 
-    pico_dns_client_send(q);
-  } else {
-    pico_err = PICO_ERR_EIO;
-    q->callback(NULL, q->arg);
-    pico_dns_client_del_query(q->id);
-  }
+    if (q->retrans++ <= PICO_DNS_CLIENT_MAX_RETRANS) {
+        q->q_ns = pico_dns_client_next_ns(&q->q_ns.ns);
+        pico_dns_client_send(q);
+    } else {
+        pico_err = PICO_ERR_EIO;
+        q->callback(NULL, q->arg);
+        pico_dns_client_del_query(q->id);
+    }
 }
 
 static int pico_dns_client_user_callback(struct pico_dns_answer_suffix *asuffix, struct pico_dns_query *q)
 {
-  uint32_t ip = 0;
-  char *str = NULL;
-  
-  switch (q->qtype)
-  {
+    uint32_t ip = 0;
+    char *str = NULL;
+
+    switch (q->qtype)
+    {
     case PICO_DNS_TYPE_A:
-      ip = long_from(asuffix->rdata);
-      str = pico_zalloc(PICO_DNS_IPV4_ADDR_LEN);
-      pico_ipv4_to_string(str, ip);
-      break;
+        ip = long_from(asuffix->rdata);
+        str = pico_zalloc(PICO_DNS_IPV4_ADDR_LEN);
+        pico_ipv4_to_string(str, ip);
+        break;
 
     case PICO_DNS_TYPE_PTR:
-      pico_dns_client_answer_domain((char *)asuffix->rdata);
-      str = pico_zalloc((size_t)(asuffix->rdlength - PICO_DNS_LABEL_INITIAL));
-      memcpy(str, asuffix->rdata + PICO_DNS_LABEL_INITIAL, short_be(asuffix->rdlength) - PICO_DNS_LABEL_INITIAL);
-      break;
+        pico_dns_client_answer_domain((char *)asuffix->rdata);
+        str = pico_zalloc((size_t)(asuffix->rdlength - PICO_DNS_LABEL_INITIAL));
+        memcpy(str, asuffix->rdata + PICO_DNS_LABEL_INITIAL, short_be(asuffix->rdlength) - PICO_DNS_LABEL_INITIAL);
+        break;
 
     default:
-      dns_dbg("DNS ERROR: incorrect qtype (%u)\n", q->qtype);
-      break;
-  }
+        dns_dbg("DNS ERROR: incorrect qtype (%u)\n", q->qtype);
+        break;
+    }
 
-  if (q->retrans) {
-    q->callback(str, q->arg);
-    q->retrans = 0;
-    pico_dns_client_del_query(q->id);
-  }
-  return 0;
+    if (q->retrans) {
+        q->callback(str, q->arg);
+        q->retrans = 0;
+        pico_dns_client_del_query(q->id);
+    }
+
+    return 0;
 }
 
 static void pico_dns_client_callback(uint16_t ev, struct pico_socket *s)
 {
-  struct pico_dns_prefix *prefix = NULL;
-  struct pico_dns_name *domain = NULL;
-  struct pico_dns_query_suffix *qsuffix = NULL;
-  struct pico_dns_answer_suffix *asuffix = NULL;
-  struct pico_dns_query *q = NULL;
-  char *p_asuffix = NULL;
-  char msg[PICO_DNS_MAX_RESPONSE_LEN] = {0};
+    struct pico_dns_prefix *prefix = NULL;
+    struct pico_dns_name *domain = NULL;
+    struct pico_dns_query_suffix *qsuffix = NULL;
+    struct pico_dns_answer_suffix *asuffix = NULL;
+    struct pico_dns_query *q = NULL;
+    char *p_asuffix = NULL;
+    char msg[PICO_DNS_MAX_RESPONSE_LEN] = {
+        0
+    };
 
-  if (ev == PICO_SOCK_EV_ERR) {
-    dns_dbg("DNS: socket error received\n");
-    return;
-  }
+    if (ev == PICO_SOCK_EV_ERR) {
+        dns_dbg("DNS: socket error received\n");
+        return;
+    }
 
-  if (ev & PICO_SOCK_EV_RD) {
-    if (pico_socket_read(s, msg, PICO_DNS_MAX_RESPONSE_LEN) <= 0)
-      return;
-  }
+    if (ev & PICO_SOCK_EV_RD) {
+        if (pico_socket_read(s, msg, PICO_DNS_MAX_RESPONSE_LEN) <= 0)
+            return;
+    }
 
-  prefix = (struct pico_dns_prefix *)msg;
-  domain = &prefix->domain;
-  qsuffix = (struct pico_dns_query_suffix *)pico_dns_client_seek(domain->name);
-  /* valid asuffix is determined dynamically later on */
+    prefix = (struct pico_dns_prefix *)msg;
+    domain = &prefix->domain;
+    qsuffix = (struct pico_dns_query_suffix *)pico_dns_client_seek(domain->name);
+    /* valid asuffix is determined dynamically later on */
+
+    if (pico_dns_client_check_prefix(prefix) < 0)
+        return;
+
+    q = pico_dns_client_find_query(short_be(prefix->id));
+    if (!q)
+        return;
 
-  if (pico_dns_client_check_prefix(prefix) < 0)
-    return;
-  q = pico_dns_client_find_query(short_be(prefix->id));
-  if (!q)
+    if (pico_dns_client_check_qsuffix(qsuffix, q) < 0)
+        return;
+
+    p_asuffix = (char *)qsuffix + sizeof(struct pico_dns_query_suffix);
+    p_asuffix = pico_dns_client_seek_suffix(p_asuffix, prefix, q);
+    if (!p_asuffix)
+        return;
+
+    asuffix = (struct pico_dns_answer_suffix *)p_asuffix;
+    pico_dns_client_user_callback(asuffix, q);
+
     return;
-  if (pico_dns_client_check_qsuffix(qsuffix, q) < 0)
-    return;
-
-  p_asuffix = (char *)qsuffix + sizeof(struct pico_dns_query_suffix);
-  p_asuffix = pico_dns_client_seek_suffix(p_asuffix, prefix, q);
-  if (!p_asuffix)
-    return;
-
-  asuffix = (struct pico_dns_answer_suffix *)p_asuffix;
-  pico_dns_client_user_callback(asuffix, q);
-
-  return;
 }
 
 int pico_dns_client_getaddr(const char *url, void (*callback)(char *, void *), void *arg)
 {
-  char *msg = NULL;
-  struct pico_dns_prefix *prefix = NULL;
-  struct pico_dns_name *domain = NULL;
-  struct pico_dns_query_suffix *qsuffix = NULL;
-  struct pico_dns_query *q = NULL;
-  uint16_t len = 0, lblen = 0, strlen = 0;
+    char *msg = NULL;
+    struct pico_dns_prefix *prefix = NULL;
+    struct pico_dns_name *domain = NULL;
+    struct pico_dns_query_suffix *qsuffix = NULL;
+    struct pico_dns_query *q = NULL;
+    uint16_t len = 0, lblen = 0, strlen = 0;
 
-  if (!url || !callback) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (!url || !callback) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    strlen = pico_dns_client_strlen(url);
+    lblen = (uint16_t)(PICO_DNS_LABEL_INITIAL + strlen + PICO_DNS_LABEL_ROOT);
+    len = (uint16_t)(sizeof(struct pico_dns_prefix) + lblen + sizeof(struct pico_dns_query_suffix));
+    msg = pico_zalloc(len);
+    if (!msg) {
+        pico_err = PICO_ERR_ENOMEM;
+        return -1;
+    }
 
-  strlen = pico_dns_client_strlen(url);
-  lblen = (uint16_t)(PICO_DNS_LABEL_INITIAL + strlen + PICO_DNS_LABEL_ROOT);
-  len = (uint16_t)(sizeof(struct pico_dns_prefix) + lblen + sizeof(struct pico_dns_query_suffix));
-  msg = pico_zalloc(len);
-  if (!msg) {
-    pico_err = PICO_ERR_ENOMEM;
-    return -1;
-  }
-  prefix = (struct pico_dns_prefix *)msg;
-  domain = &prefix->domain;
-  qsuffix = (struct pico_dns_query_suffix *)(domain->name + lblen);
-  memcpy(domain->name + PICO_DNS_LABEL_INITIAL, url, strlen);
+    prefix = (struct pico_dns_prefix *)msg;
+    domain = &prefix->domain;
+    qsuffix = (struct pico_dns_query_suffix *)(domain->name + lblen);
+    memcpy(domain->name + PICO_DNS_LABEL_INITIAL, url, strlen);
+
+    /* assemble dns message */
+    pico_dns_client_query_prefix(prefix);
+    pico_dns_client_query_domain(domain->name);
+    pico_dns_client_query_suffix(qsuffix, PICO_DNS_TYPE_A, PICO_DNS_CLASS_IN);
 
-  /* assemble dns message */
-  pico_dns_client_query_prefix(prefix);
-  pico_dns_client_query_domain(domain->name);
-  pico_dns_client_query_suffix(qsuffix, PICO_DNS_TYPE_A, PICO_DNS_CLASS_IN);
+    q = pico_dns_client_add_query(prefix, len, qsuffix, callback, arg);
+    if (!q) {
+        pico_free(msg);
+        return -1;
+    }
 
-  q = pico_dns_client_add_query(prefix, len, qsuffix, callback, arg);
-  if (!q) {
-    pico_free(msg);
-    return -1;
-  }
-  if (pico_dns_client_send(q) < 0) {
-    pico_dns_client_del_query(q->id); /* frees msg */
-    return -1;
-  }
+    if (pico_dns_client_send(q) < 0) {
+        pico_dns_client_del_query(q->id); /* frees msg */
+        return -1;
+    }
 
-  return 0;
+    return 0;
 }
 
 int pico_dns_client_getname(const char *ip, void (*callback)(char *, void *), void *arg)
 {
-  const char *inaddr_arpa = ".in-addr.arpa";
-  char *msg = NULL;
-  struct pico_dns_prefix *prefix = NULL;
-  struct pico_dns_name *domain = NULL;
-  struct pico_dns_query_suffix *qsuffix = NULL;
-  struct pico_dns_query *q = NULL;
-  uint16_t len = 0, lblen = 0, strlen = 0, arpalen = 0;
+    const char *inaddr_arpa = ".in-addr.arpa";
+    char *msg = NULL;
+    struct pico_dns_prefix *prefix = NULL;
+    struct pico_dns_name *domain = NULL;
+    struct pico_dns_query_suffix *qsuffix = NULL;
+    struct pico_dns_query *q = NULL;
+    uint16_t len = 0, lblen = 0, strlen = 0, arpalen = 0;
 
-  if (!ip || !callback) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (!ip || !callback) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    strlen = pico_dns_client_strlen(ip);
+    arpalen = pico_dns_client_strlen(inaddr_arpa);
+    lblen = (uint16_t)(PICO_DNS_LABEL_INITIAL + strlen + arpalen + PICO_DNS_LABEL_ROOT);
+    len = (uint16_t)(sizeof(struct pico_dns_prefix) + lblen + sizeof(struct pico_dns_query_suffix));
+    msg = pico_zalloc(len);
+    if (!msg) {
+        pico_err = PICO_ERR_ENOMEM;
+        return -1;
+    }
 
-  strlen = pico_dns_client_strlen(ip);
-  arpalen = pico_dns_client_strlen(inaddr_arpa);
-  lblen = (uint16_t)(PICO_DNS_LABEL_INITIAL + strlen + arpalen + PICO_DNS_LABEL_ROOT);
-  len = (uint16_t)(sizeof(struct pico_dns_prefix) + lblen + sizeof(struct pico_dns_query_suffix));
-  msg = pico_zalloc(len);
-  if (!msg) {
-    pico_err = PICO_ERR_ENOMEM;
-    return -1;
-  }
-  prefix = (struct pico_dns_prefix *)msg;
-  domain = &prefix->domain;
-  qsuffix = (struct pico_dns_query_suffix *)(prefix->domain.name + lblen);
-  memcpy(domain->name + PICO_DNS_LABEL_INITIAL, ip, strlen);
-  pico_dns_client_mirror(domain->name + PICO_DNS_LABEL_INITIAL);
-  memcpy(domain->name + PICO_DNS_LABEL_INITIAL + strlen, inaddr_arpa, arpalen);
+    prefix = (struct pico_dns_prefix *)msg;
+    domain = &prefix->domain;
+    qsuffix = (struct pico_dns_query_suffix *)(prefix->domain.name + lblen);
+    memcpy(domain->name + PICO_DNS_LABEL_INITIAL, ip, strlen);
+    pico_dns_client_mirror(domain->name + PICO_DNS_LABEL_INITIAL);
+    memcpy(domain->name + PICO_DNS_LABEL_INITIAL + strlen, inaddr_arpa, arpalen);
+
+    /* assemble dns message */
+    pico_dns_client_query_prefix(prefix);
+    pico_dns_client_query_domain(domain->name);
+    pico_dns_client_query_suffix(qsuffix, PICO_DNS_TYPE_PTR, PICO_DNS_CLASS_IN);
 
-  /* assemble dns message */
-  pico_dns_client_query_prefix(prefix);
-  pico_dns_client_query_domain(domain->name);
-  pico_dns_client_query_suffix(qsuffix, PICO_DNS_TYPE_PTR, PICO_DNS_CLASS_IN);
+    q = pico_dns_client_add_query(prefix, len, qsuffix, callback, arg);
+    if (!q) {
+        pico_free(msg);
+        return -1;
+    }
 
-  q = pico_dns_client_add_query(prefix, len, qsuffix, callback, arg);
-  if (!q) {
-    pico_free(msg);
-    return -1;
-  }
-  if (pico_dns_client_send(q) < 0) {
-    pico_dns_client_del_query(q->id); /* frees msg */
-    return -1;
-  }
+    if (pico_dns_client_send(q) < 0) {
+        pico_dns_client_del_query(q->id); /* frees msg */
+        return -1;
+    }
 
-  return 0;
+    return 0;
 }
 
 int pico_dns_client_nameserver(struct pico_ip4 *ns, uint8_t flag)
 {
-  if (!ns) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (!ns) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  switch (flag)
-  {
+    switch (flag)
+    {
     case PICO_DNS_NS_ADD:
-      if (!pico_dns_client_add_ns(ns))
-        return -1;
-      break;
+        if (!pico_dns_client_add_ns(ns))
+            return -1;
+
+        break;
 
     case PICO_DNS_NS_DEL:
-      if (pico_dns_client_del_ns(ns) < 0) {
+        if (pico_dns_client_del_ns(ns) < 0) {
+            pico_err = PICO_ERR_EINVAL;
+            return -1;
+        }
+
+        break;
+
+    default:
         pico_err = PICO_ERR_EINVAL;
         return -1;
-      }
-      break;
-
-    default:
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
-  }
-  return 0;
+    }
+    return 0;
 }
 
 int pico_dns_client_init()
 {
-  struct pico_ip4 default_ns = {0};
+    struct pico_ip4 default_ns = {
+        0
+    };
 
-  if (pico_string_to_ipv4(PICO_DNS_NS_GOOGLE, &default_ns.addr) < 0)
-    return -1;
+    if (pico_string_to_ipv4(PICO_DNS_NS_GOOGLE, &default_ns.addr) < 0)
+        return -1;
 
-  return pico_dns_client_nameserver(&default_ns, PICO_DNS_NS_ADD);
+    return pico_dns_client_nameserver(&default_ns, PICO_DNS_NS_ADD);
 }
 
 #endif /* PICO_SUPPORT_DNS_CLIENT */
--- a/modules/pico_dns_client.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_dns_client.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
-  
-Authors: Kristof Roelants
-*********************************************************************/
+   .
+
+   Authors: Kristof Roelants
+ *********************************************************************/
 
 #ifndef _INCLUDE_PICO_DNS_CLIENT
 #define _INCLUDE_PICO_DNS_CLIENT
--- a/modules/pico_http_client.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_http_client.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Author: Andrei Carp <andrei.carp@tass.be>
-*********************************************************************/
+   Author: Andrei Carp <andrei.carp@tass.be>
+ *********************************************************************/
 #include <string.h>
 #include <stdint.h>
 #include "pico_tree.h"
@@ -31,185 +31,185 @@
 
 #define HTTP_GET_BASIC_SIZE   63u
 #define HTTP_HEADER_LINE_SIZE 50u
-#define RESPONSE_INDEX				9u
+#define RESPONSE_INDEX              9u
 
-#define HTTP_CHUNK_ERROR	0xFFFFFFFFu
+#define HTTP_CHUNK_ERROR    0xFFFFFFFFu
 
 #ifdef dbg
-	#undef dbg
+    #undef dbg
 #endif
 
-#define dbg(...) do{}while(0)
-#define nop() do{}while(0)
+#define dbg(...) do {} while(0)
+#define nop() do {} while(0)
 
-#define consumeChar(c) 							(pico_socket_read(client->sck,&c,1u))
-#define isLocation(line) 						(memcmp(line,"Location",8u) == 0)
-#define isContentLength(line) 			(memcmp(line,"Content-Length",14u) == 0u)
-#define isTransferEncoding(line)		(memcmp(line,"Transfer-Encoding",17u) == 0u)
-#define isChunked(line)							(memcmp(line," chunked",8u) == 0u)
-#define isNotHTTPv1(line)						(memcmp(line,"HTTP/1.",7u))
+#define consumeChar(c)                          (pico_socket_read(client->sck, &c, 1u))
+#define isLocation(line)                        (memcmp(line, "Location", 8u) == 0)
+#define isContentLength(line)           (memcmp(line, "Content-Length", 14u) == 0u)
+#define isTransferEncoding(line)        (memcmp(line, "Transfer-Encoding", 17u) == 0u)
+#define isChunked(line)                         (memcmp(line, " chunked", 8u) == 0u)
+#define isNotHTTPv1(line)                       (memcmp(line, "HTTP/1.", 7u))
 #define is_hex_digit(x) ((('0' <= x) && (x <= '9')) || (('a' <= x) && (x <= 'f')))
-#define hex_digit_to_dec(x) ( (('0' <= x) && (x <= '9')) ? (x-'0') : ( (('a' <= x) && (x <= 'f')) ? (x-'a' + 10) : (-1)) )
+#define hex_digit_to_dec(x) ((('0' <= x) && (x <= '9')) ? (x - '0') : ((('a' <= x) && (x <= 'f')) ? (x - 'a' + 10) : (-1)))
 
 struct pico_http_client
 {
-	uint16_t connectionID;
-	uint8_t state;
-	struct pico_socket * sck;
-	void (*wakeup)(uint16_t ev, uint16_t conn);
-	struct pico_ip4 ip;
-	struct pico_http_uri * uriKey;
-	struct pico_http_header * header;
+    uint16_t connectionID;
+    uint8_t state;
+    struct pico_socket *sck;
+    void (*wakeup)(uint16_t ev, uint16_t conn);
+    struct pico_ip4 ip;
+    struct pico_http_uri *uriKey;
+    struct pico_http_header *header;
 };
 
-// HTTP Client internal states
+/* HTTP Client internal states */
 #define HTTP_READING_HEADER      0
-#define HTTP_READING_BODY				 1
+#define HTTP_READING_BODY                1
 #define HTTP_READING_CHUNK_VALUE 2
 #define HTTP_READING_CHUNK_TRAIL 3
 
 
-static int compareClients(void * ka, void * kb)
+static int compareClients(void *ka, void *kb)
 {
-	return ((struct pico_http_client *)ka)->connectionID - ((struct pico_http_client *)kb)->connectionID;
+    return ((struct pico_http_client *)ka)->connectionID - ((struct pico_http_client *)kb)->connectionID;
 }
 
-PICO_TREE_DECLARE(pico_client_list,compareClients);
+PICO_TREE_DECLARE(pico_client_list, compareClients);
 
-// Local functions
-int parseHeaderFromServer(struct pico_http_client * client, struct pico_http_header * header);
-int readChunkLine(struct pico_http_client * client);
-//
-static inline void processConnErrClose(uint16_t ev, struct pico_http_client * client)
+/* Local functions */
+int parseHeaderFromServer(struct pico_http_client *client, struct pico_http_header *header);
+int readChunkLine(struct pico_http_client *client);
+/*  */
+static inline void processConnErrClose(uint16_t ev, struct pico_http_client *client)
 {
-	if(ev & PICO_SOCK_EV_CONN)
-		client->wakeup(EV_HTTP_CON,client->connectionID);
+    if(ev & PICO_SOCK_EV_CONN)
+        client->wakeup(EV_HTTP_CON, client->connectionID);
 
-	if(ev & PICO_SOCK_EV_ERR)
-	{
-		client->wakeup(EV_HTTP_ERROR,client->connectionID);
-	}
+    if(ev & PICO_SOCK_EV_ERR)
+    {
+        client->wakeup(EV_HTTP_ERROR, client->connectionID);
+    }
 
-	if( (ev & PICO_SOCK_EV_CLOSE) || (ev & PICO_SOCK_EV_FIN) )
-	{
-		client->wakeup(EV_HTTP_CLOSE,client->connectionID);
-	}
+    if((ev & PICO_SOCK_EV_CLOSE) || (ev & PICO_SOCK_EV_FIN))
+    {
+        client->wakeup(EV_HTTP_CLOSE, client->connectionID);
+    }
 }
 
-static inline void waitForHeader(struct pico_http_client * client)
+static inline void waitForHeader(struct pico_http_client *client)
 {
-	// wait for header
-	if(parseHeaderFromServer(client,client->header) < 0)
-	{
-		client->wakeup(EV_HTTP_ERROR,client->connectionID);
-	}
-	else
-	{
-		// call wakeup
-		if(client->header->responseCode != HTTP_CONTINUE)
-		{
-			client->wakeup(
-					(client->header->responseCode == HTTP_OK) ?
-					(EV_HTTP_REQ | EV_HTTP_BODY) : // data comes for sure only when 200 is received
-					EV_HTTP_REQ
-					,client->connectionID);
-		}
-	}
+    /* wait for header */
+    if(parseHeaderFromServer(client, client->header) < 0)
+    {
+        client->wakeup(EV_HTTP_ERROR, client->connectionID);
+    }
+    else
+    {
+        /* call wakeup */
+        if(client->header->responseCode != HTTP_CONTINUE)
+        {
+            client->wakeup(
+                (client->header->responseCode == HTTP_OK) ?
+                (EV_HTTP_REQ | EV_HTTP_BODY) :     /* data comes for sure only when 200 is received */
+                EV_HTTP_REQ
+                , client->connectionID);
+        }
+    }
 }
 
-static inline void treatReadEvent(struct pico_http_client * client)
+static inline void treatReadEvent(struct pico_http_client *client)
 {
-	// read the header, if not read
-	if(client->state == HTTP_READING_HEADER)
-	{
-		// wait for header
-		client->header = pico_zalloc(sizeof(struct pico_http_header));
-		if(!client->header)
-		{
-			pico_err = PICO_ERR_ENOMEM;
-			return;
-		}
+    /* read the header, if not read */
+    if(client->state == HTTP_READING_HEADER)
+    {
+        /* wait for header */
+        client->header = pico_zalloc(sizeof(struct pico_http_header));
+        if(!client->header)
+        {
+            pico_err = PICO_ERR_ENOMEM;
+            return;
+        }
 
-		waitForHeader(client);
-	}
-	else
-	{
-		// just let the user know that data has arrived, if chunked data comes, will be treated in the
-		// read api.
-		client->wakeup(EV_HTTP_BODY,client->connectionID);
-	}
+        waitForHeader(client);
+    }
+    else
+    {
+        /* just let the user know that data has arrived, if chunked data comes, will be treated in the */
+        /* read api. */
+        client->wakeup(EV_HTTP_BODY, client->connectionID);
+    }
 }
 void tcpCallback(uint16_t ev, struct pico_socket *s)
 {
 
-	struct pico_http_client * client = NULL;
-	struct pico_tree_node * index;
+    struct pico_http_client *client = NULL;
+    struct pico_tree_node *index;
 
-	// find httpClient
-	pico_tree_foreach(index,&pico_client_list)
-	{
-		if( ((struct pico_http_client *)index->keyValue)->sck == s )
-		{
-			client = (struct pico_http_client *)index->keyValue;
-			break;
-		}
-	}
+    /* find httpClient */
+    pico_tree_foreach(index, &pico_client_list)
+    {
+        if(((struct pico_http_client *)index->keyValue)->sck == s )
+        {
+            client = (struct pico_http_client *)index->keyValue;
+            break;
+        }
+    }
 
-	if(!client)
-	{
-		dbg("Client not found...Something went wrong !\n");
-		return;
-	}
+    if(!client)
+    {
+        dbg("Client not found...Something went wrong !\n");
+        return;
+    }
 
-	processConnErrClose(ev,client);
+    processConnErrClose(ev, client);
 
-	if(ev & PICO_SOCK_EV_RD)
-	{
-		treatReadEvent(client);
+    if(ev & PICO_SOCK_EV_RD)
+    {
+        treatReadEvent(client);
 
-	}
+    }
 }
 
-// used for getting a response from DNS servers
-static void dnsCallback(char *ip, void * ptr)
+/* used for getting a response from DNS servers */
+static void dnsCallback(char *ip, void *ptr)
 {
-	struct pico_http_client * client = (struct pico_http_client *)ptr;
+    struct pico_http_client *client = (struct pico_http_client *)ptr;
 
-	if(!client)
-	{
-		dbg("Who made the request ?!\n");
-		return;
-	}
+    if(!client)
+    {
+        dbg("Who made the request ?!\n");
+        return;
+    }
 
-	if(ip)
-	{
-		client->wakeup(EV_HTTP_DNS,client->connectionID);
+    if(ip)
+    {
+        client->wakeup(EV_HTTP_DNS, client->connectionID);
 
-		// add the ip address to the client, and start a tcp connection socket
-		pico_string_to_ipv4(ip,&client->ip.addr);
-		pico_free(ip);
-		client->sck = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &tcpCallback);
-		if(!client->sck)
-		{
-			client->wakeup(EV_HTTP_ERROR,client->connectionID);
-			return;
-		}
+        /* add the ip address to the client, and start a tcp connection socket */
+        pico_string_to_ipv4(ip, &client->ip.addr);
+        pico_free(ip);
+        client->sck = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &tcpCallback);
+        if(!client->sck)
+        {
+            client->wakeup(EV_HTTP_ERROR, client->connectionID);
+            return;
+        }
 
-		if(pico_socket_connect(client->sck,&client->ip,short_be(client->uriKey->port)) < 0)
-		{
-			client->wakeup(EV_HTTP_ERROR,client->connectionID);
-			return;
-		}
+        if(pico_socket_connect(client->sck, &client->ip, short_be(client->uriKey->port)) < 0)
+        {
+            client->wakeup(EV_HTTP_ERROR, client->connectionID);
+            return;
+        }
 
-	}
-	else
-	{
-		// wakeup client and let know error occured
-		client->wakeup(EV_HTTP_ERROR,client->connectionID);
+    }
+    else
+    {
+        /* wakeup client and let know error occured */
+        client->wakeup(EV_HTTP_ERROR, client->connectionID);
 
-		// close the client (free used heap)
-		pico_http_client_close(client->connectionID);
-	}
+        /* close the client (free used heap) */
+        pico_http_client_close(client->connectionID);
+    }
 }
 
 /*
@@ -221,47 +221,47 @@
  * The function returns a connection ID >= 0 if successful
  * -1 if an error occured.
  */
-int pico_http_client_open(char * uri, void (*wakeup)(uint16_t ev, uint16_t conn))
+int pico_http_client_open(char *uri, void (*wakeup)(uint16_t ev, uint16_t conn))
 {
-	struct pico_http_client * client;
+    struct pico_http_client *client;
 
-	client = pico_zalloc(sizeof(struct pico_http_client));
-	if(!client)
-	{
-		// memory error
-		pico_err = PICO_ERR_ENOMEM;
-		return HTTP_RETURN_ERROR;
-	}
+    client = pico_zalloc(sizeof(struct pico_http_client));
+    if(!client)
+    {
+        /* memory error */
+        pico_err = PICO_ERR_ENOMEM;
+        return HTTP_RETURN_ERROR;
+    }
 
-	client->wakeup = wakeup;
-	client->connectionID = (uint16_t)pico_rand() & 0x7FFFu; // negative values mean error, still not good generation
+    client->wakeup = wakeup;
+    client->connectionID = (uint16_t)pico_rand() & 0x7FFFu; /* negative values mean error, still not good generation */
 
-	client->uriKey = pico_zalloc(sizeof(struct pico_http_uri));
+    client->uriKey = pico_zalloc(sizeof(struct pico_http_uri));
 
-	if(!client->uriKey)
-	{
-		pico_err = PICO_ERR_ENOMEM;
-		pico_free(client);
-		return HTTP_RETURN_ERROR;
-	}
+    if(!client->uriKey)
+    {
+        pico_err = PICO_ERR_ENOMEM;
+        pico_free(client);
+        return HTTP_RETURN_ERROR;
+    }
 
-	pico_processURI(uri,client->uriKey);
+    pico_processURI(uri, client->uriKey);
 
-	if(pico_tree_insert(&pico_client_list,client))
-	{
-		// already in
-		pico_err = PICO_ERR_EEXIST;
-		pico_free(client->uriKey);
-		pico_free(client);
-		return HTTP_RETURN_ERROR;
-	}
+    if(pico_tree_insert(&pico_client_list, client))
+    {
+        /* already in */
+        pico_err = PICO_ERR_EEXIST;
+        pico_free(client->uriKey);
+        pico_free(client);
+        return HTTP_RETURN_ERROR;
+    }
 
-	// dns query
-	dbg("Querying : %s \n",client->uriKey->host);
-	pico_dns_client_getaddr(client->uriKey->host, dnsCallback,client);
+    /* dns query */
+    dbg("Querying : %s \n", client->uriKey->host);
+    pico_dns_client_getaddr(client->uriKey->host, dnsCallback, client);
 
-	// return the connection ID
-	return client->connectionID;
+    /* return the connection ID */
+    return client->connectionID;
 }
 
 /*
@@ -275,147 +275,150 @@
  * based on the uri passed when opening the client.
  *
  */
-int32_t pico_http_client_sendHeader(uint16_t conn, char * header, uint8_t hdr)
+int32_t pico_http_client_sendHeader(uint16_t conn, char *header, uint8_t hdr)
 {
-	struct pico_http_client search = {.connectionID = conn};
-	struct pico_http_client * http = pico_tree_findKey(&pico_client_list,&search);
-	int32_t length ;
-	if(!http)
-	{
-		dbg("Client not found !\n");
-		return HTTP_RETURN_ERROR;
-	}
+    struct pico_http_client search = {
+        .connectionID = conn
+    };
+    struct pico_http_client *http = pico_tree_findKey(&pico_client_list, &search);
+    int32_t length;
+    if(!http)
+    {
+        dbg("Client not found !\n");
+        return HTTP_RETURN_ERROR;
+    }
 
-	// the api gives the possibility to the user to build the GET header
-	// based on the uri passed when opening the client, less headache for the user
-	if(hdr == HTTP_HEADER_DEFAULT)
-	{
-		header = pico_http_client_buildHeader(http->uriKey);
+    /* the api gives the possibility to the user to build the GET header */
+    /* based on the uri passed when opening the client, less headache for the user */
+    if(hdr == HTTP_HEADER_DEFAULT)
+    {
+        header = pico_http_client_buildHeader(http->uriKey);
 
-		if(!header)
-		{
-			pico_err = PICO_ERR_ENOMEM;
-			return HTTP_RETURN_ERROR;
-		}
-	}
+        if(!header)
+        {
+            pico_err = PICO_ERR_ENOMEM;
+            return HTTP_RETURN_ERROR;
+        }
+    }
 
-	length = pico_socket_write(http->sck,(void *)header,(int)strlen(header)+1);
+    length = pico_socket_write(http->sck, (void *)header, (int)strlen(header) + 1);
 
-	if(hdr == HTTP_HEADER_DEFAULT)
-		pico_free(header);
+    if(hdr == HTTP_HEADER_DEFAULT)
+        pico_free(header);
 
-	return length;
+    return length;
 }
 
 
-///
+/* / */
 
-static inline int checkChunkLine(struct pico_http_client *client,int tmpLenRead)
+static inline int checkChunkLine(struct pico_http_client *client, int tmpLenRead)
 {
-	if(readChunkLine(client) == HTTP_RETURN_ERROR)
-	{
-		dbg("Probably the chunk is malformed or parsed wrong...\n");
-		client->wakeup(EV_HTTP_ERROR,client->connectionID);
-		return HTTP_RETURN_ERROR;
-	}
+    if(readChunkLine(client) == HTTP_RETURN_ERROR)
+    {
+        dbg("Probably the chunk is malformed or parsed wrong...\n");
+        client->wakeup(EV_HTTP_ERROR, client->connectionID);
+        return HTTP_RETURN_ERROR;
+    }
 
-	if(client->state != HTTP_READING_BODY || !tmpLenRead)
-		return 0; // force out
+    if(client->state != HTTP_READING_BODY || !tmpLenRead)
+        return 0; /* force out */
 
-	return 1;
+    return 1;
 }
 
-static inline void updateContentLength(struct pico_http_client *client,int tmpLenRead )
+static inline void updateContentLength(struct pico_http_client *client, int tmpLenRead )
 {
-	if(tmpLenRead > 0)
-	{
-		client->header->contentLengthOrChunk = client->header->contentLengthOrChunk - (uint32_t)tmpLenRead;
-	}
+    if(tmpLenRead > 0)
+    {
+        client->header->contentLengthOrChunk = client->header->contentLengthOrChunk - (uint32_t)tmpLenRead;
+    }
 }
 
-static inline int readBody(struct pico_http_client *client,char * data, uint16_t size, int *lenRead,int *tmpLenRead)
+static inline int readBody(struct pico_http_client *client, char *data, uint16_t size, int *lenRead, int *tmpLenRead)
 {
-	*tmpLenRead = 0;
+    *tmpLenRead = 0;
 
-	if(client->state == HTTP_READING_BODY)
-	{
+    if(client->state == HTTP_READING_BODY)
+    {
 
-		// if needed truncate the data
-		*tmpLenRead = pico_socket_read(client->sck,data + (*lenRead),
-		(client->header->contentLengthOrChunk < ((uint32_t)(size-(*lenRead)))) ? ((int)client->header->contentLengthOrChunk) : (size-(*lenRead)));
+        /* if needed truncate the data */
+        *tmpLenRead = pico_socket_read(client->sck, data + (*lenRead),
+                                       (client->header->contentLengthOrChunk < ((uint32_t)(size - (*lenRead)))) ? ((int)client->header->contentLengthOrChunk) : (size - (*lenRead)));
 
-		updateContentLength(client,*tmpLenRead);
-		if(*tmpLenRead < 0)
-		{
-			// error on reading
-			dbg(">>> Error returned pico_socket_read\n");
-			pico_err = PICO_ERR_EBUSY;
-			// return how much data was read until now
-			return (*lenRead);
-		}
-	}
+        updateContentLength(client, *tmpLenRead);
+        if(*tmpLenRead < 0)
+        {
+            /* error on reading */
+            dbg(">>> Error returned pico_socket_read\n");
+            pico_err = PICO_ERR_EBUSY;
+            /* return how much data was read until now */
+            return (*lenRead);
+        }
+    }
 
-	*lenRead += *tmpLenRead;
-	return 0;
+    *lenRead += *tmpLenRead;
+    return 0;
 }
 
-static inline int readBigChunk(struct pico_http_client *client,char * data, uint16_t size, int *lenRead)
+static inline int readBigChunk(struct pico_http_client *client, char *data, uint16_t size, int *lenRead)
 {
-	int value;
-	// check if we need more than one chunk
-	if(size >= client->header->contentLengthOrChunk)
-	{
-		// read the rest of the chunk, if chunk is done, proceed to the next chunk
-		while((uint16_t)(*lenRead) <= size)
-		{
-			int tmpLenRead = 0;
-			if(readBody(client,data,size,lenRead,&tmpLenRead))
-				return (*lenRead);
+    int value;
+    /* check if we need more than one chunk */
+    if(size >= client->header->contentLengthOrChunk)
+    {
+        /* read the rest of the chunk, if chunk is done, proceed to the next chunk */
+        while((uint16_t)(*lenRead) <= size)
+        {
+            int tmpLenRead = 0;
+            if(readBody(client, data, size, lenRead, &tmpLenRead))
+                return (*lenRead);
 
-			if((value = checkChunkLine(client,tmpLenRead)) <= 0)
-				return value;
-		}
-	}
-	return 0;
+            if((value = checkChunkLine(client, tmpLenRead)) <= 0)
+                return value;
+        }
+    }
+
+    return 0;
 }
 
-static inline void readSmallChunk(struct pico_http_client *client,char * data, uint16_t size, int *lenRead)
+static inline void readSmallChunk(struct pico_http_client *client, char *data, uint16_t size, int *lenRead)
 {
-	if(size < client->header->contentLengthOrChunk)
-	{
-		// read the data from the chunk
-		*lenRead = pico_socket_read(client->sck,(void *)data,size);
+    if(size < client->header->contentLengthOrChunk)
+    {
+        /* read the data from the chunk */
+        *lenRead = pico_socket_read(client->sck, (void *)data, size);
 
-		if(*lenRead)
-			client->header->contentLengthOrChunk = client->header->contentLengthOrChunk - (uint32_t)(*lenRead);
-	}
+        if(*lenRead)
+            client->header->contentLengthOrChunk = client->header->contentLengthOrChunk - (uint32_t)(*lenRead);
+    }
 }
-static inline int readChunkedData(struct pico_http_client *client,char * data, uint16_t size)
+static inline int readChunkedData(struct pico_http_client *client, char *data, uint16_t size)
 {
-	int lenRead = 0;
-	int value;
-	// read the chunk line
-	if(readChunkLine(client) == HTTP_RETURN_ERROR)
-	{
-		dbg("Probably the chunk is malformed or parsed wrong...\n");
-		client->wakeup(EV_HTTP_ERROR,client->connectionID);
-		return HTTP_RETURN_ERROR;
-	}
+    int lenRead = 0;
+    int value;
+    /* read the chunk line */
+    if(readChunkLine(client) == HTTP_RETURN_ERROR)
+    {
+        dbg("Probably the chunk is malformed or parsed wrong...\n");
+        client->wakeup(EV_HTTP_ERROR, client->connectionID);
+        return HTTP_RETURN_ERROR;
+    }
 
-	// nothing to read, no use to try
-	if(client->state != HTTP_READING_BODY)
-	{
-		pico_err = PICO_ERR_EAGAIN;
-		return HTTP_RETURN_OK;
-	}
+    /* nothing to read, no use to try */
+    if(client->state != HTTP_READING_BODY)
+    {
+        pico_err = PICO_ERR_EAGAIN;
+        return HTTP_RETURN_OK;
+    }
 
 
-	readSmallChunk(client,data,size,&lenRead);
+    readSmallChunk(client, data, size, &lenRead);
 
-	if((value = readBigChunk(client,data,size,&lenRead)))
-		return value;
+    if((value = readBigChunk(client, data, size, &lenRead)))
+        return value;
 
-	return lenRead;
+    return lenRead;
 }
 
 /*
@@ -426,23 +429,25 @@
  * a chunked transfer encoding will "de-chunk" the data
  * and pass it to the user.
  */
-int32_t pico_http_client_readData(uint16_t conn, char * data, uint16_t size)
+int32_t pico_http_client_readData(uint16_t conn, char *data, uint16_t size)
 {
-	struct pico_http_client dummy = {.connectionID = conn};
-	struct pico_http_client * client = pico_tree_findKey(&pico_client_list,&dummy);
+    struct pico_http_client dummy = {
+        .connectionID = conn
+    };
+    struct pico_http_client *client = pico_tree_findKey(&pico_client_list, &dummy);
 
-	if(!client)
-	{
-		dbg("Wrong connection id !\n");
-		pico_err = PICO_ERR_EINVAL;
-		return HTTP_RETURN_ERROR;
-	}
+    if(!client)
+    {
+        dbg("Wrong connection id !\n");
+        pico_err = PICO_ERR_EINVAL;
+        return HTTP_RETURN_ERROR;
+    }
 
-	// for the moment just read the data, do not care if it's chunked or not
-	if(client->header->transferCoding == HTTP_TRANSFER_FULL)
-		return pico_socket_read(client->sck,(void *)data,size);
-	else
-		return readChunkedData(client,data,size);
+    /* for the moment just read the data, do not care if it's chunked or not */
+    if(client->header->transferCoding == HTTP_TRANSFER_FULL)
+        return pico_socket_read(client->sck, (void *)data, size);
+    else
+        return readChunkedData(client, data, size);
 }
 
 /*
@@ -450,22 +455,24 @@
  *
  * Reads out the header struct received from server.
  */
-struct pico_http_header * pico_http_client_readHeader(uint16_t conn)
+struct pico_http_header *pico_http_client_readHeader(uint16_t conn)
 {
-	struct pico_http_client dummy = {.connectionID = conn};
-	struct pico_http_client * client = pico_tree_findKey(&pico_client_list,&dummy);
+    struct pico_http_client dummy = {
+        .connectionID = conn
+    };
+    struct pico_http_client *client = pico_tree_findKey(&pico_client_list, &dummy);
 
-	if(client)
-	{
-		return client->header;
-	}
-	else
-	{
-		// not found
-		dbg("Wrong connection id !\n");
-		pico_err = PICO_ERR_EINVAL;
-		return NULL;
-	}
+    if(client)
+    {
+        return client->header;
+    }
+    else
+    {
+        /* not found */
+        dbg("Wrong connection id !\n");
+        pico_err = PICO_ERR_EINVAL;
+        return NULL;
+    }
 }
 
 /*
@@ -473,20 +480,22 @@
  *
  * Reads out the uri struct after was processed.
  */
-struct pico_http_uri * pico_http_client_readUriData(uint16_t conn)
+struct pico_http_uri *pico_http_client_readUriData(uint16_t conn)
 {
-	struct pico_http_client dummy = {.connectionID = conn};
-	struct pico_http_client * client = pico_tree_findKey(&pico_client_list,&dummy);
-	//
-	if(client)
-		return client->uriKey;
-	else
-	{
-		// not found
-		dbg("Wrong connection id !\n");
-		pico_err = PICO_ERR_EINVAL;
-		return NULL;
-	}
+    struct pico_http_client dummy = {
+        .connectionID = conn
+    };
+    struct pico_http_client *client = pico_tree_findKey(&pico_client_list, &dummy);
+    /*  */
+    if(client)
+        return client->uriKey;
+    else
+    {
+        /* not found */
+        dbg("Wrong connection id !\n");
+        pico_err = PICO_ERR_EINVAL;
+        return NULL;
+    }
 }
 
 /*
@@ -494,54 +503,57 @@
  *
  * Close the client.
  */
-static inline void freeHeader(struct pico_http_client * toBeRemoved)
+static inline void freeHeader(struct pico_http_client *toBeRemoved)
 {
-	if(toBeRemoved->header)
-	{
-		// free space used
-			if(toBeRemoved->header->location)
-				pico_free(toBeRemoved->header->location);
+    if(toBeRemoved->header)
+    {
+        /* free space used */
+        if(toBeRemoved->header->location)
+            pico_free(toBeRemoved->header->location);
 
-		pico_free(toBeRemoved->header);
-	}
+        pico_free(toBeRemoved->header);
+    }
 }
 
-static inline void freeUri(struct pico_http_client * toBeRemoved)
+static inline void freeUri(struct pico_http_client *toBeRemoved)
 {
-	if(toBeRemoved->uriKey)
-	{
-		if(toBeRemoved->uriKey->host)
-			pico_free(toBeRemoved->uriKey->host);
+    if(toBeRemoved->uriKey)
+    {
+        if(toBeRemoved->uriKey->host)
+            pico_free(toBeRemoved->uriKey->host);
 
-		if(toBeRemoved->uriKey->resource)
-			pico_free(toBeRemoved->uriKey->resource);
-		pico_free(toBeRemoved->uriKey);
-	}
+        if(toBeRemoved->uriKey->resource)
+            pico_free(toBeRemoved->uriKey->resource);
+
+        pico_free(toBeRemoved->uriKey);
+    }
 }
 int pico_http_client_close(uint16_t conn)
 {
-	struct pico_http_client * toBeRemoved = NULL;
-	struct pico_http_client dummy = {0};
-	dummy.connectionID = conn;
+    struct pico_http_client *toBeRemoved = NULL;
+    struct pico_http_client dummy = {
+        0
+    };
+    dummy.connectionID = conn;
 
-	dbg("Closing the client...\n");
-	toBeRemoved = pico_tree_delete(&pico_client_list,&dummy);
-	if(!toBeRemoved)
-	{
-		dbg("Warning ! Element not found ...");
-		return HTTP_RETURN_ERROR;
-	}
+    dbg("Closing the client...\n");
+    toBeRemoved = pico_tree_delete(&pico_client_list, &dummy);
+    if(!toBeRemoved)
+    {
+        dbg("Warning ! Element not found ...");
+        return HTTP_RETURN_ERROR;
+    }
 
-	// close socket
-	if(toBeRemoved->sck)
-	pico_socket_close(toBeRemoved->sck);
+    /* close socket */
+    if(toBeRemoved->sck)
+        pico_socket_close(toBeRemoved->sck);
 
-	freeHeader(toBeRemoved);
-	freeUri(toBeRemoved);
+    freeHeader(toBeRemoved);
+    freeUri(toBeRemoved);
 
-	pico_free(toBeRemoved);
+    pico_free(toBeRemoved);
 
-	return 0;
+    return 0;
 }
 
 /*
@@ -549,259 +561,257 @@
  *
  * Builds a GET header based on the fields on the uri.
  */
-char * pico_http_client_buildHeader(const struct pico_http_uri * uriData)
+char *pico_http_client_buildHeader(const struct pico_http_uri *uriData)
 {
-	char * header;
-	char port[6u]; // 6 = max length of a uint16 + \0
+    char *header;
+    char port[6u]; /* 6 = max length of a uint16 + \0 */
 
-	unsigned long headerSize = HTTP_GET_BASIC_SIZE;
+    unsigned long headerSize = HTTP_GET_BASIC_SIZE;
 
-	if(!uriData->host || !uriData->resource || !uriData->port)
-	{
-		pico_err = PICO_ERR_EINVAL;
-		return NULL;
-	}
+    if(!uriData->host || !uriData->resource || !uriData->port)
+    {
+        pico_err = PICO_ERR_EINVAL;
+        return NULL;
+    }
 
-	//
-	headerSize = (headerSize + strlen(uriData->host));
-	headerSize = (headerSize + strlen(uriData->resource));
-	headerSize = (headerSize + pico_itoa(uriData->port,port) + 4u); // 3 = size(CRLF + \0)
-	header = pico_zalloc(headerSize);
+    /*  */
+    headerSize = (headerSize + strlen(uriData->host));
+    headerSize = (headerSize + strlen(uriData->resource));
+    headerSize = (headerSize + pico_itoa(uriData->port, port) + 4u); /* 3 = size(CRLF + \0) */
+    header = pico_zalloc(headerSize);
 
-	if(!header)
-	{
-		// not enought memory
-		pico_err = PICO_ERR_ENOMEM;
-		return NULL;
-	}
+    if(!header)
+    {
+        /* not enought memory */
+        pico_err = PICO_ERR_ENOMEM;
+        return NULL;
+    }
 
-	// build the actual header
-	strcpy(header,"GET ");
-	strcat(header,uriData->resource);
-	strcat(header," HTTP/1.1\r\n");
-	strcat(header,"Host: ");
-	strcat(header,uriData->host);
-	strcat(header,":");
-	strcat(header,port);
-	strcat(header,"\r\n");
-	strcat(header,"User-Agent: picoTCP\r\nConnection: close\r\n\r\n"); //?
+    /* build the actual header */
+    strcpy(header, "GET ");
+    strcat(header, uriData->resource);
+    strcat(header, " HTTP/1.1\r\n");
+    strcat(header, "Host: ");
+    strcat(header, uriData->host);
+    strcat(header, ":");
+    strcat(header, port);
+    strcat(header, "\r\n");
+    strcat(header, "User-Agent: picoTCP\r\nConnection: close\r\n\r\n"); /* ? */
 
-	return header;
+    return header;
 }
 
 
-//
-static inline void readFirstLine(struct pico_http_client * client, char * line, uint32_t * index)
+/*  */
+static inline void readFirstLine(struct pico_http_client *client, char *line, uint32_t *index)
 {
-	char c;
+    char c;
 
-	// read the first line of the header
-	while(consumeChar(c)>0 && c!='\r')
-	{
-		if(*index < HTTP_HEADER_LINE_SIZE) // truncate if too long
-			line[(*index)++] = c;
-	}
-
-	consumeChar(c); // consume \n
+    /* read the first line of the header */
+    while(consumeChar(c) > 0 && c != '\r')
+    {
+        if(*index < HTTP_HEADER_LINE_SIZE) /* truncate if too long */
+            line[(*index)++] = c;
+    }
+    consumeChar(c); /* consume \n */
 }
 
-static inline void startReadingBody(struct pico_http_client * client, struct pico_http_header * header)
+static inline void startReadingBody(struct pico_http_client *client, struct pico_http_header *header)
 {
 
-	if(header->transferCoding == HTTP_TRANSFER_CHUNKED)
-	{
-		// read the first chunk
-		header->contentLengthOrChunk = 0;
+    if(header->transferCoding == HTTP_TRANSFER_CHUNKED)
+    {
+        /* read the first chunk */
+        header->contentLengthOrChunk = 0;
 
-		client->state = HTTP_READING_CHUNK_VALUE;
-		readChunkLine(client);
+        client->state = HTTP_READING_CHUNK_VALUE;
+        readChunkLine(client);
 
-	}
-	else
-		client->state = HTTP_READING_BODY;
+    }
+    else
+        client->state = HTTP_READING_BODY;
 }
 
-static inline int parseLocAndCont(struct pico_http_client * client, struct pico_http_header * header, char *line, uint32_t * index)
+static inline int parseLocAndCont(struct pico_http_client *client, struct pico_http_header *header, char *line, uint32_t *index)
 {
-	   char c;
-	   // Location:
+    char c;
+    /* Location: */
 
-		if(isLocation(line))
-		{
-			*index = 0;
-			while(consumeChar(c)>0 && c!='\r')
-			{
-				line[(*index)++] = c;
-			}
-
-			// allocate space for the field
-			header->location = pico_zalloc((*index)+1u);
+    if(isLocation(line))
+    {
+        *index = 0;
+        while(consumeChar(c) > 0 && c != '\r')
+        {
+            line[(*index)++] = c;
+        }
+        /* allocate space for the field */
+        header->location = pico_zalloc((*index) + 1u);
 
-			memcpy(header->location,line,(*index));
-			return 1;
-		}// Content-Length:
-		else if(isContentLength(line))
-		{
-			header->contentLengthOrChunk = 0u;
-			header->transferCoding = HTTP_TRANSFER_FULL;
-			// consume the first space
-			consumeChar(c);
-			while(consumeChar(c)>0 && c!='\r')
-			{
-				header->contentLengthOrChunk = header->contentLengthOrChunk*10u + (uint32_t)(c-'0');
-			}
-			return 1;
-		}// Transfer-Encoding: chunked
-		return 0;
+        memcpy(header->location, line, (*index));
+        return 1;
+    }    /* Content-Length: */
+    else if(isContentLength(line))
+    {
+        header->contentLengthOrChunk = 0u;
+        header->transferCoding = HTTP_TRANSFER_FULL;
+        /* consume the first space */
+        consumeChar(c);
+        while(consumeChar(c) > 0 && c != '\r')
+        {
+            header->contentLengthOrChunk = header->contentLengthOrChunk * 10u + (uint32_t)(c - '0');
+        }
+        return 1;
+    }    /* Transfer-Encoding: chunked */
+
+    return 0;
 }
 
-static inline int parseTransferEncoding(struct pico_http_client * client, struct pico_http_header * header, char *line, uint32_t * index)
+static inline int parseTransferEncoding(struct pico_http_client *client, struct pico_http_header *header, char *line, uint32_t *index)
 {
-	char c;
+    char c;
 
-	if(isTransferEncoding(line))
-	{
-		(*index) = 0;
-		while(consumeChar(c)>0 && c!='\r')
-		{
-			line[(*index)++] = c;
-		}
+    if(isTransferEncoding(line))
+    {
+        (*index) = 0;
+        while(consumeChar(c) > 0 && c != '\r')
+        {
+            line[(*index)++] = c;
+        }
+        if(isChunked(line))
+        {
+            header->contentLengthOrChunk = 0u;
+            header->transferCoding = HTTP_TRANSFER_CHUNKED;
+        }
 
-		if(isChunked(line))
-		{
-			header->contentLengthOrChunk = 0u;
-			header->transferCoding = HTTP_TRANSFER_CHUNKED;
-		}
-		return 1;
-	}// just ignore the line
-	return 0;
+        return 1;
+    } /* just ignore the line */
+
+    return 0;
 }
 
 
-static inline void parseFields(struct pico_http_client * client, struct pico_http_header * header, char *line, uint32_t * index)
+static inline void parseFields(struct pico_http_client *client, struct pico_http_header *header, char *line, uint32_t *index)
 {
-	char c;
+    char c;
 
-	if(!parseLocAndCont(client,header,line,index))
-	{
-		if(!parseTransferEncoding(client,header,line,index))
-		{
-			while(consumeChar(c)>0 && c!='\r') nop();
-		}
-	}
+    if(!parseLocAndCont(client, header, line, index))
+    {
+        if(!parseTransferEncoding(client, header, line, index))
+        {
+            while(consumeChar(c) > 0 && c != '\r') nop();
+        }
+    }
 
-	// consume the next one
-	consumeChar(c);
-	// reset the index
-	(*index) = 0u;
+    /* consume the next one */
+    consumeChar(c);
+    /* reset the index */
+    (*index) = 0u;
 }
 
-static inline void parseRestOfHeader(struct pico_http_client * client, struct pico_http_header * header, char *line, uint32_t * index)
+static inline void parseRestOfHeader(struct pico_http_client *client, struct pico_http_header *header, char *line, uint32_t *index)
 {
-	char c;
+    char c;
 
-	// parse the rest of the header
-	while(consumeChar(c)>0)
-	{
-		if(c==':')
-		{
-			parseFields(client,header,line,index);
-		}
-		else if(c=='\r' && !(*index))
-		{
-				// consume the \n
-				consumeChar(c);
-				break;
-		}
-		else
-		{
-			line[(*index)++] = c;
-		}
-	}
+    /* parse the rest of the header */
+    while(consumeChar(c) > 0)
+    {
+        if(c == ':')
+        {
+            parseFields(client, header, line, index);
+        }
+        else if(c == '\r' && !(*index))
+        {
+            /* consume the \n */
+            consumeChar(c);
+            break;
+        }
+        else
+        {
+            line[(*index)++] = c;
+        }
+    }
 }
 
-int parseHeaderFromServer(struct pico_http_client * client, struct pico_http_header * header)
+int parseHeaderFromServer(struct pico_http_client *client, struct pico_http_header *header)
 {
-	char line[HTTP_HEADER_LINE_SIZE];
-	uint32_t index = 0;
+    char line[HTTP_HEADER_LINE_SIZE];
+    uint32_t index = 0;
 
-	readFirstLine(client,line,&index);
-	// check the integrity of the response
-	// make sure we have enough characters to include the response code
-	// make sure the server response starts with HTTP/1.
-	if((index < RESPONSE_INDEX+2u) || isNotHTTPv1(line))
-	{
-		// wrong format of the the response
-		pico_err = PICO_ERR_EINVAL;
-		return HTTP_RETURN_ERROR;
-	}
+    readFirstLine(client, line, &index);
+    /* check the integrity of the response */
+    /* make sure we have enough characters to include the response code */
+    /* make sure the server response starts with HTTP/1. */
+    if((index < RESPONSE_INDEX + 2u) || isNotHTTPv1(line))
+    {
+        /* wrong format of the the response */
+        pico_err = PICO_ERR_EINVAL;
+        return HTTP_RETURN_ERROR;
+    }
 
-	// extract response code
-	header->responseCode = (uint16_t)((line[RESPONSE_INDEX] - '0') * 100 +
-												 (line[RESPONSE_INDEX+1] - '0') * 10 +
-												 (line[RESPONSE_INDEX+2] - '0'));
+    /* extract response code */
+    header->responseCode = (uint16_t)((line[RESPONSE_INDEX] - '0') * 100 +
+                                      (line[RESPONSE_INDEX + 1] - '0') * 10 +
+                                      (line[RESPONSE_INDEX + 2] - '0'));
 
-	if(header->responseCode/100u > 5u)
-	{
-		// invalid response type
-		header->responseCode = 0;
-		return HTTP_RETURN_ERROR;
-	}
+    if(header->responseCode / 100u > 5u)
+    {
+        /* invalid response type */
+        header->responseCode = 0;
+        return HTTP_RETURN_ERROR;
+    }
 
-	dbg("Server response : %d \n",header->responseCode);
+    dbg("Server response : %d \n", header->responseCode);
 
-	parseRestOfHeader(client,header,line,&index);
+    parseRestOfHeader(client, header, line, &index);
 
-	startReadingBody(client,header);
-	dbg("End of header\n");
-	return HTTP_RETURN_OK;
+    startReadingBody(client, header);
+    dbg("End of header\n");
+    return HTTP_RETURN_OK;
 
 }
 
-// an async read of the chunk part, since in theory a chunk can be split in 2 packets
-static inline void setClientChunkState(struct pico_http_client * client)
+/* an async read of the chunk part, since in theory a chunk can be split in 2 packets */
+static inline void setClientChunkState(struct pico_http_client *client)
 {
 
-	if(client->header->contentLengthOrChunk==0 && client->state == HTTP_READING_BODY)
-	{
-		client->state = HTTP_READING_CHUNK_VALUE;
-	}
+    if(client->header->contentLengthOrChunk == 0 && client->state == HTTP_READING_BODY)
+    {
+        client->state = HTTP_READING_CHUNK_VALUE;
+    }
 }
-static inline void readChunkTrail(struct pico_http_client * client)
+static inline void readChunkTrail(struct pico_http_client *client)
 {
-	char c;
+    char c;
 
-	if(client->state == HTTP_READING_CHUNK_TRAIL)
-	{
-
-		while(consumeChar(c)>0 && c!='\n') nop();
+    if(client->state == HTTP_READING_CHUNK_TRAIL)
+    {
 
-
-		if(c=='\n') client->state = HTTP_READING_BODY;
-	}
+        while(consumeChar(c) > 0 && c != '\n') nop();
+        if(c == '\n') client->state = HTTP_READING_BODY;
+    }
 }
-static inline void readChunkValue(struct pico_http_client * client)
+static inline void readChunkValue(struct pico_http_client *client)
 {
-	char c;
+    char c;
 
-	while(consumeChar(c)>0 && c!='\r' && c!=';')
-	{
-		if(is_hex_digit(c))
-			client->header->contentLengthOrChunk = (client->header->contentLengthOrChunk << 4u) + (uint32_t)hex_digit_to_dec(c);
-	}
-
-	if(c=='\r' || c==';') client->state = HTTP_READING_CHUNK_TRAIL;
+    while(consumeChar(c) > 0 && c != '\r' && c != ';')
+    {
+        if(is_hex_digit(c))
+            client->header->contentLengthOrChunk = (client->header->contentLengthOrChunk << 4u) + (uint32_t)hex_digit_to_dec(c);
+    }
+    if(c == '\r' || c == ';') client->state = HTTP_READING_CHUNK_TRAIL;
 }
-int readChunkLine(struct pico_http_client * client)
+int readChunkLine(struct pico_http_client *client)
 {
-	setClientChunkState(client);
+    setClientChunkState(client);
 
-	if(client->state == HTTP_READING_CHUNK_VALUE)
-	{
-		readChunkValue(client);
-	}
-	readChunkTrail(client);
+    if(client->state == HTTP_READING_CHUNK_VALUE)
+    {
+        readChunkValue(client);
+    }
 
-	return HTTP_RETURN_OK;
+    readChunkTrail(client);
+
+    return HTTP_RETURN_OK;
 }
 #endif
--- a/modules/pico_http_client.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_http_client.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Author: Andrei Carp <andrei.carp@tass.be>
-*********************************************************************/
+   Author: Andrei Carp <andrei.carp@tass.be>
+ *********************************************************************/
 
 
 #ifndef PICO_HTTP_CLIENT_H_
@@ -15,13 +15,13 @@
  * Transfer encodings
  */
 #define HTTP_TRANSFER_CHUNKED  1u
-#define HTTP_TRANSFER_FULL		 0u
+#define HTTP_TRANSFER_FULL       0u
 
 /*
  * Parameters for the send header function
  */
-#define HTTP_HEADER_RAW					0u
-#define HTTP_HEADER_DEFAULT			1u
+#define HTTP_HEADER_RAW                 0u
+#define HTTP_HEADER_DEFAULT         1u
 
 /*
  * Data types
@@ -29,21 +29,21 @@
 
 struct pico_http_header
 {
-	uint16_t responseCode;     				// http response
-	char * location;					 				// if redirect is reported
-	uint32_t contentLengthOrChunk;    // size of the message
-	uint8_t transferCoding;   				// chunked or full
+    uint16_t responseCode;                  /* http response */
+    char *location;                                     /* if redirect is reported */
+    uint32_t contentLengthOrChunk;    /* size of the message */
+    uint8_t transferCoding;                 /* chunked or full */
 
 };
 
-int pico_http_client_open(char * uri, void (*wakeup)(uint16_t ev, uint16_t conn));
-int32_t pico_http_client_sendHeader(uint16_t conn, char * header, uint8_t hdr);
+int pico_http_client_open(char *uri, void (*wakeup)(uint16_t ev, uint16_t conn));
+int32_t pico_http_client_sendHeader(uint16_t conn, char *header, uint8_t hdr);
 
-struct pico_http_header * pico_http_client_readHeader(uint16_t conn);
-struct pico_http_uri * pico_http_client_readUriData(uint16_t conn);
-char * pico_http_client_buildHeader(const struct pico_http_uri * uriData);
+struct pico_http_header *pico_http_client_readHeader(uint16_t conn);
+struct pico_http_uri *pico_http_client_readUriData(uint16_t conn);
+char *pico_http_client_buildHeader(const struct pico_http_uri *uriData);
 
-int32_t pico_http_client_readData(uint16_t conn, char * data, uint16_t size);
+int32_t pico_http_client_readData(uint16_t conn, char *data, uint16_t size);
 int pico_http_client_close(uint16_t conn);
 
 #endif /* PICO_HTTP_CLIENT_H_ */
--- a/modules/pico_http_server.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_http_server.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Author: Andrei Carp <andrei.carp@tass.be>
-*********************************************************************/
+   Author: Andrei Carp <andrei.carp@tass.be>
+ *********************************************************************/
 
 #include "pico_stack.h"
 #include "pico_http_server.h"
@@ -13,31 +13,31 @@
 
 #ifdef PICO_SUPPORT_HTTP_SERVER
 
-#define BACKLOG 						 	10
+#define BACKLOG                             10
 
-#define HTTP_SERVER_CLOSED	 	0
-#define HTTP_SERVER_LISTEN	 	1
+#define HTTP_SERVER_CLOSED      0
+#define HTTP_SERVER_LISTEN      1
 
-#define HTTP_HEADER_MAX_LINE 	256u
+#define HTTP_HEADER_MAX_LINE    256u
 
-#define consumeChar(c) (pico_socket_read(client->sck,&c,1u))
+#define consumeChar(c) (pico_socket_read(client->sck, &c, 1u))
 
 static const char returnOkHeader[] =
-"HTTP/1.1 200 OK\r\n\
+    "HTTP/1.1 200 OK\r\n\
 Host: localhost\r\n\
 Transfer-Encoding: chunked\r\n\
 Connection: close\r\n\
 \r\n";
 
 static const char returnFailHeader[] =
-"HTTP/1.1 404 Not Found\r\n\
+    "HTTP/1.1 404 Not Found\r\n\
 Host: localhost\r\n\
 Connection: close\r\n\
 \r\n\
 <html><body>The resource you requested cannot be found !</body></html>";
 
 static const char errorHeader[] =
-"HTTP/1.1 400 Bad Request\r\n\
+    "HTTP/1.1 400 Bad Request\r\n\
 Host: localhost\r\n\
 Connection: close\r\n\
 \r\n\
@@ -45,117 +45,120 @@
 
 struct httpServer
 {
-	uint16_t state;
-	struct pico_socket * sck;
-	uint16_t port;
-	void (*wakeup)(uint16_t ev, uint16_t param);
-	uint8_t accepted;
+    uint16_t state;
+    struct pico_socket *sck;
+    uint16_t port;
+    void (*wakeup)(uint16_t ev, uint16_t param);
+    uint8_t accepted;
 };
 
 struct httpClient
 {
-	uint16_t connectionID;
-	struct pico_socket * sck;
-	void * buffer;
-	uint16_t bufferSize;
-	uint16_t bufferSent;
-	char * resource;
-	uint16_t state;
+    uint16_t connectionID;
+    struct pico_socket *sck;
+    void *buffer;
+    uint16_t bufferSize;
+    uint16_t bufferSent;
+    char *resource;
+    uint16_t state;
 };
 
 /* Local states for clients */
-#define HTTP_WAIT_HDR				0
-#define HTTP_WAIT_EOF_HDR		1
-#define HTTP_EOF_HDR				2
+#define HTTP_WAIT_HDR               0
+#define HTTP_WAIT_EOF_HDR       1
+#define HTTP_EOF_HDR                2
 #define HTTP_WAIT_RESPONSE  3
-#define HTTP_WAIT_DATA			4
-#define HTTP_SENDING_DATA		5
-#define HTTP_ERROR					6
-#define HTTP_CLOSED					7
+#define HTTP_WAIT_DATA          4
+#define HTTP_SENDING_DATA       5
+#define HTTP_ERROR                  6
+#define HTTP_CLOSED                 7
 
-static struct httpServer server = {0};
+static struct httpServer server = {
+    0
+};
 
 /*
  * Private functions
  */
-static int parseRequest(struct httpClient * client);
-static int readRemainingHeader(struct httpClient * client);
-static void sendData(struct httpClient * client);
-static inline int readData(struct httpClient * client); // used only in a place
-static inline struct httpClient * findClient(uint16_t conn);
+static int parseRequest(struct httpClient *client);
+static int readRemainingHeader(struct httpClient *client);
+static void sendData(struct httpClient *client);
+static inline int readData(struct httpClient *client);  /* used only in a place */
+static inline struct httpClient *findClient(uint16_t conn);
 
-static int compareClients(void * ka, void * kb)
+static int compareClients(void *ka, void *kb)
 {
-	return ((struct httpClient *)ka)->connectionID - ((struct httpClient *)kb)->connectionID;
+    return ((struct httpClient *)ka)->connectionID - ((struct httpClient *)kb)->connectionID;
 }
 
-PICO_TREE_DECLARE(pico_http_clients,compareClients);
+PICO_TREE_DECLARE(pico_http_clients, compareClients);
 
 void httpServerCbk(uint16_t ev, struct pico_socket *s)
 {
-	struct pico_tree_node * index;
-	struct httpClient * client = NULL;
-  uint8_t serverEvent = FALSE;
+    struct pico_tree_node *index;
+    struct httpClient *client = NULL;
+    uint8_t serverEvent = FALSE;
 
-  // determine the client for the socket
-  if( s == server.sck)
-  {
-		serverEvent = TRUE;
-  }
-  else
-  {
-		pico_tree_foreach(index,&pico_http_clients)
-		{
-			client = index->keyValue;
-			if(client->sck == s) break;
-			client = NULL;
-		}
-  }
+    /* determine the client for the socket */
+    if( s == server.sck)
+    {
+        serverEvent = TRUE;
+    }
+    else
+    {
+        pico_tree_foreach(index, &pico_http_clients)
+        {
+            client = index->keyValue;
+            if(client->sck == s) break;
 
-	if(!client && !serverEvent)
-	{
-		return;
-	}
+            client = NULL;
+        }
+    }
 
-	if (ev & PICO_SOCK_EV_RD)
-	{
+    if(!client && !serverEvent)
+    {
+        return;
+    }
+
+    if (ev & PICO_SOCK_EV_RD)
+    {
 
-		if(readData(client) == HTTP_RETURN_ERROR)
-		{
-			// send out error
-			client->state = HTTP_ERROR;
-			pico_socket_write(client->sck,(const char *)errorHeader,sizeof(errorHeader)-1);
-			server.wakeup(EV_HTTP_ERROR,client->connectionID);
-		}
-	}
+        if(readData(client) == HTTP_RETURN_ERROR)
+        {
+            /* send out error */
+            client->state = HTTP_ERROR;
+            pico_socket_write(client->sck, (const char *)errorHeader, sizeof(errorHeader) - 1);
+            server.wakeup(EV_HTTP_ERROR, client->connectionID);
+        }
+    }
 
-	if(ev & PICO_SOCK_EV_WR)
-	{
-		if(client->state == HTTP_SENDING_DATA)
-		{
-			sendData(client);
-		}
-	}
+    if(ev & PICO_SOCK_EV_WR)
+    {
+        if(client->state == HTTP_SENDING_DATA)
+        {
+            sendData(client);
+        }
+    }
 
-	if(ev & PICO_SOCK_EV_CONN)
-	{
-		server.accepted = FALSE;
-		server.wakeup(EV_HTTP_CON,HTTP_SERVER_ID);
-		if(!server.accepted)
-		{
-			pico_socket_close(s); // reject socket
-		}
-	}
+    if(ev & PICO_SOCK_EV_CONN)
+    {
+        server.accepted = FALSE;
+        server.wakeup(EV_HTTP_CON, HTTP_SERVER_ID);
+        if(!server.accepted)
+        {
+            pico_socket_close(s); /* reject socket */
+        }
+    }
 
-	if( (ev & PICO_SOCK_EV_CLOSE) || (ev & PICO_SOCK_EV_FIN) )
-	{
-		server.wakeup(EV_HTTP_CLOSE,(uint16_t)(serverEvent ? HTTP_SERVER_ID : client->connectionID));
-	}
+    if((ev & PICO_SOCK_EV_CLOSE) || (ev & PICO_SOCK_EV_FIN))
+    {
+        server.wakeup(EV_HTTP_CLOSE, (uint16_t)(serverEvent ? HTTP_SERVER_ID : client->connectionID));
+    }
 
-	if(ev & PICO_SOCK_EV_ERR)
-	{
-		server.wakeup(EV_HTTP_ERROR,(uint16_t)(serverEvent ? HTTP_SERVER_ID : client->connectionID));
-	}
+    if(ev & PICO_SOCK_EV_ERR)
+    {
+        server.wakeup(EV_HTTP_ERROR, (uint16_t)(serverEvent ? HTTP_SERVER_ID : client->connectionID));
+    }
 }
 
 /*
@@ -164,38 +167,41 @@
  */
 int8_t pico_http_server_start(uint16_t port, void (*wakeup)(uint16_t ev, uint16_t conn))
 {
-	struct pico_ip4 anything = {0};
+    struct pico_ip4 anything = {
+        0
+    };
 
-	server.port = (uint16_t)(port ? short_be(port) : short_be(80u));
+    server.port = (uint16_t)(port ? short_be(port) : short_be(80u));
 
-	if(!wakeup)
-	{
-		pico_err = PICO_ERR_EINVAL;
-		return HTTP_RETURN_ERROR;
-	}
+    if(!wakeup)
+    {
+        pico_err = PICO_ERR_EINVAL;
+        return HTTP_RETURN_ERROR;
+    }
 
-	server.sck = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &httpServerCbk);
+    server.sck = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &httpServerCbk);
 
-	if(!server.sck)
-	{
-		pico_err = PICO_ERR_EFAULT;
-		return HTTP_RETURN_ERROR;
-	}
+    if(!server.sck)
+    {
+        pico_err = PICO_ERR_EFAULT;
+        return HTTP_RETURN_ERROR;
+    }
 
-	if(pico_socket_bind(server.sck , &anything, &server.port)!=0)
-	{
-		pico_err = PICO_ERR_EADDRNOTAVAIL;
-		return HTTP_RETURN_ERROR;
-	}
+    if(pico_socket_bind(server.sck, &anything, &server.port) != 0)
+    {
+        pico_err = PICO_ERR_EADDRNOTAVAIL;
+        return HTTP_RETURN_ERROR;
+    }
 
-	if (pico_socket_listen(server.sck, BACKLOG) != 0)
-	{
-		pico_err = PICO_ERR_EADDRINUSE;
-		return HTTP_RETURN_ERROR;
-	}
-	server.wakeup = wakeup;
-	server.state = HTTP_SERVER_LISTEN;
-	return HTTP_RETURN_OK;
+    if (pico_socket_listen(server.sck, BACKLOG) != 0)
+    {
+        pico_err = PICO_ERR_EADDRINUSE;
+        return HTTP_RETURN_ERROR;
+    }
+
+    server.wakeup = wakeup;
+    server.state = HTTP_SERVER_LISTEN;
+    return HTTP_RETURN_OK;
 }
 
 /*
@@ -208,39 +214,38 @@
  */
 int pico_http_server_accept(void)
 {
-  struct pico_ip4 orig;
-  struct httpClient * client;
-  uint16_t port;
+    struct pico_ip4 orig;
+    struct httpClient *client;
+    uint16_t port;
 
-  client = pico_zalloc(sizeof(struct httpClient));
-  if(!client)
-  {
-		pico_err = PICO_ERR_ENOMEM;
-  	return HTTP_RETURN_ERROR;
-  }
+    client = pico_zalloc(sizeof(struct httpClient));
+    if(!client)
+    {
+        pico_err = PICO_ERR_ENOMEM;
+        return HTTP_RETURN_ERROR;
+    }
 
-	client->sck = pico_socket_accept(server.sck,&orig,&port);
+    client->sck = pico_socket_accept(server.sck, &orig, &port);
 
-	if(!client->sck)
-	{
-		pico_err = PICO_ERR_ENOMEM;
-		pico_free(client);
-		return HTTP_RETURN_ERROR;
-	}
+    if(!client->sck)
+    {
+        pico_err = PICO_ERR_ENOMEM;
+        pico_free(client);
+        return HTTP_RETURN_ERROR;
+    }
 
-	server.accepted = TRUE;
-	// buffer used for async sending
-	client->state = HTTP_WAIT_HDR;
-	client->buffer = NULL;
-	client->bufferSize = 0;
-	client->connectionID = pico_rand() & 0x7FFF;
+    server.accepted = TRUE;
+    /* buffer used for async sending */
+    client->state = HTTP_WAIT_HDR;
+    client->buffer = NULL;
+    client->bufferSize = 0;
+    client->connectionID = pico_rand() & 0x7FFF;
 
-	//add element to the tree, if duplicate because the rand
-	//regenerate
-	while(pico_tree_insert(&pico_http_clients,client)!=NULL)
-		client->connectionID = pico_rand() & 0x7FFF;
-
-	return client->connectionID;
+    /* add element to the tree, if duplicate because the rand */
+    /* regenerate */
+    while(pico_tree_insert(&pico_http_clients, client) != NULL)
+        client->connectionID = pico_rand() & 0x7FFF;
+    return client->connectionID;
 }
 
 /*
@@ -248,14 +253,14 @@
  * client. It is useful after the request header (EV_HTTP_REQ)
  * from client was received, otherwise NULL is returned.
  */
-char * pico_http_getResource(uint16_t conn)
+char *pico_http_getResource(uint16_t conn)
 {
-	struct httpClient * client = findClient(conn);
+    struct httpClient *client = findClient(conn);
 
-	if(!client)
-		return NULL;
-	else
-		return client->resource;
+    if(!client)
+        return NULL;
+    else
+        return client->resource;
 }
 
 /*
@@ -275,37 +280,37 @@
  */
 int pico_http_respond(uint16_t conn, uint16_t code)
 {
-	struct httpClient * client = findClient(conn);
+    struct httpClient *client = findClient(conn);
 
-	if(!client)
-	{
-		dbg("Client not found !\n");
-		return HTTP_RETURN_ERROR;
-	}
+    if(!client)
+    {
+        dbg("Client not found !\n");
+        return HTTP_RETURN_ERROR;
+    }
 
-	if(client->state == HTTP_WAIT_RESPONSE)
-	{
-		if(code == HTTP_RESOURCE_FOUND)
-		{
-			client->state = HTTP_WAIT_DATA;
-			return pico_socket_write(client->sck,(const char *)returnOkHeader,sizeof(returnOkHeader)-1);//remove \0
-		}
-		else
-		{
-			int length;
+    if(client->state == HTTP_WAIT_RESPONSE)
+    {
+        if(code == HTTP_RESOURCE_FOUND)
+        {
+            client->state = HTTP_WAIT_DATA;
+            return pico_socket_write(client->sck, (const char *)returnOkHeader, sizeof(returnOkHeader) - 1); /* remove \0 */
+        }
+        else
+        {
+            int length;
 
-			length = pico_socket_write(client->sck,(const char *)returnFailHeader,sizeof(returnFailHeader)-1);//remove \0
-			pico_socket_close(client->sck);
-			client->state = HTTP_CLOSED;
-			return length;
+            length = pico_socket_write(client->sck, (const char *)returnFailHeader, sizeof(returnFailHeader) - 1); /* remove \0 */
+            pico_socket_close(client->sck);
+            client->state = HTTP_CLOSED;
+            return length;
 
-		}
-	}
-	else
-	{
-		dbg("Bad state for the client \n");
-		return HTTP_RETURN_ERROR;
-	}
+        }
+    }
+    else
+    {
+        dbg("Bad state for the client \n");
+        return HTTP_RETURN_ERROR;
+    }
 
 }
 
@@ -323,74 +328,75 @@
  * To let the client know this is the last chunk, the user
  * should pass a NULL buffer.
  */
-int8_t pico_http_submitData(uint16_t conn, void * buffer, uint16_t len)
+int8_t pico_http_submitData(uint16_t conn, void *buffer, uint16_t len)
 {
 
-	struct httpClient * client = findClient(conn);
-	char chunkStr[10];
-	int chunkCount;
+    struct httpClient *client = findClient(conn);
+    char chunkStr[10];
+    int chunkCount;
 
-	if(client->state != HTTP_WAIT_DATA)
-	{
-		dbg("Client is in a different state than accepted\n");
-		return HTTP_RETURN_ERROR;
-	}
+    if(client->state != HTTP_WAIT_DATA)
+    {
+        dbg("Client is in a different state than accepted\n");
+        return HTTP_RETURN_ERROR;
+    }
 
-	if(client->buffer)
-	{
-		dbg("Already a buffer submited\n");
-		return HTTP_RETURN_ERROR;
-	}
+    if(client->buffer)
+    {
+        dbg("Already a buffer submited\n");
+        return HTTP_RETURN_ERROR;
+    }
 
-	if(!client)
-	{
-		dbg("Wrong connection ID\n");
-		return HTTP_RETURN_ERROR;
-	}
+    if(!client)
+    {
+        dbg("Wrong connection ID\n");
+        return HTTP_RETURN_ERROR;
+    }
 
-	if(!buffer)
-	{
-		len = 0;
-	}
+    if(!buffer)
+    {
+        len = 0;
+    }
 
-	if(len > 0)
-	{
-		client->buffer = pico_zalloc(len);
-		if(!client->buffer)
-		{
-			pico_err = PICO_ERR_ENOMEM;
-			return HTTP_RETURN_ERROR;
-		}
-		// taking over the buffer
-		memcpy(client->buffer,buffer,len);
-	}
-	else
-		client->buffer = NULL;
+    if(len > 0)
+    {
+        client->buffer = pico_zalloc(len);
+        if(!client->buffer)
+        {
+            pico_err = PICO_ERR_ENOMEM;
+            return HTTP_RETURN_ERROR;
+        }
+
+        /* taking over the buffer */
+        memcpy(client->buffer, buffer, len);
+    }
+    else
+        client->buffer = NULL;
 
 
-	client->bufferSize = len;
-	client->bufferSent = 0;
+    client->bufferSize = len;
+    client->bufferSent = 0;
 
-	// create the chunk size and send it
-	if(len > 0)
-	{
-		client->state = HTTP_SENDING_DATA;
-		chunkCount = pico_itoaHex(client->bufferSize,chunkStr);
-		chunkStr[chunkCount++] = '\r';
-		chunkStr[chunkCount++] = '\n';
-		pico_socket_write(client->sck,chunkStr,chunkCount);
-	}
-	else if(len == 0)
-	{
-		dbg("->\n");
-		// end of transmision
-		pico_socket_write(client->sck,"0\r\n\r\n",5u);
-		// nothing left, close the client
-		pico_socket_close(client->sck);
-		client->state = HTTP_CLOSED;
-	}
+    /* create the chunk size and send it */
+    if(len > 0)
+    {
+        client->state = HTTP_SENDING_DATA;
+        chunkCount = pico_itoaHex(client->bufferSize, chunkStr);
+        chunkStr[chunkCount++] = '\r';
+        chunkStr[chunkCount++] = '\n';
+        pico_socket_write(client->sck, chunkStr, chunkCount);
+    }
+    else if(len == 0)
+    {
+        dbg("->\n");
+        /* end of transmision */
+        pico_socket_write(client->sck, "0\r\n\r\n", 5u);
+        /* nothing left, close the client */
+        pico_socket_close(client->sck);
+        client->state = HTTP_CLOSED;
+    }
 
-	return HTTP_RETURN_OK;
+    return HTTP_RETURN_OK;
 }
 
 /*
@@ -398,20 +404,20 @@
  * function to check the state of the chunk.
  */
 
-int pico_http_getProgress(uint16_t conn, uint16_t * sent, uint16_t *total)
+int pico_http_getProgress(uint16_t conn, uint16_t *sent, uint16_t *total)
 {
-	struct httpClient * client = findClient(conn);
+    struct httpClient *client = findClient(conn);
 
-	if(!client)
-	{
-		dbg("Wrong connection id !\n");
-		return HTTP_RETURN_ERROR;
-	}
+    if(!client)
+    {
+        dbg("Wrong connection id !\n");
+        return HTTP_RETURN_ERROR;
+    }
 
-	*sent = client->bufferSent;
-	*total = client->bufferSize;
+    *sent = client->bufferSent;
+    *total = client->bufferSize;
 
-	return HTTP_RETURN_OK;
+    return HTTP_RETURN_OK;
 }
 
 /*
@@ -420,217 +426,217 @@
  */
 int pico_http_close(uint16_t conn)
 {
-	// close the server
-	if(conn == HTTP_SERVER_ID)
-	{
-		if(server.state == HTTP_SERVER_LISTEN)
-		{
-			struct pico_tree_node * index, * tmp;
-			// close the server
-			pico_socket_close(server.sck);
-			server.sck = NULL;
+    /* close the server */
+    if(conn == HTTP_SERVER_ID)
+    {
+        if(server.state == HTTP_SERVER_LISTEN)
+        {
+            struct pico_tree_node *index, *tmp;
+            /* close the server */
+            pico_socket_close(server.sck);
+            server.sck = NULL;
 
-			// destroy the tree
-			pico_tree_foreach_safe(index,&pico_http_clients,tmp)
-			{
-				struct httpClient * client = index->keyValue;
+            /* destroy the tree */
+            pico_tree_foreach_safe(index, &pico_http_clients, tmp)
+            {
+                struct httpClient *client = index->keyValue;
 
-				if(client->resource)
-					pico_free(client->resource);
+                if(client->resource)
+                    pico_free(client->resource);
 
-				pico_socket_close(client->sck);
-				pico_tree_delete(&pico_http_clients,client);
-			}
+                pico_socket_close(client->sck);
+                pico_tree_delete(&pico_http_clients, client);
+            }
 
-			server.state = HTTP_SERVER_CLOSED;
-			return HTTP_RETURN_OK;
-		}
-		else // nothing to close
-			return HTTP_RETURN_ERROR;
-	} // close a connection in this case
-	else
-	{
+            server.state = HTTP_SERVER_CLOSED;
+            return HTTP_RETURN_OK;
+        }
+        else /* nothing to close */
+            return HTTP_RETURN_ERROR;
+    } /* close a connection in this case */
+    else
+    {
 
-		struct httpClient * client = findClient(conn);
+        struct httpClient *client = findClient(conn);
 
-		if(!client)
-		{
-			dbg("Client not found..\n");
-			return HTTP_RETURN_ERROR;
-		}
+        if(!client)
+        {
+            dbg("Client not found..\n");
+            return HTTP_RETURN_ERROR;
+        }
 
-		pico_tree_delete(&pico_http_clients,client);
+        pico_tree_delete(&pico_http_clients, client);
 
-		if(client->resource)
-			pico_free(client->resource);
+        if(client->resource)
+            pico_free(client->resource);
 
-		if(client->buffer)
-			pico_free(client->buffer);
+        if(client->buffer)
+            pico_free(client->buffer);
 
-		if(client->state != HTTP_CLOSED || !client->sck)
-			pico_socket_close(client->sck);
+        if(client->state != HTTP_CLOSED || !client->sck)
+            pico_socket_close(client->sck);
 
-		pico_free(client);
-		return HTTP_RETURN_OK;
-	}
+        pico_free(client);
+        return HTTP_RETURN_OK;
+    }
 }
 
-// check the integrity of the request
-int parseRequest(struct httpClient * client)
+/* check the integrity of the request */
+int parseRequest(struct httpClient *client)
 {
-	char c;
-	//read first line
-	consumeChar(c);
-	if(c == 'G')
-	{ // possible GET
-
-		char line[HTTP_HEADER_MAX_LINE];
-		uint32_t index = 0;
-
-		line[index] = c;
+    char c;
+    /* read first line */
+    consumeChar(c);
+    if(c == 'G')
+    { /* possible GET */
 
-		// consume the full line
-		while(consumeChar(c)>0) // read char by char only the first line
-		{
-			line[++index] = c;
-			if(c == '\n')
-				break;
+        char line[HTTP_HEADER_MAX_LINE];
+        uint32_t index = 0;
+
+        line[index] = c;
 
-			if(index >= HTTP_HEADER_MAX_LINE)
-			{
-				dbg("Size exceeded \n");
-				return HTTP_RETURN_ERROR;
-			}
-		}
+        /* consume the full line */
+        while(consumeChar(c) > 0) /* read char by char only the first line */
+        {
+            line[++index] = c;
+            if(c == '\n')
+                break;
 
-		// extract the function and the resource
-		if(memcmp(line,"GET",3u) || line[3u]!=' ' || index < 10u || line[index] !='\n')
-		{
-			dbg("Wrong command or wrong ending\n");
-			return HTTP_RETURN_ERROR;
-		}
-
-		// start reading the resource
-		index = 4u; // go after ' '
-		while(line[index]!=' ')
-		{
-			if(line[index]=='\n') // no terminator ' '
-			{
-				dbg("No terminator...\n");
-				return HTTP_RETURN_ERROR;
-			}
+            if(index >= HTTP_HEADER_MAX_LINE)
+            {
+                dbg("Size exceeded \n");
+                return HTTP_RETURN_ERROR;
+            }
+        }
+        /* extract the function and the resource */
+        if(memcmp(line, "GET", 3u) || line[3u] != ' ' || index < 10u || line[index] != '\n')
+        {
+            dbg("Wrong command or wrong ending\n");
+            return HTTP_RETURN_ERROR;
+        }
 
-			index++;
-		}
+        /* start reading the resource */
+        index = 4u; /* go after ' ' */
+        while(line[index] != ' ')
+        {
+            if(line[index] == '\n') /* no terminator ' ' */
+            {
+                dbg("No terminator...\n");
+                return HTTP_RETURN_ERROR;
+            }
 
-		client->resource = pico_zalloc(index - 3u);// allocate without the GET in front + 1 which is \0
+            index++;
+        }
+        client->resource = pico_zalloc(index - 3u); /* allocate without the GET in front + 1 which is \0 */
 
-		if(!client)
-		{
-			pico_err = PICO_ERR_ENOMEM;
-			return HTTP_RETURN_ERROR;
-		}
+        if(!client)
+        {
+            pico_err = PICO_ERR_ENOMEM;
+            return HTTP_RETURN_ERROR;
+        }
 
-		// copy the resource
-		memcpy(client->resource,line+4u,index-4u);// copy without the \0 which was already set by pico_zalloc
+        /* copy the resource */
+        memcpy(client->resource, line + 4u, index - 4u); /* copy without the \0 which was already set by pico_zalloc */
 
-		client->state = HTTP_WAIT_EOF_HDR;
-		return HTTP_RETURN_OK;
+        client->state = HTTP_WAIT_EOF_HDR;
+        return HTTP_RETURN_OK;
 
-	}
+    }
 
-	return HTTP_RETURN_ERROR;
+    return HTTP_RETURN_ERROR;
 }
 
 
 
-int readRemainingHeader(struct httpClient * client)
+int readRemainingHeader(struct httpClient *client)
 {
-	char line[100];
-	int count = 0;
-	int len;
+    char line[100];
+    int count = 0;
+    int len;
 
-	while( (len = pico_socket_read(client->sck,line,100u)) > 0)
-	{
-		char c;
-		int index = 0;
-		// parse the response
-		while(index < len)
-		{
-			c = line[index++];
-			if(c!='\r' && c!='\n')
-				count++;
-			if(c=='\n')
-			{
-				if(!count)
-				{
-					client->state = HTTP_EOF_HDR;
-					dbg("End of header !\n");
-					break;
-				}
-				count = 0;
+    while((len = pico_socket_read(client->sck, line, 100u)) > 0)
+    {
+        char c;
+        int index = 0;
+        /* parse the response */
+        while(index < len)
+        {
+            c = line[index++];
+            if(c != '\r' && c != '\n')
+                count++;
 
-			}
-		}
-	}
+            if(c == '\n')
+            {
+                if(!count)
+                {
+                    client->state = HTTP_EOF_HDR;
+                    dbg("End of header !\n");
+                    break;
+                }
 
-	return HTTP_RETURN_OK;
+                count = 0;
+
+            }
+        }
+    }
+    return HTTP_RETURN_OK;
 }
 
-void sendData(struct httpClient * client)
+void sendData(struct httpClient *client)
 {
-	uint16_t length;
-	while( client->bufferSent < client->bufferSize &&
-	(length = (uint16_t)pico_socket_write(client->sck,client->buffer+client->bufferSent,client->bufferSize-client->bufferSent)) > 0 )
-	{
-		client->bufferSent = (uint16_t)(client->bufferSent + length);
-		server.wakeup(EV_HTTP_PROGRESS,client->connectionID);
-	}
-
-	if(client->bufferSent == client->bufferSize && client->bufferSize)
-	{
-		//send chunk trail
-		if(pico_socket_write(client->sck,"\r\n",2) > 0)
-		{
-			client->state = HTTP_WAIT_DATA;
-			//free the buffer
-			pico_free(client->buffer);
-			client->buffer = NULL;
-			server.wakeup(EV_HTTP_SENT,client->connectionID);
-		}
-	}
+    uint16_t length;
+    while( client->bufferSent < client->bufferSize &&
+           (length = (uint16_t)pico_socket_write(client->sck, client->buffer + client->bufferSent, client->bufferSize - client->bufferSent)) > 0 )
+    {
+        client->bufferSent = (uint16_t)(client->bufferSent + length);
+        server.wakeup(EV_HTTP_PROGRESS, client->connectionID);
+    }
+    if(client->bufferSent == client->bufferSize && client->bufferSize)
+    {
+        /* send chunk trail */
+        if(pico_socket_write(client->sck, "\r\n", 2) > 0)
+        {
+            client->state = HTTP_WAIT_DATA;
+            /* free the buffer */
+            pico_free(client->buffer);
+            client->buffer = NULL;
+            server.wakeup(EV_HTTP_SENT, client->connectionID);
+        }
+    }
 
 }
 
-int readData(struct httpClient * client)
+int readData(struct httpClient *client)
 {
-	if(client->state == HTTP_WAIT_HDR)
-	{
-		if(parseRequest(client)<0 || readRemainingHeader(client)<0)
-		{
-			return HTTP_RETURN_ERROR;
-		}
-	} // continue with this in case the header comes line by line not a big chunk
-	else if(client->state == HTTP_WAIT_EOF_HDR)
-	{
-		if(readRemainingHeader(client)<0 )
-			return HTTP_RETURN_ERROR;
-	}
+    if(client->state == HTTP_WAIT_HDR)
+    {
+        if(parseRequest(client) < 0 || readRemainingHeader(client) < 0)
+        {
+            return HTTP_RETURN_ERROR;
+        }
+    } /* continue with this in case the header comes line by line not a big chunk */
+    else if(client->state == HTTP_WAIT_EOF_HDR)
+    {
+        if(readRemainingHeader(client) < 0 )
+            return HTTP_RETURN_ERROR;
+    }
 
-	if(client->state == HTTP_EOF_HDR)
-	{
-		client->state = HTTP_WAIT_RESPONSE;
-		pico_socket_shutdown(client->sck,PICO_SHUT_RD);
-		server.wakeup(EV_HTTP_REQ,client->connectionID);
-	}
+    if(client->state == HTTP_EOF_HDR)
+    {
+        client->state = HTTP_WAIT_RESPONSE;
+        pico_socket_shutdown(client->sck, PICO_SHUT_RD);
+        server.wakeup(EV_HTTP_REQ, client->connectionID);
+    }
 
-	return HTTP_RETURN_OK;
+    return HTTP_RETURN_OK;
 }
 
-struct httpClient * findClient(uint16_t conn)
+struct httpClient *findClient(uint16_t conn)
 {
-	struct httpClient dummy = {.connectionID = conn};
+    struct httpClient dummy = {
+        .connectionID = conn
+    };
 
-	return pico_tree_findKey(&pico_http_clients,&dummy);
+    return pico_tree_findKey(&pico_http_clients, &dummy);
 }
 #endif
--- a/modules/pico_http_server.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_http_server.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Author: Andrei Carp <andrei.carp@tass.be>
-*********************************************************************/
+   Author: Andrei Carp <andrei.carp@tass.be>
+ *********************************************************************/
 
 #ifndef PICO_HTTP_SERVER_H_
 #define PICO_HTTP_SERVER_H_
@@ -11,12 +11,12 @@
 #include <stdint.h>
 #include "pico_http_util.h"
 
-// Response codes
-#define HTTP_RESOURCE_FOUND				0
-#define HTTP_RESOURCE_NOT_FOUND		1
+/* Response codes */
+#define HTTP_RESOURCE_FOUND             0
+#define HTTP_RESOURCE_NOT_FOUND     1
 
-// Generic id for the server
-#define HTTP_SERVER_ID					0u
+/* Generic id for the server */
+#define HTTP_SERVER_ID                  0u
 
 /*
  * Server functions
@@ -27,14 +27,14 @@
 /*
  * Client functions
  */
-char * pico_http_getResource(uint16_t conn);
-int 	 pico_http_getProgress(uint16_t conn, uint16_t * sent, uint16_t *total);
+char *pico_http_getResource(uint16_t conn);
+int      pico_http_getProgress(uint16_t conn, uint16_t *sent, uint16_t *total);
 
 /*
  * Handshake and data functions
  */
-int 	 pico_http_respond(uint16_t conn, uint16_t code);
-int8_t 	 pico_http_submitData(uint16_t conn, void * buffer, uint16_t len);
-int 	 pico_http_close(uint16_t conn);
+int      pico_http_respond(uint16_t conn, uint16_t code);
+int8_t   pico_http_submitData(uint16_t conn, void *buffer, uint16_t len);
+int      pico_http_close(uint16_t conn);
 
 #endif /* PICO_HTTP_SERVER_H_ */
--- a/modules/pico_http_util.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_http_util.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Author: Andrei Carp <andrei.carp@tass.be>
-*********************************************************************/
+   Author: Andrei Carp <andrei.carp@tass.be>
+ *********************************************************************/
 
 #include <stdint.h>
 #include "pico_config.h"
@@ -11,176 +11,176 @@
 #include "pico_protocol.h"
 #include "pico_http_util.h"
 
-#define TRUE	1
+#define TRUE    1
 #define FALSE 0
 
-#define HTTP_PROTO_TOK		"http://"
-#define HTTP_PROTO_LEN		7u
+#define HTTP_PROTO_TOK      "http://"
+#define HTTP_PROTO_LEN      7u
 
 #if defined PICO_SUPPORT_HTTP_CLIENT || defined PICO_SUPPORT_HTTP_SERVER
 
-int pico_itoaHex(uint16_t port, char * ptr)
+int pico_itoaHex(uint16_t port, char *ptr)
 {
-	int size = 0;
-	int index;
+    int size = 0;
+    int index;
 
-	// transform to from number to string [ in backwards ]
-	while(port)
-	{
-		ptr[size] = (char)(((port & 0xF) < 10) ? ((port & 0xF) + '0') : ((port & 0xF) - 10 + 'a'));
-		port = port>>4u; //divide by 16
-		size++;
-	}
-
-	// invert positions
-	for(index=0 ;index < size>>1u ;index++)
-	{
-		char c = ptr[index];
-		ptr[index] = ptr[size-index-1];
-		ptr[size-index-1] = c;
-	}
-	ptr[size] = '\0';
-	return size;
+    /* transform to from number to string [ in backwards ] */
+    while(port)
+    {
+        ptr[size] = (char)(((port & 0xF) < 10) ? ((port & 0xF) + '0') : ((port & 0xF) - 10 + 'a'));
+        port = port >> 4u; /* divide by 16 */
+        size++;
+    }
+    /* invert positions */
+    for(index = 0; index < (size >> 1u); index++)
+    {
+        char c = ptr[index];
+        ptr[index] = ptr[size - index - 1];
+        ptr[size - index - 1] = c;
+    }
+    ptr[size] = '\0';
+    return size;
 }
 
-uint16_t pico_itoa(uint16_t port, char * ptr)
+uint16_t pico_itoa(uint16_t port, char *ptr)
 {
-	uint16_t size = 0;
-	uint16_t index;
+    uint16_t size = 0;
+    uint16_t index;
 
-	// transform to from number to string [ in backwards ]
-	while(port)
-	{
-		ptr[size] = (char)(port%10 + '0');
-		port = port/10;
-		size++;
-	}
-
-	// invert positions
-	for(index=0 ;index < size>>1u ;index++)
-	{
-		char c = ptr[index];
-		ptr[index] = ptr[size-index-1];
-		ptr[size-index-1] = c;
-	}
-	ptr[size] = '\0';
-	return size;
+    /* transform to from number to string [ in backwards ] */
+    while(port)
+    {
+        ptr[size] = (char)(port % 10 + '0');
+        port = port / 10;
+        size++;
+    }
+    /* invert positions */
+    for(index = 0; index < (size >> 1u); index++)
+    {
+        char c = ptr[index];
+        ptr[index] = ptr[size - index - 1];
+        ptr[size - index - 1] = c;
+    }
+    ptr[size] = '\0';
+    return size;
 }
 
 
-int8_t pico_processURI(const char * uri, struct pico_http_uri * urikey)
+int8_t pico_processURI(const char *uri, struct pico_http_uri *urikey)
 {
 
-	uint16_t lastIndex = 0, index;
+    uint16_t lastIndex = 0, index;
 
-	if(!uri || !urikey || uri[0] == '/')
-	{
-		pico_err = PICO_ERR_EINVAL;
-		goto error;
-	}
+    if(!uri || !urikey || uri[0] == '/')
+    {
+        pico_err = PICO_ERR_EINVAL;
+        goto error;
+    }
 
-	// detect protocol => search for  "://"
-	if(memcmp(uri,HTTP_PROTO_TOK,HTTP_PROTO_LEN) == 0) // could be optimized
-	{ // protocol identified, it is http
-		urikey->protoHttp = TRUE;
-		lastIndex = HTTP_PROTO_LEN;
-	}
-	else
-	{
-		if(strstr(uri,"://")) // different protocol specified
-		{
-			urikey->protoHttp = FALSE;
-			goto error;
-		}
-		// no protocol specified, assuming by default it's http
-		urikey->protoHttp = TRUE;
-	}
+    /* detect protocol => search for  "://" */
+    if(memcmp(uri, HTTP_PROTO_TOK, HTTP_PROTO_LEN) == 0) /* could be optimized */
+    { /* protocol identified, it is http */
+        urikey->protoHttp = TRUE;
+        lastIndex = HTTP_PROTO_LEN;
+    }
+    else
+    {
+        if(strstr(uri, "://")) /* different protocol specified */
+        {
+            urikey->protoHttp = FALSE;
+            goto error;
+        }
 
-	// detect hostname
-	index = lastIndex;
-	while(uri[index] && uri[index]!='/' && uri[index]!=':') index++;
+        /* no protocol specified, assuming by default it's http */
+        urikey->protoHttp = TRUE;
+    }
 
-	if(index == lastIndex)
-	{
-		// wrong format
-		urikey->host = urikey->resource = NULL;
-		urikey->port = urikey->protoHttp = 0u;
+    /* detect hostname */
+    index = lastIndex;
+    while(uri[index] && uri[index] != '/' && uri[index] != ':') index++;
+    if(index == lastIndex)
+    {
+        /* wrong format */
+        urikey->host = urikey->resource = NULL;
+        urikey->port = urikey->protoHttp = 0u;
 
-		goto error;
-	}
-	else
-	{
-		// extract host
-		urikey->host = (char *)pico_zalloc((uint32_t)(index-lastIndex+1));
+        goto error;
+    }
+    else
+    {
+        /* extract host */
+        urikey->host = (char *)pico_zalloc((uint32_t)(index - lastIndex + 1));
 
-		if(!urikey->host)
-		{
-			// no memory
-			goto error;
-		}
-		memcpy(urikey->host,uri+lastIndex,(size_t)(index-lastIndex));
-	}
+        if(!urikey->host)
+        {
+            /* no memory */
+            goto error;
+        }
+
+        memcpy(urikey->host, uri + lastIndex, (size_t)(index - lastIndex));
+    }
 
-	if(!uri[index])
-	{
-		// nothing specified
-		urikey->port = 80u;
-		urikey->resource = pico_zalloc(2u);
-		urikey->resource[0] = '/';
-		return HTTP_RETURN_OK;
-	}
-	else if(uri[index] == '/')
-	{
-		urikey->port = 80u;
-	}
-	else if(uri[index] == ':')
-	{
-		urikey->port = 0u;
-		index++;
-		while(uri[index] && uri[index]!='/')
-		{
-			// should check if every component is a digit
-			urikey->port = (uint16_t)(urikey->port*10 + (uri[index] - '0'));
-			index++;
-		}
-	}
+    if(!uri[index])
+    {
+        /* nothing specified */
+        urikey->port = 80u;
+        urikey->resource = pico_zalloc(2u);
+        urikey->resource[0] = '/';
+        return HTTP_RETURN_OK;
+    }
+    else if(uri[index] == '/')
+    {
+        urikey->port = 80u;
+    }
+    else if(uri[index] == ':')
+    {
+        urikey->port = 0u;
+        index++;
+        while(uri[index] && uri[index] != '/')
+        {
+            /* should check if every component is a digit */
+            urikey->port = (uint16_t)(urikey->port * 10 + (uri[index] - '0'));
+            index++;
+        }
+    }
 
-  // extract resource
-	if(!uri[index])
-	{
-		urikey->resource = pico_zalloc(2u);
-		urikey->resource[0] = '/';
-	}
-	else
-	{
-		lastIndex = index;
-		while(uri[index] && uri[index]!='?' && uri[index]!='&' && uri[index]!='#') index++;
-		urikey->resource = (char *)pico_zalloc((size_t)(index-lastIndex+1));
+    /* extract resource */
+    if(!uri[index])
+    {
+        urikey->resource = pico_zalloc(2u);
+        urikey->resource[0] = '/';
+    }
+    else
+    {
+        lastIndex = index;
+        while(uri[index] && uri[index] != '?' && uri[index] != '&' && uri[index] != '#') index++;
+        urikey->resource = (char *)pico_zalloc((size_t)(index - lastIndex + 1));
 
-		if(!urikey->resource)
-		{
-			// no memory
-			pico_err = PICO_ERR_ENOMEM;
-			goto error;
-		}
+        if(!urikey->resource)
+        {
+            /* no memory */
+            pico_err = PICO_ERR_ENOMEM;
+            goto error;
+        }
 
-		memcpy(urikey->resource,uri+lastIndex,(size_t)(index-lastIndex));
-	}
+        memcpy(urikey->resource, uri + lastIndex, (size_t)(index - lastIndex));
+    }
 
-	return HTTP_RETURN_OK;
+    return HTTP_RETURN_OK;
 
-	error :
-	if(urikey->resource)
-	{
-		pico_free(urikey->resource);
-		urikey->resource = NULL;
-	}
-	if(urikey->host)
-	{
-		pico_free(urikey->host);
-		urikey->host = NULL;
-	}
+error:
+    if(urikey->resource)
+    {
+        pico_free(urikey->resource);
+        urikey->resource = NULL;
+    }
 
-	return HTTP_RETURN_ERROR;
+    if(urikey->host)
+    {
+        pico_free(urikey->host);
+        urikey->host = NULL;
+    }
+
+    return HTTP_RETURN_ERROR;
 }
 #endif
--- a/modules/pico_http_util.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_http_util.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,117 +1,117 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Author: Andrei Carp <andrei.carp@tass.be>
-*********************************************************************/
+   Author: Andrei Carp <andrei.carp@tass.be>
+ *********************************************************************/
 
 #ifndef PICO_HTTP_UTIL_H_
 #define PICO_HTTP_UTIL_H_
 
 /* Informational reponses */
-#define HTTP_CONTINUE 						100u
+#define HTTP_CONTINUE                       100u
 #define HTTP_SWITCHING_PROTOCOLS  101u
-#define HTTP_PROCESSING					  102u
+#define HTTP_PROCESSING                   102u
 
 /* Success */
-#define HTTP_OK										200u
-#define HTTP_CREATED							201u
-#define HTTP_ACCEPTED							202u
-#define HTTP_NON_AUTH_INFO				203u
-#define HTTP_NO_CONTENT						204u
-#define HTTP_RESET_CONTENT				205u
-#define HTTP_PARTIAL_CONTENT			206u
-#define HTTP_MULTI_STATUS					207u
-#define HTTP_ALREADY_REPORTED			208u
-#define HTTP_LOW_SPACE						250u
-#define HTTP_IM_SPACE							226u
+#define HTTP_OK                                     200u
+#define HTTP_CREATED                            201u
+#define HTTP_ACCEPTED                           202u
+#define HTTP_NON_AUTH_INFO              203u
+#define HTTP_NO_CONTENT                     204u
+#define HTTP_RESET_CONTENT              205u
+#define HTTP_PARTIAL_CONTENT            206u
+#define HTTP_MULTI_STATUS                   207u
+#define HTTP_ALREADY_REPORTED           208u
+#define HTTP_LOW_SPACE                      250u
+#define HTTP_IM_SPACE                           226u
 
 /* Redirection */
-#define HTTP_MULTI_CHOICE					300u
-#define HTTP_MOVED_PERMANENT			301u
-#define HTTP_FOUND								302u
-#define HTTP_SEE_OTHER						303u
-#define HTTP_NOT_MODIFIED					304u
-#define HTTP_USE_PROXY						305u
-#define HTTP_SWITCH_PROXY					306u
-#define HTTP_TEMP_REDIRECT				307u
-#define HTTP_PERM_REDIRECT				308u
+#define HTTP_MULTI_CHOICE                   300u
+#define HTTP_MOVED_PERMANENT            301u
+#define HTTP_FOUND                              302u
+#define HTTP_SEE_OTHER                      303u
+#define HTTP_NOT_MODIFIED                   304u
+#define HTTP_USE_PROXY                      305u
+#define HTTP_SWITCH_PROXY                   306u
+#define HTTP_TEMP_REDIRECT              307u
+#define HTTP_PERM_REDIRECT              308u
 
 /* Client error */
-#define HTTP_BAD_REQUEST					400u
-#define HTTP_UNAUTH								401u
-#define HTTP_PAYMENT_REQ					402u
-#define HTTP_FORBIDDEN						403u
-#define HTTP_NOT_FOUND						404u
-#define HTTP_METH_NOT_ALLOWED			405u
-#define HTTP_NOT_ACCEPTABLE				406u
-#define HTTP_PROXY_AUTH_REQ				407u
-#define HTTP_REQ_TIMEOUT					408u
-#define HTTP_CONFLICT							409u
-#define HTTP_GONE									410u
-#define HTTP_LEN_REQ							411u
-#define HTTP_PRECONDITION_FAIL		412u
-#define HTTP_REQ_ENT_LARGE				413u
-#define HTTP_URI_TOO_LONG					414u
-#define HTTP_UNSUPORTED_MEDIA			415u
-#define HTTP_REQ_RANGE_NOK				416u
-#define HTTP_EXPECT_FAILED				417u
-#define HTTP_TEAPOT								418u
-#define HTTP_UNPROC_ENTITY				422u
-#define HTTP_LOCKED								423u
-#define HTTP_METHOD_FAIL					424u
-#define HTTP_UNORDERED						425u
-#define HTTP_UPGRADE_REQ					426u
-#define HTTP_PRECOND_REQ					428u
-#define HTTP_TOO_MANY_REQ					429u
-#define HTTP_HEDER_FIELD_LARGE		431u
+#define HTTP_BAD_REQUEST                    400u
+#define HTTP_UNAUTH                             401u
+#define HTTP_PAYMENT_REQ                    402u
+#define HTTP_FORBIDDEN                      403u
+#define HTTP_NOT_FOUND                      404u
+#define HTTP_METH_NOT_ALLOWED           405u
+#define HTTP_NOT_ACCEPTABLE             406u
+#define HTTP_PROXY_AUTH_REQ             407u
+#define HTTP_REQ_TIMEOUT                    408u
+#define HTTP_CONFLICT                           409u
+#define HTTP_GONE                                   410u
+#define HTTP_LEN_REQ                            411u
+#define HTTP_PRECONDITION_FAIL      412u
+#define HTTP_REQ_ENT_LARGE              413u
+#define HTTP_URI_TOO_LONG                   414u
+#define HTTP_UNSUPORTED_MEDIA           415u
+#define HTTP_REQ_RANGE_NOK              416u
+#define HTTP_EXPECT_FAILED              417u
+#define HTTP_TEAPOT                             418u
+#define HTTP_UNPROC_ENTITY              422u
+#define HTTP_LOCKED                             423u
+#define HTTP_METHOD_FAIL                    424u
+#define HTTP_UNORDERED                      425u
+#define HTTP_UPGRADE_REQ                    426u
+#define HTTP_PRECOND_REQ                    428u
+#define HTTP_TOO_MANY_REQ                   429u
+#define HTTP_HEDER_FIELD_LARGE      431u
 
 /* Server error */
-#define HTTP_INTERNAL_SERVER_ERR	500u
-#define HTTP_NOT_IMPLEMENTED			501u
-#define HTTP_BAD_GATEWAY					502u
-#define HTTP_SERVICE_UNAVAILABLE	503u
-#define HTTP_GATEWAY_TIMEOUT			504u
-#define HTTP_NOT_SUPPORTED				505u
-#define HTTP_SERV_LOW_STORAGE			507u
-#define HTTP_LOOP_DETECTED				508u
-#define HTTP_NOT_EXTENDED					510u
-#define HTTP_NETWORK_AUTH					511u
-#define HTTP_PERMISSION_DENIED		550u
+#define HTTP_INTERNAL_SERVER_ERR    500u
+#define HTTP_NOT_IMPLEMENTED            501u
+#define HTTP_BAD_GATEWAY                    502u
+#define HTTP_SERVICE_UNAVAILABLE    503u
+#define HTTP_GATEWAY_TIMEOUT            504u
+#define HTTP_NOT_SUPPORTED              505u
+#define HTTP_SERV_LOW_STORAGE           507u
+#define HTTP_LOOP_DETECTED              508u
+#define HTTP_NOT_EXTENDED                   510u
+#define HTTP_NETWORK_AUTH                   511u
+#define HTTP_PERMISSION_DENIED      550u
 
 /* Returns used  */
 #define HTTP_RETURN_ERROR    -1
-#define HTTP_RETURN_OK				0
+#define HTTP_RETURN_OK              0
 
 /* List of events - shared between client and server */
-#define EV_HTTP_CON		    1u
+#define EV_HTTP_CON         1u
 #define EV_HTTP_REQ       2u
 #define EV_HTTP_PROGRESS  4u
-#define EV_HTTP_SENT		  8u
+#define EV_HTTP_SENT          8u
 #define EV_HTTP_CLOSE     16u
 #define EV_HTTP_ERROR     32u
-#define EV_HTTP_BODY			64u
-#define EV_HTTP_DNS				128u
+#define EV_HTTP_BODY            64u
+#define EV_HTTP_DNS             128u
 
 #ifndef TRUE
-	#define TRUE	1
+    #define TRUE    1
 #endif
 
 #ifndef FALSE
-	#define FALSE 0
+    #define FALSE 0
 #endif
 
 struct pico_http_uri
 {
-	uint8_t protoHttp; // is the protocol Http ?
-	char * host;			 // hostname
-	uint16_t port;		 // port if specified
-	char * resource;	 // resource , ignoring the other possible parameters
+    uint8_t protoHttp; /* is the protocol Http ? */
+    char *host;              /* hostname */
+    uint16_t port;       /* port if specified */
+    char *resource;      /* resource , ignoring the other possible parameters */
 };
 
-// used for chunks
-int pico_itoaHex(uint16_t port, char * ptr);
-uint16_t pico_itoa(uint16_t port, char * ptr);
-int8_t pico_processURI(const char * uri, struct pico_http_uri * urikey);
+/* used for chunks */
+int pico_itoaHex(uint16_t port, char *ptr);
+uint16_t pico_itoa(uint16_t port, char *ptr);
+int8_t pico_processURI(const char *uri, struct pico_http_uri *urikey);
 
 #endif /* PICO_HTTP_UTIL_H_ */
--- a/modules/pico_icmp4.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_icmp4.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 
 #include "pico_icmp4.h"
@@ -17,22 +17,27 @@
 #include "pico_tree.h"
 
 /* Queues */
-static struct pico_queue icmp_in = {0};
-static struct pico_queue icmp_out = {0};
+static struct pico_queue icmp_in = {
+    0
+};
+static struct pico_queue icmp_out = {
+    0
+};
 
 
 /* Functions */
 
 static int pico_icmp4_checksum(struct pico_frame *f)
 {
-  struct pico_icmp4_hdr *hdr = (struct pico_icmp4_hdr *) f->transport_hdr;
-  if (!hdr) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  hdr->crc = 0;
-  hdr->crc = short_be(pico_checksum(hdr, f->transport_len));
-  return 0;
+    struct pico_icmp4_hdr *hdr = (struct pico_icmp4_hdr *) f->transport_hdr;
+    if (!hdr) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    hdr->crc = 0;
+    hdr->crc = short_be(pico_checksum(hdr, f->transport_len));
+    return 0;
 }
 
 #ifdef PICO_SUPPORT_PING
@@ -41,102 +46,105 @@
 
 static int pico_icmp4_process_in(struct pico_protocol *self, struct pico_frame *f)
 {
-	struct pico_icmp4_hdr *hdr = (struct pico_icmp4_hdr *) f->transport_hdr;
-	IGNORE_PARAMETER(self);
+    struct pico_icmp4_hdr *hdr = (struct pico_icmp4_hdr *) f->transport_hdr;
+    IGNORE_PARAMETER(self);
+
+    if (hdr->type == PICO_ICMP_ECHO) {
+        hdr->type = PICO_ICMP_ECHOREPLY;
+        /* outgoing frames require a f->len without the ethernet header len */
+        if (f->dev->eth)
+            f->len -= PICO_SIZE_ETHHDR;
 
-  if (hdr->type == PICO_ICMP_ECHO) {
-    hdr->type = PICO_ICMP_ECHOREPLY;
-    /* outgoing frames require a f->len without the ethernet header len */
-    if (f->dev->eth)
-      f->len -= PICO_SIZE_ETHHDR;
-    pico_icmp4_checksum(f);
-    pico_ipv4_rebound(f);
-  } else if (hdr->type == PICO_ICMP_UNREACH) {
-    f->net_hdr = f->transport_hdr + PICO_ICMPHDR_UN_SIZE;
-    pico_ipv4_unreachable(f, hdr->code);
-  } else if (hdr->type == PICO_ICMP_ECHOREPLY) {
+        pico_icmp4_checksum(f);
+        pico_ipv4_rebound(f);
+    } else if (hdr->type == PICO_ICMP_UNREACH) {
+        f->net_hdr = f->transport_hdr + PICO_ICMPHDR_UN_SIZE;
+        pico_ipv4_unreachable(f, hdr->code);
+    } else if (hdr->type == PICO_ICMP_ECHOREPLY) {
 #ifdef PICO_SUPPORT_PING
-    ping_recv_reply(f);
+        ping_recv_reply(f);
 #endif
-    pico_frame_discard(f);
-  } else {
-    pico_frame_discard(f);
-  }
-  return 0;
+        pico_frame_discard(f);
+    } else {
+        pico_frame_discard(f);
+    }
+
+    return 0;
 }
 
 static int pico_icmp4_process_out(struct pico_protocol *self, struct pico_frame *f)
 {
-	IGNORE_PARAMETER(self);
-	IGNORE_PARAMETER(f);
-	dbg("Called %s\n", __FUNCTION__);
-  return 0;
+    IGNORE_PARAMETER(self);
+    IGNORE_PARAMETER(f);
+    dbg("Called %s\n", __FUNCTION__);
+    return 0;
 }
 
 /* Interface: protocol definition */
 struct pico_protocol pico_proto_icmp4 = {
-  .name = "icmp4",
-  .proto_number = PICO_PROTO_ICMP4,
-  .layer = PICO_LAYER_TRANSPORT,
-  .process_in = pico_icmp4_process_in,
-  .process_out = pico_icmp4_process_out,
-  .q_in = &icmp_in,
-  .q_out = &icmp_out,
+    .name = "icmp4",
+    .proto_number = PICO_PROTO_ICMP4,
+    .layer = PICO_LAYER_TRANSPORT,
+    .process_in = pico_icmp4_process_in,
+    .process_out = pico_icmp4_process_out,
+    .q_in = &icmp_in,
+    .q_out = &icmp_out,
 };
 
 static int pico_icmp4_notify(struct pico_frame *f, uint8_t type, uint8_t code)
 {
-  struct pico_frame *reply;
-  struct pico_icmp4_hdr *hdr;
-  struct pico_ipv4_hdr *info;
-  if (f == NULL) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  reply = pico_proto_ipv4.alloc(&pico_proto_ipv4, 8 + sizeof(struct pico_ipv4_hdr) + PICO_ICMPHDR_UN_SIZE);
-  info = (struct pico_ipv4_hdr*)(f->net_hdr);
-  hdr = (struct pico_icmp4_hdr *) reply->transport_hdr;
-  hdr->type = type;
-  hdr->code = code;
-  hdr->hun.ih_pmtu.ipm_nmtu = short_be(1500);
-  hdr->hun.ih_pmtu.ipm_void = 0;
-  reply->transport_len = 8 + sizeof(struct pico_ipv4_hdr) +  PICO_ICMPHDR_UN_SIZE;
-  reply->payload = reply->transport_hdr + PICO_ICMPHDR_UN_SIZE;
-  memcpy(reply->payload, f->net_hdr, 8 + sizeof(struct pico_ipv4_hdr));
-  pico_icmp4_checksum(reply);
-  pico_ipv4_frame_push(reply, &info->src, PICO_PROTO_ICMP4);
-  return 0;
+    struct pico_frame *reply;
+    struct pico_icmp4_hdr *hdr;
+    struct pico_ipv4_hdr *info;
+    if (f == NULL) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    reply = pico_proto_ipv4.alloc(&pico_proto_ipv4, 8 + sizeof(struct pico_ipv4_hdr) + PICO_ICMPHDR_UN_SIZE);
+    info = (struct pico_ipv4_hdr*)(f->net_hdr);
+    hdr = (struct pico_icmp4_hdr *) reply->transport_hdr;
+    hdr->type = type;
+    hdr->code = code;
+    hdr->hun.ih_pmtu.ipm_nmtu = short_be(1500);
+    hdr->hun.ih_pmtu.ipm_void = 0;
+    reply->transport_len = 8 + sizeof(struct pico_ipv4_hdr) +  PICO_ICMPHDR_UN_SIZE;
+    reply->payload = reply->transport_hdr + PICO_ICMPHDR_UN_SIZE;
+    memcpy(reply->payload, f->net_hdr, 8 + sizeof(struct pico_ipv4_hdr));
+    pico_icmp4_checksum(reply);
+    pico_ipv4_frame_push(reply, &info->src, PICO_PROTO_ICMP4);
+    return 0;
 }
 
 int pico_icmp4_port_unreachable(struct pico_frame *f)
 {
-  /*Parameter check executed in pico_icmp4_notify*/
-  return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_PORT);
+    /*Parameter check executed in pico_icmp4_notify*/
+    return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_PORT);
 }
 
 int pico_icmp4_proto_unreachable(struct pico_frame *f)
 {
-  /*Parameter check executed in pico_icmp4_notify*/
-  return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_PROTOCOL);
+    /*Parameter check executed in pico_icmp4_notify*/
+    return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_PROTOCOL);
 }
 
 int pico_icmp4_dest_unreachable(struct pico_frame *f)
 {
-  /*Parameter check executed in pico_icmp4_notify*/
-  return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_HOST);
+    /*Parameter check executed in pico_icmp4_notify*/
+    return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_HOST);
 }
 
 int pico_icmp4_ttl_expired(struct pico_frame *f)
 {
-  /*Parameter check executed in pico_icmp4_notify*/
-  return pico_icmp4_notify(f, PICO_ICMP_TIME_EXCEEDED, PICO_ICMP_TIMXCEED_INTRANS);
+    /*Parameter check executed in pico_icmp4_notify*/
+    return pico_icmp4_notify(f, PICO_ICMP_TIME_EXCEEDED, PICO_ICMP_TIMXCEED_INTRANS);
 }
 
 int pico_icmp4_packet_filtered(struct pico_frame *f)
 {
-  /*Parameter check executed in pico_icmp4_notify*/
-  /*Packet Filtered: type 3, code 13 (Communication Administratively Prohibited)*/
-  return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_FILTER_PROHIB);
+    /*Parameter check executed in pico_icmp4_notify*/
+    /*Packet Filtered: type 3, code 13 (Communication Administratively Prohibited)*/
+    return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_FILTER_PROHIB);
 }
 
 /***********************/
@@ -152,162 +160,166 @@
 
 struct pico_icmp4_ping_cookie
 {
-  struct pico_ip4 dst;
-  uint16_t err;
-  uint16_t id;
-  uint16_t seq;
-  uint16_t size;
-  int count;
-  pico_time timestamp;
-  int interval;
-  int timeout;
-  void (*cb)(struct pico_icmp4_stats*);
+    struct pico_ip4 dst;
+    uint16_t err;
+    uint16_t id;
+    uint16_t seq;
+    uint16_t size;
+    int count;
+    pico_time timestamp;
+    int interval;
+    int timeout;
+    void (*cb)(struct pico_icmp4_stats*);
 
 };
 
 static int cookie_compare(void *ka, void *kb)
 {
-	struct pico_icmp4_ping_cookie *a = ka, *b = kb;
-  if (a->id < b->id)
-    return -1;
-  if (a->id > b->id)
-    return 1;
-  return (a->seq - b->seq);
+    struct pico_icmp4_ping_cookie *a = ka, *b = kb;
+    if (a->id < b->id)
+        return -1;
+
+    if (a->id > b->id)
+        return 1;
+
+    return (a->seq - b->seq);
 }
 
-PICO_TREE_DECLARE(Pings,cookie_compare);
+PICO_TREE_DECLARE(Pings, cookie_compare);
 
 static uint8_t pico_icmp4_send_echo(struct pico_icmp4_ping_cookie *cookie)
 {
-  struct pico_frame *echo = pico_proto_ipv4.alloc(&pico_proto_ipv4, (uint16_t)(PICO_ICMPHDR_UN_SIZE + cookie->size));
-  struct pico_icmp4_hdr *hdr;
+    struct pico_frame *echo = pico_proto_ipv4.alloc(&pico_proto_ipv4, (uint16_t)(PICO_ICMPHDR_UN_SIZE + cookie->size));
+    struct pico_icmp4_hdr *hdr;
 
-  hdr = (struct pico_icmp4_hdr *) echo->transport_hdr;
+    hdr = (struct pico_icmp4_hdr *) echo->transport_hdr;
 
-  hdr->type = PICO_ICMP_ECHO;
-  hdr->code = 0;
-  hdr->hun.ih_idseq.idseq_id = short_be(cookie->id);
-  hdr->hun.ih_idseq.idseq_seq = short_be(cookie->seq);
-  echo->transport_len = (uint16_t)(PICO_ICMPHDR_UN_SIZE + cookie->size);
-  echo->payload = echo->transport_hdr + PICO_ICMPHDR_UN_SIZE;
-  echo->payload_len = cookie->size;
-  /* XXX: Fill payload */
-  pico_icmp4_checksum(echo);
-  pico_ipv4_frame_push(echo, &cookie->dst, PICO_PROTO_ICMP4);
-  return 0;
+    hdr->type = PICO_ICMP_ECHO;
+    hdr->code = 0;
+    hdr->hun.ih_idseq.idseq_id = short_be(cookie->id);
+    hdr->hun.ih_idseq.idseq_seq = short_be(cookie->seq);
+    echo->transport_len = (uint16_t)(PICO_ICMPHDR_UN_SIZE + cookie->size);
+    echo->payload = echo->transport_hdr + PICO_ICMPHDR_UN_SIZE;
+    echo->payload_len = cookie->size;
+    /* XXX: Fill payload */
+    pico_icmp4_checksum(echo);
+    pico_ipv4_frame_push(echo, &cookie->dst, PICO_PROTO_ICMP4);
+    return 0;
 }
 
 
 static void ping_timeout(pico_time now, void *arg)
 {
-  struct pico_icmp4_ping_cookie *cookie = (struct pico_icmp4_ping_cookie *)arg;
-  IGNORE_PARAMETER(now);
+    struct pico_icmp4_ping_cookie *cookie = (struct pico_icmp4_ping_cookie *)arg;
+    IGNORE_PARAMETER(now);
 
-  if(pico_tree_findKey(&Pings,cookie)){
-    if (cookie->err == PICO_PING_ERR_PENDING) {
-      struct pico_icmp4_stats stats;
-      stats.dst = cookie->dst;
-      stats.seq = cookie->seq;
-      stats.time = 0;
-      stats.size = cookie->size;
-      stats.err = PICO_PING_ERR_TIMEOUT;
-      dbg(" ---- Ping timeout!!!\n");
-      cookie->cb(&stats);
+    if(pico_tree_findKey(&Pings, cookie)) {
+        if (cookie->err == PICO_PING_ERR_PENDING) {
+            struct pico_icmp4_stats stats;
+            stats.dst = cookie->dst;
+            stats.seq = cookie->seq;
+            stats.time = 0;
+            stats.size = cookie->size;
+            stats.err = PICO_PING_ERR_TIMEOUT;
+            dbg(" ---- Ping timeout!!!\n");
+            cookie->cb(&stats);
+        }
+
+        pico_tree_delete(&Pings, cookie);
+        pico_free(cookie);
     }
-
-    pico_tree_delete(&Pings,cookie);
-    pico_free(cookie);
-  }
 }
 
 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);
-  cookie->timestamp = pico_tick;
-  pico_timer_add((uint32_t)cookie->timeout, ping_timeout, cookie);
-  if (cookie->seq < cookie->count)
-    pico_timer_add((uint32_t)cookie->interval, next_ping, cookie);
+    pico_icmp4_send_echo(cookie);
+    cookie->timestamp = pico_tick;
+    pico_timer_add((uint32_t)cookie->timeout, ping_timeout, cookie);
+    if (cookie->seq < cookie->count)
+        pico_timer_add((uint32_t)cookie->interval, next_ping, cookie);
 }
 
 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);
+    struct pico_icmp4_ping_cookie *newcookie, *cookie = (struct pico_icmp4_ping_cookie *)arg;
+    IGNORE_PARAMETER(now);
 
-	if(pico_tree_findKey(&Pings,cookie)){
-    if (cookie->seq < cookie->count) {
-      newcookie = pico_zalloc(sizeof(struct pico_icmp4_ping_cookie));
-      if (!newcookie)
-        return;
-      memcpy(newcookie, cookie, sizeof(struct pico_icmp4_ping_cookie));
-      newcookie->seq++;
+    if(pico_tree_findKey(&Pings, cookie)) {
+        if (cookie->seq < cookie->count) {
+            newcookie = pico_zalloc(sizeof(struct pico_icmp4_ping_cookie));
+            if (!newcookie)
+                return;
 
-	    pico_tree_insert(&Pings,newcookie);
-      send_ping(newcookie);
+            memcpy(newcookie, cookie, sizeof(struct pico_icmp4_ping_cookie));
+            newcookie->seq++;
+
+            pico_tree_insert(&Pings, newcookie);
+            send_ping(newcookie);
+        }
     }
-  }
 }
 
 
 static void ping_recv_reply(struct pico_frame *f)
 {
-  struct pico_icmp4_ping_cookie test, *cookie;
-  struct pico_icmp4_hdr *hdr = (struct pico_icmp4_hdr *) f->transport_hdr;
-  test.id  = short_be(hdr->hun.ih_idseq.idseq_id );
-  test.seq = short_be(hdr->hun.ih_idseq.idseq_seq);
+    struct pico_icmp4_ping_cookie test, *cookie;
+    struct pico_icmp4_hdr *hdr = (struct pico_icmp4_hdr *) f->transport_hdr;
+    test.id  = short_be(hdr->hun.ih_idseq.idseq_id );
+    test.seq = short_be(hdr->hun.ih_idseq.idseq_seq);
 
-  cookie = pico_tree_findKey(&Pings, &test);
-  if (cookie) {
-    struct pico_icmp4_stats stats;
-    cookie->err = PICO_PING_ERR_REPLIED;
-    stats.dst = cookie->dst;
-    stats.seq = cookie->seq;
-    stats.size = cookie->size;
-    stats.time = pico_tick - cookie->timestamp;
-    stats.err = cookie->err;
-    stats.ttl = ((struct pico_ipv4_hdr *)f->net_hdr)->ttl;
-		if(cookie->cb != NULL)
-    	cookie->cb(&stats);
-  } else {
-    dbg("Reply for seq=%d, not found.\n", test.seq);
-  }
+    cookie = pico_tree_findKey(&Pings, &test);
+    if (cookie) {
+        struct pico_icmp4_stats stats;
+        cookie->err = PICO_PING_ERR_REPLIED;
+        stats.dst = cookie->dst;
+        stats.seq = cookie->seq;
+        stats.size = cookie->size;
+        stats.time = pico_tick - cookie->timestamp;
+        stats.err = cookie->err;
+        stats.ttl = ((struct pico_ipv4_hdr *)f->net_hdr)->ttl;
+        if(cookie->cb != NULL)
+            cookie->cb(&stats);
+    } else {
+        dbg("Reply for seq=%d, not found.\n", test.seq);
+    }
 }
 
 int pico_icmp4_ping(char *dst, int count, int interval, int timeout, int size, void (*cb)(struct pico_icmp4_stats *))
 {
-  static uint16_t next_id = 0x91c0;
-  struct pico_icmp4_ping_cookie *cookie;
+    static uint16_t next_id = 0x91c0;
+    struct pico_icmp4_ping_cookie *cookie;
 
-  if((dst == NULL) || (interval == 0) || (timeout == 0) || (count == 0)){
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if((dst == NULL) || (interval == 0) || (timeout == 0) || (count == 0)) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  cookie = pico_zalloc(sizeof(struct pico_icmp4_ping_cookie));
-  if (!cookie) {
-    pico_err = PICO_ERR_ENOMEM;
-    return -1;
-  }
+    cookie = pico_zalloc(sizeof(struct pico_icmp4_ping_cookie));
+    if (!cookie) {
+        pico_err = PICO_ERR_ENOMEM;
+        return -1;
+    }
 
-  if (pico_string_to_ipv4(dst, &cookie->dst.addr) < 0) {
-    pico_err = PICO_ERR_EINVAL;
-    pico_free(cookie);
-    return -1;
-  }
-  cookie->seq = 1;
-  cookie->id = next_id++;
-  cookie->err = PICO_PING_ERR_PENDING;
-  cookie->size = (uint16_t)size;
-  cookie->interval = interval;
-  cookie->timeout = timeout;
-  cookie->cb = cb;
-  cookie->count = count;
+    if (pico_string_to_ipv4(dst, &cookie->dst.addr) < 0) {
+        pico_err = PICO_ERR_EINVAL;
+        pico_free(cookie);
+        return -1;
+    }
 
-  pico_tree_insert(&Pings,cookie);
-  send_ping(cookie);
+    cookie->seq = 1;
+    cookie->id = next_id++;
+    cookie->err = PICO_PING_ERR_PENDING;
+    cookie->size = (uint16_t)size;
+    cookie->interval = interval;
+    cookie->timeout = timeout;
+    cookie->cb = cb;
+    cookie->count = count;
 
-  return 0;
+    pico_tree_insert(&Pings, cookie);
+    send_ping(cookie);
+
+    return 0;
 
 }
 
--- a/modules/pico_icmp4.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_icmp4.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_ICMP4
 #define _INCLUDE_PICO_ICMP4
 #include "pico_addressing.h"
@@ -13,56 +13,56 @@
 extern struct pico_protocol pico_proto_icmp4;
 
 struct __attribute__((packed)) pico_icmp4_hdr {
-  uint8_t type;
-  uint8_t code;
-  uint16_t crc;
+    uint8_t type;
+    uint8_t code;
+    uint16_t crc;
 
-  /* hun */
-  union {
-    uint8_t ih_pptr;
-    struct pico_ip4 ih_gwaddr;
-    struct {
-      uint16_t idseq_id;
-      uint16_t idseq_seq;
-    } ih_idseq;
-    uint32_t ih_void;
-    struct {
-      uint16_t ipm_void;
-      uint16_t ipm_nmtu;
-    } ih_pmtu;
-    struct {
-      uint8_t rta_numgw;
-      uint8_t rta_wpa;
-      uint16_t rta_lifetime;
-    } ih_rta;
-  } hun;
+    /* hun */
+    union {
+        uint8_t ih_pptr;
+        struct pico_ip4 ih_gwaddr;
+        struct {
+            uint16_t idseq_id;
+            uint16_t idseq_seq;
+        } ih_idseq;
+        uint32_t ih_void;
+        struct {
+            uint16_t ipm_void;
+            uint16_t ipm_nmtu;
+        } ih_pmtu;
+        struct {
+            uint8_t rta_numgw;
+            uint8_t rta_wpa;
+            uint16_t rta_lifetime;
+        } ih_rta;
+    } hun;
 
-  /* dun */
-  union {
-    struct {
-      uint32_t ts_otime;
-      uint32_t ts_rtime;
-      uint32_t ts_ttime;
-    } id_ts;
-    struct {
-      uint32_t ip_options;
-      uint32_t ip_data_hi;
-      uint32_t ip_data_lo;
-    } id_ip;
-    struct {
-      uint32_t ira_addr;
-      uint32_t ira_pref;
-    } id_ra;
-    uint32_t id_mask;
-    uint8_t  id_data[1];
-  } dun;
+    /* dun */
+    union {
+        struct {
+            uint32_t ts_otime;
+            uint32_t ts_rtime;
+            uint32_t ts_ttime;
+        } id_ts;
+        struct {
+            uint32_t ip_options;
+            uint32_t ip_data_hi;
+            uint32_t ip_data_lo;
+        } id_ip;
+        struct {
+            uint32_t ira_addr;
+            uint32_t ira_pref;
+        } id_ra;
+        uint32_t id_mask;
+        uint8_t id_data[1];
+    } dun;
 };
 
 #define PICO_ICMPHDR_DRY_SIZE  4
 #define PICO_ICMPHDR_UN_SIZE  8u
 
-#define PICO_ICMP_ECHOREPLY    0 
-#define PICO_ICMP_DEST_UNREACH 3 
+#define PICO_ICMP_ECHOREPLY    0
+#define PICO_ICMP_DEST_UNREACH 3
 #define PICO_ICMP_SOURCE_QUENCH  4
 #define PICO_ICMP_REDIRECT   5
 #define PICO_ICMP_ECHO   8
@@ -76,62 +76,62 @@
 #define PICO_ICMP_ADDRESSREPLY 18
 
 
-#define  PICO_ICMP_UNREACH    3  
-#define  PICO_ICMP_SOURCEQUENCH  4  
-#define  PICO_ICMP_ROUTERADVERT  9  
-#define  PICO_ICMP_ROUTERSOLICIT  10  
-#define  PICO_ICMP_TIMXCEED    11  
-#define  PICO_ICMP_PARAMPROB    12  
-#define  PICO_ICMP_TSTAMP    13  
-#define  PICO_ICMP_TSTAMPREPLY  14  
-#define  PICO_ICMP_IREQ    15  
-#define  PICO_ICMP_IREQREPLY    16    
-#define  PICO_ICMP_MASKREQ    17    
-#define  PICO_ICMP_MASKREPLY    18    
+#define  PICO_ICMP_UNREACH    3
+#define  PICO_ICMP_SOURCEQUENCH  4
+#define  PICO_ICMP_ROUTERADVERT  9
+#define  PICO_ICMP_ROUTERSOLICIT  10
+#define  PICO_ICMP_TIMXCEED    11
+#define  PICO_ICMP_PARAMPROB    12
+#define  PICO_ICMP_TSTAMP    13
+#define  PICO_ICMP_TSTAMPREPLY  14
+#define  PICO_ICMP_IREQ    15
+#define  PICO_ICMP_IREQREPLY    16
+#define  PICO_ICMP_MASKREQ    17
+#define  PICO_ICMP_MASKREPLY    18
 
 #define  PICO_ICMP_MAXTYPE    18
 
 
-#define  PICO_ICMP_UNREACH_NET          0  
-#define  PICO_ICMP_UNREACH_HOST          1  
-#define  PICO_ICMP_UNREACH_PROTOCOL          2  
-#define  PICO_ICMP_UNREACH_PORT          3  
-#define  PICO_ICMP_UNREACH_NEEDFRAG          4  
-#define  PICO_ICMP_UNREACH_SRCFAIL          5  
-#define  PICO_ICMP_UNREACH_NET_UNKNOWN        6  
-#define  PICO_ICMP_UNREACH_HOST_UNKNOWN       7  
-#define  PICO_ICMP_UNREACH_ISOLATED          8  
-#define  PICO_ICMP_UNREACH_NET_PROHIB          9  
-#define  PICO_ICMP_UNREACH_HOST_PROHIB        10  
-#define  PICO_ICMP_UNREACH_TOSNET          11  
-#define  PICO_ICMP_UNREACH_TOSHOST          12  
-#define  PICO_ICMP_UNREACH_FILTER_PROHIB      13  
-#define  PICO_ICMP_UNREACH_HOST_PRECEDENCE    14  
-#define  PICO_ICMP_UNREACH_PRECEDENCE_CUTOFF  15  
+#define  PICO_ICMP_UNREACH_NET          0
+#define  PICO_ICMP_UNREACH_HOST          1
+#define  PICO_ICMP_UNREACH_PROTOCOL          2
+#define  PICO_ICMP_UNREACH_PORT          3
+#define  PICO_ICMP_UNREACH_NEEDFRAG          4
+#define  PICO_ICMP_UNREACH_SRCFAIL          5
+#define  PICO_ICMP_UNREACH_NET_UNKNOWN        6
+#define  PICO_ICMP_UNREACH_HOST_UNKNOWN       7
+#define  PICO_ICMP_UNREACH_ISOLATED          8
+#define  PICO_ICMP_UNREACH_NET_PROHIB          9
+#define  PICO_ICMP_UNREACH_HOST_PROHIB        10
+#define  PICO_ICMP_UNREACH_TOSNET          11
+#define  PICO_ICMP_UNREACH_TOSHOST          12
+#define  PICO_ICMP_UNREACH_FILTER_PROHIB      13
+#define  PICO_ICMP_UNREACH_HOST_PRECEDENCE    14
+#define  PICO_ICMP_UNREACH_PRECEDENCE_CUTOFF  15
 
 
-#define  PICO_ICMP_REDIRECT_NET  0    
-#define  PICO_ICMP_REDIRECT_HOST  1    
-#define  PICO_ICMP_REDIRECT_TOSNET  2    
-#define  PICO_ICMP_REDIRECT_TOSHOST  3    
+#define  PICO_ICMP_REDIRECT_NET  0
+#define  PICO_ICMP_REDIRECT_HOST  1
+#define  PICO_ICMP_REDIRECT_TOSNET  2
+#define  PICO_ICMP_REDIRECT_TOSHOST  3
 
 
-#define  PICO_ICMP_TIMXCEED_INTRANS  0    
-#define  PICO_ICMP_TIMXCEED_REASS  1    
+#define  PICO_ICMP_TIMXCEED_INTRANS  0
+#define  PICO_ICMP_TIMXCEED_REASS  1
 
 
-#define  PICO_ICMP_PARAMPROB_OPTABSENT 1    
+#define  PICO_ICMP_PARAMPROB_OPTABSENT 1
 
 #define PICO_SIZE_ICMP4HDR ((sizeof(struct pico_icmp4_hdr)))
 
 struct pico_icmp4_stats
 {
-  struct pico_ip4 dst;
-  unsigned long size;
-  unsigned long seq;
-  pico_time time;
-  unsigned long ttl;
-  int err;
+    struct pico_ip4 dst;
+    unsigned long size;
+    unsigned long seq;
+    pico_time time;
+    unsigned long ttl;
+    int err;
 };
 
 int pico_icmp4_port_unreachable(struct pico_frame *f);
--- a/modules/pico_igmp.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_igmp.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-RFC 1112, 2236, 3376, 3569, 3678, 4607
+   RFC 1112, 2236, 3376, 3569, 3678, 4607
 
-Authors: Kristof Roelants (IGMPv3), Simon Maes, Brecht Van Cauwenberghe 
-*********************************************************************/
+   Authors: Kristof Roelants (IGMPv3), Simon Maes, Brecht Van Cauwenberghe
+ *********************************************************************/
 
 #include "pico_stack.h"
 #include "pico_ipv4.h"
@@ -18,15 +18,15 @@
 #include "pico_device.h"
 #include "pico_socket.h"
 
-#define igmp_dbg(...) do{}while(0)
-//#define igmp_dbg dbg
+#define igmp_dbg(...) do {} while(0)
+/* #define igmp_dbg dbg */
 
 /* membership states */
 #define IGMP_STATE_NON_MEMBER             (0x0)
 #define IGMP_STATE_DELAYING_MEMBER        (0x1)
 #define IGMP_STATE_IDLE_MEMBER            (0x2)
 
-/* events */ 
+/* events */
 #define IGMP_EVENT_DELETE_GROUP           (0x0)
 #define IGMP_EVENT_CREATE_GROUP           (0x1)
 #define IGMP_EVENT_UPDATE_GROUP           (0x2)
@@ -80,66 +80,70 @@
 #define IGMP_MAX_GROUPS                   (32) /* max 255 */
 
 struct __attribute__((packed)) igmp_message {
-  uint8_t type;
-  uint8_t max_resp_time;
-  uint16_t crc;
-  uint32_t mcast_group;
+    uint8_t type;
+    uint8_t max_resp_time;
+    uint16_t crc;
+    uint32_t mcast_group;
 };
 
 struct __attribute__((packed)) igmpv3_query {
-  uint8_t type;
-  uint8_t max_resp_time;
-  uint16_t crc;
-  uint32_t mcast_group;
-  uint8_t rsq;
-  uint8_t qqic;
-  uint16_t sources;
-  uint32_t source_addr[];
+    uint8_t type;
+    uint8_t max_resp_time;
+    uint16_t crc;
+    uint32_t mcast_group;
+    uint8_t rsq;
+    uint8_t qqic;
+    uint16_t sources;
+    uint32_t source_addr[];
 };
 
 struct __attribute__((packed)) igmpv3_group_record {
-  uint8_t type;
-  uint8_t aux;
-  uint16_t sources;
-  uint32_t mcast_group;
-  uint32_t source_addr[];
+    uint8_t type;
+    uint8_t aux;
+    uint16_t sources;
+    uint32_t mcast_group;
+    uint32_t source_addr[];
 };
 
 struct __attribute__((packed)) igmpv3_report {
-  uint8_t type;
-  uint8_t res0;
-  uint16_t crc;
-  uint16_t res1;
-  uint16_t groups;
-  struct igmpv3_group_record record[];
+    uint8_t type;
+    uint8_t res0;
+    uint16_t crc;
+    uint16_t res1;
+    uint16_t groups;
+    struct igmpv3_group_record record[];
 };
 
 struct igmp_parameters {
-  uint8_t event;
-  uint8_t state;
-  uint8_t last_host;
-  uint8_t filter_mode;
-  uint8_t max_resp_time;
-  struct pico_ip4 mcast_link;
-  struct pico_ip4 mcast_group;
-  struct pico_tree *MCASTFilter;
-  struct pico_frame *f;
+    uint8_t event;
+    uint8_t state;
+    uint8_t last_host;
+    uint8_t filter_mode;
+    uint8_t max_resp_time;
+    struct pico_ip4 mcast_link;
+    struct pico_ip4 mcast_group;
+    struct pico_tree *MCASTFilter;
+    struct pico_frame *f;
 };
 
 struct igmp_timer {
-  uint8_t type;
-  uint8_t stopped;
-  pico_time start;
-  pico_time delay;
-  struct pico_ip4 mcast_link;
-  struct pico_ip4 mcast_group;
-  struct pico_frame *f;
-  void (*callback)(struct igmp_timer *t);
+    uint8_t type;
+    uint8_t stopped;
+    pico_time start;
+    pico_time delay;
+    struct pico_ip4 mcast_link;
+    struct pico_ip4 mcast_group;
+    struct pico_frame *f;
+    void (*callback)(struct igmp_timer *t);
 };
 
 /* queues */
-static struct pico_queue igmp_in = {0};
-static struct pico_queue igmp_out = {0};
+static struct pico_queue igmp_in = {
+    0
+};
+static struct pico_queue igmp_out = {
+    0
+};
 
 /* finite state machine caller */
 static int pico_igmp_process_event(struct igmp_parameters *p);
@@ -150,975 +154,1051 @@
 /* redblack trees */
 static int igmp_timer_cmp(void *ka, void *kb)
 {
-  struct igmp_timer *a = ka, *b =kb;
-  if (a->type < b->type)
-    return -1;
-  if (a->type > b->type)
-    return 1;
-  if (a->mcast_group.addr < b->mcast_group.addr)
-    return -1;
-  if (a->mcast_group.addr > b->mcast_group.addr)
-    return 1;
-  if (a->mcast_link.addr < b->mcast_link.addr)
-    return -1;
-  if (a->mcast_link.addr > b->mcast_link.addr)
-    return 1;
-  return 0;
+    struct igmp_timer *a = ka, *b = kb;
+    if (a->type < b->type)
+        return -1;
+
+    if (a->type > b->type)
+        return 1;
+
+    if (a->mcast_group.addr < b->mcast_group.addr)
+        return -1;
+
+    if (a->mcast_group.addr > b->mcast_group.addr)
+        return 1;
+
+    if (a->mcast_link.addr < b->mcast_link.addr)
+        return -1;
+
+    if (a->mcast_link.addr > b->mcast_link.addr)
+        return 1;
+
+    return 0;
 }
 PICO_TREE_DECLARE(IGMPTimers, igmp_timer_cmp);
 
 static int igmp_parameters_cmp(void *ka, void *kb)
 {
-  struct igmp_parameters *a = ka, *b = kb;
-  if (a->mcast_group.addr < b->mcast_group.addr)
-    return -1;
-  if (a->mcast_group.addr > b->mcast_group.addr)
-    return 1;
-  if (a->mcast_link.addr < b->mcast_link.addr)
-    return -1;
-  if (a->mcast_link.addr > b->mcast_link.addr)
-    return 1;
-  return 0;
+    struct igmp_parameters *a = ka, *b = kb;
+    if (a->mcast_group.addr < b->mcast_group.addr)
+        return -1;
+
+    if (a->mcast_group.addr > b->mcast_group.addr)
+        return 1;
+
+    if (a->mcast_link.addr < b->mcast_link.addr)
+        return -1;
+
+    if (a->mcast_link.addr > b->mcast_link.addr)
+        return 1;
+
+    return 0;
 }
 PICO_TREE_DECLARE(IGMPParameters, igmp_parameters_cmp);
 
 static int igmp_sources_cmp(void *ka, void *kb)
 {
-  struct pico_ip4 *a = ka, *b = kb;
-  if (a->addr < b->addr)
-    return -1;
-  if (a->addr > b->addr)
-    return 1;
-  return 0;
+    struct pico_ip4 *a = ka, *b = kb;
+    if (a->addr < b->addr)
+        return -1;
+
+    if (a->addr > b->addr)
+        return 1;
+
+    return 0;
 }
 PICO_TREE_DECLARE(IGMPAllow, igmp_sources_cmp);
 PICO_TREE_DECLARE(IGMPBlock, igmp_sources_cmp);
 
 static struct igmp_parameters *pico_igmp_find_parameter(struct pico_ip4 *mcast_link, struct pico_ip4 *mcast_group)
 {
-  struct igmp_parameters test = {0};
-  test.mcast_link.addr = mcast_link->addr;
-  test.mcast_group.addr = mcast_group->addr;
-  return pico_tree_findKey(&IGMPParameters, &test);
+    struct igmp_parameters test = {
+        0
+    };
+    test.mcast_link.addr = mcast_link->addr;
+    test.mcast_group.addr = mcast_group->addr;
+    return pico_tree_findKey(&IGMPParameters, &test);
 }
 
 static int pico_igmp_delete_parameter(struct igmp_parameters *p)
 {
-  if (pico_tree_delete(&IGMPParameters, p))
-    pico_free(p);
-  else
-    return -1;
+    if (pico_tree_delete(&IGMPParameters, p))
+        pico_free(p);
+    else
+        return -1;
 
-  return 0;
+    return 0;
 }
 
 static void pico_igmp_timer_expired(pico_time now, void *arg)
 {
-  struct igmp_timer *t = NULL, *timer = NULL, test = {0};
+    struct igmp_timer *t = NULL, *timer = NULL, test = {
+        0
+    };
+
+    IGNORE_PARAMETER(now);
+    t = (struct igmp_timer *)arg;
+    test.type = t->type;
+    test.mcast_link = t->mcast_link;
+    test.mcast_group = t->mcast_group;
+    igmp_dbg("IGMP: timer expired for %08X link %08X type %u, delay %lu\n", t->mcast_group.addr, t->mcast_link.addr, t->type, t->delay);
+    timer = pico_tree_findKey(&IGMPTimers, &test);
+    if (!timer) {
+        return;
+    }
 
-  IGNORE_PARAMETER(now);
-  t = (struct igmp_timer *)arg;
-  test.type = t->type;
-  test.mcast_link = t->mcast_link;
-  test.mcast_group = t->mcast_group;
-  igmp_dbg("IGMP: timer expired for %08X link %08X type %u, delay %lu\n", t->mcast_group.addr, t->mcast_link.addr, t->type, t->delay);
-  timer = pico_tree_findKey(&IGMPTimers, &test);
-  if (!timer) {
+    if (timer->stopped == IGMP_TIMER_STOPPED) {
+        pico_free(t);
+        return;
+    }
+
+    if (timer->start + timer->delay < PICO_TIME_MS()) {
+        pico_tree_delete(&IGMPTimers, timer);
+        if (timer->callback)
+            timer->callback(timer);
+
+        pico_free(timer);
+    } else {
+        igmp_dbg("IGMP: restart timer for %08X, delay %lu, new delay %lu\n", t->mcast_group.addr, t->delay,  (timer->start + timer->delay) - PICO_TIME_MS());
+        pico_timer_add((timer->start + timer->delay) - PICO_TIME_MS(), &pico_igmp_timer_expired, timer);
+    }
+
     return;
-  }
-  if (timer->stopped == IGMP_TIMER_STOPPED) {
-    pico_free(t);
-    return;
-  }
-  if (timer->start + timer->delay < PICO_TIME_MS()) {
-    pico_tree_delete(&IGMPTimers, timer);
-    if (timer->callback)
-      timer->callback(timer);
-    pico_free(timer);
-  } else {
-    igmp_dbg("IGMP: restart timer for %08X, delay %lu, new delay %lu\n", t->mcast_group.addr, t->delay,  (timer->start + timer->delay) - PICO_TIME_MS());
-    pico_timer_add((timer->start + timer->delay) - PICO_TIME_MS(), &pico_igmp_timer_expired, timer);
-  }
-  return;
 }
 
 static int pico_igmp_timer_reset(struct igmp_timer *t)
 {
-  struct igmp_timer *timer = NULL, test = {0};
+    struct igmp_timer *timer = NULL, test = {
+        0
+    };
 
-  igmp_dbg("IGMP: reset timer for %08X, delay %lu\n", t->mcast_group.addr, t->delay);
-  test.type = t->type;
-  test.mcast_link = t->mcast_link;
-  test.mcast_group = t->mcast_group;
-  timer = pico_tree_findKey(&IGMPTimers, &test);
-  if (!timer)
-    return -1;
+    igmp_dbg("IGMP: reset timer for %08X, delay %lu\n", t->mcast_group.addr, t->delay);
+    test.type = t->type;
+    test.mcast_link = t->mcast_link;
+    test.mcast_group = t->mcast_group;
+    timer = pico_tree_findKey(&IGMPTimers, &test);
+    if (!timer)
+        return -1;
 
-  *timer = *t;
-  timer->start = PICO_TIME_MS();
-  return 0;
+    *timer = *t;
+    timer->start = PICO_TIME_MS();
+    return 0;
 }
 
 static int pico_igmp_timer_start(struct igmp_timer *t)
 {
-  struct igmp_timer *timer = NULL, test = {0};
+    struct igmp_timer *timer = NULL, test = {
+        0
+    };
 
-  igmp_dbg("IGMP: start timer for %08X link %08X type %u, delay %lu\n", t->mcast_group.addr, t->mcast_link.addr, t->type, t->delay);
-  test.type = t->type;
-  test.mcast_link = t->mcast_link;
-  test.mcast_group = t->mcast_group;
-  timer = pico_tree_findKey(&IGMPTimers, &test);
-  if (timer)
-    return pico_igmp_timer_reset(t);
+    igmp_dbg("IGMP: start timer for %08X link %08X type %u, delay %lu\n", t->mcast_group.addr, t->mcast_link.addr, t->type, t->delay);
+    test.type = t->type;
+    test.mcast_link = t->mcast_link;
+    test.mcast_group = t->mcast_group;
+    timer = pico_tree_findKey(&IGMPTimers, &test);
+    if (timer)
+        return pico_igmp_timer_reset(t);
 
-  timer = pico_zalloc(sizeof(struct igmp_timer));
-  if (!timer) {
-    pico_err = PICO_ERR_ENOMEM;
-    return -1;
-  }
-  *timer = *t;
-  timer->start = PICO_TIME_MS();
+    timer = pico_zalloc(sizeof(struct igmp_timer));
+    if (!timer) {
+        pico_err = PICO_ERR_ENOMEM;
+        return -1;
+    }
 
-  pico_tree_insert(&IGMPTimers, timer);
-  pico_timer_add(timer->delay, &pico_igmp_timer_expired, timer);
-  return 0;
+    *timer = *t;
+    timer->start = PICO_TIME_MS();
+
+    pico_tree_insert(&IGMPTimers, timer);
+    pico_timer_add(timer->delay, &pico_igmp_timer_expired, timer);
+    return 0;
 }
 
 static int pico_igmp_timer_stop(struct igmp_timer *t)
 {
-  struct igmp_timer *timer = NULL, test = {0};
+    struct igmp_timer *timer = NULL, test = {
+        0
+    };
 
-  test.type = t->type;
-  test.mcast_link = t->mcast_link;
-  test.mcast_group = t->mcast_group;
-  timer = pico_tree_findKey(&IGMPTimers, &test);
-  if (!timer)
+    test.type = t->type;
+    test.mcast_link = t->mcast_link;
+    test.mcast_group = t->mcast_group;
+    timer = pico_tree_findKey(&IGMPTimers, &test);
+    if (!timer)
+        return 0;
+
+    igmp_dbg("IGMP: stop timer for %08X, delay %lu\n", timer->mcast_group.addr, timer->delay);
+    timer->stopped = IGMP_TIMER_STOPPED;
     return 0;
-
-  igmp_dbg("IGMP: stop timer for %08X, delay %lu\n", timer->mcast_group.addr, timer->delay);
-  timer->stopped = IGMP_TIMER_STOPPED;
-  return 0;
 }
 
 static int pico_igmp_timer_is_running(struct igmp_timer *t)
 {
-  struct igmp_timer *timer = NULL, test = {0};
+    struct igmp_timer *timer = NULL, test = {
+        0
+    };
 
-  test.type = t->type;
-  test.mcast_link = t->mcast_link;
-  test.mcast_group = t->mcast_group;
-  timer = pico_tree_findKey(&IGMPTimers, &test);
-  if (timer)
-    return 1;
-  return 0;
+    test.type = t->type;
+    test.mcast_link = t->mcast_link;
+    test.mcast_group = t->mcast_group;
+    timer = pico_tree_findKey(&IGMPTimers, &test);
+    if (timer)
+        return 1;
+
+    return 0;
 }
 
 static struct igmp_timer *pico_igmp_find_timer(uint8_t type, struct pico_ip4 *mcast_link, struct pico_ip4 *mcast_group)
 {
-  struct igmp_timer test = {0};
+    struct igmp_timer test = {
+        0
+    };
 
-  test.type = type;
-  test.mcast_link = *mcast_link;
-  test.mcast_group = *mcast_group;
-  return pico_tree_findKey(&IGMPTimers, &test);
+    test.type = type;
+    test.mcast_link = *mcast_link;
+    test.mcast_group = *mcast_group;
+    return pico_tree_findKey(&IGMPTimers, &test);
 }
 
 static void pico_igmp_report_expired(struct igmp_timer *t)
 {
-  struct igmp_parameters *p = NULL;
+    struct igmp_parameters *p = NULL;
 
-  p = pico_igmp_find_parameter(&t->mcast_link, &t->mcast_group);
-  if (!p)
-    return;
+    p = pico_igmp_find_parameter(&t->mcast_link, &t->mcast_group);
+    if (!p)
+        return;
 
-  p->event = IGMP_EVENT_TIMER_EXPIRED;
-  pico_igmp_process_event(p);
+    p->event = IGMP_EVENT_TIMER_EXPIRED;
+    pico_igmp_process_event(p);
 }
 
 static void pico_igmp_v2querier_expired(struct igmp_timer *t)
 {
-  struct pico_ipv4_link *link = NULL;
-  struct pico_tree_node *index = NULL, *_tmp = NULL;
+    struct pico_ipv4_link *link = NULL;
+    struct pico_tree_node *index = NULL, *_tmp = NULL;
 
-  link = pico_ipv4_link_by_dev(t->f->dev);
-  if (!link)
-    return;
+    link = pico_ipv4_link_by_dev(t->f->dev);
+    if (!link)
+        return;
 
-  /* When changing compatibility mode, cancel all pending response 
-   * and retransmission timers.
-   */
-  pico_tree_foreach_safe(index, &IGMPTimers, _tmp) 
-  {
-    ((struct igmp_timer *)index->keyValue)->stopped = IGMP_TIMER_STOPPED;
-    pico_tree_delete(&IGMPTimers, index->keyValue);
-  }
-  igmp_dbg("IGMP: switch to compatibility mode IGMPv3\n");
-  link->mcast_compatibility = PICO_IGMPV3;
-  return;
+    /* When changing compatibility mode, cancel all pending response
+     * and retransmission timers.
+     */
+    pico_tree_foreach_safe(index, &IGMPTimers, _tmp)
+    {
+        ((struct igmp_timer *)index->keyValue)->stopped = IGMP_TIMER_STOPPED;
+        pico_tree_delete(&IGMPTimers, index->keyValue);
+    }
+    igmp_dbg("IGMP: switch to compatibility mode IGMPv3\n");
+    link->mcast_compatibility = PICO_IGMPV3;
+    return;
 }
 
 static int pico_igmp_is_checksum_valid(struct pico_frame *f)
 {
-  struct pico_ipv4_hdr *hdr = NULL;
-  uint8_t ihl = 24, datalen = 0;
+    struct pico_ipv4_hdr *hdr = NULL;
+    uint8_t ihl = 24, datalen = 0;
 
-  hdr = (struct pico_ipv4_hdr *)f->net_hdr;
-  ihl = (uint8_t)((hdr->vhl & 0x0F) * 4); /* IHL is in 32bit words */
-  datalen = (uint8_t)(short_be(hdr->len) - ihl);
+    hdr = (struct pico_ipv4_hdr *)f->net_hdr;
+    ihl = (uint8_t)((hdr->vhl & 0x0F) * 4); /* IHL is in 32bit words */
+    datalen = (uint8_t)(short_be(hdr->len) - ihl);
 
-  if (short_be(pico_checksum(f->transport_hdr, datalen)) == 0)
-    return 1;
-  igmp_dbg("IGMP: invalid checksum\n");
-  return 0;
+    if (short_be(pico_checksum(f->transport_hdr, datalen)) == 0)
+        return 1;
+
+    igmp_dbg("IGMP: invalid checksum\n");
+    return 0;
 }
 
 /* RFC 3376 $7.1 */
 static int pico_igmp_compatibility_mode(struct pico_frame *f)
 {
-  struct pico_ipv4_hdr *hdr = NULL;
-  struct pico_ipv4_link *link = NULL;
-  struct pico_tree_node *index = NULL, *_tmp = NULL;
-  struct igmp_timer t = {0};
-  uint8_t ihl = 24, datalen = 0;
+    struct pico_ipv4_hdr *hdr = NULL;
+    struct pico_ipv4_link *link = NULL;
+    struct pico_tree_node *index = NULL, *_tmp = NULL;
+    struct igmp_timer t = {
+        0
+    };
+    uint8_t ihl = 24, datalen = 0;
 
-  link = pico_ipv4_link_by_dev(f->dev);
-  if (!link)
-    return -1;
+    link = pico_ipv4_link_by_dev(f->dev);
+    if (!link)
+        return -1;
+
+    hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    ihl = (uint8_t)((hdr->vhl & 0x0F) * 4); /* IHL is in 32bit words */
+    datalen = (uint8_t)(short_be(hdr->len) - ihl);
+    igmp_dbg("IGMP: IHL = %u, LEN = %u, OCTETS = %u\n", ihl, short_be(hdr->len), datalen);
 
-  hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-  ihl = (uint8_t)((hdr->vhl & 0x0F) * 4); /* IHL is in 32bit words */
-  datalen = (uint8_t)(short_be(hdr->len) - ihl);
-  igmp_dbg("IGMP: IHL = %u, LEN = %u, OCTETS = %u\n", ihl, short_be(hdr->len), datalen);
-
-  if (datalen > 12) {
-    /* IGMPv3 query */
-    t.type = IGMP_TIMER_V2_QUERIER;
-    if (pico_igmp_timer_is_running(&t)) { /* IGMPv2 querier present timer still running */
-      return -1;
+    if (datalen > 12) {
+        /* IGMPv3 query */
+        t.type = IGMP_TIMER_V2_QUERIER;
+        if (pico_igmp_timer_is_running(&t)) { /* IGMPv2 querier present timer still running */
+            return -1;
+        } else {
+            link->mcast_compatibility = PICO_IGMPV3;
+            return 0;
+        }
+    } else if (datalen == 8) {
+        struct igmp_message *query = (struct igmp_message *)f->transport_hdr;
+        if (query->max_resp_time != 0) {
+            /* IGMPv2 query */
+            /* When changing compatibility mode, cancel all pending response
+             * and retransmission timers.
+             */
+            pico_tree_foreach_safe(index, &IGMPTimers, _tmp)
+            {
+                ((struct igmp_timer *)index->keyValue)->stopped = IGMP_TIMER_STOPPED;
+                pico_tree_delete(&IGMPTimers, index->keyValue);
+            }
+            igmp_dbg("IGMP: switch to compatibility mode IGMPv2\n");
+            link->mcast_compatibility = PICO_IGMPV2;
+            t.type = IGMP_TIMER_V2_QUERIER;
+            t.delay = ((IGMP_ROBUSTNESS * link->mcast_last_query_interval) + IGMP_QUERY_RESPONSE_INTERVAL) * 1000;
+            t.f = f;
+            t.callback = pico_igmp_v2querier_expired;
+            /* only one of this type of timer may exist! */
+            pico_igmp_timer_start(&t);
+        } else {
+            /* IGMPv1 query, not supported */
+            return -1;
+        }
     } else {
-      link->mcast_compatibility = PICO_IGMPV3;
-      return 0;
+        /* invalid query, silently ignored */
+        return -1;
     }
-  } else if (datalen == 8) {
-    struct igmp_message *query = (struct igmp_message *)f->transport_hdr;
-    if (query->max_resp_time != 0) {
-      /* IGMPv2 query */
-      /* When changing compatibility mode, cancel all pending response 
-       * and retransmission timers.
-       */
-      pico_tree_foreach_safe(index, &IGMPTimers, _tmp) 
-      {
-        ((struct igmp_timer *)index->keyValue)->stopped = IGMP_TIMER_STOPPED;
-        pico_tree_delete(&IGMPTimers, index->keyValue);
-      }
-      igmp_dbg("IGMP: switch to compatibility mode IGMPv2\n");
-      link->mcast_compatibility = PICO_IGMPV2;
-      t.type = IGMP_TIMER_V2_QUERIER;
-      t.delay = ((IGMP_ROBUSTNESS * link->mcast_last_query_interval) + IGMP_QUERY_RESPONSE_INTERVAL) * 1000;
-      t.f = f;
-      t.callback = pico_igmp_v2querier_expired;
-      /* only one of this type of timer may exist! */
-      pico_igmp_timer_start(&t);
-    } else {
-      /* IGMPv1 query, not supported */
-      return -1;
-    }
-  } else {
-    /* invalid query, silently ignored */
-    return -1;
-  }
-  return 0;
+
+    return 0;
 }
 
 static struct igmp_parameters *pico_igmp_analyse_packet(struct pico_frame *f)
 {
-  struct igmp_message *message = NULL;
-  struct igmp_parameters *p = NULL;
-  struct pico_ipv4_link *link = NULL;
-  struct pico_ip4 mcast_group = {0};
+    struct igmp_message *message = NULL;
+    struct igmp_parameters *p = NULL;
+    struct pico_ipv4_link *link = NULL;
+    struct pico_ip4 mcast_group = {
+        0
+    };
 
-  link = pico_ipv4_link_by_dev(f->dev);
-  if (!link)
-    return NULL;
+    link = pico_ipv4_link_by_dev(f->dev);
+    if (!link)
+        return NULL;
+
+    /* IGMPv2 and IGMPv3 have a similar structure for the first 8 bytes */
+    message = (struct igmp_message *)f->transport_hdr;
+    mcast_group.addr = message->mcast_group;
+    p = pico_igmp_find_parameter(&link->address, &mcast_group);
+    if (!p && mcast_group.addr == 0) { /* general query */
+        p = pico_zalloc(sizeof(struct igmp_parameters));
+        if (!p)
+            return NULL;
 
-  /* IGMPv2 and IGMPv3 have a similar structure for the first 8 bytes */ 
-  message = (struct igmp_message *)f->transport_hdr;
-  mcast_group.addr = message->mcast_group;
-  p = pico_igmp_find_parameter(&link->address, &mcast_group);
-  if (!p && mcast_group.addr == 0) { /* general query */
-    p = pico_zalloc(sizeof(struct igmp_parameters));
-    if (!p)
-      return NULL;
-    p->state = IGMP_STATE_NON_MEMBER;
-    p->mcast_link.addr = link->address.addr;
-    p->mcast_group.addr = mcast_group.addr;
-    pico_tree_insert(&IGMPParameters, p);
-  } else if (!p) {
-    return NULL;
-  }
+        p->state = IGMP_STATE_NON_MEMBER;
+        p->mcast_link.addr = link->address.addr;
+        p->mcast_group.addr = mcast_group.addr;
+        pico_tree_insert(&IGMPParameters, p);
+    } else if (!p) {
+        return NULL;
+    }
 
-  switch (message->type) {
+    switch (message->type) {
     case IGMP_TYPE_MEM_QUERY:
-       p->event = IGMP_EVENT_QUERY_RECV;
-       break;
+        p->event = IGMP_EVENT_QUERY_RECV;
+        break;
     case IGMP_TYPE_MEM_REPORT_V1:
-       p->event = IGMP_EVENT_REPORT_RECV;
-       break;
+        p->event = IGMP_EVENT_REPORT_RECV;
+        break;
     case IGMP_TYPE_MEM_REPORT_V2:
-       p->event = IGMP_EVENT_REPORT_RECV;
-       break;
+        p->event = IGMP_EVENT_REPORT_RECV;
+        break;
     case IGMP_TYPE_MEM_REPORT_V3:
-       p->event = IGMP_EVENT_REPORT_RECV;
-       break;
+        p->event = IGMP_EVENT_REPORT_RECV;
+        break;
     default:
-       return NULL;
-  }
-  p->max_resp_time = message->max_resp_time; /* if IGMPv3 report this will be 0 (res0 field) */
-  p->f = f;
+        return NULL;
+    }
+    p->max_resp_time = message->max_resp_time; /* if IGMPv3 report this will be 0 (res0 field) */
+    p->f = f;
 
-  return p;
+    return p;
 }
 
 static int pico_igmp_process_in(struct pico_protocol *self, struct pico_frame *f)
 {
-  struct igmp_parameters *p = NULL;
-  IGNORE_PARAMETER(self);
+    struct igmp_parameters *p = NULL;
+    IGNORE_PARAMETER(self);
+
+    if (!pico_igmp_is_checksum_valid(f))
+        goto out;
 
-  if (!pico_igmp_is_checksum_valid(f))
-    goto out;
-  if (pico_igmp_compatibility_mode(f) < 0)
-    goto out;
-  p = pico_igmp_analyse_packet(f);
-  if (!p)
-    goto out;
+    if (pico_igmp_compatibility_mode(f) < 0)
+        goto out;
 
-  return pico_igmp_process_event(p);
+    p = pico_igmp_analyse_packet(f);
+    if (!p)
+        goto out;
 
-  out:
+    return pico_igmp_process_event(p);
+
+out:
     pico_frame_discard(f);
     return 0;
 }
 
-static int pico_igmp_process_out(struct pico_protocol *self, struct pico_frame *f) {
-  /* packets are directly transferred to the IP layer by calling pico_ipv4_frame_push */
-	IGNORE_PARAMETER(self);
-	IGNORE_PARAMETER(f);
-	return 0;
+static int pico_igmp_process_out(struct pico_protocol *self, struct pico_frame *f)
+{
+    /* packets are directly transferred to the IP layer by calling pico_ipv4_frame_push */
+    IGNORE_PARAMETER(self);
+    IGNORE_PARAMETER(f);
+    return 0;
 }
 
 /* Interface: protocol definition */
 struct pico_protocol pico_proto_igmp = {
-  .name = "igmp",
-  .proto_number = PICO_PROTO_IGMP,
-  .layer = PICO_LAYER_TRANSPORT,
-  .process_in = pico_igmp_process_in,
-  .process_out = pico_igmp_process_out,
-  .q_in = &igmp_in,
-  .q_out = &igmp_out,
+    .name = "igmp",
+    .proto_number = PICO_PROTO_IGMP,
+    .layer = PICO_LAYER_TRANSPORT,
+    .process_in = pico_igmp_process_in,
+    .process_out = pico_igmp_process_out,
+    .q_in = &igmp_in,
+    .q_out = &igmp_out,
 };
 
-int pico_igmp_state_change(struct pico_ip4 *mcast_link, struct pico_ip4 *mcast_group, uint8_t filter_mode, struct pico_tree *MCASTFilter, uint8_t state) 
+int pico_igmp_state_change(struct pico_ip4 *mcast_link, struct pico_ip4 *mcast_group, uint8_t filter_mode, struct pico_tree *MCASTFilter, uint8_t state)
 {
-  struct igmp_parameters *p = NULL;
-  
-  if (mcast_group->addr == IGMP_ALL_HOST_GROUP)
-    return 0;
+    struct igmp_parameters *p = NULL;
+
+    if (mcast_group->addr == IGMP_ALL_HOST_GROUP)
+        return 0;
 
-  p = pico_igmp_find_parameter(mcast_link, mcast_group);
-  if (!p && state == PICO_IGMP_STATE_CREATE) {
-    p = pico_zalloc(sizeof(struct igmp_parameters));
-    if (!p) {
-      pico_err = PICO_ERR_ENOMEM;
-      return -1;
+    p = pico_igmp_find_parameter(mcast_link, mcast_group);
+    if (!p && state == PICO_IGMP_STATE_CREATE) {
+        p = pico_zalloc(sizeof(struct igmp_parameters));
+        if (!p) {
+            pico_err = PICO_ERR_ENOMEM;
+            return -1;
+        }
+
+        p->state = IGMP_STATE_NON_MEMBER;
+        p->mcast_link = *mcast_link;
+        p->mcast_group = *mcast_group;
+        pico_tree_insert(&IGMPParameters, p);
+    } else if (!p) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
     }
-    p->state = IGMP_STATE_NON_MEMBER;
-    p->mcast_link = *mcast_link;
-    p->mcast_group = *mcast_group;
-    pico_tree_insert(&IGMPParameters, p);
-  } else if (!p) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
 
-  switch (state) {
+    switch (state) {
     case PICO_IGMP_STATE_CREATE:
-      p->event = IGMP_EVENT_CREATE_GROUP;
-      break;
+        p->event = IGMP_EVENT_CREATE_GROUP;
+        break;
 
     case PICO_IGMP_STATE_UPDATE:
-      p->event = IGMP_EVENT_UPDATE_GROUP;
-      break;
-    
+        p->event = IGMP_EVENT_UPDATE_GROUP;
+        break;
+
     case PICO_IGMP_STATE_DELETE:
-      p->event = IGMP_EVENT_DELETE_GROUP;
-      break;
+        p->event = IGMP_EVENT_DELETE_GROUP;
+        break;
 
     default:
-      return -1;
-  }
-  p->filter_mode = filter_mode;
-  p->MCASTFilter = MCASTFilter;
+        return -1;
+    }
+    p->filter_mode = filter_mode;
+    p->MCASTFilter = MCASTFilter;
 
-  return pico_igmp_process_event(p);
+    return pico_igmp_process_event(p);
 }
 
 static int pico_igmp_send_report(struct igmp_parameters *p, struct pico_frame *f)
 {
-  struct pico_ip4 dst = {0};
-  struct pico_ip4 mcast_group = {0};
-  struct pico_ipv4_link *link = NULL;
-  
-  link = pico_ipv4_link_get(&p->mcast_link);
-  if (!link)
-    return -1;
+    struct pico_ip4 dst = {
+        0
+    };
+    struct pico_ip4 mcast_group = {
+        0
+    };
+    struct pico_ipv4_link *link = NULL;
 
-  mcast_group.addr = p->mcast_group.addr;
-  switch (link->mcast_compatibility) {
+    link = pico_ipv4_link_get(&p->mcast_link);
+    if (!link)
+        return -1;
+
+    mcast_group.addr = p->mcast_group.addr;
+    switch (link->mcast_compatibility) {
     case PICO_IGMPV2:
-      if (p->event == IGMP_EVENT_DELETE_GROUP)
-        dst.addr = IGMP_ALL_ROUTER_GROUP;
-      else
-        dst.addr = mcast_group.addr;
-      break;
+        if (p->event == IGMP_EVENT_DELETE_GROUP)
+            dst.addr = IGMP_ALL_ROUTER_GROUP;
+        else
+            dst.addr = mcast_group.addr;
+
+        break;
 
     case PICO_IGMPV3:
-      dst.addr = IGMPV3_ALL_ROUTER_GROUP;
-      break;
+        dst.addr = IGMPV3_ALL_ROUTER_GROUP;
+        break;
 
     default:
-      pico_err = PICO_ERR_EPROTONOSUPPORT;
-      return -1;
-  }
+        pico_err = PICO_ERR_EPROTONOSUPPORT;
+        return -1;
+    }
 
-  igmp_dbg("IGMP: send membership report on group %08X to %08X\n", mcast_group.addr, dst.addr);
-  pico_ipv4_frame_push(f, &dst, PICO_PROTO_IGMP);
-  return 0;
+    igmp_dbg("IGMP: send membership report on group %08X to %08X\n", mcast_group.addr, dst.addr);
+    pico_ipv4_frame_push(f, &dst, PICO_PROTO_IGMP);
+    return 0;
 }
 
 static int8_t pico_igmp_generate_report(struct igmp_parameters *p)
 {
-  struct pico_ipv4_link *link = NULL;
-  int i = 0;
+    struct pico_ipv4_link *link = NULL;
+    int i = 0;
 
-  link = pico_ipv4_link_get(&p->mcast_link);
-  if (!link) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    link = pico_ipv4_link_get(&p->mcast_link);
+    if (!link) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  switch (link->mcast_compatibility) {
+    switch (link->mcast_compatibility) {
     case PICO_IGMPV1:
-      pico_err = PICO_ERR_EPROTONOSUPPORT;
-      return -1;
-      
+        pico_err = PICO_ERR_EPROTONOSUPPORT;
+        return -1;
+
     case PICO_IGMPV2:
     {
-      struct igmp_message *report = NULL;
-      uint8_t report_type = IGMP_TYPE_MEM_REPORT_V2;
-      if (p->event == IGMP_EVENT_DELETE_GROUP)
-        report_type = IGMP_TYPE_LEAVE_GROUP;
+        struct igmp_message *report = NULL;
+        uint8_t report_type = IGMP_TYPE_MEM_REPORT_V2;
+        if (p->event == IGMP_EVENT_DELETE_GROUP)
+            report_type = IGMP_TYPE_LEAVE_GROUP;
 
-      p->f = pico_proto_ipv4.alloc(&pico_proto_ipv4, IP_OPTION_ROUTER_ALERT_LEN + sizeof(struct igmp_message));
-      p->f->net_len = (uint16_t)(p->f->net_len + IP_OPTION_ROUTER_ALERT_LEN);
-      p->f->transport_hdr += IP_OPTION_ROUTER_ALERT_LEN;
-      p->f->transport_len = (uint16_t)(p->f->transport_len - IP_OPTION_ROUTER_ALERT_LEN);
-      p->f->dev = pico_ipv4_link_find(&p->mcast_link);
-      /* p->f->len is correctly set by alloc */
+        p->f = pico_proto_ipv4.alloc(&pico_proto_ipv4, IP_OPTION_ROUTER_ALERT_LEN + sizeof(struct igmp_message));
+        p->f->net_len = (uint16_t)(p->f->net_len + IP_OPTION_ROUTER_ALERT_LEN);
+        p->f->transport_hdr += IP_OPTION_ROUTER_ALERT_LEN;
+        p->f->transport_len = (uint16_t)(p->f->transport_len - IP_OPTION_ROUTER_ALERT_LEN);
+        p->f->dev = pico_ipv4_link_find(&p->mcast_link);
+        /* p->f->len is correctly set by alloc */
 
-      report = (struct igmp_message *)p->f->transport_hdr;
-      report->type = report_type;
-      report->max_resp_time = IGMP_DEFAULT_MAX_RESPONSE_TIME;
-      report->mcast_group = p->mcast_group.addr;
+        report = (struct igmp_message *)p->f->transport_hdr;
+        report->type = report_type;
+        report->max_resp_time = IGMP_DEFAULT_MAX_RESPONSE_TIME;
+        report->mcast_group = p->mcast_group.addr;
 
-      report->crc = 0;
-      report->crc = short_be(pico_checksum(report, sizeof(struct igmp_message)));
-      break;
+        report->crc = 0;
+        report->crc = short_be(pico_checksum(report, sizeof(struct igmp_message)));
+        break;
     }
     case PICO_IGMPV3:
     {
-      struct igmpv3_report *report = NULL;
-      struct igmpv3_group_record *record = NULL;
-      struct pico_mcast_group *g = NULL, test = {0};
-      struct pico_tree_node *index = NULL, *_tmp = NULL;
-      struct pico_tree *IGMPFilter = NULL;
-      struct pico_ip4 *source = NULL;
-      uint8_t record_type = 0;
-      uint8_t sources = 0;
-      uint16_t len = 0;
-
-      test.mcast_addr = p->mcast_group;
-      g = pico_tree_findKey(link->MCASTGroups, &test);
-      if (!g) {
-        pico_err = PICO_ERR_EINVAL;
-        return -1;
-      }
+        struct igmpv3_report *report = NULL;
+        struct igmpv3_group_record *record = NULL;
+        struct pico_mcast_group *g = NULL, test = {
+            0
+        };
+        struct pico_tree_node *index = NULL, *_tmp = NULL;
+        struct pico_tree *IGMPFilter = NULL;
+        struct pico_ip4 *source = NULL;
+        uint8_t record_type = 0;
+        uint8_t sources = 0;
+        uint16_t len = 0;
 
-      if (p->event == IGMP_EVENT_DELETE_GROUP) { /* "non-existent" state of filter mode INCLUDE and empty source list */
-        p->filter_mode = PICO_IP_MULTICAST_INCLUDE;
-        p->MCASTFilter = NULL;
-      }
+        test.mcast_addr = p->mcast_group;
+        g = pico_tree_findKey(link->MCASTGroups, &test);
+        if (!g) {
+            pico_err = PICO_ERR_EINVAL;
+            return -1;
+        }
+
+        if (p->event == IGMP_EVENT_DELETE_GROUP) { /* "non-existent" state of filter mode INCLUDE and empty source list */
+            p->filter_mode = PICO_IP_MULTICAST_INCLUDE;
+            p->MCASTFilter = NULL;
+        }
 
-      /* cleanup filters */
-      pico_tree_foreach_safe(index, &IGMPAllow, _tmp) 
-      {
-        pico_tree_delete(&IGMPAllow, index->keyValue);
-      }
-      pico_tree_foreach_safe(index, &IGMPBlock, _tmp) 
-      {
-        pico_tree_delete(&IGMPBlock, index->keyValue);
-      }
+        /* cleanup filters */
+        pico_tree_foreach_safe(index, &IGMPAllow, _tmp)
+        {
+            pico_tree_delete(&IGMPAllow, index->keyValue);
+        }
+        pico_tree_foreach_safe(index, &IGMPBlock, _tmp)
+        {
+            pico_tree_delete(&IGMPBlock, index->keyValue);
+        }
 
-      switch (g->filter_mode) {
+        switch (g->filter_mode) {
 
         case PICO_IP_MULTICAST_INCLUDE:
-          switch (p->filter_mode) {
+            switch (p->filter_mode) {
             case PICO_IP_MULTICAST_INCLUDE:
-              if (p->event == IGMP_EVENT_DELETE_GROUP) { /* all ADD_SOURCE_MEMBERSHIP had an equivalent DROP_SOURCE_MEMBERSHIP */
+                if (p->event == IGMP_EVENT_DELETE_GROUP) { /* all ADD_SOURCE_MEMBERSHIP had an equivalent DROP_SOURCE_MEMBERSHIP */
+                    /* TO_IN (B) */
+                    record_type = IGMP_CHANGE_TO_INCLUDE_MODE;
+                    IGMPFilter = &IGMPAllow;
+                    if (p->MCASTFilter) {
+                        pico_tree_foreach(index, p->MCASTFilter) /* B */
+                        {
+                            pico_tree_insert(&IGMPAllow, index->keyValue);
+                            sources++;
+                        }
+                    } /* else { IGMPAllow stays empty } */
+
+                    break;
+                }
+
+                /* ALLOW (B-A) */
+                /* if event is CREATE A will be empty, thus only ALLOW (B-A) has sense */
+                if (p->event == IGMP_EVENT_CREATE_GROUP) /* first ADD_SOURCE_MEMBERSHIP */
+                    record_type = IGMP_CHANGE_TO_INCLUDE_MODE;
+                else
+                    record_type = IGMP_ALLOW_NEW_SOURCES;
+
+                IGMPFilter = &IGMPAllow;
+                pico_tree_foreach(index, p->MCASTFilter) /* B */
+                {
+                    pico_tree_insert(&IGMPAllow, index->keyValue);
+                    sources++;
+                }
+                pico_tree_foreach(index, &g->MCASTSources) /* A */
+                {
+                    source = pico_tree_findKey(&IGMPAllow, index->keyValue);
+                    if (source) {
+                        pico_tree_delete(&IGMPAllow, source);
+                        sources--;
+                    }
+                }
+                if (!pico_tree_empty(&IGMPAllow)) /* record type is ALLOW */
+                    break;
+
+                /* BLOCK (A-B) */
+                record_type = IGMP_BLOCK_OLD_SOURCES;
+                IGMPFilter = &IGMPBlock;
+                pico_tree_foreach(index, &g->MCASTSources) /* A */
+                {
+                    pico_tree_insert(&IGMPBlock, index->keyValue);
+                    sources++;
+                }
+                pico_tree_foreach(index, p->MCASTFilter) /* B */
+                {
+                    source = pico_tree_findKey(&IGMPBlock, index->keyValue);
+                    if (source) {
+                        pico_tree_delete(&IGMPBlock, source);
+                        sources--;
+                    }
+                }
+                if (!pico_tree_empty(&IGMPBlock)) /* record type is BLOCK */
+                    break;
+
+                /* ALLOW (B-A) and BLOCK (A-B) are empty: do not send report (RFC 3376 $5.1) */
+                p->f = NULL;
+                return 0;
+
+            case PICO_IP_MULTICAST_EXCLUDE:
+                /* TO_EX (B) */
+                record_type = IGMP_CHANGE_TO_EXCLUDE_MODE;
+                IGMPFilter = &IGMPBlock;
+                pico_tree_foreach(index, p->MCASTFilter) /* B */
+                {
+                    pico_tree_insert(&IGMPBlock, index->keyValue);
+                    sources++;
+                }
+                break;
+
+            default:
+                pico_err = PICO_ERR_EINVAL;
+                return -1;
+            }
+            break;
+
+        case PICO_IP_MULTICAST_EXCLUDE:
+            switch (p->filter_mode) {
+            case PICO_IP_MULTICAST_INCLUDE:
                 /* TO_IN (B) */
                 record_type = IGMP_CHANGE_TO_INCLUDE_MODE;
                 IGMPFilter = &IGMPAllow;
                 if (p->MCASTFilter) {
-                  pico_tree_foreach(index, p->MCASTFilter) /* B */
-                  {
-                    pico_tree_insert(&IGMPAllow, index->keyValue);
-                    sources++;
-                  }
+                    pico_tree_foreach(index, p->MCASTFilter) /* B */
+                    {
+                        pico_tree_insert(&IGMPAllow, index->keyValue);
+                        sources++;
+                    }
                 } /* else { IGMPAllow stays empty } */
-                break;
-              }
 
-              /* ALLOW (B-A) */
-              /* if event is CREATE A will be empty, thus only ALLOW (B-A) has sense */
-              if (p->event == IGMP_EVENT_CREATE_GROUP) /* first ADD_SOURCE_MEMBERSHIP */
-                record_type = IGMP_CHANGE_TO_INCLUDE_MODE;
-              else
-                record_type = IGMP_ALLOW_NEW_SOURCES;
-              IGMPFilter = &IGMPAllow;
-              pico_tree_foreach(index, p->MCASTFilter) /* B */
-              {
-                pico_tree_insert(&IGMPAllow, index->keyValue);
-                sources++;
-              }
-              pico_tree_foreach(index, &g->MCASTSources) /* A */
-              {
-                source = pico_tree_findKey(&IGMPAllow, index->keyValue);
-                if (source) {
-                  pico_tree_delete(&IGMPAllow, source);
-                  sources--;
-                }
-              }
-              if (!pico_tree_empty(&IGMPAllow)) /* record type is ALLOW */
                 break;
 
-              /* BLOCK (A-B) */
-              record_type = IGMP_BLOCK_OLD_SOURCES;
-              IGMPFilter = &IGMPBlock;
-              pico_tree_foreach(index, &g->MCASTSources) /* A */
-              {
-                pico_tree_insert(&IGMPBlock, index->keyValue);
-                sources++;
-              }
-              pico_tree_foreach(index, p->MCASTFilter) /* B */
-              {
-                source = pico_tree_findKey(&IGMPBlock, index->keyValue);
-                if (source) {
-                  pico_tree_delete(&IGMPBlock, source);
-                  sources--;
+            case PICO_IP_MULTICAST_EXCLUDE:
+                /* BLOCK (B-A) */
+                record_type = IGMP_BLOCK_OLD_SOURCES;
+                IGMPFilter = &IGMPBlock;
+                pico_tree_foreach(index, p->MCASTFilter)
+                {
+                    pico_tree_insert(&IGMPBlock, index->keyValue);
+                    sources++;
                 }
-              }
-              if (!pico_tree_empty(&IGMPBlock)) /* record type is BLOCK */
-                break;
-
-              /* ALLOW (B-A) and BLOCK (A-B) are empty: do not send report (RFC 3376 $5.1) */
-              p->f = NULL;
-              return 0;
+                pico_tree_foreach(index, &g->MCASTSources) /* A */
+                {
+                    source = pico_tree_findKey(&IGMPBlock, index->keyValue); /* B */
+                    if (source) {
+                        pico_tree_delete(&IGMPBlock, source);
+                        sources--;
+                    }
+                }
+                if (!pico_tree_empty(&IGMPBlock)) /* record type is BLOCK */
+                    break;
 
-            case PICO_IP_MULTICAST_EXCLUDE:
-              /* TO_EX (B) */
-              record_type = IGMP_CHANGE_TO_EXCLUDE_MODE;
-              IGMPFilter = &IGMPBlock;
-              pico_tree_foreach(index, p->MCASTFilter) /* B */
-              {
-                pico_tree_insert(&IGMPBlock, index->keyValue);
-                sources++;
-              }
-              break;
-
-            default:
-              pico_err = PICO_ERR_EINVAL;
-              return -1;
-          }
-          break;
-
-        case PICO_IP_MULTICAST_EXCLUDE:
-          switch (p->filter_mode) {
-            case PICO_IP_MULTICAST_INCLUDE:
-              /* TO_IN (B) */
-              record_type = IGMP_CHANGE_TO_INCLUDE_MODE;
-              IGMPFilter = &IGMPAllow;
-              if (p->MCASTFilter) {
+                /* ALLOW (A-B) */
+                record_type = IGMP_ALLOW_NEW_SOURCES;
+                IGMPFilter = &IGMPAllow;
+                pico_tree_foreach(index, &g->MCASTSources)
+                {
+                    pico_tree_insert(&IGMPAllow, index->keyValue);
+                    sources++;
+                }
                 pico_tree_foreach(index, p->MCASTFilter) /* B */
                 {
-                  pico_tree_insert(&IGMPAllow, index->keyValue);
-                  sources++;
-                }
-              } /* else { IGMPAllow stays empty } */
-              break;
-
-            case PICO_IP_MULTICAST_EXCLUDE:
-              /* BLOCK (B-A) */
-              record_type = IGMP_BLOCK_OLD_SOURCES;
-              IGMPFilter = &IGMPBlock;
-              pico_tree_foreach(index, p->MCASTFilter)
-              {
-                pico_tree_insert(&IGMPBlock, index->keyValue);
-                sources++;
-              }
-              pico_tree_foreach(index, &g->MCASTSources) /* A */
-              {
-                source = pico_tree_findKey(&IGMPBlock, index->keyValue); /* B */
-                if (source) {
-                  pico_tree_delete(&IGMPBlock, source);
-                  sources--;
+                    source = pico_tree_findKey(&IGMPAllow, index->keyValue); /* A */
+                    if (source) {
+                        pico_tree_delete(&IGMPAllow, source);
+                        sources--;
+                    }
                 }
-              }
-              if (!pico_tree_empty(&IGMPBlock)) /* record type is BLOCK */
-                break;
+                if (!pico_tree_empty(&IGMPAllow)) /* record type is ALLOW */
+                    break;
 
-              /* ALLOW (A-B) */
-              record_type = IGMP_ALLOW_NEW_SOURCES;
-              IGMPFilter = &IGMPAllow;
-              pico_tree_foreach(index, &g->MCASTSources)
-              {
-                pico_tree_insert(&IGMPAllow, index->keyValue);
-                sources++;
-              }
-              pico_tree_foreach(index, p->MCASTFilter) /* B */
-              {
-                source = pico_tree_findKey(&IGMPAllow, index->keyValue); /* A */
-                if (source) {
-                  pico_tree_delete(&IGMPAllow, source);
-                  sources--;
-                }
-              }
-              if (!pico_tree_empty(&IGMPAllow)) /* record type is ALLOW */
-                break;
-
-              /* BLOCK (B-A) and ALLOW (A-B) are empty: do not send report (RFC 3376 $5.1) */
-              p->f = NULL;
-              return 0;
+                /* BLOCK (B-A) and ALLOW (A-B) are empty: do not send report (RFC 3376 $5.1) */
+                p->f = NULL;
+                return 0;
 
             default:
-              pico_err = PICO_ERR_EINVAL;
-              return -1;
-          }
-          break;
+                pico_err = PICO_ERR_EINVAL;
+                return -1;
+            }
+            break;
 
         default:
-          pico_err = PICO_ERR_EINVAL;
-          return -1;
-      }
+            pico_err = PICO_ERR_EINVAL;
+            return -1;
+        }
 
-      len = (uint16_t)(sizeof(struct igmpv3_report) + sizeof(struct igmpv3_group_record) + (sources * sizeof(struct pico_ip4)));
-      p->f = pico_proto_ipv4.alloc(&pico_proto_ipv4, (uint16_t)(IP_OPTION_ROUTER_ALERT_LEN + len));
-      p->f->net_len = (uint16_t)(p->f->net_len + IP_OPTION_ROUTER_ALERT_LEN);
-      p->f->transport_hdr += IP_OPTION_ROUTER_ALERT_LEN;
-      p->f->transport_len = (uint16_t)(p->f->transport_len - IP_OPTION_ROUTER_ALERT_LEN);
-      p->f->dev = pico_ipv4_link_find(&p->mcast_link);
-      /* p->f->len is correctly set by alloc */
+        len = (uint16_t)(sizeof(struct igmpv3_report) + sizeof(struct igmpv3_group_record) + (sources * sizeof(struct pico_ip4)));
+        p->f = pico_proto_ipv4.alloc(&pico_proto_ipv4, (uint16_t)(IP_OPTION_ROUTER_ALERT_LEN + len));
+        p->f->net_len = (uint16_t)(p->f->net_len + IP_OPTION_ROUTER_ALERT_LEN);
+        p->f->transport_hdr += IP_OPTION_ROUTER_ALERT_LEN;
+        p->f->transport_len = (uint16_t)(p->f->transport_len - IP_OPTION_ROUTER_ALERT_LEN);
+        p->f->dev = pico_ipv4_link_find(&p->mcast_link);
+        /* p->f->len is correctly set by alloc */
 
-      report = (struct igmpv3_report *)p->f->transport_hdr;
-      report->type = IGMP_TYPE_MEM_REPORT_V3;
-      report->res0 = 0;
-      report->crc = 0;
-      report->res1 = 0;
-      report->groups = short_be(1);
+        report = (struct igmpv3_report *)p->f->transport_hdr;
+        report->type = IGMP_TYPE_MEM_REPORT_V3;
+        report->res0 = 0;
+        report->crc = 0;
+        report->res1 = 0;
+        report->groups = short_be(1);
 
-      record = &report->record[0];
-      record->type = record_type;
-      record->aux = 0;
-      record->sources = short_be(sources);
-      record->mcast_group = p->mcast_group.addr;
-      if (!pico_tree_empty(IGMPFilter)) {
-        i = 0;
-        pico_tree_foreach(index, IGMPFilter)
-        {
-          record->source_addr[i] = ((struct pico_ip4 *)index->keyValue)->addr;
-          i++;
+        record = &report->record[0];
+        record->type = record_type;
+        record->aux = 0;
+        record->sources = short_be(sources);
+        record->mcast_group = p->mcast_group.addr;
+        if (!pico_tree_empty(IGMPFilter)) {
+            i = 0;
+            pico_tree_foreach(index, IGMPFilter)
+            {
+                record->source_addr[i] = ((struct pico_ip4 *)index->keyValue)->addr;
+                i++;
+            }
         }
-      }
-      report->crc = short_be(pico_checksum(report, len));
-      break;
+
+        report->crc = short_be(pico_checksum(report, len));
+        break;
     }
 
     default:
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
-  }
-  return 0;
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+    return 0;
 }
 
 /* stop timer, send leave if flag set */
 static int stslifs(struct igmp_parameters *p)
 {
-  struct igmp_timer t = {0};
+    struct igmp_timer t = {
+        0
+    };
 
-  igmp_dbg("IGMP: event = leave group | action = stop timer, send leave if flag set\n");
+    igmp_dbg("IGMP: event = leave group | action = stop timer, send leave if flag set\n");
 
-  t.type = IGMP_TIMER_GROUP_REPORT;
-  t.mcast_link = p->mcast_link;
-  t.mcast_group = p->mcast_group;
-  if (pico_igmp_timer_stop(&t) < 0)
-    return -1;
+    t.type = IGMP_TIMER_GROUP_REPORT;
+    t.mcast_link = p->mcast_link;
+    t.mcast_group = p->mcast_group;
+    if (pico_igmp_timer_stop(&t) < 0)
+        return -1;
 
-  /* always send leave, even if not last host */
-  if (pico_igmp_send_report(p, p->f) < 0)
-    return -1;
+    /* always send leave, even if not last host */
+    if (pico_igmp_send_report(p, p->f) < 0)
+        return -1;
 
-  pico_igmp_delete_parameter(p);
-  igmp_dbg("IGMP: new state = non-member\n");
-  return 0;
+    pico_igmp_delete_parameter(p);
+    igmp_dbg("IGMP: new state = non-member\n");
+    return 0;
 }
 
 /* send report, set flag, start timer */
 static int srsfst(struct igmp_parameters *p)
 {
-  struct igmp_timer t = {0};
-  struct pico_frame *copy_frame = NULL;
+    struct igmp_timer t = {
+        0
+    };
+    struct pico_frame *copy_frame = NULL;
+
+    igmp_dbg("IGMP: event = join group | action = send report, set flag, start timer\n");
 
-  igmp_dbg("IGMP: event = join group | action = send report, set flag, start timer\n");
+    p->last_host = IGMP_HOST_LAST;
+
+    if (pico_igmp_generate_report(p) < 0)
+        return -1;
+
+    if (!p->f)
+        return 0;
 
-  p->last_host = IGMP_HOST_LAST;
+    copy_frame = pico_frame_copy(p->f);
+    if (!copy_frame) {
+        pico_err = PICO_ERR_ENOMEM;
+        return -1;
+    }
+
+    if (pico_igmp_send_report(p, copy_frame) < 0)
+        return -1;
 
-  if (pico_igmp_generate_report(p) < 0)
-    return -1;
-  if (!p->f)
+    t.type = IGMP_TIMER_GROUP_REPORT;
+    t.mcast_link = p->mcast_link;
+    t.mcast_group = p->mcast_group;
+    t.delay = (pico_rand() % (IGMP_UNSOLICITED_REPORT_INTERVAL * 10000));
+    t.f = p->f;
+    t.callback = pico_igmp_report_expired;
+    pico_igmp_timer_start(&t);
+
+    p->state = IGMP_STATE_DELAYING_MEMBER;
+    igmp_dbg("IGMP: new state = delaying member\n");
     return 0;
-  copy_frame = pico_frame_copy(p->f);
-  if (!copy_frame) {
-    pico_err = PICO_ERR_ENOMEM;
-    return -1;
-  }
-  if (pico_igmp_send_report(p, copy_frame) < 0)
-    return -1;
-
-  t.type = IGMP_TIMER_GROUP_REPORT;
-  t.mcast_link = p->mcast_link;
-  t.mcast_group = p->mcast_group;
-  t.delay = (pico_rand() % (IGMP_UNSOLICITED_REPORT_INTERVAL * 10000)); 
-  t.f = p->f;
-  t.callback = pico_igmp_report_expired;
-  pico_igmp_timer_start(&t);
-
-  p->state = IGMP_STATE_DELAYING_MEMBER;
-  igmp_dbg("IGMP: new state = delaying member\n");
-  return 0;
 }
 
 /* merge report, send report, reset timer (IGMPv3 only) */
 static int mrsrrt(struct igmp_parameters *p)
 {
-  struct igmp_timer *t = NULL;
-  struct pico_frame *copy_frame = NULL;
-  struct pico_ipv4_link *link = NULL;
+    struct igmp_timer *t = NULL;
+    struct pico_frame *copy_frame = NULL;
+    struct pico_ipv4_link *link = NULL;
 
-  igmp_dbg("IGMP: event = update group | action = merge report, send report, reset timer (IGMPv3 only)\n");
+    igmp_dbg("IGMP: event = update group | action = merge report, send report, reset timer (IGMPv3 only)\n");
 
-  link = pico_ipv4_link_get(&p->mcast_link);
-  if (!link)
-    return -1;
+    link = pico_ipv4_link_get(&p->mcast_link);
+    if (!link)
+        return -1;
 
-  if (link->mcast_compatibility != PICO_IGMPV3) {
-    igmp_dbg("IGMP: no IGMPv3 compatible router on network\n");
-    return -1;
-  }
+    if (link->mcast_compatibility != PICO_IGMPV3) {
+        igmp_dbg("IGMP: no IGMPv3 compatible router on network\n");
+        return -1;
+    }
+
+    /* XXX: merge with pending report rfc 3376 $5.1 */
 
-  /* XXX: merge with pending report rfc 3376 $5.1 */
+    copy_frame = pico_frame_copy(p->f);
+    if (!copy_frame)
+        return -1;
 
-  copy_frame = pico_frame_copy(p->f);
-  if (!copy_frame)
-    return -1;
-  if (pico_igmp_send_report(p, copy_frame) < 0)
-    return -1;
+    if (pico_igmp_send_report(p, copy_frame) < 0)
+        return -1;
 
-  t = pico_igmp_find_timer(IGMP_TIMER_GROUP_REPORT, &p->mcast_link, &p->mcast_group);
-  if (!t)
-    return -1;
-  t->delay = (pico_rand() % (IGMP_UNSOLICITED_REPORT_INTERVAL * 10000)); 
-  pico_igmp_timer_reset(t);
+    t = pico_igmp_find_timer(IGMP_TIMER_GROUP_REPORT, &p->mcast_link, &p->mcast_group);
+    if (!t)
+        return -1;
 
-  p->state = IGMP_STATE_DELAYING_MEMBER;
-  igmp_dbg("IGMP: new state = delaying member\n");
-  return 0;
+    t->delay = (pico_rand() % (IGMP_UNSOLICITED_REPORT_INTERVAL * 10000));
+    pico_igmp_timer_reset(t);
+
+    p->state = IGMP_STATE_DELAYING_MEMBER;
+    igmp_dbg("IGMP: new state = delaying member\n");
+    return 0;
 }
 
 /* send report, start timer (IGMPv3 only) */
 static int srst(struct igmp_parameters *p)
 {
-  struct igmp_timer t = {0};
-  struct pico_frame *copy_frame = NULL;
-  struct pico_ipv4_link *link = NULL;
+    struct igmp_timer t = {
+        0
+    };
+    struct pico_frame *copy_frame = NULL;
+    struct pico_ipv4_link *link = NULL;
 
-  igmp_dbg("IGMP: event = update group | action = send report, start timer (IGMPv3 only)\n");
+    igmp_dbg("IGMP: event = update group | action = send report, start timer (IGMPv3 only)\n");
 
-  link = pico_ipv4_link_get(&p->mcast_link);
-  if (!link)
-    return -1;
+    link = pico_ipv4_link_get(&p->mcast_link);
+    if (!link)
+        return -1;
 
-  if (link->mcast_compatibility != PICO_IGMPV3) {
-    igmp_dbg("IGMP: no IGMPv3 compatible router on network\n");
-    return -1;
-  }
+    if (link->mcast_compatibility != PICO_IGMPV3) {
+        igmp_dbg("IGMP: no IGMPv3 compatible router on network\n");
+        return -1;
+    }
+
+    if (pico_igmp_generate_report(p) < 0)
+        return -1;
 
-  if (pico_igmp_generate_report(p) < 0)
-    return -1;
-  if (!p->f)
-    return 0;
-  copy_frame = pico_frame_copy(p->f);
-  if (!copy_frame)
-    return -1;
-  if (pico_igmp_send_report(p, copy_frame) < 0)
-    return -1;
+    if (!p->f)
+        return 0;
+
+    copy_frame = pico_frame_copy(p->f);
+    if (!copy_frame)
+        return -1;
+
+    if (pico_igmp_send_report(p, copy_frame) < 0)
+        return -1;
 
-  t.type = IGMP_TIMER_GROUP_REPORT;
-  t.mcast_link = p->mcast_link;
-  t.mcast_group = p->mcast_group;
-  t.delay = (pico_rand() % (IGMP_UNSOLICITED_REPORT_INTERVAL * 10000)); 
-  t.f = p->f;
-  t.callback = pico_igmp_report_expired;
-  pico_igmp_timer_start(&t);
+    t.type = IGMP_TIMER_GROUP_REPORT;
+    t.mcast_link = p->mcast_link;
+    t.mcast_group = p->mcast_group;
+    t.delay = (pico_rand() % (IGMP_UNSOLICITED_REPORT_INTERVAL * 10000));
+    t.f = p->f;
+    t.callback = pico_igmp_report_expired;
+    pico_igmp_timer_start(&t);
 
-  p->state = IGMP_STATE_DELAYING_MEMBER;
-  igmp_dbg("IGMP: new state = delaying member\n");
-  return 0;
+    p->state = IGMP_STATE_DELAYING_MEMBER;
+    igmp_dbg("IGMP: new state = delaying member\n");
+    return 0;
 }
 
 /* send leave if flag set */
 static int slifs(struct igmp_parameters *p)
 {
-  igmp_dbg("IGMP: event = leave group | action = send leave if flag set\n");
+    igmp_dbg("IGMP: event = leave group | action = send leave if flag set\n");
 
-  /* always send leave, even if not last host */
-  if (pico_igmp_send_report(p, p->f) < 0)
-    return -1;
+    /* always send leave, even if not last host */
+    if (pico_igmp_send_report(p, p->f) < 0)
+        return -1;
 
-  pico_igmp_delete_parameter(p);
-  igmp_dbg("IGMP: new state = non-member\n");
-  return 0;
+    pico_igmp_delete_parameter(p);
+    igmp_dbg("IGMP: new state = non-member\n");
+    return 0;
 }
 
 /* start timer */
 static int st(struct igmp_parameters *p)
 {
-  struct igmp_timer t = {0};
+    struct igmp_timer t = {
+        0
+    };
 
-  igmp_dbg("IGMP: event = query received | action = start timer\n");
+    igmp_dbg("IGMP: event = query received | action = start timer\n");
 
-  if (pico_igmp_generate_report(p) < 0)
-    return -1;
-  if (!p->f)
-    return -1;
+    if (pico_igmp_generate_report(p) < 0)
+        return -1;
+
+    if (!p->f)
+        return -1;
 
-  t.type = IGMP_TIMER_GROUP_REPORT;
-  t.mcast_link = p->mcast_link;
-  t.mcast_group = p->mcast_group;
-  t.delay = (pico_rand() % (p->max_resp_time * 100u));
-  t.f = p->f;
-  t.callback = pico_igmp_report_expired;
-  pico_igmp_timer_start(&t);
+    t.type = IGMP_TIMER_GROUP_REPORT;
+    t.mcast_link = p->mcast_link;
+    t.mcast_group = p->mcast_group;
+    t.delay = (pico_rand() % (p->max_resp_time * 100u));
+    t.f = p->f;
+    t.callback = pico_igmp_report_expired;
+    pico_igmp_timer_start(&t);
 
-  p->state = IGMP_STATE_DELAYING_MEMBER;
-  igmp_dbg("IGMP: new state = delaying member\n");
-  return 0;
+    p->state = IGMP_STATE_DELAYING_MEMBER;
+    igmp_dbg("IGMP: new state = delaying member\n");
+    return 0;
 }
 
 /* stop timer, clear flag */
 static int stcl(struct igmp_parameters *p)
 {
-  struct igmp_timer t = {0};
+    struct igmp_timer t = {
+        0
+    };
 
-  igmp_dbg("IGMP: event = report received | action = stop timer, clear flag\n");
+    igmp_dbg("IGMP: event = report received | action = stop timer, clear flag\n");
 
-  t.type = IGMP_TIMER_GROUP_REPORT;
-  t.mcast_link = p->mcast_link;
-  t.mcast_group = p->mcast_group;
-  if (pico_igmp_timer_stop(&t) < 0)
-    return -1;
+    t.type = IGMP_TIMER_GROUP_REPORT;
+    t.mcast_link = p->mcast_link;
+    t.mcast_group = p->mcast_group;
+    if (pico_igmp_timer_stop(&t) < 0)
+        return -1;
 
-  p->last_host = IGMP_HOST_NOT_LAST;
-  p->state = IGMP_STATE_IDLE_MEMBER;
-  igmp_dbg("IGMP: new state = idle member\n");
-  return 0;
+    p->last_host = IGMP_HOST_NOT_LAST;
+    p->state = IGMP_STATE_IDLE_MEMBER;
+    igmp_dbg("IGMP: new state = idle member\n");
+    return 0;
 }
 
 /* send report, set flag */
 static int srsf(struct igmp_parameters *p)
 {
-  igmp_dbg("IGMP: event = timer expired | action = send report, set flag\n");
+    igmp_dbg("IGMP: event = timer expired | action = send report, set flag\n");
 
-  if (pico_igmp_send_report(p, p->f) < 0)
-    return -1;
+    if (pico_igmp_send_report(p, p->f) < 0)
+        return -1;
 
-  p->state = IGMP_STATE_IDLE_MEMBER;
-  igmp_dbg("IGMP: new state = idle member\n"); 
-  return 0;
+    p->state = IGMP_STATE_IDLE_MEMBER;
+    igmp_dbg("IGMP: new state = idle member\n");
+    return 0;
 }
 
 /* reset timer if max response time < current timer */
 static int rtimrtct(struct igmp_parameters *p)
 {
-  struct igmp_timer *t = NULL;
-  uint32_t time_to_run = 0;
+    struct igmp_timer *t = NULL;
+    uint32_t time_to_run = 0;
 
-  igmp_dbg("IGMP: event = query received | action = reset timer if max response time < current timer\n");
+    igmp_dbg("IGMP: event = query received | action = reset timer if max response time < current timer\n");
 
-  t = pico_igmp_find_timer(IGMP_TIMER_GROUP_REPORT, &p->mcast_link, &p->mcast_group);
-  if (!t)
-    return -1;
+    t = pico_igmp_find_timer(IGMP_TIMER_GROUP_REPORT, &p->mcast_link, &p->mcast_group);
+    if (!t)
+        return -1;
 
-  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);
-  }
+    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);
+    }
 
-  p->state = IGMP_STATE_DELAYING_MEMBER;
-  igmp_dbg("IGMP: new state = delaying member\n"); 
-  return 0;
+    p->state = IGMP_STATE_DELAYING_MEMBER;
+    igmp_dbg("IGMP: new state = delaying member\n");
+    return 0;
 }
 
-static int discard(struct igmp_parameters *p){
-  igmp_dbg("IGMP: ignore and discard frame\n");
-  pico_frame_discard(p->f);
-  return 0;
+static int discard(struct igmp_parameters *p)
+{
+    igmp_dbg("IGMP: ignore and discard frame\n");
+    pico_frame_discard(p->f);
+    return 0;
 }
 
 /* finite state machine table */
 const callback host_membership_diagram_table[3][6] =
 { /* event                    |Delete Group  |Create Group |Update Group |Query Received  |Report Received  |Timer Expired */
-/* state Non-Member      */ { discard,       srsfst,       srsfst,       discard,         discard,          discard },
+/* state Non-Member      */
+    { discard,       srsfst,       srsfst,       discard,         discard,          discard },
 /* state Delaying Member */ { stslifs,       mrsrrt,       mrsrrt,       rtimrtct,        stcl,             srsf    },
 /* state Idle Member     */ { slifs,         srst,         srst,         st,              discard,          discard }
 };
 
 static int pico_igmp_process_event(struct igmp_parameters *p)
 {
-  struct pico_tree_node *index = NULL;
-  struct igmp_parameters *_p = NULL;
+    struct pico_tree_node *index = NULL;
+    struct igmp_parameters *_p = NULL;
 
-  igmp_dbg("IGMP: process event on group address %08X\n", p->mcast_group.addr);
-  if (p->event == IGMP_EVENT_QUERY_RECV && p->mcast_group.addr == 0) { /* general query */
-    pico_tree_foreach(index, &IGMPParameters) {
-      _p = index->keyValue;
-      _p->max_resp_time = p->max_resp_time;
-      _p->event = IGMP_EVENT_QUERY_RECV;
-      igmp_dbg("IGMP: for each mcast_group = %08X | state = %u\n", _p->mcast_group.addr, _p->state);
-      host_membership_diagram_table[_p->state][_p->event](_p);
+    igmp_dbg("IGMP: process event on group address %08X\n", p->mcast_group.addr);
+    if (p->event == IGMP_EVENT_QUERY_RECV && p->mcast_group.addr == 0) { /* general query */
+        pico_tree_foreach(index, &IGMPParameters) {
+            _p = index->keyValue;
+            _p->max_resp_time = p->max_resp_time;
+            _p->event = IGMP_EVENT_QUERY_RECV;
+            igmp_dbg("IGMP: for each mcast_group = %08X | state = %u\n", _p->mcast_group.addr, _p->state);
+            host_membership_diagram_table[_p->state][_p->event](_p);
+        }
+    } else {
+        igmp_dbg("IGMP: state = %u (0: non-member - 1: delaying member - 2: idle member)\n", p->state);
+        host_membership_diagram_table[p->state][p->event](p);
     }
-  } else {
-    igmp_dbg("IGMP: state = %u (0: non-member - 1: delaying member - 2: idle member)\n", p->state); 
-    host_membership_diagram_table[p->state][p->event](p);
-  }
-  return 0;
+
+    return 0;
 }
 
--- a/modules/pico_igmp.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_igmp.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-Authors: Kristof Roelants, Simon Maes, Brecht Van Cauwenberghe
-*********************************************************************/
+   Authors: Kristof Roelants, Simon Maes, Brecht Van Cauwenberghe
+ *********************************************************************/
 
 #ifndef _INCLUDE_PICO_IGMP
 #define _INCLUDE_PICO_IGMP
--- a/modules/pico_ipfilter.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_ipfilter.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Authors: Andrei Carp
-		 Simon  Maes
-*********************************************************************/
+   Authors: Andrei Carp
+         Simon  Maes
+ *********************************************************************/
 
 #include "pico_ipv4.h"
 #include "pico_config.h"
@@ -19,223 +19,230 @@
 #include "pico_tree.h"
 
 /**************** LOCAL MACROS ****************/
-#define MAX_PRIORITY	(10)
-#define MIN_PRIORITY	(-10)
+#define MAX_PRIORITY    (10)
+#define MIN_PRIORITY    (-10)
 
-#define ipf_dbg(...) do{}while(0)
+#define ipf_dbg(...) do {} while(0)
 
 /**************** LOCAL DECLARATIONS ****************/
 struct filter_node;
-typedef int (*func_pntr)(struct filter_node *filter, struct pico_frame *f);
 static int filter_compare(void *filterA, void *filterB);
 
 /**************** FILTER TREE ****************/
 
 struct filter_node {
-  struct pico_device *fdev;
-  // output address
-  uint32_t out_addr;
-  uint32_t out_addr_netmask;
-  // input address
-  uint32_t in_addr;
-  uint32_t in_addr_netmask;
-  // transport
-  uint16_t out_port;
-  uint16_t in_port;
-  // filter details
-  uint8_t proto;
-  int8_t priority;
-  uint8_t tos;
-  uint8_t filter_id;
-  func_pntr function_ptr;
+    struct pico_device *fdev;
+    /* output address */
+    uint32_t out_addr;
+    uint32_t out_addr_netmask;
+    /* input address */
+    uint32_t in_addr;
+    uint32_t in_addr_netmask;
+    /* transport */
+    uint16_t out_port;
+    uint16_t in_port;
+    /* filter details */
+    uint8_t proto;
+    int8_t priority;
+    uint8_t tos;
+    uint8_t filter_id;
+    int (*function_ptr)(struct filter_node *filter, struct pico_frame *f);
 };
 
-PICO_TREE_DECLARE(filter_tree,&filter_compare);
+PICO_TREE_DECLARE(filter_tree, &filter_compare);
 
-#define CHECK_AND_RETURN(a,b) do{ \
-									if((a) && ((a)!=(b)))\
-									{ \
-									  if((a)>(b)) return 1; \
-									  else return -1;\
-									}\
-								}while(0) \
+#define CHECK_AND_RETURN(a, b) do { \
+        if((a) && ((a) != (b))) \
+        { \
+            if((a) > (b)) return 1; \
+            else return -1; \
+        } \
+} while(0) \
 
 
 int filter_compare(void *filterA, void *filterB)
 {
-	struct filter_node * filter = (struct filter_node *)filterA,
-			* temp = (struct filter_node *)filterB;
+    struct filter_node *filter = (struct filter_node *)filterA,
+    *temp = (struct filter_node *)filterB;
 
-	// improve the search
-	if(temp->filter_id && filter->filter_id == temp->filter_id)
-		return 0;
+    /* improve the search */
+    if(temp->filter_id && filter->filter_id == temp->filter_id)
+        return 0;
 
-	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->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);
-	CHECK_AND_RETURN(filter->proto,temp->proto);
+    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->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);
+    CHECK_AND_RETURN(filter->proto, temp->proto);
 
-	return 0;
+    return 0;
 }
 
 /**************** FILTER CALLBACKS ****************/
 
-static int fp_priority(struct filter_node *filter, struct pico_frame *f) {
-  //TODO do priority-stuff
-  IGNORE_PARAMETER(filter);
-  IGNORE_PARAMETER(f);
-  return 0;
+static int fp_priority(struct filter_node *filter, struct pico_frame *f)
+{
+    /* TODO do priority-stuff */
+    IGNORE_PARAMETER(filter);
+    IGNORE_PARAMETER(f);
+    return 0;
 }
 
-static int fp_reject(struct filter_node *filter, struct pico_frame *f) {
-// TODO check first if sender is pico itself or not
-  IGNORE_PARAMETER(filter);
-  ipf_dbg("ipfilter> reject\n");
-  pico_icmp4_packet_filtered(f);
-  pico_frame_discard(f);
-  return 1;
+static int fp_reject(struct filter_node *filter, struct pico_frame *f)
+{
+/* TODO check first if sender is pico itself or not */
+    IGNORE_PARAMETER(filter);
+    ipf_dbg("ipfilter> reject\n");
+    pico_icmp4_packet_filtered(f);
+    pico_frame_discard(f);
+    return 1;
 }
 
-static int fp_drop(struct filter_node *filter, struct pico_frame *f) {
-  IGNORE_PARAMETER(filter);
-  ipf_dbg("ipfilter> drop\n");
-  pico_frame_discard(f);
-  return 1;
+static int fp_drop(struct filter_node *filter, struct pico_frame *f)
+{
+    IGNORE_PARAMETER(filter);
+    ipf_dbg("ipfilter> drop\n");
+    pico_frame_discard(f);
+    return 1;
 }
 
 /**************** FILTER API's ****************/
 int pico_ipv4_filter_add(struct pico_device *dev, uint8_t proto, struct pico_ip4 *out_addr, struct pico_ip4 *out_addr_netmask, struct pico_ip4 *in_addr, struct pico_ip4 *in_addr_netmask, uint16_t out_port, uint16_t in_port, int8_t priority, uint8_t tos, enum filter_action action)
 {
-  static uint8_t filter_id = 0;
-  struct filter_node *new_filter;
+    static uint8_t filter_id = 0;
+    struct filter_node *new_filter;
 
-  if (proto != PICO_PROTO_IPV4 || tos != 0 )
-  {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (proto != PICO_PROTO_IPV4 || tos != 0 )
+    {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    if ( priority > MAX_PRIORITY || priority < MIN_PRIORITY) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  if ( priority > MAX_PRIORITY || priority < MIN_PRIORITY) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  if (action > FILTER_COUNT) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  ipf_dbg("ipfilter> adding filter\n");
+    if (action > FILTER_COUNT) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    ipf_dbg("ipfilter> adding filter\n");
 
-  new_filter = pico_zalloc(sizeof(struct filter_node));
+    new_filter = pico_zalloc(sizeof(struct filter_node));
 
-  new_filter->fdev = dev;
-  new_filter->proto = proto;
+    new_filter->fdev = dev;
+    new_filter->proto = proto;
 
-  new_filter->out_addr = (!out_addr)? 0U : out_addr->addr;
-  new_filter->out_addr_netmask = (!out_addr_netmask) ? 0U : out_addr_netmask->addr;
-  new_filter->in_addr = (!in_addr) ? 0U : in_addr->addr;
-  new_filter->in_addr_netmask = (!in_addr_netmask) ? 0U : in_addr_netmask->addr;
+    new_filter->out_addr = (!out_addr) ? 0U : out_addr->addr;
+    new_filter->out_addr_netmask = (!out_addr_netmask) ? 0U : out_addr_netmask->addr;
+    new_filter->in_addr = (!in_addr) ? 0U : in_addr->addr;
+    new_filter->in_addr_netmask = (!in_addr_netmask) ? 0U : in_addr_netmask->addr;
 
-  new_filter->out_port = out_port;
-  new_filter->in_port = in_port;
-  new_filter->priority = priority;
-  new_filter->tos = tos;
+    new_filter->out_port = out_port;
+    new_filter->in_port = in_port;
+    new_filter->priority = priority;
+    new_filter->tos = tos;
 
-  if(filter_id == 0)
-	  filter_id = 1;
+    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) {
+    /*Define filterType_functionPointer here instead of in ipfilter-function, to prevent running multiple times through switch*/
+    switch (action) {
     case FILTER_PRIORITY:
-      new_filter->function_ptr = fp_priority;
-      break;
+        new_filter->function_ptr = fp_priority;
+        break;
     case FILTER_REJECT:
-      new_filter->function_ptr = fp_reject;
-      break;
+        new_filter->function_ptr = fp_reject;
+        break;
     case FILTER_DROP:
-      new_filter->function_ptr = fp_drop;
-      break;
+        new_filter->function_ptr = fp_drop;
+        break;
     default:
-      ipf_dbg("ipfilter> unknown filter action\n");
-      break;
-  }
-  if(pico_tree_insert(&filter_tree,new_filter))
-  {
-	  pico_free(new_filter);
-	  ipf_dbg("ipfilter> failed adding filter to tree.\n");
-	  return -1;
-  }
+        ipf_dbg("ipfilter> unknown filter action\n");
+        break;
+    }
+    if(pico_tree_insert(&filter_tree, new_filter))
+    {
+        pico_free(new_filter);
+        ipf_dbg("ipfilter> failed adding filter to tree.\n");
+        return -1;
+    }
 
-  return new_filter->filter_id;
+    return new_filter->filter_id;
 }
 
 int pico_ipv4_filter_del(uint8_t filter_id)
 {
-	struct filter_node *node = NULL;
-	struct filter_node dummy={.filter_id=filter_id};
-	if((node = pico_tree_delete(&filter_tree,&dummy))== NULL)
-	{
-		ipf_dbg("ipfilter> failed to delete filter :%d\n",filter_id);
-		return -1;
-	}
+    struct filter_node *node = NULL;
+    struct filter_node dummy = {
+        .filter_id = filter_id
+    };
+    if((node = pico_tree_delete(&filter_tree, &dummy)) == NULL)
+    {
+        ipf_dbg("ipfilter> failed to delete filter :%d\n", filter_id);
+        return -1;
+    }
 
-	pico_free(node);
-	return 0;
+    pico_free(node);
+    return 0;
 }
 
 int ipfilter(struct pico_frame *f)
 {
-  struct filter_node temp;
-  struct filter_node * filter_frame = NULL;
-  struct pico_ipv4_hdr *ipv4_hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-  struct pico_tcp_hdr *tcp_hdr;
-  struct pico_udp_hdr *udp_hdr;
-  struct pico_icmp4_hdr *icmp_hdr;
+    struct filter_node temp;
+    struct filter_node *filter_frame = NULL;
+    struct pico_ipv4_hdr *ipv4_hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    struct pico_tcp_hdr *tcp_hdr;
+    struct pico_udp_hdr *udp_hdr;
+    struct pico_icmp4_hdr *icmp_hdr;
 
-  memset(&temp,0u,sizeof(struct filter_node));
+    memset(&temp, 0u, sizeof(struct filter_node));
 
-  temp.fdev = f->dev;
-  temp.out_addr = ipv4_hdr->dst.addr;
-  temp.in_addr = ipv4_hdr->src.addr;
+    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);
-      temp.in_port = short_be(tcp_hdr->trans.sport);
-  }else if (ipv4_hdr->proto == PICO_PROTO_UDP ) {
-      udp_hdr = (struct pico_udp_hdr *) f->transport_hdr;
-      temp.out_port = short_be(udp_hdr->trans.dport);
-      temp.in_port = short_be(udp_hdr->trans.sport);
-  }
-  else
-  {
-	  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->type == PICO_ICMP_UNREACH_FILTER_PROHIB)
-			  return 0;
-	  }
-	  temp.out_port = temp.in_port = 0;
-  }
+    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);
+        temp.in_port = short_be(tcp_hdr->trans.sport);
+    }else if (ipv4_hdr->proto == PICO_PROTO_UDP ) {
+        udp_hdr = (struct pico_udp_hdr *) f->transport_hdr;
+        temp.out_port = short_be(udp_hdr->trans.dport);
+        temp.in_port = short_be(udp_hdr->trans.sport);
+    }
+    else
+    {
+        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->type == PICO_ICMP_UNREACH_FILTER_PROHIB)
+                return 0;
+        }
 
-  temp.proto = ipv4_hdr->proto;
-  temp.priority = f->priority;
-  temp.tos = ipv4_hdr->tos;
+        temp.out_port = temp.in_port = 0;
+    }
+
+    temp.proto = ipv4_hdr->proto;
+    temp.priority = f->priority;
+    temp.tos = ipv4_hdr->tos;
 
-  filter_frame = pico_tree_findKey(&filter_tree,&temp);
-  if(filter_frame)
-  {
-	  ipf_dbg("Filtering frame %p with filter %p\n",f,filter_frame);
-	  filter_frame->function_ptr(filter_frame,f);
-	  return 1;
-  }
+    filter_frame = pico_tree_findKey(&filter_tree, &temp);
+    if(filter_frame)
+    {
+        ipf_dbg("Filtering frame %p with filter %p\n", f, filter_frame);
+        filter_frame->function_ptr(filter_frame, f);
+        return 1;
+    }
 
-  return 0;
+    return 0;
 }
 
--- a/modules/pico_ipfilter.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_ipfilter.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,25 +1,25 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Authors: Simon Maes
-*********************************************************************/
+   Authors: Simon Maes
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_IPFILTER
 #define _INCLUDE_PICO_IPFILTER
 
 #include "pico_device.h"
 
 enum filter_action {
-  FILTER_PRIORITY=0,
-  FILTER_REJECT,
-  FILTER_DROP,
-  FILTER_COUNT
+    FILTER_PRIORITY = 0,
+    FILTER_REJECT,
+    FILTER_DROP,
+    FILTER_COUNT
 };
 
 int pico_ipv4_filter_add(struct pico_device *dev, uint8_t proto,
-  struct pico_ip4 *out_addr, struct pico_ip4 *out_addr_netmask, struct pico_ip4 *in_addr,
-  struct pico_ip4 *in_addr_netmask, uint16_t out_port, uint16_t in_port,
-  int8_t priority, uint8_t tos, enum filter_action action);
+                         struct pico_ip4 *out_addr, struct pico_ip4 *out_addr_netmask, struct pico_ip4 *in_addr,
+                         struct pico_ip4 *in_addr_netmask, uint16_t out_port, uint16_t in_port,
+                         int8_t priority, uint8_t tos, enum filter_action action);
 
 int pico_ipv4_filter_del(uint8_t filter_id);
 
--- a/modules/pico_ipv4.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_ipv4.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Authors: Daniele Lacamera, Markian Yskout
-*********************************************************************/
+   Authors: Daniele Lacamera, Markian Yskout
+ *********************************************************************/
 
 
 #include "pico_config.h"
@@ -23,220 +23,228 @@
 #ifdef PICO_SUPPORT_IPV4
 
 #ifdef PICO_SUPPORT_MCAST
-# define ip_mcast_dbg(...) do{}while(0) /* so_mcast_dbg in pico_socket.c */
+# define ip_mcast_dbg(...) do {} while(0) /* so_mcast_dbg in pico_socket.c */
 # define PICO_MCAST_ALL_HOSTS long_be(0xE0000001) /* 224.0.0.1 */
 /* Default network interface for multicast transmission */
 static struct pico_ipv4_link *mcast_default_link = NULL;
 #endif
 #ifdef PICO_SUPPORT_IPFRAG
-# define reassembly_dbg(...) do{}while(0) 
+# define reassembly_dbg(...) do {} while(0)
 #endif
 
 /* Queues */
-static struct pico_queue in = {0};
-static struct pico_queue out = {0};
+static struct pico_queue in = {
+    0
+};
+static struct pico_queue out = {
+    0
+};
 
 /* Functions */
-static int ipv4_route_compare(void *ka, void * kb);
+static int ipv4_route_compare(void *ka, void *kb);
 
 int pico_ipv4_to_string(char *ipbuf, const uint32_t ip)
 {
-  const unsigned char *addr = (const unsigned char *) &ip;
-  int i;
+    const unsigned char *addr = (const unsigned char *) &ip;
+    int i;
 
-  if (!ipbuf) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (!ipbuf) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  for(i = 0; i < 4; i++)
-  {
-    if(addr[i] > 99){
-      *ipbuf++ = (char)('0' + (addr[i] / 100));
-      *ipbuf++ = (char)('0' + ((addr[i] % 100) / 10));
-      *ipbuf++ = (char)('0' + ((addr[i] % 100) % 10));
-    }else if(addr[i] > 9){
-      *ipbuf++ = (char)('0' + (addr[i] / 10));
-      *ipbuf++ = (char)('0' + (addr[i] % 10));
-    }else{
-      *ipbuf++ = (char)('0' + addr[i]);
+    for(i = 0; i < 4; i++)
+    {
+        if(addr[i] > 99) {
+            *ipbuf++ = (char)('0' + (addr[i] / 100));
+            *ipbuf++ = (char)('0' + ((addr[i] % 100) / 10));
+            *ipbuf++ = (char)('0' + ((addr[i] % 100) % 10));
+        }else if(addr[i] > 9) {
+            *ipbuf++ = (char)('0' + (addr[i] / 10));
+            *ipbuf++ = (char)('0' + (addr[i] % 10));
+        }else{
+            *ipbuf++ = (char)('0' + addr[i]);
+        }
+
+        if(i < 3)
+            *ipbuf++ = '.';
     }
-    if(i < 3)
-      *ipbuf++ = '.';
-  }
-  *ipbuf = '\0';
-  
-  return 0;
+    *ipbuf = '\0';
+
+    return 0;
 }
-    
+
 int pico_string_to_ipv4(const char *ipstr, uint32_t *ip)
 {
-  unsigned char buf[4] = {0};
-  int cnt = 0;
-  char p;
-
-  if(!ipstr || !ip) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    unsigned char buf[4] = {
+        0
+    };
+    int cnt = 0;
+    char p;
 
-  while((p = *ipstr++) != 0)
-  {
-    if(pico_is_digit(p)){
-      buf[cnt] = (uint8_t)((10 * buf[cnt]) + (p - '0'));
-    }else if(p == '.'){
-        cnt++;
-    }else{
-      return -1;
+    if(!ipstr || !ip) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
     }
-  }   
-  
-  /* Handle short notation */
-  if(cnt == 1){
-    buf[3] = buf[1];
-    buf[1] = 0;
-    buf[2] = 0;
-  }else if (cnt == 2){
-    buf[3] = buf[2];
-    buf[2] = 0;
-  }else if(cnt != 3){
-    /* String could not be parsed, return error */
-    return -1;
-  }   
 
-  *ip = long_from(buf);
+    while((p = *ipstr++) != 0)
+    {
+        if(pico_is_digit(p)) {
+            buf[cnt] = (uint8_t)((10 * buf[cnt]) + (p - '0'));
+        }else if(p == '.') {
+            cnt++;
+        }else{
+            return -1;
+        }
+    }
+    /* Handle short notation */
+    if(cnt == 1) {
+        buf[3] = buf[1];
+        buf[1] = 0;
+        buf[2] = 0;
+    }else if (cnt == 2) {
+        buf[3] = buf[2];
+        buf[2] = 0;
+    }else if(cnt != 3) {
+        /* String could not be parsed, return error */
+        return -1;
+    }
 
-  return 0;
+    *ip = long_from(buf);
 
-}  
+    return 0;
+
+}
 
 int pico_ipv4_valid_netmask(uint32_t mask)
 {
-  int cnt = 0;
-  int end = 0;
-  int i;
-  uint32_t mask_swap = long_be(mask);
+    int cnt = 0;
+    int end = 0;
+    int i;
+    uint32_t mask_swap = long_be(mask);
 
-  /* 
-   * Swap bytes for convenient parsing 
-   * e.g. 0x..f8ff will become 0xfff8..
-   * Then, we count the consecutive bits
-   *
-   * */
+    /*
+     * Swap bytes for convenient parsing
+     * e.g. 0x..f8ff will become 0xfff8..
+     * Then, we count the consecutive bits
+     *
+     * */
 
-  for(i = 0; i < 32; i++){
-    if((mask_swap << i) & 0x80000000){
-      if(end) {
-        pico_err = PICO_ERR_EINVAL;
-        return -1;
-      }
-      cnt++;
-    }else{
-      end = 1;
-    }        
-  }
-  return cnt;
+    for(i = 0; i < 32; i++) {
+        if((mask_swap << i) & 0x80000000) {
+            if(end) {
+                pico_err = PICO_ERR_EINVAL;
+                return -1;
+            }
+
+            cnt++;
+        }else{
+            end = 1;
+        }
+    }
+    return cnt;
 }
 
-int pico_ipv4_is_unicast(uint32_t address) 
+int pico_ipv4_is_unicast(uint32_t address)
 {
-  const unsigned char *addr = (unsigned char *) &address;
-  if((addr[0] & 0xe0) == 0xe0)
-    return 0; /* multicast */
-    
-  return 1;
+    const unsigned char *addr = (unsigned char *) &address;
+    if((addr[0] & 0xe0) == 0xe0)
+        return 0; /* multicast */
+
+    return 1;
 }
 
-int pico_ipv4_is_multicast(uint32_t address) 
+int pico_ipv4_is_multicast(uint32_t address)
 {
-  const unsigned char *addr = (unsigned char *) &address;
-  if((addr[0] != 0xff) && ((addr[0] & 0xe0) == 0xe0))
-    return 1; /* multicast */
-    
-  return 0;
+    const unsigned char *addr = (unsigned char *) &address;
+    if((addr[0] != 0xff) && ((addr[0] & 0xe0) == 0xe0))
+        return 1; /* multicast */
+
+    return 0;
 }
 
 static int pico_ipv4_checksum(struct pico_frame *f)
 {
-  struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-  if (!hdr)
-    return -1;
-  hdr->crc = 0;
-  hdr->crc = short_be(pico_checksum(hdr, f->net_len));
-  return 0;
+    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    if (!hdr)
+        return -1;
+
+    hdr->crc = 0;
+    hdr->crc = short_be(pico_checksum(hdr, f->net_len));
+    return 0;
 }
 
 #ifdef PICO_SUPPORT_IPFRAG
 struct pico_ipv4_fragmented_packet {
-  uint16_t id;
-  uint8_t proto;
-  struct pico_ip4 src;
-  struct pico_ip4 dst;
-  uint16_t total_len;
-  struct pico_tree *t;
+    uint16_t id;
+    uint8_t proto;
+    struct pico_ip4 src;
+    struct pico_ip4 dst;
+    uint16_t total_len;
+    struct pico_tree *t;
 };
 
 static int pico_ipv4_fragmented_packet_cmp(void *ka, void *kb)
 {
-  struct pico_ipv4_fragmented_packet *a = ka, *b = kb;
+    struct pico_ipv4_fragmented_packet *a = ka, *b = kb;
 
-  if (a->id < b->id)
-    return -1; 
-  else if (a->id > b->id)
-    return 1;
-  else {
-    if (a->proto < b->proto)
-      return -1;
-    else if (a->proto > b->proto)
-      return 1;
-    else {
-      if (a->src.addr < b->src.addr)
+    if (a->id < b->id)
         return -1;
-      else if (a->src.addr > b->src.addr)
+    else if (a->id > b->id)
         return 1;
-      else {
-        if (a->dst.addr < b->dst.addr)
-          return -1;
-        else if (a->dst.addr > b->dst.addr)
-          return 1;
-        else
-          return 0;
-      }
+    else {
+        if (a->proto < b->proto)
+            return -1;
+        else if (a->proto > b->proto)
+            return 1;
+        else {
+            if (a->src.addr < b->src.addr)
+                return -1;
+            else if (a->src.addr > b->src.addr)
+                return 1;
+            else {
+                if (a->dst.addr < b->dst.addr)
+                    return -1;
+                else if (a->dst.addr > b->dst.addr)
+                    return 1;
+                else
+                    return 0;
+            }
+        }
     }
-  }
-} 
+}
 
 static int pico_ipv4_fragmented_element_cmp(void *ka, void *kb)
 {
-  struct pico_frame *frame_a = ka, *frame_b = kb;
-  struct pico_ipv4_hdr *a, *b;
-  a = (struct pico_ipv4_hdr *) frame_a->net_hdr;
-  b = (struct pico_ipv4_hdr *) frame_b->net_hdr;
+    struct pico_frame *frame_a = ka, *frame_b = kb;
+    struct pico_ipv4_hdr *a, *b;
+    a = (struct pico_ipv4_hdr *) frame_a->net_hdr;
+    b = (struct pico_ipv4_hdr *) frame_b->net_hdr;
 
-  if (short_be((a->frag & PICO_IPV4_FRAG_MASK)) < short_be((b->frag & PICO_IPV4_FRAG_MASK)))
-    return -1; 
-  else if (short_be((a->frag & PICO_IPV4_FRAG_MASK)) > short_be((b->frag & PICO_IPV4_FRAG_MASK)))
-    return 1;
-  else
-    return 0;
-} 
-    
+    if (short_be((a->frag & PICO_IPV4_FRAG_MASK)) < short_be((b->frag & PICO_IPV4_FRAG_MASK)))
+        return -1;
+    else if (short_be((a->frag & PICO_IPV4_FRAG_MASK)) > short_be((b->frag & PICO_IPV4_FRAG_MASK)))
+        return 1;
+    else
+        return 0;
+}
+
 PICO_TREE_DECLARE(pico_ipv4_fragmented_tree, pico_ipv4_fragmented_packet_cmp);
 
 static inline void pico_ipv4_fragmented_cleanup(struct pico_ipv4_fragmented_packet *pfrag)
 {
-  struct pico_tree_node *index = NULL, *_tmp = NULL;
-  struct pico_frame *f_frag = NULL;
+    struct pico_tree_node *index = NULL, *_tmp = NULL;
+    struct pico_frame *f_frag = NULL;
 
-  pico_tree_foreach_safe(index, pfrag->t, _tmp) {
-    f_frag = index->keyValue;
-    reassembly_dbg("REASSEMBLY: remove packet with offset %u\n", short_be(((struct pico_ipv4_hdr *)f_frag->net_hdr)->frag) & PICO_IPV4_FRAG_MASK);
-    pico_tree_delete(pfrag->t, f_frag);
-    pico_frame_discard(f_frag);
-  }
-  pico_tree_delete(&pico_ipv4_fragmented_tree, pfrag);
-  pico_free(pfrag->t);
-  pico_free(pfrag);
+    pico_tree_foreach_safe(index, pfrag->t, _tmp) {
+        f_frag = index->keyValue;
+        reassembly_dbg("REASSEMBLY: remove packet with offset %u\n", short_be(((struct pico_ipv4_hdr *)f_frag->net_hdr)->frag) & PICO_IPV4_FRAG_MASK);
+        pico_tree_delete(pfrag->t, f_frag);
+        pico_frame_discard(f_frag);
+    }
+    pico_tree_delete(&pico_ipv4_fragmented_tree, pfrag);
+    pico_free(pfrag->t);
+    pico_free(pfrag);
 }
 #endif /* PICO_SUPPORT_IPFRAG */
 
@@ -244,196 +252,206 @@
 
 static inline struct pico_ipv4_fragmented_packet *fragment_find_by_hdr(struct pico_ipv4_hdr *hdr)
 {
-  struct pico_ipv4_fragmented_packet frag = {0};
-  frag.id = short_be(hdr->id);
-  frag.proto = hdr->proto;
-  frag.src.addr = long_be(hdr->src.addr);
-  frag.dst.addr = long_be(hdr->dst.addr);
-  return pico_tree_findKey(&pico_ipv4_fragmented_tree, &frag);
+    struct pico_ipv4_fragmented_packet frag = {
+        0
+    };
+    frag.id = short_be(hdr->id);
+    frag.proto = hdr->proto;
+    frag.src.addr = long_be(hdr->src.addr);
+    frag.dst.addr = long_be(hdr->dst.addr);
+    return pico_tree_findKey(&pico_ipv4_fragmented_tree, &frag);
 }
 
 
 static inline int8_t pico_ipv4_fragmented_check(struct pico_protocol *self, struct pico_frame **f)
 {
-  uint8_t *running_pointer = NULL;
-  uint16_t running_offset = 0;
-  uint16_t offset = 0;
-  uint16_t data_len = 0;
-  struct pico_ipv4_hdr *f_frag_hdr = NULL, *hdr = (struct pico_ipv4_hdr *) (*f)->net_hdr;
-  struct pico_udp_hdr *udp_hdr = NULL;
-  struct pico_tcp_hdr *tcp_hdr = NULL;
-  struct pico_ipv4_fragmented_packet *pfrag = NULL; 
-  struct pico_frame *f_new = NULL, *f_frag = NULL;
-  struct pico_tree_node *index, *_tmp;
+    uint8_t *running_pointer = NULL;
+    uint16_t running_offset = 0;
+    uint16_t offset = 0;
+    uint16_t data_len = 0;
+    struct pico_ipv4_hdr *f_frag_hdr = NULL, *hdr = (struct pico_ipv4_hdr *) (*f)->net_hdr;
+    struct pico_udp_hdr *udp_hdr = NULL;
+    struct pico_tcp_hdr *tcp_hdr = NULL;
+    struct pico_ipv4_fragmented_packet *pfrag = NULL;
+    struct pico_frame *f_new = NULL, *f_frag = NULL;
+    struct pico_tree_node *index, *_tmp;
+
+    data_len = (uint16_t)(short_be(hdr->len) - (*f)->net_len);
+    offset = short_be(hdr->frag) & PICO_IPV4_FRAG_MASK;
+    if (short_be(hdr->frag) & PICO_IPV4_MOREFRAG) {
+        if (!offset) {
+            reassembly_dbg("REASSEMBLY: first element of a fragmented packet with id %X and offset %u\n", short_be(hdr->id), offset);
+            if (!pico_tree_empty(&pico_ipv4_fragmented_tree)) {
+                reassembly_dbg("REASSEMBLY: cleanup tree\n");
+                /* only one entry allowed in this tree */
+                pfrag = pico_tree_first(&pico_ipv4_fragmented_tree);
+                pico_ipv4_fragmented_cleanup(pfrag);
+            }
+
+            /* add entry in tree for this ID and create secondary tree to contain fragmented elements */
+            pfrag = pico_zalloc(sizeof(struct pico_ipv4_fragmented_packet));
+            if (!pfrag) {
+                pico_err = PICO_ERR_ENOMEM;
+                return -1;
+            }
+
+            pfrag->id = short_be(hdr->id);
+            pfrag->proto = hdr->proto;
+            pfrag->src.addr = long_be(hdr->src.addr);
+            pfrag->dst.addr = long_be(hdr->dst.addr);
+            pfrag->total_len = (uint16_t)(short_be(hdr->len) - (*f)->net_len);
+            pfrag->t = pico_zalloc(sizeof(struct pico_tree));
+            if (!pfrag->t) {
+                pico_free(pfrag);
+                pico_err = PICO_ERR_ENOMEM;
+                return -1;
+            }
+
+            pfrag->t->root = &LEAF;
+            pfrag->t->compare = pico_ipv4_fragmented_element_cmp;
+
+            pico_tree_insert(pfrag->t, *f);
+            pico_tree_insert(&pico_ipv4_fragmented_tree, pfrag);
+            return 0;
+        }
+        else {
+            reassembly_dbg("REASSEMBLY: intermediate element of a fragmented packet with id %X and offset %u\n", short_be(hdr->id), offset);
+            pfrag = fragment_find_by_hdr(hdr);
+            if (pfrag) {
+                pfrag->total_len = (uint16_t)(pfrag->total_len + (short_be(hdr->len) - (*f)->net_len));
+                if (pfrag->total_len > PICO_IPV4_FRAG_MAX_SIZE) {
+                    reassembly_dbg("BIG frame!!!\n");
+                    pfrag = pico_tree_first(&pico_ipv4_fragmented_tree);
+                    pico_ipv4_fragmented_cleanup(pfrag);
+                    pico_frame_discard(*f);
+                    return 0;
+                }
 
-  data_len = (uint16_t)(short_be(hdr->len) - (*f)->net_len);
-  offset = short_be(hdr->frag) & PICO_IPV4_FRAG_MASK;
-  if (short_be(hdr->frag) & PICO_IPV4_MOREFRAG) {
-    if (!offset) {
-      reassembly_dbg("REASSEMBLY: first element of a fragmented packet with id %X and offset %u\n", short_be(hdr->id), offset);
-      if (!pico_tree_empty(&pico_ipv4_fragmented_tree)) {
-        reassembly_dbg("REASSEMBLY: cleanup tree\n");
-        // only one entry allowed in this tree
-        pfrag = pico_tree_first(&pico_ipv4_fragmented_tree);
-        pico_ipv4_fragmented_cleanup(pfrag);
-      }
-      // add entry in tree for this ID and create secondary tree to contain fragmented elements
-      pfrag = pico_zalloc(sizeof(struct pico_ipv4_fragmented_packet));
-      if (!pfrag) {
-        pico_err = PICO_ERR_ENOMEM;
-        return -1;
-      }
-      pfrag->id = short_be(hdr->id);
-      pfrag->proto = hdr->proto;
-      pfrag->src.addr = long_be(hdr->src.addr);
-      pfrag->dst.addr = long_be(hdr->dst.addr);
-      pfrag->total_len = (uint16_t)(short_be(hdr->len) - (*f)->net_len);
-      pfrag->t = pico_zalloc(sizeof(struct pico_tree));
-      if (!pfrag->t) {
-        pico_free(pfrag);
-        pico_err = PICO_ERR_ENOMEM;
-        return -1;
-      }
-      pfrag->t->root = &LEAF;
-      pfrag->t->compare = pico_ipv4_fragmented_element_cmp;
-       
-      pico_tree_insert(pfrag->t, *f);
-      pico_tree_insert(&pico_ipv4_fragmented_tree, pfrag);
-      return 0;
-    }
-    else {
-      reassembly_dbg("REASSEMBLY: intermediate element of a fragmented packet with id %X and offset %u\n", short_be(hdr->id), offset);
-      pfrag = fragment_find_by_hdr(hdr);
-      if (pfrag) {
-        pfrag->total_len = (uint16_t)(pfrag->total_len + (short_be(hdr->len) - (*f)->net_len));
-				if (pfrag->total_len > PICO_IPV4_FRAG_MAX_SIZE) {
-           reassembly_dbg("BIG frame!!!\n");
-           pfrag = pico_tree_first(&pico_ipv4_fragmented_tree);
-           pico_ipv4_fragmented_cleanup(pfrag);
-    			 pico_frame_discard(*f);
-					 return 0;
-				}
-        pico_tree_insert(pfrag->t, *f);
-        return 0;
-      } else {
-        reassembly_dbg("REASSEMBLY: silently discard intermediate frame, first packet was lost or disallowed (one fragmented packet at a time)\n");
-        pico_frame_discard(*f);
-        return 0;
-      }
-    }
-  } else if (offset) {
-    reassembly_dbg("REASSEMBLY: last element of a fragmented packet with id %X and offset %u\n", short_be(hdr->id), offset);
-    pfrag = fragment_find_by_hdr(hdr);
-    if (pfrag) {
-      pfrag->total_len = (uint16_t)(pfrag->total_len + (short_be(hdr->len) - (*f)->net_len));
-      reassembly_dbg("REASSEMBLY: fragmented packet in tree, reassemble packet of %u data bytes\n", pfrag->total_len);
-			if (pfrag->total_len > PICO_IPV4_FRAG_MAX_SIZE) {
-           reassembly_dbg("BIG frame!!!\n");
-           pfrag = pico_tree_first(&pico_ipv4_fragmented_tree);
-           pico_ipv4_fragmented_cleanup(pfrag);
-    			 pico_frame_discard(*f);
-					 return 0;
-				}
-      f_new = self->alloc(self, pfrag->total_len);
+                pico_tree_insert(pfrag->t, *f);
+                return 0;
+            } else {
+                reassembly_dbg("REASSEMBLY: silently discard intermediate frame, first packet was lost or disallowed (one fragmented packet at a time)\n");
+                pico_frame_discard(*f);
+                return 0;
+            }
+        }
+    } else if (offset) {
+        reassembly_dbg("REASSEMBLY: last element of a fragmented packet with id %X and offset %u\n", short_be(hdr->id), offset);
+        pfrag = fragment_find_by_hdr(hdr);
+        if (pfrag) {
+            pfrag->total_len = (uint16_t)(pfrag->total_len + (short_be(hdr->len) - (*f)->net_len));
+            reassembly_dbg("REASSEMBLY: fragmented packet in tree, reassemble packet of %u data bytes\n", pfrag->total_len);
+            if (pfrag->total_len > PICO_IPV4_FRAG_MAX_SIZE) {
+                reassembly_dbg("BIG frame!!!\n");
+                pfrag = pico_tree_first(&pico_ipv4_fragmented_tree);
+                pico_ipv4_fragmented_cleanup(pfrag);
+                pico_frame_discard(*f);
+                return 0;
+            }
+
+            f_new = self->alloc(self, pfrag->total_len);
+
+            f_frag = pico_tree_first(pfrag->t);
+            reassembly_dbg("REASSEMBLY: copy IP header information len = %lu\n", f_frag->net_len);
+            f_frag_hdr = (struct pico_ipv4_hdr *)f_frag->net_hdr;
+            data_len = (uint16_t)(short_be(f_frag_hdr->len) - f_frag->net_len);
+            memcpy(f_new->net_hdr, f_frag->net_hdr, f_frag->net_len);
+            memcpy(f_new->transport_hdr, f_frag->transport_hdr, data_len);
+            running_pointer = f_new->transport_hdr + data_len;
+            offset = short_be(f_frag_hdr->frag) & PICO_IPV4_FRAG_MASK;
+            running_offset = data_len / 8;
+            pico_tree_delete(pfrag->t, f_frag);
+            pico_frame_discard(f_frag);
+            reassembly_dbg("REASSEMBLY: reassembled first packet of %u data bytes, offset = %u next expected offset = %u\n", data_len, offset, running_offset);
 
-      f_frag = pico_tree_first(pfrag->t);
-      reassembly_dbg("REASSEMBLY: copy IP header information len = %lu\n", f_frag->net_len);
-      f_frag_hdr = (struct pico_ipv4_hdr *)f_frag->net_hdr;
-      data_len = (uint16_t)(short_be(f_frag_hdr->len) - f_frag->net_len);
-      memcpy(f_new->net_hdr, f_frag->net_hdr, f_frag->net_len);
-      memcpy(f_new->transport_hdr, f_frag->transport_hdr, data_len);
-      running_pointer = f_new->transport_hdr + data_len;
-      offset = short_be(f_frag_hdr->frag) & PICO_IPV4_FRAG_MASK;
-      running_offset = data_len / 8;
-      pico_tree_delete(pfrag->t, f_frag);
-      pico_frame_discard(f_frag);
-      reassembly_dbg("REASSEMBLY: reassembled first packet of %u data bytes, offset = %u next expected offset = %u\n", data_len, offset, running_offset);
+            pico_tree_foreach_safe(index, pfrag->t, _tmp)
+            {
+                f_frag = index->keyValue;
+                f_frag_hdr = (struct pico_ipv4_hdr *)f_frag->net_hdr;
+                data_len = (uint16_t)(short_be(f_frag_hdr->len) - f_frag->net_len);
+                memcpy(running_pointer, f_frag->transport_hdr, data_len);
+                running_pointer += data_len;
+                offset = short_be(f_frag_hdr->frag) & PICO_IPV4_FRAG_MASK;
+                if (offset != running_offset) {
+                    reassembly_dbg("REASSEMBLY: error reassembling intermediate packet: offset %u != expected offset %u (missing fragment)\n", offset, running_offset);
+                    pico_ipv4_fragmented_cleanup(pfrag);
+                    return -1;
+                }
 
-      pico_tree_foreach_safe(index, pfrag->t, _tmp)
-      {
-        f_frag = index->keyValue;
-        f_frag_hdr = (struct pico_ipv4_hdr *)f_frag->net_hdr;
-        data_len = (uint16_t)(short_be(f_frag_hdr->len) - f_frag->net_len);
-        memcpy(running_pointer, f_frag->transport_hdr, data_len);
-        running_pointer += data_len;
-        offset = short_be(f_frag_hdr->frag) & PICO_IPV4_FRAG_MASK;
-        if (offset != running_offset) {
-          reassembly_dbg("REASSEMBLY: error reassembling intermediate packet: offset %u != expected offset %u (missing fragment)\n", offset, running_offset);
-          pico_ipv4_fragmented_cleanup(pfrag);
-          return -1;
-        }
-        running_offset = (uint16_t)(running_offset + (data_len / 8));
-        pico_tree_delete(pfrag->t, f_frag);
-        pico_frame_discard(f_frag);
-        reassembly_dbg("REASSEMBLY: reassembled intermediate packet of %u data bytes, offset = %u next expected offset = %u\n", data_len, offset, running_offset);
-      }
-      pico_tree_delete(&pico_ipv4_fragmented_tree, pfrag);
-      pico_free(pfrag);
+                running_offset = (uint16_t)(running_offset + (data_len / 8));
+                pico_tree_delete(pfrag->t, f_frag);
+                pico_frame_discard(f_frag);
+                reassembly_dbg("REASSEMBLY: reassembled intermediate packet of %u data bytes, offset = %u next expected offset = %u\n", data_len, offset, running_offset);
+            }
+            pico_tree_delete(&pico_ipv4_fragmented_tree, pfrag);
+            pico_free(pfrag);
 
-      data_len = (uint16_t)(short_be(hdr->len) - (*f)->net_len);
-      memcpy(running_pointer, (*f)->transport_hdr, data_len);
-      offset = short_be(hdr->frag) & PICO_IPV4_FRAG_MASK;
-      pico_frame_discard(*f);
-      reassembly_dbg("REASSEMBLY: reassembled last packet of %u data bytes, offset = %u\n", data_len, offset);
-      
-      hdr = (struct pico_ipv4_hdr *)f_new->net_hdr;
-      hdr->len = pfrag->total_len;
-      hdr->frag = 0; /* flags cleared and no offset */
-      hdr->crc = 0;
-      hdr->crc = short_be(pico_checksum(hdr, f_new->net_len));
-      /* Optional, the UDP/TCP CRC should already be correct */
-      if (0) {
+            data_len = (uint16_t)(short_be(hdr->len) - (*f)->net_len);
+            memcpy(running_pointer, (*f)->transport_hdr, data_len);
+            offset = short_be(hdr->frag) & PICO_IPV4_FRAG_MASK;
+            pico_frame_discard(*f);
+            reassembly_dbg("REASSEMBLY: reassembled last packet of %u data bytes, offset = %u\n", data_len, offset);
+
+            hdr = (struct pico_ipv4_hdr *)f_new->net_hdr;
+            hdr->len = pfrag->total_len;
+            hdr->frag = 0; /* flags cleared and no offset */
+            hdr->crc = 0;
+            hdr->crc = short_be(pico_checksum(hdr, f_new->net_len));
+            /* Optional, the UDP/TCP CRC should already be correct */
+            if (0) {
   #ifdef PICO_SUPPORT_TCP
-      } else if (hdr->proto == PICO_PROTO_TCP) {
-        tcp_hdr = (struct pico_tcp_hdr *) f_new->transport_hdr;
-        tcp_hdr->crc = 0;
-        tcp_hdr->crc = short_be(pico_tcp_checksum_ipv4(f_new));
+            } else if (hdr->proto == PICO_PROTO_TCP) {
+                tcp_hdr = (struct pico_tcp_hdr *) f_new->transport_hdr;
+                tcp_hdr->crc = 0;
+                tcp_hdr->crc = short_be(pico_tcp_checksum_ipv4(f_new));
   #endif
   #ifdef PICO_SUPPORT_UDP
-      } else if (hdr->proto == PICO_PROTO_UDP){
-        udp_hdr = (struct pico_udp_hdr *) f_new->transport_hdr;
-        udp_hdr->crc = 0;
-        udp_hdr->crc = short_be(pico_udp_checksum_ipv4(f_new));
+            } else if (hdr->proto == PICO_PROTO_UDP) {
+                udp_hdr = (struct pico_udp_hdr *) f_new->transport_hdr;
+                udp_hdr->crc = 0;
+                udp_hdr->crc = short_be(pico_udp_checksum_ipv4(f_new));
   #endif
-      }
-      reassembly_dbg("REASSEMBLY: packet with id %X reassembled correctly\n", short_be(hdr->id));
-      *f = f_new;
-      return 1;
+            }
+
+            reassembly_dbg("REASSEMBLY: packet with id %X reassembled correctly\n", short_be(hdr->id));
+            *f = f_new;
+            return 1;
+        } else {
+            reassembly_dbg("REASSEMBLY: silently discard last frame, first packet was lost or disallowed (one fragmented packet at a time)\n");
+            pico_frame_discard(*f);
+            return 0;
+        }
     } else {
-      reassembly_dbg("REASSEMBLY: silently discard last frame, first packet was lost or disallowed (one fragmented packet at a time)\n");
-      pico_frame_discard(*f);
-      return 0;
+        return 1;
     }
-  } else {
-    return 1;
-  }
 }
 #else
 static inline int8_t pico_ipv4_fragmented_check(struct pico_protocol *self, struct pico_frame **f)
 {
-  return 1;
+    return 1;
 }
 #endif /* PICO_SUPPORT_IPFRAG */
 
 #ifdef PICO_SUPPORT_CRC
 static inline int pico_ipv4_crc_check(struct pico_frame *f)
 {
-  uint16_t checksum_invalid = 1;
-  struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    uint16_t checksum_invalid = 1;
+    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
 
-  checksum_invalid = short_be(pico_checksum(hdr, f->net_len));
-  if (checksum_invalid) {
-    dbg("IP: checksum failed!\n");
-    pico_frame_discard(f);
-    return 0;
-  }
-  return 1;
+    checksum_invalid = short_be(pico_checksum(hdr, f->net_len));
+    if (checksum_invalid) {
+        dbg("IP: checksum failed!\n");
+        pico_frame_discard(f);
+        return 0;
+    }
+
+    return 1;
 }
 #else
 static inline int pico_ipv4_crc_check(struct pico_frame *f)
 {
-	IGNORE_PARAMETER(f);
-  return 1;
+    IGNORE_PARAMETER(f);
+    return 1;
 }
 #endif /* PICO_SUPPORT_CRC */
 
@@ -444,110 +462,122 @@
 
 static int ipv4_link_compare(void *ka, void *kb)
 {
-  struct pico_ipv4_link *a = ka, *b =kb;
-  if (a->address.addr < b->address.addr)
-    return -1;
-  if (a->address.addr > b->address.addr)
-    return 1;
+    struct pico_ipv4_link *a = ka, *b = kb;
+    if (a->address.addr < b->address.addr)
+        return -1;
+
+    if (a->address.addr > b->address.addr)
+        return 1;
 
-  //zero can be assigned multiple times (e.g. for DHCP)
-  if (a->dev != NULL && b->dev != NULL && a->address.addr == PICO_IP4_ANY && b->address.addr == PICO_IP4_ANY){
-    if (a->dev < b->dev)
-      return -1;
-    if (a->dev > b->dev)
-      return 1;
-  }
-  return 0;
+    /* zero can be assigned multiple times (e.g. for DHCP) */
+    if (a->dev != NULL && b->dev != NULL && a->address.addr == PICO_IP4_ANY && b->address.addr == PICO_IP4_ANY) {
+        if (a->dev < b->dev)
+            return -1;
+
+        if (a->dev > b->dev)
+            return 1;
+    }
+
+    return 0;
 }
 
 PICO_TREE_DECLARE(Tree_dev_link, ipv4_link_compare);
 
 static int pico_ipv4_process_in(struct pico_protocol *self, struct pico_frame *f)
 {
-  uint8_t option_len = 0;
-  int ret = 0;
-  struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-  struct pico_ipv4_link test = {.address = {.addr = PICO_IP4_ANY}, .dev = NULL};
+    uint8_t option_len = 0;
+    int ret = 0;
+    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    struct pico_ipv4_link test = {
+        .address = {.addr = PICO_IP4_ANY}, .dev = NULL
+    };
 
-  /* NAT needs transport header information */
-  if(((hdr->vhl) & 0x0F )> 5){
-     option_len =  (uint8_t)(4*(((hdr->vhl) & 0x0F)-5));
-  }
-  f->transport_hdr = ((uint8_t *)f->net_hdr) + PICO_SIZE_IP4HDR + option_len;
-  f->transport_len = (uint16_t)(short_be(hdr->len) - PICO_SIZE_IP4HDR - option_len);
-  f->net_len = (uint16_t)(PICO_SIZE_IP4HDR + option_len);
+    /* NAT needs transport header information */
+    if(((hdr->vhl) & 0x0F) > 5) {
+        option_len =  (uint8_t)(4 * (((hdr->vhl) & 0x0F) - 5));
+    }
+
+    f->transport_hdr = ((uint8_t *)f->net_hdr) + PICO_SIZE_IP4HDR + option_len;
+    f->transport_len = (uint16_t)(short_be(hdr->len) - PICO_SIZE_IP4HDR - option_len);
+    f->net_len = (uint16_t)(PICO_SIZE_IP4HDR + option_len);
 
 #ifdef PICO_SUPPORT_IPFILTER
-  if (ipfilter(f)) {
-    /*pico_frame is discarded as result of the filtering*/
-    return 0;
-  }
+    if (ipfilter(f)) {
+        /*pico_frame is discarded as result of the filtering*/
+        return 0;
+    }
+
 #endif
 
-  /* ret == 1 indicates to continue the function */
-  ret = pico_ipv4_crc_check(f);
-  if (ret < 1)
-    return ret;
-  ret = pico_ipv4_fragmented_check(self, &f);
-  if (ret < 1)
-    return ret;
+    /* ret == 1 indicates to continue the function */
+    ret = pico_ipv4_crc_check(f);
+    if (ret < 1)
+        return ret;
+
+    ret = pico_ipv4_fragmented_check(self, &f);
+    if (ret < 1)
+        return ret;
 
 #ifdef PICO_SUPPORT_MCAST
-  /* Multicast address in source, discard quietly */
-  if (pico_ipv4_is_multicast(hdr->src.addr)) {
-    ip_mcast_dbg("MCAST: ERROR multicast address %08X in source address\n", hdr->src.addr);
-    pico_frame_discard(f);
-    return 0;
-  }
+    /* Multicast address in source, discard quietly */
+    if (pico_ipv4_is_multicast(hdr->src.addr)) {
+        ip_mcast_dbg("MCAST: ERROR multicast address %08X in source address\n", hdr->src.addr);
+        pico_frame_discard(f);
+        return 0;
+    }
+
 #endif
-  if (hdr->frag & 0x80) {
-    pico_frame_discard(f); //RFC 3514
-    return 0;
-  }
-  if (0) {
+    if (hdr->frag & 0x80) {
+        pico_frame_discard(f); /* RFC 3514 */
+        return 0;
+    }
+
+    if (0) {
 #ifdef PICO_SUPPORT_UDP
-  } else if (pico_ipv4_is_broadcast(hdr->dst.addr) && (hdr->proto == PICO_PROTO_UDP)) {
-      /* Receiving UDP broadcast datagram */
-      f->flags |= PICO_FRAME_FLAG_BCAST;
-      pico_enqueue(pico_proto_udp.q_in, f);
+    } else if (pico_ipv4_is_broadcast(hdr->dst.addr) && (hdr->proto == PICO_PROTO_UDP)) {
+        /* Receiving UDP broadcast datagram */
+        f->flags |= PICO_FRAME_FLAG_BCAST;
+        pico_enqueue(pico_proto_udp.q_in, f);
 #endif
-  } else if (pico_ipv4_is_multicast(hdr->dst.addr)) {
+    } else if (pico_ipv4_is_multicast(hdr->dst.addr)) {
 #ifdef PICO_SUPPORT_MCAST
-    /* Receiving UDP multicast datagram TODO set f->flags? */
-    if (hdr->proto == PICO_PROTO_IGMP) {
-      ip_mcast_dbg("MCAST: received IGMP message\n");
-      pico_transport_receive(f, PICO_PROTO_IGMP);
-    } else if ((pico_ipv4_mcast_filter(f) == 0) && (hdr->proto == PICO_PROTO_UDP)) {
-      pico_enqueue(pico_proto_udp.q_in, f);
+        /* Receiving UDP multicast datagram TODO set f->flags? */
+        if (hdr->proto == PICO_PROTO_IGMP) {
+            ip_mcast_dbg("MCAST: received IGMP message\n");
+            pico_transport_receive(f, PICO_PROTO_IGMP);
+        } else if ((pico_ipv4_mcast_filter(f) == 0) && (hdr->proto == PICO_PROTO_UDP)) {
+            pico_enqueue(pico_proto_udp.q_in, f);
+        } else {
+            pico_frame_discard(f);
+        }
+
+#endif
+    } else if (pico_ipv4_link_find(&hdr->dst)) {
+        if (pico_ipv4_nat_inbound(f, &hdr->dst) == 0)
+            pico_enqueue(pico_proto_ipv4.q_in, f); /* dst changed, reprocess */
+        else
+            pico_transport_receive(f, hdr->proto);
+    } else if (pico_tree_findKey(&Tree_dev_link, &test)) {
+#ifdef PICO_SUPPORT_UDP
+        /* address of this device is apparently 0.0.0.0; might be a DHCP packet */
+        /* XXX KRO: is obsolete. Broadcast flag is set on outgoing DHCP messages.
+         * incomming DHCP messages are to be broadcasted. Our current DHCP server
+         * implementation does not take this flag into account yet though ... */
+        pico_enqueue(pico_proto_udp.q_in, f);
+#endif
     } else {
-      pico_frame_discard(f);
+
+        if((pico_ipv4_is_broadcast(hdr->dst.addr)))
+        {
+            /* don't forward broadcast frame, discard! */
+            pico_frame_discard(f);
+        } else if (pico_ipv4_forward(f) != 0) {
+            pico_frame_discard(f);
+            /* dbg("Forward failed.\n"); */
+        }
     }
-#endif
-  } else if (pico_ipv4_link_find(&hdr->dst)) {
-    if (pico_ipv4_nat_inbound(f, &hdr->dst) == 0)
-      pico_enqueue(pico_proto_ipv4.q_in, f); /* dst changed, reprocess */
-    else 
-      pico_transport_receive(f, hdr->proto);
-  } else if (pico_tree_findKey(&Tree_dev_link, &test)){
-#ifdef PICO_SUPPORT_UDP
-    //address of this device is apparently 0.0.0.0; might be a DHCP packet
-    /* XXX KRO: is obsolete. Broadcast flag is set on outgoing DHCP messages.
-     * incomming DHCP messages are to be broadcasted. Our current DHCP server
-     * implementation does not take this flag into account yet though ... */
-    pico_enqueue(pico_proto_udp.q_in, f);
-#endif
-  } else {
-    
-    if((pico_ipv4_is_broadcast(hdr->dst.addr)))
-    {
-      /* don't forward broadcast frame, discard! */
-      pico_frame_discard(f);
-    } else if (pico_ipv4_forward(f) != 0) {
-        pico_frame_discard(f);
-        //dbg("Forward failed.\n");
-    }
-  }
-  return 0;
+
+    return 0;
 }
 
 PICO_TREE_DECLARE(Routes, ipv4_route_compare);
@@ -555,904 +585,947 @@
 
 static int pico_ipv4_process_out(struct pico_protocol *self, struct pico_frame *f)
 {
-	IGNORE_PARAMETER(self);
-	f->start = (uint8_t*) f->net_hdr;
+    IGNORE_PARAMETER(self);
+    f->start = (uint8_t*) f->net_hdr;
   #ifdef PICO_SUPPORT_IPFILTER
-  if (ipfilter(f)) {
-    /*pico_frame is discarded as result of the filtering*/
-    return 0;
-  }
+    if (ipfilter(f)) {
+        /*pico_frame is discarded as result of the filtering*/
+        return 0;
+    }
+
   #endif
-  return pico_sendto_dev(f);
+    return pico_sendto_dev(f);
 }
 
 
 static struct pico_frame *pico_ipv4_alloc(struct pico_protocol *self, uint16_t size)
 {
-	struct pico_frame *f =  pico_frame_alloc(size + PICO_SIZE_IP4HDR + PICO_SIZE_ETHHDR);
-	IGNORE_PARAMETER(self);
+    struct pico_frame *f =  pico_frame_alloc(size + PICO_SIZE_IP4HDR + PICO_SIZE_ETHHDR);
+    IGNORE_PARAMETER(self);
+
+    if (!f)
+        return NULL;
 
-	if (!f)
-    return NULL;
-  f->datalink_hdr = f->buffer;
-  f->net_hdr = f->buffer + PICO_SIZE_ETHHDR;
-  f->net_len = PICO_SIZE_IP4HDR;
-  f->transport_hdr = f->net_hdr + PICO_SIZE_IP4HDR;
-  f->transport_len = size;
-  f->len =  size + PICO_SIZE_IP4HDR;
-  return f;
+    f->datalink_hdr = f->buffer;
+    f->net_hdr = f->buffer + PICO_SIZE_ETHHDR;
+    f->net_len = PICO_SIZE_IP4HDR;
+    f->transport_hdr = f->net_hdr + PICO_SIZE_IP4HDR;
+    f->transport_len = size;
+    f->len =  size + PICO_SIZE_IP4HDR;
+    return f;
 }
 
 static int pico_ipv4_frame_sock_push(struct pico_protocol *self, struct pico_frame *f);
 
 /* Interface: protocol definition */
 struct pico_protocol pico_proto_ipv4 = {
-  .name = "ipv4",
-  .proto_number = PICO_PROTO_IPV4,
-  .layer = PICO_LAYER_NETWORK,
-  .alloc = pico_ipv4_alloc,
-  .process_in = pico_ipv4_process_in,
-  .process_out = pico_ipv4_process_out,
-  .push = pico_ipv4_frame_sock_push,
-  .q_in = &in,
-  .q_out = &out,
+    .name = "ipv4",
+    .proto_number = PICO_PROTO_IPV4,
+    .layer = PICO_LAYER_NETWORK,
+    .alloc = pico_ipv4_alloc,
+    .process_in = pico_ipv4_process_in,
+    .process_out = pico_ipv4_process_out,
+    .push = pico_ipv4_frame_sock_push,
+    .q_in = &in,
+    .q_out = &out,
 };
 
 struct pico_ipv4_route
 {
-  struct pico_ip4 dest;
-  struct pico_ip4 netmask;
-  struct pico_ip4 gateway;
-  struct pico_ipv4_link *link;
-  uint32_t metric;
+    struct pico_ip4 dest;
+    struct pico_ip4 netmask;
+    struct pico_ip4 gateway;
+    struct pico_ipv4_link *link;
+    uint32_t metric;
 };
 
 
-static int ipv4_route_compare(void *ka, void * kb)
+static int ipv4_route_compare(void *ka, void *kb)
 {
-  struct pico_ipv4_route *a = ka, *b = kb;
+    struct pico_ipv4_route *a = ka, *b = kb;
 
-  /* Routes are sorted by (host side) netmask len, then by addr, then by metric. */
-  if (long_be(a->netmask.addr) < long_be(b->netmask.addr))
-    return -1;
+    /* Routes are sorted by (host side) netmask len, then by addr, then by metric. */
+    if (long_be(a->netmask.addr) < long_be(b->netmask.addr))
+        return -1;
 
-  if (long_be(a->netmask.addr) > long_be(b->netmask.addr))
-    return 1;
+    if (long_be(a->netmask.addr) > long_be(b->netmask.addr))
+        return 1;
 
-  if (a->dest.addr < b->dest.addr)
-    return -1;
+    if (a->dest.addr < b->dest.addr)
+        return -1;
 
-  if (a->dest.addr > b->dest.addr)
-    return 1;
+    if (a->dest.addr > b->dest.addr)
+        return 1;
 
-  if (a->metric < b->metric)
-    return -1;
+    if (a->metric < b->metric)
+        return -1;
 
-  if (a->metric > b->metric)
-    return 1;
+    if (a->metric > b->metric)
+        return 1;
 
-  return 0;
+    return 0;
 }
 
 static struct pico_ipv4_route *route_find(const struct pico_ip4 *addr)
 {
-  struct pico_ipv4_route *r;
-  struct pico_tree_node * index;
+    struct pico_ipv4_route *r;
+    struct pico_tree_node *index;
 
-  if(addr->addr != PICO_IP4_BCAST)
-  {
-    pico_tree_foreach_reverse(index, &Routes) {
-      r = index->keyValue;
-      if ((addr->addr & (r->netmask.addr)) == (r->dest.addr)) {
-        return r;
-      }
-    }
-  }
-  else
-  {
-    r = pico_tree_first(&Routes);
-    if(!r->netmask.addr)
+    if(addr->addr != PICO_IP4_BCAST)
     {
-      return r;
+        pico_tree_foreach_reverse(index, &Routes) {
+            r = index->keyValue;
+            if ((addr->addr & (r->netmask.addr)) == (r->dest.addr)) {
+                return r;
+            }
+        }
     }
     else
     {
-      dbg("WARNING: no default route for a global broadcast found\n");
+        r = pico_tree_first(&Routes);
+        if(!r->netmask.addr)
+        {
+            return r;
+        }
+        else
+        {
+            dbg("WARNING: no default route for a global broadcast found\n");
+        }
     }
-  }
 
-  return NULL;
+    return NULL;
 }
 
 struct pico_ip4 pico_ipv4_route_get_gateway(struct pico_ip4 *addr)
 {
-  struct pico_ip4 nullip;
-  struct pico_ipv4_route *route;
-  nullip.addr = 0U;
+    struct pico_ip4 nullip;
+    struct pico_ipv4_route *route;
+    nullip.addr = 0U;
 
-  if(!addr) {
-    pico_err = PICO_ERR_EINVAL;
-    return nullip;
-  }
+    if(!addr) {
+        pico_err = PICO_ERR_EINVAL;
+        return nullip;
+    }
 
-  route = route_find(addr);
-  if (!route) {
-    pico_err = PICO_ERR_EHOSTUNREACH;
-    return nullip;
-  }
-  else
-    return route->gateway;
+    route = route_find(addr);
+    if (!route) {
+        pico_err = PICO_ERR_EHOSTUNREACH;
+        return nullip;
+    }
+    else
+        return route->gateway;
 }
 
 struct pico_ip4 *pico_ipv4_source_find(const struct pico_ip4 *dst)
 {
-  struct pico_ip4 *myself = NULL;
-  struct pico_ipv4_route *rt;
+    struct pico_ip4 *myself = NULL;
+    struct pico_ipv4_route *rt;
+
+    if(!dst) {
+        pico_err = PICO_ERR_EINVAL;
+        return NULL;
+    }
 
-  if(!dst) {
-    pico_err = PICO_ERR_EINVAL;
-    return NULL;
-  }
+    rt = route_find(dst);
+    if (rt) {
+        myself = &rt->link->address;
+    } else
+        pico_err = PICO_ERR_EHOSTUNREACH;
 
-  rt = route_find(dst);
-  if (rt) {
-    myself = &rt->link->address;
-  } else
-    pico_err = PICO_ERR_EHOSTUNREACH;
-  return myself;
+    return myself;
 }
 
 
 #ifdef PICO_SUPPORT_MCAST
 /*                        link
- *                         |  
+ *                         |
  *                    MCASTGroups
  *                    |    |     |
  *         ------------    |     ------------
  *         |               |                |
- *   MCASTSources    MCASTSources     MCASTSources    
+ *   MCASTSources    MCASTSources     MCASTSources
  *   |  |  |  |      |  |  |  |       |  |  |  |
  *   S  S  S  S      S  S  S  S       S  S  S  S
  *
  *   MCASTGroups: RBTree(mcast_group)
  *   MCASTSources: RBTree(source)
  */
-static int ipv4_mcast_groups_cmp(void * ka, void * kb)
+static int ipv4_mcast_groups_cmp(void *ka, void *kb)
 {
-  struct pico_mcast_group *a = ka, *b = kb;
-  if (a->mcast_addr.addr < b->mcast_addr.addr) {
-    return -1;
-  } else if (a->mcast_addr.addr > b->mcast_addr.addr) {
-    return 1;
-  } else {
-    return 0;
-  }
+    struct pico_mcast_group *a = ka, *b = kb;
+    if (a->mcast_addr.addr < b->mcast_addr.addr) {
+        return -1;
+    } else if (a->mcast_addr.addr > b->mcast_addr.addr) {
+        return 1;
+    } else {
+        return 0;
+    }
 }
 
 static int ipv4_mcast_sources_cmp(void *ka, void *kb)
 {
-  struct pico_ip4 *a = ka, *b = kb;
-  if (a->addr < b->addr)
-    return -1;
-  if (a->addr > b->addr)
-    return 1;
-  return 0;
+    struct pico_ip4 *a = ka, *b = kb;
+    if (a->addr < b->addr)
+        return -1;
+
+    if (a->addr > b->addr)
+        return 1;
+
+    return 0;
 }
 
 static void pico_ipv4_mcast_print_groups(struct pico_ipv4_link *mcast_link)
 {
-  uint16_t i = 0;
-  struct pico_mcast_group __attribute__ ((unused)) *g = NULL;
-  struct pico_ip4 __attribute__ ((unused)) *source = NULL;
-  struct pico_tree_node *index = NULL, *index2 = NULL;
+    uint16_t i = 0;
+    struct pico_mcast_group __attribute__ ((unused)) *g = NULL;
+    struct pico_ip4 __attribute__ ((unused)) *source = NULL;
+    struct pico_tree_node *index = NULL, *index2 = NULL;
 
-  ip_mcast_dbg("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
-  ip_mcast_dbg("+                           MULTICAST list interface %-16s             +\n", mcast_link->dev->name);
-  ip_mcast_dbg("+---------------------------------------------------------------------------------+\n");
-  ip_mcast_dbg("+  nr  |    interface     | host group | reference count | filter mode |  source  +\n");
-  ip_mcast_dbg("+---------------------------------------------------------------------------------+\n");
+    ip_mcast_dbg("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
+    ip_mcast_dbg("+                           MULTICAST list interface %-16s             +\n", mcast_link->dev->name);
+    ip_mcast_dbg("+---------------------------------------------------------------------------------+\n");
+    ip_mcast_dbg("+  nr  |    interface     | host group | reference count | filter mode |  source  +\n");
+    ip_mcast_dbg("+---------------------------------------------------------------------------------+\n");
 
-  pico_tree_foreach(index, mcast_link->MCASTGroups)
-  {
-    g = index->keyValue;
-    ip_mcast_dbg("+ %04d | %16s |  %08X  |      %05u      |      %u      | %8s +\n", i, mcast_link->dev->name, g->mcast_addr.addr, g->reference_count, g->filter_mode, "");
-    pico_tree_foreach(index2, &g->MCASTSources)
+    pico_tree_foreach(index, mcast_link->MCASTGroups)
     {
-      source = index2->keyValue;
-      ip_mcast_dbg("+ %4s | %16s |  %8s  |      %5s      |      %s      | %08X +\n", "", "", "", "", "", source->addr);
+        g = index->keyValue;
+        ip_mcast_dbg("+ %04d | %16s |  %08X  |      %05u      |      %u      | %8s +\n", i, mcast_link->dev->name, g->mcast_addr.addr, g->reference_count, g->filter_mode, "");
+        pico_tree_foreach(index2, &g->MCASTSources)
+        {
+            source = index2->keyValue;
+            ip_mcast_dbg("+ %4s | %16s |  %8s  |      %5s      |      %s      | %08X +\n", "", "", "", "", "", source->addr);
+        }
+        i++;
     }
-    i++;
-  }
-  ip_mcast_dbg("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
+    ip_mcast_dbg("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
 }
 
 static int mcast_group_update(struct pico_mcast_group *g, struct pico_tree *MCASTFilter, uint8_t filter_mode)
 {
-  struct pico_tree_node *index = NULL, *_tmp = NULL;
-  struct pico_ip4 *source = NULL;
-  /* cleanup filter */
-  pico_tree_foreach_safe(index, &g->MCASTSources, _tmp)
-  {
-    source = index->keyValue;
-    pico_tree_delete(&g->MCASTSources, source);
-    pico_free(source);
-  }
-  /* insert new filter */
-  if (MCASTFilter) {
-    pico_tree_foreach(index, MCASTFilter)
+    struct pico_tree_node *index = NULL, *_tmp = NULL;
+    struct pico_ip4 *source = NULL;
+    /* cleanup filter */
+    pico_tree_foreach_safe(index, &g->MCASTSources, _tmp)
     {
-      source = pico_zalloc(sizeof(struct pico_ip4));
-      if (!source) {
-        pico_err = PICO_ERR_ENOMEM;
-        return -1;
-      }
-      source->addr = ((struct pico_ip4 *)index->keyValue)->addr;
-      pico_tree_insert(&g->MCASTSources, source);
+        source = index->keyValue;
+        pico_tree_delete(&g->MCASTSources, source);
+        pico_free(source);
     }
-  }
-  g->filter_mode = filter_mode;
-  return 0;
+    /* insert new filter */
+    if (MCASTFilter) {
+        pico_tree_foreach(index, MCASTFilter)
+        {
+            source = pico_zalloc(sizeof(struct pico_ip4));
+            if (!source) {
+                pico_err = PICO_ERR_ENOMEM;
+                return -1;
+            }
+
+            source->addr = ((struct pico_ip4 *)index->keyValue)->addr;
+            pico_tree_insert(&g->MCASTSources, source);
+        }
+    }
+
+    g->filter_mode = filter_mode;
+    return 0;
 }
 
 int pico_ipv4_mcast_join(struct pico_ip4 *mcast_link, struct pico_ip4 *mcast_group, uint8_t reference_count, uint8_t filter_mode, struct pico_tree *MCASTFilter)
 {
-  struct pico_mcast_group *g = NULL, test = {0};
-  struct pico_ipv4_link *link = NULL;
+    struct pico_mcast_group *g = NULL, test = {
+        0
+    };
+    struct pico_ipv4_link *link = NULL;
 
-  if (mcast_link)
-    link = pico_ipv4_link_get(mcast_link);
-  else
-    link = mcast_default_link;
+    if (mcast_link)
+        link = pico_ipv4_link_get(mcast_link);
+    else
+        link = mcast_default_link;
+
+    test.mcast_addr = *mcast_group;
+    g = pico_tree_findKey(link->MCASTGroups, &test);
+    if (g) {
+        if (reference_count)
+            g->reference_count++;
 
-  test.mcast_addr = *mcast_group;
-  g = pico_tree_findKey(link->MCASTGroups, &test);
-  if (g) {
-    if (reference_count)
-      g->reference_count++;
-    pico_igmp_state_change(mcast_link, mcast_group, filter_mode, MCASTFilter, PICO_IGMP_STATE_UPDATE);
-  } else {
-    g = pico_zalloc(sizeof(struct pico_mcast_group));
-    if (!g) {
-      pico_err = PICO_ERR_ENOMEM;
-      return -1;
+        pico_igmp_state_change(mcast_link, mcast_group, filter_mode, MCASTFilter, PICO_IGMP_STATE_UPDATE);
+    } else {
+        g = pico_zalloc(sizeof(struct pico_mcast_group));
+        if (!g) {
+            pico_err = PICO_ERR_ENOMEM;
+            return -1;
+        }
+
+        /* "non-existent" state of filter mode INCLUDE and empty source list */
+        g->filter_mode = PICO_IP_MULTICAST_INCLUDE;
+        g->reference_count = 1;
+        g->mcast_addr = *mcast_group;
+        g->MCASTSources.root = &LEAF;
+        g->MCASTSources.compare = ipv4_mcast_sources_cmp;
+        pico_tree_insert(link->MCASTGroups, g);
+        pico_igmp_state_change(mcast_link, mcast_group, filter_mode, MCASTFilter, PICO_IGMP_STATE_CREATE);
     }
-    /* "non-existent" state of filter mode INCLUDE and empty source list */
-    g->filter_mode = PICO_IP_MULTICAST_INCLUDE;
-    g->reference_count = 1;
-    g->mcast_addr = *mcast_group;
-    g->MCASTSources.root = &LEAF;
-    g->MCASTSources.compare = ipv4_mcast_sources_cmp;
-    pico_tree_insert(link->MCASTGroups, g);
-    pico_igmp_state_change(mcast_link, mcast_group, filter_mode, MCASTFilter, PICO_IGMP_STATE_CREATE);
-  }
-  if (mcast_group_update(g, MCASTFilter, filter_mode) < 0)
-    return -1;
-  pico_ipv4_mcast_print_groups(link);
-  return 0;
+
+    if (mcast_group_update(g, MCASTFilter, filter_mode) < 0)
+        return -1;
+
+    pico_ipv4_mcast_print_groups(link);
+    return 0;
 }
 
 int pico_ipv4_mcast_leave(struct pico_ip4 *mcast_link, struct pico_ip4 *mcast_group, uint8_t reference_count, uint8_t filter_mode, struct pico_tree *MCASTFilter)
 {
 
-  struct pico_mcast_group *g = NULL, test = {0};
-  struct pico_ipv4_link *link = NULL;
-  struct pico_tree_node *index = NULL, *_tmp = NULL;
-  struct pico_ip4 *source = NULL;
+    struct pico_mcast_group *g = NULL, test = {
+        0
+    };
+    struct pico_ipv4_link *link = NULL;
+    struct pico_tree_node *index = NULL, *_tmp = NULL;
+    struct pico_ip4 *source = NULL;
 
-  if (mcast_link)
-    link = pico_ipv4_link_get(mcast_link);
-  else
-    link = mcast_default_link;
+    if (mcast_link)
+        link = pico_ipv4_link_get(mcast_link);
+    else
+        link = mcast_default_link;
 
-  test.mcast_addr = *mcast_group;
-  g = pico_tree_findKey(link->MCASTGroups, &test);
-  if (!g) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  } else {
-    if (reference_count && (--(g->reference_count) < 1)) {
-      pico_igmp_state_change(mcast_link, mcast_group, filter_mode, MCASTFilter, PICO_IGMP_STATE_DELETE);
-      /* cleanup filter */
-      pico_tree_foreach_safe(index, &g->MCASTSources, _tmp)
-      {
-        source = index->keyValue;
-        pico_tree_delete(&g->MCASTSources, source);
-        pico_free(source);
-      }
-      pico_tree_delete(link->MCASTGroups, g);
-      pico_free(g); 
+    test.mcast_addr = *mcast_group;
+    g = pico_tree_findKey(link->MCASTGroups, &test);
+    if (!g) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
     } else {
-      pico_igmp_state_change(mcast_link, mcast_group, filter_mode, MCASTFilter, PICO_IGMP_STATE_UPDATE);
-      if (mcast_group_update(g, MCASTFilter, filter_mode) < 0)
-        return -1;
+        if (reference_count && (--(g->reference_count) < 1)) {
+            pico_igmp_state_change(mcast_link, mcast_group, filter_mode, MCASTFilter, PICO_IGMP_STATE_DELETE);
+            /* cleanup filter */
+            pico_tree_foreach_safe(index, &g->MCASTSources, _tmp)
+            {
+                source = index->keyValue;
+                pico_tree_delete(&g->MCASTSources, source);
+                pico_free(source);
+            }
+            pico_tree_delete(link->MCASTGroups, g);
+            pico_free(g);
+        } else {
+            pico_igmp_state_change(mcast_link, mcast_group, filter_mode, MCASTFilter, PICO_IGMP_STATE_UPDATE);
+            if (mcast_group_update(g, MCASTFilter, filter_mode) < 0)
+                return -1;
+        }
     }
-  }
 
-  pico_ipv4_mcast_print_groups(link);
-  return 0;
+    pico_ipv4_mcast_print_groups(link);
+    return 0;
 }
 
 struct pico_ipv4_link *pico_ipv4_get_default_mcastlink(void)
 {
-  return mcast_default_link;
+    return mcast_default_link;
 }
 
 static int pico_ipv4_mcast_filter(struct pico_frame *f)
 {
-  struct pico_ipv4_link *link = NULL;
-  struct pico_tree_node *index = NULL, *index2 = NULL;
-  struct pico_mcast_group *g = NULL, test = {0};
-  struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    struct pico_ipv4_link *link = NULL;
+    struct pico_tree_node *index = NULL, *index2 = NULL;
+    struct pico_mcast_group *g = NULL, test = {
+        0
+    };
+    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
 
-  test.mcast_addr = hdr->dst; 
+    test.mcast_addr = hdr->dst;
 
-  pico_tree_foreach(index, &Tree_dev_link) 
-  {
-    link = index->keyValue;
-    g = pico_tree_findKey(link->MCASTGroups, &test);
-    if (g) {
-      if (f->dev == link->dev) {
-        ip_mcast_dbg("MCAST: IP %08X is group member of current link %s\n", hdr->dst.addr, f->dev->name);
-        /* perform source filtering */
-        switch (g->filter_mode)
-        {
-          case PICO_IP_MULTICAST_INCLUDE:
-            pico_tree_foreach(index2, &g->MCASTSources)
-            {
-              if (hdr->src.addr == ((struct pico_ip4 *)index2->keyValue)->addr) {
-                ip_mcast_dbg("MCAST: IP %08X in included interface source list\n", hdr->src.addr);
-                return 0;
-              }
+    pico_tree_foreach(index, &Tree_dev_link)
+    {
+        link = index->keyValue;
+        g = pico_tree_findKey(link->MCASTGroups, &test);
+        if (g) {
+            if (f->dev == link->dev) {
+                ip_mcast_dbg("MCAST: IP %08X is group member of current link %s\n", hdr->dst.addr, f->dev->name);
+                /* perform source filtering */
+                switch (g->filter_mode)
+                {
+                case PICO_IP_MULTICAST_INCLUDE:
+                    pico_tree_foreach(index2, &g->MCASTSources)
+                    {
+                        if (hdr->src.addr == ((struct pico_ip4 *)index2->keyValue)->addr) {
+                            ip_mcast_dbg("MCAST: IP %08X in included interface source list\n", hdr->src.addr);
+                            return 0;
+                        }
+                    }
+                    ip_mcast_dbg("MCAST: IP %08X NOT in included interface source list\n", hdr->src.addr);
+                    return -1;
+
+                case PICO_IP_MULTICAST_EXCLUDE:
+                    pico_tree_foreach(index2, &g->MCASTSources)
+                    {
+                        if (hdr->src.addr == ((struct pico_ip4 *)index2->keyValue)->addr) {
+                            ip_mcast_dbg("MCAST: IP %08X in excluded interface source list\n", hdr->src.addr);
+                            return -1;
+                        }
+                    }
+                    ip_mcast_dbg("MCAST: IP %08X NOT in excluded interface source list\n", hdr->src.addr);
+                    return 0;
+
+                default:
+                    return -1;
+                }
+            } else {
+                ip_mcast_dbg("MCAST: IP %08X is group member of different link %s\n", hdr->dst.addr, link->dev->name);
             }
-            ip_mcast_dbg("MCAST: IP %08X NOT in included interface source list\n", hdr->src.addr);
-            return -1;
-
-          case PICO_IP_MULTICAST_EXCLUDE:
-            pico_tree_foreach(index2, &g->MCASTSources)
-            {
-              if (hdr->src.addr == ((struct pico_ip4 *)index2->keyValue)->addr) {
-                ip_mcast_dbg("MCAST: IP %08X in excluded interface source list\n", hdr->src.addr);
-                return -1;
-              }
-            }
-            ip_mcast_dbg("MCAST: IP %08X NOT in excluded interface source list\n", hdr->src.addr);
-            return 0;
-
-          default:
-            return -1;
+        } else {
+            ip_mcast_dbg("MCAST: IP %08X is not a group member of link %s\n", hdr->dst.addr, f->dev->name);
         }
-      } else {
-        ip_mcast_dbg("MCAST: IP %08X is group member of different link %s\n", hdr->dst.addr, link->dev->name);
-      }
-    } else {
-      ip_mcast_dbg("MCAST: IP %08X is not a group member of link %s\n", hdr->dst.addr, f->dev->name);
     }
-  }
-  return -1;
+    return -1;
 }
 
-#else 
+#else
 
 int pico_ipv4_mcast_join(struct pico_ip4 *mcast_link, struct pico_ip4 *mcast_group, uint8_t reference_count, uint8_t filter_mode, struct pico_tree *MCASTFilter)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 int pico_ipv4_mcast_leave(struct pico_ip4 *mcast_link, struct pico_ip4 *mcast_group, uint8_t reference_count, uint8_t filter_mode, struct pico_tree *MCASTFilter)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 struct pico_ipv4_link *pico_ipv4_get_default_mcastlink(void)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return NULL;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return NULL;
 }
 #endif /* PICO_SUPPORT_MCAST */
 
 #ifdef DEBUG_ROUTE
 static void dbg_route(void)
 {
-  struct pico_ipv4_route *r;
-  struct pico_tree_node * index;
-  pico_tree_foreach(index,&Routes){
-    r = index->keyValue;
-    dbg("Route to %08x/%08x, gw %08x, dev: %s, metric: %d\n", r->dest.addr, r->netmask.addr, r->gateway.addr, r->link->dev->name, r->metric);
-  }
+    struct pico_ipv4_route *r;
+    struct pico_tree_node *index;
+    pico_tree_foreach(index, &Routes){
+        r = index->keyValue;
+        dbg("Route to %08x/%08x, gw %08x, dev: %s, metric: %d\n", r->dest.addr, r->netmask.addr, r->gateway.addr, r->link->dev->name, r->metric);
+    }
 }
 #else
-#define dbg_route() do{ }while(0)
+#define dbg_route() do { } while(0)
 #endif
 
 int pico_ipv4_frame_push(struct pico_frame *f, struct pico_ip4 *dst, uint8_t proto)
 {
 
-  struct pico_ipv4_route *route;
-  struct pico_ipv4_link *link;
-  struct pico_ipv4_hdr *hdr;
-  uint8_t ttl = PICO_IPV4_DEFAULT_TTL;
-  uint8_t vhl = 0x45; /* version 4, header length 20 */
-  static uint16_t ipv4_progressive_id = 0x91c0;
+    struct pico_ipv4_route *route;
+    struct pico_ipv4_link *link;
+    struct pico_ipv4_hdr *hdr;
+    uint8_t ttl = PICO_IPV4_DEFAULT_TTL;
+    uint8_t vhl = 0x45; /* version 4, header length 20 */
+    static uint16_t ipv4_progressive_id = 0x91c0;
 #ifdef PICO_SUPPORT_MCAST
-  struct pico_tree_node *index;
+    struct pico_tree_node *index;
 #endif
 
-  if(!f || !dst) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-  if (!hdr) {
-    dbg("IP header error\n");
-    pico_err = PICO_ERR_EINVAL;
-    goto drop;
-  }
+    if(!f || !dst) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    if (!hdr) {
+        dbg("IP header error\n");
+        pico_err = PICO_ERR_EINVAL;
+        goto drop;
+    }
 
-  if (dst->addr == 0) {
-    dbg("IP destination addr error\n");
-    pico_err = PICO_ERR_EINVAL;
-    goto drop;
-  }
+    if (dst->addr == 0) {
+        dbg("IP destination addr error\n");
+        pico_err = PICO_ERR_EINVAL;
+        goto drop;
+    }
 
-  route = route_find(dst);
-  if (!route) {
-    //dbg("Route to %08x not found.\n", long_be(dst->addr));
-      
-    
-    pico_err = PICO_ERR_EHOSTUNREACH;
-    goto drop;
-  } else {
-    link = route->link;
+    route = route_find(dst);
+    if (!route) {
+        /* dbg("Route to %08x not found.\n", long_be(dst->addr)); */
+
+
+        pico_err = PICO_ERR_EHOSTUNREACH;
+        goto drop;
+    } else {
+        link = route->link;
 #ifdef PICO_SUPPORT_MCAST
-    if (pico_ipv4_is_multicast(dst->addr)) { /* if multicast */
-      switch (proto) {
-        case PICO_PROTO_UDP:
-          if(pico_udp_get_mc_ttl(f->sock, &ttl) < 0)
-            ttl = PICO_IP_DEFAULT_MULTICAST_TTL;
-          break;
-        case PICO_PROTO_IGMP:
-          vhl = 0x46; /* header length 24 */
-          ttl = 1;
-          /* router alert (RFC 2113) */ 
-          hdr->options[0] = 0x94;
-          hdr->options[1] = 0x04;
-          hdr->options[2] = 0x00;
-          hdr->options[3] = 0x00;
-          if (f->dev && link->dev != f->dev) { /* default link is not requested link */
-            pico_tree_foreach(index, &Tree_dev_link) {
-              link = index->keyValue;
-              if (link->dev == f->dev)
+        if (pico_ipv4_is_multicast(dst->addr)) { /* if multicast */
+            switch (proto) {
+            case PICO_PROTO_UDP:
+                if(pico_udp_get_mc_ttl(f->sock, &ttl) < 0)
+                    ttl = PICO_IP_DEFAULT_MULTICAST_TTL;
+
                 break;
+            case PICO_PROTO_IGMP:
+                vhl = 0x46; /* header length 24 */
+                ttl = 1;
+                /* router alert (RFC 2113) */
+                hdr->options[0] = 0x94;
+                hdr->options[1] = 0x04;
+                hdr->options[2] = 0x00;
+                hdr->options[3] = 0x00;
+                if (f->dev && link->dev != f->dev) { /* default link is not requested link */
+                    pico_tree_foreach(index, &Tree_dev_link) {
+                        link = index->keyValue;
+                        if (link->dev == f->dev)
+                            break;
+                    }
+                }
+
+                break;
+            default:
+                ttl = PICO_IPV4_DEFAULT_TTL;
             }
-          }
-          break;
-        default:
-          ttl = PICO_IPV4_DEFAULT_TTL;
-      }
+        }
+
+#endif
     }
-#endif
-  }
 
-  hdr->vhl = vhl;
-  hdr->len = short_be((uint16_t)(f->transport_len + f->net_len));
-  if (f->transport_hdr != f->payload)
-    ipv4_progressive_id++;
-  hdr->id = short_be(ipv4_progressive_id);
-  hdr->dst.addr = dst->addr;
-  hdr->src.addr = link->address.addr;
-  hdr->ttl = ttl;
-  hdr->proto = proto;
-  hdr->frag = short_be(PICO_IPV4_DONTFRAG);
+    hdr->vhl = vhl;
+    hdr->len = short_be((uint16_t)(f->transport_len + f->net_len));
+    if (f->transport_hdr != f->payload)
+        ipv4_progressive_id++;
+
+    hdr->id = short_be(ipv4_progressive_id);
+    hdr->dst.addr = dst->addr;
+    hdr->src.addr = link->address.addr;
+    hdr->ttl = ttl;
+    hdr->proto = proto;
+    hdr->frag = short_be(PICO_IPV4_DONTFRAG);
 #ifdef PICO_SUPPORT_IPFRAG
 #  ifdef PICO_SUPPORT_UDP
-  if (proto == PICO_PROTO_UDP) {
-    /* first fragment, can not use transport_len to calculate IP length */
-    if (f->transport_hdr != f->payload)
-      hdr->len = short_be((uint16_t)(f->payload_len + sizeof(struct pico_udp_hdr) + f->net_len));
-    /* set fragmentation flags and offset calculated in socket layer */
-    hdr->frag = f->frag;
-  }
+    if (proto == PICO_PROTO_UDP) {
+        /* first fragment, can not use transport_len to calculate IP length */
+        if (f->transport_hdr != f->payload)
+            hdr->len = short_be((uint16_t)(f->payload_len + sizeof(struct pico_udp_hdr) + f->net_len));
+
+        /* set fragmentation flags and offset calculated in socket layer */
+        hdr->frag = f->frag;
+    }
+
 #  endif /* PICO_SUPPORT_UDP */
 #endif /* PICO_SUPPORT_IPFRAG */
-  pico_ipv4_checksum(f);
+    pico_ipv4_checksum(f);
 
-  if (f->sock && f->sock->dev){
-    //if the socket has its device set, use that (currently used for DHCP)
-    f->dev = f->sock->dev;
-  } else {
-    f->dev = link->dev;
-  }
+    if (f->sock && f->sock->dev) {
+        /* if the socket has its device set, use that (currently used for DHCP) */
+        f->dev = f->sock->dev;
+    } else {
+        f->dev = link->dev;
+    }
 
 #ifdef PICO_SUPPORT_MCAST
-  if (pico_ipv4_is_multicast(hdr->dst.addr)) {
-    struct pico_frame *cpy;
-    /* Sending UDP multicast datagram, am I member? If so, loopback copy */
-    if ((proto != PICO_PROTO_IGMP) && (pico_ipv4_mcast_filter(f) == 0)) {
-      ip_mcast_dbg("MCAST: sender is member of group, loopback copy\n");
-      cpy = pico_frame_copy(f);
-      pico_enqueue(&in, cpy);
+    if (pico_ipv4_is_multicast(hdr->dst.addr)) {
+        struct pico_frame *cpy;
+        /* Sending UDP multicast datagram, am I member? If so, loopback copy */
+        if ((proto != PICO_PROTO_IGMP) && (pico_ipv4_mcast_filter(f) == 0)) {
+            ip_mcast_dbg("MCAST: sender is member of group, loopback copy\n");
+            cpy = pico_frame_copy(f);
+            pico_enqueue(&in, cpy);
+        }
     }
-  }
+
 #endif
 
-  if(pico_ipv4_link_get(&hdr->dst)){
-    //it's our own IP
-    return pico_enqueue(&in, f);
-  }else{
-    /* TODO: Check if there are members subscribed here */
-    return pico_enqueue(&out, f);
-  }
+    if(pico_ipv4_link_get(&hdr->dst)) {
+        /* it's our own IP */
+        return pico_enqueue(&in, f);
+    }else{
+        /* TODO: Check if there are members subscribed here */
+        return pico_enqueue(&out, f);
+    }
 
 drop:
-  pico_frame_discard(f);
-  return -1;
+    pico_frame_discard(f);
+    return -1;
 }
 
 
 static int pico_ipv4_frame_sock_push(struct pico_protocol *self, struct pico_frame *f)
 {
-	struct pico_ip4 *dst;
-  struct pico_remote_duple *remote_duple = (struct pico_remote_duple *) f->info;
-  IGNORE_PARAMETER(self);
+    struct pico_ip4 *dst;
+    struct pico_remote_duple *remote_duple = (struct pico_remote_duple *) f->info;
+    IGNORE_PARAMETER(self);
 
-  if (!f->sock) {
-    pico_frame_discard(f);
-    return -1;
-  }
+    if (!f->sock) {
+        pico_frame_discard(f);
+        return -1;
+    }
 
-  if (remote_duple) {
-    dst = &remote_duple->remote_addr.ip4;
-  } else {
-    dst = &f->sock->remote_addr.ip4;
-  }
+    if (remote_duple) {
+        dst = &remote_duple->remote_addr.ip4;
+    } else {
+        dst = &f->sock->remote_addr.ip4;
+    }
 
-  return pico_ipv4_frame_push(f, dst, (uint8_t)f->sock->proto->proto_number);
+    return pico_ipv4_frame_push(f, dst, (uint8_t)f->sock->proto->proto_number);
 }
 
 
 int pico_ipv4_route_add(struct pico_ip4 address, struct pico_ip4 netmask, struct pico_ip4 gateway, int metric, struct pico_ipv4_link *link)
 {
-  struct pico_ipv4_route test, *new;
-  test.dest.addr = address.addr;
-  test.netmask.addr = netmask.addr;
-  test.metric = (uint32_t)metric;
+    struct pico_ipv4_route test, *new;
+    test.dest.addr = address.addr;
+    test.netmask.addr = netmask.addr;
+    test.metric = (uint32_t)metric;
+
+    if(pico_tree_findKey(&Routes, &test)) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    new = pico_zalloc(sizeof(struct pico_ipv4_route));
+    if (!new) {
+        pico_err = PICO_ERR_ENOMEM;
+        return -1;
+    }
 
-  if(pico_tree_findKey(&Routes,&test)){
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  
-  new = pico_zalloc(sizeof(struct pico_ipv4_route));
-  if (!new) {
-    pico_err = PICO_ERR_ENOMEM;
-    return -1;
-  }
-  new->dest.addr = address.addr;
-  new->netmask.addr = netmask.addr;
-  new->gateway.addr = gateway.addr;
-  new->metric = (uint32_t)metric;
-  if (gateway.addr == 0) {
-    /* No gateway provided, use the link */
-    new->link = link;
-  } else {
-    struct pico_ipv4_route *r = route_find(&gateway);
-    if (!r ) { /* Specified Gateway is unreachable */
-      pico_err = PICO_ERR_EHOSTUNREACH;
-      pico_free(new);
-      return -1;
+    new->dest.addr = address.addr;
+    new->netmask.addr = netmask.addr;
+    new->gateway.addr = gateway.addr;
+    new->metric = (uint32_t)metric;
+    if (gateway.addr == 0) {
+        /* No gateway provided, use the link */
+        new->link = link;
+    } else {
+        struct pico_ipv4_route *r = route_find(&gateway);
+        if (!r ) { /* Specified Gateway is unreachable */
+            pico_err = PICO_ERR_EHOSTUNREACH;
+            pico_free(new);
+            return -1;
+        }
+
+        if (r->gateway.addr) { /* Specified Gateway is not a neighbor */
+            pico_err = PICO_ERR_ENETUNREACH;
+            pico_free(new);
+            return -1;
+        }
+
+        new->link = r->link;
     }
-    if (r->gateway.addr) { /* Specified Gateway is not a neighbor */
-      pico_err = PICO_ERR_ENETUNREACH;
-      pico_free(new);
-      return -1;
+
+    if (!new->link) {
+        pico_err = PICO_ERR_EINVAL;
+        pico_free(new);
+        return -1;
     }
-    new->link = r->link;
-  }
-  if (!new->link) {
-      pico_err = PICO_ERR_EINVAL;
-      pico_free(new);
-      return -1;
-  }
 
-  pico_tree_insert(&Routes,new);
-  //dbg_route();
-  return 0;
+    pico_tree_insert(&Routes, new);
+    /* dbg_route(); */
+    return 0;
 }
 
 int pico_ipv4_route_del(struct pico_ip4 address, struct pico_ip4 netmask, int metric)
 {
-  struct pico_ipv4_route test, *found;
+    struct pico_ipv4_route test, *found;
 
-  test.dest.addr = address.addr;
-  test.netmask.addr = netmask.addr;
-  test.metric = (uint32_t)metric;
+    test.dest.addr = address.addr;
+    test.netmask.addr = netmask.addr;
+    test.metric = (uint32_t)metric;
 
-  found = pico_tree_findKey(&Routes,&test);
-  if (found) {
+    found = pico_tree_findKey(&Routes, &test);
+    if (found) {
 
-    pico_tree_delete(&Routes,found);
-    pico_free(found);
+        pico_tree_delete(&Routes, found);
+        pico_free(found);
 
-    //dbg_route();
-    return 0;
-  }
-  pico_err = PICO_ERR_EINVAL;
-  return -1;
+        /* dbg_route(); */
+        return 0;
+    }
+
+    pico_err = PICO_ERR_EINVAL;
+    return -1;
 }
 
 
 int pico_ipv4_link_add(struct pico_device *dev, struct pico_ip4 address, struct pico_ip4 netmask)
 {
-  struct pico_ipv4_link test, *new;
-  struct pico_ip4 network, gateway;
-  char ipstr[30];
+    struct pico_ipv4_link test, *new;
+    struct pico_ip4 network, gateway;
+    char ipstr[30];
+
+    if(!dev) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  if(!dev) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  test.address.addr = address.addr;
-  test.netmask.addr = netmask.addr;
-  test.dev = dev;
-  /** XXX: Valid netmask / unicast address test **/
+    test.address.addr = address.addr;
+    test.netmask.addr = netmask.addr;
+    test.dev = dev;
+    /** XXX: Valid netmask / unicast address test **/
 
-  if(pico_tree_findKey(&Tree_dev_link, &test)) {
-    dbg("IPv4: Trying to assign an invalid address (in use)\n");
-    pico_err = PICO_ERR_EADDRINUSE;
-    return -1;
-  }
+    if(pico_tree_findKey(&Tree_dev_link, &test)) {
+        dbg("IPv4: Trying to assign an invalid address (in use)\n");
+        pico_err = PICO_ERR_EADDRINUSE;
+        return -1;
+    }
 
-  /** XXX: Check for network already in use (e.g. trying to assign 10.0.0.1/24 where 10.1.0.1/8 is in use) **/
-  new = pico_zalloc(sizeof(struct pico_ipv4_link));
-  if (!new) {
-    dbg("IPv4: Out of memory!\n");
-    pico_err = PICO_ERR_ENOMEM;
-    return -1;
-  }
-  new->address.addr = address.addr;
-  new->netmask.addr = netmask.addr;
-  new->dev = dev;
+    /** XXX: Check for network already in use (e.g. trying to assign 10.0.0.1/24 where 10.1.0.1/8 is in use) **/
+    new = pico_zalloc(sizeof(struct pico_ipv4_link));
+    if (!new) {
+        dbg("IPv4: Out of memory!\n");
+        pico_err = PICO_ERR_ENOMEM;
+        return -1;
+    }
+
+    new->address.addr = address.addr;
+    new->netmask.addr = netmask.addr;
+    new->dev = dev;
 #ifdef PICO_SUPPORT_MCAST
-  new->MCASTGroups = pico_zalloc(sizeof(struct pico_tree));
-  if (!new->MCASTGroups) {
-    pico_free(new);
-    dbg("IPv4: Out of memory!\n");
-    pico_err = PICO_ERR_ENOMEM;
-    return -1;
-  }
+    new->MCASTGroups = pico_zalloc(sizeof(struct pico_tree));
+    if (!new->MCASTGroups) {
+        pico_free(new);
+        dbg("IPv4: Out of memory!\n");
+        pico_err = PICO_ERR_ENOMEM;
+        return -1;
+    }
 
-  new->MCASTGroups->root = &LEAF;
-  new->MCASTGroups->compare = ipv4_mcast_groups_cmp;
-  new->mcast_compatibility = PICO_IGMPV3; /* default RFC 3376 $7.2.1 */
-  new->mcast_last_query_interval = PICO_IGMP_QUERY_INTERVAL;
+    new->MCASTGroups->root = &LEAF;
+    new->MCASTGroups->compare = ipv4_mcast_groups_cmp;
+    new->mcast_compatibility = PICO_IGMPV3; /* default RFC 3376 $7.2.1 */
+    new->mcast_last_query_interval = PICO_IGMP_QUERY_INTERVAL;
 #endif
 
-  pico_tree_insert(&Tree_dev_link, new);
+    pico_tree_insert(&Tree_dev_link, new);
 #ifdef PICO_SUPPORT_MCAST
-  do {
-    struct pico_ip4 mcast_all_hosts, mcast_addr, mcast_nm, mcast_gw;
-    if (!mcast_default_link) {
-      mcast_addr.addr = long_be(0xE0000000); /* 224.0.0.0 */
-      mcast_nm.addr = long_be(0xF0000000); /* 15.0.0.0 */
-      mcast_gw.addr = long_be(0x00000000);
-      mcast_default_link = new;
-      pico_ipv4_route_add(mcast_addr, mcast_nm, mcast_gw, 1, new);
-    }
-    mcast_all_hosts.addr = PICO_MCAST_ALL_HOSTS;
-    pico_ipv4_mcast_join(&address, &mcast_all_hosts, 1, PICO_IP_MULTICAST_EXCLUDE, NULL);
-  } while(0);
+    do {
+        struct pico_ip4 mcast_all_hosts, mcast_addr, mcast_nm, mcast_gw;
+        if (!mcast_default_link) {
+            mcast_addr.addr = long_be(0xE0000000); /* 224.0.0.0 */
+            mcast_nm.addr = long_be(0xF0000000); /* 15.0.0.0 */
+            mcast_gw.addr = long_be(0x00000000);
+            mcast_default_link = new;
+            pico_ipv4_route_add(mcast_addr, mcast_nm, mcast_gw, 1, new);
+        }
+
+        mcast_all_hosts.addr = PICO_MCAST_ALL_HOSTS;
+        pico_ipv4_mcast_join(&address, &mcast_all_hosts, 1, PICO_IP_MULTICAST_EXCLUDE, NULL);
+    } while(0);
 #endif
 
-  network.addr = address.addr & netmask.addr;
-  gateway.addr = 0U;
-  pico_ipv4_route_add(network, netmask, gateway, 1, new);
-  pico_ipv4_to_string(ipstr, new->address.addr);
-  dbg("Assigned ipv4 %s to device %s\n", ipstr, new->dev->name);
-  return 0;
+    network.addr = address.addr & netmask.addr;
+    gateway.addr = 0U;
+    pico_ipv4_route_add(network, netmask, gateway, 1, new);
+    pico_ipv4_to_string(ipstr, new->address.addr);
+    dbg("Assigned ipv4 %s to device %s\n", ipstr, new->dev->name);
+    return 0;
 }
 
 static int pico_ipv4_cleanup_routes(struct pico_ipv4_link *link)
 {
-  struct pico_tree_node *index = NULL, *tmp = NULL;
-  struct pico_ipv4_route *route = NULL;
+    struct pico_tree_node *index = NULL, *tmp = NULL;
+    struct pico_ipv4_route *route = NULL;
 
-  pico_tree_foreach_safe(index, &Routes, tmp)
-  {
-    route = index->keyValue;
-    if (link == route->link)
-      pico_ipv4_route_del(route->dest, route->netmask, (int)route->metric);
-  }
-  return 0;
+    pico_tree_foreach_safe(index, &Routes, tmp)
+    {
+        route = index->keyValue;
+        if (link == route->link)
+            pico_ipv4_route_del(route->dest, route->netmask, (int)route->metric);
+    }
+    return 0;
 }
 
 int pico_ipv4_link_del(struct pico_device *dev, struct pico_ip4 address)
 {
-  struct pico_ipv4_link test, *found;
+    struct pico_ipv4_link test, *found;
+
+    if(!dev) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  if(!dev) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  test.address.addr = address.addr;
-  test.dev = dev;
-  found = pico_tree_findKey(&Tree_dev_link, &test);
-  if (!found) {
-    pico_err = PICO_ERR_ENXIO;
-    return -1;
-  }
+    test.address.addr = address.addr;
+    test.dev = dev;
+    found = pico_tree_findKey(&Tree_dev_link, &test);
+    if (!found) {
+        pico_err = PICO_ERR_ENXIO;
+        return -1;
+    }
 
 #ifdef PICO_SUPPORT_MCAST
-  do {
-    struct pico_ip4 mcast_all_hosts, mcast_addr, mcast_nm;
-    struct pico_mcast_group *g = NULL;
-    struct pico_tree_node * index, * _tmp;
-    if (found == mcast_default_link) {
-      mcast_addr.addr = long_be(0xE0000000); /* 224.0.0.0 */
-      mcast_nm.addr = long_be(0xF0000000); /* 15.0.0.0 */
-      mcast_default_link = NULL;
-      pico_ipv4_route_del(mcast_addr, mcast_nm, 1);
-    }
-    mcast_all_hosts.addr = PICO_MCAST_ALL_HOSTS;
-    pico_ipv4_mcast_leave(&address, &mcast_all_hosts, 1, PICO_IP_MULTICAST_EXCLUDE, NULL);
-    pico_tree_foreach_safe(index,found->MCASTGroups, _tmp)
-    {
-      g = index->keyValue;
-      pico_tree_delete(found->MCASTGroups, g);
-      pico_free(g);
-    }
-  } while(0);
+    do {
+        struct pico_ip4 mcast_all_hosts, mcast_addr, mcast_nm;
+        struct pico_mcast_group *g = NULL;
+        struct pico_tree_node *index, *_tmp;
+        if (found == mcast_default_link) {
+            mcast_addr.addr = long_be(0xE0000000); /* 224.0.0.0 */
+            mcast_nm.addr = long_be(0xF0000000); /* 15.0.0.0 */
+            mcast_default_link = NULL;
+            pico_ipv4_route_del(mcast_addr, mcast_nm, 1);
+        }
+
+        mcast_all_hosts.addr = PICO_MCAST_ALL_HOSTS;
+        pico_ipv4_mcast_leave(&address, &mcast_all_hosts, 1, PICO_IP_MULTICAST_EXCLUDE, NULL);
+        pico_tree_foreach_safe(index, found->MCASTGroups, _tmp)
+        {
+            g = index->keyValue;
+            pico_tree_delete(found->MCASTGroups, g);
+            pico_free(g);
+        }
+    } while(0);
 #endif
 
-  pico_ipv4_cleanup_routes(found);
-  pico_tree_delete(&Tree_dev_link, found);
-  pico_free(found);
+    pico_ipv4_cleanup_routes(found);
+    pico_tree_delete(&Tree_dev_link, found);
+    pico_free(found);
 
-  return 0;
+    return 0;
 }
 
 
 struct pico_ipv4_link *pico_ipv4_link_get(struct pico_ip4 *address)
 {
-  struct pico_ipv4_link test = {0}, *found = NULL;
-  test.address.addr = address->addr;
+    struct pico_ipv4_link test = {
+        0
+    }, *found = NULL;
+    test.address.addr = address->addr;
 
-  found = pico_tree_findKey(&Tree_dev_link, &test);
-  if (!found)
-    return NULL;
-  else
-    return found;
+    found = pico_tree_findKey(&Tree_dev_link, &test);
+    if (!found)
+        return NULL;
+    else
+        return found;
 }
 
 struct pico_ipv4_link *pico_ipv4_link_by_dev(struct pico_device *dev)
 {
-  struct pico_tree_node *index = NULL;
-  struct pico_ipv4_link *link = NULL;
+    struct pico_tree_node *index = NULL;
+    struct pico_ipv4_link *link = NULL;
 
-  pico_tree_foreach(index, &Tree_dev_link) 
-  {
-    link = index->keyValue;
-    if (link->dev == dev)
-      return link;
-  }
-  return NULL;
+    pico_tree_foreach(index, &Tree_dev_link)
+    {
+        link = index->keyValue;
+        if (link->dev == dev)
+            return link;
+    }
+    return NULL;
 }
 
 struct pico_ipv4_link *pico_ipv4_link_by_dev_next(struct pico_device *dev, struct pico_ipv4_link *last)
 {
-  struct pico_tree_node *index = NULL;
-  struct pico_ipv4_link *link = NULL;
-  int valid = 0;
+    struct pico_tree_node *index = NULL;
+    struct pico_ipv4_link *link = NULL;
+    int valid = 0;
 
-  if (last == NULL)
-    valid = 1;
+    if (last == NULL)
+        valid = 1;
 
-  pico_tree_foreach(index, &Tree_dev_link) 
-  {
-    link = index->keyValue;
-    if (link->dev == dev) {
-      if (last == link)
-        valid = 1;
-      else if (valid > 0)
-        return link;
+    pico_tree_foreach(index, &Tree_dev_link)
+    {
+        link = index->keyValue;
+        if (link->dev == dev) {
+            if (last == link)
+                valid = 1;
+            else if (valid > 0)
+                return link;
+        }
     }
-  }
-  return NULL;
+    return NULL;
 }
 
 struct pico_device *pico_ipv4_link_find(struct pico_ip4 *address)
 {
-  struct pico_ipv4_link test, *found;
-  if(!address) {
-    pico_err = PICO_ERR_EINVAL;
-    return NULL;
-  }
-  test.dev = NULL;
-  test.address.addr = address->addr;
-  found = pico_tree_findKey(&Tree_dev_link, &test);
-  if (!found) {
-    pico_err = PICO_ERR_ENXIO;
-    return NULL;
-  }
-  return found->dev;
+    struct pico_ipv4_link test, *found;
+    if(!address) {
+        pico_err = PICO_ERR_EINVAL;
+        return NULL;
+    }
+
+    test.dev = NULL;
+    test.address.addr = address->addr;
+    found = pico_tree_findKey(&Tree_dev_link, &test);
+    if (!found) {
+        pico_err = PICO_ERR_ENXIO;
+        return NULL;
+    }
+
+    return found->dev;
 }
 
 int pico_ipv4_rebound(struct pico_frame *f)
 {
-  struct pico_ip4 dst;
-  struct pico_ipv4_hdr *hdr;
-  if(!f) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    struct pico_ip4 dst;
+    struct pico_ipv4_hdr *hdr;
+    if(!f) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-  if (!hdr) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  dst.addr = hdr->src.addr;
-  return pico_ipv4_frame_push(f, &dst, hdr->proto);
+    hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    if (!hdr) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    dst.addr = hdr->src.addr;
+    return pico_ipv4_frame_push(f, &dst, hdr->proto);
 }
 
 static int pico_ipv4_forward(struct pico_frame *f)
 {
-  struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *)f->net_hdr;
-  struct pico_ipv4_route *rt;
-  if (!hdr) {
-    return -1;
-  }
+    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *)f->net_hdr;
+    struct pico_ipv4_route *rt;
+    if (!hdr) {
+        return -1;
+    }
 
-  rt = route_find(&hdr->dst);
-  if (!rt) {
-    pico_notify_dest_unreachable(f);
-    return -1;
-  }
+    rt = route_find(&hdr->dst);
+    if (!rt) {
+        pico_notify_dest_unreachable(f);
+        return -1;
+    }
 
-  if (f->dev == rt->link->dev)
-	  return -1;
+    if (f->dev == rt->link->dev)
+        return -1;
 
-  f->dev = rt->link->dev;
-  hdr->ttl= (uint8_t)(hdr->ttl - 1);
-  if (hdr->ttl < 1) {
-    pico_notify_ttl_expired(f);
-    dbg(" ------------------- TTL EXPIRED\n");
-    return -1;
-  }
-  hdr->crc++;
+    f->dev = rt->link->dev;
+    hdr->ttl = (uint8_t)(hdr->ttl - 1);
+    if (hdr->ttl < 1) {
+        pico_notify_ttl_expired(f);
+        dbg(" ------------------- TTL EXPIRED\n");
+        return -1;
+    }
 
-  pico_ipv4_nat_outbound(f, &rt->link->address);
+    hdr->crc++;
+
+    pico_ipv4_nat_outbound(f, &rt->link->address);
 
-  f->start = f->net_hdr;
-  if(f->dev->eth != NULL)
-    f->len -= PICO_SIZE_ETHHDR;
-  pico_sendto_dev(f);
-  return 0;
+    f->start = f->net_hdr;
+    if(f->dev->eth != NULL)
+        f->len -= PICO_SIZE_ETHHDR;
+
+    pico_sendto_dev(f);
+    return 0;
 
 }
 
 int pico_ipv4_is_broadcast(uint32_t addr)
 {
-  struct pico_ipv4_link *link;
-  struct pico_tree_node * index;
-  if (addr == PICO_IP4_ANY)
-    return 1;
-  if (addr == PICO_IP4_BCAST)
-    return 1;
+    struct pico_ipv4_link *link;
+    struct pico_tree_node *index;
+    if (addr == PICO_IP4_ANY)
+        return 1;
 
-  pico_tree_foreach(index,&Tree_dev_link) {
-    link = index->keyValue;
-    if ((link->address.addr | (~link->netmask.addr)) == addr)
-      return 1;
-  }
-  return 0;
+    if (addr == PICO_IP4_BCAST)
+        return 1;
+
+    pico_tree_foreach(index, &Tree_dev_link) {
+        link = index->keyValue;
+        if ((link->address.addr | (~link->netmask.addr)) == addr)
+            return 1;
+    }
+    return 0;
 }
 
 void pico_ipv4_unreachable(struct pico_frame *f, int err)
 {
-  struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
 #if defined PICO_SUPPORT_TCP || defined PICO_SUPPORT_UDP
-  f->transport_hdr = ((uint8_t *)f->net_hdr) + PICO_SIZE_IP4HDR;
-  pico_transport_error(f, hdr->proto, err);
+    f->transport_hdr = ((uint8_t *)f->net_hdr) + PICO_SIZE_IP4HDR;
+    pico_transport_error(f, hdr->proto, err);
 #endif
 }
 
--- a/modules/pico_ipv4.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_ipv4.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_IPV4
 #define _INCLUDE_PICO_IPV4
 #include "pico_addressing.h"
@@ -21,32 +21,32 @@
 #ifndef MBED
     #define PICO_IPV4_FRAG_MAX_SIZE (63 * 1024)
 #else
-	#define PICO_IPV4_FRAG_MAX_SIZE PICO_DEFAULT_SOCKETQ
+    #define PICO_IPV4_FRAG_MAX_SIZE PICO_DEFAULT_SOCKETQ
 #endif
 
 extern struct pico_protocol pico_proto_ipv4;
 
 struct __attribute__((packed)) pico_ipv4_hdr {
-  uint8_t vhl;
-  uint8_t tos;
-  uint16_t len;
-  uint16_t id;
-  uint16_t frag;
-  uint8_t ttl;
-  uint8_t proto;
-  uint16_t crc;
-  struct pico_ip4 src;
-  struct pico_ip4 dst;
-  uint8_t options[];
+    uint8_t vhl;
+    uint8_t tos;
+    uint16_t len;
+    uint16_t id;
+    uint16_t frag;
+    uint8_t ttl;
+    uint8_t proto;
+    uint16_t crc;
+    struct pico_ip4 src;
+    struct pico_ip4 dst;
+    uint8_t options[];
 };
 
 struct __attribute__((packed)) pico_ipv4_pseudo_hdr
 {
-  struct pico_ip4 src;
-  struct pico_ip4 dst;
-  uint8_t zeros;
-  uint8_t proto;
-  uint16_t len;
+    struct pico_ip4 src;
+    struct pico_ip4 dst;
+    uint8_t zeros;
+    uint8_t proto;
+    uint16_t len;
 };
 
 /* Interface: link to device */
@@ -54,30 +54,30 @@
 
 struct pico_ipv4_link
 {
-  struct pico_device *dev;
-  struct pico_ip4 address;
-  struct pico_ip4 netmask;
+    struct pico_device *dev;
+    struct pico_ip4 address;
+    struct pico_ip4 netmask;
 #ifdef PICO_SUPPORT_MCAST
-  struct pico_tree *MCASTGroups;
-  uint8_t mcast_compatibility;
-  uint8_t mcast_last_query_interval;
+    struct pico_tree *MCASTGroups;
+    uint8_t mcast_compatibility;
+    uint8_t mcast_last_query_interval;
 #endif
 };
 
 #ifdef PICO_SUPPORT_MCAST
 struct pico_mcast_group {
-  uint8_t filter_mode;
-  uint16_t reference_count;
-  struct pico_ip4 mcast_addr;
-  struct pico_tree MCASTSources;
+    uint8_t filter_mode;
+    uint16_t reference_count;
+    struct pico_ip4 mcast_addr;
+    struct pico_tree MCASTSources;
 };
 #endif
 
 int pico_ipv4_to_string(char *ipbuf, const uint32_t ip);
 int pico_string_to_ipv4(const char *ipstr, uint32_t *ip);
 int pico_ipv4_valid_netmask(uint32_t mask);
-int pico_ipv4_is_unicast(uint32_t address); 
-int pico_ipv4_is_multicast(uint32_t address); 
+int pico_ipv4_is_unicast(uint32_t address);
+int pico_ipv4_is_multicast(uint32_t address);
 int pico_ipv4_is_broadcast(uint32_t addr);
 
 int pico_ipv4_link_add(struct pico_device *dev, struct pico_ip4 address, struct pico_ip4 netmask);
--- a/modules/pico_ipv6.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_ipv6.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_IPV6
 #define _INCLUDE_PICO_IPV6
 #include "pico_addressing.h"
@@ -14,7 +14,7 @@
 extern const uint8_t PICO_IPV6_ANY[PICO_SIZE_IP6];
 
 
-/* This module is responsible for routing outgoing packets and 
+/* This module is responsible for routing outgoing packets and
  * delivering incoming packets to other layers
  */
 
--- a/modules/pico_nat.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_nat.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,12 +1,12 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-Authors: Kristof Roelants, Brecht Van Cauwenberghe,
+   Authors: Kristof Roelants, Brecht Van Cauwenberghe,
          Simon Maes, Philippe Mariman
-*********************************************************************/
+ *********************************************************************/
 
 #include "pico_stack.h"
 #include "pico_frame.h"
@@ -19,63 +19,73 @@
 #ifdef PICO_SUPPORT_IPV4
 #ifdef PICO_SUPPORT_NAT
 
-#define nat_dbg(...) do{}while(0)
-//#define nat_dbg dbg
+#define nat_dbg(...) do {} while(0)
+/* #define nat_dbg dbg */
 #define PICO_NAT_TIMEWAIT  240000 /* msec (4 mins) */
 
 #define PICO_NAT_INBOUND   0
 #define PICO_NAT_OUTBOUND  1
 
 struct pico_nat_tuple {
-  uint8_t proto;
-  uint16_t conn_active:11;
-  uint16_t portforward:1;
-  uint16_t rst:1;
-  uint16_t syn:1;
-  uint16_t fin_in:1;
-  uint16_t fin_out:1;
-  uint16_t src_port;
-  uint16_t dst_port;
-  uint16_t nat_port;
-  struct pico_ip4 src_addr;
-  struct pico_ip4 dst_addr;
-  struct pico_ip4 nat_addr;
+    uint8_t proto;
+    uint16_t conn_active : 11;
+    uint16_t portforward : 1;
+    uint16_t rst : 1;
+    uint16_t syn : 1;
+    uint16_t fin_in : 1;
+    uint16_t fin_out : 1;
+    uint16_t src_port;
+    uint16_t dst_port;
+    uint16_t nat_port;
+    struct pico_ip4 src_addr;
+    struct pico_ip4 dst_addr;
+    struct pico_ip4 nat_addr;
 };
 
 static struct pico_ipv4_link *nat_link = NULL;
 
-static int nat_cmp_inbound(void * ka, void * kb)
+static int nat_cmp_inbound(void *ka, void *kb)
 {
-	struct pico_nat_tuple *a = ka, *b = kb;
+    struct pico_nat_tuple *a = ka, *b = kb;
+
+    if (a->nat_port < b->nat_port)
+        return -1;
 
-  if (a->nat_port < b->nat_port)
-    return -1;
-  if (a->nat_port > b->nat_port)
-    return 1;
-  if (a->proto < b->proto)
-    return -1;
-  if (a->proto > b->proto)
-      return 1;
-  return 0; /* identical */
+    if (a->nat_port > b->nat_port)
+        return 1;
+
+    if (a->proto < b->proto)
+        return -1;
+
+    if (a->proto > b->proto)
+        return 1;
+
+    return 0; /* identical */
 }
 
-static int nat_cmp_outbound(void * ka, void * kb)
+static int nat_cmp_outbound(void *ka, void *kb)
 {
-	struct pico_nat_tuple *a =ka, *b = kb;
+    struct pico_nat_tuple *a = ka, *b = kb;
+
+    if (a->src_addr.addr < b->src_addr.addr)
+        return -1;
+
+    if (a->src_addr.addr > b->src_addr.addr)
+        return 1;
 
-  if (a->src_addr.addr < b->src_addr.addr)
-    return -1;
-  if (a->src_addr.addr > b->src_addr.addr)
-    return 1;
-  if (a->src_port < b->src_port)
-    return -1;
-  if (a->src_port > b->src_port)
-    return 1;
-  if (a->proto < b->proto)
-    return -1;
-  if (a->proto > b->proto)
-    return 1;
-  return 0; /* identical */
+    if (a->src_port < b->src_port)
+        return -1;
+
+    if (a->src_port > b->src_port)
+        return 1;
+
+    if (a->proto < b->proto)
+        return -1;
+
+    if (a->proto > b->proto)
+        return 1;
+
+    return 0; /* identical */
 }
 
 PICO_TREE_DECLARE(NATOutbound, nat_cmp_outbound);
@@ -83,417 +93,436 @@
 
 void pico_ipv4_nat_print_table(void)
 {
-  struct pico_nat_tuple __attribute__((unused)) *t = NULL;
-  struct pico_tree_node *index = NULL;
+    struct pico_nat_tuple __attribute__((unused)) *t = NULL;
+    struct pico_tree_node *index = NULL;
 
-  nat_dbg("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
-  nat_dbg("+                                                        NAT table                                                       +\n");
-  nat_dbg("+------------------------------------------------------------------------------------------------------------------------+\n");
-  nat_dbg("+ src_addr | src_port | dst_addr | dst_port | nat_addr | nat_port | proto | conn active | FIN1 | FIN2 | SYN | RST | FORW +\n");
-  nat_dbg("+------------------------------------------------------------------------------------------------------------------------+\n");
+    nat_dbg("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
+    nat_dbg("+                                                        NAT table                                                       +\n");
+    nat_dbg("+------------------------------------------------------------------------------------------------------------------------+\n");
+    nat_dbg("+ src_addr | src_port | dst_addr | dst_port | nat_addr | nat_port | proto | conn active | FIN1 | FIN2 | SYN | RST | FORW +\n");
+    nat_dbg("+------------------------------------------------------------------------------------------------------------------------+\n");
 
-  pico_tree_foreach(index, &NATOutbound)
-  {
-  	t = index->keyValue;
-    nat_dbg("+ %08X |  %05u   | %08X |  %05u   | %08X |  %05u   |  %03u  |     %03u     |   %u  |   %u  |  %u  |  %u  |   %u  +\n", 
-           long_be(t->src_addr.addr), t->src_port, long_be(t->dst_addr.addr), t->dst_port, long_be(t->nat_addr.addr), t->nat_port, 
-           t->proto, t->conn_active, t->fin_in, t->fin_out, t->syn, t->rst, t->portforward);
-  }
-  nat_dbg("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
+    pico_tree_foreach(index, &NATOutbound)
+    {
+        t = index->keyValue;
+        nat_dbg("+ %08X |  %05u   | %08X |  %05u   | %08X |  %05u   |  %03u  |     %03u     |   %u  |   %u  |  %u  |  %u  |   %u  +\n",
+                long_be(t->src_addr.addr), t->src_port, long_be(t->dst_addr.addr), t->dst_port, long_be(t->nat_addr.addr), t->nat_port,
+                t->proto, t->conn_active, t->fin_in, t->fin_out, t->syn, t->rst, t->portforward);
+    }
+    nat_dbg("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
 }
 
-/* 
-  2 options: 
+/*
+   2 options:
     find on nat_port and proto
-    find on src_addr, src_port and proto 
-  zero the unused parameters 
-*/
+    find on src_addr, src_port and proto
+   zero the unused parameters
+ */
 static struct pico_nat_tuple *pico_ipv4_nat_find_tuple(uint16_t nat_port, struct pico_ip4 *src_addr, uint16_t src_port, uint8_t proto)
 {
-  struct pico_nat_tuple *found = NULL, test = {0};
+    struct pico_nat_tuple *found = NULL, test = {
+        0
+    };
 
-  test.nat_port = nat_port;
-  test.src_port = src_port;
-  test.proto = proto;
-  if (src_addr)
-    test.src_addr = *src_addr;
+    test.nat_port = nat_port;
+    test.src_port = src_port;
+    test.proto = proto;
+    if (src_addr)
+        test.src_addr = *src_addr;
 
-  if (nat_port)
-  	found = pico_tree_findKey(&NATInbound, &test);
-  else
-  	found = pico_tree_findKey(&NATOutbound, &test);
+    if (nat_port)
+        found = pico_tree_findKey(&NATInbound, &test);
+    else
+        found = pico_tree_findKey(&NATOutbound, &test);
 
-  if (found)
-    return found;
-  else
-    return NULL;
+    if (found)
+        return found;
+    else
+        return NULL;
 }
 
 int pico_ipv4_nat_find(uint16_t nat_port, struct pico_ip4 *src_addr, uint16_t src_port, uint8_t proto)
 {
-  struct pico_nat_tuple *t = NULL;
+    struct pico_nat_tuple *t = NULL;
 
-  t = pico_ipv4_nat_find_tuple(nat_port, src_addr, src_port, proto); 
-  if (t)
-    return 1;
-  else
-    return 0;
+    t = pico_ipv4_nat_find_tuple(nat_port, src_addr, src_port, proto);
+    if (t)
+        return 1;
+    else
+        return 0;
 }
 
-static struct pico_nat_tuple *pico_ipv4_nat_add(struct pico_ip4 dst_addr, uint16_t dst_port, struct pico_ip4 src_addr, uint16_t src_port, 
+static struct pico_nat_tuple *pico_ipv4_nat_add(struct pico_ip4 dst_addr, uint16_t dst_port, struct pico_ip4 src_addr, uint16_t src_port,
                                                 struct pico_ip4 nat_addr, uint16_t nat_port, uint8_t proto)
 {
-  struct pico_nat_tuple *t = pico_zalloc(sizeof(struct pico_nat_tuple));
-  if (!t) {
-    pico_err = PICO_ERR_ENOMEM;
-    return NULL;
-  }
+    struct pico_nat_tuple *t = pico_zalloc(sizeof(struct pico_nat_tuple));
+    if (!t) {
+        pico_err = PICO_ERR_ENOMEM;
+        return NULL;
+    }
 
-  t->dst_addr = dst_addr;
-  t->dst_port = dst_port;
-  t->src_addr = src_addr;
-  t->src_port = src_port;
-  t->nat_addr = nat_addr;
-  t->nat_port = nat_port;
-  t->proto = proto;
-  t->conn_active = 1;
-  t->portforward = 0;
-  t->rst = 0;
-  t->syn = 0;
-  t->fin_in = 0;
-  t->fin_out = 0;
+    t->dst_addr = dst_addr;
+    t->dst_port = dst_port;
+    t->src_addr = src_addr;
+    t->src_port = src_port;
+    t->nat_addr = nat_addr;
+    t->nat_port = nat_port;
+    t->proto = proto;
+    t->conn_active = 1;
+    t->portforward = 0;
+    t->rst = 0;
+    t->syn = 0;
+    t->fin_in = 0;
+    t->fin_out = 0;
 
-  if (pico_tree_insert(&NATOutbound, t)) {
-    pico_free(t);
-    return NULL;
-  }
-  if (pico_tree_insert(&NATInbound, t)) {
-    pico_tree_delete(&NATOutbound, t);
-    pico_free(t);
-    return NULL;
-  }
+    if (pico_tree_insert(&NATOutbound, t)) {
+        pico_free(t);
+        return NULL;
+    }
 
-  return t;
+    if (pico_tree_insert(&NATInbound, t)) {
+        pico_tree_delete(&NATOutbound, t);
+        pico_free(t);
+        return NULL;
+    }
+
+    return t;
 }
 
 static int pico_ipv4_nat_del(uint16_t nat_port, uint8_t proto)
 {
-  struct pico_nat_tuple *t = NULL;
-  t = pico_ipv4_nat_find_tuple(nat_port, NULL, 0, proto);
-  if (t) {
-    pico_tree_delete(&NATOutbound, t);
-    pico_tree_delete(&NATInbound, t);
-    pico_free(t);
-  }
-  return 0;
+    struct pico_nat_tuple *t = NULL;
+    t = pico_ipv4_nat_find_tuple(nat_port, NULL, 0, proto);
+    if (t) {
+        pico_tree_delete(&NATOutbound, t);
+        pico_tree_delete(&NATInbound, t);
+        pico_free(t);
+    }
+
+    return 0;
 }
 
 static struct pico_nat_tuple *pico_ipv4_nat_generate_tuple(struct pico_frame *f)
 {
-  struct pico_trans *trans = NULL;
-  struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr;
-  uint16_t nport = 0;
-  uint8_t retry = 32;
+    struct pico_trans *trans = NULL;
+    struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr;
+    uint16_t nport = 0;
+    uint8_t retry = 32;
 
-  /* generate NAT port */
-  do {
-    uint32_t rand = pico_rand();
-    nport = (uint16_t) (rand & 0xFFFFU);
-    nport = (uint16_t)((nport % (65535 - 1024)) + 1024U);
-		nport = short_be(nport);
+    /* generate NAT port */
+    do {
+        uint32_t rand = pico_rand();
+        nport = (uint16_t) (rand & 0xFFFFU);
+        nport = (uint16_t)((nport % (65535 - 1024)) + 1024U);
+        nport = short_be(nport);
 
-    if (pico_is_port_free(net->proto, nport, NULL, &pico_proto_ipv4))
-      break;
-  } while (--retry);
+        if (pico_is_port_free(net->proto, nport, NULL, &pico_proto_ipv4))
+            break;
+    } while (--retry);
 
-  if (!retry)
-    return NULL;
-    
-  switch (net->proto) {
+    if (!retry)
+        return NULL;
+
+    switch (net->proto) {
     case PICO_PROTO_TCP:
     {
-      struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr;
-      trans = &tcp->trans; 
-      break;
+        struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr;
+        trans = &tcp->trans;
+        break;
     }
     case PICO_PROTO_UDP:
     {
-      struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr;
-      trans = &udp->trans;
-      break;
+        struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr;
+        trans = &udp->trans;
+        break;
     }
     case PICO_PROTO_ICMP4:
-      /* XXX: implement */
-      break;
+        /* XXX: implement */
+        break;
 
     default:
-      return NULL;
-  }
+        return NULL;
+    }
 
-  return pico_ipv4_nat_add(net->dst, trans->dport, net->src, trans->sport, nat_link->address, nport, net->proto);
-  // XXX return pico_ipv4_nat_add(nat_link->address, port, net->src, trans->sport, net->proto);
+    return pico_ipv4_nat_add(net->dst, trans->dport, net->src, trans->sport, nat_link->address, nport, net->proto);
+    /* XXX return pico_ipv4_nat_add(nat_link->address, port, net->src, trans->sport, net->proto); */
 }
 
 static int pico_ipv4_nat_snif_session(struct pico_nat_tuple *t, struct pico_frame *f, uint8_t direction)
 {
-  struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr;
- 
-  switch (net->proto) {
+    struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr;
+
+    switch (net->proto) {
     case PICO_PROTO_TCP:
     {
-      struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr;
-      if (tcp->flags & PICO_TCP_SYN)
-        t->syn = 1;
-      if (tcp->flags & PICO_TCP_RST)
-        t->rst = 1;
-      if ((tcp->flags & PICO_TCP_FIN) && (direction == PICO_NAT_INBOUND))
-          t->fin_in = 1;
-      if ((tcp->flags & PICO_TCP_FIN) && (direction == PICO_NAT_OUTBOUND))
-          t->fin_out = 1;
-      break;
+        struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr;
+        if (tcp->flags & PICO_TCP_SYN)
+            t->syn = 1;
+
+        if (tcp->flags & PICO_TCP_RST)
+            t->rst = 1;
+
+        if ((tcp->flags & PICO_TCP_FIN) && (direction == PICO_NAT_INBOUND))
+            t->fin_in = 1;
+
+        if ((tcp->flags & PICO_TCP_FIN) && (direction == PICO_NAT_OUTBOUND))
+            t->fin_out = 1;
+
+        break;
     }
 
     case PICO_PROTO_UDP:
-      t->conn_active = 1;
-      break;
+        t->conn_active = 1;
+        break;
 
     case PICO_PROTO_ICMP4:
-      /* XXX: implement */
-      break;
+        /* XXX: implement */
+        break;
 
     default:
-      return -1;
-  }
+        return -1;
+    }
 
-  return 0;
+    return 0;
 }
 
 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;
-  IGNORE_PARAMETER(now);
-  IGNORE_PARAMETER(_unused);
-	nat_dbg("NAT: before table cleanup:\n");
-  pico_ipv4_nat_print_table();
+    struct pico_tree_node *index = NULL, *_tmp = NULL;
+    struct pico_nat_tuple *t = NULL;
+    IGNORE_PARAMETER(now);
+    IGNORE_PARAMETER(_unused);
+    nat_dbg("NAT: before table cleanup:\n");
+    pico_ipv4_nat_print_table();
 
-  pico_tree_foreach_reverse_safe(index, &NATOutbound, _tmp)
-  {
-  	t = index->keyValue;
-    switch (t->proto)
+    pico_tree_foreach_reverse_safe(index, &NATOutbound, _tmp)
     {
-      case PICO_PROTO_TCP:
-        if (t->portforward)
-          break;
-        else if (t->conn_active == 0 || t->conn_active > 360) /* conn active for > 24 hours */
-          pico_ipv4_nat_del(t->nat_port, t->proto);
-        else if (t->rst || (t->fin_in && t->fin_out))
-          t->conn_active = 0;
-        else
-          t->conn_active++;
-        break;
+        t = index->keyValue;
+        switch (t->proto)
+        {
+        case PICO_PROTO_TCP:
+            if (t->portforward)
+                break;
+            else if (t->conn_active == 0 || t->conn_active > 360) /* conn active for > 24 hours */
+                pico_ipv4_nat_del(t->nat_port, t->proto);
+            else if (t->rst || (t->fin_in && t->fin_out))
+                t->conn_active = 0;
+            else
+                t->conn_active++;
+
+            break;
 
-      case PICO_PROTO_UDP:
-        if (t->portforward)
-          break;
-        else if (t->conn_active > 1)
-          pico_ipv4_nat_del(t->nat_port, t->proto);
-        else
-          t->conn_active++;
-        break;
+        case PICO_PROTO_UDP:
+            if (t->portforward)
+                break;
+            else if (t->conn_active > 1)
+                pico_ipv4_nat_del(t->nat_port, t->proto);
+            else
+                t->conn_active++;
+
+            break;
 
-      case PICO_PROTO_ICMP4:
-        if (t->conn_active > 1)
-          pico_ipv4_nat_del(t->nat_port, t->proto);
-        else
-          t->conn_active++;
+        case PICO_PROTO_ICMP4:
+            if (t->conn_active > 1)
+                pico_ipv4_nat_del(t->nat_port, t->proto);
+            else
+                t->conn_active++;
 
-      default:
-        /* unknown protocol in NAT table, delete when it has existed NAT_TIMEWAIT */
-        if (t->conn_active > 1)
-          pico_ipv4_nat_del(t->nat_port, t->proto);
-        else
-          t->conn_active++;
+        default:
+            /* unknown protocol in NAT table, delete when it has existed NAT_TIMEWAIT */
+            if (t->conn_active > 1)
+                pico_ipv4_nat_del(t->nat_port, t->proto);
+            else
+                t->conn_active++;
+        }
     }
-  }
 
-  nat_dbg("NAT: after table cleanup:\n");
-  pico_ipv4_nat_print_table();
-  pico_timer_add(PICO_NAT_TIMEWAIT, pico_ipv4_nat_table_cleanup, NULL);
+    nat_dbg("NAT: after table cleanup:\n");
+    pico_ipv4_nat_print_table();
+    pico_timer_add(PICO_NAT_TIMEWAIT, pico_ipv4_nat_table_cleanup, NULL);
 }
 
 int pico_ipv4_port_forward(struct pico_ip4 nat_addr, uint16_t nat_port, struct pico_ip4 src_addr, uint16_t src_port, uint8_t proto, uint8_t flag)
 {
-  struct pico_nat_tuple *t = NULL;
-  struct pico_ip4 any_addr = {0};
-  uint16_t any_port = 0;
+    struct pico_nat_tuple *t = NULL;
+    struct pico_ip4 any_addr = {
+        0
+    };
+    uint16_t any_port = 0;
 
-  switch (flag)
-  {
+    switch (flag)
+    {
     case PICO_NAT_PORT_FORWARD_ADD:
-      t = pico_ipv4_nat_add(any_addr, any_port, src_addr, src_port, nat_addr, nat_port, proto);
-      if (!t) {
-        pico_err = PICO_ERR_EAGAIN;
-        return -1;
-      }
-      t->portforward = 1;
-      break;
+        t = pico_ipv4_nat_add(any_addr, any_port, src_addr, src_port, nat_addr, nat_port, proto);
+        if (!t) {
+            pico_err = PICO_ERR_EAGAIN;
+            return -1;
+        }
+
+        t->portforward = 1;
+        break;
 
     case PICO_NAT_PORT_FORWARD_DEL:
-      return pico_ipv4_nat_del(nat_port, proto);
+        return pico_ipv4_nat_del(nat_port, proto);
 
     default:
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
-  }
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  pico_ipv4_nat_print_table();
-  return 0;
+    pico_ipv4_nat_print_table();
+    return 0;
 }
 
 int pico_ipv4_nat_inbound(struct pico_frame *f, struct pico_ip4 *link_addr)
 {
-  struct pico_nat_tuple *tuple = NULL;
-  struct pico_trans *trans = NULL; 
-  struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr; 
+    struct pico_nat_tuple *tuple = NULL;
+    struct pico_trans *trans = NULL;
+    struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr;
 
-  if (!pico_ipv4_nat_is_enabled(link_addr))
-    return -1;
+    if (!pico_ipv4_nat_is_enabled(link_addr))
+        return -1;
 
-  switch (net->proto) {
+    switch (net->proto) {
     case PICO_PROTO_TCP:
     {
-      struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr;
-      trans = &tcp->trans;
-      tuple = pico_ipv4_nat_find_tuple(trans->dport, 0, 0, net->proto);
-      if (!tuple)
-        return -1;
-      /* replace dst IP and dst PORT */
-      net->dst = tuple->src_addr;
-      trans->dport = tuple->src_port;
-      /* recalculate CRC */
-      tcp->crc = 0;
-      tcp->crc = short_be(pico_tcp_checksum_ipv4(f));
-      break;
+        struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr;
+        trans = &tcp->trans;
+        tuple = pico_ipv4_nat_find_tuple(trans->dport, 0, 0, net->proto);
+        if (!tuple)
+            return -1;
+
+        /* replace dst IP and dst PORT */
+        net->dst = tuple->src_addr;
+        trans->dport = tuple->src_port;
+        /* recalculate CRC */
+        tcp->crc = 0;
+        tcp->crc = short_be(pico_tcp_checksum_ipv4(f));
+        break;
     }
     case PICO_PROTO_UDP:
     {
-      struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr;
-      trans = &udp->trans;
-      tuple = pico_ipv4_nat_find_tuple(trans->dport, 0, 0, net->proto);
-      if (!tuple)
-        return -1;
-      /* replace dst IP and dst PORT */
-      net->dst = tuple->src_addr;
-      trans->dport = tuple->src_port;
-      /* recalculate CRC */
-      udp->crc = 0;
-      udp->crc = short_be(pico_udp_checksum_ipv4(f));
-      break;
+        struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr;
+        trans = &udp->trans;
+        tuple = pico_ipv4_nat_find_tuple(trans->dport, 0, 0, net->proto);
+        if (!tuple)
+            return -1;
+
+        /* replace dst IP and dst PORT */
+        net->dst = tuple->src_addr;
+        trans->dport = tuple->src_port;
+        /* recalculate CRC */
+        udp->crc = 0;
+        udp->crc = short_be(pico_udp_checksum_ipv4(f));
+        break;
     }
     case PICO_PROTO_ICMP4:
-      /* XXX reimplement */
-      break;
+        /* XXX reimplement */
+        break;
 
     default:
-      nat_dbg("NAT ERROR: inbound NAT on erroneous protocol\n");
-      return -1;
-  }
+        nat_dbg("NAT ERROR: inbound NAT on erroneous protocol\n");
+        return -1;
+    }
 
-  pico_ipv4_nat_snif_session(tuple, f, PICO_NAT_INBOUND);
-  net->crc = 0;
-  net->crc = short_be(pico_checksum(net, f->net_len));
+    pico_ipv4_nat_snif_session(tuple, f, PICO_NAT_INBOUND);
+    net->crc = 0;
+    net->crc = short_be(pico_checksum(net, f->net_len));
 
-  nat_dbg("NAT: inbound translation {dst.addr, dport}: {%08X,%u} -> {%08X,%u}\n", 
+    nat_dbg("NAT: inbound translation {dst.addr, dport}: {%08X,%u} -> {%08X,%u}\n",
             tuple->nat_addr.addr, short_be(tuple->nat_port), tuple->src_addr.addr, short_be(tuple->src_port));
 
-  return 0;
+    return 0;
 }
 
 int pico_ipv4_nat_outbound(struct pico_frame *f, struct pico_ip4 *link_addr)
 {
-  struct pico_nat_tuple *tuple = NULL;
-  struct pico_trans *trans = NULL; 
-  struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr; 
+    struct pico_nat_tuple *tuple = NULL;
+    struct pico_trans *trans = NULL;
+    struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr;
 
-  if (!pico_ipv4_nat_is_enabled(link_addr))
-    return -1;
+    if (!pico_ipv4_nat_is_enabled(link_addr))
+        return -1;
 
-  switch (net->proto) {
+    switch (net->proto) {
     case PICO_PROTO_TCP:
     {
-      struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr;
-      trans = &tcp->trans;
-      tuple = pico_ipv4_nat_find_tuple(0, &net->src, trans->sport, net->proto);
-      if (!tuple)
-        tuple = pico_ipv4_nat_generate_tuple(f);
-      /* replace src IP and src PORT */
-      net->src = tuple->nat_addr;
-      trans->sport = tuple->nat_port;
-      /* recalculate CRC */
-      tcp->crc = 0;
-      tcp->crc = short_be(pico_tcp_checksum_ipv4(f));
-      break;
+        struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr;
+        trans = &tcp->trans;
+        tuple = pico_ipv4_nat_find_tuple(0, &net->src, trans->sport, net->proto);
+        if (!tuple)
+            tuple = pico_ipv4_nat_generate_tuple(f);
+
+        /* replace src IP and src PORT */
+        net->src = tuple->nat_addr;
+        trans->sport = tuple->nat_port;
+        /* recalculate CRC */
+        tcp->crc = 0;
+        tcp->crc = short_be(pico_tcp_checksum_ipv4(f));
+        break;
     }
     case PICO_PROTO_UDP:
     {
-      struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr;
-      trans = &udp->trans;
-      tuple = pico_ipv4_nat_find_tuple(0, &net->src, trans->sport, net->proto);
-      if (!tuple)
-        tuple = pico_ipv4_nat_generate_tuple(f);
-      /* replace src IP and src PORT */
-      net->src = tuple->nat_addr;
-      trans->sport = tuple->nat_port;
-      /* recalculate CRC */
-      udp->crc = 0;
-      udp->crc = short_be(pico_udp_checksum_ipv4(f));
-      break;
+        struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr;
+        trans = &udp->trans;
+        tuple = pico_ipv4_nat_find_tuple(0, &net->src, trans->sport, net->proto);
+        if (!tuple)
+            tuple = pico_ipv4_nat_generate_tuple(f);
+
+        /* replace src IP and src PORT */
+        net->src = tuple->nat_addr;
+        trans->sport = tuple->nat_port;
+        /* recalculate CRC */
+        udp->crc = 0;
+        udp->crc = short_be(pico_udp_checksum_ipv4(f));
+        break;
     }
     case PICO_PROTO_ICMP4:
-      /* XXX reimplement */
-      break;
+        /* XXX reimplement */
+        break;
 
     default:
-      nat_dbg("NAT ERROR: outbound NAT on erroneous protocol\n");
-      return -1;
-  }
+        nat_dbg("NAT ERROR: outbound NAT on erroneous protocol\n");
+        return -1;
+    }
 
-  pico_ipv4_nat_snif_session(tuple, f, PICO_NAT_OUTBOUND);
-  net->crc = 0;
-  net->crc = short_be(pico_checksum(net, f->net_len));
+    pico_ipv4_nat_snif_session(tuple, f, PICO_NAT_OUTBOUND);
+    net->crc = 0;
+    net->crc = short_be(pico_checksum(net, f->net_len));
 
-  nat_dbg("NAT: outbound translation {src.addr, sport}: {%08X,%u} -> {%08X,%u}\n", 
+    nat_dbg("NAT: outbound translation {src.addr, sport}: {%08X,%u} -> {%08X,%u}\n",
             tuple->src_addr.addr, short_be(tuple->src_port), tuple->nat_addr.addr, short_be(tuple->nat_port));
 
-  return 0;
+    return 0;
 }
 
 int pico_ipv4_nat_enable(struct pico_ipv4_link *link)
 {
-  if (link == NULL) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (link == NULL) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  nat_link = link;
-  pico_timer_add(PICO_NAT_TIMEWAIT, pico_ipv4_nat_table_cleanup, NULL);
-  return 0;
+    nat_link = link;
+    pico_timer_add(PICO_NAT_TIMEWAIT, pico_ipv4_nat_table_cleanup, NULL);
+    return 0;
 }
- 
+
 int pico_ipv4_nat_disable(void)
 {
-  nat_link = NULL;
-  return 0;
+    nat_link = NULL;
+    return 0;
 }
 
 int pico_ipv4_nat_is_enabled(struct pico_ip4 *link_addr)
 {
-  if (!nat_link)
-    return 0;
-  if (nat_link->address.addr != link_addr->addr)
-    return 0;
-  return 1;
+    if (!nat_link)
+        return 0;
+
+    if (nat_link->address.addr != link_addr->addr)
+        return 0;
+
+    return 1;
 }
 
 #endif
--- a/modules/pico_nat.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_nat.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
-  
-Authors: Kristof Roelants, Simon Maes, Brecht Van Cauwenberghe
-*********************************************************************/
+   .
+
+   Authors: Kristof Roelants, Simon Maes, Brecht Van Cauwenberghe
+ *********************************************************************/
 
 #ifndef _INCLUDE_PICO_NAT
 #define _INCLUDE_PICO_NAT
@@ -26,47 +26,47 @@
 int pico_ipv4_nat_is_enabled(struct pico_ip4 *link_addr);
 #else
 
-#define pico_ipv4_nat_print_table() do{}while(0)
+#define pico_ipv4_nat_print_table() do {} while(0)
 static inline int pico_ipv4_nat_inbound(struct pico_frame *f, struct pico_ip4 *link_addr)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 
 static inline int pico_ipv4_nat_outbound(struct pico_frame *f, struct pico_ip4 *link_addr)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 
 static inline int pico_ipv4_nat_enable(struct pico_ipv4_link *link)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 
 static inline int pico_ipv4_nat_disable(void)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 
 static inline int pico_ipv4_nat_is_enabled(struct pico_ip4 *link_addr)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 
 static inline int pico_ipv4_nat_find(uint16_t nat_port, struct pico_ip4 *src_addr, uint16_t src_port, uint8_t proto)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 
 static inline int pico_ipv4_port_forward(struct pico_ip4 nat_addr, uint16_t nat_port, struct pico_ip4 src_addr, uint16_t src_port, uint8_t proto, uint8_t flag)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 #endif
 
--- a/modules/pico_olsr.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_olsr.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 #include "pico_stack.h"
 #include "pico_config.h"
@@ -19,36 +19,38 @@
 #define OLSR_HELLO_INTERVAL   ((uint32_t)2000)
 #define OLSR_TC_INTERVAL      ((uint32_t)5000)
 #define OLSR_MAXJITTER        ((uint32_t)(OLSR_HELLO_INTERVAL >> 2))
-static const struct pico_ip4 HOST_NETMASK = { 0xFFFFFFFF };
+static const struct pico_ip4 HOST_NETMASK = {
+    0xFFFFFFFF
+};
 #ifndef MIN
-# define MIN(a,b) (a<b?a:b)
+# define MIN(a, b) (a < b ? a : b)
 #endif
 
-#define fresher(a,b) ((a>b) || ((b - a) > 32768))
+#define fresher(a, b) ((a > b) || ((b - a) > 32768))
 
 
 /* Objects */
 struct olsr_route_entry
 {
-  struct olsr_route_entry         *next;
-  uint32_t                        time_left;
-  struct pico_ip4                  destination;
-  struct olsr_route_entry         *gateway;
-  struct pico_device              *iface;
-  uint16_t                        metric;
-  uint8_t                          link_type;
-  struct olsr_route_entry         *children;
-  uint16_t                        ansn;
-  uint16_t                        seq;
-  uint8_t                         lq, nlq;
-  uint8_t                         *advertised_tc;
+    struct olsr_route_entry         *next;
+    uint32_t time_left;
+    struct pico_ip4 destination;
+    struct olsr_route_entry         *gateway;
+    struct pico_device              *iface;
+    uint16_t metric;
+    uint8_t link_type;
+    struct olsr_route_entry         *children;
+    uint16_t ansn;
+    uint16_t seq;
+    uint8_t lq, nlq;
+    uint8_t                         *advertised_tc;
 };
 
 struct olsr_dev_entry
 {
-  struct olsr_dev_entry *next;
-  struct pico_device *dev;
-  uint16_t pkt_counter;
+    struct olsr_dev_entry *next;
+    struct pico_device *dev;
+    uint16_t pkt_counter;
 };
 
 
@@ -69,48 +71,48 @@
 
 struct __attribute__((packed)) olsr_link
 {
-  uint8_t link_code;
-  uint8_t reserved;
-  uint16_t link_msg_size;
+    uint8_t link_code;
+    uint8_t reserved;
+    uint16_t link_msg_size;
 };
 
 struct __attribute__((packed)) olsr_neighbor
 {
-  uint32_t addr;
-  uint8_t  lq;
-  uint8_t  nlq;
-  uint16_t reserved;
+    uint32_t addr;
+    uint8_t lq;
+    uint8_t nlq;
+    uint16_t reserved;
 };
 
 struct __attribute__((packed)) olsr_hmsg_hello
 {
-  uint16_t reserved;
-  uint8_t htime;
-  uint8_t willingness;
+    uint16_t reserved;
+    uint8_t htime;
+    uint8_t willingness;
 };
 
 struct __attribute__((packed)) olsr_hmsg_tc
 {
-  uint16_t ansn;
-  uint16_t reserved;
+    uint16_t ansn;
+    uint16_t reserved;
 };
 
 
 struct __attribute__((packed)) olsrmsg
 {
-  uint8_t type;
-  uint8_t vtime;
-  uint16_t size;
-  struct pico_ip4 orig;
-  uint8_t ttl;
-  uint8_t hop;
-  uint16_t seq;
+    uint8_t type;
+    uint8_t vtime;
+    uint16_t size;
+    struct pico_ip4 orig;
+    uint8_t ttl;
+    uint8_t hop;
+    uint16_t seq;
 };
 
 struct __attribute__((packed)) olsrhdr
 {
-  uint16_t len;
-  uint16_t seq;
+    uint16_t len;
+    uint16_t seq;
 };
 
 
@@ -123,117 +125,126 @@
 
 static struct olsr_dev_entry *olsr_get_deventry(struct pico_device *dev)
 {
-  struct olsr_dev_entry *cur = Local_devices;
-  while(cur) {
-    if (cur->dev == dev)
-      return cur;
-    cur = cur->next;
-  }
-  return NULL;
+    struct olsr_dev_entry *cur = Local_devices;
+    while(cur) {
+        if (cur->dev == dev)
+            return cur;
+
+        cur = cur->next;
+    }
+    return NULL;
 }
 
 static struct olsr_route_entry *olsr_get_ethentry(struct pico_device *vif)
 {
-  struct olsr_route_entry *cur = Local_interfaces;
-  while(cur) {
-    if (cur->iface == vif)
-      return cur;
-    cur = cur->next;
-  }
-  return NULL;
+    struct olsr_route_entry *cur = Local_interfaces;
+    while(cur) {
+        if (cur->iface == vif)
+            return cur;
+
+        cur = cur->next;
+    }
+    return NULL;
 }
 
 static struct olsr_route_entry *get_next_hop(struct olsr_route_entry *dst)
 {
-  struct olsr_route_entry *hop = dst;
-  while(hop) {
-    if(hop->metric <= 1)
-      return hop;
-    hop = hop->gateway;
-  }
-  return NULL;
+    struct olsr_route_entry *hop = dst;
+    while(hop) {
+        if(hop->metric <= 1)
+            return hop;
+
+        hop = hop->gateway;
+    }
+    return NULL;
 }
 
 static inline void olsr_route_add(struct olsr_route_entry *el)
 {
-  struct olsr_route_entry *nexthop;
+    struct olsr_route_entry *nexthop;
 
-  my_ansn++;
+    my_ansn++;
 
-  if (el->gateway) {
-    nexthop = get_next_hop(el);
-    /* 2-hops route or more */
-    el->next = el->gateway->children;
-    el->gateway->children = el;
-    el->link_type = OLSRLINK_MPR;
-    if (nexthop->destination.addr != el->destination.addr) {
-      //dbg("[OLSR] Adding route to %08x via %08x metric %d..................", el->destination.addr, nexthop->destination.addr, el->metric);
-      pico_ipv4_route_add(el->destination, HOST_NETMASK, nexthop->destination, el->metric, NULL);
-      //dbg("route added: %d err: %s\n", ret, strerror(pico_err));
+    if (el->gateway) {
+        nexthop = get_next_hop(el);
+        /* 2-hops route or more */
+        el->next = el->gateway->children;
+        el->gateway->children = el;
+        el->link_type = OLSRLINK_MPR;
+        if (nexthop->destination.addr != el->destination.addr) {
+            /* dbg("[OLSR] Adding route to %08x via %08x metric %d..................", el->destination.addr, nexthop->destination.addr, el->metric); */
+            pico_ipv4_route_add(el->destination, HOST_NETMASK, nexthop->destination, el->metric, NULL);
+            /* dbg("route added: %d err: %s\n", ret, strerror(pico_err)); */
+        }
+    } else if (el->iface) {
+        /* neighbor */
+        struct olsr_route_entry *ei = olsr_get_ethentry(el->iface);
+        if (el->link_type == OLSRLINK_UNKNOWN)
+            el->link_type = OLSRLINK_SYMMETRIC;
+
+        if (ei) {
+            el->next = ei->children;
+            ei->children = el;
+        }
     }
-  } else if (el->iface) {
-    /* neighbor */
-    struct olsr_route_entry *ei = olsr_get_ethentry(el->iface);
-    if (el->link_type == OLSRLINK_UNKNOWN)
-      el->link_type = OLSRLINK_SYMMETRIC;
-    if (ei) {
-      el->next = ei->children;
-      ei->children = el;
-    }
-  }
 }
 
 static inline void olsr_route_del(struct olsr_route_entry *r)
 {
-  struct olsr_route_entry *cur, *prev = NULL, *lst;
-  //dbg("[OLSR] DELETING route..................\n");
-  my_ansn++;
-  if (r->gateway) {
-    lst = r->gateway->children;
-  } else if (r->iface) {
-    lst = olsr_get_ethentry(r->iface);
-  } else {
-    lst = Local_interfaces;
-  }
-  cur = lst, prev = NULL;
-  while(cur) {
-    if (cur == r) {
-      /* found */
-      if (r->gateway) {
-        pico_ipv4_route_del(r->destination, HOST_NETMASK, r->metric);
-        if (!prev)
-          r->gateway->children = r->next;
-        else
-          prev->next = r->next;
-      }
+    struct olsr_route_entry *cur, *prev = NULL, *lst;
+    /* dbg("[OLSR] DELETING route..................\n"); */
+    my_ansn++;
+    if (r->gateway) {
+        lst = r->gateway->children;
+    } else if (r->iface) {
+        lst = olsr_get_ethentry(r->iface);
+    } else {
+        lst = Local_interfaces;
+    }
 
-      while (r->children) {
-        olsr_route_del(r->children);
-        /* Orphans must die. */
-        pico_free(r->children);
-      }
-      return;
+    cur = lst, prev = NULL;
+    while(cur) {
+        if (cur == r) {
+            /* found */
+            if (r->gateway) {
+                pico_ipv4_route_del(r->destination, HOST_NETMASK, r->metric);
+                if (!prev)
+                    r->gateway->children = r->next;
+                else
+                    prev->next = r->next;
+            }
+
+            while (r->children) {
+                olsr_route_del(r->children);
+                /* Orphans must die. */
+                pico_free(r->children);
+            }
+            return;
+        }
+
+        prev = cur;
+        cur = cur->next;
     }
-    prev = cur;
-    cur = cur->next;
-  }
 }
 
 static struct olsr_route_entry *get_route_by_address(struct olsr_route_entry *lst, uint32_t ip)
 {
-  struct olsr_route_entry *found;
-  if(lst) {
-    if (lst->destination.addr == ip) {
-      return lst;
+    struct olsr_route_entry *found;
+    if(lst) {
+        if (lst->destination.addr == ip) {
+            return lst;
+        }
+
+        found = get_route_by_address(lst->children, ip);
+        if (found)
+            return found;
+
+        found = get_route_by_address(lst->next, ip);
+        if (found)
+            return found;
     }
-    found = get_route_by_address(lst->children, ip);
-    if (found)
-      return found;
-    found = get_route_by_address(lst->next, ip);
-    if (found)
-      return found;
-  }
-  return NULL;
+
+    return NULL;
 }
 
 #define OLSR_C_SHIFT (uint32_t)4 /* 1/16 */
@@ -242,41 +253,43 @@
 uint8_t seconds2olsr(uint32_t seconds)
 {
     uint16_t a, b;
-    //dbg("seconds=%u\n", (uint16_t)seconds);
-    
+    /* dbg("seconds=%u\n", (uint16_t)seconds); */
+
     if (seconds > 32767)
         seconds = 32767;
 
     /* find largest b such as seconds/C >= 2^b */
     for (b = 1; b <= 0x0fu; b++) {
-        if ((uint16_t)(seconds * 16u) < (1u << b)){
+        if ((uint16_t)(seconds * 16u) < (1u << b)) {
             b--;
             break;
         }
     }
-    //dbg("b=%u", b);
+    /* dbg("b=%u", b); */
     /* compute the expression 16*(T/(C*(2^b))-1), which may not be a
        integer, and round it up.  This results in the value for 'a' */
-    //a = (T / ( C * (1u << b) ) ) - 1u;
+    /* a = (T / ( C * (1u << b) ) ) - 1u; */
     {
-        uint16_t den = ((uint16_t)(1u << b) >> 4u );
-        //dbg(" den=%u ", den);
+        uint16_t den = ((uint16_t)(1u << b) >> 4u);
+        /* dbg(" den=%u ", den); */
         if (den == 0)
         {
-          //dbg("div by 0!\n");
-          den = 1u;
+            /* dbg("div by 0!\n"); */
+            den = 1u;
         }
-        a = (uint16_t)(((uint16_t)seconds / den ) - (uint16_t)1);
+
+        a = (uint16_t)(((uint16_t)seconds / den) - (uint16_t)1);
     }
-    //a = a & 0x0Fu;
+    /* a = a & 0x0Fu; */
 
-    //dbg(" a=%u\n", a);
-    
+    /* dbg(" a=%u\n", a); */
+
     /* if 'a' is equal to 16: increment 'b' by one, and set 'a' to 0 */
     if (16u == a) {
         b++;
         a = 0u;
     }
+
     return (uint8_t)((a << 4u) + b);
 }
 
@@ -284,672 +297,699 @@
 {
     uint8_t a, b;
     uint16_t seconds;
-    //dbg("olsr format: %u -- ", olsr);
+    /* dbg("olsr format: %u -- ", olsr); */
     a = (olsr >> 4) & 0xFu;
     b = olsr & 0x0f;
-    //dbg("o2s: a=%u, b=%u\n", a,b);
-    if (b<4)
-        seconds = (uint16_t)(( (1u << b) + (uint16_t)(((uint16_t)(a << b) >> 4u) & 0xFu) ) >> OLSR_C_SHIFT);
+    /* dbg("o2s: a=%u, b=%u\n", a,b); */
+    if (b < 4)
+        seconds = (uint16_t)(((1u << b) + (uint16_t)(((uint16_t)(a << b) >> 4u) & 0xFu)) >> OLSR_C_SHIFT);
     else
-        seconds = (uint16_t)(( (1u << b) + (uint16_t)(((uint16_t)(a << (b-4))) & 0xFu) ) >> OLSR_C_SHIFT);
-    //dbg("o2s: seconds: %u\n", seconds);
+        seconds = (uint16_t)(((1u << b) + (uint16_t)(((uint16_t)(a << (b - 4))) & 0xFu)) >> OLSR_C_SHIFT);
+
+    /* dbg("o2s: seconds: %u\n", seconds); */
     return seconds;
 }
 
 
 static void refresh_neighbors(struct pico_device *iface)
 {
-  struct pico_ip4 neighbors[256];
-  int i;
-  struct olsr_route_entry *found = NULL, *ancestor = NULL;
-  int n_vec_size;
-        
-        n_vec_size = pico_arp_get_neighbors(iface, neighbors, 256);
+    struct pico_ip4 neighbors[256];
+    int i;
+    struct olsr_route_entry *found = NULL, *ancestor = NULL;
+    int n_vec_size;
 
-  ancestor = olsr_get_ethentry(iface);
-  if (!ancestor)
-    return;
+    n_vec_size = pico_arp_get_neighbors(iface, neighbors, 256);
+
+    ancestor = olsr_get_ethentry(iface);
+    if (!ancestor)
+        return;
 
-  for (i = 0; i < n_vec_size; i++) {
-    found = get_route_by_address(Local_interfaces, neighbors[i].addr);
-    if (found) {
-      if (found->metric > 1) { /* Reposition among neighbors */
-        olsr_route_del(found);
-        found->gateway = olsr_get_ethentry(iface);
-        found->iface = iface;
-        found->metric = 1;
-        found->lq = 0xFF;
-        found->nlq = 0xFF;
-        olsr_route_add(found);
-      }
-      if (found->link_type == OLSRLINK_UNKNOWN)
-        found->link_type = OLSRLINK_SYMMETRIC;
-      found->time_left = (OLSR_HELLO_INTERVAL << 2);
-    } else {
-      struct olsr_route_entry *e = pico_zalloc(sizeof (struct olsr_route_entry));
-      if (!e) {
-        dbg("olsr: adding local route entry\n");
-        return;
-      }
-      e->destination.addr = neighbors[i].addr;
-      e->link_type = OLSRLINK_SYMMETRIC;
-      e->time_left = (OLSR_HELLO_INTERVAL << 2);
-      e->gateway = olsr_get_ethentry(iface);
-      e->iface = iface;
-      e->metric = 1;
-      e->lq = 0xFF;
-      e->nlq = 0xFF;
-      olsr_route_add(e);
+    for (i = 0; i < n_vec_size; i++) {
+        found = get_route_by_address(Local_interfaces, neighbors[i].addr);
+        if (found) {
+            if (found->metric > 1) { /* Reposition among neighbors */
+                olsr_route_del(found);
+                found->gateway = olsr_get_ethentry(iface);
+                found->iface = iface;
+                found->metric = 1;
+                found->lq = 0xFF;
+                found->nlq = 0xFF;
+                olsr_route_add(found);
+            }
+
+            if (found->link_type == OLSRLINK_UNKNOWN)
+                found->link_type = OLSRLINK_SYMMETRIC;
+
+            found->time_left = (OLSR_HELLO_INTERVAL << 2);
+        } else {
+            struct olsr_route_entry *e = pico_zalloc(sizeof (struct olsr_route_entry));
+            if (!e) {
+                dbg("olsr: adding local route entry\n");
+                return;
+            }
+
+            e->destination.addr = neighbors[i].addr;
+            e->link_type = OLSRLINK_SYMMETRIC;
+            e->time_left = (OLSR_HELLO_INTERVAL << 2);
+            e->gateway = olsr_get_ethentry(iface);
+            e->iface = iface;
+            e->metric = 1;
+            e->lq = 0xFF;
+            e->nlq = 0xFF;
+            olsr_route_add(e);
+        }
     }
-  }
 }
 
 static void olsr_garbage_collector(struct olsr_route_entry *sublist)
 {
-  if(!sublist)
-    return;
-  if (sublist->time_left <= 0) {
-    olsr_route_del(sublist);
-    pico_free(sublist);
-    return;
-  } else {
-    sublist->time_left -= 2u;
-  }
-  olsr_garbage_collector(sublist->children);
-  olsr_garbage_collector(sublist->next);
+    if(!sublist)
+        return;
+
+    if (sublist->time_left <= 0) {
+        olsr_route_del(sublist);
+        pico_free(sublist);
+        return;
+    } else {
+        sublist->time_left -= 2u;
+    }
+
+    olsr_garbage_collector(sublist->children);
+    olsr_garbage_collector(sublist->next);
 }
 
 struct olsr_fwd_pkt
 {
-  void *buf;
-  uint16_t len;
-  struct pico_device *pdev;
+    void *buf;
+    uint16_t len;
+    struct pico_device *pdev;
 };
 
 static uint32_t buffer_mem_used = 0U;
 
 void olsr_process_out(pico_time now, void *arg)
 {
-  struct olsr_fwd_pkt *p = (struct olsr_fwd_pkt *)arg;
-  struct pico_ip4 bcast;
-  struct pico_ipv4_link *addr;
-  struct olsr_dev_entry *pdev = Local_devices;
-  struct olsrhdr *ohdr;
-  (void)now;
+    struct olsr_fwd_pkt *p = (struct olsr_fwd_pkt *)arg;
+    struct pico_ip4 bcast;
+    struct pico_ipv4_link *addr;
+    struct olsr_dev_entry *pdev = Local_devices;
+    struct olsrhdr *ohdr;
+    (void)now;
+
+    /* Send the thing out */
+    ohdr = (struct olsrhdr *)p->buf;
+    ohdr->len = short_be((uint16_t)p->len);
+
+    if (p->pdev) {
+        struct olsr_dev_entry *odev = olsr_get_deventry(p->pdev);
+        if (!odev) {
+            goto out_free;
+        }
 
-  /* Send the thing out */
-  ohdr = (struct olsrhdr *)p->buf;
-  ohdr->len = short_be((uint16_t)p->len);
+        addr = pico_ipv4_link_by_dev(p->pdev);
+        if (!addr)
+            goto out_free;
 
-  if (p->pdev) {
-    struct olsr_dev_entry *odev = olsr_get_deventry(p->pdev);
-    if (!odev) {
-      goto out_free;
+        ohdr->seq = short_be((uint16_t)(odev->pkt_counter)++);
+        bcast.addr = (addr->netmask.addr & addr->address.addr) | (~addr->netmask.addr);
+        if ( 0 > pico_socket_sendto(udpsock, p->buf, p->len, &bcast, OLSR_PORT)) {
+            dbg("olsr send\n");
+        }
+    } else {
+        while(pdev) {
+            ohdr->seq = short_be((uint16_t)(pdev->pkt_counter++));
+            addr = pico_ipv4_link_by_dev(pdev->dev);
+            if (!addr)
+                continue;
+
+            bcast.addr = (addr->netmask.addr & addr->address.addr) | (~addr->netmask.addr);
+            if ( 0 > pico_socket_sendto(udpsock, p->buf, p->len, &bcast, OLSR_PORT)) {
+                dbg("olsr send\n");
+            }
+
+            pdev = pdev->next;
+        }
     }
-    addr = pico_ipv4_link_by_dev(p->pdev);
-    if (!addr)
-      goto out_free;
-    ohdr->seq = short_be((uint16_t)(odev->pkt_counter)++);
-    bcast.addr = (addr->netmask.addr & addr->address.addr) | (~addr->netmask.addr);
-    if ( 0 > pico_socket_sendto(udpsock, p->buf, p->len, &bcast, OLSR_PORT) ) {
-      dbg("olsr send\n");
-    }
-  } else {
-    while(pdev) { 
-      ohdr->seq = short_be((uint16_t)(pdev->pkt_counter++));
-      addr = pico_ipv4_link_by_dev(pdev->dev);
-      if (!addr)
-        continue;
-      bcast.addr = (addr->netmask.addr & addr->address.addr) | (~addr->netmask.addr);
-      if ( 0 > pico_socket_sendto(udpsock, p->buf, p->len, &bcast, OLSR_PORT) ) {
-        dbg("olsr send\n");
-      }
-      pdev = pdev->next;
-    }
-  }
 
 out_free:
-  pico_free(p->buf);
-  buffer_mem_used -= DGRAM_MAX_SIZE;
-  pico_free(p);
+    pico_free(p->buf);
+    buffer_mem_used -= DGRAM_MAX_SIZE;
+    pico_free(p);
 }
 
 static void olsr_scheduled_output(uint32_t when, void *buffer, uint16_t size, struct pico_device *pdev)
 {
-  struct olsr_fwd_pkt *p;
-  if ((buffer_mem_used + DGRAM_MAX_SIZE) > MAX_OLSR_MEM)
-    return;
+    struct olsr_fwd_pkt *p;
+    if ((buffer_mem_used + DGRAM_MAX_SIZE) > MAX_OLSR_MEM)
+        return;
 
-  p = pico_zalloc(sizeof(struct olsr_fwd_pkt));
-  if (!p) {
-    pico_free(buffer);
-    return;
-  }
-  p->buf = buffer;
-  p->len = size;
-  p->pdev = pdev;
-  buffer_mem_used += DGRAM_MAX_SIZE;
-  pico_timer_add(1 + when - ((pico_rand() % OLSR_MAXJITTER)), &olsr_process_out, p);
+    p = pico_zalloc(sizeof(struct olsr_fwd_pkt));
+    if (!p) {
+        pico_free(buffer);
+        return;
+    }
+
+    p->buf = buffer;
+    p->len = size;
+    p->pdev = pdev;
+    buffer_mem_used += DGRAM_MAX_SIZE;
+    pico_timer_add(1 + when - ((pico_rand() % OLSR_MAXJITTER)), &olsr_process_out, p);
 }
 
 
 static void refresh_routes(void)
 {
-  struct olsr_route_entry *local, *neighbor = NULL;
-  struct olsr_dev_entry *icur = Local_devices;
-
-  /* Refresh local entries */
+    struct olsr_route_entry *local, *neighbor = NULL;
+    struct olsr_dev_entry *icur = Local_devices;
 
-  /* Step 1: set zero expire time for local addresses and neighbors*/
-  local = Local_interfaces;
-  while(local) {
-    local->time_left = 0;
-    neighbor = local->children;
-    while (neighbor && (neighbor->metric < 2)) {
-      //dbg("Setting to zero. Neigh: %08x metric %d\n", neighbor->destination, neighbor->metric);
-      neighbor->time_left = 0;
-      neighbor = neighbor->next;
-    }
-    local = local->next;
-  }
+    /* Refresh local entries */
 
-  /* Step 2: refresh timer for entries that are still valid. 
-   * Add new entries.
-   */
-  while(icur) {
-    struct pico_ipv4_link *lnk = NULL;
-    do { 
-      lnk = pico_ipv4_link_by_dev_next(icur->dev, lnk);
-      if (!lnk) break;
-      local = olsr_get_ethentry(icur->dev);
-      if (local) {
-        local->time_left = (OLSR_HELLO_INTERVAL << 2);
-      } else if (lnk) {
-        struct olsr_route_entry *e = pico_zalloc(sizeof (struct olsr_route_entry));
-        if (!e) {
-          dbg("olsr: adding local route entry\n");
-          return;
+    /* Step 1: set zero expire time for local addresses and neighbors*/
+    local = Local_interfaces;
+    while(local) {
+        local->time_left = 0;
+        neighbor = local->children;
+        while (neighbor && (neighbor->metric < 2)) {
+            /* dbg("Setting to zero. Neigh: %08x metric %d\n", neighbor->destination, neighbor->metric); */
+            neighbor->time_left = 0;
+            neighbor = neighbor->next;
         }
-        e->destination.addr = lnk->address.addr; /* Always pick the first address */
-        e->time_left = (OLSR_HELLO_INTERVAL << 2);
-        e->iface = icur->dev;
-        e->metric = 0;
-        e->lq = 0xFF;
-        e->nlq = 0xFF;
-        e->next = Local_interfaces;
-        Local_interfaces = e;
-      }
-    } while (lnk);
+        local = local->next;
+    }
+    /* Step 2: refresh timer for entries that are still valid.
+     * Add new entries.
+     */
+    while(icur) {
+        struct pico_ipv4_link *lnk = NULL;
+        do {
+            lnk = pico_ipv4_link_by_dev_next(icur->dev, lnk);
+            if (!lnk) break;
 
-    refresh_neighbors(icur->dev);
-    icur = icur->next;
-  }
+            local = olsr_get_ethentry(icur->dev);
+            if (local) {
+                local->time_left = (OLSR_HELLO_INTERVAL << 2);
+            } else if (lnk) {
+                struct olsr_route_entry *e = pico_zalloc(sizeof (struct olsr_route_entry));
+                if (!e) {
+                    dbg("olsr: adding local route entry\n");
+                    return;
+                }
+
+                e->destination.addr = lnk->address.addr; /* Always pick the first address */
+                e->time_left = (OLSR_HELLO_INTERVAL << 2);
+                e->iface = icur->dev;
+                e->metric = 0;
+                e->lq = 0xFF;
+                e->nlq = 0xFF;
+                e->next = Local_interfaces;
+                Local_interfaces = e;
+            }
+        } while (lnk);
+
+        refresh_neighbors(icur->dev);
+        icur = icur->next;
+    }
 }
 
 static uint32_t olsr_build_hello_neighbors(uint8_t *buf, uint32_t size)
 {
-  uint32_t ret = 0;
-  struct olsr_route_entry *local, *neighbor;
-  struct olsr_neighbor *dst = (struct olsr_neighbor *) buf;
-  local = Local_interfaces;
-  while (local) {
-    neighbor = local->children;
-    while (neighbor) {
-      struct olsr_link *li = (struct olsr_link *) (buf + ret);
-      li->link_code = neighbor->link_type;
-      li->reserved = 0;
-      li->link_msg_size = short_be(sizeof(struct olsr_neighbor) + sizeof(struct olsr_link));
-      ret += (uint32_t)sizeof(struct olsr_link);
-      dst = (struct olsr_neighbor *) (buf+ret);
-      dst->addr = neighbor->destination.addr;
-      dst->nlq = neighbor->nlq;
-      dst->lq = neighbor->lq;
-      dst->reserved = 0;
-      ret += (uint32_t)sizeof(struct olsr_neighbor);
-      if (ret >= size)
-        return (uint32_t)((uint32_t)(ret - sizeof(struct olsr_neighbor)) - sizeof(struct olsr_link));
-      neighbor = neighbor->next;
+    uint32_t ret = 0;
+    struct olsr_route_entry *local, *neighbor;
+    struct olsr_neighbor *dst = (struct olsr_neighbor *) buf;
+    local = Local_interfaces;
+    while (local) {
+        neighbor = local->children;
+        while (neighbor) {
+            struct olsr_link *li = (struct olsr_link *) (buf + ret);
+            li->link_code = neighbor->link_type;
+            li->reserved = 0;
+            li->link_msg_size = short_be(sizeof(struct olsr_neighbor) + sizeof(struct olsr_link));
+            ret += (uint32_t)sizeof(struct olsr_link);
+            dst = (struct olsr_neighbor *) (buf + ret);
+            dst->addr = neighbor->destination.addr;
+            dst->nlq = neighbor->nlq;
+            dst->lq = neighbor->lq;
+            dst->reserved = 0;
+            ret += (uint32_t)sizeof(struct olsr_neighbor);
+            if (ret >= size)
+                return (uint32_t)((uint32_t)(ret - sizeof(struct olsr_neighbor)) - sizeof(struct olsr_link));
+
+            neighbor = neighbor->next;
+        }
+        local = local->next;
     }
-    local = local->next;
-  }
-  return ret;
+    return ret;
 }
 
 static uint32_t olsr_build_tc_neighbors(uint8_t *buf, uint32_t size)
 {
-  uint32_t ret = 0;
-  struct olsr_route_entry *local, *neighbor;
-  struct olsr_neighbor *dst = (struct olsr_neighbor *) buf;
-  local = Local_interfaces;
-  while (local) {
-    neighbor = local->children;
-    while (neighbor) {
-      dst->addr = neighbor->destination.addr;
-      dst->nlq = neighbor->nlq;
-      dst->lq = neighbor->lq;
-      dst->reserved = 0;
-      ret += (uint32_t)sizeof(struct olsr_neighbor);
-      dst = (struct olsr_neighbor *) (buf + ret);
-      if (ret >= size)
-        return (uint32_t)(ret - sizeof(struct olsr_neighbor));
-      neighbor = neighbor->next;
+    uint32_t ret = 0;
+    struct olsr_route_entry *local, *neighbor;
+    struct olsr_neighbor *dst = (struct olsr_neighbor *) buf;
+    local = Local_interfaces;
+    while (local) {
+        neighbor = local->children;
+        while (neighbor) {
+            dst->addr = neighbor->destination.addr;
+            dst->nlq = neighbor->nlq;
+            dst->lq = neighbor->lq;
+            dst->reserved = 0;
+            ret += (uint32_t)sizeof(struct olsr_neighbor);
+            dst = (struct olsr_neighbor *) (buf + ret);
+            if (ret >= size)
+                return (uint32_t)(ret - sizeof(struct olsr_neighbor));
+
+            neighbor = neighbor->next;
+        }
+        local = local->next;
     }
-    local = local->next;
-  }
-  return ret;
+    return ret;
 }
 
 static uint32_t olsr_build_mid(uint8_t *buf, uint32_t size, struct pico_device *excluded)
 {
-  uint32_t ret = 0;
-  struct olsr_route_entry *local;
-  struct pico_ip4 *dst = (struct pico_ip4 *) buf;
-  local = Local_interfaces;
-  while (local) {
-    if (local->iface != excluded) {
-      dst->addr = local->destination.addr;
-      ret += (uint32_t)sizeof(uint32_t);
-      dst = (struct pico_ip4 *) (buf + ret);
-      if (ret >= size)
-        return (uint32_t)(ret - sizeof(uint32_t));
+    uint32_t ret = 0;
+    struct olsr_route_entry *local;
+    struct pico_ip4 *dst = (struct pico_ip4 *) buf;
+    local = Local_interfaces;
+    while (local) {
+        if (local->iface != excluded) {
+            dst->addr = local->destination.addr;
+            ret += (uint32_t)sizeof(uint32_t);
+            dst = (struct pico_ip4 *) (buf + ret);
+            if (ret >= size)
+                return (uint32_t)(ret - sizeof(uint32_t));
+        }
+
+        local = local->next;
     }
-    local = local->next;
-  }
-  return ret;
+    return ret;
 }
 
 static void olsr_make_dgram(struct pico_device *pdev, int full)
 {
-  uint8_t * dgram;
-  uint32_t size = 0, r;
-  struct pico_ipv4_link *ep;
-  struct olsrmsg *msg_hello, *msg_mid, *msg_tc;
-  struct olsr_hmsg_hello *hello;
-  struct olsr_hmsg_tc *tc;
-  static uint16_t msg_counter; /* Global message sequence number */
-  uint32_t interval = OLSR_HELLO_INTERVAL;
-
-  dgram = pico_zalloc(DGRAM_MAX_SIZE);
-  if (!dgram)
-    return;
-
-  size += (uint32_t)sizeof(struct olsrhdr);
-  ep = pico_ipv4_link_by_dev(pdev);
-  if (!ep) {
-    pico_free(dgram);
-    return;
-  }
+    uint8_t *dgram;
+    uint32_t size = 0, r;
+    struct pico_ipv4_link *ep;
+    struct olsrmsg *msg_hello, *msg_mid, *msg_tc;
+    struct olsr_hmsg_hello *hello;
+    struct olsr_hmsg_tc *tc;
+    static uint16_t msg_counter; /* Global message sequence number */
+    uint32_t interval = OLSR_HELLO_INTERVAL;
 
-  if (!full) {
-    /* HELLO Message */
+    dgram = pico_zalloc(DGRAM_MAX_SIZE);
+    if (!dgram)
+        return;
 
-    msg_hello = (struct olsrmsg *) (dgram + size);
-    size += (uint32_t)sizeof(struct olsrmsg);
-    msg_hello->type = OLSRMSG_HELLO;
-    msg_hello->vtime = seconds2olsr(DEFAULT_VTIME);
-    msg_hello->orig.addr = ep->address.addr;
-    msg_hello->ttl = 1;
-    msg_hello->hop = 0;
-    msg_hello->seq = short_be(msg_counter++);
-    hello = (struct olsr_hmsg_hello *)(dgram + size);
-    size += (uint32_t)sizeof(struct olsr_hmsg_hello);
-    hello->reserved = 0;
-    hello->htime = seconds2olsr(OLSR_HELLO_INTERVAL);
-    hello->htime = 0x05; /* Todo: find and define values */
-    hello->willingness = 0x07;
-    r = olsr_build_hello_neighbors(dgram + size, DGRAM_MAX_SIZE - size);
-    if (r == 0) {
-      //dbg("Building hello message\n");
-      pico_free(dgram);
-      return;
+    size += (uint32_t)sizeof(struct olsrhdr);
+    ep = pico_ipv4_link_by_dev(pdev);
+    if (!ep) {
+        pico_free(dgram);
+        return;
     }
-    size += r;
-    msg_hello->size = short_be((uint16_t)(sizeof(struct olsrmsg) + sizeof(struct olsr_hmsg_hello) + r));
+
+    if (!full) {
+        /* HELLO Message */
 
-  } else {
-    /* MID Message */
-  
-    msg_mid = (struct olsrmsg *)(dgram + size);
-    size += (uint32_t)sizeof(struct olsrmsg);
-    msg_mid->type = OLSRMSG_MID;
-    msg_mid->vtime = seconds2olsr(60);
-    msg_mid->orig.addr = ep->address.addr;
-    msg_mid->ttl = 0xFF;
-    msg_mid->hop = 0;
-    msg_mid->seq = short_be(msg_counter++);
-    r = olsr_build_mid(dgram + size, DGRAM_MAX_SIZE - size, pdev);
-    if (r == 0) {
-      size -= (uint32_t)sizeof(struct olsrmsg);
+        msg_hello = (struct olsrmsg *) (dgram + size);
+        size += (uint32_t)sizeof(struct olsrmsg);
+        msg_hello->type = OLSRMSG_HELLO;
+        msg_hello->vtime = seconds2olsr(DEFAULT_VTIME);
+        msg_hello->orig.addr = ep->address.addr;
+        msg_hello->ttl = 1;
+        msg_hello->hop = 0;
+        msg_hello->seq = short_be(msg_counter++);
+        hello = (struct olsr_hmsg_hello *)(dgram + size);
+        size += (uint32_t)sizeof(struct olsr_hmsg_hello);
+        hello->reserved = 0;
+        hello->htime = seconds2olsr(OLSR_HELLO_INTERVAL);
+        hello->htime = 0x05; /* Todo: find and define values */
+        hello->willingness = 0x07;
+        r = olsr_build_hello_neighbors(dgram + size, DGRAM_MAX_SIZE - size);
+        if (r == 0) {
+            /* dbg("Building hello message\n"); */
+            pico_free(dgram);
+            return;
+        }
+
+        size += r;
+        msg_hello->size = short_be((uint16_t)(sizeof(struct olsrmsg) + sizeof(struct olsr_hmsg_hello) + r));
+
     } else {
-      size += r;
-      msg_mid->size = short_be((uint16_t)(sizeof(struct olsrmsg) + r));
-    }
-  
-    msg_tc = (struct olsrmsg *) (dgram + size);
-    size += (uint32_t)sizeof(struct olsrmsg);
-    msg_tc->type = OLSRMSG_TC;
-    msg_tc->vtime = seconds2olsr(DEFAULT_VTIME); 
-    msg_tc->orig.addr = ep->address.addr;
-    msg_tc->ttl = 0xFF;
-    msg_tc->hop = 0;
-    msg_tc->seq = short_be(msg_counter++);
-    tc = (struct olsr_hmsg_tc *)(dgram + size);
-    size += (uint32_t)sizeof(struct olsr_hmsg_tc);
-    tc->ansn = short_be(my_ansn);
-    r = olsr_build_tc_neighbors(dgram + size, DGRAM_MAX_SIZE  - size);
-    size += r;
-    msg_tc->size = short_be((uint16_t)(sizeof(struct olsrmsg) + sizeof(struct olsr_hmsg_tc) + r));
-    interval = OLSR_TC_INTERVAL;
-  } /*if full */
-  
+        /* MID Message */
+
+        msg_mid = (struct olsrmsg *)(dgram + size);
+        size += (uint32_t)sizeof(struct olsrmsg);
+        msg_mid->type = OLSRMSG_MID;
+        msg_mid->vtime = seconds2olsr(60);
+        msg_mid->orig.addr = ep->address.addr;
+        msg_mid->ttl = 0xFF;
+        msg_mid->hop = 0;
+        msg_mid->seq = short_be(msg_counter++);
+        r = olsr_build_mid(dgram + size, DGRAM_MAX_SIZE - size, pdev);
+        if (r == 0) {
+            size -= (uint32_t)sizeof(struct olsrmsg);
+        } else {
+            size += r;
+            msg_mid->size = short_be((uint16_t)(sizeof(struct olsrmsg) + r));
+        }
 
-  /* Send the thing out */
-  olsr_scheduled_output(interval, dgram, (uint16_t)size, pdev );
+        msg_tc = (struct olsrmsg *) (dgram + size);
+        size += (uint32_t)sizeof(struct olsrmsg);
+        msg_tc->type = OLSRMSG_TC;
+        msg_tc->vtime = seconds2olsr(DEFAULT_VTIME);
+        msg_tc->orig.addr = ep->address.addr;
+        msg_tc->ttl = 0xFF;
+        msg_tc->hop = 0;
+        msg_tc->seq = short_be(msg_counter++);
+        tc = (struct olsr_hmsg_tc *)(dgram + size);
+        size += (uint32_t)sizeof(struct olsr_hmsg_tc);
+        tc->ansn = short_be(my_ansn);
+        r = olsr_build_tc_neighbors(dgram + size, DGRAM_MAX_SIZE  - size);
+        size += r;
+        msg_tc->size = short_be((uint16_t)(sizeof(struct olsrmsg) + sizeof(struct olsr_hmsg_tc) + r));
+        interval = OLSR_TC_INTERVAL;
+    } /*if full */
+
+    /* Send the thing out */
+    olsr_scheduled_output(interval, dgram, (uint16_t)size, pdev );
 }
 
 static inline void arp_storm(struct pico_ip4 *addr)
 {
-  struct olsr_dev_entry *icur = Local_devices;
-  while(icur) {
-    pico_arp_request(icur->dev, addr, PICO_ARP_QUERY);
-    icur = icur->next;
-  }
+    struct olsr_dev_entry *icur = Local_devices;
+    while(icur) {
+        pico_arp_request(icur->dev, addr, PICO_ARP_QUERY);
+        icur = icur->next;
+    }
 }
 
-static void recv_mid(uint8_t *buffer, uint32_t len, struct olsr_route_entry *origin) 
+static void recv_mid(uint8_t *buffer, uint32_t len, struct olsr_route_entry *origin)
 {
-  uint32_t parsed = 0;
-  uint32_t *address;
-  struct olsr_route_entry *e;
+    uint32_t parsed = 0;
+    uint32_t *address;
+    struct olsr_route_entry *e;
 
-  if (len % sizeof(uint32_t)) /*drop*/
-    return;
-
-  while (len > parsed) {
-    address = (uint32_t *)(buffer + parsed);
-    e = get_route_by_address(Local_interfaces, *address);
-    if (!e) {
-      e = pico_zalloc(sizeof(struct olsr_route_entry));
-      if (!e) {
-        dbg("olsr allocating route\n");
+    if (len % sizeof(uint32_t)) /*drop*/
         return;
-      }
-      e->time_left = (OLSR_HELLO_INTERVAL << 2);
-      e->destination.addr = *address;
-      e->gateway = origin;
-      e->iface = origin->iface;
-      e->metric = (uint16_t)(origin->metric + 1u);
-      e->lq = origin->lq;
-      e->nlq = origin->nlq;
-      olsr_route_add(e);
-      arp_storm(&e->destination);
-    } else if (e->metric > (origin->metric + 1)) {
-      olsr_route_del(e);
-      e->metric = origin->metric;
-      e->gateway = origin;
-      olsr_route_add(e);
+
+    while (len > parsed) {
+        address = (uint32_t *)(buffer + parsed);
+        e = get_route_by_address(Local_interfaces, *address);
+        if (!e) {
+            e = pico_zalloc(sizeof(struct olsr_route_entry));
+            if (!e) {
+                dbg("olsr allocating route\n");
+                return;
+            }
+
+            e->time_left = (OLSR_HELLO_INTERVAL << 2);
+            e->destination.addr = *address;
+            e->gateway = origin;
+            e->iface = origin->iface;
+            e->metric = (uint16_t)(origin->metric + 1u);
+            e->lq = origin->lq;
+            e->nlq = origin->nlq;
+            olsr_route_add(e);
+            arp_storm(&e->destination);
+        } else if (e->metric > (origin->metric + 1)) {
+            olsr_route_del(e);
+            e->metric = origin->metric;
+            e->gateway = origin;
+            olsr_route_add(e);
+        }
+
+        parsed += (uint32_t)sizeof(uint32_t);
     }
-    parsed += (uint32_t)sizeof(uint32_t);
-  }
 }
 
 static void recv_hello(uint8_t *buffer, uint32_t len, struct olsr_route_entry *origin)
 {
-  struct olsr_link *li;
-  struct olsr_route_entry *e;
-  uint32_t parsed = 0;
-  struct olsr_neighbor *neigh;
-
-  if (!origin)
-    return;
+    struct olsr_link *li;
+    struct olsr_route_entry *e;
+    uint32_t parsed = 0;
+    struct olsr_neighbor *neigh;
 
-  while (len > parsed) {
-    li = (struct olsr_link *) buffer;
-    neigh = (struct olsr_neighbor *)(buffer + parsed + sizeof(struct olsr_link));
-    parsed += short_be(li->link_msg_size);
-    e = get_route_by_address(Local_interfaces, neigh->addr);
-    if (!e) {
-      e = pico_zalloc(sizeof(struct olsr_route_entry));
-      if (!e) {
-        dbg("olsr allocating route\n");
+    if (!origin)
         return;
-      }
-      e->time_left = (OLSR_HELLO_INTERVAL << 2);
-      e->destination.addr = neigh->addr;
-      e->gateway = origin;
-      e->iface = origin->iface;
-      e->metric = (uint16_t)(origin->metric + 1u);
-      e->link_type = OLSRLINK_UNKNOWN;
-      e->lq = MIN(origin->lq, neigh->lq);
-      e->nlq = MIN(origin->nlq, neigh->nlq);
-      olsr_route_add(e);
-      arp_storm(&e->destination);
-    } else if ((e->gateway != origin) && (e->metric > (origin->metric + 1))) {
-      olsr_route_del(e);
-      e->metric = (uint16_t)(origin->metric + 1u);
-      e->gateway = origin;
-      olsr_route_add(e);
+
+    while (len > parsed) {
+        li = (struct olsr_link *) buffer;
+        neigh = (struct olsr_neighbor *)(buffer + parsed + sizeof(struct olsr_link));
+        parsed += short_be(li->link_msg_size);
+        e = get_route_by_address(Local_interfaces, neigh->addr);
+        if (!e) {
+            e = pico_zalloc(sizeof(struct olsr_route_entry));
+            if (!e) {
+                dbg("olsr allocating route\n");
+                return;
+            }
+
+            e->time_left = (OLSR_HELLO_INTERVAL << 2);
+            e->destination.addr = neigh->addr;
+            e->gateway = origin;
+            e->iface = origin->iface;
+            e->metric = (uint16_t)(origin->metric + 1u);
+            e->link_type = OLSRLINK_UNKNOWN;
+            e->lq = MIN(origin->lq, neigh->lq);
+            e->nlq = MIN(origin->nlq, neigh->nlq);
+            olsr_route_add(e);
+            arp_storm(&e->destination);
+        } else if ((e->gateway != origin) && (e->metric > (origin->metric + 1))) {
+            olsr_route_del(e);
+            e->metric = (uint16_t)(origin->metric + 1u);
+            e->gateway = origin;
+            olsr_route_add(e);
+        }
     }
-  }
 }
 
 static uint32_t reconsider_topology(uint8_t *buf, uint32_t size, struct olsr_route_entry *e)
 {
-  struct olsr_hmsg_tc *tc = (struct olsr_hmsg_tc *) buf;
-  uint16_t new_ansn = short_be(tc->ansn);
-  uint32_t parsed = sizeof(struct olsr_hmsg_tc);
-  struct olsr_route_entry *rt;
-  struct olsr_neighbor *n;
-  uint32_t retval = 0;
-
-  if (!e->advertised_tc)
-    retval = 1;
+    struct olsr_hmsg_tc *tc = (struct olsr_hmsg_tc *) buf;
+    uint16_t new_ansn = short_be(tc->ansn);
+    uint32_t parsed = sizeof(struct olsr_hmsg_tc);
+    struct olsr_route_entry *rt;
+    struct olsr_neighbor *n;
+    uint32_t retval = 0;
 
-  if (e->advertised_tc && fresher(new_ansn, e->ansn))
-  {
-    pico_free(e->advertised_tc);
-    e->advertised_tc = NULL;
-    retval = 1;
-  }
+    if (!e->advertised_tc)
+        retval = 1;
 
-  if (!e->advertised_tc) {
-    e->advertised_tc = pico_zalloc(size);
-    if (!e->advertised_tc) {
-      dbg("Allocating forward packet\n");
-      return 0;
+    if (e->advertised_tc && fresher(new_ansn, e->ansn))
+    {
+        pico_free(e->advertised_tc);
+        e->advertised_tc = NULL;
+        retval = 1;
     }
-    memcpy(e->advertised_tc, buf, size);
-    e->ansn = new_ansn;
-    while (parsed < size) {
-      n = (struct olsr_neighbor *) (buf + parsed);
-      parsed += (uint32_t)sizeof(struct olsr_neighbor);
-      rt = get_route_by_address(Local_interfaces, n->addr);
-      if (rt && (rt->gateway == e)) {
-        /* Refresh existing node */
-        rt->time_left = e->time_left;
-      } else if (!rt || (rt->metric > (e->metric + 1)) || (rt->nlq < n->nlq)) {
-        if (!rt) {
-          rt = pico_zalloc(sizeof (struct olsr_route_entry));
-          rt->destination.addr = n->addr;
-          rt->link_type = OLSRLINK_UNKNOWN;
-        } else {
-          olsr_route_del(rt);
+
+    if (!e->advertised_tc) {
+        e->advertised_tc = pico_zalloc(size);
+        if (!e->advertised_tc) {
+            dbg("Allocating forward packet\n");
+            return 0;
         }
-        rt->iface = e->iface;
-        rt->gateway = e;
-        rt->metric = (uint16_t)(e->metric + 1);
-        rt->lq = n->lq;
-        rt->nlq = n->nlq;
-        rt->time_left = e->time_left;
-        olsr_route_add(rt);
-      }
+
+        memcpy(e->advertised_tc, buf, size);
+        e->ansn = new_ansn;
+        while (parsed < size) {
+            n = (struct olsr_neighbor *) (buf + parsed);
+            parsed += (uint32_t)sizeof(struct olsr_neighbor);
+            rt = get_route_by_address(Local_interfaces, n->addr);
+            if (rt && (rt->gateway == e)) {
+                /* Refresh existing node */
+                rt->time_left = e->time_left;
+            } else if (!rt || (rt->metric > (e->metric + 1)) || (rt->nlq < n->nlq)) {
+                if (!rt) {
+                    rt = pico_zalloc(sizeof (struct olsr_route_entry));
+                    rt->destination.addr = n->addr;
+                    rt->link_type = OLSRLINK_UNKNOWN;
+                } else {
+                    olsr_route_del(rt);
+                }
+
+                rt->iface = e->iface;
+                rt->gateway = e;
+                rt->metric = (uint16_t)(e->metric + 1);
+                rt->lq = n->lq;
+                rt->nlq = n->nlq;
+                rt->time_left = e->time_left;
+                olsr_route_add(rt);
+            }
+        }
+        /* dbg("Routes changed...\n"); */
     }
-    //dbg("Routes changed...\n");
-  }
-  return retval;
+
+    return retval;
 }
 
 
 static void olsr_recv(uint8_t *buffer, uint32_t len)
 {
-  struct olsrmsg *msg;
-  struct olsrhdr *oh = (struct olsrhdr *) buffer;
-  struct olsr_route_entry *ancestor;
-  uint32_t parsed = 0;
-  uint16_t outsize = 0;
-  uint8_t *datagram;
-
-  if (len != short_be(oh->len)) {
-    return;
-  }
-
-  /* RFC 3626, section 3.4, if a packet is too small, it is silently discarded */
-  if (len < 16) {
-    return;
-  }
+    struct olsrmsg *msg;
+    struct olsrhdr *oh = (struct olsrhdr *) buffer;
+    struct olsr_route_entry *ancestor;
+    uint32_t parsed = 0;
+    uint16_t outsize = 0;
+    uint8_t *datagram;
 
-  parsed += (uint32_t)sizeof(struct olsrhdr); 
-
-  datagram = pico_zalloc(DGRAM_MAX_SIZE);
-  if (!datagram)
-    return;
-  outsize = (uint16_t) (outsize + (sizeof(struct olsrhdr)));
-
-  /* Section 1: parsing received messages. */
-
-  while (len > parsed) {
-    struct olsr_route_entry *origin;
-    msg = (struct olsrmsg *) (buffer + parsed);
-    origin = get_route_by_address(Local_interfaces, msg->orig.addr);
-
-    if(pico_ipv4_link_find(&msg->orig) != NULL) {
-      //dbg("rebound\n");
-      parsed += short_be(msg->size);
-      continue;
+    if (len != short_be(oh->len)) {
+        return;
     }
 
-    /* OLSR's TTL expired. */
-    if (msg->ttl < 1u) {
-      parsed += short_be(msg->size);
-      continue;
-    }
-
-    if (!origin) {
-      arp_storm(&msg->orig);
-      parsed += short_be(msg->size);
-      continue;
+    /* RFC 3626, section 3.4, if a packet is too small, it is silently discarded */
+    if (len < 16) {
+        return;
     }
 
-    /* We know this is a Master host and a neighbor */
-    origin->link_type = OLSRLINK_MPR;
-    origin->time_left = olsr2seconds(msg->vtime);
-    switch(msg->type) {
-      case OLSRMSG_HELLO:
-        ancestor = olsr_get_ethentry(origin->iface);
-        if ((origin->metric > 1) && ancestor) {
-          olsr_route_del(origin);
-          origin->gateway = ancestor;
-          origin->metric = 1;
-          olsr_route_add(origin);
+    parsed += (uint32_t)sizeof(struct olsrhdr);
+
+    datagram = pico_zalloc(DGRAM_MAX_SIZE);
+    if (!datagram)
+        return;
+
+    outsize = (uint16_t) (outsize + (sizeof(struct olsrhdr)));
+
+    /* Section 1: parsing received messages. */
+
+    while (len > parsed) {
+        struct olsr_route_entry *origin;
+        msg = (struct olsrmsg *) (buffer + parsed);
+        origin = get_route_by_address(Local_interfaces, msg->orig.addr);
+
+        if(pico_ipv4_link_find(&msg->orig) != NULL) {
+            /* dbg("rebound\n"); */
+            parsed += short_be(msg->size);
+            continue;
         }
-        recv_hello(buffer + (uint32_t)parsed + (uint32_t)sizeof(struct olsrmsg) + (uint32_t)sizeof(struct olsr_hmsg_hello),
-          (uint32_t) ((short_be(msg->size) - (sizeof(struct olsrmsg))) - (uint32_t)sizeof(struct olsr_hmsg_hello)),
-          origin);
-        msg->ttl = 0;
-        break;
-      case OLSRMSG_MID:
-        if ((origin->seq != 0) && (!fresher(short_be(msg->seq), origin->seq))) {
-          msg->ttl = 0; 
-        } else {
-          recv_mid(buffer + parsed + sizeof(struct olsrmsg), (uint32_t)(short_be(msg->size) - (sizeof(struct olsrmsg))), origin);
-          //dbg("MID forwarded from origin %08x (seq: %u)\n", long_be(msg->orig.addr), short_be(msg->seq));
-          origin->seq = short_be(msg->seq);
+
+        /* OLSR's TTL expired. */
+        if (msg->ttl < 1u) {
+            parsed += short_be(msg->size);
+            continue;
+        }
+
+        if (!origin) {
+            arp_storm(&msg->orig);
+            parsed += short_be(msg->size);
+            continue;
         }
-        break;
-      case OLSRMSG_TC:
-        reconsider_topology(buffer + parsed + sizeof(struct olsrmsg), (uint32_t)(short_be(msg->size) - (sizeof(struct olsrmsg))), origin);
-        if ((origin->seq != 0) && (!fresher(short_be(msg->seq), origin->seq))) {
-          msg->ttl = 0; 
-        } else {
-          //dbg("TC forwarded from origin %08x (seq: %u)\n", long_be(msg->orig.addr), short_be(msg->seq));
-          origin->seq = short_be(msg->seq);
+
+        /* We know this is a Master host and a neighbor */
+        origin->link_type = OLSRLINK_MPR;
+        origin->time_left = olsr2seconds(msg->vtime);
+        switch(msg->type) {
+        case OLSRMSG_HELLO:
+            ancestor = olsr_get_ethentry(origin->iface);
+            if ((origin->metric > 1) && ancestor) {
+                olsr_route_del(origin);
+                origin->gateway = ancestor;
+                origin->metric = 1;
+                olsr_route_add(origin);
+            }
+
+            recv_hello(buffer + (uint32_t)parsed + (uint32_t)sizeof(struct olsrmsg) + (uint32_t)sizeof(struct olsr_hmsg_hello),
+                       (uint32_t) ((short_be(msg->size) - (sizeof(struct olsrmsg))) - (uint32_t)sizeof(struct olsr_hmsg_hello)),
+                       origin);
+            msg->ttl = 0;
+            break;
+        case OLSRMSG_MID:
+            if ((origin->seq != 0) && (!fresher(short_be(msg->seq), origin->seq))) {
+                msg->ttl = 0;
+            } else {
+                recv_mid(buffer + parsed + sizeof(struct olsrmsg), (uint32_t)(short_be(msg->size) - (sizeof(struct olsrmsg))), origin);
+                /* dbg("MID forwarded from origin %08x (seq: %u)\n", long_be(msg->orig.addr), short_be(msg->seq)); */
+                origin->seq = short_be(msg->seq);
+            }
+
+            break;
+        case OLSRMSG_TC:
+            reconsider_topology(buffer + parsed + sizeof(struct olsrmsg), (uint32_t)(short_be(msg->size) - (sizeof(struct olsrmsg))), origin);
+            if ((origin->seq != 0) && (!fresher(short_be(msg->seq), origin->seq))) {
+                msg->ttl = 0;
+            } else {
+                /* dbg("TC forwarded from origin %08x (seq: %u)\n", long_be(msg->orig.addr), short_be(msg->seq)); */
+                origin->seq = short_be(msg->seq);
+            }
+
+            break;
+        default:
+            pico_free(datagram);
+            return;
         }
-        break;
-      default:
-        pico_free(datagram);
-        return;
+        if (msg->ttl > 1) {
+            msg->ttl--;
+            msg->hop++;
+            memcpy(datagram + outsize, msg, short_be(msg->size));
+            outsize = (uint16_t)(outsize + short_be(msg->size));
+        }
+
+        parsed += short_be(msg->size);
     }
-    if (msg->ttl > 1) {
-      msg->ttl--;
-      msg->hop++;
-      memcpy(datagram + outsize, msg, short_be(msg->size));
-      outsize = (uint16_t)(outsize + short_be(msg->size));
+    /* Section 2: forwarding parsed messages that got past the filter. */
+    if ((outsize > sizeof(struct olsrhdr))) {
+        /* Finalize FWD packet */
+        olsr_scheduled_output(OLSR_MAXJITTER, datagram, outsize, NULL);
+    } else {
+        /* Nothing to forward. */
+        pico_free(datagram);
     }
-    parsed += short_be(msg->size);
-  }
-
-
-  /* Section 2: forwarding parsed messages that got past the filter. */
-  if ((outsize > sizeof(struct olsrhdr))) {
-    /* Finalize FWD packet */
-    olsr_scheduled_output(OLSR_MAXJITTER, datagram, outsize, NULL);
-  } else {
-    /* Nothing to forward. */
-    pico_free(datagram);
-  }
 }
 
 static void wakeup(uint16_t ev, struct pico_socket *s)
 {
-  unsigned char *recvbuf; 
-  int r = 0;
-  struct pico_ip4 ANY = {0};
-  uint16_t port = OLSR_PORT;
-  recvbuf = pico_zalloc(DGRAM_MAX_SIZE);
-  if (!recvbuf)
-    return;
+    unsigned char *recvbuf;
+    int r = 0;
+    struct pico_ip4 ANY = {
+        0
+    };
+    uint16_t port = OLSR_PORT;
+    recvbuf = pico_zalloc(DGRAM_MAX_SIZE);
+    if (!recvbuf)
+        return;
 
-  if (ev & PICO_SOCK_EV_RD) {
-    r = pico_socket_recv(s, recvbuf, DGRAM_MAX_SIZE);
-    if (r > 0)
-      olsr_recv(recvbuf, (uint32_t)r);
-  }
+    if (ev & PICO_SOCK_EV_RD) {
+        r = pico_socket_recv(s, recvbuf, DGRAM_MAX_SIZE);
+        if (r > 0)
+            olsr_recv(recvbuf, (uint32_t)r);
+    }
 
-  if (ev == PICO_SOCK_EV_ERR) {
-    pico_socket_close(udpsock);
-    udpsock = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &wakeup);
-    if (udpsock)
-      pico_socket_bind(udpsock, &ANY, &port);
-  }
-  pico_free(recvbuf);
+    if (ev == PICO_SOCK_EV_ERR) {
+        pico_socket_close(udpsock);
+        udpsock = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &wakeup);
+        if (udpsock)
+            pico_socket_bind(udpsock, &ANY, &port);
+    }
+
+    pico_free(recvbuf);
 }
 
 static void olsr_hello_tick(pico_time when, void *unused)
 {
-  struct olsr_dev_entry *d;
-  (void)when;
-  (void)unused;
-  olsr_garbage_collector(Local_interfaces);
-  refresh_routes();
-  d = Local_devices;
-  while(d) {
-    olsr_make_dgram(d->dev, 0);
-    d = d->next;
-  }
-  pico_timer_add(OLSR_HELLO_INTERVAL, &olsr_hello_tick, NULL);
+    struct olsr_dev_entry *d;
+    (void)when;
+    (void)unused;
+    olsr_garbage_collector(Local_interfaces);
+    refresh_routes();
+    d = Local_devices;
+    while(d) {
+        olsr_make_dgram(d->dev, 0);
+        d = d->next;
+    }
+    pico_timer_add(OLSR_HELLO_INTERVAL, &olsr_hello_tick, NULL);
 }
 
 static void olsr_tc_tick(pico_time when, void *unused)
 {
-  struct olsr_dev_entry *d;
-  (void)when;
-  (void)unused;
-  d = Local_devices;
-  while(d) {
-    olsr_make_dgram(d->dev, 1);
-    d = d->next;
-  }
-  pico_timer_add(OLSR_TC_INTERVAL, &olsr_tc_tick, NULL);
+    struct olsr_dev_entry *d;
+    (void)when;
+    (void)unused;
+    d = Local_devices;
+    while(d) {
+        olsr_make_dgram(d->dev, 1);
+        d = d->next;
+    }
+    pico_timer_add(OLSR_TC_INTERVAL, &olsr_tc_tick, NULL);
 }
 
 
@@ -957,62 +997,68 @@
 
 void pico_olsr_init(void)
 {
-  struct pico_ip4 ANY = {0};
-  uint16_t port = OLSR_PORT;
-  dbg("OLSR initialized.\n");
-  if (!udpsock) {
-    udpsock = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &wakeup);
-    if (udpsock)
-      pico_socket_bind(udpsock, &ANY, &port);
-  }
-  pico_timer_add(100, &olsr_hello_tick, NULL);
-  pico_timer_add(1100, &olsr_tc_tick, NULL);
+    struct pico_ip4 ANY = {
+        0
+    };
+    uint16_t port = OLSR_PORT;
+    dbg("OLSR initialized.\n");
+    if (!udpsock) {
+        udpsock = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, &wakeup);
+        if (udpsock)
+            pico_socket_bind(udpsock, &ANY, &port);
+    }
+
+    pico_timer_add(100, &olsr_hello_tick, NULL);
+    pico_timer_add(1100, &olsr_tc_tick, NULL);
 }
 
 int pico_olsr_add(struct pico_device *dev)
 {
-  struct pico_ipv4_link *lnk = NULL;
-  struct olsr_dev_entry *od; 
-  if (!dev) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  dbg("OLSR: Adding device %s\n", dev->name);
-  od = pico_zalloc(sizeof(struct olsr_dev_entry));
-  if (!od) {
-     pico_err = PICO_ERR_ENOMEM;
-     return -1;
-  }
-  od->dev = dev;
-  od->next = Local_devices;
-  Local_devices = od;
+    struct pico_ipv4_link *lnk = NULL;
+    struct olsr_dev_entry *od;
+    if (!dev) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  do {
-      char ipaddr[20];
-    lnk = pico_ipv4_link_by_dev_next(dev, lnk);
-    if (lnk) {
-      struct olsr_route_entry *e = pico_zalloc(sizeof(struct olsr_route_entry));
-      //dbg("OLSR: Found IP address %08x\n", long_be(lnk->address.addr));
-      pico_ipv4_to_string(ipaddr, (lnk->address.addr));
-      dbg("OLSR: Found IP address %s\n", ipaddr);
-      if (!e) {
+    dbg("OLSR: Adding device %s\n", dev->name);
+    od = pico_zalloc(sizeof(struct olsr_dev_entry));
+    if (!od) {
         pico_err = PICO_ERR_ENOMEM;
         return -1;
-      }
-      e->destination.addr = lnk->address.addr;
-      e->link_type = OLSRLINK_SYMMETRIC;
-      e->time_left = (OLSR_HELLO_INTERVAL << 2);
-      e->gateway = NULL;
-      e->iface = dev;
-      e->metric = 0;
-      e->lq = 0xFF;
-      e->nlq = 0xFF;
-      e->next = Local_interfaces;
-      Local_interfaces = e;
+    }
+
+    od->dev = dev;
+    od->next = Local_devices;
+    Local_devices = od;
 
-    }
-  } while(lnk);
-  return 0;
+    do {
+        char ipaddr[20];
+        lnk = pico_ipv4_link_by_dev_next(dev, lnk);
+        if (lnk) {
+            struct olsr_route_entry *e = pico_zalloc(sizeof(struct olsr_route_entry));
+            /* dbg("OLSR: Found IP address %08x\n", long_be(lnk->address.addr)); */
+            pico_ipv4_to_string(ipaddr, (lnk->address.addr));
+            dbg("OLSR: Found IP address %s\n", ipaddr);
+            if (!e) {
+                pico_err = PICO_ERR_ENOMEM;
+                return -1;
+            }
+
+            e->destination.addr = lnk->address.addr;
+            e->link_type = OLSRLINK_SYMMETRIC;
+            e->time_left = (OLSR_HELLO_INTERVAL << 2);
+            e->gateway = NULL;
+            e->iface = dev;
+            e->metric = 0;
+            e->lq = 0xFF;
+            e->nlq = 0xFF;
+            e->next = Local_interfaces;
+            Local_interfaces = e;
+
+        }
+    } while(lnk);
+    return 0;
 }
 
 #endif
--- a/modules/pico_olsr.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_olsr.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 #ifndef __PICO_OLSR_H
 #define __PICO_OLSR_H
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/pico_posix.c	Mon Dec 16 11:25:54 2013 +0100
@@ -0,0 +1,99 @@
+/*********************************************************************
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
+
+   Authors: Andrei Carp, Maarten Vandersteegen
+ *********************************************************************/
+
+#ifdef PICO_SUPPORT_THREADING
+
+#include <pthread.h>
+#include <semaphore.h>
+#include "pico_config.h"
+
+/* POSIX mutex implementation */
+void *pico_mutex_init(void)
+{
+    pthread_mutex_t *m;
+    m = (pthread_mutex_t *)pico_zalloc(sizeof(pthread_mutex_t));
+    pthread_mutex_init(m, NULL);
+    return m;
+}
+
+void pico_mutex_destroy(void *mux)
+{
+    pico_free(mux);
+    mux = NULL;
+}
+
+void pico_mutex_lock(void *mux)
+{
+    if (mux == NULL) return;
+
+    pthread_mutex_t *m = (pthread_mutex_t *)mux;
+    pthread_mutex_lock(m);
+}
+
+void pico_mutex_unlock(void *mux)
+{
+    if (mux == NULL) return;
+
+    pthread_mutex_t *m = (pthread_mutex_t *)mux;
+    pthread_mutex_unlock(m);
+}
+
+/* POSIX semaphore implementation */
+void *pico_sem_init(void)
+{
+    sem_t *s;
+    s = (sem_t *)pico_zalloc(sizeof(sem_t));
+    sem_init(s, 0, 0);
+    return s;
+}
+
+void pico_sem_destroy(void *sem)
+{
+    pico_free(sem);
+    sem = NULL;
+}
+
+void pico_sem_post(void *sem)
+{
+    if (sem == NULL) return;
+
+    sem_t *s = (sem_t *)sem;
+    sem_post(s);
+}
+
+int pico_sem_wait(void *sem, int timeout)
+{
+    struct timespec t;
+    if (sem == NULL) return 0;
+
+    sem_t *s = (sem_t *)sem;
+
+    if (timeout < 0) {
+        sem_wait(s);
+    } else {
+        clock_gettime(CLOCK_REALTIME, &t);
+        t.tv_sec += timeout / 1000;
+        t.tv_nsec += (timeout % 1000) * 1000000;
+        if (sem_timedwait(s, &t) == -1)
+            return -1;
+    }
+
+    return 0;
+}
+
+/* POSIX thread implementation */
+void *pico_thread_create(void *(*routine)(void *), void *arg)
+{
+    pthread_t *thread;
+    thread = (pthread_t *)pico_zalloc(sizeof(pthread_t));
+
+    if (pthread_create(thread, NULL, routine, arg) == -1)
+        return NULL;
+
+    return thread;
+}
+#endif  /* PICO_SUPPORT_THREADING */
--- a/modules/pico_simple_http.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_simple_http.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Author: Andrei Carp <andrei.carp@tass.be>
-*********************************************************************/
+   Author: Andrei Carp <andrei.carp@tass.be>
+ *********************************************************************/
 
 #include "pico_config.h"
 #include "pico_socket.h"
@@ -14,16 +14,16 @@
 /* The HTTP Server cannot be available without TCP support */
 #if (defined PICO_SUPPORT_HTTP) && (defined PICO_SUPPORT_IPV4) && (defined PICO_SUPPORT_TCP)
 
-#define HTTP_LISTEN_PORT	80u
-#define HTTP_BACKLOG 			5u
+#define HTTP_LISTEN_PORT    80u
+#define HTTP_BACKLOG            5u
 #define HTTP_HEADER_SIZE  256u
 
-#define HTTP_SUCCESS			0
-#define HTTP_ERROR				-1
+#define HTTP_SUCCESS            0
+#define HTTP_ERROR              -1
 
-static struct pico_socket * httpServer = NULL;
-static char   httpResponse[] =
-"HTTP/1.0 200 OK\r\n\
+static struct pico_socket *httpServer = NULL;
+static char httpResponse[] =
+    "HTTP/1.0 200 OK\r\n\
 Content-Type: text/html\r\n\
 \r\n\
 <html><head>\r\n\
@@ -35,92 +35,94 @@
 
 static void httpEventCbk(uint16_t ev, struct pico_socket *self)
 {
-	static struct pico_socket * client = NULL;
-	uint32_t peer;
-	uint16_t port;
-	int r;
-	char buffer[HTTP_HEADER_SIZE];
+    static struct pico_socket *client = NULL;
+    uint32_t peer;
+    uint16_t port;
+    int r;
+    char buffer[HTTP_HEADER_SIZE];
 
-	switch(ev)
-	{
-		case PICO_SOCK_EV_CONN :
-			if(!client)
-				client = pico_socket_accept(self, &peer, &port);
-			break;
+    switch(ev)
+    {
+    case PICO_SOCK_EV_CONN:
+        if(!client)
+            client = pico_socket_accept(self, &peer, &port);
+
+        break;
 
-		case PICO_SOCK_EV_RD:
-			// do not check http integrity, just mark that the http header has arrived
-			// prepare to send the response
-			r = pico_socket_recvfrom(self, buffer, HTTP_HEADER_SIZE, &peer, &port);
-			if(r>0 && memcmp(buffer,"GET",3u) == 0u)
-			{ // it is an http header asking for data, return data and close
-				pico_socket_write(self,httpResponse,sizeof(httpResponse));
-				pico_socket_close(self);
-			}
-			else
-			{
-				// kill the connection, invalid header
-				pico_socket_close(self);
-			}
-			break;
+    case PICO_SOCK_EV_RD:
+        /* do not check http integrity, just mark that the http header has arrived */
+        /* prepare to send the response */
+        r = pico_socket_recvfrom(self, buffer, HTTP_HEADER_SIZE, &peer, &port);
+        if(r > 0 && memcmp(buffer, "GET", 3u) == 0u)
+        {     /* it is an http header asking for data, return data and close */
+            pico_socket_write(self, httpResponse, sizeof(httpResponse));
+            pico_socket_close(self);
+        }
+        else
+        {
+            /* kill the connection, invalid header */
+            pico_socket_close(self);
+        }
 
-		case PICO_SOCK_EV_ERR:
-		case PICO_SOCK_EV_CLOSE:
-			// free the used socket
-			client = NULL;
-			break;
+        break;
 
-		default :
-			break;
-	}
+    case PICO_SOCK_EV_ERR:
+    case PICO_SOCK_EV_CLOSE:
+        /* free the used socket */
+        client = NULL;
+        break;
+
+    default:
+        break;
+    }
 }
 
-int pico_startHttpServer(struct pico_ip4 * address)
+int pico_startHttpServer(struct pico_ip4 *address)
 {
 
-	uint16_t localHttpPort = short_be(HTTP_LISTEN_PORT);
+    uint16_t localHttpPort = short_be(HTTP_LISTEN_PORT);
 
-	if(!pico_is_port_free(localHttpPort,PICO_PROTO_TCP, address, &pico_proto_ipv4))
-	{
-		pico_err = PICO_ERR_EADDRINUSE;
-		return HTTP_ERROR;
-	}
+    if(!pico_is_port_free(localHttpPort, PICO_PROTO_TCP, address, &pico_proto_ipv4))
+    {
+        pico_err = PICO_ERR_EADDRINUSE;
+        return HTTP_ERROR;
+    }
 
-	httpServer = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, httpEventCbk);
+    httpServer = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, httpEventCbk);
 
-	if(!httpServer)
-	{
-		pico_err = PICO_ERR_ENOMEM;
-		return HTTP_ERROR;
-	}
+    if(!httpServer)
+    {
+        pico_err = PICO_ERR_ENOMEM;
+        return HTTP_ERROR;
+    }
 
-	// both functions set the pico_err themselves.
-	if(pico_socket_bind(httpServer,address,&localHttpPort))
-		return HTTP_ERROR;
+    /* both functions set the pico_err themselves. */
+    if(pico_socket_bind(httpServer, address, &localHttpPort))
+        return HTTP_ERROR;
 
-	if(pico_socket_listen(httpServer,HTTP_BACKLOG))
-		return HTTP_ERROR;
+    if(pico_socket_listen(httpServer, HTTP_BACKLOG))
+        return HTTP_ERROR;
 
-	return HTTP_SUCCESS;
+    return HTTP_SUCCESS;
 }
 
 int pico_stopHttpServer(void)
 {
-	if(!httpServer)
-	{
-		pico_err = PICO_ERR_EINVAL;
-		return HTTP_ERROR;
-	}
+    if(!httpServer)
+    {
+        pico_err = PICO_ERR_EINVAL;
+        return HTTP_ERROR;
+    }
 
-	if(pico_socket_close(httpServer))
-	{
-		// no need to set the error here, function already set it
-		httpServer = NULL;
-		return HTTP_ERROR;
-	}
+    if(pico_socket_close(httpServer))
+    {
+        /* no need to set the error here, function already set it */
+        httpServer = NULL;
+        return HTTP_ERROR;
+    }
 
-	httpServer = NULL;
-	return HTTP_SUCCESS;
+    httpServer = NULL;
+    return HTTP_SUCCESS;
 }
 
 #endif
--- a/modules/pico_simple_http.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_simple_http.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,14 +1,14 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Author: Andrei Carp <andrei.carp@tass.be>
-*********************************************************************/
+   Author: Andrei Carp <andrei.carp@tass.be>
+ *********************************************************************/
 
 #ifndef PICO_SIMPLE_HTTP
 #define PICO_SIMPLE_HTTP
 
-extern int pico_startHttpServer(struct pico_ip4 * address);
+extern int pico_startHttpServer(struct pico_ip4 *address);
 extern int pico_stopHttpServer(void);
 
 #endif
--- a/modules/pico_slaacv4.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_slaacv4.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Authors: Bogdan Lupu
-*********************************************************************/
+   Authors: Bogdan Lupu
+ *********************************************************************/
 #include "pico_slaacv4.h"
 #include "pico_arp.h"
 #include "pico_constants.h"
@@ -16,29 +16,29 @@
 #define SLAACV4_MINRANGE 0x00000100
 #define SLAACV4_MAXRANGE 0x0000FD00
 
-#define SLAACV4_CREATE_IPV4(seed) (((seed % SLAACV4_MAXRANGE ) + SLAACV4_MINRANGE) & SLAACV4_MASK) | SLAACV4_ADDRESS
+#define SLAACV4_CREATE_IPV4(seed) (((seed % SLAACV4_MAXRANGE) + SLAACV4_MINRANGE) & SLAACV4_MASK) | SLAACV4_ADDRESS
 
-#define PROBE_WAIT           1 // delay between two tries during claim
-#define PROBE_NB             3 // number of probe packets during claim
-//#define PROBE_MIN  1
-//#define PROBE_MAX  2
-#define ANNOUNCE_WAIT        2 // delay before start announcing
-#define ANNOUNCE_NB          2 // number of announcement packets
-#define ANNOUNCE_INTERVAL    2 // time between announcement packets
-#define MAX_CONFLICTS       10 // max conflicts before rate limiting
-#define RATE_LIMIT_INTERVAL 60 // time between successive attempts
-#define DEFEND_INTERVAL     10 // minimum interval between defensive ARP
+#define PROBE_WAIT           1 /* delay between two tries during claim */
+#define PROBE_NB             3 /* number of probe packets during claim */
+/* #define PROBE_MIN  1 */
+/* #define PROBE_MAX  2 */
+#define ANNOUNCE_WAIT        2 /* delay before start announcing */
+#define ANNOUNCE_NB          2 /* number of announcement packets */
+#define ANNOUNCE_INTERVAL    2 /* time between announcement packets */
+#define MAX_CONFLICTS       10 /* max conflicts before rate limiting */
+#define RATE_LIMIT_INTERVAL 60 /* time between successive attempts */
+#define DEFEND_INTERVAL     10 /* minimum interval between defensive ARP */
 
-enum slaacv4_state{
-	SLAACV4_RESET = 0,
-	SLAACV4_CLAIMING,
-	SLAACV4_CLAIMED,
-	SLAACV4_ANNOUNCING,
-	SLAACV4_ERROR
+enum slaacv4_state {
+    SLAACV4_RESET = 0,
+    SLAACV4_CLAIMING,
+    SLAACV4_CLAIMED,
+    SLAACV4_ANNOUNCING,
+    SLAACV4_ERROR
 };
 
-struct slaacv4_cookie{
-	uint8_t state;
+struct slaacv4_cookie {
+    uint8_t state;
     uint8_t probe_try_nb;
     uint8_t conflict_nb;
     uint8_t announce_nb;
@@ -52,140 +52,147 @@
 
 static uint32_t pico_slaacv4_getip(struct pico_device *dev, uint8_t rand)
 {
-  uint32_t seed = 0;
-  if (dev->eth != NULL)
-  {
-    seed = pico_hash((const char *)dev->eth->mac.addr);
-  }
-  if (rand)
-  {
-    seed += pico_rand();
-  }
-  return SLAACV4_CREATE_IPV4(seed);
+    uint32_t seed = 0;
+    if (dev->eth != NULL)
+    {
+        seed = pico_hash((const char *)dev->eth->mac.addr);
+    }
+
+    if (rand)
+    {
+        seed += pico_rand();
+    }
+
+    return SLAACV4_CREATE_IPV4(seed);
 }
 
 static void pico_slaacv4_init_cookie(struct pico_ip4 *ip, struct pico_device *dev, struct slaacv4_cookie *ck, void (*cb)(struct pico_ip4 *ip,  uint8_t code))
 {
-  ck->state = SLAACV4_RESET;
-  ck->probe_try_nb = 0;
-  ck->conflict_nb = 0;
-  ck->announce_nb = 0;
-  ck->cb = cb;
-  ck->device = dev;
-  ck->ip.addr = ip->addr;
-  ck->timer = NULL;
+    ck->state = SLAACV4_RESET;
+    ck->probe_try_nb = 0;
+    ck->conflict_nb = 0;
+    ck->announce_nb = 0;
+    ck->cb = cb;
+    ck->device = dev;
+    ck->ip.addr = ip->addr;
+    ck->timer = NULL;
 }
 
 static void pico_slaacv4_cancel_timers(struct slaacv4_cookie *tmp)
 {
-  pico_timer_cancel(tmp->timer);
+    pico_timer_cancel(tmp->timer);
 
-  tmp->timer = NULL;
+    tmp->timer = NULL;
 }
 
 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};
+    struct slaacv4_cookie *tmp = (struct slaacv4_cookie *)arg;
+    struct pico_ip4 netmask = {
+        .addr = 0x0000FFFF
+    };
 
-  if (tmp->announce_nb < ANNOUNCE_NB)
-  {
-	pico_arp_request(tmp->device, &tmp->ip, PICO_ARP_ANNOUNCE);
-	tmp->announce_nb++;
-	tmp->timer = pico_timer_add(ANNOUNCE_INTERVAL*1000,pico_slaacv4_send_announce_timer, arg);
-  }
-  else
-  {
-	tmp->state = SLAACV4_CLAIMED;
-	pico_ipv4_link_add(tmp->device, tmp->ip, netmask);
-	if (tmp->cb != NULL)
-      tmp->cb(&tmp->ip, PICO_SLAACV4_SUCCESS);
-  }
+    if (tmp->announce_nb < ANNOUNCE_NB)
+    {
+        pico_arp_request(tmp->device, &tmp->ip, PICO_ARP_ANNOUNCE);
+        tmp->announce_nb++;
+        tmp->timer = pico_timer_add(ANNOUNCE_INTERVAL * 1000, pico_slaacv4_send_announce_timer, arg);
+    }
+    else
+    {
+        tmp->state = SLAACV4_CLAIMED;
+        pico_ipv4_link_add(tmp->device, tmp->ip, netmask);
+        if (tmp->cb != NULL)
+            tmp->cb(&tmp->ip, PICO_SLAACV4_SUCCESS);
+    }
 }
 
 static void pico_slaacv4_send_probe_timer(pico_time __attribute__((unused)) now, void *arg)
 {
 
-  struct slaacv4_cookie *tmp = (struct slaacv4_cookie *)arg;
+    struct slaacv4_cookie *tmp = (struct slaacv4_cookie *)arg;
 
-  if (tmp->probe_try_nb < PROBE_NB)
-  {
-    pico_arp_request(tmp->device, &tmp->ip, PICO_ARP_PROBE);
-    tmp->probe_try_nb++;
-    tmp->timer = pico_timer_add(PROBE_WAIT * 1000, pico_slaacv4_send_probe_timer, tmp);
-  }
-  else
-  {
-	tmp->state = SLAACV4_ANNOUNCING;
-	tmp->timer = pico_timer_add(ANNOUNCE_WAIT*1000,pico_slaacv4_send_announce_timer, arg);
-  }
+    if (tmp->probe_try_nb < PROBE_NB)
+    {
+        pico_arp_request(tmp->device, &tmp->ip, PICO_ARP_PROBE);
+        tmp->probe_try_nb++;
+        tmp->timer = pico_timer_add(PROBE_WAIT * 1000, pico_slaacv4_send_probe_timer, tmp);
+    }
+    else
+    {
+        tmp->state = SLAACV4_ANNOUNCING;
+        tmp->timer = pico_timer_add(ANNOUNCE_WAIT * 1000, pico_slaacv4_send_announce_timer, arg);
+    }
 }
 
 
 
 static void pico_slaacv4_receive_ipconflict(void)
 {
-  struct slaacv4_cookie *tmp = &slaacv4_local;
+    struct slaacv4_cookie *tmp = &slaacv4_local;
 
-  tmp->conflict_nb++;
-  pico_slaacv4_cancel_timers(tmp);
+    tmp->conflict_nb++;
+    pico_slaacv4_cancel_timers(tmp);
 
-  if(tmp->state == SLAACV4_CLAIMED)
-  {
-	pico_ipv4_link_del(tmp->device,tmp->ip);
-  }
+    if(tmp->state == SLAACV4_CLAIMED)
+    {
+        pico_ipv4_link_del(tmp->device, tmp->ip);
+    }
 
-  if (tmp->conflict_nb < MAX_CONFLICTS)
-  {
-	tmp->state = SLAACV4_CLAIMING;
-	tmp->probe_try_nb = 0;
-	tmp->announce_nb = 0;
-    tmp->ip.addr = long_be(pico_slaacv4_getip(tmp->device, (uint8_t)1));
-    pico_arp_register_ipconflict(&tmp->ip, &tmp->device->eth->mac, pico_slaacv4_receive_ipconflict);
-    pico_arp_request(tmp->device, &tmp->ip, PICO_ARP_PROBE);
-    tmp->probe_try_nb++;
-    tmp->timer = pico_timer_add(PROBE_WAIT * 1000, pico_slaacv4_send_probe_timer, tmp);
-  }
-  else
-  {
-    if (tmp->cb != NULL)
+    if (tmp->conflict_nb < MAX_CONFLICTS)
     {
-      tmp->cb(&tmp->ip, PICO_SLAACV4_ERROR);
+        tmp->state = SLAACV4_CLAIMING;
+        tmp->probe_try_nb = 0;
+        tmp->announce_nb = 0;
+        tmp->ip.addr = long_be(pico_slaacv4_getip(tmp->device, (uint8_t)1));
+        pico_arp_register_ipconflict(&tmp->ip, &tmp->device->eth->mac, pico_slaacv4_receive_ipconflict);
+        pico_arp_request(tmp->device, &tmp->ip, PICO_ARP_PROBE);
+        tmp->probe_try_nb++;
+        tmp->timer = pico_timer_add(PROBE_WAIT * 1000, pico_slaacv4_send_probe_timer, tmp);
     }
-    tmp->state = SLAACV4_ERROR;
-  }
+    else
+    {
+        if (tmp->cb != NULL)
+        {
+            tmp->cb(&tmp->ip, PICO_SLAACV4_ERROR);
+        }
+
+        tmp->state = SLAACV4_ERROR;
+    }
 
 }
 
 uint8_t pico_slaacv4_claimip(struct pico_device *dev, void (*cb)(struct pico_ip4 *ip,  uint8_t code))
 {
-  struct pico_ip4 ip;
+    struct pico_ip4 ip;
 
-  ip.addr = long_be(pico_slaacv4_getip(dev, 0));
+    ip.addr = long_be(pico_slaacv4_getip(dev, 0));
 
-  pico_slaacv4_init_cookie(&ip, dev, &slaacv4_local, cb);
-  pico_arp_register_ipconflict(&ip, &dev->eth->mac, pico_slaacv4_receive_ipconflict);
-  pico_arp_request(dev, &ip, PICO_ARP_PROBE);
-  slaacv4_local.state = SLAACV4_CLAIMING;
-  slaacv4_local.probe_try_nb++;
-  slaacv4_local.timer = pico_timer_add(PROBE_WAIT * 1000, pico_slaacv4_send_probe_timer, &slaacv4_local);
+    pico_slaacv4_init_cookie(&ip, dev, &slaacv4_local, cb);
+    pico_arp_register_ipconflict(&ip, &dev->eth->mac, pico_slaacv4_receive_ipconflict);
+    pico_arp_request(dev, &ip, PICO_ARP_PROBE);
+    slaacv4_local.state = SLAACV4_CLAIMING;
+    slaacv4_local.probe_try_nb++;
+    slaacv4_local.timer = pico_timer_add(PROBE_WAIT * 1000, pico_slaacv4_send_probe_timer, &slaacv4_local);
 
-  return 0;
+    return 0;
 }
 
 void pico_slaacv4_unregisterip(void)
 {
-  struct slaacv4_cookie *tmp = &slaacv4_local;
-  struct pico_ip4 empty = { .addr = 0x00000000 };
+    struct slaacv4_cookie *tmp = &slaacv4_local;
+    struct pico_ip4 empty = {
+        .addr = 0x00000000
+    };
 
-  if (tmp->state == SLAACV4_CLAIMED)
-  {
-	pico_ipv4_link_del(tmp->device,tmp->ip);
-  }
+    if (tmp->state == SLAACV4_CLAIMED)
+    {
+        pico_ipv4_link_del(tmp->device, tmp->ip);
+    }
 
-  pico_slaacv4_cancel_timers(tmp);
-  pico_slaacv4_init_cookie(&empty, NULL, tmp, NULL);
-  pico_arp_register_ipconflict(&tmp->ip, NULL, NULL);
+    pico_slaacv4_cancel_timers(tmp);
+    pico_slaacv4_init_cookie(&empty, NULL, tmp, NULL);
+    pico_arp_register_ipconflict(&tmp->ip, NULL, NULL);
 
 }
 
--- a/modules/pico_slaacv4.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_slaacv4.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Authors: Bogdan Lupu
-*********************************************************************/
+   Authors: Bogdan Lupu
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_SUPPORT_SLAACV4
 #define _INCLUDE_PICO_SUPPORT_SLAACV4
 #include "pico_arp.h"
--- a/modules/pico_tcp.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_tcp.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
-
-.
-
-Authors: Daniele Lacamera, Philippe Mariman
-*********************************************************************/
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
+
+   .
+
+   Authors: Daniele Lacamera, Philippe Mariman
+ *********************************************************************/
 
 #include "pico_tcp.h"
 #include "pico_config.h"
@@ -16,18 +16,18 @@
 #include "pico_queue.h"
 #include "pico_tree.h"
 
-#define TCP_IS_STATE(s,st) (s->state & st)
+#define TCP_IS_STATE(s, st) (s->state & st)
 #define TCP_SOCK(s) ((struct pico_socket_tcp *)s)
-#define SEQN(f) ((f)?(long_be(((struct pico_tcp_hdr *)((f)->transport_hdr))->seq)):0)
-#define ACKN(f) ((f)?(long_be(((struct pico_tcp_hdr *)((f)->transport_hdr))->ack)):0)
+#define SEQN(f) ((f) ? (long_be(((struct pico_tcp_hdr *)((f)->transport_hdr))->seq)) : 0)
+#define ACKN(f) ((f) ? (long_be(((struct pico_tcp_hdr *)((f)->transport_hdr))->ack)) : 0)
 
 #define PICO_TCP_RTO_MIN 50
 #define PICO_TCP_RTO_MAX 120000
-#define PICO_TCP_IW 		 2
-#define PICO_TCP_SYN_TO	 1000u
+#define PICO_TCP_IW          2
+#define PICO_TCP_SYN_TO  1000u
 #define PICO_TCP_ZOMBIE_TO 30000
 
-#define PICO_TCP_MAX_RETRANS		 10
+#define PICO_TCP_MAX_RETRANS         10
 #define PICO_TCP_MAX_CONNECT_RETRIES 7
 
 #define PICO_TCP_LOOKAHEAD      0x00
@@ -45,450 +45,517 @@
 /* check if the hold queue contains data (again Nagle) */
 #define IS_TCP_HOLDQ_EMPTY(t)   (t->tcpq_hold.size == 0)
 
-#define IS_INPUT_QUEUE(q)  (q->pool.compare==input_segment_compare)
-#define TCP_INPUT_OVERHEAD (sizeof(struct tcp_input_segment)+sizeof(struct pico_tree_node))
+#define IS_INPUT_QUEUE(q)  (q->pool.compare == input_segment_compare)
+#define TCP_INPUT_OVERHEAD (sizeof(struct tcp_input_segment) + sizeof(struct pico_tree_node))
 
 
 #ifdef PICO_SUPPORT_TCP
-#define tcp_dbg_nagle(...) do{}while(0)
-#define tcp_dbg_options(...) do{}while(0)
-
-
-#define tcp_dbg(...) do{}while(0)
-//#define tcp_dbg dbg
+#define tcp_dbg_nagle(...) do {} while(0)
+#define tcp_dbg_options(...) do {} while(0)
+
+
+#define tcp_dbg(...) do {} while(0)
+/* #define tcp_dbg dbg */
 
 #ifdef PICO_SUPPORT_MUTEX
-static void * Mutex = NULL;
-#define LOCK(x) {\
-  if (x == NULL) \
-    x = pico_mutex_init(); \
-  pico_mutex_lock(x); \
+static void *Mutex = NULL;
+#define LOCK(x) { \
+        if (x == NULL) \
+            x = pico_mutex_init(); \
+        pico_mutex_lock(x); \
 }
-#define UNLOCK(x) pico_mutex_unlock(x);
+#define UNLOCK(x) pico_mutex_unlock(x)
 
 #else
-#define LOCK(x) do{}while(0)
-#define UNLOCK(x) do{}while(0)
+#define LOCK(x) do {} while(0)
+#define UNLOCK(x) do {} while(0)
 #endif
 
 
 static inline int seq_compare(uint32_t a, uint32_t b)
 {
-  uint32_t thresh = ((uint32_t)(-1))>>1;
-  if (((a > thresh) && (b > thresh)) || ((a <= thresh) && (b <= thresh))) {
-    if (a > b)
-      return 1;
-    if (b > a)
-      return -1;
-  } else {
-    if (a > b)
-      return -2;
-    if (b > a)
-      return 2;
-  }
-  return 0;
+    uint32_t thresh = ((uint32_t)(-1)) >> 1;
+    if (((a > thresh) && (b > thresh)) || ((a <= thresh) && (b <= thresh))) {
+        if (a > b)
+            return 1;
+
+        if (b > a)
+            return -1;
+    } else {
+        if (a > b)
+            return -2;
+
+        if (b > a)
+            return 2;
+    }
+
+    return 0;
 }
 
-// Input segment, used to keep only needed data, not the full frame
+/* Input segment, used to keep only needed data, not the full frame */
 struct tcp_input_segment
 {
-  uint32_t seq;
-  /* Pointer to payload */
-  unsigned char *payload;
-  uint16_t payload_len;
+    uint32_t seq;
+    /* Pointer to payload */
+    unsigned char *payload;
+    uint16_t payload_len;
 };
 
-// Function to compare input segments
+/* Function to compare input segments */
 static int input_segment_compare(void *ka, void *kb)
 {
-  struct tcp_input_segment *a = ka, *b = kb;
-  return seq_compare(a->seq,b->seq);
+    struct tcp_input_segment *a = ka, *b = kb;
+    return seq_compare(a->seq, b->seq);
 }
 
-static struct tcp_input_segment * segment_from_frame(struct pico_frame * f)
+static struct tcp_input_segment *segment_from_frame(struct pico_frame *f)
 {
-	struct tcp_input_segment * seg = pico_zalloc(sizeof(struct tcp_input_segment));
-	if(!seg)
-		return NULL;
-	seg->payload = pico_zalloc(f->payload_len);
-	if(!seg->payload)
-	{
-		pico_free(seg);
-		return NULL;
-	}
-	seg->seq = SEQN(f);
-	seg->payload_len = f->payload_len;
-	memcpy(seg->payload,f->payload,seg->payload_len);
-	return seg;
+    struct tcp_input_segment *seg = pico_zalloc(sizeof(struct tcp_input_segment));
+    if(!seg)
+        return NULL;
+
+    seg->payload = pico_zalloc(f->payload_len);
+    if(!seg->payload)
+    {
+        pico_free(seg);
+        return NULL;
+    }
+
+    seg->seq = SEQN(f);
+    seg->payload_len = f->payload_len;
+    memcpy(seg->payload, f->payload, seg->payload_len);
+    return seg;
 }
 
-static int segment_compare(void * ka, void * kb)
+static int segment_compare(void *ka, void *kb)
 {
-  struct pico_frame *a = ka, *b = kb;
-  return seq_compare(SEQN(a), SEQN(b));
+    struct pico_frame *a = ka, *b = kb;
+    return seq_compare(SEQN(a), SEQN(b));
 }
 
 struct pico_tcp_queue
 {
-  struct pico_tree pool;
-  uint32_t max_size;
-  uint32_t size;
-  uint32_t frames;
-  uint16_t overhead;
+    struct pico_tree pool;
+    uint32_t max_size;
+    uint32_t size;
+    uint32_t frames;
+    uint16_t overhead;
 };
+
 static void tcp_discard_all_segments(struct pico_tcp_queue *tq);
 static void *peek_segment(struct pico_tcp_queue *tq, uint32_t seq)
 {
-	if(!IS_INPUT_QUEUE(tq))
-	{
-	  struct pico_tcp_hdr H;
-	  struct pico_frame f = {0};
-	  f.transport_hdr = (uint8_t *) (&H);
-	  H.seq = long_be(seq);
-
-	  return pico_tree_findKey(&tq->pool,&f);
-	}
-	else
-	{
-	  struct tcp_input_segment dummy={.seq=seq};
-
-	  return pico_tree_findKey(&tq->pool,&dummy);
-	}
+    if(!IS_INPUT_QUEUE(tq))
+    {
+        struct pico_tcp_hdr H;
+        struct pico_frame f = {
+            0
+        };
+        f.transport_hdr = (uint8_t *) (&H);
+        H.seq = long_be(seq);
+
+        return pico_tree_findKey(&tq->pool, &f);
+    }
+    else
+    {
+        struct tcp_input_segment dummy = {
+            .seq = seq
+        };
+
+        return pico_tree_findKey(&tq->pool, &dummy);
+    }
 
 }
 
 static void *first_segment(struct pico_tcp_queue *tq)
 {
-  return pico_tree_first(&tq->pool);
+    return pico_tree_first(&tq->pool);
 }
 
 static void *next_segment(struct pico_tcp_queue *tq, void *cur)
 {
-  if (!cur)
-    return NULL;
-  if(IS_INPUT_QUEUE(tq))
-  {
-    return peek_segment(tq,((struct tcp_input_segment *)cur)->seq + ((struct tcp_input_segment *)cur)->payload_len);
-  }
-  else
-  {
-    return peek_segment(tq, SEQN((struct pico_frame *)cur) + ((struct pico_frame *)cur)->payload_len);
-  }
+    if (!cur)
+        return NULL;
+
+    if(IS_INPUT_QUEUE(tq))
+    {
+        return peek_segment(tq, ((struct tcp_input_segment *)cur)->seq + ((struct tcp_input_segment *)cur)->payload_len);
+    }
+    else
+    {
+        return peek_segment(tq, SEQN((struct pico_frame *)cur) + ((struct pico_frame *)cur)->payload_len);
+    }
 }
 
 static int32_t pico_enqueue_segment(struct pico_tcp_queue *tq, void *f)
 {
-  int32_t ret = -1;
-  uint16_t payload_len = (uint16_t)(IS_INPUT_QUEUE(tq) ?
-		  ((struct tcp_input_segment *)f)->payload_len:
-		  ((struct pico_frame *)f)->buffer_len);
-
-  if (payload_len <= 0) {
-    tcp_dbg("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TRIED TO ENQUEUE INVALID SEGMENT!\n");
-    //abort();
-    return -1;
-  }
-	LOCK(Mutex);
-  if ((tq->size + payload_len) > tq->max_size)
-  {
-    ret = 0;
-    goto out;
-  }
-  if (pico_tree_insert(&tq->pool,f) != 0)
-  {
-    ret = 0;
-    goto out;
-  }
-  tq->size += (uint16_t)(payload_len + tq->overhead);
-  if (payload_len > 0)
-    tq->frames++;
-  ret = (int32_t)payload_len;
-
-out :
-  UNLOCK(Mutex);
-  return ret;
+    int32_t ret = -1;
+    uint16_t payload_len = (uint16_t)(IS_INPUT_QUEUE(tq) ?
+                                      ((struct tcp_input_segment *)f)->payload_len :
+                                      ((struct pico_frame *)f)->buffer_len);
+
+    if (payload_len <= 0) {
+        tcp_dbg("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TRIED TO ENQUEUE INVALID SEGMENT!\n");
+        /* abort(); */
+        return -1;
+    }
+
+    LOCK(Mutex);
+    if ((tq->size + payload_len) > tq->max_size)
+    {
+        ret = 0;
+        goto out;
+    }
+
+    if (pico_tree_insert(&tq->pool, f) != 0)
+    {
+        ret = 0;
+        goto out;
+    }
+
+    tq->size += (uint16_t)(payload_len + tq->overhead);
+    if (payload_len > 0)
+        tq->frames++;
+
+    ret = (int32_t)payload_len;
+
+out:
+    UNLOCK(Mutex);
+    return ret;
 }
 
 static void pico_discard_segment(struct pico_tcp_queue *tq, void *f)
 {
-  void *f1;
-  uint16_t payload_len = (uint16_t)(IS_INPUT_QUEUE(tq)?
-		  ((struct tcp_input_segment *)f)->payload_len:
-		  ((struct pico_frame *)f)->buffer_len);
-  LOCK(Mutex);
-  f1 = pico_tree_delete(&tq->pool,f);
-  if (f1) {
-    tq->size -= (uint16_t)(payload_len + tq->overhead);
-    if (payload_len > 0)
-      tq->frames--;
-  }
-  if(IS_INPUT_QUEUE(tq))
-  {
-	struct tcp_input_segment * inp = f1;
-	pico_free(inp->payload);
-	pico_free(inp);
-  }
-  else
-    pico_frame_discard(f);
-  UNLOCK(Mutex);
+    void *f1;
+    uint16_t payload_len = (uint16_t)(IS_INPUT_QUEUE(tq) ?
+                                      ((struct tcp_input_segment *)f)->payload_len :
+                                      ((struct pico_frame *)f)->buffer_len);
+    LOCK(Mutex);
+    f1 = pico_tree_delete(&tq->pool, f);
+    if (f1) {
+        tq->size -= (uint16_t)(payload_len + tq->overhead);
+        if (payload_len > 0)
+            tq->frames--;
+    }
+
+    if(IS_INPUT_QUEUE(tq))
+    {
+        struct tcp_input_segment *inp = f1;
+        pico_free(inp->payload);
+        pico_free(inp);
+    }
+    else
+        pico_frame_discard(f);
+
+    UNLOCK(Mutex);
 }
 
 /* Structure for TCP socket */
 struct tcp_sack_block {
-  uint32_t left;
-  uint32_t right;
-  struct tcp_sack_block *next;
+    uint32_t left;
+    uint32_t right;
+    struct tcp_sack_block *next;
 };
 
 struct pico_socket_tcp {
-  struct pico_socket sock;
-
-  /* Tree/queues */
-  struct pico_tcp_queue tcpq_in;    // updated the input queue to hold input segments not the full frame.
-  struct pico_tcp_queue tcpq_out;
-  struct pico_tcp_queue tcpq_hold;  /* buffer to hold delayed frames according to Nagle */
-
-  /* tcp_output */
-  uint32_t snd_nxt;
-  uint32_t snd_last;
-  uint32_t snd_old_ack;
-  uint32_t snd_retry;
-  uint32_t snd_last_out;
-
-  /* congestion control */
-  uint32_t avg_rtt;
-  uint32_t rttvar;
-  uint32_t rto;
-  uint32_t in_flight;
-  uint8_t  timer_running;
-  struct pico_timer * retrans_tmr;
-  uint8_t  keepalive_timer_running;
-  uint16_t cwnd_counter;
-  uint16_t cwnd;
-  uint16_t ssthresh;
-  uint16_t recv_wnd;
-  uint16_t recv_wnd_scale;
-
-  /* tcp_input */
-  uint32_t rcv_nxt;
-  uint32_t rcv_ackd;
-  uint32_t rcv_processed;
-  uint16_t wnd;
-  uint16_t wnd_scale;
-
-  /* options */
-  uint32_t ts_nxt;
-  uint16_t mss;
-  uint8_t sack_ok;
-  uint8_t ts_ok;
-  uint8_t mss_ok;
-  uint8_t scale_ok;
-  struct tcp_sack_block *sacks;
-  uint8_t jumbo;
-
-  /* Transmission */
-  uint8_t  x_mode;
-  uint8_t  dupacks;
-  uint8_t  backoff;
-  uint8_t  localZeroWindow;
+    struct pico_socket sock;
+
+    /* Tree/queues */
+    struct pico_tcp_queue tcpq_in;  /* updated the input queue to hold input segments not the full frame. */
+    struct pico_tcp_queue tcpq_out;
+    struct pico_tcp_queue tcpq_hold; /* buffer to hold delayed frames according to Nagle */
+
+    /* tcp_output */
+    uint32_t snd_nxt;
+    uint32_t snd_last;
+    uint32_t snd_old_ack;
+    uint32_t snd_retry;
+    uint32_t snd_last_out;
+
+    /* congestion control */
+    uint32_t avg_rtt;
+    uint32_t rttvar;
+    uint32_t rto;
+    uint32_t in_flight;
+    uint8_t timer_running;
+    struct pico_timer *retrans_tmr;
+    uint8_t keepalive_timer_running;
+    uint16_t cwnd_counter;
+    uint16_t cwnd;
+    uint16_t ssthresh;
+    uint16_t recv_wnd;
+    uint16_t recv_wnd_scale;
+
+    /* tcp_input */
+    uint32_t rcv_nxt;
+    uint32_t rcv_ackd;
+    uint32_t rcv_processed;
+    uint16_t wnd;
+    uint16_t wnd_scale;
+
+    /* options */
+    uint32_t ts_nxt;
+    uint16_t mss;
+    uint8_t sack_ok;
+    uint8_t ts_ok;
+    uint8_t mss_ok;
+    uint8_t scale_ok;
+    struct tcp_sack_block *sacks;
+    uint8_t jumbo;
+
+    /* Transmission */
+    uint8_t x_mode;
+    uint8_t dupacks;
+    uint8_t backoff;
+    uint8_t localZeroWindow;
 };
 
 /* Queues */
-static struct pico_queue tcp_in = {0};
-static struct pico_queue tcp_out = {0};
+static struct pico_queue tcp_in = {
+    0
+};
+static struct pico_queue tcp_out = {
+    0
+};
 
 /* If Nagle enabled, this function can make 1 new segment from smaller segments in hold queue */
-static struct pico_frame * pico_hold_segment_make(struct pico_socket_tcp *t);
+static struct pico_frame *pico_hold_segment_make(struct pico_socket_tcp *t);
 
 /* checks if tcpq_in is empty */
 int pico_tcp_queue_in_is_empty(struct pico_socket *s)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
-
-  if (t->tcpq_in.frames == 0)
-    return 1;
-  else
-    return 0;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
+
+    if (t->tcpq_in.frames == 0)
+        return 1;
+    else
+        return 0;
 }
 
 /* Useful for getting rid of the beginning of the buffer (read() op) */
 static int release_until(struct pico_tcp_queue *q, uint32_t seq)
 {
-  void *head = first_segment(q);
-  int ret = 0;
-  int seq_result = 0;
-  if(head)
-	  	  seq_result = IS_INPUT_QUEUE(q) ?
-		  seq_compare( ((struct tcp_input_segment *)head)->seq + ((struct tcp_input_segment *)head)->payload_len, seq) :
-		  seq_compare(SEQN((struct pico_frame *)head) + ((struct pico_frame *)head)->payload_len, seq);
-
-  while (head && (seq_result <= 0)) {
-    void *cur = head;
-    head = next_segment(q, cur);
-    tcp_dbg("Releasing %p\n", q);
-    pico_discard_segment(q, cur);
-    ret++;
+    void *head = first_segment(q);
+    int ret = 0;
+    int seq_result = 0;
     if(head)
-  	  	  seq_result = IS_INPUT_QUEUE(q) ?
-  		  seq_compare( ((struct tcp_input_segment *)head)->seq + ((struct tcp_input_segment *)head)->payload_len, seq) :
-  		  seq_compare(SEQN((struct pico_frame *)head) + ((struct pico_frame *)head)->payload_len, seq);
-  }
-  return ret;
+        seq_result = IS_INPUT_QUEUE(q) ?
+                     seq_compare(((struct tcp_input_segment *)head)->seq + ((struct tcp_input_segment *)head)->payload_len, seq) :
+                     seq_compare(SEQN((struct pico_frame *)head) + ((struct pico_frame *)head)->payload_len, seq);
+
+    while (head && (seq_result <= 0)) {
+        void *cur = head;
+        head = next_segment(q, cur);
+        tcp_dbg("Releasing %p\n", q);
+        pico_discard_segment(q, cur);
+        ret++;
+        if(head)
+            seq_result = IS_INPUT_QUEUE(q) ?
+                         seq_compare(((struct tcp_input_segment *)head)->seq + ((struct tcp_input_segment *)head)->payload_len, seq) :
+                         seq_compare(SEQN((struct pico_frame *)head) + ((struct pico_frame *)head)->payload_len, seq);
+    }
+    return ret;
 }
 
-static int release_all_until(struct pico_tcp_queue *q, uint32_t seq,pico_time * 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;
-  int seq_result;
-  int ret = 0;
-  *timestamp = 0;
-
-  pico_tree_foreach_safe(idx,&q->pool,temp){
-  f = idx->keyValue;
-  seq_result = IS_INPUT_QUEUE(q) ?
-		  seq_compare( ((struct tcp_input_segment *)f)->seq + ((struct tcp_input_segment *)f)->payload_len, seq):
-		  seq_compare(SEQN((struct pico_frame *)f) + ((struct pico_frame *)f)->payload_len, seq);
-    if (seq_result<= 0) {
-      tcp_dbg("Releasing %p\n", f);
-      if(seq_result == 0)
-    	  *timestamp = ((struct pico_frame *)f)->timestamp;
-      pico_discard_segment(q, f);
-      ret++;
-    } else
-      return ret;
-  }
-  return ret;
+    void *f = NULL, *tmp __attribute__((unused));
+    struct pico_tree_node *idx, *temp;
+    int seq_result;
+    int ret = 0;
+    *timestamp = 0;
+
+    pico_tree_foreach_safe(idx, &q->pool, temp){
+        f = idx->keyValue;
+        seq_result = IS_INPUT_QUEUE(q) ?
+                     seq_compare(((struct tcp_input_segment *)f)->seq + ((struct tcp_input_segment *)f)->payload_len, seq) :
+                     seq_compare(SEQN((struct pico_frame *)f) + ((struct pico_frame *)f)->payload_len, seq);
+        if (seq_result <= 0) {
+            tcp_dbg("Releasing %p\n", f);
+            if(seq_result == 0)
+                *timestamp = ((struct pico_frame *)f)->timestamp;
+
+            pico_discard_segment(q, f);
+            ret++;
+        } else
+            return ret;
+    }
+    return ret;
 }
 
 /* API calls */
 
 uint16_t pico_tcp_checksum_ipv4(struct pico_frame *f)
 {
-  struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-  struct pico_tcp_hdr *tcp_hdr = (struct pico_tcp_hdr *) f->transport_hdr;
-  struct pico_socket *s = f->sock;
-  struct pico_ipv4_pseudo_hdr pseudo;
-
-  if (s) {
-    /* Case of outgoing frame */
-    //dbg("TCP CRC: on outgoing frame\n");
-    pseudo.src.addr = s->local_addr.ip4.addr;
-    pseudo.dst.addr = s->remote_addr.ip4.addr;
-  } else {
-    /* Case of incomming frame */
-    //dbg("TCP CRC: on incomming frame\n");
-    pseudo.src.addr = hdr->src.addr;
-    pseudo.dst.addr = hdr->dst.addr;
-  }
-  pseudo.zeros = 0;
-  pseudo.proto = PICO_PROTO_TCP;
-  pseudo.len = (uint16_t)short_be(f->transport_len);
-
-  return pico_dualbuffer_checksum(&pseudo, sizeof(struct pico_ipv4_pseudo_hdr), tcp_hdr, f->transport_len);
+    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    struct pico_tcp_hdr *tcp_hdr = (struct pico_tcp_hdr *) f->transport_hdr;
+    struct pico_socket *s = f->sock;
+    struct pico_ipv4_pseudo_hdr pseudo;
+
+    if (s) {
+        /* Case of outgoing frame */
+        /* dbg("TCP CRC: on outgoing frame\n"); */
+        pseudo.src.addr = s->local_addr.ip4.addr;
+        pseudo.dst.addr = s->remote_addr.ip4.addr;
+    } else {
+        /* Case of incomming frame */
+        /* dbg("TCP CRC: on incomming frame\n"); */
+        pseudo.src.addr = hdr->src.addr;
+        pseudo.dst.addr = hdr->dst.addr;
+    }
+
+    pseudo.zeros = 0;
+    pseudo.proto = PICO_PROTO_TCP;
+    pseudo.len = (uint16_t)short_be(f->transport_len);
+
+    return pico_dualbuffer_checksum(&pseudo, sizeof(struct pico_ipv4_pseudo_hdr), tcp_hdr, f->transport_len);
 }
 
 static void tcp_send_fin(struct pico_socket_tcp *t);
 static int pico_tcp_process_out(struct pico_protocol *self, struct pico_frame *f)
 {
-  struct pico_tcp_hdr *hdr;
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)f->sock;
-  IGNORE_PARAMETER(self);
-  hdr = (struct pico_tcp_hdr *)f->transport_hdr;
-  f->sock->timestamp = PICO_TIME_MS();
-  if (f->payload_len > 0) {
-    tcp_dbg("Process out: sending %p (%d bytes)\n",f, f->payload_len);
-  } else {
-    tcp_dbg("Sending empty packet\n");
-  }
-
-  if (f->payload_len > 0) {
-    if (seq_compare(SEQN(f) + f->payload_len, t->snd_nxt) > 0) {
-      t->snd_nxt = SEQN(f) + f->payload_len;
-      tcp_dbg("%s: snd_nxt is now %08x\n", __FUNCTION__, t->snd_nxt);
+    struct pico_tcp_hdr *hdr;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)f->sock;
+    IGNORE_PARAMETER(self);
+    hdr = (struct pico_tcp_hdr *)f->transport_hdr;
+    f->sock->timestamp = PICO_TIME_MS();
+    if (f->payload_len > 0) {
+        tcp_dbg("Process out: sending %p (%d bytes)\n", f, f->payload_len);
+    } else {
+        tcp_dbg("Sending empty packet\n");
     }
-  } else if (hdr->flags == PICO_TCP_ACK) { /* pure ack */
-    //hdr->seq = long_be(t->snd_nxt);   /* XXX disabled this to not to mess with seq nrs of ACKs anymore */
-  } else {
-    tcp_dbg("%s: non-pure ACK with len=0, fl:%04x\n", __FUNCTION__, hdr->flags);
-  }
-
-  pico_network_send(f);
-  return 0;
+
+    if (f->payload_len > 0) {
+        if (seq_compare(SEQN(f) + f->payload_len, t->snd_nxt) > 0) {
+            t->snd_nxt = SEQN(f) + f->payload_len;
+            tcp_dbg("%s: snd_nxt is now %08x\n", __FUNCTION__, t->snd_nxt);
+        }
+    } else if (hdr->flags == PICO_TCP_ACK) { /* pure ack */
+        /* hdr->seq = long_be(t->snd_nxt);   / * XXX disabled this to not to mess with seq nrs of ACKs anymore * / */
+    } else {
+        tcp_dbg("%s: non-pure ACK with len=0, fl:%04x\n", __FUNCTION__, hdr->flags);
+    }
+
+    pico_network_send(f);
+    return 0;
 }
 
 int32_t pico_tcp_push(struct pico_protocol *self, struct pico_frame *data);
 
 /* Interface: protocol definition */
 struct pico_protocol pico_proto_tcp = {
-  .name = "tcp",
-  .proto_number = PICO_PROTO_TCP,
-  .layer = PICO_LAYER_TRANSPORT,
-  .process_in = pico_transport_process_in,
-  .process_out = pico_tcp_process_out,
-  .push = pico_tcp_push,
-  .q_in = &tcp_in,
-  .q_out = &tcp_out,
+    .name = "tcp",
+    .proto_number = PICO_PROTO_TCP,
+    .layer = PICO_LAYER_TRANSPORT,
+    .process_in = pico_transport_process_in,
+    .process_out = pico_tcp_process_out,
+    .push = pico_tcp_push,
+    .q_in = &tcp_in,
+    .q_out = &tcp_out,
 };
 
 static uint32_t pico_paws(void)
 {
-  static uint32_t _paws = 0;
-  _paws = pico_rand();
-  return long_be(_paws); /*XXX: implement paws */
+    static uint32_t _paws = 0;
+    _paws = pico_rand();
+    return long_be(_paws); /*XXX: implement paws */
 }
 
 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((uint32_t)pico_tick);
-  uint32_t tsecr = long_be(ts->ts_nxt);
-  uint32_t i = 0;
-  f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
-
-  memset(f->start, PICO_TCP_OPTION_NOOP, optsiz); /* fill blanks with noop */
-
-  if (flags & PICO_TCP_SYN) {
-    f->start[i++] = PICO_TCP_OPTION_MSS;
-    f->start[i++] = PICO_TCPOPTLEN_MSS;
-    f->start[i++] = (uint8_t)((ts->mss >> 8) & 0xFF);
-    f->start[i++] = (uint8_t)(ts->mss & 0xFF);
-    f->start[i++] = PICO_TCP_OPTION_SACK_OK;
-    f->start[i++] = PICO_TCPOPTLEN_SACK_OK;
-  }
-
-  f->start[i++] = PICO_TCP_OPTION_WS;
-  f->start[i++] = PICO_TCPOPTLEN_WS;
-  f->start[i++] = (uint8_t)(ts->wnd_scale);
-
-  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);
-    i += 4;
-    memcpy(f->start + i, &tsecr, 4);
-    i += 4;
-  }
-
-  if (flags & PICO_TCP_ACK) {
-    struct tcp_sack_block *sb;
-    uint32_t len_off;
-
-    if (ts->sack_ok && ts->sacks) {
-      f->start[i++] = PICO_TCP_OPTION_SACK;
-      len_off = i;
-      f->start[i++] = PICO_TCPOPTLEN_SACK;
-      while(ts->sacks) {
-        sb = ts->sacks;
-        ts->sacks = sb->next;
-        memcpy(f->start + i, sb, 2 * sizeof(uint32_t));
-        i += (2 * (uint32_t)sizeof(uint32_t));
-        f->start[len_off] = (uint8_t)(f->start[len_off] + (2 * sizeof(uint32_t)));
-        pico_free(sb);
-      }
+    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;
+
+    memset(f->start, PICO_TCP_OPTION_NOOP, optsiz); /* fill blanks with noop */
+
+    if (flags & PICO_TCP_SYN) {
+        f->start[i++] = PICO_TCP_OPTION_MSS;
+        f->start[i++] = PICO_TCPOPTLEN_MSS;
+        f->start[i++] = (uint8_t)((ts->mss >> 8) & 0xFF);
+        f->start[i++] = (uint8_t)(ts->mss & 0xFF);
+        f->start[i++] = PICO_TCP_OPTION_SACK_OK;
+        f->start[i++] = PICO_TCPOPTLEN_SACK_OK;
+    }
+
+    f->start[i++] = PICO_TCP_OPTION_WS;
+    f->start[i++] = PICO_TCPOPTLEN_WS;
+    f->start[i++] = (uint8_t)(ts->wnd_scale);
+
+    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);
+        i += 4;
+        memcpy(f->start + i, &tsecr, 4);
+        i += 4;
+    }
+
+    if (flags & PICO_TCP_ACK) {
+        struct tcp_sack_block *sb;
+        uint32_t len_off;
+
+        if (ts->sack_ok && ts->sacks) {
+            f->start[i++] = PICO_TCP_OPTION_SACK;
+            len_off = i;
+            f->start[i++] = PICO_TCPOPTLEN_SACK;
+            while(ts->sacks) {
+                sb = ts->sacks;
+                ts->sacks = sb->next;
+                memcpy(f->start + i, sb, 2 * sizeof(uint32_t));
+                i += (2 * (uint32_t)sizeof(uint32_t));
+                f->start[len_off] = (uint8_t)(f->start[len_off] + (2 * sizeof(uint32_t)));
+                pico_free(sb);
+            }
+        }
     }
-  }
-  if (i < optsiz)
-    f->start[ optsiz - 1 ] = PICO_TCP_OPTION_END;
+
+    if (i < optsiz)
+        f->start[ optsiz - 1 ] = PICO_TCP_OPTION_END;
+}
+
+static uint16_t tcp_options_size_frame(struct pico_frame *f)
+{
+    uint16_t size = 0;
+
+    /* Always update window scale. */
+    size = (uint16_t)(size + PICO_TCPOPTLEN_WS);
+    if (f->transport_flags_saved)
+        size = (uint16_t)(size + PICO_TCPOPTLEN_TIMESTAMP);
+
+    size = (uint16_t)(size + PICO_TCPOPTLEN_END);
+    size = (uint16_t)(((size + 3) >> 2) << 2);
+    return size;
+}
+
+static void tcp_add_options_frame(struct pico_socket_tcp *ts, struct pico_frame *f)
+{
+    uint32_t tsval = long_be((uint32_t)pico_tick);
+    uint32_t tsecr = long_be(ts->ts_nxt);
+    uint32_t i = 0;
+    uint16_t optsiz = tcp_options_size_frame(f);
+
+    f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
+
+    memset(f->start, PICO_TCP_OPTION_NOOP, optsiz); /* fill blanks with noop */
+
+
+    f->start[i++] = PICO_TCP_OPTION_WS;
+    f->start[i++] = PICO_TCPOPTLEN_WS;
+    f->start[i++] = (uint8_t)(ts->wnd_scale);
+
+    if (f->transport_flags_saved) {
+        f->start[i++] = PICO_TCP_OPTION_TIMESTAMP;
+        f->start[i++] = PICO_TCPOPTLEN_TIMESTAMP;
+        memcpy(f->start + i, &tsval, 4);
+        i += 4;
+        memcpy(f->start + i, &tsecr, 4);
+        i += 4;
+    }
+
+    if (i < optsiz)
+        f->start[ optsiz - 1 ] = PICO_TCP_OPTION_END;
 }
 
 static void tcp_send_ack(struct pico_socket_tcp *t);
@@ -496,1674 +563,1789 @@
 
 static void tcp_set_space(struct pico_socket_tcp *t)
 {
-  uint32_t mtu;
-  int32_t space;
-  uint32_t shift = 0;
-
-  mtu = t->mss + PICO_SIZE_TCPHDR + PICO_SIZE_TCPOPT_SYN ;
-  if (t->tcpq_in.max_size == 0) {
-    space = 1024 * 1024 * 1024; /* One Gigabyte, for unlimited sockets. */
-  } else {
-    space = (int32_t)(((t->tcpq_in.max_size - t->tcpq_in.size) / mtu) * t->mss);
-  }
-  if (space < 0)
-    space = 0;
-  while(space > 0xFFFF) {
-    space >>= 1;
-    shift++;
-  }
-  if ((space != t->wnd) || (shift != t->wnd_scale) || ((space - t->wnd) > (space>>2))) {
-    t->wnd = (uint16_t)space;
-    t->wnd_scale = (uint16_t)shift;
-
-    if(t->wnd == 0) // mark the entering to zero window state
-    	t->localZeroWindow = 1u;
-    else if(t->localZeroWindow)
-    {
-    	t->localZeroWindow = 0u;
-    	tcp_send_windowUpdate(t);
+    uint32_t mtu;
+    int32_t space;
+    uint32_t shift = 0;
+
+    mtu = t->mss + PICO_SIZE_TCPHDR + PICO_SIZE_TCPOPT_SYN;
+    if (t->tcpq_in.max_size == 0) {
+        space = 1024 * 1024 * 1024; /* One Gigabyte, for unlimited sockets. */
+    } else {
+        space = (int32_t)(((t->tcpq_in.max_size - t->tcpq_in.size) / mtu) * t->mss);
     }
-  }
+
+    if (space < 0)
+        space = 0;
+
+    while(space > 0xFFFF) {
+        space >>= 1;
+        shift++;
+    }
+    if ((space != t->wnd) || (shift != t->wnd_scale) || ((space - t->wnd) > (space >> 2))) {
+        t->wnd = (uint16_t)space;
+        t->wnd_scale = (uint16_t)shift;
+
+        if(t->wnd == 0) /* mark the entering to zero window state */
+            t->localZeroWindow = 1u;
+        else if(t->localZeroWindow)
+        {
+            t->localZeroWindow = 0u;
+            tcp_send_windowUpdate(t);
+        }
+    }
 }
 
 /* Return 32-bit aligned option size */
 static uint16_t tcp_options_size(struct pico_socket_tcp *t, uint16_t flags)
 {
-	uint16_t size = 0;
-  struct tcp_sack_block *sb = t->sacks;
-
-  if (flags & PICO_TCP_SYN) {  /* Full options */
-    size = PICO_TCPOPTLEN_MSS + PICO_TCP_OPTION_SACK_OK + PICO_TCPOPTLEN_WS + PICO_TCPOPTLEN_TIMESTAMP;
-  } else {
-
-   /* Always update window scale. */
-    size = (uint16_t)(size + PICO_TCPOPTLEN_WS);
-
-    if (t->ts_ok)
-      size = (uint16_t)(size + PICO_TCPOPTLEN_TIMESTAMP);
-
-    size= (uint16_t)(size + PICO_TCPOPTLEN_END);
-  }
-  if ((flags & PICO_TCP_ACK) && (t->sack_ok && sb)) {
-    size = (uint16_t)(size + 2);
-    while(sb) {
-      size = (uint16_t)(size + (2 * sizeof(uint32_t)));
-      sb = sb->next;
+    uint16_t size = 0;
+    struct tcp_sack_block *sb = t->sacks;
+
+    if (flags & PICO_TCP_SYN) { /* Full options */
+        size = PICO_TCPOPTLEN_MSS + PICO_TCP_OPTION_SACK_OK + PICO_TCPOPTLEN_WS + PICO_TCPOPTLEN_TIMESTAMP;
+    } else {
+
+        /* Always update window scale. */
+        size = (uint16_t)(size + PICO_TCPOPTLEN_WS);
+
+        if (t->ts_ok)
+            size = (uint16_t)(size + PICO_TCPOPTLEN_TIMESTAMP);
+
+        size = (uint16_t)(size + PICO_TCPOPTLEN_END);
     }
-  }
-  size = (uint16_t)(((size + 3) >> 2) << 2);
-  return size;
+
+    if ((flags & PICO_TCP_ACK) && (t->sack_ok && sb)) {
+        size = (uint16_t)(size + 2);
+        while(sb) {
+            size = (uint16_t)(size + (2 * sizeof(uint32_t)));
+            sb = sb->next;
+        }
+    }
+
+    size = (uint16_t)(((size + 3) >> 2) << 2);
+    return size;
 }
 
 uint16_t pico_tcp_overhead(struct pico_socket *s)
 {
-  if (!s)
-    return 0;
-
-  return (uint16_t)(PICO_SIZE_TCPHDR + tcp_options_size((struct pico_socket_tcp *)s, (uint16_t)0)); /* hdr + Options size for data pkt */
+    if (!s)
+        return 0;
+
+    return (uint16_t)(PICO_SIZE_TCPHDR + tcp_options_size((struct pico_socket_tcp *)s, (uint16_t)0)); /* hdr + Options size for data pkt */
 
 }
 
 static void tcp_process_sack(struct pico_socket_tcp *t, uint32_t start, uint32_t end)
 {
-  struct pico_frame *f;
-  struct pico_tree_node * index, * temp;
-  int cmp;
-  uint16_t count = 0;
-
-  pico_tree_foreach_safe(index,&t->tcpq_out.pool,temp){
-    f = index->keyValue;
-    cmp = seq_compare(SEQN(f), start);
-    if (cmp > 0)
-      goto done;
-
-    if (cmp == 0) {
-      cmp = seq_compare(SEQN(f) + f->payload_len, end);
-      if (cmp > 0) {
-        tcp_dbg("Invalid SACK: ignoring.\n");
-      }
-
-      tcp_dbg("Marking (by SACK) segment %08x BLK:[%08x::%08x]\n", SEQN(f), start, end);
-      f->flags |= PICO_FRAME_FLAG_SACKED;
-      count++;
-
-      if (cmp == 0) {
-        /* that was last segment sacked. Job done */
-        goto done;
-      }
+    struct pico_frame *f;
+    struct pico_tree_node *index, *temp;
+    int cmp;
+    uint16_t count = 0;
+
+    pico_tree_foreach_safe(index, &t->tcpq_out.pool, temp){
+        f = index->keyValue;
+        cmp = seq_compare(SEQN(f), start);
+        if (cmp > 0)
+            goto done;
+
+        if (cmp == 0) {
+            cmp = seq_compare(SEQN(f) + f->payload_len, end);
+            if (cmp > 0) {
+                tcp_dbg("Invalid SACK: ignoring.\n");
+            }
+
+            tcp_dbg("Marking (by SACK) segment %08x BLK:[%08x::%08x]\n", SEQN(f), start, end);
+            f->flags |= PICO_FRAME_FLAG_SACKED;
+            count++;
+
+            if (cmp == 0) {
+                /* that was last segment sacked. Job done */
+                goto done;
+            }
+        }
     }
-  }
 
 done:
-  if (t->x_mode > PICO_TCP_LOOKAHEAD) {
-    if (t->in_flight > (count))
-      t->in_flight -= (count);
-    else
-      t->in_flight = 0;
-  }
+    if (t->x_mode > PICO_TCP_LOOKAHEAD) {
+        if (t->in_flight > (count))
+            t->in_flight -= (count);
+        else
+            t->in_flight = 0;
+    }
 }
 
-inline static void tcp_add_header(struct pico_socket_tcp *t, struct pico_frame * f)
+inline static void tcp_add_header(struct pico_socket_tcp *t, struct pico_frame *f)
 {
-  struct pico_tcp_hdr *	hdr = (struct pico_tcp_hdr *)f->transport_hdr;
-  f->timestamp = pico_tick;
-  tcp_add_options(t, f, 0, (uint16_t)(f->transport_len - f->payload_len - (uint16_t)PICO_SIZE_TCPHDR));
-  hdr->rwnd = short_be(t->wnd);
-  hdr->flags |= PICO_TCP_PSH;
-  hdr->ack = long_be(t->rcv_nxt);
-  hdr->crc = 0;
-  hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
+    struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *)f->transport_hdr;
+    f->timestamp = pico_tick;
+    tcp_add_options(t, f, 0, (uint16_t)(f->transport_len - f->payload_len - (uint16_t)PICO_SIZE_TCPHDR));
+    hdr->rwnd = short_be(t->wnd);
+    hdr->flags |= PICO_TCP_PSH;
+    hdr->ack = long_be(t->rcv_nxt);
+    hdr->crc = 0;
+    hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
 }
 
 static void tcp_rcv_sack(struct pico_socket_tcp *t, uint8_t *opt, int len)
 {
-  uint32_t start, end;
-  int i = 0;
-  if (len % 8) {
-    tcp_dbg("SACK: Invalid len.\n");
-    return;
-  }
-  while (i < len) {
-    start = long_from(opt + i);
-    i += 4;
-    end = long_from(opt + i);
-    i += 4;
-    tcp_process_sack(t, long_be(start), long_be(end));
-  }
+    uint32_t start, end;
+    int i = 0;
+    if (len % 8) {
+        tcp_dbg("SACK: Invalid len.\n");
+        return;
+    }
+
+    while (i < len) {
+        start = long_from(opt + i);
+        i += 4;
+        end = long_from(opt + i);
+        i += 4;
+        tcp_process_sack(t, long_be(start), long_be(end));
+    }
 }
 
 static void tcp_parse_options(struct pico_frame *f)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)f->sock;
-  uint8_t *opt = f->transport_hdr + PICO_SIZE_TCPHDR;
-  uint32_t i = 0;
-  f->timestamp = 0;
-  while (i < (f->transport_len - PICO_SIZE_TCPHDR)) {
-    uint8_t type =  opt[i++];
-    uint8_t len;
-    if(i < (f->transport_len - PICO_SIZE_TCPHDR) && (type > 1))
-      len =  opt[i++];
-    else
-      len = 1;
-    if (f->payload && ((opt + i) > f->payload))
-      break;
-    tcp_dbg_options("Received option '%d', len = %d \n", type, len);
-    switch (type) {
-      case PICO_TCP_OPTION_NOOP:
-      case PICO_TCP_OPTION_END:
-        break;
-      case PICO_TCP_OPTION_WS:
-        if (len != PICO_TCPOPTLEN_WS) {
-          tcp_dbg_options("TCP Window scale: bad len received (%d).\n", len);
-          i = i + len - 2;
-          break;
-        }
-        t->recv_wnd_scale = opt[i++];
-        tcp_dbg_options("TCP Window scale: received %d\n", t->recv_wnd_scale);
-        break;
-      case PICO_TCP_OPTION_SACK_OK:
-        if (len != PICO_TCPOPTLEN_SACK_OK) {
-          tcp_dbg_options("TCP option sack: bad len received.\n");
-          i = i + len - 2;
-          break;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)f->sock;
+    uint8_t *opt = f->transport_hdr + PICO_SIZE_TCPHDR;
+    uint32_t i = 0;
+    f->timestamp = 0;
+    while (i < (f->transport_len - PICO_SIZE_TCPHDR)) {
+        uint8_t type =  opt[i++];
+        uint8_t len;
+        if(i < (f->transport_len - PICO_SIZE_TCPHDR) && (type > 1))
+            len =  opt[i++];
+        else
+            len = 1;
+
+        if (f->payload && ((opt + i) > f->payload))
+            break;
+
+        tcp_dbg_options("Received option '%d', len = %d \n", type, len);
+        switch (type) {
+        case PICO_TCP_OPTION_NOOP:
+        case PICO_TCP_OPTION_END:
+            break;
+        case PICO_TCP_OPTION_WS:
+            if (len != PICO_TCPOPTLEN_WS) {
+                tcp_dbg_options("TCP Window scale: bad len received (%d).\n", len);
+                i = i + len - 2;
+                break;
+            }
+
+            t->recv_wnd_scale = opt[i++];
+            tcp_dbg_options("TCP Window scale: received %d\n", t->recv_wnd_scale);
+            break;
+        case PICO_TCP_OPTION_SACK_OK:
+            if (len != PICO_TCPOPTLEN_SACK_OK) {
+                tcp_dbg_options("TCP option sack: bad len received.\n");
+                i = i + len - 2;
+                break;
+            }
+
+            if(((struct pico_tcp_hdr *)(f->transport_hdr))->flags & PICO_TCP_SYN )
+                t->sack_ok = 1;
+
+            break;
+        case PICO_TCP_OPTION_MSS: {
+            uint16_t mss;
+            if (len != PICO_TCPOPTLEN_MSS) {
+                tcp_dbg_options("TCP option mss: bad len received.\n");
+                i = i + len - 2;
+                break;
+            }
+
+            t->mss_ok = 1;
+            mss = short_from(opt + i);
+            i += (uint32_t)sizeof(uint16_t);
+            if (t->mss > short_be(mss))
+                t->mss = short_be(mss);
+
+            break;
         }
-        t->sack_ok = 1;
-        break;
-      case PICO_TCP_OPTION_MSS: {
-        uint16_t mss;
-        if (len != PICO_TCPOPTLEN_MSS) {
-          tcp_dbg_options("TCP option mss: bad len received.\n");
-          i = i + len - 2;
-          break;
+        case PICO_TCP_OPTION_TIMESTAMP: {
+            uint32_t tsval, tsecr;
+            if (len != PICO_TCPOPTLEN_TIMESTAMP) {
+                tcp_dbg_options("TCP option timestamp: bad len received.\n");
+                i = i + len - 2;
+                break;
+            }
+
+            t->ts_ok = 1;
+            tsval = long_from(opt + i);
+            i += (uint32_t)sizeof(uint32_t);
+            tsecr = long_from(opt + i);
+            f->timestamp = long_be(tsecr);
+            i += (uint32_t)sizeof(uint32_t);
+            t->ts_nxt = long_be(tsval);
+            break;
         }
-        t->mss_ok = 1;
-        mss = short_from(opt + i);
-        i += (uint32_t)sizeof(uint16_t);
-        if (t->mss > short_be(mss))
-          t->mss = short_be(mss);
-        break;
-      }
-      case PICO_TCP_OPTION_TIMESTAMP: {
-        uint32_t tsval, tsecr;
-        if (len != PICO_TCPOPTLEN_TIMESTAMP) {
-          tcp_dbg_options("TCP option timestamp: bad len received.\n");
-          i = i + len - 2;
-          break;
+        case PICO_TCP_OPTION_SACK:
+        {
+            tcp_rcv_sack(t, opt + i, len - 2);
+            i = i + len - 2;
+            break;
         }
-        t->ts_ok = 1;
-        tsval = long_from(opt + i);
-        i += (uint32_t)sizeof(uint32_t);
-        tsecr = long_from(opt + i);
-        f->timestamp = long_be(tsecr);
-        i += (uint32_t)sizeof(uint32_t);
-        t->ts_nxt = long_be(tsval);
-        break;
-      }
-      case PICO_TCP_OPTION_SACK:
-      {
-        tcp_rcv_sack(t, opt + i, len - 2);
-        i = i + len - 2;
-        break;
-      }
-      default:
-        tcp_dbg_options("TCP: received unsupported option %u\n", type);
-        i = i + len - 2;
+        default:
+            tcp_dbg_options("TCP: received unsupported option %u\n", type);
+            i = i + len - 2;
+        }
     }
-  }
 }
 
 static int tcp_send(struct pico_socket_tcp *ts, struct pico_frame *f)
 {
-  struct pico_tcp_hdr *hdr= (struct pico_tcp_hdr *) f->transport_hdr;
-  struct pico_frame *cpy;
-  hdr->trans.sport = ts->sock.local_port;
-  hdr->trans.dport = ts->sock.remote_port;
-  if (!hdr->seq)
-    hdr->seq = long_be(ts->snd_nxt);
-
-  if (ts->rcv_nxt != 0) {
-    if ( (ts->rcv_ackd == 0) || (seq_compare(ts->rcv_ackd, ts->rcv_nxt) != 0) || (hdr->flags & PICO_TCP_ACK)) {
-      hdr->flags |= PICO_TCP_ACK;
-      hdr->ack = long_be(ts->rcv_nxt);
-      ts->rcv_ackd = ts->rcv_nxt;
+    struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *) f->transport_hdr;
+    struct pico_frame *cpy;
+    hdr->trans.sport = ts->sock.local_port;
+    hdr->trans.dport = ts->sock.remote_port;
+    if (!hdr->seq)
+        hdr->seq = long_be(ts->snd_nxt);
+
+    if (ts->rcv_nxt != 0) {
+        if ((ts->rcv_ackd == 0) || (seq_compare(ts->rcv_ackd, ts->rcv_nxt) != 0) || (hdr->flags & PICO_TCP_ACK)) {
+            hdr->flags |= PICO_TCP_ACK;
+            hdr->ack = long_be(ts->rcv_nxt);
+            ts->rcv_ackd = ts->rcv_nxt;
+        }
+    }
+
+    if (hdr->flags & PICO_TCP_SYN) {
+        ts->snd_nxt++;
+    }
+
+    if (f->payload_len > 0) {
+        hdr->flags |= PICO_TCP_PSH | PICO_TCP_ACK;
+        hdr->ack = long_be(ts->rcv_nxt);
+        ts->rcv_ackd = ts->rcv_nxt;
+        ts->keepalive_timer_running = 2; /* XXX TODO check fix: added 1 to counter to postpone sending keepalive, ACK is in data segments */
     }
-  }
-
-  if (hdr->flags & PICO_TCP_SYN) {
-    ts->snd_nxt++;
-  }
-  if (f->payload_len > 0) {
-    hdr->flags |= PICO_TCP_PSH | PICO_TCP_ACK;
-    hdr->ack = long_be(ts->rcv_nxt);
-    ts->rcv_ackd = ts->rcv_nxt;
-    ts->keepalive_timer_running = 2;    /* XXX TODO check fix: added 1 to counter to postpone sending keepalive, ACK is in data segments */
-  }
-
-  f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
-  hdr->rwnd = short_be(ts->wnd);
-  hdr->crc = 0;
-  hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
-
-  /* TCP: ENQUEUE to PROTO ( Transmit ) */
-  cpy = pico_frame_copy(f);
-  if ((pico_enqueue(&tcp_out, cpy) > 0)) {
-    if (f->payload_len > 0) {
-      ts->in_flight++;
-      ts->snd_nxt += f->payload_len;  /* update next pointer here to prevent sending same segment twice when called twice in same tick */
+
+    f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
+    hdr->rwnd = short_be(ts->wnd);
+    hdr->crc = 0;
+    hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
+
+    /* TCP: ENQUEUE to PROTO ( Transmit ) */
+    cpy = pico_frame_copy(f);
+    if ((pico_enqueue(&tcp_out, cpy) > 0)) {
+        if (f->payload_len > 0) {
+            ts->in_flight++;
+            ts->snd_nxt += f->payload_len; /* update next pointer here to prevent sending same segment twice when called twice in same tick */
+        }
+
+        tcp_dbg("DBG> [tcp output] state: %02x --> local port:%d remote port: %d seq: %08x ack: %08x flags: %02x = t_len: %d, hdr: %u payload: %d\n",
+                TCPSTATE(&ts->sock) >> 8, short_be(hdr->trans.sport), short_be(hdr->trans.dport), SEQN(f), ACKN(f), hdr->flags, f->transport_len, (hdr->len & 0xf0) >> 2, f->payload_len );
+    } else {
+        pico_frame_discard(cpy);
     }
-    tcp_dbg("DBG> [tcp output] state: %02x --> local port:%d remote port: %d seq: %08x ack: %08x flags: %02x = t_len: %d, hdr: %u payload: %d\n",
-      TCPSTATE(&ts->sock) >> 8, short_be(hdr->trans.sport), short_be(hdr->trans.dport), SEQN(f), ACKN(f), hdr->flags, f->transport_len, (hdr->len & 0xf0) >> 2 , f->payload_len );
-  } else {
-    pico_frame_discard(cpy);
-  }
-  return 0;
+
+    return 0;
 }
 
-//#define PICO_TCP_SUPPORT_SOCKET_STATS
+/* #define PICO_TCP_SUPPORT_SOCKET_STATS */
 
 #ifdef PICO_TCP_SUPPORT_SOCKET_STATS
 static void sock_stats(uint32_t when, void *arg)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)arg;
-  tcp_dbg("STATISTIC> [%lu] socket state: %02x --> local port:%d remote port: %d queue size: %d snd_una: %08x snd_nxt: %08x timer: %d cwnd: %d\n",
-    when, t->sock.state, short_be(t->sock.local_port), short_be(t->sock.remote_port), t->tcpq_out.size, SEQN(first_segment(&t->tcpq_out)), t->snd_nxt, t->timer_running, t->cwnd);
-  pico_timer_add(2000, sock_stats, t);
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)arg;
+    tcp_dbg("STATISTIC> [%lu] socket state: %02x --> local port:%d remote port: %d queue size: %d snd_una: %08x snd_nxt: %08x timer: %d cwnd: %d\n",
+            when, t->sock.state, short_be(t->sock.local_port), short_be(t->sock.remote_port), t->tcpq_out.size, SEQN(first_segment(&t->tcpq_out)), t->snd_nxt, t->timer_running, t->cwnd);
+    pico_timer_add(2000, sock_stats, t);
 }
 #endif
 
 struct pico_socket *pico_tcp_open(void)
 {
-  struct pico_socket_tcp *t = pico_zalloc(sizeof(struct pico_socket_tcp));
-  if (!t)
-    return NULL;
-  t->sock.timestamp = PICO_TIME_MS();
-  t->mss = PICO_TCP_DEFAULT_MSS;
-
-  t->tcpq_in.pool.root = t->tcpq_hold.pool.root = t->tcpq_out.pool.root = &LEAF;
-  t->tcpq_hold.pool.compare = t->tcpq_out.pool.compare = segment_compare;
-  t->tcpq_in.pool.compare = input_segment_compare;
-  t->tcpq_in.max_size = PICO_DEFAULT_SOCKETQ;
-  t->tcpq_out.max_size = PICO_DEFAULT_SOCKETQ;
-  t->tcpq_hold.max_size = 2*PICO_TCP_DEFAULT_MSS;
-  t->tcpq_in.overhead=(sizeof(struct tcp_input_segment)+sizeof(struct pico_tree_node));
-  t->tcpq_out.overhead=t->tcpq_hold.overhead=sizeof(struct pico_frame)+sizeof(struct pico_tree_node);
-  /* disable Nagle by default */
-  //t->sock.opt_flags |= (1 << PICO_SOCKET_OPT_TCPNODELAY);
-  /* Nagle is enabled by default */
-  t->sock.opt_flags &= (uint16_t)~(1 << PICO_SOCKET_OPT_TCPNODELAY);
+    struct pico_socket_tcp *t = pico_zalloc(sizeof(struct pico_socket_tcp));
+    if (!t)
+        return NULL;
+
+    t->sock.timestamp = PICO_TIME_MS();
+    t->mss = PICO_TCP_DEFAULT_MSS;
+
+    t->tcpq_in.pool.root = t->tcpq_hold.pool.root = t->tcpq_out.pool.root = &LEAF;
+    t->tcpq_hold.pool.compare = t->tcpq_out.pool.compare = segment_compare;
+    t->tcpq_in.pool.compare = input_segment_compare;
+    t->tcpq_in.max_size = PICO_DEFAULT_SOCKETQ;
+    t->tcpq_out.max_size = PICO_DEFAULT_SOCKETQ;
+    t->tcpq_hold.max_size = 2 * PICO_TCP_DEFAULT_MSS;
+    t->tcpq_in.overhead = (sizeof(struct tcp_input_segment) + sizeof(struct pico_tree_node));
+    t->tcpq_out.overhead = t->tcpq_hold.overhead = sizeof(struct pico_frame) + sizeof(struct pico_tree_node);
+    /* disable Nagle by default */
+    /* t->sock.opt_flags |= (1 << PICO_SOCKET_OPT_TCPNODELAY); */
+    /* Nagle is enabled by default */
+    t->sock.opt_flags &= (uint16_t) ~(1 << PICO_SOCKET_OPT_TCPNODELAY);
 
 #ifdef PICO_TCP_SUPPORT_SOCKET_STATS
-  pico_timer_add(2000, sock_stats, t);
+    pico_timer_add(2000, sock_stats, t);
 #endif
-  tcp_set_space(t);
-
-  return &t->sock;
+    tcp_set_space(t);
+
+    return &t->sock;
 }
 
 uint32_t pico_tcp_read(struct pico_socket *s, void *buf, uint32_t len)
 {
-  struct pico_socket_tcp *t = TCP_SOCK(s);
-  struct tcp_input_segment *f;
-  uint32_t in_frame_off, in_frame_len;
-  uint32_t tot_rd_len = 0;
-
-  while (tot_rd_len < len) {
-    /* To be sure we don't have garbage at the beginning */
-    release_until(&t->tcpq_in, t->rcv_processed);
-    f = first_segment(&t->tcpq_in);
-    if (!f) {
-      tcp_set_space(t);
-      goto out;
-    }
-
-    /* Hole at the beginning of data, awaiting retransmissions. */
-    if (seq_compare(t->rcv_processed, f->seq) < 0) {
-      tcp_dbg("TCP> read hole beginning of data, %08x - %08x. rcv_nxt is %08x\n",t->rcv_processed, f->seq, t->rcv_nxt);
-      goto out;
+    struct pico_socket_tcp *t = TCP_SOCK(s);
+    struct tcp_input_segment *f;
+    uint32_t in_frame_off, in_frame_len;
+    uint32_t tot_rd_len = 0;
+
+    while (tot_rd_len < len) {
+        /* To be sure we don't have garbage at the beginning */
+        release_until(&t->tcpq_in, t->rcv_processed);
+        f = first_segment(&t->tcpq_in);
+        if (!f) {
+            tcp_set_space(t);
+            goto out;
+        }
+
+        /* Hole at the beginning of data, awaiting retransmissions. */
+        if (seq_compare(t->rcv_processed, f->seq) < 0) {
+            tcp_dbg("TCP> read hole beginning of data, %08x - %08x. rcv_nxt is %08x\n", t->rcv_processed, f->seq, t->rcv_nxt);
+            goto out;
+        }
+
+        if(seq_compare(t->rcv_processed, f->seq) > 0) {
+            in_frame_off = t->rcv_processed - f->seq;
+            in_frame_len = f->payload_len - in_frame_off;
+        } else {
+            in_frame_off = 0;
+            in_frame_len = f->payload_len;
+        }
+
+        if ((in_frame_len + tot_rd_len) > (uint32_t)len) {
+            in_frame_len = len - tot_rd_len;
+        }
+
+        if (in_frame_len > f->payload_len - in_frame_off)
+            in_frame_len = f->payload_len - in_frame_off;
+
+        memcpy((uint8_t *)buf + tot_rd_len, f->payload + in_frame_off, in_frame_len);
+        tot_rd_len += in_frame_len;
+        t->rcv_processed += in_frame_len;
+
+        if ((in_frame_len == 0u) || (in_frame_len == (uint32_t)f->payload_len)) {
+            pico_discard_segment(&t->tcpq_in, f);
+        }
     }
-
-    if(seq_compare(t->rcv_processed, f->seq) > 0) {
-      in_frame_off = t->rcv_processed - f->seq;
-      in_frame_len = f->payload_len - in_frame_off;
-    } else {
-      in_frame_off = 0;
-      in_frame_len = f->payload_len;
-    }
-    if ((in_frame_len + tot_rd_len) > (uint32_t)len) {
-      in_frame_len = len - tot_rd_len;
+out:
+    tcp_set_space(t);
+    if (t->tcpq_in.size == 0) {
+        s->ev_pending &= (uint16_t)(~PICO_SOCK_EV_RD);
     }
 
-    if (in_frame_len > f->payload_len - in_frame_off)
-      in_frame_len = f->payload_len - in_frame_off;
-
-    memcpy((uint8_t *)buf + tot_rd_len, f->payload + in_frame_off, in_frame_len);
-    tot_rd_len += in_frame_len;
-    t->rcv_processed += in_frame_len;
-
-    if ((in_frame_len == 0u) || (in_frame_len == (uint32_t)f->payload_len)) {
-      pico_discard_segment(&t->tcpq_in, f);
-    }
-  }
-
-out:
-  tcp_set_space(t);
-  if (t->tcpq_in.size == 0) {
-    s->ev_pending &= (uint16_t)(~PICO_SOCK_EV_RD);
-  }
-  return tot_rd_len;
+    return tot_rd_len;
 }
 
 int pico_tcp_initconn(struct pico_socket *s);
 static void initconn_retry(pico_time when, void *arg)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)arg;
-  IGNORE_PARAMETER(when);
-  if (TCPSTATE(&t->sock) == PICO_SOCKET_STATE_TCP_SYN_SENT
-	&& !(t->sock.state & PICO_SOCKET_STATE_SHUT_LOCAL)
-	&& !(t->sock.state & PICO_SOCKET_STATE_SHUT_REMOTE)) {
-    if (t->backoff > PICO_TCP_MAX_CONNECT_RETRIES) {
-      tcp_dbg("TCP> Connection timeout. \n");
-      if (t->sock.wakeup)
-        t->sock.wakeup(PICO_SOCK_EV_ERR, &t->sock);
-      return;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)arg;
+    IGNORE_PARAMETER(when);
+    if (TCPSTATE(&t->sock) == PICO_SOCKET_STATE_TCP_SYN_SENT
+        && !(t->sock.state & PICO_SOCKET_STATE_SHUT_LOCAL)
+        && !(t->sock.state & PICO_SOCKET_STATE_SHUT_REMOTE)) {
+        if (t->backoff > PICO_TCP_MAX_CONNECT_RETRIES) {
+            tcp_dbg("TCP> Connection timeout. \n");
+            if (t->sock.wakeup)
+                t->sock.wakeup(PICO_SOCK_EV_ERR, &t->sock);
+
+            return;
+        }
+
+        tcp_dbg("TCP> SYN retry %d...\n", t->backoff);
+        t->backoff++;
+        pico_tcp_initconn(&t->sock);
+    } else {
+        tcp_dbg("TCP> Connection is already established: no retry needed. good.\n");
     }
-    tcp_dbg("TCP> SYN retry %d...\n", t->backoff);
-    t->backoff++;
-    pico_tcp_initconn(&t->sock);
-  } else {
-    tcp_dbg("TCP> Connection is already established: no retry needed. good.\n");
-  }
 }
 
 int pico_tcp_initconn(struct pico_socket *s)
 {
-  struct pico_socket_tcp *ts = TCP_SOCK(s);
-  struct pico_frame *syn;
-  struct pico_tcp_hdr *hdr;
-  uint16_t opt_len = tcp_options_size(ts, PICO_TCP_SYN);
-
-  syn = s->net->alloc(s->net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
-  if (!syn)
-    return -1;
-  hdr = (struct pico_tcp_hdr *) syn->transport_hdr;
-
-  if (!ts->snd_nxt)
-    ts->snd_nxt = long_be(pico_paws());
-  ts->snd_last = ts->snd_nxt;
-  ts->cwnd = PICO_TCP_IW;
-  ts->ssthresh = 40;
-  syn->sock = s;
-  hdr->seq = long_be(ts->snd_nxt);
-  hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | ts->jumbo);
-  hdr->flags = PICO_TCP_SYN;
-  tcp_set_space(ts);
-  hdr->rwnd = short_be(ts->wnd);
-  tcp_add_options(ts,syn, PICO_TCP_SYN, opt_len);
-  hdr->trans.sport = ts->sock.local_port;
-  hdr->trans.dport = ts->sock.remote_port;
-
-  hdr->crc = 0;
-  hdr->crc = short_be(pico_tcp_checksum_ipv4(syn));
-
-  /* TCP: ENQUEUE to PROTO ( SYN ) */
-  tcp_dbg("Sending SYN... (ports: %d - %d) size: %d\n", short_be(ts->sock.local_port), short_be(ts->sock.remote_port), syn->buffer_len);
-  pico_enqueue(&tcp_out, syn);
-  pico_timer_add(PICO_TCP_SYN_TO << ts->backoff, initconn_retry, ts);
-  return 0;
+    struct pico_socket_tcp *ts = TCP_SOCK(s);
+    struct pico_frame *syn;
+    struct pico_tcp_hdr *hdr;
+    uint16_t opt_len = tcp_options_size(ts, PICO_TCP_SYN);
+
+    syn = s->net->alloc(s->net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
+    if (!syn)
+        return -1;
+
+    hdr = (struct pico_tcp_hdr *) syn->transport_hdr;
+
+    if (!ts->snd_nxt)
+        ts->snd_nxt = long_be(pico_paws());
+
+    ts->snd_last = ts->snd_nxt;
+    ts->cwnd = PICO_TCP_IW;
+    ts->ssthresh = 40;
+    syn->sock = s;
+    hdr->seq = long_be(ts->snd_nxt);
+    hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | ts->jumbo);
+    hdr->flags = PICO_TCP_SYN;
+    tcp_set_space(ts);
+    hdr->rwnd = short_be(ts->wnd);
+    tcp_add_options(ts, syn, PICO_TCP_SYN, opt_len);
+    hdr->trans.sport = ts->sock.local_port;
+    hdr->trans.dport = ts->sock.remote_port;
+
+    hdr->crc = 0;
+    hdr->crc = short_be(pico_tcp_checksum_ipv4(syn));
+
+    /* TCP: ENQUEUE to PROTO ( SYN ) */
+    tcp_dbg("Sending SYN... (ports: %d - %d) size: %d\n", short_be(ts->sock.local_port), short_be(ts->sock.remote_port), syn->buffer_len);
+    pico_enqueue(&tcp_out, syn);
+    pico_timer_add(PICO_TCP_SYN_TO << ts->backoff, initconn_retry, ts);
+    return 0;
 }
 
 static int tcp_send_synack(struct pico_socket *s)
 {
-  struct pico_socket_tcp *ts = TCP_SOCK(s);
-  struct pico_frame *synack;
-  struct pico_tcp_hdr *hdr;
-  uint16_t opt_len = tcp_options_size(ts, PICO_TCP_SYN | PICO_TCP_ACK);
-
-  synack = s->net->alloc(s->net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
-  if (!synack)
-    return -1;
-  hdr = (struct pico_tcp_hdr *) synack->transport_hdr;
-
-  synack->sock = s;
-  hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | ts->jumbo);
-  hdr->flags = PICO_TCP_SYN | PICO_TCP_ACK;
-  hdr->rwnd = short_be(ts->wnd);
-  hdr->seq = long_be(ts->snd_nxt);
-  ts->rcv_processed = long_be(hdr->seq);
-  ts->snd_last = ts->snd_nxt;
-  tcp_set_space(ts);
-  tcp_add_options(ts,synack, hdr->flags, opt_len);
-  synack->payload_len = 0;
-  synack->timestamp = pico_tick;
-  tcp_send(ts, synack);
-  pico_frame_discard(synack);
-  return 0;
+    struct pico_socket_tcp *ts = TCP_SOCK(s);
+    struct pico_frame *synack;
+    struct pico_tcp_hdr *hdr;
+    uint16_t opt_len = tcp_options_size(ts, PICO_TCP_SYN | PICO_TCP_ACK);
+
+    synack = s->net->alloc(s->net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
+    if (!synack)
+        return -1;
+
+    hdr = (struct pico_tcp_hdr *) synack->transport_hdr;
+
+    synack->sock = s;
+    hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | ts->jumbo);
+    hdr->flags = PICO_TCP_SYN | PICO_TCP_ACK;
+    hdr->rwnd = short_be(ts->wnd);
+    hdr->seq = long_be(ts->snd_nxt);
+    ts->rcv_processed = long_be(hdr->seq);
+    ts->snd_last = ts->snd_nxt;
+    tcp_set_space(ts);
+    tcp_add_options(ts, synack, hdr->flags, opt_len);
+    synack->payload_len = 0;
+    synack->timestamp = pico_tick;
+    tcp_send(ts, synack);
+    pico_frame_discard(synack);
+    return 0;
 }
 
 static void tcp_send_empty(struct pico_socket_tcp *t, uint16_t flags)
 {
-  struct pico_frame *f;
-  struct pico_tcp_hdr *hdr;
-  uint16_t opt_len = tcp_options_size(t, flags);
-  f = t->sock.net->alloc(t->sock.net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
-  if (!f) {
-    return;
-  }
-  f->sock = &t->sock;
-  hdr = (struct pico_tcp_hdr *) f->transport_hdr;
-  hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | t->jumbo);
-  hdr->flags = (uint8_t)flags;
-  hdr->rwnd = short_be(t->wnd);
-  tcp_set_space(t);
-  tcp_add_options(t,f, flags, opt_len);
-  hdr->trans.sport = t->sock.local_port;
-  hdr->trans.dport = t->sock.remote_port;
-  hdr->seq = long_be(t->snd_nxt);
-  if ((flags & PICO_TCP_ACK) != 0)
-    hdr->ack = long_be(t->rcv_nxt);
-  t->rcv_ackd = t->rcv_nxt;
-
-  f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
-  hdr->rwnd = short_be(t->wnd);
-  hdr->crc = 0;
-  hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
-
-  /* TCP: ENQUEUE to PROTO */
-  pico_enqueue(&tcp_out, f);
+    struct pico_frame *f;
+    struct pico_tcp_hdr *hdr;
+    uint16_t opt_len = tcp_options_size(t, flags);
+    f = t->sock.net->alloc(t->sock.net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
+    if (!f) {
+        return;
+    }
+
+    f->sock = &t->sock;
+    hdr = (struct pico_tcp_hdr *) f->transport_hdr;
+    hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | t->jumbo);
+    hdr->flags = (uint8_t)flags;
+    hdr->rwnd = short_be(t->wnd);
+    tcp_set_space(t);
+    tcp_add_options(t, f, flags, opt_len);
+    hdr->trans.sport = t->sock.local_port;
+    hdr->trans.dport = t->sock.remote_port;
+    hdr->seq = long_be(t->snd_nxt);
+    if ((flags & PICO_TCP_ACK) != 0)
+        hdr->ack = long_be(t->rcv_nxt);
+
+    t->rcv_ackd = t->rcv_nxt;
+
+    f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
+    hdr->rwnd = short_be(t->wnd);
+    hdr->crc = 0;
+    hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
+
+    /* TCP: ENQUEUE to PROTO */
+    pico_enqueue(&tcp_out, f);
 }
 
 static void tcp_send_ack(struct pico_socket_tcp *t)
 {
-  return tcp_send_empty(t, PICO_TCP_ACK);
+    return tcp_send_empty(t, PICO_TCP_ACK);
 }
 
 static int tcp_send_rst(struct pico_socket *s, struct pico_frame *fr)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
-  struct pico_frame *f;
-  struct pico_tcp_hdr *hdr, *hdr_rcv;
-  uint16_t opt_len = tcp_options_size(t, PICO_TCP_RST);
-  int close;
-
-  tcp_dbg("TCP SEND_RST >>>>>>>>>>>>>>> START\n");
-
-  f = t->sock.net->alloc(t->sock.net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
-
-  if (!f) {
-    return -1;
-  }
-
-  hdr_rcv = (struct pico_tcp_hdr *) fr->transport_hdr;
-
-  f->sock = &t->sock;
-  hdr = (struct pico_tcp_hdr *) f->transport_hdr;
-  hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | t->jumbo);
-  hdr->flags = PICO_TCP_RST;
-  hdr->rwnd = short_be(t->wnd);
-  tcp_set_space(t);
-  tcp_add_options(t,f, PICO_TCP_RST, opt_len);
-  hdr->trans.sport = t->sock.local_port;
-  hdr->trans.dport = t->sock.remote_port;
-  hdr->seq = long_be(t->snd_nxt);
-
-  /* check if state is synchronized */
-  if (((s->state & PICO_SOCKET_STATE_TCP) > PICO_SOCKET_STATE_TCP_SYN_RECV)) {
-    /* in synchronized state: send RST with seq = ack from previous segment */
-    hdr->seq = hdr_rcv->ack;
-    close = 0;
-  } else {
-    /* non-synchronized state */
-    /* go to CLOSED here to prevent timer callback to go on after timeout */
-    (t->sock).state &= 0x00FFU;
-    (t->sock).state |= PICO_SOCKET_STATE_TCP_CLOSED;
-    close = 1;
-  }
-
-  hdr->ack = long_be(t->rcv_nxt);
-  t->rcv_ackd = t->rcv_nxt;
-  f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
-  hdr->rwnd = short_be(t->wnd);
-  hdr->crc = 0;
-  hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
-
-  /* TCP: ENQUEUE to PROTO */
-  pico_enqueue(&tcp_out, f);
-
-  /* goto CLOSED */
-  if (close) {
-    (t->sock).state &= 0xFF00U;
-    (t->sock).state |= PICO_SOCKET_STATE_CLOSED;
-
-    /* call EV_FIN wakeup before deleting */
-    if ((t->sock).wakeup)
-      (t->sock).wakeup(PICO_SOCK_EV_FIN, &(t->sock));
-
-    /* delete socket */
-      pico_socket_del(&t->sock);
-
-    tcp_dbg("TCP SEND_RST >>>>>>>>>>>>>>> DONE, deleted socket\n");
-  }
-
-  return 0;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
+    struct pico_frame *f;
+    struct pico_tcp_hdr *hdr, *hdr_rcv;
+    uint16_t opt_len = tcp_options_size(t, PICO_TCP_RST);
+    int close;
+
+    tcp_dbg("TCP SEND_RST >>>>>>>>>>>>>>> START\n");
+
+    f = t->sock.net->alloc(t->sock.net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
+
+    if (!f) {
+        return -1;
+    }
+
+    hdr_rcv = (struct pico_tcp_hdr *) fr->transport_hdr;
+
+    f->sock = &t->sock;
+    hdr = (struct pico_tcp_hdr *) f->transport_hdr;
+    hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | t->jumbo);
+    hdr->flags = PICO_TCP_RST;
+    hdr->rwnd = short_be(t->wnd);
+    tcp_set_space(t);
+    tcp_add_options(t, f, PICO_TCP_RST, opt_len);
+    hdr->trans.sport = t->sock.local_port;
+    hdr->trans.dport = t->sock.remote_port;
+    hdr->seq = long_be(t->snd_nxt);
+
+    /* check if state is synchronized */
+    if (((s->state & PICO_SOCKET_STATE_TCP) > PICO_SOCKET_STATE_TCP_SYN_RECV)) {
+        /* in synchronized state: send RST with seq = ack from previous segment */
+        hdr->seq = hdr_rcv->ack;
+        close = 0;
+    } else {
+        /* non-synchronized state */
+        /* go to CLOSED here to prevent timer callback to go on after timeout */
+        (t->sock).state &= 0x00FFU;
+        (t->sock).state |= PICO_SOCKET_STATE_TCP_CLOSED;
+        close = 1;
+    }
+
+    hdr->ack = long_be(t->rcv_nxt);
+    t->rcv_ackd = t->rcv_nxt;
+    f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
+    hdr->rwnd = short_be(t->wnd);
+    hdr->crc = 0;
+    hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
+
+    /* TCP: ENQUEUE to PROTO */
+    pico_enqueue(&tcp_out, f);
+
+    /* goto CLOSED */
+    if (close) {
+        (t->sock).state &= 0xFF00U;
+        (t->sock).state |= PICO_SOCKET_STATE_CLOSED;
+
+        /* call EV_FIN wakeup before deleting */
+        if ((t->sock).wakeup)
+            (t->sock).wakeup(PICO_SOCK_EV_FIN, &(t->sock));
+
+        /* delete socket */
+        pico_socket_del(&t->sock);
+
+        tcp_dbg("TCP SEND_RST >>>>>>>>>>>>>>> DONE, deleted socket\n");
+    }
+
+    return 0;
 }
 
 int pico_tcp_reply_rst(struct pico_frame *fr)
 {
-  struct pico_tcp_hdr *hdr;
-  struct pico_frame *f;
-  uint16_t size = PICO_SIZE_TCPHDR;
-
-  tcp_dbg("TCP>>>>>>>>>>>>>>>> sending RST ... <<<<<<<<<<<<<<<<<<\n");
-
-  f = fr->sock->net->alloc(fr->sock->net, size);
-
-  /* fill in IP data from original frame */
-  // TODO if IPv4
-  ((struct pico_ipv4_hdr *)(f->net_hdr))->dst.addr = ((struct pico_ipv4_hdr *)(fr->net_hdr))->src.addr;
-  ((struct pico_ipv4_hdr *)(f->net_hdr))->src.addr = ((struct pico_ipv4_hdr *)(fr->net_hdr))->dst.addr;
-
-  /* fill in TCP data from original frame */
-  ((struct pico_tcp_hdr *)(f->transport_hdr))->trans.dport = ((struct pico_tcp_hdr *)(fr->transport_hdr))->trans.sport;
-  ((struct pico_tcp_hdr *)(f->transport_hdr))->trans.sport = ((struct pico_tcp_hdr *)(fr->transport_hdr))->trans.dport;
-  hdr = (struct pico_tcp_hdr *) f->transport_hdr;
-  hdr->len   = (uint8_t)(size << 2);
-  hdr->flags = PICO_TCP_RST | PICO_TCP_ACK;
-  hdr->rwnd  = 0;
-  if (((struct pico_tcp_hdr *)(fr->transport_hdr))->flags & PICO_TCP_ACK) {
-    hdr->seq = ((struct pico_tcp_hdr *)(fr->transport_hdr))->ack;
-  } else {
-    hdr->seq = 0U;
-  }
-
-  hdr->ack = long_be(long_be(((struct pico_tcp_hdr *)(fr->transport_hdr))->seq) + fr->payload_len);
-  hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
-  /* enqueue for transmission */
-  pico_ipv4_frame_push(f,&(((struct pico_ipv4_hdr *)(f->net_hdr))->dst),PICO_PROTO_TCP);
-
-  return 0;
+    struct pico_tcp_hdr *hdr, *hdr1;
+    struct pico_frame *f;
+    uint16_t size = PICO_SIZE_TCPHDR;
+
+    tcp_dbg("TCP>>>>>>>>>>>>>>>> sending RST ... <<<<<<<<<<<<<<<<<<\n");
+
+    hdr1 = (struct pico_tcp_hdr *) (fr->transport_hdr);
+    f = fr->sock->net->alloc(fr->sock->net, size);
+
+    /* fill in IP data from original frame */
+    /* TODO if IPv4 */
+    ((struct pico_ipv4_hdr *)(f->net_hdr))->dst.addr = ((struct pico_ipv4_hdr *)(fr->net_hdr))->src.addr;
+    ((struct pico_ipv4_hdr *)(f->net_hdr))->src.addr = ((struct pico_ipv4_hdr *)(fr->net_hdr))->dst.addr;
+
+    /* fill in TCP data from original frame */
+    ((struct pico_tcp_hdr *)(f->transport_hdr))->trans.dport = ((struct pico_tcp_hdr *)(fr->transport_hdr))->trans.sport;
+    ((struct pico_tcp_hdr *)(f->transport_hdr))->trans.sport = ((struct pico_tcp_hdr *)(fr->transport_hdr))->trans.dport;
+    hdr = (struct pico_tcp_hdr *) f->transport_hdr;
+    hdr->len   = (uint8_t)(size << 2);
+    hdr->flags = PICO_TCP_RST;
+    if(!(hdr1->flags & PICO_TCP_ACK))
+        hdr->flags |= PICO_TCP_ACK;
+
+    hdr->rwnd  = 0;
+    if (((struct pico_tcp_hdr *)(fr->transport_hdr))->flags & PICO_TCP_ACK) {
+        hdr->seq = ((struct pico_tcp_hdr *)(fr->transport_hdr))->ack;
+    } else {
+        hdr->seq = 0U;
+    }
+
+    hdr->ack = 0;
+    if(!(hdr1->flags & PICO_TCP_ACK))
+        hdr->ack = long_be(long_be(((struct pico_tcp_hdr *)(fr->transport_hdr))->seq) + fr->payload_len);
+
+    hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
+    /* enqueue for transmission */
+    pico_ipv4_frame_push(f, &(((struct pico_ipv4_hdr *)(f->net_hdr))->dst), PICO_PROTO_TCP);
+
+    return 0;
 }
 
 static int tcp_nosync_rst(struct pico_socket *s, struct pico_frame *fr)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
-  struct pico_frame *f;
-  struct pico_tcp_hdr *hdr, *hdr_rcv;
-  uint16_t opt_len = tcp_options_size(t, PICO_TCP_RST | PICO_TCP_ACK);
-
-  tcp_dbg("TCP SEND RST (NON-SYNC) >>>>>>>>>>>>>>>>>> state %x\n",(s->state & PICO_SOCKET_STATE_TCP));
-  if (((s->state & PICO_SOCKET_STATE_TCP) ==  PICO_SOCKET_STATE_TCP_LISTEN)) {
-    /* XXX TODO NOTE: to prevent the parent socket from trying to send, because this socket has no knowledge of dst IP !!! */
-    return pico_tcp_reply_rst(fr);
-  }
-
-  /***************************************************************************/
-  /* sending RST */
-  f = t->sock.net->alloc(t->sock.net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
-
-  if (!f) {
-    return -1;
-  }
-
-  hdr_rcv = (struct pico_tcp_hdr *) fr->transport_hdr;
-
-  f->sock = &t->sock;
-  hdr = (struct pico_tcp_hdr *) f->transport_hdr;
-  hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | t->jumbo);
-  hdr->flags = PICO_TCP_RST | PICO_TCP_ACK;
-  hdr->rwnd = short_be(t->wnd);
-  tcp_set_space(t);
-  tcp_add_options(t,f, PICO_TCP_RST | PICO_TCP_ACK, opt_len);
-  hdr->trans.sport = t->sock.local_port;
-  hdr->trans.dport = t->sock.remote_port;
-
-  /* non-synchronized state */
-  if (hdr_rcv->flags & PICO_TCP_ACK) {
-    hdr->seq = hdr_rcv->ack;
-  } else {
-    hdr->seq = 0U;
-  }
-
-  hdr->ack = long_be(SEQN(fr) + fr->payload_len);
-
-  t->rcv_ackd = t->rcv_nxt;
-  f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
-  hdr->rwnd = short_be(t->wnd);
-  hdr->crc = 0;
-  hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
-
-  /* TCP: ENQUEUE to PROTO */
-  pico_enqueue(&tcp_out, f);
-
-  /***************************************************************************/
-
-  tcp_dbg("TCP SEND_RST (NON_SYNC) >>>>>>>>>>>>>>> DONE, ...\n");
-
-  return 0;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
+    struct pico_frame *f;
+    struct pico_tcp_hdr *hdr, *hdr_rcv;
+    uint16_t opt_len = tcp_options_size(t, PICO_TCP_RST | PICO_TCP_ACK);
+
+    tcp_dbg("TCP SEND RST (NON-SYNC) >>>>>>>>>>>>>>>>>> state %x\n", (s->state & PICO_SOCKET_STATE_TCP));
+    if (((s->state & PICO_SOCKET_STATE_TCP) ==  PICO_SOCKET_STATE_TCP_LISTEN)) {
+        /* XXX TODO NOTE: to prevent the parent socket from trying to send, because this socket has no knowledge of dst IP !!! */
+        return pico_tcp_reply_rst(fr);
+    }
+
+    /***************************************************************************/
+    /* sending RST */
+    f = t->sock.net->alloc(t->sock.net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
+
+    if (!f) {
+        return -1;
+    }
+
+    hdr_rcv = (struct pico_tcp_hdr *) fr->transport_hdr;
+
+    f->sock = &t->sock;
+    hdr = (struct pico_tcp_hdr *) f->transport_hdr;
+    hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | t->jumbo);
+    hdr->flags = PICO_TCP_RST | PICO_TCP_ACK;
+    hdr->rwnd = short_be(t->wnd);
+    tcp_set_space(t);
+    tcp_add_options(t, f, PICO_TCP_RST | PICO_TCP_ACK, opt_len);
+    hdr->trans.sport = t->sock.local_port;
+    hdr->trans.dport = t->sock.remote_port;
+
+    /* non-synchronized state */
+    if (hdr_rcv->flags & PICO_TCP_ACK) {
+        hdr->seq = hdr_rcv->ack;
+    } else {
+        hdr->seq = 0U;
+    }
+
+    hdr->ack = long_be(SEQN(fr) + fr->payload_len);
+
+    t->rcv_ackd = t->rcv_nxt;
+    f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
+    hdr->rwnd = short_be(t->wnd);
+    hdr->crc = 0;
+    hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
+
+    /* TCP: ENQUEUE to PROTO */
+    pico_enqueue(&tcp_out, f);
+
+    /***************************************************************************/
+
+    tcp_dbg("TCP SEND_RST (NON_SYNC) >>>>>>>>>>>>>>> DONE, ...\n");
+
+    return 0;
 }
 
 static void tcp_send_fin(struct pico_socket_tcp *t)
 {
-  struct pico_frame *f;
-  struct pico_tcp_hdr *hdr;
-  uint16_t opt_len = tcp_options_size(t, PICO_TCP_FIN);
-  f = t->sock.net->alloc(t->sock.net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
-  if (!f) {
-    return;
-  }
-  f->sock = &t->sock;
-  hdr = (struct pico_tcp_hdr *) f->transport_hdr;
-  hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | t->jumbo);
-  hdr->flags = PICO_TCP_FIN | PICO_TCP_ACK;
-  hdr->ack = long_be(t->rcv_nxt);
-  t->rcv_ackd = t->rcv_nxt;
-  hdr->rwnd = short_be(t->wnd);
-  tcp_set_space(t);
-  tcp_add_options(t,f, PICO_TCP_FIN, opt_len);
-  hdr->trans.sport = t->sock.local_port;
-  hdr->trans.dport = t->sock.remote_port;
-  hdr->seq = long_be(t->snd_nxt);   /* XXX TODO check correct ?? --> snd_last? otherwise maybe data after FIN */
-
-  f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
-  hdr->rwnd = short_be(t->wnd);
-  hdr->crc = 0;
-  hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
-  //tcp_dbg("SENDING FIN...\n");
-  /* TCP: ENQUEUE to PROTO ( Pure ACK ) */
-  pico_enqueue(&tcp_out, f);
-  t->snd_nxt++;
+    struct pico_frame *f;
+    struct pico_tcp_hdr *hdr;
+    uint16_t opt_len = tcp_options_size(t, PICO_TCP_FIN);
+    f = t->sock.net->alloc(t->sock.net, (uint16_t)(PICO_SIZE_TCPHDR + opt_len));
+    if (!f) {
+        return;
+    }
+
+    f->sock = &t->sock;
+    hdr = (struct pico_tcp_hdr *) f->transport_hdr;
+    hdr->len = (uint8_t)((PICO_SIZE_TCPHDR + opt_len) << 2 | t->jumbo);
+    hdr->flags = PICO_TCP_FIN | PICO_TCP_ACK;
+    hdr->ack = long_be(t->rcv_nxt);
+    t->rcv_ackd = t->rcv_nxt;
+    hdr->rwnd = short_be(t->wnd);
+    tcp_set_space(t);
+    tcp_add_options(t, f, PICO_TCP_FIN, opt_len);
+    hdr->trans.sport = t->sock.local_port;
+    hdr->trans.dport = t->sock.remote_port;
+    hdr->seq = long_be(t->snd_nxt); /* XXX TODO check correct ?? --> snd_last? otherwise maybe data after FIN */
+
+    f->start = f->transport_hdr + PICO_SIZE_TCPHDR;
+    hdr->rwnd = short_be(t->wnd);
+    hdr->crc = 0;
+    hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
+    /* tcp_dbg("SENDING FIN...\n"); */
+    /* TCP: ENQUEUE to PROTO ( Pure ACK ) */
+    pico_enqueue(&tcp_out, f);
+    t->snd_nxt++;
 }
 
 static void tcp_sack_prepare(struct pico_socket_tcp *t)
 {
-  struct tcp_input_segment *pkt;
-  uint32_t left=0, right=0;
-  struct tcp_sack_block *sb;
-  int n = 0;
-  if (t->sacks) /* previous sacks are pending */
-    return;
-
-  pkt = first_segment(&t->tcpq_in);
-  while(n < 3) {
-    if (!pkt) {
-      if(left) {
-        sb = pico_zalloc(sizeof(struct tcp_sack_block));
-        if (!sb)
-          break;
-        sb->left = long_be(left);
-        sb->right = long_be(right);
-        n++;
-        sb->next = t->sacks;
-        t->sacks = sb;
-        left = 0;
-        right = 0;
-      }
-      break;
+    struct tcp_input_segment *pkt;
+    uint32_t left = 0, right = 0;
+    struct tcp_sack_block *sb;
+    int n = 0;
+    if (t->sacks) /* previous sacks are pending */
+        return;
+
+    pkt = first_segment(&t->tcpq_in);
+    while(n < 3) {
+        if (!pkt) {
+            if(left) {
+                sb = pico_zalloc(sizeof(struct tcp_sack_block));
+                if (!sb)
+                    break;
+
+                sb->left = long_be(left);
+                sb->right = long_be(right);
+                n++;
+                sb->next = t->sacks;
+                t->sacks = sb;
+                left = 0;
+                right = 0;
+            }
+
+            break;
+        }
+
+        if (pkt->seq < t->rcv_nxt) {
+            pkt = next_segment(&t->tcpq_in, pkt);
+            continue;
+        }
+
+        if (!left) {
+            left = pkt->seq;
+            right = pkt->seq + pkt->payload_len;
+            pkt = next_segment(&t->tcpq_in, pkt);
+            continue;
+        }
+
+        if(pkt->seq == right) {
+            right += pkt->payload_len;
+            pkt = next_segment(&t->tcpq_in, pkt);
+            continue;
+        } else {
+            sb = pico_zalloc(sizeof(struct tcp_sack_block));
+            if (!sb)
+                break;
+
+            sb->left = long_be(left);
+            sb->right = long_be(right);
+            n++;
+            sb->next = t->sacks;
+            t->sacks = sb;
+            left = 0;
+            right = 0;
+            pkt = next_segment(&t->tcpq_in, pkt);
+        }
     }
-    if (pkt->seq < t->rcv_nxt) {
-      pkt = next_segment(&t->tcpq_in, pkt);
-      continue;
-    }
-    if (!left) {
-      left = pkt->seq;
-      right = pkt->seq + pkt->payload_len;
-      pkt = next_segment(&t->tcpq_in, pkt);
-      continue;
-    }
-    if(pkt->seq == right) {
-      right += pkt->payload_len;
-      pkt = next_segment(&t->tcpq_in, pkt);
-      continue;
-    } else {
-      sb = pico_zalloc(sizeof(struct tcp_sack_block));
-      if (!sb)
-        break;
-      sb->left = long_be(left);
-      sb->right = long_be(right);
-      n++;
-      sb->next = t->sacks;
-      t->sacks = sb;
-      left = 0;
-      right = 0;
-      pkt = next_segment(&t->tcpq_in, pkt);
-    }
-  }
 }
 
 static int tcp_data_in(struct pico_socket *s, struct pico_frame *f)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *) f->transport_hdr;
-
-  if (((hdr->len & 0xf0) >> 2) <= f->transport_len) {
-    tcp_parse_options(f);
-    f->payload = f->transport_hdr + ((hdr->len & 0xf0) >>2);
-    f->payload_len = (uint16_t)(f->transport_len - ((hdr->len & 0xf0) >>2));
-    tcp_dbg("TCP> Received segment. (exp: %x got: %x)\n", t->rcv_nxt, SEQN(f));
-
-    if (seq_compare(SEQN(f), t->rcv_nxt) <= 0) {
-      struct tcp_input_segment *nxt;
-      if (seq_compare(SEQN(f), t->rcv_nxt) == 0) { /* Exactly what we expected */
-        // Create new segment and enqueue it
-    	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);
-    	}
-
-        t->rcv_nxt = SEQN(f) + f->payload_len;
-        nxt = peek_segment(&t->tcpq_in, t->rcv_nxt);
-        while(nxt) {
-          tcp_dbg("scrolling rcv_nxt...%08x\n", t->rcv_nxt);
-          t->rcv_nxt += nxt->payload_len;
-          nxt = peek_segment(&t->tcpq_in, t->rcv_nxt);
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *) f->transport_hdr;
+
+    if (((hdr->len & 0xf0) >> 2) <= f->transport_len) {
+        tcp_parse_options(f);
+        f->payload = f->transport_hdr + ((hdr->len & 0xf0) >> 2);
+        f->payload_len = (uint16_t)(f->transport_len - ((hdr->len & 0xf0) >> 2));
+        tcp_dbg("TCP> Received segment. (exp: %x got: %x)\n", t->rcv_nxt, SEQN(f));
+
+        if (seq_compare(SEQN(f), t->rcv_nxt) <= 0) {
+            struct tcp_input_segment *nxt;
+            if (seq_compare(SEQN(f), t->rcv_nxt) == 0) { /* Exactly what we expected */
+                /* Create new segment and enqueue it */
+                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);
+                }
+
+                t->rcv_nxt = SEQN(f) + f->payload_len;
+                nxt = peek_segment(&t->tcpq_in, t->rcv_nxt);
+                while(nxt) {
+                    tcp_dbg("scrolling rcv_nxt...%08x\n", t->rcv_nxt);
+                    t->rcv_nxt += nxt->payload_len;
+                    nxt = peek_segment(&t->tcpq_in, t->rcv_nxt);
+                }
+                t->sock.ev_pending |= PICO_SOCK_EV_RD;
+            } 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) {
+                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);
+            }
         }
-        t->sock.ev_pending |= PICO_SOCK_EV_RD;
-      } else {
-        tcp_dbg("TCP> lo segment. Uninteresting retransmission. (exp: %x got: %x)\n", t->rcv_nxt, SEQN(f));
-      }
+
+        /* 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); */
+            tcp_send_ack(t);
+        } else {
+            /* tcp_dbg("SENDACK PREVENTED IN SYNSENT STATE\n"); */
+        }
+
+        return 0;
     } 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) {
-    	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);
-      tcp_send_ack(t);
-    } else {
-      //tcp_dbg("SENDACK PREVENTED IN SYNSENT STATE\n");
+        tcp_dbg("TCP: invalid data in pkt len, exp: %d, got %d\n", (hdr->len & 0xf0) >> 2, f->transport_len);
+        return -1;
     }
-    return 0;
-  } else {
-    tcp_dbg("TCP: invalid data in pkt len, exp: %d, got %d\n", (hdr->len & 0xf0) >> 2, f->transport_len);
-    return -1;
-  }
 }
 
-static int tcp_ack_advance_una(struct pico_socket_tcp *t, struct pico_frame *f, pico_time * 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)
-    t->sock.ev_pending |= PICO_SOCK_EV_WR;
-  return ret;
+    int ret =  release_all_until(&t->tcpq_out, ACKN(f), timestamp);
+    if (ret > 0)
+        t->sock.ev_pending |= PICO_SOCK_EV_WR;
+
+    return ret;
 }
 
 static uint16_t time_diff(pico_time a, pico_time b)
 {
-  if (a >= b)
-    return (uint16_t)(a - b);
-  else
-    return (uint16_t)(b - a);
+    if (a >= b)
+        return (uint16_t)(a - b);
+    else
+        return (uint16_t)(b - a);
 }
 
 static void tcp_rtt(struct pico_socket_tcp *t, uint32_t rtt)
 {
 
-  uint32_t avg = t->avg_rtt;
-  uint32_t rvar = t->rttvar;
-  if (!avg) {
-    /* This follows RFC2988
-     * (2.2) When the first RTT measurement R is made, the host MUST set
-     *
-     * SRTT <- R
-     * RTTVAR <- R/2
-     * RTO <- SRTT + max (G, K*RTTVAR)
-     */
-    t->avg_rtt = rtt;
-    t->rttvar = rtt >> 1;
-    t->rto = t->avg_rtt + (t->rttvar << 4);
-  } else {
-    int32_t var = (int32_t)(t->avg_rtt - rtt);
-    if (var < 0)
-      var = 0-var;
-    /* RFC2988, section (2.3). Alpha and beta are the ones suggested. */
-
-    /* First, evaluate a new value for the rttvar */
-    t->rttvar <<= 2;
-    t->rttvar -= rvar;
-    t->rttvar += (uint32_t)var;
-    t->rttvar >>= 2;
-
-    /* Then, calculate the new avg_rtt */
-    t->avg_rtt <<= 3;
-    t->avg_rtt -= avg;
-    t->avg_rtt += rtt;
-    t->avg_rtt >>= 3;
-
-    /* Finally, assign a new value for the RTO, as specified in the RFC, with K=4 */
-    t->rto = t->avg_rtt + (t->rttvar << 2);
-  }
-  tcp_dbg(" -----=============== RTT CUR: %u AVG: %u RTTVAR: %u RTO: %u ======================----\n", rtt, t->avg_rtt, t->rttvar, t->rto);
+    uint32_t avg = t->avg_rtt;
+    uint32_t rvar = t->rttvar;
+    if (!avg) {
+        /* This follows RFC2988
+         * (2.2) When the first RTT measurement R is made, the host MUST set
+         *
+         * SRTT <- R
+         * RTTVAR <- R/2
+         * RTO <- SRTT + max (G, K*RTTVAR)
+         */
+        t->avg_rtt = rtt;
+        t->rttvar = rtt >> 1;
+        t->rto = t->avg_rtt + (t->rttvar << 4);
+    } else {
+        int32_t var = (int32_t)(t->avg_rtt - rtt);
+        if (var < 0)
+            var = 0 - var;
+
+        /* RFC2988, section (2.3). Alpha and beta are the ones suggested. */
+
+        /* First, evaluate a new value for the rttvar */
+        t->rttvar <<= 2;
+        t->rttvar -= rvar;
+        t->rttvar += (uint32_t)var;
+        t->rttvar >>= 2;
+
+        /* Then, calculate the new avg_rtt */
+        t->avg_rtt <<= 3;
+        t->avg_rtt -= avg;
+        t->avg_rtt += rtt;
+        t->avg_rtt >>= 3;
+
+        /* Finally, assign a new value for the RTO, as specified in the RFC, with K=4 */
+        t->rto = t->avg_rtt + (t->rttvar << 2);
+    }
+
+    tcp_dbg(" -----=============== RTT CUR: %u AVG: %u RTTVAR: %u RTO: %u ======================----\n", rtt, t->avg_rtt, t->rttvar, t->rto);
 }
 
 static void tcp_congestion_control(struct pico_socket_tcp *t)
 {
-  if (t->x_mode > PICO_TCP_LOOKAHEAD)
-    return;
-  if (t->cwnd > t->tcpq_out.frames) {
-    tcp_dbg("Limited by app: %d\n", t->cwnd);
-    if (t->sock.wakeup)
-      t->sock.wakeup(PICO_SOCK_EV_WR, &t->sock);
-    return;
-  }
-  tcp_dbg("Doing congestion control\n");
-  if (t->cwnd < t->ssthresh) {
-    t->cwnd++;
-  } else {
-    t->cwnd_counter++;
-    if (t->cwnd_counter >= t->cwnd) {
-      t->cwnd++;
-      t->cwnd_counter = 0;
+    if (t->x_mode > PICO_TCP_LOOKAHEAD)
+        return;
+
+    if (t->cwnd > t->tcpq_out.frames) {
+        tcp_dbg("Limited by app: %d\n", t->cwnd);
+        if (t->sock.wakeup)
+            t->sock.wakeup(PICO_SOCK_EV_WR, &t->sock);
+
+        return;
     }
-  }
-  tcp_dbg("TCP_CWND, %lu, %u, %u, %u\n", pico_tick, t->cwnd, t->ssthresh, t->in_flight);
+
+    tcp_dbg("Doing congestion control\n");
+    if (t->cwnd < t->ssthresh) {
+        t->cwnd++;
+    } else {
+        t->cwnd_counter++;
+        if (t->cwnd_counter >= t->cwnd) {
+            t->cwnd++;
+            t->cwnd_counter = 0;
+        }
+    }
+
+    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, 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;
-  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)
-  {
-		tcp_dbg("TIMEOUT! backoff = %d\n", t->backoff);
-		/* was timer cancelled? */
-		if (t->timer_running == 0) {
-			add_retransmission_timer(t, 0);
-			return;
-		}
-		t->timer_running--;
-
-		f = first_segment(&t->tcpq_out);
-		while (f) {
-			if ((t->x_mode == PICO_TCP_WINDOW_FULL) ||
-					((f->timestamp != 0) && (f->timestamp <= limit))) {
-				struct pico_frame *cpy;
-				tcp_dbg("TCP BLACKOUT> TIMED OUT (output) frame %08x, len= %d rto=%d Win full: %d frame flags: %04x\n", SEQN(f), f->payload_len, t->rto, t->x_mode == PICO_TCP_WINDOW_FULL, f->flags);
-				if ((t->x_mode != PICO_TCP_WINDOW_FULL) ) {
-					t->x_mode = PICO_TCP_BLACKOUT;
-					tcp_dbg("Mode: Blackout.\n");
-					t->cwnd = PICO_TCP_IW;
-					t->in_flight = 0;
-				}
-				tcp_add_header(t,f);
-				/* TCP: ENQUEUE to PROTO ( retransmit )*/
-				cpy = pico_frame_copy(f);
-				if (pico_enqueue(&tcp_out, cpy) > 0) {
-					t->backoff++;
-					t->snd_last_out = SEQN(cpy);
-					add_retransmission_timer(t, (t->rto << t->backoff) + pico_tick);
-					tcp_dbg("TCP_CWND, %lu, %u, %u, %u\n", pico_tick, t->cwnd, t->ssthresh, t->in_flight);
-					return;
-				} else {
-					add_retransmission_timer(t, (t->rto << t->backoff) + pico_tick);
-					pico_frame_discard(cpy);
-				}
-			}
-			f = next_segment(&t->tcpq_out, f);
-		}
-		t->backoff = 0;
-		add_retransmission_timer(t, 0);
-		if (t->tcpq_out.size < t->tcpq_out.max_size)
-		  t->sock.ev_pending |= PICO_SOCK_EV_WR;
-		return;
-	}
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) sock;
+    struct pico_frame *f = NULL;
+    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)
+    {
+        tcp_dbg("TIMEOUT! backoff = %d\n", t->backoff);
+        /* was timer cancelled? */
+        if (t->timer_running == 0) {
+            add_retransmission_timer(t, 0);
+            return;
+        }
+
+        t->timer_running--;
+
+        f = first_segment(&t->tcpq_out);
+        while (f) {
+            if ((t->x_mode == PICO_TCP_WINDOW_FULL) ||
+                ((f->timestamp != 0) && (f->timestamp <= limit))) {
+                struct pico_frame *cpy;
+                tcp_dbg("TCP BLACKOUT> TIMED OUT (output) frame %08x, len= %d rto=%d Win full: %d frame flags: %04x\n", SEQN(f), f->payload_len, t->rto, t->x_mode == PICO_TCP_WINDOW_FULL, f->flags);
+                if ((t->x_mode != PICO_TCP_WINDOW_FULL)) {
+                    t->x_mode = PICO_TCP_BLACKOUT;
+                    tcp_dbg("Mode: Blackout.\n");
+                    t->cwnd = PICO_TCP_IW;
+                    t->in_flight = 0;
+                }
+
+                tcp_add_header(t, f);
+                /* TCP: ENQUEUE to PROTO ( retransmit )*/
+                cpy = pico_frame_copy(f);
+                if (pico_enqueue(&tcp_out, cpy) > 0) {
+                    t->backoff++;
+                    t->snd_last_out = SEQN(cpy);
+                    add_retransmission_timer(t, (t->rto << t->backoff) + pico_tick);
+                    tcp_dbg("TCP_CWND, %lu, %u, %u, %u\n", pico_tick, t->cwnd, t->ssthresh, t->in_flight);
+                    return;
+                } else {
+                    add_retransmission_timer(t, (t->rto << t->backoff) + pico_tick);
+                    pico_frame_discard(cpy);
+                }
+            }
+
+            f = next_segment(&t->tcpq_out, f);
+        }
+        t->backoff = 0;
+        add_retransmission_timer(t, 0);
+        if (t->tcpq_out.size < t->tcpq_out.max_size)
+            t->sock.ev_pending |= PICO_SOCK_EV_WR;
+
+        return;
+    }
     else if(t->backoff >= PICO_TCP_MAX_RETRANS && (t->sock.state & 0xFF00) == PICO_SOCKET_STATE_TCP_ESTABLISHED )
-	{
-		// the retransmission timer, failed to get an ack for a frame, giving up on the connection
-		tcp_discard_all_segments(&t->tcpq_out);
-		if(t->sock.wakeup)
-		  t->sock.wakeup(PICO_SOCK_EV_FIN,&t->sock);
-	}
+    {
+        /* the retransmission timer, failed to get an ack for a frame, giving up on the connection */
+        tcp_discard_all_segments(&t->tcpq_out);
+        if(t->sock.wakeup)
+            t->sock.wakeup(PICO_SOCK_EV_FIN, &t->sock);
+    }
 }
 
 static void add_retransmission_timer(struct pico_socket_tcp *t, pico_time next_ts)
 {
-  struct pico_tree_node * index;
-
-  if (t->timer_running > 0)
-    return;
-
-  if (next_ts == 0) {
-    struct pico_frame *f;
-
-    pico_tree_foreach(index,&t->tcpq_out.pool){
-      f = index->keyValue;
-      if (((next_ts == 0) || (f->timestamp < next_ts)) && (f->timestamp > 0)) {
-        next_ts = f->timestamp;
-      }
+    struct pico_tree_node *index;
+
+    if (t->timer_running > 0)
+        return;
+
+    if (next_ts == 0) {
+        struct pico_frame *f;
+
+        pico_tree_foreach(index, &t->tcpq_out.pool){
+            f = index->keyValue;
+            if (((next_ts == 0) || (f->timestamp < next_ts)) && (f->timestamp > 0)) {
+                next_ts = f->timestamp;
+            }
+        }
     }
-  }
-  if (next_ts > 0) {
-    if ((next_ts + t->rto) > pico_tick) {
-      t->retrans_tmr = pico_timer_add(next_ts + t->rto - pico_tick, tcp_retrans_timeout, t);
-    } else {
-        t->retrans_tmr = pico_timer_add(1, tcp_retrans_timeout, t);
+
+    if (next_ts > 0) {
+        if ((next_ts + t->rto) > pico_tick) {
+            t->retrans_tmr = pico_timer_add(next_ts + t->rto - pico_tick, tcp_retrans_timeout, t);
+        } else {
+            t->retrans_tmr = pico_timer_add(1, tcp_retrans_timeout, t);
+        }
+
+        t->timer_running++;
     }
-    t->timer_running++;
-  }
 }
 
 static int tcp_retrans(struct pico_socket_tcp *t, struct pico_frame *f)
 {
-  struct pico_frame *cpy;
-  if (f) {
-    tcp_dbg("TCP> RETRANS (by dupack) frame %08x, len= %d\n", SEQN(f), f->payload_len);
-    tcp_add_header(t,f);
-    /* TCP: ENQUEUE to PROTO ( retransmit )*/
-    cpy = pico_frame_copy(f);
-    if (pico_enqueue(&tcp_out, cpy) > 0) {
-      t->in_flight++;
-      t->snd_last_out = SEQN(cpy);
-      add_retransmission_timer(t, pico_tick + t->rto);
-    } else {
-      pico_frame_discard(cpy);
+    struct pico_frame *cpy;
+    if (f) {
+        tcp_dbg("TCP> RETRANS (by dupack) frame %08x, len= %d\n", SEQN(f), f->payload_len);
+        tcp_add_header(t, f);
+        /* TCP: ENQUEUE to PROTO ( retransmit )*/
+        cpy = pico_frame_copy(f);
+        if (pico_enqueue(&tcp_out, cpy) > 0) {
+            t->in_flight++;
+            t->snd_last_out = SEQN(cpy);
+            add_retransmission_timer(t, pico_tick + t->rto);
+        } else {
+            pico_frame_discard(cpy);
+        }
+
+        return(f->payload_len);
     }
-    return(f->payload_len);
-  }
-  return 0;
+
+    return 0;
 }
 
 #ifdef TCP_ACK_DBG
 static void tcp_ack_dbg(struct pico_socket *s, struct pico_frame *f)
 {
-  uint32_t una, nxt, ack, cur;
-  struct pico_frame *una_f = NULL, *cur_f;
-  struct pico_tree_node *idx;
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  char info[64];
-  char tmp[64];
-  ack = ACKN(f);
-  nxt = t->snd_nxt;
-  tcp_dbg("===================================\n");
-  tcp_dbg("Queue out (%d/%d). ACKED=%08x\n", t->tcpq_out.size, t->tcpq_out.max_size, ack);
-
-  pico_tree_foreach(idx, &t->tcpq_out.pool) {
-    info[0] = 0;
-    cur_f = idx->keyValue;
-    cur = SEQN(cur_f);
-    if (!una_f) {
-      una_f = cur_f;
-      una = SEQN(una_f);
+    uint32_t una, nxt, ack, cur;
+    struct pico_frame *una_f = NULL, *cur_f;
+    struct pico_tree_node *idx;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    char info[64];
+    char tmp[64];
+    ack = ACKN(f);
+    nxt = t->snd_nxt;
+    tcp_dbg("===================================\n");
+    tcp_dbg("Queue out (%d/%d). ACKED=%08x\n", t->tcpq_out.size, t->tcpq_out.max_size, ack);
+
+    pico_tree_foreach(idx, &t->tcpq_out.pool) {
+        info[0] = 0;
+        cur_f = idx->keyValue;
+        cur = SEQN(cur_f);
+        if (!una_f) {
+            una_f = cur_f;
+            una = SEQN(una_f);
+        }
+
+        if (cur == nxt) {
+            strncpy(tmp, info, strlen(info));
+            snprintf(info, 64, "%s SND_NXT", tmp);
+        }
+
+        if (cur == ack) {
+            strncpy(tmp, info, strlen(info));
+            snprintf(info, 64, "%s ACK", tmp);
+        }
+
+        if (cur == una) {
+            strncpy(tmp, info, strlen(info));
+            snprintf(info, 64, "%s SND_UNA", tmp);
+        }
+
+        if (cur == t->snd_last) {
+            strncpy(tmp, info, strlen(info));
+            snprintf(info, 64, "%s SND_LAST", tmp);
+        }
+
+        tcp_dbg("%08x %d%s\n", cur, cur_f->payload_len, info);
+
     }
-
-    if (cur == nxt) {
-      strncpy(tmp, info, strlen(info));
-      snprintf(info,64, "%s SND_NXT", tmp);
-    }
-    if (cur == ack) {
-      strncpy(tmp, info, strlen(info));
-      snprintf(info,64, "%s ACK", tmp);
-    }
-    if (cur == una) {
-      strncpy(tmp, info, strlen(info));
-      snprintf(info,64, "%s SND_UNA", tmp);
-    }
-    if (cur == t->snd_last) {
-      strncpy(tmp, info, strlen(info));
-      snprintf(info,64, "%s SND_LAST", tmp);
-    }
-    tcp_dbg("%08x %d%s\n", cur, cur_f->payload_len, info);
-
-  }
-  tcp_dbg("SND_NXT is %08x, snd_LAST is %08x\n", nxt, t->snd_last);
-  tcp_dbg("===================================\n");
-  tcp_dbg("\n\n");
+    tcp_dbg("SND_NXT is %08x, snd_LAST is %08x\n", nxt, t->snd_last);
+    tcp_dbg("===================================\n");
+    tcp_dbg("\n\n");
 }
 #endif
 
 static int tcp_ack(struct pico_socket *s, struct pico_frame *f)
 {
-  struct pico_frame *f_new; /* use with Nagle to push to out queue */
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *) f->transport_hdr;
-  uint32_t rtt = 0;
-  uint16_t acked = 0;
-  pico_time acked_timestamp = 0;
-  uint8_t restart_tmr = 0;
-
-  struct pico_frame *una = NULL;
-  if ((hdr->flags & PICO_TCP_ACK) == 0)
-    return -1;
+    struct pico_frame *f_new; /* use with Nagle to push to out queue */
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *) f->transport_hdr;
+    uint32_t rtt = 0;
+    uint16_t acked = 0;
+    pico_time acked_timestamp = 0;
+    uint8_t restart_tmr = 0;
+
+    struct pico_frame *una = NULL;
+    if ((hdr->flags & PICO_TCP_ACK) == 0)
+        return -1;
 
 #ifdef TCP_ACK_DBG
-  tcp_ack_dbg(s,f);
+    tcp_ack_dbg(s, f);
 #endif
 
-  tcp_parse_options(f);
-  t->recv_wnd = short_be(hdr->rwnd);
-
-  acked = (uint16_t)tcp_ack_advance_una(t, f,&acked_timestamp);
-  una = first_segment(&t->tcpq_out);
-
-  if ((t->x_mode == PICO_TCP_BLACKOUT) || 
-    ((t->x_mode == PICO_TCP_WINDOW_FULL) && ((t->recv_wnd << t->recv_wnd_scale) > t->mss))) {
-	int prev_mode = t->x_mode;
-    tcp_dbg("Re-entering look-ahead...\n\n\n");
-    t->x_mode = PICO_TCP_LOOKAHEAD;
-    t->backoff = 0;
-
-    if((prev_mode == PICO_TCP_BLACKOUT) && (acked>0) && una)
-    {
-		t->snd_nxt = SEQN(una);
-		// restart the retrans timer
-		pico_timer_cancel(t->retrans_tmr);
-		t->timer_running = 0;
-		restart_tmr = 1u;
-    }
-  }
-
-  /* One should be acked. */
-  if ((acked == 0) && (f->payload_len  == 0) && (t->in_flight > 0))
-    t->in_flight--;
-  if (!una || acked > 0) {
-    t->x_mode = PICO_TCP_LOOKAHEAD;
-    tcp_dbg("Mode: Look-ahead. In flight: %d/%d buf: %d\n", t->in_flight, t->cwnd, t->tcpq_out.frames);
-    t->backoff = 0;
-
-    /* Do rtt/rttvar/rto calculations */
-    /* First, try with timestamps, using the value from options */
-    if(f && (f->timestamp != 0)) {
-      rtt = time_diff(pico_tick, f->timestamp);
-      if (rtt)
-        tcp_rtt(t, rtt);
-    } else if(acked_timestamp) {
-      /* If no timestamps are there, use conservatve estimation on the una */
-        rtt = time_diff(pico_tick, acked_timestamp);
-        if (rtt)
-          tcp_rtt(t, rtt);
+    tcp_parse_options(f);
+    t->recv_wnd = short_be(hdr->rwnd);
+
+    acked = (uint16_t)tcp_ack_advance_una(t, f, &acked_timestamp);
+    una = first_segment(&t->tcpq_out);
+
+    if ((t->x_mode == PICO_TCP_BLACKOUT) ||
+        ((t->x_mode == PICO_TCP_WINDOW_FULL) && ((t->recv_wnd << t->recv_wnd_scale) > t->mss))) {
+        int prev_mode = t->x_mode;
+        tcp_dbg("Re-entering look-ahead...\n\n\n");
+        t->x_mode = PICO_TCP_LOOKAHEAD;
+        t->backoff = 0;
+
+        if((prev_mode == PICO_TCP_BLACKOUT) && (acked > 0) && una)
+        {
+            t->snd_nxt = SEQN(una);
+            /* restart the retrans timer */
+            pico_timer_cancel(t->retrans_tmr);
+            t->timer_running = 0;
+            restart_tmr = 1u;
+        }
     }
 
-    tcp_dbg("TCP ACK> FRESH ACK %08x (acked %d) Queue size: %u/%u frames: %u cwnd: %u in_flight: %u snd_una: %u\n", ACKN(f), acked, t->tcpq_out.size, t->tcpq_out.max_size, t->tcpq_out.frames, t->cwnd, t->in_flight, SEQN(una));
-    if (acked > t->in_flight) {
-      tcp_dbg("WARNING: in flight < 0\n");
-      t->in_flight = 0;
-    } else
-      t->in_flight -= (acked);
-
-  } else if ((t->snd_old_ack == ACKN(f)) && /* We've just seen this ack, and... */
-      ((0 == (hdr->flags & (PICO_TCP_PSH | PICO_TCP_SYN))) &&
-          (f->payload_len == 0)) && /* This is a pure ack, and... */
-      (ACKN(f) != t->snd_nxt)) /* There is something in flight awaiting to be acked... */
-  {
-    /* Process incoming duplicate ack. */
-    if (t->x_mode < PICO_TCP_RECOVER) {
-      t->x_mode++;
-      tcp_dbg("Mode: DUPACK %d, due to PURE ACK %0x, len = %d\n", t->x_mode, SEQN(f), f->payload_len);
-      tcp_dbg("ACK: %x - QUEUE: %x\n",ACKN(f), SEQN(first_segment(&t->tcpq_out)));
-      if (t->x_mode == PICO_TCP_RECOVER) { /* Switching mode */
-        t->snd_retry = SEQN((struct pico_frame *)first_segment(&t->tcpq_out));
-        if (t->ssthresh > t->cwnd)
-          t->ssthresh >>=2;
-        else
-          t->ssthresh = (t->cwnd >> 1);
-        if (t->ssthresh < 2)
-          t->ssthresh = 2;
-      }
-    } else if (t->x_mode == PICO_TCP_RECOVER) {
-     tcp_dbg("TCP RECOVER> DUPACK! snd_una: %08x, snd_nxt: %08x, acked now: %08x\n", SEQN(first_segment(&t->tcpq_out)), t->snd_nxt, ACKN(f));
-      if (t->in_flight <= t->cwnd) {
-        struct pico_frame *nxt = peek_segment(&t->tcpq_out, t->snd_retry);
-        if (!nxt)
-          nxt = first_segment(&t->tcpq_out);
-
-        while (nxt && (nxt->flags & PICO_FRAME_FLAG_SACKED) && (nxt != first_segment(&t->tcpq_out))) {
-          tcp_dbg("Skipping %08x because it is sacked.\n", SEQN(nxt));
-          nxt = next_segment(&t->tcpq_out, nxt);
-        }
-
-        if (nxt && (seq_compare(SEQN(nxt), t->snd_nxt)) > 0)
-          nxt = NULL;
-        if (nxt && (seq_compare(SEQN(nxt), SEQN((struct pico_frame *)first_segment(&t->tcpq_out))) > (t->recv_wnd << t->recv_wnd_scale)))
-          nxt = NULL;
-
-        if(!nxt)
-          nxt = first_segment(&t->tcpq_out);
-        if (nxt) {
-          tcp_retrans(t, peek_segment(&t->tcpq_out, t->snd_retry));
-          t->snd_retry = SEQN(nxt);
+    /* One should be acked. */
+    if ((acked == 0) && (f->payload_len  == 0) && (t->in_flight > 0))
+        t->in_flight--;
+
+    if (!una || acked > 0) {
+        t->x_mode = PICO_TCP_LOOKAHEAD;
+        tcp_dbg("Mode: Look-ahead. In flight: %d/%d buf: %d\n", t->in_flight, t->cwnd, t->tcpq_out.frames);
+        t->backoff = 0;
+
+        /* Do rtt/rttvar/rto calculations */
+        /* First, try with timestamps, using the value from options */
+        if(f && (f->timestamp != 0)) {
+            rtt = time_diff(pico_tick, f->timestamp);
+            if (rtt)
+                tcp_rtt(t, rtt);
+        } else if(acked_timestamp) {
+            /* If no timestamps are there, use conservatve estimation on the una */
+            rtt = time_diff(pico_tick, acked_timestamp);
+            if (rtt)
+                tcp_rtt(t, rtt);
         }
-      }
-
-      if (++t->cwnd_counter > 1) {
-        t->cwnd--;
-        if (t->cwnd < 2)
-          t->cwnd = 2;
-        t->cwnd_counter = 0;
-      }
-    } else {
-      tcp_dbg("DUPACK in mode %d \n", t->x_mode);
-
+
+        tcp_dbg("TCP ACK> FRESH ACK %08x (acked %d) Queue size: %u/%u frames: %u cwnd: %u in_flight: %u snd_una: %u\n", ACKN(f), acked, t->tcpq_out.size, t->tcpq_out.max_size, t->tcpq_out.frames, t->cwnd, t->in_flight, SEQN(una));
+        if (acked > t->in_flight) {
+            tcp_dbg("WARNING: in flight < 0\n");
+            t->in_flight = 0;
+        } else
+            t->in_flight -= (acked);
+
+    } else if ((t->snd_old_ack == ACKN(f)) && /* We've just seen this ack, and... */
+               ((0 == (hdr->flags & (PICO_TCP_PSH | PICO_TCP_SYN))) &&
+                (f->payload_len == 0)) && /* This is a pure ack, and... */
+               (ACKN(f) != t->snd_nxt)) /* There is something in flight awaiting to be acked... */
+    {
+        /* Process incoming duplicate ack. */
+        if (t->x_mode < PICO_TCP_RECOVER) {
+            t->x_mode++;
+            tcp_dbg("Mode: DUPACK %d, due to PURE ACK %0x, len = %d\n", t->x_mode, SEQN(f), f->payload_len);
+            tcp_dbg("ACK: %x - QUEUE: %x\n", ACKN(f), SEQN(first_segment(&t->tcpq_out)));
+            if (t->x_mode == PICO_TCP_RECOVER) { /* Switching mode */
+                t->snd_retry = SEQN((struct pico_frame *)first_segment(&t->tcpq_out));
+                if (t->ssthresh > t->cwnd)
+                    t->ssthresh >>= 2;
+                else
+                    t->ssthresh = (t->cwnd >> 1);
+
+                if (t->ssthresh < 2)
+                    t->ssthresh = 2;
+            }
+        } else if (t->x_mode == PICO_TCP_RECOVER) {
+            tcp_dbg("TCP RECOVER> DUPACK! snd_una: %08x, snd_nxt: %08x, acked now: %08x\n", SEQN(first_segment(&t->tcpq_out)), t->snd_nxt, ACKN(f));
+            if (t->in_flight <= t->cwnd) {
+                struct pico_frame *nxt = peek_segment(&t->tcpq_out, t->snd_retry);
+                if (!nxt)
+                    nxt = first_segment(&t->tcpq_out);
+
+                while (nxt && (nxt->flags & PICO_FRAME_FLAG_SACKED) && (nxt != first_segment(&t->tcpq_out))) {
+                    tcp_dbg("Skipping %08x because it is sacked.\n", SEQN(nxt));
+                    nxt = next_segment(&t->tcpq_out, nxt);
+                }
+                if (nxt && (seq_compare(SEQN(nxt), t->snd_nxt)) > 0)
+                    nxt = NULL;
+
+                if (nxt && (seq_compare(SEQN(nxt), SEQN((struct pico_frame *)first_segment(&t->tcpq_out))) > (t->recv_wnd << t->recv_wnd_scale)))
+                    nxt = NULL;
+
+                if(!nxt)
+                    nxt = first_segment(&t->tcpq_out);
+
+                if (nxt) {
+                    tcp_retrans(t, peek_segment(&t->tcpq_out, t->snd_retry));
+                    t->snd_retry = SEQN(nxt);
+                }
+            }
+
+            if (++t->cwnd_counter > 1) {
+                t->cwnd--;
+                if (t->cwnd < 2)
+                    t->cwnd = 2;
+
+                t->cwnd_counter = 0;
+            }
+        } else {
+            tcp_dbg("DUPACK in mode %d \n", t->x_mode);
+
+        }
+    } /* End case duplicate ack detection */
+
+    /* Do congestion control */
+    tcp_congestion_control(t);
+    if ((acked > 0) && t->sock.wakeup) {
+        if (t->tcpq_out.size < t->tcpq_out.max_size)
+            t->sock.wakeup(PICO_SOCK_EV_WR, &(t->sock));
+
+        /* t->sock.ev_pending |= PICO_SOCK_EV_WR; */
     }
-  } /* End case duplicate ack detection */
-
-  /* Do congestion control */
-  tcp_congestion_control(t);
-  if ((acked > 0) && t->sock.wakeup) {
-    if (t->tcpq_out.size < t->tcpq_out.max_size)
-      t->sock.wakeup(PICO_SOCK_EV_WR, &(t->sock));
-      //t->sock.ev_pending |= PICO_SOCK_EV_WR;
-  }
-
-  /* if Nagle enabled, check if no unack'ed data and fill out queue (till window) */
-  if (IS_NAGLE_ENABLED((&(t->sock)))) {
-    while (!IS_TCP_HOLDQ_EMPTY(t) && ((t->tcpq_out.max_size - t->tcpq_out.size) >= PICO_TCP_DEFAULT_MSS)) {
-      tcp_dbg_nagle("TCP_ACK - NAGLE add new segment\n");
-      f_new = pico_hold_segment_make(t);
-      if (f_new == NULL)
-        break;            /* XXX corrupt !!! (or no memory) */
-      if (pico_enqueue_segment(&t->tcpq_out,f_new) <= 0)
-        // handle error
-        tcp_dbg_nagle("TCP_ACK - NAGLE FAILED to enqueue in out\n");
+
+    /* if Nagle enabled, check if no unack'ed data and fill out queue (till window) */
+    if (IS_NAGLE_ENABLED((&(t->sock)))) {
+        while (!IS_TCP_HOLDQ_EMPTY(t) && ((t->tcpq_out.max_size - t->tcpq_out.size) >= PICO_TCP_DEFAULT_MSS)) {
+            tcp_dbg_nagle("TCP_ACK - NAGLE add new segment\n");
+            f_new = pico_hold_segment_make(t);
+            if (f_new == NULL)
+                break;    /* XXX corrupt !!! (or no memory) */
+
+            if (pico_enqueue_segment(&t->tcpq_out, f_new) <= 0)
+                /* handle error */
+                tcp_dbg_nagle("TCP_ACK - NAGLE FAILED to enqueue in out\n");
+        }
     }
-  }
-
-  /* If some space was created, put a few segments out. */
-  tcp_dbg("TCP_CWND, %lu, %u, %u, %u\n", pico_tick, t->cwnd, t->ssthresh, t->in_flight);
-  if (t->x_mode ==  PICO_TCP_LOOKAHEAD) {
-    if ((t->cwnd >= t->in_flight) && (t->snd_nxt > t->snd_last_out)) {
-      pico_tcp_output(&t->sock, (int)t->cwnd - (int)t->in_flight);
+
+    /* If some space was created, put a few segments out. */
+    tcp_dbg("TCP_CWND, %lu, %u, %u, %u\n", pico_tick, t->cwnd, t->ssthresh, t->in_flight);
+    if (t->x_mode ==  PICO_TCP_LOOKAHEAD) {
+        if ((t->cwnd >= t->in_flight) && (t->snd_nxt > t->snd_last_out)) {
+            pico_tcp_output(&t->sock, (int)t->cwnd - (int)t->in_flight);
+        }
     }
-  }
-
-  if(restart_tmr)
-  {
-  	add_retransmission_timer(t,pico_tick+t->rto);
-  }
-
-  t->snd_old_ack = ACKN(f);
-  return 0;
+
+    if(restart_tmr)
+    {
+        add_retransmission_timer(t, pico_tick + t->rto);
+    }
+
+    t->snd_old_ack = ACKN(f);
+    return 0;
 }
 
 static int tcp_finwaitack(struct pico_socket *s, struct pico_frame *f)
 {
-  tcp_dbg("RECEIVED ACK IN FIN_WAIT1\n");
-  tcp_dbg("TCP> IN STATE FIN_WAIT2\n");
-
-  /* acking part */
-  tcp_ack(s,f);
-  /* update TCP state */
-  s->state &= 0x00FFU;
-  s->state |= PICO_SOCKET_STATE_TCP_FIN_WAIT2;
-
-  return 0;
+    tcp_dbg("RECEIVED ACK IN FIN_WAIT1\n");
+    tcp_dbg("TCP> IN STATE FIN_WAIT2\n");
+
+    /* acking part */
+    tcp_ack(s, f);
+    /* update TCP state */
+    s->state &= 0x00FFU;
+    s->state |= PICO_SOCKET_STATE_TCP_FIN_WAIT2;
+
+    return 0;
 }
 
 static void tcp_deltcb(pico_time when, void *arg)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)arg;
-  IGNORE_PARAMETER(when);
-
-  if (TCPSTATE(&t->sock) == PICO_SOCKET_STATE_TCP_TIME_WAIT) {
-    tcp_dbg("TCP> state: time_wait, final timer expired, going to closed state\n");
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)arg;
+    IGNORE_PARAMETER(when);
+
+    if (TCPSTATE(&t->sock) == PICO_SOCKET_STATE_TCP_TIME_WAIT) {
+        tcp_dbg("TCP> state: time_wait, final timer expired, going to closed state\n");
+        /* update state */
+        (t->sock).state &= 0x00FFU;
+        (t->sock).state |= PICO_SOCKET_STATE_TCP_CLOSED;
+        (t->sock).state &= 0xFF00U;
+        (t->sock).state |= PICO_SOCKET_STATE_CLOSED;
+        /* call EV_FIN wakeup before deleting */
+        if (t->sock.wakeup) {
+            (t->sock).wakeup(PICO_SOCK_EV_FIN, &(t->sock));
+        }
+
+        /* delete socket */
+        pico_socket_del(&t->sock);
+    } else {
+        tcp_dbg("TCP> trying to go to closed, wrong state\n");
+    }
+}
+
+static int tcp_finwaitfin(struct pico_socket *s, struct pico_frame *f)
+{
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    struct pico_tcp_hdr *hdr  = (struct pico_tcp_hdr *) (f->transport_hdr);
+    tcp_dbg("TCP> received fin in FIN_WAIT2\n");
+    /* received FIN, increase ACK nr */
+    t->rcv_nxt = long_be(hdr->seq) + 1;
+    s->state &= 0x00FFU;
+    s->state |= PICO_SOCKET_STATE_TCP_TIME_WAIT;
+    /* set SHUT_REMOTE */
+    s->state |= PICO_SOCKET_STATE_SHUT_REMOTE;
+    if (s->wakeup)
+        s->wakeup(PICO_SOCK_EV_CLOSE, s);
+
+    if (f->payload_len > 0) /* needed?? */
+        tcp_data_in(s, f);
+
+    /* send ACK */
+    tcp_send_ack(t);
+    /* set timer */
+    pico_timer_add(200, tcp_deltcb, t);
+    return 0;
+}
+
+static int tcp_closewaitack(struct pico_socket *s, struct pico_frame *f)
+{
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    tcp_dbg("TCP> received ack in CLOSING\n");
+    /* acking part */
+    tcp_ack(s, f);
+    /* update TCP state */
+    s->state &= 0x00FFU;
+    s->state |= PICO_SOCKET_STATE_TCP_TIME_WAIT;
+    /* set timer */
+    pico_timer_add(200, tcp_deltcb, t);
+    return 0;
+}
+
+static int tcp_lastackwait(struct pico_socket *s, struct pico_frame *f)
+{
+    IGNORE_PARAMETER(f);
+    tcp_dbg("TCP> state: last_ack, received ack, to closed\n");
+    s->state &= 0x00FFU;
+    s->state |= PICO_SOCKET_STATE_TCP_CLOSED;
+    s->state &= 0xFF00U;
+    s->state |= PICO_SOCKET_STATE_CLOSED;
+    /* call socket wakeup with EV_FIN */
+    if (s->wakeup)
+        s->wakeup(PICO_SOCK_EV_FIN, s);
+
+    /* delete socket */
+    pico_socket_del(s);
+    return 0;
+}
+
+static int tcp_syn(struct pico_socket *s, struct pico_frame *f)
+{
+    struct pico_socket_tcp *new = NULL;
+    struct pico_tcp_hdr *hdr = NULL;
+    if(s->number_of_pending_conn >= s->max_backlog)
+        return -1;
+
+    new = (struct pico_socket_tcp *)pico_socket_clone(s);
+    hdr = (struct pico_tcp_hdr *)f->transport_hdr;
+    if (!new)
+        return -1;
+
+#ifdef PICO_TCP_SUPPORT_SOCKET_STATS
+    pico_timer_add(2000, sock_stats, s);
+#endif
+
+    new->sock.remote_port = ((struct pico_trans *)f->transport_hdr)->sport;
+#ifdef PICO_SUPPORT_IPV4
+    if (IS_IPV4(f)) {
+        new->sock.remote_addr.ip4.addr = ((struct pico_ipv4_hdr *)(f->net_hdr))->src.addr;
+        new->sock.local_addr.ip4.addr = ((struct pico_ipv4_hdr *)(f->net_hdr))->dst.addr;
+    }
+
+#endif
+#ifdef PICO_SUPPORT_IPV6
+    if (IS_IPV6(f)) {
+        new->sock.remote_addr.ip6 = ((struct pico_ipv6_hdr *)(f->net_hdr))->src;
+        new->sock.local_addr.ip6 = ((struct pico_ipv6_hdr *)(f->net_hdr))->dst;
+    }
+
+#endif
+
+    /* Set socket limits */
+    new->tcpq_in.max_size = PICO_DEFAULT_SOCKETQ;
+    new->tcpq_out.max_size = PICO_DEFAULT_SOCKETQ;
+    new->tcpq_hold.max_size = 2 * PICO_TCP_DEFAULT_MSS;
+    new->tcpq_in.overhead = (sizeof(struct tcp_input_segment) + sizeof(struct pico_tree_node));
+    new->tcpq_out.overhead = new->tcpq_hold.overhead = sizeof(struct pico_frame) + sizeof(struct pico_tree_node);
+
+    f->sock = &new->sock;
+    tcp_parse_options(f);
+    new->mss = PICO_TCP_DEFAULT_MSS;
+    new->rcv_nxt = long_be(hdr->seq) + 1;
+    new->snd_nxt = long_be(pico_paws());
+    new->snd_last = new->snd_nxt;
+    new->cwnd = PICO_TCP_IW;
+    new->ssthresh = 40;
+    new->recv_wnd = short_be(hdr->rwnd);
+    new->jumbo = hdr->len & 0x07;
+    s->number_of_pending_conn++;
+    new->sock.parent = s;
+    new->sock.wakeup = s->wakeup;
+    /* Initialize timestamp values */
+    new->sock.state = PICO_SOCKET_STATE_BOUND | PICO_SOCKET_STATE_CONNECTED | PICO_SOCKET_STATE_TCP_SYN_RECV;
+    pico_socket_add(&new->sock);
+    tcp_send_synack(&new->sock);
+    tcp_dbg("SYNACK sent, socket added. snd_nxt is %08x\n", new->snd_nxt);
+    return 0;
+}
+
+static void tcp_set_init_point(struct pico_socket *s)
+{
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    t->rcv_processed = t->rcv_nxt;
+}
+
+static int tcp_synack(struct pico_socket *s, struct pico_frame *f)
+{
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
+    struct pico_tcp_hdr *hdr  = (struct pico_tcp_hdr *)f->transport_hdr;
+
+    if (ACKN(f) ==  (1 + t->snd_nxt)) {
+        t->rcv_nxt = long_be(hdr->seq);
+        t->rcv_processed = t->rcv_nxt + 1;
+        tcp_ack(s, f);
+
+        s->state &= 0x00FFU;
+        s->state |= PICO_SOCKET_STATE_TCP_ESTABLISHED;
+        tcp_dbg("TCP> Established. State: %x\n", s->state);
+
+        if (s->wakeup)
+            s->wakeup(PICO_SOCK_EV_CONN, s);
+
+        s->ev_pending |= PICO_SOCK_EV_WR;
+
+        t->rcv_nxt++;
+        t->snd_nxt++;
+        tcp_send_ack(t); /* return ACK */
+
+        return 0;
+
+    } else {
+        tcp_dbg("TCP> Not established, RST sent.\n");
+        tcp_nosync_rst(s, f);
+        return 0;
+    }
+}
+
+static int tcp_first_ack(struct pico_socket *s, struct pico_frame *f)
+{
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    tcp_dbg("ACK in SYN_RECV: expecting %08x got %08x\n", t->snd_nxt, ACKN(f));
+    if (t->snd_nxt == ACKN(f)) {
+        tcp_set_init_point(s);
+        tcp_ack(s, f);
+        s->state &= 0x00FFU;
+        s->state |= PICO_SOCKET_STATE_TCP_ESTABLISHED;
+        tcp_dbg("TCP: Established. State now: %04x\n", s->state);
+        if( !s->parent && s->wakeup) { /* If the socket has no parent, -> sending socket that has a sim_open */
+            tcp_dbg("FIRST ACK - No parent found -> sending socket\n");
+            s->wakeup(PICO_SOCK_EV_CONN,  s);
+        }
+
+        if (s->parent && s->parent->wakeup) {
+            tcp_dbg("FIRST ACK - Parent found -> listening socket\n");
+            s->wakeup = s->parent->wakeup;
+            s->parent->wakeup(PICO_SOCK_EV_CONN, s->parent);
+        }
+
+        s->ev_pending |= PICO_SOCK_EV_WR;
+        tcp_dbg("%s: snd_nxt is now %08x\n", __FUNCTION__, t->snd_nxt);
+        return 0;
+    } else {
+        tcp_nosync_rst(s, f);
+        return 0;
+    }
+}
+
+static int tcp_closewait(struct pico_socket *s, struct pico_frame *f)
+{
+
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    struct pico_tcp_hdr *hdr  = (struct pico_tcp_hdr *) (f->transport_hdr);
+
+
+    if (f->payload_len > 0)
+        tcp_data_in(s, f);
+
+    if (f->flags & PICO_TCP_ACK)
+        tcp_ack(s, f);
+
+    if (seq_compare(SEQN(f), t->rcv_nxt) == 0) {
+        /* received FIN, increase ACK nr */
+        t->rcv_nxt = long_be(hdr->seq) + 1;
+        s->state &= 0x00FFU;
+        s->state |= PICO_SOCKET_STATE_TCP_CLOSE_WAIT;
+        /* set SHUT_REMOTE */
+        s->state |= PICO_SOCKET_STATE_SHUT_REMOTE;
+        tcp_dbg("TCP> Close-wait\n");
+
+        if (s->wakeup) {
+            if(f->payload_len > 0) {
+                struct pico_socket_tcp *_t = (struct pico_socket_tcp *)s;
+                _t->sock.ev_pending |= PICO_SOCK_EV_CLOSE;
+            }else
+                s->wakeup(PICO_SOCK_EV_CLOSE, s);
+        }
+    } else {
+        tcp_send_ack(t); /* return ACK */
+    }
+
+    return 0;
+}
+
+/*static int tcp_fin(struct pico_socket *s, struct pico_frame *f)
+   {
+   return 0;
+   }*/
+
+static int tcp_rcvfin(struct pico_socket *s, struct pico_frame *f)
+{
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    IGNORE_PARAMETER(f);
+    tcp_dbg("TCP> Received FIN in FIN_WAIT1\n");
+    s->state &= 0x00FFU;
+    s->state |= PICO_SOCKET_STATE_TCP_CLOSING;
+    t->rcv_processed = t->rcv_nxt + 1;
+    t->rcv_nxt++;
+    /* send ACK */
+    tcp_send_ack(t);
+    return 0;
+}
+
+static int tcp_finack(struct pico_socket *s, struct pico_frame *f)
+{
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    IGNORE_PARAMETER(f);
+
+    tcp_dbg("TCP> ENTERED finack\n");
+    t->rcv_nxt++;
+    /* send ACK */
+    tcp_send_ack(t);
+
+    /* call socket wakeup with EV_FIN */
+    if (s->wakeup)
+        s->wakeup(PICO_SOCK_EV_FIN, s);
+
+    s->state &= 0x00FFU;
+    s->state |= PICO_SOCKET_STATE_TCP_TIME_WAIT;
+    /* set SHUT_REMOTE */
+    s->state |= PICO_SOCKET_STATE_SHUT_REMOTE;
+    pico_timer_add(2000, tcp_deltcb, t);
+
+    return 0;
+}
+
+static void tcp_force_closed(struct pico_socket *s)
+{
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
     /* update state */
     (t->sock).state &= 0x00FFU;
     (t->sock).state |= PICO_SOCKET_STATE_TCP_CLOSED;
     (t->sock).state &= 0xFF00U;
     (t->sock).state |= PICO_SOCKET_STATE_CLOSED;
-    /* call EV_FIN wakeup before deleting */
-    if (t->sock.wakeup) {
-      (t->sock).wakeup(PICO_SOCK_EV_FIN, &(t->sock));
-    }
-    /* delete socket */
-    pico_socket_del(&t->sock);
-  } else {
-    tcp_dbg("TCP> trying to go to closed, wrong state\n");
-  }
-}
-
-static int tcp_finwaitfin(struct pico_socket *s, struct pico_frame *f)
-{
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  struct pico_tcp_hdr *hdr  = (struct pico_tcp_hdr *) (f->transport_hdr);
-  tcp_dbg("TCP> received fin in FIN_WAIT2\n");
-  /* received FIN, increase ACK nr */
-  t->rcv_nxt = long_be(hdr->seq) + 1;
-  s->state &= 0x00FFU;
-  s->state |= PICO_SOCKET_STATE_TCP_TIME_WAIT;
-  /* set SHUT_REMOTE */
-  s->state |= PICO_SOCKET_STATE_SHUT_REMOTE;
-  if (s->wakeup)
-    s->wakeup(PICO_SOCK_EV_CLOSE, s);
-  if (f->payload_len > 0) /* needed?? */
-    tcp_data_in(s,f);
-  /* send ACK */
-  tcp_send_ack(t);
-  /* set timer */
-  pico_timer_add(200, tcp_deltcb, t);
-  return 0;
-}
-
-static int tcp_closewaitack(struct pico_socket *s, struct pico_frame *f)
-{
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  tcp_dbg("TCP> received ack in CLOSING\n");
-  /* acking part */
-  tcp_ack(s,f);
-  /* update TCP state */
-  s->state &= 0x00FFU;
-  s->state |= PICO_SOCKET_STATE_TCP_TIME_WAIT;
-  /* set timer */
-  pico_timer_add(200, tcp_deltcb, t);
-  return 0;
-}
-
-static int tcp_lastackwait(struct pico_socket *s, struct pico_frame *f)
-{
-	IGNORE_PARAMETER(f);
-  tcp_dbg("TCP> state: last_ack, received ack, to closed\n");
-  s->state &= 0x00FFU;
-  s->state |= PICO_SOCKET_STATE_TCP_CLOSED;
-  s->state &= 0xFF00U;
-  s->state |= PICO_SOCKET_STATE_CLOSED;
-  /* call socket wakeup with EV_FIN */
-  if (s->wakeup)
-    s->wakeup(PICO_SOCK_EV_FIN, s);
-  /* delete socket */
-  pico_socket_del(s);
-  return 0;
-}
-
-static int tcp_syn(struct pico_socket *s, struct pico_frame *f)
-{
-  struct pico_socket_tcp *new=NULL;
-  struct pico_tcp_hdr *hdr=NULL;
-  if(s->number_of_pending_conn >= s->max_backlog)
-	return -1;
-  new = (struct pico_socket_tcp *)pico_socket_clone(s);
-  hdr = (struct pico_tcp_hdr *)f->transport_hdr;
-  if (!new)
-    return -1;
-
-#ifdef PICO_TCP_SUPPORT_SOCKET_STATS
-  pico_timer_add(2000, sock_stats, s);
-#endif
-
-  new->sock.remote_port = ((struct pico_trans *)f->transport_hdr)->sport;
-#ifdef PICO_SUPPORT_IPV4
-  if (IS_IPV4(f)) {
-    new->sock.remote_addr.ip4.addr = ((struct pico_ipv4_hdr *)(f->net_hdr))->src.addr;
-    new->sock.local_addr.ip4.addr = ((struct pico_ipv4_hdr *)(f->net_hdr))->dst.addr;
-  }
-#endif
-#ifdef PICO_SUPPORT_IPV6
-  if (IS_IPV6(f)) {
-    new->sock.remote_addr.ip6 = ((struct pico_ipv6_hdr *)(f->net_hdr))->src;
-    new->sock.local_addr.ip6 = ((struct pico_ipv6_hdr *)(f->net_hdr))->dst;
-  }
-#endif
-
-  /* Set socket limits */
-  new->tcpq_in.max_size = PICO_DEFAULT_SOCKETQ;
-  new->tcpq_out.max_size = PICO_DEFAULT_SOCKETQ;
-  new->tcpq_hold.max_size = 2*PICO_TCP_DEFAULT_MSS;
-  new->tcpq_in.overhead=(sizeof(struct tcp_input_segment)+sizeof(struct pico_tree_node));
-  new->tcpq_out.overhead=new->tcpq_hold.overhead=sizeof(struct pico_frame)+sizeof(struct pico_tree_node);
-
-  f->sock = &new->sock;
-  tcp_parse_options(f);
-  new->mss = PICO_TCP_DEFAULT_MSS;
-  new->rcv_nxt = long_be(hdr->seq) + 1;
-  new->snd_nxt = long_be(pico_paws());
-  new->snd_last = new->snd_nxt;
-  new->cwnd = PICO_TCP_IW;
-  new->ssthresh = 40;
-  new->recv_wnd = short_be(hdr->rwnd);
-  new->jumbo = hdr->len & 0x07;
-  s->number_of_pending_conn++;
-  new->sock.parent = s;
-  new->sock.wakeup = s->wakeup;
-  /* Initialize timestamp values */
-  new->sock.state = PICO_SOCKET_STATE_BOUND | PICO_SOCKET_STATE_CONNECTED | PICO_SOCKET_STATE_TCP_SYN_RECV;
-  pico_socket_add(&new->sock);
-  tcp_send_synack(&new->sock);
-  tcp_dbg("SYNACK sent, socket added. snd_nxt is %08x\n", new->snd_nxt);
-  return 0;
-}
-
-static void tcp_set_init_point(struct pico_socket *s)
-{
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  t->rcv_processed = t->rcv_nxt;
-}
-
-static int tcp_synack(struct pico_socket *s, struct pico_frame *f)
-{
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
-  struct pico_tcp_hdr *hdr  = (struct pico_tcp_hdr *)f->transport_hdr;
-
-  if (ACKN(f) ==  (1 + t->snd_nxt)) {
-    t->rcv_nxt = long_be(hdr->seq);
-    t->rcv_processed = t->rcv_nxt + 1;
-    tcp_ack(s, f);
-
-    s->state &= 0x00FFU;
-    s->state |= PICO_SOCKET_STATE_TCP_ESTABLISHED;
-    tcp_dbg("TCP> Established. State: %x\n", s->state);
-
-    if (s->wakeup)
-      s->wakeup(PICO_SOCK_EV_CONN, s);
-    s->ev_pending |= PICO_SOCK_EV_WR;
-
-    t->rcv_nxt++;
-    t->snd_nxt++;
-    tcp_send_ack(t);  /* return ACK */
-
-    return 0;
-
-  } else {
-    tcp_dbg("TCP> Not established, RST sent.\n");
-    tcp_nosync_rst(s,f);
-    return 0;
-  }
-}
-
-static int tcp_first_ack(struct pico_socket *s, struct pico_frame *f)
-{
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  tcp_dbg("ACK in SYN_RECV: expecting %08x got %08x\n", t->snd_nxt, ACKN(f));
-  if (t->snd_nxt == ACKN(f)) {
-    tcp_set_init_point(s);
-    tcp_ack(s, f);
-    s->state &= 0x00FFU;
-    s->state |= PICO_SOCKET_STATE_TCP_ESTABLISHED;
-    tcp_dbg("TCP: Established. State now: %04x\n", s->state);
-    if( !s->parent && s->wakeup) { /* If the socket has no parent, -> sending socket that has a sim_open */
-        tcp_dbg("FIRST ACK - No parent found -> sending socket\n");
-        s->wakeup(PICO_SOCK_EV_CONN,  s);
-    }
-    if (s->parent && s->parent->wakeup) {
-      tcp_dbg("FIRST ACK - Parent found -> listening socket\n");
-      s->wakeup = s->parent->wakeup;
-      s->parent->wakeup(PICO_SOCK_EV_CONN, s->parent);
-    }
-    s->ev_pending |= PICO_SOCK_EV_WR;
-    tcp_dbg("%s: snd_nxt is now %08x\n", __FUNCTION__, t->snd_nxt);
-    return 0;
-  } else {
-    tcp_nosync_rst(s,f);
-    return 0;
-  }
-}
-
-static int tcp_closewait(struct pico_socket *s, struct pico_frame *f)
-{
-
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  struct pico_tcp_hdr *hdr  = (struct pico_tcp_hdr *) (f->transport_hdr);
-
-
-  if (f->payload_len > 0)
-    tcp_data_in(s,f);
-  if (f->flags & PICO_TCP_ACK)
-    tcp_ack(s,f);
-  if (seq_compare(SEQN(f), t->rcv_nxt) == 0) {
-    /* received FIN, increase ACK nr */
-  	t->rcv_nxt = long_be(hdr->seq) + 1;
-		s->state &= 0x00FFU;
-		s->state |= PICO_SOCKET_STATE_TCP_CLOSE_WAIT;
-		/* set SHUT_REMOTE */
-    s->state |= PICO_SOCKET_STATE_SHUT_REMOTE;
-      tcp_dbg("TCP> Close-wait\n");
-
-    if (s->wakeup){
-      if(f->payload_len>0){
-        struct pico_socket_tcp *_t = (struct pico_socket_tcp *)s;
-        _t->sock.ev_pending |=PICO_SOCK_EV_CLOSE;
-      }else
-        s->wakeup(PICO_SOCK_EV_CLOSE, s);
-    }
-  } else {
-    tcp_send_ack(t);  /* return ACK */
-  }
-  return 0;
-}
-
-/*static int tcp_fin(struct pico_socket *s, struct pico_frame *f)
-{
-  return 0;
-}*/
-
-static int tcp_rcvfin(struct pico_socket *s, struct pico_frame *f)
-{
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  IGNORE_PARAMETER(f);
-  tcp_dbg("TCP> Received FIN in FIN_WAIT1\n");
-  s->state &= 0x00FFU;
-  s->state |= PICO_SOCKET_STATE_TCP_CLOSING;
-  t->rcv_processed = t->rcv_nxt + 1;
-  t->rcv_nxt++;
-  /* send ACK */
-  tcp_send_ack(t);
-  return 0;
-}
-
-static int tcp_finack(struct pico_socket *s, struct pico_frame *f)
-{
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  IGNORE_PARAMETER(f);
-
-  tcp_dbg("TCP> ENTERED finack\n");
-  t->rcv_nxt++;
-  /* send ACK */
-  tcp_send_ack(t);
-
-  /* call socket wakeup with EV_FIN */
-  if (s->wakeup)
-    s->wakeup(PICO_SOCK_EV_FIN, s);
-  s->state &= 0x00FFU;
-  s->state |= PICO_SOCKET_STATE_TCP_TIME_WAIT;
-  /* set SHUT_REMOTE */
-  s->state |= PICO_SOCKET_STATE_SHUT_REMOTE;
-  pico_timer_add(2000, tcp_deltcb, t);
-
-  return 0;
-}
-
-static void tcp_force_closed(struct pico_socket *s)
-{
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
-  /* update state */
-  (t->sock).state &= 0x00FFU;
-  (t->sock).state |= PICO_SOCKET_STATE_TCP_CLOSED;
-  (t->sock).state &= 0xFF00U;
-  (t->sock).state |= PICO_SOCKET_STATE_CLOSED;
 }
 
 static void tcp_wakeup_pending(struct pico_socket *s, uint16_t ev)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
-  if ((t->sock).wakeup)
-    (t->sock).wakeup(ev, &(t->sock));
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
+    if ((t->sock).wakeup)
+        (t->sock).wakeup(ev, &(t->sock));
 }
 
 static int tcp_rst(struct pico_socket *s, struct pico_frame *f)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
-  struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *) (f->transport_hdr);
-
-  tcp_dbg("TCP >>>>>>>>>>>>>> received RST <<<<<<<<<<<<<<<<<<<<\n");
-  if ((s->state & PICO_SOCKET_STATE_TCP) == PICO_SOCKET_STATE_TCP_SYN_SENT) {
-    /* the RST is acceptable if the ACK field acknowledges the SYN */
-    if ((t->snd_nxt + 1) == ACKN(f)) {  /* valid, got to closed state */
-      tcp_force_closed(s);
-      pico_err = PICO_ERR_ECONNRESET;
-      tcp_wakeup_pending(s, PICO_SOCK_EV_ERR);
-      pico_socket_del(&t->sock);  /* delete socket */
-    } else {                      /* not valid, ignore */
-      tcp_dbg("TCP RST> IGNORE\n");
-      return 0;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
+    struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *) (f->transport_hdr);
+
+    tcp_dbg("TCP >>>>>>>>>>>>>> received RST <<<<<<<<<<<<<<<<<<<<\n");
+    if ((s->state & PICO_SOCKET_STATE_TCP) == PICO_SOCKET_STATE_TCP_SYN_SENT) {
+        /* the RST is acceptable if the ACK field acknowledges the SYN */
+        if ((t->snd_nxt + 1) == ACKN(f)) { /* valid, got to closed state */
+            tcp_force_closed(s);
+            pico_err = PICO_ERR_ECONNRESET;
+            tcp_wakeup_pending(s, PICO_SOCK_EV_ERR);
+            pico_socket_del(&t->sock); /* delete socket */
+        } else {                  /* not valid, ignore */
+            tcp_dbg("TCP RST> IGNORE\n");
+            return 0;
+        }
+    } else { /* all other states */
+        /* all reset (RST) segments are validated by checking their SEQ-fields,
+           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);
+                pico_socket_del(&t->sock); /* delete socket */
+                tcp_dbg("TCP RST> SOCKET BACK TO LISTEN\n");
+                /*   pico_socket_del(s); */
+            } else {
+                tcp_force_closed(s);
+                tcp_wakeup_pending(s, PICO_SOCK_EV_FIN);
+                pico_err = PICO_ERR_ECONNRESET;
+                tcp_wakeup_pending(s, PICO_SOCK_EV_ERR);
+                pico_socket_del(&t->sock); /* delete socket */
+            }
+        } else {                  /* not valid, ignore */
+            tcp_dbg("TCP RST> IGNORE\n");
+            return 0;
+        }
     }
-  } else {  /* all other states */
-    /* all reset (RST) segments are validated by checking their SEQ-fields,
-    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);
-        pico_socket_del(&t->sock);  /* delete socket */
-        tcp_dbg("TCP RST> SOCKET BACK TO LISTEN\n");
-   //   pico_socket_del(s);
-      } else {
-        tcp_force_closed(s);
-        tcp_wakeup_pending(s, PICO_SOCK_EV_FIN);
-        pico_err = PICO_ERR_ECONNRESET;
-        tcp_wakeup_pending(s, PICO_SOCK_EV_ERR);
-        pico_socket_del(&t->sock);  /* delete socket */
-      }
-    } else {                      /* not valid, ignore */
-      tcp_dbg("TCP RST> IGNORE\n");
-      return 0;
-    }
-  }
-
-  return 0;
+
+    return 0;
 }
 static int tcp_halfopencon(struct pico_socket *s, struct pico_frame *fr)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
-  IGNORE_PARAMETER(fr);
-  tcp_send_ack(t);
-  return 0;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
+    IGNORE_PARAMETER(fr);
+    tcp_send_ack(t);
+    return 0;
 }
 
 static int tcp_closeconn(struct pico_socket *s, struct pico_frame *fr)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
-  struct pico_tcp_hdr *hdr  = (struct pico_tcp_hdr *) (fr->transport_hdr);
-
-  if (seq_compare(SEQN(fr), t->rcv_nxt) == 0) {
-      /* received FIN, increase ACK nr */
-  	  t->rcv_nxt = long_be(hdr->seq) + 1;
-	  s->state &= 0x00FFU;
-	  s->state |= PICO_SOCKET_STATE_TCP_CLOSE_WAIT;
-	  /* set SHUT_LOCAL */
-      s->state |= PICO_SOCKET_STATE_SHUT_LOCAL;
-      pico_socket_close(s);
-      s->parent->number_of_pending_conn--;
-      return 1;
-  }
-  return 0;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) s;
+    struct pico_tcp_hdr *hdr  = (struct pico_tcp_hdr *) (fr->transport_hdr);
+
+    if (seq_compare(SEQN(fr), t->rcv_nxt) == 0) {
+        /* received FIN, increase ACK nr */
+        t->rcv_nxt = long_be(hdr->seq) + 1;
+        s->state &= 0x00FFU;
+        s->state |= PICO_SOCKET_STATE_TCP_CLOSE_WAIT;
+        /* set SHUT_LOCAL */
+        s->state |= PICO_SOCKET_STATE_SHUT_LOCAL;
+        pico_socket_close(s);
+        s->parent->number_of_pending_conn--;
+        return 1;
+    }
+
+    return 0;
 }
 
 struct tcp_action_entry {
-  uint16_t tcpstate;
-  int (*syn)(struct pico_socket *s, struct pico_frame *f);
-  int (*synack)(struct pico_socket *s, struct pico_frame *f);
-  int (*ack)(struct pico_socket *s, struct pico_frame *f);
-  int (*data)(struct pico_socket *s, struct pico_frame *f);
-  int (*fin)(struct pico_socket *s, struct pico_frame *f);
-  int (*finack)(struct pico_socket *s, struct pico_frame *f);
-  int (*rst)(struct pico_socket *s, struct pico_frame *f);
+    uint16_t tcpstate;
+    int (*syn)(struct pico_socket *s, struct pico_frame *f);
+    int (*synack)(struct pico_socket *s, struct pico_frame *f);
+    int (*ack)(struct pico_socket *s, struct pico_frame *f);
+    int (*data)(struct pico_socket *s, struct pico_frame *f);
+    int (*fin)(struct pico_socket *s, struct pico_frame *f);
+    int (*finack)(struct pico_socket *s, struct pico_frame *f);
+    int (*rst)(struct pico_socket *s, struct pico_frame *f);
 };
 
 static struct tcp_action_entry tcp_fsm[] = {
-    /* State                            syn              synack             ack                data             fin              finack           rst*/
-  { PICO_SOCKET_STATE_TCP_UNDEF,        NULL,            NULL,              NULL,              NULL,            NULL,            NULL,            NULL     },
-  { PICO_SOCKET_STATE_TCP_CLOSED,       NULL,            NULL,              NULL,              NULL,            NULL,            NULL,            NULL     },
-  { PICO_SOCKET_STATE_TCP_LISTEN,       &tcp_syn,        &tcp_nosync_rst,   &tcp_nosync_rst,   &tcp_nosync_rst, &tcp_nosync_rst, &tcp_nosync_rst, NULL     },
-  { PICO_SOCKET_STATE_TCP_SYN_SENT,     &tcp_nosync_rst, &tcp_synack,       &tcp_nosync_rst,   &tcp_nosync_rst, &tcp_nosync_rst, &tcp_nosync_rst, &tcp_rst },
-  { PICO_SOCKET_STATE_TCP_SYN_RECV,     NULL,            &tcp_nosync_rst,   &tcp_first_ack,    &tcp_data_in,    &tcp_nosync_rst, &tcp_closeconn, &tcp_rst },
-  { PICO_SOCKET_STATE_TCP_ESTABLISHED,  &tcp_halfopencon,&tcp_ack,          &tcp_ack,          &tcp_data_in,    &tcp_closewait,  &tcp_closewait,  &tcp_rst },
-  { PICO_SOCKET_STATE_TCP_CLOSE_WAIT,   NULL,            &tcp_ack,          &tcp_ack,          &tcp_send_rst,   &tcp_closewait,  &tcp_closewait,  &tcp_rst },
-  { PICO_SOCKET_STATE_TCP_LAST_ACK,     NULL,            &tcp_ack,          &tcp_lastackwait,  &tcp_send_rst,   &tcp_send_rst,   &tcp_send_rst,   &tcp_rst },
-  { PICO_SOCKET_STATE_TCP_FIN_WAIT1,    NULL,            &tcp_ack,          &tcp_finwaitack,   &tcp_data_in,    &tcp_rcvfin,     &tcp_finack,     &tcp_rst },
-  { PICO_SOCKET_STATE_TCP_FIN_WAIT2,    NULL,            &tcp_ack,          &tcp_ack,          &tcp_data_in,    &tcp_finwaitfin, &tcp_finack,     &tcp_rst },
-  { PICO_SOCKET_STATE_TCP_CLOSING,      NULL,            &tcp_ack,          &tcp_closewaitack, &tcp_send_rst,   &tcp_send_rst,   &tcp_send_rst,   &tcp_rst },
-  { PICO_SOCKET_STATE_TCP_TIME_WAIT,    NULL,            &tcp_ack,          &tcp_send_rst,     &tcp_send_rst,   &tcp_send_rst,   &tcp_send_rst,   &tcp_rst }
+    /* State                              syn              synack             ack                data             fin              finack           rst*/
+    { PICO_SOCKET_STATE_TCP_UNDEF,        NULL,            NULL,              NULL,              NULL,            NULL,            NULL,            NULL     },
+    { PICO_SOCKET_STATE_TCP_CLOSED,       NULL,            NULL,              NULL,              NULL,            NULL,            NULL,            NULL     },
+    { PICO_SOCKET_STATE_TCP_LISTEN,       &tcp_syn,        NULL,              NULL,              NULL,            NULL,            NULL,            NULL     },
+    { PICO_SOCKET_STATE_TCP_SYN_SENT,     NULL,            &tcp_synack,       NULL,              NULL,            NULL,            NULL,            &tcp_rst },
+    { PICO_SOCKET_STATE_TCP_SYN_RECV,     NULL,            NULL,              &tcp_first_ack,    &tcp_data_in,    NULL,            &tcp_closeconn,  &tcp_rst },
+    { PICO_SOCKET_STATE_TCP_ESTABLISHED,  &tcp_halfopencon, &tcp_ack,          &tcp_ack,          &tcp_data_in,    &tcp_closewait,  &tcp_closewait,  &tcp_rst },
+    { PICO_SOCKET_STATE_TCP_CLOSE_WAIT,   NULL,            &tcp_ack,          &tcp_ack,          &tcp_send_rst,   &tcp_closewait,  &tcp_closewait,  &tcp_rst },
+    { PICO_SOCKET_STATE_TCP_LAST_ACK,     NULL,            &tcp_ack,          &tcp_lastackwait,  &tcp_send_rst,   &tcp_send_rst,   &tcp_send_rst,   &tcp_rst },
+    { PICO_SOCKET_STATE_TCP_FIN_WAIT1,    NULL,            &tcp_ack,          &tcp_finwaitack,   &tcp_data_in,    &tcp_rcvfin,     &tcp_finack,     &tcp_rst },
+    { PICO_SOCKET_STATE_TCP_FIN_WAIT2,    NULL,            &tcp_ack,          &tcp_ack,          &tcp_data_in,    &tcp_finwaitfin, &tcp_finack,     &tcp_rst },
+    { PICO_SOCKET_STATE_TCP_CLOSING,      NULL,            &tcp_ack,          &tcp_closewaitack, &tcp_send_rst,   &tcp_send_rst,   &tcp_send_rst,   &tcp_rst },
+    { PICO_SOCKET_STATE_TCP_TIME_WAIT,    NULL,            &tcp_ack,          &tcp_send_rst,     &tcp_send_rst,   &tcp_send_rst,   &tcp_send_rst,   &tcp_rst }
 };
 
 /*
    NOTE: in SYN-RECV receiving syn when cloned by default (see yellow pos-it), should send reset.
-*/
-
+ */
+#define MAX_VALID_FLAGS  9  /* Maximum number of valid flag combinations */
+static uint8_t invalid_flags(struct pico_socket *s, uint8_t flags)
+{
+    uint8_t i;
+    static uint8_t valid_flags[PICO_SOCKET_STATE_TCP_ARRAYSIZ][MAX_VALID_FLAGS] = {
+        { /* 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_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},
+        { /* PICO_SOCKET_STATE_TCP_LAST_ACK   */ 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_FIN_WAIT1  */ 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_FIN_WAIT2  */ 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_CLOSING    */ 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_TIME_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},
+    };
+    if(!flags)
+        return 1;
+
+    for(i = 0; i < MAX_VALID_FLAGS; i++)
+        if(valid_flags[s->state >> 8u][i] == flags)
+            return 0;
+
+    return 1;
+}
 int pico_tcp_input(struct pico_socket *s, struct pico_frame *f)
 {
-  struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *) (f->transport_hdr);
-  int ret = 0;
-  uint8_t flags = hdr->flags;
-  struct tcp_action_entry *action = &tcp_fsm[s->state >> 8];
-
-  f->payload = (f->transport_hdr + ((hdr->len & 0xf0) >> 2));
-  f->payload_len = (uint16_t)(f->transport_len - ((hdr->len & 0xf0) >> 2));
-
-  tcp_dbg("[%lu] TCP> [tcp input] socket: %p state: %d <-- local port:%d remote port: %d seq: %08x ack: %08x flags: %02x = t_len: %d, hdr: %u payload: %d\n", pico_tick,
-      s, s->state >> 8, short_be(hdr->trans.dport), short_be(hdr->trans.sport), SEQN(f), ACKN(f), hdr->flags, f->transport_len, (hdr->len & 0xf0) >> 2, f->payload_len );
-
-  /* This copy of the frame has the current socket as owner */
-  f->sock = s;
-  s->timestamp = PICO_TIME_MS();
-  /* Those are not supported at this time. */
-  flags &= (uint8_t)~(PICO_TCP_CWR | PICO_TCP_URG | PICO_TCP_ECN);
-  if (flags == PICO_TCP_SYN) {
-    if (action->syn)
-      action->syn(s,f);
-  } else if (flags == (PICO_TCP_SYN | PICO_TCP_ACK)) {
-    if (action->synack)
-      action->synack(s,f);
-  } else {
-    if ((flags == PICO_TCP_ACK) || (flags == (PICO_TCP_ACK | PICO_TCP_PSH))) {
-      if (action->ack) {
-        action->ack(s,f);
-      }
+    struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *) (f->transport_hdr);
+    int ret = 0;
+    uint8_t flags = hdr->flags;
+    struct tcp_action_entry *action = &tcp_fsm[s->state >> 8];
+
+    f->payload = (f->transport_hdr + ((hdr->len & 0xf0) >> 2));
+    f->payload_len = (uint16_t)(f->transport_len - ((hdr->len & 0xf0) >> 2));
+
+    tcp_dbg("[%lu] TCP> [tcp input] socket: %p state: %d <-- local port:%d remote port: %d seq: %08x ack: %08x flags: %02x = t_len: %d, hdr: %u payload: %d\n", pico_tick,
+            s, s->state >> 8, short_be(hdr->trans.dport), short_be(hdr->trans.sport), SEQN(f), ACKN(f), hdr->flags, f->transport_len, (hdr->len & 0xf0) >> 2, f->payload_len );
+
+    /* This copy of the frame has the current socket as owner */
+    f->sock = s;
+    s->timestamp = PICO_TIME_MS();
+    /* Those are not supported at this time. */
+    /* flags &= (uint8_t) ~(PICO_TCP_CWR | PICO_TCP_URG | PICO_TCP_ECN); */
+    if(invalid_flags(s, flags))
+        pico_tcp_reply_rst(f);
+    else if (flags == PICO_TCP_SYN) {
+        if (action->syn)
+            action->syn(s, f);
+    } else if (flags == (PICO_TCP_SYN | PICO_TCP_ACK)) {
+        if (action->synack)
+            action->synack(s, f);
+    } else {
+        if ((flags == PICO_TCP_ACK) || (flags == (PICO_TCP_ACK | PICO_TCP_PSH))) {
+            if (action->ack) {
+                action->ack(s, f);
+            }
+        }
+
+        if (f->payload_len > 0 && !(s->state & PICO_SOCKET_STATE_CLOSED) && !TCP_IS_STATE(s, PICO_SOCKET_STATE_TCP_LISTEN))
+        {
+            ret = f->payload_len;
+            if (action->data)
+                action->data(s, f);
+        }
+
+        if (flags == PICO_TCP_FIN) {
+            if (action->fin)
+                action->fin(s, f);
+        }
+
+        if ((flags == (PICO_TCP_FIN | PICO_TCP_ACK)) || (flags == (PICO_TCP_FIN | PICO_TCP_ACK | PICO_TCP_PSH))) {
+            if (action->finack)
+                action->finack(s, f);
+        }
+
+        if (flags & PICO_TCP_RST) {
+            if (action->rst)
+                action->rst(s, f);
+        }
     }
-    if (f->payload_len > 0 && !(s->state & PICO_SOCKET_STATE_CLOSED) && !TCP_IS_STATE(s,PICO_SOCKET_STATE_TCP_LISTEN))
-    {
-      ret = f->payload_len;
-      if (action->data)
-        action->data(s,f);
-    }
-    if (flags == PICO_TCP_FIN) {
-      if (action->fin)
-        action->fin(s,f);
-    }
-    if ((flags == (PICO_TCP_FIN | PICO_TCP_ACK)) || (flags == (PICO_TCP_FIN | PICO_TCP_ACK | PICO_TCP_PSH))) {
-      if (action->finack)
-        action->finack(s,f);
-    }
-    if (flags & PICO_TCP_RST) {
-      if (action->rst)
-        action->rst(s,f);
-    }
-  }
-
-//discard:
-  pico_frame_discard(f);
-  return ret;
+
+/* discard: */
+    pico_frame_discard(f);
+    return ret;
 }
 
 static void tcp_send_keepalive(pico_time when, void *_t)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)_t;
-  IGNORE_PARAMETER(when);
-  tcp_dbg("Sending keepalive (%d), [State = %d]...\n", t->backoff,t->sock.state );
-  if( t->sock.net && ((t->sock.state & 0xFF00) == PICO_SOCKET_STATE_TCP_ESTABLISHED) )
-  {
-		tcp_send_ack(t);
-
-		if (t->keepalive_timer_running > 0) {
-			t->keepalive_timer_running--;
-		}
-
-		if (t->keepalive_timer_running == 0) {
-			t->keepalive_timer_running++;
-			tcp_dbg("[Self] Adding timer(retransmit keepalive)\n");
-			pico_timer_add(t->rto << (++t->backoff), tcp_send_keepalive, t);
-		}
-  }
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)_t;
+    IGNORE_PARAMETER(when);
+    tcp_dbg("Sending keepalive (%d), [State = %d]...\n", t->backoff, t->sock.state );
+    if( t->sock.net && ((t->sock.state & 0xFF00) == PICO_SOCKET_STATE_TCP_ESTABLISHED))
+    {
+        tcp_send_ack(t);
+
+        if (t->keepalive_timer_running > 0) {
+            t->keepalive_timer_running--;
+        }
+
+        if (t->keepalive_timer_running == 0) {
+            t->keepalive_timer_running++;
+            tcp_dbg("[Self] Adding timer(retransmit keepalive)\n");
+            pico_timer_add(t->rto << (++t->backoff), tcp_send_keepalive, t);
+        }
+    }
 }
 
 inline static int checkLocalClosing(struct pico_socket *s);
@@ -2172,292 +2354,303 @@
 
 int pico_tcp_output(struct pico_socket *s, int loop_score)
 {
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
-  struct pico_frame *f, *una;
-  struct pico_tcp_hdr *hdr;
-  int sent = 0;
-
-  una = first_segment(&t->tcpq_out);
-  f = peek_segment(&t->tcpq_out, t->snd_nxt);
-
-  while((f) && (t->cwnd >= t->in_flight)) {
-    hdr = (struct pico_tcp_hdr *)f->transport_hdr;
-    f->timestamp = pico_tick;
-    tcp_add_options(t, f, hdr->flags, tcp_options_size(t, hdr->flags));
-    if (seq_compare((SEQN(f) + f->payload_len), (SEQN(una) + (uint32_t)(t->recv_wnd << t->recv_wnd_scale))) > 0) {
-      t->cwnd = (uint16_t)t->in_flight;
-      if (t->cwnd < 1)
-        t->cwnd = 1;
-      if (t->x_mode != PICO_TCP_WINDOW_FULL) {
-        tcp_dbg("TCP> RIGHT SIZING (rwnd: %d, frame len: %d\n",t->recv_wnd << t->recv_wnd_scale, f->payload_len);
-        tcp_dbg("In window full...\n");
-        t->snd_nxt = SEQN(una);   /* XXX prevent out-of-order-packets ! */ /*DLA re-enabled.*/
-        t->snd_retry = SEQN(una);   /* XXX replace by retry pointer? */
-
-        /* Alternative to the line above:  (better performance, but seems to lock anyway with larger buffers)
-        if (seq_compare(t->snd_nxt, SEQN(una)) > 0)
-          t->snd_nxt -= f->payload_len;
-        */
-
-        t->x_mode = PICO_TCP_WINDOW_FULL;
-        if (t->keepalive_timer_running == 0) {
-          tcp_dbg("[Window full] Adding timer(send keepalive)\n");
-          tcp_send_keepalive(0, t);
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    struct pico_frame *f, *una;
+    int sent = 0;
+
+    una = first_segment(&t->tcpq_out);
+    f = peek_segment(&t->tcpq_out, t->snd_nxt);
+
+    while((f) && (t->cwnd >= t->in_flight)) {
+        f->timestamp = pico_tick;
+        tcp_add_options_frame(t, f);
+        if (seq_compare((SEQN(f) + f->payload_len), (SEQN(una) + (uint32_t)(t->recv_wnd << t->recv_wnd_scale))) > 0) {
+            t->cwnd = (uint16_t)t->in_flight;
+            if (t->cwnd < 1)
+                t->cwnd = 1;
+
+            if (t->x_mode != PICO_TCP_WINDOW_FULL) {
+                tcp_dbg("TCP> RIGHT SIZING (rwnd: %d, frame len: %d\n", t->recv_wnd << t->recv_wnd_scale, f->payload_len);
+                tcp_dbg("In window full...\n");
+                t->snd_nxt = SEQN(una); /* XXX prevent out-of-order-packets ! */ /*DLA re-enabled.*/
+                t->snd_retry = SEQN(una); /* XXX replace by retry pointer? */
+
+                /* Alternative to the line above:  (better performance, but seems to lock anyway with larger buffers)
+                   if (seq_compare(t->snd_nxt, SEQN(una)) > 0)
+                   t->snd_nxt -= f->payload_len;
+                 */
+
+                t->x_mode = PICO_TCP_WINDOW_FULL;
+                if (t->keepalive_timer_running == 0) {
+                    tcp_dbg("[Window full] Adding timer(send keepalive)\n");
+                    tcp_send_keepalive(0, t);
+                }
+            }
+
+            break;
         }
-      }
-      break;
-    }
-    tcp_dbg("TCP> DEQUEUED (for output) frame %08x, acks %08x len= %d, remaining frames %d\n", SEQN(f), ACKN(f), f->payload_len,t->tcpq_out.frames);
-    tcp_send(t, f);
-    sent++;
-    loop_score--;
-    t->snd_last_out = SEQN(f);
-    if (loop_score < 1)
-      break;
-    if (f->payload_len > 0) {
-      f = next_segment(&t->tcpq_out, f);
-    } else {
-      f = NULL;
+
+        tcp_dbg("TCP> DEQUEUED (for output) frame %08x, acks %08x len= %d, remaining frames %d\n", SEQN(f), ACKN(f), f->payload_len, t->tcpq_out.frames);
+        tcp_send(t, f);
+        sent++;
+        loop_score--;
+        t->snd_last_out = SEQN(f);
+        if (loop_score < 1)
+            break;
+
+        if (f->payload_len > 0) {
+            f = next_segment(&t->tcpq_out, f);
+        } else {
+            f = NULL;
+        }
     }
-  }
-  if (sent > 0) {
-    if (t->rto < PICO_TCP_RTO_MIN)
-      t->rto = PICO_TCP_RTO_MIN;
-    //if (s->wakeup)
-    //  t->sock.wakeup(PICO_SOCK_EV_WR, &t->sock);
-    add_retransmission_timer(t, pico_tick + t->rto);
-  } else {
-    /* Nothing to transmit. */
-  }
-
-  if ((t->tcpq_out.frames == 0) && (s->state & PICO_SOCKET_STATE_SHUT_LOCAL)) {    /* if no more packets in queue, XXX replacled !f by tcpq check */
-	  if(!checkLocalClosing(&t->sock)) // check if local closing started and send fin
-	  {
-		  checkRemoteClosing(&t->sock); // check if remote closing started and send fin
-	  }
-  }
-  return loop_score;
+    if (sent > 0) {
+        if (t->rto < PICO_TCP_RTO_MIN)
+            t->rto = PICO_TCP_RTO_MIN;
+
+        /* if (s->wakeup) */
+        /*  t->sock.wakeup(PICO_SOCK_EV_WR, &t->sock); */
+        add_retransmission_timer(t, pico_tick + t->rto);
+    } else {
+        /* Nothing to transmit. */
+    }
+
+    if ((t->tcpq_out.frames == 0) && (s->state & PICO_SOCKET_STATE_SHUT_LOCAL)) {  /* if no more packets in queue, XXX replacled !f by tcpq check */
+        if(!checkLocalClosing(&t->sock)) /* check if local closing started and send fin */
+        {
+            checkRemoteClosing(&t->sock); /* check if remote closing started and send fin */
+        }
+    }
+
+    return loop_score;
 }
 
 /* function to make new segment from hold queue with specific size (mss) */
-static struct pico_frame * pico_hold_segment_make(struct pico_socket_tcp *t)
+static struct pico_frame *pico_hold_segment_make(struct pico_socket_tcp *t)
 {
-  struct pico_frame *f_temp,*f_new;
-  struct pico_socket *s = (struct pico_socket *) &t->sock;
-  struct pico_tcp_hdr *hdr;
-  uint16_t total_len = 0, total_payload_len = 0;
-  uint16_t off = 0, test = 0;
-
-  off = pico_tcp_overhead(s);
-
-  /* init with first frame in hold queue */
-  f_temp = first_segment(&t->tcpq_hold);
-  total_len = f_temp->payload_len;
-  f_temp = next_segment(&t->tcpq_hold, f_temp);
-
-  /* check till total_len <= MSS */
-  while ((f_temp != NULL) && ((total_len+f_temp->payload_len) <= PICO_TCP_DEFAULT_MSS)) {
-    total_len = (uint16_t)(total_len + f_temp->payload_len);
+    struct pico_frame *f_temp, *f_new;
+    struct pico_socket *s = (struct pico_socket *) &t->sock;
+    struct pico_tcp_hdr *hdr;
+    uint16_t total_len = 0, total_payload_len = 0;
+    uint16_t off = 0, test = 0;
+
+    off = pico_tcp_overhead(s);
+
+    /* init with first frame in hold queue */
+    f_temp = first_segment(&t->tcpq_hold);
+    total_len = f_temp->payload_len;
     f_temp = next_segment(&t->tcpq_hold, f_temp);
-    if (f_temp == NULL)
-      break;
-  }
-  /* alloc new frame with payload size = off + total_len */
-  f_new = pico_socket_frame_alloc(s, (uint16_t)(off + total_len));
-  if (!f_new) {
-    pico_err = PICO_ERR_ENOMEM;
+
+    /* check till total_len <= MSS */
+    while ((f_temp != NULL) && ((total_len + f_temp->payload_len) <= PICO_TCP_DEFAULT_MSS)) {
+        total_len = (uint16_t)(total_len + f_temp->payload_len);
+        f_temp = next_segment(&t->tcpq_hold, f_temp);
+        if (f_temp == NULL)
+            break;
+    }
+    /* alloc new frame with payload size = off + total_len */
+    f_new = pico_socket_frame_alloc(s, (uint16_t)(off + total_len));
+    if (!f_new) {
+        pico_err = PICO_ERR_ENOMEM;
+        return f_new;
+    }
+
+    hdr = (struct pico_tcp_hdr *) f_new->transport_hdr;
+    /* init new frame */
+    f_new->payload += off;
+    f_new->payload_len = (uint16_t)(f_new->payload_len - off);
+    f_new->sock = s;
+
+    f_temp = first_segment(&t->tcpq_hold);
+    hdr->seq = ((struct pico_tcp_hdr *)(f_temp->transport_hdr))->seq; /* get sequence number of first frame */
+    hdr->trans.sport = t->sock.local_port;
+    hdr->trans.dport = t->sock.remote_port;
+
+    /* check till total_payload_len <= MSS */
+    while ((f_temp != NULL) && ((total_payload_len + f_temp->payload_len) <= PICO_TCP_DEFAULT_MSS)) {
+        /* cpy data and discard frame */
+        test++;
+        memcpy(f_new->payload + total_payload_len, f_temp->payload, f_temp->payload_len);
+        total_payload_len = (uint16_t)(total_payload_len + f_temp->payload_len);
+        pico_discard_segment(&t->tcpq_hold, f_temp);
+        f_temp = first_segment(&t->tcpq_hold);
+    }
+    hdr->len = (uint8_t)((f_new->payload - f_new->transport_hdr) << 2 | t->jumbo);
+
+    tcp_dbg_nagle("NAGLE make - joined %d segments, len %d bytes\n", test, total_payload_len);
+
     return f_new;
-  }
-
-  hdr = (struct pico_tcp_hdr *) f_new->transport_hdr;
-  /* init new frame */
-  f_new->payload += off;
-  f_new->payload_len = (uint16_t)(f_new->payload_len - off);
-  f_new->sock = s;
-
-  f_temp = first_segment(&t->tcpq_hold);
-  hdr->seq = ((struct pico_tcp_hdr *)(f_temp->transport_hdr))->seq;  /* get sequence number of first frame */
-  hdr->trans.sport = t->sock.local_port;
-  hdr->trans.dport = t->sock.remote_port;
-
-  /* check till total_payload_len <= MSS */
-  while ((f_temp != NULL) && ((total_payload_len + f_temp->payload_len) <= PICO_TCP_DEFAULT_MSS)) {
-    /* cpy data and discard frame */
-    test++;
-    memcpy(f_new->payload + total_payload_len, f_temp->payload, f_temp->payload_len);
-    total_payload_len = (uint16_t)(total_payload_len + f_temp->payload_len);
-    pico_discard_segment(&t->tcpq_hold, f_temp);
-    f_temp = first_segment(&t->tcpq_hold);
-  }
-
-  hdr->len = (uint8_t)((f_new->payload - f_new->transport_hdr) << 2 | t->jumbo);
-
-  tcp_dbg_nagle("NAGLE make - joined %d segments, len %d bytes\n",test,total_payload_len);
-
-  return f_new;
 }
 
 /* original behavior kept when Nagle disabled;
    Nagle algorithm added here, keeping hold frame queue instead of eg linked list of data */
 int32_t pico_tcp_push(struct pico_protocol *self, struct pico_frame *f)
 {
-  struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *)f->transport_hdr;
-  struct pico_socket_tcp *t = (struct pico_socket_tcp *) f->sock;
-  struct pico_frame *f_new;
-  uint32_t total_len = 0;
-  IGNORE_PARAMETER(self);
-  pico_err = PICO_ERR_NOERR;
-  hdr->trans.sport = t->sock.local_port;
-  hdr->trans.dport = t->sock.remote_port;
-  hdr->seq = long_be(t->snd_last + 1);
-  hdr->len = (uint8_t)((f->payload - f->transport_hdr) << 2 | t->jumbo);
-
-  if ((uint32_t)f->payload_len > (uint32_t)(t->tcpq_out.max_size - t->tcpq_out.size))
-    t->sock.ev_pending &= (uint16_t)(~PICO_SOCK_EV_WR);
-
-  /***************************************************************************/
-
-  if (!IS_NAGLE_ENABLED((&(t->sock)))) {
-    /* TCP_NODELAY enabled, original behavior */
-    if (pico_enqueue_segment(&t->tcpq_out,f) > 0) {
-      tcp_dbg_nagle("TCP_PUSH - NO NAGLE - Pushing segment %08x, len %08x to socket %p\n", t->snd_last + 1, f->payload_len, t);
-      t->snd_last += f->payload_len;
-      return f->payload_len;
-    } else {
-      tcp_dbg("Enqueue failed.\n");
-      return 0;
-    }
-  }
-  /***************************************************************************/
-  else {
-    /* Nagle's algorithm enabled, check if ready to send, or put frame in hold queue */
-    if (IS_TCP_IDLE(t) && IS_TCP_HOLDQ_EMPTY(t)) {  /* opt 1. send frame */
-      if (pico_enqueue_segment(&t->tcpq_out,f) > 0) {
-        tcp_dbg_nagle("TCP_PUSH - NAGLE - Pushing segment %08x, len %08x to socket %p\n", t->snd_last + 1, f->payload_len, t);
-        t->snd_last += f->payload_len;
-        return f->payload_len;
-      } else {
-        tcp_dbg("Enqueue failed.\n");
-        return 0;
-      }
-    } else {                                        /* opt 2. hold data back */
-      total_len = f->payload_len + t->tcpq_hold.size;
-      if ((total_len >= PICO_TCP_DEFAULT_MSS) && ((t->tcpq_out.max_size - t->tcpq_out.size) >= PICO_TCP_DEFAULT_MSS)) {/* TODO check mss socket */
-        /* IF enough data in hold (>mss) AND space in out queue (>mss) */
-        /* add current frame in hold and make new segment */
-        if (pico_enqueue_segment(&t->tcpq_hold,f) > 0 ) {
-          tcp_dbg_nagle("TCP_PUSH - NAGLE - Pushed into hold, make new (enqueued frames out %d)\n",t->tcpq_out.frames);
-          t->snd_last += f->payload_len;    /* XXX  WATCH OUT */
-          f_new = pico_hold_segment_make(t);
+    struct pico_tcp_hdr *hdr = (struct pico_tcp_hdr *)f->transport_hdr;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *) f->sock;
+    struct pico_frame *f_new;
+    uint32_t total_len = 0;
+    IGNORE_PARAMETER(self);
+    pico_err = PICO_ERR_NOERR;
+    hdr->trans.sport = t->sock.local_port;
+    hdr->trans.dport = t->sock.remote_port;
+    hdr->seq = long_be(t->snd_last + 1);
+    hdr->len = (uint8_t)((f->payload - f->transport_hdr) << 2 | t->jumbo);
+
+    if ((uint32_t)f->payload_len > (uint32_t)(t->tcpq_out.max_size - t->tcpq_out.size))
+        t->sock.ev_pending &= (uint16_t)(~PICO_SOCK_EV_WR);
+
+    /***************************************************************************/
+
+    if (!IS_NAGLE_ENABLED((&(t->sock)))) {
+        /* TCP_NODELAY enabled, original behavior */
+        if (pico_enqueue_segment(&t->tcpq_out, f) > 0) {
+            tcp_dbg_nagle("TCP_PUSH - NO NAGLE - Pushing segment %08x, len %08x to socket %p\n", t->snd_last + 1, f->payload_len, t);
+            t->snd_last += f->payload_len;
+            return f->payload_len;
         } else {
-          tcp_dbg_nagle("TCP_PUSH - NAGLE - enqueue hold failed 1\n");
-          return 0;
-        }
-        /* and put new frame in out queue */
-        if ((f_new != NULL) && (pico_enqueue_segment(&t->tcpq_out,f_new) > 0)) {
-          return f_new->payload_len;
-        } else {
-          tcp_dbg_nagle("TCP_PUSH - NAGLE - enqueue out failed, f_new = %p\n",f_new);
-          return -1;                        /* XXX something seriously wrong */
-        }
-      } else {
-        /* ELSE put frame in hold queue */
-        if (pico_enqueue_segment(&t->tcpq_hold,f) > 0) {
-          tcp_dbg_nagle("TCP_PUSH - NAGLE - Pushed into hold (enqueued frames out %d)\n",t->tcpq_out.frames);
-          t->snd_last += f->payload_len;    /* XXX  WATCH OUT */
-          return f->payload_len;
-        } else {
-          pico_err = PICO_ERR_EAGAIN;
-          tcp_dbg_nagle("TCP_PUSH - NAGLE - enqueue hold failed 2\n");
-          return 0;
+            tcp_dbg("Enqueue failed.\n");
+            return 0;
         }
     }
-  }
-      }
-  /***************************************************************************/
+    /***************************************************************************/
+    else {
+        /* Nagle's algorithm enabled, check if ready to send, or put frame in hold queue */
+        if (IS_TCP_IDLE(t) && IS_TCP_HOLDQ_EMPTY(t)) { /* opt 1. send frame */
+            if (pico_enqueue_segment(&t->tcpq_out, f) > 0) {
+                tcp_dbg_nagle("TCP_PUSH - NAGLE - Pushing segment %08x, len %08x to socket %p\n", t->snd_last + 1, f->payload_len, t);
+                t->snd_last += f->payload_len;
+                return f->payload_len;
+            } else {
+                tcp_dbg("Enqueue failed.\n");
+                return 0;
+            }
+        } else {                                    /* opt 2. hold data back */
+            total_len = f->payload_len + t->tcpq_hold.size;
+            if ((total_len >= PICO_TCP_DEFAULT_MSS) && ((t->tcpq_out.max_size - t->tcpq_out.size) >= PICO_TCP_DEFAULT_MSS)) { /* TODO check mss socket */
+                /* IF enough data in hold (>mss) AND space in out queue (>mss) */
+                /* add current frame in hold and make new segment */
+                if (pico_enqueue_segment(&t->tcpq_hold, f) > 0 ) {
+                    tcp_dbg_nagle("TCP_PUSH - NAGLE - Pushed into hold, make new (enqueued frames out %d)\n", t->tcpq_out.frames);
+                    t->snd_last += f->payload_len; /* XXX  WATCH OUT */
+                    f_new = pico_hold_segment_make(t);
+                } else {
+                    tcp_dbg_nagle("TCP_PUSH - NAGLE - enqueue hold failed 1\n");
+                    return 0;
+                }
+
+                /* and put new frame in out queue */
+                if ((f_new != NULL) && (pico_enqueue_segment(&t->tcpq_out, f_new) > 0)) {
+                    return f_new->payload_len;
+                } else {
+                    tcp_dbg_nagle("TCP_PUSH - NAGLE - enqueue out failed, f_new = %p\n", f_new);
+                    return -1;              /* XXX something seriously wrong */
+                }
+            } else {
+                /* ELSE put frame in hold queue */
+                if (pico_enqueue_segment(&t->tcpq_hold, f) > 0) {
+                    tcp_dbg_nagle("TCP_PUSH - NAGLE - Pushed into hold (enqueued frames out %d)\n", t->tcpq_out.frames);
+                    t->snd_last += f->payload_len; /* XXX  WATCH OUT */
+                    return f->payload_len;
+                } else {
+                    pico_err = PICO_ERR_EAGAIN;
+                    tcp_dbg_nagle("TCP_PUSH - NAGLE - enqueue hold failed 2\n");
+                    return 0;
+                }
+            }
+        }
+    }
+
+    /***************************************************************************/
 }
 
 inline static void tcp_discard_all_segments(struct pico_tcp_queue *tq)
 {
-  struct pico_tree_node *index = NULL, *index_safe = NULL;
-  LOCK(Mutex);
-	pico_tree_foreach_safe(index,&tq->pool,index_safe)
-	{
-	  void *f = index->keyValue;
-	  if(!f)
-		break;
-	  pico_tree_delete(&tq->pool,f);
-	  if(IS_INPUT_QUEUE(tq))
-	  {
-		struct tcp_input_segment *inp = (struct tcp_input_segment *)f;
-		pico_free(inp->payload);
-		pico_free(inp);
-	  }
-	  else
-	    pico_frame_discard(f);
-	}
- tq->frames = 0;
- tq->size = 0;
- UNLOCK(Mutex);
+    struct pico_tree_node *index = NULL, *index_safe = NULL;
+    LOCK(Mutex);
+    pico_tree_foreach_safe(index, &tq->pool, index_safe)
+    {
+        void *f = index->keyValue;
+        if(!f)
+            break;
+
+        pico_tree_delete(&tq->pool, f);
+        if(IS_INPUT_QUEUE(tq))
+        {
+            struct tcp_input_segment *inp = (struct tcp_input_segment *)f;
+            pico_free(inp->payload);
+            pico_free(inp);
+        }
+        else
+            pico_frame_discard(f);
+    }
+    tq->frames = 0;
+    tq->size = 0;
+    UNLOCK(Mutex);
 }
 
 void pico_tcp_cleanup_queues(struct pico_socket *sck)
 {
-  struct pico_socket_tcp * tcp = (struct pico_socket_tcp *)sck;
-  if(tcp->retrans_tmr)
-	  pico_timer_cancel(tcp->retrans_tmr);
-  tcp_discard_all_segments(&tcp->tcpq_in);
-  tcp_discard_all_segments(&tcp->tcpq_out);
-  tcp_discard_all_segments(&tcp->tcpq_hold);
+    struct pico_socket_tcp *tcp = (struct pico_socket_tcp *)sck;
+    if(tcp->retrans_tmr)
+        pico_timer_cancel(tcp->retrans_tmr);
+
+    tcp_discard_all_segments(&tcp->tcpq_in);
+    tcp_discard_all_segments(&tcp->tcpq_out);
+    tcp_discard_all_segments(&tcp->tcpq_hold);
 }
 
 static int checkLocalClosing(struct pico_socket *s)
 {
-	struct pico_socket_tcp * t = (struct pico_socket_tcp *)s;
-	if ((s->state & PICO_SOCKET_STATE_TCP) == PICO_SOCKET_STATE_TCP_ESTABLISHED) {
-		  tcp_dbg("TCP> buffer empty, shutdown established ...\n");
-		  /* send fin if queue empty and in state shut local (write) */
-		  tcp_send_fin(t);
-		  /* change tcp state to FIN_WAIT1 */
-		  s->state &= 0x00FFU;
-		  s->state |= PICO_SOCKET_STATE_TCP_FIN_WAIT1;
-		  return 1;
-	}
-	return 0;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    if ((s->state & PICO_SOCKET_STATE_TCP) == PICO_SOCKET_STATE_TCP_ESTABLISHED) {
+        tcp_dbg("TCP> buffer empty, shutdown established ...\n");
+        /* send fin if queue empty and in state shut local (write) */
+        tcp_send_fin(t);
+        /* change tcp state to FIN_WAIT1 */
+        s->state &= 0x00FFU;
+        s->state |= PICO_SOCKET_STATE_TCP_FIN_WAIT1;
+        return 1;
+    }
+
+    return 0;
 }
 
 static int checkRemoteClosing(struct pico_socket *s)
 {
-	struct pico_socket_tcp * t = (struct pico_socket_tcp *)s;
-	if ((s->state & PICO_SOCKET_STATE_TCP) == PICO_SOCKET_STATE_TCP_CLOSE_WAIT) {
-      /* send fin if queue empty and in state shut local (write) */
-      tcp_send_fin(t);
-      /* change tcp state to LAST_ACK */
-      s->state &= 0x00FFU;
-      s->state |= PICO_SOCKET_STATE_TCP_LAST_ACK;
-      tcp_dbg("TCP> STATE: LAST_ACK.\n");
-      return 1;
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)s;
+    if ((s->state & PICO_SOCKET_STATE_TCP) == PICO_SOCKET_STATE_TCP_CLOSE_WAIT) {
+        /* send fin if queue empty and in state shut local (write) */
+        tcp_send_fin(t);
+        /* change tcp state to LAST_ACK */
+        s->state &= 0x00FFU;
+        s->state |= PICO_SOCKET_STATE_TCP_LAST_ACK;
+        tcp_dbg("TCP> STATE: LAST_ACK.\n");
+        return 1;
     }
-	return 0;
+
+    return 0;
 }
 
 void pico_tcp_notify_closing(struct pico_socket *sck)
 {
-	struct pico_socket_tcp *t=(struct pico_socket_tcp *)sck;
-	if(t->tcpq_out.frames == 0)
-	{
-	    // canceling retrans timer when closing
-		if(t->retrans_tmr)
-	    {
-		    pico_timer_cancel(t->retrans_tmr);
-		    t->retrans_tmr = NULL;
-  	    }
-
-		if(!checkLocalClosing(sck))
-			checkRemoteClosing(sck);
-	}
+    struct pico_socket_tcp *t = (struct pico_socket_tcp *)sck;
+    if(t->tcpq_out.frames == 0)
+    {
+        /* canceling retrans timer when closing */
+        if(t->retrans_tmr)
+        {
+            pico_timer_cancel(t->retrans_tmr);
+            t->retrans_tmr = NULL;
+        }
+
+        if(!checkLocalClosing(sck))
+            checkRemoteClosing(sck);
+    }
 }
 
-#endif //PICO_SUPPORT_TCP
-
-
+void transport_flags_update(struct pico_frame *f, struct pico_socket *s)
+{
+    f->transport_flags_saved = ((struct pico_socket_tcp *)s)->ts_ok;
+}
+#endif /* PICO_SUPPORT_TCP */
--- a/modules/pico_tcp.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_tcp.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_TCP
 #define _INCLUDE_PICO_TCP
 #include "pico_addressing.h"
@@ -14,23 +14,23 @@
 extern struct pico_protocol pico_proto_tcp;
 
 struct __attribute__((packed)) pico_tcp_hdr {
-  struct pico_trans trans;
-  uint32_t seq;
-  uint32_t ack;
-  uint8_t  len;
-  uint8_t flags;
-  uint16_t  rwnd;
-  uint16_t crc;
-  uint16_t urgent;
+    struct pico_trans trans;
+    uint32_t seq;
+    uint32_t ack;
+    uint8_t len;
+    uint8_t flags;
+    uint16_t rwnd;
+    uint16_t crc;
+    uint16_t urgent;
 };
 
 struct __attribute__((packed)) tcp_pseudo_hdr_ipv4
 {
-  struct pico_ip4 src;
-  struct pico_ip4 dst;
-  uint16_t tcp_len;
-  uint8_t res;
-  uint8_t proto;
+    struct pico_ip4 src;
+    struct pico_ip4 dst;
+    uint16_t tcp_len;
+    uint8_t res;
+    uint8_t proto;
 };
 
 #define PICO_TCPHDR_SIZE 20
@@ -67,21 +67,25 @@
 #define PICO_TCP_ECN 0x40u
 #define PICO_TCP_CWR 0x80u
 
+#define PICO_TCP_SYNACK    (PICO_TCP_SYN | PICO_TCP_ACK)
+#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)
 
 
 struct __attribute__((packed)) pico_tcp_option
 {
-  uint8_t kind;
-  uint8_t len;
+    uint8_t kind;
+    uint8_t len;
 #if 0
-  union {
-   uint16_t mss;
-    uint8_t wshift;
-    struct {
-      uint32_t tsval;
-      uint32_t tsecr;
-    } timestamp;
-  } data;
+    union {
+        uint16_t mss;
+        uint8_t wshift;
+        struct {
+            uint32_t tsval;
+            uint32_t tsecr;
+        } timestamp;
+    } data;
 #endif
 };
 
@@ -96,5 +100,6 @@
 int pico_tcp_reply_rst(struct pico_frame *f);
 void pico_tcp_cleanup_queues(struct pico_socket *sck);
 void pico_tcp_notify_closing(struct pico_socket *sck);
+void transport_flags_update(struct pico_frame *, struct pico_socket *);
 
 #endif
--- a/modules/pico_udp.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_udp.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 
 #include "pico_udp.h"
@@ -16,81 +16,87 @@
 
 
 /* Queues */
-static struct pico_queue udp_in = {0};
-static struct pico_queue udp_out = {0};
+static struct pico_queue udp_in = {
+    0
+};
+static struct pico_queue udp_out = {
+    0
+};
 
 
 /* Functions */
 
 uint16_t pico_udp_checksum_ipv4(struct pico_frame *f)
 {
-  struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-  struct pico_udp_hdr *udp_hdr = (struct pico_udp_hdr *) f->transport_hdr;
-  struct pico_socket *s = f->sock;
-  struct pico_ipv4_pseudo_hdr pseudo;
+    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    struct pico_udp_hdr *udp_hdr = (struct pico_udp_hdr *) f->transport_hdr;
+    struct pico_socket *s = f->sock;
+    struct pico_ipv4_pseudo_hdr pseudo;
 
-  if (s) {
-    /* Case of outgoing frame */
-    //dbg("UDP CRC: on outgoing frame\n");
-    pseudo.src.addr = s->local_addr.ip4.addr;
-    pseudo.dst.addr = s->remote_addr.ip4.addr;
-  } else {
-    /* Case of incomming frame */
-    //dbg("UDP CRC: on incomming frame\n");
-    pseudo.src.addr = hdr->src.addr;
-    pseudo.dst.addr = hdr->dst.addr;
-  }
-  pseudo.zeros = 0;
-  pseudo.proto = PICO_PROTO_UDP;
-  pseudo.len = short_be(f->transport_len);
+    if (s) {
+        /* Case of outgoing frame */
+        /* dbg("UDP CRC: on outgoing frame\n"); */
+        pseudo.src.addr = s->local_addr.ip4.addr;
+        pseudo.dst.addr = s->remote_addr.ip4.addr;
+    } else {
+        /* Case of incomming frame */
+        /* dbg("UDP CRC: on incomming frame\n"); */
+        pseudo.src.addr = hdr->src.addr;
+        pseudo.dst.addr = hdr->dst.addr;
+    }
 
-  return pico_dualbuffer_checksum(&pseudo, sizeof(struct pico_ipv4_pseudo_hdr), udp_hdr, f->transport_len);
+    pseudo.zeros = 0;
+    pseudo.proto = PICO_PROTO_UDP;
+    pseudo.len = short_be(f->transport_len);
+
+    return pico_dualbuffer_checksum(&pseudo, sizeof(struct pico_ipv4_pseudo_hdr), udp_hdr, f->transport_len);
 }
 
 
 static int pico_udp_process_out(struct pico_protocol *self, struct pico_frame *f)
 {
-	IGNORE_PARAMETER(self);
-	return pico_network_send(f);
+    IGNORE_PARAMETER(self);
+    return pico_network_send(f);
 }
 
 static int pico_udp_push(struct pico_protocol *self, struct pico_frame *f)
 {
-  struct pico_udp_hdr *hdr = (struct pico_udp_hdr *) f->transport_hdr;
-  struct pico_remote_duple *remote_duple = (struct pico_remote_duple *) f->info;
+    struct pico_udp_hdr *hdr = (struct pico_udp_hdr *) f->transport_hdr;
+    struct pico_remote_duple *remote_duple = (struct pico_remote_duple *) f->info;
 
-  /* this (fragmented) frame should contain a transport header */
-  if (f->transport_hdr != f->payload) {
-    hdr->trans.sport = f->sock->local_port;
-    if (remote_duple) {
-      hdr->trans.dport = remote_duple->remote_port;
-    } else {
-      hdr->trans.dport = f->sock->remote_port;
+    /* this (fragmented) frame should contain a transport header */
+    if (f->transport_hdr != f->payload) {
+        hdr->trans.sport = f->sock->local_port;
+        if (remote_duple) {
+            hdr->trans.dport = remote_duple->remote_port;
+        } else {
+            hdr->trans.dport = f->sock->remote_port;
+        }
+
+        hdr->len = short_be(f->transport_len);
+        /* do not perform CRC validation. If you want to, a system needs to be
+           implemented to calculate the CRC over the total payload of a
+           fragmented payload */
+        hdr->crc = 0;
     }
-    hdr->len = short_be(f->transport_len);
-    /* do not perform CRC validation. If you want to, a system needs to be 
-       implemented to calculate the CRC over the total payload of a 
-       fragmented payload */
-    hdr->crc = 0;
-  }
 
-  if (pico_enqueue(self->q_out, f) > 0) {
-    return f->payload_len;
-  } else {
-    return 0;
-  }	
+    if (pico_enqueue(self->q_out, f) > 0) {
+        return f->payload_len;
+    } else {
+        return 0;
+    }
 }
 
 /* Interface: protocol definition */
 struct pico_protocol pico_proto_udp = {
-  .name = "udp",
-  .proto_number = PICO_PROTO_UDP,
-  .layer = PICO_LAYER_TRANSPORT,
-  .process_in = pico_transport_process_in,
-  .process_out = pico_udp_process_out,
-  .push = pico_udp_push,
-  .q_in = &udp_in,
-  .q_out = &udp_out,
+    .name = "udp",
+    .proto_number = PICO_PROTO_UDP,
+    .layer = PICO_LAYER_TRANSPORT,
+    .process_in = pico_transport_process_in,
+    .process_out = pico_udp_process_out,
+    .push = pico_udp_push,
+    .q_in = &udp_in,
+    .q_out = &udp_out,
 };
 
 
@@ -100,80 +106,86 @@
 
 struct pico_socket_udp
 {
-  struct pico_socket sock;
-  int mode;
+    struct pico_socket sock;
+    int mode;
 #ifdef PICO_SUPPORT_MCAST
-  uint8_t mc_ttl; /* Multicasting TTL */
+    uint8_t mc_ttl; /* Multicasting TTL */
 #endif
 };
 
 #ifdef PICO_SUPPORT_MCAST
 int pico_udp_set_mc_ttl(struct pico_socket *s, uint8_t ttl)
 {
-  struct pico_socket_udp *u;
-  if(!s) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-  u = (struct pico_socket_udp *) s;
-  u->mc_ttl = ttl;
-  return 0;
+    struct pico_socket_udp *u;
+    if(!s) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
+
+    u = (struct pico_socket_udp *) s;
+    u->mc_ttl = ttl;
+    return 0;
 }
 
 int pico_udp_get_mc_ttl(struct pico_socket *s, uint8_t *ttl)
 {
-  struct pico_socket_udp *u;
-  if(!s)
-    return -1;
-  u = (struct pico_socket_udp *) s;
-  *ttl = u->mc_ttl;
-  return 0;
+    struct pico_socket_udp *u;
+    if(!s)
+        return -1;
+
+    u = (struct pico_socket_udp *) s;
+    *ttl = u->mc_ttl;
+    return 0;
 }
 #endif /* PICO_SUPPORT_MCAST */
 
 struct pico_socket *pico_udp_open(void)
 {
-  struct pico_socket_udp *u = pico_zalloc(sizeof(struct pico_socket_udp));
-  if (!u)
-    return NULL;
-  u->mode = PICO_UDP_MODE_UNICAST;
+    struct pico_socket_udp *u = pico_zalloc(sizeof(struct pico_socket_udp));
+    if (!u)
+        return NULL;
+
+    u->mode = PICO_UDP_MODE_UNICAST;
 
 #ifdef PICO_SUPPORT_MCAST
-  u->mc_ttl = PICO_IP_DEFAULT_MULTICAST_TTL;
-  /* enable multicast loopback by default */
-  u->sock.opt_flags |= (1 << PICO_SOCKET_OPT_MULTICAST_LOOP);
+    u->mc_ttl = PICO_IP_DEFAULT_MULTICAST_TTL;
+    /* enable multicast loopback by default */
+    u->sock.opt_flags |= (1 << PICO_SOCKET_OPT_MULTICAST_LOOP);
 #endif
 
-  return &u->sock;
+    return &u->sock;
 }
 
 uint16_t pico_udp_recv(struct pico_socket *s, void *buf, uint16_t len, void *src, uint16_t *port)
 {
-  struct pico_frame *f = pico_queue_peek(&s->q_in);
-  if (f) {
-		if(!f->payload_len){
-      f->payload = f->transport_hdr + sizeof(struct pico_udp_hdr);
-      f->payload_len = (uint16_t)(f->transport_len - sizeof(struct pico_udp_hdr));
-    }
-//    dbg("expected: %d, got: %d\n", len, f->payload_len);
-    if (src)
-      pico_store_network_origin(src, f);
-    if (port) {
-      struct pico_trans *hdr = (struct pico_trans *)f->transport_hdr;
-      *port = hdr->sport;
-    }
-    if (f->payload_len > len) {
-      memcpy(buf, f->payload, len);
-      f->payload += len;
-      f->payload_len = (uint16_t)(f->payload_len - len);
-      return len;
-    } else {
-      uint16_t ret = f->payload_len;
-      memcpy(buf, f->payload, f->payload_len);
-      f = pico_dequeue(&s->q_in);
-      pico_frame_discard(f);
-      return ret;
-    }
-  } else return 0;
+    struct pico_frame *f = pico_queue_peek(&s->q_in);
+    if (f) {
+        if(!f->payload_len) {
+            f->payload = f->transport_hdr + sizeof(struct pico_udp_hdr);
+            f->payload_len = (uint16_t)(f->transport_len - sizeof(struct pico_udp_hdr));
+        }
+
+/*    dbg("expected: %d, got: %d\n", len, f->payload_len); */
+        if (src)
+            pico_store_network_origin(src, f);
+
+        if (port) {
+            struct pico_trans *hdr = (struct pico_trans *)f->transport_hdr;
+            *port = hdr->sport;
+        }
+
+        if (f->payload_len > len) {
+            memcpy(buf, f->payload, len);
+            f->payload += len;
+            f->payload_len = (uint16_t)(f->payload_len - len);
+            return len;
+        } else {
+            uint16_t ret = f->payload_len;
+            memcpy(buf, f->payload, f->payload_len);
+            f = pico_dequeue(&s->q_in);
+            pico_frame_discard(f);
+            return ret;
+        }
+    } else return 0;
 }
 
--- a/modules/pico_udp.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_udp.h	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-*********************************************************************/
+ *********************************************************************/
 #ifndef _INCLUDE_PICO_UDP
 #define _INCLUDE_PICO_UDP
 #include "pico_addressing.h"
@@ -13,9 +13,9 @@
 extern struct pico_protocol pico_proto_udp;
 
 struct __attribute__((packed)) pico_udp_hdr {
-  struct pico_trans trans;
-  uint16_t len;
-  uint16_t crc;
+    struct pico_trans trans;
+    uint16_t len;
+    uint16_t crc;
 };
 #define PICO_UDPHDR_SIZE 8
 
@@ -29,13 +29,13 @@
 #else
 static inline int pico_udp_set_mc_ttl(struct pico_socket *s, uint8_t ttl)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 static inline int pico_udp_get_mc_ttl(struct pico_socket *s, uint8_t *ttl)
 {
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  return -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    return -1;
 }
 #endif /* PICO_SUPPORT_MCAST */
 
--- a/modules/pico_zmq.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_zmq.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,9 +1,9 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 #include "pico_stack.h"
 #include "pico_config.h"
@@ -13,219 +13,237 @@
 
 #define MY_VERSION 1u
 
- 
+
 enum zmq_state {
-  ST_OPEN = 0,
-  ST_CONNECTED,
-  ST_SIGNATURE,
-  ST_VERSION,
-  ST_GREETING,
-  ST_RDY,
-  ST_BUSY
+    ST_OPEN = 0,
+    ST_CONNECTED,
+    ST_SIGNATURE,
+    ST_VERSION,
+    ST_GREETING,
+    ST_RDY,
+    ST_BUSY
 };
 
 enum zmq_role {
-  ROLE_NONE = 0,
-  ROLE_PUBLISHER,
-  ROLE_SUBSCRIBER
+    ROLE_NONE = 0,
+    ROLE_PUBLISHER,
+    ROLE_SUBSCRIBER
 };
 
 struct __attribute__((packed)) zmq_msg {
-   uint8_t flags;
+    uint8_t flags;
     uint8_t len;
-    char    txt[];
+    char txt[];
 };
 
 struct zmq_socket;
 
 struct zmq_connector {
-  struct pico_socket *sock;
-  enum zmq_state state;
-  ZMQ parent;
-  enum zmq_role role;
-  uint8_t bytes_received;
-  struct zmq_connector *next;
+    struct pico_socket *sock;
+    enum zmq_state state;
+    ZMQ parent;
+    enum zmq_role role;
+    uint8_t bytes_received;
+    struct zmq_connector *next;
 };
 
 struct zmq_socket {
-  struct pico_socket *sock;
-  void (*ready)(ZMQ z);
-  enum zmq_state state;
-  struct zmq_connector *subs;
-  enum zmq_role role;
+    struct pico_socket *sock;
+    void (*ready)(ZMQ z);
+    enum zmq_state state;
+    struct zmq_connector *subs;
+    enum zmq_role role;
 };
 
 static int zmq_socket_cmp(void *ka, void *kb)
 {
-  ZMQ a = ka;
-  ZMQ b = kb;
-  if (a->sock < b->sock)
-    return -1;
-  if (b->sock < a->sock)
-    return 1;
-  return 0;
+    ZMQ a = ka;
+    ZMQ b = kb;
+    if (a->sock < b->sock)
+        return -1;
+
+    if (b->sock < a->sock)
+        return 1;
+
+    return 0;
 }
 PICO_TREE_DECLARE(zmq_sockets, zmq_socket_cmp);
 
 static inline ZMQ ZMTP(struct pico_socket *s)
 {
-  struct zmq_socket tst = { .sock = s };
-  return (pico_tree_findKey(&zmq_sockets, &tst));
+    struct zmq_socket tst = {
+        .sock = s
+    };
+    return (pico_tree_findKey(&zmq_sockets, &tst));
 }
 
 static inline struct zmq_connector *find_subscriber(struct pico_socket *s)
 {
-  ZMQ search;
-  struct pico_tree_node *idx;
-  struct zmq_connector *el;
-  pico_tree_foreach(idx, &zmq_sockets) {
-    search = idx->keyValue;
-    el = search->subs;
-    while(el) {
-      if (el->sock == s)
-        return el;
-      el = el->next;
+    ZMQ search;
+    struct pico_tree_node *idx;
+    struct zmq_connector *el;
+    pico_tree_foreach(idx, &zmq_sockets) {
+        search = idx->keyValue;
+        el = search->subs;
+        while(el) {
+            if (el->sock == s)
+                return el;
+
+            el = el->next;
+        }
     }
-  }
-  return NULL;
+    return NULL;
 }
 
 
 static void zmq_connector_add(ZMQ z, struct zmq_connector *zc)
 {
-  zc->next = z->subs;
-  z->subs = zc;
-  zc->parent = z;
-  dbg("Added connector %p, sock is %p\n", zc, zc->sock);
+    zc->next = z->subs;
+    z->subs = zc;
+    zc->parent = z;
+    dbg("Added connector %p, sock is %p\n", zc, zc->sock);
 }
 
 static void zmq_connector_del(struct zmq_connector *zc)
 {
-  ZMQ z = zc->parent;
-  if(z) {
-    struct zmq_connector *el = z->subs, *prev = NULL;      /* el = pointer to linked list */
-    while(el) {
-      if (el == zc) {               /* did we find the connector that we want to delete? */
-        if (prev)                   /* was there a previous list item? */
-          prev->next = zc->next;    /* link the linked list again */
-        else
-          z->subs = zc->next;       /* we were at the beginning of the list */
-        break;
-      }
-      prev = el;
-      el = el->next;
+    ZMQ z = zc->parent;
+    if(z) {
+        struct zmq_connector *el = z->subs, *prev = NULL;  /* el = pointer to linked list */
+        while(el) {
+            if (el == zc) {         /* did we find the connector that we want to delete? */
+                if (prev)           /* was there a previous list item? */
+                    prev->next = zc->next; /* link the linked list again */
+                else
+                    z->subs = zc->next; /* we were at the beginning of the list */
+
+                break;
+            }
+
+            prev = el;
+            el = el->next;
+        }
     }
-  }
-  pico_socket_close(zc->sock);
-  pico_free(zc);
+
+    pico_socket_close(zc->sock);
+    pico_free(zc);
 }
 
-static void zmq_check_state(ZMQ z) 
+static void zmq_check_state(ZMQ z)
 {
-  struct zmq_connector *c = z->subs;
-  enum zmq_state default_state, option_state;
-  if ((z->state != ST_RDY) && (z->state != ST_BUSY))
-    return;
-  if (z->role == ROLE_SUBSCRIBER) {
-    default_state = ST_RDY;
-    option_state = ST_BUSY;
-  } else {
-    default_state = ST_BUSY;
-    option_state = ST_RDY;
-  }
-  z->state = default_state;
-  while(c) {
-    if (c->state == option_state) {
-      z->state = option_state;
-      return;
+    struct zmq_connector *c = z->subs;
+    enum zmq_state default_state, option_state;
+    if ((z->state != ST_RDY) && (z->state != ST_BUSY))
+        return;
+
+    if (z->role == ROLE_SUBSCRIBER) {
+        default_state = ST_RDY;
+        option_state = ST_BUSY;
+    } else {
+        default_state = ST_BUSY;
+        option_state = ST_RDY;
     }
-    c = c->next;
-  }
+
+    z->state = default_state;
+    while(c) {
+        if (c->state == option_state) {
+            z->state = option_state;
+            return;
+        }
+
+        c = c->next;
+    }
 }
 
 
 static void zmq_hs_connected(struct zmq_connector *z)
 {
-  /* v2 signature */
-  uint8_t my_signature[14] =  {0xff, 0, 0, 0, 0, 0, 0, 0, 1, 0x7f, 1, 1, 0, 0};
+    /* v2 signature */
+    uint8_t my_signature[14] =  {
+        0xff, 0, 0, 0, 0, 0, 0, 0, 1, 0x7f, 1, 1, 0, 0
+    };
 
-//  uint8_t my_ver[2] = {MY_VERSION, 0};
-//  uint8_t my_greeting[52] = {'N','U','L','L', 0};
+/*  uint8_t my_ver[2] = {MY_VERSION, 0}; */
+/*  uint8_t my_greeting[52] = {'N','U','L','L', 0}; */
 
-  pico_socket_write(z->sock, my_signature, 14);
-//  pico_socket_write(z->sock, my_ver, 2);
+    pico_socket_write(z->sock, my_signature, 14);
+/*  pico_socket_write(z->sock, my_ver, 2); */
 
-//  if (MY_VERSION > 2)
-//    pico_socket_write(z->sock, my_greeting, 52);
+/*  if (MY_VERSION > 2) */
+/*    pico_socket_write(z->sock, my_greeting, 52); */
 
-  z->state = ST_SIGNATURE;
-//  z->state = ST_RDY;
+    z->state = ST_SIGNATURE;
+/*  z->state = ST_RDY; */
 }
- 
+
 static void zmq_hs_signature(struct zmq_connector *zc)
 {
-  uint8_t incoming[20];
-  int ret;
-  
-  ret = pico_socket_read(zc->sock, incoming, 14);
-  if (zc->bytes_received == 0 && ret > 0 &&  incoming[0] != 0xFF) {
-    //dbg("Received invalid signature: [0]!=0xFF\n");
-    zmq_connector_del(zc);
-  }
-  zc->bytes_received = (uint8_t)(zc->bytes_received + ret);
-  if (zc->bytes_received < 14) {
-    //dbg("Waiting for the rest of the sig - got %u bytes\n",zc->bytes_received);
-    return;
-  }
+    uint8_t incoming[20];
+    int ret;
+
+    ret = pico_socket_read(zc->sock, incoming, 14);
+    if (zc->bytes_received == 0 && ret > 0 &&  incoming[0] != 0xFF) {
+        /* dbg("Received invalid signature: [0]!=0xFF\n"); */
+        zmq_connector_del(zc);
+    }
 
-  //dbg("Valid signature received. len = %d, first byte: %02x\n", ret, incoming[0]);
-  zc->state = ST_RDY;
+    zc->bytes_received = (uint8_t)(zc->bytes_received + ret);
+    if (zc->bytes_received < 14) {
+        /* dbg("Waiting for the rest of the sig - got %u bytes\n",zc->bytes_received); */
+        return;
+    }
+
+    /* dbg("Valid signature received. len = %d, first byte: %02x\n", ret, incoming[0]); */
+    zc->state = ST_RDY;
 }
- 
+
 static void zmq_hs_version(struct zmq_connector *zc)
 {
-  uint8_t incoming[20];
-  int ret;
-  ret = pico_socket_read(zc->sock, incoming, 2);
-  if (ret < 0) {
-    dbg("Cannot exchange valid version information. Read returned -1\n");
-    zmq_connector_del(zc);
-    return;
-  }
-  if (ret == 0)
-     return;
-/* Version check?    
-  if (incoming[0] != 3) {
+    uint8_t incoming[20];
+    int ret;
+    ret = pico_socket_read(zc->sock, incoming, 2);
+    if (ret < 0) {
+        dbg("Cannot exchange valid version information. Read returned -1\n");
+        zmq_connector_del(zc);
+        return;
+    }
+
+    if (ret == 0)
+        return;
+
+/* Version check?
+   if (incoming[0] != 3) {
     dbg("Version %d.x not supported by this publisher\n", incoming[0]);
     zmq_connector_del(zc);
     return;
-  }
-  dbg("Subscriber is using version 3. Good!\n");
-*/
-  dbg("Subscriber is using version %d. Good!\n", incoming[0]);
-  if (incoming[0] == 3)
-    zc->state = ST_GREETING;
-  else
-    zc->state = ST_RDY;
+   }
+   dbg("Subscriber is using version 3. Good!\n");
+ */
+    dbg("Subscriber is using version %d. Good!\n", incoming[0]);
+    if (incoming[0] == 3)
+        zc->state = ST_GREETING;
+    else
+        zc->state = ST_RDY;
 }
- 
+
 static void zmq_hs_greeting(struct zmq_connector *zc)
 {
-  uint8_t incoming[64];
-  int ret;
-  ret = pico_socket_read(zc->sock, incoming, 64);
-  dbg("zmq_socket_read in greeting returned %d\n", ret);    
-  if (ret == 0)
-   return;  
-  if (ret < 0) {
-    dbg("Cannot retrieve valid greeting\n");
-    zmq_connector_del(zc);
-    return;
-  }
-  zc->state = ST_RDY;
-  zmq_check_state(zc->parent);
-  dbg("Paired. Sending Ready.\n");
-  pico_socket_write(zc->sock, "READY   ",8);
+    uint8_t incoming[64];
+    int ret;
+    ret = pico_socket_read(zc->sock, incoming, 64);
+    dbg("zmq_socket_read in greeting returned %d\n", ret);
+    if (ret == 0)
+        return;
+
+    if (ret < 0) {
+        dbg("Cannot retrieve valid greeting\n");
+        zmq_connector_del(zc);
+        return;
+    }
+
+    zc->state = ST_RDY;
+    zmq_check_state(zc->parent);
+    dbg("Paired. Sending Ready.\n");
+    pico_socket_write(zc->sock, "READY   ", 8);
 }
 
 static void zmq_hs_rdy(struct zmq_connector *zc)
@@ -233,22 +251,24 @@
     int ret;
     uint8_t incoming[258];
     if (zc->role == ROLE_SUBSCRIBER)
-      return;
+        return;
+
     ret = pico_socket_read(zc->sock, incoming, 258);
     dbg("Got %d bytes from subscriber whilst in rdy state.\n", ret);
 }
 
 static void zmq_hs_busy(struct zmq_connector *zc)
 {
-  int was_busy = 0;
-  if (zc->parent->state == ST_BUSY)
-    was_busy = 1;
-  zmq_check_state(zc->parent);
-  if (was_busy && (zc->parent->state == ST_RDY) && zc->parent->ready)
-    zc->parent->ready(zc->parent);
+    int was_busy = 0;
+    if (zc->parent->state == ST_BUSY)
+        was_busy = 1;
+
+    zmq_check_state(zc->parent);
+    if (was_busy && (zc->parent->state == ST_RDY) && zc->parent->ready)
+        zc->parent->ready(zc->parent);
 }
- 
-static void(*zmq_hs_cb[])(struct zmq_connector *) = {
+
+static void (*zmq_hs_cb[])(struct zmq_connector *) = {
     NULL,
     zmq_hs_connected,
     zmq_hs_signature,
@@ -261,157 +281,169 @@
 
 static void cb_tcp0mq(uint16_t ev, struct pico_socket *s)
 {
-  struct pico_ip4 orig;
-  uint16_t port;
-  char peer[30];
-  struct zmq_connector *z_a, *zc;
-  ZMQ z = ZMTP(s);
-  
-  /* Publisher. Accepting new subscribers */
-  if (z) {
-    if (ev & PICO_SOCK_EV_CONN) { 
-      z_a = pico_zalloc(sizeof(struct zmq_socket));
-      if (z_a == NULL)
+    struct pico_ip4 orig;
+    uint16_t port;
+    char peer[30];
+    struct zmq_connector *z_a, *zc;
+    ZMQ z = ZMTP(s);
+
+    /* Publisher. Accepting new subscribers */
+    if (z) {
+        if (ev & PICO_SOCK_EV_CONN) {
+            z_a = pico_zalloc(sizeof(struct zmq_socket));
+            if (z_a == NULL)
+                return;
+
+            z_a->sock = pico_socket_accept(s, &orig, &port);
+            pico_ipv4_to_string(peer, orig.addr);
+            dbg("tcp0mq> Connection requested by %s:%u.\n", peer, short_be(port));
+            if (z->state == ST_OPEN) {
+                dbg("tcp0mq> Accepted connection! New subscriber on sock %p.\n", z_a->sock);
+                zmq_connector_add(z, z_a);
+                z_a->role = ROLE_PUBLISHER;
+                z_a->state = ST_CONNECTED;
+                zmq_hs_connected(z_a);
+            } else {
+                dbg("tcp0mq> Server busy, connection rejected\n");
+                pico_socket_close(z_a->sock);
+            }
+        }
+
         return;
-      
-      z_a->sock = pico_socket_accept(s, &orig, &port);
-      pico_ipv4_to_string(peer, orig.addr);
-      dbg("tcp0mq> Connection requested by %s:%u.\n", peer, short_be(port));
-      if (z->state == ST_OPEN) {
-          dbg("tcp0mq> Accepted connection! New subscriber on sock %p.\n",z_a->sock);
-          zmq_connector_add(z, z_a);
-          z_a->role = ROLE_PUBLISHER;
-          z_a->state = ST_CONNECTED;
-          zmq_hs_connected(z_a);
-      } else {
-          dbg("tcp0mq> Server busy, connection rejected\n");
-          pico_socket_close(z_a->sock);
-      }
     }
-    return;
-  }
 
-  zc = find_subscriber(s);
-  if (!zc) {
-    dbg("Cannot find subscriber with socket %p, ev = %d!\n", s, ev);
-//    pico_socket_close(s);
-    return;
-  }
+    zc = find_subscriber(s);
+    if (!zc) {
+        dbg("Cannot find subscriber with socket %p, ev = %d!\n", s, ev);
+/*    pico_socket_close(s); */
+        return;
+    }
 
-  if ((ev & PICO_SOCK_EV_CONN) && zc->role == ROLE_SUBSCRIBER && zc->state == ST_OPEN)
-  {
-     zc->state = ST_CONNECTED;
-     zmq_hs_connected(zc);
-  }
+    if ((ev & PICO_SOCK_EV_CONN) && zc->role == ROLE_SUBSCRIBER && zc->state == ST_OPEN)
+    {
+        zc->state = ST_CONNECTED;
+        zmq_hs_connected(zc);
+    }
 
 
-  if (ev & PICO_SOCK_EV_RD) {
-    if (zmq_hs_cb[zc->state])
-      zmq_hs_cb[zc->state](zc);
-  }
+    if (ev & PICO_SOCK_EV_RD) {
+        if (zmq_hs_cb[zc->state])
+            zmq_hs_cb[zc->state](zc);
+    }
+
+    if ((ev & PICO_SOCK_EV_WR) && zc->parent && (zc->parent->role == ROLE_PUBLISHER) && (zc->state == ST_BUSY)) {
+        if (zmq_hs_cb[zc->state])
+            zmq_hs_cb[zc->state](zc);
+    }
+
 
-  if ((ev & PICO_SOCK_EV_WR) && zc->parent && (zc->parent->role == ROLE_PUBLISHER) && (zc->state == ST_BUSY)) {
-    if (zmq_hs_cb[zc->state])
-      zmq_hs_cb[zc->state](zc);
-  }
- 
- 
-  if (ev & PICO_SOCK_EV_FIN) {
-    dbg("tcp0mq> Connection closed.\n");
-    zmq_connector_del(zc);
-  }
- 
-  if (ev & PICO_SOCK_EV_ERR) {
-    dbg("tcp0mq> Socket Error received: %s. Bailing out.\n", strerror(pico_err));
-    zmq_connector_del(zc);
-  }
- 
-  if (ev & PICO_SOCK_EV_CLOSE) {
-    dbg("tcp0mq> event close\n");
-    zmq_connector_del(zc);
-  }
- 
+    if (ev & PICO_SOCK_EV_FIN) {
+        dbg("tcp0mq> Connection closed.\n");
+        zmq_connector_del(zc);
+    }
+
+    if (ev & PICO_SOCK_EV_ERR) {
+        dbg("tcp0mq> Socket Error received: %s. Bailing out.\n", strerror(pico_err));
+        zmq_connector_del(zc);
+    }
+
+    if (ev & PICO_SOCK_EV_CLOSE) {
+        dbg("tcp0mq> event close\n");
+        zmq_connector_del(zc);
+    }
+
 }
 
 ZMQ zmq_subscriber(void (*cb)(ZMQ z))
 {
-  ZMQ z = pico_zalloc(sizeof(struct zmq_socket));
-  if (!z) {
-    pico_err = PICO_ERR_ENOMEM;
-    return NULL;
-  }
-  z->state = ST_BUSY;
-  z->ready = cb;
-  z->role = ROLE_SUBSCRIBER;
-  pico_tree_insert(&zmq_sockets, z);
-  return z;
+    ZMQ z = pico_zalloc(sizeof(struct zmq_socket));
+    if (!z) {
+        pico_err = PICO_ERR_ENOMEM;
+        return NULL;
+    }
+
+    z->state = ST_BUSY;
+    z->ready = cb;
+    z->role = ROLE_SUBSCRIBER;
+    pico_tree_insert(&zmq_sockets, z);
+    return z;
 }
 
-int zmq_connect(ZMQ z, char *address, uint16_t port) 
+int zmq_connect(ZMQ z, char *address, uint16_t port)
 {
-  struct pico_ip4 ip = {0};
-  struct zmq_connector *z_c;
-  uint8_t sockopts = 1;
-  if (pico_string_to_ipv4(address, &ip.addr) < 0) {
-    dbg("FIXME!! I need to synchronize with the dns client to get to my publisher :(\n");
-    return -1;
-  }
+    struct pico_ip4 ip = {
+        0
+    };
+    struct zmq_connector *z_c;
+    uint8_t sockopts = 1;
+    if (pico_string_to_ipv4(address, &ip.addr) < 0) {
+        dbg("FIXME!! I need to synchronize with the dns client to get to my publisher :(\n");
+        return -1;
+    }
 
-  z_c = pico_zalloc(sizeof(struct zmq_connector));
-  if (!z_c)
-    return -1;
-  z_c->role = ROLE_SUBSCRIBER;
-  z_c->state = ST_OPEN;
-  z_c->sock = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &cb_tcp0mq);
-  if (!z_c->sock) {
-    pico_free(z_c);
-    return -1;
-  }
-  pico_socket_setoption(z_c->sock, PICO_TCP_NODELAY, &sockopts);
-  if (pico_socket_connect(z_c->sock, &ip, short_be(port)) < 0)
-    return -1;
-  zmq_connector_add(z, z_c);
-  return 0;
+    z_c = pico_zalloc(sizeof(struct zmq_connector));
+    if (!z_c)
+        return -1;
+
+    z_c->role = ROLE_SUBSCRIBER;
+    z_c->state = ST_OPEN;
+    z_c->sock = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &cb_tcp0mq);
+    if (!z_c->sock) {
+        pico_free(z_c);
+        return -1;
+    }
+
+    pico_socket_setoption(z_c->sock, PICO_TCP_NODELAY, &sockopts);
+    if (pico_socket_connect(z_c->sock, &ip, short_be(port)) < 0)
+        return -1;
+
+    zmq_connector_add(z, z_c);
+    return 0;
 }
 
 ZMQ zmq_publisher(uint16_t _port, void (*cb)(ZMQ z))
 {
-  struct pico_socket *s;
-  struct pico_ip4 inaddr_any = {0};
-  uint8_t sockopts = 1;
-  uint16_t port = short_be(_port);
-  ZMQ z = NULL;
-  s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &cb_tcp0mq);
-  if (!s)
-    return NULL;
- 
-  pico_socket_setoption(s, PICO_TCP_NODELAY, &sockopts);
+    struct pico_socket *s;
+    struct pico_ip4 inaddr_any = {
+        0
+    };
+    uint8_t sockopts = 1;
+    uint16_t port = short_be(_port);
+    ZMQ z = NULL;
+    s = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, &cb_tcp0mq);
+    if (!s)
+        return NULL;
+
+    pico_socket_setoption(s, PICO_TCP_NODELAY, &sockopts);
+
+    dbg("zmq_publisher: BIND\n");
+    if (pico_socket_bind(s, &inaddr_any, &port) != 0) {
+        dbg("zmq publisher: BIND failed\n");
+        return NULL;
+    }
 
-  dbg("zmq_publisher: BIND\n");
-  if (pico_socket_bind(s, &inaddr_any, &port)!= 0) {
-    dbg("zmq publisher: BIND failed\n");
-    return NULL;
-  }
-  if (pico_socket_listen(s, 2) != 0) {
-    dbg("zmq publisher: LISTEN failed\n");
-    return NULL;
-  }
-  dbg("zmq_publisher: Active and bound to local port %d\n", short_be(port));
+    if (pico_socket_listen(s, 2) != 0) {
+        dbg("zmq publisher: LISTEN failed\n");
+        return NULL;
+    }
+
+    dbg("zmq_publisher: Active and bound to local port %d\n", short_be(port));
 
-  z = pico_zalloc(sizeof(struct zmq_socket));
-  if (!z) {
-    pico_socket_close(s);
-    pico_err = PICO_ERR_ENOMEM;
-    return NULL;
-  }
-  z->sock = s;
-  z->state = ST_OPEN;
-  z->ready = cb;
-  z->role = ROLE_PUBLISHER;
-  z->subs = NULL;
-  pico_tree_insert(&zmq_sockets, z);
-  dbg("zmq publisher created.\n");
-  return z;
+    z = pico_zalloc(sizeof(struct zmq_socket));
+    if (!z) {
+        pico_socket_close(s);
+        pico_err = PICO_ERR_ENOMEM;
+        return NULL;
+    }
+
+    z->sock = s;
+    z->state = ST_OPEN;
+    z->ready = cb;
+    z->role = ROLE_PUBLISHER;
+    z->subs = NULL;
+    pico_tree_insert(&zmq_sockets, z);
+    dbg("zmq publisher created.\n");
+    return z;
 }
 
 int zmq_send(ZMQ z, char *txt, int len)
@@ -420,21 +452,23 @@
     struct zmq_connector *c = z->subs;
     int ret = 0;
 
-    if (!c) 
+    if (!c)
     {
         dbg("no subscribers, bailing out\n");
         return 0; /* Need at least one subscriber */
     }
+
     msg = pico_zalloc((size_t)(len + 2));
     msg->flags = 4;
     msg->len = (uint8_t) len;
-    memcpy(msg->txt, txt,(size_t) len);
+    memcpy(msg->txt, txt, (size_t) len);
 
     while (c) {
-      dbg("write to %u\n",c->state);
-      if ((ST_RDY == c->state) && (pico_socket_write(c->sock, msg, len + 2) > 0))
-        ret++;
-      c = c->next;
+        dbg("write to %u\n", c->state);
+        if ((ST_RDY == c->state) && (pico_socket_write(c->sock, msg, len + 2) > 0))
+            ret++;
+
+        c = c->next;
     }
     pico_free(msg);
     return ret;
@@ -442,36 +476,38 @@
 
 int zmq_recv(ZMQ z, char *txt)
 {
-  int ret;
-  struct zmq_msg msg;
-  struct zmq_connector *nxt, *c = z->subs;
-  if (z->state != ST_RDY)
+    int ret;
+    struct zmq_msg msg;
+    struct zmq_connector *nxt, *c = z->subs;
+    if (z->state != ST_RDY)
+        return 0;
+
+    while (c) {
+        nxt = c->next;
+        ret = pico_socket_read(c->sock, &msg, 2);
+        if (ret < 0) {
+            dbg("Error reading!\n");
+            zmq_connector_del(c);
+        } else if (ret < 2) {
+            c->state = ST_BUSY;
+        } else {
+            return pico_socket_read(c->sock, txt, msg.len);
+        }
+
+        c = nxt;
+    }
+    zmq_check_state(z);
     return 0;
-  while (c) {
-    nxt = c->next;
-    ret = pico_socket_read(c->sock, &msg, 2);
-    if (ret < 0) {
-      dbg("Error reading!\n");
-      zmq_connector_del(c);
-    } else if (ret < 2) {
-      c->state = ST_BUSY;
-    } else {
-      return pico_socket_read(c->sock, txt, msg.len);
-    }
-    c = nxt;
-  }
-  zmq_check_state(z);
-  return 0;
 }
 
 void zmq_close(ZMQ z)
 {
-  struct zmq_connector *nxt, *c = z->subs;
-  while(c) {
-    nxt = c->next;
-    zmq_connector_del(c);
-    c = nxt;
-  }
-  pico_socket_close(z->sock);
-  pico_free(z); 
+    struct zmq_connector *nxt, *c = z->subs;
+    while(c) {
+        nxt = c->next;
+        zmq_connector_del(c);
+        c = nxt;
+    }
+    pico_socket_close(z->sock);
+    pico_free(z);
 }
--- a/modules/pico_zmq.h	Wed Dec 11 07:20:17 2013 +0000
+++ b/modules/pico_zmq.h	Mon Dec 16 11:25:54 2013 +0100
@@ -2,7 +2,7 @@
 #define __PICO_ZMQ_H
 
 struct zmq_socket;
-typedef struct zmq_socket *ZMQ; 
+typedef struct zmq_socket *ZMQ;
 
 ZMQ zmq_publisher(uint16_t _port, void (*cb)(ZMQ z));
 ZMQ zmq_subscriber(void (*cb)(ZMQ z));
--- a/stack/pico_arp.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/stack/pico_arp.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 
 #include "pico_config.h"
@@ -15,14 +15,16 @@
 #include "pico_device.h"
 #include "pico_stack.h"
 
-const uint8_t PICO_ETHADDR_ALL[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
+const uint8_t PICO_ETHADDR_ALL[6] = {
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff
+};
 #define PICO_ARP_TIMEOUT 600000
 #define PICO_ARP_RETRY 300
 
 #ifdef DEBUG_ARP
-	#define arp_dbg dbg
+    #define arp_dbg dbg
 #else
-	#define arp_dbg(...) do{}while(0)
+    #define arp_dbg(...) do {} while(0)
 #endif
 
 static struct pico_queue pending;
@@ -30,38 +32,40 @@
 
 void check_pending(pico_time now, void *_unused)
 {
-  struct pico_frame *f = pico_dequeue(&pending);
-  IGNORE_PARAMETER(now);
-  IGNORE_PARAMETER(_unused);
-  if (!f) {
-    pending_timer_on = 0;
-    return;
-  }
-  if(pico_ethernet_send(f) > 0)
-    pico_frame_discard(f);
-  pico_timer_add(PICO_ARP_RETRY, &check_pending, NULL);
+    struct pico_frame *f = pico_dequeue(&pending);
+    IGNORE_PARAMETER(now);
+    IGNORE_PARAMETER(_unused);
+    if (!f) {
+        pending_timer_on = 0;
+        return;
+    }
+
+    if(pico_ethernet_send(f) > 0)
+        pico_frame_discard(f);
+
+    pico_timer_add(PICO_ARP_RETRY, &check_pending, NULL);
 }
 
 
 struct
-__attribute__ ((__packed__)) 
+__attribute__ ((__packed__))
 pico_arp_hdr
 {
-  uint16_t htype;
-  uint16_t ptype;
-  uint8_t hsize;
-  uint8_t psize;
-  uint16_t opcode;
-  uint8_t s_mac[PICO_SIZE_ETH];
-  struct pico_ip4 src;
-  uint8_t d_mac[PICO_SIZE_ETH];
-  struct pico_ip4 dst;
+    uint16_t htype;
+    uint16_t ptype;
+    uint8_t hsize;
+    uint8_t psize;
+    uint16_t opcode;
+    uint8_t s_mac[PICO_SIZE_ETH];
+    struct pico_ip4 src;
+    uint8_t d_mac[PICO_SIZE_ETH];
+    struct pico_ip4 dst;
 };
 
-struct arp_service_ipconflict{
-	struct pico_eth mac;
-	struct pico_ip4 ip;
-	void (*conflict)(void);
+struct arp_service_ipconflict {
+    struct pico_eth mac;
+    struct pico_ip4 ip;
+    void (*conflict)(void);
 };
 
 static struct arp_service_ipconflict conflict_ipv4;
@@ -70,13 +74,13 @@
 
 /* Arp Entries for the tables. */
 struct pico_arp {
-/* CAREFUL MAN! ARP entry MUST begin with a pico_eth structure, 
+/* CAREFUL MAN! ARP entry MUST begin with a pico_eth structure,
  * due to in-place casting!!! */
-  struct pico_eth eth;
-  struct pico_ip4 ipv4;
-  int    arp_status;
-  pico_time timestamp;
-  struct pico_device *dev;
+    struct pico_eth eth;
+    struct pico_ip4 ipv4;
+    int arp_status;
+    pico_time timestamp;
+    struct pico_device *dev;
 };
 
 
@@ -87,14 +91,15 @@
 
 /* Routing destination */
 
-static int arp_compare(void * ka, void * kb)
+static int arp_compare(void *ka, void *kb)
 {
-	struct pico_arp *a = ka, *b = kb;
-  if (a->ipv4.addr < b->ipv4.addr)
-    return -1;
-  else if (a->ipv4.addr > b->ipv4.addr)
-    return 1;
-  return 0;
+    struct pico_arp *a = ka, *b = kb;
+    if (a->ipv4.addr < b->ipv4.addr)
+        return -1;
+    else if (a->ipv4.addr > b->ipv4.addr)
+        return 1;
+
+    return 0;
 }
 
 PICO_TREE_DECLARE(arp_tree, arp_compare);
@@ -105,87 +110,91 @@
 
 struct pico_eth *pico_arp_lookup(struct pico_ip4 *dst)
 {
-  struct pico_arp search, *found;
-  search.ipv4.addr = dst->addr;
-  found = pico_tree_findKey(&arp_tree,&search);
-  if (found && (found->arp_status != PICO_ARP_STATUS_STALE))
-    return &found->eth;
-  return NULL;
+    struct pico_arp search, *found;
+    search.ipv4.addr = dst->addr;
+    found = pico_tree_findKey(&arp_tree, &search);
+    if (found && (found->arp_status != PICO_ARP_STATUS_STALE))
+        return &found->eth;
+
+    return NULL;
 }
 
 struct pico_ip4 *pico_arp_reverse_lookup(struct pico_eth *dst)
 {
-  struct pico_arp* search;
-  struct pico_tree_node * index;
-  pico_tree_foreach(index,&arp_tree){
-  	search = index->keyValue;
-    if(memcmp(&(search->eth.addr), &dst->addr, 6) == 0)
-      return &search->ipv4;
-  }
-  return NULL;
+    struct pico_arp*search;
+    struct pico_tree_node *index;
+    pico_tree_foreach(index, &arp_tree){
+        search = index->keyValue;
+        if(memcmp(&(search->eth.addr), &dst->addr, 6) == 0)
+            return &search->ipv4;
+    }
+    return NULL;
 }
 
-struct pico_eth *pico_arp_get(struct pico_frame *f) {
-  struct pico_eth *a4;
-  struct pico_ip4 gateway;
-  struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-  struct pico_ipv4_link *l;
+struct pico_eth *pico_arp_get(struct pico_frame *f)
+{
+    struct pico_eth *a4;
+    struct pico_ip4 gateway;
+    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    struct pico_ipv4_link *l;
 
-  l = pico_ipv4_link_get(&hdr->dst);
-  if(l){
-    //address belongs to ourself
-    return &l->dev->eth->mac;
-  }
+    l = pico_ipv4_link_get(&hdr->dst);
+    if(l) {
+        /* address belongs to ourself */
+        return &l->dev->eth->mac;
+    }
+
+    gateway = pico_ipv4_route_get_gateway(&hdr->dst);
+    /* check if dst is local (gateway = 0), or if to use gateway */
+    if (gateway.addr != 0)
+        a4 = pico_arp_lookup(&gateway);      /* check if gateway ip mac in cache */
+    else
+        a4 = pico_arp_lookup(&hdr->dst);     /* check if local ip mac in cache */
 
-  gateway = pico_ipv4_route_get_gateway(&hdr->dst);
-  /* check if dst is local (gateway = 0), or if to use gateway */
-  if (gateway.addr != 0)
-    a4 = pico_arp_lookup(&gateway);          /* check if gateway ip mac in cache */
-  else
-    a4 = pico_arp_lookup(&hdr->dst);         /* check if local ip mac in cache */
-  if (!a4) {
-     if (++f->failure_count < 4) {
-       dbg ("================= ARP REQUIRED: %d =============\n\n", f->failure_count);
-       /* check if dst is local (gateway = 0), or if to use gateway */
-       if (gateway.addr != 0)
-    	 pico_arp_request(f->dev, &gateway, PICO_ARP_QUERY);  /* arp to gateway */
-       else
-    	 pico_arp_request(f->dev, &hdr->dst, PICO_ARP_QUERY); /* arp to dst */
+    if (!a4) {
+        if (++f->failure_count < 4) {
+            dbg ("================= ARP REQUIRED: %d =============\n\n", f->failure_count);
+            /* check if dst is local (gateway = 0), or if to use gateway */
+            if (gateway.addr != 0)
+                pico_arp_request(f->dev, &gateway, PICO_ARP_QUERY); /* arp to gateway */
+            else
+                pico_arp_request(f->dev, &hdr->dst, PICO_ARP_QUERY); /* arp to dst */
 
-       pico_enqueue(&pending, f);
-       if (!pending_timer_on) {
-        pending_timer_on++;
-        pico_timer_add(PICO_ARP_RETRY, &check_pending, NULL);
-       }
-     } else {
-      dbg("ARP: Destination Unreachable\n");
-      pico_notify_dest_unreachable(f);
-      pico_frame_discard(f);
+            pico_enqueue(&pending, f);
+            if (!pending_timer_on) {
+                pending_timer_on++;
+                pico_timer_add(PICO_ARP_RETRY, &check_pending, NULL);
+            }
+        } else {
+            dbg("ARP: Destination Unreachable\n");
+            pico_notify_dest_unreachable(f);
+            pico_frame_discard(f);
+        }
     }
-  }
-  return a4;
+
+    return a4;
 }
 
 #ifdef DEBUG_ARP
 void dbg_arp(void)
 {
-  struct pico_arp *a;
-  struct pico_tree_node * index;
+    struct pico_arp *a;
+    struct pico_tree_node *index;
 
-  pico_tree_foreach(index,&arp_tree) {
-  	a = index->keyValue;
-    arp_dbg("ARP to  %08x, mac: %02x:%02x:%02x:%02x:%02x:%02x\n", a->ipv4.addr,a->eth.addr[0],a->eth.addr[1],a->eth.addr[2],a->eth.addr[3],a->eth.addr[4],a->eth.addr[5] );
-  }
+    pico_tree_foreach(index, &arp_tree) {
+        a = index->keyValue;
+        arp_dbg("ARP to  %08x, mac: %02x:%02x:%02x:%02x:%02x:%02x\n", a->ipv4.addr, a->eth.addr[0], a->eth.addr[1], a->eth.addr[2], a->eth.addr[3], a->eth.addr[4], a->eth.addr[5] );
+    }
 }
 #endif
 
 void arp_expire(pico_time now, void *_stale)
 {
-  struct pico_arp *stale = (struct pico_arp *) _stale;
-  IGNORE_PARAMETER(now);
-  stale->arp_status = PICO_ARP_STATUS_STALE;
-  arp_dbg("ARP: Setting arp_status to STALE\n");
-  pico_arp_request(stale->dev, &stale->ipv4, PICO_ARP_QUERY);
+    struct pico_arp *stale = (struct pico_arp *) _stale;
+    IGNORE_PARAMETER(now);
+    stale->arp_status = PICO_ARP_STATUS_STALE;
+    arp_dbg("ARP: Setting arp_status to STALE\n");
+    pico_arp_request(stale->dev, &stale->ipv4, PICO_ARP_QUERY);
 
 }
 
@@ -194,179 +203,182 @@
     entry->arp_status = PICO_ARP_STATUS_REACHABLE;
     entry->timestamp  = PICO_TIME();
 
-    pico_tree_insert(&arp_tree,entry);
+    pico_tree_insert(&arp_tree, entry);
     arp_dbg("ARP ## reachable.\n");
     pico_timer_add(PICO_ARP_TIMEOUT, arp_expire, entry);
 }
 
-int pico_arp_create_entry(uint8_t* hwaddr, struct pico_ip4 ipv4, struct pico_device* dev)
+int pico_arp_create_entry(uint8_t*hwaddr, struct pico_ip4 ipv4, struct pico_device*dev)
 {
-	struct pico_arp* arp = pico_zalloc(sizeof(struct pico_arp));
-	if(!arp){
-		pico_err = PICO_ERR_ENOMEM;
-		return -1;
-	}
-	memcpy(arp->eth.addr, hwaddr, 6);
-	arp->ipv4.addr = ipv4.addr;
-	arp->dev = dev;
+    struct pico_arp*arp = pico_zalloc(sizeof(struct pico_arp));
+    if(!arp) {
+        pico_err = PICO_ERR_ENOMEM;
+        return -1;
+    }
 
-	pico_arp_add_entry(arp);
+    memcpy(arp->eth.addr, hwaddr, 6);
+    arp->ipv4.addr = ipv4.addr;
+    arp->dev = dev;
 
-	return 0;
+    pico_arp_add_entry(arp);
+
+    return 0;
 }
 
 int pico_arp_receive(struct pico_frame *f)
 {
-  struct pico_arp_hdr *hdr;
-  struct pico_arp search, *found, *new = NULL;
-  int ret = -1;
-  hdr = (struct pico_arp_hdr *) f->net_hdr;
+    struct pico_arp_hdr *hdr;
+    struct pico_arp search, *found, *new = NULL;
+    int ret = -1;
+    hdr = (struct pico_arp_hdr *) f->net_hdr;
 
-  if (!hdr)
-    goto end;
+    if (!hdr)
+        goto end;
 
-  if (conflict_ipv4.conflict != NULL)
-  {
-    if ((conflict_ipv4.ip.addr == hdr->src.addr) && (memcmp(hdr->s_mac,conflict_ipv4.mac.addr,6) != 0))
-      conflict_ipv4.conflict();
-  }
+    if (conflict_ipv4.conflict != NULL)
+    {
+        if ((conflict_ipv4.ip.addr == hdr->src.addr) && (memcmp(hdr->s_mac, conflict_ipv4.mac.addr, 6) != 0))
+            conflict_ipv4.conflict();
+    }
 
-  /* Populate a new arp entry */
-  search.ipv4.addr = hdr->src.addr;
-  memcpy(search.eth.addr, hdr->s_mac, PICO_SIZE_ETH);
+    /* Populate a new arp entry */
+    search.ipv4.addr = hdr->src.addr;
+    memcpy(search.eth.addr, hdr->s_mac, PICO_SIZE_ETH);
 
-  /* Search for already existing entry */
+    /* Search for already existing entry */
 
-  found = pico_tree_findKey(&arp_tree,&search);
-  if (!found) {
-    new = pico_zalloc(sizeof(struct pico_arp));
-    if (!new)
-      goto end;
-    new->ipv4.addr = hdr->src.addr;
-  }
-  else if (found->arp_status == PICO_ARP_STATUS_STALE) {
-    /* Replace if stale */
-    new = found;
+    found = pico_tree_findKey(&arp_tree, &search);
+    if (!found) {
+        new = pico_zalloc(sizeof(struct pico_arp));
+        if (!new)
+            goto end;
+
+        new->ipv4.addr = hdr->src.addr;
+    }
+    else if (found->arp_status == PICO_ARP_STATUS_STALE) {
+        /* Replace if stale */
+        new = found;
 
-    pico_tree_delete(&arp_tree,new);
-  }
+        pico_tree_delete(&arp_tree, new);
+    }
 
-  ret = 0;
+    ret = 0;
 
-  if (new) {
-    memcpy(new->eth.addr, hdr->s_mac, PICO_SIZE_ETH);
-    new->dev = f->dev;
-    pico_arp_add_entry(new);
-  }
+    if (new) {
+        memcpy(new->eth.addr, hdr->s_mac, PICO_SIZE_ETH);
+        new->dev = f->dev;
+        pico_arp_add_entry(new);
+    }
 
-  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;
+    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;
+        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);
-    hdr->dst.addr = hdr->src.addr;
-    hdr->src.addr = me.addr;
+        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);
+        hdr->dst.addr = hdr->src.addr;
+        hdr->src.addr = me.addr;
 
-    /* Prepare eth header for arp reply */
-    memcpy(eh->daddr, eh->saddr, PICO_SIZE_ETH);
-    memcpy(eh->saddr, f->dev->eth->mac.addr, PICO_SIZE_ETH);
-    f->start = f->datalink_hdr;
-    f->len = PICO_SIZE_ETHHDR + PICO_SIZE_ARPHDR;
-    f->dev->send(f->dev, f->start, (int)f->len);
-  }
+        /* Prepare eth header for arp reply */
+        memcpy(eh->daddr, eh->saddr, PICO_SIZE_ETH);
+        memcpy(eh->saddr, f->dev->eth->mac.addr, PICO_SIZE_ETH);
+        f->start = f->datalink_hdr;
+        f->len = PICO_SIZE_ETHHDR + PICO_SIZE_ARPHDR;
+        f->dev->send(f->dev, f->start, (int)f->len);
+    }
 
 #ifdef DEBUG_ARG
-  dbg_arp();
+    dbg_arp();
 #endif
 
 end:
-  pico_frame_discard(f);
-  return ret;
+    pico_frame_discard(f);
+    return ret;
 }
 
 int32_t pico_arp_request(struct pico_device *dev, struct pico_ip4 *dst, uint8_t type)
 {
-  struct pico_frame *q = pico_frame_alloc(PICO_SIZE_ETHHDR + PICO_SIZE_ARPHDR);
-  struct pico_eth_hdr *eh;
-  struct pico_arp_hdr *ah;
-  struct pico_ip4 *src;
-  int ret;
+    struct pico_frame *q = pico_frame_alloc(PICO_SIZE_ETHHDR + PICO_SIZE_ARPHDR);
+    struct pico_eth_hdr *eh;
+    struct pico_arp_hdr *ah;
+    struct pico_ip4 *src;
+    int ret;
+
+    if (type == PICO_ARP_QUERY)
+    {
+        src = pico_ipv4_source_find(dst);
+        if (!src)
+            return -1;
+    }
 
-  if (type == PICO_ARP_QUERY)
-  {
-    src = pico_ipv4_source_find(dst);
-    if (!src)
-      return -1;
-  }
+    arp_dbg("QUERY: %08x\n", dst->addr);
 
-  arp_dbg("QUERY: %08x\n", dst->addr);
+    if (!q)
+        return -1;
 
-  if (!q)
-    return -1;
-  eh = (struct pico_eth_hdr *)q->start;
-  ah = (struct pico_arp_hdr *) (q->start + PICO_SIZE_ETHHDR);
+    eh = (struct pico_eth_hdr *)q->start;
+    ah = (struct pico_arp_hdr *) (q->start + PICO_SIZE_ETHHDR);
 
-  /* Fill eth header */
-  memcpy(eh->saddr, dev->eth->mac.addr, PICO_SIZE_ETH);
-  memcpy(eh->daddr, PICO_ETHADDR_ALL, PICO_SIZE_ETH);
-  eh->proto = PICO_IDETH_ARP;
+    /* Fill eth header */
+    memcpy(eh->saddr, dev->eth->mac.addr, PICO_SIZE_ETH);
+    memcpy(eh->daddr, PICO_ETHADDR_ALL, PICO_SIZE_ETH);
+    eh->proto = PICO_IDETH_ARP;
 
-  /* Fill arp header */
-  ah->htype  = PICO_ARP_HTYPE_ETH;
-  ah->ptype  = PICO_IDETH_IPV4;
-  ah->hsize  = PICO_SIZE_ETH;
-  ah->psize  = PICO_SIZE_IP4;
-  ah->opcode = PICO_ARP_REQUEST;
-  memcpy(ah->s_mac, dev->eth->mac.addr, PICO_SIZE_ETH);
+    /* Fill arp header */
+    ah->htype  = PICO_ARP_HTYPE_ETH;
+    ah->ptype  = PICO_IDETH_IPV4;
+    ah->hsize  = PICO_SIZE_ETH;
+    ah->psize  = PICO_SIZE_IP4;
+    ah->opcode = PICO_ARP_REQUEST;
+    memcpy(ah->s_mac, dev->eth->mac.addr, PICO_SIZE_ETH);
 
-  switch (type){
-  case PICO_ARP_ANNOUNCE:
-	  ah->src.addr = dst->addr;
-	  ah->dst.addr = dst->addr;
-	  break;
-  case PICO_ARP_PROBE:
-	  ah->src.addr = 0;
-	  ah->dst.addr = dst->addr;
-	  break;
-  default:
-	  ah->src.addr = src->addr;
-	  ah->dst.addr = dst->addr;
-  }
+    switch (type) {
+    case PICO_ARP_ANNOUNCE:
+        ah->src.addr = dst->addr;
+        ah->dst.addr = dst->addr;
+        break;
+    case PICO_ARP_PROBE:
+        ah->src.addr = 0;
+        ah->dst.addr = dst->addr;
+        break;
+    default:
+        ah->src.addr = src->addr;
+        ah->dst.addr = dst->addr;
+    }
 
-  arp_dbg("Sending arp request.\n");
-  ret = dev->send(dev, q->start,(int) q->len);
-  pico_frame_discard(q);
-  return ret;
+    arp_dbg("Sending arp request.\n");
+    ret = dev->send(dev, q->start, (int) q->len);
+    pico_frame_discard(q);
+    return ret;
 }
 
 int pico_arp_get_neighbors(struct pico_device *dev, struct pico_ip4 *neighbors, int maxlen)
 {
-  struct pico_arp* search;
-  struct pico_tree_node * index;
-  int i = 0;
-  pico_tree_foreach(index,&arp_tree){
-    search = index->keyValue;
-    if (search->dev == dev) {
-      neighbors[i++].addr = search->ipv4.addr;
-      if (i >= maxlen)
-        return i;
+    struct pico_arp*search;
+    struct pico_tree_node *index;
+    int i = 0;
+    pico_tree_foreach(index, &arp_tree){
+        search = index->keyValue;
+        if (search->dev == dev) {
+            neighbors[i++].addr = search->ipv4.addr;
+            if (i >= maxlen)
+                return i;
+        }
     }
-  }
-  return i;
+    return i;
 }
 
-void pico_arp_register_ipconflict(struct pico_ip4 *ip, struct pico_eth *mac, void(*cb)(void))
+void pico_arp_register_ipconflict(struct pico_ip4 *ip, struct pico_eth *mac, void (*cb)(void))
 {
-  conflict_ipv4.conflict = cb;
-  conflict_ipv4.ip.addr = ip->addr;
-  if (mac != NULL)
-    memcpy(conflict_ipv4.mac.addr, mac, 6);
+    conflict_ipv4.conflict = cb;
+    conflict_ipv4.ip.addr = ip->addr;
+    if (mac != NULL)
+        memcpy(conflict_ipv4.mac.addr, mac, 6);
 }
--- a/stack/pico_device.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/stack/pico_device.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 
 #include "pico_config.h"
@@ -17,131 +17,140 @@
 
 static int pico_dev_cmp(void *ka, void *kb)
 {
-	struct pico_device *a = ka, *b = kb;
-  if (a->hash < b->hash)
-    return -1;
-  if (a->hash > b->hash)
-    return 1;
-  return 0;
+    struct pico_device *a = ka, *b = kb;
+    if (a->hash < b->hash)
+        return -1;
+
+    if (a->hash > b->hash)
+        return 1;
+
+    return 0;
 }
 
-PICO_TREE_DECLARE(Device_tree,pico_dev_cmp);
+PICO_TREE_DECLARE(Device_tree, pico_dev_cmp);
 
 int pico_device_init(struct pico_device *dev, const char *name, uint8_t *mac)
 {
-	uint32_t len = (uint32_t)strlen(name);
-	if(len>MAX_DEVICE_NAME)
-		len = MAX_DEVICE_NAME;
-  memcpy(dev->name, name, len);
-  dev->hash = pico_hash(dev->name);
+    uint32_t len = (uint32_t)strlen(name);
+    if(len > MAX_DEVICE_NAME)
+        len = MAX_DEVICE_NAME;
 
-  pico_tree_insert(&Device_tree,dev);
-  dev->q_in = pico_zalloc(sizeof(struct pico_queue));
-  dev->q_out = pico_zalloc(sizeof(struct pico_queue));
+    memcpy(dev->name, name, len);
+    dev->hash = pico_hash(dev->name);
+
+    pico_tree_insert(&Device_tree, dev);
+    dev->q_in = pico_zalloc(sizeof(struct pico_queue));
+    dev->q_out = pico_zalloc(sizeof(struct pico_queue));
 
-  if (mac) {
-    dev->eth = pico_zalloc(sizeof(struct pico_ethdev));
-    memcpy(dev->eth->mac.addr, mac, PICO_SIZE_ETH);
-  } else {
-    dev->eth = NULL;
-  }
+    if (mac) {
+        dev->eth = pico_zalloc(sizeof(struct pico_ethdev));
+        memcpy(dev->eth->mac.addr, mac, PICO_SIZE_ETH);
+    } else {
+        dev->eth = NULL;
+    }
 
-  if (!dev->q_in || !dev->q_out || (mac && !dev->eth))
-    return -1;
-  return 0;
+    if (!dev->q_in || !dev->q_out || (mac && !dev->eth))
+        return -1;
+
+    return 0;
 }
 
 void pico_device_destroy(struct pico_device *dev)
 {
-  if (dev->destroy)
-    dev->destroy(dev);
+    if (dev->destroy)
+        dev->destroy(dev);
+
+    if (dev->q_in) {
+        pico_queue_empty(dev->q_in);
+        pico_free(dev->q_in);
+    }
 
-  if (dev->q_in) {
-    pico_queue_empty(dev->q_in);
-    pico_free(dev->q_in);
-  }
-  if (dev->q_out) {
-    pico_queue_empty(dev->q_out);
-    pico_free(dev->q_out);
-  }
+    if (dev->q_out) {
+        pico_queue_empty(dev->q_out);
+        pico_free(dev->q_out);
+    }
 
-  if (dev->eth)
-    pico_free(dev->eth);
+    if (dev->eth)
+        pico_free(dev->eth);
 
-  pico_tree_delete(&Device_tree,dev);
-  pico_free(dev);
+    pico_tree_delete(&Device_tree, dev);
+    pico_free(dev);
 }
 
 static int devloop(struct pico_device *dev, int loop_score, int direction)
 {
-  struct pico_frame *f;
-
-  /* If device supports interrupts, read the value of the condition and trigger the dsr */
-  if ((dev->__serving_interrupt) && (dev->dsr)) {
-    /* call dsr routine */
-    loop_score = dev->dsr(dev, loop_score);
-  }
-
-  /* If device supports polling, give control. Loop score is managed internally, 
-   * remaining loop points are returned. */
-  if (dev->poll) {
-    loop_score = dev->poll(dev, loop_score);
-  }
-
-  if (direction == PICO_LOOP_DIR_OUT) {
-
-    while(loop_score > 0) {
-      if (dev->q_out->frames <= 0)
-        break;
+    struct pico_frame *f;
 
-      /* Device dequeue + send */
-      f = pico_dequeue(dev->q_out);
-      if (f) {
-        if (dev->eth) {
-          int ret = pico_ethernet_send(f);
-          if (0 == ret) {
-            loop_score--;
-            continue;
-          } if (ret < 0) {
-            if (!pico_source_is_local(f)) { 
-              dbg("Destination unreachable -------> SEND ICMP\n");
-              pico_notify_dest_unreachable(f);
-            } else {
-              dbg("Destination unreachable -------> LOCAL\n");
-            }
-            pico_frame_discard(f);
-            continue;
-          }
-        } else {
-          dev->send(dev, f->start, (int)f->len);
-        }
-        pico_frame_discard(f);
-        loop_score--;
-      }
+    /* If device supports interrupts, read the value of the condition and trigger the dsr */
+    if ((dev->__serving_interrupt) && (dev->dsr)) {
+        /* call dsr routine */
+        loop_score = dev->dsr(dev, loop_score);
+    }
+
+    /* If device supports polling, give control. Loop score is managed internally,
+     * remaining loop points are returned. */
+    if (dev->poll) {
+        loop_score = dev->poll(dev, loop_score);
     }
 
-  } else if (direction == PICO_LOOP_DIR_IN) {
+    if (direction == PICO_LOOP_DIR_OUT) {
+
+        while(loop_score > 0) {
+            if (dev->q_out->frames <= 0)
+                break;
 
-    while(loop_score > 0) {
-      if (dev->q_in->frames <= 0)
-        break;
+            /* Device dequeue + send */
+            f = pico_dequeue(dev->q_out);
+            if (f) {
+                if (dev->eth) {
+                    int ret = pico_ethernet_send(f);
+                    if (0 == ret) {
+                        loop_score--;
+                        continue;
+                    }
+
+                    if (ret < 0) {
+                        if (!pico_source_is_local(f)) {
+                            dbg("Destination unreachable -------> SEND ICMP\n");
+                            pico_notify_dest_unreachable(f);
+                        } else {
+                            dbg("Destination unreachable -------> LOCAL\n");
+                        }
 
-      /* Receive */
-      f = pico_dequeue(dev->q_in);
-      if (f) {
-        if (dev->eth) {
-          f->datalink_hdr = f->buffer;
-          pico_ethernet_receive(f);
-        } else {
-          f->net_hdr = f->buffer;
-          pico_network_receive(f);
+                        pico_frame_discard(f);
+                        continue;
+                    }
+                } else {
+                    dev->send(dev, f->start, (int)f->len);
+                }
+
+                pico_frame_discard(f);
+                loop_score--;
+            }
         }
-        loop_score--;
-      }
+    } else if (direction == PICO_LOOP_DIR_IN) {
+
+        while(loop_score > 0) {
+            if (dev->q_in->frames <= 0)
+                break;
+
+            /* Receive */
+            f = pico_dequeue(dev->q_in);
+            if (f) {
+                if (dev->eth) {
+                    f->datalink_hdr = f->buffer;
+                    pico_ethernet_receive(f);
+                } else {
+                    f->net_hdr = f->buffer;
+                    pico_network_receive(f);
+                }
+
+                loop_score--;
+            }
+        }
     }
-  }
 
-  return loop_score;
+    return loop_score;
 }
 
 
@@ -149,98 +158,100 @@
 
 int pico_devices_loop(int loop_score, int direction)
 {
-  struct pico_device *start;
-  static struct pico_device *next = NULL, *next_in = NULL, *next_out = NULL;
-  static struct pico_tree_node * next_node, * in_node, * out_node;
+    struct pico_device *start;
+    static struct pico_device *next = NULL, *next_in = NULL, *next_out = NULL;
+    static struct pico_tree_node *next_node, *in_node, *out_node;
+
+    if (next_in == NULL) {
+        in_node = pico_tree_firstNode(Device_tree.root);
+        next_in = in_node->keyValue;
+    }
+
+    if (next_out == NULL) {
+        out_node = pico_tree_firstNode(Device_tree.root);
+        next_out = out_node->keyValue;
+    }
 
-  if (next_in == NULL) {
-    in_node = pico_tree_firstNode(Device_tree.root);
-    next_in = in_node->keyValue;
-  }
-  if (next_out == NULL) {
-  	out_node = pico_tree_firstNode(Device_tree.root);
-    next_out = out_node->keyValue;
-  }
-  
-  if (direction == PICO_LOOP_DIR_IN)
-  {
-  	next_node = in_node;
-    next = next_in;
-  }
-  else if (direction == PICO_LOOP_DIR_OUT)
-  {
-  	next_node = out_node;
-    next = next_out;
-  }
+    if (direction == PICO_LOOP_DIR_IN)
+    {
+        next_node = in_node;
+        next = next_in;
+    }
+    else if (direction == PICO_LOOP_DIR_OUT)
+    {
+        next_node = out_node;
+        next = next_out;
+    }
 
-  /* init start node */
-  start = next;
+    /* init start node */
+    start = next;
 
-  /* round-robin all devices, break if traversed all devices */
-  while (loop_score > DEV_LOOP_MIN && next != NULL) {
-    loop_score = devloop(next, loop_score, direction);
+    /* round-robin all devices, break if traversed all devices */
+    while (loop_score > DEV_LOOP_MIN && next != NULL) {
+        loop_score = devloop(next, loop_score, direction);
+
+        next_node = pico_tree_next(next_node);
+        next = next_node->keyValue;
 
-    next_node = pico_tree_next(next_node);
-    next = next_node->keyValue;
+        if (next == NULL)
+        {
+            next_node = pico_tree_firstNode(Device_tree.root);
+            next = next_node->keyValue;
+        }
 
-    if (next == NULL)
-    {
-    	next_node = pico_tree_firstNode(Device_tree.root);
-      next = next_node->keyValue;
+        if (next == start)
+            break;
     }
-    if (next == start)
-      break;
-  }
+    if (direction == PICO_LOOP_DIR_IN)
+    {
+        in_node = next_node;
+        next_in = next;
+    }
+    else if (direction == PICO_LOOP_DIR_OUT)
+    {
+        out_node = next_node;
+        next_out = next;
+    }
 
-  if (direction == PICO_LOOP_DIR_IN)
-  {
-  	in_node = next_node;
-    next_in = next;
-  }
-  else if (direction == PICO_LOOP_DIR_OUT)
-  {
-  	out_node = next_node;
-    next_out = next;
-  }
-
-  return loop_score;
+    return loop_score;
 }
 
-struct pico_device* pico_get_device(const char* name)
+struct pico_device*pico_get_device(const char*name)
 {
-  struct pico_device *dev;
-  struct pico_tree_node * index;
-  pico_tree_foreach(index, &Device_tree){
-  	dev = index->keyValue;
-    if(strcmp(name, dev->name) == 0)
-      return dev;
-  }
-  return NULL;
+    struct pico_device *dev;
+    struct pico_tree_node *index;
+    pico_tree_foreach(index, &Device_tree){
+        dev = index->keyValue;
+        if(strcmp(name, dev->name) == 0)
+            return dev;
+    }
+    return NULL;
 }
 
-int32_t pico_device_broadcast(struct pico_frame * f)
+int32_t pico_device_broadcast(struct pico_frame *f)
 {
-	struct pico_tree_node * index;
-	int32_t ret = -1;
+    struct pico_tree_node *index;
+    int32_t ret = -1;
 
-	pico_tree_foreach(index,&Device_tree)
-	{
-		struct pico_device * dev = index->keyValue;
-		if(dev != f->dev)
-		{
-			struct pico_frame * copy = pico_frame_copy(f);
+    pico_tree_foreach(index, &Device_tree)
+    {
+        struct pico_device *dev = index->keyValue;
+        if(dev != f->dev)
+        {
+            struct pico_frame *copy = pico_frame_copy(f);
 
-			if(!copy)
-				return -1;
-			copy->dev = dev;
-			copy->dev->send(copy->dev, copy->start, (int)copy->len);
-			pico_frame_discard(copy);
-		}
-		else
-		{
-			ret = (int32_t)f->dev->send(f->dev, f->start, (int)f->len);
-		}
-	}
+            if(!copy)
+                return -1;
 
-	return ret;
+            copy->dev = dev;
+            copy->dev->send(copy->dev, copy->start, (int)copy->len);
+            pico_frame_discard(copy);
+        }
+        else
+        {
+            ret = (int32_t)f->dev->send(f->dev, f->start, (int)f->len);
+        }
+    }
+
+    return ret;
 }
--- a/stack/pico_frame.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/stack/pico_frame.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 
 #include "pico_config.h"
@@ -18,103 +18,108 @@
 /** frame alloc/dealloc/copy **/
 void pico_frame_discard(struct pico_frame *f)
 {
-  (*f->usage_count)--;
-  if (*f->usage_count <= 0) {
-    pico_free(f->usage_count);
+    (*f->usage_count)--;
+    if (*f->usage_count <= 0) {
+        pico_free(f->usage_count);
 #ifdef PICO_SUPPORT_DEBUG_MEMORY
-    dbg("Discarded buffer @%p, caller: %p\n", f->buffer, __builtin_return_address(3));
-    dbg("DEBUG MEMORY: %d frames in use.\n", --n_frames_allocated);
+        dbg("Discarded buffer @%p, caller: %p\n", f->buffer, __builtin_return_address(3));
+        dbg("DEBUG MEMORY: %d frames in use.\n", --n_frames_allocated);
 #endif
-    pico_free(f->buffer);
-    if (f->info)
-      pico_free(f->info);
-  }
+        pico_free(f->buffer);
+        if (f->info)
+            pico_free(f->info);
+    }
+
 #ifdef PICO_SUPPORT_DEBUG_MEMORY
-  else {
-    dbg("Removed frame @%p(copy), usage count now: %d\n", f, *f->usage_count);
-  }
+    else {
+        dbg("Removed frame @%p(copy), usage count now: %d\n", f, *f->usage_count);
+    }
 #endif
-  pico_free(f);
+    pico_free(f);
 }
 
 struct pico_frame *pico_frame_copy(struct pico_frame *f)
 {
-  struct pico_frame *new = pico_zalloc(sizeof(struct pico_frame));
-  if (!new)
-    return NULL;
-  memcpy(new, f, sizeof(struct pico_frame));
-  *(new->usage_count) += 1;
+    struct pico_frame *new = pico_zalloc(sizeof(struct pico_frame));
+    if (!new)
+        return NULL;
+
+    memcpy(new, f, sizeof(struct pico_frame));
+    *(new->usage_count) += 1;
 #ifdef PICO_SUPPORT_DEBUG_MEMORY
-  dbg("Copied frame @%p, into %p, usage count now: %d\n", f, new, *new->usage_count);
+    dbg("Copied frame @%p, into %p, usage count now: %d\n", f, new, *new->usage_count);
 #endif
-  new->next = NULL;
-  return new;
+    new->next = NULL;
+    return new;
 }
 
 
 struct pico_frame *pico_frame_alloc(uint32_t size)
 {
-  struct pico_frame *p = pico_zalloc(sizeof(struct pico_frame));
-  if (!p)
-    return NULL;
-  p->buffer = pico_zalloc(size);
-  if (!p->buffer) {
-    pico_free(p);
-    return NULL;
-  }
-  p->usage_count = pico_zalloc(sizeof(uint32_t));
-  if (!p->usage_count) {
-    pico_free(p->buffer);
-    pico_free(p);
-    return NULL;
-  }
-  p->buffer_len = size;
+    struct pico_frame *p = pico_zalloc(sizeof(struct pico_frame));
+    if (!p)
+        return NULL;
+
+    p->buffer = pico_zalloc(size);
+    if (!p->buffer) {
+        pico_free(p);
+        return NULL;
+    }
+
+    p->usage_count = pico_zalloc(sizeof(uint32_t));
+    if (!p->usage_count) {
+        pico_free(p->buffer);
+        pico_free(p);
+        return NULL;
+    }
+
+    p->buffer_len = size;
 
 
-  /* By default, frame content is the full buffer. */
-  p->start = p->buffer;
-  p->len = p->buffer_len;
-  *p->usage_count = 1;
+    /* By default, frame content is the full buffer. */
+    p->start = p->buffer;
+    p->len = p->buffer_len;
+    *p->usage_count = 1;
 #ifdef PICO_SUPPORT_DEBUG_MEMORY
     dbg("Allocated buffer @%p, len= %d caller: %p\n", p->buffer, p->buffer_len, __builtin_return_address(2));
     dbg("DEBUG MEMORY: %d frames in use.\n", ++n_frames_allocated);
 #endif
-  return p;
+    return p;
 }
 
 struct pico_frame *pico_frame_deepcopy(struct pico_frame *f)
 {
-  struct pico_frame *new = pico_frame_alloc(f->buffer_len);
-  int addr_diff;
-  unsigned char *buf;
-  uint32_t *uc;
-  if (!new)
-    return NULL;
+    struct pico_frame *new = pico_frame_alloc(f->buffer_len);
+    int addr_diff;
+    unsigned char *buf;
+    uint32_t *uc;
+    if (!new)
+        return NULL;
 
-  /* Save the two key pointers... */
-  buf = new->buffer;
-  uc  = new->usage_count;
+    /* Save the two key pointers... */
+    buf = new->buffer;
+    uc  = new->usage_count;
 
-  /* Overwrite all fields with originals */
-  memcpy(new, f, sizeof(struct pico_frame));
+    /* Overwrite all fields with originals */
+    memcpy(new, f, sizeof(struct pico_frame));
 
-  /* ...restore the two key pointers */
-  new->buffer = buf;
-  new->usage_count = uc;
+    /* ...restore the two key pointers */
+    new->buffer = buf;
+    new->usage_count = uc;
 
-  /* Update in-buffer pointers with offset */
-  addr_diff = (int)(new->buffer - f->buffer);
-  new->net_hdr += addr_diff;
-  new->transport_hdr += addr_diff;
-  new->app_hdr += addr_diff;
-  new->start += addr_diff;
-  new->payload += addr_diff;
+    /* Update in-buffer pointers with offset */
+    addr_diff = (int)(new->buffer - f->buffer);
+    new->net_hdr += addr_diff;
+    new->transport_hdr += addr_diff;
+    new->app_hdr += addr_diff;
+    new->start += addr_diff;
+    new->payload += addr_diff;
 
 #ifdef PICO_SUPPORT_DEBUG_MEMORY
-  dbg("Deep-Copied frame @%p, into %p, usage count now: %d\n", f, new, *new->usage_count);
+    dbg("Deep-Copied frame @%p, into %p, usage count now: %d\n", f, new, *new->usage_count);
 #endif
-  new->next = NULL;
-  return new;
+    new->next = NULL;
+    return new;
 }
 
 /**
@@ -122,80 +127,80 @@
  */
 uint16_t pico_checksum(void *inbuf, uint32_t len)
 {
-  uint8_t *buf = (uint8_t *) inbuf;
-  uint32_t tmp = 0;
-  uint32_t sum = 0;
-  uint32_t i = 0;
+    uint8_t *buf = (uint8_t *) inbuf;
+    uint32_t tmp = 0;
+    uint32_t sum = 0;
+    uint32_t i = 0;
 
-  for(i=0; i < len; i++) {
-    if (i%2) {
-      sum += buf[i];
-    } else {
-      tmp = buf[i];
-      sum += (tmp << 8);
+    for(i = 0; i < len; i++) {
+        if (i % 2) {
+            sum += buf[i];
+        } else {
+            tmp = buf[i];
+            sum += (tmp << 8);
+        }
     }
-  }
-
-  while (sum >> 16) { /* a second carry is possible! */
-    sum = (sum & 0x0000FFFF) + (sum >> 16);
-  } 
-  return (uint16_t) (~sum);
+    while (sum >> 16) { /* a second carry is possible! */
+        sum = (sum & 0x0000FFFF) + (sum >> 16);
+    }
+    return (uint16_t) (~sum);
 }
 
 uint16_t pico_dualbuffer_checksum(void *inbuf1, uint32_t len1, void *inbuf2, uint32_t len2)
 {
-  uint8_t *b1 = (uint8_t *) inbuf1;
-  uint8_t *b2 = (uint8_t *) inbuf2;
-  uint16_t tmp = 0;
-  uint32_t sum = 0;
-  uint32_t i = 0, j = 0;
+    uint8_t *b1 = (uint8_t *) inbuf1;
+    uint8_t *b2 = (uint8_t *) inbuf2;
+    uint16_t tmp = 0;
+    uint32_t sum = 0;
+    uint32_t i = 0, j = 0;
 
-  for(i=0; i < len1; i++) {
-    if (j%2) {
-      sum += b1[i];
-    } else {
-      tmp = b1[i];
-      sum = sum +(uint32_t)(tmp << 8);
+    for(i = 0; i < len1; i++) {
+        if (j % 2) {
+            sum += b1[i];
+        } else {
+            tmp = b1[i];
+            sum = sum + (uint32_t)(tmp << 8);
+        }
+
+        j++;
     }
-    j++;
-  }
+    j = 0; /* j has to be reset if len1 is odd */
+    for(i = 0; i < len2; i++) {
+        if (j % 2) {
+            sum += b2[i];
+        } else {
+            tmp = b2[i];
+            sum = sum + (uint32_t)(tmp << 8);
+        }
 
-  j = 0; /* j has to be reset if len1 is odd */
-  for(i=0; i < len2; i++) {
-    if (j%2) {
-      sum += b2[i];
-    } else {
-      tmp = b2[i];
-      sum = sum +(uint32_t)(tmp << 8);
+        j++;
     }
-    j++;
-  }
-  while (sum >> 16) { /* a second carry is possible! */
-    sum = (sum & 0x0000FFFF) + (sum >> 16);
-  } 
-  return (uint16_t) (~sum);
+    while (sum >> 16) { /* a second carry is possible! */
+        sum = (sum & 0x0000FFFF) + (sum >> 16);
+    }
+    return (uint16_t) (~sum);
 }
 
 uint16_t pico_dualbuffer_checksum_broken(void *inbuf1, uint16_t len1, void *inbuf2, uint16_t len2)
 {
-  uint16_t *b1 = (uint16_t *) inbuf1;
-  uint16_t *b2 = (uint16_t *) inbuf2;
-  uint32_t sum = 0;
-  int i=0, j=0;
-  for(i=0; i<(len1>>1); i++){
-    sum += short_be(b1[i]);
-    j++;
-  }
-  for(i=0; i<(len2>>1); i++){
-    sum += short_be(b2[i]);
-    j++;
-  }
-  sum = (sum & 0xFFFF) + (sum >> 16);
-  sum += (sum >> 16);
-  
-  // Take the bitwise complement of sum
-  sum = ~sum;
-  return (uint16_t) (sum)  ;
+    uint16_t *b1 = (uint16_t *) inbuf1;
+    uint16_t *b2 = (uint16_t *) inbuf2;
+    uint32_t sum = 0;
+    int i = 0, j = 0;
+    for(i = 0; i < (len1 >> 1); i++) {
+        sum += short_be(b1[i]);
+        j++;
+    }
+    for(i = 0; i < (len2 >> 1); i++) {
+        sum += short_be(b2[i]);
+        j++;
+    }
+    sum = (sum & 0xFFFF) + (sum >> 16);
+    sum += (sum >> 16);
+
+    /* Take the bitwise complement of sum */
+    sum = ~sum;
+    return (uint16_t) (sum);
 }
 
 
--- a/stack/pico_protocol.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/stack/pico_protocol.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 
 #include "pico_protocol.h"
@@ -13,113 +13,115 @@
 
 static int pico_proto_cmp(void *ka, void *kb)
 {
-	struct pico_protocol *a = ka, *b=kb;
-  if (a->hash < b->hash)
-    return -1;
-  if (a->hash > b->hash)
-    return 1;
-  return 0;
+    struct pico_protocol *a = ka, *b = kb;
+    if (a->hash < b->hash)
+        return -1;
+
+    if (a->hash > b->hash)
+        return 1;
+
+    return 0;
 }
 
-PICO_TREE_DECLARE(Datalink_proto_tree,pico_proto_cmp);
-PICO_TREE_DECLARE(Network_proto_tree,pico_proto_cmp);
-PICO_TREE_DECLARE(Transport_proto_tree,pico_proto_cmp);
-PICO_TREE_DECLARE(Socket_proto_tree,pico_proto_cmp);
+PICO_TREE_DECLARE(Datalink_proto_tree, pico_proto_cmp);
+PICO_TREE_DECLARE(Network_proto_tree, pico_proto_cmp);
+PICO_TREE_DECLARE(Transport_proto_tree, pico_proto_cmp);
+PICO_TREE_DECLARE(Socket_proto_tree, pico_proto_cmp);
 
 static int proto_loop(struct pico_protocol *proto, int loop_score, int direction)
 {
-  struct pico_frame *f;
+    struct pico_frame *f;
+
+    if (direction == PICO_LOOP_DIR_IN) {
 
-  if (direction == PICO_LOOP_DIR_IN) {
+        while(loop_score > 0) {
+            if (proto->q_in->frames <= 0)
+                break;
 
-    while(loop_score >0) {
-      if (proto->q_in->frames <= 0)
-        break;
+            f = pico_dequeue(proto->q_in);
+            if ((f) && (proto->process_in(proto, f) > 0)) {
+                loop_score--;
+            }
+        }
+    } else if (direction == PICO_LOOP_DIR_OUT) {
 
-      f = pico_dequeue(proto->q_in);
-      if ((f) &&(proto->process_in(proto, f) > 0)) {
-        loop_score--;
-      }
+        while(loop_score > 0) {
+            if (proto->q_out->frames <= 0)
+                break;
+
+            f = pico_dequeue(proto->q_out);
+            if ((f) && (proto->process_out(proto, f) > 0)) {
+                loop_score--;
+            }
+        }
     }
 
-  } else if (direction == PICO_LOOP_DIR_OUT) {
-
-    while(loop_score >0) {
-      if (proto->q_out->frames <= 0)
-        break;
-
-      f = pico_dequeue(proto->q_out);
-      if ((f) &&(proto->process_out(proto, f) > 0)) {
-        loop_score--;
-      }
-    }
-  }
-
-  return loop_score;
+    return loop_score;
 }
 
 #define DL_LOOP_MIN 1
 
 int pico_protocol_datalink_loop(int loop_score, int direction)
 {
-  struct pico_protocol *start;
-  static struct pico_protocol *next = NULL, *next_in = NULL, *next_out = NULL;
-  static struct pico_tree_node * next_node, * in_node, * out_node;
+    struct pico_protocol *start;
+    static struct pico_protocol *next = NULL, *next_in = NULL, *next_out = NULL;
+    static struct pico_tree_node *next_node, *in_node, *out_node;
+
+    if (next_in == NULL) {
+        in_node = pico_tree_firstNode(Datalink_proto_tree.root);
+        if (in_node)
+            next_in = in_node->keyValue;
+    }
 
-  if (next_in == NULL) {
-  	in_node = pico_tree_firstNode(Datalink_proto_tree.root);
-    if (in_node)
-      next_in = in_node->keyValue;
-  }
-  if (next_out == NULL) {
-    out_node = pico_tree_firstNode(Datalink_proto_tree.root);
-    if (out_node)
-      next_out = out_node->keyValue;
-  }
-  
-  if (direction == PICO_LOOP_DIR_IN)
-  {
-  	next_node = in_node;
-    next = next_in;
-  }
-  else if (direction == PICO_LOOP_DIR_OUT)
-  {
-  	next_node = out_node;
-    next = next_out;
-  }
+    if (next_out == NULL) {
+        out_node = pico_tree_firstNode(Datalink_proto_tree.root);
+        if (out_node)
+            next_out = out_node->keyValue;
+    }
 
-  /* init start node */
-  start = next;
+    if (direction == PICO_LOOP_DIR_IN)
+    {
+        next_node = in_node;
+        next = next_in;
+    }
+    else if (direction == PICO_LOOP_DIR_OUT)
+    {
+        next_node = out_node;
+        next = next_out;
+    }
+
+    /* init start node */
+    start = next;
 
-  /* round-robin all datalink protocols, break if traversed all protocols */
-  while (loop_score > DL_LOOP_MIN && next != NULL) {
-    loop_score = proto_loop(next, loop_score, direction);
+    /* round-robin all datalink protocols, break if traversed all protocols */
+    while (loop_score > DL_LOOP_MIN && next != NULL) {
+        loop_score = proto_loop(next, loop_score, direction);
+
+        /* next = RB_NEXT(pico_protocol_tree, &Datalink_proto_tree, next); */
+        next_node = pico_tree_next(next_node);
+        next = next_node->keyValue;
 
-    //next = RB_NEXT(pico_protocol_tree, &Datalink_proto_tree, next);
-    next_node = pico_tree_next(next_node);
-    next = next_node->keyValue;
+        if (next == NULL)
+        {
+            next_node = pico_tree_firstNode(Datalink_proto_tree.root);
+            next = next_node->keyValue;
+        }
 
-    if (next == NULL)
-    {
-    	next_node = pico_tree_firstNode(Datalink_proto_tree.root);
-    	next = next_node->keyValue;
+        if (next == start)
+            break;
     }
-    if (next == start)
-      break;
-  }
+    if (direction == PICO_LOOP_DIR_IN)
+    {
+        in_node = next_node;
+        next_in = next;
+    }
+    else if (direction == PICO_LOOP_DIR_OUT)
+    {
+        out_node = next_node;
+        next_out = next;
+    }
 
-  if (direction == PICO_LOOP_DIR_IN)
-  {
-  	in_node = next_node;
-    next_in = next;
-  }
-  else if (direction == PICO_LOOP_DIR_OUT)
-  {
-  	out_node = next_node;
-    next_out = next;
-  }
-
-  return loop_score;
+    return loop_score;
 }
 
 
@@ -127,126 +129,129 @@
 
 int pico_protocol_network_loop(int loop_score, int direction)
 {
-  struct pico_protocol *start;
-  static struct pico_protocol *next = NULL, *next_in = NULL, *next_out = NULL;
-  static struct pico_tree_node * next_node, * in_node, * out_node;
+    struct pico_protocol *start;
+    static struct pico_protocol *next = NULL, *next_in = NULL, *next_out = NULL;
+    static struct pico_tree_node *next_node, *in_node, *out_node;
+
+    if (next_in == NULL) {
+        in_node = pico_tree_firstNode(Network_proto_tree.root);
+        if (in_node)
+            next_in = in_node->keyValue;
+    }
 
-  if (next_in == NULL) {
-    in_node = pico_tree_firstNode(Network_proto_tree.root);
-    if (in_node)
-      next_in = in_node->keyValue;
-  }
-  if (next_out == NULL) {
-  	out_node = pico_tree_firstNode(Network_proto_tree.root);
-    if (out_node)
-  	  next_out = out_node->keyValue;
-  }
-  if (direction == PICO_LOOP_DIR_IN)
-  {
-  	next_node = in_node;
-    next = next_in;
-  }
-  else if (direction == PICO_LOOP_DIR_OUT)
-  {
-  	next_node = out_node;
-    next = next_out;
-  }
+    if (next_out == NULL) {
+        out_node = pico_tree_firstNode(Network_proto_tree.root);
+        if (out_node)
+            next_out = out_node->keyValue;
+    }
 
-  /* init start node */
-  start = next;
+    if (direction == PICO_LOOP_DIR_IN)
+    {
+        next_node = in_node;
+        next = next_in;
+    }
+    else if (direction == PICO_LOOP_DIR_OUT)
+    {
+        next_node = out_node;
+        next = next_out;
+    }
 
-  /* round-robin all network protocols, break if traversed all protocols */
-  while (loop_score > NW_LOOP_MIN && next != NULL) {
-    loop_score = proto_loop(next, loop_score, direction);
+    /* init start node */
+    start = next;
+
+    /* round-robin all network protocols, break if traversed all protocols */
+    while (loop_score > NW_LOOP_MIN && next != NULL) {
+        loop_score = proto_loop(next, loop_score, direction);
 
-    next_node = pico_tree_next(next_node);
-    next = next_node->keyValue;
+        next_node = pico_tree_next(next_node);
+        next = next_node->keyValue;
 
-    if (next == NULL)
-    {
-    	next_node = pico_tree_firstNode(Network_proto_tree.root);
-    	next = next_node->keyValue;
+        if (next == NULL)
+        {
+            next_node = pico_tree_firstNode(Network_proto_tree.root);
+            next = next_node->keyValue;
+        }
+
+        if (next == start)
+            break;
     }
-    if (next == start)
-      break;
-  }
+    if (direction == PICO_LOOP_DIR_IN)
+    {
+        in_node = next_node;
+        next_in = next;
+    }
+    else if (direction == PICO_LOOP_DIR_OUT)
+    {
+        out_node = next_node;
+        next_out = next;
+    }
 
-  if (direction == PICO_LOOP_DIR_IN)
-  {
-  	in_node = next_node;
-    next_in = next;
-  }
-  else if (direction == PICO_LOOP_DIR_OUT)
-  {
-  	out_node = next_node;
-    next_out = next;
-  }
-
-  return loop_score;
+    return loop_score;
 }
 
 #define TP_LOOP_MIN 1
 
 int pico_protocol_transport_loop(int loop_score, int direction)
 {
-  struct pico_protocol *start;
-  static struct pico_protocol *next = NULL, *next_in = NULL, *next_out = NULL;
-  static struct pico_tree_node * next_node, * in_node, * out_node;
+    struct pico_protocol *start;
+    static struct pico_protocol *next = NULL, *next_in = NULL, *next_out = NULL;
+    static struct pico_tree_node *next_node, *in_node, *out_node;
+
+    if (next_in == NULL) {
+        in_node = pico_tree_firstNode(Transport_proto_tree.root);
+        if (in_node)
+            next_in = in_node->keyValue;
+    }
 
-  if (next_in == NULL) {
-  	in_node = pico_tree_firstNode(Transport_proto_tree.root);
-    if (in_node)
-    	next_in = in_node->keyValue;
-  }
-  if (next_out == NULL) {
-  	out_node = pico_tree_firstNode(Transport_proto_tree.root);
-    if (out_node)
-  	  next_out = out_node->keyValue;
-  }
-  
-  if (direction == PICO_LOOP_DIR_IN)
-  {
-  	next_node = in_node;
-    next = next_in;
-  }
-  else if (direction == PICO_LOOP_DIR_OUT)
-  {
-  	next_node = out_node;
-    next = next_out;
-  }
+    if (next_out == NULL) {
+        out_node = pico_tree_firstNode(Transport_proto_tree.root);
+        if (out_node)
+            next_out = out_node->keyValue;
+    }
 
-  /* init start node */
-  start = next;
+    if (direction == PICO_LOOP_DIR_IN)
+    {
+        next_node = in_node;
+        next = next_in;
+    }
+    else if (direction == PICO_LOOP_DIR_OUT)
+    {
+        next_node = out_node;
+        next = next_out;
+    }
+
+    /* init start node */
+    start = next;
 
-  /* round-robin all transport protocols, break if traversed all protocols */
-  while (loop_score > DL_LOOP_MIN && next != NULL) {
-    loop_score = proto_loop(next, loop_score, direction);
+    /* round-robin all transport protocols, break if traversed all protocols */
+    while (loop_score > DL_LOOP_MIN && next != NULL) {
+        loop_score = proto_loop(next, loop_score, direction);
+
+        /* next = RB_NEXT(pico_protocol_tree, &Transport_proto_tree, next); */
+        next_node = pico_tree_next(next_node);
+        next = next_node->keyValue;
 
-    //next = RB_NEXT(pico_protocol_tree, &Transport_proto_tree, next);
-    next_node = pico_tree_next(next_node);
-    next = next_node->keyValue;
+        if (next == NULL)
+        {
+            next_node = pico_tree_firstNode(Transport_proto_tree.root);
+            next = next_node->keyValue;
+        }
 
-    if (next == NULL)
-    {
-    	next_node = pico_tree_firstNode(Transport_proto_tree.root);
-    	next = next_node->keyValue;
+        if (next == start)
+            break;
     }
-    if (next == start)
-      break;
-  }
+    if (direction == PICO_LOOP_DIR_IN)
+    {
+        in_node = next_node;
+        next_in = next;
+    }
+    else if (direction == PICO_LOOP_DIR_OUT)
+    {
+        out_node = next_node;
+        next_out = next;
+    }
 
-  if (direction == PICO_LOOP_DIR_IN)
-  {
-  	in_node = next_node;
-    next_in = next;
-  }
-  else if (direction == PICO_LOOP_DIR_OUT)
-  {
-  	out_node = next_node;
-    next_out = next;
-  }
-
-  return loop_score;
+    return loop_score;
 }
 
 
@@ -254,97 +259,98 @@
 
 int pico_protocol_socket_loop(int loop_score, int direction)
 {
-  struct pico_protocol *start;
-  static struct pico_protocol *next = NULL, *next_in = NULL, *next_out = NULL;
-  static struct pico_tree_node * next_node, * in_node, * out_node;
+    struct pico_protocol *start;
+    static struct pico_protocol *next = NULL, *next_in = NULL, *next_out = NULL;
+    static struct pico_tree_node *next_node, *in_node, *out_node;
+
+    if (next_in == NULL) {
+        in_node = pico_tree_firstNode(Socket_proto_tree.root);
+        if(in_node)
+            next_in = in_node->keyValue;
+    }
 
-  if (next_in == NULL) {
-  	in_node = pico_tree_firstNode(Socket_proto_tree.root);
-    if(in_node)
-  	  next_in = in_node->keyValue;
-  }
-  if (next_out == NULL) {
-  	out_node = pico_tree_firstNode(Socket_proto_tree.root);
-    if(out_node)
-      next_out = out_node->keyValue;
-  }
-  
-  if (direction == PICO_LOOP_DIR_IN)
-  {
-  	next_node = in_node;
-    next = next_in;
-  }
-  else if (direction == PICO_LOOP_DIR_OUT)
-  {
-	 	next_node = out_node;
-  	next = next_out;
-  }
+    if (next_out == NULL) {
+        out_node = pico_tree_firstNode(Socket_proto_tree.root);
+        if(out_node)
+            next_out = out_node->keyValue;
+    }
 
-  /* init start node */
-  start = next;
+    if (direction == PICO_LOOP_DIR_IN)
+    {
+        next_node = in_node;
+        next = next_in;
+    }
+    else if (direction == PICO_LOOP_DIR_OUT)
+    {
+        next_node = out_node;
+        next = next_out;
+    }
 
-  /* round-robin all transport protocols, break if traversed all protocols */
-  while (loop_score > SOCK_LOOP_MIN && next != NULL) {
-    loop_score = proto_loop(next, loop_score,direction);
+    /* init start node */
+    start = next;
+
+    /* round-robin all transport protocols, break if traversed all protocols */
+    while (loop_score > SOCK_LOOP_MIN && next != NULL) {
+        loop_score = proto_loop(next, loop_score, direction);
 
-    next_node = pico_tree_next(next_node);
-    next = next_node->keyValue;
+        next_node = pico_tree_next(next_node);
+        next = next_node->keyValue;
 
-    if (next == NULL)
-    {
-      next_node = pico_tree_firstNode(next_node);
-    	next = next_node->keyValue;
+        if (next == NULL)
+        {
+            next_node = pico_tree_firstNode(next_node);
+            next = next_node->keyValue;
+        }
+
+        if (next == start)
+            break;
     }
-    if (next == start)
-      break;
-  }
+    if (direction == PICO_LOOP_DIR_IN)
+    {
+        in_node = next_node;
+        next_in = next;
+    }
+    else if (direction == PICO_LOOP_DIR_OUT)
+    {
+        out_node = next_node;
+        next_out = next;
+    }
 
-  if (direction == PICO_LOOP_DIR_IN)
-  {
-  	in_node = next_node;
-    next_in = next;
-  }
-  else if (direction == PICO_LOOP_DIR_OUT)
-  {
-  	out_node = next_node;
-    next_out = next;
-  }
-
-  return loop_score;
+    return loop_score;
 }
 
 int pico_protocols_loop(int loop_score)
 {
 /*
-  loop_score = pico_protocol_datalink_loop(loop_score);
-  loop_score = pico_protocol_network_loop(loop_score);
-  loop_score = pico_protocol_transport_loop(loop_score);
-  loop_score = pico_protocol_socket_loop(loop_score);
-*/
-  return loop_score;
+   loop_score = pico_protocol_datalink_loop(loop_score);
+   loop_score = pico_protocol_network_loop(loop_score);
+   loop_score = pico_protocol_transport_loop(loop_score);
+   loop_score = pico_protocol_socket_loop(loop_score);
+ */
+    return loop_score;
 }
 
 void pico_protocol_init(struct pico_protocol *p)
 {
-  if (!p)
-    return;
+    if (!p)
+        return;
 
-  p->hash = pico_hash(p->name);
-  switch (p->layer) {
+    p->hash = pico_hash(p->name);
+    switch (p->layer) {
     case PICO_LAYER_DATALINK:
-      pico_tree_insert(&Datalink_proto_tree, p);
-      break;
+        pico_tree_insert(&Datalink_proto_tree, p);
+        break;
     case PICO_LAYER_NETWORK:
-      pico_tree_insert(&Network_proto_tree,p);
-      break;
+        pico_tree_insert(&Network_proto_tree, p);
+        break;
     case PICO_LAYER_TRANSPORT:
-      pico_tree_insert(&Transport_proto_tree,p);
-      break;
+        pico_tree_insert(&Transport_proto_tree, p);
+        break;
     case PICO_LAYER_SOCKET:
-      pico_tree_insert(&Socket_proto_tree,p);
-      break;
-  }
-  dbg("Protocol %s registered (layer: %d).\n", p->name, p->layer);
+        pico_tree_insert(&Socket_proto_tree, p);
+        break;
+    }
+    dbg("Protocol %s registered (layer: %d).\n", p->name, p->layer);
 
 }
 
--- a/stack/pico_socket.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/stack/pico_socket.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,10 +1,10 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 
 #include "pico_config.h"
@@ -31,17 +31,17 @@
 #define TCP_STATE(s) (s->state & PICO_SOCKET_STATE_TCP)
 
 #ifdef PICO_SUPPORT_MUTEX
-static void * Mutex = NULL;
-#define LOCK(x) {\
-  if (x == NULL) \
-    x = pico_mutex_init(); \
-  pico_mutex_lock(x); \
+static void *Mutex = NULL;
+#define LOCK(x) { \
+        if (x == NULL) \
+            x = pico_mutex_init(); \
+        pico_mutex_lock(x); \
 }
-#define UNLOCK(x) pico_mutex_unlock(x);
+#define UNLOCK(x) pico_mutex_unlock(x)
 
 #else
-#define LOCK(x) do{}while(0)
-#define UNLOCK(x) do{}while(0)
+#define LOCK(x) do {} while(0)
+#define UNLOCK(x) do {} while(0)
 #endif
 
 
@@ -66,97 +66,101 @@
 #endif
 
 #ifdef PICO_SUPPORT_IPFRAG
-# define frag_dbg(...) do{}while(0) 
+# define frag_dbg(...) do {} while(0)
 #endif
 
-#ifdef PICO_SUPPORT_MCAST 
-# define so_mcast_dbg(...) do{}while(0) /* ip_mcast_dbg in pico_ipv4.c */
+#ifdef PICO_SUPPORT_MCAST
+# define so_mcast_dbg(...) do {} while(0) /* ip_mcast_dbg in pico_ipv4.c */
 #endif
 
-static struct pico_sockport *sp_udp = NULL ,*sp_tcp = NULL;
+static struct pico_sockport *sp_udp = NULL, *sp_tcp = NULL;
 
 struct pico_frame *pico_socket_frame_alloc(struct pico_socket *s, uint16_t len);
 
-static int32_t socket_cmp(void * ka, void * kb)
+static int32_t socket_cmp(void *ka, void *kb)
 {
-  struct pico_socket *a = ka, *b = kb;
-  uint32_t a_is_ip6 = is_sock_ipv6(a);
-  uint32_t b_is_ip6 = is_sock_ipv6(b);
+    struct pico_socket *a = ka, *b = kb;
+    uint32_t a_is_ip6 = is_sock_ipv6(a);
+    uint32_t b_is_ip6 = is_sock_ipv6(b);
 
-  int32_t diff;
+    int32_t diff;
 
-  /* First, order by network ver */
-  if (a_is_ip6 < b_is_ip6)
-    return -1;
-  if (a_is_ip6 > b_is_ip6)
-    return 1;
+    /* First, order by network ver */
+    if (a_is_ip6 < b_is_ip6)
+        return -1;
 
-  /* If either socket is PICO_IPV4_INADDR_ANY mode, skip local address comparison */
+    if (a_is_ip6 > b_is_ip6)
+        return 1;
 
-  /* At this point, sort by local host */
+    /* If either socket is PICO_IPV4_INADDR_ANY mode, skip local address comparison */
+
+    /* At this point, sort by local host */
 
-  if (0) {
+    if (0) {
 #ifdef PICO_SUPPORT_IPV6
-  } else if (a_is_ip6) {
-    if ((memcmp(a->local_addr.ip6.addr, PICO_IP6_ANY, PICO_SIZE_IP6)==0) || memcmp((b->local_addr.ip6.addr, PICO_IP6_ANY, PICO_SIZE_IP6) == 0))
-      diff = 0;
-    else
-      diff = memcmp(a->local_addr.ip6.addr, b->local_addr.ip6.addr, PICO_SIZE_IP6);
+    } else if (a_is_ip6) {
+        if ((memcmp(a->local_addr.ip6.addr, PICO_IP6_ANY, PICO_SIZE_IP6) == 0) || memcmp((b->local_addr.ip6.addr, PICO_IP6_ANY, PICO_SIZE_IP6) == 0))
+            diff = 0;
+        else
+            diff = memcmp(a->local_addr.ip6.addr, b->local_addr.ip6.addr, PICO_SIZE_IP6);
+
 #endif
-  } else {
-    if ((a->local_addr.ip4.addr == PICO_IP4_ANY) || (b->local_addr.ip4.addr == PICO_IP4_ANY))
-      diff = 0;
-    else
-      diff = (int32_t)(a->local_addr.ip4.addr - b->local_addr.ip4.addr);
-  }
+    } else {
+        if ((a->local_addr.ip4.addr == PICO_IP4_ANY) || (b->local_addr.ip4.addr == PICO_IP4_ANY))
+            diff = 0;
+        else
+            diff = (int32_t)(a->local_addr.ip4.addr - b->local_addr.ip4.addr);
+    }
 
-  if (diff)
-    return diff;
+    if (diff)
+        return diff;
 
 
-  /* Sort by remote host */
-  if (a_is_ip6)
-    diff = memcmp(a->remote_addr.ip6.addr, b->remote_addr.ip6.addr, PICO_SIZE_IP6);
-  else
-    diff = (int32_t)(a->remote_addr.ip4.addr - b->remote_addr.ip4.addr);
+    /* Sort by remote host */
+    if (a_is_ip6)
+        diff = memcmp(a->remote_addr.ip6.addr, b->remote_addr.ip6.addr, PICO_SIZE_IP6);
+    else
+        diff = (int32_t)(a->remote_addr.ip4.addr - b->remote_addr.ip4.addr);
 
-  if (diff)
-    return diff;
+    if (diff)
+        return diff;
 
-  /* And finally by remote port. The two sockets are coincident if the quad is the same. */
-  return b->remote_port - a->remote_port;
+    /* And finally by remote port. The two sockets are coincident if the quad is the same. */
+    return b->remote_port - a->remote_port;
 }
 
 struct pico_sockport
 {
-  struct pico_tree socks; // how you make the connection ?
-  uint16_t number;
-  uint16_t proto;
+    struct pico_tree socks; /* how you make the connection ? */
+    uint16_t number;
+    uint16_t proto;
 };
 
-#define INIT_SOCKPORT { {&LEAF , socket_cmp}, 0, 0 }
+#define INIT_SOCKPORT { {&LEAF, socket_cmp}, 0, 0 }
 
-int sockport_cmp(void * ka, void * kb)
+int sockport_cmp(void *ka, void *kb)
 {
-  struct pico_sockport *a = ka, *b = kb;
-  if (a->number < b->number)
-    return -1;
-  if (a->number > b->number)
-    return 1;
-  return 0;
+    struct pico_sockport *a = ka, *b = kb;
+    if (a->number < b->number)
+        return -1;
+
+    if (a->number > b->number)
+        return 1;
+
+    return 0;
 }
 
-PICO_TREE_DECLARE(UDPTable,sockport_cmp);
-PICO_TREE_DECLARE(TCPTable,sockport_cmp);
+PICO_TREE_DECLARE(UDPTable, sockport_cmp);
+PICO_TREE_DECLARE(TCPTable, sockport_cmp);
 
 #ifdef PICO_SUPPORT_MCAST
 /*                       socket
- *                         |  
+ *                         |
  *                    MCASTListen
  *                    |    |     |
  *         ------------    |     ------------
  *         |               |                |
- *   MCASTSources    MCASTSources     MCASTSources    
+ *   MCASTSources    MCASTSources     MCASTSources
  *   |  |  |  |      |  |  |  |       |  |  |  |
  *   S  S  S  S      S  S  S  S       S  S  S  S
  *
@@ -165,1349 +169,1425 @@
  */
 struct pico_mcast_listen
 {
-  uint8_t filter_mode;
-  struct pico_ip4 mcast_link;
-  struct pico_ip4 mcast_group;
-  struct pico_tree MCASTSources;
+    uint8_t filter_mode;
+    struct pico_ip4 mcast_link;
+    struct pico_ip4 mcast_group;
+    struct pico_tree MCASTSources;
 };
 
 static int mcast_listen_cmp(void *ka, void *kb)
 {
-  struct pico_mcast_listen *a = ka, *b = kb;
-  if (a->mcast_group.addr < b->mcast_group.addr)
-    return -1;
-  if (a->mcast_group.addr > b->mcast_group.addr)
-    return 1;
+    struct pico_mcast_listen *a = ka, *b = kb;
+    if (a->mcast_group.addr < b->mcast_group.addr)
+        return -1;
+
+    if (a->mcast_group.addr > b->mcast_group.addr)
+        return 1;
 
-  if (a->mcast_link.addr < b->mcast_link.addr)
-    return -1;
-  if (a->mcast_link.addr > b->mcast_link.addr)
-    return 1;
+    if (a->mcast_link.addr < b->mcast_link.addr)
+        return -1;
 
-  return 0;
+    if (a->mcast_link.addr > b->mcast_link.addr)
+        return 1;
+
+    return 0;
 }
 
 static int mcast_sources_cmp(void *ka, void *kb)
 {
-  struct pico_ip4 *a = ka, *b = kb;
-  if (a->addr < b->addr)
-    return -1;
-  if (a->addr > b->addr)
-    return 1;
-  return 0;
+    struct pico_ip4 *a = ka, *b = kb;
+    if (a->addr < b->addr)
+        return -1;
+
+    if (a->addr > b->addr)
+        return 1;
+
+    return 0;
 }
 
 static int mcast_socket_cmp(void *ka, void *kb)
 {
-  struct pico_socket *a = ka, *b = kb;
-  if (a < b)
-    return -1;
-  if (a > b)
-    return 1;
-  return 0;
+    struct pico_socket *a = ka, *b = kb;
+    if (a < b)
+        return -1;
+
+    if (a > b)
+        return 1;
+
+    return 0;
 }
 /* gather all multicast sockets to hasten filter aggregation */
 PICO_TREE_DECLARE(MCASTSockets, mcast_socket_cmp);
 
 static int mcast_filter_cmp(void *ka, void *kb)
 {
-  struct pico_ip4 *a = ka, *b = kb;
-  if (a->addr < b->addr)
-    return -1;
-  if (a->addr > b->addr)
-    return 1;
-  return 0;
+    struct pico_ip4 *a = ka, *b = kb;
+    if (a->addr < b->addr)
+        return -1;
+
+    if (a->addr > b->addr)
+        return 1;
+
+    return 0;
 }
 /* gather sources to be filtered */
 PICO_TREE_DECLARE(MCASTFilter, mcast_filter_cmp);
 
 static struct pico_mcast_listen *listen_find(struct pico_socket *s, struct pico_ip4 *lnk, struct pico_ip4 *grp)
 {
-  struct pico_mcast_listen ltest = {0};
-  ltest.mcast_link.addr = lnk->addr;
-  ltest.mcast_group.addr = grp->addr;
-  return pico_tree_findKey(s->MCASTListen, &ltest);
+    struct pico_mcast_listen ltest = {
+        0
+    };
+    ltest.mcast_link.addr = lnk->addr;
+    ltest.mcast_group.addr = grp->addr;
+    return pico_tree_findKey(s->MCASTListen, &ltest);
 }
 
 /* MCASTFilter will be empty if no socket is listening on mcast_group on mcast_link anymore */
 static int pico_socket_aggregate_mcastfilters(struct pico_ip4 *mcast_link, struct pico_ip4 *mcast_group)
 {
-  uint8_t filter_mode = PICO_IP_MULTICAST_INCLUDE;
-  struct pico_mcast_listen *listen = NULL;
-  struct pico_ip4 *source = NULL;
-  struct pico_socket *mcast_sock = NULL;
-  struct pico_tree_node *index = NULL, *_tmp = NULL, *index2 = NULL, *_tmp2 = NULL;
+    uint8_t filter_mode = PICO_IP_MULTICAST_INCLUDE;
+    struct pico_mcast_listen *listen = NULL;
+    struct pico_ip4 *source = NULL;
+    struct pico_socket *mcast_sock = NULL;
+    struct pico_tree_node *index = NULL, *_tmp = NULL, *index2 = NULL, *_tmp2 = NULL;
 
 
-  /* cleanup old filter */
-  pico_tree_foreach_safe(index, &MCASTFilter, _tmp)
-  {
-    pico_tree_delete(&MCASTFilter, index->keyValue);
-  }
+    /* cleanup old filter */
+    pico_tree_foreach_safe(index, &MCASTFilter, _tmp)
+    {
+        pico_tree_delete(&MCASTFilter, index->keyValue);
+    }
 
-  /* construct new filter */
-  pico_tree_foreach(index, &MCASTSockets)
-  {
-    mcast_sock = index->keyValue;
-    listen = listen_find(mcast_sock, mcast_link, mcast_group);
-    if (listen) {
-      /* aggregate filter */
-      switch(filter_mode)
-      {
-        case PICO_IP_MULTICAST_INCLUDE:
-          switch (listen->filter_mode)
-          {
+    /* construct new filter */
+    pico_tree_foreach(index, &MCASTSockets)
+    {
+        mcast_sock = index->keyValue;
+        listen = listen_find(mcast_sock, mcast_link, mcast_group);
+        if (listen) {
+            /* aggregate filter */
+            switch(filter_mode)
+            {
             case PICO_IP_MULTICAST_INCLUDE:
-              /* filter = summation of INCLUDEs */
-              /* mode stays INCLUDE, add all sources to filter */
-              pico_tree_foreach(index2, &listen->MCASTSources)
-              {
-                source = index2->keyValue;
-                pico_tree_insert(&MCASTFilter, source); 
-              }
-              break;
+                switch (listen->filter_mode)
+                {
+                case PICO_IP_MULTICAST_INCLUDE:
+                    /* filter = summation of INCLUDEs */
+                    /* mode stays INCLUDE, add all sources to filter */
+                    pico_tree_foreach(index2, &listen->MCASTSources)
+                    {
+                        source = index2->keyValue;
+                        pico_tree_insert(&MCASTFilter, source);
+                    }
+                    break;
+
+                case PICO_IP_MULTICAST_EXCLUDE:
+                    /* filter = EXCLUDE - INCLUDE */
+                    /* delete from the interface INCLUDE filter any source NOT in the socket EXCLUDE filter */
+                    pico_tree_foreach_safe(index2, &MCASTFilter, _tmp2)
+                    {
+                        source = pico_tree_findKey(&listen->MCASTSources, index2->keyValue);
+                        if (!source)
+                            pico_tree_delete(&MCASTFilter, index2->keyValue);
+                    }
+                    /* any record with filter mode EXCLUDE, causes the interface mode to be EXCLUDE */
+                    filter_mode = PICO_IP_MULTICAST_EXCLUDE;
+                    /* add to the interface EXCLUDE filter any socket source NOT in the former interface INCLUDE filter */
+                    pico_tree_foreach(index2, &listen->MCASTSources)
+                    {
+                        source = pico_tree_insert(&MCASTFilter, index2->keyValue);
+                        if (source)
+                            pico_tree_delete(&MCASTFilter, source);
+                    }
+                    break;
+
+                default:
+                    return -1;
+                }
+                break;
 
             case PICO_IP_MULTICAST_EXCLUDE:
-              /* filter = EXCLUDE - INCLUDE */
-              /* delete from the interface INCLUDE filter any source NOT in the socket EXCLUDE filter */
-              pico_tree_foreach_safe(index2, &MCASTFilter, _tmp2)
-              {
-                source = pico_tree_findKey(&listen->MCASTSources, index2->keyValue);
-                if (!source)
-                  pico_tree_delete(&MCASTFilter, index2->keyValue);
-              }
-              /* any record with filter mode EXCLUDE, causes the interface mode to be EXCLUDE */
-              filter_mode = PICO_IP_MULTICAST_EXCLUDE;
-              /* add to the interface EXCLUDE filter any socket source NOT in the former interface INCLUDE filter */
-              pico_tree_foreach(index2, &listen->MCASTSources)
-              {
-                source = pico_tree_insert(&MCASTFilter, index2->keyValue); 
-                if (source) 
-                  pico_tree_delete(&MCASTFilter, source);
-              }
-              break;
+                switch (listen->filter_mode)
+                {
+                case PICO_IP_MULTICAST_INCLUDE:
+                    /* filter = EXCLUDE - INCLUDE */
+                    /* any record with filter mode EXCLUDE, causes the interface mode to be EXCLUDE */
+                    /* remove from the interface EXCLUDE filter any source in the socket INCLUDE filter */
+                    pico_tree_foreach(index2, &listen->MCASTSources)
+                    {
+                        source = pico_tree_findKey(&MCASTFilter, index2->keyValue);
+                        if (source)
+                            pico_tree_delete(&MCASTFilter, source);
+                    }
+                    break;
+
+                case PICO_IP_MULTICAST_EXCLUDE:
+                    /* filter = intersection of EXCLUDEs */
+                    /* any record with filter mode EXCLUDE, causes the interface mode to be EXCLUDE */
+                    /* remove from the interface EXCLUDE filter any source not in the socket EXCLUDE filter */
+                    pico_tree_foreach_safe(index2, &MCASTFilter, _tmp2)
+                    {
+                        source = pico_tree_findKey(&listen->MCASTSources, index2->keyValue);
+                        if (!source)
+                            pico_tree_delete(&MCASTFilter, index2->keyValue);
+                    }
+                    break;
+
+                default:
+                    return -1;
+                }
+                break;
 
             default:
-              return -1;
-          }
-          break;
-
-        case PICO_IP_MULTICAST_EXCLUDE:
-          switch (listen->filter_mode)
-          {
-            case PICO_IP_MULTICAST_INCLUDE:
-              /* filter = EXCLUDE - INCLUDE */
-              /* any record with filter mode EXCLUDE, causes the interface mode to be EXCLUDE */
-              /* remove from the interface EXCLUDE filter any source in the socket INCLUDE filter */
-              pico_tree_foreach(index2, &listen->MCASTSources)
-              {
-                source = pico_tree_findKey(&MCASTFilter, index2->keyValue);
-                if (source)
-                  pico_tree_delete(&MCASTFilter, source);
-              }
-              break;
-
-            case PICO_IP_MULTICAST_EXCLUDE:
-              /* filter = intersection of EXCLUDEs */
-              /* any record with filter mode EXCLUDE, causes the interface mode to be EXCLUDE */
-              /* remove from the interface EXCLUDE filter any source not in the socket EXCLUDE filter */
-              pico_tree_foreach_safe(index2, &MCASTFilter, _tmp2)
-              {
-                source = pico_tree_findKey(&listen->MCASTSources, index2->keyValue);
-                if (!source)
-                  pico_tree_delete(&MCASTFilter, index2->keyValue);
-              }
-              break;
-
-            default:
-              return -1;
-          }
-          break;
-
-        default:
-          return -1;
-      }
+                return -1;
+            }
+        }
     }
-  }
-  return filter_mode;
+    return filter_mode;
 }
 
 static int pico_socket_mcast_filter(struct pico_socket *s, struct pico_ip4 *mcast_group, struct pico_ip4 *src)
 {
-  struct pico_ipv4_link *mcast_link = NULL;
-  struct pico_mcast_listen *listen = NULL;
-  struct pico_tree_node *index = NULL;
+    struct pico_ipv4_link *mcast_link = NULL;
+    struct pico_mcast_listen *listen = NULL;
+    struct pico_tree_node *index = NULL;
 
-  /* no multicast enabled on socket */
-  if (!s->MCASTListen)
-    return 0;
+    /* no multicast enabled on socket */
+    if (!s->MCASTListen)
+        return 0;
 
-  mcast_link = pico_ipv4_link_get(&s->local_addr.ip4);
-  if (!mcast_link) 
-    return -1;
-  listen = listen_find(s, &mcast_link->address, mcast_group);
-  if (!listen)
-    return -1;
+    mcast_link = pico_ipv4_link_get(&s->local_addr.ip4);
+    if (!mcast_link)
+        return -1;
 
-  /* perform source filtering */
-  switch (listen->filter_mode)
-  {
+    listen = listen_find(s, &mcast_link->address, mcast_group);
+    if (!listen)
+        return -1;
+
+    /* perform source filtering */
+    switch (listen->filter_mode)
+    {
     case PICO_IP_MULTICAST_INCLUDE:
-      pico_tree_foreach(index, &listen->MCASTSources)
-      {
-        if (src->addr == ((struct pico_ip4 *)index->keyValue)->addr) {
-          so_mcast_dbg("MCAST: IP %08X in included socket source list\n", src->addr);
-          return 0;
+        pico_tree_foreach(index, &listen->MCASTSources)
+        {
+            if (src->addr == ((struct pico_ip4 *)index->keyValue)->addr) {
+                so_mcast_dbg("MCAST: IP %08X in included socket source list\n", src->addr);
+                return 0;
+            }
         }
-      }
-      so_mcast_dbg("MCAST: IP %08X NOT in included socket source list\n", src->addr);
-      return -1;
-      break;
+        so_mcast_dbg("MCAST: IP %08X NOT in included socket source list\n", src->addr);
+        return -1;
+        break;
 
     case PICO_IP_MULTICAST_EXCLUDE:
-      pico_tree_foreach(index, &listen->MCASTSources)
-      {
-        if (src->addr == ((struct pico_ip4 *)index->keyValue)->addr) {
-          so_mcast_dbg("MCAST: IP %08X in excluded socket source list\n", src->addr);
-          return -1;
+        pico_tree_foreach(index, &listen->MCASTSources)
+        {
+            if (src->addr == ((struct pico_ip4 *)index->keyValue)->addr) {
+                so_mcast_dbg("MCAST: IP %08X in excluded socket source list\n", src->addr);
+                return -1;
+            }
         }
-      }
-      so_mcast_dbg("MCAST: IP %08X NOT in excluded socket source list\n", src->addr);
-      return 0;
-      break;
+        so_mcast_dbg("MCAST: IP %08X NOT in excluded socket source list\n", src->addr);
+        return 0;
+        break;
 
     default:
-      return -1;
-      break;
-  }
-  return -1;
+        return -1;
+        break;
+    }
+    return -1;
 }
 
 static inline struct pico_ipv4_link *pico_socket_setoption_mcastargs_validation(struct pico_ip_mreq *mreq, struct pico_ip_mreq_source *mreq_source)
 {
-  struct pico_ipv4_link *mcast_link = NULL;
+    struct pico_ipv4_link *mcast_link = NULL;
 
-  if (!mreq && !mreq_source)
-    return NULL;
+    if (!mreq && !mreq_source)
+        return NULL;
+
+    if (mreq) {
+        if (!mreq->mcast_group_addr.addr)
+            return NULL;
 
-  if (mreq) {
-    if (!mreq->mcast_group_addr.addr)
-      return NULL;
-    if (pico_ipv4_is_unicast(mreq->mcast_group_addr.addr))
-      return NULL;
+        if (pico_ipv4_is_unicast(mreq->mcast_group_addr.addr))
+            return NULL;
 
-    if (!mreq->mcast_link_addr.addr) {
-      mcast_link = pico_ipv4_get_default_mcastlink();
-      if (!mcast_link)
-        return NULL;
-    } else {
-      mcast_link = pico_ipv4_link_get(&mreq->mcast_link_addr);
-      if (!mcast_link)
-        return NULL;
+        if (!mreq->mcast_link_addr.addr) {
+            mcast_link = pico_ipv4_get_default_mcastlink();
+            if (!mcast_link)
+                return NULL;
+        } else {
+            mcast_link = pico_ipv4_link_get(&mreq->mcast_link_addr);
+            if (!mcast_link)
+                return NULL;
+        }
     }
-  }
-  if (mreq_source) {
-    if (!mreq_source->mcast_group_addr.addr)
-      return NULL;
-    if (pico_ipv4_is_unicast(mreq_source->mcast_group_addr.addr))
-      return NULL;
+
+    if (mreq_source) {
+        if (!mreq_source->mcast_group_addr.addr)
+            return NULL;
 
-    if (!mreq_source->mcast_source_addr.addr)
-      return NULL;
-    if (!pico_ipv4_is_unicast(mreq_source->mcast_source_addr.addr))
-      return NULL;
+        if (pico_ipv4_is_unicast(mreq_source->mcast_group_addr.addr))
+            return NULL;
+
+        if (!mreq_source->mcast_source_addr.addr)
+            return NULL;
 
-    if (!mreq_source->mcast_link_addr.addr) {
-      mcast_link = pico_ipv4_get_default_mcastlink();
-      if (!mcast_link)
-        return NULL;
-    } else {
-      mcast_link = pico_ipv4_link_get(&mreq_source->mcast_link_addr);
-      if (!mcast_link)
-        return NULL;
+        if (!pico_ipv4_is_unicast(mreq_source->mcast_source_addr.addr))
+            return NULL;
+
+        if (!mreq_source->mcast_link_addr.addr) {
+            mcast_link = pico_ipv4_get_default_mcastlink();
+            if (!mcast_link)
+                return NULL;
+        } else {
+            mcast_link = pico_ipv4_link_get(&mreq_source->mcast_link_addr);
+            if (!mcast_link)
+                return NULL;
+        }
     }
-  }
-  return mcast_link;
+
+    return mcast_link;
 }
 #else
-static int pico_socket_mcast_filter(struct pico_socket *s, struct pico_ip4 *mcast_group, struct pico_ip4 *src) {
-  return 0;
+static int pico_socket_mcast_filter(struct pico_socket *s, struct pico_ip4 *mcast_group, struct pico_ip4 *src)
+{
+    return 0;
 }
 #endif /* PICO_SUPPORT_MCAST */
 
 static struct pico_sockport *pico_get_sockport(uint16_t proto, uint16_t port)
 {
-  struct pico_sockport test = INIT_SOCKPORT;
-  test.number = port;
+    struct pico_sockport test = INIT_SOCKPORT;
+    test.number = port;
 
-  if (proto == PICO_PROTO_UDP)
-    return pico_tree_findKey(&UDPTable,&test);
+    if (proto == PICO_PROTO_UDP)
+        return pico_tree_findKey(&UDPTable, &test);
 
-  else if (proto == PICO_PROTO_TCP)
-    return pico_tree_findKey(&TCPTable,&test);
+    else if (proto == PICO_PROTO_TCP)
+        return pico_tree_findKey(&TCPTable, &test);
 
-  else return NULL;
+    else return NULL;
 }
 
 int pico_is_port_free(uint16_t proto, uint16_t port, void *addr, void *net)
 {
-  struct pico_sockport *sp;
-  struct pico_ip4 ip;
-  sp = pico_get_sockport(proto, port);
+    struct pico_sockport *sp;
+    struct pico_ip4 ip;
+    sp = pico_get_sockport(proto, port);
 
-  if (!net)
-    net = &pico_proto_ipv4;
+    if (!net)
+        net = &pico_proto_ipv4;
 
-  /** IPv6 (wip) ***/
-  if (net != &pico_proto_ipv4) {
-    dbg("IPV6!!!!!\n");
-    return (!sp);
-  }
+    /** IPv6 (wip) ***/
+    if (net != &pico_proto_ipv4) {
+        dbg("IPV6!!!!!\n");
+        return (!sp);
+    }
 
-  /* IPv4 */
+    /* IPv4 */
 #ifdef PICO_SUPPORT_NAT
-  if (pico_ipv4_nat_find(port, NULL, 0, (uint8_t)proto)) {
-    dbg("In use by nat....\n");
-    return 0;
-  }
+    if (pico_ipv4_nat_find(port, NULL, 0, (uint8_t)proto)) {
+        dbg("In use by nat....\n");
+        return 0;
+    }
+
 #endif
-  if (addr)
-    ip.addr = ((struct pico_ip4 *)addr)->addr;
-  else
-    ip.addr = PICO_IPV4_INADDR_ANY;
+    if (addr)
+        ip.addr = ((struct pico_ip4 *)addr)->addr;
+    else
+        ip.addr = PICO_IPV4_INADDR_ANY;
+
+    if (ip.addr == PICO_IPV4_INADDR_ANY) {
+        if (!sp) return 1;
+        else {
+            dbg("In use, and asked for ANY\n");
+            return 0;
+        }
+    }
 
-  if (ip.addr == PICO_IPV4_INADDR_ANY) {
-    if (!sp) return 1;
-      else {
-        dbg("In use, and asked for ANY\n");
-        return 0;
-      }
-  }
-  if (sp) {
-    struct pico_ip4 *s_local;
-    struct pico_tree_node *idx;
-    struct pico_socket *s;
-    pico_tree_foreach(idx, &sp->socks) {
-      s = idx->keyValue;
-      if (s->net == &pico_proto_ipv4) {
-        s_local = (struct pico_ip4*) &s->local_addr;
-        if ((s_local->addr == PICO_IPV4_INADDR_ANY) || (s_local->addr == ip.addr))
-          return 0;
-      }
+    if (sp) {
+        struct pico_ip4 *s_local;
+        struct pico_tree_node *idx;
+        struct pico_socket *s;
+        pico_tree_foreach(idx, &sp->socks) {
+            s = idx->keyValue;
+            if (s->net == &pico_proto_ipv4) {
+                s_local = (struct pico_ip4*) &s->local_addr;
+                if ((s_local->addr == PICO_IPV4_INADDR_ANY) || (s_local->addr == ip.addr))
+                    return 0;
+            }
+        }
     }
-  }
-  return 1;
+
+    return 1;
 }
 
 static int pico_check_socket(struct pico_socket *s)
 {
-  struct pico_sockport *test;
-  struct pico_socket *found;
-  struct pico_tree_node * index;
+    struct pico_sockport *test;
+    struct pico_socket *found;
+    struct pico_tree_node *index;
+
+    test = pico_get_sockport(PROTO(s), s->local_port);
 
-  test = pico_get_sockport(PROTO(s), s->local_port);
-  
-  if (!test) {
+    if (!test) {
+        return -1;
+    }
+
+    pico_tree_foreach(index, &test->socks){
+        found = index->keyValue;
+        if (s == found) {
+            return 0;
+        }
+    }
+
     return -1;
-  }
-
-  pico_tree_foreach(index,&test->socks){
-    found = index->keyValue;
-    if (s == found) {
-      return 0;
-    }
-  }
-
-  return -1;
 }
 
-struct pico_socket* pico_sockets_find(uint16_t local,uint16_t remote)
+struct pico_socket*pico_sockets_find(uint16_t local, uint16_t remote)
 {
-	struct pico_socket * sock = NULL;
-	struct pico_tree_node *index = NULL;
-	struct pico_sockport *sp = NULL;
+    struct pico_socket *sock = NULL;
+    struct pico_tree_node *index = NULL;
+    struct pico_sockport *sp = NULL;
 
-	sp = pico_get_sockport(PICO_PROTO_TCP,local);
-	if(sp)
-	{
-		pico_tree_foreach(index,&sp->socks)
-		{
-			if( ((struct pico_socket *)index->keyValue)->remote_port == remote)
-			{
-				sock = (struct pico_socket *)index->keyValue;
-				break;
-			}
-		}
-	}
+    sp = pico_get_sockport(PICO_PROTO_TCP, local);
+    if(sp)
+    {
+        pico_tree_foreach(index, &sp->socks)
+        {
+            if(((struct pico_socket *)index->keyValue)->remote_port == remote)
+            {
+                sock = (struct pico_socket *)index->keyValue;
+                break;
+            }
+        }
+    }
 
-	return sock;
+    return sock;
 }
 
 
 int8_t pico_socket_add(struct pico_socket *s)
 {
-  struct pico_sockport *sp = pico_get_sockport(PROTO(s), s->local_port);
-  LOCK(Mutex);
-  if (!sp) {
-    //dbg("Creating sockport..%04x\n", s->local_port); /* In comment due to spam during test */
-    sp = pico_zalloc(sizeof(struct pico_sockport));
-
+    struct pico_sockport *sp = pico_get_sockport(PROTO(s), s->local_port);
+    LOCK(Mutex);
     if (!sp) {
-      pico_err = PICO_ERR_ENOMEM;
-      UNLOCK(Mutex);
-      return -1;
-    }
-    sp->proto = PROTO(s);
-    sp->number = s->local_port;
-    sp->socks.root = &LEAF;
-    sp->socks.compare = socket_cmp;
+        /* dbg("Creating sockport..%04x\n", s->local_port); / * In comment due to spam during test * / */
+        sp = pico_zalloc(sizeof(struct pico_sockport));
+
+        if (!sp) {
+            pico_err = PICO_ERR_ENOMEM;
+            UNLOCK(Mutex);
+            return -1;
+        }
 
-    if (PROTO(s) == PICO_PROTO_UDP)
-    {
-      pico_tree_insert(&UDPTable,sp);
-    }
-    else if (PROTO(s) == PICO_PROTO_TCP)
-    {
-      pico_tree_insert(&TCPTable,sp);
-    }
-  }
+        sp->proto = PROTO(s);
+        sp->number = s->local_port;
+        sp->socks.root = &LEAF;
+        sp->socks.compare = socket_cmp;
 
-  pico_tree_insert(&sp->socks,s);
-  s->state |= PICO_SOCKET_STATE_BOUND;
-	UNLOCK(Mutex);
-#if DEBUG_SOCKET_TREE
-  {
-    struct pico_tree_node * index;
-    //RB_FOREACH(s, socket_tree, &sp->socks) {
-    pico_tree_foreach(index,&sp->socks){
-      s = index->keyValue;
-      dbg(">>>> List Socket lc=%hu rm=%hu\n", short_be(s->local_port), short_be(s->remote_port));
+        if (PROTO(s) == PICO_PROTO_UDP)
+        {
+            pico_tree_insert(&UDPTable, sp);
+        }
+        else if (PROTO(s) == PICO_PROTO_TCP)
+        {
+            pico_tree_insert(&TCPTable, sp);
+        }
     }
 
-  }
+    pico_tree_insert(&sp->socks, s);
+    s->state |= PICO_SOCKET_STATE_BOUND;
+    UNLOCK(Mutex);
+#if DEBUG_SOCKET_TREE
+    {
+        struct pico_tree_node *index;
+        /* RB_FOREACH(s, socket_tree, &sp->socks) { */
+        pico_tree_foreach(index, &sp->socks){
+            s = index->keyValue;
+            dbg(">>>> List Socket lc=%hu rm=%hu\n", short_be(s->local_port), short_be(s->remote_port));
+        }
+
+    }
 #endif
-  return 0;
+    return 0;
 }
 
 static void socket_clean_queues(struct pico_socket *sock)
 {
-  struct pico_frame * f_in = pico_dequeue(&sock->q_in);
-  struct pico_frame * f_out = pico_dequeue(&sock->q_out);
-  while(f_in || f_out)
-  {
-    if(f_in)
+    struct pico_frame *f_in = pico_dequeue(&sock->q_in);
+    struct pico_frame *f_out = pico_dequeue(&sock->q_out);
+    while(f_in || f_out)
     {
-      pico_frame_discard(f_in);
-      f_in = pico_dequeue(&sock->q_in);
-    }
+        if(f_in)
+        {
+            pico_frame_discard(f_in);
+            f_in = pico_dequeue(&sock->q_in);
+        }
 
-    if(f_out)
-    {
-      pico_frame_discard(f_out);
-      f_out = pico_dequeue(&sock->q_out);
+        if(f_out)
+        {
+            pico_frame_discard(f_out);
+            f_out = pico_dequeue(&sock->q_out);
+        }
     }
-  }
-  // for tcp sockets go further and clean the sockets inside queue
-  if(sock->proto == &pico_proto_tcp)
-    pico_tcp_cleanup_queues(sock);
+    /* for tcp sockets go further and clean the sockets inside queue */
+    if(sock->proto == &pico_proto_tcp)
+        pico_tcp_cleanup_queues(sock);
 }
 
 static void socket_garbage_collect(pico_time now, void *arg)
 {
-  struct pico_socket *s = (struct pico_socket *) arg;
-  IGNORE_PARAMETER(now);
+    struct pico_socket *s = (struct pico_socket *) arg;
+    IGNORE_PARAMETER(now);
 
-  if(s->parent)
-	  s->parent->number_of_pending_conn--;
+#ifdef PICO_SUPPORT_TCP
+    if(s->parent)
+        s->parent->number_of_pending_conn--;
 
-  socket_clean_queues(s);
-  pico_free(s);
+#endif
+
+    socket_clean_queues(s);
+    pico_free(s);
 }
 
 int8_t pico_socket_del(struct pico_socket *s)
 {
-  struct pico_sockport *sp = pico_get_sockport(PROTO(s), s->local_port);
-
-  if (!sp) {
-    pico_err = PICO_ERR_ENXIO;
-    return -1;
-  }
+    struct pico_sockport *sp = pico_get_sockport(PROTO(s), s->local_port);
 
-  LOCK(Mutex);
-  pico_tree_delete(&sp->socks,s);
-  s->net = NULL;
-  if(pico_tree_empty(&sp->socks)){
-    if (PROTO(s) == PICO_PROTO_UDP)
-    {
-      pico_tree_delete(&UDPTable,sp);
-    }
-    else if (PROTO(s) == PICO_PROTO_TCP)
-    {
-      pico_tree_delete(&TCPTable,sp);
+    if (!sp) {
+        pico_err = PICO_ERR_ENXIO;
+        return -1;
     }
 
-    if(sp_tcp == sp)  sp_tcp = NULL;
-
-    if(sp_udp == sp)   sp_udp = NULL;
+    LOCK(Mutex);
+    pico_tree_delete(&sp->socks, s);
+    s->net = NULL;
+    if(pico_tree_empty(&sp->socks)) {
+        if (PROTO(s) == PICO_PROTO_UDP)
+        {
+            pico_tree_delete(&UDPTable, sp);
+        }
+        else if (PROTO(s) == PICO_PROTO_TCP)
+        {
+            pico_tree_delete(&TCPTable, sp);
+        }
 
-    pico_free(sp);
+        if(sp_tcp == sp) sp_tcp = NULL;
+
+        if(sp_udp == sp) sp_udp = NULL;
 
-  }
+        pico_free(sp);
+
+    }
 
 #ifdef PICO_SUPPORT_MCAST
-  do {
-    int filter_mode;
-    struct pico_tree_node *index = NULL, *_tmp = NULL, *index2 = NULL, *_tmp2 = NULL;
-    struct pico_mcast_listen *listen = NULL;
-    struct pico_ip4 *source = NULL;
-    if (s->MCASTListen) {
-      pico_tree_delete(&MCASTSockets, s);
-      pico_tree_foreach_safe(index, s->MCASTListen, _tmp)
-      {
-        listen = index->keyValue;
-        pico_tree_foreach_safe(index2, &listen->MCASTSources, _tmp2)
-        {
-          source = index->keyValue;
-          pico_tree_delete(&listen->MCASTSources, source);
-          pico_free(source);
+    do {
+        int filter_mode;
+        struct pico_tree_node *index = NULL, *_tmp = NULL, *index2 = NULL, *_tmp2 = NULL;
+        struct pico_mcast_listen *listen = NULL;
+        struct pico_ip4 *source = NULL;
+        if (s->MCASTListen) {
+            pico_tree_delete(&MCASTSockets, s);
+            pico_tree_foreach_safe(index, s->MCASTListen, _tmp)
+            {
+                listen = index->keyValue;
+                pico_tree_foreach_safe(index2, &listen->MCASTSources, _tmp2)
+                {
+                    source = index->keyValue;
+                    pico_tree_delete(&listen->MCASTSources, source);
+                    pico_free(source);
+                }
+                filter_mode = pico_socket_aggregate_mcastfilters(&listen->mcast_link, &listen->mcast_group);
+                if (filter_mode >= 0)
+                    pico_ipv4_mcast_leave(&listen->mcast_link, &listen->mcast_group, 1, (uint8_t)filter_mode, &MCASTFilter);
+
+                pico_tree_delete(s->MCASTListen, listen);
+                pico_free(listen);
+            }
+            pico_free(s->MCASTListen);
         }
-        filter_mode = pico_socket_aggregate_mcastfilters(&listen->mcast_link, &listen->mcast_group);
-        if (filter_mode >= 0) 
-          pico_ipv4_mcast_leave(&listen->mcast_link, &listen->mcast_group, 1, (uint8_t)filter_mode, &MCASTFilter);
-        pico_tree_delete(s->MCASTListen, listen);
-        pico_free(listen);
-      }
-      pico_free(s->MCASTListen);
-    }
-  } while (0);
+    } while (0);
 #endif
 
-  s->state = PICO_SOCKET_STATE_CLOSED;
-  pico_timer_add(3000, socket_garbage_collect, s);
-	UNLOCK(Mutex);
-  return 0;
+    s->state = PICO_SOCKET_STATE_CLOSED;
+    pico_timer_add(3000, socket_garbage_collect, s);
+    UNLOCK(Mutex);
+    return 0;
 }
 
 static int8_t pico_socket_alter_state(struct pico_socket *s, uint16_t more_states, uint16_t less_states, uint16_t tcp_state)
 {
-  struct pico_sockport *sp;
-  if (more_states & PICO_SOCKET_STATE_BOUND)
-    return pico_socket_add(s);
+    struct pico_sockport *sp;
+    if (more_states & PICO_SOCKET_STATE_BOUND)
+        return pico_socket_add(s);
 
-  if (less_states & PICO_SOCKET_STATE_BOUND)
-    return pico_socket_del(s);
+    if (less_states & PICO_SOCKET_STATE_BOUND)
+        return pico_socket_del(s);
 
-  sp = pico_get_sockport(PROTO(s), s->local_port);
-  if (!sp) {
-    pico_err = PICO_ERR_ENXIO;
-    return -1;
-  }
+    sp = pico_get_sockport(PROTO(s), s->local_port);
+    if (!sp) {
+        pico_err = PICO_ERR_ENXIO;
+        return -1;
+    }
 
-  s->state |= more_states;
-  s->state = (uint16_t)(s->state & (~less_states));
-  if (tcp_state) {
-    s->state &= 0x00FF;
-    s->state |= tcp_state;
-  }
+    s->state |= more_states;
+    s->state = (uint16_t)(s->state & (~less_states));
+    if (tcp_state) {
+        s->state &= 0x00FF;
+        s->state |= tcp_state;
+    }
 
-  return 0;
+    return 0;
 }
 
 static int pico_socket_deliver(struct pico_protocol *p, struct pico_frame *f, uint16_t localport)
 {
-  struct pico_frame *cpy = NULL;
-  struct pico_sockport *sp = NULL;
-  struct pico_socket *s = NULL;
-  struct pico_tree_node *index = NULL;
-  struct pico_tree_node *_tmp;
-  struct pico_trans *tr = (struct pico_trans *) f->transport_hdr;
+    struct pico_frame *cpy = NULL;
+    struct pico_sockport *sp = NULL;
+    struct pico_socket *s = NULL;
+    struct pico_tree_node *index = NULL;
+    struct pico_tree_node *_tmp;
+    struct pico_trans *tr = (struct pico_trans *) f->transport_hdr;
   #ifdef PICO_SUPPORT_IPV4
-  struct pico_ipv4_hdr *ip4hdr;
+    struct pico_ipv4_hdr *ip4hdr;
   #endif
   #ifdef PICO_SUPPORT_IPV6
-  struct pico_ipv6_hdr *ip6hdr;
+    struct pico_ipv6_hdr *ip6hdr;
   #endif
 
 #ifdef PICO_SUPPORT_TCP
-  struct pico_socket *found = NULL;
+    struct pico_socket *found = NULL;
 #endif
 
-  if (!tr)
-    return -1;
+    if (!tr)
+        return -1;
 
-  sp = pico_get_sockport(p->proto_number, localport);
+    sp = pico_get_sockport(p->proto_number, localport);
 
-  if (!sp) {
-    dbg("No such port %d\n",short_be(localport));
-    return -1;
-  }
+    if (!sp) {
+        dbg("No such port %d\n", short_be(localport));
+        return -1;
+    }
 
   #ifdef PICO_SUPPORT_TCP
-  if (p->proto_number == PICO_PROTO_TCP) {
-    pico_tree_foreach_safe(index,&sp->socks, _tmp){
-      s = index->keyValue;
-      /* 4-tuple identification of socket (port-IP) */
+    if (p->proto_number == PICO_PROTO_TCP) {
+        pico_tree_foreach_safe(index, &sp->socks, _tmp){
+            s = index->keyValue;
+            /* 4-tuple identification of socket (port-IP) */
       #ifdef PICO_SUPPORT_IPV4
-      if (IS_IPV4(f)) {
-        struct pico_ip4 s_local, s_remote, p_src, p_dst;
-        ip4hdr = (struct pico_ipv4_hdr*)(f->net_hdr);
-        s_local.addr = s->local_addr.ip4.addr;
-        s_remote.addr = s->remote_addr.ip4.addr;
-        p_src.addr = ip4hdr->src.addr;
-        p_dst.addr = ip4hdr->dst.addr;
-        if (  (s->remote_port == tr->sport) && /* remote port check */
-              (s_remote.addr == p_src.addr) && /* remote addr check */ 
-              ((s_local.addr == PICO_IPV4_INADDR_ANY) || (s_local.addr == p_dst.addr))) { /* Either local socket is ANY, or matches dst */
-          found = s;
-          break;
-        } else if ( (s->remote_port == 0)  && /* not connected... listening */
-         ((s_local.addr == PICO_IPV4_INADDR_ANY) || (s_local.addr == p_dst.addr))) { /* Either local socket is ANY, or matches dst */
-          /* listen socket */
-          found = s;
-        }
-      }
+            if (IS_IPV4(f)) {
+                struct pico_ip4 s_local, s_remote, p_src, p_dst;
+                ip4hdr = (struct pico_ipv4_hdr*)(f->net_hdr);
+                s_local.addr = s->local_addr.ip4.addr;
+                s_remote.addr = s->remote_addr.ip4.addr;
+                p_src.addr = ip4hdr->src.addr;
+                p_dst.addr = ip4hdr->dst.addr;
+                if ((s->remote_port == tr->sport) && /* remote port check */
+                    (s_remote.addr == p_src.addr) && /* remote addr check */
+                    ((s_local.addr == PICO_IPV4_INADDR_ANY) || (s_local.addr == p_dst.addr))) { /* Either local socket is ANY, or matches dst */
+                    found = s;
+                    break;
+                } else if ((s->remote_port == 0)  && /* not connected... listening */
+                           ((s_local.addr == PICO_IPV4_INADDR_ANY) || (s_local.addr == p_dst.addr))) { /* Either local socket is ANY, or matches dst */
+                    /* listen socket */
+                    found = s;
+                }
+            }
+
       #endif
       #ifdef PICO_SUPPORT_IPV6    /* XXX TODO make compare for ipv6 addresses */
-      if (IS_IPV6(f)) {
-        ip6hdr = (struct pico_ipv6_hdr*)(f->net_hdr);
-        if ( (s->remote_port == localport) ) { // && (((struct pico_ip6) s->remote_addr.ip6).addr == ((struct pico_ip6)(ip6hdr->src)).addr) ) {
-          found = s;
-          break;
-        } else if (s->remote_port == 0) {
-          /* listen socket */
-          found = s;
+            if (IS_IPV6(f)) {
+                ip6hdr = (struct pico_ipv6_hdr*)(f->net_hdr);
+                if ((s->remote_port == localport)) { /* && (((struct pico_ip6) s->remote_addr.ip6).addr == ((struct pico_ip6)(ip6hdr->src)).addr) ) { */
+                    found = s;
+                    break;
+                } else if (s->remote_port == 0) {
+                    /* listen socket */
+                    found = s;
+                }
+            }
+
+      #endif
+        } /* FOREACH */
+        if (found != NULL) {
+            pico_tcp_input(found, f);
+            if ((found->ev_pending) && found->wakeup) {
+                found->wakeup(found->ev_pending, found);
+                if(!found->parent)
+                    found->ev_pending = 0;
+            }
+
+            return 0;
+        } else {
+            dbg("SOCKET> mmm something wrong (prob sockport)\n");
+            return -1;
         }
-      }
-      #endif 
-    } /* FOREACH */
-    if (found != NULL) {
-      pico_tcp_input(found,f);
-      if ((found->ev_pending) && found->wakeup) {
-        found->wakeup(found->ev_pending, found);
-        if(!found->parent)
-        	found->ev_pending = 0;
-      }
-      return 0;
-    } else {
-      dbg("SOCKET> mmm something wrong (prob sockport)\n");
-      return -1;
-    }
-  } /* TCP CASE */
+    } /* TCP CASE */
+
 #endif
 
 #ifdef PICO_SUPPORT_UDP
-  if (p->proto_number == PICO_PROTO_UDP) {
-    pico_tree_foreach_safe(index,&sp->socks, _tmp){
-      s = index->keyValue;
-      if (IS_IPV4(f)) { /* IPV4 */
-        struct pico_ip4 s_local, p_dst;
-        ip4hdr = (struct pico_ipv4_hdr*)(f->net_hdr);
-        s_local.addr = s->local_addr.ip4.addr;
-        p_dst.addr = ip4hdr->dst.addr;
-        if ((pico_ipv4_is_broadcast(p_dst.addr)) || pico_ipv4_is_multicast(p_dst.addr)) {
-          struct pico_device *dev = pico_ipv4_link_find(&s->local_addr.ip4);
-          if (pico_ipv4_is_multicast(p_dst.addr) && (pico_socket_mcast_filter(s, &ip4hdr->dst, &ip4hdr->src) < 0))
-            return -1;
-          if ((s_local.addr == PICO_IPV4_INADDR_ANY) || /* If our local ip is ANY, or.. */
-            (dev == f->dev) ) { /* the source of the bcast packet is a neighbor... */
-            cpy = pico_frame_copy(f);
-            if (!cpy)
-              return -1;
-            if (pico_enqueue(&s->q_in, cpy) > 0) {
-              if (s->wakeup)
-                s->wakeup(PICO_SOCK_EV_RD, s);
-            }
-            else
-            	pico_frame_discard(cpy);
+    if (p->proto_number == PICO_PROTO_UDP) {
+        pico_tree_foreach_safe(index, &sp->socks, _tmp){
+            s = index->keyValue;
+            if (IS_IPV4(f)) { /* IPV4 */
+                struct pico_ip4 s_local, p_dst;
+                ip4hdr = (struct pico_ipv4_hdr*)(f->net_hdr);
+                s_local.addr = s->local_addr.ip4.addr;
+                p_dst.addr = ip4hdr->dst.addr;
+                if ((pico_ipv4_is_broadcast(p_dst.addr)) || pico_ipv4_is_multicast(p_dst.addr)) {
+                    struct pico_device *dev = pico_ipv4_link_find(&s->local_addr.ip4);
+                    if (pico_ipv4_is_multicast(p_dst.addr) && (pico_socket_mcast_filter(s, &ip4hdr->dst, &ip4hdr->src) < 0))
+                        return -1;
+
+                    if ((s_local.addr == PICO_IPV4_INADDR_ANY) || /* If our local ip is ANY, or.. */
+                        (dev == f->dev)) { /* the source of the bcast packet is a neighbor... */
+                        cpy = pico_frame_copy(f);
+                        if (!cpy)
+                            return -1;
+
+                        if (pico_enqueue(&s->q_in, cpy) > 0) {
+                            if (s->wakeup)
+                                s->wakeup(PICO_SOCK_EV_RD, s);
+                        }
+                        else
+                            pico_frame_discard(cpy);
 
-          }
-        } else if ((s_local.addr == PICO_IPV4_INADDR_ANY) || (s_local.addr == p_dst.addr))
-        { /* Either local socket is ANY, or matches dst */
-          cpy = pico_frame_copy(f);
-          if (!cpy)
+                    }
+                } else if ((s_local.addr == PICO_IPV4_INADDR_ANY) || (s_local.addr == p_dst.addr))
+                { /* Either local socket is ANY, or matches dst */
+                    cpy = pico_frame_copy(f);
+                    if (!cpy)
+                        return -1;
+
+                    if (pico_enqueue(&s->q_in, cpy) > 0) {
+                        if (s->wakeup)
+                            s->wakeup(PICO_SOCK_EV_RD, s);
+                    }
+                    else
+                        pico_frame_discard(cpy);
+                }
+            } else {
+                /*... IPv6 */
+            }
+        } /* FOREACH */
+        pico_frame_discard(f);
+        if (s)
+            return 0;
+        else
             return -1;
-          if (pico_enqueue(&s->q_in, cpy) > 0) {
-            if (s->wakeup)
-              s->wakeup(PICO_SOCK_EV_RD, s);
-          }
-          else
-        	  pico_frame_discard(cpy);
-        }
-      } else {
-        /*... IPv6 */
-      }
-    } /* FOREACH */
-    pico_frame_discard(f);
-    if (s)
-      return 0;
-    else
-      return -1;
-  }
+    }
+
 #endif
-  return -1;
+    return -1;
 }
 
 struct pico_socket *pico_socket_open(uint16_t net, uint16_t proto, void (*wakeup)(uint16_t ev, struct pico_socket *))
 {
 
-  struct pico_socket *s = NULL;
+    struct pico_socket *s = NULL;
 
 #ifdef PICO_SUPPORT_UDP
-  if (proto == PICO_PROTO_UDP) {
-    s = pico_udp_open();
-    s->proto = &pico_proto_udp;
-    s->q_in.overhead = s->q_out.overhead = UDP_FRAME_OVERHEAD;
-  }
+    if (proto == PICO_PROTO_UDP) {
+        s = pico_udp_open();
+        s->proto = &pico_proto_udp;
+        s->q_in.overhead = s->q_out.overhead = UDP_FRAME_OVERHEAD;
+    }
+
 #endif
 
 #ifdef PICO_SUPPORT_TCP
-  if (proto == PICO_PROTO_TCP) {
-    s = pico_tcp_open();
-    s->proto = &pico_proto_tcp;
-    /*check if Nagle enabled */
-    if (!IS_NAGLE_ENABLED(s))
-      dbg("ERROR Nagle should be enabled here\n\n");
-  }
+    if (proto == PICO_PROTO_TCP) {
+        s = pico_tcp_open();
+        s->proto = &pico_proto_tcp;
+        /*check if Nagle enabled */
+        if (!IS_NAGLE_ENABLED(s))
+            dbg("ERROR Nagle should be enabled here\n\n");
+    }
+
 #endif
 
-  if (!s) {
-    pico_err = PICO_ERR_EPROTONOSUPPORT;
-    return NULL;
-  }
+    if (!s) {
+        pico_err = PICO_ERR_EPROTONOSUPPORT;
+        return NULL;
+    }
 
 #ifdef PICO_SUPPORT_IPV4
-  if (net == PICO_PROTO_IPV4)
-    s->net = &pico_proto_ipv4;
+    if (net == PICO_PROTO_IPV4)
+        s->net = &pico_proto_ipv4;
+
 #endif
 
 #ifdef PICO_SUPPORT_IPV6
-  if (net == PICO_PROTO_IPV6)
-    s->net = &pico_proto_ipv6;
+    if (net == PICO_PROTO_IPV6)
+        s->net = &pico_proto_ipv6;
+
 #endif
 
-  s->q_in.max_size = PICO_DEFAULT_SOCKETQ;
-  s->q_out.max_size = PICO_DEFAULT_SOCKETQ;
+    s->q_in.max_size = PICO_DEFAULT_SOCKETQ;
+    s->q_out.max_size = PICO_DEFAULT_SOCKETQ;
 
-  s->wakeup = wakeup;
+    s->wakeup = wakeup;
 
-  if (!s->net) {
-    pico_free(s);
-    pico_err = PICO_ERR_ENETUNREACH;
-    return NULL;
-  }
-  return s;
+    if (!s->net) {
+        pico_free(s);
+        pico_err = PICO_ERR_ENETUNREACH;
+        return NULL;
+    }
+
+    return s;
 }
 
 
 struct pico_socket *pico_socket_clone(struct pico_socket *facsimile)
 {
-  struct pico_socket *s = NULL;
+    struct pico_socket *s = NULL;
 
 #ifdef PICO_SUPPORT_UDP
-  if (facsimile->proto->proto_number == PICO_PROTO_UDP) {
-    s = pico_udp_open();
-    s->proto = &pico_proto_udp;
-    s->q_in.overhead = s->q_out.overhead = UDP_FRAME_OVERHEAD;
-  }
+    if (facsimile->proto->proto_number == PICO_PROTO_UDP) {
+        s = pico_udp_open();
+        s->proto = &pico_proto_udp;
+        s->q_in.overhead = s->q_out.overhead = UDP_FRAME_OVERHEAD;
+    }
+
 #endif
 
 #ifdef PICO_SUPPORT_TCP
-  if (facsimile->proto->proto_number == PICO_PROTO_TCP) {
-    s = pico_tcp_open();
-    s->proto = &pico_proto_tcp; 
-  }
+    if (facsimile->proto->proto_number == PICO_PROTO_TCP) {
+        s = pico_tcp_open();
+        s->proto = &pico_proto_tcp;
+    }
+
 #endif
 
-  if (!s) {
-    pico_err = PICO_ERR_EPROTONOSUPPORT;
-    return NULL;
-  }
-  s->local_port = facsimile->local_port;
-  s->remote_port = facsimile->remote_port;
-  s->state = facsimile->state;
+    if (!s) {
+        pico_err = PICO_ERR_EPROTONOSUPPORT;
+        return NULL;
+    }
+
+    s->local_port = facsimile->local_port;
+    s->remote_port = facsimile->remote_port;
+    s->state = facsimile->state;
 
 #ifdef PICO_SUPPORT_IPV4
-  if (facsimile->net == &pico_proto_ipv4) {
-    s->net = &pico_proto_ipv4;
-    memcpy(&s->local_addr, &facsimile->local_addr, sizeof(struct pico_ip4));
-    memcpy(&s->remote_addr, &facsimile->remote_addr, sizeof(struct pico_ip4));
-  }
+    if (facsimile->net == &pico_proto_ipv4) {
+        s->net = &pico_proto_ipv4;
+        memcpy(&s->local_addr, &facsimile->local_addr, sizeof(struct pico_ip4));
+        memcpy(&s->remote_addr, &facsimile->remote_addr, sizeof(struct pico_ip4));
+    }
+
 #endif
 
 #ifdef PICO_SUPPORT_IPV6
-  if (net == &pico_proto_ipv6) {
-    s->net = &pico_proto_ipv6;
-    memcpy(&s->local_addr, &facsimile->local_addr, sizeof(struct pico_ip6));
-    memcpy(&s->remote_addr, &facsimile->remote_addr, sizeof(struct pico_ip6));
-  }
+    if (net == &pico_proto_ipv6) {
+        s->net = &pico_proto_ipv6;
+        memcpy(&s->local_addr, &facsimile->local_addr, sizeof(struct pico_ip6));
+        memcpy(&s->remote_addr, &facsimile->remote_addr, sizeof(struct pico_ip6));
+    }
+
 #endif
-  s->q_in.max_size = PICO_DEFAULT_SOCKETQ;
-  s->q_out.max_size = PICO_DEFAULT_SOCKETQ;
-  s->wakeup = NULL;
-  if (!s->net) {
-    pico_free(s);
-    pico_err = PICO_ERR_ENETUNREACH;
-    return NULL;
-  }
-  return s;
+    s->q_in.max_size = PICO_DEFAULT_SOCKETQ;
+    s->q_out.max_size = PICO_DEFAULT_SOCKETQ;
+    s->wakeup = NULL;
+    if (!s->net) {
+        pico_free(s);
+        pico_err = PICO_ERR_ENETUNREACH;
+        return NULL;
+    }
+
+    return s;
 }
 
 int pico_socket_read(struct pico_socket *s, void *buf, int len)
 {
-  if (!s || buf == NULL) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  } else {
-    /* check if exists in tree */
-    /* See task #178 */
-    if (pico_check_socket(s) != 0) {
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
+    if (!s || buf == NULL) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    } else {
+        /* check if exists in tree */
+        /* See task #178 */
+        if (pico_check_socket(s) != 0) {
+            pico_err = PICO_ERR_EINVAL;
+            return -1;
+        }
     }
-  }
 
-  if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
-    pico_err = PICO_ERR_EIO;
-    return -1;
-  }
-#ifdef PICO_SUPPORT_UDP 
-  if (PROTO(s) == PICO_PROTO_UDP)
-    return pico_udp_recv(s, buf, (uint16_t)len, NULL, NULL);
+    if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
+        pico_err = PICO_ERR_EIO;
+        return -1;
+    }
+
+#ifdef PICO_SUPPORT_UDP
+    if (PROTO(s) == PICO_PROTO_UDP)
+        return pico_udp_recv(s, buf, (uint16_t)len, NULL, NULL);
+
 #endif
 
 #ifdef PICO_SUPPORT_TCP
-  if (PROTO(s) == PICO_PROTO_TCP){
-    /* check if in shutdown state and if no more data in tcpq_in */
-    if ((s->state & PICO_SOCKET_STATE_SHUT_REMOTE) && pico_tcp_queue_in_is_empty(s) ) {  
-      pico_err = PICO_ERR_ESHUTDOWN;
-      return -1;
-    } else {
-      return (int)pico_tcp_read(s, buf, (uint32_t)len);
+    if (PROTO(s) == PICO_PROTO_TCP) {
+        /* check if in shutdown state and if no more data in tcpq_in */
+        if ((s->state & PICO_SOCKET_STATE_SHUT_REMOTE) && pico_tcp_queue_in_is_empty(s)) {
+            pico_err = PICO_ERR_ESHUTDOWN;
+            return -1;
+        } else {
+            return (int)pico_tcp_read(s, buf, (uint32_t)len);
+        }
     }
-  }
+
 #endif
-  return 0;
+    return 0;
 }
 
 int pico_socket_write(struct pico_socket *s, const void *buf, int len)
 {
-  if (!s || buf == NULL) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  } else {
-    /* check if exists in tree */
-    /* See task #178 */
-    if (pico_check_socket(s) != 0) {
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
+    if (!s || buf == NULL) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    } else {
+        /* check if exists in tree */
+        /* See task #178 */
+        if (pico_check_socket(s) != 0) {
+            pico_err = PICO_ERR_EINVAL;
+            return -1;
+        }
     }
-  }
 
-  if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
-    pico_err = PICO_ERR_EIO;
-    return -1;
-  }
-  if ((s->state & PICO_SOCKET_STATE_CONNECTED) == 0) {
-    pico_err = PICO_ERR_ENOTCONN;
-    return -1;
-  } else if (s->state & PICO_SOCKET_STATE_SHUT_LOCAL) {  /* check if in shutdown state */
-    pico_err = PICO_ERR_ESHUTDOWN;
-    return -1;
-  } else {
-    return pico_socket_sendto(s, buf, len, &s->remote_addr, s->remote_port);
-  }
+    if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
+        pico_err = PICO_ERR_EIO;
+        return -1;
+    }
+
+    if ((s->state & PICO_SOCKET_STATE_CONNECTED) == 0) {
+        pico_err = PICO_ERR_ENOTCONN;
+        return -1;
+    } else if (s->state & PICO_SOCKET_STATE_SHUT_LOCAL) { /* check if in shutdown state */
+        pico_err = PICO_ERR_ESHUTDOWN;
+        return -1;
+    } else {
+        return pico_socket_sendto(s, buf, len, &s->remote_addr, s->remote_port);
+    }
 }
 
 uint16_t pico_socket_high_port(uint16_t proto)
 {
-  uint16_t port;
-  if (0 || 
+    uint16_t port;
+    if (0 ||
 #ifdef PICO_SUPPORT_TCP
-  (proto == PICO_PROTO_TCP) ||
+        (proto == PICO_PROTO_TCP) ||
 #endif
 #ifdef PICO_SUPPORT_TCP
-  (proto == PICO_PROTO_UDP) ||
+        (proto == PICO_PROTO_UDP) ||
 #endif
-  0) {
-    do {
-      uint32_t rand = pico_rand();
-      port = (uint16_t) (rand & 0xFFFFU);
-      port = (uint16_t)((port % (65535 - 1024)) + 1024U);
-      if (pico_is_port_free(proto, port, NULL, NULL)) {
-        return short_be(port);
-      }
-    } while(1);
-  }
-  else return 0U;
+        0) {
+        do {
+            uint32_t rand = pico_rand();
+            port = (uint16_t) (rand & 0xFFFFU);
+            port = (uint16_t)((port % (65535 - 1024)) + 1024U);
+            if (pico_is_port_free(proto, port, NULL, NULL)) {
+                return short_be(port);
+            }
+        } while(1);
+    }
+    else return 0U;
 }
 
 
 int pico_socket_sendto(struct pico_socket *s, const void *buf, const int len, void *dst, uint16_t remote_port)
 {
-  struct pico_frame *f;
-  struct pico_remote_duple *remote_duple = NULL;
-  int socket_mtu = PICO_SOCKET4_MTU;
-  int header_offset = 0;
-  int total_payload_written = 0;
+    struct pico_frame *f;
+    struct pico_remote_duple *remote_duple = NULL;
+    int socket_mtu = PICO_SOCKET4_MTU;
+    int header_offset = 0;
+    int total_payload_written = 0;
 #ifdef PICO_SUPPORT_IPV4
-  struct pico_ip4 *src4;
+    struct pico_ip4 *src4;
 #endif
 
 #ifdef PICO_SUPPORT_IPV6
-  struct pico_ip6 *src6;
+    struct pico_ip6 *src6;
 #endif
-  if (len == 0) {
-    return 0;
-  } else if (len < 0) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (len == 0) {
+        return 0;
+    } else if (len < 0) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  if (buf == NULL || s == NULL) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (buf == NULL || s == NULL) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  if (!dst || !remote_port) {
-    pico_err = PICO_ERR_EADDRNOTAVAIL;
-    return -1;
-  }
+    if (!dst || !remote_port) {
+        pico_err = PICO_ERR_EADDRNOTAVAIL;
+        return -1;
+    }
 
-  if ((s->state & PICO_SOCKET_STATE_CONNECTED) != 0) {
-    if (remote_port != s->remote_port) {
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
+    if ((s->state & PICO_SOCKET_STATE_CONNECTED) != 0) {
+        if (remote_port != s->remote_port) {
+            pico_err = PICO_ERR_EINVAL;
+            return -1;
+        }
     }
-  }
 
 #ifdef PICO_SUPPORT_IPV4
-  if (IS_SOCK_IPV4(s)) {
-    socket_mtu = PICO_SOCKET4_MTU;
-    if ((s->state & PICO_SOCKET_STATE_CONNECTED)) {
-      if  (s->remote_addr.ip4.addr != ((struct pico_ip4 *)dst)->addr ) {
-        pico_err = PICO_ERR_EADDRNOTAVAIL;
-        return -1;
-      }
-    } else {
-      src4 = pico_ipv4_source_find(dst);
-      if (!src4) {
-        pico_err = PICO_ERR_EHOSTUNREACH;
-        return -1;
-      }
-      if (src4->addr != PICO_IPV4_INADDR_ANY)
-        s->local_addr.ip4.addr = src4->addr;
+    if (IS_SOCK_IPV4(s)) {
+        socket_mtu = PICO_SOCKET4_MTU;
+        if ((s->state & PICO_SOCKET_STATE_CONNECTED)) {
+            if  (s->remote_addr.ip4.addr != ((struct pico_ip4 *)dst)->addr ) {
+                pico_err = PICO_ERR_EADDRNOTAVAIL;
+                return -1;
+            }
+        } else {
+            src4 = pico_ipv4_source_find(dst);
+            if (!src4) {
+                pico_err = PICO_ERR_EHOSTUNREACH;
+                return -1;
+            }
+
+            if (src4->addr != PICO_IPV4_INADDR_ANY)
+                s->local_addr.ip4.addr = src4->addr;
+
 #     ifdef PICO_SUPPORT_UDP
-      /* socket remote info could change in a consecutive call, make persistent */
-      if (PROTO(s) == PICO_PROTO_UDP) {
-        remote_duple = pico_zalloc(sizeof(struct pico_remote_duple));
-        remote_duple->remote_addr.ip4.addr = ((struct pico_ip4 *)dst)->addr;
-        remote_duple->remote_port = remote_port;
-      }
+            /* socket remote info could change in a consecutive call, make persistent */
+            if (PROTO(s) == PICO_PROTO_UDP) {
+                remote_duple = pico_zalloc(sizeof(struct pico_remote_duple));
+                remote_duple->remote_addr.ip4.addr = ((struct pico_ip4 *)dst)->addr;
+                remote_duple->remote_port = remote_port;
+            }
+
 #     endif
-    }
+        }
+
     #endif
-  } else if (IS_SOCK_IPV6(s)) {
+}
+else if (IS_SOCK_IPV6(s)) {
     socket_mtu = PICO_SOCKET6_MTU;
     #ifdef PICO_SUPPORT_IPV6
     if (s->state & PICO_SOCKET_STATE_CONNECTED) {
-      if (memcmp(&s->remote_addr, dst, PICO_SIZE_IP6))
-        return -1;
+        if (memcmp(&s->remote_addr, dst, PICO_SIZE_IP6))
+            return -1;
     } else {
-      src6 = pico_ipv6_source_find(dst);
-      if (!src6) {
-        pico_err = PICO_ERR_EHOSTUNREACH;
-        return -1;
-      }
-      memcpy(&s->local_addr, src6, PICO_SIZE_IP6);
-      memcpy(&s->remote_addr, dst, PICO_SIZE_IP6);
+        src6 = pico_ipv6_source_find(dst);
+        if (!src6) {
+            pico_err = PICO_ERR_EHOSTUNREACH;
+            return -1;
+        }
+
+        memcpy(&s->local_addr, src6, PICO_SIZE_IP6);
+        memcpy(&s->remote_addr, dst, PICO_SIZE_IP6);
 #     ifdef PICO_SUPPORT_UDP
-      if (PROTO(s) == PICO_PROTO_UDP) {
-        remote_duple = pico_zalloc(sizeof(struct pico_remote_duple));
-        remote_duple->remote_addr.ip6.addr = ((struct pico_ip6 *)dst)->addr;
-        remote_duple->remote_port = remote_port;
-      }
+        if (PROTO(s) == PICO_PROTO_UDP) {
+            remote_duple = pico_zalloc(sizeof(struct pico_remote_duple));
+            remote_duple->remote_addr.ip6.addr = ((struct pico_ip6 *)dst)->addr;
+            remote_duple->remote_port = remote_port;
+        }
+
 #     endif
     }
+
 #endif
-  }
+}
 
-  if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
+if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
     s->local_port = pico_socket_high_port(s->proto->proto_number);
     if (s->local_port == 0) {
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
     }
-  }
-  if ((s->state & PICO_SOCKET_STATE_CONNECTED) == 0) {
+}
+
+if ((s->state & PICO_SOCKET_STATE_CONNECTED) == 0) {
     s->remote_port = remote_port;
-  }
+}
 
 #ifdef PICO_SUPPORT_TCP
-  if (PROTO(s) == PICO_PROTO_TCP)
+if (PROTO(s) == PICO_PROTO_TCP)
     header_offset = pico_tcp_overhead(s);
+
 #endif
 
 #ifdef PICO_SUPPORT_UDP
-  if (PROTO(s) == PICO_PROTO_UDP)
+if (PROTO(s) == PICO_PROTO_UDP)
     header_offset = sizeof(struct pico_udp_hdr);
+
 #endif
 
-  while (total_payload_written < len) {
-    int transport_len = (len - total_payload_written) + header_offset; 
+while (total_payload_written < len) {
+    int transport_len = (len - total_payload_written) + header_offset;
     if (transport_len > socket_mtu)
-      transport_len = socket_mtu;
+        transport_len = socket_mtu;
+
     #ifdef PICO_SUPPORT_IPFRAG
     else {
-      if (total_payload_written)
-        transport_len -= header_offset; /* last fragment, do not allocate memory for transport header */
+        if (total_payload_written)
+            transport_len -= header_offset; /* last fragment, do not allocate memory for transport header */
+
     }
 #endif /* PICO_SUPPORT_IPFRAG */
 
     f = pico_socket_frame_alloc(s, (uint16_t)transport_len);
     if (!f) {
-      pico_err = PICO_ERR_ENOMEM;
-      return -1;
+        pico_err = PICO_ERR_ENOMEM;
+        return -1;
     }
+
     f->payload += header_offset;
     f->payload_len = (uint16_t)(f->payload_len - header_offset);
     f->sock = s;
+    transport_flags_update(f, s);
     if (remote_duple) {
-      f->info = pico_zalloc(sizeof(struct pico_remote_duple));
-      memcpy(f->info, remote_duple, sizeof(struct pico_remote_duple));
+        f->info = pico_zalloc(sizeof(struct pico_remote_duple));
+        memcpy(f->info, remote_duple, sizeof(struct pico_remote_duple));
     }
 
     #ifdef PICO_SUPPORT_IPFRAG
     #ifdef PICO_SUPPORT_UDP
     if (PROTO(s) == PICO_PROTO_UDP && ((len + header_offset) > socket_mtu)) {
-      /* hacking way to identify fragmentation frames: payload != transport_hdr -> first frame */
-      if (!total_payload_written) {
-        frag_dbg("FRAG: first fragmented frame %p | len = %u offset = 0\n", f, f->payload_len);
-        /* transport header length field contains total length + header length */
-        f->transport_len = (uint16_t)(len + header_offset);
-        f->frag = short_be(PICO_IPV4_MOREFRAG); 
-      } else {
-        /* no transport header in fragmented IP */
-        f->payload = f->transport_hdr;
-        f->payload_len = (uint16_t)(f->payload_len + header_offset);
-        /* set offset in octets */
-        f->frag = short_be((uint16_t)((total_payload_written + header_offset) / 8));
-        if (total_payload_written + f->payload_len < len) {
-          frag_dbg("FRAG: intermediate fragmented frame %p | len = %u offset = %u\n", f, f->payload_len, short_be(f->frag));
-          f->frag |= short_be(PICO_IPV4_MOREFRAG);
+        /* hacking way to identify fragmentation frames: payload != transport_hdr -> first frame */
+        if (!total_payload_written) {
+            frag_dbg("FRAG: first fragmented frame %p | len = %u offset = 0\n", f, f->payload_len);
+            /* transport header length field contains total length + header length */
+            f->transport_len = (uint16_t)(len + header_offset);
+            f->frag = short_be(PICO_IPV4_MOREFRAG);
         } else {
-          frag_dbg("FRAG: last fragmented frame %p | len = %u offset = %u\n", f, f->payload_len, short_be(f->frag));
-          f->frag &= short_be(PICO_IPV4_FRAG_MASK);
+            /* no transport header in fragmented IP */
+            f->payload = f->transport_hdr;
+            f->payload_len = (uint16_t)(f->payload_len + header_offset);
+            /* set offset in octets */
+            f->frag = short_be((uint16_t)((total_payload_written + header_offset) / 8));
+            if (total_payload_written + f->payload_len < len) {
+                frag_dbg("FRAG: intermediate fragmented frame %p | len = %u offset = %u\n", f, f->payload_len, short_be(f->frag));
+                f->frag |= short_be(PICO_IPV4_MOREFRAG);
+            } else {
+                frag_dbg("FRAG: last fragmented frame %p | len = %u offset = %u\n", f, f->payload_len, short_be(f->frag));
+                f->frag &= short_be(PICO_IPV4_FRAG_MASK);
+            }
         }
-      }
     } else {
-      f->frag = short_be(PICO_IPV4_DONTFRAG);
+        f->frag = short_be(PICO_IPV4_DONTFRAG);
     }
+
 #  endif /* PICO_SUPPORT_UDP */
 #endif /* PICO_SUPPORT_IPFRAG */
 
     if (f->payload_len <= 0) {
-      pico_frame_discard(f);
-      if (remote_duple)
-        pico_free(remote_duple);
-      return total_payload_written;
+        pico_frame_discard(f);
+        if (remote_duple)
+            pico_free(remote_duple);
+
+        return total_payload_written;
     }
 
     memcpy(f->payload, buf + total_payload_written, f->payload_len);
-    //dbg("Pushing segment, hdr len: %d, payload_len: %d\n", header_offset, f->payload_len);
+    /* dbg("Pushing segment, hdr len: %d, payload_len: %d\n", header_offset, f->payload_len); */
 
     if (s->proto->push(s->proto, f) > 0) {
-      total_payload_written += (int)f->payload_len;
+        total_payload_written += (int)f->payload_len;
     } else {
-      pico_frame_discard(f);
-      pico_err = PICO_ERR_EAGAIN;
-      break;
+        pico_frame_discard(f);
+        pico_err = PICO_ERR_EAGAIN;
+        break;
     }
-  }
-  if (remote_duple)
+}
+if (remote_duple)
     pico_free(remote_duple);
-  return total_payload_written;
+
+return total_payload_written;
 }
 
 int pico_socket_send(struct pico_socket *s, const void *buf, int len)
 {
-  if (!s || buf == NULL) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  } else {
-    /* check if exists in tree */
-    /* See task #178 */
-    if (pico_check_socket(s) != 0) {
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
+    if (!s || buf == NULL) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    } else {
+        /* check if exists in tree */
+        /* See task #178 */
+        if (pico_check_socket(s) != 0) {
+            pico_err = PICO_ERR_EINVAL;
+            return -1;
+        }
     }
-  }
 
-  if ((s->state & PICO_SOCKET_STATE_CONNECTED) == 0) {
-    pico_err = PICO_ERR_ENOTCONN;
-    return -1;
-  }
-  return pico_socket_sendto(s, buf, len, &s->remote_addr, s->remote_port);
+    if ((s->state & PICO_SOCKET_STATE_CONNECTED) == 0) {
+        pico_err = PICO_ERR_ENOTCONN;
+        return -1;
+    }
+
+    return pico_socket_sendto(s, buf, len, &s->remote_addr, s->remote_port);
 }
 
 int pico_socket_recvfrom(struct pico_socket *s, void *buf, int len, void *orig, uint16_t *remote_port)
 {
-  if (!s || buf == NULL) { /// || orig == NULL || remote_port == NULL) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  } else {
-    /* check if exists in tree */
-    if (pico_check_socket(s) != 0) {
-      pico_err = PICO_ERR_EINVAL;
-    /* See task #178 */
-      return -1;
+    if (!s || buf == NULL) { /* / || orig == NULL || remote_port == NULL) { */
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    } else {
+        /* check if exists in tree */
+        if (pico_check_socket(s) != 0) {
+            pico_err = PICO_ERR_EINVAL;
+            /* See task #178 */
+            return -1;
+        }
     }
-  }
+
+    if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
+        pico_err = PICO_ERR_EADDRNOTAVAIL;
+        return -1;
+    }
 
-  if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
-    pico_err = PICO_ERR_EADDRNOTAVAIL;
-    return -1;
-  }
-#ifdef PICO_SUPPORT_UDP 
-  if (PROTO(s) == PICO_PROTO_UDP) {
-    return pico_udp_recv(s, buf, (uint16_t)len, orig, remote_port);
-  }
+#ifdef PICO_SUPPORT_UDP
+    if (PROTO(s) == PICO_PROTO_UDP) {
+        return pico_udp_recv(s, buf, (uint16_t)len, orig, remote_port);
+    }
+
 #endif
 #ifdef PICO_SUPPORT_TCP
-  if (PROTO(s) == PICO_PROTO_TCP) {
-    /* check if in shutdown state and if tcpq_in empty */
-    if ((s->state & PICO_SOCKET_STATE_SHUT_REMOTE) && pico_tcp_queue_in_is_empty(s)) {
-      pico_err = PICO_ERR_ESHUTDOWN;
-      return -1;
-    } else {
-      //dbg("socket tcp recv\n");
-      return (int)pico_tcp_read(s, buf, (uint32_t)len);
+    if (PROTO(s) == PICO_PROTO_TCP) {
+        /* check if in shutdown state and if tcpq_in empty */
+        if ((s->state & PICO_SOCKET_STATE_SHUT_REMOTE) && pico_tcp_queue_in_is_empty(s)) {
+            pico_err = PICO_ERR_ESHUTDOWN;
+            return -1;
+        } else {
+            /* dbg("socket tcp recv\n"); */
+            return (int)pico_tcp_read(s, buf, (uint32_t)len);
+        }
     }
-  }
+
 #endif
-  //dbg("socket return 0\n");
-  return 0;
+    /* dbg("socket return 0\n"); */
+    return 0;
 }
 
 int pico_socket_recv(struct pico_socket *s, void *buf, int len)
 {
-  return pico_socket_recvfrom(s, buf, len, NULL, NULL);
+    return pico_socket_recvfrom(s, buf, len, NULL, NULL);
 }
 
 
 int pico_socket_bind(struct pico_socket *s, void *local_addr, uint16_t *port)
 {
-  if (!s || !local_addr || !port) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-
-  if (!is_sock_ipv6(s)) {
-    struct pico_ip4 *ip = (struct pico_ip4 *)local_addr;
-    if (ip->addr != PICO_IPV4_INADDR_ANY) {
-      if (!pico_ipv4_link_find(local_addr)) {
+    if (!s || !local_addr || !port) {
         pico_err = PICO_ERR_EINVAL;
         return -1;
-      }
     }
-  } else {
-    /*... IPv6 */
-  }
+
+    if (!is_sock_ipv6(s)) {
+        struct pico_ip4 *ip = (struct pico_ip4 *)local_addr;
+        if (ip->addr != PICO_IPV4_INADDR_ANY) {
+            if (!pico_ipv4_link_find(local_addr)) {
+                pico_err = PICO_ERR_EINVAL;
+                return -1;
+            }
+        }
+    } else {
+        /*... IPv6 */
+    }
 
 
-  /* When given port = 0, get a random high port to bind to. */
-  if (*port == 0) {
-    *port = pico_socket_high_port(PROTO(s));
+    /* When given port = 0, get a random high port to bind to. */
     if (*port == 0) {
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
+        *port = pico_socket_high_port(PROTO(s));
+        if (*port == 0) {
+            pico_err = PICO_ERR_EINVAL;
+            return -1;
+        }
     }
-  }
 
-  if (pico_is_port_free(PROTO(s), *port, local_addr, s->net) == 0) {
-    pico_err = PICO_ERR_EADDRINUSE;
-    return -1;
-  }
-  s->local_port = *port;
+    if (pico_is_port_free(PROTO(s), *port, local_addr, s->net) == 0) {
+        pico_err = PICO_ERR_EADDRINUSE;
+        return -1;
+    }
+
+    s->local_port = *port;
 
-  if (is_sock_ipv6(s)) {
-    struct pico_ip6 *ip = (struct pico_ip6 *) local_addr;
-    memcpy(s->local_addr.ip6.addr, ip, PICO_SIZE_IP6);
-    /* XXX: port ipv4 functionality to ipv6 */
-    /* Check for port already in use */
-    if (pico_is_port_free(PROTO(s), *port, &local_addr, s->net)) {
-      pico_err = PICO_ERR_EADDRINUSE;
-      return -1;
+    if (is_sock_ipv6(s)) {
+        struct pico_ip6 *ip = (struct pico_ip6 *) local_addr;
+        memcpy(s->local_addr.ip6.addr, ip, PICO_SIZE_IP6);
+        /* XXX: port ipv4 functionality to ipv6 */
+        /* Check for port already in use */
+        if (pico_is_port_free(PROTO(s), *port, &local_addr, s->net)) {
+            pico_err = PICO_ERR_EADDRINUSE;
+            return -1;
+        }
+    } else if (is_sock_ipv4(s)) {
+        struct pico_ip4 *ip = (struct pico_ip4 *) local_addr;
+        s->local_addr.ip4.addr = ip->addr;
     }
-  } else if (is_sock_ipv4(s)) {
-    struct pico_ip4 *ip = (struct pico_ip4 *) local_addr;
-    s->local_addr.ip4.addr = ip->addr;
-  }
-  return pico_socket_alter_state(s, PICO_SOCKET_STATE_BOUND, 0, 0);
+
+    return pico_socket_alter_state(s, PICO_SOCKET_STATE_BOUND, 0, 0);
 }
 
 int pico_socket_connect(struct pico_socket *s, const void *remote_addr, uint16_t remote_port)
 {
-  int ret = -1;
-  pico_err = PICO_ERR_EPROTONOSUPPORT;
-  if (!s || remote_addr == NULL || remote_port == 0) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    int ret = -1;
+    pico_err = PICO_ERR_EPROTONOSUPPORT;
+    if (!s || remote_addr == NULL || remote_port == 0) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  s->remote_port = remote_port;
+    s->remote_port = remote_port;
 
-  if (s->local_port == 0) {
-    s->local_port = pico_socket_high_port(PROTO(s));
-    if (!s->local_port) {
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
+    if (s->local_port == 0) {
+        s->local_port = pico_socket_high_port(PROTO(s));
+        if (!s->local_port) {
+            pico_err = PICO_ERR_EINVAL;
+            return -1;
+        }
     }
-  }
 
-  if (is_sock_ipv6(s)) {
-    const struct pico_ip6 *ip = (const struct pico_ip6 *) remote_addr;
-    memcpy(s->remote_addr.ip6.addr, ip, PICO_SIZE_IP6);
-  } else if (is_sock_ipv4(s)) {
-    const struct pico_ip4 *local, *ip = (const struct pico_ip4 *) remote_addr;
-    s->remote_addr.ip4.addr = ip->addr;
-    local = pico_ipv4_source_find(ip);
-    if (local) {
-      s->local_addr.ip4.addr = local->addr;
-    } else {
-      pico_err = PICO_ERR_EHOSTUNREACH;
-      return -1;
+    if (is_sock_ipv6(s)) {
+        const struct pico_ip6 *ip = (const struct pico_ip6 *) remote_addr;
+        memcpy(s->remote_addr.ip6.addr, ip, PICO_SIZE_IP6);
+    } else if (is_sock_ipv4(s)) {
+        const struct pico_ip4 *local, *ip = (const struct pico_ip4 *) remote_addr;
+        s->remote_addr.ip4.addr = ip->addr;
+        local = pico_ipv4_source_find(ip);
+        if (local) {
+            s->local_addr.ip4.addr = local->addr;
+        } else {
+            pico_err = PICO_ERR_EHOSTUNREACH;
+            return -1;
+        }
     }
-  }
 
-  pico_socket_alter_state(s, PICO_SOCKET_STATE_BOUND, 0, 0);
+    pico_socket_alter_state(s, PICO_SOCKET_STATE_BOUND, 0, 0);
 
 #ifdef PICO_SUPPORT_UDP
-  if (PROTO(s) == PICO_PROTO_UDP) {
-    pico_socket_alter_state(s, PICO_SOCKET_STATE_CONNECTED, 0, 0);
-    pico_err = PICO_ERR_NOERR;
-    ret = 0;
-  }
+    if (PROTO(s) == PICO_PROTO_UDP) {
+        pico_socket_alter_state(s, PICO_SOCKET_STATE_CONNECTED, 0, 0);
+        pico_err = PICO_ERR_NOERR;
+        ret = 0;
+    }
+
 #endif
 
 #ifdef PICO_SUPPORT_TCP
-  if (PROTO(s) == PICO_PROTO_TCP) {
-    if (pico_tcp_initconn(s) == 0) {
-      pico_socket_alter_state(s, PICO_SOCKET_STATE_CONNECTED | PICO_SOCKET_STATE_TCP_SYN_SENT, 0, 0);
-      pico_err = PICO_ERR_NOERR;
-      ret = 0;
-    } else {
-      pico_err = PICO_ERR_EHOSTUNREACH;
+    if (PROTO(s) == PICO_PROTO_TCP) {
+        if (pico_tcp_initconn(s) == 0) {
+            pico_socket_alter_state(s, PICO_SOCKET_STATE_CONNECTED | PICO_SOCKET_STATE_TCP_SYN_SENT, 0, 0);
+            pico_err = PICO_ERR_NOERR;
+            ret = 0;
+        } else {
+            pico_err = PICO_ERR_EHOSTUNREACH;
+        }
     }
-  }
+
 #endif
 
-  return ret;
+    return ret;
 }
 
 #ifdef PICO_SUPPORT_TCP
 
 int pico_socket_listen(struct pico_socket *s, int backlog)
 {
-  if (!s || backlog < 1) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  } else {
-    /* check if exists in tree */
-    /* See task #178 */
-    if (pico_check_socket(s) != 0) {
-      pico_err = PICO_ERR_EINVAL;
-      return -1;
+    if (!s || backlog < 1) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    } else {
+        /* check if exists in tree */
+        /* See task #178 */
+        if (pico_check_socket(s) != 0) {
+            pico_err = PICO_ERR_EINVAL;
+            return -1;
+        }
     }
-  }
+
+    if (PROTO(s) == PICO_PROTO_UDP) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  if (PROTO(s) == PICO_PROTO_UDP) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
-
-  if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
-    pico_err = PICO_ERR_EISCONN;
-    return -1;
-  }
+    if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
+        pico_err = PICO_ERR_EISCONN;
+        return -1;
+    }
 
-  if (backlog < 1) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (backlog < 1) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  if (PROTO(s) == PICO_PROTO_TCP)
-    pico_socket_alter_state(s, PICO_SOCKET_STATE_TCP_SYN_SENT, 0, PICO_SOCKET_STATE_TCP_LISTEN);
-  s->max_backlog = backlog;
+    if (PROTO(s) == PICO_PROTO_TCP)
+        pico_socket_alter_state(s, PICO_SOCKET_STATE_TCP_SYN_SENT, 0, PICO_SOCKET_STATE_TCP_LISTEN);
 
-  return 0;
+    s->max_backlog = backlog;
+
+    return 0;
 }
 
 struct pico_socket *pico_socket_accept(struct pico_socket *s, void *orig, uint16_t *port)
 {
-  if (!s || !orig || !port) {
+    if (!s || !orig || !port) {
+        pico_err = PICO_ERR_EINVAL;
+        return NULL;
+    }
+
     pico_err = PICO_ERR_EINVAL;
-    return NULL;
-  }
-
-  pico_err = PICO_ERR_EINVAL;
 
-  if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
-    return NULL;
-  }
+    if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
+        return NULL;
+    }
 
-  if (PROTO(s) == PICO_PROTO_UDP) {
-    return NULL;
-  }
+    if (PROTO(s) == PICO_PROTO_UDP) {
+        return NULL;
+    }
 
-  if (TCPSTATE(s) == PICO_SOCKET_STATE_TCP_LISTEN) {
-    struct pico_sockport *sp = pico_get_sockport(PICO_PROTO_TCP, s->local_port);
-    struct pico_socket *found;
-    /* If at this point no incoming connection socket is found,
-     * the accept call is valid, but no connection is established yet.
-     */
-    pico_err = PICO_ERR_EAGAIN; 
-    if (sp) {
-      struct pico_tree_node * index;
-      //RB_FOREACH(found, socket_tree, &sp->socks) {
-      pico_tree_foreach(index,&sp->socks){
-        found = index->keyValue;
-        if ((s == found->parent) && ((found->state & PICO_SOCKET_STATE_TCP) == PICO_SOCKET_STATE_TCP_ESTABLISHED)) {
-          found->parent = NULL;
-          pico_err = PICO_ERR_NOERR;
-          memcpy(orig, &found->remote_addr, sizeof(struct pico_ip4));
-          *port = found->remote_port;
-          s->number_of_pending_conn--;
-          return found;
+    if (TCPSTATE(s) == PICO_SOCKET_STATE_TCP_LISTEN) {
+        struct pico_sockport *sp = pico_get_sockport(PICO_PROTO_TCP, s->local_port);
+        struct pico_socket *found;
+        /* If at this point no incoming connection socket is found,
+         * the accept call is valid, but no connection is established yet.
+         */
+        pico_err = PICO_ERR_EAGAIN;
+        if (sp) {
+            struct pico_tree_node *index;
+            /* RB_FOREACH(found, socket_tree, &sp->socks) { */
+            pico_tree_foreach(index, &sp->socks){
+                found = index->keyValue;
+                if ((s == found->parent) && ((found->state & PICO_SOCKET_STATE_TCP) == PICO_SOCKET_STATE_TCP_ESTABLISHED)) {
+                    found->parent = NULL;
+                    pico_err = PICO_ERR_NOERR;
+                    memcpy(orig, &found->remote_addr, sizeof(struct pico_ip4));
+                    *port = found->remote_port;
+                    s->number_of_pending_conn--;
+                    return found;
+                }
+            }
         }
-      }
     }
-  }
-  return NULL;
+
+    return NULL;
 }
 
 #else
 
 int pico_socket_listen(struct pico_socket *s, int backlog)
 {
-  IGNORE_PARAMETER(s);
-  IGNORE_PARAMETER(backlog);
-  pico_err = PICO_ERR_EINVAL;
-  return -1;
+    IGNORE_PARAMETER(s);
+    IGNORE_PARAMETER(backlog);
+    pico_err = PICO_ERR_EINVAL;
+    return -1;
 }
 
 struct pico_socket *pico_socket_accept(struct pico_socket *s, void *orig, uint16_t *local_port)
 {
-  IGNORE_PARAMETER(s);
-  IGNORE_PARAMETER(orig);
-  IGNORE_PARAMETER(local_port);
-  pico_err = PICO_ERR_EINVAL;
-  return NULL;
+    IGNORE_PARAMETER(s);
+    IGNORE_PARAMETER(orig);
+    IGNORE_PARAMETER(local_port);
+    pico_err = PICO_ERR_EINVAL;
+    return NULL;
 }
 
 #endif
 
-#define PICO_SOCKET_SETOPT_EN(socket,index)  (socket->opt_flags |=  (1 << index))
-#define PICO_SOCKET_SETOPT_DIS(socket,index) (socket->opt_flags &= (uint16_t)~(1 << index))
+#define PICO_SOCKET_SETOPT_EN(socket, index)  (socket->opt_flags |=  (1 << index))
+#define PICO_SOCKET_SETOPT_DIS(socket, index) (socket->opt_flags &= (uint16_t) ~(1 << index))
 
 #ifdef PICO_SUPPORT_MCAST
 static struct pico_ipv4_link *setopt_multicast_check(struct pico_socket *s, void *value, int alloc, int bysource)
@@ -1517,583 +1597,627 @@
     struct pico_ip_mreq_source *mreq_src = NULL;
 
     if (!value) {
-      pico_err = PICO_ERR_EINVAL;
-      return NULL;
+        pico_err = PICO_ERR_EINVAL;
+        return NULL;
     }
+
     if (!bysource) {
-      mreq = (struct pico_ip_mreq *) value;
-      mcast_link = pico_socket_setoption_mcastargs_validation(mreq, NULL);
-      if (!mreq->mcast_link_addr.addr)
-        mreq->mcast_link_addr.addr = mcast_link->address.addr;
+        mreq = (struct pico_ip_mreq *) value;
+        mcast_link = pico_socket_setoption_mcastargs_validation(mreq, NULL);
+        if (!mreq->mcast_link_addr.addr)
+            mreq->mcast_link_addr.addr = mcast_link->address.addr;
     } else {
-      mreq_src = (struct pico_ip_mreq_source *) value;
-      mcast_link = pico_socket_setoption_mcastargs_validation(NULL, mreq_src);
-      if (!mreq_src->mcast_link_addr.addr)
-        mreq_src->mcast_link_addr.addr = mcast_link->address.addr;
+        mreq_src = (struct pico_ip_mreq_source *) value;
+        mcast_link = pico_socket_setoption_mcastargs_validation(NULL, mreq_src);
+        if (!mreq_src->mcast_link_addr.addr)
+            mreq_src->mcast_link_addr.addr = mcast_link->address.addr;
     }
+
     if (!mcast_link) {
-      pico_err = PICO_ERR_EINVAL;
-      return NULL;
+        pico_err = PICO_ERR_EINVAL;
+        return NULL;
     }
 
     if (!s->MCASTListen) { /* No RBTree allocated yet */
-      if (alloc) {
-        s->MCASTListen = pico_zalloc(sizeof(struct pico_tree));
-        if (!s->MCASTListen) {
-          pico_err = PICO_ERR_ENOMEM;
-          return NULL;
-        }
-        s->MCASTListen->root = &LEAF;
-        s->MCASTListen->compare = mcast_listen_cmp;
-      } else return NULL;
+        if (alloc) {
+            s->MCASTListen = pico_zalloc(sizeof(struct pico_tree));
+            if (!s->MCASTListen) {
+                pico_err = PICO_ERR_ENOMEM;
+                return NULL;
+            }
+
+            s->MCASTListen->root = &LEAF;
+            s->MCASTListen->compare = mcast_listen_cmp;
+        } else return NULL;
     }
+
     return mcast_link;
 }
 #endif
 
-int pico_socket_setoption(struct pico_socket *s, int option, void *value) // XXX no check against proto (vs setsockopt) or implicit by socket?
+int pico_socket_setoption(struct pico_socket *s, int option, void *value) /* XXX no check against proto (vs setsockopt) or implicit by socket? */
 {
-  if (s == NULL) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+    if (s == NULL) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  pico_err = PICO_ERR_NOERR;
+    pico_err = PICO_ERR_NOERR;
 
-  switch (option)
-  {
+    switch (option)
+    {
 #ifdef PICO_SUPPORT_TCP
     case PICO_TCP_NODELAY:
-          if (!value) {
+        if (!value) {
             pico_err = PICO_ERR_EINVAL;
             return -1;
-          }
-          if (s->proto->proto_number == PICO_PROTO_TCP) {
+        }
+
+        if (s->proto->proto_number == PICO_PROTO_TCP) {
             int *val = (int*)value;
             if (*val > 0) {
-              dbg("setsockopt: Nagle algorithm disabled.\n");
-              PICO_SOCKET_SETOPT_EN(s,PICO_SOCKET_OPT_TCPNODELAY);
+                dbg("setsockopt: Nagle algorithm disabled.\n");
+                PICO_SOCKET_SETOPT_EN(s, PICO_SOCKET_OPT_TCPNODELAY);
             } else {
-              dbg("setsockopt: Nagle algorithm enabled.\n");
-              PICO_SOCKET_SETOPT_DIS(s,PICO_SOCKET_OPT_TCPNODELAY);
+                dbg("setsockopt: Nagle algorithm enabled.\n");
+                PICO_SOCKET_SETOPT_DIS(s, PICO_SOCKET_OPT_TCPNODELAY);
             }
-          } else {
+        } else {
             pico_err = PICO_ERR_EINVAL;
-          }
-          break;
+        }
+
+        break;
 #endif
 
 
 #ifdef PICO_SUPPORT_MCAST
     case PICO_IP_MULTICAST_IF:
-          pico_err = PICO_ERR_EOPNOTSUPP;
-          return -1;
-          break;
+        pico_err = PICO_ERR_EOPNOTSUPP;
+        return -1;
+        break;
 
     case PICO_IP_MULTICAST_TTL:
-          if (s->proto->proto_number == PICO_PROTO_UDP) {
+        if (s->proto->proto_number == PICO_PROTO_UDP) {
             return pico_udp_set_mc_ttl(s, *((uint8_t *) value));
-          }
-          break;
+        }
+
+        break;
 
     case PICO_IP_MULTICAST_LOOP:
-          if (s->proto->proto_number == PICO_PROTO_UDP) {
+        if (s->proto->proto_number == PICO_PROTO_UDP) {
             switch (*(uint8_t *) value)
             {
-              case 0:
+            case 0:
                 /* do not loop back multicast datagram */
-                PICO_SOCKET_SETOPT_DIS(s,PICO_SOCKET_OPT_MULTICAST_LOOP);
+                PICO_SOCKET_SETOPT_DIS(s, PICO_SOCKET_OPT_MULTICAST_LOOP);
                 break;
 
-              case 1:
+            case 1:
                 /* do loop back multicast datagram */
-                PICO_SOCKET_SETOPT_EN(s,PICO_SOCKET_OPT_MULTICAST_LOOP);
-                break;  
+                PICO_SOCKET_SETOPT_EN(s, PICO_SOCKET_OPT_MULTICAST_LOOP);
+                break;
 
-              default:
+            default:
                 pico_err = PICO_ERR_EINVAL;
                 return -1;
-             }
-          }
-          break;
+            }
+        }
+
+        break;
 
     case PICO_IP_ADD_MEMBERSHIP:
-      /* EXCLUDE mode */
-      if (s->proto->proto_number == PICO_PROTO_UDP) {
-        int filter_mode;
-        struct pico_mcast_listen *listen;
-        struct pico_ip_mreq *mreq = (struct pico_ip_mreq *)value;
-        struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 1, 0);
-        if (!mcast_link)
-          return -1;
-        listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
-        if (listen) {
-          if (listen->filter_mode != PICO_IP_MULTICAST_EXCLUDE) {
-            so_mcast_dbg("pico_socket_setoption: ERROR any-source multicast (exclude) on source-specific multicast (include)\n");
-            pico_err = PICO_ERR_EINVAL;
-            return -1;
-          } else {
-            so_mcast_dbg("pico_socket_setoption: ERROR duplicate PICO_IP_ADD_MEMBERSHIP\n");
-            pico_err = PICO_ERR_EINVAL;
-            return -1;
-          }
-        } else {
-          listen = pico_zalloc(sizeof(struct pico_mcast_listen));
-          if (!listen) {
-            pico_err = PICO_ERR_ENOMEM;
-            return -1;
-          }
-          listen->filter_mode = PICO_IP_MULTICAST_EXCLUDE;
-          listen->mcast_link = mreq->mcast_link_addr;
-          listen->mcast_group = mreq->mcast_group_addr;
-          listen->MCASTSources.root = &LEAF;
-          listen->MCASTSources.compare = mcast_sources_cmp;
-          pico_tree_insert(s->MCASTListen, listen);
+        /* EXCLUDE mode */
+        if (s->proto->proto_number == PICO_PROTO_UDP) {
+            int filter_mode;
+            struct pico_mcast_listen *listen;
+            struct pico_ip_mreq *mreq = (struct pico_ip_mreq *)value;
+            struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 1, 0);
+            if (!mcast_link)
+                return -1;
+
+            listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
+            if (listen) {
+                if (listen->filter_mode != PICO_IP_MULTICAST_EXCLUDE) {
+                    so_mcast_dbg("pico_socket_setoption: ERROR any-source multicast (exclude) on source-specific multicast (include)\n");
+                    pico_err = PICO_ERR_EINVAL;
+                    return -1;
+                } else {
+                    so_mcast_dbg("pico_socket_setoption: ERROR duplicate PICO_IP_ADD_MEMBERSHIP\n");
+                    pico_err = PICO_ERR_EINVAL;
+                    return -1;
+                }
+            } else {
+                listen = pico_zalloc(sizeof(struct pico_mcast_listen));
+                if (!listen) {
+                    pico_err = PICO_ERR_ENOMEM;
+                    return -1;
+                }
+
+                listen->filter_mode = PICO_IP_MULTICAST_EXCLUDE;
+                listen->mcast_link = mreq->mcast_link_addr;
+                listen->mcast_group = mreq->mcast_group_addr;
+                listen->MCASTSources.root = &LEAF;
+                listen->MCASTSources.compare = mcast_sources_cmp;
+                pico_tree_insert(s->MCASTListen, listen);
+            }
+
+            pico_tree_insert(&MCASTSockets, s);
+            filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
+            if (filter_mode < 0)
+                return -1;
+
+            return pico_ipv4_mcast_join(&mreq->mcast_link_addr, &mreq->mcast_group_addr, 1, (uint8_t)filter_mode, &MCASTFilter);
         }
 
-        pico_tree_insert(&MCASTSockets, s);
-        filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
-        if (filter_mode < 0) 
-          return -1;
-
-        return pico_ipv4_mcast_join(&mreq->mcast_link_addr, &mreq->mcast_group_addr, 1, (uint8_t)filter_mode, &MCASTFilter);
-      }          
-      break;
+        break;
 
     case PICO_IP_DROP_MEMBERSHIP:
-      /* EXCLUDE mode */
-      if (s->proto->proto_number == PICO_PROTO_UDP) {
-        int filter_mode = 0;
-        struct pico_mcast_listen *listen;
-        struct pico_ip_mreq *mreq = (struct pico_ip_mreq *)value;
-        struct pico_ip4 *source = NULL;
-        struct pico_tree_node *index, *_tmp;
-        struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 0, 0);
-        if (!mcast_link)
-          return -1;
+        /* EXCLUDE mode */
+        if (s->proto->proto_number == PICO_PROTO_UDP) {
+            int filter_mode = 0;
+            struct pico_mcast_listen *listen;
+            struct pico_ip_mreq *mreq = (struct pico_ip_mreq *)value;
+            struct pico_ip4 *source = NULL;
+            struct pico_tree_node *index, *_tmp;
+            struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 0, 0);
+            if (!mcast_link)
+                return -1;
 
-        listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
-        if (!listen) {
-          so_mcast_dbg("pico_socket_setoption: ERROR PICO_IP_DROP_MEMBERSHIP before PICO_IP_ADD_MEMBERSHIP/SOURCE_MEMBERSHIP\n");
-          pico_err = PICO_ERR_EADDRNOTAVAIL;
-          return -1;
-        } else {
-          pico_tree_foreach_safe(index, &listen->MCASTSources, _tmp)
-          {
-            source = index->keyValue;
-            pico_tree_delete(&listen->MCASTSources, source);
-            pico_free(source);
-          }
-          pico_tree_delete(s->MCASTListen, listen);
-          pico_free(listen);
-          if (pico_tree_empty(s->MCASTListen)) {
-            pico_free(s->MCASTListen);
-            s->MCASTListen = NULL;
-            pico_tree_delete(&MCASTSockets, s);
-          }
+            listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
+            if (!listen) {
+                so_mcast_dbg("pico_socket_setoption: ERROR PICO_IP_DROP_MEMBERSHIP before PICO_IP_ADD_MEMBERSHIP/SOURCE_MEMBERSHIP\n");
+                pico_err = PICO_ERR_EADDRNOTAVAIL;
+                return -1;
+            } else {
+                pico_tree_foreach_safe(index, &listen->MCASTSources, _tmp)
+                {
+                    source = index->keyValue;
+                    pico_tree_delete(&listen->MCASTSources, source);
+                    pico_free(source);
+                }
+                pico_tree_delete(s->MCASTListen, listen);
+                pico_free(listen);
+                if (pico_tree_empty(s->MCASTListen)) {
+                    pico_free(s->MCASTListen);
+                    s->MCASTListen = NULL;
+                    pico_tree_delete(&MCASTSockets, s);
+                }
+            }
+
+            filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
+            if (filter_mode < 0)
+                return -1;
+
+            return pico_ipv4_mcast_leave(&mreq->mcast_link_addr, &mreq->mcast_group_addr, 1, (uint8_t)filter_mode, &MCASTFilter);
         }
 
-        filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
-        if (filter_mode < 0) 
-          return -1;
-
-        return pico_ipv4_mcast_leave(&mreq->mcast_link_addr, &mreq->mcast_group_addr, 1, (uint8_t)filter_mode, &MCASTFilter);
-      }          
-      break;
+        break;
 
     case PICO_IP_UNBLOCK_SOURCE:
-      /* EXCLUDE mode */
-      if (s->proto->proto_number == PICO_PROTO_UDP) {
-        int filter_mode = 0;
-        struct pico_ip_mreq_source *mreq = (struct pico_ip_mreq_source *)value;
-        struct pico_mcast_listen *listen = NULL; 
-        struct pico_ip4 *source = NULL, stest = {0};
-        struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 0, 1);
-        if (!mcast_link)
-          return -1;
-        listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
-        if (!listen) {
-          so_mcast_dbg("pico_socket_setoption: ERROR PICO_IP_UNBLOCK_SOURCE before PICO_IP_ADD_MEMBERSHIP\n");
-          pico_err = PICO_ERR_EINVAL;
-          return -1;
-        } else {
-          if (listen->filter_mode != PICO_IP_MULTICAST_EXCLUDE) {
-            so_mcast_dbg("pico_socket_setoption: ERROR any-source multicast (exclude) on source-specific multicast (include)\n");
-            pico_err = PICO_ERR_EINVAL;
-            return -1;
-          }
-          stest.addr = mreq->mcast_source_addr.addr;
-          source = pico_tree_findKey(&listen->MCASTSources, &stest);
-          if (!source) {
-            so_mcast_dbg("pico_socket_setoption: ERROR address to unblock not in source list\n");
-            pico_err = PICO_ERR_EADDRNOTAVAIL;
-            return -1;
-          } else {
-            pico_tree_delete(&listen->MCASTSources, source);
-            pico_free(source);
-          }
+        /* EXCLUDE mode */
+        if (s->proto->proto_number == PICO_PROTO_UDP) {
+            int filter_mode = 0;
+            struct pico_ip_mreq_source *mreq = (struct pico_ip_mreq_source *)value;
+            struct pico_mcast_listen *listen = NULL;
+            struct pico_ip4 *source = NULL, stest = {
+                0
+            };
+            struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 0, 1);
+            if (!mcast_link)
+                return -1;
+
+            listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
+            if (!listen) {
+                so_mcast_dbg("pico_socket_setoption: ERROR PICO_IP_UNBLOCK_SOURCE before PICO_IP_ADD_MEMBERSHIP\n");
+                pico_err = PICO_ERR_EINVAL;
+                return -1;
+            } else {
+                if (listen->filter_mode != PICO_IP_MULTICAST_EXCLUDE) {
+                    so_mcast_dbg("pico_socket_setoption: ERROR any-source multicast (exclude) on source-specific multicast (include)\n");
+                    pico_err = PICO_ERR_EINVAL;
+                    return -1;
+                }
+
+                stest.addr = mreq->mcast_source_addr.addr;
+                source = pico_tree_findKey(&listen->MCASTSources, &stest);
+                if (!source) {
+                    so_mcast_dbg("pico_socket_setoption: ERROR address to unblock not in source list\n");
+                    pico_err = PICO_ERR_EADDRNOTAVAIL;
+                    return -1;
+                } else {
+                    pico_tree_delete(&listen->MCASTSources, source);
+                    pico_free(source);
+                }
+            }
+
+            filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
+            if (filter_mode < 0)
+                return -1;
+
+            return pico_ipv4_mcast_leave(&mreq->mcast_link_addr, &mreq->mcast_group_addr, 0, (uint8_t)filter_mode, &MCASTFilter);
         }
 
-        filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
-        if (filter_mode < 0) 
-          return -1;
-
-        return pico_ipv4_mcast_leave(&mreq->mcast_link_addr, &mreq->mcast_group_addr, 0, (uint8_t)filter_mode, &MCASTFilter);
-      }          
-      break;
+        break;
 
     case PICO_IP_BLOCK_SOURCE:
-      /* EXCLUDE mode */
-      if (s->proto->proto_number == PICO_PROTO_UDP) {
-        int filter_mode = 0;
-        struct pico_ip_mreq_source *mreq = (struct pico_ip_mreq_source *)value;
-        struct pico_mcast_listen *listen = NULL;
-        struct pico_ip4 *source = NULL, stest = {0};
-        struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 0, 1);
-        if (!mcast_link)
-          return -1;
-        listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
-        if (!listen) {
-          dbg("pico_socket_setoption: ERROR PICO_IP_BLOCK_SOURCE before PICO_IP_ADD_MEMBERSHIP\n");
-          pico_err = PICO_ERR_EINVAL;
-          return -1;
-        } else {
-          if (listen->filter_mode != PICO_IP_MULTICAST_EXCLUDE) {
-            so_mcast_dbg("pico_socket_setoption: ERROR any-source multicast (exclude) on source-specific multicast (include)\n");
-            pico_err = PICO_ERR_EINVAL;
-            return -1;
-          }
-          stest.addr = mreq->mcast_source_addr.addr;
-          source = pico_tree_findKey(&listen->MCASTSources, &stest);
-          if (source) {
-            so_mcast_dbg("pico_socket_setoption: ERROR address to block already in source list\n");
-            pico_err = PICO_ERR_EADDRNOTAVAIL;
-            return -1;
-          } else {
-            source = pico_zalloc(sizeof(struct pico_ip4));
-            if (!source) {
-              pico_err = PICO_ERR_ENOMEM;
-              return -1;
+        /* EXCLUDE mode */
+        if (s->proto->proto_number == PICO_PROTO_UDP) {
+            int filter_mode = 0;
+            struct pico_ip_mreq_source *mreq = (struct pico_ip_mreq_source *)value;
+            struct pico_mcast_listen *listen = NULL;
+            struct pico_ip4 *source = NULL, stest = {
+                0
+            };
+            struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 0, 1);
+            if (!mcast_link)
+                return -1;
+
+            listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
+            if (!listen) {
+                dbg("pico_socket_setoption: ERROR PICO_IP_BLOCK_SOURCE before PICO_IP_ADD_MEMBERSHIP\n");
+                pico_err = PICO_ERR_EINVAL;
+                return -1;
+            } else {
+                if (listen->filter_mode != PICO_IP_MULTICAST_EXCLUDE) {
+                    so_mcast_dbg("pico_socket_setoption: ERROR any-source multicast (exclude) on source-specific multicast (include)\n");
+                    pico_err = PICO_ERR_EINVAL;
+                    return -1;
+                }
+
+                stest.addr = mreq->mcast_source_addr.addr;
+                source = pico_tree_findKey(&listen->MCASTSources, &stest);
+                if (source) {
+                    so_mcast_dbg("pico_socket_setoption: ERROR address to block already in source list\n");
+                    pico_err = PICO_ERR_EADDRNOTAVAIL;
+                    return -1;
+                } else {
+                    source = pico_zalloc(sizeof(struct pico_ip4));
+                    if (!source) {
+                        pico_err = PICO_ERR_ENOMEM;
+                        return -1;
+                    }
+
+                    source->addr = mreq->mcast_source_addr.addr;
+                    pico_tree_insert(&listen->MCASTSources, source);
+                }
             }
-            source->addr = mreq->mcast_source_addr.addr;
-            pico_tree_insert(&listen->MCASTSources, source);
-          }
+
+            filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
+            if (filter_mode < 0)
+                return -1;
+
+            return pico_ipv4_mcast_join(&mreq->mcast_link_addr, &mreq->mcast_group_addr, 0, (uint8_t)filter_mode, &MCASTFilter);
         }
 
-        filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
-        if (filter_mode < 0) 
-          return -1;
-
-        return pico_ipv4_mcast_join(&mreq->mcast_link_addr, &mreq->mcast_group_addr, 0, (uint8_t)filter_mode, &MCASTFilter);
-      }          
-      break;
+        break;
 
     case PICO_IP_ADD_SOURCE_MEMBERSHIP:
-      /* INCLUDE mode */
-      if (s->proto->proto_number == PICO_PROTO_UDP) {
-        int filter_mode = 0, reference_count = 0;
-        struct pico_ip_mreq_source *mreq = (struct pico_ip_mreq_source *)value;
-        struct pico_mcast_listen *listen = NULL; 
-        struct pico_ip4 *source = NULL, stest = {0};
-        struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 1, 1);
-        if (!mcast_link)
-          return -1;
-        listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
-        if (listen) {
-          if (listen->filter_mode != PICO_IP_MULTICAST_INCLUDE) {
-            so_mcast_dbg("pico_socket_setoption: ERROR source-specific multicast (include) on any-source multicast (exclude)\n");
-            pico_err = PICO_ERR_EINVAL;
-            return -1;
-          }
-          stest.addr = mreq->mcast_source_addr.addr;
-          source = pico_tree_findKey(&listen->MCASTSources, &stest);
-          if (source) {
-            so_mcast_dbg("pico_socket_setoption: ERROR source address to allow already in source list\n");
-            pico_err = PICO_ERR_EADDRNOTAVAIL;
-            return -1;
-          } else {
-            source = pico_zalloc(sizeof(struct pico_ip4));
-            if (!source) {
-              pico_err = PICO_ERR_ENOMEM;
-              return -1;
+        /* INCLUDE mode */
+        if (s->proto->proto_number == PICO_PROTO_UDP) {
+            int filter_mode = 0, reference_count = 0;
+            struct pico_ip_mreq_source *mreq = (struct pico_ip_mreq_source *)value;
+            struct pico_mcast_listen *listen = NULL;
+            struct pico_ip4 *source = NULL, stest = {
+                0
+            };
+            struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 1, 1);
+            if (!mcast_link)
+                return -1;
+
+            listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
+            if (listen) {
+                if (listen->filter_mode != PICO_IP_MULTICAST_INCLUDE) {
+                    so_mcast_dbg("pico_socket_setoption: ERROR source-specific multicast (include) on any-source multicast (exclude)\n");
+                    pico_err = PICO_ERR_EINVAL;
+                    return -1;
+                }
+
+                stest.addr = mreq->mcast_source_addr.addr;
+                source = pico_tree_findKey(&listen->MCASTSources, &stest);
+                if (source) {
+                    so_mcast_dbg("pico_socket_setoption: ERROR source address to allow already in source list\n");
+                    pico_err = PICO_ERR_EADDRNOTAVAIL;
+                    return -1;
+                } else {
+                    source = pico_zalloc(sizeof(struct pico_ip4));
+                    if (!source) {
+                        pico_err = PICO_ERR_ENOMEM;
+                        return -1;
+                    }
+
+                    source->addr = mreq->mcast_source_addr.addr;
+                    pico_tree_insert(&listen->MCASTSources, source);
+                }
+            } else {
+                listen = pico_zalloc(sizeof(struct pico_mcast_listen));
+                if (!listen) {
+                    pico_err = PICO_ERR_ENOMEM;
+                    return -1;
+                }
+
+                listen->filter_mode = PICO_IP_MULTICAST_INCLUDE;
+                listen->mcast_link = mreq->mcast_link_addr;
+                listen->mcast_group = mreq->mcast_group_addr;
+                listen->MCASTSources.root = &LEAF;
+                listen->MCASTSources.compare = mcast_sources_cmp;
+                source = pico_zalloc(sizeof(struct pico_ip4));
+                if (!source) {
+                    pico_free(listen);
+                    pico_err = PICO_ERR_ENOMEM;
+                    return -1;
+                }
+
+                source->addr = mreq->mcast_source_addr.addr;
+                pico_tree_insert(&listen->MCASTSources, source);
+                pico_tree_insert(s->MCASTListen, listen);
+                reference_count = 1;
             }
-            source->addr = mreq->mcast_source_addr.addr;
-            pico_tree_insert(&listen->MCASTSources, source);
-          }
-        } else {
-          listen = pico_zalloc(sizeof(struct pico_mcast_listen));
-          if (!listen) {
-            pico_err = PICO_ERR_ENOMEM;
-            return -1;
-          }
-          listen->filter_mode = PICO_IP_MULTICAST_INCLUDE;
-          listen->mcast_link = mreq->mcast_link_addr;
-          listen->mcast_group = mreq->mcast_group_addr;
-          listen->MCASTSources.root = &LEAF;
-          listen->MCASTSources.compare = mcast_sources_cmp;
-          source = pico_zalloc(sizeof(struct pico_ip4));
-          if (!source) {
-            pico_free(listen);
-            pico_err = PICO_ERR_ENOMEM;
-            return -1;
-          }
-          source->addr = mreq->mcast_source_addr.addr;
-          pico_tree_insert(&listen->MCASTSources, source);
-          pico_tree_insert(s->MCASTListen, listen);
-          reference_count = 1;
+
+            pico_tree_insert(&MCASTSockets, s);
+            filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
+            if (filter_mode < 0)
+                return -1;
+
+            return pico_ipv4_mcast_join(&mreq->mcast_link_addr, &mreq->mcast_group_addr, (uint8_t)reference_count, (uint8_t)filter_mode, &MCASTFilter);
         }
 
-        pico_tree_insert(&MCASTSockets, s);
-        filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
-        if (filter_mode < 0) 
-          return -1;
-
-        return pico_ipv4_mcast_join(&mreq->mcast_link_addr, &mreq->mcast_group_addr, (uint8_t)reference_count, (uint8_t)filter_mode, &MCASTFilter);
-      }          
-      break;
+        break;
 
     case PICO_IP_DROP_SOURCE_MEMBERSHIP:
-      /* INCLUDE mode */
-      if (s->proto->proto_number == PICO_PROTO_UDP) {
-        int filter_mode = 0, reference_count = 0;
-        struct pico_ip_mreq_source *mreq = (struct pico_ip_mreq_source *)value;
-        struct pico_mcast_listen *listen = NULL;
-        struct pico_ip4 *source = NULL, stest = {0};
-        struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 0, 1);
-        if (!mcast_link)
-          return -1;
-        listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
-        if (!listen) {
-          so_mcast_dbg("pico_socket_setoption: ERROR PICO_IP_DROP_SOURCE_MEMBERSHIP before PICO_IP_ADD_SOURCE_MEMBERSHIP\n");
-          pico_err = PICO_ERR_EADDRNOTAVAIL;
-          return -1;
-        } else {
-          if (listen->filter_mode != PICO_IP_MULTICAST_INCLUDE) {
-            so_mcast_dbg("pico_socket_setoption: ERROR source-specific multicast (include) on any-source multicast (exclude)\n");
-            pico_err = PICO_ERR_EINVAL;
-            return -1;
-          }
-          stest.addr = mreq->mcast_source_addr.addr;
-          source = pico_tree_findKey(&listen->MCASTSources, &stest);
-          if (!source) {
-            so_mcast_dbg("pico_socket_setoption: ERROR address to drop not in source list\n");
-            pico_err = PICO_ERR_EADDRNOTAVAIL;
-            return -1;
-          } else {
-            pico_tree_delete(&listen->MCASTSources, source);
-            pico_free(source);
-            if (pico_tree_empty(&listen->MCASTSources)) { /* 1 if empty, 0 otherwise */
-              reference_count = 1;
-              pico_tree_delete(s->MCASTListen, listen);
-              pico_free(listen);
-              if (pico_tree_empty(s->MCASTListen)) {
-                pico_free(s->MCASTListen);
-                s->MCASTListen = NULL;
-                pico_tree_delete(&MCASTSockets, s);
-              }
+        /* INCLUDE mode */
+        if (s->proto->proto_number == PICO_PROTO_UDP) {
+            int filter_mode = 0, reference_count = 0;
+            struct pico_ip_mreq_source *mreq = (struct pico_ip_mreq_source *)value;
+            struct pico_mcast_listen *listen = NULL;
+            struct pico_ip4 *source = NULL, stest = {
+                0
+            };
+            struct pico_ipv4_link *mcast_link = setopt_multicast_check(s, value, 0, 1);
+            if (!mcast_link)
+                return -1;
+
+            listen = listen_find(s, &mreq->mcast_link_addr, &mreq->mcast_group_addr);
+            if (!listen) {
+                so_mcast_dbg("pico_socket_setoption: ERROR PICO_IP_DROP_SOURCE_MEMBERSHIP before PICO_IP_ADD_SOURCE_MEMBERSHIP\n");
+                pico_err = PICO_ERR_EADDRNOTAVAIL;
+                return -1;
+            } else {
+                if (listen->filter_mode != PICO_IP_MULTICAST_INCLUDE) {
+                    so_mcast_dbg("pico_socket_setoption: ERROR source-specific multicast (include) on any-source multicast (exclude)\n");
+                    pico_err = PICO_ERR_EINVAL;
+                    return -1;
+                }
+
+                stest.addr = mreq->mcast_source_addr.addr;
+                source = pico_tree_findKey(&listen->MCASTSources, &stest);
+                if (!source) {
+                    so_mcast_dbg("pico_socket_setoption: ERROR address to drop not in source list\n");
+                    pico_err = PICO_ERR_EADDRNOTAVAIL;
+                    return -1;
+                } else {
+                    pico_tree_delete(&listen->MCASTSources, source);
+                    pico_free(source);
+                    if (pico_tree_empty(&listen->MCASTSources)) { /* 1 if empty, 0 otherwise */
+                        reference_count = 1;
+                        pico_tree_delete(s->MCASTListen, listen);
+                        pico_free(listen);
+                        if (pico_tree_empty(s->MCASTListen)) {
+                            pico_free(s->MCASTListen);
+                            s->MCASTListen = NULL;
+                            pico_tree_delete(&MCASTSockets, s);
+                        }
+                    }
+                }
             }
-          }
+
+            filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
+            if (filter_mode < 0)
+                return -1;
+
+            return pico_ipv4_mcast_leave(&mreq->mcast_link_addr, &mreq->mcast_group_addr, (uint8_t)reference_count, (uint8_t)filter_mode, &MCASTFilter);
         }
 
-        filter_mode = pico_socket_aggregate_mcastfilters(&mcast_link->address, &mreq->mcast_group_addr);
-        if (filter_mode < 0) 
-          return -1;
-
-        return pico_ipv4_mcast_leave(&mreq->mcast_link_addr, &mreq->mcast_group_addr, (uint8_t)reference_count, (uint8_t)filter_mode, &MCASTFilter);
-      }          
-      break;
+        break;
 #endif /* PICO_SUPPORT_MCAST */
 
     default:
-          pico_err = PICO_ERR_EINVAL;
-          return -1;
-  }
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  if (pico_err != PICO_ERR_NOERR)
-    return -1;
-  else
-    return 0;
+    if (pico_err != PICO_ERR_NOERR)
+        return -1;
+    else
+        return 0;
 }
 
-#define PICO_SOCKET_GETOPT(socket,index) ((socket->opt_flags & (1 << index)) != 0)
+#define PICO_SOCKET_GETOPT(socket, index) ((socket->opt_flags & (1 << index)) != 0)
 
 int pico_socket_getoption(struct pico_socket *s, int option, void *value)
-{  
-  if (!s || !value) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  }
+{
+    if (!s || !value) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  switch (option)
-  {
+    switch (option)
+    {
 #ifdef PICO_SUPPORT_TCP
     case PICO_TCP_NODELAY:
-          if (s->proto->proto_number == PICO_PROTO_TCP)
+        if (s->proto->proto_number == PICO_PROTO_TCP)
             /* state of the NODELAY option */
-            *(int *)value = PICO_SOCKET_GETOPT(s,PICO_SOCKET_OPT_TCPNODELAY);
-          else
+            *(int *)value = PICO_SOCKET_GETOPT(s, PICO_SOCKET_OPT_TCPNODELAY);
+        else
             *(int *)value = 0;
-          break;
+
+        break;
 #endif
 
 #ifdef PICO_SUPPORT_MCAST
     case PICO_IP_MULTICAST_IF:
-          pico_err = PICO_ERR_EOPNOTSUPP;
-          return -1;
-          break;
+        pico_err = PICO_ERR_EOPNOTSUPP;
+        return -1;
+        break;
 
     case PICO_IP_MULTICAST_TTL:
-          if (s->proto->proto_number == PICO_PROTO_UDP) {
+        if (s->proto->proto_number == PICO_PROTO_UDP) {
             pico_udp_get_mc_ttl(s, (uint8_t *) value);
-          } else {
+        } else {
             *(uint8_t *)value = 0;
             pico_err = PICO_ERR_EINVAL;
             return -1;
-          }            
-          break;
+        }
+
+        break;
 
     case PICO_IP_MULTICAST_LOOP:
-          if (s->proto->proto_number == PICO_PROTO_UDP) {
-            *(uint8_t *)value = PICO_SOCKET_GETOPT(s,PICO_SOCKET_OPT_MULTICAST_LOOP);
-          } else {
+        if (s->proto->proto_number == PICO_PROTO_UDP) {
+            *(uint8_t *)value = PICO_SOCKET_GETOPT(s, PICO_SOCKET_OPT_MULTICAST_LOOP);
+        } else {
             *(uint8_t *)value = 0;
             pico_err = PICO_ERR_EINVAL;
             return -1;
-          }
-          break;
+        }
+
+        break;
 #endif /* PICO_SUPPORT_MCAST */
 
     default:
-          pico_err = PICO_ERR_EINVAL;
-          return -1;
-  }
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    }
 
-  return 0;
+    return 0;
 }
 
 
 int pico_socket_shutdown(struct pico_socket *s, int mode)
 {
-  if (!s) {
-    pico_err = PICO_ERR_EINVAL;
-    return -1;
-  } else {
-    /* check if exists in tree */
-    /* See task #178 */
-    if (pico_check_socket(s) != 0) {
-      pico_free(s); /* close socket after bind or connect failed */
-      return 0;
+    if (!s) {
+        pico_err = PICO_ERR_EINVAL;
+        return -1;
+    } else {
+        /* check if exists in tree */
+        /* See task #178 */
+        if (pico_check_socket(s) != 0) {
+            pico_free(s); /* close socket after bind or connect failed */
+            return 0;
+        }
     }
-  }
 
 #ifdef PICO_SUPPORT_UDP
-  if (PROTO(s) == PICO_PROTO_UDP) {
-    if (mode & PICO_SHUT_RDWR)
-      pico_socket_alter_state(s, PICO_SOCKET_STATE_CLOSED, PICO_SOCKET_STATE_CLOSING |PICO_SOCKET_STATE_BOUND | PICO_SOCKET_STATE_CONNECTED, 0);
-    else if (mode & PICO_SHUT_RD)
-      pico_socket_alter_state(s, 0, PICO_SOCKET_STATE_BOUND, 0);
-  }
+    if (PROTO(s) == PICO_PROTO_UDP) {
+        if (mode & PICO_SHUT_RDWR)
+            pico_socket_alter_state(s, PICO_SOCKET_STATE_CLOSED, PICO_SOCKET_STATE_CLOSING | PICO_SOCKET_STATE_BOUND | PICO_SOCKET_STATE_CONNECTED, 0);
+        else if (mode & PICO_SHUT_RD)
+            pico_socket_alter_state(s, 0, PICO_SOCKET_STATE_BOUND, 0);
+    }
+
 #endif
 #ifdef PICO_SUPPORT_TCP
-  if (PROTO(s) == PICO_PROTO_TCP) {
-  	if(mode & PICO_SHUT_RDWR)
-  	{
-  		pico_socket_alter_state(s, PICO_SOCKET_STATE_SHUT_LOCAL | PICO_SOCKET_STATE_SHUT_REMOTE, 0, 0);
-  		pico_tcp_notify_closing(s);
-  	}
-  	else if (mode & PICO_SHUT_WR)
-      pico_socket_alter_state(s, PICO_SOCKET_STATE_SHUT_LOCAL, 0, 0);
-    else if (mode & PICO_SHUT_RD)
-      pico_socket_alter_state(s, PICO_SOCKET_STATE_SHUT_REMOTE, 0, 0);
+    if (PROTO(s) == PICO_PROTO_TCP) {
+        if(mode & PICO_SHUT_RDWR)
+        {
+            pico_socket_alter_state(s, PICO_SOCKET_STATE_SHUT_LOCAL | PICO_SOCKET_STATE_SHUT_REMOTE, 0, 0);
+            pico_tcp_notify_closing(s);
+        }
+        else if (mode & PICO_SHUT_WR)
+            pico_socket_alter_state(s, PICO_SOCKET_STATE_SHUT_LOCAL, 0, 0);
+        else if (mode & PICO_SHUT_RD)
+            pico_socket_alter_state(s, PICO_SOCKET_STATE_SHUT_REMOTE, 0, 0);
 
-  }
+    }
+
 #endif
-  return 0;
+    return 0;
 }
 
 int pico_socket_close(struct pico_socket *s)
 {
-  return pico_socket_shutdown(s, PICO_SHUT_RDWR);
+    return pico_socket_shutdown(s, PICO_SHUT_RDWR);
 }
 
 #ifdef PICO_SUPPORT_CRC
 static inline int pico_transport_crc_check(struct pico_frame *f)
 {
-  struct pico_ipv4_hdr *net_hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-  struct pico_udp_hdr *udp_hdr = NULL;
-  uint16_t checksum_invalid = 1;
+    struct pico_ipv4_hdr *net_hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    struct pico_udp_hdr *udp_hdr = NULL;
+    uint16_t checksum_invalid = 1;
 
-  switch (net_hdr->proto)
-  {
+    switch (net_hdr->proto)
+    {
     case PICO_PROTO_TCP:
-      checksum_invalid = short_be(pico_tcp_checksum_ipv4(f));
-      //dbg("TCP CRC validation == %u\n", checksum_invalid);
-      if (checksum_invalid) {
-        //dbg("TCP CRC: validation failed!\n");
-        pico_frame_discard(f);
-        return 0;
-      }
-      break;
+        checksum_invalid = short_be(pico_tcp_checksum_ipv4(f));
+        /* dbg("TCP CRC validation == %u\n", checksum_invalid); */
+        if (checksum_invalid) {
+            /* dbg("TCP CRC: validation failed!\n"); */
+            pico_frame_discard(f);
+            return 0;
+        }
+
+        break;
 
     case PICO_PROTO_UDP:
-      udp_hdr = (struct pico_udp_hdr *) f->transport_hdr;
-      if (short_be(udp_hdr->crc)) {
-        checksum_invalid = short_be(pico_udp_checksum_ipv4(f));
-        //dbg("UDP CRC validation == %u\n", checksum_invalid);
-        if (checksum_invalid) {
-          //dbg("UDP CRC: validation failed!\n");
-          pico_frame_discard(f);
-          return 0;
+        udp_hdr = (struct pico_udp_hdr *) f->transport_hdr;
+        if (short_be(udp_hdr->crc)) {
+            checksum_invalid = short_be(pico_udp_checksum_ipv4(f));
+            /* dbg("UDP CRC validation == %u\n", checksum_invalid); */
+            if (checksum_invalid) {
+                /* dbg("UDP CRC: validation failed!\n"); */
+                pico_frame_discard(f);
+                return 0;
+            }
         }
-      }
-      break;
+
+        break;
 
     default:
-      // Do nothing
-      break;
-  }
-  return 1;
+        /* Do nothing */
+        break;
+    }
+    return 1;
 }
 #else
 static inline int pico_transport_crc_check(struct pico_frame *f)
 {
-	IGNORE_PARAMETER(f);
-	return 1;
+    IGNORE_PARAMETER(f);
+    return 1;
 }
 #endif /* PICO_SUPPORT_CRC */
 
 int pico_transport_process_in(struct pico_protocol *self, struct pico_frame *f)
 {
-  struct pico_trans *hdr = (struct pico_trans *) f->transport_hdr;
-  int ret = 0;
+    struct pico_trans *hdr = (struct pico_trans *) f->transport_hdr;
+    int ret = 0;
 
-  if (!hdr) {
-    pico_err = PICO_ERR_EFAULT;
-    return -1;
-  }
+    if (!hdr) {
+        pico_err = PICO_ERR_EFAULT;
+        return -1;
+    }
 
-  ret = pico_transport_crc_check(f);
-  if (ret < 1)
-    return ret;
-  else
-    ret = 0;
+    ret = pico_transport_crc_check(f);
+    if (ret < 1)
+        return ret;
+    else
+        ret = 0;
 
-  if ((hdr) && (pico_socket_deliver(self, f, hdr->dport) == 0))
-    return ret;
+    if ((hdr) && (pico_socket_deliver(self, f, hdr->dport) == 0))
+        return ret;
 
-  if (!IS_BCAST(f)) {
-    dbg("Socket not found... \n");
-    pico_notify_socket_unreachable(f);
+    if (!IS_BCAST(f)) {
+        dbg("Socket not found... \n");
+        pico_notify_socket_unreachable(f);
 #ifdef PICO_SUPPORT_TCP
-    /* if tcp protocol send RST segment */
-    //if (self->proto_number == PICO_PROTO_TCP)
-    //  pico_tcp_reply_rst(f);
+        /* if tcp protocol send RST segment */
+        /* if (self->proto_number == PICO_PROTO_TCP) */
+        /*  pico_tcp_reply_rst(f); */
 #endif
-    ret = -1;
-    pico_err = PICO_ERR_ENOENT;
-  }
-  pico_frame_discard(f);
-  return ret;
+        ret = -1;
+        pico_err = PICO_ERR_ENOENT;
+    }
+
+    pico_frame_discard(f);
+    return ret;
 }
 
 #define SL_LOOP_MIN 1
@@ -2102,219 +2226,231 @@
 static int checkSocketSanity(struct pico_socket *s)
 {
 
-// 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--;
-	  return -1;
-	}
-  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;
-	// if no activity, force the socket into closing state
-	if( TCP_STATE(s) == PICO_SOCKET_STATE_TCP_ESTABLISHED )
-	{
-	  s->wakeup(PICO_SOCK_EV_CLOSE,s);
-	  pico_socket_close(s);
-	  s->timestamp = PICO_TIME_MS();
-	}
-  }
-  return 0;
+/* 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--;
+            return -1;
+        }
+
+    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;
+
+        /* if no activity, force the socket into closing state */
+        if( TCP_STATE(s) == PICO_SOCKET_STATE_TCP_ESTABLISHED )
+        {
+            s->wakeup(PICO_SOCK_EV_CLOSE, s);
+            pico_socket_close(s);
+            s->timestamp = PICO_TIME_MS();
+        }
+    }
+
+    return 0;
 }
 #endif
 
 int pico_sockets_loop(int loop_score)
 {
 #ifdef PICO_SUPPORT_UDP
-  static struct pico_tree_node *index_udp;
+    static struct pico_tree_node *index_udp;
 #endif
 
 #ifdef PICO_SUPPORT_TCP
-  static struct pico_tree_node* index_tcp;
+    static struct pico_tree_node*index_tcp;
 #endif
 
-  struct pico_sockport *start;
-  struct pico_socket *s;
+    struct pico_sockport *start;
+    struct pico_socket *s;
 
 #ifdef PICO_SUPPORT_UDP
-  struct pico_frame *f;
-
-  if (sp_udp == NULL)
-  {
-    index_udp = pico_tree_firstNode(UDPTable.root);
-    sp_udp = index_udp->keyValue;
-  }
-
-  /* init start node */
-  start = sp_udp;
-
-  /* round-robin all transport protocols, break if traversed all protocols */
-  while (loop_score > SL_LOOP_MIN && sp_udp != NULL) {
-    struct pico_tree_node * index;
-
-    pico_tree_foreach(index,&sp_udp->socks){
-      s = index->keyValue;
-      f = pico_dequeue(&s->q_out);
-      while (f && (loop_score > 0)) {
-        pico_proto_udp.push(&pico_proto_udp, f);
-        loop_score -= 1;
-        f = pico_dequeue(&s->q_out);
-      }
-    }
-
-    index_udp = pico_tree_next(index_udp);
-    sp_udp = index_udp->keyValue;
+    struct pico_frame *f;
 
     if (sp_udp == NULL)
     {
-      index_udp = pico_tree_firstNode(UDPTable.root);
-      sp_udp = index_udp->keyValue;
+        index_udp = pico_tree_firstNode(UDPTable.root);
+        sp_udp = index_udp->keyValue;
     }
-    if (sp_udp == start)
-      break;
-  }
+
+    /* init start node */
+    start = sp_udp;
+
+    /* round-robin all transport protocols, break if traversed all protocols */
+    while (loop_score > SL_LOOP_MIN && sp_udp != NULL) {
+        struct pico_tree_node *index;
+
+        pico_tree_foreach(index, &sp_udp->socks){
+            s = index->keyValue;
+            f = pico_dequeue(&s->q_out);
+            while (f && (loop_score > 0)) {
+                pico_proto_udp.push(&pico_proto_udp, f);
+                loop_score -= 1;
+                f = pico_dequeue(&s->q_out);
+            }
+        }
+
+        index_udp = pico_tree_next(index_udp);
+        sp_udp = index_udp->keyValue;
+
+        if (sp_udp == NULL)
+        {
+            index_udp = pico_tree_firstNode(UDPTable.root);
+            sp_udp = index_udp->keyValue;
+        }
+
+        if (sp_udp == start)
+            break;
+    }
 #endif
 
 #ifdef PICO_SUPPORT_TCP
-  if (sp_tcp == NULL)
-  {
-    index_tcp = pico_tree_firstNode(TCPTable.root);
-    sp_tcp = index_tcp->keyValue;
-  }
-
-  /* init start node */
-  start = sp_tcp;
-
-  while (loop_score > SL_LOOP_MIN && sp_tcp != NULL) {
-    struct pico_tree_node * index = NULL, * safe_index = NULL;
-    pico_tree_foreach_safe(index, &sp_tcp->socks,safe_index){
-      s = index->keyValue;
-      loop_score = pico_tcp_output(s, loop_score);
-      if ((s->ev_pending) && s->wakeup) {
-        s->wakeup(s->ev_pending, s);
-        if(!s->parent)
-        	s->ev_pending = 0;
-      }
-      if (loop_score <= 0) {
-        loop_score = 0;
-        break;
-      }
-      if(checkSocketSanity(s) < 0)
-      {
-    	pico_socket_del(s);
-    	index_tcp = NULL; // forcing the restart of loop
-    	sp_tcp = NULL;
-    	break;
-      }
+    if (sp_tcp == NULL)
+    {
+        index_tcp = pico_tree_firstNode(TCPTable.root);
+        sp_tcp = index_tcp->keyValue;
     }
 
-    /* check if RB_FOREACH ended, if not, break to keep the cur sp_tcp */
-    if (!index_tcp || (index && index->keyValue) )
-      break;
+    /* init start node */
+    start = sp_tcp;
 
-    index_tcp = pico_tree_next(index_tcp);
-    sp_tcp = index_tcp->keyValue;
+    while (loop_score > SL_LOOP_MIN && sp_tcp != NULL) {
+        struct pico_tree_node *index = NULL, *safe_index = NULL;
+        pico_tree_foreach_safe(index, &sp_tcp->socks, safe_index){
+            s = index->keyValue;
+            loop_score = pico_tcp_output(s, loop_score);
+            if ((s->ev_pending) && s->wakeup) {
+                s->wakeup(s->ev_pending, s);
+                if(!s->parent)
+                    s->ev_pending = 0;
+            }
+
+            if (loop_score <= 0) {
+                loop_score = 0;
+                break;
+            }
 
-    if (sp_tcp == NULL)
-    {
-      index_tcp = pico_tree_firstNode(TCPTable.root);
-      sp_tcp = index_tcp->keyValue;
+            if(checkSocketSanity(s) < 0)
+            {
+                pico_socket_del(s);
+                index_tcp = NULL; /* forcing the restart of loop */
+                sp_tcp = NULL;
+                break;
+            }
+        }
+
+        /* check if RB_FOREACH ended, if not, break to keep the cur sp_tcp */
+        if (!index_tcp || (index && index->keyValue))
+            break;
+
+        index_tcp = pico_tree_next(index_tcp);
+        sp_tcp = index_tcp->keyValue;
+
+        if (sp_tcp == NULL)
+        {
+            index_tcp = pico_tree_firstNode(TCPTable.root);
+            sp_tcp = index_tcp->keyValue;
+        }
+
+        if (sp_tcp == start)
+            break;
     }
-    if (sp_tcp == start)
-      break;
-  }
 #endif
 
-  return loop_score;
+    return loop_score;
 }
 
 
 struct pico_frame *pico_socket_frame_alloc(struct pico_socket *s, uint16_t len)
 {
-  struct pico_frame *f = NULL;
+    struct pico_frame *f = NULL;
 
 #ifdef PICO_SUPPORT_IPV6
-  if (IS_SOCK_IPV6(s))
-    f = pico_proto_ipv6.alloc(&pico_proto_ipv6, len);
+    if (IS_SOCK_IPV6(s))
+        f = pico_proto_ipv6.alloc(&pico_proto_ipv6, len);
+
 #endif
 
 #ifdef PICO_SUPPORT_IPV4
-  if (IS_SOCK_IPV4(s))
-    f = pico_proto_ipv4.alloc(&pico_proto_ipv4, (uint16_t)len);
+    if (IS_SOCK_IPV4(s))
+        f = pico_proto_ipv4.alloc(&pico_proto_ipv4, (uint16_t)len);
+
 #endif
-  if (!f) {
-    pico_err = PICO_ERR_ENOMEM;
+    if (!f) {
+        pico_err = PICO_ERR_ENOMEM;
+        return f;
+    }
+
+    f->payload = f->transport_hdr;
+    f->payload_len = len;
+    f->sock = s;
     return f;
-  }
-  f->payload = f->transport_hdr;
-  f->payload_len = len;
-  f->sock = s;
-  return f;
 }
 
 int pico_transport_error(struct pico_frame *f, uint8_t proto, int code)
 {
-  int ret = -1;
-  struct pico_trans *trans = (struct pico_trans*) f->transport_hdr;
-  struct pico_sockport *port = NULL;
-  struct pico_socket *s = NULL;
-  switch (proto) {
+    int ret = -1;
+    struct pico_trans *trans = (struct pico_trans*) f->transport_hdr;
+    struct pico_sockport *port = NULL;
+    struct pico_socket *s = NULL;
+    switch (proto) {
 
 
 #ifdef PICO_SUPPORT_UDP
-  case PICO_PROTO_UDP:
-    port = pico_get_sockport(proto, trans->sport);
-    break;
+    case PICO_PROTO_UDP:
+        port = pico_get_sockport(proto, trans->sport);
+        break;
 #endif
 
 #ifdef PICO_SUPPORT_TCP
-  case PICO_PROTO_TCP:
-    port = pico_get_sockport(proto, trans->sport);
-    break;
+    case PICO_PROTO_TCP:
+        port = pico_get_sockport(proto, trans->sport);
+        break;
 #endif
 
-  default:
-    /* Protocol not available */
-    ret = -1;
-  }
-  if (port) {
-    struct pico_tree_node * index;
-    ret = 0;
+    default:
+        /* Protocol not available */
+        ret = -1;
+    }
+    if (port) {
+        struct pico_tree_node *index;
+        ret = 0;
 
-    pico_tree_foreach(index,&port->socks) {
-      s = index->keyValue;
-      if (trans->dport == s->remote_port) {
-        if (s->wakeup) {
-          //dbg("SOCKET ERROR FROM ICMP NOTIFICATION. (icmp code= %d)\n\n", code);
-          switch(code) {
-            case PICO_ICMP_UNREACH_PROTOCOL:
-              pico_err = PICO_ERR_EPROTO;
-              break;
+        pico_tree_foreach(index, &port->socks) {
+            s = index->keyValue;
+            if (trans->dport == s->remote_port) {
+                if (s->wakeup) {
+                    /* dbg("SOCKET ERROR FROM ICMP NOTIFICATION. (icmp code= %d)\n\n", code); */
+                    switch(code) {
+                    case PICO_ICMP_UNREACH_PROTOCOL:
+                        pico_err = PICO_ERR_EPROTO;
+                        break;
+
+                    case PICO_ICMP_UNREACH_PORT:
+                        pico_err = PICO_ERR_ECONNREFUSED;
+                        break;
 
-            case PICO_ICMP_UNREACH_PORT:
-              pico_err = PICO_ERR_ECONNREFUSED;
-              break;
-
-            case PICO_ICMP_UNREACH_NET:
-            case PICO_ICMP_UNREACH_NET_PROHIB:
-            case PICO_ICMP_UNREACH_NET_UNKNOWN:
-              pico_err = PICO_ERR_ENETUNREACH;
-              break;
+                    case PICO_ICMP_UNREACH_NET:
+                    case PICO_ICMP_UNREACH_NET_PROHIB:
+                    case PICO_ICMP_UNREACH_NET_UNKNOWN:
+                        pico_err = PICO_ERR_ENETUNREACH;
+                        break;
 
-            default:
-              pico_err = PICO_ERR_EHOSTUNREACH;
-          }
-          s->state |= PICO_SOCKET_STATE_SHUT_REMOTE;
-          s->wakeup(PICO_SOCK_EV_ERR, s);
+                    default:
+                        pico_err = PICO_ERR_EHOSTUNREACH;
+                    }
+                    s->state |= PICO_SOCKET_STATE_SHUT_REMOTE;
+                    s->wakeup(PICO_SOCK_EV_ERR, s);
 
+                }
+
+                break;
+            }
         }
-        break;
-      }
     }
-  }
-  pico_frame_discard(f);
-  return ret;
+
+    pico_frame_discard(f);
+    return ret;
 }
 #endif
 #endif
--- a/stack/pico_stack.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/stack/pico_stack.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,11 +1,11 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-.
+   .
 
-Authors: Daniele Lacamera
-*********************************************************************/
+   Authors: Daniele Lacamera
+ *********************************************************************/
 
 
 #include "pico_config.h"
@@ -27,11 +27,13 @@
 #include "pico_socket.h"
 #include "heap.h"
 
-#define IS_LIMITED_BCAST(f) ( ((struct pico_ipv4_hdr *) f->net_hdr)->dst.addr == PICO_IP4_BCAST )
+#define IS_LIMITED_BCAST(f) (((struct pico_ipv4_hdr *) f->net_hdr)->dst.addr == PICO_IP4_BCAST)
 
 #ifdef PICO_SUPPORT_MCAST
 # define PICO_SIZE_MCAST 3
-  const uint8_t PICO_ETHADDR_MCAST[6] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
+const uint8_t PICO_ETHADDR_MCAST[6] = {
+    0x01, 0x00, 0x5e, 0x00, 0x00, 0x00
+};
 #endif
 
 volatile pico_time pico_tick;
@@ -41,17 +43,18 @@
 
 void pico_rand_feed(uint32_t feed)
 {
-  if (!feed)
-    return;
-  _rand_seed *= 1664525;
-  _rand_seed += 1013904223;
-  _rand_seed ^= ~(feed);
+    if (!feed)
+        return;
+
+    _rand_seed *= 1664525;
+    _rand_seed += 1013904223;
+    _rand_seed ^= ~(feed);
 }
 
 uint32_t pico_rand(void)
 {
-  pico_rand_feed((uint32_t)pico_tick);
-  return _rand_seed;
+    pico_rand_feed((uint32_t)pico_tick);
+    return _rand_seed;
 }
 
 /* NOTIFICATIONS: distributed notifications for stack internal errors.
@@ -59,186 +62,196 @@
 
 int pico_notify_socket_unreachable(struct pico_frame *f)
 {
-  if (0) {}
-#ifdef PICO_SUPPORT_ICMP4 
-  else if (IS_IPV4(f)) {
-    pico_icmp4_port_unreachable(f);
-  }
+    if (0) {}
+
+#ifdef PICO_SUPPORT_ICMP4
+    else if (IS_IPV4(f)) {
+        pico_icmp4_port_unreachable(f);
+    }
 #endif
-#ifdef PICO_SUPPORT_ICMP6 
-  else if (IS_IPV6(f)) {
-    pico_icmp6_port_unreachable(f);
-  }
+#ifdef PICO_SUPPORT_ICMP6
+    else if (IS_IPV6(f)) {
+        pico_icmp6_port_unreachable(f);
+    }
 #endif
 
-  return 0;
+    return 0;
 }
 
 int pico_notify_proto_unreachable(struct pico_frame *f)
 {
-  if (0) {}
-#ifdef PICO_SUPPORT_ICMP4 
-  else if (IS_IPV4(f)) {
-    pico_icmp4_proto_unreachable(f);
-  }
+    if (0) {}
+
+#ifdef PICO_SUPPORT_ICMP4
+    else if (IS_IPV4(f)) {
+        pico_icmp4_proto_unreachable(f);
+    }
 #endif
-#ifdef PICO_SUPPORT_ICMP6 
-  else if (IS_IPV6(f)) {
-    pico_icmp6_proto_unreachable(f);
-  }
+#ifdef PICO_SUPPORT_ICMP6
+    else if (IS_IPV6(f)) {
+        pico_icmp6_proto_unreachable(f);
+    }
 #endif
-  return 0;
+    return 0;
 }
 
 int pico_notify_dest_unreachable(struct pico_frame *f)
 {
-  if (0) {}
-#ifdef PICO_SUPPORT_ICMP4 
-  else if (IS_IPV4(f)) {
-    pico_icmp4_dest_unreachable(f);
-  }
+    if (0) {}
+
+#ifdef PICO_SUPPORT_ICMP4
+    else if (IS_IPV4(f)) {
+        pico_icmp4_dest_unreachable(f);
+    }
 #endif
-#ifdef PICO_SUPPORT_ICMP6 
-  else if (IS_IPV6(f)) {
-    pico_icmp6_dest_unreachable(f);
-  }
+#ifdef PICO_SUPPORT_ICMP6
+    else if (IS_IPV6(f)) {
+        pico_icmp6_dest_unreachable(f);
+    }
 #endif
-  return 0;
+    return 0;
 }
 
 int pico_notify_ttl_expired(struct pico_frame *f)
 {
-  if (0) {}
-#ifdef PICO_SUPPORT_ICMP4 
-  else if (IS_IPV4(f)) {
-    pico_icmp4_ttl_expired(f);
-  }
+    if (0) {}
+
+#ifdef PICO_SUPPORT_ICMP4
+    else if (IS_IPV4(f)) {
+        pico_icmp4_ttl_expired(f);
+    }
 #endif
-#ifdef PICO_SUPPORT_ICMP6 
-  else if (IS_IPV6(f)) {
-    pico_icmp6_ttl_expired(f);
-  }
+#ifdef PICO_SUPPORT_ICMP6
+    else if (IS_IPV6(f)) {
+        pico_icmp6_ttl_expired(f);
+    }
 #endif
-  return 0;
+    return 0;
 }
 
 
 /* Transport layer */
 int32_t pico_transport_receive(struct pico_frame *f, uint8_t proto)
 {
-	int32_t ret = -1;
-  switch (proto) {
+    int32_t ret = -1;
+    switch (proto) {
 
 #ifdef PICO_SUPPORT_ICMP4
-  case PICO_PROTO_ICMP4:
-    ret = pico_enqueue(pico_proto_icmp4.q_in, f);
-    break;
+    case PICO_PROTO_ICMP4:
+        ret = pico_enqueue(pico_proto_icmp4.q_in, f);
+        break;
 #endif
 
 #ifdef PICO_SUPPORT_IGMP
-  case PICO_PROTO_IGMP:
-    ret = pico_enqueue(pico_proto_igmp.q_in, f);
-    break;
+    case PICO_PROTO_IGMP:
+        ret = pico_enqueue(pico_proto_igmp.q_in, f);
+        break;
 #endif
 
 #ifdef PICO_SUPPORT_UDP
-  case PICO_PROTO_UDP:
-    ret = pico_enqueue(pico_proto_udp.q_in, f);
-    break;
+    case PICO_PROTO_UDP:
+        ret = pico_enqueue(pico_proto_udp.q_in, f);
+        break;
 #endif
 
 #ifdef PICO_SUPPORT_TCP
-  case PICO_PROTO_TCP:
-    ret = pico_enqueue(pico_proto_tcp.q_in, f);
-    break;
+    case PICO_PROTO_TCP:
+        ret = pico_enqueue(pico_proto_tcp.q_in, f);
+        break;
 #endif
 
-  default:
-    /* Protocol not available */
-    dbg("pkt: no such protocol (%d)\n", proto);
-    pico_notify_proto_unreachable(f);
-    pico_frame_discard(f);
-    ret = -1;
- }
- return ret;
+    default:
+        /* Protocol not available */
+        dbg("pkt: no such protocol (%d)\n", proto);
+        pico_notify_proto_unreachable(f);
+        pico_frame_discard(f);
+        ret = -1;
+    }
+    return ret;
 }
 
 int32_t pico_transport_send(struct pico_frame *f)
 {
-  if (!f || !f->sock || !f->sock->proto) {
-    pico_frame_discard(f);
-    return -1;
-  }
-  return f->sock->proto->push(f->sock->net, f);
+    if (!f || !f->sock || !f->sock->proto) {
+        pico_frame_discard(f);
+        return -1;
+    }
+
+    return f->sock->proto->push(f->sock->net, f);
 }
 
 int32_t pico_network_receive(struct pico_frame *f)
 {
-  if (0) {}
+    if (0) {}
+
 #ifdef PICO_SUPPORT_IPV4
-  else if (IS_IPV4(f)) {
-    pico_enqueue(pico_proto_ipv4.q_in, f);
-  }
+    else if (IS_IPV4(f)) {
+        pico_enqueue(pico_proto_ipv4.q_in, f);
+    }
 #endif
 #ifdef PICO_SUPPORT_IPV6
-  else if (IS_IPV6(f)) {
-    pico_enqueue(pico_proto_ipv6.q_in, f);
-  }
+    else if (IS_IPV6(f)) {
+        pico_enqueue(pico_proto_ipv6.q_in, f);
+    }
 #endif
-  else {
-    dbg("Network not found.\n");
-    pico_frame_discard(f);
-    return -1;
-  }
-  return (int32_t)f->buffer_len;
+    else {
+        dbg("Network not found.\n");
+        pico_frame_discard(f);
+        return -1;
+    }
+    return (int32_t)f->buffer_len;
 }
 
 
 /* Network layer: interface towards socket for frame sending */
 int32_t pico_network_send(struct pico_frame *f)
 {
-  if (!f || !f->sock || !f->sock->net) {
-    pico_frame_discard(f);
-    return -1;
-  }
-  return f->sock->net->push(f->sock->net, f);
+    if (!f || !f->sock || !f->sock->net) {
+        pico_frame_discard(f);
+        return -1;
+    }
+
+    return f->sock->net->push(f->sock->net, f);
 }
 
 int pico_destination_is_local(struct pico_frame *f)
 {
-  if (0) { }
+    if (0) { }
+
 #ifdef PICO_SUPPORT_IPV4
-  else if (IS_IPV4(f)) {
-    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *)f->net_hdr;
-    if (pico_ipv4_link_find(&hdr->dst))
-      return 1;
-  }
+    else if (IS_IPV4(f)) {
+        struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *)f->net_hdr;
+        if (pico_ipv4_link_find(&hdr->dst))
+            return 1;
+    }
 #endif
 #ifdef PICO_SUPPORT_IPV6
-  else if (IS_IPV6(f)) {
-  }
+    else if (IS_IPV6(f)) {
+    }
 #endif
-  return 0;
+    return 0;
 }
 
 int pico_source_is_local(struct pico_frame *f)
 {
-  if (0) { }
+    if (0) { }
+
 #ifdef PICO_SUPPORT_IPV4
-  else if (IS_IPV4(f)) {
-    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *)f->net_hdr;
-    if (hdr->src.addr == PICO_IPV4_INADDR_ANY)
-      return 1;
-    if (pico_ipv4_link_find(&hdr->src))
-      return 1;
-  }
+    else if (IS_IPV4(f)) {
+        struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *)f->net_hdr;
+        if (hdr->src.addr == PICO_IPV4_INADDR_ANY)
+            return 1;
+
+        if (pico_ipv4_link_find(&hdr->src))
+            return 1;
+    }
 #endif
 #ifdef PICO_SUPPORT_IPV6
-  else if (IS_IPV6(f)) {
-  /* XXX */
-  }
+    else if (IS_IPV6(f)) {
+        /* XXX */
+    }
 #endif
-  return 0;
+    return 0;
 
 
 }
@@ -248,76 +261,81 @@
  * and vice versa.
  */
 
-/* The pico_ethernet_receive() function is used by 
- * those devices supporting ETH in order to push packets up 
- * into the stack. 
+/* The pico_ethernet_receive() function is used by
+ * those devices supporting ETH in order to push packets up
+ * into the stack.
  */
 int32_t pico_ethernet_receive(struct pico_frame *f)
 {
-  struct pico_eth_hdr *hdr;
-  if (!f || !f->dev || !f->datalink_hdr)
-    goto discard;
-  hdr = (struct pico_eth_hdr *) f->datalink_hdr;
-  if ( (memcmp(hdr->daddr, f->dev->eth->mac.addr, PICO_SIZE_ETH) != 0) && 
+    struct pico_eth_hdr *hdr;
+    if (!f || !f->dev || !f->datalink_hdr)
+        goto discard;
+
+    hdr = (struct pico_eth_hdr *) f->datalink_hdr;
+    if ((memcmp(hdr->daddr, f->dev->eth->mac.addr, PICO_SIZE_ETH) != 0) &&
 #ifdef PICO_SUPPORT_MCAST
-    (memcmp(hdr->daddr, PICO_ETHADDR_MCAST, PICO_SIZE_MCAST) != 0) &&
+        (memcmp(hdr->daddr, PICO_ETHADDR_MCAST, PICO_SIZE_MCAST) != 0) &&
 #endif
-    (memcmp(hdr->daddr, PICO_ETHADDR_ALL, PICO_SIZE_ETH) != 0) ) 
-    goto discard;
+        (memcmp(hdr->daddr, PICO_ETHADDR_ALL, PICO_SIZE_ETH) != 0))
+        goto discard;
+
+    f->net_hdr = f->datalink_hdr + sizeof(struct pico_eth_hdr);
+    if (hdr->proto == PICO_IDETH_ARP)
+        return pico_arp_receive(f);
 
-  f->net_hdr = f->datalink_hdr + sizeof(struct pico_eth_hdr);
-  if (hdr->proto == PICO_IDETH_ARP)
-    return pico_arp_receive(f);
-  if ((hdr->proto == PICO_IDETH_IPV4) || (hdr->proto == PICO_IDETH_IPV6))
-    return pico_network_receive(f);
+    if ((hdr->proto == PICO_IDETH_IPV4) || (hdr->proto == PICO_IDETH_IPV6))
+        return pico_network_receive(f);
+
 discard:
-  pico_frame_discard(f);
-  return -1;
+    pico_frame_discard(f);
+    return -1;
 }
 
 static int destination_is_bcast(struct pico_frame *f)
 {
-  if (!f)
-    return 0;
+    if (!f)
+        return 0;
 
-  if (IS_IPV6(f))
-    return 0;
+    if (IS_IPV6(f))
+        return 0;
+
 #ifdef PICO_SUPPORT_IPV4
-  else {
-    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-    return pico_ipv4_is_broadcast(hdr->dst.addr);
-  }
+    else {
+        struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+        return pico_ipv4_is_broadcast(hdr->dst.addr);
+    }
 #endif
-  return 0;
+    return 0;
 }
 
 #ifdef PICO_SUPPORT_MCAST
 static int destination_is_mcast(struct pico_frame *f)
 {
-  if (!f)
-    return 0;
+    if (!f)
+        return 0;
 
-  if (IS_IPV6(f))
-    return 0;
+    if (IS_IPV6(f))
+        return 0;
+
 #ifdef PICO_SUPPORT_IPV4
-  else {
-    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-    return pico_ipv4_is_multicast(hdr->dst.addr);
-  }
+    else {
+        struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+        return pico_ipv4_is_multicast(hdr->dst.addr);
+    }
 #endif
-  return 0;
+    return 0;
 }
 
 static struct pico_eth *pico_ethernet_mcast_translate(struct pico_frame *f, uint8_t *pico_mcast_mac)
 {
-  struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
 
-  /* place 23 lower bits of IP in lower 23 bits of MAC */
-  pico_mcast_mac[5] = (long_be(hdr->dst.addr) & 0x000000FF);
-  pico_mcast_mac[4] = (uint8_t)((long_be(hdr->dst.addr) & 0x0000FF00) >> 8);
-  pico_mcast_mac[3] = (uint8_t)((long_be(hdr->dst.addr) & 0x007F0000) >> 16);
+    /* place 23 lower bits of IP in lower 23 bits of MAC */
+    pico_mcast_mac[5] = (long_be(hdr->dst.addr) & 0x000000FF);
+    pico_mcast_mac[4] = (uint8_t)((long_be(hdr->dst.addr) & 0x0000FF00) >> 8);
+    pico_mcast_mac[3] = (uint8_t)((long_be(hdr->dst.addr) & 0x007F0000) >> 16);
 
-  return (struct pico_eth *)pico_mcast_mac;
+    return (struct pico_eth *)pico_mcast_mac;
 }
 
 
@@ -325,91 +343,98 @@
 
 /* This is called by dev loop in order to ensure correct ethernet addressing.
  * Returns 0 if the destination is unknown, and -1 if the packet is not deliverable
- * due to ethernet addressing (i.e., no arp association was possible. 
+ * due to ethernet addressing (i.e., no arp association was possible.
  *
  * Only IP packets must pass by this. ARP will always use direct dev->send() function, so
  * we assume IP is used.
  */
 int32_t pico_ethernet_send(struct pico_frame *f)
 {
-  const struct pico_eth *dstmac = NULL;
-  int32_t ret = -1;
-
-  if (IS_IPV6(f)) {
-    /*TODO: Neighbor solicitation */
-    dstmac = NULL;
-  }
+    const struct pico_eth *dstmac = NULL;
+    int32_t ret = -1;
 
-  else if (IS_IPV4(f)) {
-    if (IS_BCAST(f) || destination_is_bcast(f)) {
-      dstmac = (const struct pico_eth *const) PICO_ETHADDR_ALL;
-    } 
-#ifdef PICO_SUPPORT_MCAST
-    else if (destination_is_mcast(f)) {
-      uint8_t pico_mcast_mac[6] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0x00};
-      dstmac = pico_ethernet_mcast_translate(f, pico_mcast_mac);
-    } 
-#endif
-    else {
-      dstmac = pico_arp_get(f);
-      if (!dstmac)
-        return 0;
+    if (IS_IPV6(f)) {
+        /*TODO: Neighbor solicitation */
+        dstmac = NULL;
     }
-    /* This sets destination and source address, then pushes the packet to the device. */
-    if (dstmac && (f->start > f->buffer) && ((f->start - f->buffer) >= PICO_SIZE_ETHHDR)) {
-      struct pico_eth_hdr *hdr;
-      f->start -= PICO_SIZE_ETHHDR;
-      f->len += PICO_SIZE_ETHHDR;
-      f->datalink_hdr = f->start;
-      hdr = (struct pico_eth_hdr *) f->datalink_hdr;
-      memcpy(hdr->saddr, f->dev->eth->mac.addr, PICO_SIZE_ETH);
-      memcpy(hdr->daddr, dstmac, PICO_SIZE_ETH);
-      hdr->proto = PICO_IDETH_IPV4;
-      if(!memcmp(hdr->daddr, hdr->saddr, PICO_SIZE_ETH)){
-        dbg("sending out packet destined for our own mac\n");
-      return pico_ethernet_receive(f);
-      }else if(IS_LIMITED_BCAST(f)){
-        ret = pico_device_broadcast(f);
-      }else {
-        ret = (int32_t)f->dev->send(f->dev, f->start,(int) f->len);
-        /* Frame is discarded after this return by the caller */
-      }
+
+    else if (IS_IPV4(f)) {
+        if (IS_BCAST(f) || destination_is_bcast(f)) {
+            dstmac = (const struct pico_eth *const) PICO_ETHADDR_ALL;
+        }
 
-      if(!ret) pico_frame_discard(f);
-      	return ret;
-    } else {
-      return -1;
-    }
-  } /* End IPV4 ethernet addressing */
-  return -1;
+#ifdef PICO_SUPPORT_MCAST
+        else if (destination_is_mcast(f)) {
+            uint8_t pico_mcast_mac[6] = {
+                0x01, 0x00, 0x5e, 0x00, 0x00, 0x00
+            };
+            dstmac = pico_ethernet_mcast_translate(f, pico_mcast_mac);
+        }
+#endif
+        else {
+            dstmac = pico_arp_get(f);
+            if (!dstmac)
+                return 0;
+        }
+        /* This sets destination and source address, then pushes the packet to the device. */
+        if (dstmac && (f->start > f->buffer) && ((f->start - f->buffer) >= PICO_SIZE_ETHHDR)) {
+            struct pico_eth_hdr *hdr;
+            f->start -= PICO_SIZE_ETHHDR;
+            f->len += PICO_SIZE_ETHHDR;
+            f->datalink_hdr = f->start;
+            hdr = (struct pico_eth_hdr *) f->datalink_hdr;
+            memcpy(hdr->saddr, f->dev->eth->mac.addr, PICO_SIZE_ETH);
+            memcpy(hdr->daddr, dstmac, PICO_SIZE_ETH);
+            hdr->proto = PICO_IDETH_IPV4;
+            if(!memcmp(hdr->daddr, hdr->saddr, PICO_SIZE_ETH)) {
+                dbg("sending out packet destined for our own mac\n");
+                return pico_ethernet_receive(f);
+            }else if(IS_LIMITED_BCAST(f)) {
+                ret = pico_device_broadcast(f);
+            }else {
+                ret = (int32_t)f->dev->send(f->dev, f->start, (int) f->len);
+                /* Frame is discarded after this return by the caller */
+            }
+
+            if(!ret) pico_frame_discard(f);
+
+            return ret;
+        } else {
+            return -1;
+        }
+    } /* End IPV4 ethernet addressing */
+
+    return -1;
 
 }
 
 void pico_store_network_origin(void *src, struct pico_frame *f)
 {
   #ifdef PICO_SUPPORT_IPV4
-  struct pico_ip4 *ip4;
+    struct pico_ip4 *ip4;
   #endif
 
   #ifdef PICO_SUPPORT_IPV6
-  struct pico_ip6 *ip6;
+    struct pico_ip6 *ip6;
   #endif
 
   #ifdef PICO_SUPPORT_IPV4
-  if (IS_IPV4(f)) {
-    struct pico_ipv4_hdr *hdr;
-    hdr = (struct pico_ipv4_hdr *) f->net_hdr;
-    ip4 = (struct pico_ip4 *) src;
-    ip4->addr = hdr->src.addr;
-  }
+    if (IS_IPV4(f)) {
+        struct pico_ipv4_hdr *hdr;
+        hdr = (struct pico_ipv4_hdr *) f->net_hdr;
+        ip4 = (struct pico_ip4 *) src;
+        ip4->addr = hdr->src.addr;
+    }
+
   #endif
   #ifdef PICO_SUPPORT_IPV6
-  if (IS_IPV6(f)) {
-    struct pico_ipv6_hdr *hdr;
-    hdr = (struct pico_ipv6_hdr *) f->net_hdr;
-    ip6 = (struct pico_ip6 *) src;
-    memcpy(ip6->addr, hdr->src.addr, PICO_SIZE_IP6);
-  }
+    if (IS_IPV6(f)) {
+        struct pico_ipv6_hdr *hdr;
+        hdr = (struct pico_ipv6_hdr *) f->net_hdr;
+        ip6 = (struct pico_ip6 *) src;
+        memcpy(ip6->addr, hdr->src.addr, PICO_SIZE_IP6);
+    }
+
   #endif
 }
 
@@ -420,59 +445,63 @@
  */
 int32_t pico_stack_recv(struct pico_device *dev, uint8_t *buffer, uint32_t len)
 {
-  struct pico_frame *f;
-  int32_t ret;
-  if (len <= 0)
-    return -1;
-  f = pico_frame_alloc(len);
-  if (!f)
-    return -1;
+    struct pico_frame *f;
+    int32_t ret;
+    if (len <= 0)
+        return -1;
 
-  /* Association to the device that just received the frame. */
-  f->dev = dev;
+    f = pico_frame_alloc(len);
+    if (!f)
+        return -1;
+
+    /* Association to the device that just received the frame. */
+    f->dev = dev;
 
-  /* Setup the start pointer, lenght. */
-  f->start = f->buffer;
-  f->len = f->buffer_len;
-  if (f->len > 8) {
-    uint32_t mid_frame = (f->buffer_len >> 2)<<1;
-    mid_frame -= (mid_frame % 4);
-    pico_rand_feed(*(uint32_t*)(f->buffer + mid_frame));
-  }
-  memcpy(f->buffer, buffer, len);
-  ret = pico_enqueue(dev->q_in, f);
-  if (ret <= 0) {
-    pico_frame_discard(f);
-  }
-  return ret;
+    /* Setup the start pointer, lenght. */
+    f->start = f->buffer;
+    f->len = f->buffer_len;
+    if (f->len > 8) {
+        uint32_t mid_frame = (f->buffer_len >> 2) << 1;
+        mid_frame -= (mid_frame % 4);
+        pico_rand_feed(*(uint32_t*)(f->buffer + mid_frame));
+    }
+
+    memcpy(f->buffer, buffer, len);
+    ret = pico_enqueue(dev->q_in, f);
+    if (ret <= 0) {
+        pico_frame_discard(f);
+    }
+
+    return ret;
 }
 
 int32_t pico_sendto_dev(struct pico_frame *f)
 {
-  if (!f->dev) {
-    pico_frame_discard(f);
-    return -1;
-  } else {
-    if (f->len > 8) {
-      uint32_t mid_frame = (f->buffer_len >> 2)<<1;
-      mid_frame -= (mid_frame % 4);
-      pico_rand_feed(*(uint32_t*)(f->buffer + mid_frame));
+    if (!f->dev) {
+        pico_frame_discard(f);
+        return -1;
+    } else {
+        if (f->len > 8) {
+            uint32_t mid_frame = (f->buffer_len >> 2) << 1;
+            mid_frame -= (mid_frame % 4);
+            pico_rand_feed(*(uint32_t*)(f->buffer + mid_frame));
+        }
+
+        return pico_enqueue(f->dev->q_out, f);
     }
-    return pico_enqueue(f->dev->q_out, f);
-  }
 }
 
 struct pico_timer
 {
-  uint32_t expire;
-  void *arg;
-  void (*timer)(pico_time timestamp, void *arg);
+    uint32_t expire;
+    void *arg;
+    void (*timer)(pico_time timestamp, void *arg);
 };
 
 struct pico_timer_ref
 {
-  pico_time expire;
-  struct pico_timer *tmr;
+    pico_time expire;
+    struct pico_timer *tmr;
 };
 
 typedef struct pico_timer_ref pico_timer_ref;
@@ -483,34 +512,37 @@
 
 void pico_check_timers(void)
 {
-  struct pico_timer *t;
-  struct pico_timer_ref tref_unused, *tref = heap_first(Timers);
-  pico_tick = PICO_TIME_MS();
-  while((tref) && (tref->expire < pico_tick)) {
-    t = tref->tmr;
-    if (t && t->timer)
-      t->timer(pico_tick, t->arg);
-    if (t)
-      pico_free(t);
-    t = NULL;
-    heap_peek(Timers, &tref_unused);
-    tref = heap_first(Timers);
-  }
+    struct pico_timer *t;
+    struct pico_timer_ref tref_unused, *tref = heap_first(Timers);
+    pico_tick = PICO_TIME_MS();
+    while((tref) && (tref->expire < pico_tick)) {
+        t = tref->tmr;
+        if (t && t->timer)
+            t->timer(pico_tick, t->arg);
+
+        if (t)
+            pico_free(t);
+
+        t = NULL;
+        heap_peek(Timers, &tref_unused);
+        tref = heap_first(Timers);
+    }
 }
 
 void pico_timer_cancel(struct pico_timer *t)
 {
-  uint32_t i;
-  struct pico_timer_ref *tref = Timers->top;
-  if (!t)
-    return;
-  for (i = 1; i <= Timers->n; i++) {
-    if (tref[i].tmr == t) {
-      Timers->top[i].tmr = NULL;
-      pico_free(t);
-      break;
+    uint32_t i;
+    struct pico_timer_ref *tref = Timers->top;
+    if (!t)
+        return;
+
+    for (i = 1; i <= Timers->n; i++) {
+        if (tref[i].tmr == t) {
+            Timers->top[i].tmr = NULL;
+            pico_free(t);
+            break;
+        }
     }
-  }
 }
 
 
@@ -520,141 +552,148 @@
 #define PROTO_MIN_SCORE   32
 #define PROTO_MAX_SCORE   128
 #define PROTO_LAT_IND     3   /* latecy indication 0-3 (lower is better latency performance), x1, x2, x4, x8 */
-#define PROTO_MAX_LOOP    (PROTO_MAX_SCORE<<PROTO_LAT_IND) /* max global loop score, so per tick */
+#define PROTO_MAX_LOOP    (PROTO_MAX_SCORE << PROTO_LAT_IND) /* max global loop score, so per tick */
 
 static int calc_score(int *score, int *index, int avg[][PROTO_DEF_AVG_NR], int *ret)
 {
-  int temp, i, j, sum;
-  int max_total = PROTO_MAX_LOOP, total = 0;
+    int temp, i, j, sum;
+    int max_total = PROTO_MAX_LOOP, total = 0;
 
-  //dbg("USED SCORES> "); 
+    /* dbg("USED SCORES> "); */
 
-  for (i = 0; i < PROTO_DEF_NR; i++) {
+    for (i = 0; i < PROTO_DEF_NR; i++) {
 
-    /* if used looped score */
-    if (ret[i] < score[i]) {
-      temp = score[i] - ret[i]; /* remaining loop score */
-      
-      //dbg("%3d - ",temp);
+        /* if used looped score */
+        if (ret[i] < score[i]) {
+            temp = score[i] - ret[i]; /* remaining loop score */
+
+            /* dbg("%3d - ",temp); */
 
-      if (index[i] >= PROTO_DEF_AVG_NR)
-        index[i] = 0;           /* reset index */
-      j = index[i];
-      avg[i][j] = temp;
+            if (index[i] >= PROTO_DEF_AVG_NR)
+                index[i] = 0;   /* reset index */
+
+            j = index[i];
+            avg[i][j] = temp;
 
-      index[i]++; 
+            index[i]++;
 
-      if (ret[i] == 0 && (score[i]<<1 <= PROTO_MAX_SCORE) && ((total+(score[i]<<1)) < max_total) ) {        /* used all loop score -> increase next score directly */
-        score[i] <<= 1;
-        total += score[i];
-        continue;
-      }
+            if (ret[i] == 0 && (score[i] << 1 <= PROTO_MAX_SCORE) && ((total + (score[i] << 1)) < max_total)) { /* used all loop score -> increase next score directly */
+                score[i] <<= 1;
+                total += score[i];
+                continue;
+            }
 
-      sum = 0;
-      for (j = 0; j < PROTO_DEF_AVG_NR; j++)
-        sum += avg[i][j];       /* calculate sum */
+            sum = 0;
+            for (j = 0; j < PROTO_DEF_AVG_NR; j++)
+                sum += avg[i][j]; /* calculate sum */
 
-      sum >>= 2;                /* divide by 4 to get average used score */
+            sum >>= 2;          /* divide by 4 to get average used score */
 
-      /* criterion to increase next loop score */
-      if (sum > (score[i] - (score[i]>>2))  && (score[i]<<1 <= PROTO_MAX_SCORE) && ((total+(score[i]<<1)) < max_total)) { /* > 3/4 */
-        score[i] <<= 1;         /* double loop score */
-        total += score[i];
-        continue;
-      }
+            /* criterion to increase next loop score */
+            if (sum > (score[i] - (score[i] >> 2))  && (score[i] << 1 <= PROTO_MAX_SCORE) && ((total + (score[i] << 1)) < max_total)) { /* > 3/4 */
+                score[i] <<= 1; /* double loop score */
+                total += score[i];
+                continue;
+            }
 
-      /* criterion to decrease next loop score */
-      if (sum < (score[i]>>2) && (score[i]>>1 >= PROTO_MIN_SCORE)) { /* < 1/4 */
-        score[i] >>= 1;         /* half loop score */
-        total += score[i];
-        continue;
-      }
+            /* criterion to decrease next loop score */
+            if (sum < (score[i] >> 2) && (score[i] >> 1 >= PROTO_MIN_SCORE)) { /* < 1/4 */
+                score[i] >>= 1; /* half loop score */
+                total += score[i];
+                continue;
+            }
 
-      /* also add non-changed scores */
-      total += score[i];
-    }
-    else if (ret[i] == score[i]) {
-      /* no used loop score - gradually decrease */
-      
-    //  dbg("%3d - ",0);
+            /* also add non-changed scores */
+            total += score[i];
+        }
+        else if (ret[i] == score[i]) {
+            /* no used loop score - gradually decrease */
 
-      if (index[i] >= PROTO_DEF_AVG_NR)
-        index[i] = 0;           /* reset index */
-      j = index[i];
-      avg[i][j] = 0;
+            /*  dbg("%3d - ",0); */
+
+            if (index[i] >= PROTO_DEF_AVG_NR)
+                index[i] = 0;   /* reset index */
+
+            j = index[i];
+            avg[i][j] = 0;
 
-      index[i]++; 
+            index[i]++;
 
-      sum = 0;
-      for (j = 0; j < PROTO_DEF_AVG_NR; j++)
-        sum += avg[i][j];       /* calculate sum */
+            sum = 0;
+            for (j = 0; j < PROTO_DEF_AVG_NR; j++)
+                sum += avg[i][j]; /* calculate sum */
 
-      sum >>= 2;                /* divide by 4 to get average used score */
+            sum >>= 2;          /* divide by 4 to get average used score */
 
-      if ((sum == 0) && (score[i]>>1 >= PROTO_MIN_SCORE)) {
-        score[i] >>= 1;         /* half loop score */
-        total += score[i];
-        for (j = 0; j < PROTO_DEF_AVG_NR; j++)
-          avg[i][j] = score[i];
-      }
-      
+            if ((sum == 0) && (score[i] >> 1 >= PROTO_MIN_SCORE)) {
+                score[i] >>= 1; /* half loop score */
+                total += score[i];
+                for (j = 0; j < PROTO_DEF_AVG_NR; j++)
+                    avg[i][j] = score[i];
+            }
+
+        }
     }
-  }
+    /* dbg("\n"); */
 
-  //dbg("\n");
-
-  return 0;
+    return 0;
 }
 
 
 
-/* 
+/*
 
-         .                                                               
-       .vS.                                                              
-     <aoSo.                                                              
-    .XoS22.                                                              
-    .S2S22.             ._...              ......            ..._.       
-  :=|2S2X2|=++;      <vSX2XX2z+          |vSSSXSSs>.      :iXXZUZXXe=    
-  )2SS2SS2S2S2I    =oS2S2S2S2X22;.    _vuXS22S2S2S22i  ._wZZXZZZXZZXZX=  
-  )22S2S2S2S2Sl    |S2S2S22S2SSSXc:  .S2SS2S2S22S2SS= .]#XZZZXZXZZZZZZ:  
-  )oSS2SS2S2Sol     |2}!"""!32S22S(. uS2S2Se**12oS2e  ]dXZZXX2?YYXXXZ*   
-   .:2S2So:..-.      .      :]S2S2e;=X2SS2o     .)oc  ]XZZXZ(     =nX:   
-    .S2S22.          ___s_i,.)oS2So(;2SS2So,       `  3XZZZZc,      -    
-    .S2SSo.        =oXXXSSS2XoS2S2o( XS2S2XSos;.      ]ZZZZXXXX|=        
-    .S2S22.      .)S2S2S22S2S2S2S2o( "X2SS2S2S2Sus,,  +3XZZZZZXZZoos_    
-    .S2S22.     .]2S2SS22S222S2SS2o(  ]S22S2S2S222So   :3XXZZZZZZZZXXv   
-    .S2S22.     =u2SS2e"~---"{2S2So(   -"12S2S2SSS2Su.   "?SXXXZXZZZZXo  
-    .S2SSo.     )SS22z;      :S2S2o(       ={vS2S2S22v      .<vXZZZZZZZ; 
-    .S2S2S:     ]oSS2c;      =22S2o(          -"S2SS2n          ~4XXZXZ( 
-    .2S2S2i     )2S2S2[.    .)XS2So(  <;.      .2S2S2o :<.       ]XZZZX( 
-     nX2S2S,,_s_=3oSS2SoaasuXXS2S2o( .oXoasi_aioSSS22l.]dZoaas_aadXZZXZ' 
-     vS2SSSXXX2; )S2S2S2SoS2S2S2S2o( iS2S222XSoSS22So.)nXZZXXXZZXXZZXZo  
-     +32S22S2Sn  -+S2S2S2S2So22S2So( 12S2SS2S2SS22S}- )SXXZZZZZZZZZXX!-  
-      .)S22222i    .i2S2S2o>;:S2S2o(  .<vSoSoSo2S(;     :nXXXXXZXXX(     
-       .-~~~~-        --- .   - -        --~~~--           --^^~~-       
-                                  .                                      
-                                                                         
-
- ... curious about our source code? We are hiring! mailto:<recruiting@tass.be>
+         .
+       .vS.
+     <aoSo.
+    .XoS22.
+    .S2S22.             ._...              ......            ..._.
+   :=|2S2X2|=++;      <vSX2XX2z+          |vSSSXSSs>.      :iXXZUZXXe=
+   )2SS2SS2S2S2I    =oS2S2S2S2X22;.    _vuXS22S2S2S22i  ._wZZXZZZXZZXZX=
+   )22S2S2S2S2Sl    |S2S2S22S2SSSXc:  .S2SS2S2S22S2SS= .]#XZZZXZXZZZZZZ:
+   )oSS2SS2S2Sol     |2}!"""!32S22S(. uS2S2Se**12oS2e  ]dXZZXX2?YYXXXZ*
+   .:2S2So:..-.      .      :]S2S2e;=X2SS2o     .)oc  ]XZZXZ(     =nX:
+    .S2S22.          ___s_i,.)oS2So(;2SS2So,       `  3XZZZZc,      -
+    .S2SSo.        =oXXXSSS2XoS2S2o( XS2S2XSos;.      ]ZZZZXXXX|=
+    .S2S22.      .)S2S2S22S2S2S2S2o( "X2SS2S2S2Sus,,  +3XZZZZZXZZoos_
+    .S2S22.     .]2S2SS22S222S2SS2o(  ]S22S2S2S222So   :3XXZZZZZZZZXXv
+    .S2S22.     =u2SS2e"~---"{2S2So(   -"12S2S2SSS2Su.   "?SXXXZXZZZZXo
+    .S2SSo.     )SS22z;      :S2S2o(       ={vS2S2S22v      .<vXZZZZZZZ;
+    .S2S2S:     ]oSS2c;      =22S2o(          -"S2SS2n          ~4XXZXZ(
+    .2S2S2i     )2S2S2[.    .)XS2So(  <;.      .2S2S2o :<.       ]XZZZX(
+     nX2S2S,,_s_=3oSS2SoaasuXXS2S2o( .oXoasi_aioSSS22l.]dZoaas_aadXZZXZ'
+     vS2SSSXXX2; )S2S2S2SoS2S2S2S2o( iS2S222XSoSS22So.)nXZZXXXZZXXZZXZo
+     x32S22S2Sn  -+S2S2S2S2So22S2So( 12S2SS2S2SS22S}- )SXXZZZZZZZZZXX!-
+      .)S22222i    .i2S2S2o>;:S2S2o(  .<vSoSoSo2S(;     :nXXXXXZXXX(
+       .-~~~~-        --- .   - -        --~~~--           --^^~~-
+                                  .
 
 
-*/
+   ... curious about our source code? We are hiring! mailto:<recruiting@tass.be>
+
+
+ */
 
 void pico_stack_tick(void)
 {
-    static int score[PROTO_DEF_NR] = {PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE};
-    static int index[PROTO_DEF_NR] = {0,0,0,0,0,0};
+    static int score[PROTO_DEF_NR] = {
+        PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE, PROTO_DEF_SCORE
+    };
+    static int index[PROTO_DEF_NR] = {
+        0, 0, 0, 0, 0, 0
+    };
     static int avg[PROTO_DEF_NR][PROTO_DEF_AVG_NR];
-    static int ret[PROTO_DEF_NR] = {0};
+    static int ret[PROTO_DEF_NR] = {
+        0
+    };
 
     pico_check_timers();
 
-    //dbg("LOOP_SCORES> %3d - %3d - %3d - %3d - %3d - %3d - %3d - %3d - %3d - %3d - %3d\n",score[0],score[1],score[2],score[3],score[4],score[5],score[6],score[7],score[8],score[9],score[10]);
+    /* dbg("LOOP_SCORES> %3d - %3d - %3d - %3d - %3d - %3d - %3d - %3d - %3d - %3d - %3d\n",score[0],score[1],score[2],score[3],score[4],score[5],score[6],score[7],score[8],score[9],score[10]); */
 
-    //score = pico_protocols_loop(100);
+    /* score = pico_protocols_loop(100); */
 
-    ret[0] = pico_devices_loop(score[0],PICO_LOOP_DIR_IN);
+    ret[0] = pico_devices_loop(score[0], PICO_LOOP_DIR_IN);
     pico_rand_feed((uint32_t)ret[0]);
 
     ret[1] = pico_protocol_datalink_loop(score[1], PICO_LOOP_DIR_IN);
@@ -670,7 +709,7 @@
     ret[5] = score[5];
 #if defined (PICO_SUPPORT_IPV4) || defined (PICO_SUPPORT_IPV6)
 #if defined (PICO_SUPPORT_TCP) || defined (PICO_SUPPORT_UDP)
-    ret[5] = pico_sockets_loop(score[5]); // swapped
+    ret[5] = pico_sockets_loop(score[5]); /* swapped */
     pico_rand_feed((uint32_t)ret[5]);
 #endif
 #endif
@@ -691,77 +730,79 @@
     ret[9] = pico_protocol_datalink_loop(score[9], PICO_LOOP_DIR_OUT);
     pico_rand_feed((uint32_t)ret[9]);
 
-    ret[10] = pico_devices_loop(score[10],PICO_LOOP_DIR_OUT);
+    ret[10] = pico_devices_loop(score[10], PICO_LOOP_DIR_OUT);
     pico_rand_feed((uint32_t)ret[10]);
 
     /* calculate new loop scores for next iteration */
-    calc_score(score, index,(int (*)[]) avg, ret);
+    calc_score(score, index, (int (*)[])avg, ret);
 }
 
 void pico_stack_loop(void)
 {
-  while(1) {
-    pico_stack_tick();
-    PICO_IDLE();
-  }
+    while(1) {
+        pico_stack_tick();
+        PICO_IDLE();
+    }
 }
 
 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;
-  if (!t) {
-    pico_err = PICO_ERR_ENOMEM;
-    return NULL;
-  }
-  tref.expire = PICO_TIME_MS() + expire;
-  t->arg = arg;
-  t->timer = timer;
-  tref.tmr = t;
-  heap_insert(Timers, &tref);
-  if (Timers->n > PICO_MAX_TIMERS) {
-    dbg("Warning: I have %d timers\n", Timers->n);
-  }
-  return t;
+    struct pico_timer *t = pico_zalloc(sizeof(struct pico_timer));
+    struct pico_timer_ref tref;
+    if (!t) {
+        pico_err = PICO_ERR_ENOMEM;
+        return NULL;
+    }
+
+    tref.expire = PICO_TIME_MS() + expire;
+    t->arg = arg;
+    t->timer = timer;
+    tref.tmr = t;
+    heap_insert(Timers, &tref);
+    if (Timers->n > PICO_MAX_TIMERS) {
+        dbg("Warning: I have %d timers\n", Timers->n);
+    }
+
+    return t;
 }
 
 void pico_stack_init(void)
 {
 
 #ifdef PICO_SUPPORT_IPV4
-  pico_protocol_init(&pico_proto_ipv4);
+    pico_protocol_init(&pico_proto_ipv4);
 #endif
 
 #ifdef PICO_SUPPORT_IPV6
-  pico_protocol_init(&pico_proto_ipv6);
+    pico_protocol_init(&pico_proto_ipv6);
 #endif
 
 #ifdef PICO_SUPPORT_ICMP4
-  pico_protocol_init(&pico_proto_icmp4);
+    pico_protocol_init(&pico_proto_icmp4);
 #endif
 
 #ifdef PICO_SUPPORT_IGMP
-  pico_protocol_init(&pico_proto_igmp);
+    pico_protocol_init(&pico_proto_igmp);
 #endif
 
 #ifdef PICO_SUPPORT_UDP
-  pico_protocol_init(&pico_proto_udp);
+    pico_protocol_init(&pico_proto_udp);
 #endif
 
 #ifdef PICO_SUPPORT_TCP
-  pico_protocol_init(&pico_proto_tcp);
+    pico_protocol_init(&pico_proto_tcp);
 #endif
 
 #ifdef PICO_SUPPORT_DNS_CLIENT
-  pico_dns_client_init();
+    pico_dns_client_init();
 #endif
 
-  pico_rand_feed(123456);
+    pico_rand_feed(123456);
 
-  /* Initialize timer heap */
-  Timers = heap_init();
-  pico_stack_tick();
-  pico_stack_tick();
-  pico_stack_tick();
+    /* Initialize timer heap */
+    Timers = heap_init();
+    pico_stack_tick();
+    pico_stack_tick();
+    pico_stack_tick();
 }
 
--- a/stack/pico_tree.c	Wed Dec 11 07:20:17 2013 +0000
+++ b/stack/pico_tree.c	Mon Dec 16 11:25:54 2013 +0100
@@ -1,22 +1,22 @@
 /*********************************************************************
-PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
-See LICENSE and COPYING for usage.
+   PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
+   See LICENSE and COPYING for usage.
 
-Author: Andrei Carp <andrei.carp@tass.be>
-*********************************************************************/
+   Author: Andrei Carp <andrei.carp@tass.be>
+ *********************************************************************/
 
 #include "pico_tree.h"
 #include "pico_config.h"
 #include "pico_protocol.h"
 
-#define RED 	0
+#define RED     0
 #define BLACK 1
 
-// By default the null leafs are black
+/* By default the null leafs are black */
 struct pico_tree_node LEAF = {
-  NULL, // key
-  &LEAF, &LEAF, &LEAF, // parent, left,right
-  BLACK, // color
+    NULL, /* key */
+    &LEAF, &LEAF, &LEAF, /* parent, left,right */
+    BLACK, /* color */
 };
 
 #define IS_LEAF(x) (x == &LEAF)
@@ -32,390 +32,390 @@
 /*
  * Local Functions
  */
-static struct pico_tree_node * create_node(struct pico_tree * tree,void *key);
-static void rotateToLeft(struct pico_tree* tree, struct pico_tree_node* node);
-static void rotateToRight(struct pico_tree* root, struct pico_tree_node* node);
-static void fix_insert_collisions(struct pico_tree* tree, struct pico_tree_node* node);
-static void fix_delete_collisions(struct pico_tree* tree, struct pico_tree_node * node);
-static void switchNodes(struct pico_tree* tree, struct pico_tree_node* nodeA, struct pico_tree_node* nodeB);
+static struct pico_tree_node *create_node(struct pico_tree *tree, void *key);
+static void rotateToLeft(struct pico_tree*tree, struct pico_tree_node*node);
+static void rotateToRight(struct pico_tree*root, struct pico_tree_node*node);
+static void fix_insert_collisions(struct pico_tree*tree, struct pico_tree_node*node);
+static void fix_delete_collisions(struct pico_tree*tree, struct pico_tree_node *node);
+static void switchNodes(struct pico_tree*tree, struct pico_tree_node*nodeA, struct pico_tree_node*nodeB);
 
 /*
  * Exported functions
  */
 
-struct pico_tree_node *pico_tree_firstNode(struct pico_tree_node * node)
+struct pico_tree_node *pico_tree_firstNode(struct pico_tree_node *node)
 {
-	while(IS_NOT_LEAF(node->leftChild))
-		node = node->leftChild;
-	return node;
+    while(IS_NOT_LEAF(node->leftChild))
+        node = node->leftChild;
+    return node;
 }
 
-struct pico_tree_node * pico_tree_lastNode(struct pico_tree_node * node)
+struct pico_tree_node *pico_tree_lastNode(struct pico_tree_node *node)
 {
-	while(IS_NOT_LEAF(node->rightChild))
-		node = node->rightChild;
-	return node;
+    while(IS_NOT_LEAF(node->rightChild))
+        node = node->rightChild;
+    return node;
 }
 
-struct pico_tree_node * pico_tree_next(struct pico_tree_node * node)
+struct pico_tree_node *pico_tree_next(struct pico_tree_node *node)
 {
-	if(IS_NOT_LEAF(node->rightChild))
-	{
-		node = node->rightChild;
-		while(IS_NOT_LEAF(node->leftChild))
-			node = node->leftChild;
-	}
-	else
-	{
-		if (IS_NOT_LEAF(node->parent) &&  AM_I_LEFT_CHILD(node))
-		  		node = node->parent;
-		else {
-			while (IS_NOT_LEAF(node->parent) &&	AM_I_RIGHT_CHILD(node))
-				node = node->parent;
+    if(IS_NOT_LEAF(node->rightChild))
+    {
+        node = node->rightChild;
+        while(IS_NOT_LEAF(node->leftChild))
+            node = node->leftChild;
+    }
+    else
+    {
+        if (IS_NOT_LEAF(node->parent) &&  AM_I_LEFT_CHILD(node))
+            node = node->parent;
+        else {
+            while (IS_NOT_LEAF(node->parent) && AM_I_RIGHT_CHILD(node))
+                node = node->parent;
+            node = node->parent;
+        }
+    }
 
-			node = node->parent;
-		}
-	}
-	return node;
+    return node;
 }
 
-struct pico_tree_node * pico_tree_prev(struct pico_tree_node * node)
+struct pico_tree_node *pico_tree_prev(struct pico_tree_node *node)
 {
-	if (IS_NOT_LEAF(node->leftChild)) {
-  	node = node->leftChild;
-  	while (IS_NOT_LEAF(node->rightChild))
-  		node = node->rightChild;
-  } else {
-  	if (IS_NOT_LEAF(node->parent) && AM_I_RIGHT_CHILD(node))
-  		node = node->parent;
-  	else {
-  		while (IS_NOT_LEAF(node) &&	AM_I_LEFT_CHILD(node))
-  			node = node->parent;
+    if (IS_NOT_LEAF(node->leftChild)) {
+        node = node->leftChild;
+        while (IS_NOT_LEAF(node->rightChild))
+            node = node->rightChild;
+    } else {
+        if (IS_NOT_LEAF(node->parent) && AM_I_RIGHT_CHILD(node))
+            node = node->parent;
+        else {
+            while (IS_NOT_LEAF(node) && AM_I_LEFT_CHILD(node))
+                node = node->parent;
+            node = node->parent;
+        }
+    }
 
-  		node = node->parent;
-  	}
-  }
-	return node;
+    return node;
 }
 
-void * pico_tree_insert(struct pico_tree* tree, void * key){
+void *pico_tree_insert(struct pico_tree*tree, void *key)
+{
 
-	struct pico_tree_node * last_node = INIT_LEAF;
-  struct pico_tree_node * temp = tree->root;
-  struct pico_tree_node * insert;
-  void * LocalKey;
-  int result = 0;
+    struct pico_tree_node *last_node = INIT_LEAF;
+    struct pico_tree_node *temp = tree->root;
+    struct pico_tree_node *insert;
+    void *LocalKey;
+    int result = 0;
 
-	LocalKey = (IS_NOT_LEAF(tree->root) ? pico_tree_findKey(tree,key) : NULL);
-	// if node already in, bail out
-  if(LocalKey)
-  	return LocalKey;
-  else
-  {
-  	insert = create_node(tree,key);
-  	if(!insert)
-  	{
-  		pico_err = PICO_ERR_ENOMEM;
-  		// to let the user know that it couldn't insert
-  		return (void *)&LEAF;
-  	}
-  }
+    LocalKey = (IS_NOT_LEAF(tree->root) ? pico_tree_findKey(tree, key) : NULL);
+    /* if node already in, bail out */
+    if(LocalKey)
+        return LocalKey;
+    else
+    {
+        insert = create_node(tree, key);
+        if(!insert)
+        {
+            pico_err = PICO_ERR_ENOMEM;
+            /* to let the user know that it couldn't insert */
+            return (void *)&LEAF;
+        }
+    }
 
-  // search for the place to insert the new node
-  while(IS_NOT_LEAF(temp))
-  {
-		last_node = temp;
-		result = tree->compare(insert->keyValue,temp->keyValue);
+    /* search for the place to insert the new node */
+    while(IS_NOT_LEAF(temp))
+    {
+        last_node = temp;
+        result = tree->compare(insert->keyValue, temp->keyValue);
 
-		temp = (result < 0 ? temp->leftChild : temp->rightChild);
-  }
-
-  // make the needed connections
-  insert->parent = last_node;
+        temp = (result < 0 ? temp->leftChild : temp->rightChild);
+    }
+    /* make the needed connections */
+    insert->parent = last_node;
 
-  if(IS_LEAF(last_node))
-    tree->root = insert;
-  else{
-  	result = tree->compare(insert->keyValue,last_node->keyValue);
-    if(result < 0)
-      last_node->leftChild = insert;
-    else
-      last_node->rightChild = insert;
-  }
+    if(IS_LEAF(last_node))
+        tree->root = insert;
+    else{
+        result = tree->compare(insert->keyValue, last_node->keyValue);
+        if(result < 0)
+            last_node->leftChild = insert;
+        else
+            last_node->rightChild = insert;
+    }
 
-  // fix colour issues
-  fix_insert_collisions(tree, insert);
+    /* fix colour issues */
+    fix_insert_collisions(tree, insert);
 
-	return NULL;
+    return NULL;
 }
 
-struct pico_tree_node * pico_tree_findNode(struct pico_tree * tree, void * key)
+struct pico_tree_node *pico_tree_findNode(struct pico_tree *tree, void *key)
 {
-		struct pico_tree_node *found;
+    struct pico_tree_node *found;
 
-		found = tree->root;
+    found = tree->root;
 
-	  while(IS_NOT_LEAF(found))
-	  {
-	  	int result;
-	  	result = tree->compare(found->keyValue, key);
-	  	if(result == 0)
-	  	{
-	  	   return found;
-	  	}
-	  	else if(result < 0)
-	       found = found->rightChild;
-	     else
-	       found = found->leftChild;
-	   }
-
-
-
-	  return NULL;
+    while(IS_NOT_LEAF(found))
+    {
+        int result;
+        result = tree->compare(found->keyValue, key);
+        if(result == 0)
+        {
+            return found;
+        }
+        else if(result < 0)
+            found = found->rightChild;
+        else
+            found = found->leftChild;
+    }
+    return NULL;
 }
 
-void * pico_tree_findKey(struct pico_tree * tree, void * key)
+void *pico_tree_findKey(struct pico_tree *tree, void *key)
 {
-  struct pico_tree_node *found;
+    struct pico_tree_node *found;
 
 
-  found = tree->root;
+    found = tree->root;
 
-  while(IS_NOT_LEAF(found))
-  {
-  	int result;
+    while(IS_NOT_LEAF(found))
+    {
+        int result;
 
-  	result = tree->compare(found->keyValue, key);
-  	if(result == 0)
-  	   return found->keyValue;
-  	else if(result < 0)
-       found = found->rightChild;
-     else
-       found = found->leftChild;
+        result = tree->compare(found->keyValue, key);
+        if(result == 0)
+            return found->keyValue;
+        else if(result < 0)
+            found = found->rightChild;
+        else
+            found = found->leftChild;
 
-   }
-
-  return NULL;
+    }
+    return NULL;
 }
 
-void * pico_tree_first(struct pico_tree * tree)
+void *pico_tree_first(struct pico_tree *tree)
 {
-	return pico_tree_firstNode(tree->root)->keyValue;
+    return pico_tree_firstNode(tree->root)->keyValue;
 }
 
-void * pico_tree_last(struct pico_tree * tree)
+void *pico_tree_last(struct pico_tree *tree)
 {
-	return pico_tree_lastNode(tree->root)->keyValue;
+    return pico_tree_lastNode(tree->root)->keyValue;
 }
 
-void * pico_tree_delete(struct pico_tree * tree, void * key){
+void *pico_tree_delete(struct pico_tree *tree, void *key)
+{
 
-	uint8_t nodeColor; // keeps the color of the node to be deleted
-  void * lkey; // keeps a copy of the key which will be removed
-	struct pico_tree_node * delete; // keeps a copy of the node to be extracted
-	struct pico_tree_node * temp; // temporary
-	struct pico_tree_node * ltemp; // used for switching nodes, as a copy
+    uint8_t nodeColor; /* keeps the color of the node to be deleted */
+    void *lkey; /* keeps a copy of the key which will be removed */
+    struct pico_tree_node *delete;  /* keeps a copy of the node to be extracted */
+    struct pico_tree_node *temp;  /* temporary */
+    struct pico_tree_node *ltemp;  /* used for switching nodes, as a copy */
 
-	delete = pico_tree_findNode(tree, key);
-	ltemp = delete;
+    delete = pico_tree_findNode(tree, key);
+    ltemp = delete;
 
-  // this key isn't in the tree, bail out
-  if(!delete)
-    return NULL;
+    /* this key isn't in the tree, bail out */
+    if(!delete)
+        return NULL;
 
-  lkey = delete->keyValue;
-  nodeColor = delete->color;
+    lkey = delete->keyValue;
+    nodeColor = delete->color;
 
-  if(IS_LEAF(delete->leftChild))
-  {
-    temp = ltemp->rightChild;
-    switchNodes(tree, ltemp, ltemp->rightChild);
-  }
-  else
+    if(IS_LEAF(delete->leftChild))
+    {
+        temp = ltemp->rightChild;
+        switchNodes(tree, ltemp, ltemp->rightChild);
+    }
+    else
     if(IS_LEAF(delete->rightChild))
     {
-    	struct pico_tree_node * _ltemp = delete;
-      temp = _ltemp->leftChild;
-      switchNodes(tree, _ltemp, _ltemp->leftChild);
+        struct pico_tree_node *_ltemp = delete;
+        temp = _ltemp->leftChild;
+        switchNodes(tree, _ltemp, _ltemp->leftChild);
     }
     else{
-    	struct pico_tree_node * min;
-    	min = pico_tree_firstNode(delete->rightChild);
-      nodeColor = min->color;
+        struct pico_tree_node *min;
+        min = pico_tree_firstNode(delete->rightChild);
+        nodeColor = min->color;
 
-       temp = min->rightChild;
-       if(min->parent == ltemp && IS_NOT_LEAF(temp))
- 	 temp->parent = min;
-       else{
-      	 switchNodes(tree, min, min->rightChild);
-      	 min->rightChild = ltemp->rightChild;
-      	 if(IS_NOT_LEAF(min->rightChild)) min->rightChild->parent = min;
-       }
-       switchNodes(tree, ltemp, min);
-       min->leftChild = ltemp->leftChild;
-       if(IS_NOT_LEAF(min->leftChild)) min->leftChild->parent = min;
-       min->color = ltemp->color;
+        temp = min->rightChild;
+        if(min->parent == ltemp && IS_NOT_LEAF(temp))
+            temp->parent = min;
+        else{
+            switchNodes(tree, min, min->rightChild);
+            min->rightChild = ltemp->rightChild;
+            if(IS_NOT_LEAF(min->rightChild)) min->rightChild->parent = min;
+        }
+
+        switchNodes(tree, ltemp, min);
+        min->leftChild = ltemp->leftChild;
+        if(IS_NOT_LEAF(min->leftChild)) min->leftChild->parent = min;
+
+        min->color = ltemp->color;
     }
 
-  // deleted node is black, this will mess up the black path property
-  if(nodeColor == BLACK)
-    fix_delete_collisions(tree, temp);
+    /* deleted node is black, this will mess up the black path property */
+    if(nodeColor == BLACK)
+        fix_delete_collisions(tree, temp);
 
-  pico_free(delete);
+    pico_free(delete);
 
-  return lkey;
+    return lkey;
 }
 
-int pico_tree_empty(struct pico_tree * tree)
+int pico_tree_empty(struct pico_tree *tree)
 {
-	return (!tree->root || IS_LEAF(tree->root));
+    return (!tree->root || IS_LEAF(tree->root));
 }
 
 /*
  * Private functions
  */
-static void rotateToLeft(struct pico_tree* tree, struct pico_tree_node* node)
+static void rotateToLeft(struct pico_tree*tree, struct pico_tree_node*node)
 {
-  struct pico_tree_node* temp;
+    struct pico_tree_node*temp;
 
-  temp = node->rightChild;
+    temp = node->rightChild;
 
-  if(temp == &LEAF) return;
+    if(temp == &LEAF) return;
 
-  node->rightChild = temp->leftChild;
+    node->rightChild = temp->leftChild;
 
-  if(IS_NOT_LEAF(temp->leftChild))
-    temp->leftChild->parent = node;
+    if(IS_NOT_LEAF(temp->leftChild))
+        temp->leftChild->parent = node;
 
-  temp->parent = node->parent;
+    temp->parent = node->parent;
 
-  if(IS_LEAF(node->parent))
-    tree->root = temp;
-  else
+    if(IS_LEAF(node->parent))
+        tree->root = temp;
+    else
     if(node == node->parent->leftChild)
-    	node->parent->leftChild = temp;
+        node->parent->leftChild = temp;
     else
-    	node->parent->rightChild = temp;
+        node->parent->rightChild = temp;
 
-  temp->leftChild = node;
-  node->parent = temp;
+    temp->leftChild = node;
+    node->parent = temp;
 }
 
 
-static void rotateToRight(struct pico_tree * tree, struct pico_tree_node * node)
+static void rotateToRight(struct pico_tree *tree, struct pico_tree_node *node)
 {
-  struct pico_tree_node* temp;
+    struct pico_tree_node*temp;
 
-  temp = node->leftChild;
-  node->leftChild = temp->rightChild;
+    temp = node->leftChild;
+    node->leftChild = temp->rightChild;
 
-  if(temp == &LEAF) return;
+    if(temp == &LEAF) return;
 
-  if(IS_NOT_LEAF(temp->rightChild))
-    temp->rightChild->parent = node;
+    if(IS_NOT_LEAF(temp->rightChild))
+        temp->rightChild->parent = node;
 
-  temp->parent = node->parent;
+    temp->parent = node->parent;
 
-  if(IS_LEAF(node->parent))
-    tree->root = temp;
-  else
+    if(IS_LEAF(node->parent))
+        tree->root = temp;
+    else
     if(node == node->parent->rightChild)
-      node->parent->rightChild = temp;
+        node->parent->rightChild = temp;
     else
-      node->parent->leftChild = temp;
+        node->parent->leftChild = temp;
 
-  temp->rightChild = node;
-  node->parent = temp;
-  return;
+    temp->rightChild = node;
+    node->parent = temp;
+    return;
 }
 
-static struct pico_tree_node * create_node(struct pico_tree * tree, void* key)
+static struct pico_tree_node *create_node(struct pico_tree *tree, void*key)
 {
-  struct pico_tree_node *temp;
-  IGNORE_PARAMETER(tree);
-  temp = (struct pico_tree_node *)pico_zalloc(sizeof(struct pico_tree_node));
+    struct pico_tree_node *temp;
+    IGNORE_PARAMETER(tree);
+    temp = (struct pico_tree_node *)pico_zalloc(sizeof(struct pico_tree_node));
 
-  if(!temp)
-  	return NULL;
+    if(!temp)
+        return NULL;
 
-  temp->keyValue = key;
-  temp->parent = &LEAF;
-  temp->leftChild = &LEAF;
-  temp->rightChild = &LEAF;
-  // by default every new node is red
-  temp->color = RED;
-  return temp;
+    temp->keyValue = key;
+    temp->parent = &LEAF;
+    temp->leftChild = &LEAF;
+    temp->rightChild = &LEAF;
+    /* by default every new node is red */
+    temp->color = RED;
+    return temp;
 }
 
 /*
  * This function fixes the possible collisions in the tree.
  * Eg. if a node is red his children must be black !
  */
-static void fix_insert_collisions(struct pico_tree* tree, struct pico_tree_node* node)
+static void fix_insert_collisions(struct pico_tree*tree, struct pico_tree_node*node)
 {
-  struct pico_tree_node* temp;
+    struct pico_tree_node*temp;
+
+    while(node->parent->color == RED && IS_NOT_LEAF(GRANPA(node)))
+    {
+        if(AM_I_RIGHT_CHILD(node->parent))
+        {
+            temp = GRANPA(node)->leftChild;
+            if(temp->color == RED) {
+                node->parent->color = BLACK;
+                temp->color = BLACK;
+                GRANPA(node)->color = RED;
+                node = GRANPA(node);
+            }
+            else if(temp->color == BLACK) {
+                if(node == node->parent->leftChild) {
+                    node = node->parent;
+                    rotateToRight(tree, node);
+                }
 
-  while(node->parent->color == RED && IS_NOT_LEAF(GRANPA(node)) )
-  {
-  	if(AM_I_RIGHT_CHILD(node->parent))
-	 {
-			temp = GRANPA(node)->leftChild;
-			if(temp->color == RED){
-				node->parent->color = BLACK;
-				temp->color = BLACK;
-				GRANPA(node)->color = RED;
-				node = GRANPA(node);
-			}
-			else if(temp->color == BLACK){
-				if(node == node->parent->leftChild){
-				node = node->parent;
-				rotateToRight(tree, node);
-			}
-			node->parent->color = BLACK;
-			GRANPA(node)->color = RED;
-			rotateToLeft(tree, GRANPA(node));
-			}
-		}
-  	else if(AM_I_LEFT_CHILD(node->parent))
-    {
-      temp = GRANPA(node)->rightChild;
-      if(temp->color == RED){
-				node->parent->color = BLACK;
-				temp->color = BLACK;
-				GRANPA(node)->color = RED;
-				node = GRANPA(node);
-      }
-      else if(temp->color == BLACK){
-				if(AM_I_RIGHT_CHILD(node)){
-					node = node->parent;
-					rotateToLeft(tree, node);
-				}
-				node->parent->color = BLACK;
-				GRANPA(node)->color = RED;
-				rotateToRight(tree, GRANPA(node));
-  		}
+                node->parent->color = BLACK;
+                GRANPA(node)->color = RED;
+                rotateToLeft(tree, GRANPA(node));
+            }
+        }
+        else if(AM_I_LEFT_CHILD(node->parent))
+        {
+            temp = GRANPA(node)->rightChild;
+            if(temp->color == RED) {
+                node->parent->color = BLACK;
+                temp->color = BLACK;
+                GRANPA(node)->color = RED;
+                node = GRANPA(node);
+            }
+            else if(temp->color == BLACK) {
+                if(AM_I_RIGHT_CHILD(node)) {
+                    node = node->parent;
+                    rotateToLeft(tree, node);
+                }
+
+                node->parent->color = BLACK;
+                GRANPA(node)->color = RED;
+                rotateToRight(tree, GRANPA(node));
+            }
+        }
     }
-  }
-
-  // make sure that the root of the tree stays black
-  tree->root->color = BLACK;
+    /* make sure that the root of the tree stays black */
+    tree->root->color = BLACK;
 }
 
-static void switchNodes(struct pico_tree* tree, struct pico_tree_node* nodeA, struct pico_tree_node* nodeB)
+static void switchNodes(struct pico_tree*tree, struct pico_tree_node*nodeA, struct pico_tree_node*nodeB)
 {
 
-	if(IS_LEAF(nodeA->parent))
-    tree->root = nodeB;
-  else
-  if(IS_NOT_LEAF(nodeA))
-  {	
-    if(AM_I_LEFT_CHILD(nodeA))
-      nodeA->parent->leftChild = nodeB;
+    if(IS_LEAF(nodeA->parent))
+        tree->root = nodeB;
     else
-      nodeA->parent->rightChild = nodeB;
-   }
+    if(IS_NOT_LEAF(nodeA))
+    {
+        if(AM_I_LEFT_CHILD(nodeA))
+            nodeA->parent->leftChild = nodeB;
+        else
+            nodeA->parent->rightChild = nodeB;
+    }
 
-  if(IS_NOT_LEAF(nodeB))  nodeB->parent = nodeA->parent;
+    if(IS_NOT_LEAF(nodeB)) nodeB->parent = nodeA->parent;
 
 }
 
@@ -424,73 +424,76 @@
  * Eg. if a node is red his children must be black !
  * In this case the function fixes the constant black path property.
  */
-static void fix_delete_collisions(struct pico_tree* tree, struct pico_tree_node * node)
+static void fix_delete_collisions(struct pico_tree*tree, struct pico_tree_node *node)
 {
-  struct pico_tree_node* temp;
+    struct pico_tree_node*temp;
+
+    while( node != tree->root && node->color == BLACK && IS_NOT_LEAF(node))
+    {
+        if(AM_I_LEFT_CHILD(node)) {
 
-  while( node != tree->root && node->color == BLACK && IS_NOT_LEAF(node))
-  {
-  	if(AM_I_LEFT_CHILD(node)){
+            temp = node->parent->rightChild;
+            if(temp->color == RED)
+            {
+                temp->color = BLACK;
+                node->parent->color = RED;
+                rotateToLeft(tree, node->parent);
+                temp = node->parent->rightChild;
+            }
+
+            if(temp->leftChild->color == BLACK && temp->rightChild->color == BLACK)
+            {
+                temp->color = RED;
+                node = node->parent;
+            }
+            else
+            {
+                if(temp->rightChild->color == BLACK)
+                {
+                    temp->leftChild->color = BLACK;
+                    temp->color = RED;
+                    rotateToRight(tree, temp);
+                    temp = temp->parent->rightChild;
+                }
 
-      temp = node->parent->rightChild;
-      if(temp->color == RED)
-      {
-				temp->color = BLACK;
-				node->parent->color = RED;
-				rotateToLeft(tree, node->parent);
-				temp = node->parent->rightChild;
-      }
-      if(temp->leftChild->color == BLACK && temp->rightChild->color == BLACK)
-      {
-				temp->color = RED;
-				node = node->parent;
-      }
-      else
-      {
-				if(temp->rightChild->color == BLACK)
-				{
-					temp->leftChild->color = BLACK;
-					temp->color = RED;
-					rotateToRight(tree, temp);
-					temp = temp->parent->rightChild;
-				}
-				temp->color = node->parent->color;
-				node->parent->color = BLACK;
-				temp->rightChild->color = BLACK;
-				rotateToLeft(tree, node->parent);
-				node = tree->root;
-      }
+                temp->color = node->parent->color;
+                node->parent->color = BLACK;
+                temp->rightChild->color = BLACK;
+                rotateToLeft(tree, node->parent);
+                node = tree->root;
+            }
+        }
+        else{
+            temp = node->parent->leftChild;
+            if(temp->color == RED)
+            {
+                temp->color = BLACK;
+                node->parent->color = RED;
+                rotateToRight(tree, node->parent);
+                temp = node->parent->leftChild;
+            }
+
+            if(temp->rightChild->color == BLACK && temp->leftChild->color == BLACK)
+            {
+                temp->color = RED;
+                node = node->parent;
+            }
+            else{
+                if(temp->leftChild->color == BLACK)
+                {
+                    temp->rightChild->color = BLACK;
+                    temp->color = RED;
+                    rotateToLeft(tree, temp);
+                    temp = temp->parent->leftChild;
+                }
+
+                temp->color = node->parent->color;
+                node->parent->color = BLACK;
+                temp->leftChild->color = BLACK;
+                rotateToRight(tree, node->parent);
+                node = tree->root;
+            }
+        }
     }
-    else{
-      temp = node->parent->leftChild;
-      if(temp->color == RED)
-      {
-				temp->color = BLACK;
-				node->parent->color = RED;
-				rotateToRight(tree, node->parent);
-				temp = node->parent->leftChild;
-      }
-      if(temp->rightChild->color == BLACK && temp->leftChild->color == BLACK)
-      {
-				temp->color = RED;
-				node = node->parent;
-      }
-      else{
-				if(temp->leftChild->color == BLACK)
-				{
-					temp->rightChild->color = BLACK;
-					temp->color = RED;
-					rotateToLeft(tree, temp);
-					temp = temp->parent->leftChild;
-				}
-			temp->color = node->parent->color;
-			node->parent->color = BLACK;
-			temp->leftChild->color = BLACK;
-			rotateToRight(tree, node->parent);
-			node = tree->root;
-      }
-    }
-  }
-
-  node->color = BLACK;
+    node->color = BLACK;
 }