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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation */
/* Copyright (c) 2026 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 */
/***************************************************************************/
/* This is a small demo of the NetX Duo DHCPv6 Client and Server for the high-performance
NetX Duo stack. */
#include <stdio.h>
#include "tx_api.h"
#include "nx_api.h"
extern void test_control_return(UINT status);
#if defined(FEATURE_NX_IPV6) && !defined(NX_DISABLE_FRAGMENTATION)
#include "nxd_dhcpv6_client.h"
#include "nxd_dhcpv6_server.h"
#define DEMO_STACK_SIZE 2048
#define NX_DHCPV6_THREAD_STACK_SIZE 2048
#define NX_PACKET_SIZE 1536
#define NX_PACKET_POOL_SIZE (NX_PACKET_SIZE * 8)
/* Define the ThreadX and NetX object control blocks... */
static NX_PACKET_POOL pool_0;
static TX_THREAD thread_client;
static TX_THREAD thread_server;
static NX_IP client_ip;
static NX_IP server_ip;
/* Define the Client and Server instances. */
static NX_DHCPV6 dhcp_client;
static NX_DHCPV6_SERVER dhcp_server;
/* Define the error counter used in the demo application... */
static ULONG error_counter;
static CHAR *pointer;
static NXD_ADDRESS server_address;
static NXD_ADDRESS dns_ipv6_address;
static NXD_ADDRESS start_ipv6_address;
static NXD_ADDRESS end_ipv6_address;
/* Define thread prototypes. */
static void thread_client_entry(ULONG thread_input);
static void thread_server_entry(ULONG thread_input);
extern void _nx_ram_network_driver(NX_IP_DRIVER *driver_req_ptr);
/* Define some DHCPv6 parameters. */
#define DHCPV6_IANA_ID 0xC0DEDBAD
#define DHCPV6_T1 NX_DHCPV6_INFINITE_LEASE
#define DHCPV6_T2 NX_DHCPV6_INFINITE_LEASE
#define NX_DHCPV6_REFERRED_LIFETIME NX_DHCPV6_INFINITE_LEASE
#define NX_DHCPV6_VALID_LIFETIME NX_DHCPV6_INFINITE_LEASE
/* Define what the initial system looks like. */
#ifdef CTEST
VOID test_application_define(void *first_unused_memory)
#else
void netx_dhcpv6_basic_test_application_define(void *first_unused_memory)
#endif
{
UINT status;
/* Setup the working pointer. */
pointer = (CHAR *) first_unused_memory;
/* Initialize the NetX system. */
nx_system_initialize();
/* Create the Client thread. */
status = tx_thread_create(&thread_client, "Client thread", thread_client_entry, 0,
pointer, DEMO_STACK_SIZE,
4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Check for IP create errors. */
if (status)
{
error_counter++;
}
/* Create the Server thread. */
status = tx_thread_create(&thread_server, "Server thread", thread_server_entry, 0,
pointer, DEMO_STACK_SIZE,
3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
pointer = pointer + DEMO_STACK_SIZE;
/* Check for IP create errors. */
if (status)
{
error_counter++;
}
/* Create a packet pool. */
status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 1024, pointer, NX_PACKET_POOL_SIZE);
pointer = pointer + NX_PACKET_POOL_SIZE;
/* Check for pool creation error. */
if (status)
error_counter++;
/* Create a Client IP instance. */
status = nx_ip_create(&client_ip, "Client IP", IP_ADDRESS(0, 0, 0, 0),
0xFFFFFF00UL, &pool_0, _nx_ram_network_driver,
pointer, 2048, 1);
pointer = pointer + 2048;
/* Check for IP create errors. */
if (status)
{
error_counter++;
}
/* Create a Server IP instance. */
status = nx_ip_create(&server_ip, "Server IP", IP_ADDRESS(1, 2, 3, 4),
0xFFFFFF00UL, &pool_0, _nx_ram_network_driver,
pointer, 2048, 1);
pointer = pointer + 2048;
/* Check for IP create errors. */
if (status)
{
error_counter++;
}
/* Enable UDP traffic for sending DHCPv6 messages. */
status = nx_udp_enable(&client_ip);
status += nx_udp_enable(&server_ip);
/* Check for UDP enable errors. */
if (status)
{
error_counter++;
}
/* Enable the IPv6 services. */
status = nxd_ipv6_enable(&client_ip);
status += nxd_ipv6_enable(&server_ip);
/* Check for IPv6 enable errors. */
if (status)
{
error_counter++;
}
/* Enable the ICMPv6 services. */
status = nxd_icmp_enable(&client_ip);
status += nxd_icmp_enable(&server_ip);
/* Check for ICMP enable errors. */
if (status)
{
error_counter++;
}
/* Enable the fragment feature. */
status = nx_ip_fragment_enable(&client_ip);
status += nx_ip_fragment_enable(&server_ip);
/* Check for ICMP enable errors. */
if (status)
{
error_counter++;
}
}
/* Define the Client host application thread. */
void thread_client_entry(ULONG thread_input)
{
UINT status;
NXD_ADDRESS ipv6_address;
NXD_ADDRESS dns_address;
ULONG prefix_length;
UINT interface_index;
ULONG T1, T2, preferred_lifetime, valid_lifetime;
UINT address_count;
UINT address_index;
NX_PACKET *my_packet;
/* Print out test information banner. */
printf("NetX Test: DHCPv6 Basic Test.........................................");
/* Check for earlier error. */
if(error_counter)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Establish the link local address for the host. The RAM driver creates
a virtual MAC address of 0x1122334456. */
status = nxd_ipv6_address_set(&client_ip, 0, NX_NULL, 10, NULL);
/* Let NetX Duo and the network driver get initialized. Also give the server time to get set up. */
tx_thread_sleep(5 * NX_IP_PERIODIC_RATE);
/* Create the DHCPv6 Client. */
status = nx_dhcpv6_client_create(&dhcp_client, &client_ip, "DHCPv6 Client", &pool_0, pointer, NX_DHCPV6_THREAD_STACK_SIZE,
NX_NULL, NX_NULL);
pointer = pointer + NX_DHCPV6_THREAD_STACK_SIZE;
/* Check for errors. */
if(status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Create a Link Layer Plus Time DUID for the DHCPv6 Client. Set time ID field
to NULL; the DHCPv6 Client API will supply one. */
status = nx_dhcpv6_create_client_duid(&dhcp_client, NX_DHCPV6_DUID_TYPE_LINK_TIME,
NX_DHCPV6_HW_TYPE_IEEE_802, 0);
/* Check for errors. */
if(status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Create the DHCPv6 client's Identity Association (IA-NA) now.
Note that if this host had already been assigned in IPv6 lease, it
would have to use the assigned T1 and T2 values in loading the DHCPv6
client with an IANA block.
*/
status = nx_dhcpv6_create_client_iana(&dhcp_client, DHCPV6_IANA_ID, DHCPV6_T1, DHCPV6_T2);
/* Check for errors. */
if(status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Starting up the NetX DHCPv6 Client. */
status = nx_dhcpv6_start(&dhcp_client);
/* Check for errors. */
if(status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* If the host also want to get the option message, set the list of desired options to enabled. */
nx_dhcpv6_request_option_timezone(&dhcp_client, NX_TRUE);
nx_dhcpv6_request_option_DNS_server(&dhcp_client, NX_TRUE);
nx_dhcpv6_request_option_time_server(&dhcp_client, NX_TRUE);
nx_dhcpv6_request_option_domain_name(&dhcp_client, NX_TRUE);
/* Now, the host send the solicit message to get the IPv6 address and other options from the DHCPv6 server. */
status = nx_dhcpv6_request_solicit(&dhcp_client);
/* Check for errors. */
if(status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Waiting for get the IPv6 address and do the duplicate address detection. */
tx_thread_sleep(6 * NX_IP_PERIODIC_RATE);
/* Get the valid IPv6 address count which the DHCPv6 server assigned . */
status = nx_dhcpv6_get_valid_ip_address_count(&dhcp_client, &address_count);
/* Check for errors. */
if ((status) || (address_count != 1))
{
printf("ERROR!\n");
test_control_return(1);
}
/* Get the IPv6 address, preferred lifetime and valid lifetime according to the address index. */
address_index = 0;
status = nx_dhcpv6_get_valid_ip_address_lease_time(&dhcp_client, address_index, &ipv6_address, &preferred_lifetime, &valid_lifetime);
/* Check for errors. */
if ((status) || (address_count != 1))
{
printf("ERROR!\n");
test_control_return(1);
}
/* Get the IPv6 address by DHCPv6 API. */
status = nx_dhcpv6_get_IP_address(&dhcp_client, &ipv6_address);
/* Check status. */
if (status != NX_SUCCESS)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Check the address. */
if ((ipv6_address.nxd_ip_version != start_ipv6_address.nxd_ip_version) ||
(ipv6_address.nxd_ip_address.v6[0] != start_ipv6_address.nxd_ip_address.v6[0]) ||
(ipv6_address.nxd_ip_address.v6[1] != start_ipv6_address.nxd_ip_address.v6[1]) ||
(ipv6_address.nxd_ip_address.v6[2] != start_ipv6_address.nxd_ip_address.v6[2]) ||
(ipv6_address.nxd_ip_address.v6[3] != start_ipv6_address.nxd_ip_address.v6[3]))
{
printf("ERROR!\n");
test_control_return(1);
}
/* Get the IPv6 address by NetX Duo API. */
status = nxd_ipv6_address_get(&client_ip, 1, &ipv6_address, &prefix_length, &interface_index);
/* Check status. */
if (status != NX_SUCCESS)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Check the IPv6 address. */
if ((prefix_length != 64) || (interface_index != 0) ||
(ipv6_address.nxd_ip_version != start_ipv6_address.nxd_ip_version) ||
(ipv6_address.nxd_ip_address.v6[0] != start_ipv6_address.nxd_ip_address.v6[0]) ||
(ipv6_address.nxd_ip_address.v6[1] != start_ipv6_address.nxd_ip_address.v6[1]) ||
(ipv6_address.nxd_ip_address.v6[2] != start_ipv6_address.nxd_ip_address.v6[2]) ||
(ipv6_address.nxd_ip_address.v6[3] != start_ipv6_address.nxd_ip_address.v6[3]))
{
printf("ERROR!\n");
test_control_return(1);
}
/* Get IP address lease time.
Note, This API only applies to one IA. */
status = nx_dhcpv6_get_lease_time_data(&dhcp_client, &T1, &T2, &preferred_lifetime, &valid_lifetime);
/* Check status. */
if (status != NX_SUCCESS)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Check the value. */
if ((T1 != NX_DHCPV6_DEFAULT_T1_TIME) ||
(T2 != NX_DHCPV6_DEFAULT_T2_TIME) ||
(preferred_lifetime != NX_DHCPV6_DEFAULT_PREFERRED_TIME) ||
(valid_lifetime != NX_DHCPV6_DEFAULT_VALID_TIME))
{
printf("ERROR!\n");
test_control_return(1);
}
/* Get the DNS Server address. */
status = nx_dhcpv6_get_DNS_server_address(&dhcp_client, 0, &dns_address);
/* Check status. */
if (status != NX_SUCCESS)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Check the IPv6 DNS address. */
if ((dns_address.nxd_ip_version != dns_ipv6_address.nxd_ip_version) ||
(dns_address.nxd_ip_address.v6[0] != dns_ipv6_address.nxd_ip_address.v6[0]) ||
(dns_address.nxd_ip_address.v6[1] != dns_ipv6_address.nxd_ip_address.v6[1]) ||
(dns_address.nxd_ip_address.v6[2] != dns_ipv6_address.nxd_ip_address.v6[2]) ||
(dns_address.nxd_ip_address.v6[3] != dns_ipv6_address.nxd_ip_address.v6[3]))
{
printf("ERROR!\n");
test_control_return(1);
}
/* Ping the DHCPv6 Server. */
status = nxd_icmp_ping(&client_ip, &server_address, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 28, &my_packet, NX_IP_PERIODIC_RATE);
/* Check for errors. */
if (status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Release the address. */
status = nx_dhcpv6_request_release(&dhcp_client);
/* Check status. */
if (status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Waiting for release the IPv6 address. */
tx_thread_sleep(NX_IP_PERIODIC_RATE);
/* Get the IPv6 address by DHCPv6 API. */
status = nx_dhcpv6_get_IP_address(&dhcp_client, &ipv6_address);
/* Check status. */
if (status == NX_SUCCESS)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Get the IPv6 address by NetX Duo API. */
status = nxd_ipv6_address_get(&client_ip, 1, &ipv6_address, &prefix_length, &interface_index);
/* Check status. */
if (status == NX_SUCCESS)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Stop the Client task. */
status = nx_dhcpv6_stop(&dhcp_client);
/* Check status. */
if (status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Delete the DHCPv6 client. */
status = nx_dhcpv6_client_delete(&dhcp_client);
/* Check status. */
if (status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Output successfully. */
printf("SUCCESS!\n");
test_control_return(0);
}
/* Define the test server thread. */
void thread_server_entry(ULONG thread_input)
{
UINT status;
ULONG duid_time;
UINT addresses_added;
/* Set the IPv6 address of DHCPv6 Server. */
server_address.nxd_ip_version = NX_IP_VERSION_V6 ;
server_address.nxd_ip_address.v6[0] = 0x20010db8;
server_address.nxd_ip_address.v6[1] = 0xf101;
server_address.nxd_ip_address.v6[2] = 0x00000000;
server_address.nxd_ip_address.v6[3] = 0x00000101;
/* Set the link local and global addresses. */
status = nxd_ipv6_address_set(&server_ip, 0, NX_NULL, 10, NULL);
status += nxd_ipv6_address_set(&server_ip, 0, &server_address, 64, NULL);
/* Check for errors. */
if(status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Validate the link local and global addresses. */
tx_thread_sleep(5 * NX_IP_PERIODIC_RATE);
/* Create the DHCPv6 Server. */
status = nx_dhcpv6_server_create(&dhcp_server, &server_ip, "DHCPv6 Server", &pool_0, pointer, NX_DHCPV6_SERVER_THREAD_STACK_SIZE, NX_NULL, NX_NULL);
pointer += NX_DHCPV6_SERVER_THREAD_STACK_SIZE;
/* Check for errors. */
if(status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Set up the DNS IPv6 server address. */
dns_ipv6_address.nxd_ip_version = NX_IP_VERSION_V6 ;
dns_ipv6_address.nxd_ip_address.v6[0] = 0x20010db8;
dns_ipv6_address.nxd_ip_address.v6[1] = 0x0000f101;
dns_ipv6_address.nxd_ip_address.v6[2] = 0x00000000;
dns_ipv6_address.nxd_ip_address.v6[3] = 0x00000107;
status = nx_dhcpv6_create_dns_address(&dhcp_server, &dns_ipv6_address);
/* Check for errors. */
if(status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Note: For DUID types that do not require time, the 'duid_time' input can be left at zero.
The DUID_TYPE and HW_TYPE are configurable options that are user defined in nx_dhcpv6_server.h. */
/* Set the DUID time as the start of the millenium. */
duid_time = SECONDS_SINCE_JAN_1_2000_MOD_32;
status = nx_dhcpv6_set_server_duid(&dhcp_server,
NX_DHCPV6_SERVER_DUID_TYPE, NX_DHCPV6_SERVER_HW_TYPE,
dhcp_server.nx_dhcpv6_ip_ptr -> nx_ip_arp_physical_address_msw,
dhcp_server.nx_dhcpv6_ip_ptr -> nx_ip_arp_physical_address_lsw,
duid_time);
/* Check for errors. */
if(status)
{
printf("ERROR!\n");
test_control_return(1);
}
/* Set the IPv6 start address. */
start_ipv6_address.nxd_ip_version = NX_IP_VERSION_V6 ;
start_ipv6_address.nxd_ip_address.v6[0] = 0x20010db8;
start_ipv6_address.nxd_ip_address.v6[1] = 0x00000f101;
start_ipv6_address.nxd_ip_address.v6[2] = 0x0;
start_ipv6_address.nxd_ip_address.v6[3] = 0x00000110;
/* Set the IPv6 end address. */
end_ipv6_address.nxd_ip_version = NX_IP_VERSION_V6 ;
end_ipv6_address.nxd_ip_address.v6[0] = 0x20010db8;
end_ipv6_address.nxd_ip_address.v6[1] = 0x0000f101;
end_ipv6_address.nxd_ip_address.v6[2] = 0x00000000;
end_ipv6_address.nxd_ip_address.v6[3] = 0x00000120;
/* Set the IPv6 address range. */
status = nx_dhcpv6_create_ip_address_range(&dhcp_server, &start_ipv6_address, &end_ipv6_address, &addresses_added);
/* Check for errors. */
if ((status) || (addresses_added != 16))
{
printf("ERROR!\n");
test_control_return(1);
}
/* Start the NetX DHCPv6 server! */
status = nx_dhcpv6_server_start(&dhcp_server);
/* Check for errors. */
if(status)
{
printf("ERROR!\n");
test_control_return(1);
}
}
#else
#ifdef CTEST
VOID test_application_define(void *first_unused_memory)
#else
void netx_dhcpv6_basic_test_application_define(void * first_unused_memory)
#endif
{
printf("NetX Test: DHCPv6 Basic Test.........................................N/A\n");
test_control_return(3);
}
#endif /* FEATURE_NX_IPV6 */
|