1 /* PPPoE support library "libpppoe"
3 * Copyright 2000 Jamal Hadi Salim <hadi@cyberus.ca>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
18 fprintf (stderr,"Received signal %d", src);
27 pid = waitpid (-1, &status, WNOHANG);
30 fprintf (stderr,"Child received signal %d PID %d, status %d", src, pid, status);
40 fprintf (stdout,"\npppoe version %d.%d build %d", VERSION_MAJOR, VERSION_MINOR,
42 fprintf (stdout,"\nrecognized options are:");
43 fprintf (stdout,"\n -I <interface> : overrides the default interface of eth0");
44 fprintf (stdout,"\n -S : starts pppoed in server mode");
45 fprintf (stdout,"\n -R <num_retries>: forces pppoed to be restarted num_retries");
46 fprintf (stdout,"\n should the other end be detected to be dead.");
47 fprintf (stdout,"\n Needs lcp_echo. Read the INSTALL file instructions");
48 fprintf (stdout,"\n -F <filename> : specifies additional ppp options file");
49 fprintf (stdout,"\n -C <filename> : ppp options file in /etc/ppp/peers/");
50 fprintf (stdout,"\n -d <level> : sets debug level");
51 fprintf (stdout,"\n -D : prevents pppoed from detaching itself and running in the background");
52 fprintf (stdout,"\n -P <path to pppd> : selects a different pppd. Defaults to " _PATH_PPPD);
53 fprintf (stdout,"\n -A <AC name> to select a specific AC by name");
54 fprintf (stdout,"\n -E <AC service name> to select a specific AC service by name");
55 fprintf (stdout,"\n -G Do service discovery only");
56 fprintf (stdout,"\n -H Do service discovery and connection (no pppd)\n");
61 get_args (int argc, char **argv,struct session *sess)
73 sess->opt_daemonize = 0;
75 sess->log_to_fd = fileno (stdout);
77 /* defaults to eth0 */
78 strcpy (sess->name, "eth0");
81 if ((sess->filt=malloc(sizeof(struct filter))) == NULL) {
82 poe_error (sess,"failed to malloc for Filter ");
86 filt=sess->filt; /* makes the code more readable */
87 memset(filt,0,sizeof(struct filter));
91 /* set default filters; move this to routine */
94 while ((opt = getopt (argc, argv, "A:C:E:d:DR:I:F:L:V:P:SN:GH")) != -1)
97 case 'R': /* sets number of retries */
98 filt->num_restart = strtol (optarg, (char **) NULL, 10);
99 filt->num_restart += 1;
101 case 'I': /* sets interface */
102 if (strlen (optarg) >= IFNAMSIZ) {
103 poe_error (sess,"interface name cannot exceed %d characters", IFNAMSIZ - 1);
106 strncpy (sess->name, optarg, strlen(optarg)+1);
108 case 'C': /* name of the file in /etc/ppp/peers */
109 if (NULL != filt->fname) {
110 poe_error (sess,"-F can not be used with -C");
113 if (strlen(optarg) > MAX_FNAME) {
114 poe_error (sess,"file name cannot exceed %d characters", MAX_FNAME - 1);
117 filt->fname=malloc(strlen(optarg));
118 strncpy (filt->fname, optarg, strlen(optarg));
121 case 'F': /* sets the options file */
122 if (NULL != filt->fname) {
123 poe_error (sess,"-F can not be used with -C");
127 if (strlen(optarg) > MAX_FNAME) {
128 poe_error (sess,"file name cannot exceed %d characters", MAX_FNAME - 1);
131 filt->fname=malloc(strlen(optarg)+1);
132 strncpy (filt->fname, optarg, strlen(optarg)+1);
134 poe_info (sess,"selected %s as filename\n",filt->fname);
136 case 'D': /* don't daemonize */
137 sess->opt_daemonize = 1;
140 case 'd': /* debug level */
141 sess->opt_debug = strtol (optarg, (char **) NULL, 10);
142 if (sess->opt_debug & 0x0002)
144 if (sess->opt_debug & 0x0004)
147 case 'P': /* sets the pppd binary */
148 if (strlen(optarg) > MAX_FNAME) {
149 poe_error (sess,"pppd binary cant exceed %d characters", MAX_FNAME - 1);
152 filt->pppd=malloc(strlen(optarg));
153 strncpy (filt->pppd, optarg, strlen(optarg));
161 case 'V': /* version */
162 fprintf (stdout,"pppoe version %d.%d build %d", VERSION_MAJOR,
163 VERSION_MINOR, VERSION_DATE);
165 case 'S': /* server mode */
166 sess->type = SESSION_SERVER;
168 case 'A': /* AC override */
169 poe_info (sess,"AC name override to %s", optarg);
170 if (strlen (optarg) > 255) {
171 poe_error (sess," AC name too long
172 (maximum allowed 256 chars)");
175 if ((sess->filt->ntag= malloc (sizeof (struct pppoe_tag) +
176 strlen (optarg)))== NULL) {
177 poe_error (sess,"failed to malloc for AC name");
180 sess->filt->ntag->tag_len=htons(strlen(optarg));
181 sess->filt->ntag->tag_type=PTT_AC_NAME;
182 poe_error (sess," pppoe_ac_name: AC name Override %p\n",
184 strcpy(sess->filt->ntag->tag_data,optarg);
186 case 'E': /* AC service name override */
187 poe_info (sess,"AC service name override to %s", optarg);
188 if (strlen (optarg) > 255) {
189 poe_error (sess," Service name too long
190 (maximum allowed 256 chars)");
194 if ((filt->stag = malloc (strlen (optarg) + sizeof (struct pppoe_tag))) == NULL) {
195 poe_error (sess,"failed to malloc for service name: %m");
199 filt->stag->tag_len = htons (strlen (optarg));
200 filt->stag->tag_type = PTT_SRV_NAME;
201 strcpy ((char *) (filt->stag->tag_data), optarg);
204 poe_error (sess,"Unknown option '%c'", optopt);
215 int main(int argc, char** argv){
218 struct session *ses = (struct session *)malloc(sizeof(struct session));
220 ses=(void *)malloc(sizeof(struct session));
225 memset(ses,0,sizeof(struct session));
229 openlog ("pppoed", LOG_PID | LOG_NDELAY, LOG_PPPOE);
230 setlogmask (LOG_UPTO (ses->opt_debug ? LOG_DEBUG : LOG_INFO));
233 if ((get_args (argc,(char **) argv,ses)) <1)
236 filt=ses->filt; /* makes the code more readable */
239 poe_create_pidfile (ses);
240 // signal (SIGINT, &sigproc);
241 // signal (SIGTERM, &sigproc);
242 signal (SIGCHLD, &sigchild);
245 if(ses->type == SESSION_CLIENT){
247 poe_info(ses,"calling client_init_ses\n");
248 ret = client_init_ses(ses,ses->name);
254 while (ses->filt->num_restart > 0)
256 poe_info(ses,"Restart number %d ",ses->filt->num_restart);
258 ses->filt->num_restart--;
261 }else if( ses->type == SESSION_SERVER ){
263 poe_info(ses,"calling srv_init_ses\n");
264 ret = srv_init_ses(ses,ses->name);
272 ret = ppp_connect(ses);
279 poe_info(ses,"ppp_connect came back! %d",ret);