Network overhaul : support for multiple NICs, performance improvement
- Add support for multiple NICs - Switch from polling to an event loop for the host networking to avoid latency and locking issues
This commit is contained in:
@@ -50,15 +50,38 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
#include <stdbool.h>
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <poll.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
|
||||
#define HAVE_STDARG_H
|
||||
#include <86box/86box.h>
|
||||
#include <86box/device.h>
|
||||
#include <86box/plat.h>
|
||||
#include <86box/plat_dynld.h>
|
||||
#include <86box/thread.h>
|
||||
#include <86box/timer.h>
|
||||
#include <86box/network.h>
|
||||
#include <86box/net_event.h>
|
||||
|
||||
enum {
|
||||
NET_EVENT_STOP = 0,
|
||||
NET_EVENT_TX,
|
||||
NET_EVENT_RX,
|
||||
NET_EVENT_MAX
|
||||
};
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <pcap/pcap.h>
|
||||
#else
|
||||
typedef int bpf_int32;
|
||||
typedef unsigned int bpf_u_int32;
|
||||
|
||||
@@ -66,33 +89,28 @@ typedef unsigned int bpf_u_int32;
|
||||
* The instruction data structure.
|
||||
*/
|
||||
struct bpf_insn {
|
||||
unsigned short code;
|
||||
unsigned char jt;
|
||||
unsigned char jf;
|
||||
bpf_u_int32 k;
|
||||
unsigned short code;
|
||||
unsigned char jt;
|
||||
unsigned char jf;
|
||||
bpf_u_int32 k;
|
||||
};
|
||||
|
||||
/*
|
||||
* Structure for "pcap_compile()", "pcap_setfilter()", etc..
|
||||
*/
|
||||
struct bpf_program {
|
||||
unsigned int bf_len;
|
||||
struct bpf_insn *bf_insns;
|
||||
unsigned int bf_len;
|
||||
struct bpf_insn *bf_insns;
|
||||
};
|
||||
|
||||
typedef struct pcap_if pcap_if_t;
|
||||
typedef struct pcap_if pcap_if_t;
|
||||
|
||||
typedef struct net_timeval {
|
||||
long tv_sec;
|
||||
long tv_usec;
|
||||
} net_timeval;
|
||||
|
||||
#define PCAP_ERRBUF_SIZE 256
|
||||
#define PCAP_ERRBUF_SIZE 256
|
||||
|
||||
struct pcap_pkthdr {
|
||||
struct net_timeval ts;
|
||||
bpf_u_int32 caplen;
|
||||
bpf_u_int32 len;
|
||||
struct timeval ts;
|
||||
bpf_u_int32 caplen;
|
||||
bpf_u_int32 len;
|
||||
};
|
||||
|
||||
struct pcap_if {
|
||||
@@ -100,49 +118,79 @@ struct pcap_if {
|
||||
char *name;
|
||||
char *description;
|
||||
void *addresses;
|
||||
unsigned int flags;
|
||||
bpf_u_int32 flags;
|
||||
};
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
void *pcap; /* handle to pcap lib instance */
|
||||
netcard_t *card; /* netcard linked to us */
|
||||
thread_t *poll_tid;
|
||||
net_evt_t tx_event;
|
||||
net_evt_t stop_event;
|
||||
netpkt_t pkt;
|
||||
uint8_t mac_addr[6];
|
||||
} net_pcap_t;
|
||||
|
||||
static volatile void *pcap_handle; /* handle to WinPcap DLL */
|
||||
static volatile void *pcap; /* handle to WinPcap library */
|
||||
static volatile thread_t *poll_tid;
|
||||
static const netcard_t *poll_card; /* netcard linked to us */
|
||||
static event_t *poll_state;
|
||||
typedef struct {
|
||||
char *intf_name;
|
||||
uint8_t *mac_addr;
|
||||
} net_pcap_params_t;
|
||||
|
||||
static volatile void *libpcap_handle; /* handle to WinPcap DLL */
|
||||
|
||||
/* Pointers to the real functions. */
|
||||
static const char *(*f_pcap_lib_version)(void);
|
||||
static int (*f_pcap_findalldevs)(pcap_if_t **,char *);
|
||||
static void (*f_pcap_freealldevs)(void *);
|
||||
static void *(*f_pcap_open_live)(const char *,int,int,int,char *);
|
||||
static int (*f_pcap_compile)(void *,void *,
|
||||
const char *,int,bpf_u_int32);
|
||||
static int (*f_pcap_setfilter)(void *,void *);
|
||||
static int (*f_pcap_findalldevs)(pcap_if_t **,char *);
|
||||
static void (*f_pcap_freealldevs)(void *);
|
||||
static void *(*f_pcap_open_live)(const char *,int,int,int,char *);
|
||||
static int (*f_pcap_compile)(void *,void *, const char *,int,bpf_u_int32);
|
||||
static int (*f_pcap_setfilter)(void *,void *);
|
||||
static const unsigned char
|
||||
*(*f_pcap_next)(void *,void *);
|
||||
static int (*f_pcap_sendpacket)(void *,const unsigned char *,int);
|
||||
static void (*f_pcap_close)(void *);
|
||||
static int (*f_pcap_setnonblock)(void*, int, char*);
|
||||
static dllimp_t pcap_imports[] = {
|
||||
{ "pcap_lib_version", &f_pcap_lib_version },
|
||||
{ "pcap_findalldevs", &f_pcap_findalldevs },
|
||||
{ "pcap_freealldevs", &f_pcap_freealldevs },
|
||||
{ "pcap_open_live", &f_pcap_open_live },
|
||||
{ "pcap_compile", &f_pcap_compile },
|
||||
{ "pcap_setfilter", &f_pcap_setfilter },
|
||||
{ "pcap_next", &f_pcap_next },
|
||||
{ "pcap_sendpacket", &f_pcap_sendpacket },
|
||||
{ "pcap_close", &f_pcap_close },
|
||||
{ "pcap_setnonblock", &f_pcap_setnonblock },
|
||||
{ NULL, NULL },
|
||||
};
|
||||
*(*f_pcap_next)(void *,void *);
|
||||
static int (*f_pcap_sendpacket)(void *,const unsigned char *,int);
|
||||
static void (*f_pcap_close)(void *);
|
||||
static int (*f_pcap_setnonblock)(void*, int, char*);
|
||||
static int (*f_pcap_set_immediate_mode)(void *, int);
|
||||
static int (*f_pcap_set_promisc)(void *, int);
|
||||
static int (*f_pcap_set_snaplen)(void *, int);
|
||||
static void *(*f_pcap_create)(const char *, char*);
|
||||
static int (*f_pcap_activate)(void *);
|
||||
static void *(*f_pcap_geterr)(void *);
|
||||
#ifdef _WIN32
|
||||
static HANDLE (*f_pcap_getevent)(void *);
|
||||
#else
|
||||
static int (*f_pcap_get_selectable_fd)(void *);
|
||||
#endif
|
||||
|
||||
static dllimp_t pcap_imports[] = {
|
||||
{ "pcap_lib_version", &f_pcap_lib_version },
|
||||
{ "pcap_findalldevs", &f_pcap_findalldevs },
|
||||
{ "pcap_freealldevs", &f_pcap_freealldevs },
|
||||
{ "pcap_open_live", &f_pcap_open_live },
|
||||
{ "pcap_compile", &f_pcap_compile },
|
||||
{ "pcap_setfilter", &f_pcap_setfilter },
|
||||
{ "pcap_next", &f_pcap_next },
|
||||
{ "pcap_sendpacket", &f_pcap_sendpacket },
|
||||
{ "pcap_close", &f_pcap_close },
|
||||
{ "pcap_setnonblock", &f_pcap_setnonblock },
|
||||
{ "pcap_set_immediate_mode", &f_pcap_set_immediate_mode},
|
||||
{ "pcap_set_promisc", &f_pcap_set_promisc },
|
||||
{ "pcap_set_snaplen", &f_pcap_set_snaplen },
|
||||
{ "pcap_create", &f_pcap_create },
|
||||
{ "pcap_activate", &f_pcap_activate },
|
||||
{ "pcap_geterr", &f_pcap_geterr },
|
||||
#ifdef _WIN32
|
||||
{ "pcap_getevent", &f_pcap_getevent },
|
||||
#else
|
||||
{ "pcap_get_selectable_fd", &f_pcap_get_selectable_fd },
|
||||
#endif
|
||||
{ NULL, NULL },
|
||||
};
|
||||
|
||||
#ifdef ENABLE_PCAP_LOG
|
||||
int pcap_do_log = ENABLE_PCAP_LOG;
|
||||
|
||||
|
||||
static void
|
||||
pcap_log(const char *fmt, ...)
|
||||
{
|
||||
@@ -159,76 +207,118 @@ pcap_log(const char *fmt, ...)
|
||||
#endif
|
||||
|
||||
|
||||
/* Handle the receiving of frames from the channel. */
|
||||
static void
|
||||
poll_thread(void *arg)
|
||||
net_pcap_read_packet(net_pcap_t *pcap)
|
||||
{
|
||||
uint8_t *mac = (uint8_t *)arg;
|
||||
uint8_t *data = NULL;
|
||||
struct pcap_pkthdr h;
|
||||
uint32_t mac_cmp32[2];
|
||||
uint16_t mac_cmp16[2];
|
||||
event_t *evt;
|
||||
int tx;
|
||||
|
||||
pcap_log("PCAP: polling started.\n");
|
||||
thread_set_event(poll_state);
|
||||
uint8_t *data = (uint8_t *) f_pcap_next((void *) pcap->pcap, &h);
|
||||
if (!data)
|
||||
return;
|
||||
|
||||
/* Create a waitable event. */
|
||||
pcap_log("PCAP: Creating event...\n");
|
||||
evt = thread_create_event();
|
||||
|
||||
/* As long as the channel is open.. */
|
||||
while (pcap != NULL) {
|
||||
/* Request ownership of the device. */
|
||||
network_wait(1);
|
||||
|
||||
if (pcap == NULL) {
|
||||
network_wait(0);
|
||||
break;
|
||||
}
|
||||
|
||||
if (network_get_wait() || (poll_card->set_link_state && poll_card->set_link_state(poll_card->priv)) || (poll_card->wait && poll_card->wait(poll_card->priv)))
|
||||
data = NULL;
|
||||
else
|
||||
data = (uint8_t *)f_pcap_next((void *)pcap, &h);
|
||||
if (data != NULL) {
|
||||
/* Received MAC. */
|
||||
mac_cmp32[0] = *(uint32_t *)(data+6);
|
||||
mac_cmp16[0] = *(uint16_t *)(data+10);
|
||||
|
||||
/* Local MAC. */
|
||||
mac_cmp32[1] = *(uint32_t *)mac;
|
||||
mac_cmp16[1] = *(uint16_t *)(mac+4);
|
||||
if ((mac_cmp32[0] != mac_cmp32[1]) ||
|
||||
(mac_cmp16[0] != mac_cmp16[1]))
|
||||
network_queue_put(0, poll_card->priv, data, h.caplen);
|
||||
else {
|
||||
/* Mark as invalid packet. */
|
||||
data = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait for the next packet to arrive - network_do_tx() is called from there. */
|
||||
tx = network_tx_queue_check();
|
||||
|
||||
/* Release ownership of the device. */
|
||||
network_wait(0);
|
||||
|
||||
/* If we did not get anything, wait a while. */
|
||||
if (!tx)
|
||||
thread_wait_event(evt, 10);
|
||||
}
|
||||
|
||||
/* No longer needed. */
|
||||
if (evt != NULL)
|
||||
thread_destroy_event(evt);
|
||||
|
||||
pcap_log("PCAP: polling stopped.\n");
|
||||
if (poll_state != NULL)
|
||||
thread_set_event(poll_state);
|
||||
network_rx_put(pcap->card, data, h.caplen);
|
||||
}
|
||||
|
||||
/* Send a packet to the Pcap interface. */
|
||||
void
|
||||
net_pcap_in(void *pcap, uint8_t *bufp, int len)
|
||||
{
|
||||
if (pcap == NULL)
|
||||
return;
|
||||
|
||||
f_pcap_sendpacket((void *)pcap, bufp, len);
|
||||
}
|
||||
|
||||
void
|
||||
net_pcap_in_available(void *priv)
|
||||
{
|
||||
net_pcap_t *pcap = (net_pcap_t *)priv;
|
||||
net_event_set(&pcap->tx_event);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
static void
|
||||
net_pcap_thread(void *priv)
|
||||
{
|
||||
net_pcap_t *pcap = (net_pcap_t*)priv;
|
||||
|
||||
pcap_log("PCAP: polling started.\n");
|
||||
|
||||
HANDLE events[NET_EVENT_MAX];
|
||||
events[NET_EVENT_STOP] = net_event_get_handle(&pcap->stop_event);
|
||||
events[NET_EVENT_TX] = net_event_get_handle(&pcap->tx_event);
|
||||
events[NET_EVENT_RX] = f_pcap_getevent((void *)pcap->pcap);
|
||||
|
||||
bool run = true;
|
||||
|
||||
while (run) {
|
||||
int ret = WaitForMultipleObjects(NET_EVENT_MAX, events, FALSE, INFINITE);
|
||||
|
||||
switch (ret - WAIT_OBJECT_0) {
|
||||
case NET_EVENT_STOP:
|
||||
net_event_clear(&pcap->stop_event);
|
||||
run = false;
|
||||
break;
|
||||
|
||||
case NET_EVENT_TX:
|
||||
net_event_clear(&pcap->tx_event);
|
||||
while (network_tx_pop(pcap->card, &pcap->pkt)) {
|
||||
net_pcap_in(pcap->pcap, pcap->pkt.data, pcap->pkt.len);
|
||||
}
|
||||
break;
|
||||
|
||||
case NET_EVENT_RX:
|
||||
net_pcap_read_packet(pcap);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pcap_log("PCAP: polling stopped.\n");
|
||||
}
|
||||
#else
|
||||
static void
|
||||
net_pcap_thread(void *priv)
|
||||
{
|
||||
net_pcap_t *pcap = (net_pcap_t*)priv;
|
||||
|
||||
pcap_log("PCAP: polling started.\n");
|
||||
|
||||
struct pollfd pfd[NET_EVENT_MAX];
|
||||
pfd[NET_EVENT_STOP].fd = net_event_get_fd(&pcap->stop_event);
|
||||
pfd[NET_EVENT_STOP].events = POLLIN | POLLPRI;
|
||||
|
||||
pfd[NET_EVENT_TX].fd = net_event_get_fd(&pcap->tx_event);
|
||||
pfd[NET_EVENT_TX].events = POLLIN | POLLPRI;
|
||||
|
||||
pfd[NET_EVENT_RX].fd = f_pcap_get_selectable_fd((void *) pcap->pcap);
|
||||
pfd[NET_EVENT_RX].events = POLLIN | POLLPRI;
|
||||
|
||||
/* As long as the channel is open.. */
|
||||
while (1) {
|
||||
poll(pfd, NET_EVENT_MAX, -1);
|
||||
|
||||
if (pfd[NET_EVENT_STOP].revents & POLLIN) {
|
||||
net_event_clear(&pcap->stop_event);
|
||||
break;
|
||||
}
|
||||
|
||||
if (pfd[NET_EVENT_TX].revents & POLLIN) {
|
||||
net_event_clear(&pcap->tx_event);
|
||||
|
||||
if (network_tx_pop(pcap->card, &pcap->pkt)) {
|
||||
net_pcap_in(pcap->pcap, pcap->pkt.data, pcap->pkt.len);
|
||||
}
|
||||
}
|
||||
|
||||
if (pfd[NET_EVENT_RX].revents & POLLIN) {
|
||||
net_pcap_read_packet(pcap);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pcap_log("PCAP: polling stopped.\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Prepare the (Win)Pcap module for use.
|
||||
@@ -244,18 +334,18 @@ net_pcap_prepare(netdev_t *list)
|
||||
pcap_if_t *devlist, *dev;
|
||||
int i = 0;
|
||||
|
||||
/* Local variables. */
|
||||
pcap = NULL;
|
||||
|
||||
/* Try loading the DLL. */
|
||||
#ifdef _WIN32
|
||||
pcap_handle = dynld_module("wpcap.dll", pcap_imports);
|
||||
libpcap_handle = dynld_module("wpcap.dll", pcap_imports);
|
||||
#elif defined __APPLE__
|
||||
pcap_handle = dynld_module("libpcap.dylib", pcap_imports);
|
||||
libpcap_handle = dynld_module("libpcap.dylib", pcap_imports);
|
||||
#else
|
||||
pcap_handle = dynld_module("libpcap.so", pcap_imports);
|
||||
libpcap_handle = dynld_module("libpcap.so", pcap_imports);
|
||||
#endif
|
||||
if (pcap_handle == NULL) return(-1);
|
||||
if (libpcap_handle == NULL) {
|
||||
pcap_log("PCAP: error loading pcap module\n");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
/* Retrieve the device list from the local machine */
|
||||
if (f_pcap_findalldevs(&devlist, errbuf) == -1) {
|
||||
@@ -292,141 +382,132 @@ net_pcap_prepare(netdev_t *list)
|
||||
/*
|
||||
* Initialize (Win)Pcap for use.
|
||||
*
|
||||
* This is called on every 'cycle' of the emulator,
|
||||
* if and as long the NetworkType is set to PCAP,
|
||||
* and also as long as we have a NetCard defined.
|
||||
*/
|
||||
int
|
||||
net_pcap_init(void)
|
||||
{
|
||||
char errbuf[PCAP_ERRBUF_SIZE];
|
||||
char *str;
|
||||
|
||||
/* Did we already load the library? */
|
||||
if (pcap_handle == NULL)
|
||||
return(-1);
|
||||
|
||||
/* Get the PCAP library name and version. */
|
||||
strcpy(errbuf, f_pcap_lib_version());
|
||||
str = strchr(errbuf, '(');
|
||||
if (str != NULL) *(str-1) = '\0';
|
||||
pcap_log("PCAP: initializing, %s\n", errbuf);
|
||||
|
||||
/* Get the value of our capture interface. */
|
||||
if ((network_host[0] == '\0') || !strcmp(network_host, "none")) {
|
||||
pcap_log("PCAP: no interface configured!\n");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
poll_tid = NULL;
|
||||
poll_state = NULL;
|
||||
poll_card = NULL;
|
||||
|
||||
return(0);
|
||||
}
|
||||
|
||||
|
||||
/* Close up shop. */
|
||||
void
|
||||
net_pcap_close(void)
|
||||
{
|
||||
void *pc;
|
||||
|
||||
if (pcap == NULL) return;
|
||||
|
||||
pcap_log("PCAP: closing.\n");
|
||||
|
||||
/* Tell the polling thread to shut down. */
|
||||
pc = (void *)pcap; pcap = NULL;
|
||||
|
||||
/* Tell the thread to terminate. */
|
||||
if (poll_tid != NULL) {
|
||||
/* Wait for the thread to finish. */
|
||||
pcap_log("PCAP: waiting for thread to end...\n");
|
||||
thread_wait_event(poll_state, -1);
|
||||
pcap_log("PCAP: thread ended\n");
|
||||
thread_destroy_event(poll_state);
|
||||
|
||||
poll_tid = NULL;
|
||||
poll_state = NULL;
|
||||
poll_card = NULL;
|
||||
}
|
||||
|
||||
/* OK, now shut down Pcap itself. */
|
||||
f_pcap_close(pc);
|
||||
pcap = NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Reset (Win)Pcap and activate it.
|
||||
*
|
||||
* This is called on every 'cycle' of the emulator,
|
||||
* if and as long the NetworkType is set to PCAP,
|
||||
* and also as long as we have a NetCard defined.
|
||||
*
|
||||
* We already know we have PCAP available, as this
|
||||
* is called when the network activates itself and
|
||||
* tries to attach to the network module.
|
||||
*/
|
||||
int
|
||||
net_pcap_reset(const netcard_t *card, uint8_t *mac)
|
||||
void *
|
||||
net_pcap_init(const netcard_t *card, const uint8_t *mac_addr, void *priv)
|
||||
{
|
||||
char errbuf[PCAP_ERRBUF_SIZE];
|
||||
char errbuf[PCAP_ERRBUF_SIZE];
|
||||
char *str;
|
||||
char filter_exp[255];
|
||||
struct bpf_program fp;
|
||||
|
||||
/* Open a PCAP live channel. */
|
||||
if ((pcap = f_pcap_open_live(network_host, /* interface name */
|
||||
1518, /* max packet size */
|
||||
1, /* promiscuous mode? */
|
||||
10, /* timeout in msec */
|
||||
errbuf)) == NULL) { /* error buffer */
|
||||
pcap_log(" Unable to open device: %s!\n", network_host);
|
||||
return(-1);
|
||||
char *intf_name = (char*)priv;
|
||||
|
||||
/* Did we already load the library? */
|
||||
if (libpcap_handle == NULL) {
|
||||
pcap_log("PCAP: net_pcap_init without handle.\n");
|
||||
return NULL;
|
||||
}
|
||||
if (f_pcap_setnonblock((void*)pcap, 1, errbuf) != 0)
|
||||
|
||||
/* Get the PCAP library name and version. */
|
||||
strcpy(errbuf, f_pcap_lib_version());
|
||||
str = strchr(errbuf, '(');
|
||||
if (str != NULL)
|
||||
*(str - 1) = '\0';
|
||||
pcap_log("PCAP: initializing, %s\n", errbuf);
|
||||
|
||||
/* Get the value of our capture interface. */
|
||||
if ((intf_name[0] == '\0') || !strcmp(intf_name, "none")) {
|
||||
pcap_log("PCAP: no interface configured!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pcap_log("PCAP: interface: %s\n", intf_name);
|
||||
|
||||
net_pcap_t *pcap = calloc(1, sizeof(net_pcap_t));
|
||||
pcap->card = (netcard_t *)card;
|
||||
memcpy(pcap->mac_addr, mac_addr, sizeof(pcap->mac_addr));
|
||||
|
||||
if ((pcap->pcap = f_pcap_create(intf_name, errbuf)) == NULL) {
|
||||
pcap_log(" Unable to open device: %s!\n", intf_name);
|
||||
free(pcap);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (f_pcap_setnonblock((void *) pcap->pcap, 1, errbuf) != 0)
|
||||
pcap_log("PCAP: failed nonblock %s\n", errbuf);
|
||||
|
||||
pcap_log("PCAP: interface: %s\n", network_host);
|
||||
if (f_pcap_set_immediate_mode((void *) pcap->pcap, 1) != 0)
|
||||
pcap_log("PCAP: error setting immediate mode\n");
|
||||
|
||||
if (f_pcap_set_promisc((void *) pcap->pcap, 1) != 0)
|
||||
pcap_log("PCAP: error enabling promiscuous mode\n");
|
||||
|
||||
if (f_pcap_set_snaplen((void *) pcap->pcap, NET_MAX_FRAME) != 0)
|
||||
pcap_log("PCAP: error setting snaplen\n");
|
||||
|
||||
if (f_pcap_activate((void *) pcap->pcap) != 0) {
|
||||
pcap_log("PCAP: failed pcap_activate");
|
||||
f_pcap_close((void *) pcap->pcap);
|
||||
free(pcap);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Create a MAC address based packet filter. */
|
||||
pcap_log("PCAP: installing filter for MAC=%02x:%02x:%02x:%02x:%02x:%02x\n",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
|
||||
sprintf(filter_exp,
|
||||
"( ((ether dst ff:ff:ff:ff:ff:ff) or (ether dst %02x:%02x:%02x:%02x:%02x:%02x)) and not (ether src %02x:%02x:%02x:%02x:%02x:%02x) )",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
if (f_pcap_compile((void *)pcap, &fp, filter_exp, 0, 0xffffffff) != -1) {
|
||||
if (f_pcap_setfilter((void *)pcap, &fp) != 0) {
|
||||
pcap_log("PCAP: error installing filter (%s) !\n", filter_exp);
|
||||
f_pcap_close((void *)pcap);
|
||||
return(-1);
|
||||
}
|
||||
"( ((ether dst ff:ff:ff:ff:ff:ff) or (ether dst %02x:%02x:%02x:%02x:%02x:%02x)) and not (ether src %02x:%02x:%02x:%02x:%02x:%02x) )",
|
||||
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5],
|
||||
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
|
||||
if (f_pcap_compile((void *) pcap->pcap, &fp, filter_exp, 0, 0xffffffff) != -1) {
|
||||
if (f_pcap_setfilter((void *) pcap->pcap, &fp) != 0) {
|
||||
pcap_log("PCAP: error installing filter (%s) !\n", filter_exp);
|
||||
f_pcap_close((void *) pcap->pcap);
|
||||
free(pcap);
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
pcap_log("PCAP: could not compile filter (%s) !\n", filter_exp);
|
||||
f_pcap_close((void *)pcap);
|
||||
return(-1);
|
||||
pcap_log("PCAP: could not compile filter (%s) : %s!\n", filter_exp, f_pcap_geterr((void*)pcap->pcap));
|
||||
f_pcap_close((void *) pcap->pcap);
|
||||
free(pcap);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Save the callback info. */
|
||||
poll_card = card;
|
||||
pcap->pkt.data = calloc(1, NET_MAX_FRAME);
|
||||
net_event_init(&pcap->tx_event);
|
||||
net_event_init(&pcap->stop_event);
|
||||
pcap->poll_tid = thread_create(net_pcap_thread, pcap);
|
||||
|
||||
pcap_log("PCAP: starting thread..\n");
|
||||
poll_state = thread_create_event();
|
||||
poll_tid = thread_create(poll_thread, mac);
|
||||
thread_wait_event(poll_state, -1);
|
||||
|
||||
return(0);
|
||||
return pcap;
|
||||
}
|
||||
|
||||
|
||||
/* Send a packet to the Pcap interface. */
|
||||
/* Close up shop. */
|
||||
void
|
||||
net_pcap_in(uint8_t *bufp, int len)
|
||||
net_pcap_close(void *priv)
|
||||
{
|
||||
if (pcap == NULL)
|
||||
return;
|
||||
if (!priv)
|
||||
return;
|
||||
|
||||
f_pcap_sendpacket((void *)pcap, bufp, len);
|
||||
net_pcap_t *pcap = (net_pcap_t *)priv;
|
||||
|
||||
pcap_log("PCAP: closing.\n");
|
||||
|
||||
/* Tell the thread to terminate. */
|
||||
net_event_set(&pcap->stop_event);
|
||||
|
||||
/* Wait for the thread to finish. */
|
||||
pcap_log("PCAP: waiting for thread to end...\n");
|
||||
thread_wait(pcap->poll_tid);
|
||||
pcap_log("PCAP: thread ended\n");
|
||||
|
||||
free(pcap->pkt.data);
|
||||
|
||||
/* OK, now shut down Pcap itself. */
|
||||
f_pcap_close((void*)pcap->pcap);
|
||||
|
||||
net_event_close(&pcap->tx_event);
|
||||
net_event_close(&pcap->stop_event);
|
||||
|
||||
free(pcap);
|
||||
}
|
||||
|
||||
const netdrv_t net_pcap_drv = {
|
||||
&net_pcap_in_available,
|
||||
&net_pcap_init,
|
||||
&net_pcap_close,
|
||||
NULL
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user