1
0
mirror of https://github.com/solemnwarning/ipxwrapper synced 2024-12-30 16:45:37 +01:00

Split interface.[ch] into two files to fix ipxconfig link error.

This commit is contained in:
Daniel Collins 2023-09-14 00:30:03 +01:00
parent 59d2bbd2b7
commit 056ec4b115
6 changed files with 257 additions and 188 deletions

View File

@ -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)

View File

@ -24,6 +24,7 @@
#include <pcap.h>
#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;

View File

@ -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

192
src/interface2.c Normal file
View File

@ -0,0 +1,192 @@
/* IPXWrapper - Interface functions
* Copyright (C) 2011-2023 Daniel Collins <solemnwarning@solemnwarning.net>
*
* 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 <windows.h>
#include <iphlpapi.h>
#include <utlist.h>
#include <time.h>
#include <pcap.h>
#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);
}
}

59
src/interface2.h Normal file
View File

@ -0,0 +1,59 @@
/* IPXWrapper - Interface header
* Copyright (C) 2011-2023 Daniel Collins <solemnwarning@solemnwarning.net>
*
* 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 <iphlpapi.h>
#include <stdint.h>
#include <utlist.h>
#include <pcap.h>
#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 */

View File

@ -1,5 +1,5 @@
/* IPXWrapper - Configuration tool
* Copyright (C) 2011-2021 Daniel Collins <solemnwarning@solemnwarning.net>
* Copyright (C) 2011-2022 Daniel Collins <solemnwarning@solemnwarning.net>
*
* 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 <set>
#include "config.h"
#include "interface.h"
#include "interface2.h"
#include "addr.h"
const char *IPXCONFIG_WINDOW_CLASS = "ipxconfig_class";