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

📄 ppp.c

📁 ppp协议的lwip源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
        pc->vjEnabled = 0;
        vj_compress_init(&pc->vjComp);
#endif

        /* 
         * Default the in and out accm so that escape and flag characters
         * are always escaped. 
         */
        memset(pc->inACCM, 0, sizeof(ext_accm));
        pc->inACCM[15] = 0x60;
        memset(pc->outACCM, 0, sizeof(ext_accm));
        pc->outACCM[15] = 0x60;

		pc->linkStatusCB = linkStatusCB;
		pc->linkStatusArg = linkStatusArg;

		sys_thread_new(pppMain, (void*)pd);

		if(!linkStatusCB) {
        	while(pd >= 0 && !pc->if_up) {
            	ppp_msleep(500);
            	if (lcp_phase[pd] == PHASE_DEAD) {
                	pppClose(pd);
                	if (pc->errCode)
                    	pd = pc->errCode;
                	else
                    	pd = PPPERR_CONNECT;
            	}
//            else if (buttonStatus() == YESNOBUTTON) {
//                pppClose(pd);
//                pd = PPPERR_USER;
//            }
        	}
		}
    }
    return pd;
}

/* Close a PPP connection and release the descriptor. 
 * Any outstanding packets in the queues are dropped.
 * Return 0 on success, an error code on failure. */
int pppClose(int pd)
{
    PPPControl *pc = &pppControl[pd];
    int st = 0;

    /* Disconnect */
    pc->kill_link = !0;
	pppMainWakeup(pd);
    
	if(!pc->linkStatusCB) {
    	while(st >= 0 && lcp_phase[pd] != PHASE_DEAD) {
        	ppp_msleep(500);
//        if (buttonStatus() == YESNOBUTTON)
//            st = PPPERR_USER;
			break;
    	}
	}
    return st;
}

static void nPut(PPPControl *pc, struct pbuf *nb)
{
	struct pbuf *b;
	int c;

	for(b = nb; b != NULL; b = b->next) {
	    if((c = ppp_sio_write(pc->fd, b->payload, b->len)) != b->len) {
		PPPDEBUG((LOG_WARNING,
			    "PPP nPut: incomplete ppp_sio_write(%d,, %u) = %d\n", pc->fd, b->len, c));
#ifdef LINK_STATS
		lwip_stats.link.err++;
#endif /* LINK_STATS */
		pc->lastXMit = 0; /* prepend PPP_FLAG to next packet */
		break;
	    }
	}
	pbuf_free(nb);

#ifdef LINK_STATS
	lwip_stats.link.xmit++;
#endif /* LINK_STATS */
}

