Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_ip.h"
...
_nx_ip_route_find(NX_IP *, ULONG, NX_INTERFACE **, ULONG *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduocommon/src/nx_ip_route_find.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 */ /** */ /** Internet Protocol (IP) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SOURCE_CODE /* Include necessary system files. */ #include "nx_api.h" #include "nx_ip.h" #ifndef NX_DISABLE_IPV4... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_ip_route_find PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function finds an outgoing interface and the next hop address */ /* for a given destination address. The caller may also set desired */ /* interface information in the ip_interface_ptr input. For multicast*/ /* or limited broadcast, this routine looks for the first enabled */ /* interface (link up) starting with the primary interface if */ /* a hint was not set by the caller. For directed broadcast or */ /* unicast destinations, the hint is ignored and the proper outgoing */ /* interface is selected. */ /* */ /* INPUT */ /* */ /* ip_ptr IN Pointer to IP instance */ /* destination_address IN Destination address */ /* ip_interface_ptr OUT Interface to use, must point */ /* to valid storage space. */ /* next_hop_address OUT IP address for the next hop, */ /* must point to valid storage */ /* space. */ /* */ /* OUTPUT */ /* */ /* NX_SUCCESS Operation was successful */ /* NX_IP_ADDRESS_ERROR No suitable interface found */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* _nx_arp_dynamic_entry_set.c ARP entry set */ /* _nx_icmp_ping Transmit ICMP echo request */ /* _nx_ip_packet_send IP packet transmit */ /* _nx_tcp_client_socket_connect TCP Client socket connection */ /* _nx_udp_socket_send UDP packet send */ /* */ /* NOTE: */ /* */ /* None */ /* */ /* 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 */ /* */... /**************************************************************************/ ULONG _nx_ip_route_find(NX_IP *ip_ptr, ULONG destination_address, NX_INTERFACE **ip_interface_ptr, ULONG *next_hop_address) { NX_INTERFACE *interface_ptr; ULONG i; /* Initialize the next hop address. */ *next_hop_address = 0; /* Determine if the destination_address is multicast or directed broadcast. */ if (((destination_address & NX_IP_CLASS_D_MASK) == NX_IP_CLASS_D_TYPE) || (destination_address == NX_IP_LIMITED_BROADCAST)) { *next_hop_address = destination_address; /* If the caller did not set the ip_interface value, find a link enabled interface, starting with the primary interface, for transmission. *//* ... */ if (*ip_interface_ptr == NX_NULL) { /* Find an interface whose link is up. */ for (i = 0; i < NX_MAX_PHYSICAL_INTERFACES; i++) { if (ip_ptr -> nx_ip_interface[i].nx_interface_link_up) { *ip_interface_ptr = &(ip_ptr -> nx_ip_interface[i]); return(NX_SUCCESS); }if (ip_ptr -> nx_ip_interface[i].nx_interface_link_up) { ... } }for (i = 0; i < NX_MAX_PHYSICAL_INTERFACES; i++) { ... } }if (*ip_interface_ptr == NX_NULL) { ... } /* If the specified interface is up, return success. */ else if ((*ip_interface_ptr) -> nx_interface_link_up) { return(NX_SUCCESS); }else if ((*ip_interface_ptr) -> nx_interface_link_up) { ... } /* No available interface. */ return(NX_IP_ADDRESS_ERROR); }if (((destination_address & NX_IP_CLASS_D_MASK) == NX_IP_CLASS_D_TYPE) || (destination_address == NX_IP_LIMITED_BROADCAST)) { ... } /* Search through the interfaces associated with the IP instance, check if the the destination address is one of the local interface addresses. *//* ... */ for (i = 0; i < NX_MAX_PHYSICAL_INTERFACES; i++) { /* Use a local variable for convenience. */ interface_ptr = &(ip_ptr -> nx_ip_interface[i]); /* Check for a valid interface that maps onto the same network domain as the destination address. */ if ((interface_ptr -> nx_interface_valid) && (interface_ptr -> nx_interface_link_up) && (interface_ptr -> nx_interface_ip_address == destination_address) && ((*ip_interface_ptr == NX_NULL) || (*ip_interface_ptr == interface_ptr))) { /* Yes, use the entry information for interface and next hop. */ *ip_interface_ptr = interface_ptr; *next_hop_address = destination_address; return(NX_SUCCESS); }if ((interface_ptr -> nx_interface_valid) && (interface_ptr -> nx_interface_link_up) && (interface_ptr -> nx_interface_ip_address == destination_address) && ((*ip_interface_ptr == NX_NULL) || (*ip_interface_ptr == interface_ptr))) { ... } }for (i = 0; i < NX_MAX_PHYSICAL_INTERFACES; i++) { ... } #ifdef NX_ENABLE_IP_STATIC_ROUTING /* Search through the routing table for a suitable interface. */ for (i = 0; i < ip_ptr -> nx_ip_routing_table_entry_count; i++) { /* Get the interface. */ interface_ptr = ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_entry_ip_interface; /* Skip interface that is not up. */ if (interface_ptr -> nx_interface_link_up == NX_FALSE) { continue; }if (interface_ptr -> nx_interface_link_up == NX_FALSE) { ... } /* Does this table entry match the destination table network domain?*/ if (ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_dest_ip == (destination_address & ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_net_mask)) { /* Yes, is next hop address still reachable? */ if (interface_ptr -> nx_interface_ip_network != (ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_next_hop_address & interface_ptr -> nx_interface_ip_network_mask)) { continue; }if (interface_ptr -> nx_interface_ip_network != (ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_next_hop_address & interface_ptr -> nx_interface_ip_network_mask)) { ... } /* Use the entry information for interface and next hop. */ if (*ip_interface_ptr == NX_NULL) { *ip_interface_ptr = interface_ptr; }if (*ip_interface_ptr == NX_NULL) { ... } else if (*ip_interface_ptr != interface_ptr) { continue; }else if (*ip_interface_ptr != interface_ptr) { ... } *next_hop_address = ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_next_hop_address; return(NX_SUCCESS); }if (ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_dest_ip == (destination_address & ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_net_mask)) { ... } }for (i = 0; i < ip_ptr -> nx_ip_routing_table_entry_count; i++) { ... } /* ... */ #endif /* NX_ENABLE_IP_STATIC_ROUTING */ /* Search through the interfaces associated with the IP instance, check if the entry exists. *//* ... */ for (i = 0; i < NX_MAX_IP_INTERFACES; i++) { /* Use a local variable for convenience. */ interface_ptr = &(ip_ptr -> nx_ip_interface[i]); /* Check for a valid interface that maps onto the same network domain as the destination address. */ if ((interface_ptr -> nx_interface_valid) && (interface_ptr -> nx_interface_link_up) && ((interface_ptr -> nx_interface_ip_network_mask & destination_address) == interface_ptr -> nx_interface_ip_network)) { /* Yes, use the entry information for interface and next hop. */ if (*ip_interface_ptr == NX_NULL) { *ip_interface_ptr = interface_ptr; }if (*ip_interface_ptr == NX_NULL) { ... } /* Match loopback interface. */ /* Suppress constant value, since "NX_MAX_IP_INTERFACES" can be redefined. */ #if (NX_MAX_IP_INTERFACES == (NX_MAX_PHYSICAL_INTERFACES + 1)) else if (i == NX_MAX_PHYSICAL_INTERFACES) { *ip_interface_ptr = interface_ptr; }else if (i == NX_MAX_PHYSICAL_INTERFACES) { ... } /* ... */#endif else if (*ip_interface_ptr != interface_ptr) { continue; }else if (*ip_interface_ptr != interface_ptr) { ... } *next_hop_address = destination_address; return(NX_SUCCESS); }if ((interface_ptr -> nx_interface_valid) && (interface_ptr -> nx_interface_link_up) && ((interface_ptr -> nx_interface_ip_network_mask & destination_address) == interface_ptr -> nx_interface_ip_network)) { ... } }for (i = 0; i < NX_MAX_IP_INTERFACES; i++) { ... } /* Search the interfaces for IPv4 Link-Local Address according to RFC3927, section2.6. */ /* Determine if destination addrss is link-local address(169.254/16 Hexadecimal:0xA9FE0000). */ if ((destination_address & 0xFFFF0000) == 0xA9FE0000) { /* Yes, check if the interface is set. */ if (*ip_interface_ptr) { /* Determine if the interface is valid. */ if (((*ip_interface_ptr) -> nx_interface_valid) && ((*ip_interface_ptr) -> nx_interface_link_up)) { /* Set the next hop address. */ *next_hop_address = destination_address; return(NX_SUCCESS); }if (((*ip_interface_ptr) -> nx_interface_valid) && ((*ip_interface_ptr) -> nx_interface_link_up)) { ... } }if (*ip_interface_ptr) { ... } else { /* Search through the interfaces associated with the IP instance, set the inteface as first valid interface. */ for (i = 0; i < NX_MAX_IP_INTERFACES; i++) { /* Check for a valid interface that the address is link-local address. */ if ((ip_ptr -> nx_ip_interface[i].nx_interface_valid) && (ip_ptr -> nx_ip_interface[i].nx_interface_link_up)) { /* Yes, use the entry information for interface and next hop. */ *ip_interface_ptr = &(ip_ptr -> nx_ip_interface[i]); *next_hop_address = destination_address; return(NX_SUCCESS); }if ((ip_ptr -> nx_ip_interface[i].nx_interface_valid) && (ip_ptr -> nx_ip_interface[i].nx_interface_link_up)) { ... } }for (i = 0; i < NX_MAX_IP_INTERFACES; i++) { ... } }else { ... } }if ((destination_address & 0xFFFF0000) == 0xA9FE0000) { ... } /* Does the IP instance have a gateway? */ if ((ip_ptr -> nx_ip_gateway_address) && (ip_ptr -> nx_ip_gateway_interface) && (ip_ptr -> nx_ip_gateway_interface -> nx_interface_link_up)) { /* Get the interface. */ interface_ptr = ip_ptr -> nx_ip_gateway_interface; /* Yes, is gateway address still reachable? */ if (interface_ptr -> nx_interface_ip_network != (ip_ptr -> nx_ip_gateway_address & interface_ptr -> nx_interface_ip_network_mask)) { return(NX_IP_ADDRESS_ERROR); }if (interface_ptr -> nx_interface_ip_network != (ip_ptr -> nx_ip_gateway_address & interface_ptr -> nx_interface_ip_network_mask)) { ... } /* Use the gateway as default. */ if (*ip_interface_ptr == NX_NULL) { *ip_interface_ptr = interface_ptr; }if (*ip_interface_ptr == NX_NULL) { ... } else if (*ip_interface_ptr != interface_ptr) { return(NX_IP_ADDRESS_ERROR); }else if (*ip_interface_ptr != interface_ptr) { ... } *next_hop_address = ip_ptr -> nx_ip_gateway_address; return(NX_SUCCESS); }if ((ip_ptr -> nx_ip_gateway_address) && (ip_ptr -> nx_ip_gateway_interface) && (ip_ptr -> nx_ip_gateway_interface -> nx_interface_link_up)) { ... } /* Determine if source addrss is link-local address(169.254/16 Hexadecimal:0xA9FE0000). */ if (*ip_interface_ptr) { /* Determine if the interface is valid and the address of interface is link-local address. */ if (((*ip_interface_ptr) -> nx_interface_valid) && ((*ip_interface_ptr) -> nx_interface_link_up) && (((*ip_interface_ptr) -> nx_interface_ip_address & 0xFFFF0000) == 0xA9FE0000)) { /* Set the next hop address. */ *next_hop_address = destination_address; return(NX_SUCCESS); }if (((*ip_interface_ptr) -> nx_interface_valid) && ((*ip_interface_ptr) -> nx_interface_link_up) && (((*ip_interface_ptr) -> nx_interface_ip_address & 0xFFFF0000) == 0xA9FE0000)) { ... } }if (*ip_interface_ptr) { ... } else { /* Search through the interfaces associated with the IP instance, check if interface is valid and the address of interface is link-local address. *//* ... */ for (i = 0; i < NX_MAX_IP_INTERFACES; i++) { /* Use a local variable for convenience. */ interface_ptr = &(ip_ptr -> nx_ip_interface[i]); /* Check for a valid interface that the address is link-local address. */ if ((interface_ptr -> nx_interface_valid) && (interface_ptr -> nx_interface_link_up) && ((interface_ptr -> nx_interface_ip_address & 0xFFFF0000) == 0xA9FE0000)) { /* Yes, use the entry information for interface and next hop. */ *ip_interface_ptr = interface_ptr; *next_hop_address = destination_address; return(NX_SUCCESS); }if ((interface_ptr -> nx_interface_valid) && (interface_ptr -> nx_interface_link_up) && ((interface_ptr -> nx_interface_ip_address & 0xFFFF0000) == 0xA9FE0000)) { ... } }for (i = 0; i < NX_MAX_IP_INTERFACES; i++) { ... } }else { ... } /* Cannot find a proper way to transmit this packet. Return the error status. *//* ... */ return(NX_IP_ADDRESS_ERROR); }{ ... } #endif/* ... */ /* NX_DISABLE_IPV4 */