1 /* This demo tests the abnormal IPCP packet.
2
3 */
4
5 #include "tx_api.h"
6 #include "nx_api.h"
7 #include "nx_ppp.h"
8
9 extern void test_control_return(UINT status);
10
11 #if !defined(NX_DISABLE_IPV4)
12
13 /* Define demo stack size. */
14
15 #define DEMO_STACK_SIZE 2048
16
17
18 /* Define the ThreadX and NetX object control blocks... */
19
20 static TX_THREAD thread_0;
21 static TX_THREAD thread_check;
22 static NX_PACKET_POOL pool_0;
23 static NX_IP ip_0;
24 static NX_PPP ppp_0;
25 static NX_PPP ppp_1;
26 static UINT checkpoint;
27
28
29 /* Define the counters used in the demo application... */
30
31 static ULONG ppp_0_link_up_counter;
32 static ULONG ppp_0_link_down_counter;
33 static ULONG error_counter = 0;
34
35 /* Define thread prototypes. */
36 static void thread_0_entry(ULONG thread_input);
37 static void thread_check_entry(ULONG thread_input);
38 static void link_up_callback(NX_PPP *ppp_ptr);
39 static void link_down_callback(NX_PPP *ppp_ptr);
40 static void ppp_0_serial_byte_output(UCHAR byte);
41 static void invalid_packet_handler(NX_PACKET *packet_ptr);
42
43
44 /* Define what the initial system looks like. */
45 #ifdef CTEST
test_application_define(void * first_unused_memory)46 VOID test_application_define(void *first_unused_memory)
47 #else
48 void netx_ppp_IPCP_abnormal_packet_test_application_define(void *first_unused_memory)
49 #endif
50 {
51
52 CHAR *pointer;
53 UINT status;
54
55
56 /* Setup the working pointer. */
57 pointer = (CHAR *) first_unused_memory;
58
59 /* Create the thread 0. */
60 tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
61 pointer, DEMO_STACK_SIZE,
62 5, 5, TX_NO_TIME_SLICE, TX_AUTO_START);
63 pointer = pointer + DEMO_STACK_SIZE;
64
65 /* Create the check thread. */
66 tx_thread_create(&thread_check, "thread check", thread_check_entry, 0,
67 pointer, DEMO_STACK_SIZE,
68 4, 4, TX_NO_TIME_SLICE, TX_DONT_START);
69 pointer = pointer + DEMO_STACK_SIZE;
70
71 /* Initialize the NetX system. */
72 nx_system_initialize();
73
74 /* Create a packet pool. */
75 status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool", NX_PPP_MIN_PACKET_PAYLOAD, pointer, 2048);
76 pointer = pointer + 2048;
77
78 /* Check for pool creation error. */
79 if (status)
80 error_counter++;
81
82 /* Create an IP instance. */
83 status = nx_ip_create(&ip_0, "NetX IP Instance 0", IP_ADDRESS(0, 0, 0, 0), 0xFFFFFF00UL,
84 &pool_0, nx_ppp_driver, pointer, 2048, 1);
85 pointer = pointer + 2048;
86
87 /* Check for pool creation error. */
88 if (status)
89 error_counter++;
90
91 /* Create the PPP instance. */
92 status = nx_ppp_create(&ppp_0, "PPP0", &ip_0, pointer, 2048, 1, &pool_0, invalid_packet_handler, ppp_0_serial_byte_output);
93 pointer = pointer + 2048;
94
95 /* Check for PPP create error. */
96 if (status)
97 error_counter++;
98
99 /* Define IP address. This PPP instance is effectively the server since it has both IP addresses. */
100 status = nx_ppp_ip_address_assign(&ppp_0, IP_ADDRESS(1, 2, 3, 4), IP_ADDRESS(1, 2, 3, 5));
101
102 /* Check for PPP IP address assign error. */
103 if (status)
104 error_counter++;
105
106 /* Register the link up/down callbacks. */
107 status = nx_ppp_link_up_notify(&ppp_0, link_up_callback);
108 status += nx_ppp_link_down_notify(&ppp_0, link_down_callback);
109
110 /* Check for PPP link up/down callback registration error(s). */
111 if (status)
112 error_counter++;
113
114 /* Enable UDP traffic. */
115 nx_udp_enable(&ip_0);
116 }
117
118 static char ipcp_data_0[] = {
119 0x80, 0x21, 0x01, 0x07, 0x00, 0x16,
120 0x81, 0x06, 0x00, 0x00, 0x00, 0x00,
121 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
122 0x83, 0x06, 0x00, 0x00, 0x00, 0x00
123 };
124
125 static char ipcp_data_1[] = {
126 0x80, 0x21, 0x01, 0x07, 0x00, 0x4C,
127 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
128 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
129 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
130 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
131 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
132 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
133 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
134 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
135 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
136 0x03, 0x06, 0x00, 0x00, 0x00, 0x00,
137 0x81, 0x06, 0x00, 0x00, 0x00, 0x00,
138 0x83, 0x06, 0x00, 0x00, 0x00, 0x00
139 };
140
141 extern void _nx_ppp_receive_packet_process(NX_PPP *ppp_ptr, NX_PACKET *packet_ptr);
142
143 /* Define the test threads. */
144
thread_0_entry(ULONG thread_input)145 static void thread_0_entry(ULONG thread_input)
146 {
147
148 UINT status;
149 NX_PACKET *packet_ptr;
150 UINT i = 0;
151 UCHAR *data_ptr;
152 UINT data_size;
153
154
155 /* Print out test information banner. */
156 printf("NetX Test: PPP IPCP Abnormal Packet Test.............................");
157
158 if (error_counter)
159 {
160 printf("ERROR\n");
161 test_control_return(1);
162 }
163
164 checkpoint = NX_FALSE;
165 tx_thread_resume(&thread_check);
166
167 for (i = 0; i < 2; i++)
168 {
169
170 /* Allocate a packet. */
171 status = nx_packet_allocate(&pool_0, &packet_ptr, 0, TX_WAIT_FOREVER);
172
173 /* Check status. */
174 if (status != NX_SUCCESS)
175 error_counter++;
176
177 if (i == 0)
178 {
179
180 /* NX_PPP_DNS_SERVER_OPTION before NX_PPP_IP_ADDRESS_OPTION. */
181 data_ptr = ipcp_data_0;
182 data_size = sizeof(ipcp_data_0);
183 }
184 else
185 {
186
187 /* Option length exceed the NX_PPP_OPTION_MESSAGE_LENGTH. */
188 data_ptr = ipcp_data_1;
189 data_size = sizeof(ipcp_data_1);
190 }
191
192 /* Write IPCP data into the packet payload. */
193 nx_packet_data_append(packet_ptr, data_ptr, data_size, &pool_0, TX_WAIT_FOREVER);
194
195 ppp_0.nx_ppp_ipcp_state = NX_PPP_IPCP_CONFIGURE_REQUEST_ACKED_STATE;
196
197 /* Call PPP packet process function. */
198 _nx_ppp_receive_packet_process(&ppp_0, packet_ptr);
199
200 if ((i == 0) && (ppp_0.nx_ppp_peer_naked_list[0] != 18))
201 {
202 error_counter++;
203 }
204 }
205
206 checkpoint = NX_TRUE;
207 nx_ppp_delete(&ppp_0);
208 }
209
210
thread_check_entry(ULONG thread_input)211 static void thread_check_entry(ULONG thread_input)
212 {
213 tx_thread_sleep(5 * NX_IP_PERIODIC_RATE);
214
215 /* Check status. */
216 if ((checkpoint != NX_TRUE) || error_counter)
217 {
218 printf("ERROR!\n");
219 test_control_return(1);
220 }
221
222 printf("SUCCESS!\n");
223 test_control_return(0);
224 }
225
226 /* Define serial output routines. Normally these routines would
227 map to physical UART routines and the nx_ppp_byte_receive call
228 would be made from a UART receive interrupt. */
229
ppp_0_serial_byte_output(UCHAR byte)230 static void ppp_0_serial_byte_output(UCHAR byte)
231 {
232
233 /* Just feed the PPP 1 input routine. */
234 nx_ppp_byte_receive(&ppp_1, byte);
235 }
236
237
invalid_packet_handler(NX_PACKET * packet_ptr)238 static void invalid_packet_handler(NX_PACKET *packet_ptr)
239 {
240
241 error_counter++;
242 nx_packet_release(packet_ptr);
243 }
244
245
link_up_callback(NX_PPP * ppp_ptr)246 static void link_up_callback(NX_PPP *ppp_ptr)
247 {
248
249 /* Just increment the link up counter. */
250 ppp_0_link_up_counter++;
251 }
252
253
link_down_callback(NX_PPP * ppp_ptr)254 static void link_down_callback(NX_PPP *ppp_ptr)
255 {
256
257 /* Just increment the link down counter. */
258 ppp_0_link_down_counter++;
259
260 return;
261 }
262 #else
263
264 #ifdef CTEST
test_application_define(void * first_unused_memory)265 VOID test_application_define(void *first_unused_memory)
266 #else
267 void netx_ppp_IPCP_abnormal_packet_test_application_define(void *first_unused_memory)
268 #endif
269 {
270
271 /* Print out test information banner. */
272 printf("NetX Test: PPP IPCP Abnormal Packet Test........................................N/A\n");
273
274 test_control_return(3);
275 }
276 #endif
277
278
279
280