TCP.h 11.6 KB
Newer Older
1 2
/*
             LUFA Library
3
     Copyright (C) Dean Camera, 2011.
4

5
  dean [at] fourwalledcubicle [dot] com
6
           www.lufa-lib.org
7 8 9
*/

/*
10
  Copyright 2011  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 17 18
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  Header file for TCP.c.
 */

#ifndef _TCP_H_
#define _TCP_H_

	/* Includes: */
		#include <avr/io.h>
		#include <stdbool.h>
42

43 44 45
		#include "EthernetProtocols.h"
		#include "Ethernet.h"
		#include "ProtocolDecoders.h"
46

47
	/* Macros: */
Dean Camera's avatar
Dean Camera committed
48
		/** Maximum number of TCP ports which can be open at the one time. */
49 50
		#define MAX_OPEN_TCP_PORTS              1

Dean Camera's avatar
Dean Camera committed
51
		/** Maximum number of TCP connections which can be sustained at the one time. */
52 53
		#define MAX_TCP_CONNECTIONS             3

Dean Camera's avatar
Dean Camera committed
54
		/** TCP window size, giving the maximum number of bytes which can be buffered at the one time. */
55
		#define TCP_WINDOW_SIZE                 512
56

Dean Camera's avatar
Dean Camera committed
57
		/** Port number for HTTP transmissions. */
58
		#define TCP_PORT_HTTP                   SwapEndian_16(80)
59

Dean Camera's avatar
Dean Camera committed
60
		/** Data direction indicator for a TCP application buffer, indicating data from host-to-device. */
61 62
		#define TCP_PACKETDIR_IN                false

Dean Camera's avatar
Dean Camera committed
63
		/** Data direction indicator for a TCP application buffer, indicating data from device-to-host. */
64
		#define TCP_PACKETDIR_OUT               true
65

Dean Camera's avatar
Dean Camera committed
66
		/** Congestion Window Reduced TCP flag mask. */
67 68
		#define TCP_FLAG_CWR                    (1 << 7)

Dean Camera's avatar
Dean Camera committed
69
		/** Explicit Congestion Notification TCP flag mask. */
70 71
		#define TCP_FLAG_ECE                    (1 << 6)

Dean Camera's avatar
Dean Camera committed
72
		/** Urgent TCP flag mask. */
73 74
		#define TCP_FLAG_URG                    (1 << 5)

Dean Camera's avatar
Dean Camera committed
75
		/** Data Acknowledge TCP flag mask. */
76 77
		#define TCP_FLAG_ACK                    (1 << 4)

Dean Camera's avatar
Dean Camera committed
78
		/** Data Push TCP flag mask. */
79 80
		#define TCP_FLAG_PSH                    (1 << 3)

Dean Camera's avatar
Dean Camera committed
81
		/** Reset TCP flag mask. */
82 83
		#define TCP_FLAG_RST                    (1 << 2)

Dean Camera's avatar
Dean Camera committed
84
		/** Synchronize TCP flag mask. */
85 86
		#define TCP_FLAG_SYN                    (1 << 1)

Dean Camera's avatar
Dean Camera committed
87
		/** Connection Finalize TCP flag mask. */
88
		#define TCP_FLAG_FIN                    (1 << 0)
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103
		/** Application macro: Determines if the given application buffer contains a packet received from the host
		 *
		 *  \param[in] Buffer  Application buffer to check
		 *
		 *  \return Boolean true if the buffer contains a packet from the host, false otherwise
		 */
		#define TCP_APP_HAS_RECEIVED_PACKET(Buffer)  (Buffer->Ready && (Buffer->Direction == TCP_PACKETDIR_IN))

		/** Application macro: Indicates if the application buffer is currently locked by the application for device-to-host transfers.
		 *
		 *  \param[in] Buffer  Application buffer to check
		 *
		 *  \return Boolean true if the buffer has been captured by the application for device-to-host transmissions, false otherwise
		 */
104
		#define TCP_APP_HAVE_CAPTURED_BUFFER(Buffer) (!(Buffer->Ready) && Buffer->InUse && (Buffer->Direction == TCP_PACKETDIR_OUT))
105 106 107 108 109 110 111 112 113 114 115 116

		/** Application macro: Indicates if the application can lock the buffer for multiple continued device-to-host transmissions.
		 *
		 *  \param[in] Buffer  Application buffer to check
		 *
		 *  \return Boolean true if the buffer may be captured by the application for device-to-host transmissions, false otherwise
		 */
		#define TCP_APP_CAN_CAPTURE_BUFFER(Buffer)   Buffer->InUse

		/** Application macro: Captures the application buffer, locking it for device-to-host transmissions only. This should be
		 *  performed when the application needs to transmit several packets worth of data in succession with no interruptions from the host.
		 *
117
		 *  \pre The application must check that the buffer can be locked first using TCP_APP_CAN_CAPTURE_BUFFER().
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		 *
		 *  \param[in] Buffer  Application buffer to lock
		 */
		#define TCP_APP_CAPTURE_BUFFER(Buffer)       MACROS{ Buffer->Direction = TCP_PACKETDIR_OUT; Buffer->InUse = true; }MACROE

		/** Application macro: Releases a captured application buffer, allowing for host-to-device packets to be received.
		 *
		 *  \param[in] Buffer  Application buffer to release
		 */
		#define TCP_APP_RELEASE_BUFFER(Buffer)       MACROS{ Buffer->InUse = false; }MACROE

		/** Application macro: Sends the contents of the given application buffer to the host.
		 *
		 *  \param[in] Buffer  Application buffer to send
		 *  \param[in] Len     Length of data contained in the buffer
		 */
		#define TCP_APP_SEND_BUFFER(Buffer, Len)     MACROS{ Buffer->Direction = TCP_PACKETDIR_OUT; Buffer->Length = Len; Buffer->Ready = true; }MACROE

		/** Application macro: Clears the application buffer, ready for a packet to be written to it.
		 *
		 *  \param[in] Buffer  Application buffer to clear
		 */
		#define TCP_APP_CLEAR_BUFFER(Buffer)         MACROS{ Buffer->Ready = false; Buffer->Length = 0; }MACROE
141

142 143 144 145 146 147 148
		/** Application macro: Closes an open connection to a host.
		 *
		 *  \param[in] Connection  Open TCP connection to close
		 */
		#define TCP_APP_CLOSECONNECTION(Connection)  MACROS{ Connection->State = TCP_Connection_Closing;  }MACROE

	/* Enums: */
Dean Camera's avatar
Dean Camera committed
149
		/** Enum for possible TCP port states. */
150 151 152 153 154
		enum TCP_PortStates_t
		{
			TCP_Port_Closed            = 0, /**< TCP port closed, no connections to a host may be made on this port. */
			TCP_Port_Open              = 1, /**< TCP port open, connections to a host may be made on this port. */
		};
155

Dean Camera's avatar
Dean Camera committed
156
		/** Enum for possible TCP connection states. */
157 158 159 160 161 162 163 164 165 166 167 168
		enum TCP_ConnectionStates_t
		{
			TCP_Connection_Listen      = 0, /**< Listening for a connection from a host */
			TCP_Connection_SYNSent     = 1, /**< Unused */
			TCP_Connection_SYNReceived = 2, /**< SYN received, waiting for ACK */
			TCP_Connection_Established = 3, /**< Connection established in both directions */
			TCP_Connection_FINWait1    = 4, /**< Closing, waiting for ACK */
			TCP_Connection_FINWait2    = 5, /**< Closing, waiting for FIN ACK */
			TCP_Connection_CloseWait   = 6, /**< Closing, waiting for ACK */
			TCP_Connection_Closing     = 7, /**< Unused */
			TCP_Connection_LastACK     = 8, /**< Unused */
			TCP_Connection_TimeWait    = 9, /**< Unused */
169
			TCP_Connection_Closed      = 10, /**< Connection closed in both directions */
170
		};
171

172
	/* Type Defines: */
