📄 pcapif.c
字号:
printf("Using adapter: \"%s\"\n", pa->description);
pcap_freealldevs(alldevs);
#if PCAPIF_HANDLE_LINKSTATE
pa->link_state = pcapifh_linkstate_init(pa->name);
pa->last_link_event = PCAPIF_LINKEVENT_UNKNOWN;
#endif /* PCAPIF_HANDLE_LINKSTATE */
return pa;
}
#if PCAPIF_HANDLE_LINKSTATE
void
pcapif_check_linkstate(void *netif)
{
struct pcapif_private *pa = (struct pcapif_private*)((struct netif *)netif)->state;
enum pcapifh_link_event le;
le = pcapifh_linkstate_get(pa->link_state);
if (pa->last_link_event != le) {
pa->last_link_event = le;
switch (le) {
case PCAPIF_LINKEVENT_UP: {
PCAPIF_NOTIFY_LINKSTATE(netif, netif_set_link_up);
break;
}
case PCAPIF_LINKEVENT_DOWN: {
PCAPIF_NOTIFY_LINKSTATE(netif, netif_set_link_down);
break;
}
}
}
sys_timeout(PCAPIF_LINKCHECK_INTERVAL_MS, pcapif_check_linkstate, netif);
}
#endif /* PCAPIF_HANDLE_LINKSTATE */
/**
* Close the adapter (no more packets can be sent or received)
*
* @param netif netif to shutdown
*/
void
pcapif_shutdown(struct netif *netif)
{
struct pcapif_private *pa = (struct pcapif_private*)netif->state;
if (pa) {
#if PCAPIF_RX_USE_THREAD
pa->rx_run = 0;
#endif /* PCAPIF_RX_USE_THREAD */
if (pa->adapter) {
pcap_breakloop(pa->adapter);
pcap_close(pa->adapter);
}
#if PCAPIF_RX_USE_THREAD
/* wait for rxthread to end */
while(pa->rx_running);
#endif /* PCAPIF_RX_USE_THREAD */
#if PCAPIF_HANDLE_LINKSTATE
pcapifh_linkstate_close(pa->link_state);
#endif /* PCAPIF_HANDLE_LINKSTATE */
free(pa);
}
}
#if PCAPIF_RX_USE_THREAD
/** RX running in its own thread */
static void
pcapif_input_thread(void *arg)
{
struct netif *netif = (struct netif *)arg;
struct pcapif_private *pa = (struct pcapif_private*)netif->state;
int ret;
do
{
ret = pcap_loop(pa->adapter, -1, pcapif_input, (u_char*)pa);
if (ret < 0)
{
sys_msleep(1);
}
} while (pa->rx_run);
pa->rx_running = 0;
}
#endif /* PCAPIF_RX_USE_THREAD */
/** Low-level initialization: find the correct adapter and initialize it.
*/
static void
pcapif_low_level_init(struct netif *netif)
{
u8_t my_mac_addr[ETHARP_HWADDR_LEN] = LWIP_MAC_ADDR_BASE;
int adapter_num = PACKET_LIB_ADAPTER_NR;
struct pcapif_private *pa;
#ifdef PACKET_LIB_GET_ADAPTER_NETADDRESS
ip_addr_t netaddr;
#define GUID_LEN 128
char guid[GUID_LEN + 1];
memset(&guid, 0, sizeof(guid));
PACKET_LIB_GET_ADAPTER_NETADDRESS(&netaddr);
if (get_adapter_index_from_addr((struct in_addr *)&netaddr, guid, GUID_LEN) < 0) {
printf("ERROR initializing network adapter, failed to get GUID for network address %s\n", ip_ntoa(&netaddr));
LWIP_ASSERT("ERROR initializing network adapter, failed to get GUID for network address!", 0);
return;
}
adapter_num = get_adapter_index(guid);
if (adapter_num < 0) {
printf("ERROR finding network adapter with GUID \"%s\"!\n", guid);
LWIP_ASSERT("ERROR finding network adapter with expected GUID!", 0);
return;
}
#else /* PACKET_LIB_GET_ADAPTER_NETADDRESS */
#ifdef PACKET_LIB_ADAPTER_GUID
/* get adapter index for guid string */
adapter_num = get_adapter_index(PACKET_LIB_ADAPTER_GUID);
if (adapter_num < 0) {
printf("ERROR finding network adapter with GUID \"%s\"!\n", PACKET_LIB_ADAPTER_GUID);
LWIP_ASSERT("ERROR initializing network adapter!\n", 0);
return;
}
#endif /* PACKET_LIB_ADAPTER_GUID */
#endif /* PACKET_LIB_GET_ADAPTER_NETADDRESS */
/* Do whatever else is needed to initialize interface. */
pa = pcapif_init_adapter(adapter_num, netif);
if (pa == NULL) {
printf("ERROR initializing network adapter %d!\n", PACKET_LIB_ADAPTER_NR);
LWIP_ASSERT("ERROR initializing network adapter!", 0);
return;
}
netif->state = pa;
/* change the MAC address to a unique value
so that multiple ethernetifs are supported */
/* @todo: this does NOT support multiple processes using this adapter! */
my_mac_addr[ETHARP_HWADDR_LEN - 1] += netif->num;
/* Copy MAC addr */
memcpy(&netif->hwaddr, my_mac_addr, ETHARP_HWADDR_LEN);
/* get the initial link state of the selected interface */
#if PCAPIF_HANDLE_LINKSTATE
pa->last_link_event = pcapifh_linkstate_get(pa->link_state);
if (pa->last_link_event == PCAPIF_LINKEVENT_DOWN) {
netif_set_link_down(netif);
} else {
netif_set_link_up(netif);
}
sys_timeout(PCAPIF_LINKCHECK_INTERVAL_MS, pcapif_check_linkstate, netif);
#endif /* PCAPIF_HANDLE_LINKSTATE */
#if PCAPIF_RX_USE_THREAD
pa->rx_run = 1;
pa->rx_running = 1;
sys_thread_new("pktif_rxthread", pcapif_input_thread, netif, 0, 0);
#endif
LWIP_DEBUGF(NETIF_DEBUG, ("pktif: eth_addr %02X%02X%02X%02X%02X%02X\n",netif->hwaddr[0],netif->hwaddr[1],netif->hwaddr[2],netif->hwaddr[3],netif->hwaddr[4],netif->hwaddr[5]));
}
/** low_level_output():
* Transmit a packet. The packet is contained in the pbuf that is passed to
* the function. This pbuf might be chained.
*/
static err_t
pcapif_low_level_output(struct netif *netif, struct pbuf *p)
{
struct pbuf *q;
unsigned char buffer[1520];
unsigned char *buf = buffer;
unsigned char *ptr;
struct eth_hdr *ethhdr;
u16_t tot_len = p->tot_len - ETH_PAD_SIZE;
struct pcapif_private *pa = (struct pcapif_private*)netif->state;
#if defined(LWIP_DEBUG) && LWIP_NETIF_TX_SINGLE_PBUF
LWIP_ASSERT("p->next == NULL && p->len == p->tot_len", p->next == NULL && p->len == p->tot_len);
#endif
/* initiate transfer */
if (p->len == p->tot_len) {
/* no pbuf chain, don't have to copy -> faster */
buf = &((unsigned char*)p->payload)[ETH_PAD_SIZE];
} else {
/* pbuf chain, copy into contiguous buffer */
if (p->tot_len >= sizeof(buffer)) {
LINK_STATS_INC(link.lenerr);
LINK_STATS_INC(link.drop);
snmp_inc_ifoutdiscards(netif);
return ERR_BUF;
}
ptr = buffer;
for(q = p; q != NULL; q = q->next) {
/* Send the data from the pbuf to the interface, one pbuf at a
time. The size of the data in each pbuf is kept in the ->len
variable. */
/* send data from(q->payload, q->len); */
LWIP_DEBUGF(NETIF_DEBUG, ("netif: send ptr %p q->payload %p q->len %i q->next %p\n", ptr, q->payload, (int)q->len, (void*)q->next));
if (q == p) {
memcpy(ptr, &((char*)q->payload)[ETH_PAD_SIZE], q->len - ETH_PAD_SIZE);
ptr += q->len - ETH_PAD_SIZE;
} else {
memcpy(ptr, q->payload, q->len);
ptr += q->len;
}
}
}
/* signal that packet should be sent */
if (pcap_sendpacket(pa->adapter, buf, tot_len) < 0) {
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.drop);
snmp_inc_ifoutdiscards(netif);
return ERR_BUF;
}
LINK_STATS_INC(link.xmit);
snmp_add_ifoutoctets(netif, tot_len);
ethhdr = (struct eth_hdr *)p->payload;
if ((ethhdr->dest.addr[0] & 1) != 0) {
/* broadcast or multicast packet*/
snmp_inc_ifoutnucastpkts(netif);
} else {
/* unicast packet */
snmp_inc_ifoutucastpkts(netif);
}
return ERR_OK;
}
/** low_level_input(): Allocate a pbuf and transfer the bytes of the incoming
* packet from the interface into the pbuf.
*/
static struct pbuf *
pcapif_low_level_input(struct netif *netif, const void *packet, int packet_len)
{
struct pbuf *p, *q;
int start;
int length = packet_len;
struct eth_addr *dest = (struct eth_addr*)packet;
struct eth_addr *src = dest + 1;
int unicast;
/* MAC filter: only let my MAC or non-unicast through (pcap receives loopback traffic, too) */
unicast = ((dest->addr[0] & 0x01) == 0);
if (((memcmp(dest, &netif->hwaddr, ETHARP_HWADDR_LEN)) && unicast) ||
/* and don't let feedback packets through (limitation in winpcap?) */
(!memcmp(src, netif->hwaddr, ETHARP_HWADDR_LEN))) {
/* don't update counters here! */
return NULL;
}
/* We allocate a pbuf chain of pbufs from the pool. */
p = pbuf_alloc(PBUF_RAW, (u16_t)length + ETH_PAD_SIZE, PBUF_POOL);
LWIP_DEBUGF(NETIF_DEBUG, ("netif: recv length %i p->tot_len %i\n", length, (int)p->tot_len));
if (p != NULL) {
/* We iterate over the pbuf chain until we have read the entire
packet into the pbuf. */
start=0;
for (q = p; q != NULL; q = q->next) {
u16_t copy_len = q->len;
/* Read enough bytes to fill this pbuf in the chain. The
available data in the pbuf is given by the q->len
variable. */
/* read data into(q->payload, q->len); */
LWIP_DEBUGF(NETIF_DEBUG, ("netif: recv start %i length %i q->payload %p q->len %i q->next %p\n", start, length, q->payload, (int)q->len, (void*)q->next));
if (q == p) {
#if ETH_PAD_SIZE
LWIP_ASSERT("q->len >= ETH_PAD_SIZE", q->len >= ETH_PAD_SIZE);
copy_len -= ETH_PAD_SIZE;
#endif /* ETH_PAD_SIZE*/
memcpy(&((char*)q->payload)[ETH_PAD_SIZE], &((char*)packet)[start], copy_len);
} else {
memcpy(q->payload, &((char*)packet)[start], copy_len);
}
start += copy_len;
length -= copy_len;
if (length <= 0) {
break;
}
}
LINK_STATS_INC(link.recv);
snmp_add_ifinoctets(netif, p->tot_len);
if (unicast) {
snmp_inc_ifinucastpkts(netif);
} else {
snmp_inc_ifinnucastpkts(netif);
}
} else {
/* drop packet(); */
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.drop);
}
return p;
}
/** pcapif_input: This function is called when a packet is ready to be read
* from the interface. It uses the function low_level_input() that should
* handle the actual reception of bytes from the network interface.
*/
static void
pcapif_input(u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *packet)
{
struct pcapif_private *pa = (struct pcapif_private*)user;
int packet_len = pkt_header->caplen;
struct netif *netif = (struct netif *)pa->input_fn_arg;
struct pbuf *p;
/* move received packet into a new pbuf */
p = pcapif_low_level_input(netif, packet, packet_len);
/* no packet could be read, silently ignore this */
if (p != NULL) {
/* pass all packets to ethernet_input, which decides what packets it supports */
if (netif->input(p, netif) != ERR_OK) {
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
pbuf_free(p);
}
}
}
/**
* pcapif_init(): initialization function, pass to netif_add().
*/
err_t
pcapif_init(struct netif *netif)
{
static int ethernetif_index;
int local_index;
SYS_ARCH_DECL_PROTECT(lev);
SYS_ARCH_PROTECT(lev);
local_index = ethernetif_index++;
SYS_ARCH_UNPROTECT(lev);
netif->name[0] = IFNAME0;
netif->name[1] = (char)(IFNAME1 + local_index);
netif->linkoutput = pcapif_low_level_output;
#if LWIP_ARP
netif->output = etharp_output;
#else /* LWIP_ARP */
netif->output = NULL; /* not used for PPPoE */
#endif /* LWIP_ARP */
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
netif_set_hostname(netif, "lwip");
#endif /* LWIP_NETIF_HOSTNAME */
netif->mtu = 1500;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
netif->hwaddr_len = ETHARP_HWADDR_LEN;
NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000);
/* sets link up or down based on current status */
pcapif_low_level_init(netif);
return ERR_OK;
}
#if !PCAPIF_RX_USE_THREAD
void
pcapif_poll(struct netif *netif)
{
struct pcapif_private *pa = (struct pcapif_private*)netif->state;
int ret;
do
{
ret = pcap_dispatch(pa->adapter, -1, pcapif_input, (u_char*)pa);
} while(ret > 0);
}
#endif /* !PCAPIF_RX_USE_THREAD */
#endif /* LWIP_ETHERNET */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -