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