Dean Camera's avatar
Dean Camera committed
173
		/** Type define for a TCP connection buffer structure, including size, data and direction. */
174 175 176 177 178 179 180 181
		typedef struct
		{
			uint16_t               Length; /**< Length of data in the TCP application buffer */
			uint8_t                Data[TCP_WINDOW_SIZE]; /**< TCP application data buffer */
			bool                   Direction; /**< Buffer transmission direction, either TCP_PACKETDIR_IN  or TCP_PACKETDIR_OUT */
			bool                   Ready; /**< If data from host, indicates buffer ready to be read, otherwise indicates
			                               *   buffer ready to be sent to the host
			                               */
Dean Camera's avatar
Dean Camera committed
182
			bool                   InUse; /**< Indicates if the buffer is locked to to the current direction, and cannot be changed */
183 184
		} TCP_ConnectionBuffer_t;

Dean Camera's avatar
Dean Camera committed
185
		/** Type define for a TCP connection information structure. */
186 187
		typedef struct
		{
188
			uint32_t               SequenceNumberIn; /**< Current TCP sequence number for host-to-device */
189 190 191 192
			uint32_t               SequenceNumberOut; /**< Current TCP sequence number for device-to-host */
			TCP_ConnectionBuffer_t Buffer; /**< Connection application data buffer */
		} TCP_ConnectionInfo_t;

Dean Camera's avatar
Dean Camera committed
193
		/** Type define for a complete TCP connection state. */
194 195 196 197 198 199 200 201 202
		typedef struct
		{
			uint16_t               Port; /**< Connection port number on the device */
			uint16_t               RemotePort; /**< Connection port number on the host */
			IP_Address_t           RemoteAddress; /**< Connection protocol IP address of the host */
			TCP_ConnectionInfo_t   Info; /**< Connection information, including application buffer */
			uint8_t                State; /**< Current connection state, a value from the TCP_ConnectionStates_t enum */
		} TCP_ConnectionState_t;

Dean Camera's avatar
Dean Camera committed
203
		/** Type define for a TCP port state. */
204 205 206 207 208 209 210 211
		typedef struct
		{
			uint16_t               Port; /**< TCP port number on the device */
			uint8_t                State; /**< Current port state, a value from the TCP_PortStates_t enum */
			void                   (*ApplicationHandler) (TCP_ConnectionState_t* ConnectionState,
			                                              TCP_ConnectionBuffer_t* Buffer); /**< Port application handler */
		} TCP_PortState_t;

Dean Camera's avatar
Dean Camera committed
212
		/** Type define for a TCP packet header. */
213 214 215 216
		typedef struct
		{
			uint16_t               SourcePort; /**< Source port of the TCP packet */
			uint16_t               DestinationPort; /**< Destination port of the TCP packet */
217

218 219
			uint32_t               SequenceNumber; /**< Data sequence number of the packet */
			uint32_t               AcknowledgmentNumber; /**< Data acknowledgment number of the packet */
220

221 222
			unsigned               Reserved : 4; /**< Reserved, must be all 0 */
			unsigned               DataOffset : 4; /**< Offset of the data from the start of the header, in 4 byte chunks */
223 224
			uint8_t                Flags; /**< TCP packet flags */
			uint16_t               WindowSize; /**< Current data window size (bytes remaining in reception buffer) */
225

226 227 228
			uint16_t               Checksum; /**< TCP checksum */
			uint16_t               UrgentPointer; /**< Urgent data pointer */
		} TCP_Header_t;
229

230
	/* Function Prototypes: */
231 232
		void                  TCP_TCPTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo,
		                                  Ethernet_Frame_Info_t* const FrameOUT);
233
		void                  TCP_Init(void);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		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);
251 252

		#if defined(INCLUDE_FROM_TCP_C)
253 254 255 256
			static uint16_t TCP_Checksum16(void* TCPHeaderOutStart,
			                               const IP_Address_t SourceAddress,
										   const IP_Address_t DestinationAddress,
			                               uint16_t TCPOutSize);
257 258 259
		#endif

#endif
260