Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_tcp.h"
...
...
_nx_tcp_socket_state_syn_sent(NX_TCP_SOCKET *, NX_TCP_HEADER *, NX_PACKET *)
Files
loading...
SourceVuSTM32 Libraries and Samplesnetxduocommon/src/nx_tcp_socket_state_syn_sent.c
 
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Component */ /** */ /** Transmission Control Protocol (TCP) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SOURCE_CODE /* Include necessary system files. */ #include "nx_api.h" #include "nx_tcp.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_tcp_socket_state_syn_sent PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function processes packets during the SYN SENT state, which is */ /* the state of the socket immediately after the initial SYN is sent */ /* in the establishment of a TCP connection. We are expecting a SYN */ /* and an ACK from the other side of the connection in order to move */ /* into an established state. */ /* */ /* INPUT */ /* */ /* socket_ptr Pointer to owning socket */ /* tcp_header_ptr Pointer to packet header */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* _nx_tcp_packet_send_ack Send ACK packet */ /* _nx_tcp_packet_send_syn Send SYN packet */ /* _nx_tcp_packet_send_rst Send RST packet */ /* _nx_tcp_socket_thread_resume Resume suspended thread */ /* */ /* CALLED BY */ /* */ /* _nx_tcp_socket_packet_process Process TCP packet for socket */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */ /* 09-30-2020 Yuxin Zhou Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ VOID _nx_tcp_socket_state_syn_sent(NX_TCP_SOCKET *socket_ptr, NX_TCP_HEADER *tcp_header_ptr, NX_PACKET *packet_ptr) { #ifndef TX_ENABLE_EVENT_TRACE NX_PARAMETER_NOT_USED(packet_ptr); #endif /* TX_ENABLE_EVENT_TRACE */ /* Check if a RST is present. */ if (tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_RST_BIT) { /* Check if the ACK was acceptable. According to RFC 793, Section 3.9, Page 67. */ if ((tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT) && (tcp_header_ptr -> nx_tcp_acknowledgment_number == socket_ptr -> nx_tcp_socket_tx_sequence)) { #ifndef NX_DISABLE_TCP_INFO /* Increment the resets received count. */ (socket_ptr -> nx_tcp_socket_ip_ptr) -> nx_ip_tcp_resets_received++;/* ... */ #endif /* If trace is enabled, insert this event into the trace buffer. */ NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_RESET_RECEIVE, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, packet_ptr, tcp_header_ptr -> nx_tcp_sequence_number, NX_TRACE_INTERNAL_EVENTS, 0, 0); /* Reset connection. */ _nx_tcp_socket_connection_reset(socket_ptr); }if ((tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT) && (tcp_header_ptr -> nx_tcp_acknowledgment_number == socket_ptr -> nx_tcp_socket_tx_sequence)) { ... } /* Finished processing, simply return! */ return; }if (tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_RST_BIT) { ... } /* Determine if a valid SYN/ACK is present. */ else if ((tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_SYN_BIT) && (tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT) && (tcp_header_ptr -> nx_tcp_acknowledgment_number == socket_ptr -> nx_tcp_socket_tx_sequence)) { /* Yes, this is a proper SYN/ACK message. We need to send an ACK back the other direction before we go into the ESTABLISHED state. *//* ... */ /* Save the sequence number. */ socket_ptr -> nx_tcp_socket_rx_sequence = tcp_header_ptr -> nx_tcp_sequence_number + 1; /* Save the window size. */ socket_ptr -> nx_tcp_socket_tx_window_advertised = tcp_header_ptr -> nx_tcp_header_word_3 & NX_LOWER_16_MASK; #ifdef NX_ENABLE_TCP_WINDOW_SCALING /* The window size advertised in the SYN packet is NEVER scaled. Therefore there is no need to apply the scale shift. However validate snd_win_scale and rcv_win_scale. *//* ... */ if (socket_ptr -> nx_tcp_snd_win_scale_value == 0xFF) { /* Peer does not support window scale option. */ socket_ptr -> nx_tcp_snd_win_scale_value = 0; socket_ptr -> nx_tcp_rcv_win_scale_value = 0; /* Since the peer does not offer window scaling feature, make sure our default window size for this connection does not exceed 65535 bytes. *//* ... */ if (socket_ptr -> nx_tcp_socket_rx_window_maximum > 65535) { socket_ptr -> nx_tcp_socket_rx_window_default = 65535; socket_ptr -> nx_tcp_socket_rx_window_current = 65535; }if (socket_ptr -> nx_tcp_socket_rx_window_maximum > 65535) { ... } }if (socket_ptr -> nx_tcp_snd_win_scale_value == 0xFF) { ... } /* ... */ #endif /* NX_ENABLE_TCP_WINDOW_SCALING */ /* Initialize the slow start threshold to be the advertised window size. */ socket_ptr -> nx_tcp_socket_tx_slow_start_threshold = socket_ptr -> nx_tcp_socket_tx_window_advertised; /* Set the Initial transmit outstanding byte count. */ socket_ptr -> nx_tcp_socket_tx_outstanding_bytes = 0; /* Set the initial congestion control window size. */ /* Section 3.1, Page 5, RFC5681. */ if (socket_ptr -> nx_tcp_socket_timeout_retries > 0) { /* Set the initial congestion control window size to be the mss. */ socket_ptr -> nx_tcp_socket_tx_window_congestion = socket_ptr -> nx_tcp_socket_connect_mss; }if (socket_ptr -> nx_tcp_socket_timeout_retries > 0) { ... } else { socket_ptr -> nx_tcp_socket_tx_window_congestion = (socket_ptr -> nx_tcp_socket_connect_mss << 2); if (socket_ptr -> nx_tcp_socket_connect_mss > 1095) { socket_ptr -> nx_tcp_socket_tx_window_congestion -= socket_ptr -> nx_tcp_socket_connect_mss; }if (socket_ptr -> nx_tcp_socket_connect_mss > 1095) { ... } if (socket_ptr -> nx_tcp_socket_connect_mss > 2190) { socket_ptr -> nx_tcp_socket_tx_window_congestion -= socket_ptr -> nx_tcp_socket_connect_mss; }if (socket_ptr -> nx_tcp_socket_connect_mss > 2190) { ... } }else { ... } /* Send the ACK. */ _nx_tcp_packet_send_ack(socket_ptr, socket_ptr -> nx_tcp_socket_tx_sequence); /* If trace is enabled, insert this event into the trace buffer. */ NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_ESTABLISHED, NX_TRACE_INTERNAL_EVENTS, 0, 0); /* Move to the ESTABLISHED state. */ socket_ptr -> nx_tcp_socket_state = NX_TCP_ESTABLISHED; /* Clear the socket timeout. */ socket_ptr -> nx_tcp_socket_timeout = 0; #ifndef NX_DISABLE_EXTENDED_NOTIFY_SUPPORT /* Is a connection completion callback registered with the TCP socket? */ if (socket_ptr -> nx_tcp_establish_notify) { /* Call the application's establish callback function. */ (socket_ptr -> nx_tcp_establish_notify)(socket_ptr); }if (socket_ptr -> nx_tcp_establish_notify) { ... } /* ... */#endif #ifdef NX_ENABLE_TCP_KEEPALIVE /* Is the keepalive feature enabled on this socket? */ if (socket_ptr -> nx_tcp_socket_keepalive_enabled) { /* Setup the TCP Keepalive timer to initial values. */ socket_ptr -> nx_tcp_socket_keepalive_timeout = NX_TCP_KEEPALIVE_INITIAL; socket_ptr -> nx_tcp_socket_keepalive_retries = 0; }if (socket_ptr -> nx_tcp_socket_keepalive_enabled) { ... } /* ... */#endif /* Determine if we need to wake a thread suspended on the connection. */ if (socket_ptr -> nx_tcp_socket_connect_suspended_thread) { /* Resume the suspended thread. */ _nx_tcp_socket_thread_resume(&(socket_ptr -> nx_tcp_socket_connect_suspended_thread), NX_SUCCESS); }if (socket_ptr -> nx_tcp_socket_connect_suspended_thread) { ... } }else if ((tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_SYN_BIT) && (tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT) && (tcp_header_ptr -> nx_tcp_acknowledgment_number == socket_ptr -> nx_tcp_socket_tx_sequence)) { ... } else if ((tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_SYN_BIT) && (!(tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT))) { /* Simultaneous Connection Synchronization, A SYN message was received. We need to send both a SYN and ACK and move to the SYN RECEIVED state. *//* ... */ /* Save the sequence number. */ socket_ptr -> nx_tcp_socket_rx_sequence = tcp_header_ptr -> nx_tcp_sequence_number + 1; /* Save the window size. */ socket_ptr -> nx_tcp_socket_tx_window_advertised = tcp_header_ptr -> nx_tcp_header_word_3 & NX_LOWER_16_MASK; #ifdef NX_ENABLE_TCP_WINDOW_SCALING socket_ptr -> nx_tcp_socket_tx_window_advertised <<= socket_ptr -> nx_tcp_rcv_win_scale_value; #endif /* NX_ENABLE_TCP_WINDOW_SCALING */ /* Initialize the slow start threshold to be the advertised window size. */ socket_ptr -> nx_tcp_socket_tx_slow_start_threshold = socket_ptr -> nx_tcp_socket_tx_window_advertised; /* Set the initial congestion control window size. */ /* Section 3.1, Page 5, RFC5681. */ socket_ptr -> nx_tcp_socket_tx_window_congestion = (socket_ptr -> nx_tcp_socket_connect_mss << 2); if (socket_ptr -> nx_tcp_socket_connect_mss > 1095) { socket_ptr -> nx_tcp_socket_tx_window_congestion -= socket_ptr -> nx_tcp_socket_connect_mss; }if (socket_ptr -> nx_tcp_socket_connect_mss > 1095) { ... } if (socket_ptr -> nx_tcp_socket_connect_mss > 2190) { socket_ptr -> nx_tcp_socket_tx_window_congestion -= socket_ptr -> nx_tcp_socket_connect_mss; }if (socket_ptr -> nx_tcp_socket_connect_mss > 2190) { ... } /* Set the Initial transmit outstanding byte count. */ socket_ptr -> nx_tcp_socket_tx_outstanding_bytes = 0; /* If trace is enabled, insert this event into the trace buffer. */ NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_SYN_RECEIVED, NX_TRACE_INTERNAL_EVENTS, 0, 0); /* Move to the SYN RECEIVED state. */ socket_ptr -> nx_tcp_socket_state = NX_TCP_SYN_RECEIVED; /* Clear the timeout. */ socket_ptr -> nx_tcp_socket_timeout = 0; /* Send the SYN packet. */ _nx_tcp_packet_send_syn(socket_ptr, (socket_ptr -> nx_tcp_socket_tx_sequence - 1)); }else if ((tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_SYN_BIT) && (!(tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT))) { ... } /* Check for an invalid response to an attempted connection. */ else if ((tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT) && (tcp_header_ptr -> nx_tcp_acknowledgment_number != socket_ptr -> nx_tcp_socket_tx_sequence)) { /* Invalid response was received, it is likely that the other side still thinks a previous connection is active. Send a reset (RST) message to the other side to clear any previous connection. *//* ... */ /* Send the RST packet. */ _nx_tcp_packet_send_rst(socket_ptr, tcp_header_ptr); }else if ((tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT) && (tcp_header_ptr -> nx_tcp_acknowledgment_number != socket_ptr -> nx_tcp_socket_tx_sequence)) { ... } }{ ... }
Details