Cleanup, mostly network threading madness. Statusbar is now created at hard reset, IF the configuration has changed. Other minor things.

This commit is contained in:
waltje
2017-10-29 04:20:20 -05:00
parent f4367133e7
commit bfe038ded9
37 changed files with 506 additions and 600 deletions

View File

@@ -10,7 +10,7 @@
*
* NOTE: The file will also implement an NE1000 for 8-bit ISA systems.
*
* Version: @(#)net_ne2000.c 1.0.20 2017/10/19
* Version: @(#)net_ne2000.c 1.0.21 2017/10/28
*
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
* Peter Grehan, grehan@iprg.nokia.com>
@@ -1673,8 +1673,6 @@ mcast_index(const void *dst)
static void
nic_tx(nic_t *dev, uint32_t val)
{
ui_sb_update_icon(SB_NETWORK, 1);
dev->CR.tx_packet = 0;
dev->TSR.tx_ok = 1;
dev->ISR.pkt_tx = 1;
@@ -1683,8 +1681,6 @@ nic_tx(nic_t *dev, uint32_t val)
if (dev->IMR.tx_inte)
nic_interrupt(dev, 1);
dev->tx_timer_active = 0;
ui_sb_update_icon(SB_NETWORK, 0);
}
@@ -1705,6 +1701,7 @@ nic_rx(void *priv, uint8_t *buf, int io_len)
int idx, nextpage;
int endbytes;
//FIXME: move to upper layer
ui_sb_update_icon(SB_NETWORK, 1);
if (io_len != 60)
@@ -1736,6 +1733,7 @@ nic_rx(void *priv, uint8_t *buf, int io_len)
) {
nelog(1, "%s: no space\n", dev->name);
//FIXME: move to upper layer
ui_sb_update_icon(SB_NETWORK, 0);
return;
}
@@ -1743,6 +1741,7 @@ nic_rx(void *priv, uint8_t *buf, int io_len)
if ((io_len < 40/*60*/) && !dev->RCR.runts_ok) {
nelog(1, "%s: rejected small packet, length %d\n", dev->name, io_len);
//FIXME: move to upper layer
ui_sb_update_icon(SB_NETWORK, 0);
return;
}
@@ -1765,6 +1764,7 @@ nic_rx(void *priv, uint8_t *buf, int io_len)
if (! dev->RCR.broadcast) {
nelog(2, "%s: RX BC disabled\n", dev->name);
//FIXME: move to upper layer
ui_sb_update_icon(SB_NETWORK, 0);
return;
}
@@ -1778,6 +1778,7 @@ nic_rx(void *priv, uint8_t *buf, int io_len)
nelog(2, "%s: RX MC disabled\n", dev->name);
#endif
//FIXME: move to upper layer
ui_sb_update_icon(SB_NETWORK, 0);
return;
}
@@ -1787,6 +1788,7 @@ nic_rx(void *priv, uint8_t *buf, int io_len)
if (! (dev->mchash[idx>>3] & (1<<(idx&0x7)))) {
nelog(2, "%s: RX MC not listed\n", dev->name);
//FIXME: move to upper layer
ui_sb_update_icon(SB_NETWORK, 0);
return;
}
@@ -1833,6 +1835,7 @@ nic_rx(void *priv, uint8_t *buf, int io_len)
if (dev->IMR.rx_inte)
nic_interrupt(dev, 1);
//FIXME: move to upper layer
ui_sb_update_icon(SB_NETWORK, 0);
}
@@ -2030,29 +2033,12 @@ nic_init(device_t *info)
/* Reset the board. */
nic_reset(dev);
if (network_attach(dev, dev->physaddr, nic_rx) < 0) {
#if 0
msgbox_error_wstr(ghwnd, L"Unable to init platform network");
#endif
nelog(0, "%s: unable to init platform network type %d\n",
dev->name, network_type);
#if 0
/*
* To avoid crashes, we ignore the fact that even though
* there is no active platform support, we just continue
* initializing. If we return an error here, the device
* handling code will throw a fatal error... --FvK
*/
free(dev);
return(NULL);
#endif
}
/* Attach ourselves to the network module. */
network_attach(dev, dev->physaddr, nic_rx);
nelog(1, "%s: %s attached IO=0x%X IRQ=%d\n", dev->name,
dev->is_pci?"PCI":"ISA", dev->base_address, dev->base_irq);
ui_sb_update_icon(SB_NETWORK, 0);
return(dev);
}

View File

