1 /* This NetX test concentrates on the basic DNS operation. Unencode invalid string.  */
2 
3 /* Frame (125 bytes) */
4 static unsigned char invalid_ptr_response_0[125] = {
5 0xf4, 0x8e, 0x38, 0xa3, 0xab, 0xf6, 0x8c, 0xec, /* ..8..... */
6 0x4b, 0x68, 0xd1, 0xfe, 0x08, 0x00, 0x45, 0x00, /* Kh....E. */
7 0x00, 0x6f, 0x1a, 0x9a, 0x40, 0x00, 0x40, 0x11, /* .o..@.@. */
8 0xd6, 0x2e, 0xc0, 0xa8, 0x64, 0x02, 0xc0, 0xa8, /* ....d... */
9 0x64, 0x62, 0x00, 0x35, 0xef, 0x67, 0x00, 0x5b, /* db.5.g.[ */
10 0x7e, 0xb5, 0x00, 0x02, 0x81, 0x80, 0x00, 0x01, /* ~....... */
11 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x31, /* .......1 */
12 0x34, 0x03, 0x32, 0x30, 0x33, 0x02, 0x35, 0x38, /* 4.203.58 */
13 0x03, 0x32, 0x31, 0x36, 0x07, 0x69, 0x6e, 0x2d, /* .216.in- */
14 0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, /* addr.arp */
15 0x61, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x02, 0x31, /* a......1 */
16 0x34, 0x03, 0x32, 0x30, 0x33, 0x02, 0x35, 0x38, /* 4.203.58 */
17 0x03, 0x32, 0x31, 0x36, 0x07, 0x69, 0x6e, 0x2d, /* .216.in- */
18 0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, /* addr.arp */
19 0x61, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, /* a....... */
20 0x95, 0x04, 0x00, 0x01, 0x00                    /* ..... */
21 };
22 
23 /* Frame-2 (128 bytes) */
24 static unsigned char invalid_ptr_response_1[128] = {
25 0xf4, 0x8e, 0x38, 0xa3, 0xab, 0xf6, 0x8c, 0xec, /* ..8..... */
26 0x4b, 0x68, 0xd1, 0xfe, 0x08, 0x00, 0x45, 0x00, /* Kh....E. */
27 0x00, 0x6f, 0x1a, 0x9a, 0x40, 0x00, 0x40, 0x11, /* .o..@.@. */
28 0xd6, 0x2e, 0xc0, 0xa8, 0x64, 0x02, 0xc0, 0xa8, /* ....d... */
29 0x64, 0x62, 0x00, 0x35, 0xef, 0x67, 0x00, 0x5b, /* db.5.g.[ */
30 0x7e, 0xb5, 0x00, 0x02, 0x81, 0x80, 0x00, 0x01, /* ~....... */
31 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x31, /* .......1 */
32 0x34, 0x03, 0x32, 0x30, 0x33, 0x02, 0x35, 0x38, /* 4.203.58 */
33 0x03, 0x32, 0x31, 0x36, 0x07, 0x69, 0x6e, 0x2d, /* .216.in- */
34 0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, /* addr.arp */
35 0x61, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x02, 0x31, /* a......1 */
36 0x34, 0x03, 0x32, 0x30, 0x33, 0x02, 0x35, 0x38, /* 4.203.58 */
37 0x03, 0x32, 0x31, 0x36, 0x07, 0x69, 0x6e, 0x2d, /* .216.in- */
38 0x61, 0x64, 0x64, 0x72, 0x04, 0x61, 0x72, 0x70, /* addr.arp */
39 0x61, 0x00, 0x00, 0x0c, 0x00, 0x01, 0x00, 0x00, /* a....... */
40 0x95, 0x04, 0x00, 0x01, 0xc0, 0x54, 0xc0, 0x52                    /* ..... */
41 };
42 
43 #include   "tx_api.h"
44 #include   "nx_api.h"
45 #include   "nx_udp.h"
46 #include   "nxd_dns.h"
47 #include   "nx_ram_network_driver_test_1500.h"
48 
49 extern void    test_control_return(UINT status);
50 #ifndef NX_DISABLE_IPV4
51 
52 #define     DEMO_STACK_SIZE         2048
53 
54 
55 /* Define the ThreadX and NetX object control blocks...  */
56 
57 static TX_THREAD               thread_0;
58 static TX_THREAD               thread_1;
59 
60 static NX_PACKET_POOL          pool_0;
61 static NX_IP                   client_ip;
62 static NX_IP                   server_ip;
63 
64 static NX_UDP_SOCKET           server_socket;
65 static NX_DNS                  client_dns;
66 
67 
68 #ifdef NX_DNS_CLIENT_USER_CREATE_PACKET_POOL
69 NX_PACKET_POOL                 client_pool;
70 #endif
71 
72 /* Define the counters used in the demo application...  */
73 
74 static UINT                    status;
75 static ULONG                   error_counter;
76 
77 /* Define thread prototypes.  */
78 
79 static void    thread_0_entry(ULONG thread_input);
80 static void    thread_1_entry(ULONG thread_input);
81 extern void    _nx_ram_network_driver_512(struct NX_IP_DRIVER_STRUCT *driver_req);
82 
83 #define        DNS_START_OFFSET (14 + 20 + 8)
84 
85 /* Define what the initial system looks like.  */
86 
87 #ifdef CTEST
test_application_define(void * first_unused_memory)88 VOID test_application_define(void *first_unused_memory)
89 #else
90 void    netx_dns_invalid_name_unencode_test_application_define(void *first_unused_memory)
91 #endif
92 {
93 
94 CHAR    *pointer;
95 
96     /* Setup the working pointer.  */
97     pointer =  (CHAR *) first_unused_memory;
98 
99     /* Create the DNS main thread.  */
100     tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
101             pointer, DEMO_STACK_SIZE,
102             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
103     pointer =  pointer + DEMO_STACK_SIZE;
104 
105     /* .  */
106     tx_thread_create(&thread_1, "thread 1", thread_1_entry, 0,
107             pointer, DEMO_STACK_SIZE,
108             3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
109     pointer =  pointer + DEMO_STACK_SIZE;
110 
111     /* Initialize the NetX system.  */
112     nx_system_initialize();
113 
114     /* Create a packet pool.  */
115     status =  nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 1500, pointer, 8192);
116     pointer = pointer + 8192;
117 
118 #ifdef NX_DNS_CLIENT_USER_CREATE_PACKET_POOL
119 
120     /* Create the packet pool for the DNS Client to send packets.
121 
122         If the DNS Client is configured for letting the host application create
123         the DNS packet pool, (see NX_DNS_CLIENT_USER_CREATE_PACKET_POOL option), see
124        nx_dns_create() for guidelines on packet payload size and pool size.
125        packet traffic for NetX Duo processes.
126     */
127     status =  nx_packet_pool_create(&client_pool, "DNS Client Packet Pool", NX_DNS_PACKET_PAYLOAD, pointer, NX_DNS_PACKET_POOL_SIZE);
128 
129     pointer = pointer + NX_DNS_PACKET_POOL_SIZE;
130 
131     /* Check for pool creation error.  */
132     if (status)
133         return;
134 #endif
135 
136     /* Check for pool creation error.  */
137     if (status)
138         error_counter++;
139 
140     /* Create an IP instance.  */
141     status = nx_ip_create(&client_ip, "NetX IP Instance 0", IP_ADDRESS(192, 168, 100, 98), 0xFFFF0000UL, &pool_0, _nx_ram_network_driver_512,
142                     pointer, 2048, 1);
143     pointer =  pointer + 2048;
144 
145     /* Create another IP instance.  */
146     status += nx_ip_create(&server_ip, "NetX IP Instance 1", IP_ADDRESS(192, 168, 100, 2), 0xFFFF0000UL, &pool_0, _nx_ram_network_driver_512,
147                     pointer, 2048, 1);
148     pointer =  pointer + 2048;
149 
150     /* Check for IP create errors.  */
151     if (status)
152         error_counter++;
153 
154     /* Enable ARP and supply ARP cache memory for IP Instance 0.  */
155     status =  nx_arp_enable(&client_ip, (void *) pointer, 1024);
156     pointer = pointer + 1024;
157 
158     /* Enable ARP and supply ARP cache memory for IP Instance 1.  */
159     status +=  nx_arp_enable(&server_ip, (void *) pointer, 1024);
160     pointer = pointer + 1024;
161 
162     /* Check for ARP enable errors.  */
163     if (status)
164         error_counter++;
165 
166     /* Enable UDP traffic.  */
167     status =  nx_udp_enable(&client_ip);
168     status += nx_udp_enable(&server_ip);
169 
170     /* Check for UDP enable errors.  */
171     if (status)
172         error_counter++;
173 }
174 
175 
176 
177 /* Define the test threads.  */
178 
thread_0_entry(ULONG thread_input)179 static void    thread_0_entry(ULONG thread_input)
180 {
181 
182 UCHAR       host_name[300];
183 UINT        i;
184 
185     /* Print out some test information banners.  */
186     printf("NetX Test:   DNS Invalid Name Unencode Test............................");
187 
188     /* Check for earlier error.  */
189     if(error_counter)
190     {
191         printf("ERROR!\n");
192         test_control_return(1);
193     }
194 
195     /* Create a DNS instance for the Client.  Note this function will create
196        the DNS Client packet pool for creating DNS message packets intended
197        for querying its DNS server. */
198     status =  nx_dns_create(&client_dns, &client_ip, (UCHAR *)"DNS Client");
199 
200     /* Check status.  */
201     if(status)
202     {
203         printf("ERROR!\n");
204         test_control_return(1);
205     }
206 
207     /* Is the DNS client configured for the host application to create the pecket pool? */
208 #ifdef NX_DNS_CLIENT_USER_CREATE_PACKET_POOL
209 
210     /* Yes, use the packet pool created above which has appropriate payload size
211        for DNS messages. */
212      status = nx_dns_packet_pool_set(&client_dns, &client_pool);
213 
214      /* Check for set DNS packet pool error.  */
215     if(status)
216     {
217         printf("ERROR!\n");
218         test_control_return(1);
219     }
220 
221 #endif /* NX_DNS_CLIENT_USER_CREATE_PACKET_POOL */
222 
223     /* Add an IPv4 server address to the Client list. */
224     status = nx_dns_server_add(&client_dns, IP_ADDRESS(192, 168, 100, 2));
225 
226     /* Check status.  */
227     if(status)
228     {
229         printf("ERROR!\n");
230         test_control_return(1);
231     }
232 
233     host_name[0] = '.';
234     host_name[1] = '.';
235     host_name[2] = '.';
236     host_name[3] = '.';
237 
238     for (i = 0; i < 2; i++) {
239         /* Send DNS PTR query, response invalid name. */
240         status = nx_dns_host_by_address_get(&client_dns, IP_ADDRESS(216, 58, 203, 14), &host_name[4], 256, NX_IP_PERIODIC_RATE);
241 
242         /* Sleep 1s to check if crash.  */
243         tx_thread_sleep(NX_IP_PERIODIC_RATE);
244 
245         /* Check status.  */
246         if(status == NX_SUCCESS)
247         {
248             printf("ERROR!\n");
249             test_control_return(1);
250         }
251 
252         /* Determine if the test was successful.  */
253         if(error_counter)
254         {
255             printf("ERROR!\n");
256             test_control_return(1);
257         }
258     }
259 
260     printf("SUCCESS!\n");
261     test_control_return(0);
262 }
263 
264 
thread_1_entry(ULONG thread_input)265 static void    thread_1_entry(ULONG thread_input)
266 {
267 
268 NX_PACKET   *my_packet;
269 UINT        port;
270 USHORT      nx_dns_transmit_id;
271 UCHAR       *data_ptr;
272 NX_PACKET   *response_packet[2];
273 UINT        i;
274 UCHAR       *invalid_ptr_response;
275 UINT        response_len;
276 
277     /* Create a UDP socket act as the DNS server.  */
278     status = nx_udp_socket_create(&server_ip, &server_socket, "Socket 1", NX_IP_NORMAL, NX_FRAGMENT_OKAY, 0x80, 5);
279 
280     /* Check status.  */
281     if (status)
282     {
283         error_counter++;
284     }
285 
286     /* Bind the UDP socket to the IP port.  */
287     status =  nx_udp_socket_bind(&server_socket, 53, TX_WAIT_FOREVER);
288 
289 
290     /* Act as the DNS server to receive the DNS query and send the DNS response.  */
291     for (i = 0; i < 2; i++)
292     {
293 
294         /* Receive a UDP packet.  */
295         status = nx_udp_socket_receive(&server_socket, &my_packet, 10 * NX_IP_PERIODIC_RATE);
296 
297         /* Check status.  */
298         if (status)
299         {
300             error_counter++;
301             return;
302         }
303 
304         /* Get the DNS client UDP port.  */
305         status = nx_udp_packet_info_extract(my_packet, NX_NULL, NX_NULL, &port, NX_NULL);
306 
307         /* Check status.  */
308         if (status)
309         {
310             error_counter++;
311             return;
312         }
313 
314         /* Get the DNS transmit ID.  */
315         data_ptr = my_packet -> nx_packet_prepend_ptr + NX_DNS_ID_OFFSET;
316         nx_dns_transmit_id = *data_ptr++;
317         nx_dns_transmit_id = (USHORT)((nx_dns_transmit_id << 8) | *data_ptr);
318 
319         /* Release the packet.  */
320         nx_packet_release(my_packet);
321 
322         /* Send the DNS response packet.  */
323         /* Allocate a response packet.  */
324         status =  nx_packet_allocate(&pool_0, &response_packet[i], NX_UDP_PACKET, TX_WAIT_FOREVER);
325 
326         /* Check status.  */
327         if (status)
328         {
329             error_counter++;
330             return;
331         }
332 
333         if (i == 0)
334         {
335             invalid_ptr_response = invalid_ptr_response_0;
336             response_len = 125;
337         } else {
338             invalid_ptr_response = invalid_ptr_response_1;
339             response_len = 128;
340         }
341         /* Write the DNS response messages into the packet payload!  */
342         memcpy(response_packet[i] -> nx_packet_prepend_ptr, invalid_ptr_response + DNS_START_OFFSET, response_len - DNS_START_OFFSET);
343 
344         /* Adjust the write pointer.  */
345         response_packet[i] -> nx_packet_length =  response_len - DNS_START_OFFSET;
346         response_packet[i] -> nx_packet_append_ptr =  response_packet[i] -> nx_packet_prepend_ptr + response_packet[i] -> nx_packet_length;
347 
348         /* Update the DNS transmit ID.  */
349         data_ptr = response_packet[i] -> nx_packet_prepend_ptr + NX_DNS_ID_OFFSET;
350         *data_ptr++ = (UCHAR)(nx_dns_transmit_id >> 8);
351         *data_ptr = (UCHAR)nx_dns_transmit_id;
352 
353         /* Send the UDP packet with the correct port.  */
354         status =  nx_udp_socket_send(&server_socket, response_packet[i], IP_ADDRESS(192, 168, 100, 98), port);
355 
356         /* Check the status.  */
357         if (status)
358         {
359             error_counter++;
360             nx_packet_release(response_packet[i]);
361             return;
362         }
363 
364     }
365     /* Unbind the UDP socket.  */
366     status =  nx_udp_socket_unbind(&server_socket);
367 
368     /* Check status.  */
369     if (status)
370     {
371         error_counter++;
372         return;
373     }
374 
375     /* Delete the UDP socket.  */
376     status =  nx_udp_socket_delete(&server_socket);
377 
378     /* Check status.  */
379     if (status)
380     {
381         error_counter++;
382         return;
383     }
384 
385     /* Let the DNS threads execute.    */
386     tx_thread_relinquish();
387 }
388 #else
389 
390 #ifdef CTEST
test_application_define(void * first_unused_memory)391 VOID test_application_define(void *first_unused_memory)
392 #else
393 void    netx_dns_invalid_name_unencode_test_application_define(void *first_unused_memory)
394 #endif
395 
396 {
397 
398     /* Print out test information banner.  */
399     printf("NetX Test:   DNS Invalid Name Unencode Test............................N/A\n");
400     test_control_return(3);
401 
402 }
403 #endif
404