/* Send a packet on the given connection. */
static err_t pppifOutput(struct netif *netif, struct pbuf *pb, struct ip_addr *ipaddr)
{
	int pd = (int)netif->state;
	u_short protocol = PPP_IP;
    PPPControl *pc = &pppControl[pd];
    u_int fcsOut = PPP_INITFCS;
    struct pbuf *headMB = NULL, *tailMB = NULL, *p;
    u_char c;

	(void)ipaddr;

    /* Validate parameters. */
    /* We let any protocol value go through - it can't hurt us
     * and the peer will just drop it if it's not accepting it. */
	if (pd < 0 || pd >= NUM_PPP || !pc->openFlag || !pb) {
        PPPDEBUG((LOG_WARNING, "pppifOutput[%d]: bad parms prot=%d pb=%p\n",
                    pd, protocol, pb));
#ifdef LINK_STATS
		lwip_stats.link.opterr++;
		lwip_stats.link.drop++;
#endif
		return ERR_ARG;
	}

    /* Check that the link is up. */
	if (lcp_phase[pd] == PHASE_DEAD) {
        PPPDEBUG((LOG_ERR, "pppifOutput[%d]: link not up\n", pd));
#ifdef LINK_STATS
		lwip_stats.link.rterr++;
		lwip_stats.link.drop++;
#endif
		return ERR_RTE;
	}

    /* Grab an output buffer. */
	headMB = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
    if (headMB == NULL) {
        PPPDEBUG((LOG_WARNING, "pppifOutput[%d]: first alloc fail\n", pd));
#ifdef LINK_STATS
		lwip_stats.link.memerr++;
		lwip_stats.link.drop++;
#endif /* LINK_STATS */
        return ERR_MEM;
    }
        
#if VJ_SUPPORT > 0
    /* 
     * Attempt Van Jacobson header compression if VJ is configured and
     * this is an IP packet. 
     */
    if (protocol == PPP_IP && pc->vjEnabled) {
        switch (vj_compress_tcp(&pc->vjComp, pb)) {
        case TYPE_IP:
            /* No change...
            protocol = PPP_IP_PROTOCOL;
             */
            break;
        case TYPE_COMPRESSED_TCP:
            protocol = PPP_VJC_COMP;
            break;
        case TYPE_UNCOMPRESSED_TCP:
            protocol = PPP_VJC_UNCOMP;
            break;
        default:
            PPPDEBUG((LOG_WARNING, "pppifOutput[%d]: bad IP packet\n", pd));
#ifdef LINK_STATS
			lwip_stats.link.proterr++;
			lwip_stats.link.drop++;
#endif
        	pbuf_free(headMB);
            return ERR_VAL;
        }
    }
#endif
        
    tailMB = headMB;
        
    /* Build the PPP header. */
    if ((ppp_jiffies() - pc->lastXMit) >= PPP_MAXIDLEFLAG)
        tailMB = pppMPutRaw(PPP_FLAG, tailMB);
    pc->lastXMit = ppp_jiffies();
    if (!pc->accomp) {
        fcsOut = PPP_FCS(fcsOut, PPP_ALLSTATIONS);
        tailMB = pppMPutC(PPP_ALLSTATIONS, &pc->outACCM, tailMB);
        fcsOut = PPP_FCS(fcsOut, PPP_UI);
        tailMB = pppMPutC(PPP_UI, &pc->outACCM, tailMB);
    }
    if (!pc->pcomp || protocol > 0xFF) {
        c = (protocol >> 8) & 0xFF;
        fcsOut = PPP_FCS(fcsOut, c);
        tailMB = pppMPutC(c, &pc->outACCM, tailMB);
    }
    c = protocol & 0xFF;
    fcsOut = PPP_FCS(fcsOut, c);
    tailMB = pppMPutC(c, &pc->outACCM, tailMB);
    
    /* Load packet. */
	for(p = pb; p; p = p->next) {
    	int n;
    	u_char *sPtr;

        sPtr = (u_char*)p->payload;
        n = p->len;
        while (n-- > 0) {
            c = *sPtr++;
            
            /* Update FCS before checking for special characters. */
            fcsOut = PPP_FCS(fcsOut, c);
            
            /* Copy to output buffer escaping special characters. */
            tailMB = pppMPutC(c, &pc->outACCM, tailMB);
        }
    }

    /* Add FCS and trailing flag. */
    c = ~fcsOut & 0xFF;
    tailMB = pppMPutC(c, &pc->outACCM, tailMB);
    c = (~fcsOut >> 8) & 0xFF;
    tailMB = pppMPutC(c, &pc->outACCM, tailMB);
    tailMB = pppMPutRaw(PPP_FLAG, tailMB);
        
    /* If we failed to complete the packet, throw it away. */
    if (!tailMB) {
        PPPDEBUG((LOG_WARNING,
                    "pppifOutput[%d]: Alloc err - dropping proto=%d\n", 
                    pd, protocol));
        pbuf_free(headMB);
#ifdef LINK_STATS
		lwip_stats.link.memerr++;
		lwip_stats.link.drop++;
#endif
        return ERR_MEM;
    }

	/* Send it. */
    PPPDEBUG((LOG_INFO, "pppifOutput[%d]: proto=0x%04X\n", pd, protocol));

    nPut(pc, headMB);

    return ERR_OK;
}

/* Get and set parameters for the given connection.
 * Return 0 on success, an error code on failure. */
int  pppIOCtl(int pd, int cmd, void *arg)
{
    PPPControl *pc = &pppControl[pd];
    int st = 0;

    if (pd < 0 || pd >= NUM_PPP)
        st = PPPERR_PARAM;
    else {
        switch(cmd) {
        case PPPCTLG_UPSTATUS:      /* Get the PPP up status. */
            if (arg) 
                *(int *)arg = (int)(pc->if_up);
            else
                st = PPPERR_PARAM;
            break;
        case PPPCTLS_ERRCODE:       /* Set the PPP error code. */
            if (arg) 
                pc->errCode = *(int *)arg;
            else
                st = PPPERR_PARAM;
            break;
        case PPPCTLG_ERRCODE:       /* Get the PPP error code. */
            if (arg) 
                *(int *)arg = (int)(pc->errCode);
            else
                st = PPPERR_PARAM;
            break;
        case PPPCTLG_FD:
            if (arg) 
                *(ppp_sio_fd_t *)arg = pc->fd;
            else
                st = PPPERR_PARAM;
            break;
        default:
            st = PPPERR_PARAM;
            break;
        }
    }
    
    return st;
}

