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:
Thu Sep 26 07:05:22 2013 +0000
Revision:
70:cd218dd180e5
Parent:
68:0847e35d08a6
Child:
73:dfb737147f6e
Update from masterbranch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tass 68:0847e35d08a6 1 /*********************************************************************
tass 68:0847e35d08a6 2 PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
tass 68:0847e35d08a6 3 See LICENSE and COPYING for usage.
tass 68:0847e35d08a6 4
tass 68:0847e35d08a6 5 .
tass 68:0847e35d08a6 6
tass 68:0847e35d08a6 7 Authors: Daniele Lacamera
tass 68:0847e35d08a6 8 *********************************************************************/
tass 68:0847e35d08a6 9
tass 68:0847e35d08a6 10
tass 68:0847e35d08a6 11 #include "pico_udp.h"
tass 68:0847e35d08a6 12 #include "pico_config.h"
tass 68:0847e35d08a6 13 #include "pico_eth.h"
tass 68:0847e35d08a6 14 #include "pico_socket.h"
tass 68:0847e35d08a6 15 #include "pico_stack.h"
tass 68:0847e35d08a6 16
tass 68:0847e35d08a6 17
tass 68:0847e35d08a6 18 /* Queues */
tass 68:0847e35d08a6 19 static struct pico_queue udp_in = {0};
tass 68:0847e35d08a6 20 static struct pico_queue udp_out = {0};
tass 68:0847e35d08a6 21
tass 68:0847e35d08a6 22
tass 68:0847e35d08a6 23 /* Functions */
tass 68:0847e35d08a6 24
tass 68:0847e35d08a6 25 uint16_t pico_udp_checksum_ipv4(struct pico_frame *f)
tass 68:0847e35d08a6 26 {
tass 68:0847e35d08a6 27 struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) f->net_hdr;
tass 68:0847e35d08a6 28 struct pico_udp_hdr *udp_hdr = (struct pico_udp_hdr *) f->transport_hdr;
tass 68:0847e35d08a6 29 struct pico_socket *s = f->sock;
tass 68:0847e35d08a6 30 struct pico_ipv4_pseudo_hdr pseudo;
tass 68:0847e35d08a6 31
tass 68:0847e35d08a6 32 if (s) {
tass 68:0847e35d08a6 33 /* Case of outgoing frame */
tass 68:0847e35d08a6 34 //dbg("UDP CRC: on outgoing frame\n");
tass 68:0847e35d08a6 35 pseudo.src.addr = s->local_addr.ip4.addr;
tass 68:0847e35d08a6 36 pseudo.dst.addr = s->remote_addr.ip4.addr;
tass 68:0847e35d08a6 37 } else {
tass 68:0847e35d08a6 38 /* Case of incomming frame */
tass 68:0847e35d08a6 39 //dbg("UDP CRC: on incomming frame\n");
tass 68:0847e35d08a6 40 pseudo.src.addr = hdr->src.addr;
tass 68:0847e35d08a6 41 pseudo.dst.addr = hdr->dst.addr;
tass 68:0847e35d08a6 42 }
tass 68:0847e35d08a6 43 pseudo.zeros = 0;
tass 68:0847e35d08a6 44 pseudo.proto = PICO_PROTO_UDP;
tass 68:0847e35d08a6 45 pseudo.len = short_be(f->transport_len);
tass 68:0847e35d08a6 46
tass 68:0847e35d08a6 47 return pico_dualbuffer_checksum(&pseudo, sizeof(struct pico_ipv4_pseudo_hdr), udp_hdr, f->transport_len);
tass 68:0847e35d08a6 48 }
tass 68:0847e35d08a6 49
tass 68:0847e35d08a6 50
tass 68:0847e35d08a6 51 static int pico_udp_process_out(struct pico_protocol *self, struct pico_frame *f)
tass 68:0847e35d08a6 52 {
tass 68:0847e35d08a6 53 IGNORE_PARAMETER(self);
tass 68:0847e35d08a6 54 return pico_network_send(f);
tass 68:0847e35d08a6 55 }
tass 68:0847e35d08a6 56
tass 68:0847e35d08a6 57 static int pico_udp_push(struct pico_protocol *self, struct pico_frame *f)
tass 68:0847e35d08a6 58 {
tass 68:0847e35d08a6 59 struct pico_udp_hdr *hdr = (struct pico_udp_hdr *) f->transport_hdr;
tass 68:0847e35d08a6 60 struct pico_remote_duple *remote_duple = (struct pico_remote_duple *) f->info;
tass 68:0847e35d08a6 61
tass 68:0847e35d08a6 62 /* this (fragmented) frame should contain a transport header */
tass 68:0847e35d08a6 63 if (f->transport_hdr != f->payload) {
tass 68:0847e35d08a6 64 hdr->trans.sport = f->sock->local_port;
tass 68:0847e35d08a6 65 if (remote_duple) {
tass 68:0847e35d08a6 66 hdr->trans.dport = remote_duple->remote_port;
tass 68:0847e35d08a6 67 } else {
tass 68:0847e35d08a6 68 hdr->trans.dport = f->sock->remote_port;
tass 68:0847e35d08a6 69 }
tass 68:0847e35d08a6 70 hdr->len = short_be(f->transport_len);
tass 68:0847e35d08a6 71 /* do not perform CRC validation. If you want to, a system needs to be
tass 68:0847e35d08a6 72 implemented to calculate the CRC over the total payload of a
tass 68:0847e35d08a6 73 fragmented payload */
tass 68:0847e35d08a6 74 hdr->crc = 0;
tass 68:0847e35d08a6 75 }
tass 68:0847e35d08a6 76
tass 68:0847e35d08a6 77 if (pico_enqueue(self->q_out, f) > 0) {
tass 68:0847e35d08a6 78 return f->payload_len;
tass 68:0847e35d08a6 79 } else {
tass 68:0847e35d08a6 80 return 0;
tass 68:0847e35d08a6 81 }
tass 68:0847e35d08a6 82 }
tass 68:0847e35d08a6 83
tass 68:0847e35d08a6 84 /* Interface: protocol definition */
tass 68:0847e35d08a6 85 struct pico_protocol pico_proto_udp = {
tass 68:0847e35d08a6 86 .name = "udp",
tass 68:0847e35d08a6 87 .proto_number = PICO_PROTO_UDP,
tass 68:0847e35d08a6 88 .layer = PICO_LAYER_TRANSPORT,
tass 68:0847e35d08a6 89 .process_in = pico_transport_process_in,
tass 68:0847e35d08a6 90 .process_out = pico_udp_process_out,
tass 68:0847e35d08a6 91 .push = pico_udp_push,
tass 68:0847e35d08a6 92 .q_in = &udp_in,
tass 68:0847e35d08a6 93 .q_out = &udp_out,
tass 68:0847e35d08a6 94 };
tass 68:0847e35d08a6 95
tass 68:0847e35d08a6 96
tass 68:0847e35d08a6 97 #define PICO_UDP_MODE_UNICAST 0x01
tass 68:0847e35d08a6 98 #define PICO_UDP_MODE_MULTICAST 0x02
tass 68:0847e35d08a6 99 #define PICO_UDP_MODE_BROADCAST 0xFF
tass 68:0847e35d08a6 100
tass 68:0847e35d08a6 101 struct pico_socket_udp
tass 68:0847e35d08a6 102 {
tass 68:0847e35d08a6 103 struct pico_socket sock;
tass 68:0847e35d08a6 104 int mode;
tass 68:0847e35d08a6 105 #ifdef PICO_SUPPORT_MCAST
tass 68:0847e35d08a6 106 uint8_t mc_ttl; /* Multicasting TTL */
tass 68:0847e35d08a6 107 #endif
tass 68:0847e35d08a6 108 };
tass 68:0847e35d08a6 109
tass 68:0847e35d08a6 110 #ifdef PICO_SUPPORT_MCAST
tass 68:0847e35d08a6 111 int pico_udp_set_mc_ttl(struct pico_socket *s, uint8_t ttl)
tass 68:0847e35d08a6 112 {
tass 68:0847e35d08a6 113 struct pico_socket_udp *u;
tass 68:0847e35d08a6 114 if(!s) {
tass 68:0847e35d08a6 115 pico_err = PICO_ERR_EINVAL;
tass 68:0847e35d08a6 116 return -1;
tass 68:0847e35d08a6 117 }
tass 68:0847e35d08a6 118 u = (struct pico_socket_udp *) s;
tass 68:0847e35d08a6 119 u->mc_ttl = ttl;
tass 68:0847e35d08a6 120 return 0;
tass 68:0847e35d08a6 121 }
tass 68:0847e35d08a6 122
tass 68:0847e35d08a6 123 int pico_udp_get_mc_ttl(struct pico_socket *s, uint8_t *ttl)
tass 68:0847e35d08a6 124 {
tass 68:0847e35d08a6 125 struct pico_socket_udp *u;
tass 68:0847e35d08a6 126 if(!s)
tass 68:0847e35d08a6 127 return -1;
tass 68:0847e35d08a6 128 u = (struct pico_socket_udp *) s;
tass 68:0847e35d08a6 129 *ttl = u->mc_ttl;
tass 68:0847e35d08a6 130 return 0;
tass 68:0847e35d08a6 131 }
tass 68:0847e35d08a6 132 #endif /* PICO_SUPPORT_MCAST */
tass 68:0847e35d08a6 133
tass 68:0847e35d08a6 134 struct pico_socket *pico_udp_open(void)
tass 68:0847e35d08a6 135 {
tass 68:0847e35d08a6 136 struct pico_socket_udp *u = pico_zalloc(sizeof(struct pico_socket_udp));
tass 68:0847e35d08a6 137 if (!u)
tass 68:0847e35d08a6 138 return NULL;
tass 68:0847e35d08a6 139 u->mode = PICO_UDP_MODE_UNICAST;
tass 68:0847e35d08a6 140
tass 68:0847e35d08a6 141 #ifdef PICO_SUPPORT_MCAST
tass 68:0847e35d08a6 142 u->mc_ttl = PICO_IP_DEFAULT_MULTICAST_TTL;
tass 68:0847e35d08a6 143 /* enable multicast loopback by default */
tass 68:0847e35d08a6 144 u->sock.opt_flags |= (1 << PICO_SOCKET_OPT_MULTICAST_LOOP);
tass 68:0847e35d08a6 145 #endif
tass 68:0847e35d08a6 146
tass 68:0847e35d08a6 147 return &u->sock;
tass 68:0847e35d08a6 148 }
tass 68:0847e35d08a6 149
tass 70:cd218dd180e5 150 uint16_t pico_udp_recv(struct pico_socket *s, void *buf, uint16_t len, void *src, uint16_t *port)
tass 68:0847e35d08a6 151 {
tass 68:0847e35d08a6 152 struct pico_frame *f = pico_queue_peek(&s->q_in);
tass 68:0847e35d08a6 153 if (f) {
tass 68:0847e35d08a6 154 f->payload = f->transport_hdr + sizeof(struct pico_udp_hdr);
tass 70:cd218dd180e5 155 f->payload_len = (uint16_t)(f->transport_len - sizeof(struct pico_udp_hdr));
tass 68:0847e35d08a6 156 // dbg("expected: %d, got: %d\n", len, f->payload_len);
tass 68:0847e35d08a6 157 if (src)
tass 68:0847e35d08a6 158 pico_store_network_origin(src, f);
tass 68:0847e35d08a6 159 if (port) {
tass 68:0847e35d08a6 160 struct pico_trans *hdr = (struct pico_trans *)f->transport_hdr;
tass 68:0847e35d08a6 161 *port = hdr->sport;
tass 68:0847e35d08a6 162 }
tass 68:0847e35d08a6 163 if (f->payload_len > len) {
tass 68:0847e35d08a6 164 memcpy(buf, f->payload, len);
tass 68:0847e35d08a6 165 f->payload += len;
tass 68:0847e35d08a6 166 f->payload_len -= len;
tass 68:0847e35d08a6 167 return len;
tass 68:0847e35d08a6 168 } else {
tass 70:cd218dd180e5 169 uint16_t ret = f->payload_len;
tass 68:0847e35d08a6 170 memcpy(buf, f->payload, f->payload_len);
tass 68:0847e35d08a6 171 f = pico_dequeue(&s->q_in);
tass 68:0847e35d08a6 172 pico_frame_discard(f);
tass 68:0847e35d08a6 173 return ret;
tass 68:0847e35d08a6 174 }
tass 68:0847e35d08a6 175 } else return 0;
tass 68:0847e35d08a6 176 }
tass 68:0847e35d08a6 177