2017-05-12 05:05:20 -04:00
|
|
|
/*
|
2018-03-19 01:02:04 +01:00
|
|
|
* VARCem Virtual ARchaeological Computer EMulator.
|
|
|
|
|
* An emulator of (mostly) x86-based PC systems and devices,
|
|
|
|
|
* using the ISA,EISA,VLB,MCA and PCI system buses, roughly
|
|
|
|
|
* spanning the era between 1981 and 1995.
|
2017-05-12 05:05:20 -04:00
|
|
|
*
|
2018-03-19 01:02:04 +01:00
|
|
|
* This file is part of the VARCem Project.
|
2017-05-12 05:05:20 -04:00
|
|
|
*
|
|
|
|
|
* Handle SLiRP library processing.
|
|
|
|
|
*
|
2019-11-14 20:59:51 +01:00
|
|
|
* Version: @(#)net_slirp.c 1.0.9 2019/11/14
|
2017-05-12 05:05:20 -04:00
|
|
|
*
|
|
|
|
|
* Author: Fred N. van Kempen, <decwiz@yahoo.com>
|
2017-10-11 05:40:44 -04:00
|
|
|
*
|
2019-11-14 20:59:51 +01:00
|
|
|
* Copyright 2017-2019 Fred N. van Kempen.
|
2018-03-19 01:02:04 +01:00
|
|
|
*
|
|
|
|
|
* 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 entire
|
|
|
|
|
* above 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. Neither the name of the copyright holder nor the names
|
|
|
|
|
* of its contributors may be used to endorse or promote
|
|
|
|
|
* products derived from this software without specific
|
|
|
|
|
* prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
* "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 COPYRIGHT
|
|
|
|
|
* HOLDER OR CONTRIBUTORS 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.
|
2017-05-12 05:05:20 -04:00
|
|
|
*/
|
2018-05-21 19:04:05 +02:00
|
|
|
#include <stdarg.h>
|
2017-09-25 04:31:20 -04:00
|
|
|
#include <stdint.h>
|
2018-05-21 19:04:05 +02:00
|
|
|
#include <stdio.h>
|
2017-05-12 05:05:20 -04:00
|
|
|
#include <string.h>
|
2017-09-25 04:31:20 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <wchar.h>
|
2018-05-21 19:04:05 +02:00
|
|
|
#define HAVE_STDARG_H
|
2017-08-27 04:33:47 +01:00
|
|
|
#include "slirp/slirp.h"
|
|
|
|
|
#include "slirp/queue.h"
|
2020-02-29 19:12:23 +01:00
|
|
|
#include "86box.h"
|
|
|
|
|
#include "device.h"
|
|
|
|
|
#include "plat.h"
|
2017-10-10 03:07:29 -04:00
|
|
|
#include "network.h"
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
static volatile queueADT slirpq; /* SLiRP library handle */
|
|
|
|
|
static volatile thread_t *poll_tid;
|
2018-03-19 01:02:04 +01:00
|
|
|
static const netcard_t *poll_card; /* netcard attached to us */
|
2017-10-29 04:20:20 -05:00
|
|
|
static event_t *poll_state;
|
2017-10-14 07:03:19 +02:00
|
|
|
|
|
|
|
|
|
2018-05-21 19:04:05 +02:00
|
|
|
#ifdef ENABLE_SLIRP_LOG
|
|
|
|
|
int slirp_do_log = ENABLE_SLIRP_LOG;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2018-10-19 00:37:25 +02:00
|
|
|
slirp_log(const char *fmt, ...)
|
2018-05-21 19:04:05 +02:00
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
if (slirp_do_log) {
|
2018-10-19 00:37:25 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
|
pclog_ex(fmt, ap);
|
2018-05-21 19:04:05 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-19 00:37:25 +02:00
|
|
|
#else
|
|
|
|
|
#define slirp_log(fmt, ...)
|
|
|
|
|
#endif
|
2018-05-21 19:04:05 +02:00
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
static void
|
|
|
|
|
slirp_tic(void)
|
|
|
|
|
{
|
2017-05-18 01:57:16 -04:00
|
|
|
int ret2, nfds;
|
2017-05-12 05:05:20 -04:00
|
|
|
struct timeval tv;
|
|
|
|
|
fd_set rfds, wfds, xfds;
|
|
|
|
|
int tmo;
|
|
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
/* Let SLiRP create a list of all open sockets. */
|
|
|
|
|
nfds = -1;
|
2017-05-12 05:05:20 -04:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
|
FD_ZERO(&xfds);
|
|
|
|
|
tmo = slirp_select_fill(&nfds, &rfds, &wfds, &xfds); /* this can crash */
|
2017-05-18 01:57:16 -04:00
|
|
|
if (tmo < 0)
|
2017-05-12 05:05:20 -04:00
|
|
|
tmo = 500;
|
|
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
2017-05-18 01:57:16 -04:00
|
|
|
tv.tv_usec = tmo;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
/* Now wait for something to happen, or at most 'tmo' usec. */
|
2017-05-12 05:05:20 -04:00
|
|
|
ret2 = select(nfds+1, &rfds, &wfds, &xfds, &tv);
|
2017-05-18 01:57:16 -04:00
|
|
|
|
|
|
|
|
/* If something happened, let SLiRP handle it. */
|
|
|
|
|
if (ret2 >= 0)
|
2017-05-12 05:05:20 -04:00
|
|
|
slirp_select_poll(&rfds, &wfds, &xfds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-22 01:08:10 -04:00
|
|
|
/* Handle the receiving of frames. */
|
2017-05-12 05:05:20 -04:00
|
|
|
static void
|
2018-10-19 00:37:25 +02:00
|
|
|
poll_thread(void *arg)
|
2017-05-12 05:05:20 -04:00
|
|
|
{
|
2018-10-19 00:37:25 +02:00
|
|
|
uint8_t *mac = (uint8_t *)arg;
|
2017-05-12 05:05:20 -04:00
|
|
|
struct queuepacket *qp;
|
2018-10-19 00:37:25 +02:00
|
|
|
uint32_t mac_cmp32[2];
|
|
|
|
|
uint16_t mac_cmp16[2];
|
2017-05-12 05:05:20 -04:00
|
|
|
event_t *evt;
|
2018-10-19 00:37:25 +02:00
|
|
|
int data_valid = 0;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2018-05-21 19:04:05 +02:00
|
|
|
slirp_log("SLiRP: polling started.\n");
|
2017-10-29 04:20:20 -05:00
|
|
|
thread_set_event(poll_state);
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
/* Create a waitable event. */
|
|
|
|
|
evt = thread_create_event();
|
|
|
|
|
|
|
|
|
|
while (slirpq != NULL) {
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Request ownership of the queue. */
|
|
|
|
|
network_wait(1);
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Wait for a poll request. */
|
|
|
|
|
network_poll();
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
/* See if there is any work. */
|
|
|
|
|
slirp_tic();
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-11-01 01:51:19 -05:00
|
|
|
/* Our queue may have been nuked.. */
|
|
|
|
|
if (slirpq == NULL) break;
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* Wait for the next packet to arrive. */
|
2018-10-19 00:37:25 +02:00
|
|
|
data_valid = 0;
|
|
|
|
|
|
2020-02-29 19:12:23 +01:00
|
|
|
if ((!network_get_wait() && !(poll_card->wait && poll_card->wait(poll_card->priv))) && (QueuePeek(slirpq) != 0)) {
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Grab a packet from the queue. */
|
2018-10-19 19:35:39 +02:00
|
|
|
// ui_sb_update_icon(SB_NETWORK, 1);
|
2018-10-19 00:37:25 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
qp = QueueDelete(slirpq);
|
2018-05-21 19:04:05 +02:00
|
|
|
slirp_log("SLiRP: inQ:%d got a %dbyte packet @%08lx\n",
|
2017-05-12 05:05:20 -04:00
|
|
|
QueuePeek(slirpq), qp->len, qp);
|
|
|
|
|
|
2018-10-19 00:37:25 +02:00
|
|
|
/* Received MAC. */
|
|
|
|
|
mac_cmp32[0] = *(uint32_t *)(((uint8_t *)qp->data)+6);
|
|
|
|
|
mac_cmp16[0] = *(uint16_t *)(((uint8_t *)qp->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])) {
|
|
|
|
|
|
|
|
|
|
poll_card->rx(poll_card->priv, (uint8_t *)qp->data, qp->len);
|
|
|
|
|
data_valid = 1;
|
|
|
|
|
}
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Done with this one. */
|
|
|
|
|
free(qp);
|
2018-10-19 00:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If we did not get anything, wait a while. */
|
|
|
|
|
if (!data_valid) {
|
2018-10-19 19:35:39 +02:00
|
|
|
// ui_sb_update_icon(SB_NETWORK, 0);
|
2017-10-29 04:20:20 -05:00
|
|
|
thread_wait_event(evt, 10);
|
|
|
|
|
}
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Release ownership of the queue. */
|
|
|
|
|
network_wait(0);
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* No longer needed. */
|
2017-11-01 01:51:19 -05:00
|
|
|
if (evt != NULL)
|
|
|
|
|
thread_destroy_event(evt);
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2018-05-21 19:04:05 +02:00
|
|
|
slirp_log("SLiRP: polling stopped.\n");
|
2017-10-29 04:20:20 -05:00
|
|
|
thread_set_event(poll_state);
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Initialize SLiRP for use. */
|
2017-05-12 05:05:20 -04:00
|
|
|
int
|
2017-10-29 04:20:20 -05:00
|
|
|
net_slirp_init(void)
|
2017-05-12 05:05:20 -04:00
|
|
|
{
|
2018-05-21 19:04:05 +02:00
|
|
|
slirp_log("SLiRP: initializing..\n");
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
if (slirp_init() != 0) {
|
2018-05-21 19:04:05 +02:00
|
|
|
slirp_log("SLiRP could not be initialized!\n");
|
2017-05-12 05:05:20 -04:00
|
|
|
return(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slirpq = QueueCreate();
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
poll_tid = NULL;
|
|
|
|
|
poll_state = NULL;
|
|
|
|
|
poll_card = NULL;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
return(0);
|
|
|
|
|
}
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-10-28 07:33:08 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Initialize SLiRP for use. */
|
|
|
|
|
int
|
2018-03-19 01:02:04 +01:00
|
|
|
net_slirp_reset(const netcard_t *card, uint8_t *mac)
|
2017-10-29 04:20:20 -05:00
|
|
|
{
|
2018-10-19 19:35:39 +02:00
|
|
|
// ui_sb_update_icon(SB_NETWORK, 0);
|
2018-10-19 00:37:25 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Save the callback info. */
|
|
|
|
|
poll_card = card;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2018-05-21 19:04:05 +02:00
|
|
|
slirp_log("SLiRP: creating thread..\n");
|
2017-10-29 04:20:20 -05:00
|
|
|
poll_state = thread_create_event();
|
2018-03-18 20:48:10 +01:00
|
|
|
poll_tid = thread_create(poll_thread, mac);
|
2017-10-29 04:20:20 -05:00
|
|
|
thread_wait_event(poll_state, -1);
|
2017-10-19 23:55:51 +02:00
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
return(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-10-29 04:20:20 -05:00
|
|
|
net_slirp_close(void)
|
2017-05-12 05:05:20 -04:00
|
|
|
{
|
|
|
|
|
queueADT sl;
|
|
|
|
|
|
2018-10-19 19:35:39 +02:00
|
|
|
// ui_sb_update_icon(SB_NETWORK, 0);
|
2018-10-19 00:37:25 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
if (slirpq == NULL) return;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2018-05-21 19:04:05 +02:00
|
|
|
slirp_log("SLiRP: closing.\n");
|
2017-10-19 23:55:51 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Tell the polling thread to shut down. */
|
|
|
|
|
sl = slirpq; slirpq = NULL;
|
2017-10-19 23:55:51 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Tell the thread to terminate. */
|
|
|
|
|
if (poll_tid != NULL) {
|
|
|
|
|
network_busy(0);
|
2017-10-19 23:55:51 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Wait for the thread to finish. */
|
2018-05-21 19:04:05 +02:00
|
|
|
slirp_log("SLiRP: waiting for thread to end...\n");
|
2017-10-29 04:20:20 -05:00
|
|
|
thread_wait_event(poll_state, -1);
|
2018-05-21 19:04:05 +02:00
|
|
|
slirp_log("SLiRP: thread ended\n");
|
2017-10-29 04:20:20 -05:00
|
|
|
thread_destroy_event(poll_state);
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
poll_tid = NULL;
|
|
|
|
|
poll_state = NULL;
|
|
|
|
|
poll_card = NULL;
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* OK, now shut down SLiRP itself. */
|
|
|
|
|
QueueDestroy(sl);
|
|
|
|
|
slirp_exit(0);
|
2017-08-22 02:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* Send a packet to the SLiRP interface. */
|
|
|
|
|
void
|
2017-10-29 04:20:20 -05:00
|
|
|
net_slirp_in(uint8_t *pkt, int pkt_len)
|
2017-05-12 05:05:20 -04:00
|
|
|
{
|
2017-10-29 04:20:20 -05:00
|
|
|
if (slirpq == NULL) return;
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2018-10-19 19:35:39 +02:00
|
|
|
// ui_sb_update_icon(SB_NETWORK, 1);
|
2018-10-19 00:37:25 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
network_busy(1);
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
slirp_input((const uint8_t *)pkt, pkt_len);
|
|
|
|
|
|
|
|
|
|
network_busy(0);
|
2018-10-19 00:37:25 +02:00
|
|
|
|
2018-10-19 19:35:39 +02:00
|
|
|
// ui_sb_update_icon(SB_NETWORK, 0);
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Needed by SLiRP library. */
|
2017-05-12 05:05:20 -04:00
|
|
|
void
|
|
|
|
|
slirp_output(const uint8_t *pkt, int pkt_len)
|
|
|
|
|
{
|
|
|
|
|
struct queuepacket *qp;
|
|
|
|
|
|
|
|
|
|
if (slirpq != NULL) {
|
|
|
|
|
qp = (struct queuepacket *)malloc(sizeof(struct queuepacket));
|
|
|
|
|
qp->len = pkt_len;
|
|
|
|
|
memcpy(qp->data, pkt, pkt_len);
|
|
|
|
|
QueueEnter(slirpq, qp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Needed by SLiRP library. */
|
2017-05-12 05:05:20 -04:00
|
|
|
int
|
|
|
|
|
slirp_can_output(void)
|
|
|
|
|
{
|
|
|
|
|
return((slirpq != NULL)?1:0);
|
|
|
|
|
}
|