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:
149:5f4cb161cec3
Parent:
141:fb35b52d1c80
Child:
152:a3d286bf94e5
--- a/modules/pico_dhcp_server.c	Tue Mar 11 15:34:51 2014 +0100
+++ b/modules/pico_dhcp_server.c	Wed Apr 09 14:31:41 2014 +0200
@@ -28,8 +28,6 @@
 /* maximum size of a DHCP message */
 #define DHCP_SERVER_MAXMSGSIZE (PICO_IP_MTU - sizeof(struct pico_ipv4_hdr) - sizeof(struct pico_udp_hdr))
 
-#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,
@@ -44,8 +42,22 @@
     struct pico_dhcp_server_setting *dhcps;
     struct pico_ip4 ciaddr;
     struct pico_eth hwaddr;
+    uint8_t bcast;
 };
 
+static inline int ip_address_is_in_dhcp_range(struct pico_dhcp_server_negotiation *n, uint32_t x)
+{
+    uint32_t ip_hostendian = long_be(x);
+    if (ip_hostendian < long_be(n->dhcps->pool_start))
+        return 0;
+
+    if (ip_hostendian > long_be(n->dhcps->pool_end))
+        return 0;
+
+    return 1;
+}
+
+
 static void pico_dhcpd_wakeup(uint16_t ev, struct pico_socket *s);
 
 static int dhcp_settings_cmp(void *ka, void *kb)
@@ -54,7 +66,7 @@
     if (a->dev == b->dev)
         return 0;
 
-    return (a->dev < b->dev) ? -1 : 1;
+    return (a->dev < b->dev) ? (-1) : (1);
 }
 PICO_TREE_DECLARE(DHCPSettings, dhcp_settings_cmp);
 
@@ -64,7 +76,7 @@
     if (a->xid == b->xid)
         return 0;
 
-    return (a->xid < b->xid) ? -1 : 1;
+    return (a->xid < b->xid) ? (-1) : (1);
 }
 PICO_TREE_DECLARE(DHCPNegotiations, dhcp_negotiations_cmp);
 
@@ -89,7 +101,7 @@
         return NULL;
     }
 
-    dhcps = pico_zalloc(sizeof(struct pico_dhcp_server_setting));
+    dhcps = PICO_ZALLOC(sizeof(struct pico_dhcp_server_setting));
     if (!dhcps) {
         pico_err = PICO_ERR_ENOMEM;
         return NULL;
@@ -118,13 +130,13 @@
     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);
+        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);
+        PICO_FREE(dhcps);
         return NULL;
     }
 
@@ -157,7 +169,7 @@
     if (pico_dhcp_server_find_negotiation(hdr->xid))
         return NULL;
 
-    dhcpn = pico_zalloc(sizeof(struct pico_dhcp_server_negotiation));
+    dhcpn = PICO_ZALLOC(sizeof(struct pico_dhcp_server_negotiation));
     if (!dhcpn) {
         pico_err = PICO_ERR_ENOMEM;
         return NULL;
@@ -165,13 +177,14 @@
 
     dhcpn->xid = hdr->xid;
     dhcpn->state = PICO_DHCP_STATE_DISCOVER;
+    dhcpn->bcast = ((short_be(hdr->flags) & PICO_DHCP_FLAG_BROADCAST) != 0) ? (1) : (0);
     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);
+        PICO_FREE(dhcpn);
         return NULL;
     }
 
@@ -205,7 +218,10 @@
 
     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 = PICO_ZALLOC(sizeof(struct pico_dhcp_hdr) + (uint32_t)optlen);
+    if (!hdr) {
+        return;
+    }
 
     hdr->op = PICO_DHCP_OP_REPLY;
     hdr->htype = PICO_DHCP_HTYPE_ETH;
@@ -226,7 +242,13 @@
     offset += pico_dhcp_opt_dns(&hdr->options[offset], &dns);
     offset += pico_dhcp_opt_end(&hdr->options[offset]);
 
-    destination.addr = hdr->yiaddr;
+    if (dhcpn->bcast == 0)
+        destination.addr = hdr->yiaddr;
+    else {
+        hdr->flags |= short_be(PICO_DHCP_FLAG_BROADCAST);
+        destination.addr = broadcast.addr;
+    }
+
     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));
@@ -256,7 +278,7 @@
     if (!dhcpn)
         dhcpn = pico_dhcp_server_add_negotiation(dev, hdr);
 
-    if (!ip_inrange(dhcpn->ciaddr.addr))
+    if (!ip_address_is_in_dhcp_range(dhcpn, dhcpn->ciaddr.addr))
         return;
 
     do {
@@ -344,4 +366,21 @@
 
     return 0;
 }
+
+int pico_dhcp_server_destroy(struct pico_device *dev)
+{
+    struct pico_dhcp_server_setting *found, test = {
+        .dev = dev,
+    };
+    found = pico_tree_findKey(&DHCPSettings, &test);
+    if (!found) {
+        pico_err = PICO_ERR_ENOENT;
+        return -1;
+    }
+
+    pico_tree_delete(&DHCPSettings, found);
+    PICO_FREE(found);
+    return 0;
+}
+
 #endif /* PICO_SUPPORT_DHCP */