Network portability fixes - SLiRP and Pcap now both work on Linux and FreeBSD.

This commit is contained in:
waltje
2017-10-28 03:32:46 -04:00
parent 24d2c0866b
commit 4a1bd38435
3 changed files with 67 additions and 71 deletions

View File

@@ -8,7 +8,7 @@
* *
* Handle WinPcap library processing. * Handle WinPcap library processing.
* *
* Version: @(#)net_pcap.c 1.0.10 2017/10/16 * Version: @(#)net_pcap.c 1.0.11 2017/10/27
* *
* Author: Fred N. van Kempen, <decwiz@yahoo.com> * Author: Fred N. van Kempen, <decwiz@yahoo.com>
* *
@@ -79,9 +79,8 @@ poll_thread(void *arg)
uint16_t mac_cmp16[2]; uint16_t mac_cmp16[2];
event_t *evt; event_t *evt;
thread_set_event((event_t *) thread_started);
pclog("PCAP: polling thread started, arg %08lx\n", arg); pclog("PCAP: polling thread started, arg %08lx\n", arg);
thread_set_event((event_t *)thread_started);
/* Create a waitable event. */ /* Create a waitable event. */
evt = thread_create_event(); evt = thread_create_event();
@@ -93,7 +92,7 @@ poll_thread(void *arg)
network_wait_for_poll(); network_wait_for_poll();
/* Wait for the next packet to arrive. */ /* Wait for the next packet to arrive. */
data = f_pcap_next((pcap_t *) pcap, &h); data = f_pcap_next((pcap_t *)pcap, &h);
if (data != NULL) { if (data != NULL) {
/* Received MAC. */ /* Received MAC. */
mac_cmp32[0] = *(uint32_t *)(data+6); mac_cmp32[0] = *(uint32_t *)(data+6);
@@ -105,7 +104,7 @@ poll_thread(void *arg)
if ((mac_cmp32[0] != mac_cmp32[1]) || if ((mac_cmp32[0] != mac_cmp32[1]) ||
(mac_cmp16[0] != mac_cmp16[1])) { (mac_cmp16[0] != mac_cmp16[1])) {
if (poll_rx != NULL) if (poll_rx != NULL)
poll_rx((void *) poll_arg, (uint8_t *)data, h.caplen); poll_rx((void *)poll_arg, (uint8_t *)data, h.caplen);
} else { } else {
/* Mark as invalid packet. */ /* Mark as invalid packet. */
data = NULL; data = NULL;
@@ -137,7 +136,11 @@ network_pcap_init(netdev_t *list)
pcap = NULL; pcap = NULL;
/* Try loading the DLL. */ /* Try loading the DLL. */
#ifdef WIN32
pcap_handle = dynld_module("wpcap.dll", pcap_imports); pcap_handle = dynld_module("wpcap.dll", pcap_imports);
#else
pcap_handle = dynld_module("libpcap.so", pcap_imports);
#endif
if (pcap_handle == NULL) return(-1); if (pcap_handle == NULL) return(-1);
/* Retrieve the device list from the local machine */ /* Retrieve the device list from the local machine */
@@ -167,7 +170,12 @@ void
network_pcap_reset(void) network_pcap_reset(void)
{ {
/* Try loading the DLL. */ /* Try loading the DLL. */
#ifdef WIN32
pcap_handle = dynld_module("wpcap.dll", pcap_imports); pcap_handle = dynld_module("wpcap.dll", pcap_imports);
#else
pcap_handle = dynld_module("libpcap.so", pcap_imports);
#endif
return; return;
} }
@@ -184,42 +192,24 @@ network_pcap_setup(uint8_t *mac, NETRXCB func, void *arg)
/* Did we already load the DLL? */ /* Did we already load the DLL? */
if (pcap_handle == NULL) return(-1); if (pcap_handle == NULL) return(-1);
#if 1
/* Get the value of our capture interface. */ /* Get the value of our capture interface. */
dev = network_pcap; dev = network_pcap;
if (dev == NULL) { if ((dev == NULL) || (dev[0] == '\0') || !strcmp(dev, "none")) {
pclog(" PCap device is a null pointer!\n"); pclog(" No PCap interface configured!\n");
return(-1);
}
if ((dev[0] == '\0') || !strcmp(dev, "none")) {
pclog(" No network device configured!\n");
return(-1); return(-1);
} }
pclog(" Network interface: '%s'\n", dev); pclog(" Network interface: '%s'\n", dev);
#endif
strcpy(temp, f_pcap_lib_version()); strcpy(temp, f_pcap_lib_version());
dev = strchr(temp, '('); dev = strchr(temp, '(');
if (dev != NULL) *(dev-1) = '\0'; if (dev != NULL) *(dev-1) = '\0';
pclog("PCAP: initializing, %s\n", temp); pclog("PCAP: initializing, %s\n", temp);
#if 0 pcap = f_pcap_open_live(dev, /* interface name */
/* Get the value of our capture interface. */ 1518, /* maximum packet size */
dev = network_pcap; 1, /* promiscuous mode? */
if ((dev[0] == '\0') || !strcmp(dev, "none")) { 10, /* timeout in msec */
pclog(" No network device configured!\n"); temp); /* error buffer */
return(-1);
}
pclog(" Network interface: '%s'\n", dev);
#else
dev = network_pcap;
#endif
pcap = f_pcap_open_live(dev, /* interface name */
1518, /* maximum packet size */
1, /* promiscuous mode? */
10, /* timeout in msec */
temp); /* error buffer */
if (pcap == NULL) { if (pcap == NULL) {
pclog(" Unable to open device: %s!\n", temp); pclog(" Unable to open device: %s!\n", temp);
return(-1); return(-1);
@@ -232,15 +222,20 @@ network_pcap_setup(uint8_t *mac, NETRXCB func, void *arg)
"( ((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) )", "( ((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],
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
#ifdef WIN32
if (f_pcap_compile((pcap_t *) pcap, &fp, filter_exp, 0, 0xffffffff) != -1) { if (f_pcap_compile((pcap_t *) pcap, &fp, filter_exp, 0, 0xffffffff) != -1) {
if (f_pcap_setfilter((pcap_t *) pcap, &fp) == -1) { if (f_pcap_setfilter((pcap_t *) pcap, &fp) == -1) {
#else
if (f_pcap_compile((pcap_t *) pcap, &fp, filter_exp, 0, 0xffffffff) != 0) {
if (f_pcap_setfilter((pcap_t *) pcap, &fp) == 0) {
#endif
pclog(" Error installing filter (%s) !\n", filter_exp); pclog(" Error installing filter (%s) !\n", filter_exp);
f_pcap_close((pcap_t *) pcap); f_pcap_close((pcap_t *) pcap);
return (-1); return (-1);
} }
} else { } else {
pclog(" Could not compile filter (%s) !\n", filter_exp); pclog(" Could not compile filter (%s) !\n", filter_exp);
f_pcap_close((pcap_t *) pcap); f_pcap_close((pcap_t *)pcap);
return (-1); return (-1);
} }
@@ -250,12 +245,10 @@ network_pcap_setup(uint8_t *mac, NETRXCB func, void *arg)
network_thread_init(); network_thread_init();
thread_started = thread_create_event();
pclog(" Starting thread..\n"); pclog(" Starting thread..\n");
thread_started = thread_create_event();
poll_tid = thread_create(poll_thread, mac); poll_tid = thread_create(poll_thread, mac);
thread_wait_event((event_t *)thread_started, -1);
thread_wait_event((event_t *) thread_started, -1);
return(0); return(0);
} }
@@ -265,52 +258,38 @@ network_pcap_setup(uint8_t *mac, NETRXCB func, void *arg)
void void
network_pcap_close(void) network_pcap_close(void)
{ {
volatile pcap_t *pc; pcap_t *pc;
if (pcap != NULL) { if (pcap != NULL) {
pclog("Closing WinPcap\n"); pclog("Closing WinPcap\n");
/* Tell the polling thread to shut down. */ /* Tell the polling thread to shut down. */
pc = pcap; pcap = NULL; pc = (pcap_t *)pcap; pcap = NULL;
#if 0
#if 1
/* Terminate the polling thread. */
if (poll_tid != NULL) {
thread_kill(poll_tid);
poll_tid = NULL;
}
#else
/* Wait for the polling thread to shut down. */
while (poll_tid != NULL)
;
#endif
#endif
/* Tell the thread to terminate. */ /* Tell the thread to terminate. */
if (poll_tid != NULL) { if (poll_tid != NULL) {
network_busy(0); network_busy(0);
pclog("Waiting for network thread to end...\n");
/* Wait for the end event. */ /* Wait for the end event. */
network_wait_for_end((void *) poll_tid); pclog("Waiting for network thread to end...\n");
network_wait_for_end((void *)poll_tid);
pclog("Network thread ended\n"); pclog("Network thread ended\n");
poll_tid = NULL; poll_tid = NULL;
} }
if (thread_started) { if (thread_started) {
thread_destroy_event((event_t *) thread_started); thread_destroy_event((event_t *)thread_started);
thread_started = NULL; thread_started = NULL;
} }
/* OK, now shut down WinPcap itself. */ /* OK, now shut down WinPcap itself. */
f_pcap_close((pcap_t *) pc); f_pcap_close((pcap_t *)pc);
pc = pcap = NULL; pcap = NULL;
/* Unload the DLL if possible. */ /* Unload the DLL if possible. */
if (pcap_handle != NULL) { if (pcap_handle != NULL) {
dynld_close((void *) pcap_handle); dynld_close((void *)pcap_handle);
pcap_handle = NULL; pcap_handle = NULL;
} }
} }
@@ -322,15 +301,15 @@ network_pcap_close(void)
void void
network_pcap_stop(void) network_pcap_stop(void)
{ {
/* OK, now shut down WinPcap itself. */ /* OK, now shut down WinPcap itself. */
f_pcap_close((pcap_t *) pcap); f_pcap_close((pcap_t *)pcap);
pcap = NULL; pcap = NULL;
/* Unload the DLL if possible. */ /* Unload the DLL if possible. */
if (pcap_handle != NULL) { if (pcap_handle != NULL) {
dynld_close((void *) pcap_handle); dynld_close((void *)pcap_handle);
pcap_handle = NULL; pcap_handle = NULL;
} }
} }

View File

@@ -6,21 +6,26 @@
* *
* This file is part of the 86Box distribution. * This file is part of the 86Box distribution.
* *
* Simple program to show usage of WinPcap. * Simple program to show usage of (Win)Pcap.
* *
* Based on the "libpcap" examples. * Based on the "libpcap" examples.
* *
* Version: @(#)pcap_if.c 1.0.5 2017/10/10 * Version: @(#)pcap_if.c 1.0.6 2017/10/27
* *
* Author: Fred N. van Kempen, <decwiz@yahoo.com> * Author: Fred N. van Kempen, <decwiz@yahoo.com>
* *
* Copyright 2017 Fred N. van Kempen. * Copyright 2017 Fred N. van Kempen.
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <ctype.h> #include <ctype.h>
#include <pcap.h> #include <pcap.h>
#include <time.h>
#include "../86box.h"
#include "../plat.h"
#include "../plat_dynld.h" #include "../plat_dynld.h"
@@ -46,12 +51,12 @@ static dllimp_t pcap_imports[] = {
typedef struct { typedef struct {
char device[128]; char device[128];
char description[128]; char description[128];
} dev_t; } capdev_t;
/* Retrieve an easy-to-use list of devices. */ /* Retrieve an easy-to-use list of devices. */
static int static int
get_devlist(dev_t *list) get_devlist(capdev_t *list)
{ {
char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *devlist, *dev; pcap_if_t *devlist, *dev;
@@ -192,7 +197,7 @@ start_cap(char *dev)
/* Show a list of available network interfaces. */ /* Show a list of available network interfaces. */
static void static void
show_devs(dev_t *list, int num) show_devs(capdev_t *list, int num)
{ {
int i; int i;
@@ -228,13 +233,21 @@ pclog(const char *fmt, ...)
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
dev_t interfaces[32]; capdev_t interfaces[32];
int numdev, i; int numdev, i;
/* Try loading the DLL. */ /* Try loading the DLL. */
#ifdef WIN32
pcap_handle = dynld_module("wpcap.dll", pcap_imports); pcap_handle = dynld_module("wpcap.dll", pcap_imports);
#else
pcap_handle = dynld_module("libpcap.so", pcap_imports);
#endif
if (pcap_handle == NULL) { if (pcap_handle == NULL) {
#ifdef WIN32
fprintf(stderr, "Unable to load WinPcap DLL !\n"); fprintf(stderr, "Unable to load WinPcap DLL !\n");
#else
fprintf(stderr, "Unable to load libpcap.so !\n");
#endif
return(1); return(1);
} }

View File

@@ -222,7 +222,11 @@ void* fluidsynth_init(device_t *info)
memset(data, 0, sizeof(fluidsynth_t)); memset(data, 0, sizeof(fluidsynth_t));
/* Try loading the DLL. */ /* Try loading the DLL. */
#ifdef WIN32
fluidsynth_handle = dynld_module("libfluidsynth.dll", fluidsynth_imports); fluidsynth_handle = dynld_module("libfluidsynth.dll", fluidsynth_imports);
#else
fluidsynth_handle = dynld_module("libfluidsynth.so", fluidsynth_imports);
#endif
if (fluidsynth_handle == NULL) if (fluidsynth_handle == NULL)
{ {
ui_msgbox(MBX_ERROR, (wchar_t *)IDS_2171); ui_msgbox(MBX_ERROR, (wchar_t *)IDS_2171);