1 
2 #include   "tx_api.h"
3 #include   "nx_api.h"
4 extern void    test_control_return(UINT status);
5 
6 #if !defined(NX_DISABLE_UDP_TX_CHECKSUM) && !defined(NX_DISABLE_IPV4)
7 #include   "nxd_dhcp_client.h"
8 #include   "nxd_dhcp_server.h"
9 
10 #define     DEMO_STACK_SIZE             4096
11 #define     NX_PACKET_SIZE              1536
12 #define     NX_PACKET_POOL_SIZE         NX_PACKET_SIZE * 8
13 
14 #define     NX_DHCP_SERVER_IP_ADDRESS_0 IP_ADDRESS(192, 168, 3, 8)
15 #define     START_IP_ADDRESS_LIST_0     IP_ADDRESS(192, 168, 3, 11)
16 #define     END_IP_ADDRESS_LIST_0       IP_ADDRESS(192, 168, 3,20)
17 
18 #define     NX_DHCP_SUBNET_MASK_0       IP_ADDRESS(255,255,255,0)
19 #define     NX_DHCP_DEFAULT_GATEWAY_0   IP_ADDRESS(192, 168, 3, 8)
20 #define     NX_DHCP_DNS_SERVER_0        IP_ADDRESS(192, 168, 3, 8)
21 
22 /* Define the ThreadX and NetX object control blocks...  */
23 static TX_THREAD               client_thread;
24 static NX_PACKET_POOL          client_pool;
25 static NX_IP                   client_ip;
26 
27 static TX_THREAD               server_thread;
28 static NX_PACKET_POOL          server_pool;
29 static NX_IP                   server_ip;
30 static NX_DHCP_SERVER          dhcp_server;
31 
32 /* Define the counters used in the demo application...  */
33 
34 static ULONG                   error_counter;
35 static ULONG                   resposner_counter;
36 static CHAR                    *pointer;
37 
38 /* Define thread prototypes.  */
39 
40 static void    server_thread_entry(ULONG thread_input);
41 static void    client_thread_entry(ULONG thread_input);
42 
43 /******** Optionally substitute your Ethernet driver here. ***********/
44 extern void    _nx_ram_network_driver_1024(struct NX_IP_DRIVER_STRUCT *driver_req);
45 extern UINT    (*advanced_packet_process_callback)(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT *operation_ptr, UINT *delay_ptr);
46 static UINT    my_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT *operation_ptr, UINT *delay_ptr);
47 
48 
49 /* Frame (342 bytes)  DHCP DISCOVER  */
50 static const unsigned char pkt1[342] = {
51 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x50, /* .......P */
52 0xb6, 0x07, 0xa1, 0x69, 0x08, 0x00, 0x45, 0x00, /* ...i..E. */
53 0x01, 0x48, 0x00, 0x01, 0x00, 0x00, 0x80, 0x11, /* .H...... */
54 0x39, 0xa5, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, /* 9....... */
55 0xff, 0xff, 0x00, 0x44, 0x00, 0x43, 0x01, 0x34, /* ...D.C.4 */
56 0x83, 0x9e, 0x01, 0x01, 0x06, 0x00, 0x00, 0xf6, /* ........ */
57 0x16, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, /* .......P */
60 0xb6, 0x07, 0xa1, 0x69, 0x00, 0x00, 0x00, 0x00, /* ...i.... */
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
85 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, /* ......c. */
86 0x53, 0x63, 0x35, 0x01, 0x01, 0x3d, 0x07, 0x01, /* Sc5..=.. */
87 0x00, 0x50, 0xb6, 0x07, 0xa1, 0x69, 0x0c, 0x0e, /* .P...i.. */
88 0x43, 0x57, 0x2d, 0x39, 0x57, 0x44, 0x50, 0x4a, /* CW-9WDPJ */
89 0x56, 0x31, 0x2d, 0x47, 0x41, 0x31, 0x3c, 0x08, /* V1-GA1<. */
90 0x4d, 0x53, 0x46, 0x54, 0x20, 0x35, 0x2e, 0x30, /* MSFT 5.0 */
91 0x37, 0x0c, 0x01, 0x0f, 0x03, 0x06, 0x2c, 0x2e, /* 7.....,. */
92 0x2f, 0x1f, 0x21, 0x79, 0xf9, 0x2b, 0xff, 0x00, /* /.!y.+.. */
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00              /* ...... */
94 };
95 
96 /* Frame (342 bytes)  DHCP OFFER  */
97 static const unsigned char pkt2[342] = {
98 0x00, 0x50, 0xb6, 0x07, 0xa1, 0x69, 0x00, 0x08, /* .P...i.. */
99 0xee, 0x03, 0x6a, 0xc6, 0x08, 0x00, 0x45, 0x00, /* ..j...E. */
100 0x01, 0x48, 0x00, 0x01, 0x40, 0x00, 0x80, 0x11, /* .H..@... */
101 0x72, 0x40, 0xc0, 0xa8, 0x03, 0x08, 0xc0, 0xa8, /* r@...... */
102 0x03, 0x0b, 0x00, 0x43, 0x00, 0x44, 0x01, 0x34, /* ...C.D.4 */
103 0x54, 0xf8, 0x02, 0x01, 0x06, 0x00, 0x00, 0xf6, /* T....... */
104 0x16, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
105 0x00, 0x00, 0xc0, 0xa8, 0x03, 0x0b, 0x00, 0x00, /* ........ */
106 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, /* .......P */
107 0xb6, 0x07, 0xa1, 0x69, 0x00, 0x00, 0x00, 0x00, /* ...i.... */
108 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
109 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
111 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
112 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
116 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
117 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
118 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
119 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
120 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
121 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
122 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
123 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
124 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
126 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
127 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
129 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
131 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
132 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, /* ......c. */
133 0x53, 0x63, 0x35, 0x01, 0x02, 0x36, 0x04, 0xc0, /* Sc5..6.. */
134 0xa8, 0x03, 0x08, 0x01, 0x04, 0xff, 0xff, 0xff, /* ........ */
135 0x00, 0x03, 0x04, 0xc0, 0xa8, 0x03, 0x08, 0x06, /* ........ */
136 0x04, 0xc0, 0xa8, 0x03, 0x08, 0x33, 0x04, 0x00, /* .....3.. */
137 0x00, 0x27, 0x10, 0x3a, 0x04, 0x00, 0x00, 0x13, /* .'.:.... */
138 0x88, 0x3b, 0x04, 0x00, 0x00, 0x22, 0x2e, 0xff, /* .;...".. */
139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
140 0x00, 0x00, 0x00, 0x00, 0x00, 0x00              /* ...... */
141 };
142 
143 /* Frame (379 bytes) DHCP REQUEST  */
144 static const unsigned char pkt3[379] = {
145 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x50, /* .......P */
146 0xb6, 0x07, 0xa1, 0x69, 0x08, 0x00, 0x45, 0x00, /* ...i..E. */
147 0x01, 0x6d, 0x00, 0x02, 0x00, 0x00, 0x80, 0x11, /* .m...... */
148 0x39, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, /* 9....... */
149 0xff, 0xff, 0x00, 0x44, 0x00, 0x43, 0x01, 0x59, /* ...D.C.Y */
150 0x4d, 0x72, 0x01, 0x01, 0x06, 0x00, 0x00, 0xf6, /* Mr...... */
151 0x16, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, /* .......P */
154 0xb6, 0x07, 0xa1, 0x69, 0x00, 0x00, 0x00, 0x00, /* ...i.... */
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
158 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
159 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
160 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
162 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
163 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
164 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
165 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
166 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
167 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
168 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
169 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
170 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
171 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
172 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
173 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
174 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
175 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
176 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
177 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
178 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
179 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, /* ......c. */
180 0x53, 0x63, 0x35, 0x01, 0x03, 0x3d, 0x07, 0x01, /* Sc5..=.. */
181 0x00, 0x50, 0xb6, 0x07, 0xa1, 0x69, 0x32, 0x04, /* .P...i2. */
182 0xc0, 0xa8, 0x03, 0x0b, 0x36, 0x04, 0xc0, 0xa8, /* ....6... */
183 0x03, 0x08, 0x0c, 0x0e, 0x43, 0x57, 0x2d, 0x39, /* ....CW-9 */
184 0x57, 0x44, 0x50, 0x4a, 0x56, 0x31, 0x2d, 0x47, /* WDPJV1-G */
185 0x41, 0x31, 0x51, 0x1e, 0x00, 0x00, 0x00, 0x43, /* A1Q....C */
186 0x57, 0x2d, 0x39, 0x57, 0x44, 0x50, 0x4a, 0x56, /* W-9WDPJV */
187 0x31, 0x2d, 0x47, 0x41, 0x31, 0x2e, 0x54, 0x54, /* 1-GA1.TT */
188 0x50, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x2e, 0x63, /* PGroup.c */
189 0x6f, 0x6d, 0x3c, 0x08, 0x4d, 0x53, 0x46, 0x54, /* om<.MSFT */
190 0x20, 0x35, 0x2e, 0x30, 0x37, 0x0c, 0x01, 0x0f, /*  5.07... */
191 0x03, 0x06, 0x2c, 0x2e, 0x2f, 0x1f, 0x21, 0x79, /* ..,./.!y */
192 0xf9, 0x2b, 0xff                                /* .+. */
193 };
194 
195 /* Frame (342 bytes)  DHCP ACK  */
196 static const unsigned char pkt4[342] = {
197 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x08, /* ........ */
198 0xee, 0x03, 0x6a, 0xc6, 0x08, 0x00, 0x45, 0x00, /* ..j...E. */
199 0x01, 0x48, 0x00, 0x02, 0x40, 0x00, 0x80, 0x11, /* .H..@... */
200 0x72, 0x3f, 0xc0, 0xa8, 0x03, 0x08, 0xc0, 0xa8, /* r?...... */
201 0x03, 0x0b, 0x00, 0x43, 0x00, 0x44, 0x01, 0x34, /* ...C.D.4 */
202 0x51, 0xf8, 0x02, 0x01, 0x06, 0x00, 0x00, 0xf6, /* Q....... */
203 0x16, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
204 0x00, 0x00, 0xc0, 0xa8, 0x03, 0x0b, 0x00, 0x00, /* ........ */
205 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, /* .......P */
206 0xb6, 0x07, 0xa1, 0x69, 0x00, 0x00, 0x00, 0x00, /* ...i.... */
207 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
208 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
209 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
210 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
211 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
212 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
213 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
214 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
215 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
216 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
217 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
218 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
219 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
220 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
221 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
222 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
223 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
224 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
225 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
226 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
227 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
228 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
229 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
230 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
231 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, /* ......c. */
232 0x53, 0x63, 0x35, 0x01, 0x05, 0x36, 0x04, 0xc0, /* Sc5..6.. */
233 0xa8, 0x03, 0x08, 0x01, 0x04, 0xff, 0xff, 0xff, /* ........ */
234 0x00, 0x03, 0x04, 0xc0, 0xa8, 0x03, 0x08, 0x06, /* ........ */
235 0x04, 0xc0, 0xa8, 0x03, 0x08, 0x33, 0x04, 0x00, /* .....3.. */
236 0x00, 0x27, 0x10, 0x3a, 0x04, 0x00, 0x00, 0x13, /* .'.:.... */
237 0x88, 0x3b, 0x04, 0x00, 0x00, 0x22, 0x2e, 0xff, /* .;...".. */
238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
239 0x00, 0x00, 0x00, 0x00, 0x00, 0x00              /* ...... */
240 };
241 
242 /* Define what the initial system looks like.  */
243 
244 #ifdef CTEST
test_application_define(void * first_unused_memory)245 VOID test_application_define(void *first_unused_memory)
246 #else
247 void    netx_dhcp_server_test_application_define(void *first_unused_memory)
248 #endif
249 {
250 
251 UINT    status;
252 
253 
254     /* Setup the working pointer.  */
255     pointer =  (CHAR *) first_unused_memory;
256 
257     /* Create the client thread.  */
258     tx_thread_create(&client_thread, "thread client", client_thread_entry, 0,
259             pointer, DEMO_STACK_SIZE,
260             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
261     pointer =  pointer + DEMO_STACK_SIZE;
262 
263     /* Create the server thread.  */
264     tx_thread_create(&server_thread, "thread server", server_thread_entry, 0,
265             pointer, DEMO_STACK_SIZE,
266             3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
267     pointer =  pointer + DEMO_STACK_SIZE;
268 
269     /* Initialize the NetX system.  */
270     nx_system_initialize();
271 
272     /* Create the client packet pool.  */
273     status =  nx_packet_pool_create(&client_pool, "NetX Main Packet Pool", 1024, pointer, NX_PACKET_POOL_SIZE);
274     pointer = pointer + NX_PACKET_POOL_SIZE;
275 
276     /* Check for pool creation error.  */
277     if (status)
278         error_counter++;
279 
280     /* Create the server packet pool.  */
281     status =  nx_packet_pool_create(&server_pool, "NetX Main Packet Pool", 1024, pointer, NX_PACKET_POOL_SIZE);
282     pointer = pointer + NX_PACKET_POOL_SIZE;
283 
284     /* Check for pool creation error.  */
285     if (status)
286         error_counter++;
287 
288     /* Create an IP instance for the DHCP Client.  */
289     status = nx_ip_create(&client_ip, "DHCP Client", IP_ADDRESS(0, 0, 0, 0), 0xFFFFFF00UL, &client_pool, _nx_ram_network_driver_1024, pointer, 2048, 1);
290 
291     pointer =  pointer + 2048;
292 
293     /* Check for IP create errors.  */
294     if (status)
295         error_counter++;
296 
297     /* Create an IP instance for the DHCP Server.  */
298     status = nx_ip_create(&server_ip, "DHCP Server", NX_DHCP_SERVER_IP_ADDRESS_0, 0xFFFFFF00UL, &server_pool, _nx_ram_network_driver_1024, pointer, 2048, 1);
299 
300     pointer =  pointer + 2048;
301 
302     /* Check for IP create errors.  */
303     if (status)
304         error_counter++;
305 
306     /* Enable ARP and supply ARP cache memory for DHCP Client IP.  */
307     status =  nx_arp_enable(&client_ip, (void *) pointer, 1024);
308     pointer = pointer + 1024;
309 
310     /* Check for ARP enable errors.  */
311     if (status)
312         error_counter++;
313 
314     /* Enable ARP and supply ARP cache memory for DHCP Server IP.  */
315     status =  nx_arp_enable(&server_ip, (void *) pointer, 1024);
316     pointer = pointer + 1024;
317 
318     /* Check for ARP enable errors.  */
319     if (status)
320         error_counter++;
321 
322     /* Enable UDP traffic.  */
323     status =  nx_udp_enable(&client_ip);
324 
325     /* Check for UDP enable errors.  */
326     if (status)
327         error_counter++;
328 
329     /* Enable UDP traffic.  */
330     status =  nx_udp_enable(&server_ip);
331 
332     /* Check for UDP enable errors.  */
333     if (status)
334         error_counter++;
335 
336     /* Enable ICMP.  */
337     status =  nx_icmp_enable(&client_ip);
338 
339     /* Check for errors.  */
340     if (status)
341         error_counter++;
342 
343     /* Enable ICMP.  */
344     status =  nx_icmp_enable(&server_ip);
345 
346     /* Check for errors.  */
347     if (status)
348         error_counter++;
349 
350     return;
351 }
352 
353 /* Define the test threads.  */
354 
server_thread_entry(ULONG thread_input)355 void    server_thread_entry(ULONG thread_input)
356 {
357 
358 UINT        status;
359 UINT        iface_index;
360 UINT        addresses_added;
361 
362     printf("NetX Test:   DHCP Server Test..........................................");
363 
364     /* Create the DHCP Server.  */
365     status =  nx_dhcp_server_create(&dhcp_server, &server_ip, pointer, DEMO_STACK_SIZE,
366                                    "DHCP Server", &server_pool);
367 
368     pointer = pointer + DEMO_STACK_SIZE;
369 
370     /* Check for errors creating the DHCP Server. */
371     if (status)
372         error_counter++;
373 
374     /* Load the assignable DHCP IP addresses for the first interface.  */
375     iface_index = 0;
376 
377     status = nx_dhcp_create_server_ip_address_list(&dhcp_server, iface_index, START_IP_ADDRESS_LIST_0,
378                                                    END_IP_ADDRESS_LIST_0, &addresses_added);
379 
380     /* Check for errors creating the list. */
381     if (status)
382     {
383         error_counter++;
384     }
385 
386     /* Verify all the addresses were added to the list. */
387     if (addresses_added != 10)
388     {
389         error_counter++;
390     }
391 
392     status = nx_dhcp_set_interface_network_parameters(&dhcp_server, iface_index, NX_DHCP_SUBNET_MASK_0,
393                                                       NX_DHCP_DEFAULT_GATEWAY_0, NX_DHCP_DNS_SERVER_0);
394 
395     /* Check for errors setting network parameters. */
396     if (status)
397     {
398         error_counter++;
399     }
400 
401     /* Start DHCP Server task.  */
402     status = nx_dhcp_server_start(&dhcp_server);
403 
404     /* Check for errors starting up the DHCP server.  */
405     if (status)
406     {
407         error_counter++;
408     }
409 
410     tx_thread_sleep(1 * NX_IP_PERIODIC_RATE);
411 
412     if ((error_counter) || (resposner_counter != 2))
413     {
414         printf("ERROR!\n");
415         test_control_return(1);
416     }
417     else
418     {
419         printf("SUCCESS!\n");
420         test_control_return(0);
421     }
422 
423     return;
424 }
425 
426 /* Define the test threads.  */
427 
client_thread_entry(ULONG thread_input)428 void    client_thread_entry(ULONG thread_input)
429 {
430 UINT        status;
431 NX_PACKET   *my_packet;
432 
433     /* Send the DHCP DISCOVER packet.  */
434     status = nx_packet_allocate(&client_pool, &my_packet, NX_UDP_PACKET, NX_WAIT_FOREVER);
435 
436     /* Check status */
437     if(status)
438         error_counter ++;
439 
440     /* Set the callback function to process the DHCP Message packet.  */
441     advanced_packet_process_callback = my_packet_process;
442 
443     /* Fill in the packet with data. Skip the MAC header.  */
444     memcpy(my_packet -> nx_packet_prepend_ptr, &pkt1[14], sizeof(pkt1) - 14);
445     my_packet -> nx_packet_length = sizeof(pkt1) - 14;
446     my_packet -> nx_packet_append_ptr = my_packet -> nx_packet_prepend_ptr + sizeof(pkt1) - 14;
447 
448     /* Directly receive the DHCP DISCOVER packet.  */
449     _nx_ip_packet_deferred_receive(&server_ip, my_packet);
450 }
451 
my_packet_process(NX_IP * ip_ptr,NX_PACKET * packet_ptr,UINT * operation_ptr,UINT * delay_ptr)452 static UINT    my_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT *operation_ptr, UINT *delay_ptr)
453 {
454 
455 UINT        status;
456 NX_PACKET   *my_packet;
457 
458     if ((ip_ptr == &server_ip) && (packet_ptr ->nx_packet_length >= 300))
459     {
460 
461         resposner_counter ++;
462 
463         if (resposner_counter == 1)
464         {
465 
466             /* Check the DHCP OFFER.  */
467 #ifdef NX_ENABLE_IP_ID_RANDOMIZATION
468             /* Skip IP id when it is random. */
469             if (memcmp(packet_ptr ->nx_packet_prepend_ptr, &pkt2[14], 4) ||
470                 memcmp(packet_ptr ->nx_packet_prepend_ptr + 6, &pkt2[20], 4) ||
471                 memcmp(packet_ptr ->nx_packet_prepend_ptr + 12, &pkt2[26], sizeof(pkt2) - 26))
472 #else
473             if (memcmp(packet_ptr ->nx_packet_prepend_ptr, &pkt2[14], sizeof(pkt2) -14))
474 #endif
475                 error_counter ++;
476 
477             /* Send the DHCP REQUEST.  */
478             status = nx_packet_allocate(&client_pool, &my_packet, NX_UDP_PACKET, NX_WAIT_FOREVER);
479 
480             /* Check status */
481             if(status)
482                 error_counter ++;
483 
484             /* Fill in the packet with data. Skip the MAC header.  */
485             memcpy(my_packet -> nx_packet_prepend_ptr, &pkt3[14], sizeof(pkt3) - 14);
486             my_packet -> nx_packet_length = sizeof(pkt3) - 14;
487             my_packet -> nx_packet_append_ptr = my_packet -> nx_packet_prepend_ptr + sizeof(pkt3) - 14;
488 
489             /* Directly receive the DHCP REQUEST packet.  */
490             _nx_ip_packet_deferred_receive(&server_ip, my_packet);
491         }
492         else if (resposner_counter == 2)
493         {
494 
495             /* Check the DHCP ACK.  */
496 #ifdef NX_ENABLE_IP_ID_RANDOMIZATION
497             /* Skip IP id when it is random. */
498             if (memcmp(packet_ptr -> nx_packet_prepend_ptr, &pkt4[14], 4) ||
499                 memcmp(packet_ptr -> nx_packet_prepend_ptr + 6, &pkt4[20], 4) ||
500                 memcmp(packet_ptr -> nx_packet_prepend_ptr + 12, &pkt4[26], sizeof(pkt4) - 26))
501 #else
502             if (memcmp(packet_ptr -> nx_packet_prepend_ptr, &pkt4[14], sizeof(pkt4) -14))
503 #endif
504                 error_counter ++;
505         }    ;
506     }
507 
508     return NX_TRUE;
509 }
510 #else
511 #ifdef CTEST
test_application_define(void * first_unused_memory)512 VOID test_application_define(void *first_unused_memory)
513 #else
514 void    netx_dhcp_server_test_application_define(void *first_unused_memory)
515 #endif
516 {
517     printf("NetX Test:   DHCP Server Test..........................................N/A\n");
518     test_control_return(3);
519 }
520 #endif
521