embkernel
 All Classes Functions Variables Typedefs Groups Pages
NetTcp.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #ifndef NET_TCP_HPP_
7 #define NET_TCP_HPP_
8 
9 #include "NetDefs.hpp"
10 #include "NetTcpSocket.hpp"
11 #include "LibLinkedList.hpp"
12 #include "RtosSemaphore.hpp"
13 
14 class NetTcpSocket;
15 
16 class NetTcp {
17  friend class Net;
18  friend class NetIp;
19  friend class NetTcpSocket;
20 private:
21 
22  const static int TIMER_SYN_SENT = 300;
23  const static int TIMER_SYN_RCVD = 300;
24  const static int TIMER_EST_KEEP_ALIVE = 1000;
25  const static int TIMER_SEGMENT_RETRY = 100;
26  const static int TIMER_SEGMENT_FLUSH = 10;
27  const static int TIMER_LAST_ACK = 100;
28  const static int TIMER_FIN_WAIT_1 = 100;
29  const static int TIMER_FIN_WAIT_2 = 100;
30  const static int TIMER_CLOSING = 100;
31  const static int TIMER_TIME_WAIT = 100;
32 
33  const static int MAX_RETRIES_SYN_SENT = 3;
34  const static int MAX_RETRIES_SYN_RCVD = 3;
35  const static int MAX_RETRIES_EST_KEEP_ALIVE = 3;
36  const static int MAX_RETRIES_SEGMENT_RETRY = 5;
37  const static int MAX_RETRIES_LAST_ACK = 3;
38  const static int MAX_RETRIES_FIN_WAIT_1 = 5;
39  const static int MAX_RETRIES_FIN_WAIT_2 = 3;
40  const static int MAX_RETRIES_CLOSING = 3;
41 
42  const static uint8_t OPTIONS_END_OF_LIST = 0x00;
43  const static uint8_t OPTIONS_NO_OP = 0x01;
44  const static uint8_t OPTIONS_MAX_SEG_SIZE = 0x02;
45 
46  static void processFrame(NetDefs::FRAME& frame, NetDefs::TCP_HEADER& header, int len);
47  static bool onSegmentReceived(NetDefs::FRAME& frame, NetDefs::TCP_HEADER& header, NetTcpSocket& s, int len);
48  static void sendControlPacket(NetTcpSocket& s, uint8_t flags);
49  static void swapHeader(NetDefs::TCP_HEADER& header);
50  static void tick();
51  static void putHeader(NetDefs::FRAME& frame, NetTcpSocket& s, uint32_t seq, uint32_t checkSum, int dataLen, uint8_t flags);
52 
53  static LibLinkedList<NetTcpSocket> sSocketList;
54  static RtosSemaphore sSema;
55 
56  static NetTcpSocket* findMatchingSocket(NetDefs::TCP_HEADER& header, NetDefs::IP_ADDR& remoteIpAddr);
57  static void lock();
58  static void unlock();
59  static void addSocket(NetTcpSocket& socket);
60  static void removeSocket(NetTcpSocket& socket);
61  static void flushSegment(NetTcpSocket& s, NetTcpSocket::TX_SEGMENT& segment);
62 
63 public:
64  typedef struct {
65  NetDefs::IP_ADDR remoteIpAddr;
66  uint16_t localPort;
67  uint16_t remotePort;
68  NetTcpSocket::STATE state;
69  uint8_t spare[3];
70  }__attribute__((packed)) SOCKET_INFO;
71  static int getTcpSocketList(SOCKET_INFO* buffer, int count);
72 };
73 
74 // +-----------------+ rcv-FIN
75 // | CLOSE-WAIT | <---------------------+
76 // +-----------------+ |
77 // | |
78 // | CLOSE |
79 // v |
80 // +-----------------+ |
81 // | LAST-ACK | |
82 // +-----------------+ |
83 // | |
84 // | rcv-ACK-of-FIN |
85 // v |
86 // +--------------------------------------+ |
87 // +-----------> | CLOSED | <+-----------+
88 // | +--------------------------------------+ | |
89 // | | | | |
90 // | CLOSE | passive-OPEN | | |
91 // | v | | |
92 // | +-----------------+ | | |
93 // +------------ | LISTEN | -+---------------+ | |
94 // +-----------------+ | | | |
95 // | | | | |
96 // | SEND | active-OPEN | | |
97 // v | | | |
98 // +-----------------+ | | | |
99 // +------------ | SYN-SENT | <+ | | |
100 // | +-----------------+ | | |
101 // | | | | |
102 // | | rcv-SYN | | |
103 // | v | | |
104 // | +-----------------+ rcv-SYN | | |
105 // +-----+------------ | SYN-RCVD | <----------------+ | |
106 // | | +-----------------+ | |
107 // | | | | |
108 // | | rcv-SYN-ACK | rcv-ACK-of-SYN | |
109 // | | v | |
110 // | | +-----------------+ | |
111 // | +-----------> | ESTAB | ----------------------+ |
112 // | +-----------------+ |
113 // | | |
114 // | | CLOSE +-----------+
115 // | v |
116 // +------------+ | CLOSE +-----------------+ |
117 // +--------------> | FIN-WAIT-2 | +-----------------> | FIN-WAIT-1 | -----------------+ |
118 // | +------------+ +-----------------+ | |
119 // | | | | |
120 // | | | rcv-FIN | |
121 // | | v | |
122 // | | +-----------------+ | |
123 // | | | CLOSING | | |
124 // | | +-----------------+ | |
125 // | rcv-ACK-of-FIN | | | |
126 // | | | rcv-ACK-of-FIN | |
127 // | | v | |
128 // | | rcv-FIN +-----------------+ | |
129 // | +-------------------------------------> | TIME-WAIT | -----------------+----+
130 // | +-----------------+ |
131 // | |
132 // +-----------------------------------------------------------------------------------------------+
133 
134 #endif /* NET_TCP_HPP_ */