123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #ifndef _TCP_H_
- #define _TCP_H_
-
- #include <avr/io.h>
- #include <stdbool.h>
- #include "EthernetProtocols.h"
- #include "Ethernet.h"
- #include "ProtocolDecoders.h"
-
-
- #define MAX_OPEN_TCP_PORTS 1
-
- #define MAX_TCP_CONNECTIONS 3
-
- #define TCP_WINDOW_SIZE 512
-
- #define TCP_PORT_HTTP SwapEndian_16(80)
-
- #define TCP_PACKETDIR_IN false
-
- #define TCP_PACKETDIR_OUT true
-
- #define TCP_FLAG_CWR (1 << 7)
-
- #define TCP_FLAG_ECE (1 << 6)
-
- #define TCP_FLAG_URG (1 << 5)
-
- #define TCP_FLAG_ACK (1 << 4)
-
- #define TCP_FLAG_PSH (1 << 3)
-
- #define TCP_FLAG_RST (1 << 2)
-
- #define TCP_FLAG_SYN (1 << 1)
-
- #define TCP_FLAG_FIN (1 << 0)
-
- #define TCP_APP_HAS_RECEIVED_PACKET(Buffer) (Buffer->Ready && (Buffer->Direction == TCP_PACKETDIR_IN))
-
- #define TCP_APP_HAVE_CAPTURED_BUFFER(Buffer) (!(Buffer->Ready) && Buffer->InUse && (Buffer->Direction == TCP_PACKETDIR_OUT))
-
- #define TCP_APP_CAN_CAPTURE_BUFFER(Buffer) Buffer->InUse
-
- #define TCP_APP_CAPTURE_BUFFER(Buffer) do { Buffer->Direction = TCP_PACKETDIR_OUT; Buffer->InUse = true; } while (0)
-
- #define TCP_APP_RELEASE_BUFFER(Buffer) do { Buffer->InUse = false; } while (0)
-
- #define TCP_APP_SEND_BUFFER(Buffer, Len) do { Buffer->Direction = TCP_PACKETDIR_OUT; Buffer->Length = Len; Buffer->Ready = true; } while (0)
-
- #define TCP_APP_CLEAR_BUFFER(Buffer) do { Buffer->Ready = false; Buffer->Length = 0; } while (0)
-
- #define TCP_APP_CLOSECONNECTION(Connection) do { Connection->State = TCP_Connection_Closing; } while (0)
-
-
- enum TCP_PortStates_t
- {
- TCP_Port_Closed = 0,
- TCP_Port_Open = 1,
- };
-
- enum TCP_ConnectionStates_t
- {
- TCP_Connection_Listen = 0,
- TCP_Connection_SYNSent = 1,
- TCP_Connection_SYNReceived = 2,
- TCP_Connection_Established = 3,
- TCP_Connection_FINWait1 = 4,
- TCP_Connection_FINWait2 = 5,
- TCP_Connection_CloseWait = 6,
- TCP_Connection_Closing = 7,
- TCP_Connection_LastACK = 8,
- TCP_Connection_TimeWait = 9,
- TCP_Connection_Closed = 10,
- };
-
-
- typedef struct
- {
- uint16_t Length;
- uint8_t Data[TCP_WINDOW_SIZE];
- bool Direction;
- bool Ready;
- bool InUse;
- } TCP_ConnectionBuffer_t;
-
- typedef struct
- {
- uint32_t SequenceNumberIn;
- uint32_t SequenceNumberOut;
- TCP_ConnectionBuffer_t Buffer;
- } TCP_ConnectionInfo_t;
-
- typedef struct
- {
- uint16_t Port;
- uint16_t RemotePort;
- IP_Address_t RemoteAddress;
- TCP_ConnectionInfo_t Info;
- uint8_t State;
- } TCP_ConnectionState_t;
-
- typedef struct
- {
- uint16_t Port;
- uint8_t State;
- void (*ApplicationHandler) (TCP_ConnectionState_t* ConnectionState,
- TCP_ConnectionBuffer_t* Buffer);
- } TCP_PortState_t;
-
- typedef struct
- {
- uint16_t SourcePort;
- uint16_t DestinationPort;
- uint32_t SequenceNumber;
- uint32_t AcknowledgmentNumber;
- unsigned Reserved : 4;
- unsigned DataOffset : 4;
- uint8_t Flags;
- uint16_t WindowSize;
- uint16_t Checksum;
- uint16_t UrgentPointer;
- } TCP_Header_t;
-
- void TCP_TCPTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo,
- Ethernet_Frame_Info_t* const FrameOUT);
- void TCP_Init(void);
- bool TCP_SetPortState(const uint16_t Port,
- const uint8_t State,
- void (*Handler)(TCP_ConnectionState_t*, TCP_ConnectionBuffer_t*));
- uint8_t TCP_GetPortState(const uint16_t Port);
- bool TCP_SetConnectionState(const uint16_t Port,
- const IP_Address_t* RemoteAddress,
- const uint16_t RemotePort,
- const uint8_t State);
- uint8_t TCP_GetConnectionState(const uint16_t Port,
- const IP_Address_t* RemoteAddress,
- const uint16_t RemotePort);
- TCP_ConnectionInfo_t* TCP_GetConnectionInfo(const uint16_t Port,
- const IP_Address_t* RemoteAddress,
- const uint16_t RemotePort);
- int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
- void* TCPHeaderInStart,
- void* TCPHeaderOutStart);
- #if defined(INCLUDE_FROM_TCP_C)
- static uint16_t TCP_Checksum16(void* TCPHeaderOutStart,
- const IP_Address_t* SourceAddress,
- const IP_Address_t* DestinationAddress,
- uint16_t TCPOutSize);
- #endif
- #endif
|