A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.
Dependents: oldheating gps motorhome heating
tcp/tcb.c@65:37acccf2752f, 2018-01-18 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Jan 18 18:12:43 2018 +0000
- Revision:
- 65:37acccf2752f
- Parent:
- 61:aad055f1b0d1
- Child:
- 66:18a10c0b6d93
Updated with Clock tick changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdint.h> |
andrewboyson | 61:aad055f1b0d1 | 2 | |
andrewboyson | 65:37acccf2752f | 3 | #include "log.h" |
andrewboyson | 65:37acccf2752f | 4 | #include "tcp.h" |
andrewboyson | 65:37acccf2752f | 5 | #include "tcb.h" |
andrewboyson | 65:37acccf2752f | 6 | #include "tick.h" |
andrewboyson | 52:fbc5a46b5e16 | 7 | |
andrewboyson | 10:f0854784e960 | 8 | #define TCB_COUNT 10 |
andrewboyson | 10:f0854784e960 | 9 | |
andrewboyson | 10:f0854784e960 | 10 | #define TIMEOUT_SYN_RECEIVED 2 |
andrewboyson | 10:f0854784e960 | 11 | #define TIMEOUT_ESTABLISHED 20 |
andrewboyson | 10:f0854784e960 | 12 | #define TIMEOUT_CLOSING 2 |
andrewboyson | 10:f0854784e960 | 13 | |
andrewboyson | 10:f0854784e960 | 14 | struct tcb tcbs[TCB_COUNT]; |
andrewboyson | 10:f0854784e960 | 15 | |
andrewboyson | 10:f0854784e960 | 16 | uint32_t TcbGetIsn() |
andrewboyson | 10:f0854784e960 | 17 | { |
andrewboyson | 10:f0854784e960 | 18 | static uint32_t isn = 0; |
andrewboyson | 10:f0854784e960 | 19 | isn += 100000; //Gives each tcb 100,000 packets and won't repeat before 42,940 tcbs. |
andrewboyson | 10:f0854784e960 | 20 | return isn; |
andrewboyson | 10:f0854784e960 | 21 | } |
andrewboyson | 10:f0854784e960 | 22 | struct tcb* TcbGetExisting(uint16_t port) |
andrewboyson | 10:f0854784e960 | 23 | { |
andrewboyson | 10:f0854784e960 | 24 | for (int i = 0; i < TCB_COUNT; i++) |
andrewboyson | 10:f0854784e960 | 25 | { |
andrewboyson | 10:f0854784e960 | 26 | struct tcb* pTcb = tcbs + i; |
andrewboyson | 57:e0fb648acf48 | 27 | if (pTcb->state != TCB_EMPTY && pTcb->port == port) return pTcb; |
andrewboyson | 10:f0854784e960 | 28 | } |
andrewboyson | 10:f0854784e960 | 29 | return NULL; |
andrewboyson | 10:f0854784e960 | 30 | } |
andrewboyson | 10:f0854784e960 | 31 | struct tcb* TcbGetEmpty() |
andrewboyson | 10:f0854784e960 | 32 | { |
andrewboyson | 10:f0854784e960 | 33 | for (int i = 0; i < TCB_COUNT; i++) |
andrewboyson | 10:f0854784e960 | 34 | { |
andrewboyson | 10:f0854784e960 | 35 | struct tcb* pTcb = tcbs + i; |
andrewboyson | 57:e0fb648acf48 | 36 | if (pTcb->state == TCB_EMPTY) return pTcb; |
andrewboyson | 10:f0854784e960 | 37 | } |
andrewboyson | 10:f0854784e960 | 38 | return NULL; |
andrewboyson | 10:f0854784e960 | 39 | } |
andrewboyson | 10:f0854784e960 | 40 | |
andrewboyson | 52:fbc5a46b5e16 | 41 | uint32_t TcbElapsed = 0; |
andrewboyson | 52:fbc5a46b5e16 | 42 | static void reap() |
andrewboyson | 10:f0854784e960 | 43 | { |
andrewboyson | 52:fbc5a46b5e16 | 44 | static struct tcb* pTcb = tcbs; |
andrewboyson | 52:fbc5a46b5e16 | 45 | |
andrewboyson | 57:e0fb648acf48 | 46 | if (pTcb->state == TCB_EMPTY) return; |
andrewboyson | 52:fbc5a46b5e16 | 47 | |
andrewboyson | 52:fbc5a46b5e16 | 48 | uint32_t limit; |
andrewboyson | 52:fbc5a46b5e16 | 49 | switch (pTcb->state) |
andrewboyson | 10:f0854784e960 | 50 | { |
andrewboyson | 52:fbc5a46b5e16 | 51 | case TCB_SYN_RECEIVED: limit = TIMEOUT_SYN_RECEIVED; break; |
andrewboyson | 52:fbc5a46b5e16 | 52 | case TCB_ESTABLISHED: limit = TIMEOUT_ESTABLISHED; break; |
andrewboyson | 57:e0fb648acf48 | 53 | case TCB_FIN_WAIT: limit = TIMEOUT_CLOSING; break; |
andrewboyson | 57:e0fb648acf48 | 54 | case TCB_ACK_WAIT: limit = TIMEOUT_CLOSING; break; |
andrewboyson | 52:fbc5a46b5e16 | 55 | } |
andrewboyson | 52:fbc5a46b5e16 | 56 | |
andrewboyson | 52:fbc5a46b5e16 | 57 | if (TcbElapsed - pTcb->elapsed > limit) |
andrewboyson | 52:fbc5a46b5e16 | 58 | { |
andrewboyson | 52:fbc5a46b5e16 | 59 | if (TcpTrace) LogTimeF("Reaping TCB %d port %d\r\n", pTcb - tcbs, pTcb->port); |
andrewboyson | 57:e0fb648acf48 | 60 | pTcb->state = TCB_EMPTY; |
andrewboyson | 52:fbc5a46b5e16 | 61 | } |
andrewboyson | 52:fbc5a46b5e16 | 62 | |
andrewboyson | 52:fbc5a46b5e16 | 63 | pTcb++; |
andrewboyson | 52:fbc5a46b5e16 | 64 | if (pTcb >= tcbs + TCB_COUNT) pTcb = tcbs; |
andrewboyson | 52:fbc5a46b5e16 | 65 | } |
andrewboyson | 52:fbc5a46b5e16 | 66 | |
andrewboyson | 52:fbc5a46b5e16 | 67 | void TcbMain() |
andrewboyson | 52:fbc5a46b5e16 | 68 | { |
andrewboyson | 65:37acccf2752f | 69 | if (TickTicked) TcbElapsed++; |
andrewboyson | 52:fbc5a46b5e16 | 70 | reap(); |
andrewboyson | 10:f0854784e960 | 71 | } |
andrewboyson | 10:f0854784e960 | 72 | void TcbInit() |
andrewboyson | 10:f0854784e960 | 73 | { |
andrewboyson | 57:e0fb648acf48 | 74 | for (int i = 0; i < TCB_COUNT; i++) tcbs[i].state = TCB_EMPTY; |
andrewboyson | 52:fbc5a46b5e16 | 75 | } |