// // Created by xajhuang on 2022/5/10. // #include #include "pppoe_session.h" #include "netif/rawif.h" #include "netif/ppp/ppp.h" #include "netif/ppp/pppoe.h" static PPPPOE_SESSION g_pSessionList = NULL; static void pppLinkStatusCallback(ppp_pcb *pcb, int errCode, void *ctx) { struct netif *pppif = ppp_netif(pcb); struct pppoe_softc* sc = (struct pppoe_softc*)pcb->link_ctx_cb; LWIP_UNUSED_ARG(ctx); switch (errCode) { case PPPERR_NONE: { /* No error. */ dzlog_info("<%p> pppLinkStatusCallback: PPPERR_NONE(%08X), Session: %04X\n", pcb, pcb->lcp_gotoptions.magicnumber, sc->sc_session); #if LWIP_IPV4 dzlog_info(" our_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_addr(pppif))); dzlog_info(" his_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_gw(pppif))); dzlog_info(" netmask = %s\n", ip4addr_ntoa(netif_ip4_netmask(pppif))); #endif /* LWIP_IPV4 */ #if LWIP_DNS dzlog_info(" dns1 = %s\n", ipaddr_ntoa(dns_getserver(0))); dzlog_info(" dns2 = %s\n", ipaddr_ntoa(dns_getserver(1))); #endif /* LWIP_DNS */ #if PPP_IPV6_SUPPORT dzlog_info(" our6_ipaddr = %s\n", ip6addr_ntoa(netif_ip6_addr(pppif, 0))); #endif /* PPP_IPV6_SUPPORT */ break; } case PPPERR_PARAM: { /* Invalid parameter. */ printf("<%p> pppLinkStatusCallback: PPPERR_PARAM\n", pcb); break; } case PPPERR_OPEN: { /* Unable to open PPP session. */ printf("<%p> pppLinkStatusCallback: PPPERR_OPEN\n", pcb); break; } case PPPERR_DEVICE: { /* Invalid I/O device for PPP. */ printf("<%p> pppLinkStatusCallback: PPPERR_DEVICE\n", pcb); break; } case PPPERR_ALLOC: { /* Unable to allocate resources. */ printf("<%p> pppLinkStatusCallback: PPPERR_ALLOC\n", pcb); break; } case PPPERR_USER: { /* User interrupt. */ printf("<%p> pppLinkStatusCallback: PPPERR_USER\n", pcb); break; } case PPPERR_CONNECT: { /* Connection lost. */ printf("<%p> pppLinkStatusCallback: PPPERR_CONNECT\n", pcb); break; } case PPPERR_AUTHFAIL: { /* Failed authentication challenge. */ printf("<%p> pppLinkStatusCallback: PPPERR_AUTHFAIL\n", pcb); break; } case PPPERR_PROTOCOL: { /* Failed to meet protocol. */ printf("<%p> pppLinkStatusCallback: PPPERR_PROTOCOL\n", pcb); break; } case PPPERR_PEERDEAD: { /* Connection timeout */ printf("<%p> pppLinkStatusCallback: PPPERR_PEERDEAD\n", pcb); break; } case PPPERR_IDLETIMEOUT: { /* Idle Timeout */ printf("<%p> pppLinkStatusCallback: PPPERR_IDLETIMEOUT\n", pcb); break; } case PPPERR_CONNECTTIME: { /* Max connect time reached */ printf("<%p> pppLinkStatusCallback: PPPERR_CONNECTTIME\n", pcb); break; } case PPPERR_LOOPBACK: { /* Loopback detected */ printf("<%p> pppLinkStatusCallback: PPPERR_LOOPBACK\n", pcb); break; } default: { printf("<%p> pppLinkStatusCallback: unknown errCode %d\n", pcb, errCode); break; } } } int pppoe_session_init() { PUSER_INFO_LIST pList, pTmp; PUSER_INFO_LIST pUserList = user_info_getall(); HASH_ITER(hh, pUserList, pList, pTmp) { PPPPOE_SESSION pSession = (PPPPOE_SESSION)malloc(sizeof(PPPOE_SESSION)); if (pSession) { struct netif* ppp_netif = (struct netif*)malloc(sizeof(struct netif)); if(ppp_netif) { struct netif *netif = bind_rawsocket_if("ens160", pList->user_info.mac_addr); if (netif == NULL) { free(pSession); free(ppp_netif); dzlog_error("Create RAW netif error: %u\n", pList->userid); } else { ppp_pcb* ppp; memset(pSession, 0, sizeof(PPPOE_SESSION)); pSession->nicif = netif; pSession->pppif = ppp_netif; pSession->userid = pList->userid; memcpy(&pSession->user_info, &pList->user_info, sizeof(USER_INFO)); ppp = pppoe_create(pSession->pppif, pSession->nicif, NULL, NULL, pppLinkStatusCallback, NULL); if(ppp == NULL) { netif_remove(netif); free(pSession); free(ppp_netif); dzlog_error("Create PPPoE netif error: %u\n", pList->userid); } else { ppp_set_auth(ppp, PPPAUTHTYPE_ANY, pSession->user_info.pppoe_user, pSession->user_info.pppoe_passwd); ppp_connect(ppp, 0); dzlog_info("Create PPPoE netif %p: %u\n", ppp, pList->userid); HASH_ADD_INT(g_pSessionList, userid, pSession); } } } } } return ERR_OK; }