1 #include    "tx_api.h"
2 #include    "nx_api.h"
3 #include    "nxd_ftp_client.h"
4 
5 extern   void  test_control_return(UINT);
6 
7 #if !defined(NX_DISABLE_IPV4)
8 
9 #define     DEMO_STACK_SIZE         2048
10 #define     PACKET_PAYLOAD          1518
11 
12 
13 /* Define the ThreadX, NetX, and FileX object control blocks...  */
14 
15 static NX_TCP_SOCKET           server_socket;
16 static NX_TCP_SOCKET           server_socket_passive;
17 static TX_THREAD               client_thread;
18 static TX_THREAD               server_thread;
19 static NX_PACKET_POOL          client_pool;
20 static NX_PACKET_POOL          server_pool;
21 static NX_IP                   client_ip;
22 static NX_IP                   server_ip;
23 
24 /* Define the NetX FTP object control block.  */
25 
26 static NX_FTP_CLIENT           ftp_client;
27 
28 
29 /* Define the counters used in the demo application...  */
30 
31 static  UINT            error_counter = 0;
32 static  UINT            client_thread_done = NX_FALSE;
33 
34 
35 #define FTP_SERVER_ADDRESS  IP_ADDRESS(1,2,3,4)
36 #define FTP_CLIENT_ADDRESS  IP_ADDRESS(1,2,3,5)
37 
38 #define     SERVER_PORT                 21
39 #define     SERVER_PASSIVE_PORT1        21017
40 #define     SERVER_PASSIVE_PORT2        21018
41 
42 
43 static  void   server_thread_entry(ULONG thread_input);
44 static  void   client_thread_entry(ULONG thread_input);
45 static  UINT   nx_ftp_response_packet_send(NX_TCP_SOCKET *server_socket, UCHAR *data, UINT data_size);
46 
47 extern   void _nx_ram_network_driver_1024(NX_IP_DRIVER *driver_req_ptr);
48 
49 
50 /* There are for logging in */
51 static UCHAR welcome_220_response_1[27] = {
52 0x32, 0x32, 0x30, 0x2d, 0x4d, 0x69, 0x63, 0x72,  /* 220-Micr */
53 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x20, 0x46, 0x54,  /* osoft FT */
54 0x50, 0x20, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63,  /* PServic  */
55 0x65, 0x0d, 0x0a                                 /* e..      */
56 };
57 
58 static UINT welcome_220_response_1_size = 27;
59 
60 static UCHAR welcome_220_response_2[21] = {
61 0x32, 0x32, 0x30, 0x20, 0x57, 0x69, 0x6e, 0x68, /* 220 Winh */
62 0x6f, 0x73, 0x74, 0x2e, 0x63, 0x6f, 0x6d, 0x20, /* ost.com  */
63 0x46, 0x54, 0x50, 0x0d, 0x0a                    /* FTP..    */
64 };
65 
66 static UINT welcome_220_response_2_size = 21;
67 
68 static UCHAR password_request_331[23] = {
69 0x33, 0x33, 0x31, 0x20, 0x50, 0x61, 0x73, 0x73, /* 331 Pass */
70 0x77, 0x6f, 0x72, 0x64, 0x20, 0x72, 0x65, 0x71, /* word req */
71 0x75, 0x69, 0x72, 0x65, 0x64, 0x0d, 0x0a        /* uired.. */
72 };
73 
74 static UINT password_request_331_size = 23;
75 
76 static UCHAR logged_in_230_response[21] = {
77 0x32, 0x33, 0x30, 0x20, 0x55, 0x73, 0x65, 0x72, /* 230 User */
78 0x20, 0x6c, 0x6f, 0x67, 0x67, 0x65, 0x64, 0x20, /*  logged  */
79 0x69, 0x6e, 0x2e, 0x0d, 0x0a                    /* in... */
80 };
81 
82 static UINT logged_in_230_response_size = 21;
83 
84 /* Define what the initial system looks like.  */
85 
86 #ifdef CTEST
test_application_define(void * first_unused_memory)87 VOID test_application_define(void *first_unused_memory)
88 #else
89 void    netx_ftp_client_multiple_connection_response_test_application_define(void *first_unused_memory)
90 #endif
91 {
92 
93 UINT    status;
94 UCHAR   *pointer;
95 
96 
97     /* Setup the working pointer.  */
98     pointer =  (UCHAR *) first_unused_memory;
99 
100     /* Initialize NetX.  */
101     nx_system_initialize();
102 
103     /* Set up the FTP Server. */
104 
105     /* Create the main FTP server thread.  */
106     status = tx_thread_create(&server_thread, "FTP Server thread ", server_thread_entry, 0,
107                               pointer, DEMO_STACK_SIZE,
108                               6, 6, TX_NO_TIME_SLICE, TX_AUTO_START);
109 
110     pointer = pointer + DEMO_STACK_SIZE ;
111 
112     /* Check status.  */
113     if (status != NX_SUCCESS)
114     {
115         error_counter++;
116         return;
117     }
118 
119     /* Create the server packet pool.  */
120     status =  nx_packet_pool_create(&server_pool, "FTP Server Packet Pool", 700,
121                                     pointer , 700*10);
122 
123     pointer = pointer + 700*10;
124     if (status)
125         error_counter++;
126 
127     /* Create an IP instance.  */
128     status = nx_ip_create(&server_ip,
129                           "FTP Server IP",
130                           FTP_SERVER_ADDRESS,
131                           0xFFFFFF00UL,
132                           &server_pool, _nx_ram_network_driver_1024,
133                           pointer, DEMO_STACK_SIZE, 1);
134 
135     pointer = pointer + DEMO_STACK_SIZE;
136 
137     if (status)
138         error_counter++;
139 
140     /* Enable ARP and supply ARP cache memory for the server IP instance.  */
141     status =  nx_arp_enable(&server_ip, (void *) pointer, 1024);
142 
143     pointer = pointer + 1024;
144 
145     if (status)
146         error_counter++;
147 
148      /* Enable TCP traffic.  */
149     status = nx_tcp_enable(&server_ip);
150     if (status)
151         error_counter++;
152 
153 
154     /* Set up the FTP Client. */
155 
156     /* Create the main FTP client thread.  */
157     status = tx_thread_create(&client_thread, "FTP Client thread ", client_thread_entry, 0,
158                               pointer, DEMO_STACK_SIZE,
159                               6, 6, TX_NO_TIME_SLICE, TX_AUTO_START);
160 
161     pointer = pointer + DEMO_STACK_SIZE ;
162 
163     /* Check status.  */
164     if (status != NX_SUCCESS)
165     {
166         error_counter++;
167         return;
168     }
169 
170     /* Create a packet pool for the FTP client.  */
171     status =  nx_packet_pool_create(&client_pool, "NetX Client Packet Pool", PACKET_PAYLOAD, pointer, 25*PACKET_PAYLOAD);
172 
173         /* Check status.  */
174     if (status != NX_SUCCESS)
175     {
176         error_counter++;
177         return;
178     }
179 
180     pointer =  pointer + 25*PACKET_PAYLOAD;
181 
182     /* Create an IP instance for the FTP client.  */
183     status = nx_ip_create(&client_ip, "NetX Client IP Instance", FTP_CLIENT_ADDRESS, 0xFFFFFF00UL,
184                           &client_pool, _nx_ram_network_driver_1024, pointer, DEMO_STACK_SIZE, 1);
185 
186         /* Check status.  */
187     if (status != NX_SUCCESS)
188     {
189         error_counter++;
190         return;
191     }
192 
193     pointer = pointer + DEMO_STACK_SIZE;
194 
195     /* Enable ARP and supply ARP cache memory for the FTP Client IP.  */
196     nx_arp_enable(&client_ip, (void *) pointer, 1024);
197 
198     pointer = pointer + 1024;
199 
200     /* Enable TCP for client IP instance.  */
201     nx_tcp_enable(&client_ip);
202     nx_icmp_enable(&client_ip);
203 
204     return;
205 
206 }
207 
208 /* Define the FTP client thread.  */
209 
client_thread_entry(ULONG thread_input)210 void    client_thread_entry(ULONG thread_input)
211 {
212 
213 UINT        status;
214 
215     /* Let the server set up. */
216     tx_thread_sleep(20);
217 
218     NX_PARAMETER_NOT_USED(thread_input);
219 
220     /* Create an FTP client.  */
221     status =  nx_ftp_client_create(&ftp_client, "FTP Client", &client_ip, 2000, &client_pool);
222 
223     /* Check status.  */
224     if (status != NX_SUCCESS)
225     {
226 
227         error_counter++;
228      }
229 
230     /* Now connect with the NetX FTP (IPv4) server on the control socket. */
231     status =  nx_ftp_client_connect(&ftp_client, FTP_SERVER_ADDRESS, "equenet0_alpha1", "29Pi2A792N", 500);
232 
233     /* Check status.  */
234     if (status != NX_SUCCESS)
235     {
236 
237         error_counter++;
238      }
239 
240     /* Delete the FTP client.  */
241     nx_ftp_client_disconnect(&ftp_client, 0);
242     nx_ftp_client_delete(&ftp_client);
243 
244     client_thread_done = NX_TRUE;
245 }
246 
247 
248 /* Define the helper FTP server thread.  */
server_thread_entry(ULONG thread_input)249 void    server_thread_entry(ULONG thread_input)
250 {
251 
252 UINT         status;
253 NX_PACKET   *my_packet;
254 
255     /* Print out test information banner.  */
256     printf("NetX Test:   FTP Client Multiple Connection Response Test..............");
257 
258     /* Check for earlier error. */
259     if(error_counter)
260     {
261         printf("ERROR!\n");
262         test_control_return(1);
263     }
264 
265     /* Create a TCP socket as the FTP server.  */
266     status = nx_tcp_socket_create(&server_ip, &server_socket, "Socket Server", NX_IP_NORMAL, NX_FRAGMENT_OKAY,
267                                   NX_IP_TIME_TO_LIVE, 2048, NX_NULL, NX_NULL);
268 
269     /* Check status.  */
270     if (status)
271     {
272         error_counter++;
273     }
274 
275     /* Create a secondary TCP socket as the FTP server passive mode connection. Do not bind a port yet  */
276     status = nx_tcp_socket_create(&server_ip, &server_socket_passive, "Passive Socket Server", NX_IP_NORMAL, NX_FRAGMENT_OKAY,
277                                   NX_IP_TIME_TO_LIVE, 2048, NX_NULL, NX_NULL);
278 
279     /* Bind the TCP socket to the FTP control port.  */
280     status =  nx_tcp_server_socket_listen(&server_ip, SERVER_PORT, &server_socket, 5, NX_NULL);
281 
282     /* Check status.  */
283     if (status)
284     {
285         error_counter++;
286     }
287 
288     /* Wait for a connection request.  */
289     status =  nx_tcp_server_socket_accept(&server_socket, 10 * NX_IP_PERIODIC_RATE);
290 
291     /* Check status.  */
292     if (status)
293     {
294         error_counter++;
295     }
296 
297     /* Send welcome response.  */
298     if (nx_ftp_response_packet_send(&server_socket, welcome_220_response_1, welcome_220_response_1_size))
299     {
300         error_counter++;
301     }
302     if (nx_ftp_response_packet_send(&server_socket, welcome_220_response_2, welcome_220_response_2_size))
303     {
304         error_counter++;
305     }
306 
307     /* Receive USER request message.  */
308     status =  nx_tcp_socket_receive(&server_socket, &my_packet, 10 * NX_IP_PERIODIC_RATE);
309 
310     /* Check status.  */
311     if ((status) || (memcmp(my_packet ->nx_packet_prepend_ptr, "USER equenet0_alpha1", sizeof("USER equenet0_alpha1") - 1)))
312     {
313         error_counter++;
314     }
315     else
316     {
317 
318         /* Release the packet.  */
319         nx_packet_release(my_packet);
320     }
321 
322     /* Send password required message.  */
323     if (nx_ftp_response_packet_send(&server_socket, password_request_331, password_request_331_size))
324     {
325         error_counter++;
326     }
327 
328     /* Receive PASS request message.  */
329     status =  nx_tcp_socket_receive(&server_socket, &my_packet, 10 * NX_IP_PERIODIC_RATE);
330 
331     /* Check status.  */
332     if ((status) || (memcmp(my_packet ->nx_packet_prepend_ptr, "PASS 29Pi2A792N", sizeof("USER 29Pi2A792N") - 1)))
333     {
334         error_counter++;
335     }
336     else
337     {
338 
339         /* Release the packet.  */
340         nx_packet_release(my_packet);
341     }
342 
343     /* Send logged in message.  */
344     if (nx_ftp_response_packet_send(&server_socket, logged_in_230_response, logged_in_230_response_size))
345     {
346         error_counter++;
347     }
348 
349     /* Wait for client thread.  */
350     while (client_thread_done == NX_FALSE)
351     {
352         tx_thread_sleep(NX_IP_PERIODIC_RATE);
353     }
354 
355     nx_tcp_socket_disconnect(&server_socket, 0);
356     nx_tcp_socket_delete(&server_socket);
357 
358     /* Check for error.  */
359     if (error_counter)
360     {
361 
362         printf("ERROR!\n");
363         test_control_return(1);
364     }
365     else
366     {
367 
368         printf("SUCCESS!\n");
369         test_control_return(0);
370     };
371 
372     return;
373 
374 }
375 
nx_ftp_response_packet_send(NX_TCP_SOCKET * server_socket,UCHAR * data,UINT data_size)376 static UINT   nx_ftp_response_packet_send(NX_TCP_SOCKET *server_socket, UCHAR *data, UINT data_size)
377 {
378 
379 UINT        status;
380 NX_PACKET   *response_packet;
381 
382 
383     /* Allocate a response packet.  */
384     status =  nx_packet_allocate(&server_pool, &response_packet, NX_TCP_PACKET, TX_WAIT_FOREVER);
385 
386     /* Check status.  */
387     if (status)
388     {
389         error_counter++;
390     }
391 
392     /* Write the FTP response messages into the packet payload!  */
393     memcpy(response_packet -> nx_packet_prepend_ptr, data, data_size);
394 
395     /* Adjust the write pointer.  */
396     response_packet -> nx_packet_length = data_size;
397     response_packet -> nx_packet_append_ptr =  response_packet -> nx_packet_prepend_ptr + response_packet -> nx_packet_length;
398 
399     /* Send the TCP packet with the correct port.  */
400     status =  nx_tcp_socket_send(server_socket, response_packet, 100);
401 
402     /* Check the status.  */
403     if (status)
404         nx_packet_release(response_packet);
405 
406     return status;
407 }
408 #else
409 
410 #ifdef CTEST
test_application_define(void * first_unused_memory)411 VOID test_application_define(void *first_unused_memory)
412 #else
413 void    netx_ftp_client_multiple_connection_response_test_application_define(void *first_unused_memory)
414 #endif
415 {
416 
417     /* Print out test information banner.  */
418     printf("NetX Test:   FTP Client Multiple Connection Response Test..............N/A\n");
419 
420     test_control_return(3);
421 }
422 #endif
423