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 RTOSgeneric mbed Ethernet driverhigh performance NXP LPC1768 specific Ethernet driverMulti-threading support for mbed RTOSBerkeley sockets and integration with the New Socket APIFork of the apps running on top of the New Socket APIScheduling 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.
Diff: modules/pico_nat.c
- Revision:
- 152:a3d286bf94e5
- Parent:
- 149:5f4cb161cec3
--- a/modules/pico_nat.c Wed Apr 09 17:32:25 2014 +0200 +++ b/modules/pico_nat.c Mon Sep 28 13:16:18 2015 +0200 @@ -1,5 +1,5 @@ /********************************************************************* - PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved. + PicoTCP. Copyright (c) 2012-2015 Altran Intelligent Systems. Some rights reserved. See LICENSE and COPYING for usage. . @@ -44,48 +44,76 @@ static struct pico_ipv4_link *nat_link = NULL; -static int nat_cmp_inbound(void *ka, void *kb) +static int nat_cmp_natport(struct pico_nat_tuple *a, struct pico_nat_tuple *b) { - struct pico_nat_tuple *a = ka, *b = kb; if (a->nat_port < b->nat_port) return -1; if (a->nat_port > b->nat_port) + return 1; + return 0; + +} + +static int nat_cmp_srcport(struct pico_nat_tuple *a, struct pico_nat_tuple *b) +{ + + if (a->src_port < b->src_port) + return -1; + + if (a->src_port > b->src_port) + + return 1; + + return 0; + +} + +static int nat_cmp_proto(struct pico_nat_tuple *a, struct pico_nat_tuple *b) +{ if (a->proto < b->proto) return -1; if (a->proto > b->proto) return 1; - return 0; /* identical */ + return 0; +} + +static int nat_cmp_address(struct pico_nat_tuple *a, struct pico_nat_tuple *b) +{ + return pico_ipv4_compare(&a->src_addr, &b->src_addr); } +static int nat_cmp_inbound(void *ka, void *kb) +{ + struct pico_nat_tuple *a = ka, *b = kb; + int cport = nat_cmp_natport(a, b); + if (cport) + return cport; + + return nat_cmp_proto(a, b); +} + + static int nat_cmp_outbound(void *ka, void *kb) { struct pico_nat_tuple *a = ka, *b = kb; - - if (a->src_addr.addr < b->src_addr.addr) - return -1; + int caddr, cport; - if (a->src_addr.addr > b->src_addr.addr) - return 1; - - if (a->src_port < b->src_port) - return -1; + caddr = nat_cmp_address(a, b); + if (caddr) + return caddr; - if (a->src_port > b->src_port) - return 1; + cport = nat_cmp_srcport(a, b); - if (a->proto < b->proto) - return -1; + if (cport) + return cport; - if (a->proto > b->proto) - return 1; - - return 0; /* identical */ + return nat_cmp_proto(a, b); } PICO_TREE_DECLARE(NATOutbound, nat_cmp_outbound); @@ -203,6 +231,29 @@ return 0; } +static struct pico_trans *pico_nat_generate_tuple_trans(struct pico_ipv4_hdr *net, struct pico_frame *f) +{ + struct pico_trans *trans = NULL; + switch (net->proto) { + case PICO_PROTO_TCP: + { + struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr; + trans = (struct pico_trans *)&tcp->trans; + break; + } + case PICO_PROTO_UDP: + { + struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr; + trans = (struct pico_trans *)&udp->trans; + break; + } + case PICO_PROTO_ICMP4: + /* XXX: implement */ + break; + } + return trans; +} + static struct pico_nat_tuple *pico_ipv4_nat_generate_tuple(struct pico_frame *f) { struct pico_trans *trans = NULL; @@ -224,51 +275,38 @@ if (!retry) return NULL; - switch (net->proto) { - case PICO_PROTO_TCP: - { - struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr; - trans = (struct pico_trans *)&tcp->trans; - break; - } - case PICO_PROTO_UDP: - { - struct pico_udp_hdr *udp = (struct pico_udp_hdr *)f->transport_hdr; - trans = (struct pico_trans *)&udp->trans; - break; - } - case PICO_PROTO_ICMP4: - /* XXX: implement */ - break; - - default: + trans = pico_nat_generate_tuple_trans(net, f); + if(!trans) return NULL; - } return pico_ipv4_nat_add(net->dst, trans->dport, net->src, trans->sport, nat_link->address, nport, net->proto); /* XXX return pico_ipv4_nat_add(nat_link->address, port, net->src, trans->sport, net->proto); */ } -static int pico_ipv4_nat_snif_session(struct pico_nat_tuple *t, struct pico_frame *f, uint8_t direction) +static inline void pico_ipv4_nat_set_tcp_flags(struct pico_nat_tuple *t, struct pico_frame *f, uint8_t direction) +{ + struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr; + if (tcp->flags & PICO_TCP_SYN) + t->syn = 1; + + if (tcp->flags & PICO_TCP_RST) + t->rst = 1; + + if ((tcp->flags & PICO_TCP_FIN) && (direction == PICO_NAT_INBOUND)) + t->fin_in = 1; + + if ((tcp->flags & PICO_TCP_FIN) && (direction == PICO_NAT_OUTBOUND)) + t->fin_out = 1; +} + +static int pico_ipv4_nat_sniff_session(struct pico_nat_tuple *t, struct pico_frame *f, uint8_t direction) { struct pico_ipv4_hdr *net = (struct pico_ipv4_hdr *)f->net_hdr; switch (net->proto) { case PICO_PROTO_TCP: { - struct pico_tcp_hdr *tcp = (struct pico_tcp_hdr *)f->transport_hdr; - if (tcp->flags & PICO_TCP_SYN) - t->syn = 1; - - if (tcp->flags & PICO_TCP_RST) - t->rst = 1; - - if ((tcp->flags & PICO_TCP_FIN) && (direction == PICO_NAT_INBOUND)) - t->fin_in = 1; - - if ((tcp->flags & PICO_TCP_FIN) && (direction == PICO_NAT_OUTBOUND)) - t->fin_out = 1; - + pico_ipv4_nat_set_tcp_flags(t, f, direction); break; } @@ -430,7 +468,7 @@ return -1; } - pico_ipv4_nat_snif_session(tuple, f, PICO_NAT_INBOUND); + pico_ipv4_nat_sniff_session(tuple, f, PICO_NAT_INBOUND); net->crc = 0; net->crc = short_be(pico_checksum(net, f->net_len)); @@ -495,7 +533,7 @@ return -1; } - pico_ipv4_nat_snif_session(tuple, f, PICO_NAT_OUTBOUND); + pico_ipv4_nat_sniff_session(tuple, f, PICO_NAT_OUTBOUND); net->crc = 0; net->crc = short_be(pico_checksum(net, f->net_len));