1 /* This case tests the APIs of PPPoE server and client.  */
2 
3 #include   "nx_pppoe_server.h"
4 #include   "nx_pppoe_client.h"
5 #include   "tx_api.h"
6 #include   "nx_api.h"
7 #include   "nx_ppp.h"
8 
9 extern void test_control_return(UINT);
10 
11 #if !defined(NX_DISABLE_IPV4) && defined(NX_PPP_PPPOE_ENABLE) && defined(NX_PPPOE_SERVER_INITIALIZE_DRIVER_ENABLE) && defined(NX_PPPOE_CLIENT_INITIALIZE_DRIVER_ENABLE) && (NX_PHYSICAL_HEADER >= 24) && !defined(NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE)
12 
13 /* Define the block size.  */
14 #define     NX_PACKET_POOL_SIZE     ((1536 + sizeof(NX_PACKET)) * 12)
15 #define     DEMO_STACK_SIZE         2048
16 #define     PPPOE_THREAD_SIZE       2048
17 
18 /* Define the ThreadX and NetX object control blocks...  */
19 static TX_THREAD               thread_server;
20 static TX_THREAD               thread_client;
21 
22 /* Define the packet pool and IP instance for normal IP instnace.  */
23 static NX_PACKET_POOL          pool_server;
24 static NX_IP                   ip_server;
25 static NX_PACKET_POOL          pool_client;
26 static NX_IP                   ip_client;
27 static CHAR                    pool_buffer_server[NX_PACKET_POOL_SIZE];
28 static CHAR                    pool_buffer_client[NX_PACKET_POOL_SIZE];
29 
30 /* Define the PPP Server instance.  */
31 static NX_PPP                  ppp_server;
32 static NX_PPP                  ppp_client;
33 
34 /* Define the PPPoE Server instance.  */
35 static NX_PPPOE_SERVER         pppoe_server;
36 static NX_PPPOE_CLIENT         pppoe_client;
37 
38 /* Define the counters.  */
39 static CHAR                    *pointer;
40 static ULONG                   error_counter;
41 
42 /* Define thread prototypes.  */
43 static void    thread_server_entry(ULONG thread_input);
44 static void    thread_client_entry(ULONG thread_input);
45 
46 /***** Substitute your PPP driver entry function here *********/
47 extern void    _nx_ppp_driver(NX_IP_DRIVER *driver_req_ptr);
48 
49 /***** Substitute your Ethernet driver entry function here *********/
50 extern void    _nx_ram_network_driver(NX_IP_DRIVER *driver_req_ptr);
51 
52 /* Define the callback functions.  */
53 static void    pppoe_server_packet_receive(UINT interfaceHandle, ULONG length, UCHAR *data, UINT packet_id);
54 
55 /* Define the porting layer function for PPP.
56    Functions to be provided by PPP for calling by the PPPoE Stack.  */
57 static void    ppp_server_packet_send(NX_PACKET *packet_ptr);
58 
59 /* Define the porting layer function for PPP.
60    Functions to be provided by PPP for calling by the PPPoE Stack.  */
61 static void    ppp_client_packet_send(NX_PACKET *packet_ptr);
62 static void    pppoe_client_packet_receive(NX_PACKET *packet_ptr);
63 
64 #define SERVER_ADDRESS IP_ADDRESS(192, 168, 10, 43)
65 #define CLIENT_ADDRESS IP_ADDRESS(192, 168, 10, 44)
66 
generate_login(CHAR * name,CHAR * password)67 static UINT generate_login(CHAR *name, CHAR *password)
68 {
69 
70     /* Make a name and password, called "myname" and "mypassword".  */
71     name[0] = 'm';
72     name[1] = 'y';
73     name[2] = 'n';
74     name[3] = 'a';
75     name[4] = 'm';
76     name[5] = 'e';
77     name[6] = (CHAR) 0;
78 
79     password[0] = 'm';
80     password[1] = 'y';
81     password[2] = 'p';
82     password[3] = 'a';
83     password[4] = 's';
84     password[5] = 's';
85     password[6] = 'w';
86     password[7] = 'o';
87     password[8] = 'r';
88     password[9] = 'd';
89     password[10] = (CHAR) 0;
90 
91     return(NX_SUCCESS);
92 }
93 
verify_login(CHAR * name,CHAR * password)94 static UINT verify_login(CHAR *name, CHAR *password)
95 {
96 
97 if ((name[0] == 'm') &&
98     (name[1] == 'y') &&
99     (name[2] == 'n') &&
100     (name[3] == 'a') &&
101     (name[4] == 'm') &&
102     (name[5] == 'e') &&
103     (name[6] == (CHAR) 0) &&
104     (password[0] == 'm') &&
105     (password[1] == 'y') &&
106     (password[2] == 'p') &&
107     (password[3] == 'a') &&
108     (password[4] == 's') &&
109     (password[5] == 's') &&
110     (password[6] == 'w') &&
111     (password[7] == 'o') &&
112     (password[8] == 'r') &&
113     (password[9] == 'd') &&
114     (password[10] == (CHAR) 0))
115         return(NX_SUCCESS);
116    else
117         return(NX_PPP_ERROR);
118 }
119 
120 /* Define what the initial system looks like.  */
121 
122 #ifdef CTEST
test_application_define(void * first_unused_memory)123 VOID test_application_define(void *first_unused_memory)
124 #else
125 void    netx_pppoe_api_test_application_define(void *first_unused_memory)
126 #endif
127 {
128 
129 UINT    status;
130 
131     /* Setup the working pointer.  */
132     pointer =  (CHAR *) first_unused_memory;
133 
134     /* Initialize the NetX system.  */
135     nx_system_initialize();
136 
137     /* Create a packet pool for normal IP instance.  */
138     status = nx_packet_pool_create(&pool_server, "Server Packet Pool",
139                                    (1536 + sizeof(NX_PACKET)),
140                                    pool_buffer_server, NX_PACKET_POOL_SIZE);
141 
142     /* Check for error.  */
143     if (status)
144         error_counter++;
145 
146     /* Create an normal IP instance.  */
147     status = nx_ip_create(&ip_server, "Server IP Instance", IP_ADDRESS(0, 0, 0, 0), 0xFFFFFF00UL, &pool_server, nx_ppp_driver,
148                           pointer, 2048, 1);
149     pointer = pointer + 2048;
150 
151     /* Check for error.  */
152     if (status)
153         error_counter++;
154 
155     /* Create the PPP instance.  */
156     status = nx_ppp_create(&ppp_server, "PPP Server Instance", &ip_server, pointer, 2048, 1, &pool_server, NX_NULL, NX_NULL);
157     pointer = pointer + 2048;
158 
159     /* Check for PPP create error.   */
160     if (status)
161         error_counter++;
162 
163     /* Set the PPP packet send function.  */
164     status = nx_ppp_packet_send_set(&ppp_server, ppp_server_packet_send);
165 
166     /* Check for PPP packet send function set error.   */
167     if (status)
168         error_counter++;
169 
170     /* Define IP address. This PPP instance is effectively the server since it has both IP addresses. */
171     status = nx_ppp_ip_address_assign(&ppp_server, SERVER_ADDRESS, CLIENT_ADDRESS);
172 
173     /* Check for PPP IP address assign error.   */
174     if (status)
175         error_counter++;
176 
177     /* Setup PAP, this PPP instance is effectively the server since it will verify the name and password.  */
178     status = nx_ppp_pap_enable(&ppp_server, NX_NULL, verify_login);
179 
180     /* Check for PPP PAP enable error.  */
181     if (status)
182         error_counter++;
183 
184     /* Enable ARP and supply ARP cache memory for Normal IP Instance.  */
185     status = nx_arp_enable(&ip_server, (void *) pointer, 1024);
186     pointer = pointer + 1024;
187 
188     /* Check for ARP enable errors.  */
189     if (status)
190         error_counter++;
191 
192     /* Enable ICMP */
193     status = nx_icmp_enable(&ip_server);
194     if(status)
195         error_counter++;
196 
197     /* Create the main thread.  */
198     tx_thread_create(&thread_server, "thread server", thread_server_entry, 0,
199                      pointer, DEMO_STACK_SIZE,
200                      5, 5, TX_NO_TIME_SLICE, TX_AUTO_START);
201     pointer =  pointer + DEMO_STACK_SIZE;
202 
203     /* Create a packet pool for normal IP instance.  */
204     status = nx_packet_pool_create(&pool_client, "Client Packet Pool",
205                                    (1536 + sizeof(NX_PACKET)),
206                                    pool_buffer_client, NX_PACKET_POOL_SIZE);
207 
208     /* Check for error.  */
209     if (status)
210         error_counter++;
211 
212     /* Create an normal IP instance.  */
213     status = nx_ip_create(&ip_client, "Client IP Instance", IP_ADDRESS(0, 0, 0, 0), 0xFFFFFF00UL, &pool_client, nx_ppp_driver,
214                           pointer, 2048, 1);
215     pointer = pointer + 2048;
216 
217     /* Check for error.  */
218     if (status)
219         error_counter++;
220 
221     /* Create the PPP instance.  */
222     status = nx_ppp_create(&ppp_client, "PPP Client Instance", &ip_client, pointer, 2048, 1, &pool_client, NX_NULL, NX_NULL);
223     pointer = pointer + 2048;
224 
225     /* Check for PPP create error.   */
226     if (status)
227         error_counter++;
228 
229     /* Set the PPP packet send function.  */
230     status = nx_ppp_packet_send_set(&ppp_client, ppp_client_packet_send);
231 
232     /* Check for PPP packet send function set error.   */
233     if (status)
234         error_counter++;
235 
236     /* Define IP address. This PPP instance is effectively the client since it doesn't have any IP addresses. */
237     status = nx_ppp_ip_address_assign(&ppp_client, IP_ADDRESS(0, 0, 0, 0), IP_ADDRESS(0, 0, 0, 0));
238 
239     /* Check for PPP IP address assign error.   */
240     if (status)
241         error_counter++;
242 
243     /* Setup PAP, this PPP instance is effectively the since it generates the name and password for the peer..  */
244     status = nx_ppp_pap_enable(&ppp_client, generate_login, NX_NULL);
245 
246     /* Check for PPP PAP enable error.  */
247     if (status)
248         error_counter++;
249 
250     /* Enable ARP and supply ARP cache memory for Normal IP Instance.  */
251     status = nx_arp_enable(&ip_client, (void *) pointer, 1024);
252     pointer = pointer + 1024;
253 
254     /* Check for ARP enable errors.  */
255     if (status)
256         error_counter++;
257 
258     /* Enable ICMP */
259     status = nx_icmp_enable(&ip_client);
260     if(status)
261         error_counter++;
262 
263     /* Create the main thread.  */
264     tx_thread_create(&thread_client, "thread client", thread_client_entry, 0,
265                      pointer, DEMO_STACK_SIZE,
266                      6, 6, TX_NO_TIME_SLICE, TX_DONT_START);
267     pointer =  pointer + DEMO_STACK_SIZE;
268 
269 }
270 
thread_client_entry(ULONG thread_input)271 static void    thread_client_entry(ULONG thread_input)
272 {
273 UINT    status;
274 ULONG   ip_status;
275 NX_PACKET *recv_pkt;
276 ULONG   ip_address;
277 ULONG   network_mask;
278 
279     /* Create the PPPoE instance.  */
280     status = nx_pppoe_client_create(&pppoe_client, (UCHAR *)"PPPoE Client", &ip_client, 0, &pool_client, pointer, PPPOE_THREAD_SIZE, 4, _nx_ram_network_driver, pppoe_client_packet_receive);
281     pointer = pointer + PPPOE_THREAD_SIZE;
282     if (status)
283     {
284         error_counter++;
285     }
286 
287     /* Set host unique.  */
288     status = nx_pppoe_client_host_uniq_set(&pppoe_client, "220000000000000041000000");
289     if (status)
290     {
291         error_counter++;
292     }
293 
294     /* Set service name.  */
295     status = nx_pppoe_client_service_name_set(&pppoe_client, "BRAS");
296     if (status)
297     {
298         error_counter++;
299     }
300 
301     /* Establish PPPoE Client sessione.  */
302     status = nx_pppoe_client_session_connect(&pppoe_client, NX_WAIT_FOREVER);
303     if (status)
304     {
305         error_counter++;
306     }
307 
308     /* Wait for the link to come up.  */
309     status = nx_ip_interface_status_check(&ip_client, 0, NX_IP_ADDRESS_RESOLVED, &ip_status, NX_WAIT_FOREVER);
310     if (status)
311     {
312         error_counter++;
313     }
314 
315     /* Check client IP address.  */
316     status = nx_ip_address_get(&ip_client, &ip_address, &network_mask);
317     if (status || ip_address != CLIENT_ADDRESS)
318     {
319         error_counter++;
320     }
321 
322     /* Ping test.  */
323     status = nx_icmp_ping(&ip_client, SERVER_ADDRESS, "abcd", 4, &recv_pkt, NX_WAIT_FOREVER);
324     if (status)
325     {
326         error_counter++;
327     }
328     else
329     {
330         nx_packet_release(recv_pkt);
331     }
332 
333     if(error_counter)
334     {
335         printf("ERROR!\n");
336         test_control_return(1);
337     }
338     else
339     {
340         printf("SUCCESS!\n");
341         test_control_return(0);
342     }
343 }
344 
345 /* PPPoE Client receive function.  */
pppoe_client_packet_receive(NX_PACKET * packet_ptr)346 static void    pppoe_client_packet_receive(NX_PACKET *packet_ptr)
347 {
348 
349     /* Call PPP Client to receive the PPP data fame.  */
350     nx_ppp_packet_receive(&ppp_client, packet_ptr);
351 }
352 
353 /* PPP Client send function.  */
ppp_client_packet_send(NX_PACKET * packet_ptr)354 static void    ppp_client_packet_send(NX_PACKET *packet_ptr)
355 {
356 
357     /* Directly Call PPPoE send function to send out the data through PPPoE module.  */
358     nx_pppoe_client_session_packet_send(&pppoe_client, packet_ptr);
359 }
360 
361 /* Define the server threads.  */
362 
thread_server_entry(ULONG thread_input)363 static void    thread_server_entry(ULONG thread_input)
364 {
365 UINT    status;
366 ULONG   ip_status;
367 UCHAR   *nx_pppoe_service_name[] =
368 {
369     "BRAS",
370     "PRINTER",
371     NX_NULL
372 };
373 ULONG   ip_address;
374 ULONG   network_mask;
375 
376 
377     /* Print out test information banner.  */
378     printf("NetX Test:   PPPoE API Test............................................");
379 
380     /* Create the PPPoE instance.  */
381     status =  nx_pppoe_server_create(&pppoe_server, (UCHAR *)"PPPoE Server",  &ip_server, 0, _nx_ram_network_driver,  &pool_server, pointer, PPPOE_THREAD_SIZE, 4);
382     pointer = pointer + PPPOE_THREAD_SIZE;
383     if (status)
384     {
385         error_counter++;
386     }
387 
388     /* Set the callback notify function.  */
389     status = nx_pppoe_server_callback_notify_set(&pppoe_server, NX_NULL, NX_NULL, NX_NULL, NX_NULL, pppoe_server_packet_receive, NX_NULL);
390     if (status)
391     {
392         error_counter++;
393     }
394 
395     /* Set service name.  */
396     status = nx_pppoe_server_service_name_set(&pppoe_server, nx_pppoe_service_name, 2);
397     if (status)
398     {
399         error_counter++;
400     }
401 
402     /* Enable PPPoE Server.  */
403     status = nx_pppoe_server_enable(&pppoe_server);
404     if (status)
405     {
406         error_counter++;
407     }
408 
409     tx_thread_resume(&thread_client);
410 
411     /* Wait for the link to come up.  */
412     status = nx_ip_interface_status_check(&ip_server, 0, NX_IP_ADDRESS_RESOLVED, &ip_status, NX_WAIT_FOREVER);
413     if (status)
414     {
415         error_counter++;
416     }
417 
418     /* Check server IP address.  */
419     status = nx_ip_address_get(&ip_server, &ip_address, &network_mask);
420     if (status || ip_address != SERVER_ADDRESS)
421     {
422         error_counter++;
423     }
424 }
425 
pppoe_server_packet_receive(UINT interfaceHandle,ULONG length,UCHAR * data,UINT packet_id)426 static void    pppoe_server_packet_receive(UINT interfaceHandle, ULONG length, UCHAR *data, UINT packet_id)
427 {
428 
429 NX_PACKET   *packet_ptr;
430 
431     /* Get the notify that receive the PPPoE Session data.  */
432 
433     /* Call PPP Server to receive the PPP data fame.  */
434     packet_ptr = (NX_PACKET *)(packet_id);
435     nx_ppp_packet_receive(&ppp_server, packet_ptr);
436 }
437 
438 /* PPP Server send function.  */
ppp_server_packet_send(NX_PACKET * packet_ptr)439 static void    ppp_server_packet_send(NX_PACKET *packet_ptr)
440 {
441 
442 /* For PPP test, the session should be the first session, so set interfaceHandle as 0.  */
443 UINT    interfaceHandle = 0;
444 
445     /* Directly Call PPPoE send function to send out the data through PPPoE module.  */
446     nx_pppoe_server_session_packet_send(&pppoe_server, interfaceHandle, packet_ptr);
447 }
448 
449 #else
450 
451 #ifdef CTEST
test_application_define(void * first_unused_memory)452 VOID test_application_define(void *first_unused_memory)
453 #else
454 void    netx_pppoe_api_test_application_define(void *first_unused_memory)
455 #endif
456 {
457 
458     /* Print out test information banner.  */
459     printf("NetX Test:   PPPoE API Test............................................N/A\n");
460 
461     test_control_return(3);
462 }
463 #endif