forked from eclipse-threadx/netxduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_netx_pppoe_server.c
397 lines (307 loc) · 13.8 KB
/
demo_netx_pppoe_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**************************************************************************/
/**************************************************************************/
/** */
/** NetX PPPoE Server stack Component */
/** */
/** This is a small demo of the high-performance NetX PPPoE Server */
/** stack. This demo includes IP instance, PPPoE Server and PPP Server */
/** stack. Create one IP instance includes two interfaces to support */
/** for normal IP stack and PPPoE Server, PPPoE Server can use the */
/** mutex of IP instance to send PPPoE message when share one Ethernet */
/** driver. PPPoE Server work with normal IP instance at the same time. */
/** */
/** Note1: Substitute your Ethernet driver instead of */
/** _nx_ram_network_driver before run this demo */
/** */
/** Note2: Prerequisite for using PPPoE. */
/** Redefine NX_PHYSICAL_HEADER to 24 to ensure enough space for filling*/
/** in physical header. Physical header:14(Ethernet header) */
/** + 6(PPPoE header) + 2(PPP header) + 2(four-byte aligment) */
/** */
/**************************************************************************/
/**************************************************************************/
/*****************************************************************/
/* NetX Stack */
/*****************************************************************/
/***************************/
/* PPP Server */
/***************************/
/***************************/
/* PPPoE Server */
/***************************/
/***************************/ /***************************/
/* Normal Ethernet Type */ /* PPPoE Ethernet Type */
/***************************/ /***************************/
/***************************/ /***************************/
/* Interface 0 */ /* Interface 1 */
/***************************/ /***************************/
/*****************************************************************/
/* Ethernet Dirver */
/*****************************************************************/
#include "tx_api.h"
#include "nx_api.h"
#include "nx_ppp.h"
#include "nx_pppoe_server.h"
#ifndef NX_DISABLE_IPV4
/* Defined NX_PPP_PPPOE_ENABLE if using PPP, since PPP module has been modified to match PPPoE module under this definition. */
#ifdef NX_PPP_PPPOE_ENABLE
/* If the driver is not initialized in other module, define NX_PPPOE_SERVER_INITIALIZE_DRIVER_ENABLE to initialize the driver in PPPoE module .
In this demo, the driver has been initialized in IP module. */
#ifndef NX_PPPOE_SERVER_INITIALIZE_DRIVER_ENABLE
/* NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE:
If defined, enables the feature that controls the PPPoE session.
PPPoE server does not automatically response to the request until application call specific API. */
/* Define the block size. */
#define NX_PACKET_POOL_SIZE ((1536 + sizeof(NX_PACKET)) * 30)
#define DEMO_STACK_SIZE 2048
#define PPPOE_THREAD_SIZE 2048
/* Define the ThreadX and NetX object control blocks... */
TX_THREAD thread_0;
/* Define the packet pool and IP instance for normal IP instnace. */
NX_PACKET_POOL pool_0;
NX_IP ip_0;
/* Define the PPP Server instance. */
NX_PPP ppp_server;
/* Define the PPPoE Server instance. */
NX_PPPOE_SERVER pppoe_server;
/* Define the counters. */
CHAR *pointer;
ULONG error_counter;
/* Define thread prototypes. */
void thread_0_entry(ULONG thread_input);
/***** Substitute your PPP driver entry function here *********/
extern void _nx_ppp_driver(NX_IP_DRIVER *driver_req_ptr);
/***** Substitute your Ethernet driver entry function here *********/
extern void _nx_ram_network_driver(NX_IP_DRIVER *driver_req_ptr);
/* Define the callback functions. */
void PppDiscoverReq(UINT interfaceHandle);
void PppOpenReq(UINT interfaceHandle, ULONG length, UCHAR *data);
void PppCloseRsp(UINT interfaceHandle);
void PppCloseReq(UINT interfaceHandle);
void PppTransmitDataReq(UINT interfaceHandle, ULONG length, UCHAR *data, UINT packet_id);
void PppReceiveDataRsp(UINT interfaceHandle, UCHAR *data);
/* Define the porting layer function for PPP. */
void ppp_server_packet_send(NX_PACKET *packet_ptr);
/* Define main entry point. */
int main()
{
/* Enter the ThreadX kernel. */
tx_kernel_enter();
}
UINT verify_login(CHAR *name, CHAR *password)
{
if ((name[0] == 'm') &&
(name[1] == 'y') &&
(name[2] == 'n') &&
(name[3] == 'a') &&
(name[4] == 'm') &&
(name[5] == 'e') &&
(name[6] == (CHAR) 0) &&
(password[0] == 'm') &&
(password[1] == 'y') &&
(password[2] == 'p') &&
(password[3] == 'a') &&
(password[4] == 's') &&
(password[5] == 's') &&
(password[6] == 'w') &&
(password[7] == 'o') &&
(password[8] == 'r') &&
(password[9] == 'd') &&
(password[10] == (CHAR) 0))
return(NX_SUCCESS);
else
return(NX_PPP_ERROR);
}
/* Define what the initial system looks like. */
void tx_application_define(void *first_unused_memory)
{
UINT status;
/* Setup the working pointer. */
pointer = (CHAR *) first_unused_memory;
/* Initialize the NetX system. */
nx_system_initialize();
/* Create a packet pool for normal IP instance. */
status = nx_packet_pool_create(&pool_0, "NetX Main Packet Pool",
(1536 + sizeof(NX_PACKET)),
pointer, NX_PACKET_POOL_SIZE);
pointer = pointer + NX_PACKET_POOL_SIZE;
/* Check for error. */
if (status)
error_counter++;
/* Create an normal IP instance. */
status = nx_ip_create(&ip_0, "NetX IP Instance", IP_ADDRESS(192, 168, 100, 43), 0xFFFFFF00UL, &pool_0, _nx_ram_network_driver,
pointer, 2048, 1);
pointer = pointer + 2048;
/* Check for error. */
if (status)
error_counter++;
/* Create the PPP instance. */
status = nx_ppp_create(&ppp_server, "PPP Instance", &ip_0, pointer, 2048, 1, &pool_0, NX_NULL, NX_NULL);
pointer = pointer + 2048;
/* Check for PPP create error. */
if (status)
error_counter++;
/* Set the PPP packet send function. */
status = nx_ppp_packet_send_set(&ppp_server, ppp_server_packet_send);
/* Check for PPP packet send function set error. */
if (status)
error_counter++;
/* Define IP address. This PPP instance is effectively the server since it has both IP addresses. */
status = nx_ppp_ip_address_assign(&ppp_server, IP_ADDRESS(192, 168, 10, 43), IP_ADDRESS(192, 168, 10, 44));
/* Check for PPP IP address assign error. */
if (status)
error_counter++;
/* Setup PAP, this PPP instance is effectively the server since it will verify the name and password. */
status = nx_ppp_pap_enable(&ppp_server, NX_NULL, verify_login);
/* Check for PPP PAP enable error. */
if (status)
error_counter++;
/* Attach an interface for PPP. */
status = nx_ip_interface_attach(&ip_0, "Second Interface For PPP", IP_ADDRESS(0, 0, 0, 0), 0, nx_ppp_driver);
/* Check for error. */
if (status)
error_counter++;
/* Enable ARP and supply ARP cache memory for Normal IP Instance. */
status = nx_arp_enable(&ip_0, (void *) pointer, 1024);
pointer = pointer + 1024;
/* Check for ARP enable errors. */
if (status)
error_counter++;
/* Enable ICMP */
status = nx_icmp_enable(&ip_0);
if(status)
error_counter++;
/* Enable UDP traffic. */
status = nx_udp_enable(&ip_0);
if (status)
error_counter++;
/* Enable TCP traffic. */
status = nx_tcp_enable(&ip_0);
if (status)
error_counter++;
/* Create the main thread. */
tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
pointer, DEMO_STACK_SIZE,
4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
pointer = pointer + DEMO_STACK_SIZE;
}
/* Define the test threads. */
void thread_0_entry(ULONG thread_input)
{
UINT status;
ULONG ip_status;
/* Create the PPPoE instance. */
status = nx_pppoe_server_create(&pppoe_server, (UCHAR *)"PPPoE Server", &ip_0, 0, _nx_ram_network_driver, &pool_0, pointer, PPPOE_THREAD_SIZE, 4);
pointer = pointer + PPPOE_THREAD_SIZE;
if (status)
{
error_counter++;
return;
}
/* Set the callback notify function. */
status = nx_pppoe_server_callback_notify_set(&pppoe_server, PppDiscoverReq, PppOpenReq, PppCloseRsp, PppCloseReq, PppTransmitDataReq, PppReceiveDataRsp);
if (status)
{
error_counter++;
return;
}
#ifdef NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE
/* Call function function to set the default service Name. */
/*
PppInitInd(length, aData);
*/
#endif /* NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE */
/* Enable PPPoE Server. */
status = nx_pppoe_server_enable(&pppoe_server);
if (status)
{
error_counter++;
return;
}
/* Get the PPPoE Client physical address and Session ID after establish PPPoE Session. */
/*
status = nx_pppoe_server_session_get(&pppoe_server, interfaceHandle, &client_mac_msw, &client_mac_lsw, &session_id);
if (status)
error_counter++;
*/
/* Wait for the link to come up. */
status = nx_ip_interface_status_check(&ip_0, 1, NX_IP_ADDRESS_RESOLVED, &ip_status, NX_WAIT_FOREVER);
if (status)
{
error_counter++;
return;
}
#ifdef NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE
/* Call PPPoE function to terminate the PPPoE Session. */
/*
PppCloseInd(interfaceHandle, causeCode);
*/
#endif /* NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE */
}
void PppDiscoverReq(UINT interfaceHandle)
{
/* Receive the PPPoE Discovery Initiation Message. */
#ifdef NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE
/* Call PPPoE function to allow TTP's software to define the Service Name field of the PADO packet. */
PppDiscoverCnf(0, NX_NULL, interfaceHandle);
#endif /* NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE */
}
void PppOpenReq(UINT interfaceHandle, ULONG length, UCHAR *data)
{
/* Get the notify that receive the PPPoE Discovery Request Message. */
#ifdef NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE
/* Call PPPoE function to allow TTP's software to accept the PPPoE session. */
PppOpenCnf(NX_TRUE, interfaceHandle);
#endif /* NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE */
}
void PppCloseRsp(UINT interfaceHandle)
{
/* Get the notify that receive the PPPoE Discovery Terminate Message. */
#ifdef NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE
/* Call PPPoE function to allow TTP's software to confirm that the handle has been freed. */
PppCloseCnf(interfaceHandle);
#endif /* NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE */
}
void PppCloseReq(UINT interfaceHandle)
{
/* Get the notify that PPPoE Discovery Terminate Message has been sent. */
}
void PppTransmitDataReq(UINT interfaceHandle, ULONG length, UCHAR *data, UINT packet_id)
{
NX_PACKET *packet_ptr;
/* Get the notify that receive the PPPoE Session data. */
/* Call PPP Server to receive the PPP data fame. */
packet_ptr = (NX_PACKET *)(packet_id);
nx_ppp_packet_receive(&ppp_server, packet_ptr);
#ifdef NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE
/* Call PPPoE function to confirm that the data has been processed. */
PppTransmitDataCnf(interfaceHandle, data, packet_id);
#endif /* NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE */
}
void PppReceiveDataRsp(UINT interfaceHandle, UCHAR *data)
{
/* Get the notify that the PPPoE Session data has been sent. */
}
/* PPP Server send function. */
void ppp_server_packet_send(NX_PACKET *packet_ptr)
{
/* For PPP test, the session should be the first session, so set interfaceHandle as 0. */
UINT interfaceHandle = 0;
#ifdef NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE
NX_PACKET *temp_packet = packet_ptr;
while(packet_ptr)
{
/* Call functions to be provided by PPPoE for TTP. */
PppReceiveDataInd(interfaceHandle, (packet_ptr -> nx_packet_append_ptr - packet_ptr -> nx_packet_prepend_ptr), packet_ptr -> nx_packet_prepend_ptr);
/* Move to the next packet structure. */
packet_ptr = packet_ptr -> nx_packet_next;
}
nx_packet_transmit_release(temp_packet);
#else
/* Directly Call PPPoE send function to send out the data through PPPoE module. */
nx_pppoe_server_session_packet_send(&pppoe_server, interfaceHandle, packet_ptr);
#endif /* NX_PPPOE_SERVER_SESSION_CONTROL_ENABLE */
}
#endif /* NX_PPPOE_SERVER_INITIALIZE_DRIVER_ENABLE */
#endif /* NX_PPP_PPPOE_ENABLE */
#endif /* NX_DISABLE_IPV4 */