📄 main.c
字号:
{ 0x8043, "Ascom Timeplex" }, { 0x8045, "Fujitsu LBLB Control Protocol" }, { 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, { 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, { 0x804b, "SNA over 802.2 Control Protocol" }, { 0x804d, "SNA Control Protocol" }, { 0x804f, "IP6 Header Compression Control Protocol" }, { 0x006f, "Stampede Bridging Control Protocol" }, { 0x80fb, "Single Link Compression Control Protocol" }, { 0x80fd, "Compression Control Protocol" }, { 0xc021, "Link Control Protocol" }, { 0xc023, "Password Authentication Protocol" }, { 0xc025, "Link Quality Report" }, { 0xc027, "Shiva Password Authentication Protocol" }, { 0xc029, "CallBack Control Protocol (CBCP)" }, { 0xc081, "Container Control Protocol" }, { 0xc223, "Challenge Handshake Authentication Protocol" }, { 0xc281, "Proprietary Authentication Protocol" }, { 0, NULL },};/* * protocol_name - find a name for a PPP protocol. */const char *protocol_name(proto) int proto;{ struct protocol_list *lp; for (lp = protocol_list; lp->proto != 0; ++lp) if (proto == lp->proto) return lp->name; return NULL;}/* * get_input - called when incoming data is available. */static voidget_input(){ int len, i; u_char *p; u_short protocol; struct protent *protp; p = inpacket_buf; /* point to beginning of packet buffer */ len = read_packet(inpacket_buf); if (len < 0) return; if (len == 0) { notice("Modem hangup"); hungup = 1; status = EXIT_HANGUP; lcp_lowerdown(0); /* serial link is no longer available */ link_terminated(0); return; } if (debug /*&& (debugflags & DBG_INPACKET)*/) dbglog("rcvd %P", p, len); if (len < PPP_HDRLEN) { MAINDEBUG(("io(): Received short packet.")); return; } p += 2; /* Skip address and control */ GETSHORT(protocol, p); len -= PPP_HDRLEN; /* * Toss all non-LCP packets unless LCP is OPEN. */ if (protocol != PPP_LCP && lcp_fsm[0].state != OPENED) { MAINDEBUG(("get_input: Received non-LCP packet when LCP not open.")); return; } /* * Until we get past the authentication phase, toss all packets * except LCP, LQR and authentication packets. */ if (phase <= PHASE_AUTHENTICATE && !(protocol == PPP_LCP || protocol == PPP_LQR || protocol == PPP_PAP || protocol == PPP_CHAP)) { MAINDEBUG(("get_input: discarding proto 0x%x in phase %d", protocol, phase)); return; } /* * Upcall the proper protocol input routine. */ for (i = 0; (protp = protocols[i]) != NULL; ++i) { if (protp->protocol == protocol && protp->enabled_flag) { (*protp->input)(0, p, len); return; } if (protocol == (protp->protocol & ~0x8000) && protp->enabled_flag && protp->datainput != NULL) { (*protp->datainput)(0, p, len); return; } } if (debug) { const char *pname = protocol_name(protocol); if (pname != NULL) warn("Unsupported protocol '%s' (0x%x) received", pname, protocol); else warn("Unsupported protocol 0x%x received", protocol); } lcp_sprotrej(0, p - PPP_HDRLEN, len + PPP_HDRLEN);}/* * new_phase - signal the start of a new phase of pppd's operation. */voidnew_phase(p) int p;{ phase = p; if (new_phase_hook) (*new_phase_hook)(p);}/* * die - clean up state and exit with the specified status. */voiddie(status) int status;{ cleanup(); syslog(LOG_INFO, "Exit."); exit(status);}/* * cleanup - restore anything which needs to be restored before we exit *//* ARGSUSED */static voidcleanup(){ sys_cleanup(); if (fd_ppp >= 0) disestablish_ppp(ttyfd); if (real_ttyfd >= 0) close_tty(); 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) warn("unable to delete pid file %s: %m", linkpidfile); linkpidfile[0] = 0; if (locked) unlock();}/* * close_tty - restore the terminal device and close it. */static voidclose_tty(){ /* drop dtr to hang up */ if (!default_device && modem) { setdtr(real_ttyfd, 0); /* * This sleep is in case the serial port has CLOCAL set by default, * and consequently will reassert DTR when we close the device. */ sleep(1); } restore_tty(real_ttyfd); if (tty_mode != (mode_t) -1) { if (fchmod(real_ttyfd, tty_mode) != 0) { /* XXX if devnam is a symlink, this will change the link */ chmod(devnam, tty_mode); } } close(real_ttyfd); real_ttyfd = -1;}/* * update_link_stats - get stats at link termination. */voidupdate_link_stats(u) int u;{ struct timeval now; char numbuf[32]; if (!get_ppp_stats(u, &link_stats) || gettimeofday(&now, NULL) < 0) return; link_connect_time = now.tv_sec - start_time.tv_sec; link_stats_valid = 1; slprintf(numbuf, sizeof(numbuf), "%d", link_connect_time); script_setenv("CONNECT_TIME", numbuf); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_out); script_setenv("BYTES_SENT", numbuf); slprintf(numbuf, sizeof(numbuf), "%d", link_stats.bytes_in); script_setenv("BYTES_RCVD", numbuf);}struct callout { struct timeval c_time; /* time at which to call routine */ void *c_arg; /* argument to routine */ void (*c_func) __P((void *)); /* routine */ struct callout *c_next;};static struct callout *callout = NULL; /* Callout list */static struct timeval timenow; /* Current time *//* * timeout - Schedule a timeout. * * Note that this timeout takes the number of seconds, NOT hz (as in * the kernel). */voidtimeout(func, arg, time) void (*func) __P((void *)); void *arg; int time;{ struct callout *newp, *p, **pp; MAINDEBUG(("Timeout %p:%p in %d seconds.", func, arg, time)); /* * Allocate timeout. */ if ((newp = (struct callout *) malloc(sizeof(struct callout))) == NULL) fatal("Out of memory in timeout()!"); newp->c_arg = arg; newp->c_func = func; gettimeofday(&timenow, NULL); newp->c_time.tv_sec = timenow.tv_sec + time; newp->c_time.tv_usec = timenow.tv_usec; /* * Find correct place and link it in. */ for (pp = &callout; (p = *pp); pp = &p->c_next) if (newp->c_time.tv_sec < p->c_time.tv_sec || (newp->c_time.tv_sec == p->c_time.tv_sec && newp->c_time.tv_usec < p->c_time.tv_usec)) break; newp->c_next = p; *pp = newp;}/* * untimeout - Unschedule a timeout. */voiduntimeout(func, arg) void (*func) __P((void *)); void *arg;{ struct callout **copp, *freep; MAINDEBUG(("Untimeout %p:%p.", func, arg)); /* * Find first matching timeout and remove it from the list. */ for (copp = &callout; (freep = *copp); copp = &freep->c_next) if (freep->c_func == func && freep->c_arg == arg) { *copp = freep->c_next; free((char *) freep); break; }}/* * calltimeout - Call any timeout routines which are now due. */static voidcalltimeout(){ struct callout *p; while (callout != NULL) { p = callout; if (gettimeofday(&timenow, NULL) < 0) fatal("Failed to get time of day: %m"); if (!(p->c_time.tv_sec < timenow.tv_sec || (p->c_time.tv_sec == timenow.tv_sec && p->c_time.tv_usec <= timenow.tv_usec))) break; /* no, it's not time yet */ callout = p->c_next; (*p->c_func)(p->c_arg); free((char *) p); }}/* * timeleft - return the length of time until the next timeout is due. */static struct timeval *timeleft(tvp) struct timeval *tvp;{ if (callout == NULL) return NULL; gettimeofday(&timenow, NULL); tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec; tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec; if (tvp->tv_usec < 0) { tvp->tv_usec += 1000000; tvp->tv_sec -= 1; } if (tvp->tv_sec < 0) tvp->tv_sec = tvp->tv_usec = 0; return tvp;}/* * kill_my_pg - send a signal to our process group, and ignore it ourselves. */static voidkill_my_pg(sig) int sig;{ struct sigaction act, oldact; act.sa_handler = SIG_IGN; act.sa_flags = 0; kill(0, sig); sigaction(sig, &act, &oldact); sigaction(sig, &oldact, NULL);}/* * hup - Catch SIGHUP signal. * * Indicates that the physical layer has been disconnected. * We don't rely on this indication; if the user has sent this * signal, we just take the link down. */static voidhup(sig) int sig;{ info("Hangup (SIGHUP)"); kill_link = 1; if (status != EXIT_HANGUP) status = EXIT_USER_REQUEST; if (conn_running) /* Send the signal to the [dis]connector process(es) also */ kill_my_pg(sig); if (charshunt_pid) kill(charshunt_pid, sig); if (waiting) siglongjmp(sigjmp, 1);}/* * term - Catch SIGTERM signal and SIGINT signal (^C/del). * * Indicates that we should initiate a graceful disconnect and exit. *//*ARGSUSED*/static voidterm(sig) int sig;{ info("Terminating on signal %d.", sig); persist = 0; /* don't try to restart */ kill_link = 1; status = EXIT_USER_REQUEST; if (conn_running) /* Send the signal to the [dis]connector process(es) also */ kill_my_pg(sig); if (charshunt_pid) kill(charshunt_pid, sig); if (waiting) siglongjmp(sigjmp, 1);}/* * chld - Catch SIGCHLD signal. * Sets a flag so we will call reap_kids in the mainline. */static voidchld(sig) int sig;{ got_sigchld = 1; if (waiting) siglongjmp(sigjmp, 1);}/* * toggle_debug - Catch SIGUSR1 signal. * * Toggle debug flag. *//*ARGSUSED*/static voidtoggle_debug(sig) int sig;{ debug = !debug; if (debug) { setlogmask(LOG_UPTO(LOG_DEBUG)); } else { setlogmask(LOG_UPTO(LOG_WARNING)); }}/* * open_ccp - Catch SIGUSR2 signal. * * Try to (re)negotiate compression. *//*ARGSUSED*/static voidopen_ccp(sig) int sig;{ open_ccp_flag = 1; if (waiting) siglongjmp(sigjmp, 1);}/* * bad_signal - We've caught a fatal signal. Clean up state and exit. */static voidbad_signal(sig) int sig;{ static int crashed = 0; if (crashed) _exit(127); crashed = 1; error("Fatal signal %d", sig); if (conn_running) kill_my_pg(SIGTERM); if (charshunt_pid) kill(charshunt_pid, SIGTERM); die(127);}/* * device_script - run a program to talk to the serial device * (e.g. to run the connector or disconnector script). */static intdevice_script(program, in, out, dont_wait) char *program; int in, out; int dont_wait;{ int pid; int status = -1; int errfd; ++conn_running; pid = fork(); if (pid < 0) { --conn_running; error("Failed to create child process: %m"); return -1; } if (pid == 0) { sys_close(); closelog(); if (in == 2) { /* aargh!!! */ int newin = dup(in); if (in == out) out = newin; in = newin; } else if (out == 2) { out = dup(out); } if (log_to_fd >= 0) { if (log_to_fd != 2) dup2(log_to_fd, 2); } else { close(2); errfd = open(_PATH_CONNERRS, O_WRONLY | O_APPEND | O_CREAT, 0600); if (errfd >= 0 && errfd != 2) { dup2(errfd, 2); close(errfd); } } if (in != 0) { if (out == 0) out = dup(out); dup2(in, 0); } if (out != 1) { dup2(out, 1); } if (real_ttyfd > 2) close(real_ttyfd); if (pty_master > 2) close(pty_master); if (pty_slave > 2) close(pty_slave); setuid(uid); if (getuid() != uid) { error("setuid failed"); exit(1); } setgid(getgid()); execl("/bin/sh", "sh", "-c", program, (char *)0); error("could not exec /bin/sh: %m"); exit(99); /* NOTREACHED */ } if (dont_wait) { record_child(pid, program, NULL, NULL); status = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -