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

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

PicoTCP. Copyright (c) 2013 TASS Belgium NV.

Released under the GNU General Public License, version 2.

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

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

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

Development steps:

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

Demo application (measuring TCP sender performance):

Import programlpc1768-picotcp-demo

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

Revision:
66:71a2ef45a035
Parent:
63:97f481e33cb2
Parent:
64:0225b609335e
--- a/stack/pico_socket.c	Thu Sep 19 12:38:53 2013 +0000
+++ b/stack/pico_socket.c	Thu Sep 19 13:06:00 2013 +0000
@@ -24,6 +24,11 @@
 #if defined (PICO_SUPPORT_TCP) || defined (PICO_SUPPORT_UDP)
 
 
+#define PROTO(s) ((s)->proto->proto_number)
+#define PICO_SOCKET4_MTU 1480 /* Ethernet MTU(1500) - IP header size(20) */
+#define PICO_SOCKET6_MTU 1460 /* Ethernet MTU(1500) - IP header size(40) */
+#define TCP_STATE(s) (s->state & PICO_SOCKET_STATE_TCP)
+
 #ifdef PICO_SUPPORT_MUTEX
 static void * Mutex = NULL;
 #define LOCK(x) {\
@@ -457,7 +462,7 @@
 
   /* IPv4 */
 #ifdef PICO_SUPPORT_NAT
-  if (pico_ipv4_nat_find(port,NULL, 0,proto) == 0) {
+  if (pico_ipv4_nat_find(port, NULL, 0, proto)) {
     dbg("In use by nat....\n");
     return 0;
   }
@@ -512,6 +517,28 @@
   return -1;
 }
 
+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;
+
+	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;
+}
+
 
 int pico_socket_add(struct pico_socket *s)
 {
@@ -558,9 +585,34 @@
   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)
+    {
+      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);
+    }
+  }
+  // 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(unsigned long now, void *arg)
 {
   struct pico_socket *s = (struct pico_socket *) arg;
+  IGNORE_PARAMETER(now);
+  socket_clean_queues(s);
   pico_free(s);
 }
 
@@ -915,7 +967,7 @@
   return 0;
 }
 
-int pico_socket_write(struct pico_socket *s, void *buf, int len)
+int pico_socket_write(struct pico_socket *s, const void *buf, int len)
 {
   if (!s || buf == NULL) {
     pico_err = PICO_ERR_EINVAL;
@@ -968,10 +1020,11 @@
 }
 
 
-int pico_socket_sendto(struct pico_socket *s, void *buf, int len, void *dst, uint16_t remote_port)
+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;
 #ifdef PICO_SUPPORT_IPV4
@@ -1007,6 +1060,7 @@
 
 #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;
@@ -1029,11 +1083,10 @@
       }
 #     endif
     }
-  }
-#endif
-
-#ifdef PICO_SUPPORT_IPV6
-  if (IS_SOCK_IPV6(s)) {
+    #endif
+  } 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;
@@ -1053,8 +1106,8 @@
       }
 #     endif
     }
+#endif
   }
-#endif
 
   if ((s->state & PICO_SOCKET_STATE_BOUND) == 0) {
     s->local_port = pico_socket_high_port(s->proto->proto_number);
@@ -1079,9 +1132,9 @@
 
   while (total_payload_written < len) {
     int transport_len = (len - total_payload_written) + header_offset; 
-    if (transport_len > PICO_SOCKET_MTU)
-      transport_len = PICO_SOCKET_MTU;
-#ifdef PICO_SUPPORT_IPFRAG
+    if (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 */
@@ -1101,9 +1154,9 @@
       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) > PICO_SOCKET_MTU)) {
+    #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);
@@ -1153,7 +1206,7 @@
   return total_payload_written;
 }
 
-int pico_socket_send(struct pico_socket *s, void *buf, int len)
+int pico_socket_send(struct pico_socket *s, const void *buf, int len)
 {
   if (!s || buf == NULL) {
     pico_err = PICO_ERR_EINVAL;
@@ -1270,7 +1323,7 @@
   return pico_socket_alter_state(s, PICO_SOCKET_STATE_BOUND, 0, 0);
 }
 
-int pico_socket_connect(struct pico_socket *s, void *remote_addr, uint16_t remote_port)
+int pico_socket_connect(struct pico_socket *s, const void *remote_addr, uint16_t remote_port)
 {
   int ret = -1;
   pico_err = PICO_ERR_EPROTONOSUPPORT;
@@ -1290,10 +1343,10 @@
   }
 
   if (is_sock_ipv6(s)) {
-    struct pico_ip6 *ip = (struct pico_ip6 *) remote_addr;
+    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)) {
-    struct pico_ip4 *local, *ip = (struct pico_ip4 *) remote_addr;
+    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) {
@@ -1989,7 +2042,7 @@
     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, PICO_SOCKET_STATE_BOUND, 0, 0);
+      pico_socket_alter_state(s, 0, PICO_SOCKET_STATE_BOUND, 0);
   }
 #endif
 #ifdef PICO_SUPPORT_TCP
@@ -2052,7 +2105,8 @@
 #else
 static inline int pico_transport_crc_check(struct pico_frame *f)
 {
-  return 1;
+	IGNORE_PARAMETER(f);
+	return 1;
 }
 #endif /* PICO_SUPPORT_CRC */
 
@@ -2150,8 +2204,8 @@
   start = sp_tcp;
 
   while (loop_score > SL_LOOP_MIN && sp_tcp != NULL) {
-    struct pico_tree_node * index;
-    pico_tree_foreach(index, &sp_tcp->socks){
+    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) {
@@ -2161,10 +2215,26 @@
         loop_score = 0;
         break;
       }
-    }
+
+      // checking socket activity
+      if((uint32_t)(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 ) )
+    	{
+		  pico_socket_del(s);
+		  index_tcp = pico_tree_firstNode(TCPTable.root);
+    	}
+        // if no activity, force the socket into closing state
+    	if( TCP_STATE(s) == PICO_SOCKET_STATE_TCP_ESTABLISHED )
+    	{
+    	  pico_socket_close(s);
+    	  s->timestamp = PICO_TIME_MS();
+    	}
+      }
+	}
 
     /* check if RB_FOREACH ended, if not, break to keep the cur sp_tcp */
-    if (s != NULL)
+    if ( (index && index->keyValue) || !index_tcp)
       break;
 
     index_tcp = pico_tree_next(index_tcp);