From 056ec4b115aca09a035a358bd965353096c4f04e Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Thu, 14 Sep 2023 00:30:03 +0100 Subject: [PATCH] Split interface.[ch] into two files to fix ipxconfig link error. --- Makefile | 4 +- src/interface.c | 164 +-------------------------------------- src/interface.h | 22 +----- src/interface2.c | 192 ++++++++++++++++++++++++++++++++++++++++++++++ src/interface2.h | 59 ++++++++++++++ src/ipxconfig.cpp | 4 +- 6 files changed, 257 insertions(+), 188 deletions(-) create mode 100644 src/interface2.c create mode 100644 src/interface2.h diff --git a/Makefile b/Makefile index 0a7b5b3..ac9a501 100644 --- a/Makefile +++ b/Makefile @@ -85,7 +85,7 @@ dist: all # IPXWRAPPER_OBJS := src/ipxwrapper.o src/winsock.o src/ipxwrapper_stubs.o src/log.o src/common.o \ - src/interface.o src/router.o src/ipxwrapper.def src/addrcache.o src/config.o src/addr.o \ + src/interface.o src/interface2.o src/router.o src/ipxwrapper.def src/addrcache.o src/config.o src/addr.o \ src/firewall.o src/wpcap_stubs.o src/ethernet.o ipxwrapper.dll: $(IPXWRAPPER_OBJS) @@ -129,7 +129,7 @@ src/dpwsockx_stubs.s: src/dpwsockx_stubs.txt # IPXCONFIG.EXE # -IPXCONFIG_OBJS := src/ipxconfig.o icons/ipxconfig.o src/addr.o src/interface.o src/common.o \ +IPXCONFIG_OBJS := src/ipxconfig.o icons/ipxconfig.o src/addr.o src/interface2.o src/common.o \ src/config.o src/wpcap_stubs.o ipxconfig.exe: $(IPXCONFIG_OBJS) diff --git a/src/interface.c b/src/interface.c index 484e6f5..1b474d4 100644 --- a/src/interface.c +++ b/src/interface.c @@ -24,6 +24,7 @@ #include #include "interface.h" +#include "interface2.h" #include "ipxwrapper.h" #include "common.h" #include "config.h" @@ -44,71 +45,6 @@ static time_t interface_cache_ctime = 0; static void renew_interface_cache(bool force); -/* Fetch a list of network interfaces available on the system. - * - * Returns a linked list of IP_ADAPTER_INFO structures, all allocated within a - * single memory block beginning at the first node. -*/ -IP_ADAPTER_INFO *load_sys_interfaces(void) -{ - IP_ADAPTER_INFO *ifroot = NULL, *ifptr; - ULONG bufsize = sizeof(IP_ADAPTER_INFO) * 8; - - int err = ERROR_BUFFER_OVERFLOW; - - while(err == ERROR_BUFFER_OVERFLOW) - { - if(!(ifptr = realloc(ifroot, bufsize))) - { - log_printf(LOG_ERROR, "Couldn't allocate IP_ADAPTER_INFO structures!"); - break; - } - - ifroot = ifptr; - - err = GetAdaptersInfo(ifroot, &bufsize); - - if(err == ERROR_NO_DATA) - { - log_printf(LOG_WARNING, "No network interfaces detected!"); - break; - } - else if(err != ERROR_SUCCESS && err != ERROR_BUFFER_OVERFLOW) - { - log_printf(LOG_ERROR, "Error fetching network interfaces: %s", w32_error(err)); - break; - } - } - - if(err != ERROR_SUCCESS) - { - free(ifroot); - return NULL; - } - - for(ifptr = ifroot; ifptr; ifptr = ifptr->Next) - { - /* Workaround for buggy versions of Hamachi that don't initialise - * the interface hardware address correctly. - */ - - unsigned char hamachi_bug[] = {0x7A, 0x79, 0x00, 0x00, 0x00, 0x00}; - - if(ifptr->AddressLength == 6 && memcmp(ifptr->Address, hamachi_bug, 6) == 0) - { - uint32_t ipaddr = inet_addr(ifptr->IpAddressList.IpAddress.String); - - if(ipaddr) - { - log_printf(LOG_WARNING, "Invalid Hamachi interface detected, correcting node number"); - memcpy(ifptr->Address + 2, &ipaddr, sizeof(ipaddr)); - } - } - } - - return ifroot; -} - /* Allocate and initialise a new ipx_interface structure. * Returns NULL on malloc failure. */ @@ -739,104 +675,6 @@ int ipx_interface_count(void) return count; } -#define PCAP_NAME_PREFIX_OLD "rpcap://\\Device\\NPF_" -#define PCAP_NAME_PREFIX_NEW "rpcap://" - -ipx_pcap_interface_t *ipx_get_pcap_interfaces(void) -{ - ipx_pcap_interface_t *ret_interfaces = NULL; - - IP_ADAPTER_INFO *ip_interfaces = load_sys_interfaces(); - - pcap_if_t *pcap_interfaces; - char errbuf[PCAP_ERRBUF_SIZE]; - if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &pcap_interfaces, errbuf) == -1) - { - log_printf(LOG_ERROR, "Could not obtain list of WinPcap interfaces: %s", errbuf); - - free(ip_interfaces); - return NULL; - } - - for(pcap_if_t *pcap_if = pcap_interfaces; pcap_if; pcap_if = pcap_if->next) - { - const char *ifname = NULL; - - if(strncmp(pcap_if->name, PCAP_NAME_PREFIX_OLD, strlen(PCAP_NAME_PREFIX_OLD)) == 0) - { - ifname = pcap_if->name + strlen(PCAP_NAME_PREFIX_OLD); - } - else if(strncmp(pcap_if->name, PCAP_NAME_PREFIX_NEW, strlen(PCAP_NAME_PREFIX_NEW)) == 0) - { - ifname = pcap_if->name + strlen(PCAP_NAME_PREFIX_NEW); - } - else{ - log_printf(LOG_WARNING, "WinPcap interface with unexpected name format: '%s'", pcap_if->name); - log_printf(LOG_WARNING, "This interface will not be available for IPX use"); - } - - if(ifname != NULL) - { - IP_ADAPTER_INFO *ip_if = ip_interfaces; - while(ip_if && strcmp(ip_if->AdapterName, ifname)) - { - ip_if = ip_if->Next; - } - - if(ip_if && ip_if->AddressLength == 6) - { - ipx_pcap_interface_t *new_if = malloc(sizeof(ipx_pcap_interface_t)); - if(!new_if) - { - log_printf(LOG_ERROR, "Could not allocate memory!"); - continue; - } - - new_if->name = strdup(pcap_if->name); - new_if->desc = strdup(pcap_if->description - ? pcap_if->description - : pcap_if->name); - - if(!new_if->name || !new_if->desc) - { - free(new_if->name); - free(new_if->desc); - free(new_if); - - log_printf(LOG_ERROR, "Could not allocate memory!"); - continue; - } - - new_if->mac_addr = addr48_in(ip_if->Address); - - DL_APPEND(ret_interfaces, new_if); - } - else{ - log_printf(LOG_WARNING, "Could not determine MAC address of WinPcap interface '%s'", pcap_if->name); - log_printf(LOG_WARNING, "This interface will not be available for IPX use"); - } - } - } - - pcap_freealldevs(pcap_interfaces); - free(ip_interfaces); - - return ret_interfaces; -} - -void ipx_free_pcap_interfaces(ipx_pcap_interface_t **interfaces) -{ - ipx_pcap_interface_t *p, *p_tmp; - DL_FOREACH_SAFE(*interfaces, p, p_tmp) - { - DL_DELETE(*interfaces, p); - - free(p->name); - free(p->desc); - free(p); - } -} - ipx_interface_t *load_dosbox_interfaces(void) { ipx_interface_t *nics = NULL; diff --git a/src/interface.h b/src/interface.h index 288c4c4..376bdcb 100644 --- a/src/interface.h +++ b/src/interface.h @@ -25,6 +25,7 @@ #include "config.h" #include "common.h" +#include "interface2.h" #ifdef __cplusplus extern "C" { @@ -33,11 +34,6 @@ extern "C" { /* TODO: Dynamic MTU, per interface. */ #define ETHERNET_MTU 1500 -#define WILDCARD_IFACE_HWADDR ({ \ - const unsigned char x[] = {0x00,0x00,0x00,0x00,0x00,0x00}; \ - addr48_in(x); \ -}) - typedef struct ipx_interface_ip ipx_interface_ip_t; struct ipx_interface_ip { @@ -64,18 +60,6 @@ struct ipx_interface { ipx_interface_t *next; }; -typedef struct ipx_pcap_interface ipx_pcap_interface_t; - -struct ipx_pcap_interface { - char *name; - char *desc; - - addr48_t mac_addr; - - ipx_pcap_interface_t *prev; - ipx_pcap_interface_t *next; -}; - extern enum main_config_encap_type ipx_encap_type; enum dosbox_state @@ -90,7 +74,6 @@ extern enum dosbox_state dosbox_state; extern addr32_t dosbox_local_netnum; extern addr48_t dosbox_local_nodenum; -IP_ADAPTER_INFO *load_sys_interfaces(void); ipx_interface_t *load_ipx_interfaces(void); ipx_interface_t *copy_ipx_interface(const ipx_interface_t *src); @@ -109,9 +92,6 @@ ipx_interface_t *ipx_interface_by_subnet(uint32_t ipaddr); ipx_interface_t *ipx_interface_by_index(int index); int ipx_interface_count(void); -ipx_pcap_interface_t *ipx_get_pcap_interfaces(void); -void ipx_free_pcap_interfaces(ipx_pcap_interface_t **interfaces); - ipx_interface_t *load_dosbox_interfaces(void); #ifdef __cplusplus diff --git a/src/interface2.c b/src/interface2.c new file mode 100644 index 0000000..819a6cf --- /dev/null +++ b/src/interface2.c @@ -0,0 +1,192 @@ +/* IPXWrapper - Interface functions + * Copyright (C) 2011-2023 Daniel Collins + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#define WINSOCK_API_LINKAGE + +#include +#include +#include +#include +#include + +#include "interface2.h" +#include "ipxwrapper.h" +#include "common.h" +#include "config.h" + +/* Fetch a list of network interfaces available on the system. + * + * Returns a linked list of IP_ADAPTER_INFO structures, all allocated within a + * single memory block beginning at the first node. +*/ +IP_ADAPTER_INFO *load_sys_interfaces(void) +{ + IP_ADAPTER_INFO *ifroot = NULL, *ifptr; + ULONG bufsize = sizeof(IP_ADAPTER_INFO) * 8; + + int err = ERROR_BUFFER_OVERFLOW; + + while(err == ERROR_BUFFER_OVERFLOW) + { + if(!(ifptr = realloc(ifroot, bufsize))) + { + log_printf(LOG_ERROR, "Couldn't allocate IP_ADAPTER_INFO structures!"); + break; + } + + ifroot = ifptr; + + err = GetAdaptersInfo(ifroot, &bufsize); + + if(err == ERROR_NO_DATA) + { + log_printf(LOG_WARNING, "No network interfaces detected!"); + break; + } + else if(err != ERROR_SUCCESS && err != ERROR_BUFFER_OVERFLOW) + { + log_printf(LOG_ERROR, "Error fetching network interfaces: %s", w32_error(err)); + break; + } + } + + if(err != ERROR_SUCCESS) + { + free(ifroot); + return NULL; + } + + for(ifptr = ifroot; ifptr; ifptr = ifptr->Next) + { + /* Workaround for buggy versions of Hamachi that don't initialise + * the interface hardware address correctly. + */ + + unsigned char hamachi_bug[] = {0x7A, 0x79, 0x00, 0x00, 0x00, 0x00}; + + if(ifptr->AddressLength == 6 && memcmp(ifptr->Address, hamachi_bug, 6) == 0) + { + uint32_t ipaddr = inet_addr(ifptr->IpAddressList.IpAddress.String); + + if(ipaddr) + { + log_printf(LOG_WARNING, "Invalid Hamachi interface detected, correcting node number"); + memcpy(ifptr->Address + 2, &ipaddr, sizeof(ipaddr)); + } + } + } + + return ifroot; +} + +#define PCAP_NAME_PREFIX_OLD "rpcap://\\Device\\NPF_" +#define PCAP_NAME_PREFIX_NEW "rpcap://" + +ipx_pcap_interface_t *ipx_get_pcap_interfaces(void) +{ + ipx_pcap_interface_t *ret_interfaces = NULL; + + IP_ADAPTER_INFO *ip_interfaces = load_sys_interfaces(); + + pcap_if_t *pcap_interfaces; + char errbuf[PCAP_ERRBUF_SIZE]; + if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &pcap_interfaces, errbuf) == -1) + { + log_printf(LOG_ERROR, "Could not obtain list of WinPcap interfaces: %s", errbuf); + + free(ip_interfaces); + return NULL; + } + + for(pcap_if_t *pcap_if = pcap_interfaces; pcap_if; pcap_if = pcap_if->next) + { + const char *ifname = NULL; + + if(strncmp(pcap_if->name, PCAP_NAME_PREFIX_OLD, strlen(PCAP_NAME_PREFIX_OLD)) == 0) + { + ifname = pcap_if->name + strlen(PCAP_NAME_PREFIX_OLD); + } + else if(strncmp(pcap_if->name, PCAP_NAME_PREFIX_NEW, strlen(PCAP_NAME_PREFIX_NEW)) == 0) + { + ifname = pcap_if->name + strlen(PCAP_NAME_PREFIX_NEW); + } + else{ + log_printf(LOG_WARNING, "WinPcap interface with unexpected name format: '%s'", pcap_if->name); + log_printf(LOG_WARNING, "This interface will not be available for IPX use"); + } + + if(ifname != NULL) + { + IP_ADAPTER_INFO *ip_if = ip_interfaces; + while(ip_if && strcmp(ip_if->AdapterName, ifname)) + { + ip_if = ip_if->Next; + } + + if(ip_if && ip_if->AddressLength == 6) + { + ipx_pcap_interface_t *new_if = malloc(sizeof(ipx_pcap_interface_t)); + if(!new_if) + { + log_printf(LOG_ERROR, "Could not allocate memory!"); + continue; + } + + new_if->name = strdup(pcap_if->name); + new_if->desc = strdup(pcap_if->description + ? pcap_if->description + : pcap_if->name); + + if(!new_if->name || !new_if->desc) + { + free(new_if->name); + free(new_if->desc); + free(new_if); + + log_printf(LOG_ERROR, "Could not allocate memory!"); + continue; + } + + new_if->mac_addr = addr48_in(ip_if->Address); + + DL_APPEND(ret_interfaces, new_if); + } + else{ + log_printf(LOG_WARNING, "Could not determine MAC address of WinPcap interface '%s'", pcap_if->name); + log_printf(LOG_WARNING, "This interface will not be available for IPX use"); + } + } + } + + pcap_freealldevs(pcap_interfaces); + free(ip_interfaces); + + return ret_interfaces; +} + +void ipx_free_pcap_interfaces(ipx_pcap_interface_t **interfaces) +{ + ipx_pcap_interface_t *p, *p_tmp; + DL_FOREACH_SAFE(*interfaces, p, p_tmp) + { + DL_DELETE(*interfaces, p); + + free(p->name); + free(p->desc); + free(p); + } +} diff --git a/src/interface2.h b/src/interface2.h new file mode 100644 index 0000000..178a47a --- /dev/null +++ b/src/interface2.h @@ -0,0 +1,59 @@ +/* IPXWrapper - Interface header + * Copyright (C) 2011-2023 Daniel Collins + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef IPXWRAPPER_INTERFACE2_H +#define IPXWRAPPER_INTERFACE2_H + +#include +#include +#include +#include + +#include "config.h" +#include "common.h" + +#define WILDCARD_IFACE_HWADDR ({ \ + const unsigned char x[] = {0x00,0x00,0x00,0x00,0x00,0x00}; \ + addr48_in(x); \ +}) + +typedef struct ipx_pcap_interface ipx_pcap_interface_t; + +struct ipx_pcap_interface { + char *name; + char *desc; + + addr48_t mac_addr; + + ipx_pcap_interface_t *prev; + ipx_pcap_interface_t *next; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +IP_ADAPTER_INFO *load_sys_interfaces(void); + +ipx_pcap_interface_t *ipx_get_pcap_interfaces(void); +void ipx_free_pcap_interfaces(ipx_pcap_interface_t **interfaces); + +#ifdef __cplusplus +} +#endif + +#endif /* !IPXWRAPPER_INTERFACE2_H */ diff --git a/src/ipxconfig.cpp b/src/ipxconfig.cpp index e71ad4e..3df38b3 100644 --- a/src/ipxconfig.cpp +++ b/src/ipxconfig.cpp @@ -1,5 +1,5 @@ /* IPXWrapper - Configuration tool - * Copyright (C) 2011-2021 Daniel Collins + * Copyright (C) 2011-2022 Daniel Collins * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by @@ -27,7 +27,7 @@ #include #include "config.h" -#include "interface.h" +#include "interface2.h" #include "addr.h" const char *IPXCONFIG_WINDOW_CLASS = "ipxconfig_class";