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