xref: /FreeRTOS-Plus-TCP-v4.0.0/source/include/FreeRTOS_IPv6_Sockets.h (revision 14b3e241f530bb5beae65cefb3ce93cfa301b803)
1 /*
2  * FreeRTOS+TCP <DEVELOPMENT BRANCH>
3  * Copyright (C) 2022 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * http://aws.amazon.com/freertos
25  * http://www.FreeRTOS.org
26  */
27 
28 #ifndef FREERTOS_IPV6_SOCKETS_H
29     #define FREERTOS_IPV6_SOCKETS_H
30 
31 /* Standard includes. */
32     #include <string.h>
33 
34 /* FreeRTOS includes. */
35     #include "FreeRTOS.h"
36     #include "FreeRTOS_IP_Common.h"
37 
38     #ifdef __cplusplus
39         extern "C" {
40     #endif
41 
42 /** @brief When ucASCIIToHex() can not convert a character,
43  *         the value 255 will be returned.
44  */
45     #define socketINVALID_HEX_CHAR    ( 0xffU )
46 
47 /** @brief The struct sNTOP6_Set is a set of parameters used by  the function FreeRTOS_inet_ntop6().
48  * It passes this set to a few helper functions. */
49     struct sNTOP6_Set
50     {
51         const uint16_t * pusAddress; /**< The network address, 8 short values. */
52         BaseType_t xZeroStart;       /**< The position of the first byte of the longest train of zero values. */
53         BaseType_t xZeroLength;      /**< The number of short values in the longest train of zero values. */
54         BaseType_t xIndex;           /**< The read index in the array of short values, the network address. */
55         socklen_t uxTargetIndex;     /**< The write index in 'pcDestination'. */
56     };
57 
58 /** @brief The struct sNTOP6_Set is a set of parameters used by  the function FreeRTOS_inet_ntop6().
59  * It passes this set to a few helper functions.
60  */
61     struct sPTON6_Set
62     {
63         uint32_t ulValue;         /**< A 32-bit accumulator, only 16 bits are used. */
64         BaseType_t xHadDigit;     /**< Becomes pdTRUE as soon as ulValue has valid data. */
65         BaseType_t xTargetIndex;  /**< The index in the array pucTarget to write the next byte. */
66         BaseType_t xColon;        /**< The position in the output where the train of zero's will start. */
67         BaseType_t xHighestIndex; /**< The highest allowed value of xTargetIndex. */
68         uint8_t * pucTarget;      /**< The array of bytes in which the resulting IPv6 address is written. */
69     };
70 
71 /**
72  * @brief Convert an ASCII character to its corresponding hexadecimal value.
73  *        Accepted characters are 0-9, a-f, and A-F.
74  */
75     uint8_t ucASCIIToHex( char cChar );
76 
77 /* @brief Converts a hex value to a readable hex character, e.g. 14 becomes 'e'.
78  */
79     char cHexToChar( uint16_t usValue );
80 
81 /** @brief Converts a hex value to a readable hex character, *
82  *         e.g. 14 becomes 'e'.static char cHexToChar( unsigned short usValue );
83  */
84     socklen_t uxHexPrintShort( char * pcBuffer,
85                                size_t uxBufferSize,
86                                uint16_t usValue );
87 
88 /** @brief Scan the binary IPv6 address and find the longest train of consecutive zero's.
89  *         The result of this search will be stored in 'xZeroStart' and 'xZeroLength'.
90  */
91     void prv_ntop6_search_zeros( struct sNTOP6_Set * pxSet );
92 
93 /*
94  * Convert a string like 'fe80::8d11:cd9b:8b66:4a80'
95  * to a 16-byte IPv6 address
96  */
97     const char * FreeRTOS_inet_ntop6( const void * pvSource,
98                                       char * pcDestination,
99                                       socklen_t uxSize );
100 
101 /** @brief Called by pxTCPSocketLookup(), this function will check if a socket
102  *         is connected to a remote IP-address. It will be called from a loop
103  *         iterating through all sockets. */
104     FreeRTOS_Socket_t * pxTCPSocketLookup_IPv6( FreeRTOS_Socket_t * pxSocket,
105                                                 const IPv46_Address_t * pxAddress );
106 
107 /**
108  * @brief Called by prvSendUDPPacket(), this function will UDP packet
109  *        fields and IPv6 address for the packet to be send.
110  */
111     void * xSend_UDP_Update_IPv6( NetworkBufferDescriptor_t * pxNetworkBuffer,
112                                   const struct freertos_sockaddr * pxDestinationAddress );
113 
114 /**
115  * @brief Called by FreeRTOS_recvfrom(), this function will update socket
116  *        address with IPv6 address from the packet received.
117  */
118     size_t xRecv_Update_IPv6( const NetworkBufferDescriptor_t * pxNetworkBuffer,
119                               struct freertos_sockaddr * pxSourceAddress );
120 
121     #ifdef __cplusplus
122 }         /* extern "C" */
123     #endif
124 
125 #endif /* FREERTOS_IPV6_SOCKETS_H */
126