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