Fixed RNDISEthernet demos crashing when calculating checksums for Ethernet/TCP packet...
[pub/USBasp.git] / Demos / Device / LowLevel / RNDISEthernet / Lib / TCP.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Transmission Control Protocol (TCP) packet handling routines. This protocol handles the reliable in-order transmission
34 * and reception of packets to and from devices on a network, to "ports" on the device. It is used in situations where data
35 * delivery must be reliable and correct, e.g. HTTP, TELNET and most other non-streaming protocols.
36 */
37
38 #define INCLUDE_FROM_TCP_C
39 #include "TCP.h"
40
41 /** Port state table array. This contains the current status of TCP ports in the device. To save on space, only open ports are
42 * stored - closed ports may be overwritten at any time, and the system will assume any ports not present in the array are closed. This
43 * allows for MAX_OPEN_TCP_PORTS to be less than the number of ports used by the application if desired.
44 */
45 TCP_PortState_t PortStateTable[MAX_OPEN_TCP_PORTS];
46
47 /** Connection state table array. This contains the current status of TCP connections in the device. To save on space, only active
48 * (non-closed) connections are stored - closed connections may be overwritten at any time, and the system will assume any connections
49 * not present in the array are closed.
50 */
51 TCP_ConnectionState_t ConnectionStateTable[MAX_TCP_CONNECTIONS];
52
53
54 /** Task to handle the calling of each registered application's callback function, to process and generate TCP packets at the application
55 * level. If an application produces a response, this task constructs the appropriate Ethernet frame and places it into the Ethernet OUT
56 * buffer for later transmission.
57 */
58 void TCP_Task(void)
59 {
60 /* Run each application in sequence, to process incoming and generate outgoing packets */
61 for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
62 {
63 /* Find the corresponding port entry in the port table */
64 for (uint8_t PTableEntry = 0; PTableEntry < MAX_OPEN_TCP_PORTS; PTableEntry++)
65 {
66 /* Run the application handler for the port */
67 if ((PortStateTable[PTableEntry].Port == ConnectionStateTable[CSTableEntry].Port) &&
68 (PortStateTable[PTableEntry].State == TCP_Port_Open))
69 {
70 PortStateTable[PTableEntry].ApplicationHandler(&ConnectionStateTable[CSTableEntry],
71 &ConnectionStateTable[CSTableEntry].Info.Buffer);
72 }
73 }
74 }
75
76 /* Bail out early if there is already a frame waiting to be sent in the Ethernet OUT buffer */
77 if (FrameOUT.FrameInBuffer)
78 return;
79
80 /* Send response packets from each application as the TCP packet buffers are filled by the applications */
81 for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
82 {
83 /* For each completely received packet, pass it along to the listening application */
84 if ((ConnectionStateTable[CSTableEntry].Info.Buffer.Direction == TCP_PACKETDIR_OUT) &&
85 (ConnectionStateTable[CSTableEntry].Info.Buffer.Ready))
86 {
87 Ethernet_Frame_Header_t* FrameOUTHeader = (Ethernet_Frame_Header_t*)&FrameOUT.FrameData;
88 IP_Header_t* IPHeaderOUT = (IP_Header_t*)&FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t)];
89 TCP_Header_t* TCPHeaderOUT = (TCP_Header_t*)&FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t) +
90 sizeof(IP_Header_t)];
91 void* TCPDataOUT = &FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t) +
92 sizeof(IP_Header_t) +
93 sizeof(TCP_Header_t)];
94
95 uint16_t PacketSize = ConnectionStateTable[CSTableEntry].Info.Buffer.Length;
96
97 /* Fill out the TCP data */
98 TCPHeaderOUT->SourcePort = ConnectionStateTable[CSTableEntry].Port;
99 TCPHeaderOUT->DestinationPort = ConnectionStateTable[CSTableEntry].RemotePort;
100 TCPHeaderOUT->SequenceNumber = SwapEndian_32(ConnectionStateTable[CSTableEntry].Info.SequenceNumberOut);
101 TCPHeaderOUT->AcknowledgmentNumber = SwapEndian_32(ConnectionStateTable[CSTableEntry].Info.SequenceNumberIn);
102 TCPHeaderOUT->DataOffset = (sizeof(TCP_Header_t) / sizeof(uint32_t));
103 TCPHeaderOUT->WindowSize = SwapEndian_16(TCP_WINDOW_SIZE);
104
105 TCPHeaderOUT->Flags = TCP_FLAG_ACK;
106 TCPHeaderOUT->UrgentPointer = 0;
107 TCPHeaderOUT->Checksum = 0;
108 TCPHeaderOUT->Reserved = 0;
109
110 memcpy(TCPDataOUT, ConnectionStateTable[CSTableEntry].Info.Buffer.Data, PacketSize);
111
112 ConnectionStateTable[CSTableEntry].Info.SequenceNumberOut += PacketSize;
113
114 TCPHeaderOUT->Checksum = TCP_Checksum16(TCPHeaderOUT, ServerIPAddress,
115 ConnectionStateTable[CSTableEntry].RemoteAddress,
116 (sizeof(TCP_Header_t) + PacketSize));
117
118 PacketSize += sizeof(TCP_Header_t);
119
120 /* Fill out the response IP header */
121 IPHeaderOUT->TotalLength = SwapEndian_16(sizeof(IP_Header_t) + PacketSize);
122 IPHeaderOUT->TypeOfService = 0;
123 IPHeaderOUT->HeaderLength = (sizeof(IP_Header_t) / sizeof(uint32_t));
124 IPHeaderOUT->Version = 4;
125 IPHeaderOUT->Flags = 0;
126 IPHeaderOUT->FragmentOffset = 0;
127 IPHeaderOUT->Identification = 0;
128 IPHeaderOUT->HeaderChecksum = 0;
129 IPHeaderOUT->Protocol = PROTOCOL_TCP;
130 IPHeaderOUT->TTL = DEFAULT_TTL;
131 IPHeaderOUT->SourceAddress = ServerIPAddress;
132 IPHeaderOUT->DestinationAddress = ConnectionStateTable[CSTableEntry].RemoteAddress;
133
134 IPHeaderOUT->HeaderChecksum = Ethernet_Checksum16(IPHeaderOUT, sizeof(IP_Header_t));
135
136 PacketSize += sizeof(IP_Header_t);
137
138 /* Fill out the response Ethernet frame header */
139 FrameOUTHeader->Source = ServerMACAddress;
140 FrameOUTHeader->Destination = (MAC_Address_t){{0x02, 0x00, 0x02, 0x00, 0x02, 0x00}};
141 FrameOUTHeader->EtherType = SwapEndian_16(ETHERTYPE_IPV4);
142
143 PacketSize += sizeof(Ethernet_Frame_Header_t);
144
145 /* Set the response length in the buffer and indicate that a response is ready to be sent */
146 FrameOUT.FrameLength = PacketSize;
147 FrameOUT.FrameInBuffer = true;
148
149 ConnectionStateTable[CSTableEntry].Info.Buffer.Ready = false;
150
151 break;
152 }
153 }
154 }
155
156 /** Initializes the TCP protocol handler, clearing the port and connection state tables. This must be called before TCP packets are
157 * processed.
158 */
159 void TCP_Init(void)
160 {
161 /* Initialize the port state table with all CLOSED entries */
162 for (uint8_t PTableEntry = 0; PTableEntry < MAX_OPEN_TCP_PORTS; PTableEntry++)
163 PortStateTable[PTableEntry].State = TCP_Port_Closed;
164
165 /* Initialize the connection table with all CLOSED entries */
166 for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
167 ConnectionStateTable[CSTableEntry].State = TCP_Connection_Closed;
168 }
169
170 /** Sets the state and callback handler of the given port, specified in big endian to the given state.
171 *
172 * \param[in] Port Port whose state and callback function to set, specified in big endian
173 * \param[in] State New state of the port, a value from the TCP_PortStates_t enum
174 * \param[in] Handler Application callback handler for the port
175 *
176 * \return Boolean true if the port state was set, false otherwise (no more space in the port state table)
177 */
178 bool TCP_SetPortState(uint16_t Port, uint8_t State, void (*Handler)(TCP_ConnectionState_t*, TCP_ConnectionBuffer_t*))
179 {
180 /* Note, Port number should be specified in BIG endian to simplify network code */
181
182 /* Check to see if the port entry is already in the port state table */
183 for (uint8_t PTableEntry = 0; PTableEntry < MAX_TCP_CONNECTIONS; PTableEntry++)
184 {
185 /* Find existing entry for the port in the table, update it if found */
186 if (PortStateTable[PTableEntry].Port == Port)
187 {
188 PortStateTable[PTableEntry].State = State;
189 PortStateTable[PTableEntry].ApplicationHandler = Handler;
190 return true;
191 }
192 }
193
194 /* Check if trying to open the port -- if so we need to find an unused (closed) entry and replace it */
195 if (State == TCP_Port_Open)
196 {
197 for (uint8_t PTableEntry = 0; PTableEntry < MAX_TCP_CONNECTIONS; PTableEntry++)
198 {
199 /* Find a closed port entry in the table, change it to the given port and state */
200 if (PortStateTable[PTableEntry].State == TCP_Port_Closed)
201 {
202 PortStateTable[PTableEntry].Port = Port;
203 PortStateTable[PTableEntry].State = State;
204 PortStateTable[PTableEntry].ApplicationHandler = Handler;
205 return true;
206 }
207 }
208
209 /* Port not in table and no room to add it, return failure */
210 return false;
211 }
212 else
213 {
214 /* Port not in table but trying to close it, so operation successful */
215 return true;
216 }
217 }
218
219 /** Retrieves the current state of a given TCP port, specified in big endian.
220 *
221 * \param[in] Port TCP port whose state is to be retrieved, given in big-endian
222 *
223 * \return A value from the TCP_PortStates_t enum
224 */
225 uint8_t TCP_GetPortState(uint16_t Port)
226 {
227 /* Note, Port number should be specified in BIG endian to simplify network code */
228
229 for (uint8_t PTableEntry = 0; PTableEntry < MAX_TCP_CONNECTIONS; PTableEntry++)
230 {
231 /* Find existing entry for the port in the table, return the port status if found */
232 if (PortStateTable[PTableEntry].Port == Port)
233 return PortStateTable[PTableEntry].State;
234 }
235
236 /* Port not in table, assume closed */
237 return TCP_Port_Closed;
238 }
239
240 /** Sets the connection state of the given port, remote address and remote port to the given TCP connection state. If the
241 * connection exists in the connection state table it is updated, otherwise it is created if possible.
242 *
243 * \param[in] Port TCP port of the connection on the device, specified in big endian
244 * \param[in] RemoteAddress Remote protocol IP address of the connected device
245 * \param[in] RemotePort TCP port of the remote device in the connection, specified in big endian
246 * \param[in] State TCP connection state, a value from the TCP_ConnectionStates_t enum
247 *
248 * \return Boolean true if the connection was updated or created, false otherwise (no more space in the connection state table)
249 */
250 bool TCP_SetConnectionState(uint16_t Port, IP_Address_t RemoteAddress, uint16_t RemotePort, uint8_t State)
251 {
252 /* Note, Port number should be specified in BIG endian to simplify network code */
253
254 for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
255 {
256 /* Find port entry in the table */
257 if ((ConnectionStateTable[CSTableEntry].Port == Port) &&
258 IP_COMPARE(&ConnectionStateTable[CSTableEntry].RemoteAddress, &RemoteAddress) &&
259 ConnectionStateTable[CSTableEntry].RemotePort == RemotePort)
260 {
261 ConnectionStateTable[CSTableEntry].State = State;
262 return true;
263 }
264 }
265
266 for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
267 {
268 /* Find empty entry in the table */
269 if (ConnectionStateTable[CSTableEntry].State == TCP_Connection_Closed)
270 {
271 ConnectionStateTable[CSTableEntry].Port = Port;
272 ConnectionStateTable[CSTableEntry].RemoteAddress = RemoteAddress;
273 ConnectionStateTable[CSTableEntry].RemotePort = RemotePort;
274 ConnectionStateTable[CSTableEntry].State = State;
275 return true;
276 }
277 }
278
279 return false;
280 }
281
282 /** Retrieves the current state of a given TCP connection to a host.
283 *
284 * \param[in] Port TCP port on the device in the connection, specified in big endian
285 * \param[in] RemoteAddress Remote protocol IP address of the connected host
286 * \param[in] RemotePort Remote TCP port of the connected host, specified in big endian
287 *
288 * \return A value from the TCP_ConnectionStates_t enum
289 */
290 uint8_t TCP_GetConnectionState(uint16_t Port, IP_Address_t RemoteAddress, uint16_t RemotePort)
291 {
292 /* Note, Port number should be specified in BIG endian to simplify network code */
293
294 for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
295 {
296 /* Find port entry in the table */
297 if ((ConnectionStateTable[CSTableEntry].Port == Port) &&
298 IP_COMPARE(&ConnectionStateTable[CSTableEntry].RemoteAddress, &RemoteAddress) &&
299 ConnectionStateTable[CSTableEntry].RemotePort == RemotePort)
300
301 {
302 return ConnectionStateTable[CSTableEntry].State;
303 }
304 }
305
306 return TCP_Connection_Closed;
307 }
308
309 /** Retrieves the connection info structure of a given connection to a host.
310 *
311 * \param[in] Port TCP port on the device in the connection, specified in big endian
312 * \param[in] RemoteAddress Remote protocol IP address of the connected host
313 * \param[in] RemotePort Remote TCP port of the connected host, specified in big endian
314 *
315 * \return ConnectionInfo structure of the connection if found, NULL otherwise
316 */
317 TCP_ConnectionInfo_t* TCP_GetConnectionInfo(uint16_t Port, IP_Address_t RemoteAddress, uint16_t RemotePort)
318 {
319 /* Note, Port number should be specified in BIG endian to simplify network code */
320
321 for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
322 {
323 /* Find port entry in the table */
324 if ((ConnectionStateTable[CSTableEntry].Port == Port) &&
325 IP_COMPARE(&ConnectionStateTable[CSTableEntry].RemoteAddress, &RemoteAddress) &&
326 ConnectionStateTable[CSTableEntry].RemotePort == RemotePort)
327 {
328 return &ConnectionStateTable[CSTableEntry].Info;
329 }
330 }
331
332 return NULL;
333 }
334
335 /** Processes a TCP packet inside an Ethernet frame, and writes the appropriate response
336 * to the output Ethernet frame if one is created by a application handler.
337 *
338 * \param[in] IPHeaderInStart Pointer to the start of the incoming packet's IP header
339 * \param[in] TCPHeaderInStart Pointer to the start of the incoming packet's TCP header
340 * \param[out] TCPHeaderOutStart Pointer to the start of the outgoing packet's TCP header
341 *
342 * \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE if no
343 * response was generated, NO_PROCESS if the packet processing was deferred until the
344 * next Ethernet packet handler iteration
345 */
346 int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart, void* TCPHeaderInStart, void* TCPHeaderOutStart)
347 {
348 IP_Header_t* IPHeaderIN = (IP_Header_t*)IPHeaderInStart;
349 TCP_Header_t* TCPHeaderIN = (TCP_Header_t*)TCPHeaderInStart;
350 TCP_Header_t* TCPHeaderOUT = (TCP_Header_t*)TCPHeaderOutStart;
351
352 TCP_ConnectionInfo_t* ConnectionInfo;
353
354 DecodeTCPHeader(TCPHeaderInStart);
355
356 bool PacketResponse = false;
357
358 /* Check if the destination port is open and allows incoming connections */
359 if (TCP_GetPortState(TCPHeaderIN->DestinationPort) == TCP_Port_Open)
360 {
361 /* Detect SYN from host to start a connection */
362 if (TCPHeaderIN->Flags & TCP_FLAG_SYN)
363 TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort, TCP_Connection_Listen);
364
365 /* Detect RST from host to abort existing connection */
366 if (TCPHeaderIN->Flags & TCP_FLAG_RST)
367 {
368 if (TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
369 TCPHeaderIN->SourcePort, TCP_Connection_Closed))
370 {
371 TCPHeaderOUT->Flags = (TCP_FLAG_RST | TCP_FLAG_ACK);
372 PacketResponse = true;
373 }
374 }
375 else
376 {
377 /* Process the incoming TCP packet based on the current connection state for the sender and port */
378 switch (TCP_GetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort))
379 {
380 case TCP_Connection_Listen:
381 if (TCPHeaderIN->Flags == TCP_FLAG_SYN)
382 {
383 /* SYN connection starts a connection with a peer */
384 if (TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
385 TCPHeaderIN->SourcePort, TCP_Connection_SYNReceived))
386 {
387 TCPHeaderOUT->Flags = (TCP_FLAG_SYN | TCP_FLAG_ACK);
388
389 ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort);
390
391 ConnectionInfo->SequenceNumberIn = (SwapEndian_32(TCPHeaderIN->SequenceNumber) + 1);
392 ConnectionInfo->SequenceNumberOut = 0;
393 ConnectionInfo->Buffer.InUse = false;
394 }
395 else
396 {
397 TCPHeaderOUT->Flags = TCP_FLAG_RST;
398 }
399
400 PacketResponse = true;
401 }
402
403 break;
404 case TCP_Connection_SYNReceived:
405 if (TCPHeaderIN->Flags == TCP_FLAG_ACK)
406 {
407 /* ACK during the connection process completes the connection to a peer */
408
409 TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
410 TCPHeaderIN->SourcePort, TCP_Connection_Established);
411
412 ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
413 TCPHeaderIN->SourcePort);
414
415 ConnectionInfo->SequenceNumberOut++;
416 }
417
418 break;
419 case TCP_Connection_Established:
420 if (TCPHeaderIN->Flags == (TCP_FLAG_FIN | TCP_FLAG_ACK))
421 {
422 /* FIN ACK when connected to a peer starts the finalization process */
423
424 TCPHeaderOUT->Flags = (TCP_FLAG_FIN | TCP_FLAG_ACK);
425 PacketResponse = true;
426
427 TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
428 TCPHeaderIN->SourcePort, TCP_Connection_CloseWait);
429
430 ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
431 TCPHeaderIN->SourcePort);
432
433 ConnectionInfo->SequenceNumberIn++;
434 ConnectionInfo->SequenceNumberOut++;
435 }
436 else if ((TCPHeaderIN->Flags == TCP_FLAG_ACK) || (TCPHeaderIN->Flags == (TCP_FLAG_ACK | TCP_FLAG_PSH)))
437 {
438 ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
439 TCPHeaderIN->SourcePort);
440
441 /* Check if the buffer is currently in use either by a buffered data to send, or receive */
442 if ((ConnectionInfo->Buffer.InUse == false) && (ConnectionInfo->Buffer.Ready == false))
443 {
444 ConnectionInfo->Buffer.Direction = TCP_PACKETDIR_IN;
445 ConnectionInfo->Buffer.InUse = true;
446 ConnectionInfo->Buffer.Length = 0;
447 }
448
449 /* Check if the buffer has been claimed by us to read in data from the peer */
450 if ((ConnectionInfo->Buffer.Direction == TCP_PACKETDIR_IN) &&
451 (ConnectionInfo->Buffer.Length != TCP_WINDOW_SIZE))
452 {
453 uint16_t IPOffset = (IPHeaderIN->HeaderLength * sizeof(uint32_t));
454 uint16_t TCPOffset = (TCPHeaderIN->DataOffset * sizeof(uint32_t));
455 uint16_t DataLength = (SwapEndian_16(IPHeaderIN->TotalLength) - IPOffset - TCPOffset);
456
457 /* Copy the packet data into the buffer */
458 memcpy(&ConnectionInfo->Buffer.Data[ConnectionInfo->Buffer.Length],
459 &((uint8_t*)TCPHeaderInStart)[TCPOffset],
460 DataLength);
461
462 ConnectionInfo->SequenceNumberIn += DataLength;
463 ConnectionInfo->Buffer.Length += DataLength;
464
465 /* Check if the buffer is full or if the PSH flag is set, if so indicate buffer ready */
466 if ((!(TCP_WINDOW_SIZE - ConnectionInfo->Buffer.Length)) || (TCPHeaderIN->Flags & TCP_FLAG_PSH))
467 {
468 ConnectionInfo->Buffer.InUse = false;
469 ConnectionInfo->Buffer.Ready = true;
470
471 TCPHeaderOUT->Flags = TCP_FLAG_ACK;
472 PacketResponse = true;
473 }
474 }
475 else
476 {
477 /* Buffer is currently in use by the application, defer processing of the incoming packet */
478 return NO_PROCESS;
479 }
480 }
481
482 break;
483 case TCP_Connection_Closing:
484 ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
485 TCPHeaderIN->SourcePort);
486
487 TCPHeaderOUT->Flags = (TCP_FLAG_ACK | TCP_FLAG_FIN);
488 PacketResponse = true;
489
490 ConnectionInfo->Buffer.InUse = false;
491
492 TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
493 TCPHeaderIN->SourcePort, TCP_Connection_FINWait1);
494
495 break;
496 case TCP_Connection_FINWait1:
497 if (TCPHeaderIN->Flags == (TCP_FLAG_FIN | TCP_FLAG_ACK))
498 {
499 ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
500 TCPHeaderIN->SourcePort);
501
502 TCPHeaderOUT->Flags = TCP_FLAG_ACK;
503 PacketResponse = true;
504
505 ConnectionInfo->SequenceNumberIn++;
506 ConnectionInfo->SequenceNumberOut++;
507
508 TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
509 TCPHeaderIN->SourcePort, TCP_Connection_Closed);
510 }
511 else if (TCPHeaderIN->Flags == TCP_FLAG_ACK)
512 {
513 TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
514 TCPHeaderIN->SourcePort, TCP_Connection_FINWait2);
515 }
516
517 break;
518 case TCP_Connection_FINWait2:
519 if (TCPHeaderIN->Flags == (TCP_FLAG_FIN | TCP_FLAG_ACK))
520 {
521 ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
522 TCPHeaderIN->SourcePort);
523
524 TCPHeaderOUT->Flags = TCP_FLAG_ACK;
525 PacketResponse = true;
526
527 ConnectionInfo->SequenceNumberIn++;
528 ConnectionInfo->SequenceNumberOut++;
529
530 TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
531 TCPHeaderIN->SourcePort, TCP_Connection_Closed);
532 }
533
534 break;
535 case TCP_Connection_CloseWait:
536 if (TCPHeaderIN->Flags == TCP_FLAG_ACK)
537 {
538 TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
539 TCPHeaderIN->SourcePort, TCP_Connection_Closed);
540 }
541
542 break;
543 }
544 }
545 }
546 else
547 {
548 /* Port is not open, indicate via a RST/ACK response to the sender */
549 TCPHeaderOUT->Flags = (TCP_FLAG_RST | TCP_FLAG_ACK);
550 PacketResponse = true;
551 }
552
553 /* Check if we need to respond to the sent packet */
554 if (PacketResponse)
555 {
556 ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
557 TCPHeaderIN->SourcePort);
558
559 TCPHeaderOUT->SourcePort = TCPHeaderIN->DestinationPort;
560 TCPHeaderOUT->DestinationPort = TCPHeaderIN->SourcePort;
561 TCPHeaderOUT->SequenceNumber = SwapEndian_32(ConnectionInfo->SequenceNumberOut);
562 TCPHeaderOUT->AcknowledgmentNumber = SwapEndian_32(ConnectionInfo->SequenceNumberIn);
563 TCPHeaderOUT->DataOffset = (sizeof(TCP_Header_t) / sizeof(uint32_t));
564
565 if (!(ConnectionInfo->Buffer.InUse))
566 TCPHeaderOUT->WindowSize = SwapEndian_16(TCP_WINDOW_SIZE);
567 else
568 TCPHeaderOUT->WindowSize = SwapEndian_16(TCP_WINDOW_SIZE - ConnectionInfo->Buffer.Length);
569
570 TCPHeaderOUT->UrgentPointer = 0;
571 TCPHeaderOUT->Checksum = 0;
572 TCPHeaderOUT->Reserved = 0;
573
574 TCPHeaderOUT->Checksum = TCP_Checksum16(TCPHeaderOUT, IPHeaderIN->DestinationAddress,
575 IPHeaderIN->SourceAddress, sizeof(TCP_Header_t));
576
577 return sizeof(TCP_Header_t);
578 }
579
580 return NO_RESPONSE;
581 }
582
583 /** Calculates the appropriate TCP checksum, consisting of the addition of the one's compliment of each word,
584 * complimented.
585 *
586 * \param[in] TCPHeaderOutStart Pointer to the start of the packet's outgoing TCP header
587 * \param[in] SourceAddress Source protocol IP address of the outgoing IP header
588 * \param[in] DestinationAddress Destination protocol IP address of the outgoing IP header
589 * \param[in] TCPOutSize Size in bytes of the TCP data header and payload
590 *
591 * \return A 16-bit TCP checksum value
592 */
593 static uint16_t TCP_Checksum16(void* TCPHeaderOutStart, IP_Address_t SourceAddress,
594 IP_Address_t DestinationAddress, uint16_t TCPOutSize)
595 {
596 uint32_t Checksum = 0;
597
598 /* TCP/IP checksums are the addition of the one's compliment of each word including the IP pseudo-header,
599 complimented */
600
601 Checksum += ((uint16_t*)&SourceAddress)[0];
602 Checksum += ((uint16_t*)&SourceAddress)[1];
603 Checksum += ((uint16_t*)&DestinationAddress)[0];
604 Checksum += ((uint16_t*)&DestinationAddress)[1];
605 Checksum += SwapEndian_16(PROTOCOL_TCP);
606 Checksum += SwapEndian_16(TCPOutSize);
607
608 for (uint16_t CurrWord = 0; CurrWord < (TCPOutSize >> 1); CurrWord++)
609 Checksum += ((uint16_t*)TCPHeaderOutStart)[CurrWord];
610
611 if (TCPOutSize & 0x01)
612 Checksum += (((uint16_t*)TCPHeaderOutStart)[TCPOutSize >> 1] & 0x00FF);
613
614 while (Checksum & 0xFFFF0000)
615 Checksum = ((Checksum & 0xFFFF) + (Checksum >> 16));
616
617 return ~Checksum;
618 }