1 /* This NetX test concentrates on the UDP port unreachable.  */
2 
3 
4 #include   "nx_api.h"
5 
6 extern void  test_control_return(UINT status);
7 #if defined(__PRODUCT_NETXDUO__) && !defined(NX_DISABLE_ICMPV4_ERROR_MESSAGE) && !defined(NX_DISABLE_ICMP_INFO) && !defined(NX_DISABLE_IPV4)
8 #define     DEMO_STACK_SIZE         2048
9 
10 
11 /* Define the ThreadX and NetX object control blocks...  */
12 
13 static TX_THREAD               thread_0;
14 
15 static NX_PACKET_POOL          pool_0;
16 static NX_IP                   ip_0;
17 static NX_IP                   ip_1;
18 
19 static NX_UDP_SOCKET           socket_0;
20 static NX_UDP_SOCKET           socket_1;
21 
22 
23 /* Define the counters used in the demo application...  */
24 
25 static ULONG                   error_counter;
26 
27 /* Define thread prototypes.  */
28 
29 static void    thread_0_entry(ULONG thread_input);
30 extern void    _nx_ram_network_driver_256(struct NX_IP_DRIVER_STRUCT *driver_req);
31 
32 /* Define what the initial system looks like.  */
33 
34 #ifdef CTEST
test_application_define(void * first_unused_memory)35 VOID test_application_define(void *first_unused_memory)
36 #else
37 void    netx_udp_port_unreachable_test_application_define(void *first_unused_memory)
38 #endif
39 {
40 
41 CHAR    *pointer;
42 UINT    status;
43 
44 
45     /* Setup the working pointer.  */
46     pointer =  (CHAR *) first_unused_memory;
47 
48     error_counter =  0;
49 
50     /* Create the client thread.  */
51     tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
52             pointer, DEMO_STACK_SIZE,
53             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
54     pointer =  pointer + DEMO_STACK_SIZE;
55 
56     /* Initialize the NetX system.  */
57     nx_system_initialize();
58 
59     /* Create a packet pool.  */
60     status =  nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", 256, pointer, 2048);
61     pointer = pointer + 2048;
62 
63     /* Check for pool creation error.  */
64     if (status)
65         error_counter++;
66 
67     /* Create an IP instance.  */
68     status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(1, 2, 3, 4), 0xFFFFF000UL, &pool_0, _nx_ram_network_driver_256,
69                     pointer, 2048, 1);
70     pointer =  pointer + 2048;
71 
72     /* Create an IP instance.  */
73     status += nx_ip_create(&ip_1, "NetX IP Instance 1", IP_ADDRESS(1, 2, 3, 5), 0xFFFFF000UL, &pool_0, _nx_ram_network_driver_256,
74                     pointer, 2048, 1);
75     pointer =  pointer + 2048;
76 
77     /* Check for IP create errors.  */
78     if (status)
79         error_counter++;
80 
81     /* Enable ARP and supply ARP cache memory for IP Instance 0.  */
82     status =  nx_arp_enable(&ip_0, (void *) pointer, 1024);
83     pointer = pointer + 1024;
84 
85     /* Enable ARP and supply ARP cache memory for IP Instance 1.  */
86     status +=  nx_arp_enable(&ip_1, (void *) pointer, 1024);
87     pointer = pointer + 1024;
88 
89     /* Enable UDP traffic.  */
90     status +=  nx_udp_enable(&ip_0);
91     status +=  nx_udp_enable(&ip_1);
92 
93     /* Enable ICMP.  */
94     status +=  nx_icmp_enable(&ip_0);
95     status +=  nx_icmp_enable(&ip_1);
96 
97     /* Check for errors.  */
98     if (status)
99         error_counter++;
100 }
101 
102 
103 
104 /* Define the test threads.  */
105 
thread_0_entry(ULONG thread_input)106 static void    thread_0_entry(ULONG thread_input)
107 {
108 
109 UINT        status;
110 NX_PACKET  *my_packet;
111 
112 
113     /* Print out some test information banners.  */
114     printf("NetX Test:   UDP Port Unreachable Test.................................");
115 
116     /* Check for earlier error.  */
117     if (error_counter)
118     {
119 
120         printf("ERROR!\n");
121         test_control_return(1);
122     }
123 
124 
125     /* Create a UDP socket.  */
126     status = nx_udp_socket_create(&ip_0, &socket_0, "Socket 0", NX_IP_NORMAL, NX_FRAGMENT_OKAY, 0x80, 5);
127 
128     /* Check status.  */
129     if (status)
130     {
131         error_counter++;
132         test_control_return(1);
133     }
134 
135     /* Bind the UDP socket to the IP port.  */
136     status =  nx_udp_socket_bind(&socket_0, 0x88, TX_WAIT_FOREVER);
137 
138     /* Check status.  */
139     if (status)
140     {
141 
142         printf("ERROR!\n");
143         test_control_return(1);
144     }
145 
146     /* Allocate a packet.  */
147     status =  nx_packet_allocate(&pool_0, &my_packet, NX_UDP_PACKET, TX_WAIT_FOREVER);
148 
149     /* Check status.  */
150     if (status != NX_SUCCESS)
151     {
152         printf("ERROR!\n");
153         test_control_return(1);
154     }
155 
156     /* Append data.  */
157     status = nx_packet_data_append(my_packet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ  ", 28, &pool_0, NX_IP_PERIODIC_RATE);
158 
159     /* Check status.  */
160     if (status != NX_SUCCESS)
161     {
162         printf("ERROR!\n");
163         test_control_return(1);
164     }
165 
166     status = nx_udp_socket_send(&socket_0, my_packet, IP_ADDRESS(1, 2, 3, 5), 0x89);
167 
168     /* Check status.  */
169     if (status != NX_SUCCESS)
170     {
171         printf("ERROR!\n");
172         test_control_return(1);
173     }
174 
175     /* Verify the ICMPv4 error message is received. */
176     if (ip_0.nx_ip_icmp_unhandled_messages != 1)
177     {
178         printf("ERROR!\n");
179         test_control_return(1);
180     }
181 
182 
183     /* Now create socket 1 and bind to port 0x8089. The hash value of 0x8089 is the same as 0x89. */
184     /* Create a UDP socket.  */
185     status = nx_udp_socket_create(&ip_1, &socket_1, "Socket 1", NX_IP_NORMAL, NX_FRAGMENT_OKAY, 0x80, 5);
186 
187     /* Check status.  */
188     if (status != NX_SUCCESS)
189     {
190         printf("ERROR!\n");
191         test_control_return(1);
192     }
193 
194     /* Bind the UDP socket to the IP port.  */
195     status =  nx_udp_socket_bind(&socket_1, 0x8089, TX_WAIT_FOREVER);
196 
197     /* Check status.  */
198     if (status != NX_SUCCESS)
199     {
200         printf("ERROR!\n");
201         test_control_return(1);
202     }
203 
204     /* Allocate a packet.  */
205     status =  nx_packet_allocate(&pool_0, &my_packet, NX_UDP_PACKET, TX_WAIT_FOREVER);
206 
207     /* Check status.  */
208     if (status != NX_SUCCESS)
209     {
210         printf("ERROR!\n");
211         test_control_return(1);
212     }
213 
214     /* Append data.  */
215     status = nx_packet_data_append(my_packet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ  ", 28, &pool_0, NX_IP_PERIODIC_RATE);
216 
217     /* Check status.  */
218     if (status != NX_SUCCESS)
219     {
220         printf("ERROR!\n");
221         test_control_return(1);
222     }
223 
224     status = nx_udp_socket_send(&socket_0, my_packet, IP_ADDRESS(1, 2, 3, 5), 0x89);
225 
226     /* Check status.  */
227     if (status != NX_SUCCESS)
228     {
229         printf("ERROR!\n");
230         test_control_return(1);
231     }
232 
233     /* Verify the ICMPv4 error message is received. */
234     if (ip_0.nx_ip_icmp_unhandled_messages != 2)
235     {
236         printf("ERROR!\n");
237         test_control_return(1);
238     }
239 
240 
241     /* Unbind the UDP socket.  */
242     status =  nx_udp_socket_unbind(&socket_0);
243 
244     /* Check status.  */
245     if (status)
246     {
247         printf("ERROR!\n");
248         test_control_return(1);
249     }
250 
251     /* Delete the UDP socket.  */
252     status =  nx_udp_socket_delete(&socket_0);
253 
254     /* Check status.  */
255     if (status)
256     {
257         printf("ERROR!\n");
258         test_control_return(1);
259     }
260 
261     /* Unbind the UDP socket.  */
262     status =  nx_udp_socket_unbind(&socket_1);
263 
264     /* Check status.  */
265     if (status != NX_SUCCESS)
266     {
267         printf("ERROR!\n");
268         test_control_return(1);
269     }
270 
271     /* Delete the UDP socket.  */
272     status =  nx_udp_socket_delete(&socket_1);
273 
274     /* Check status.  */
275     if (status != NX_SUCCESS)
276     {
277         printf("ERROR!\n");
278         test_control_return(1);
279     }
280 
281     printf("SUCCESS!\n");
282     test_control_return(0);
283 }
284 #else
285 #ifdef CTEST
test_application_define(void * first_unused_memory)286 VOID test_application_define(void *first_unused_memory)
287 #else
288 void    netx_udp_port_unreachable_test_application_define(void *first_unused_memory)
289 #endif
290 {
291 
292     /* Print out some test information banners.  */
293     printf("NetX Test:   UDP Port Unreachable Test.................................N/A\n");
294     test_control_return(3);
295 }
296 #endif
297