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.cpp@57:e0fb648acf48, 2017-11-28 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Nov 28 17:05:46 2017 +0000
- Revision:
- 57:e0fb648acf48
- Parent:
- 52:fbc5a46b5e16
Added TFTP
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 52:fbc5a46b5e16 | 1 | #include "mbed.h" |
andrewboyson | 52:fbc5a46b5e16 | 2 | #include "log.h" |
andrewboyson | 52:fbc5a46b5e16 | 3 | #include "tcp.h" |
andrewboyson | 52:fbc5a46b5e16 | 4 | #include "tcb.h" |
andrewboyson | 52:fbc5a46b5e16 | 5 | #include "clock.h" |
andrewboyson | 52:fbc5a46b5e16 | 6 | |
andrewboyson | 10:f0854784e960 | 7 | #define TCB_COUNT 10 |
andrewboyson | 10:f0854784e960 | 8 | |
andrewboyson | 10:f0854784e960 | 9 | #define TIMEOUT_SYN_RECEIVED 2 |
andrewboyson | 10:f0854784e960 | 10 | #define TIMEOUT_ESTABLISHED 20 |
andrewboyson | 10:f0854784e960 | 11 | #define TIMEOUT_CLOSING 2 |
andrewboyson | 10:f0854784e960 | 12 | |
andrewboyson | 10:f0854784e960 | 13 | struct tcb tcbs[TCB_COUNT]; |
andrewboyson | 10:f0854784e960 | 14 | |
andrewboyson | 10:f0854784e960 | 15 | uint32_t TcbGetIsn() |
andrewboyson | 10:f0854784e960 | 16 | { |
andrewboyson | 10:f0854784e960 | 17 | static uint32_t isn = 0; |
andrewboyson | 10:f0854784e960 | 18 | isn += 100000; //Gives each tcb 100,000 packets and won't repeat before 42,940 tcbs. |
andrewboyson | 10:f0854784e960 | 19 | return isn; |
andrewboyson | 10:f0854784e960 | 20 | } |
andrewboyson | 10:f0854784e960 | 21 | struct tcb* TcbGetExisting(uint16_t port) |
andrewboyson | 10:f0854784e960 | 22 | { |
andrewboyson | 10:f0854784e960 | 23 | for (int i = 0; i < TCB_COUNT; i++) |
andrewboyson | 10:f0854784e960 | 24 | { |
andrewboyson | 10:f0854784e960 | 25 | struct tcb* pTcb = tcbs + i; |
andrewboyson | 57:e0fb648acf48 | 26 | if (pTcb->state != TCB_EMPTY && pTcb->port == port) return pTcb; |
andrewboyson | 10:f0854784e960 | 27 | } |
andrewboyson | 10:f0854784e960 | 28 | return NULL; |
andrewboyson | 10:f0854784e960 | 29 | } |
andrewboyson | 10:f0854784e960 | 30 | struct tcb* TcbGetEmpty() |
andrewboyson | 10:f0854784e960 | 31 | { |
andrewboyson | 10:f0854784e960 | 32 | for (int i = 0; i < TCB_COUNT; i++) |
andrewboyson | 10:f0854784e960 | 33 | { |
andrewboyson | 10:f0854784e960 | 34 | struct tcb* pTcb = tcbs + i; |
andrewboyson | 57:e0fb648acf48 | 35 | if (pTcb->state == TCB_EMPTY) return pTcb; |
andrewboyson | 10:f0854784e960 | 36 | } |
andrewboyson | 10:f0854784e960 | 37 | return NULL; |
andrewboyson | 10:f0854784e960 | 38 | } |
andrewboyson | 10:f0854784e960 | 39 | |
andrewboyson | 52:fbc5a46b5e16 | 40 | uint32_t TcbElapsed = 0; |
andrewboyson | 52:fbc5a46b5e16 | 41 | static void reap() |
andrewboyson | 10:f0854784e960 | 42 | { |
andrewboyson | 52:fbc5a46b5e16 | 43 | static struct tcb* pTcb = tcbs; |
andrewboyson | 52:fbc5a46b5e16 | 44 | |
andrewboyson | 57:e0fb648acf48 | 45 | if (pTcb->state == TCB_EMPTY) return; |
andrewboyson | 52:fbc5a46b5e16 | 46 | |
andrewboyson | 52:fbc5a46b5e16 | 47 | uint32_t limit; |
andrewboyson | 52:fbc5a46b5e16 | 48 | switch (pTcb->state) |
andrewboyson | 10:f0854784e960 | 49 | { |
andrewboyson | 52:fbc5a46b5e16 | 50 | case TCB_SYN_RECEIVED: limit = TIMEOUT_SYN_RECEIVED; break; |
andrewboyson | 52:fbc5a46b5e16 | 51 | case TCB_ESTABLISHED: limit = TIMEOUT_ESTABLISHED; break; |
andrewboyson | 57:e0fb648acf48 | 52 | case TCB_FIN_WAIT: limit = TIMEOUT_CLOSING; break; |
andrewboyson | 57:e0fb648acf48 | 53 | case TCB_ACK_WAIT: limit = TIMEOUT_CLOSING; break; |
andrewboyson | 52:fbc5a46b5e16 | 54 | } |
andrewboyson | 52:fbc5a46b5e16 | 55 | |
andrewboyson | 52:fbc5a46b5e16 | 56 | if (TcbElapsed - pTcb->elapsed > limit) |
andrewboyson | 52:fbc5a46b5e16 | 57 | { |
andrewboyson | 52:fbc5a46b5e16 | 58 | if (TcpTrace) LogTimeF("Reaping TCB %d port %d\r\n", pTcb - tcbs, pTcb->port); |
andrewboyson | 57:e0fb648acf48 | 59 | pTcb->state = TCB_EMPTY; |
andrewboyson | 52:fbc5a46b5e16 | 60 | } |
andrewboyson | 52:fbc5a46b5e16 | 61 | |
andrewboyson | 52:fbc5a46b5e16 | 62 | pTcb++; |
andrewboyson | 52:fbc5a46b5e16 | 63 | if (pTcb >= tcbs + TCB_COUNT) pTcb = tcbs; |
andrewboyson | 52:fbc5a46b5e16 | 64 | } |
andrewboyson | 52:fbc5a46b5e16 | 65 | |
andrewboyson | 52:fbc5a46b5e16 | 66 | void TcbMain() |
andrewboyson | 52:fbc5a46b5e16 | 67 | { |
andrewboyson | 52:fbc5a46b5e16 | 68 | if (ClockTicked) TcbElapsed++; |
andrewboyson | 52:fbc5a46b5e16 | 69 | reap(); |
andrewboyson | 10:f0854784e960 | 70 | } |
andrewboyson | 10:f0854784e960 | 71 | void TcbInit() |
andrewboyson | 10:f0854784e960 | 72 | { |
andrewboyson | 57:e0fb648acf48 | 73 | for (int i = 0; i < TCB_COUNT; i++) tcbs[i].state = TCB_EMPTY; |
andrewboyson | 52:fbc5a46b5e16 | 74 | } |