@@ -29,18 +29,11 @@
#include "network.h"
static volatile
void *pcap_handle; /* handle to WinPcap DLL */
static volatile
pcap_t *pcap; /* handle to WinPcap library */
static volatile
thread_t *poll_tid;
static volatile
NETRXCB poll_rx; /* network RX function to call */
static volatile
void *poll_arg; /* network RX function arg */
static volatile
event_t *thread_started;
static volatile void *pcap_handle; /* handle to WinPcap DLL */
static volatile pcap_t *pcap; /* handle to WinPcap library */
static volatile thread_t *poll_tid;
static netcard_t *poll_card; /* netcard linked to us */
static event_t *poll_state;
/* Pointers to the real functions. */
@@ -73,26 +66,28 @@ static void
poll_thread(void *arg)
{
uint8_t *mac = (uint8_t *)arg;
const uint8_t *data = NULL;
uint8_t *data = NULL;
struct pcap_pkthdr h;
uint32_t mac_cmp32[2];
uint16_t mac_cmp16[2];
event_t *evt;
pclog("PCAP: polling thread started, arg %08lx\n", arg);
thread_set_event((event_t *)thread_started);
pclog("PCAP: polling started.\n");
thread_set_event(poll_state);
/* Create a waitable event. */
evt = thread_create_event();
/* As long as the channel is open.. */
while (pcap != NULL) {
network_mutex_wait(1);
/* Request ownership of the device. */
network_wait(1);
network_wait_for_poll();
/* Wait for a poll request. */
network_poll();
/* Wait for the next packet to arrive. */
data = f_pcap_next((pcap_t *)pcap, &h);
data = (uint8_t *)f_pcap_next((pcap_t *)pcap, &h);
if (data != NULL) {
/* Received MAC. */
mac_cmp32[0] = *(uint32_t *)(data+6);
@@ -103,8 +98,8 @@ poll_thread(void *arg)
mac_cmp16[1] = *(uint16_t *)(mac+4);
if ((mac_cmp32[0] != mac_cmp32[1]) ||
(mac_cmp16[0] != mac_cmp16[1])) {
if (poll_rx != NULL)
poll_rx((void *)poll_arg, (uint8_t *)data, h.caplen);
poll_card->rx(poll_card->priv, data, h.caplen);
} else {
/* Mark as invalid packet. */
data = NULL;
@@ -115,18 +110,27 @@ poll_thread(void *arg)
if (data == NULL)
thread_wait_event(evt, 10);
network_mutex_wait(0);
/* Release ownership of the device. */
network_wait(0);
}
/* No longer needed. */
thread_destroy_event(evt);
pclog("PCAP: polling stopped.\n");
thread_set_event(poll_state);
}
/* Initialize the (Win)Pcap module for use. */
/*
* Prepare the (Win)Pcap module for use.
*
* This is called only once, during application init,
* to check for availability of PCAP, and to retrieve
* a list of (usable) intefaces for it.
*/
int
network_pcap_init(netdev_t *list)
net_pcap_prepare(netdev_t *list)
{
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t *devlist, *dev;
@@ -136,7 +140,7 @@ network_pcap_init(netdev_t *list)
pcap = NULL;
/* Try loading the DLL. */
#ifdef WIN32
#ifdef _WIN32
pcap_handle = dynld_module("wpcap.dll", pcap_imports);
#else
pcap_handle = dynld_module("libpcap.so", pcap_imports);
@@ -165,84 +169,49 @@ network_pcap_init(netdev_t *list)
}
/* Initialize the (Win)Pcap module for use. */
// FIXME: this will be deleted. --FvK
void
network_pcap_reset(void)
/*
* 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)
{
/* Try loading the DLL. */
#ifdef WIN32
char errbuf[PCAP_ERRBUF_SIZE];
char *str;
/* Did we already load the library? */
if (pcap_handle == NULL) return(-1);
#if 0
// no, we don't..
/* Load the DLL if needed. We already know it exists. */
#ifdef _WIN32
pcap_handle = dynld_module("wpcap.dll", pcap_imports);
#else
pcap_handle = dynld_module("libpcap.so", pcap_imports);
#endif
}
/* Initialize WinPcap for us. */
int
network_pcap_setup(uint8_t *mac, NETRXCB func, void *arg)
{
char temp[PCAP_ERRBUF_SIZE];
char filter_exp[255];
struct bpf_program fp;
char *dev;
/* Did we already load the DLL? */
if (pcap_handle == NULL) return(-1);
#endif
/* Get the PCAP library name and version. */
strcpy(errbuf, f_pcap_lib_version());
str = strchr(errbuf, '(');
if (str != NULL) *(str-1) = '\0';
pclog("PCAP: initializing, %s\n", errbuf);
/* Get the value of our capture interface. */
dev = network_pcap;
if ((dev == NULL) || (dev[0] == '\0') || !strcmp(dev, "none")) {
pclog(" No PCap interface configured!\n");
return(-1);
}
pclog(" Network interface: '%s'\n", dev);
strcpy(temp, f_pcap_lib_version());
dev = strchr(temp, '(');
if (dev != NULL) *(dev-1) = '\0';
pclog("PCAP: initializing, %s\n", temp);
pcap = f_pcap_open_live(network_pcap, /* interface name */
1518, /* maximum packet size */
1, /* promiscuous mode? */
10, /* timeout in msec */
temp); /* error buffer */
if (pcap == NULL) {
pclog(" Unable to open device: %s!\n", network_pcap);
if ((network_pcap == NULL) ||
(network_pcap[0] == '\0') ||
!strcmp(network_pcap, "none")) {
pclog("PCAP: no interface configured!\n");
return(-1);
}
/* Create a MAC address based packet filter. */
pclog(" Installing packet filter for MAC=%02x:%02x:%02x:%02x:%02x:%02x\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[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((pcap_t *) pcap, &fp, filter_exp, 0, 0xffffffff) != -1) {
if (f_pcap_setfilter((pcap_t *)pcap, &fp) != 0) {
pclog(" Error installing filter (%s) !\n", filter_exp);
f_pcap_close((pcap_t *)pcap);
return(-1);
}
} else {
pclog(" Could not compile filter (%s) !\n", filter_exp);
f_pcap_close((pcap_t *)pcap);
return (-1);
}
/* Save the callback info. */
poll_rx = func;
poll_arg = arg;
network_thread_init();
pclog(" Starting thread..\n");
thread_started = thread_create_event();
poll_tid = thread_create(poll_thread, mac);
thread_wait_event((event_t *)thread_started, -1);
poll_tid = NULL;
poll_state = NULL;
poll_card = NULL;
return(0);
}
@@ -250,166 +219,119 @@ network_pcap_setup(uint8_t *mac, NETRXCB func, void *arg)
/* Close up shop. */
void
network_pcap_close(void)
net_pcap_close(void)
{
pcap_t *pc;
if (pcap != NULL) {
pclog("Closing WinPcap\n");
if (pcap == NULL) return;
/* Tell the polling thread to shut down. */
pc = (pcap_t *)pcap; pcap = NULL;
pclog("PCAP: closing.\n");
/* Tell the thread to terminate. */
if (poll_tid != NULL) {
network_busy(0);
/* Tell the polling thread to shut down. */
pc = (pcap_t *)pcap; pcap = NULL;
/* Wait for the end event. */
pclog("Waiting for network thread to end...\n");
network_wait_for_end((void *)poll_tid);
pclog("Network thread ended\n");
/* Tell the thread to terminate. */
if (poll_tid != NULL) {
network_busy(0);
poll_tid = NULL;
}
/* Wait for the thread to finish. */
pclog("PCAP: waiting for thread to end...\n");
thread_wait_event(poll_state, -1);
pclog("PCAP: thread ended\n");
thread_destroy_event(poll_state);
if (thread_started) {
thread_destroy_event((event_t *)thread_started);
thread_started = NULL;
}
/* OK, now shut down WinPcap itself. */
f_pcap_close(pc);
pcap = NULL;
/* Unload the DLL if possible. */
if (pcap_handle != NULL) {
dynld_close((void *)pcap_handle);
pcap_handle = NULL;
}
poll_tid = NULL;
poll_state = NULL;
poll_card = NULL;
}
poll_rx = NULL;
poll_arg = NULL;
}
void
network_pcap_stop(void)
{
/* OK, now shut down WinPcap itself. */
f_pcap_close((pcap_t *)pcap);
/* OK, now shut down Pcap itself. */
f_pcap_close(pc);
pcap = NULL;
#if 0
// no, we don't..
/* Unload the DLL if possible. */
if (pcap_handle != NULL) {
dynld_close((void *)pcap_handle);
pcap_handle = NULL;
}
#endif
}
/* Test WinPcap - 1 = success, 0 = failure. */
// FIXME: this will be deleted. --FvK
/*
* 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
network_pcap_test(void)
net_pcap_reset(netcard_t *card)
{
char temp[PCAP_ERRBUF_SIZE];
char errbuf[PCAP_ERRBUF_SIZE];
char filter_exp[255];
struct bpf_program fp;
char *dev;
/* Did we already load the DLL? */
if (pcap_handle == NULL)
{
pcap_handle = dynld_module("wpcap.dll", pcap_imports);
}
#if 1
/* Get the value of our capture interface. */
dev = network_pcap;
if (dev == NULL) {
pclog(" PCap device is a null pointer!\n");
return 0;
}
if ((dev[0] == '\0') || !strcmp(dev, "none")) {
pclog(" No network device configured!\n");
return 0;
}
pclog(" Network interface: '%s'\n", dev);
#endif
strcpy(temp, f_pcap_lib_version());
dev = strchr(temp, '(');
if (dev != NULL) *(dev-1) = '\0';
pclog("PCAP: initializing, %s\n", temp);
#if 0
/* Get the value of our capture interface. */
dev = network_pcap;
if ((dev[0] == '\0') || !strcmp(dev, "none")) {
pclog(" No network device configured!\n");
/* Unload the DLL if possible. */
if (pcap_handle != NULL) {
dynld_close((void *) pcap_handle);
pcap_handle = NULL;
}
return 0;
}
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) {
pclog(" Unable to open device: %s!\n", temp);
/* Unload the DLL if possible. */
if (pcap_handle != NULL) {
dynld_close((void *) pcap_handle);
pcap_handle = NULL;
}
return 0;
/* Open a PCAP live channel. */
if ((pcap = f_pcap_open_live(network_pcap, /* interface name */
1518, /* max packet size */
1, /* promiscuous mode? */
10, /* timeout in msec */
errbuf)) == NULL) { /* error buffer */
pclog(" Unable to open device: %s!\n", network_pcap);
return(-1);
}
pclog("PCAP: interface: %s\n", network_pcap);
/* Create a MAC address based packet filter. */
pclog("PCAP: installing filter for MAC=%02x:%02x:%02x:%02x:%02x:%02x\n",
card->mac[0], card->mac[1], card->mac[2],
card->mac[3], card->mac[4], card->mac[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) )",
0, 1, 2, 3, 4, 5,
0, 1, 2, 3, 4, 5);
if (f_pcap_compile((pcap_t *) pcap, &fp, filter_exp, 0, 0xffffffff) != -1) {
if (f_pcap_setfilter((pcap_t *) pcap, &fp) == -1) {
pclog(" Error installing filter (%s) !\n", filter_exp);
network_pcap_stop();
return 0;
card->mac[0], card->mac[1], card->mac[2],
card->mac[3], card->mac[4], card->mac[5],
card->mac[0], card->mac[1], card->mac[2],
card->mac[3], card->mac[4], card->mac[5]);
if (f_pcap_compile((pcap_t *)pcap, &fp, filter_exp, 0, 0xffffffff) != -1) {
if (f_pcap_setfilter((pcap_t *)pcap, &fp) != 0) {
pclog("PCAP: error installing filter (%s) !\n", filter_exp);
f_pcap_close((pcap_t *)pcap);
return(-1);
}
} else {
pclog(" Could not compile filter (%s) !\n", filter_exp);
network_pcap_stop();
return 0;
pclog("PCAP: could not compile filter (%s) !\n", filter_exp);
f_pcap_close((pcap_t *)pcap);
return(-1);
}
network_pcap_stop();
/* Save the callback info. */
poll_card = card;
return 1;
pclog("PCAP: starting thread..\n");
poll_state = thread_create_event();
poll_tid = thread_create(poll_thread, card->mac);
thread_wait_event(poll_state, -1);
return(0);
}
/* Send a packet to the Pcap interface. */
void
network_pcap_in(uint8_t *bufp, int len)
net_pcap_in(uint8_t *bufp, int len)
{
if (pcap != NULL) {
network_busy(1);
if (pcap == NULL) return;
f_pcap_sendpacket((pcap_t *) pcap, bufp, len);
network_busy(1);
network_busy(0);
}
f_pcap_sendpacket((pcap_t *)pcap, bufp, len);
network_busy(0);
}

View File

@@ -8,7 +8,7 @@
*
* Handle SLiRP library processing.
*
* Version: @(#)net_slirp.c 1.0.10 2017/10/16
* Version: @(#)net_slirp.c 1.0.11 2017/10/28
*
* Author: Fred N. van Kempen, <decwiz@yahoo.com>
*
@@ -29,22 +29,12 @@
#include "network.h"
static volatile
queueADT slirpq; /* SLiRP library handle */
static volatile
thread_t *poll_tid;
static volatile
NETRXCB poll_rx; /* network RX function to call */
static volatile
void *poll_arg; /* network RX function arg */
static volatile
event_t *thread_started;
static volatile queueADT slirpq; /* SLiRP library handle */
static volatile thread_t *poll_tid;
static netcard_t *poll_card; /* netcard attached to us */
static event_t *poll_state;
/* Instead of calling this and crashing some times
or experencing jitter, this is called by the
60Hz clock which seems to do the job. */
static void
slirp_tic(void)
{
@@ -76,60 +66,60 @@ slirp_tic(void)
/* Handle the receiving of frames. */
static void
poll_thread(void *arg)
poll_thread(UNUSED(void *arg))
{
struct queuepacket *qp;
event_t *evt;
thread_set_event((event_t *) thread_started);
pclog("SLiRP: polling thread started, arg %08lx\n", arg);
pclog("SLiRP: polling started.\n");
thread_set_event(poll_state);
/* Create a waitable event. */
evt = thread_create_event();
while (slirpq != NULL) {
network_mutex_wait(1);
/* Request ownership of the queue. */
network_wait(1);
network_wait_for_poll();
/* Wait for a poll request. */
network_poll();
/* See if there is any work. */
slirp_tic();
/* Wait for the next packet to arrive. */
if (QueuePeek(slirpq) == 0) {
/* If we did not get anything, wait a while. */
thread_wait_event(evt, 10);
network_mutex_wait(0);
continue;
}
/* Grab a packet from the queue. */
qp = QueueDelete(slirpq);
if (QueuePeek(slirpq) != 0) {
/* Grab a packet from the queue. */
qp = QueueDelete(slirpq);
#if 0
pclog("SLiRP: inQ:%d got a %dbyte packet @%08lx\n",
pclog("SLiRP: inQ:%d got a %dbyte packet @%08lx\n",
QueuePeek(slirpq), qp->len, qp);
#endif
if (poll_rx != NULL)
poll_rx((void *) poll_arg, (uint8_t *)&qp->data, qp->len);
poll_card->rx(poll_card->priv, (uint8_t *)qp->data, qp->len);
/* Done with this one. */
free(qp);
/* Done with this one. */
free(qp);
} else {
/* If we did not get anything, wait a while. */
thread_wait_event(evt, 10);
}
network_mutex_wait(0);
/* Release ownership of the queue. */
network_wait(0);
}
/* No longer needed. */
thread_destroy_event(evt);
pclog("SLiRP: polling stopped.\n");
thread_set_event(poll_state);
}
/* Initialize SLiRP for us. */
/* Initialize SLiRP for use. */
int
network_slirp_setup(uint8_t *mac, NETRXCB func, void *arg)
net_slirp_init(void)
{
pclog("SLiRP: initializing..\n");
@@ -139,107 +129,79 @@ network_slirp_setup(uint8_t *mac, NETRXCB func, void *arg)
}
slirpq = QueueCreate();
pclog(" Packet queue is at %08lx\n", &slirpq);
poll_tid = NULL;
poll_state = NULL;
poll_card = NULL;
return(0);
}
/* Initialize SLiRP for use. */
int
net_slirp_reset(netcard_t *card)
{
/* Save the callback info. */
poll_rx = func;
poll_arg = arg;
poll_card = card;
network_thread_init();
thread_started = thread_create_event();
pclog("SLiRP: starting thread..\n");
poll_tid = thread_create(poll_thread, mac);
thread_wait_event((event_t *) thread_started, -1);
pclog("SLiRP: creating thread..\n");
poll_state = thread_create_event();
poll_tid = thread_create(poll_thread, card->mac);
thread_wait_event(poll_state, -1);
return(0);
}
void
network_slirp_close(void)
net_slirp_close(void)
{
queueADT sl;
if (slirpq != NULL) {
pclog("Closing SLiRP\n");
if (slirpq == NULL) return;
/* Tell the polling thread to shut down. */
sl = slirpq; slirpq = NULL;
pclog("SLiRP: closing.\n");
#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 polling thread to shut down. */
sl = slirpq; slirpq = NULL;
/* Tell the thread to terminate. */
if (poll_tid != NULL) {
network_busy(0);
/* Tell the thread to terminate. */
if (poll_tid != NULL) {
network_busy(0);
pclog("Waiting for network thread to end...\n");
/* Wait for the end event. */
network_wait_for_end((void *) poll_tid);
pclog("Network thread ended\n");
/* Wait for the thread to finish. */
pclog("SLiRP: waiting for thread to end...\n");
thread_wait_event(poll_state, -1);
pclog("SLiRP: thread ended\n");
thread_destroy_event(poll_state);
poll_tid = NULL;
}
if (thread_started) {
thread_destroy_event((event_t *) thread_started);
thread_started = NULL;
}
/* OK, now shut down SLiRP itself. */
QueueDestroy(sl);
slirp_exit(0);
poll_tid = NULL;
poll_state = NULL;
poll_card = NULL;
}
poll_rx = NULL;
poll_arg = NULL;
}
/* Test SLiRP - 1 = success, 0 = failure. */
int
network_slirp_test(void)
{
if (slirp_init() != 0) {
pclog("SLiRP could not be initialized!\n");
return 0;
}
else
{
slirp_exit(0);
return 1;
}
/* OK, now shut down SLiRP itself. */
QueueDestroy(sl);
slirp_exit(0);
}
/* Send a packet to the SLiRP interface. */
void
network_slirp_in(uint8_t *pkt, int pkt_len)
net_slirp_in(uint8_t *pkt, int pkt_len)
{
if (slirpq != NULL) {
network_busy(1);
if (slirpq == NULL) return;
slirp_input((const uint8_t *)pkt, pkt_len);
network_busy(1);
network_busy(0);
}
slirp_input((const uint8_t *)pkt, pkt_len);
network_busy(0);
}
/* Needed by SLiRP library. */
void
slirp_output(const uint8_t *pkt, int pkt_len)
{
@@ -254,6 +216,7 @@ slirp_output(const uint8_t *pkt, int pkt_len)
}
/* Needed by SLiRP library. */
int
slirp_can_output(void)
{

View File

@@ -12,7 +12,7 @@
* it should be malloc'ed and then linked to the NETCARD def.
* Will be done later.
*
* Version: @(#)network.c 1.0.17 2017/10/19
* Version: @(#)network.c 1.0.18 2017/10/28
*
* Author: Fred N. van Kempen, <decwiz@yahoo.com>
*
@@ -57,78 +57,53 @@ char network_pcap[512];
#ifdef ENABLE_NIC_LOG
int nic_do_log = ENABLE_NIC_LOG;
#endif
static volatile mutex_t *netMutex;
static mutex_t *network_mutex;
static struct
{
volatile int
busy,
queue_in_use;
static struct {
volatile int busy,
queue_in_use;
volatile event_t
*wake_poll_thread,
*poll_complete,
*queue_not_in_use;
event_t *wake_poll_thread,
*poll_complete,
*queue_not_in_use;
} poll_data;
void
network_mutex_wait(uint8_t wait)
network_wait(uint8_t wait)
{
if (wait)
thread_wait_mutex((mutex_t *) netMutex);
else
thread_release_mutex((mutex_t *) netMutex);
thread_wait_mutex(network_mutex);
else
thread_release_mutex(network_mutex);
}
void
network_wait_for_poll()
network_poll(void)
{
while (poll_data.busy)
thread_wait_event((event_t *) poll_data.wake_poll_thread, -1);
thread_reset_event((event_t *) poll_data.wake_poll_thread);
thread_wait_event(poll_data.wake_poll_thread, -1);
thread_reset_event(poll_data.wake_poll_thread);
}
void
network_wait_for_end(void *handle)
{
thread_wait((event_t *) handle, -1);
if (poll_data.wake_poll_thread) {
thread_destroy_event((event_t *) poll_data.wake_poll_thread);
poll_data.wake_poll_thread = NULL;
}
if (poll_data.poll_complete) {
thread_destroy_event((event_t *) poll_data.poll_complete);
poll_data.poll_complete = NULL;
}
}
void
network_thread_init(void)
{
poll_data.wake_poll_thread = thread_create_event();
poll_data.poll_complete = thread_create_event();
}
void
network_busy(uint8_t set)
{
poll_data.busy = !!set;
if (!set)
thread_set_event((event_t *) poll_data.wake_poll_thread);
if (! set)
thread_set_event(poll_data.wake_poll_thread);
}
void
network_end(void)
{
thread_set_event((event_t *) poll_data.poll_complete);
thread_set_event(poll_data.poll_complete);
}
@@ -154,12 +129,9 @@ network_init(void)
network_ndev = 1;
/* Initialize the Pcap system module, if present. */
i = network_pcap_init(&network_devs[network_ndev]);
i = net_pcap_prepare(&network_devs[network_ndev]);
if (i > 0)
network_ndev += i;
if (network_type != NET_TYPE_PCAP)
network_pcap_close();
}
@@ -170,35 +142,30 @@ network_init(void)
* finished initializing itself, to link itself to the platform support
* modules.
*/
int
void
network_attach(void *dev, uint8_t *mac, NETRXCB rx)
{
int ret = -1;
if (network_card == 0) return;
if (network_card == 0) return(ret);
/* Save the card's callback info. */
/* Save the card's info. */
net_cards[network_card].priv = dev;
net_cards[network_card].rx = rx;
net_cards[network_card].mac = mac;
netMutex = thread_create_mutex(L"86Box.NetMutex");
/* Create the network events. */
poll_data.wake_poll_thread = thread_create_event();
poll_data.poll_complete = thread_create_event();
/* Start the platform module. */
/* Activate the platform module. */
switch(network_type) {
case NET_TYPE_PCAP:
ret = network_pcap_setup(mac, rx, dev);
if (ret < 0) {
ui_msgbox(MBX_ERROR, (wchar_t *)IDS_2139);
network_type = NET_TYPE_NONE;
}
(void)net_pcap_reset(&net_cards[network_card]);
break;
case NET_TYPE_SLIRP:
ret = network_slirp_setup(mac, rx, dev);
(void)net_slirp_reset(&net_cards[network_card]);
break;
}
return(ret);
}
@@ -206,35 +173,30 @@ network_attach(void *dev, uint8_t *mac, NETRXCB rx)
void
network_close(void)
{
thread_close_mutex((mutex_t *) netMutex);
/* If already closed, do nothing. */
if (network_mutex == NULL) return;
switch(network_type) {
case NET_TYPE_PCAP:
network_pcap_close();
break;
/* Force-close the PCAP module. */
net_pcap_close();
case NET_TYPE_SLIRP:
network_slirp_close();
break;
/* Force-close the SLIRP module. */
net_slirp_close();
/* Close the network events. */
if (poll_data.wake_poll_thread != NULL) {
thread_destroy_event(poll_data.wake_poll_thread);
poll_data.wake_poll_thread = NULL;
}
}
/* Test the network. */
int
network_test(void)
{
switch(network_type) {
case NET_TYPE_PCAP:
return network_pcap_test();
break;
case NET_TYPE_SLIRP:
return network_slirp_test();
break;
if (poll_data.poll_complete != NULL) {
thread_destroy_event(poll_data.poll_complete);
poll_data.poll_complete = NULL;
}
return 0;
/* Close the network thread mutex. */
thread_close_mutex(network_mutex);
network_mutex = NULL;
pclog("NETWORK: closed.\n");
}
@@ -249,7 +211,10 @@ network_test(void)
void
network_reset(void)
{
int i = -1;
pclog("NETWORK: reset (type=%d, card=%d)\n", network_type, network_card);
ui_sb_update_icon(SB_NETWORK, 0);
/* Just in case.. */
network_close();
@@ -257,11 +222,36 @@ network_reset(void)
/* If no active card, we're done. */
if ((network_type==NET_TYPE_NONE) || (network_card==0)) return;
if (network_type==NET_TYPE_PCAP) network_pcap_reset();
network_mutex = thread_create_mutex(L"86Box.NetMutex");
/* Initialize the platform module. */
switch(network_type) {
case NET_TYPE_PCAP:
i = net_pcap_init();
break;
case NET_TYPE_SLIRP:
i = net_slirp_init();
break;
}
if (i < 0) {
/* Tell user we can't do this (at the moment.) */
ui_msgbox(MBX_ERROR, (wchar_t *)IDS_2139);
// FIXME: we should ask in the dialog if they want to
// reconfigure or quit, and throw them into the
// Settings dialog if yes.
/* Disable network. */
network_type = NET_TYPE_NONE;
return;
}
pclog("NETWORK: set up for %s, card='%s'\n",
(network_type==NET_TYPE_SLIRP)?"SLiRP":"WinPcap",
net_cards[network_card].name);
(network_type==NET_TYPE_SLIRP)?"SLiRP":"Pcap",
net_cards[network_card].name);
/* Add the (new?) card to the I/O system. */
if (net_cards[network_card].device) {
@@ -276,15 +266,45 @@ network_reset(void)
void
network_tx(uint8_t *bufp, int len)
{
ui_sb_update_icon(SB_NETWORK, 1);
switch(network_type) {
case NET_TYPE_PCAP:
network_pcap_in(bufp, len);
net_pcap_in(bufp, len);
break;
case NET_TYPE_SLIRP:
network_slirp_in(bufp, len);
net_slirp_in(bufp, len);
break;
}
ui_sb_update_icon(SB_NETWORK, 0);
}
int
network_dev_to_id(char *devname)
{
int i = 0;
for (i=0; i<network_ndev; i++) {
if (! strcmp(network_devs[i].device, devname)) {
return(i);
}
}
/* If no match found, assume "none". */
return(0);
}
/* UI */
int
network_available(void)
{
if ((network_type == NET_TYPE_NONE) || (network_card == 0)) return(0);
return(1);
}
@@ -347,19 +367,3 @@ network_card_get_from_internal_name(char *s)
return(-1);
}
int
network_dev_to_id(char *dev)
{
int i = 0;
for (i=0; i<network_ndev; i++) {
if (! strcmp(network_devs[i].device, dev)) {
return(i);
}
}
/* If no match found, assume "none". */
return(0);
}

View File

@@ -8,7 +8,7 @@
*
* Definitions for the network module.
*
* Version: @(#)network.h 1.0.8 2017/10/15
* Version: @(#)network.h 1.0.9 2017/10/28
*
* Author: Fred N. van Kempen, <decwiz@yahoo.com>
*/
@@ -38,6 +38,7 @@ typedef struct {
void *priv;
int (*poll)(void *);
NETRXCB rx;
uint8_t *mac;
} netcard_t;
typedef struct {
@@ -47,42 +48,37 @@ typedef struct {
/* Global variables. */
extern int nic_do_log;
extern int network_card;
extern int network_type;
extern int nic_do_log; /* config */
extern int network_card; /* config */
extern int network_type; /* config */
extern char network_pcap[512]; /* config */
extern int network_ndev;
extern netdev_t network_devs[32];
extern char network_pcap[512];
/* Function prototypes. */
extern void network_mutex_wait(uint8_t wait);
extern void network_wait_for_poll(void);
extern void network_wait_for_end(void *handle);
extern void network_mutex_init(void);
extern void network_thread_init(void);
extern void network_wait(uint8_t wait);
extern void network_poll(void);
extern void network_busy(uint8_t set);
extern void network_end(void);
extern void network_init(void);
extern int network_attach(void *, uint8_t *, NETRXCB);
extern void network_attach(void *, uint8_t *, NETRXCB);
extern void network_close(void);
extern int network_test(void);
extern void network_reset(void);
extern int network_available(void);
extern void network_tx(uint8_t *, int);
extern int network_pcap_init(netdev_t *);
extern void network_pcap_reset(void);
extern int network_pcap_setup(uint8_t *, NETRXCB, void *);
extern void network_pcap_close(void);
extern int network_pcap_test(void);
extern void network_pcap_in(uint8_t *, int);
extern int net_pcap_prepare(netdev_t *);
extern int net_pcap_init(void);
extern int net_pcap_reset(netcard_t *);
extern void net_pcap_close(void);
extern void net_pcap_in(uint8_t *, int);
extern void network_slirp_mutex_init(void);
extern int network_slirp_setup(uint8_t *, NETRXCB, void *);
extern void network_slirp_close(void);
extern int network_slirp_test(void);
extern void network_slirp_in(uint8_t *, int);
extern int net_slirp_init(void);
extern int net_slirp_reset(netcard_t *);
extern void net_slirp_close(void);
extern void net_slirp_in(uint8_t *, int);
extern int network_dev_to_id(char *);
extern int network_card_available(int);

View File

@@ -10,7 +10,7 @@
*
* Based on the "libpcap" examples.
*
* Version: @(#)pcap_if.c 1.0.6 2017/10/27
* Version: @(#)pcap_if.c 1.0.7 2017/10/28
*
* Author: Fred N. van Kempen, <decwiz@yahoo.com>
*
@@ -237,13 +237,13 @@ main(int argc, char **argv)
int numdev, i;
/* Try loading the DLL. */
#ifdef WIN32
#ifdef _WIN32
pcap_handle = dynld_module("wpcap.dll", pcap_imports);
#else
pcap_handle = dynld_module("libpcap.so", pcap_imports);
#endif
if (pcap_handle == NULL) {
#ifdef WIN32
#ifdef _WIN32
fprintf(stderr, "Unable to load WinPcap DLL !\n");
#else
fprintf(stderr, "Unable to load libpcap.so !\n");

View File

@@ -6,7 +6,7 @@
* terms and conditions of the copyright.
*/
#ifndef WIN32
#ifndef _WIN32
# include <unistd.h>
#endif
#include "slirp.h"

View File

@@ -7,7 +7,7 @@
#define WANT_SYS_IOCTL_H
#include <stdlib.h>
#ifndef WIN32
#ifndef _WIN32
# include <unistd.h>
#endif
#include "slirp.h"
@@ -223,12 +223,6 @@ add_exec(ex_ptr, do_pty, exec, addr, port)
/*
* For systems with no strerror
*/
#ifdef WIN32
//extern int sys_nerr;
//extern char *sys_errlist[];
#endif
char *
SLIRPstrerror(error)
int error;

View File

@@ -7,7 +7,7 @@
#define WANT_SYS_IOCTL_H
#include <stdlib.h>
#ifndef WIN32
#ifndef _WIN32
# include <unistd.h>
#endif
#include "slirp.h"

View File

@@ -40,7 +40,7 @@
#define WANT_SYS_IOCTL_H
#include <stdlib.h>
#ifndef WIN32
#ifndef _WIN32
# include <unistd.h>
#endif
#include "slirp.h"

View File

@@ -39,7 +39,7 @@
*/
#include <stdlib.h>
#ifndef WIN32
#ifndef _WIN32
# include <unistd.h>
#endif
#include "slirp.h"