X-Git-Url: http://git.ozlabs.org/?p=ppp.git;a=blobdiff_plain;f=pppd%2Fmain.c;h=16231db75ead75a846022061d3a9a4d4c2395306;hp=a6f4e06d6274ff721d2b3b4517167f5ba1385db6;hb=bcfa20820fc9ff3b25bcf62308e3e737c1897dc6;hpb=c64ecb3a2bab4543f7916ad87a011f11265c7e55 diff --git a/pppd/main.c b/pppd/main.c index a6f4e06..16231db 100644 --- a/pppd/main.c +++ b/pppd/main.c @@ -17,7 +17,7 @@ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ -#define RCSID "$Id: main.c,v 1.104 2001/03/09 00:55:14 paulus Exp $" +#define RCSID "$Id: main.c,v 1.112 2002/02/12 20:07:09 dfs Exp $" #include #include @@ -54,7 +54,10 @@ #include "chap.h" #include "ccp.h" #include "pathnames.h" + +#ifdef USE_TDB #include "tdb.h" +#endif #ifdef CBCP_SUPPORT #include "cbcp.h" @@ -80,6 +83,7 @@ char hostname[MAXNAMELEN]; /* Our hostname */ static char pidfilename[MAXPATHLEN]; /* name of pid file */ static char linkpidfile[MAXPATHLEN]; /* name of linkname pid file */ char ppp_devnam[MAXPATHLEN]; /* name of PPP tty (maybe ttypx) */ +char remote_number[MAXNAMELEN]; /* Remote telephone number, if available */ uid_t uid; /* Our real user-id */ struct notifier *pidchange = NULL; struct notifier *phasechange = NULL; @@ -94,11 +98,18 @@ volatile int status; /* exit status for pppd */ int unsuccess; /* # unsuccessful connection attempts */ int do_callback; /* != 0 if we should do callback next */ int doing_callback; /* != 0 if we are doing callback */ +int ppp_session_number; /* Session number, for channels with such a + concept (eg PPPoE) */ +#ifdef USE_TDB TDB_CONTEXT *pppdb; /* database for storing status etc. */ +#endif + char db_key[32]; int (*holdoff_hook) __P((void)) = NULL; int (*new_phase_hook) __P((int)) = NULL; +void (*snoop_recv_hook) __P((unsigned char *p, int len)) = NULL; +void (*snoop_send_hook) __P((unsigned char *p, int len)) = NULL; static int conn_running; /* we have a [dis]connector running */ static int devfd; /* fd of underlying device */ @@ -170,11 +181,16 @@ static void open_ccp __P((int)); static void bad_signal __P((int)); static void holdoff_end __P((void *)); static int reap_kids __P((int waitfor)); + +#ifdef USE_TDB static void update_db_entry __P((void)); static void add_db_key __P((const char *)); static void delete_db_key __P((const char *)); static void cleanup_db __P((void)); +#endif + static void handle_events __P((void)); +static void print_link_stats __P((void)); extern char *ttyname __P((int)); extern char *getlogin __P((void)); @@ -233,6 +249,7 @@ main(argc, argv) struct protent *protp; char numbuf[16]; + link_stats_valid = 0; new_phase(PHASE_INITIALIZE); /* @@ -351,6 +368,7 @@ main(argc, argv) */ sys_init(); +#ifdef USE_TDB pppdb = tdb_open(_PATH_PPPDB, 0, 0, O_RDWR|O_CREAT, 0644); if (pppdb != NULL) { slprintf(db_key, sizeof(db_key), "pppd%d", getpid()); @@ -362,6 +380,7 @@ main(argc, argv) multilink = 0; } } +#endif /* * Detach ourselves from the terminal, if required, @@ -398,11 +417,14 @@ main(argc, argv) /* * Open the loopback channel and set it up to be the ppp interface. */ +#ifdef USE_TDB tdb_writelock(pppdb); +#endif fd_loop = open_ppp_loopback(); set_ifunit(1); +#ifdef USE_TDB tdb_writeunlock(pppdb); - +#endif /* * Configure the interface and mark it up, etc. */ @@ -452,25 +474,33 @@ main(argc, argv) goto fail; /* set up the serial device as a ppp interface */ +#ifdef USE_TDB tdb_writelock(pppdb); - fd_ppp = establish_ppp(devfd); +#endif + fd_ppp = the_channel->establish_ppp(devfd); if (fd_ppp < 0) { +#ifdef USE_TDB tdb_writeunlock(pppdb); +#endif status = EXIT_FATAL_ERROR; goto disconnect; } if (!demand && ifunit >= 0) set_ifunit(1); +#ifdef USE_TDB tdb_writeunlock(pppdb); +#endif /* * Start opening the connection and wait for * incoming events (reply, timeout, etc.). */ - notice("Connect: %s <--> %s", ifname, ppp_devnam); + if (ifunit >= 0) + notice("Connect: %s <--> %s", ifname, ppp_devnam); + else + notice("Starting negotiation on %s", ppp_devnam); gettimeofday(&start_time, NULL); - link_stats_valid = 0; script_unsetenv("CONNECT_TIME"); script_unsetenv("BYTES_SENT"); script_unsetenv("BYTES_RCVD"); @@ -493,15 +523,7 @@ main(argc, argv) } } - /* - * Print connect time and statistics. - */ - if (link_stats_valid) { - int t = (link_connect_time + 5) / 6; /* 1/10ths of minutes */ - info("Connect time %d.%d minutes.", t/10, t%10); - info("Sent %d bytes, received %d bytes.", - link_stats.bytes_out, link_stats.bytes_in); - } + print_link_stats(); /* * Delete pid file before disestablishing ppp. Otherwise it @@ -510,7 +532,7 @@ main(argc, argv) */ if (!demand) { if (pidfilename[0] != 0 - && unlink(pidfilename) < 0 && errno != ENOENT) + && unlink(pidfilename) < 0 && errno != ENOENT) warn("unable to delete pid file %s: %m", pidfilename); pidfilename[0] = 0; } @@ -522,9 +544,7 @@ main(argc, argv) */ remove_fd(fd_ppp); clean_check(); - if (demand) - restore_loop(); - disestablish_ppp(devfd); /* XXX */ + the_channel->disestablish_ppp(devfd); fd_ppp = -1; if (!hungup) lcp_lowerdown(0); @@ -545,7 +565,7 @@ main(argc, argv) if (!demand) { if (pidfilename[0] != 0 - && unlink(pidfilename) < 0 && errno != ENOENT) + && unlink(pidfilename) < 0 && errno != ENOENT) warn("unable to delete pid file %s: %m", pidfilename); pidfilename[0] = 0; } @@ -944,14 +964,14 @@ get_input() return; } - if (debug /*&& (debugflags & DBG_INPACKET)*/) - dbglog("rcvd %P", p, len); - if (len < PPP_HDRLEN) { - MAINDEBUG(("io(): Received short packet.")); + dbglog("received short packet:%.*B", len, p); return; } + dump_packet("rcvd", p, len); + if (snoop_recv_hook) snoop_recv_hook(p, len); + p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; @@ -1021,6 +1041,7 @@ void die(status) int status; { + print_link_stats(); cleanup(); notify(exitnotify, status); syslog(LOG_INFO, "Exit."); @@ -1037,19 +1058,36 @@ cleanup() sys_cleanup(); if (fd_ppp >= 0) - disestablish_ppp(devfd); + the_channel->disestablish_ppp(devfd); if (the_channel->cleanup) (*the_channel->cleanup)(); - if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT) + if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT) warn("unable to delete pid file %s: %m", pidfilename); pidfilename[0] = 0; - if (linkpidfile[0] != 0 && unlink(linkpidfile) < 0 && errno != ENOENT) + if (linkpidfile[0] != 0 && unlink(linkpidfile) < 0 && errno != ENOENT) warn("unable to delete pid file %s: %m", linkpidfile); linkpidfile[0] = 0; +#ifdef USE_TDB if (pppdb != NULL) cleanup_db(); +#endif + +} + +void +print_link_stats() +{ + /* + * Print connect time and statistics. + */ + if (link_stats_valid) { + int t = (link_connect_time + 5) / 6; /* 1/10ths of minutes */ + info("Connect time %d.%d minutes.", t/10, t%10); + info("Sent %u bytes, received %u bytes.", + link_stats.bytes_out, link_stats.bytes_in); + } } /* @@ -1089,9 +1127,6 @@ static struct timeval timenow; /* Current time */ /* * timeout - Schedule a timeout. - * - * Note that this timeout takes the number of milliseconds, NOT hz (as in - * the kernel). */ void timeout(func, arg, secs, usecs) @@ -1100,10 +1135,10 @@ timeout(func, arg, secs, usecs) int secs, usecs; { struct callout *newp, *p, **pp; - + MAINDEBUG(("Timeout %p:%p in %d.%03d seconds.", func, arg, - time / 1000, time % 1000)); - + secs, usecs/1000)); + /* * Allocate timeout. */ @@ -1141,9 +1176,9 @@ untimeout(func, arg) void *arg; { struct callout **copp, *freep; - + MAINDEBUG(("Untimeout %p:%p.", func, arg)); - + /* * Find first matching timeout and remove it from the list. */ @@ -1497,7 +1532,7 @@ run_program(prog, args, must_exist, done, arg) #ifdef BSD /* Force the priority back to zero if pppd is running higher. */ if (setpriority (PRIO_PROCESS, 0, 0) < 0) - warn("can't reset priority to 0: %m"); + warn("can't reset priority to 0: %m"); #endif /* SysV recommends a second fork at this point. */ @@ -1578,7 +1613,8 @@ reap_kids(waitfor) (chp? chp->prog: "??"), pid, WTERMSIG(status)); } else if (debug) dbglog("Script %s finished (pid %d), status = 0x%x", - (chp? chp->prog: "??"), pid, status); + (chp? chp->prog: "??"), pid, + WIFEXITED(status) ? WEXITSTATUS(status) : status); if (chp && chp->done) (*chp->done)(chp->arg); if (chp) @@ -1684,13 +1720,17 @@ script_setenv(var, value, iskey) if (script_env != 0) { for (i = 0; (p = script_env[i]) != 0; ++i) { if (strncmp(p, var, varl) == 0 && p[varl] == '=') { +#ifdef USE_TDB if (p[-1] && pppdb != NULL) delete_db_key(p); +#endif free(p-1); script_env[i] = newstring; +#ifdef USE_TDB if (iskey && pppdb != NULL) add_db_key(newstring); update_db_entry(); +#endif return; } } @@ -1717,11 +1757,13 @@ script_setenv(var, value, iskey) script_env[i] = newstring; script_env[i+1] = 0; +#ifdef USE_TDB if (pppdb != NULL) { if (iskey) add_db_key(newstring); update_db_entry(); } +#endif } /* @@ -1740,18 +1782,23 @@ script_unsetenv(var) return; for (i = 0; (p = script_env[i]) != 0; ++i) { if (strncmp(p, var, vl) == 0 && p[vl] == '=') { +#ifdef USE_TDB if (p[-1] && pppdb != NULL) delete_db_key(p); +#endif free(p-1); while ((script_env[i] = script_env[i+1]) != 0) ++i; break; } } +#ifdef USE_TDB if (pppdb != NULL) update_db_entry(); +#endif } +#ifdef USE_TDB /* * update_db_entry - update our entry in the database. */ @@ -1781,6 +1828,9 @@ update_db_entry() if (tdb_store(pppdb, key, dbuf, TDB_REPLACE)) error("tdb_store failed: %s", tdb_error(pppdb)); + if (vbuf) + free(vbuf); + } /* @@ -1831,3 +1881,4 @@ cleanup_db() if (p[-1]) delete_db_key(p); } +#endif /* USE_TDB */