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.

Committer:
tass picotcp@tass.be
Date:
Wed Apr 09 14:31:41 2014 +0200
Revision:
149:5f4cb161cec3
Parent:
137:a1c8bfa9d691
Child:
152:a3d286bf94e5
Update from git masterbranch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TASS Belgium NV 131:4758606c9316 1 /*********************************************************************
TASS Belgium NV 131:4758606c9316 2 PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
TASS Belgium NV 131:4758606c9316 3 See LICENSE and COPYING for usage.
tass 73:dfb737147f6e 4
TASS Belgium NV 131:4758606c9316 5 *********************************************************************/
tass picotcp@tass.be 149:5f4cb161cec3 6 #ifndef INCLUDE_PICO_SOCKET
tass picotcp@tass.be 149:5f4cb161cec3 7 #define INCLUDE_PICO_SOCKET
tass 68:0847e35d08a6 8 #include "pico_queue.h"
tass 68:0847e35d08a6 9 #include "pico_addressing.h"
tass 68:0847e35d08a6 10 #include "pico_config.h"
tass 68:0847e35d08a6 11 #include "pico_protocol.h"
tass picotcp@tass.be 149:5f4cb161cec3 12 #include "pico_tree.h"
tass 68:0847e35d08a6 13
TASS Belgium NV 131:4758606c9316 14 #ifdef __linux__
tass picotcp@tass.be 137:a1c8bfa9d691 15 #define PICO_DEFAULT_SOCKETQ (16 * 1024) /* Linux host, so we want full throttle */
tass 73:dfb737147f6e 16 #else
TASS Belgium NV 131:4758606c9316 17 #define PICO_DEFAULT_SOCKETQ (4 * 1024) /* seems like an acceptable default for small embedded systems */
tass 73:dfb737147f6e 18 #endif
tass 68:0847e35d08a6 19
tass 68:0847e35d08a6 20 #define PICO_SHUT_RD 1
tass 68:0847e35d08a6 21 #define PICO_SHUT_WR 2
tass 68:0847e35d08a6 22 #define PICO_SHUT_RDWR 3
tass 68:0847e35d08a6 23
tass picotcp@tass.be 149:5f4cb161cec3 24 struct pico_sockport
tass picotcp@tass.be 149:5f4cb161cec3 25 {
tass picotcp@tass.be 149:5f4cb161cec3 26 struct pico_tree socks; /* how you make the connection ? */
tass picotcp@tass.be 149:5f4cb161cec3 27 uint16_t number;
tass picotcp@tass.be 149:5f4cb161cec3 28 uint16_t proto;
tass picotcp@tass.be 149:5f4cb161cec3 29 };
tass picotcp@tass.be 149:5f4cb161cec3 30
tass 68:0847e35d08a6 31
tass 68:0847e35d08a6 32 struct pico_socket {
TASS Belgium NV 131:4758606c9316 33 struct pico_protocol *proto;
TASS Belgium NV 131:4758606c9316 34 struct pico_protocol *net;
tass 68:0847e35d08a6 35
tass picotcp@tass.be 149:5f4cb161cec3 36 union pico_address local_addr;
tass picotcp@tass.be 149:5f4cb161cec3 37 union pico_address remote_addr;
tass 68:0847e35d08a6 38
TASS Belgium NV 131:4758606c9316 39 uint16_t local_port;
TASS Belgium NV 131:4758606c9316 40 uint16_t remote_port;
tass 68:0847e35d08a6 41
TASS Belgium NV 131:4758606c9316 42 struct pico_queue q_in;
TASS Belgium NV 131:4758606c9316 43 struct pico_queue q_out;
tass 68:0847e35d08a6 44
TASS Belgium NV 131:4758606c9316 45 void (*wakeup)(uint16_t ev, struct pico_socket *s);
tass 68:0847e35d08a6 46
tass 68:0847e35d08a6 47
tass 68:0847e35d08a6 48 #ifdef PICO_SUPPORT_TCP
TASS Belgium NV 131:4758606c9316 49 /* For the TCP backlog queue */
TASS Belgium NV 131:4758606c9316 50 struct pico_socket *backlog;
TASS Belgium NV 131:4758606c9316 51 struct pico_socket *next;
TASS Belgium NV 131:4758606c9316 52 struct pico_socket *parent;
tass picotcp@tass.be 149:5f4cb161cec3 53 uint16_t max_backlog;
tass picotcp@tass.be 149:5f4cb161cec3 54 uint16_t number_of_pending_conn;
tass 68:0847e35d08a6 55 #endif
tass 68:0847e35d08a6 56 #ifdef PICO_SUPPORT_MCAST
TASS Belgium NV 131:4758606c9316 57 struct pico_tree *MCASTListen;
tass 68:0847e35d08a6 58 #endif
TASS Belgium NV 131:4758606c9316 59 uint16_t ev_pending;
tass 68:0847e35d08a6 60
TASS Belgium NV 131:4758606c9316 61 struct pico_device *dev;
tass 68:0847e35d08a6 62
TASS Belgium NV 131:4758606c9316 63 /* Private field. */
TASS Belgium NV 131:4758606c9316 64 int id;
TASS Belgium NV 131:4758606c9316 65 uint16_t state;
TASS Belgium NV 131:4758606c9316 66 uint16_t opt_flags;
TASS Belgium NV 131:4758606c9316 67 pico_time timestamp;
TASS Belgium NV 131:4758606c9316 68 void *priv;
tass 68:0847e35d08a6 69 };
tass 68:0847e35d08a6 70
tass picotcp@tass.be 149:5f4cb161cec3 71 struct pico_remote_endpoint {
tass picotcp@tass.be 149:5f4cb161cec3 72 union pico_address remote_addr;
TASS Belgium NV 131:4758606c9316 73 uint16_t remote_port;
tass 68:0847e35d08a6 74 };
tass 68:0847e35d08a6 75
tass 68:0847e35d08a6 76
tass 68:0847e35d08a6 77 /* request struct for multicast socket opt */
tass 68:0847e35d08a6 78 struct pico_ip_mreq {
TASS Belgium NV 131:4758606c9316 79 struct pico_ip4 mcast_group_addr;
TASS Belgium NV 131:4758606c9316 80 struct pico_ip4 mcast_link_addr;
tass 68:0847e35d08a6 81 };
tass 68:0847e35d08a6 82
tass 68:0847e35d08a6 83 struct pico_ip_mreq_source {
TASS Belgium NV 131:4758606c9316 84 struct pico_ip4 mcast_group_addr;
TASS Belgium NV 131:4758606c9316 85 struct pico_ip4 mcast_source_addr;
TASS Belgium NV 131:4758606c9316 86 struct pico_ip4 mcast_link_addr;
tass 68:0847e35d08a6 87 };
tass 68:0847e35d08a6 88
tass picotcp@tass.be 149:5f4cb161cec3 89 #define PICO_SOCKET_STATE_UNDEFINED 0x0000u
tass picotcp@tass.be 149:5f4cb161cec3 90 #define PICO_SOCKET_STATE_SHUT_LOCAL 0x0001u
tass picotcp@tass.be 149:5f4cb161cec3 91 #define PICO_SOCKET_STATE_SHUT_REMOTE 0x0002u
tass picotcp@tass.be 149:5f4cb161cec3 92 #define PICO_SOCKET_STATE_BOUND 0x0004u
tass picotcp@tass.be 149:5f4cb161cec3 93 #define PICO_SOCKET_STATE_CONNECTED 0x0008u
tass picotcp@tass.be 149:5f4cb161cec3 94 #define PICO_SOCKET_STATE_CLOSING 0x0010u
tass picotcp@tass.be 149:5f4cb161cec3 95 #define PICO_SOCKET_STATE_CLOSED 0x0020u
tass 68:0847e35d08a6 96
tass picotcp@tass.be 149:5f4cb161cec3 97 # define PICO_SOCKET_STATE_TCP 0xFF00u
tass picotcp@tass.be 149:5f4cb161cec3 98 # define PICO_SOCKET_STATE_TCP_UNDEF 0x00FFu
tass picotcp@tass.be 149:5f4cb161cec3 99 # define PICO_SOCKET_STATE_TCP_CLOSED 0x0100u
tass picotcp@tass.be 149:5f4cb161cec3 100 # define PICO_SOCKET_STATE_TCP_LISTEN 0x0200u
tass picotcp@tass.be 149:5f4cb161cec3 101 # define PICO_SOCKET_STATE_TCP_SYN_SENT 0x0300u
tass picotcp@tass.be 149:5f4cb161cec3 102 # define PICO_SOCKET_STATE_TCP_SYN_RECV 0x0400u
tass picotcp@tass.be 149:5f4cb161cec3 103 # define PICO_SOCKET_STATE_TCP_ESTABLISHED 0x0500u
tass picotcp@tass.be 149:5f4cb161cec3 104 # define PICO_SOCKET_STATE_TCP_CLOSE_WAIT 0x0600u
tass picotcp@tass.be 149:5f4cb161cec3 105 # define PICO_SOCKET_STATE_TCP_LAST_ACK 0x0700u
tass picotcp@tass.be 149:5f4cb161cec3 106 # define PICO_SOCKET_STATE_TCP_FIN_WAIT1 0x0800u
tass picotcp@tass.be 149:5f4cb161cec3 107 # define PICO_SOCKET_STATE_TCP_FIN_WAIT2 0x0900u
tass picotcp@tass.be 149:5f4cb161cec3 108 # define PICO_SOCKET_STATE_TCP_CLOSING 0x0a00u
tass picotcp@tass.be 149:5f4cb161cec3 109 # define PICO_SOCKET_STATE_TCP_TIME_WAIT 0x0b00u
tass picotcp@tass.be 149:5f4cb161cec3 110 # define PICO_SOCKET_STATE_TCP_ARRAYSIZ 0x0cu
tass 68:0847e35d08a6 111
tass picotcp@tass.be 149:5f4cb161cec3 112
tass picotcp@tass.be 149:5f4cb161cec3 113 /* Socket options */
tass 68:0847e35d08a6 114 # define PICO_TCP_NODELAY 1
tass picotcp@tass.be 149:5f4cb161cec3 115 # define PICO_SOCKET_OPT_TCPNODELAY 0x0000u
tass 68:0847e35d08a6 116
tass 68:0847e35d08a6 117 # define PICO_IP_MULTICAST_EXCLUDE 0
tass 68:0847e35d08a6 118 # define PICO_IP_MULTICAST_INCLUDE 1
tass 68:0847e35d08a6 119 # define PICO_IP_MULTICAST_IF 32
tass 68:0847e35d08a6 120 # define PICO_IP_MULTICAST_TTL 33
tass 68:0847e35d08a6 121 # define PICO_IP_MULTICAST_LOOP 34
tass 68:0847e35d08a6 122 # define PICO_IP_ADD_MEMBERSHIP 35
tass 68:0847e35d08a6 123 # define PICO_IP_DROP_MEMBERSHIP 36
tass 68:0847e35d08a6 124 # define PICO_IP_UNBLOCK_SOURCE 37
tass 68:0847e35d08a6 125 # define PICO_IP_BLOCK_SOURCE 38
tass 68:0847e35d08a6 126 # define PICO_IP_ADD_SOURCE_MEMBERSHIP 39
tass 68:0847e35d08a6 127 # define PICO_IP_DROP_SOURCE_MEMBERSHIP 40
tass 68:0847e35d08a6 128
tass 68:0847e35d08a6 129 # define PICO_SOCKET_OPT_MULTICAST_LOOP 1
tass 68:0847e35d08a6 130
tass picotcp@tass.be 149:5f4cb161cec3 131 # define PICO_SOCKET_OPT_RCVBUF 52
tass picotcp@tass.be 149:5f4cb161cec3 132 # define PICO_SOCKET_OPT_SNDBUF 53
tass picotcp@tass.be 149:5f4cb161cec3 133
tass picotcp@tass.be 149:5f4cb161cec3 134 /* Constants */
tass 68:0847e35d08a6 135 # define PICO_IP_DEFAULT_MULTICAST_TTL 1
tass 68:0847e35d08a6 136 # define PICO_IP_DEFAULT_MULTICAST_LOOP 1
tass 68:0847e35d08a6 137
TASS Belgium NV 131:4758606c9316 138 #define PICO_SOCKET_TIMEOUT 90000u /* 90 seconds */
TASS Belgium NV 131:4758606c9316 139 #define PICO_SOCKET_BOUND_TIMEOUT 30000u /* 30 seconds */
tass 68:0847e35d08a6 140
tass picotcp@tass.be 149:5f4cb161cec3 141 #define PICO_SOCKET_SHUTDOWN_WRITE 0x01u
tass picotcp@tass.be 149:5f4cb161cec3 142 #define PICO_SOCKET_SHUTDOWN_READ 0x02u
tass 68:0847e35d08a6 143 #define TCPSTATE(s) ((s)->state & PICO_SOCKET_STATE_TCP)
tass 68:0847e35d08a6 144
tass picotcp@tass.be 149:5f4cb161cec3 145 #define PICO_SOCK_EV_RD 1u
tass picotcp@tass.be 149:5f4cb161cec3 146 #define PICO_SOCK_EV_WR 2u
tass picotcp@tass.be 149:5f4cb161cec3 147 #define PICO_SOCK_EV_CONN 4u
tass picotcp@tass.be 149:5f4cb161cec3 148 #define PICO_SOCK_EV_CLOSE 8u
tass picotcp@tass.be 149:5f4cb161cec3 149 #define PICO_SOCK_EV_FIN 0x10u
tass picotcp@tass.be 149:5f4cb161cec3 150 #define PICO_SOCK_EV_ERR 0x80u
tass 68:0847e35d08a6 151
tass 68:0847e35d08a6 152
tass 68:0847e35d08a6 153 struct pico_socket *pico_socket_open(uint16_t net, uint16_t proto, void (*wakeup)(uint16_t ev, struct pico_socket *s));
tass 68:0847e35d08a6 154
tass 68:0847e35d08a6 155 int pico_socket_read(struct pico_socket *s, void *buf, int len);
tass 68:0847e35d08a6 156 int pico_socket_write(struct pico_socket *s, const void *buf, int len);
tass 68:0847e35d08a6 157
tass 68:0847e35d08a6 158 int pico_socket_sendto(struct pico_socket *s, const void *buf, int len, void *dst, uint16_t remote_port);
tass 68:0847e35d08a6 159 int pico_socket_recvfrom(struct pico_socket *s, void *buf, int len, void *orig, uint16_t *local_port);
tass 68:0847e35d08a6 160
tass 68:0847e35d08a6 161 int pico_socket_send(struct pico_socket *s, const void *buf, int len);
tass 68:0847e35d08a6 162 int pico_socket_recv(struct pico_socket *s, void *buf, int len);
tass 68:0847e35d08a6 163
tass 68:0847e35d08a6 164 int pico_socket_bind(struct pico_socket *s, void *local_addr, uint16_t *port);
tass picotcp@tass.be 149:5f4cb161cec3 165 int pico_socket_getname(struct pico_socket *s, void *local_addr, uint16_t *port, uint16_t *proto);
tass picotcp@tass.be 149:5f4cb161cec3 166
tass 68:0847e35d08a6 167 int pico_socket_connect(struct pico_socket *s, const void *srv_addr, uint16_t remote_port);
tass 68:0847e35d08a6 168 int pico_socket_listen(struct pico_socket *s, const int backlog);
tass 68:0847e35d08a6 169 struct pico_socket *pico_socket_accept(struct pico_socket *s, void *orig, uint16_t *port);
tass 70:cd218dd180e5 170 int8_t pico_socket_del(struct pico_socket *s);
tass 68:0847e35d08a6 171
tass 68:0847e35d08a6 172 int pico_socket_setoption(struct pico_socket *s, int option, void *value);
tass 68:0847e35d08a6 173 int pico_socket_getoption(struct pico_socket *s, int option, void *value);
tass 68:0847e35d08a6 174
tass 68:0847e35d08a6 175 int pico_socket_shutdown(struct pico_socket *s, int mode);
tass 68:0847e35d08a6 176 int pico_socket_close(struct pico_socket *s);
tass 68:0847e35d08a6 177
tass 70:cd218dd180e5 178 struct pico_frame *pico_socket_frame_alloc(struct pico_socket *s, uint16_t len);
tass 68:0847e35d08a6 179
tass 68:0847e35d08a6 180 #ifdef PICO_SUPPORT_IPV4
tass 68:0847e35d08a6 181 # define is_sock_ipv4(x) (x->net == &pico_proto_ipv4)
tass 68:0847e35d08a6 182 #else
tass 68:0847e35d08a6 183 # define is_sock_ipv4(x) (0)
tass 68:0847e35d08a6 184 #endif
tass 68:0847e35d08a6 185
tass 68:0847e35d08a6 186 #ifdef PICO_SUPPORT_IPV6
tass 68:0847e35d08a6 187 # define is_sock_ipv6(x) (x->net == &pico_proto_ipv6)
tass 68:0847e35d08a6 188 #else
tass 68:0847e35d08a6 189 # define is_sock_ipv6(x) (0)
tass 68:0847e35d08a6 190 #endif
tass 68:0847e35d08a6 191
tass 68:0847e35d08a6 192 #ifdef PICO_SUPPORT_UDP
tass 68:0847e35d08a6 193 # define is_sock_udp(x) (x->net == &pico_proto_udp)
tass 68:0847e35d08a6 194 #else
tass 68:0847e35d08a6 195 # define is_sock_udp(x) (0)
tass 68:0847e35d08a6 196 #endif
tass 68:0847e35d08a6 197
tass 68:0847e35d08a6 198 #ifdef PICO_SUPPORT_TCP
tass 68:0847e35d08a6 199 # define is_sock_tcp(x) (x->net == &pico_proto_tcp)
tass 68:0847e35d08a6 200 #else
tass 68:0847e35d08a6 201 # define is_sock_tcp(x) (0)
tass 68:0847e35d08a6 202 #endif
tass 68:0847e35d08a6 203
tass 68:0847e35d08a6 204 /* Interface towards transport protocol */
tass 68:0847e35d08a6 205 int pico_transport_process_in(struct pico_protocol *self, struct pico_frame *f);
tass 68:0847e35d08a6 206 struct pico_socket *pico_socket_clone(struct pico_socket *facsimile);
tass 70:cd218dd180e5 207 int8_t pico_socket_add(struct pico_socket *s);
tass 68:0847e35d08a6 208 int pico_transport_error(struct pico_frame *f, uint8_t proto, int code);
tass 68:0847e35d08a6 209
tass 68:0847e35d08a6 210 /* Socket loop */
tass 68:0847e35d08a6 211 int pico_sockets_loop(int loop_score);
TASS Belgium NV 131:4758606c9316 212 struct pico_socket*pico_sockets_find(uint16_t local, uint16_t remote);
tass 68:0847e35d08a6 213 /* Port check */
tass 68:0847e35d08a6 214 int pico_is_port_free(uint16_t proto, uint16_t port, void *addr, void *net);
tass 68:0847e35d08a6 215
tass picotcp@tass.be 149:5f4cb161cec3 216 struct pico_sockport *pico_get_sockport(uint16_t proto, uint16_t port);
tass picotcp@tass.be 149:5f4cb161cec3 217
tass picotcp@tass.be 149:5f4cb161cec3 218 uint16_t pico_socket_get_mtu(struct pico_socket *s);
tass picotcp@tass.be 149:5f4cb161cec3 219 int pico_socket_set_family(struct pico_socket *s, uint16_t family);
tass picotcp@tass.be 149:5f4cb161cec3 220
tass picotcp@tass.be 149:5f4cb161cec3 221 #define PICO_SOCKET_SETOPT_EN(socket, index) (socket->opt_flags |= (1 << index))
tass picotcp@tass.be 149:5f4cb161cec3 222 #define PICO_SOCKET_SETOPT_DIS(socket, index) (socket->opt_flags &= (uint16_t) ~(1 << index))
tass picotcp@tass.be 149:5f4cb161cec3 223 #define PICO_SOCKET_GETOPT(socket, index) ((socket->opt_flags & (1u << index)) != 0)
tass picotcp@tass.be 149:5f4cb161cec3 224
tass 68:0847e35d08a6 225
tass 68:0847e35d08a6 226 #endif