network: Added new ability for drivers to provide error information upon failed init. Updated translation strings. Fixed an issue with VDE configuration settings.

This commit is contained in:
cold-brewed
2023-05-23 11:53:59 -04:00
parent 5c26d3d4b3
commit ef34e81cd1
51 changed files with 230 additions and 34 deletions

View File

@@ -158,7 +158,7 @@ net_null_thread(void *priv)
#endif
void *
net_null_init(const netcard_t *card, const uint8_t *mac_addr, void *priv)
net_null_init(const netcard_t *card, const uint8_t *mac_addr, void *priv, char *netdrv_errbuf)
{
net_null_log("Null Network: Init\n");
@@ -221,4 +221,4 @@ const netdrv_t net_null_drv = {
&net_null_init,
&net_null_close,
NULL
};
};

View File

@@ -408,6 +408,15 @@ net_pcap_prepare(netdev_t *list)
return (i);
}
/*
* Copy error message to the error buffer
* and log if enabled.
*/
void net_pcap_error(char *errbuf, const char *message) {
strncpy(errbuf, message, NET_DRV_ERRBUF_SIZE);
pcap_log("PCAP: %s\n", message);
}
/*
* Initialize (Win)Pcap for use.
*
@@ -416,18 +425,19 @@ net_pcap_prepare(netdev_t *list)
* tries to attach to the network module.
*/
void *
net_pcap_init(const netcard_t *card, const uint8_t *mac_addr, void *priv)
net_pcap_init(const netcard_t *card, const uint8_t *mac_addr, void *priv, char *netdrv_errbuf)
{
char errbuf[PCAP_ERRBUF_SIZE];
char *str;
char filter_exp[255];
struct bpf_program fp;
char errbuf_prep[NET_DRV_ERRBUF_SIZE];
char *intf_name = (char *) priv;
/* Did we already load the library? */
if (libpcap_handle == NULL) {
pcap_log("PCAP: net_pcap_init without handle.\n");
net_pcap_error(netdrv_errbuf, "net_pcap_init without handle");
return NULL;
}
@@ -440,7 +450,7 @@ net_pcap_init(const netcard_t *card, const uint8_t *mac_addr, void *priv)
/* Get the value of our capture interface. */
if ((intf_name[0] == '\0') || !strcmp(intf_name, "none")) {
pcap_log("PCAP: no interface configured!\n");
net_pcap_error(netdrv_errbuf, "No interface configured");
return NULL;
}
@@ -451,7 +461,8 @@ net_pcap_init(const netcard_t *card, const uint8_t *mac_addr, void *priv)
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);
snprintf(errbuf_prep, NET_DRV_ERRBUF_SIZE, " Unable to open device: %s!\n", intf_name);
net_pcap_error(netdrv_errbuf, errbuf_prep);
free(pcap);
return NULL;
}
@@ -469,7 +480,8 @@ net_pcap_init(const netcard_t *card, const uint8_t *mac_addr, void *priv)
pcap_log("PCAP: error setting snaplen\n");
if (f_pcap_activate((void *) pcap->pcap) != 0) {
pcap_log("PCAP: failed pcap_activate");
snprintf(errbuf_prep, NET_DRV_ERRBUF_SIZE, "%s", (char *)f_pcap_geterr(pcap->pcap));
net_pcap_error(netdrv_errbuf, errbuf_prep);
f_pcap_close((void *) pcap->pcap);
free(pcap);
return NULL;
@@ -484,13 +496,15 @@ net_pcap_init(const netcard_t *card, const uint8_t *mac_addr, void *priv)
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);
snprintf(errbuf_prep, NET_DRV_ERRBUF_SIZE, "Error installing filter (%s)\n", filter_exp);
net_pcap_error(netdrv_errbuf, errbuf_prep);
f_pcap_close((void *) pcap->pcap);
free(pcap);
return NULL;
}
} else {
pcap_log("PCAP: could not compile filter (%s) : %s!\n", filter_exp, f_pcap_geterr((void *) pcap->pcap));
snprintf(errbuf_prep, NET_DRV_ERRBUF_SIZE, "Could not compile filter (%s) : %s!\n", filter_exp, (char *)f_pcap_geterr((void *) pcap->pcap));
net_pcap_error(netdrv_errbuf, errbuf_prep);
f_pcap_close((void *) pcap->pcap);
free(pcap);
return NULL;

View File

@@ -384,7 +384,7 @@ static int slirp_card_num = 2;
/* Initialize SLiRP for use. */
void *
net_slirp_init(const netcard_t *card, const uint8_t *mac_addr, void *priv)
net_slirp_init(const netcard_t *card, const uint8_t *mac_addr, void *priv, char *netdrv_errbuf)
{
slirp_log("SLiRP: initializing...\n");
net_slirp_t *slirp = calloc(1, sizeof(net_slirp_t));
@@ -410,6 +410,7 @@ net_slirp_init(const netcard_t *card, const uint8_t *mac_addr, void *priv)
slirp->slirp = slirp_init(0, 1, net, mask, host, 0, ipv6_dummy, 0, ipv6_dummy, NULL, NULL, NULL, NULL, dhcp, dns, ipv6_dummy, NULL, NULL, &slirp_cb, slirp);
if (!slirp->slirp) {
slirp_log("SLiRP: initialization failed\n");
snprintf(netdrv_errbuf, NET_DRV_ERRBUF_SIZE, "SLiRP initialization failed");
free(slirp);
return NULL;
}

View File

@@ -235,6 +235,15 @@ void net_vde_in_available(void *priv) {
net_event_set(&vde->tx_event);
}
//+
// Copy error message to the error buffer
// and log if enabled.
//-
void net_vde_error(char *errbuf, const char *message) {
strncpy(errbuf, message, NET_DRV_ERRBUF_SIZE);
vde_log("VDE: %s\n", message);
}
//+
// Initialize VDE for use
// At this point the vdeplug library is already loaded
@@ -242,19 +251,19 @@ void net_vde_in_available(void *priv) {
// mac_addr: MAC address we are using
// priv: Name of the VDE contol socket directory
//-
void *net_vde_init(const netcard_t *card, const uint8_t *mac_addr, void *priv) {
void *net_vde_init(const netcard_t *card, const uint8_t *mac_addr, void *priv, char *netdrv_errbuf) {
struct vde_open_args vde_args;
int i;
char *socket_name = (char *) priv;
if (libvde_handle == NULL) {
vde_log("VDE: net_vde_init without library handle!\n");
net_vde_error(netdrv_errbuf, "net_vde_init without library handle");
return NULL;
}
if ((socket_name[0] == '\0') || !strcmp(socket_name, "none")) {
vde_log("VDE: No socket name configured!\n");
net_vde_error(netdrv_errbuf, "No socket name configured");
return NULL;
}
@@ -271,18 +280,10 @@ void *net_vde_init(const netcard_t *card, const uint8_t *mac_addr, void *priv) {
// We are calling vde_open_real(), not the vde_open() macro...
if ((vde->vdeconn = f_vde_open(socket_name, VDE_DESCRIPTION,
LIBVDEPLUG_INTERFACE_VERSION, &vde_args)) == NULL) {
vde_log("VDE: Unable to open socket %s (%s)!\n", socket_name, strerror(errno));
char buf[NET_DRV_ERRBUF_SIZE];
snprintf(buf, NET_DRV_ERRBUF_SIZE, "Unable to open socket %s (%s)", socket_name, strerror(errno));
net_vde_error(netdrv_errbuf, buf);
free(vde);
//+
// There is a bug upstream that causes an uncontrolled crash if the network is not
// properly initialized.
// To avoid that crash, we tell the user we cannot continue and exit the program.
// TODO: Once there is a solution for the mentioned crash, this should be removed
// and/or replaced by proper error handling code.
//-
// fatal("Could not open the specified VDE socket (%s). Please fix your networking configuration.", socket_name);
// It makes no sense to issue this warning since the program will crash anyway...
// ui_msgbox_header(MBX_WARNING, (wchar_t *) IDS_2167, (wchar_t *) IDS_2168);
return NULL;
}
vde_log("VDE: Socket opened (%s).\n", socket_name);

View File

@@ -442,6 +442,9 @@ network_attach(void *card_drv, uint8_t *mac, NETRXCB rx, NETSETLINKSTATE set_lin
card->card_num = net_card_current;
card->byte_period = NET_PERIOD_10M;
char net_drv_error[NET_DRV_ERRBUF_SIZE];
wchar_t tempmsg[NET_DRV_ERRBUF_SIZE * 2];
for (int i = 0; i < NET_QUEUE_COUNT; i++) {
network_queue_init(&card->queues[i]);
}
@@ -449,17 +452,17 @@ network_attach(void *card_drv, uint8_t *mac, NETRXCB rx, NETSETLINKSTATE set_lin
switch (net_cards_conf[net_card_current].net_type) {
case NET_TYPE_SLIRP:
card->host_drv = net_slirp_drv;
card->host_drv.priv = card->host_drv.init(card, mac, NULL);
card->host_drv.priv = card->host_drv.init(card, mac, NULL, net_drv_error);
break;
case NET_TYPE_PCAP:
card->host_drv = net_pcap_drv;
card->host_drv.priv = card->host_drv.init(card, mac, net_cards_conf[net_card_current].host_dev_name);
card->host_drv.priv = card->host_drv.init(card, mac, net_cards_conf[net_card_current].host_dev_name, net_drv_error);
break;
#ifdef HAS_VDE
case NET_TYPE_VDE:
card->host_drv = net_vde_drv;
card->host_drv.priv = card->host_drv.init(card, mac, net_cards_conf[net_card_current].host_dev_name);
card->host_drv.priv = card->host_drv.init(card, mac, net_cards_conf[net_card_current].host_dev_name, net_drv_error);
break;
#endif
default:
@@ -474,13 +477,14 @@ network_attach(void *card_drv, uint8_t *mac, NETRXCB rx, NETSETLINKSTATE set_lin
if(net_cards_conf[net_card_current].net_type != NET_TYPE_NONE) {
// We're here because of a failure
// Placeholder to display a msgbox about falling back to null
ui_msgbox(MBX_ERROR | MBX_ANSI, "Network driver initialization failed. Falling back to NULL driver.");
swprintf(tempmsg, sizeof_w(tempmsg), L"%ls:<br /><br />%s<br /><br />%ls", plat_get_string(IDS_2167), net_drv_error, plat_get_string(IDS_2168));
ui_msgbox(MBX_ERROR, tempmsg);
net_cards_conf[net_card_current].net_type = NET_TYPE_NONE;
}
// Init null driver
card->host_drv = net_null_drv;
card->host_drv.priv = card->host_drv.init(card, mac, NULL);
card->host_drv.priv = card->host_drv.init(card, mac, NULL, net_drv_error);
// Set link state to disconnected by default
network_connect(card->card_num, 0);
ui_sb_update_icon_state(SB_NETWORK | card->card_num, 1);