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