/*
 * Return the Maximum Transmission Unit for the given PPP connection.
 */
u_int pppMTU(int pd)
{
    PPPControl *pc = &pppControl[pd];
    u_int st;
    
    /* Validate parameters. */
    if (pd < 0 || pd >= NUM_PPP || !pc->openFlag)
        st = 0;
    else
        st = pc->mtu;
        
    return st;
}

/*
 * Write n characters to a ppp link.
 *  RETURN: >= 0 Number of characters written
 *           -1 Failed to write to device
 */
int pppWrite(int pd, const u_char *s, int n)
{
    PPPControl *pc = &pppControl[pd];
    u_char c;
    u_int fcsOut = PPP_INITFCS;
    struct pbuf *headMB = NULL, *tailMB;

	headMB = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
    if (headMB == NULL) {
#ifdef LINK_STATS
		lwip_stats.link.memerr++;
		lwip_stats.link.proterr++;
#endif /* LINK_STATS */
		return PPPERR_ALLOC;
    }

    tailMB = headMB;
        
    /* If the link has been idle, we'll send a fresh flag character to
     * flush any noise. */
    if ((ppp_jiffies() - pc->lastXMit) >= PPP_MAXIDLEFLAG)
        tailMB = pppMPutRaw(PPP_FLAG, tailMB);
    pc->lastXMit = ppp_jiffies();
     
    /* Load output buffer. */
    while (n-- > 0) {
        c = *s++;
        
        /* Update FCS before checking for special characters. */
        fcsOut = PPP_FCS(fcsOut, c);
        
        /* Copy to output buffer escaping special characters. */
        tailMB = pppMPutC(c, &pc->outACCM, tailMB);
    }
    
    /* Add FCS and trailing flag. */
    c = ~fcsOut & 0xFF;
    tailMB = pppMPutC(c, &pc->outACCM, tailMB);
    c = (~fcsOut >> 8) & 0xFF;
    tailMB = pppMPutC(c, &pc->outACCM, tailMB);
    tailMB = pppMPutRaw(PPP_FLAG, tailMB);
        
    /* If we failed to complete the packet, throw it away.
     * Otherwise send it. */
    if (!tailMB) {
		PPPDEBUG((LOG_WARNING,
                "pppWrite[%d]: Alloc err - dropping nBuf len=%d\n", pd, headMB->len));
/*                "pppWrite[%d]: Alloc err - dropping %d:%.*H", pd, headMB->len, MIN(headMB->len * 2, 40), headMB->payload)); */
		pbuf_free(headMB);
#ifdef LINK_STATS
		lwip_stats.link.memerr++;
		lwip_stats.link.proterr++;
#endif /* LINK_STATS */
		return PPPERR_ALLOC;
	}

    PPPDEBUG((LOG_INFO, "pppWrite[%d]: len=%d\n", pd, headMB->len));
/*     "pppWrite[%d]: %d:%.*H", pd, headMB->len, MIN(headMB->len * 2, 40), headMB->payload)); */
    nPut(pc, headMB);

    return PPPERR_NONE;
}

/*
 * ppp_send_config - configure the transmit characteristics of
 * the ppp interface.
 */
void ppp_send_config(
    int unit, 
    int mtu,
    u32_t asyncmap,
    int pcomp, 
    int accomp
)
{
    PPPControl *pc = &pppControl[unit];
    int i;
    
    pc->mtu = mtu;
    pc->pcomp = pcomp;
    pc->accomp = accomp;
    
    /* Load the ACCM bits for the 32 control codes. */
    for (i = 0; i < 32/8; i++)
        pc->outACCM[i] = (u_char)((asyncmap >> (8 * i)) & 0xFF);
    PPPDEBUG((LOG_INFO, "ppp_send_config[%d]: outACCM=%X %X %X %X\n",
                unit,
                pc->outACCM[0], pc->outACCM[1], pc->outACCM[2], pc->outACCM[3]));
}


/*
 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
 */

⌨️ 快捷键说明

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