1 
2 #include    "tx_api.h"
3 #include    "fx_api.h"
4 #include    "nx_api.h"
5 #include    "nxd_ftp_client.h"
6 #include    "nxd_ftp_server.h"
7 #include    "nx_tcp.h"
8 
9 extern void     test_control_return(UINT);
10 
11 #if !defined(NX_DISABLE_IPV4)
12 
13 #define     DEMO_STACK_SIZE         4096
14 
15 /* Define the ThreadX, NetX, and FileX object control blocks...  */
16 static TX_THREAD               server_thread;
17 static TX_THREAD               client_thread;
18 static NX_PACKET_POOL          server_pool;
19 static NX_IP                   server_ip;
20 static NX_PACKET_POOL          client_pool;
21 static NX_IP                   client_ip;
22 static FX_MEDIA                ram_disk;
23 
24 
25 /* Define the NetX FTP object control blocks.  */
26 static NX_FTP_CLIENT           ftp_client;
27 static NX_FTP_SERVER           ftp_server;
28 
29 /* Define the counters used in the demo application...  */
30 static ULONG                   error_counter = 0;
31 static ULONG                   username_counter = 0;
32 static ULONG                   flag = 0;
33 static UINT                    test_done = NX_FALSE;
34 
35 
36 /* Define the memory area for the FileX RAM disk.  */
37 static UCHAR                   ram_disk_memory[32000];
38 static UCHAR                   ram_disk_sector_cache[512];
39 
40 
41 #define FTP_SERVER_ADDRESS  IP_ADDRESS(1,2,3,4)
42 #define FTP_CLIENT_ADDRESS  IP_ADDRESS(1,2,3,5)
43 
44 extern UINT  _fx_media_format(FX_MEDIA *media_ptr, VOID (*driver)(FX_MEDIA *media), VOID *driver_info_ptr, UCHAR *memory_ptr, UINT memory_size,
45                               CHAR *volume_name, UINT number_of_fats, UINT directory_entries, UINT hidden_sectors,
46                               ULONG total_sectors, UINT bytes_per_sector, UINT sectors_per_cluster,
47                               UINT heads, UINT sectors_per_track);
48 
49 /* Define the FileX and NetX driver entry functions.  */
50 extern void     _fx_ram_driver(FX_MEDIA *media_ptr);
51 extern void     _nx_ram_network_driver_512(NX_IP_DRIVER *driver_req_ptr);
52 static void     my_ftp_packet_receive_server(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
53 
54 static void    client_thread_entry(ULONG thread_input);
55 static void    thread_server_entry(ULONG thread_input);
56 
57 
58 /* Define server login/logout functions.  These are stubs for functions that would
59 validate a client login request.   */
60 static UINT    server_login(struct NX_FTP_SERVER_STRUCT *ftp_server_ptr, ULONG client_ip_address, UINT client_port, CHAR *name, CHAR *password, CHAR *extra_info);
61 static UINT    server_logout(struct NX_FTP_SERVER_STRUCT *ftp_server_ptr, ULONG client_ip_address, UINT client_port, CHAR *name, CHAR *password, CHAR *extra_info);
62 
63 #ifdef CTEST
test_application_define(void * first_unused_memory)64 VOID test_application_define(void *first_unused_memory)
65 #else
66 void    netx_ftp_access_control_commands_01_test_application_define(void *first_unused_memory)
67 #endif
68 {
69 
70 UINT    status;
71 UCHAR   *pointer;
72 
73 
74     /* Setup the working pointer.  */
75     pointer =  (UCHAR *) first_unused_memory;
76 
77     /* Create a helper thread for the server. */
78     tx_thread_create(&server_thread, "FTP Server thread", thread_server_entry, 0,
79                      pointer, DEMO_STACK_SIZE,
80                      4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
81 
82     pointer =  pointer + DEMO_STACK_SIZE;
83 
84     /* Initialize NetX.  */
85     nx_system_initialize();
86 
87     /* Create the packet pool for the FTP Server.  */
88     status = nx_packet_pool_create(&server_pool, "NetX Server Packet Pool", 512, pointer, 8192);
89     pointer = pointer + 8192;
90     if (status)
91         error_counter++;
92 
93     /* Create the IP instance for the FTP Server.  */
94     status = nx_ip_create(&server_ip, "NetX Server IP Instance", FTP_SERVER_ADDRESS, 0xFFFFFF00UL,
95                           &server_pool, _nx_ram_network_driver_512, pointer, 2048, 1);
96     pointer = pointer + 2048;
97     if (status)
98         error_counter++;
99 
100     /* Enable ARP and supply ARP cache memory for server IP instance.  */
101     status = nx_arp_enable(&server_ip, (void *) pointer, 1024);
102     pointer = pointer + 1024;
103     if (status)
104         error_counter++;
105 
106     /* Enable TCP.  */
107     status = nx_tcp_enable(&server_ip);
108     if (status)
109         error_counter++;
110 
111     /* Create the FTP server.  */
112     status =  nx_ftp_server_create(&ftp_server, "FTP Server Instance", &server_ip, &ram_disk, pointer, DEMO_STACK_SIZE, &server_pool,
113                                    server_login, server_logout);
114     pointer =  pointer + DEMO_STACK_SIZE;
115     if (status)
116         error_counter++;
117 
118     /* Now set up the FTP Client. */
119 
120     /* Create the main FTP client thread.  */
121     status = tx_thread_create(&client_thread, "FTP Client thread ", client_thread_entry, 0,
122                               pointer, DEMO_STACK_SIZE,
123                               6, 6, TX_NO_TIME_SLICE, TX_AUTO_START);
124     pointer = pointer + DEMO_STACK_SIZE ;
125     if (status)
126         error_counter++;
127 
128     /* Create a packet pool for the FTP client.  */
129     status =  nx_packet_pool_create(&client_pool, "NetX Client Packet Pool", 256, pointer, 8192);
130     pointer =  pointer + 8192;
131     if (status)
132         error_counter++;
133 
134     /* Create an IP instance for the FTP client.  */
135     status = nx_ip_create(&client_ip, "NetX Client IP Instance", FTP_CLIENT_ADDRESS, 0xFFFFFF00UL,
136                           &client_pool, _nx_ram_network_driver_512, pointer, 2048, 1);
137     pointer = pointer + 2048;
138     if (status)
139         error_counter++;
140 
141     /* Enable ARP and supply ARP cache memory for the FTP Client IP.  */
142     status = nx_arp_enable(&client_ip, (void *) pointer, 1024);
143     pointer = pointer + 1024;
144     if (status)
145         error_counter++;
146 
147     /* Enable TCP for client IP instance.  */
148     status = nx_tcp_enable(&client_ip);
149     if (status)
150         error_counter++;
151 
152 }
153 
154 /* Define the FTP client thread.  */
155 
client_thread_entry(ULONG thread_input)156 void    client_thread_entry(ULONG thread_input)
157 {
158 
159 
160 UINT        status;
161 
162     /* Format the RAM disk - the memory for the RAM disk was defined above.  */
163     status = _fx_media_format(&ram_disk,
164                             _fx_ram_driver,                  /* Driver entry                */
165                             ram_disk_memory,                 /* RAM disk memory pointer     */
166                             ram_disk_sector_cache,           /* Media buffer pointer        */
167                             sizeof(ram_disk_sector_cache),   /* Media buffer size           */
168                             "MY_RAM_DISK",                   /* Volume Name                 */
169                             1,                               /* Number of FATs              */
170                             32,                              /* Directory Entries           */
171                             0,                               /* Hidden sectors              */
172                             256,                             /* Total sectors               */
173                             128,                             /* Sector size                 */
174                             1,                               /* Sectors per cluster         */
175                             1,                               /* Heads                       */
176                             1);                              /* Sectors per track           */
177 
178     /* Check status.  */
179     if (status)
180         error_counter++;
181 
182     /* Open the RAM disk.  */
183     status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, ram_disk_sector_cache, sizeof(ram_disk_sector_cache));
184     if (status)
185         error_counter++;
186 
187     /* Create an FTP client.  */
188     status =  nx_ftp_client_create(&ftp_client, "FTP Client", &client_ip, 2000, &client_pool);
189     if (status)
190         error_counter++;
191 
192     flag = 1;
193 
194     /* Now connect with the NetX FTP (IPv4) server. */
195     status =  nx_ftp_client_connect(&ftp_client, FTP_SERVER_ADDRESS, "name", "password", NX_IP_PERIODIC_RATE);
196     if (status)
197         error_counter++;
198 
199     /* Disconnect from the server.  */
200     status =  nx_ftp_client_disconnect(&ftp_client, NX_IP_PERIODIC_RATE);
201     if (status)
202         error_counter++;
203 
204     /* Delete the FTP client.  */
205     status =  nx_ftp_client_delete(&ftp_client);
206     if (status)
207         error_counter++;
208 
209     /* Set the flag.  */
210     test_done = NX_TRUE;
211 }
212 
213 
214 /* Define the helper FTP server thread.  */
thread_server_entry(ULONG thread_input)215 void    thread_server_entry(ULONG thread_input)
216 {
217 
218 UINT    status;
219 
220     /* Print out test information banner.  */
221     printf("NetX Test:   FTP Server Access Control Commands User 01 Test...........");
222 
223     /* Check for earlier error. */
224     if(error_counter)
225     {
226         printf("ERROR!\n");
227         test_control_return(1);
228     }
229 
230     /* OK to start the FTPv6 Server.   */
231     status = nx_ftp_server_start(&ftp_server);
232     if (status)
233         error_counter++;
234 
235     server_ip.nx_ip_tcp_packet_receive = my_ftp_packet_receive_server;
236 
237     /* Wait for test.  */
238     while(test_done == NX_FALSE)
239         tx_thread_sleep(NX_IP_PERIODIC_RATE);
240 
241     status = nx_ftp_server_delete(&ftp_server);
242     if(status)
243         error_counter++;
244 
245     if((error_counter) && ( username_counter != 1 ))
246     {
247         printf("ERROR!\n");
248         test_control_return(1);
249     }
250     else
251     {
252         printf("SUCCESS!\n");
253         test_control_return(0);
254     }
255 }
256 
my_ftp_packet_receive_server(NX_IP * ip_ptr,NX_PACKET * packet_ptr)257 void           my_ftp_packet_receive_server(NX_IP *ip_ptr, NX_PACKET *packet_ptr)
258 {
259 NX_TCP_HEADER    *tcp_header_ptr;
260 UCHAR            *message;
261 
262     tcp_header_ptr = (NX_TCP_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
263     NX_CHANGE_ULONG_ENDIAN(tcp_header_ptr -> nx_tcp_header_word_3);
264 
265     message = packet_ptr -> nx_packet_prepend_ptr + 20;
266 
267     /* Check the packet is a SYN one.  */
268     if((tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT) && (flag != 1))
269     {
270         if(!memcmp(message,"USER name",9))
271             username_counter++;
272 
273         /* Deal packets with default routing.  */
274         server_ip.nx_ip_tcp_packet_receive = _nx_tcp_packet_receive;
275     }
276 
277     NX_CHANGE_ULONG_ENDIAN(tcp_header_ptr -> nx_tcp_header_word_3);
278 
279     /* When ftp client connect the ftp server, increase the flage to make sure the "User" packet is the first packet we get. */
280     if(flag)
281         flag++;
282 
283     /* Let server receives the SYN packet.  */
284     _nx_tcp_packet_receive(ip_ptr, packet_ptr);
285 }
286 
server_login(struct NX_FTP_SERVER_STRUCT * ftp_server_ptr,ULONG client_ip_address,UINT client_port,CHAR * name,CHAR * password,CHAR * extra_info)287 static UINT  server_login(struct NX_FTP_SERVER_STRUCT *ftp_server_ptr, ULONG client_ip_address, UINT client_port, CHAR *name, CHAR *password, CHAR *extra_info)
288 {
289     /* Always return success.  */
290     return(NX_SUCCESS);
291 }
292 
server_logout(struct NX_FTP_SERVER_STRUCT * ftp_server_ptr,ULONG client_ip_address,UINT client_port,CHAR * name,CHAR * password,CHAR * extra_info)293 static UINT  server_logout(struct NX_FTP_SERVER_STRUCT *ftp_server_ptr, ULONG client_ip_address, UINT client_port, CHAR *name, CHAR *password, CHAR *extra_info)
294 {
295     /* Always return success.  */
296     return(NX_SUCCESS);
297 }
298 #else
299 
300 #ifdef CTEST
test_application_define(void * first_unused_memory)301 VOID test_application_define(void *first_unused_memory)
302 #else
303 void    netx_ftp_access_control_commands_01_test_application_define(void *first_unused_memory)
304 #endif
305 {
306 
307     /* Print out test information banner.  */
308     printf("NetX Test:   FTP Server Access Control Commands User 01 Test...........N/A\n");
309 
310     test_control_return(3);
311 }
312 #endif