⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ppp_tty.c

📁 unix and linux net driver
💻 C
📖 第 1 页 / 共 2 页
字号:
		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_stats.ppp_obytes;		(void) putc(PPP_FLAG, &tp->t_outq);	    }	    /* Calculate the FCS for the first mbuf's worth. */	    sc->sc_outfcs = pppfcs(PPP_INITFCS, mtod(m, u_char *), m->m_len);	    sc->sc_if.if_lastchange = time;	}	for (;;) {	    start = mtod(m, u_char *);	    len = m->m_len;	    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) {		    /* 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_stats.ppp_obytes += ndone;		    if (ndone < n)			break;	/* packet doesn't fit */		}		/*		 * If there are characters left in the mbuf,		 * the first one must be special.		 * Put it out in a different form.		 */		if (len) {		    s = spltty();		    if (putc(PPP_ESCAPE, &tp->t_outq))			break;		    if (putc(*start ^ PPP_TRANS, &tp->t_outq)) {			(void) unputc(&tp->t_outq);			splx(s);			break;		    }		    splx(s);		    sc->sc_stats.ppp_obytes += 2;		    start++;		    len--;		}	    }	    /*	     * If we didn't empty this mbuf, remember where we're up to.	     * If we emptied the last mbuf, try to add the FCS and closing	     * flag, and if we can't, leave sc_outm pointing to m, but with	     * m->m_len == 0, to remind us to output the FCS and flag later.	     */	    done = len == 0;	    if (done && m->m_next == NULL) {		u_char *p, *q;		int c;		u_char endseq[8];		/*		 * We may have to escape the bytes in the FCS.		 */		p = endseq;		c = ~sc->sc_outfcs & 0xFF;		if (ESCAPE_P(c)) {		    *p++ = PPP_ESCAPE;		    *p++ = c ^ PPP_TRANS;		} else		    *p++ = c;		c = (~sc->sc_outfcs >> 8) & 0xFF;		if (ESCAPE_P(c)) {		    *p++ = PPP_ESCAPE;		    *p++ = c ^ PPP_TRANS;		} else		    *p++ = c;		*p++ = PPP_FLAG;		/*		 * Try to output the FCS and flag.  If the bytes		 * don't all fit, back out.		 */		s = spltty();		for (q = endseq; q < p; ++q)		    if (putc(*q, &tp->t_outq)) {			done = 0;			for (; q > endseq; --q)			    unputc(&tp->t_outq);			break;		    }		splx(s);		if (done)		    sc->sc_stats.ppp_obytes += q - endseq;	    }	    if (!done) {		/* remember where we got to */		m->m_data = start;		m->m_len = len;		break;	    }	    /* Finished with this mbuf; free it and move on. */	    MFREE(m, m2);	    m = m2;	    if (m == NULL) {		/* Finished a packet */		break;	    }	    sc->sc_outfcs = pppfcs(sc->sc_outfcs, mtod(m, u_char *), m->m_len);	}	/*	 * If m == NULL, we have finished a packet.	 * If m != NULL, we've either done as much work this time	 * as we need to, or else we've filled up the output queue.	 */	sc->sc_outm = m;	if (m)	    break;    }    /* Call pppstart to start output again if necessary. */    s = spltty();    pppstart(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) {	timeout(ppp_timeout, (void *) sc, 1);	sc->sc_flags |= SC_TIMEOUT;    }    splx(s);}/* * This gets called when a received packet is placed on * the inq, at splsoftnet. */static voidpppasyncctlp(sc)    struct ppp_softc *sc;{    struct tty *tp;    int s;    /* Put a placeholder byte in canq for ttselect()/ttnread(). */    s = spltty();    tp = (struct tty *) sc->sc_devp;    putc(0, &tp->t_canq);    ttwakeup(tp);    splx(s);}/* * Start output on async tty interface.  If the transmit queue * has drained sufficiently, arrange for pppasyncstart to be * called later at splsoftnet. * Called at spltty or higher. */intpppstart(tp)    register struct tty *tp;{    register struct ppp_softc *sc = (struct ppp_softc *) tp->t_sc;    /*     * 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);    /*     * If the transmit queue has drained and the tty has not hung up     * or been disconnected from the ppp unit, then tell if_ppp.c that     * we need more output.     */    if (CCOUNT(&tp->t_outq) < PPP_LOWAT	&& !((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0)	&& sc != NULL && tp == (struct tty *) sc->sc_devp) {	ppp_restart(sc);    }    return 0;}/* * Timeout routine - try to start some more output. */static voidppp_timeout(x)    void *x;{    struct ppp_softc *sc = (struct ppp_softc *) x;    struct tty *tp = (struct tty *) sc->sc_devp;    int s;    s = spltty();    sc->sc_flags &= ~SC_TIMEOUT;    pppstart(tp);    splx(s);}/* * Allocate enough mbuf to handle current MRU. */static voidpppgetm(sc)    register struct ppp_softc *sc;{    struct mbuf *m, **mp;    int len;    mp = &sc->sc_m;    for (len = sc->sc_mru + PPP_HDRLEN + PPP_FCSLEN; len > 0; ){	if ((m = *mp) == NULL) {	    MGETHDR(m, M_DONTWAIT, MT_DATA);	    if (m == NULL)		break;	    *mp = m;	    MCLGET(m, M_DONTWAIT);	}	len -= M_DATASIZE(m);	mp = &m->m_next;    }}/* * tty interface receiver interrupt. */static unsigned paritytab[8] = {    0x96696996, 0x69969669, 0x69969669, 0x96696996,    0x69969669, 0x96696996, 0x96696996, 0x69969669};intpppinput(c, tp)    int c;    register struct tty *tp;{    register struct ppp_softc *sc;    struct mbuf *m;    int ilen, s;    sc = (struct ppp_softc *) tp->t_sc;    if (sc == NULL || tp != (struct tty *) sc->sc_devp)	return 0;    ++tk_nin;    ++sc->sc_stats.ppp_ibytes;    if (c & TTY_FE) {	/* framing error or overrun on this char - abort packet */	if (sc->sc_flags & SC_DEBUG)	    printf("ppp%d: bad char %x\n", sc->sc_if.if_unit, c);	goto flush;    }    c &= 0xff;    /*     * Handle software flow control of output.     */    if (tp->t_iflag & IXON) {	if (c == tp->t_cc[VSTOP] && tp->t_cc[VSTOP] != _POSIX_VDISABLE) {	    if ((tp->t_state & TS_TTSTOP) == 0) {		tp->t_state |= TS_TTSTOP;		(*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);	    }	    return 0;	}	if (c == tp->t_cc[VSTART] && tp->t_cc[VSTART] != _POSIX_VDISABLE) {	    tp->t_state &= ~TS_TTSTOP;	    if (tp->t_oproc != NULL)		(*tp->t_oproc)(tp);	    return 0;	}    }    s = spltty();    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;    splx(s);    if (sc->sc_flags & SC_LOG_RAWIN)	ppplogchar(sc, c);    if (c == PPP_FLAG) {	ilen = sc->sc_ilen;	sc->sc_ilen = 0;	if (sc->sc_rawin_count > 0) 	    ppplogchar(sc, -1);	/*	 * 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)) {	    s = spltty();	    sc->sc_flags |= SC_PKTLOST;	/* note the dropped packet */	    if ((sc->sc_flags & (SC_FLUSH | SC_ESCAPED)) == 0){		if (sc->sc_flags & SC_DEBUG)		    printf("ppp%d: bad fcs %x, pkt len %d\n",			   sc->sc_if.if_unit, sc->sc_fcs, ilen);		sc->sc_if.if_ierrors++;		sc->sc_stats.ppp_ierrors++;	    } else		sc->sc_flags &= ~(SC_FLUSH | SC_ESCAPED);	    splx(s);	    return 0;	}	if (ilen < PPP_HDRLEN + PPP_FCSLEN) {	    if (ilen) {		if (sc->sc_flags & SC_DEBUG)		    printf("ppp%d: too short (%d)\n", sc->sc_if.if_unit, ilen);		s = spltty();		sc->sc_if.if_ierrors++;		sc->sc_stats.ppp_ierrors++;		sc->sc_flags |= SC_PKTLOST;		splx(s);	    }	    return 0;	}	/*	 * Remove FCS trailer.  Somewhat painful...	 */	ilen -= 2;	if (--sc->sc_mc->m_len == 0) {	    for (m = sc->sc_m; m->m_next != sc->sc_mc; m = m->m_next)		;	    sc->sc_mc = m;	}	sc->sc_mc->m_len--;	/* excise this mbuf chain */	m = sc->sc_m;	sc->sc_m = sc->sc_mc->m_next;	sc->sc_mc->m_next = NULL;	ppppktin(sc, m, sc->sc_flags & SC_PKTLOST);	if (sc->sc_flags & SC_PKTLOST) {	    s = spltty();	    sc->sc_flags &= ~SC_PKTLOST;	    splx(s);	}	pppgetm(sc);	return 0;    }    if (sc->sc_flags & SC_FLUSH) {	if (sc->sc_flags & SC_LOG_FLUSH)	    ppplogchar(sc, c);	return 0;    }    if (c < 0x20 && (sc->sc_rasyncmap & (1 << c)))	return 0;    s = spltty();    if (sc->sc_flags & SC_ESCAPED) {	sc->sc_flags &= ~SC_ESCAPED;	c ^= PPP_TRANS;    } else if (c == PPP_ESCAPE) {	sc->sc_flags |= SC_ESCAPED;	splx(s);	return 0;    }    splx(s);    /*     * 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 first input mbuf */	if (sc->sc_m == NULL) {	    pppgetm(sc);	    if (sc->sc_m == NULL) {		if (sc->sc_flags & SC_DEBUG)		    printf("ppp%d: no input mbufs!\n", sc->sc_if.if_unit);		goto flush;	    }	}	m = sc->sc_m;	m->m_len = 0;	m->m_data = M_DATASTART(sc->sc_m);	sc->sc_mc = m;	sc->sc_mp = mtod(m, char *);	sc->sc_fcs = PPP_INITFCS;	if (c != PPP_ALLSTATIONS) {	    if (sc->sc_flags & SC_REJ_COMP_AC) {		if (sc->sc_flags & SC_DEBUG)		    printf("ppp%d: garbage received: 0x%x (need 0xFF)\n",			   sc->sc_if.if_unit, c);		goto flush;	    }	    *sc->sc_mp++ = PPP_ALLSTATIONS;	    *sc->sc_mp++ = PPP_UI;	    sc->sc_ilen += 2;	    m->m_len += 2;	}    }    if (sc->sc_ilen == 1 && c != PPP_UI) {	if (sc->sc_flags & SC_DEBUG)	    printf("ppp%d: missing UI (0x3), got 0x%x\n",		   sc->sc_if.if_unit, c);	goto flush;    }    if (sc->sc_ilen == 2 && (c & 1) == 1) {	/* a compressed protocol */	*sc->sc_mp++ = 0;	sc->sc_ilen++;	sc->sc_mc->m_len++;    }    if (sc->sc_ilen == 3 && (c & 1) == 0) {	if (sc->sc_flags & SC_DEBUG)	    printf("ppp%d: bad protocol %x\n", sc->sc_if.if_unit,		   (sc->sc_mp[-1] << 8) + c);	goto flush;    }    /* packet beyond configured mru? */    if (++sc->sc_ilen > sc->sc_mru + PPP_HDRLEN + PPP_FCSLEN) {	if (sc->sc_flags & SC_DEBUG)	    printf("ppp%d: packet too big\n", sc->sc_if.if_unit);	goto flush;    }    /* is this mbuf full? */    m = sc->sc_mc;    if (M_TRAILINGSPACE(m) <= 0) {	if (m->m_next == NULL) {	    pppgetm(sc);	    if (m->m_next == NULL) {		if (sc->sc_flags & SC_DEBUG)		    printf("ppp%d: too few input mbufs!\n", sc->sc_if.if_unit);		goto flush;	    }	}	sc->sc_mc = m = m->m_next;	m->m_len = 0;	m->m_data = M_DATASTART(m);	sc->sc_mp = mtod(m, char *);    }    ++m->m_len;    *sc->sc_mp++ = c;    sc->sc_fcs = PPP_FCS(sc->sc_fcs, c);    return 0; flush:    if (!(sc->sc_flags & SC_FLUSH)) {	s = spltty();	sc->sc_if.if_ierrors++;	sc->sc_stats.ppp_ierrors++;	sc->sc_flags |= SC_FLUSH;	splx(s);	if (sc->sc_flags & SC_LOG_FLUSH)	    ppplogchar(sc, c);    }    return 0;}#define MAX_DUMP_BYTES	128static voidppplogchar(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)) {	printf("ppp%d input: ", sc->sc_if.if_unit);	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;    printf("%s\n", buf);}#endif	/* NPPP > 0 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -