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
Date:
Mon Sep 28 14:14:17 2015 +0200
Revision:
154:6c0e92a80c4a
Parent:
152:a3d286bf94e5
To latest development.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TASS Belgium NV 131:4758606c9316 1 /*********************************************************************
tass 152:a3d286bf94e5 2 PicoTCP. Copyright (c) 2012-2015 Altran Intelligent Systems. 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 152:a3d286bf94e5 17 #define PICO_DEFAULT_SOCKETQ (6 * 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 152:a3d286bf94e5 24 #ifdef PICO_SUPPORT_IPV4
tass 152:a3d286bf94e5 25 # define IS_SOCK_IPV4(s) ((s->net == &pico_proto_ipv4))
tass 152:a3d286bf94e5 26 #else
tass 152:a3d286bf94e5 27 # define IS_SOCK_IPV4(s) (0)
tass 152:a3d286bf94e5 28 #endif
tass 152:a3d286bf94e5 29
tass 152:a3d286bf94e5 30 #ifdef PICO_SUPPORT_IPV6
tass 152:a3d286bf94e5 31 # define IS_SOCK_IPV6(s) ((s->net == &pico_proto_ipv6))
tass 152:a3d286bf94e5 32 #else
tass 152:a3d286bf94e5 33 # define IS_SOCK_IPV6(s) (0)
tass 152:a3d286bf94e5 34 #endif
tass 152:a3d286bf94e5 35
tass 152:a3d286bf94e5 36
tass picotcp@tass.be 149:5f4cb161cec3 37 struct pico_sockport
tass picotcp@tass.be 149:5f4cb161cec3 38 {
tass picotcp@tass.be 149:5f4cb161cec3 39 struct pico_tree socks; /* how you make the connection ? */
tass picotcp@tass.be 149:5f4cb161cec3 40 uint16_t number;
tass picotcp@tass.be 149:5f4cb161cec3 41 uint16_t proto;
tass picotcp@tass.be 149:5f4cb161cec3 42 };
tass picotcp@tass.be 149:5f4cb161cec3 43
tass 68:0847e35d08a6 44
tass 68:0847e35d08a6 45 struct pico_socket {
TASS Belgium NV 131:4758606c9316 46 struct pico_protocol *proto;
TASS Belgium NV 131:4758606c9316 47 struct pico_protocol *net;
tass 68:0847e35d08a6 48
tass picotcp@tass.be 149:5f4cb161cec3 49 union pico_address local_addr;
tass picotcp@tass.be 149:5f4cb161cec3 50 union pico_address remote_addr;
tass 68:0847e35d08a6 51
TASS Belgium NV 131:4758606c9316 52 uint16_t local_port;
TASS Belgium NV 131:4758606c9316 53 uint16_t remote_port;
tass 68:0847e35d08a6 54
TASS Belgium NV 131:4758606c9316 55 struct pico_queue q_in;
TASS Belgium NV 131:4758606c9316 56 struct pico_queue q_out;
tass 68:0847e35d08a6 57
TASS Belgium NV 131:4758606c9316 58 void (*wakeup)(uint16_t ev, struct pico_socket *s);
tass 68:0847e35d08a6 59
tass 68:0847e35d08a6 60
tass 68:0847e35d08a6 61 #ifdef PICO_SUPPORT_TCP
TASS Belgium NV 131:4758606c9316 62 /* For the TCP backlog queue */
TASS Belgium NV 131:4758606c9316 63 struct pico_socket *backlog;
TASS Belgium NV 131:4758606c9316 64 struct pico_socket *next;
TASS Belgium NV 131:4758606c9316 65 struct pico_socket *parent;
tass picotcp@tass.be 149:5f4cb161cec3 66 uint16_t max_backlog;
tass picotcp@tass.be 149:5f4cb161cec3 67 uint16_t number_of_pending_conn;
tass 68:0847e35d08a6 68 #endif
tass 68:0847e35d08a6 69 #ifdef PICO_SUPPORT_MCAST
TASS Belgium NV 131:4758606c9316 70 struct pico_tree *MCASTListen;
tass 152:a3d286bf94e5 71 #ifdef PICO_SUPPORT_IPV6
tass 152:a3d286bf94e5 72 struct pico_tree *MCASTListen_ipv6;
tass 152:a3d286bf94e5 73 #endif
tass 68:0847e35d08a6 74 #endif
TASS Belgium NV 131:4758606c9316 75 uint16_t ev_pending;
tass 68:0847e35d08a6 76
TASS Belgium NV 131:4758606c9316 77 struct pico_device *dev;
tass 68:0847e35d08a6 78
TASS Belgium NV 131:4758606c9316 79 /* Private field. */
TASS Belgium NV 131:4758606c9316 80 int id;
TASS Belgium NV 131:4758606c9316 81 uint16_t state;
TASS Belgium NV 131:4758606c9316 82 uint16_t opt_flags;
TASS Belgium NV 131:4758606c9316 83 pico_time timestamp;
TASS Belgium NV 131:4758606c9316 84 void *priv;
tass 68:0847e35d08a6 85 };
tass 68:0847e35d08a6 86
tass picotcp@tass.be 149:5f4cb161cec3 87 struct pico_remote_endpoint {
tass picotcp@tass.be 149:5f4cb161cec3 88 union pico_address remote_addr;
TASS Belgium NV 131:4758606c9316 89 uint16_t remote_port;
tass 68:0847e35d08a6 90 };
tass 68:0847e35d08a6 91
tass 68:0847e35d08a6 92
tass 68:0847e35d08a6 93 struct pico_ip_mreq {
tass 152:a3d286bf94e5 94 union pico_address mcast_group_addr;
tass 152:a3d286bf94e5 95 union pico_address mcast_link_addr;
tass 152:a3d286bf94e5 96 };
tass 154:6c0e92a80c4a 97 struct pico_ip_mreq_source {
tass 152:a3d286bf94e5 98 union pico_address mcast_group_addr;
tass 152:a3d286bf94e5 99 union pico_address mcast_source_addr;
tass 152:a3d286bf94e5 100 union pico_address mcast_link_addr;
tass 152:a3d286bf94e5 101 };
tass 152:a3d286bf94e5 102
tass 68:0847e35d08a6 103
tass picotcp@tass.be 149:5f4cb161cec3 104 #define PICO_SOCKET_STATE_UNDEFINED 0x0000u
tass picotcp@tass.be 149:5f4cb161cec3 105 #define PICO_SOCKET_STATE_SHUT_LOCAL 0x0001u
tass picotcp@tass.be 149:5f4cb161cec3 106 #define PICO_SOCKET_STATE_SHUT_REMOTE 0x0002u
tass picotcp@tass.be 149:5f4cb161cec3 107 #define PICO_SOCKET_STATE_BOUND 0x0004u
tass picotcp@tass.be 149:5f4cb161cec3 108 #define PICO_SOCKET_STATE_CONNECTED 0x0008u
tass picotcp@tass.be 149:5f4cb161cec3 109 #define PICO_SOCKET_STATE_CLOSING 0x0010u
tass picotcp@tass.be 149:5f4cb161cec3 110 #define PICO_SOCKET_STATE_CLOSED 0x0020u
tass 68:0847e35d08a6 111
tass picotcp@tass.be 149:5f4cb161cec3 112 # define PICO_SOCKET_STATE_TCP 0xFF00u
tass picotcp@tass.be 149:5f4cb161cec3 113 # define PICO_SOCKET_STATE_TCP_UNDEF 0x00FFu
tass picotcp@tass.be 149:5f4cb161cec3 114 # define PICO_SOCKET_STATE_TCP_CLOSED 0x0100u
tass picotcp@tass.be 149:5f4cb161cec3 115 # define PICO_SOCKET_STATE_TCP_LISTEN 0x0200u
tass picotcp@tass.be 149:5f4cb161cec3 116 # define PICO_SOCKET_STATE_TCP_SYN_SENT 0x0300u
tass picotcp@tass.be 149:5f4cb161cec3 117 # define PICO_SOCKET_STATE_TCP_SYN_RECV 0x0400u
tass picotcp@tass.be 149:5f4cb161cec3 118 # define PICO_SOCKET_STATE_TCP_ESTABLISHED 0x0500u
tass picotcp@tass.be 149:5f4cb161cec3 119 # define PICO_SOCKET_STATE_TCP_CLOSE_WAIT 0x0600u
tass picotcp@tass.be 149:5f4cb161cec3 120 # define PICO_SOCKET_STATE_TCP_LAST_ACK 0x0700u
tass picotcp@tass.be 149:5f4cb161cec3 121 # define PICO_SOCKET_STATE_TCP_FIN_WAIT1 0x0800u
tass picotcp@tass.be 149:5f4cb161cec3 122 # define PICO_SOCKET_STATE_TCP_FIN_WAIT2 0x0900u
tass picotcp@tass.be 149:5f4cb161cec3 123 # define PICO_SOCKET_STATE_TCP_CLOSING 0x0a00u
tass picotcp@tass.be 149:5f4cb161cec3 124 # define PICO_SOCKET_STATE_TCP_TIME_WAIT 0x0b00u
tass picotcp@tass.be 149:5f4cb161cec3 125 # define PICO_SOCKET_STATE_TCP_ARRAYSIZ 0x0cu
tass 68:0847e35d08a6 126
tass picotcp@tass.be 149:5f4cb161cec3 127
tass picotcp@tass.be 149:5f4cb161cec3 128 /* Socket options */
tass 68:0847e35d08a6 129 # define PICO_TCP_NODELAY 1
tass picotcp@tass.be 149:5f4cb161cec3 130 # define PICO_SOCKET_OPT_TCPNODELAY 0x0000u
tass 68:0847e35d08a6 131
tass 68:0847e35d08a6 132 # define PICO_IP_MULTICAST_EXCLUDE 0
tass 68:0847e35d08a6 133 # define PICO_IP_MULTICAST_INCLUDE 1
tass 68:0847e35d08a6 134 # define PICO_IP_MULTICAST_IF 32
tass 68:0847e35d08a6 135 # define PICO_IP_MULTICAST_TTL 33
tass 68:0847e35d08a6 136 # define PICO_IP_MULTICAST_LOOP 34
tass 68:0847e35d08a6 137 # define PICO_IP_ADD_MEMBERSHIP 35
tass 68:0847e35d08a6 138 # define PICO_IP_DROP_MEMBERSHIP 36
tass 68:0847e35d08a6 139 # define PICO_IP_UNBLOCK_SOURCE 37
tass 68:0847e35d08a6 140 # define PICO_IP_BLOCK_SOURCE 38
tass 68:0847e35d08a6 141 # define PICO_IP_ADD_SOURCE_MEMBERSHIP 39
tass 68:0847e35d08a6 142 # define PICO_IP_DROP_SOURCE_MEMBERSHIP 40
tass 68:0847e35d08a6 143
tass 68:0847e35d08a6 144 # define PICO_SOCKET_OPT_MULTICAST_LOOP 1
tass 152:a3d286bf94e5 145 # define PICO_SOCKET_OPT_KEEPIDLE 4
tass 152:a3d286bf94e5 146 # define PICO_SOCKET_OPT_KEEPINTVL 5
tass 152:a3d286bf94e5 147 # define PICO_SOCKET_OPT_KEEPCNT 6
tass 152:a3d286bf94e5 148
tass 152:a3d286bf94e5 149 #define PICO_SOCKET_OPT_LINGER 13
tass 68:0847e35d08a6 150
tass picotcp@tass.be 149:5f4cb161cec3 151 # define PICO_SOCKET_OPT_RCVBUF 52
tass picotcp@tass.be 149:5f4cb161cec3 152 # define PICO_SOCKET_OPT_SNDBUF 53
tass picotcp@tass.be 149:5f4cb161cec3 153
tass 152:a3d286bf94e5 154
tass picotcp@tass.be 149:5f4cb161cec3 155 /* Constants */
tass 68:0847e35d08a6 156 # define PICO_IP_DEFAULT_MULTICAST_TTL 1
tass 68:0847e35d08a6 157 # define PICO_IP_DEFAULT_MULTICAST_LOOP 1
tass 68:0847e35d08a6 158
tass 152:a3d286bf94e5 159 #define PICO_SOCKET_TIMEOUT 5000u /* 5 seconds */
tass 152:a3d286bf94e5 160 #define PICO_SOCKET_LINGER_TIMEOUT 3000u /* 3 seconds */
TASS Belgium NV 131:4758606c9316 161 #define PICO_SOCKET_BOUND_TIMEOUT 30000u /* 30 seconds */
tass 68:0847e35d08a6 162
tass picotcp@tass.be 149:5f4cb161cec3 163 #define PICO_SOCKET_SHUTDOWN_WRITE 0x01u
tass picotcp@tass.be 149:5f4cb161cec3 164 #define PICO_SOCKET_SHUTDOWN_READ 0x02u
tass 68:0847e35d08a6 165 #define TCPSTATE(s) ((s)->state & PICO_SOCKET_STATE_TCP)
tass 68:0847e35d08a6 166
tass picotcp@tass.be 149:5f4cb161cec3 167 #define PICO_SOCK_EV_RD 1u
tass picotcp@tass.be 149:5f4cb161cec3 168 #define PICO_SOCK_EV_WR 2u
tass picotcp@tass.be 149:5f4cb161cec3 169 #define PICO_SOCK_EV_CONN 4u
tass picotcp@tass.be 149:5f4cb161cec3 170 #define PICO_SOCK_EV_CLOSE 8u
tass picotcp@tass.be 149:5f4cb161cec3 171 #define PICO_SOCK_EV_FIN 0x10u
tass picotcp@tass.be 149:5f4cb161cec3 172 #define PICO_SOCK_EV_ERR 0x80u
tass 68:0847e35d08a6 173
tass 152:a3d286bf94e5 174 struct pico_msginfo {
tass 152:a3d286bf94e5 175 struct pico_device *dev;
tass 152:a3d286bf94e5 176 uint8_t ttl;
tass 152:a3d286bf94e5 177 uint8_t tos;
tass 152:a3d286bf94e5 178 };
tass 68:0847e35d08a6 179
tass 68:0847e35d08a6 180 struct pico_socket *pico_socket_open(uint16_t net, uint16_t proto, void (*wakeup)(uint16_t ev, struct pico_socket *s));
tass 68:0847e35d08a6 181
tass 68:0847e35d08a6 182 int pico_socket_read(struct pico_socket *s, void *buf, int len);
tass 68:0847e35d08a6 183 int pico_socket_write(struct pico_socket *s, const void *buf, int len);
tass 68:0847e35d08a6 184
tass 68:0847e35d08a6 185 int pico_socket_sendto(struct pico_socket *s, const void *buf, int len, void *dst, uint16_t remote_port);
tass 152:a3d286bf94e5 186 int pico_socket_sendto_extended(struct pico_socket *s, const void *buf, const int len,
tass 152:a3d286bf94e5 187 void *dst, uint16_t remote_port, struct pico_msginfo *msginfo);
tass 152:a3d286bf94e5 188
tass 68:0847e35d08a6 189 int pico_socket_recvfrom(struct pico_socket *s, void *buf, int len, void *orig, uint16_t *local_port);
tass 152:a3d286bf94e5 190 int pico_socket_recvfrom_extended(struct pico_socket *s, void *buf, int len, void *orig,
tass 152:a3d286bf94e5 191 uint16_t *remote_port, struct pico_msginfo *msginfo);
tass 68:0847e35d08a6 192
tass 68:0847e35d08a6 193 int pico_socket_send(struct pico_socket *s, const void *buf, int len);
tass 68:0847e35d08a6 194 int pico_socket_recv(struct pico_socket *s, void *buf, int len);
tass 68:0847e35d08a6 195
tass 68:0847e35d08a6 196 int pico_socket_bind(struct pico_socket *s, void *local_addr, uint16_t *port);
tass picotcp@tass.be 149:5f4cb161cec3 197 int pico_socket_getname(struct pico_socket *s, void *local_addr, uint16_t *port, uint16_t *proto);
tass 152:a3d286bf94e5 198 int pico_socket_getpeername(struct pico_socket *s, void *remote_addr, uint16_t *port, uint16_t *proto);
tass picotcp@tass.be 149:5f4cb161cec3 199
tass 68:0847e35d08a6 200 int pico_socket_connect(struct pico_socket *s, const void *srv_addr, uint16_t remote_port);
tass 68:0847e35d08a6 201 int pico_socket_listen(struct pico_socket *s, const int backlog);
tass 68:0847e35d08a6 202 struct pico_socket *pico_socket_accept(struct pico_socket *s, void *orig, uint16_t *port);
tass 70:cd218dd180e5 203 int8_t pico_socket_del(struct pico_socket *s);
tass 68:0847e35d08a6 204
tass 68:0847e35d08a6 205 int pico_socket_setoption(struct pico_socket *s, int option, void *value);
tass 68:0847e35d08a6 206 int pico_socket_getoption(struct pico_socket *s, int option, void *value);
tass 68:0847e35d08a6 207
tass 68:0847e35d08a6 208 int pico_socket_shutdown(struct pico_socket *s, int mode);
tass 68:0847e35d08a6 209 int pico_socket_close(struct pico_socket *s);
tass 68:0847e35d08a6 210
tass 70:cd218dd180e5 211 struct pico_frame *pico_socket_frame_alloc(struct pico_socket *s, uint16_t len);
tass 68:0847e35d08a6 212
tass 68:0847e35d08a6 213 #ifdef PICO_SUPPORT_IPV4
tass 68:0847e35d08a6 214 # define is_sock_ipv4(x) (x->net == &pico_proto_ipv4)
tass 68:0847e35d08a6 215 #else
tass 68:0847e35d08a6 216 # define is_sock_ipv4(x) (0)
tass 68:0847e35d08a6 217 #endif
tass 68:0847e35d08a6 218
tass 68:0847e35d08a6 219 #ifdef PICO_SUPPORT_IPV6
tass 68:0847e35d08a6 220 # define is_sock_ipv6(x) (x->net == &pico_proto_ipv6)
tass 68:0847e35d08a6 221 #else
tass 68:0847e35d08a6 222 # define is_sock_ipv6(x) (0)
tass 68:0847e35d08a6 223 #endif
tass 68:0847e35d08a6 224
tass 68:0847e35d08a6 225 #ifdef PICO_SUPPORT_UDP
tass 152:a3d286bf94e5 226 # define is_sock_udp(x) (x->proto == &pico_proto_udp)
tass 68:0847e35d08a6 227 #else
tass 68:0847e35d08a6 228 # define is_sock_udp(x) (0)
tass 68:0847e35d08a6 229 #endif
tass 68:0847e35d08a6 230
tass 68:0847e35d08a6 231 #ifdef PICO_SUPPORT_TCP
tass 152:a3d286bf94e5 232 # define is_sock_tcp(x) (x->proto == &pico_proto_tcp)
tass 68:0847e35d08a6 233 #else
tass 68:0847e35d08a6 234 # define is_sock_tcp(x) (0)
tass 68:0847e35d08a6 235 #endif
tass 68:0847e35d08a6 236
tass 68:0847e35d08a6 237 /* Interface towards transport protocol */
tass 68:0847e35d08a6 238 int pico_transport_process_in(struct pico_protocol *self, struct pico_frame *f);
tass 68:0847e35d08a6 239 struct pico_socket *pico_socket_clone(struct pico_socket *facsimile);
tass 70:cd218dd180e5 240 int8_t pico_socket_add(struct pico_socket *s);
tass 68:0847e35d08a6 241 int pico_transport_error(struct pico_frame *f, uint8_t proto, int code);
tass 68:0847e35d08a6 242
tass 68:0847e35d08a6 243 /* Socket loop */
tass 68:0847e35d08a6 244 int pico_sockets_loop(int loop_score);
TASS Belgium NV 131:4758606c9316 245 struct pico_socket*pico_sockets_find(uint16_t local, uint16_t remote);
tass 68:0847e35d08a6 246 /* Port check */
tass 68:0847e35d08a6 247 int pico_is_port_free(uint16_t proto, uint16_t port, void *addr, void *net);
tass 68:0847e35d08a6 248
tass picotcp@tass.be 149:5f4cb161cec3 249 struct pico_sockport *pico_get_sockport(uint16_t proto, uint16_t port);
tass picotcp@tass.be 149:5f4cb161cec3 250
tass 152:a3d286bf94e5 251 uint32_t pico_socket_get_mss(struct pico_socket *s);
tass picotcp@tass.be 149:5f4cb161cec3 252 int pico_socket_set_family(struct pico_socket *s, uint16_t family);
tass picotcp@tass.be 149:5f4cb161cec3 253
tass 152:a3d286bf94e5 254 int pico_count_sockets(uint8_t proto);
tass 152:a3d286bf94e5 255
tass picotcp@tass.be 149:5f4cb161cec3 256 #define PICO_SOCKET_SETOPT_EN(socket, index) (socket->opt_flags |= (1 << index))
tass picotcp@tass.be 149:5f4cb161cec3 257 #define PICO_SOCKET_SETOPT_DIS(socket, index) (socket->opt_flags &= (uint16_t) ~(1 << index))
tass picotcp@tass.be 149:5f4cb161cec3 258 #define PICO_SOCKET_GETOPT(socket, index) ((socket->opt_flags & (1u << index)) != 0)
tass picotcp@tass.be 149:5f4cb161cec3 259
tass 68:0847e35d08a6 260
tass 68:0847e35d08a6 261 #endif