]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/include/FreeRTOS_IP_Private.h
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / include / FreeRTOS_IP_Private.h
1 /*\r
2  * FreeRTOS+TCP V2.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 #ifndef FREERTOS_IP_PRIVATE_H\r
29 #define FREERTOS_IP_PRIVATE_H\r
30 \r
31 #ifdef __cplusplus\r
32 extern "C" {\r
33 #endif\r
34 \r
35 /* Application level configuration options. */\r
36 #include "FreeRTOSIPConfig.h"\r
37 #include "FreeRTOSIPConfigDefaults.h"\r
38 #include "FreeRTOS_Sockets.h"\r
39 #include "IPTraceMacroDefaults.h"\r
40 #include "FreeRTOS_Stream_Buffer.h"\r
41 #if( ipconfigUSE_TCP == 1 )\r
42         #include "FreeRTOS_TCP_WIN.h"\r
43         #include "FreeRTOS_TCP_IP.h"\r
44 #endif\r
45 \r
46 #include "event_groups.h"\r
47 \r
48 typedef struct xNetworkAddressingParameters\r
49 {\r
50         uint32_t ulDefaultIPAddress;\r
51         uint32_t ulNetMask;\r
52         uint32_t ulGatewayAddress;\r
53         uint32_t ulDNSServerAddress;\r
54         uint32_t ulBroadcastAddress;\r
55 } NetworkAddressingParameters_t;\r
56 \r
57 extern BaseType_t xTCPWindowLoggingLevel;\r
58 \r
59 /*-----------------------------------------------------------*/\r
60 /* Protocol headers.                                         */\r
61 /*-----------------------------------------------------------*/\r
62 \r
63 #include "pack_struct_start.h"\r
64 struct xETH_HEADER\r
65 {\r
66         MACAddress_t xDestinationAddress; /*  0 + 6 = 6  */\r
67         MACAddress_t xSourceAddress;      /*  6 + 6 = 12 */\r
68         uint16_t usFrameType;              /* 12 + 2 = 14 */\r
69 }\r
70 #include "pack_struct_end.h"\r
71 typedef struct xETH_HEADER EthernetHeader_t;\r
72 \r
73 #include "pack_struct_start.h"\r
74 struct xARP_HEADER\r
75 {\r
76         uint16_t usHardwareType;                                /*  0 +  2 =  2 */\r
77         uint16_t usProtocolType;                                /*  2 +  2 =  4 */\r
78         uint8_t ucHardwareAddressLength;                /*  4 +  1 =  5 */\r
79         uint8_t ucProtocolAddressLength;                /*  5 +  1 =  6 */\r
80         uint16_t usOperation;                                   /*  6 +  2 =  8 */\r
81         MACAddress_t xSenderHardwareAddress;    /*  8 +  6 = 14 */\r
82         uint8_t ucSenderProtocolAddress[ 4 ];   /* 14 +  4 = 18  */\r
83         MACAddress_t xTargetHardwareAddress;    /* 18 +  6 = 24  */\r
84         uint32_t ulTargetProtocolAddress;               /* 24 +  4 = 28  */\r
85 }\r
86 #include "pack_struct_end.h"\r
87 typedef struct xARP_HEADER ARPHeader_t;\r
88 \r
89 #include "pack_struct_start.h"\r
90 struct xIP_HEADER\r
91 {\r
92         uint8_t ucVersionHeaderLength;        /*  0 + 1 =  1 */\r
93         uint8_t ucDifferentiatedServicesCode; /*  1 + 1 =  2 */\r
94         uint16_t usLength;                    /*  2 + 2 =  4 */\r
95         uint16_t usIdentification;            /*  4 + 2 =  6 */\r
96         uint16_t usFragmentOffset;            /*  6 + 2 =  8 */\r
97         uint8_t ucTimeToLive;                 /*  8 + 1 =  9 */\r
98         uint8_t ucProtocol;                   /*  9 + 1 = 10 */\r
99         uint16_t usHeaderChecksum;            /* 10 + 2 = 12 */\r
100         uint32_t ulSourceIPAddress;           /* 12 + 4 = 16 */\r
101         uint32_t ulDestinationIPAddress;      /* 16 + 4 = 20 */\r
102 }\r
103 #include "pack_struct_end.h"\r
104 typedef struct xIP_HEADER IPHeader_t;\r
105 \r
106 #include "pack_struct_start.h"\r
107 struct xIGMP_HEADER\r
108 {\r
109         uint8_t ucVersionType;     /* 0 + 1 = 1 */\r
110         uint8_t ucMaxResponseTime; /* 1 + 1 = 2 */\r
111         uint16_t usChecksum;       /* 2 + 2 = 4 */\r
112         uint32_t usGroupAddress;   /* 4 + 4 = 8 */\r
113 }\r
114 #include "pack_struct_end.h"\r
115 typedef struct xIGMP_HEADER IGMPHeader_t;\r
116 \r
117 #include "pack_struct_start.h"\r
118 struct xICMP_HEADER\r
119 {\r
120         uint8_t ucTypeOfMessage;   /* 0 + 1 = 1 */\r
121         uint8_t ucTypeOfService;   /* 1 + 1 = 2 */\r
122         uint16_t usChecksum;       /* 2 + 2 = 4 */\r
123         uint16_t usIdentifier;     /* 4 + 2 = 6 */\r
124         uint16_t usSequenceNumber; /* 6 + 2 = 8 */\r
125 }\r
126 #include "pack_struct_end.h"\r
127 typedef struct xICMP_HEADER ICMPHeader_t;\r
128 \r
129 #include "pack_struct_start.h"\r
130 struct xUDP_HEADER\r
131 {\r
132         uint16_t usSourcePort;      /* 0 + 2 = 2 */\r
133         uint16_t usDestinationPort; /* 2 + 2 = 4 */\r
134         uint16_t usLength;          /* 4 + 2 = 6 */\r
135         uint16_t usChecksum;        /* 6 + 2 = 8 */\r
136 }\r
137 #include "pack_struct_end.h"\r
138 typedef struct xUDP_HEADER UDPHeader_t;\r
139 \r
140 #include "pack_struct_start.h"\r
141 struct xTCP_HEADER\r
142 {\r
143         uint16_t usSourcePort;          /* +  2 =  2 */\r
144         uint16_t usDestinationPort;     /* +  2 =  4 */\r
145         uint32_t ulSequenceNumber;      /* +  4 =  8 */\r
146         uint32_t ulAckNr;               /* +  4 = 12 */\r
147         uint8_t  ucTCPOffset;           /* +  1 = 13 */\r
148         uint8_t  ucTCPFlags;            /* +  1 = 14 */\r
149         uint16_t usWindow;                      /* +  2 = 15 */\r
150         uint16_t usChecksum;            /* +  2 = 18 */\r
151         uint16_t usUrgent;                      /* +  2 = 20 */\r
152 #if ipconfigUSE_TCP == 1\r
153         /* the option data is not a part of the TCP header */\r
154         uint8_t  ucOptdata[ipSIZE_TCP_OPTIONS];         /* + 12 = 32 */\r
155 #endif\r
156 }\r
157 #include "pack_struct_end.h"\r
158 typedef struct xTCP_HEADER TCPHeader_t;\r
159 \r
160 #include "pack_struct_start.h"\r
161 struct xPSEUDO_HEADER\r
162 {\r
163         uint32_t ulSourceAddress;\r
164         uint32_t ulDestinationAddress;\r
165         uint8_t ucZeros;\r
166         uint8_t ucProtocol;\r
167         uint16_t usUDPLength;\r
168 }\r
169 #include "pack_struct_end.h"\r
170 typedef struct xPSEUDO_HEADER PseudoHeader_t;\r
171 \r
172 /*-----------------------------------------------------------*/\r
173 /* Nested protocol packets.                                  */\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 #include "pack_struct_start.h"\r
177 struct xARP_PACKET\r
178 {\r
179         EthernetHeader_t xEthernetHeader;       /*  0 + 14 = 14 */\r
180         ARPHeader_t xARPHeader;                 /* 14 + 28 = 42 */\r
181 }\r
182 #include "pack_struct_end.h"\r
183 typedef struct xARP_PACKET ARPPacket_t;\r
184 \r
185 #include "pack_struct_start.h"\r
186 struct xIP_PACKET\r
187 {\r
188         EthernetHeader_t xEthernetHeader;\r
189         IPHeader_t xIPHeader;\r
190 }\r
191 #include "pack_struct_end.h"\r
192 typedef struct xIP_PACKET IPPacket_t;\r
193 \r
194 #include "pack_struct_start.h"\r
195 struct xICMP_PACKET\r
196 {\r
197         EthernetHeader_t xEthernetHeader;\r
198         IPHeader_t xIPHeader;\r
199         ICMPHeader_t xICMPHeader;\r
200 }\r
201 #include "pack_struct_end.h"\r
202 typedef struct xICMP_PACKET ICMPPacket_t;\r
203 \r
204 #include "pack_struct_start.h"\r
205 struct xUDP_PACKET\r
206 {\r
207         EthernetHeader_t xEthernetHeader; /*  0 + 14 = 14 */\r
208         IPHeader_t xIPHeader;             /* 14 + 20 = 34 */\r
209         UDPHeader_t xUDPHeader;           /* 34 +  8 = 42 */\r
210 }\r
211 #include "pack_struct_end.h"\r
212 typedef struct xUDP_PACKET UDPPacket_t;\r
213 \r
214 #include "pack_struct_start.h"\r
215 struct xTCP_PACKET\r
216 {\r
217         EthernetHeader_t xEthernetHeader; /*  0 + 14 = 14 */\r
218         IPHeader_t xIPHeader;             /* 14 + 20 = 34 */\r
219         TCPHeader_t xTCPHeader;           /* 34 + 32 = 66 */\r
220 }\r
221 #include "pack_struct_end.h"\r
222 typedef struct xTCP_PACKET TCPPacket_t;\r
223 \r
224 typedef union XPROT_PACKET\r
225 {\r
226         ARPPacket_t xARPPacket;\r
227         TCPPacket_t xTCPPacket;\r
228         UDPPacket_t xUDPPacket;\r
229         ICMPPacket_t xICMPPacket;\r
230 } ProtocolPacket_t;\r
231 \r
232 \r
233 /* The maximum UDP payload length. */\r
234 #define ipMAX_UDP_PAYLOAD_LENGTH ( ( ipconfigNETWORK_MTU - ipSIZE_OF_IPv4_HEADER ) - ipSIZE_OF_UDP_HEADER )\r
235 \r
236 typedef enum\r
237 {\r
238         eReleaseBuffer = 0,             /* Processing the frame did not find anything to do - just release the buffer. */\r
239         eProcessBuffer,                 /* An Ethernet frame has a valid address - continue process its contents. */\r
240         eReturnEthernetFrame,   /* The Ethernet frame contains an ARP or ICMP packet that can be returned to its source. */\r
241         eFrameConsumed                  /* Processing the Ethernet packet contents resulted in the payload being sent to the stack. */\r
242 } eFrameProcessingResult_t;\r
243 \r
244 typedef enum\r
245 {\r
246         eNoEvent = -1,\r
247         eNetworkDownEvent,              /* 0: The network interface has been lost and/or needs [re]connecting. */\r
248         eNetworkRxEvent,                /* 1: The network interface has queued a received Ethernet frame. */\r
249         eARPTimerEvent,                 /* 2: The ARP timer expired. */\r
250         eStackTxEvent,                  /* 3: The software stack has queued a packet to transmit. */\r
251         eDHCPEvent,                             /* 4: Process the DHCP state machine. */\r
252         eTCPTimerEvent,                 /* 5: See if any TCP socket needs attention. */\r
253         eTCPAcceptEvent,                /* 6: Client API FreeRTOS_accept() waiting for client connections. */\r
254         eTCPNetStat,                    /* 7: IP-task is asked to produce a netstat listing. */\r
255         eSocketBindEvent,               /* 8: Send a message to the IP-task to bind a socket to a port. */\r
256         eSocketCloseEvent,              /* 9: Send a message to the IP-task to close a socket. */\r
257         eSocketSelectEvent,             /*10: Send a message to the IP-task for select(). */\r
258         eSocketSignalEvent,             /*11: A socket must be signalled. */\r
259 } eIPEvent_t;\r
260 \r
261 typedef struct IP_TASK_COMMANDS\r
262 {\r
263         eIPEvent_t eEventType;\r
264         void *pvData;\r
265 } IPStackEvent_t;\r
266 \r
267 #define ipBROADCAST_IP_ADDRESS 0xffffffffUL\r
268 \r
269 /* Offset into the Ethernet frame that is used to temporarily store information\r
270 on the fragmentation status of the packet being sent.  The value is important,\r
271 as it is past the location into which the destination address will get placed. */\r
272 #define ipFRAGMENTATION_PARAMETERS_OFFSET               ( 6 )\r
273 #define ipSOCKET_OPTIONS_OFFSET                                 ( 6 )\r
274 \r
275 /* Only used when outgoing fragmentation is being used (FreeRTOSIPConfig.h\r
276 setting. */\r
277 #define ipGET_UDP_PAYLOAD_OFFSET_FOR_FRAGMENT( usFragmentOffset ) ( ( ( usFragmentOffset ) == 0 ) ? ipUDP_PAYLOAD_OFFSET_IPv4 : ipIP_PAYLOAD_OFFSET )\r
278 \r
279 /* The offset into a UDP packet at which the UDP data (payload) starts. */\r
280 #define ipUDP_PAYLOAD_OFFSET_IPv4       ( sizeof( UDPPacket_t ) )\r
281 \r
282 /* The offset into an IP packet into which the IP data (payload) starts. */\r
283 #define ipIP_PAYLOAD_OFFSET             ( sizeof( IPPacket_t ) )\r
284 \r
285 #include "pack_struct_start.h"\r
286 struct xUDP_IP_FRAGMENT_PARAMETERS\r
287 {\r
288         uint8_t ucSocketOptions;\r
289         uint8_t ucPadFor16BitAlignment;\r
290         uint16_t usFragmentedPacketOffset;\r
291         uint16_t usFragmentLength;\r
292         uint16_t usPayloadChecksum;\r
293 }\r
294 #include "pack_struct_end.h"\r
295 typedef struct xUDP_IP_FRAGMENT_PARAMETERS IPFragmentParameters_t;\r
296 \r
297 #if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )\r
298 \r
299         /* Ethernet frame types. */\r
300         #define ipARP_FRAME_TYPE        ( 0x0608U )\r
301         #define ipIPv4_FRAME_TYPE       ( 0x0008U )\r
302 \r
303         /* ARP related definitions. */\r
304         #define ipARP_PROTOCOL_TYPE                             ( 0x0008U )\r
305         #define ipARP_HARDWARE_TYPE_ETHERNET    ( 0x0100U )\r
306         #define ipARP_REQUEST                                   ( 0x0100U )\r
307         #define ipARP_REPLY                                             ( 0x0200U )\r
308 \r
309 #else\r
310 \r
311         /* Ethernet frame types. */\r
312         #define ipARP_FRAME_TYPE        ( 0x0806U )\r
313         #define ipIPv4_FRAME_TYPE       ( 0x0800U )\r
314 \r
315         /* ARP related definitions. */\r
316         #define ipARP_PROTOCOL_TYPE ( 0x0800U )\r
317         #define ipARP_HARDWARE_TYPE_ETHERNET ( 0x0001U )\r
318         #define ipARP_REQUEST ( 0x0001 )\r
319         #define ipARP_REPLY ( 0x0002 )\r
320 \r
321 #endif /* ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN */\r
322 \r
323 \r
324 /* For convenience, a MAC address of all zeros and another of all 0xffs are\r
325 defined const for quick reference. */\r
326 extern const MACAddress_t xBroadcastMACAddress; /* all 0xff's */\r
327 extern uint16_t usPacketIdentifier;\r
328 \r
329 /* Define a default UDP packet header (declared in FreeRTOS_UDP_IP.c) */\r
330 typedef union xUDPPacketHeader\r
331 {\r
332         uint8_t ucBytes[24];\r
333         uint32_t ulWords[6];\r
334 } UDPPacketHeader_t;\r
335 extern UDPPacketHeader_t xDefaultPartUDPPacketHeader;\r
336 \r
337 /* Structure that stores the netmask, gateway address and DNS server addresses. */\r
338 extern NetworkAddressingParameters_t xNetworkAddressing;\r
339 \r
340 /* Structure that stores the defaults for netmask, gateway address and DNS.\r
341 These values will be copied to 'xNetworkAddressing' in case DHCP is not used,\r
342 and also in case DHCP does not lead to a confirmed request. */\r
343 extern NetworkAddressingParameters_t xDefaultAddressing;\r
344 \r
345 /* True when BufferAllocation_1.c was included, false for BufferAllocation_2.c */\r
346 extern const BaseType_t xBufferAllocFixedSize;\r
347 \r
348 /* Defined in FreeRTOS_Sockets.c */\r
349 #if ( ipconfigUSE_TCP == 1 )\r
350         extern List_t xBoundTCPSocketsList;\r
351 #endif\r
352 \r
353 /* The local IP address is accessed from within xDefaultPartUDPPacketHeader,\r
354 rather than duplicated in its own variable. */\r
355 #define ipLOCAL_IP_ADDRESS_POINTER ( ( uint32_t * ) &( xDefaultPartUDPPacketHeader.ulWords[ 20u / sizeof(uint32_t) ] ) )\r
356 \r
357 /* The local MAC address is accessed from within xDefaultPartUDPPacketHeader,\r
358 rather than duplicated in its own variable. */\r
359 #define ipLOCAL_MAC_ADDRESS ( &xDefaultPartUDPPacketHeader.ucBytes[0] )\r
360 \r
361 /* ICMP packets are sent using the same function as UDP packets.  The port\r
362 number is used to distinguish between the two, as 0 is an invalid UDP port. */\r
363 #define ipPACKET_CONTAINS_ICMP_DATA                                     ( 0 )\r
364 \r
365 /* For now, the lower 8 bits in 'xEventBits' will be reserved for the above\r
366 socket events. */\r
367 #define SOCKET_EVENT_BIT_COUNT   8\r
368 \r
369 #define vSetField16( pxBase, xType, xField, usValue ) \\r
370 { \\r
371         ( ( uint8_t* )( pxBase ) ) [ offsetof( xType, xField ) + 0 ] = ( uint8_t ) ( ( usValue ) >> 8 ); \\r
372         ( ( uint8_t* )( pxBase ) ) [ offsetof( xType, xField ) + 1 ] = ( uint8_t ) ( ( usValue ) & 0xff ); \\r
373 }\r
374 \r
375 #define vSetField32( pxBase, xType, xField, ulValue ) \\r
376 { \\r
377         ( (uint8_t*)( pxBase ) ) [ offsetof( xType, xField ) + 0 ] = ( uint8_t )   ( ( ulValue ) >> 24 ); \\r
378         ( (uint8_t*)( pxBase ) ) [ offsetof( xType, xField ) + 1 ] = ( uint8_t ) ( ( ( ulValue ) >> 16 ) & 0xff ); \\r
379         ( (uint8_t*)( pxBase ) ) [ offsetof( xType, xField ) + 2 ] = ( uint8_t ) ( ( ( ulValue ) >> 8 ) & 0xff ); \\r
380         ( (uint8_t*)( pxBase ) ) [ offsetof( xType, xField ) + 3 ] = ( uint8_t )   ( ( ulValue ) & 0xff ); \\r
381 }\r
382 \r
383 #define vFlip_16( left, right ) \\r
384         do { \\r
385                 uint16_t tmp = (left); \\r
386                 (left) = (right); \\r
387                 (right) = tmp; \\r
388         } while (0)\r
389 \r
390 #define vFlip_32( left, right ) \\r
391         do { \\r
392                 uint32_t tmp = (left); \\r
393                 (left) = (right); \\r
394                 (right) = tmp; \\r
395         } while (0)\r
396 \r
397 #ifndef ARRAY_SIZE\r
398         #define ARRAY_SIZE(x)   (BaseType_t)(sizeof(x)/sizeof(x)[0])\r
399 #endif\r
400 \r
401 /*\r
402  * A version of FreeRTOS_GetReleaseNetworkBuffer() that can be called from an\r
403  * interrupt.  If a non zero value is returned, then the calling ISR should\r
404  * perform a context switch before exiting the ISR.\r
405  */\r
406 BaseType_t FreeRTOS_ReleaseFreeNetworkBufferFromISR( void );\r
407 \r
408 /*\r
409  * Create a message that contains a command to initialise the network interface.\r
410  * This is used during initialisation, and at any time the network interface\r
411  * goes down thereafter.  The network interface hardware driver is responsible\r
412  * for sending the message that contains the network interface down command/\r
413  * event.\r
414  *\r
415  * Only use the FreeRTOS_NetworkDownFromISR() version if the function is to be\r
416  * called from an interrupt service routine.  If FreeRTOS_NetworkDownFromISR()\r
417  * returns a non-zero value then a context switch should be performed ebfore\r
418  * the interrupt is exited.\r
419  */\r
420 void FreeRTOS_NetworkDown( void );\r
421 BaseType_t FreeRTOS_NetworkDownFromISR( void );\r
422 \r
423 /*\r
424  * Processes incoming ARP packets.\r
425  */\r
426 eFrameProcessingResult_t eARPProcessPacket( ARPPacket_t * const pxARPFrame );\r
427 \r
428 /*\r
429  * Inspect an Ethernet frame to see if it contains data that the stack needs to\r
430  * process.  eProcessBuffer is returned if the frame should be processed by the\r
431  * stack.  eReleaseBuffer is returned if the frame should be discarded.\r
432  */\r
433 eFrameProcessingResult_t eConsiderFrameForProcessing( const uint8_t * const pucEthernetBuffer );\r
434 \r
435 /*\r
436  * Return the checksum generated over xDataLengthBytes from pucNextData.\r
437  */\r
438 uint16_t usGenerateChecksum( uint32_t ulSum, const uint8_t * pucNextData, size_t uxDataLengthBytes );\r
439 \r
440 /* Socket related private functions. */\r
441 BaseType_t xProcessReceivedUDPPacket( NetworkBufferDescriptor_t *pxNetworkBuffer, uint16_t usPort );\r
442 void vNetworkSocketsInit( void );\r
443 \r
444 /*\r
445  * Returns pdTRUE if the IP task has been created and is initialised.  Otherwise\r
446  * returns pdFALSE.\r
447  */\r
448 BaseType_t xIPIsNetworkTaskReady( void );\r
449 \r
450 #if( ipconfigSOCKET_HAS_USER_WAKE_CALLBACK == 1 )\r
451         struct XSOCKET;\r
452         typedef void (*SocketWakeupCallback_t)( struct XSOCKET * pxSocket );\r
453 #endif\r
454 \r
455 #if( ipconfigUSE_TCP == 1 )\r
456 \r
457         /*\r
458          * Actually a user thing, but because xBoundTCPSocketsList, let it do by the\r
459          * IP-task\r
460          */\r
461         void vTCPNetStat( void );\r
462 \r
463         /*\r
464          * At least one socket needs to check for timeouts\r
465          */\r
466         TickType_t xTCPTimerCheck( BaseType_t xWillSleep );\r
467 \r
468         /* Every TCP socket has a buffer space just big enough to store\r
469         the last TCP header received.\r
470         As a reference of this field may be passed to DMA, force the\r
471         alignment to 8 bytes. */\r
472         typedef union\r
473         {\r
474                 struct\r
475                 {\r
476                         /* Increase the alignment of this union by adding a 64-bit variable. */\r
477                         uint64_t ullAlignmentWord;\r
478                 } a;\r
479                 struct\r
480                 {\r
481                         /* The next field only serves to give 'ucLastPacket' a correct\r
482                         alignment of 8 + 2.  See comments in FreeRTOS_IP.h */\r
483                         uint8_t ucFillPacket[ ipconfigPACKET_FILLER_SIZE ];\r
484                         uint8_t ucLastPacket[ sizeof( TCPPacket_t ) ];\r
485                 } u;\r
486         } LastTCPPacket_t;\r
487 \r
488         /*\r
489          * Note that the values of all short and long integers in these structs\r
490          * are being stored in the native-endian way\r
491          * Translation should take place when accessing any structure which defines\r
492          * network packets, such as IPHeader_t and TCPHeader_t\r
493          */\r
494         typedef struct TCPSOCKET\r
495         {\r
496                 uint32_t ulRemoteIP;            /* IP address of remote machine */\r
497                 uint16_t usRemotePort;          /* Port on remote machine */\r
498                 struct {\r
499                         /* Most compilers do like bit-flags */\r
500                         uint32_t\r
501                                 bMssChange : 1,         /* This socket has seen a change in MSS */\r
502                                 bPassAccept : 1,        /* when true, this socket may be returned in a call to accept() */\r
503                                 bPassQueued : 1,        /* when true, this socket is an orphan until it gets connected\r
504                                                                          * Why an orphan? Because it may not be returned in a accept() call until it\r
505                                                                          * gets the state eESTABLISHED */\r
506                                 bReuseSocket : 1,       /* When a listening socket gets a connection, do not create a new instance but keep on using it */\r
507                                 bCloseAfterSend : 1,/* As soon as the last byte has been transmitted, finalise the connection\r
508                                                                          * Useful in e.g. FTP connections, where the last data bytes are sent along with the FIN flag */\r
509                                 bUserShutdown : 1,      /* User requesting a graceful shutdown */\r
510                                 bCloseRequested : 1,/* Request to finalise the connection */\r
511                                 bLowWater : 1,          /* high-water level has been reached. Cleared as soon as 'rx-count < lo-water' */\r
512                                 bWinChange : 1,         /* The value of bLowWater has changed, must send a window update */\r
513                                 bSendKeepAlive : 1,     /* When this flag is true, a TCP keep-alive message must be send */\r
514                                 bWaitKeepAlive : 1,     /* When this flag is true, a TCP keep-alive reply is expected */\r
515                                 bConnPrepared : 1,      /* Connecting socket: Message has been prepared */\r
516                                 #if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )\r
517                                         bConnPassed : 1,        /* Connecting socket: Socket has been passed in a successful select()  */\r
518                                 #endif /* ipconfigSUPPORT_SELECT_FUNCTION */\r
519                                 bFinAccepted : 1,       /* This socket has received (or sent) a FIN and accepted it */\r
520                                 bFinSent : 1,           /* We've sent out a FIN */\r
521                                 bFinRecv : 1,           /* We've received a FIN from our peer */\r
522                                 bFinAcked : 1,          /* Our FIN packet has been acked */\r
523                                 bFinLast : 1,           /* The last ACK (after FIN and FIN+ACK) has been sent or will be sent by the peer */\r
524                                 bRxStopped : 1,         /* Application asked to temporarily stop reception */\r
525                                 bMallocError : 1,       /* There was an error allocating a stream */\r
526                                 bWinScaling : 1;        /* A TCP-Window Scaling option was offered and accepted in the SYN phase. */\r
527                 } bits;\r
528                 uint32_t ulHighestRxAllowed;\r
529                                                                 /* The highest sequence number that we can receive at any moment */\r
530                 uint16_t usTimeout;             /* Time (in ticks) after which this socket needs attention */\r
531                 uint16_t usCurMSS;              /* Current Maximum Segment Size */\r
532                 uint16_t usInitMSS;             /* Initial maximum segment Size */\r
533                 uint16_t usChildCount;  /* In case of a listening socket: number of connections on this port number */\r
534                 uint16_t usBacklog;             /* In case of a listening socket: maximum number of concurrent connections on this port number */\r
535                 uint8_t ucRepCount;             /* Send repeat count, for retransmissions\r
536                                                                  * This counter is separate from the xmitCount in the\r
537                                                                  * TCP win segments */\r
538                 uint8_t ucTCPState;             /* TCP state: see eTCP_STATE */\r
539                 struct XSOCKET *pxPeerSocket;   /* for server socket: child, for child socket: parent */\r
540                 #if( ipconfigTCP_KEEP_ALIVE == 1 )\r
541                         uint8_t ucKeepRepCount;\r
542                         TickType_t xLastAliveTime;\r
543                 #endif /* ipconfigTCP_KEEP_ALIVE */\r
544                 #if( ipconfigTCP_HANG_PROTECTION == 1 )\r
545                         TickType_t xLastActTime;\r
546                 #endif /* ipconfigTCP_HANG_PROTECTION */\r
547                 size_t uxLittleSpace;\r
548                 size_t uxEnoughSpace;\r
549                 size_t uxRxStreamSize;\r
550                 size_t uxTxStreamSize;\r
551                 StreamBuffer_t *rxStream;\r
552                 StreamBuffer_t *txStream;\r
553                 #if( ipconfigUSE_TCP_WIN == 1 )\r
554                         NetworkBufferDescriptor_t *pxAckMessage;\r
555                 #endif /* ipconfigUSE_TCP_WIN */\r
556                 /* Buffer space to store the last TCP header received. */\r
557                 LastTCPPacket_t xPacket;\r
558                 uint8_t tcpflags;               /* TCP flags */\r
559                 #if( ipconfigUSE_TCP_WIN != 0 )\r
560                         uint8_t ucMyWinScaleFactor;\r
561                         uint8_t ucPeerWinScaleFactor;\r
562                 #endif\r
563                 #if( ipconfigUSE_CALLBACKS == 1 )\r
564                         FOnTCPReceive_t pxHandleReceive;        /*\r
565                                                                                                  * In case of a TCP socket:\r
566                                                                                                  * typedef void (* FOnTCPReceive_t) (Socket_t xSocket, void *pData, size_t xLength );\r
567                                                                                                  */\r
568                         FOnTCPSent_t pxHandleSent;\r
569                         FOnConnected_t pxHandleConnected;       /* Actually type: typedef void (* FOnConnected_t) (Socket_t xSocket, BaseType_t ulConnected ); */\r
570                 #endif /* ipconfigUSE_CALLBACKS */\r
571                 uint32_t ulWindowSize;          /* Current Window size advertised by peer */\r
572                 uint32_t ulRxCurWinSize;        /* Constantly changing: this is the current size available for data reception */\r
573                 size_t uxRxWinSize;     /* Fixed value: size of the TCP reception window */\r
574                 size_t uxTxWinSize;     /* Fixed value: size of the TCP transmit window */\r
575 \r
576                 TCPWindow_t xTCPWindow;\r
577         } IPTCPSocket_t;\r
578 \r
579 #endif /* ipconfigUSE_TCP */\r
580 \r
581 typedef struct UDPSOCKET\r
582 {\r
583         List_t xWaitingPacketsList;     /* Incoming packets */\r
584         #if( ipconfigUDP_MAX_RX_PACKETS > 0 )\r
585                 UBaseType_t uxMaxPackets; /* Protection: limits the number of packets buffered per socket */\r
586         #endif /* ipconfigUDP_MAX_RX_PACKETS */\r
587         #if( ipconfigUSE_CALLBACKS == 1 )\r
588                 FOnUDPReceive_t pxHandleReceive;        /*\r
589                                                                                          * In case of a UDP socket:\r
590                                                                                          * typedef void (* FOnUDPReceive_t) (Socket_t xSocket, void *pData, size_t xLength, struct freertos_sockaddr *pxAddr );\r
591                                                                                          */\r
592                 FOnUDPSent_t pxHandleSent;\r
593         #endif /* ipconfigUSE_CALLBACKS */\r
594 } IPUDPSocket_t;\r
595 \r
596 typedef enum eSOCKET_EVENT {\r
597         eSOCKET_RECEIVE = 0x0001,\r
598         eSOCKET_SEND    = 0x0002,\r
599         eSOCKET_ACCEPT  = 0x0004,\r
600         eSOCKET_CONNECT = 0x0008,\r
601         eSOCKET_BOUND   = 0x0010,\r
602         eSOCKET_CLOSED  = 0x0020,\r
603         eSOCKET_INTR    = 0x0040,\r
604         eSOCKET_ALL             = 0x007F,\r
605 } eSocketEvent_t;\r
606 \r
607 typedef struct XSOCKET\r
608 {\r
609         EventBits_t xEventBits;\r
610         EventGroupHandle_t xEventGroup;\r
611 \r
612         ListItem_t xBoundSocketListItem; /* Used to reference the socket from a bound sockets list. */\r
613         TickType_t xReceiveBlockTime; /* if recv[to] is called while no data is available, wait this amount of time. Unit in clock-ticks */\r
614         TickType_t xSendBlockTime; /* if send[to] is called while there is not enough space to send, wait this amount of time. Unit in clock-ticks */\r
615 \r
616         uint16_t usLocalPort;           /* Local port on this machine */\r
617         uint8_t ucSocketOptions;\r
618         uint8_t ucProtocol; /* choice of FREERTOS_IPPROTO_UDP/TCP */\r
619         #if( ipconfigSOCKET_HAS_USER_SEMAPHORE == 1 )\r
620                 SemaphoreHandle_t pxUserSemaphore;\r
621         #endif /* ipconfigSOCKET_HAS_USER_SEMAPHORE */\r
622         #if( ipconfigSOCKET_HAS_USER_WAKE_CALLBACK == 1 )\r
623                 SocketWakeupCallback_t pxUserWakeCallback;\r
624         #endif /* ipconfigSOCKET_HAS_USER_WAKE_CALLBACK */\r
625 \r
626         #if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )\r
627                 struct xSOCKET_SET *pxSocketSet;\r
628                 /* User may indicate which bits are interesting for this socket. */\r
629                 EventBits_t xSelectBits;\r
630                 /* These bits indicate the events which have actually occurred.\r
631                 They are maintained by the IP-task */\r
632                 EventBits_t xSocketBits;\r
633         #endif /* ipconfigSUPPORT_SELECT_FUNCTION */\r
634         /* TCP/UDP specific fields: */\r
635         /* Before accessing any member of this structure, it should be confirmed */\r
636         /* that the protocol corresponds with the type of structure */\r
637 \r
638         union\r
639         {\r
640                 IPUDPSocket_t xUDP;\r
641                 #if( ipconfigUSE_TCP == 1 )\r
642                         IPTCPSocket_t xTCP;\r
643                         /* Make sure that xTCP is 8-bytes aligned by\r
644                         declaring a 64-bit variable in the same union */\r
645                         uint64_t ullTCPAlignment;\r
646                 #endif /* ipconfigUSE_TCP */\r
647         } u;\r
648 } FreeRTOS_Socket_t;\r
649 \r
650 #if( ipconfigUSE_TCP == 1 )\r
651         /*\r
652          * Lookup a TCP socket, using a multiple matching: both port numbers and\r
653          * return IP address.\r
654          */\r
655         FreeRTOS_Socket_t *pxTCPSocketLookup( uint32_t ulLocalIP, UBaseType_t uxLocalPort, uint32_t ulRemoteIP, UBaseType_t uxRemotePort );\r
656 \r
657 #endif /* ipconfigUSE_TCP */\r
658 \r
659 /*\r
660  * Look up a local socket by finding a match with the local port.\r
661  */\r
662 FreeRTOS_Socket_t *pxUDPSocketLookup( UBaseType_t uxLocalPort );\r
663 \r
664 /*\r
665  * Called when the application has generated a UDP packet to send.\r
666  */\r
667 void vProcessGeneratedUDPPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer );\r
668 \r
669 /*\r
670  * Calculate the upper-layer checksum\r
671  * Works both for UDP, ICMP and TCP packages\r
672  * bOut = true: checksum will be set in outgoing packets\r
673  * bOut = false: checksum will be calculated for incoming packets\r
674  *     returning 0xffff means: checksum was correct\r
675  */\r
676 uint16_t usGenerateProtocolChecksum( const uint8_t * const pucEthernetBuffer, BaseType_t xOutgoingPacket );\r
677 \r
678 /*\r
679  * An Ethernet frame has been updated (maybe it was an ARP request or a PING\r
680  * request?) and is to be sent back to its source.\r
681  */\r
682 void vReturnEthernetFrame( NetworkBufferDescriptor_t * pxNetworkBuffer, BaseType_t xReleaseAfterSend );\r
683 \r
684 /*\r
685  * The internal version of bind()\r
686  * If 'ulInternal' is true, it is called by the driver\r
687  * The TCP driver needs to bind a socket at the moment a listening socket\r
688  * creates a new connected socket\r
689  */\r
690 BaseType_t vSocketBind( FreeRTOS_Socket_t *pxSocket, struct freertos_sockaddr * pxAddress, size_t uxAddressLength, BaseType_t xInternal );\r
691 \r
692 /*\r
693  * Internal function to add streaming data to a TCP socket. If ulIn == true,\r
694  * data will be added to the rxStream, otherwise to the tXStream.  Normally data\r
695  * will be written with ulOffset == 0, meaning: at the end of the FIFO.  When\r
696  * packet come in out-of-order, an offset will be used to put it in front and\r
697  * the head will not change yet.\r
698  */\r
699 int32_t lTCPAddRxdata(FreeRTOS_Socket_t *pxSocket, size_t uxOffset, const uint8_t *pcData, uint32_t ulByteCount);\r
700 \r
701 /*\r
702  * Currently called for any important event.\r
703  */\r
704 void vSocketWakeUpUser( FreeRTOS_Socket_t *pxSocket );\r
705 \r
706 /*\r
707  * Some helping function, their meaning should be clear\r
708  */\r
709 static portINLINE uint32_t ulChar2u32 (const uint8_t *apChr);\r
710 static portINLINE uint32_t ulChar2u32 (const uint8_t *apChr)\r
711 {\r
712         return  ( ( ( uint32_t )apChr[0] ) << 24) |\r
713                         ( ( ( uint32_t )apChr[1] ) << 16) |\r
714                         ( ( ( uint32_t )apChr[2] ) << 8) |\r
715                         ( ( ( uint32_t )apChr[3] ) );\r
716 }\r
717 \r
718 static portINLINE uint16_t usChar2u16 (const uint8_t *apChr);\r
719 static portINLINE uint16_t usChar2u16 (const uint8_t *apChr)\r
720 {\r
721         return ( uint16_t )\r
722                         ( ( ( ( uint32_t )apChr[0] ) << 8) |\r
723                           ( ( ( uint32_t )apChr[1] ) ) );\r
724 }\r
725 \r
726 /* Check a single socket for retransmissions and timeouts */\r
727 BaseType_t xTCPSocketCheck( FreeRTOS_Socket_t *pxSocket );\r
728 \r
729 BaseType_t xTCPCheckNewClient( FreeRTOS_Socket_t *pxSocket );\r
730 \r
731 /* Defined in FreeRTOS_Sockets.c\r
732  * Close a socket\r
733  */\r
734 void *vSocketClose( FreeRTOS_Socket_t *pxSocket );\r
735 \r
736 /*\r
737  * Send the event eEvent to the IP task event queue, using a block time of\r
738  * zero.  Return pdPASS if the message was sent successfully, otherwise return\r
739  * pdFALSE.\r
740 */\r
741 BaseType_t xSendEventToIPTask( eIPEvent_t eEvent );\r
742 \r
743 /*\r
744  * The same as above, but a struct as a parameter, containing:\r
745  *              eIPEvent_t eEventType;\r
746  *              void *pvData;\r
747  */\r
748 BaseType_t xSendEventStructToIPTask( const IPStackEvent_t *pxEvent, TickType_t xTimeout );\r
749 \r
750 /*\r
751  * Returns a pointer to the original NetworkBuffer from a pointer to a UDP\r
752  * payload buffer.\r
753  */\r
754 NetworkBufferDescriptor_t *pxUDPPayloadBuffer_to_NetworkBuffer( void *pvBuffer );\r
755 \r
756 #if( ipconfigZERO_COPY_TX_DRIVER != 0 )\r
757         /*\r
758          * For the case where the network driver passes a buffer directly to a DMA\r
759          * descriptor, this function can be used to translate a 'network buffer' to\r
760          * a 'network buffer descriptor'.\r
761          */\r
762         NetworkBufferDescriptor_t *pxPacketBuffer_to_NetworkBuffer( const void *pvBuffer );\r
763 #endif\r
764 \r
765 /*\r
766  * Internal: Sets a new state for a TCP socket and performs the necessary\r
767  * actions like calling a OnConnected handler to notify the socket owner.\r
768  */\r
769 #if( ipconfigUSE_TCP == 1 )\r
770         void vTCPStateChange( FreeRTOS_Socket_t *pxSocket, enum eTCP_STATE eTCPState );\r
771 #endif /* ipconfigUSE_TCP */\r
772 \r
773 /*_RB_ Should this be part of the public API? */\r
774 void FreeRTOS_netstat( void );\r
775 \r
776 /* Returns pdTRUE is this function is called from the IP-task */\r
777 BaseType_t xIsCallingFromIPTask( void );\r
778 \r
779 #if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )\r
780 \r
781 typedef struct xSOCKET_SET\r
782 {\r
783         EventGroupHandle_t xSelectGroup;\r
784         BaseType_t bApiCalled;  /* True if the API was calling  the private vSocketSelect */\r
785         FreeRTOS_Socket_t *pxSocket;\r
786 } SocketSelect_t;\r
787 \r
788 extern void vSocketSelect( SocketSelect_t *pxSocketSelect );\r
789 \r
790 #endif /* ipconfigSUPPORT_SELECT_FUNCTION */\r
791 \r
792 void vIPSetDHCPTimerEnableState( BaseType_t xEnableState );\r
793 void vIPReloadDHCPTimer( uint32_t ulLeaseTime );\r
794 #if( ipconfigDNS_USE_CALLBACKS != 0 )\r
795         void vIPReloadDNSTimer( uint32_t ulCheckTime );\r
796         void vIPSetDnsTimerEnableState( BaseType_t xEnableState );\r
797 #endif\r
798 \r
799 /* Send the network-up event and start the ARP timer. */\r
800 void vIPNetworkUpCalls( void );\r
801 \r
802 #ifdef __cplusplus\r
803 } /* extern "C" */\r
804 #endif\r
805 \r
806 #endif /* FREERTOS_IP_PRIVATE_H */\r
807 \r
808 \r
809 \r
810 \r
811 \r
812 \r
813 \r
814 \r
815 \r
816 \r
817 \r
818 \r
819 \r