forked from eclipse-threadx/netxduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_netxduo_dhcpv6.c
454 lines (337 loc) · 14.9 KB
/
demo_netxduo_dhcpv6.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
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
/* 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"
#include "nxd_dhcpv6_client.h"
#include "nxd_dhcpv6_server.h"
#define DEMO_DATA "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
#ifdef FEATURE_NX_IPV6
#define DEMO_STACK_SIZE 2048
#define NX_DHCPV6_THREAD_STACK_SIZE 2048
/* Define the ThreadX and NetX object control blocks... */
NX_PACKET_POOL pool_0;
TX_THREAD thread_client;
NX_IP client_ip;
TX_THREAD thread_server;
NX_IP server_ip;
/* Define the Client and Server instances. */
NX_DHCPV6 dhcp_client;
NX_DHCPV6_SERVER dhcp_server;
/* Define the variables. */
CHAR *pointer;
NXD_ADDRESS server_address;
NXD_ADDRESS dns_ipv6_address;
NXD_ADDRESS start_ipv6_address;
NXD_ADDRESS end_ipv6_address;
/* Define thread prototypes. */
void thread_client_entry(ULONG thread_input);
void thread_server_entry(ULONG thread_input);
/***** Substitute your ethernet driver entry function here *********/
VOID _nx_ram_network_driver(NX_IP_DRIVER *driver_req_ptr);
/* Define some DHCPv6 parameters. */
#define DHCPV6_IANA_ID 0xABCDEFAB
#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 main entry point. */
int main()
{
/* Enter the ThreadX kernel. */
tx_kernel_enter();
}
/* Define what the initial system looks like. */
void tx_application_define(void *first_unused_memory)
{
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 thread create errors. */
if (status)
return;
/* 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 thread create errors. */
if (status)
return;
/* Create a packet pool. */
status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 1024, pointer, NX_DHCPV6_PACKET_POOL_SIZE);
pointer = pointer + NX_DHCPV6_PACKET_POOL_SIZE;
/* Check for pool creation error. */
if (status)
return;
/* 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)
return;
/* 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)
return;
/* 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)
return;
/* Enable the IPv6 services. */
status = nxd_ipv6_enable(&client_ip);
status += nxd_ipv6_enable(&server_ip);
/* Check for IPv6 enable errors. */
if (status)
return;
/* Enable the ICMPv6 services. */
status = nxd_icmp_enable(&client_ip);
status += nxd_icmp_enable(&server_ip);
/* Check for ICMPv6 enable errors. */
if (status)
return;
}
/* Define the Client host application thread. */
void thread_client_entry(ULONG thread_input)
{
UINT status;
#ifdef GET_ONE_SPECIFIC_ADDRESS
NXD_ADDRESS ia_ipv6_address;
#endif
NXD_ADDRESS ipv6_address;
NXD_ADDRESS dns_address;
ULONG T1, T2, preferred_lifetime, valid_lifetime;
UINT address_count;
UINT address_index;
UINT dns_index;
NX_PACKET *my_packet;
NX_PARAMETER_NOT_USED(thread_input);
/* Modified the mtu size to avoid fragmenting the DHCP packet since the default mtu size is 128 in _nx_ram_network_driver. */
status = nx_ip_interface_mtu_set(&server_ip, 0, 1500);
/* Check for MTU set errors. */
if (status)
return;
/* 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)
return;
/* 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_CLIENT_HARDWARE_TYPE_ETHERNET, 0);
/* Check for errors. */
if (status)
return;
/* 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)
return;
/* Starting up the NetX DHCPv6 Client. */
status = nx_dhcpv6_start(&dhcp_client);
/* Check for errors. */
if (status)
return;
/* Let DHCPv6 Server start. */
tx_thread_sleep(5 * NX_IP_PERIODIC_RATE);
#ifdef GET_ONE_SPECIFIC_ADDRESS
/* Create an IA address option.
The client includs IA options for any IAs to which it wants the server to assign addresses.
*/
ia_ipv6_address.nxd_ip_version = NX_IP_VERSION_V6 ;
ia_ipv6_address.nxd_ip_address.v6[0] = 0x20010db8;
ia_ipv6_address.nxd_ip_address.v6[1] = 0x00000f101;
ia_ipv6_address.nxd_ip_address.v6[2] = 0x0;
ia_ipv6_address.nxd_ip_address.v6[3] = 0x00000115;
status = nx_dhcpv6_create_client_ia(&dhcp_client, &ia_ipv6_address, NX_DHCPV6_REFERRED_LIFETIME,
NX_DHCPV6_VALID_LIFETIME);
/* Check for errors. */
if (status != NX_SUCCESS)
return;
#endif
/* 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 status. */
if (status)
return;
/* Waiting for get the IPv6 address and do the duplicate address detection. */
/*
Note, if the host detect another host withe the same address, the DHCPv6 Client can automatically
declient the address. At time T1 for an IPv6 address, the DHCPv6 Client can automatically renew the address.
At time T2 for an IPv6 address, the DHCPv6 Client can automatically rebind the address.
At time valid lifetime for an IPv6 address, the DHCPv6 Client can automatically delete the IPv6 address.
*/
tx_thread_sleep(6 * NX_IP_PERIODIC_RATE);
/* Get the T1 and T2 value of IANA option. */
status = nx_dhcpv6_get_iana_lease_time(&dhcp_client, &T1, &T2);
/* Check status. */
if (status)
return;
/* Get the valid IPv6 address count which the DHCPv6 server assigned . */
status = nx_dhcpv6_get_valid_ip_address_count(&dhcp_client, &address_count);
/* Check status. */
if (status)
return;
/* 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 status. */
if (status)
return;
/* Get the IPv6 address.
Note, This API only applies to one IA. */
status = nx_dhcpv6_get_IP_address(&dhcp_client, &ipv6_address);
/* Check status. */
if (status)
return;
/* 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)
return;
/* Get the DNS Server address lease time. */
dns_index = 0;
status = nx_dhcpv6_get_DNS_server_address(&dhcp_client, dns_index, &dns_address);
/* Check status. */
if (status)
return;
/**************************************************/
/* Ping the DHCPv6 Server, Test the IPv6 address. */
/**************************************************/
/* Ping the IPv6 address of DHCPv6 Server. */
status = nxd_icmp_ping(&client_ip, &server_address, DEMO_DATA, sizeof(DEMO_DATA), &my_packet, NX_IP_PERIODIC_RATE);
/* Check status. */
if (status)
return;
/* If we want to release the address, we can send release message to
the server we are releasing the assigned address. */
status = nx_dhcpv6_request_release(&dhcp_client);
/* Check status. */
if (status)
return;
/* Stop the Client task. */
status = nx_dhcpv6_stop(&dhcp_client);
/* Check status. */
if (status)
return;
/* Now delete the DHCPv6 client. */
status = nx_dhcpv6_client_delete(&dhcp_client);
/* Check status. */
if (status)
return;
}
/* Define the test server thread. */
void thread_server_entry(ULONG thread_input)
{
UINT status;
ULONG duid_time;
UINT addresses_added;
NX_PARAMETER_NOT_USED(thread_input);
/* Modified the mtu size to avoid fragmenting the DHCP packet since the default mtu size is 128 in _nx_ram_network_driver. */
status = nx_ip_interface_mtu_set(&client_ip, 0, 1500);
/* Check for MTU set errors. */
if (status)
return;
/* 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)
return;
/* 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 = pointer + NX_DHCPV6_SERVER_THREAD_STACK_SIZE;
/* Check for errors. */
if (status)
return;
/* Note this example assumes a single global IP address on the primary interface. If otherwise
the host should call the service to set the network interface and global IP address index.
UINT _nx_dhcpv6_server_interface_set(NX_DHCPV6_SERVER *dhcpv6_server_ptr, UINT interface_index, UINT address_index)
*/
/* Validate the link local and global addresses. */
tx_thread_sleep(5 * NX_IP_PERIODIC_RATE);
/* 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)
return;
/* 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)
return;
/* 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)
return;
/* Start the NetX DHCPv6 server! */
status = nx_dhcpv6_server_start(&dhcp_server);
/* Check for errors. */
if (status)
return;
}
#endif /* FEATURE_NX_IPV6 */