📄 ppp_tty.c
字号:
splx(s);}/* * This gets called when a received packet is placed on * the inq. */static voidpppasyncctlp(sc) struct ppp_softc *sc;{ struct tty *tp; /* Put a placeholder byte in canq for ttselect()/ttnread(). */ tp = (struct tty *) sc->sc_devp; putc(0, &tp->t_canq); ttwakeup(tp);}/* * Start output on async tty interface. Get another datagram * to send from the interface queue and start sending it. */voidpppstart(tp) register struct tty *tp;{ register struct ppp_softc *sc = (struct ppp_softc *) tp->t_sc; register NETBUF_T m; register int len; register u_char *start, *stop, *cp; int n, ndone, done, idle; struct nty *np = ttynty(tp);#ifdef NEW_CLOCAL if ((tp->t_state & TS_CARR_ON) == 0 && (np->t_pflags & TP_CLOCAL) == 0#else if ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_flags & CLOCAL) == 0 #endif /* NEW_CLOCAL */ || sc == NULL || tp != (struct tty *) sc->sc_devp) { if (tp->t_oproc != NULL) (*tp->t_oproc)(tp); return; } idle = 0;#ifdef OLD_MUX while (CCOUNT(&tp->t_outq) == 0) {#else while (CCOUNT(&tp->t_outq) < PPP_HIWAT) {#endif /* * See if we have an existing packet partly sent. * If not, get a new packet and start sending it. */ m = sc->sc_outm; if (m == NULL) { /* * Get another packet to be sent. */ m = ppp_dequeue(sc); if (m == NULL) { idle = 1; break; } /* * The extra PPP_FLAG will start up a new packet, and thus * will flush any accumulated garbage. We do this whenever * the line may have been idle for some time. */ if (CCOUNT(&tp->t_outq) == 0) { ++sc->sc_bytessent; (void) putc(PPP_FLAG, &tp->t_outq); } /* Calculate the FCS for the first netbuf's worth. */ sc->sc_outfcs = pppfcs(PPP_INITFCS, mtod(m, u_char *), NB_SIZE(m)); sc->sc_outfcs ^= 0xffff; cp = mtod(m, u_char *) + NB_SIZE(m); NB_GROW_BOT(m, PPP_FCSLEN); *cp++ = sc->sc_outfcs & 0xFF; *cp++ = (sc->sc_outfcs >> 8) & 0xFF; } start = mtod(m, u_char *); len = NB_SIZE(m); stop = start + len; while (len > 0) { /* * Find out how many bytes in the string we can * handle without doing something special. */ for (cp = start; cp < stop; cp++) if (ESCAPE_P(*cp)) break; n = cp - start; if (n) { /* * b_to_q returns the number of characters * _not_ sent * * NetBSD (0.9 or later), 4.3-Reno or similar. */ ndone = n - b_to_q(start, n, &tp->t_outq); len -= ndone; start += ndone; sc->sc_bytessent += ndone; if (ndone < n) break; /* packet doesn't fit */ } /* * If there are characters left in the netbuf, * the first one must be special.. * Put it out in a different form. */ if (len) { if (putc(PPP_ESCAPE, &tp->t_outq)) break; if (putc(*start ^ PPP_TRANS, &tp->t_outq)) { (void) unputc(&tp->t_outq); break; } sc->sc_bytessent += 2; start++; len--; } } /* * If we didn't empty this netbuf, remember where we're up to. */ done = len == 0; if (!done) { /* remember where we got to */ NB_SHRINK_TOP(m, start - mtod(m, u_char *)); break; /* can't do any more at the moment */ } /* * Output trailing PPP flag and finish packet. * We make the length zero in case the flag * cannot be output immediately. */ NB_SHRINK_TOP(m, NB_SIZE(m)); if (putc(PPP_FLAG, &tp->t_outq)) break; sc->sc_bytessent++; /* Finished with this netbuf; free it and move on. */ NB_FREE(m); m = NULL; incr_cnt(sc->sc_if, if_opackets); sc->sc_outm = m; } /* * If there is stuff in the output queue, send it now. * We are being called in lieu of ttstart and must do what it would. */ if (tp->t_oproc != NULL) (*tp->t_oproc)(tp); /* * This timeout is needed for operation on a pseudo-tty, * because the pty code doesn't call pppstart after it has * drained the t_outq. */ if (!idle && (sc->sc_flags & SC_TIMEOUT) == 0) {#if NS_TARGET >= 40 timeout(ppp_timeout, (void *) sc, 1);#else ns_timeout(ppp_timeout, (void *) sc, 1 * (1000000000L / HZ), CALLOUT_PRI_SOFTINT0);#endif /*NS_TARGET */ sc->sc_flags |= SC_TIMEOUT; } return;}/* * Timeout routine - try to start some more output. */static intppp_timeout(x) void *x;{ struct ppp_softc *sc = (struct ppp_softc *) x; struct tty *tp = (struct tty *) sc->sc_devp; int s; s = splimp(); sc->sc_flags &= ~SC_TIMEOUT; pppstart(tp); splx(s); return 0;}/* * Allocate enough netbuf to handle current MRU. * * Warning Will Robinson: pppgetm() can get called at interrupt-level! */voidpppgetm(sc) register struct ppp_softc *sc;{ int s; s = splimp(); /* * When the MRU is being changed, we could conceivably end up * nuking a packet being received, but I doubt it, since the * hand-shake is lock-step (ie. single packet). */ if (sc->sc_m != NULL) NB_FREE(sc->sc_m); sc->sc_m = nbq_dequeue(&sc->sc_freeq); splx(s);}/* * 4.3 says this is an unused function. However, * it appears to be returning a NULL terminated string * of several characters. My guess is that the serial * driver is doing a little buffering so that we don't * get burdend with interrupts. * * This function gets called when you use the NeXT * supplied serial drivers. It does not get called * with the MuX driver. * * In order to expedite the work done here, we * handle most things here that don't require * processing of a PPP_FLAG. * */voidppprend(cp, n, tp) unsigned char *cp; int n; struct tty *tp;{#ifndef OPTIMIZE_PPPREND #warning PPPREND Not optimized!!! while (n--) pppinput((u_char) *cp++, tp);#else register struct ppp_softc *sc = (struct ppp_softc *)tp->t_sc; register int ret; if (sc == NULL || tp != (struct tty *) sc->sc_devp) { printf("Warning, bad softc structure at %s %d\n", __FILE__, __LINE__); return; } /* * We can handle FLUSHs, ESCAPES, and non PPP_FLAG characters */ while (n) { if (sc->sc_flags & SC_FLUSH) { do { if (*(cp++) == PPP_FLAG) { pppinput(PPP_FLAG, tp); --n; break; } else if (sc->sc_flags & SC_LOG_FLUSH) ppplogchar(sc, *cp); } while(--n); } else if (sc->sc_ilen > 3 && (NB_SIZE(sc->sc_m) - sc->sc_ilen) > n && *cp != PPP_FLAG && *cp != PPP_ESCAPE) /* Dont really handle escapes properly...should */ { unsigned char* cp1 = cp; if (sc->sc_flags & SC_ESCAPED) { sc->sc_flags &= ~SC_ESCAPED; *cp ^= PPP_TRANS; } do { sc->sc_fcs = PPP_FCS(sc->sc_fcs, *(cp++)); if (sc->sc_flags & SC_LOG_RAWIN) ppplogchar(sc, *cp); } while(--n && *cp != PPP_FLAG && *cp != PPP_ESCAPE); bcopy(cp1, sc->sc_mp, (cp-cp1)); sc->sc_bytesrcvd += (cp - cp1); sc->sc_ilen += (cp-cp1); sc->sc_mp += (cp-cp1); } else { --n; pppinput(*(cp++), tp); } } #endif /* OPTIMIZE_PPPREND */}/* * tty interface receiver interrupt. */static const unsigned paritytab[8] = { 0x96696996, 0x69969669, 0x69969669, 0x96696996, 0x69969669, 0x96696996, 0x96696996, 0x69969669};voidpppinput(c, tp) int c; register struct tty *tp;{ register struct ppp_softc *sc; NETBUF_T m; int ilen, s; sc = (struct ppp_softc *) tp->t_sc; if (sc == NULL || tp != (struct tty *) sc->sc_devp) return; ++tk_nin; ++sc->sc_bytesrcvd; if (c & TTY_FE) { /* framing error or overrun on this char - abort packet */ IOLogDbg("ppp%d: bad char 0x%x\n", if_unit(sc->sc_if), c); goto flush; } c &= 0xff; if (c & 0x80) sc->sc_flags |= SC_RCV_B7_1; else sc->sc_flags |= SC_RCV_B7_0; if (paritytab[c >> 5] & (1 << (c & 0x1F))) sc->sc_flags |= SC_RCV_ODDP; else sc->sc_flags |= SC_RCV_EVNP; if (sc->sc_flags & SC_LOG_RAWIN) ppplogchar(sc, c); if (c == PPP_FLAG) { if (sc->sc_ilen == 0) return; ilen = sc->sc_ilen; sc->sc_ilen = 0; if (sc->sc_rawin_count > 0) ppplogchar(sc, -1); /* * From the RFC: * Each Control Escape octet is also * removed, and the following octet is exclusive-or'd with hexadecimal * 0x20, unless it is the Flag Sequence (which aborts a frame). * * So, if SC_ESCAPED is set, then we've seen the packet * abort sequence "}~". */ if ((sc->sc_flags & (SC_FLUSH | SC_ESCAPED)) || ((ilen > 0) && (sc->sc_fcs != PPP_GOODFCS))) { sc->sc_flags |= SC_PKTLOST; /* note the dropped packet */ if ((sc->sc_flags & (SC_FLUSH | SC_ESCAPED)) == 0) { IOLog("ppp%d: bad fcs 0x%04x\n", if_unit(sc->sc_if), sc->sc_fcs); incr_cnt(sc->sc_if, if_ierrors); } else { IOLog("ppp%d: bad packet flushed...\n", if_unit(sc->sc_if)); sc->sc_flags &= ~(SC_FLUSH | SC_ESCAPED); } return; } if (ilen < (PPP_HDRLEN + PPP_FCSLEN)) { if (ilen) { IOLogDbg("ppp%d: too short (%d)\n", if_unit(sc->sc_if), ilen); incr_cnt(sc->sc_if, if_ierrors); sc->sc_flags |= SC_PKTLOST; } return; } /* * Remove FCS trailer. Set packet length... */ ilen -= PPP_FCSLEN; NB_SHRINK_BOT(sc->sc_m, NB_SIZE(sc->sc_m) - ilen); /* excise this netbuf */ m = sc->sc_m; sc->sc_m = NULL; ppppktin(sc, m, sc->sc_flags & SC_PKTLOST); sc->sc_flags &= ~SC_PKTLOST; pppgetm(sc); return; } if (sc->sc_flags & SC_FLUSH) { if (sc->sc_flags & SC_LOG_FLUSH) ppplogchar(sc, c); return; }/* * From the RFC: * On reception, prior to FCS computation, each octet with value less * than hexadecimal 0x20 is checked. If it is flagged in the receiving * ACCM, it is simply removed (it may have been inserted by intervening * data communications equipment). Each Control Escape octet is also * removed, and the following octet is exclusive-or'd with hexadecimal * 0x20, unless it is the Flag Sequence (which aborts a frame). */ if (c < 0x20 && (sc->sc_rasyncmap & (1 << c))) { return; } if (sc->sc_flags & SC_ESCAPED) { sc->sc_flags &= ~SC_ESCAPED; c ^= PPP_TRANS; } else if (c == PPP_ESCAPE) { sc->sc_flags |= SC_ESCAPED; return; } /* * Initialize buffer on first octet received. * First octet could be address or protocol (when compressing * address/control). * Second octet is control. * Third octet is first or second (when compressing protocol) * octet of protocol. * Fourth octet is second octet of protocol. */ if (sc->sc_ilen == 0) { /* reset the input netbuf */ if (sc->sc_m == NULL) { pppgetm(sc); if (sc->sc_m == NULL) { /* * We schedule a call here as pppindrain will * not get scheduled and we need the free buffers */ IOLog("ppp%d: no input netbufs!\n", if_unit(sc->sc_if)); (void)pppsched(pppfillfreeq, sc); goto flush; } } m = sc->sc_m; sc->sc_mp = mtod(m, char *); sc->sc_fcs = PPP_INITFCS; if (c != PPP_ALLSTATIONS) { if (sc->sc_flags & SC_REJ_COMP_AC) { IOLogDbg("ppp%d: garbage received: 0x%02x (need 0x%02x)\n", if_unit(sc->sc_if), c, PPP_ALLSTATIONS); goto flush; } *sc->sc_mp++ = PPP_ALLSTATIONS; *sc->sc_mp++ = PPP_UI; sc->sc_ilen += 2; } } if (sc->sc_ilen == 1 && c != PPP_UI) { IOLogDbg("ppp%d: missing UI (0x%02x), got 0x%02x\n", if_unit(sc->sc_if), PPP_UI, c); goto flush; } if (sc->sc_ilen == 2 && (c & 1) == 1) { /* a compressed protocol */ *sc->sc_mp++ = 0; sc->sc_ilen++; } if (sc->sc_ilen == 3 && (c & 1) == 0) { IOLogDbg("ppp%d: bad protocol %x\n", if_unit(sc->sc_if), (sc->sc_mp[-1] << 8) + c); goto flush; } /* packet beyond configured mru? */ if (++sc->sc_ilen > sc->sc_mru + PPP_HDRLEN + PPP_FCSLEN) { IOLogDbg("ppp%d: packet too big (%d bytes)\n", if_unit(sc->sc_if), sc->sc_ilen); goto flush; } /* ilen was incremented above... */ *sc->sc_mp++ = c; sc->sc_fcs = PPP_FCS(sc->sc_fcs, c); return; flush: if (!(sc->sc_flags & SC_FLUSH)) { incr_cnt(sc->sc_if, if_ierrors); sc->sc_flags |= SC_FLUSH; if (sc->sc_flags & SC_LOG_FLUSH) ppplogchar(sc, c); } return;}intinstall_ppp_ld(void){ return tty_ld_install(PPPDISC, NORMAL_LDISC, pppopen, pppclose, pppread, pppwrite, ppptioctl, pppinput, ppprend, pppstart, ttymodem, ttselect);}#define MAX_DUMP_BYTES 128voidppplogchar(sc, c) struct ppp_softc *sc; int c;{ if (c >= 0) sc->sc_rawin[sc->sc_rawin_count++] = c; if (sc->sc_rawin_count >= sizeof(sc->sc_rawin) || c < 0 && sc->sc_rawin_count > 0) { IOLog("ppp%d input:\n", if_unit(sc->sc_if)); pppdumpb(sc->sc_rawin, sc->sc_rawin_count); sc->sc_rawin_count = 0; }}static voidpppdumpb(b, l) u_char *b; int l;{ char buf[3*MAX_DUMP_BYTES+4]; char *bp = buf; static char digits[] = "0123456789abcdef"; while (l--) { if (bp >= buf + sizeof(buf) - 3) { *bp++ = '>'; break; } *bp++ = digits[*b >> 4]; /* convert byte to ascii hex */ *bp++ = digits[*b++ & 0xf]; *bp++ = ' '; } *bp = 0; IOLog("%s\n", buf);}#endif /* NUM_PPP > 0 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -