1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Multiple TCP Socket/TLS Session support module                      */
19 /**                                                                       */
20 /**************************************************************************/
21 
22 /**************************************************************************/
23 /*                                                                        */
24 /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
25 /*                                                                        */
26 /*    nx_tcpserver.h                                      PORTABLE C      */
27 /*                                                           6.1.11       */
28 /*  AUTHOR                                                                */
29 /*                                                                        */
30 /*    Yuxin Zhou, Microsoft Corporation                                   */
31 /*                                                                        */
32 /*  DESCRIPTION                                                           */
33 /*                                                                        */
34 /*    This file defines the NetX TCP Server module component,             */
35 /*    including all data types and external references.                   */
36 /*                                                                        */
37 /*  RELEASE HISTORY                                                       */
38 /*                                                                        */
39 /*    DATE              NAME                      DESCRIPTION             */
40 /*                                                                        */
41 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
42 /*  09-30-2020     Yuxin Zhou               Modified comment(s), and      */
43 /*                                            fixed packet leak issue,    */
44 /*                                            resulting in version 6.1    */
45 /*  10-15-2021     Yuxin Zhou               Modified comment(s), and      */
46 /*                                            deprecated unused macros,   */
47 /*                                            resulting in version 6.1.9  */
48 /*  04-25-2022     Yuxin Zhou               Modified comment(s), and      */
49 /*                                            supported ECC configuration,*/
50 /*                                            resulting in version 6.1.11 */
51 /*                                                                        */
52 /**************************************************************************/
53 
54 #ifndef NX_TCPSERVER_H
55 #define NX_TCPSERVER_H
56 
57 #include "tx_api.h"
58 #include "nx_api.h"
59 #ifdef NX_WEB_HTTPS_ENABLE
60 
61 /* Enable TLS for the TCPServer module to support HTTPS */
62 #ifndef NX_TCPSERVER_ENABLE_TLS
63 #define NX_TCPSERVER_ENABLE_TLS
64 #endif
65 
66 #include "nx_secure_tls_api.h"
67 #endif /* NX_WEB_HTTPS_ENABLE */
68 
69 /* Deprecated. This symbol is defined for compatibility. */
70 #ifndef NX_TCPSERVER_ACCEPT_WAIT
71 #define NX_TCPSERVER_ACCEPT_WAIT 1
72 #endif /* NX_TCPSERVER_ACCEPT_WAIT */
73 
74 /* Deprecated. This symbol is defined for compatibility. */
75 #ifndef NX_TCPSERVER_DISCONNECT_WAIT
76 #define NX_TCPSERVER_DISCONNECT_WAIT 1
77 #endif /* NX_TCPSERVER_DISCONNECT_WAIT */
78 
79 /* Deprecated. This symbol is defined for compatibility. */
80 #ifndef NX_TCPSERVER_PRIORITY
81 #define NX_TCPSERVER_PRIORITY 4
82 #endif /* NX_TCPSERVER_PRIORITY */
83 
84 #ifndef NX_TCPSERVER_TIMEOUT_PERIOD
85 #define NX_TCPSERVER_TIMEOUT_PERIOD 1
86 #endif /* NX_TCPSERVER_TIMEOUT_PERIOD */
87 
88 /* Define thread events. */
89 #define NX_TCPSERVER_CONNECT            0x00000001
90 #define NX_TCPSERVER_DATA               0x00000002
91 #define NX_TCPSERVER_DISCONNECT         0x00000004
92 #define NX_TCPSERVER_TIMEOUT            0x00000008
93 #define NX_TCPSERVER_ANY_EVENT          0xFFFFFFFF
94 
95 /* ERROR code */
96 #define NX_TCPSERVER_FAIL 0x01
97 
98 /* TCP Server session structure - contains individual
99    TCP sockets and TLS sessions. */
100 typedef struct NX_TCP_SESSION_STRUCT
101 {
102     /* TCP socket used for this session. */
103     NX_TCP_SOCKET           nx_tcp_session_socket;
104 
105     /* Expiration timeout for this socket. */
106     ULONG                   nx_tcp_session_expiration;
107 
108     /* Connection flag. */
109     UINT                    nx_tcp_session_connected;
110 
111     /* Reserved value for passing data to/from individual sessions. */
112     ULONG                   nx_tcp_session_reserved;
113 
114 #ifdef NX_TCPSERVER_ENABLE_TLS
115     /* Flag set to NX_TRUE if using TLS. */
116     UINT                    nx_tcp_session_using_tls;
117 
118     /* If TLS is enabled, we also have a TLS session to maintain. */
119     NX_SECURE_TLS_SESSION   nx_tcp_session_tls_session;
120 #endif
121 
122 } NX_TCP_SESSION;
123 
124 /* TCPSERVER structure */
125 typedef struct NX_TCPSERVER_STRUCT
126 {
127     NX_IP                  *nx_tcpserver_ip;
128     NX_TCP_SESSION         *nx_tcpserver_sessions;
129     UINT                    nx_tcpserver_sessions_count;
130     UINT                    nx_tcpserver_listen_port;
131     NX_TCP_SESSION         *nx_tcpserver_listen_session;
132     TX_THREAD               nx_tcpserver_thread;
133     TX_TIMER                nx_tcpserver_timer;
134     TX_EVENT_FLAGS_GROUP    nx_tcpserver_event_flags;
135     ULONG                   nx_tcpserver_timeout;
136     ULONG                   nx_tcpserver_accept_wait_option;
137     VOID                  (*nx_tcpserver_new_connection)(struct NX_TCPSERVER_STRUCT *server_ptr, NX_TCP_SESSION *session_ptr);
138     VOID                  (*nx_tcpserver_receive_data)(struct NX_TCPSERVER_STRUCT *server_ptr, NX_TCP_SESSION *session_ptr);
139     VOID                  (*nx_tcpserver_connection_end)(struct NX_TCPSERVER_STRUCT *server_ptr, NX_TCP_SESSION *session_ptr);
140     VOID                  (*nx_tcpserver_connection_timeout)(struct NX_TCPSERVER_STRUCT *server_ptr, NX_TCP_SESSION *session_ptr);
141     ULONG                   nx_tcpserver_reserved;
142 } NX_TCPSERVER;
143 
144 
145 #ifndef NX_TCPSERVER_SOURCE_CODE
146 
147 /* APIs */
148 #define nx_tcpserver_create         _nx_tcpserver_create
149 #define nx_tcpserver_start          _nx_tcpserver_start
150 #define nx_tcpserver_stop           _nx_tcpserver_stop
151 #define nx_tcpserver_delete         _nx_tcpserver_delete
152 #ifdef NX_TCPSERVER_ENABLE_TLS
153 #define nx_tcpserver_tls_setup      _nx_tcpserver_tls_setup
154 #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
155 #define nx_tcpserver_tls_ecc_setup  _nx_tcpserver_tls_ecc_setup
156 #endif
157 #endif
158 
159 #ifdef NX_TCPSERVER_ENABLE_TLS
160 UINT nx_tcpserver_tls_setup(NX_TCPSERVER *server_ptr, const NX_SECURE_TLS_CRYPTO *crypto_table,
161                             VOID *metadata_buffer, ULONG metadata_size, UCHAR* packet_buffer, UINT packet_buffer_size, NX_SECURE_X509_CERT *identity_certificate,
162                             NX_SECURE_X509_CERT *trusted_certificates[], UINT trusted_certs_num, NX_SECURE_X509_CERT *remote_certificates[], UINT remote_certs_num,
163                             UCHAR *remote_certificate_buffer, UINT remote_cert_buffer_size);
164 #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
165 UINT nx_tcpserver_tls_ecc_setup(NX_TCPSERVER *server_ptr,
166                                 const USHORT *supported_groups, USHORT supported_group_count,
167                                 const NX_CRYPTO_METHOD **curves);
168 #endif
169 #endif
170 
171 UINT nx_tcpserver_create(NX_IP *ip_ptr, NX_TCPSERVER *server_ptr, CHAR *name,
172                          ULONG type_of_service, ULONG fragment, UINT time_to_live, ULONG window_size,
173                          VOID (*new_connection)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
174                          VOID (*receive_data)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
175                          VOID (*connection_end)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
176                          VOID (*connection_timeout)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
177                          ULONG timeout, VOID *stack_ptr, UINT stack_size,
178                          VOID *sessions_buffer, UINT buffer_size, UINT thread_priority, ULONG accept_wait_option);
179 
180 
181 UINT nx_tcpserver_start(NX_TCPSERVER *server_ptr, UINT port, UINT listen_queue_size);
182 
183 UINT nx_tcpserver_stop(NX_TCPSERVER *server_ptr);
184 
185 UINT nx_tcpserver_delete(NX_TCPSERVER *server_ptr);
186 
187 #else
188 
189 #ifdef NX_TCPSERVER_ENABLE_TLS
190 UINT _nx_tcpserver_tls_setup(NX_TCPSERVER *server_ptr, const NX_SECURE_TLS_CRYPTO *crypto_table,
191                              VOID *metadata_buffer, ULONG metadata_size, UCHAR* packet_buffer, UINT packet_buffer_size, NX_SECURE_X509_CERT *identity_certificate,
192                              NX_SECURE_X509_CERT *trusted_certificates[], UINT trusted_certs_num, NX_SECURE_X509_CERT *remote_certificates[], UINT remote_certs_num,
193                              UCHAR *remote_certificate_buffer, UINT remote_cert_buffer_size);
194 #ifdef NX_SECURE_ENABLE_ECC_CIPHERSUITE
195 UINT _nx_tcpserver_tls_ecc_setup(NX_TCPSERVER *server_ptr,
196                                  const USHORT *supported_groups, USHORT supported_group_count,
197                                  const NX_CRYPTO_METHOD **curves);
198 #endif
199 #endif
200 
201 UINT _nx_tcpserver_create(NX_IP *ip_ptr, NX_TCPSERVER *server_ptr, CHAR *name,
202                           ULONG type_of_service, ULONG fragment, UINT time_to_live, ULONG window_size,
203                           VOID (*new_connection)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
204                           VOID (*receive_data)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
205                           VOID (*connection_end)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
206                           VOID (*connection_timeout)(NX_TCPSERVER *server_ptr, NX_TCP_SESSION *session_ptr),
207                           ULONG timeout, VOID *stack_ptr, UINT stack_size,
208                           VOID *sessions_buffer, UINT buffer_size, UINT thread_priority, ULONG accept_wait_option);
209 
210 
211 UINT _nx_tcpserver_start(NX_TCPSERVER *server_ptr, UINT port, UINT listen_queue_size);
212 
213 UINT _nx_tcpserver_stop(NX_TCPSERVER *server_ptr);
214 
215 UINT _nx_tcpserver_delete(NX_TCPSERVER *server_ptr);
216 
217 #endif
218 
219 #endif /* NX_TCPSERVER_H */
220