diff -ru pppoed-0.49/pppoed/lib.c /home/avian/pppoe2/pppoed/lib.c --- pppoed-0.49/pppoed/lib.c Sat Jul 27 23:20:33 2002 +++ /home/avian/pppoe2/pppoed/lib.c Sun Mar 30 18:50:16 2003 @@ -1,5 +1,9 @@ +/* Modified by Tomaz Solc */ + #include "pppoed.h" +int disc_sock; /* raw socket we use */ + int create_raw_socket(struct session *ses, unsigned short type) { int optval = 1, rv; @@ -9,8 +13,7 @@ return -1; } - if (setsockopt - (rv, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0) { + if (setsockopt(rv,SOL_SOCKET,SO_BROADCAST,&optval,sizeof(optval))<0) { poe_error(ses, "setsockopt: %m"); return -1; } @@ -333,10 +336,15 @@ return c; } -int -wait_for_packet(struct session *ses, int *sock, int ns, - struct pppoe_packet **packet, int *len, int num_ret, - int ret_pkt_size, struct pppoe_packet *ret_pkt) +/* Wait for packet. + * packet : received packet (will reuse memory if already alloc'd) + * num_ret : number of retries + * ret_pkt : send this packet if receive timeouts + */ + +int wait_for_packet(struct session *ses, int *sock, int ns, + struct pppoe_packet **packet, int *len, int num_ret, + int ret_pkt_size, struct pppoe_packet *ret_pkt) { fd_set fdset; int i, maxs = -1; @@ -378,6 +386,7 @@ return -1; } if (reason == 0) { /* timeout */ + poe_dbglog(ses, "timeout waiting for packet. [%d]", num_ret-retransmits); if (send_packet(ses, *sock, ret_pkt, ret_pkt_size, ses->name) < 0) { poe_warn(ses, "unable to send PADI packet"); return -1; @@ -490,8 +499,11 @@ { if (DEB_DISC) poe_dbglog(ses, "waiting for PADO"); - if (wait_for_packet(ses, &disc_sock, 1, &packet, &pkt_size, ses->PADI_ret, ret_pkt_size, ret_pkt) != disc_sock || - (packet->code != CODE_PADO && packet->code != CODE_PADT)) { + if (wait_for_packet(ses, &disc_sock, 1, &packet, &pkt_size, ses->PADI_ret, ret_pkt_size, ret_pkt) != disc_sock) { + poe_warn(ses, "receive error"); + return(-1); + } + if (packet->code != CODE_PADO && packet->code != CODE_PADT) { if (packet->code == CODE_PADI) { /* just an echo */ poe_dbglog(ses, "got PADI echo packet"); continue; @@ -674,14 +686,11 @@ int num_restart = 1; - if (ses->filt->num_restart) - num_restart = ses->filt->num_restart; - /* - poe_info (ses,"ppp_connect: total retries to go %d", num_restart); - */ + if (ses->num_restart) num_restart=ses->num_restart; + + poe_dbglog (ses,"ppp_connect: total retries to go %d", num_restart); - if (init_ses(ses) < 0) - return -1; + if (init_ses(ses) < 0) return -1; restart: num_restart--; @@ -692,21 +701,20 @@ */ if (-1 == serv_discover(ses)) { - poe_dbglog(ses, "failed serv_discover, attempting to restart [%d]", num_restart); + poe_warn(ses, "active discovery failed. [%d]", num_restart); if (!num_restart) { - poe_dbglog(ses, "too many restart attempts, leaving ppp_connect..."); cleanup_ses(ses); - poe_error(ses, "Failed to discover server!\n"); + poe_error(ses, "failed to discover server!\n"); return (-1); } goto restart; } - poe_dbglog(ses, "discovered Server!\n"); + poe_dbglog(ses, "discovered server!\n"); if (serv_request(ses) < 0) { cleanup_ses(ses); - poe_error(ses, "Failed to Connect to server!\n"); + poe_error(ses, "failed to connect to server!\n"); poe_die(-1); } @@ -721,109 +729,19 @@ int ses_connect(struct session *ses) { - int pid, ctrl_fd; - char pppox[32]; - char fname[MAX_FNAME]; - char pppd[MAX_FNAME]; -/* at this point we know the destination MAC address, - the session ID, - the device, -*/ - - if (strlen(ses->filt->pppd)) { - strcpy(pppd, ses->filt->pppd); - } else - strcpy(pppd, _PATH_PPPD); - - - if (strlen(ses->filt->fname)) { - strcpy(fname, ses->filt->fname); - } else - fname[0] = '\0'; + int ctrl_fd; + /* at this point we know the destination MAC address, + the session ID, + the device, */ ctrl_fd = open_device(ses); -/* download the cntrl info for this lineid */ + /* download the cntrl info for this lineid */ + cntrl(ses, ctrl_fd); close(ctrl_fd); - if (ses->np) - return 1; -/* - signal (SIGCHLD, &sigchild); -*/ - -/* - now lets fire the PPP session - TODO: use pthreads -*/ - - pid = fork(); - if (pid < 0) { - poe_error(ses, "unable to fork() for pppd: %m"); - poe_die(-1); - } - - ses->pid = pid; -#if 0 - sprintf(pppox, "/dev/pppox%d", ses->line); -#endif - if (snprintf(pppox, sizeof(pppox), "/dev/pppox%d", ses->line) == - -1) { - poe_error(ses, "Aiee!!!!!!!!!! lineid too big!!!"); - poe_die(-1); - } - - if (!pid) { - /* the speed is meaningless really, but pppd insists */ - if (strlen(ses->filt->fname)) { - poe_info(ses, "starting %s to interface %s using options file: %s", pppd, pppox, fname); - - if (!ses->filt->peermode) { - if (execl(pppd, "pppd", pppox, "38400", "file", fname, NULL) < 0) { - poe_error(ses, "cannot execl %s: %m", pppd); - poe_die(-1); - } - } else { - if (execl(pppd, "pppd", "call", fname, NULL) < 0) { - poe_error(ses, "cannot execl %s: %m", pppd); - poe_die(-1); - } - - } - } else { - poe_info(ses, "starting %s to interface %s", pppd, pppox); - - if (execl(pppd, "pppd", pppox, "38400", NULL) < 0) { - poe_error(ses, "cannot execl %s: %m", pppd); - poe_die(-1); - } - } - } - - poe_dbglog(ses, "PPP started pid is: %d", ses->pid); - -/* - sit here and asynchronously wait for a PADT - or issue a PADT should the user decide to terminate the ppp_connection - Hmmmm ... maybe we should have an option to restart the ppp_connection - if it dies - */ - -/* move to ppp_connected state only after pppd has been started */ - ses->state = CONNECTED; - - pause(); - - ses->state = DISCONNECTED; - poe_info(ses, "OK we got killed"); - -/* - if (ses->filt->num_restart < 2 ) - return (0); -*/ - - return 1; + return(1); } diff -ru pppoed-0.49/pppoed/pppoed.c /home/avian/pppoe2/pppoed/pppoed.c --- pppoed-0.49/pppoed/pppoed.c Sat Jul 27 23:11:31 2002 +++ /home/avian/pppoe2/pppoed/pppoed.c Sun Mar 30 18:51:31 2003 @@ -1,5 +1,6 @@ /* * Original Author: Luke Stras + * Modified by: Tomaz Solc * * Revision History * 1999/09/22 stras Initial version @@ -30,91 +31,38 @@ #include "pppoed.h" - +struct session sess; void sigproc(int src) { - int i; -/* - info ("Received signal %d", src); -*/ - for (i = 0; i < PPPOED_NUM_SESSIONS; i++) { - if (ses_table[i].pid > 0) { - poe_dbglog(&ses_table[i], "Sending signal %d to pid %d", src, ses_table[i].pid); - kill(ses_table[i].pid, src); - } - } + poe_warn(&sess, "Received signal %d", src); poe_die(1); } - -void sigchild(int src) -{ - pid_t pid; - int status; - int i; - pid = waitpid(-1, &status, WNOHANG); -/* - poe_info(0, "Child received signal %d PID %d, status %d", src, pid, status); -*/ - if (pid < 1) { - return; - } - if (WIFEXITED(status)) { - i = WEXITSTATUS(status); - if (i != 0) { - poe_info(0, "Child exited with error: %d\n", i); - } - } else { - poe_info(0, "Child abnormal exit\n"); - } -/* scan */ - for (i = 0; i < PPPOED_NUM_SESSIONS; i++) { - if (ses_table[i].pid == pid) { - poe_info(&ses_table[i], "Line id which died is: %d", i); - ses_table[i].state = DISCONNECTED; - break; - } - - } - -} - void print_help() { - - fprintf(stdout, "\npppoe version %d.%d build %d\n", VERSION_MAJOR, + fprintf(stderr, "\npppoe version %d.%d build %d\n", VERSION_MAJOR, VERSION_MINOR, VERSION_DATE); - fprintf(stdout, "\nrecognized options are:"); - fprintf(stdout, "\n -V : displays version"); - fprintf(stdout, + fprintf(stderr, "Modified by Tomaz Solc \n"); + fprintf(stderr, "\nrecognized options are:"); + fprintf(stderr, "\n -V : displays version"); + fprintf(stderr, "\n -I : overrides the default interface of eth0"); - fprintf(stdout, + fprintf(stderr, "\n -L : overrides the default lineid of pppox0. Used to"); - fprintf(stdout, "\n have multiple sessions"); - fprintf(stdout, "\n -S : starts pppoed in server mode"); - fprintf(stdout, + fprintf(stderr, "\n have multiple sessions"); + fprintf(stderr, "\n -R : forces pppoed to be restarted num_retries"); - fprintf(stdout, + fprintf(stderr, "\n should the other end be detected to be dead."); - fprintf(stdout, + fprintf(stderr, "\n Needs lcp_echo. Read the INSTALL file instructions"); - fprintf(stdout, - "\n -F : specifies additional ppp options file"); - fprintf(stdout, - "\n -C : ppp options file in /etc/ppp/peers/"); - fprintf(stdout, "\n -d : sets debug level"); - fprintf(stdout, - "\n -D : prevents pppoed from detaching itself and running in the background"); - fprintf(stdout, - "\n -P : selects a different pppd. Defaults to " - _PATH_PPPD); - fprintf(stdout, "\n -A to select a specific AC by name"); - fprintf(stdout, + fprintf(stderr, "\n -d : sets debug level"); + fprintf(stderr, "\n -A to select a specific AC by name"); + fprintf(stderr, "\n -E to select a specific AC service by name\n"); } - int get_args(int argc, char **argv, struct session *sess) { struct filter *filt; @@ -122,25 +70,23 @@ int opt; char htag[256]; /* the number of PADI and PADR retransmits. - We should be able to use the command line options to pass these instead + * We should be able to use the command line options to pass these instead */ sess->PADR_ret = 5; sess->PADI_ret = 5; - sess->np = 0; sess->opt_debug = 0; DEB_DISC = 0; DEB_DISC2 = 0; - sess->opt_daemonize = 1; - sess->log_to_fd = fileno(stdout); + sess->log_to_fd = fileno(stderr); /* defaults to eth0 */ strcpy(sess->name, "eth0"); if ((sess->filt = malloc(sizeof(struct filter))) == NULL) { - poe_error(sess, "failed to malloc for Filter "); + poe_error(sess, "failed to malloc for filter "); poe_die(-1); } @@ -150,17 +96,16 @@ /* set default filters; move this to routine */ /* parse options */ - while ((opt = - getopt(argc, argv, "A:C:E:d:DR:I:F:L:V:P:S:N:G")) != -1) + while ((opt = getopt(argc, argv, "A:E:d:R:I:L:V")) != -1) switch (opt) { case 'R': /* sets number of retries */ - filt->num_restart = strtol(optarg, (char **) NULL, 10); - if (filt->num_restart < 0) { - poe_error(sess, "Invalid number of restarts"); + sess->num_restart = strtol(optarg, (char **) NULL, 10); + if (sess->num_restart < 0) { + poe_error(sess, "invalid number of restarts"); return (-1); } - filt->num_restart += 1; + sess->num_restart += 1; break; case 'I': /* sets interface */ if (strlen(optarg) >= IFNAMSIZ) { @@ -170,34 +115,6 @@ } strncpy(sess->name, optarg, sizeof(sess->name)); break; - case 'C': /* name of the file in /etc/ppp/peers */ - if (strlen(filt->fname)) { - poe_error(sess, "-F can not be used with -C"); - return (-1); - } - if (strlen(optarg) >= MAX_FNAME) { - poe_error(sess, "file name cannot exceed %d characters", MAX_FNAME - 1); - return (-1); - } - strncpy(filt->fname, optarg, sizeof(filt->fname)); - filt->peermode = 1; - break; - case 'F': /* sets the options file */ - if (strlen(filt->fname)) { - poe_error(sess, "-F can not be used with -C"); - return (-1); - } - - if (strlen(optarg) >= MAX_FNAME) { - poe_error(sess, "file name cannot exceed %d characters", - MAX_FNAME - 1); - return (-1); - } - strncpy(filt->fname, optarg, sizeof(filt->fname)); - break; - case 'D': /* don't daemonize */ - sess->opt_daemonize = 0; - break; case 'd': /* debug level */ sess->opt_debug = strtol(optarg, (char **) NULL, 10); @@ -206,17 +123,6 @@ if (sess->opt_debug & 0x0004) DEB_DISC2 = 1; break; - case 'P': /* sets the pppd binary */ - if (strlen(optarg) >= MAX_FNAME) { - poe_error(sess, "pppd binary cant exceed %d characters", - MAX_FNAME - 1); - return (-1); - } - strncpy(filt->pppd, optarg, sizeof(filt->pppd)); - break; - case 'G': - sess->np = 1; - break; case 'L': /* lineid */ sess->line = strtol(optarg, (char **) NULL, 10); if (sess->line < 0) { @@ -227,10 +133,7 @@ sess->line); break; case 'V': /* version */ - fprintf(stdout, "pppoe version %d.%d build %d", VERSION_MAJOR, VERSION_MINOR, VERSION_DATE); - return (0); - case 'S': /* server mode */ - poe_error(sess, "pppoe server mode not supported in this version"); + fprintf(stderr, "pppoe version %d.%d build %d\n", VERSION_MAJOR, VERSION_MINOR, VERSION_DATE); return (0); case 'A': /* server mode */ poe_info(sess, "AC name override to %s", optarg); @@ -277,42 +180,26 @@ int main(int argc, char **argv) { + int n; -/* -only worry about a single ppp_connection in this version -Multi-pppoe is achieved by starting multiple sessions. -Please dont send me any patches on this. Its part of -the restructuring plans later. It works just fine. -*/ - struct session *sess = &ses_table[0]; - memset(sess, 0, sizeof(struct session)); + memset(&sess, 0, sizeof(struct session)); - if ((get_args(argc, (char **) argv, sess)) < 1) - poe_die(-1); - - if (!sess->np) { - poe_create_pidfile(sess); - } + if ((get_args(argc, (char **) argv, &sess)) < 1) poe_die(-1); openlog("pppoed", LOG_PID | LOG_NDELAY, LOG_PPPOE); - setlogmask(LOG_UPTO(sess->opt_debug ? LOG_DEBUG : LOG_INFO)); + setlogmask(LOG_UPTO(sess.opt_debug ? LOG_DEBUG : LOG_INFO)); - if (!sess->np && sess->opt_daemonize) { - poe_detach(sess); - if (!sess->detached) - poe_error(sess, "failed to detach!"); - } + signal(SIGINT, &sigproc); + signal(SIGTERM, &sigproc); - if (!sess->np) { - signal(SIGINT, &sigproc); - signal(SIGTERM, &sigproc); - signal(SIGCHLD, &sigchild); - } + /* ppp_connect never comes back */ + /* Oh yes it does! */ + + poe_info(&sess, "version %d.%d build %d", VERSION_MAJOR, VERSION_MINOR, VERSION_DATE); -/* ppp_connect never comes back */ - ppp_connect(sess); + n=ppp_connect(&sess); - poe_info(sess, "ppp_connect came back!"); + if (n>0) poe_info(&sess, "discovery stage completed. exiting."); else exit(1); exit(0); } diff -ru pppoed-0.49/pppoed/pppoed.h /home/avian/pppoe2/pppoed/pppoed.h --- pppoed-0.49/pppoed/pppoed.h Sat Jul 27 23:23:44 2002 +++ /home/avian/pppoe2/pppoed/pppoed.h Sun Mar 23 18:11:32 2003 @@ -41,18 +41,14 @@ #define CONNECTED 1 #define DISCONNECTED 0 -#ifndef _PATH_PPPD -#define _PATH_PPPD "/usr/sbin/pppd" -#endif - #ifndef LOG_PPPOE #define LOG_PPPOE LOG_DAEMON #endif #define VERSION_MAJOR 0 -#define VERSION_MINOR 4 -#define VERSION_DATE 20020727 +#define VERSION_MINOR 5 +#define VERSION_DATE 20030321 /* Bigger than the biggest ethernet packet we'll ever see, in bytes */ #define MAX_PACKET 2000 @@ -122,11 +118,6 @@ } __attribute__ ((packed)); struct filter { - int num_restart; - char fname[MAX_FNAME]; - char pppd[MAX_FNAME]; - int peermode; - struct pppoe_tag *stag; struct pppoe_tag *ntag; struct pppoe_tag *ctag; @@ -134,42 +125,29 @@ } __attribute__ ((packed)); struct session { - int pid; /* process ID */ - int state; /* Current state */ int line; /* PPPox dev number */ int sid; /* session ID */ - char name[IFNAMSIZ]; /*dev name */ - char dmac[ETH_ALEN]; /*destination MAC */ - char smac[ETH_ALEN]; /*Source MAC */ + char name[IFNAMSIZ]; /* dev name */ + char dmac[ETH_ALEN]; /* destination MAC */ + char smac[ETH_ALEN]; /* Source MAC */ int tags_len; /* retransmit retries for the PADR and PADI packets during discovery */ + int PADR_ret; int PADI_ret; + + int num_restart; - int opt_daemonize; int opt_debug; - int detached; - int np; struct pppoe_tag *tags; int log_to_fd; struct filter *filt; } __attribute__ ((packed)); -#define PPPOED_NUM_SESSIONS 16 - -struct session ses_table[PPPOED_NUM_SESSIONS]; - -int disc_sock; /* raw socket we use */ - - - /* Functions exported from utils.c. */ -void poe_detach (struct session *) ; /* Detach from controlling tty */ - void poe_die (int) __attribute__ ((__noreturn__)); /* Cleanup and exit */ -void poe_create_pidfile (struct session *); void poe_log_packet (u_char *, int, char *, int); /* Format a packet and log it with syslog */ diff -ru pppoed-0.49/pppoed/utils.c /home/avian/pppoe2/pppoed/utils.c --- pppoed-0.49/pppoed/utils.c Sat Jul 27 23:11:38 2002 +++ /home/avian/pppoe2/pppoed/utils.c Sun Mar 30 18:50:42 2003 @@ -4,6 +4,8 @@ * * mostly stolen from ppp-2.3.10 by Marc Boucher * + * Modified by Tomaz Solc + * * Copyright (c) 1999 The Australian National University. * All rights reserved. * @@ -39,12 +41,6 @@ #include "pppoed.h" -static char pidfilename[PATH_MAX]; /* name of pid file */ - -/* -static int detached = 0; - */ - static void vslp_printer(void *, char *, ...); static void format_packet(struct pppoe_packet *, int, void (*)(void *, char *, ...), void *); @@ -600,54 +596,11 @@ } /* - * Create a file containing our process ID. - */ -void poe_create_pidfile(struct session *ses) -{ - FILE *pidfile; - - sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, "pppoed"); - if ((pidfile = fopen(pidfilename, "w")) != NULL) { - fprintf(pidfile, "%d\n", getpid()); - (void) fclose(pidfile); - } else { - poe_error(ses, "Failed to create pid file %s: %m", - pidfilename); - pidfilename[0] = 0; - } -} - -/* - * detach - detach us from the controlling terminal. - */ -void poe_detach(struct session *ses) -{ - if (ses->detached) - return; - - if ((daemon(0, 0)) < 0) { - poe_error(ses, "Couldn't detach (daemon failed: %m)"); -#if 0 - poe_die(1); /* or just return? */ -#endif - } - ses->detached = 1; - ses->log_to_fd = -1; - /* update pid files if they have been written already */ - if (pidfilename[0]) - poe_create_pidfile(ses); -} - -/* * cleanup - restore anything which needs to be restored before we exit */ /* ARGSUSED */ static void cleanup() { - if (pidfilename[0] != 0 && unlink(pidfilename) < 0 - && errno != ENOENT) - syslog(LOG_INFO, "unable to delete pid file "); - pidfilename[0] = 0; } /*