From 25adf977580efdc0b1821ad2ee0afbcc0f7285e7 Mon Sep 17 00:00:00 2001 From: huangxin Date: Sun, 12 Jun 2022 18:00:43 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E7=A1=AC=E4=BB=B6=E7=BD=91?= =?UTF-8?q?=E5=8D=A1=E6=95=B0=E6=8D=AE=E5=8C=85=E6=94=B6=E5=8F=91=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0libpcap=E5=8A=9F=E8=83=BD=EF=BC=8C=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2RawSocket=E6=A8=A1=E5=BC=8F=E3=80=82=202.=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BD=91=E5=8D=A1=E6=95=B0=E6=8D=AE=E5=8C=85?= =?UTF-8?q?=E6=8E=A5=E6=94=B6=E7=BC=93=E5=AD=98=E9=98=9F=E5=88=97=EF=BC=8C?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E6=95=B0=E6=8D=AE=E4=B8=A2=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +- srcs/libs/configure/config.c | 13 +- srcs/libs/configure/config_help.c | 10 +- srcs/libs/include/config.h | 2 + srcs/lwip/src/arch_linux/CMakeLists.txt | 4 + srcs/lwip/src/arch_linux/Filelists.cmake | 1 + .../src/arch_linux/include/netif/pcapif.h | 46 ++ .../src/arch_linux/include/netif/pppoeif.h | 9 + srcs/lwip/src/arch_linux/netif/pcapif.c | 428 ++++++++++++++++++ srcs/lwip/src/arch_linux/netif/pppoe_if.c | 7 +- srcs/pppoe/vcpe_pppoe.c | 5 +- srcs/user/user_info.c | 6 +- 12 files changed, 519 insertions(+), 16 deletions(-) create mode 100644 srcs/lwip/src/arch_linux/include/netif/pcapif.h create mode 100644 srcs/lwip/src/arch_linux/netif/pcapif.c diff --git a/.gitignore b/.gitignore index 6b01f3e..6b3686f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /cmake-build-debug/ /.idea/ */__init__.py -/cmake-build-debug-ubuntuefc/ -/cmake-build-debug-vswith_dev/ +/cmake-build-debug-*/ +/cmake-build-debug_*/ diff --git a/srcs/libs/configure/config.c b/srcs/libs/configure/config.c index e76b991..13bed9e 100644 --- a/srcs/libs/configure/config.c +++ b/srcs/libs/configure/config.c @@ -344,6 +344,10 @@ const char *config_item_dump_fmt(const char *titleMessage) { case VALUE_TYPE_STRING: tmp = sdsempty(); tmp = sdscatprintf(tmp, "\"%s\"", CFG_STRING_VALUE(pItem)); + if(sdslen(tmp) > 43) { + sdsrange(tmp, 0, 39); + sdscat(tmp, "...\""); + } s = sdscatprintf(s, "|%4d | %-25s | %-45s | %-44s |\n", pItem->cfgId, tmp2, pItem->pcfgKey, tmp); sdsfree(tmp); break; @@ -482,10 +486,11 @@ do { ADD_CFG_ITEM(CFG_MQ_SVR_PORT, "application.zero_mq.svr_port", VALUE_TYPE_INTEGRAL, "6278", "ZeroMQ server port"); \ ADD_CFG_ITEM(CFG_MQ_DATA_CH, "application.zero_mq.agent_port", VALUE_TYPE_INTEGRAL, "6279", "ZeroMQ Agent server port"); \ /* vxLan 隧道配置 */ \ - ADD_CFG_ITEM(CFG_VXLAN_NIC_NAME, "application.vxlan.vxlan_nic", VALUE_TYPE_STRING, "", "Network card name to send data"); \ - ADD_CFG_ITEM(CFG_VXLAN_SUPPORT, "application.vxlan.vxlan_enable", VALUE_TYPE_BOOL, "1", "Is support vxLan tune"); \ - ADD_CFG_ITEM(CFG_VXLAN_PEER_IP, "application.vxlan.vxlan_peer_ip", VALUE_TYPE_STRING, "", "vxLan peer ip address"); \ - ADD_CFG_ITEM(CFG_VXLAN_PEER_MAC, "application.vxlan.vxlan_peer_mac", VALUE_TYPE_STRING, "", "vxLan peer mac address"); \ + ADD_CFG_ITEM(CFG_VXLAN_NIC_NAME, "application.vxlan_wan.nic", VALUE_TYPE_STRING, "", "Network card name to send data"); \ + ADD_CFG_ITEM(CFG_VXLAN_SUPPORT, "application.vxlan_wan.enable", VALUE_TYPE_BOOL, "1", "Is support vxLan tune"); \ + ADD_CFG_ITEM(CFG_VXLAN_PEER_IP, "application.vxlan_wan.peer_ip", VALUE_TYPE_STRING, "", "vxLan peer ip address"); \ + ADD_CFG_ITEM(CFG_VXLAN_PEER_MAC, "application.vxlan_wan.peer_mac", VALUE_TYPE_STRING, "", "vxLan peer mac address"); \ + ADD_CFG_ITEM(CFG_VXLAN_PKG_FILTER, "application.vxlan_wan.pkg_filter", VALUE_TYPE_STRING, "", "vxLan package filter"); \ } while (0)// clang-format on int init_config_system(const char *pCfgFile, const char *pKey) { diff --git a/srcs/libs/configure/config_help.c b/srcs/libs/configure/config_help.c index 1a8cf74..12c7e4c 100644 --- a/srcs/libs/configure/config_help.c +++ b/srcs/libs/configure/config_help.c @@ -3,18 +3,22 @@ // #include "config.h" -const char*config_get_vxlan_nic_name() { +const char *config_get_vxlan_nic_name() { return cfg_get_string_value(CFG_VXLAN_NIC_NAME); } -const char*config_get_vxlan_peer_ip() { +const char *config_get_vxlan_peer_ip() { return cfg_get_string_value(CFG_VXLAN_PEER_IP); } -const char*config_get_vxlan_peer_mac() { +const char *config_get_vxlan_peer_mac() { return cfg_get_string_value(CFG_VXLAN_PEER_MAC); } +const char *config_get_vxlan_pkg_filter() { + return cfg_get_string_value(CFG_VXLAN_PKG_FILTER); +} + int cfg_get_support_vxlan() { return cfg_get_bool_value(CFG_VXLAN_SUPPORT); } diff --git a/srcs/libs/include/config.h b/srcs/libs/include/config.h index fe8eec0..b14b06a 100644 --- a/srcs/libs/include/config.h +++ b/srcs/libs/include/config.h @@ -46,6 +46,7 @@ typedef enum { CFG_VXLAN_SUPPORT = 24, CFG_VXLAN_PEER_IP = 25, CFG_VXLAN_PEER_MAC = 26, + CFG_VXLAN_PKG_FILTER = 27, CONFIG_ITEM_ID_MAX } CONFIG_ITEM_ID; @@ -88,6 +89,7 @@ int cfg_get_support_vxlan(); const char *config_get_vxlan_nic_name(); const char *config_get_vxlan_peer_mac(); const char *config_get_vxlan_peer_ip(); +const char *config_get_vxlan_pkg_filter(); #ifdef __cplusplus } #endif diff --git a/srcs/lwip/src/arch_linux/CMakeLists.txt b/srcs/lwip/src/arch_linux/CMakeLists.txt index 40051c9..46ca785 100644 --- a/srcs/lwip/src/arch_linux/CMakeLists.txt +++ b/srcs/lwip/src/arch_linux/CMakeLists.txt @@ -35,3 +35,7 @@ target_link_libraries(${LWIP_LIB_NAME} ${LWIP_SANITIZER_LIBS}) find_library(LIBPTHREAD pthread) target_link_libraries(${LWIP_LIB_NAME} ${LIBPTHREAD}) +find_library(LIBPCAP pcap) +target_link_libraries(${LWIP_LIB_NAME} ${LIBPCAP}) +find_library(LIBZMQ zmq) +target_link_libraries(${LWIP_LIB_NAME} ${LIBZMQ}) \ No newline at end of file diff --git a/srcs/lwip/src/arch_linux/Filelists.cmake b/srcs/lwip/src/arch_linux/Filelists.cmake index e89c888..679345a 100644 --- a/srcs/lwip/src/arch_linux/Filelists.cmake +++ b/srcs/lwip/src/arch_linux/Filelists.cmake @@ -18,6 +18,7 @@ set(lwipcontribportunixnetifs_SRCS ${LWIP_DIR}/src/arch_linux/netif/rawif.c ${LWIP_DIR}/src/arch_linux/netif/sio.c ${LWIP_DIR}/src/arch_linux/netif/pppoe_if.c + ${LWIP_DIR}/src/arch_linux/netif/pcapif.c ) if (FALSE) diff --git a/srcs/lwip/src/arch_linux/include/netif/pcapif.h b/srcs/lwip/src/arch_linux/include/netif/pcapif.h new file mode 100644 index 0000000..5748a31 --- /dev/null +++ b/srcs/lwip/src/arch_linux/include/netif/pcapif.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2001-2003 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * + */ +#ifndef LWIP_PCAPIF_H +#define LWIP_PCAPIF_H + +#include "lwip/netif.h" + +#ifdef __cplusplus +extern "C" { +#endif +err_t pcapif_init(struct netif *netif); +void pcapif_poll(struct netif *netif); +err_t pcapif_output(struct netif *netif, struct pbuf *p); +struct netif *bind_pcap_if(const char *eth_name, const char *pkg_filter); +#ifdef __cplusplus +} +#endif +#endif /* LWIP_PCAPIF_H */ diff --git a/srcs/lwip/src/arch_linux/include/netif/pppoeif.h b/srcs/lwip/src/arch_linux/include/netif/pppoeif.h index 2f70f77..fb65ef5 100644 --- a/srcs/lwip/src/arch_linux/include/netif/pppoeif.h +++ b/srcs/lwip/src/arch_linux/include/netif/pppoeif.h @@ -35,6 +35,15 @@ #include "lwip/netif.h" #include "../../../../../include/pppoe_session.h" +#define DUMP_IF_BUF(tag, buf, len) \ + do { \ + dzlog_debug("%s ++++++\n", (tag)); \ + printf("\n"); \ + hdzlog_debug((buf), (len)); \ + printf("\n"); \ + dzlog_debug("%s ------\n", (tag)); \ + } while (0) + #ifdef __cplusplus extern "C" { #endif diff --git a/srcs/lwip/src/arch_linux/netif/pcapif.c b/srcs/lwip/src/arch_linux/netif/pcapif.c new file mode 100644 index 0000000..3684afa --- /dev/null +++ b/srcs/lwip/src/arch_linux/netif/pcapif.c @@ -0,0 +1,428 @@ +/* + * Copyright (c) 2001-2003 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * + */ + +#include +#include +#include +#include + +#include "lwip/opt.h" +#include "lwip/debug.h" +#include "lwip/def.h" + +#include "lwip/mem.h" + +#include "lwip/snmp.h" +#include "lwip/pbuf.h" +#include "lwip/sys.h" + +#include "netif/etharp.h" + +#include "netif/pcapif.h" + +#if defined(LWIP_UNIX_LINUX) +#include +#include + +/* Define those to better describe your network interface. */ +#define IFNAME0 'p' +#define IFNAME1 'c' + +#ifndef PCAPIF_DEBUG +#define PCAPIF_DEBUG LWIP_DBG_OFF +#endif + +#define MAX_BYTES_PACKAGE (1518) +#define MSQ_QUEUE_HANDLE ("inproc://pcapif_msg_fifo") + +struct pcapif { + /* Add whatever per-interface state that is needed here. */ + pcap_t *pcap; + const char *eth_name; + unsigned char mac_addr[6]; + const char *pkg_filter; + void *msg_input; + void *msg_output; +}; + +#if !NO_SYS +_Noreturn static void pcapif_thread(void *arg); +#endif /* !NO_SYS */ + +static void pcapif_msg_thread(void *arg) { + struct netif *netif = (struct netif *)(void *)arg; + struct pcapif *pcapif = (struct pcapif *)netif->state; + + while (pcapif && pcapif->msg_output) { + int size; + zmq_msg_t msg; + struct pbuf *p; + + zmq_msg_init(&msg); + + size = zmq_msg_recv(&msg, pcapif->msg_output, 0); + + if (size > 0) { + /* We allocate a pbuf chain of pbufs from the pool. */ + p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL); + if (p != NULL) { + pbuf_take(p, zmq_msg_data(&msg), size); + /* acknowledge that packet has been read(); */ + if (netif->input(p, netif) != ERR_OK) { + LWIP_DEBUGF(NETIF_DEBUG, ("pcapif_input: netif input error\n")); + pbuf_free(p); + } + } else { + /* drop packet(); */ + MIB2_STATS_NETIF_INC(netif, ifindiscards); + LWIP_DEBUGF(NETIF_DEBUG, ("pcapif_input: could not allocate pbuf\n")); + } + } + + usleep(10); + } +} + +static void pcap_pkg_cb(unsigned char *args, const struct pcap_pkthdr *pkthdr, const unsigned char *packet) { + struct netif *netif = (struct netif *)(void *)args; + //struct pbuf *p; + //u16_t len = pkthdr->caplen; + struct pcapif *pcapif = (struct pcapif *)netif->state; + + if (pcapif && pcapif->msg_input) { + zmq_msg_t msg; + int rc; + zmq_msg_init_size(&msg, pkthdr->caplen); + memcpy(zmq_msg_data(&msg), packet, pkthdr->caplen); + rc = zmq_msg_send(&msg, pcapif->msg_input, 0); + if (rc == -1) { + dzlog_error("Send %d bytes message error: %s\n", pkthdr->caplen, strerror(errno)); + } + zmq_msg_close(&msg); + } +} +/*-----------------------------------------------------------------------------------*/ +static void low_level_init(struct netif *netif) { + char buf[2048]; + bpf_u_int32 netaddr = 0, mask = 0; + pcap_t *pPcap = NULL; + struct bpf_program filter; + char errBuf[PCAP_ERRBUF_SIZE]; + struct pcapif *pcapif = (struct pcapif *)netif->state; + + memset(errBuf, 0, PCAP_ERRBUF_SIZE); + memset(buf, 0, 2048); + + pPcap = pcap_open_live(pcapif->eth_name, MAX_BYTES_PACKAGE, 1, -1, errBuf); + pcap_lookupnet(pcapif->eth_name, &netaddr, &mask, errBuf); + + snprintf(buf, 2048, "(%s) and (inbound)", pcapif->pkg_filter); + + dzlog_debug("Pcap netif used filter: \"%s\"\n", buf); + if (pcap_compile(pPcap, &filter, buf, 1, mask) == -1) { + dzlog_error("Set package fileter[%s] error: %s\n", buf, pcap_geterr(pPcap)); + } + if (pcap_setfilter(pPcap, &filter) == -1) { + dzlog_error("Set package fileter[%s] error: %s\n", buf, pcap_geterr(pPcap)); + } + + pcapif->pcap = pPcap; + + /* Obtain MAC address from network interface. */ + + netif->hwaddr[0] = pcapif->mac_addr[0]; + netif->hwaddr[1] = pcapif->mac_addr[1]; + netif->hwaddr[2] = pcapif->mac_addr[2]; + netif->hwaddr[3] = pcapif->mac_addr[3]; + netif->hwaddr[4] = pcapif->mac_addr[4]; + netif->hwaddr[5] = pcapif->mac_addr[5]; + netif->hwaddr_len = 6; + + /* device capabilities */ + netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP; + + netif_set_link_up(netif); + +#if !NO_SYS + sys_thread_new("pcapif_thread", pcapif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); + sys_thread_new("pcapif_msg_thread", pcapif_msg_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); +#endif /* !NO_SYS */ +} +/*-----------------------------------------------------------------------------------*/ +/* + * low_level_output(): + * + * Should do the actual transmission of the packet. The packet is + * contained in the pbuf that is passed to the function. This pbuf + * might be chained. + * + */ +/*-----------------------------------------------------------------------------------*/ + +static err_t low_level_output(struct netif *netif, struct pbuf *p) { + char buf[1518]; /* max packet size including VLAN excluding CRC */ + ssize_t written; + struct pcapif *pcapif = (struct pcapif *)netif->state; + + if (p->tot_len > sizeof(buf)) { + MIB2_STATS_NETIF_INC(netif, ifoutdiscards); + perror("pcapif: packet too large"); + return ERR_IF; + } + + /* initiate transfer(); */ + pbuf_copy_partial(p, buf, p->tot_len, 0); + + /* signal that packet should be sent(); */ + written = pcap_inject(pcapif->pcap, buf, p->tot_len); + +#if 0 + dzlog_info("\n Send: \n"); + hdzlog_info(buf, p->tot_len); + dzlog_info("\n"); +#endif + + if (written < p->tot_len) { + MIB2_STATS_NETIF_INC(netif, ifoutdiscards); + perror("pcapif: write"); + return ERR_IF; + } else { + MIB2_STATS_NETIF_ADD(netif, ifoutoctets, (u32_t)written); + return ERR_OK; + } +} + +err_t pcapif_output(struct netif *netif, struct pbuf *p) { + return low_level_output(netif, p); +} + +/*-----------------------------------------------------------------------------------*/ +/* + * pcapif_init(): + * + * Should be called at the beginning of the program to set up the + * network interface. It calls the function low_level_init() to do the + * actual setup of the hardware. + * + */ +/*-----------------------------------------------------------------------------------*/ +err_t pcapif_init(struct netif *netif) { + struct pcapif *pcapif = (struct pcapif *)netif->state; + + if (pcapif == NULL) { + LWIP_DEBUGF(NETIF_DEBUG, ("pcapif_init: out of memory for pcapif\n")); + return ERR_MEM; + } + + MIB2_INIT_NETIF(netif, snmp_ifType_other, 100000000); + + netif->name[0] = IFNAME0; + netif->name[1] = IFNAME1; +#if LWIP_IPV4 + netif->output = etharp_output; +#endif /* LWIP_IPV4 */ +#if LWIP_IPV6 + netif->output_ip6 = ethip6_output; +#endif /* LWIP_IPV6 */ + netif->linkoutput = low_level_output; + netif->mtu = 1500; + + low_level_init(netif); + + return ERR_OK; +} + +#if NO_SYS + +int pcapif_select(struct netif *netif) { + fd_set fdset; + int ret; + struct timeval tv; + struct pcapif *pcapif; + u32_t msecs = sys_timeouts_sleeptime(); + + pcapif = (struct pcapif *)netif->state; + + tv.tv_sec = msecs / 1000; + tv.tv_usec = (msecs % 1000) * 1000; + + FD_ZERO(&fdset); + FD_SET(pcapif->fd, &fdset); + + ret = select(pcapif->fd + 1, &fdset, NULL, NULL, &tv); + if (ret > 0) { + pcapif_input(netif); + } + return ret; +} + +#else /* NO_SYS */ + +_Noreturn static void pcapif_thread(void *arg) { + struct netif *netif = (struct netif *)arg; + struct pcapif *pcapif = (struct pcapif *)netif->state; + + do { + pcap_loop(pcapif->pcap, -1, pcap_pkg_cb, (unsigned char *)(void *)netif); + } while (1); +} +#endif /* NO_SYS */ + +#if LWIP_NETIF_STATUS_CALLBACK +static void status_callback(struct netif *state_netif) { + if (netif_is_up(state_netif)) { +#if LWIP_IPV4 + dzlog_info("status_callback==UP, local interface IP is %s\n", ip4addr_ntoa(netif_ip4_addr(state_netif))); +#else + printf("status_callback==UP\n"); +#endif + } else { + dzlog_error("status_callback==DOWN\n"); + } +} +#endif /* LWIP_NETIF_STATUS_CALLBACK */ + +#if LWIP_NETIF_LINK_CALLBACK +static void link_callback(struct netif *state_netif) { + if (netif_is_link_up(state_netif)) { + dzlog_info("link_callback==UP\n"); + } else { + dzlog_error("link_callback==DOWN\n"); + } +} +#endif /* LWIP_NETIF_LINK_CALLBACK */ + +static err_t netif_input_data(struct pbuf *p, struct netif *inp) { + err_t err = ERR_OK; + struct netif *netif; + LWIP_UNUSED_ARG(inp); + + NETIF_FOREACH(netif) { + if (netif->hwaddr_len == 6 && memcmp(p->payload, netif->hwaddr, netif->hwaddr_len) == 0) { + + if ((err = netif->input(p, netif)) != ERR_OK) { + LWIP_DEBUGF(NETIF_DEBUG, ("pppoeif_input: netif input error\n")); + pbuf_free(p); + } + + return err; + } + } + + return err; +} + +struct netif *bind_pcap_if(const char *eth_name, const char *pkg_filter) { + void *msg_context = zmq_ctx_new(); + struct netif *netif; + struct ifreq ifr; + ip4_addr_t ipaddr, netmask, gw; + struct pcapif *pcapif = (struct pcapif *)mem_malloc(sizeof(struct pcapif)); + + if (pcapif == NULL) { + dzlog_error("bind_pcapsocket_if: out of memory for pcapif\n"); + return NULL; + } + + memset(pcapif, 0, sizeof(struct pcapif)); + + if (eth_name && strlen(eth_name) > 0) { + pcapif->eth_name = eth_name; + } + + pcapif->pkg_filter = pkg_filter; +#if 0 + if (vxlan_link_init(eth_name) != ERR_OK) { + dzlog_error("bind_pcapsocket_if: Get local nic info failed\n"); + return NULL; + } +#endif + ip4_addr_set_zero(&gw); + ip4_addr_set_zero(&ipaddr); + ip4_addr_set_zero(&netmask); + + IP4_ADDR(&netmask, 255, 255, 255, 0); + IP4_ADDR(&ipaddr, 192, 168, 100, 32); + IP4_ADDR(&gw, 192, 168, 100, 250); +#if 0 + IP4_ADDR(&gw, + (DEFAULT_GW_IPADDR & 0xFF000000), + (DEFAULT_GW_IPADDR & 0xFF0000), + (DEFAULT_GW_IPADDR & 0xFF00), + (DEFAULT_GW_IPADDR & 0xFF)); + + IP4_ADDR(&ipaddr, + (g_localIpAddrBegin & 0xFF000000), + (g_localIpAddrBegin & 0xFF0000), + (g_localIpAddrBegin & 0xFF00), + (g_localIpAddrBegin & 0xFF)); + + pcapif->vxlan_support = cfg_get_support_vxlan(); + pcapif->pvxLan = g_pvxLanBuf; +#endif + pcapif->mac_addr[0] = 0x00; + pcapif->mac_addr[1] = 0x0C; + pcapif->mac_addr[2] = 0x01; + pcapif->mac_addr[3] = 0x02; + pcapif->mac_addr[4] = 0x00; + pcapif->mac_addr[5] = 0x0a; + memset(&ifr, 0, sizeof(ifr)); + strcpy(ifr.ifr_name, pcapif->eth_name); + + if (msg_context) { + pcapif->msg_input = zmq_socket(msg_context, ZMQ_PAIR); + pcapif->msg_output = zmq_socket(msg_context, ZMQ_PAIR); + + zmq_bind(pcapif->msg_output, MSQ_QUEUE_HANDLE); + zmq_connect(pcapif->msg_input, MSQ_QUEUE_HANDLE); + } + + netif = (struct netif *)mem_malloc(sizeof(struct netif)); + + if (!netif) { + mem_free(pcapif); + dzlog_error("Create netif error\n"); + return NULL; + } + + netif_add(netif, &ipaddr, &netmask, &gw, pcapif, pcapif_init, netif_input_data); + +#if LWIP_NETIF_STATUS_CALLBACK + netif_set_status_callback(netif, status_callback); +#endif /* LWIP_NETIF_STATUS_CALLBACK */ +#if LWIP_NETIF_LINK_CALLBACK + netif_set_link_callback(netif, link_callback); +#endif /* LWIP_NETIF_LINK_CALLBACK */ + + netif_set_default(netif); + return netif; +} +#endif diff --git a/srcs/lwip/src/arch_linux/netif/pppoe_if.c b/srcs/lwip/src/arch_linux/netif/pppoe_if.c index 2a35a1c..fe36cdd 100644 --- a/srcs/lwip/src/arch_linux/netif/pppoe_if.c +++ b/srcs/lwip/src/arch_linux/netif/pppoe_if.c @@ -48,7 +48,7 @@ #include "netif/pppoeif.h" #include "netif/rawif.h" #include "pppoe_session.h" - +#include "netif/pcapif.h" #if defined(LWIP_UNIX_LINUX) @@ -62,7 +62,6 @@ #define DEFAULT_GW_IPADDR (0xC0A80001) - static unsigned int g_localIpAddrBegin = DEFAULT_GW_IPADDR + 1; /*-----------------------------------------------------------------------------------*/ @@ -126,8 +125,8 @@ static void low_level_init(struct netif *netif) { static err_t low_level_output(struct netif *netif, struct pbuf *p) { struct netif *rs_if = get_rawsocket_if(); - p->extra = netif->state; - return rawif_output(rs_if, p); + p->extra = netif->state; + return pcapif_output(rs_if, p); } /*-----------------------------------------------------------------------------------*/ diff --git a/srcs/pppoe/vcpe_pppoe.c b/srcs/pppoe/vcpe_pppoe.c index 844bb1c..80de872 100644 --- a/srcs/pppoe/vcpe_pppoe.c +++ b/srcs/pppoe/vcpe_pppoe.c @@ -17,6 +17,7 @@ #include "config.h" #include "msg_queue.h" #include "vxlan_pkg.h" +#include "netif/pcapif.h" typedef struct PPPOE_CACHE { PPPPOE_SESSION_DATA pSessionData; @@ -259,10 +260,12 @@ int pppoe_session_init() { static uv_thread_t uvThread, cacheThread; uv_rwlock_init(&g_cacheLock); - g_rawSocketIf = bind_rawsocket_if(config_get_vxlan_nic_name()); + g_rawSocketIf = bind_pcap_if(config_get_vxlan_nic_name(), config_get_vxlan_pkg_filter()); if (g_rawSocketIf) { dzlog_info("Create Raw Socket netif: <%p>\n", (void *)g_rawSocketIf); + } else { + dzlog_info("Create Raw Socket error: <%p>\n", (void *)g_rawSocketIf); } // 启动Session状态机线程 diff --git a/srcs/user/user_info.c b/srcs/user/user_info.c index ccb8f5e..2b51b5b 100644 --- a/srcs/user/user_info.c +++ b/srcs/user/user_info.c @@ -13,14 +13,16 @@ static USER_PARAMS g_userInfo[] = { {0, 400, 24, 371, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x03}, "user1", "password1"}, {1, 300, 16, 12, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x02}, "user2", "password1"}, {2, 300, 14, 15, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x04}, "user3", "password1"}, + {3, 400, 24, 17, {0x00, 0x0C, 0x01, 0x02, 0x00, 0x04}, "xajhuang", "aaaHuang1"}, }; void user_info_init() { uv_rwlock_init(&g_userLock); - user_info_add(0, &g_userInfo[0]); - user_info_add(1, &g_userInfo[1]); + //user_info_add(0, &g_userInfo[0]); + //user_info_add(1, &g_userInfo[1]); //user_info_add(2, &g_userInfo[2]); + user_info_add(3, &g_userInfo[3]); } void user_info_change_status(PUSER_INFO_CONTEXT pInfo, USER_STATUS status) {