diff options
Diffstat (limited to 'src/osd/modules/netdev/taptun.cpp')
-rw-r--r-- | src/osd/modules/netdev/taptun.cpp | 73 |
1 files changed, 46 insertions, 27 deletions
diff --git a/src/osd/modules/netdev/taptun.cpp b/src/osd/modules/netdev/taptun.cpp index c0d99ec750c..a2953f33efc 100644 --- a/src/osd/modules/netdev/taptun.cpp +++ b/src/osd/modules/netdev/taptun.cpp @@ -1,5 +1,9 @@ // license:BSD-3-Clause // copyright-holders:Carl +#include "netdev_module.h" + +#include "modules/osdmodule.h" + #if defined(OSD_NET_USE_TAPTUN) #if defined(_WIN32) @@ -13,12 +17,13 @@ #include <cerrno> #endif -#include "emu.h" +#include "osdcore.h" // osd_printf_verbose +#include "osdfile.h" // PATH_SEPARATOR #include "osdnet.h" -#include "modules/osdmodule.h" -#include "netdev_module.h" #include "unicode.h" +#include "util/hashing.h" // crc32_creator + #ifdef __linux__ #define IFF_TAP 0x0002 #define IFF_NO_PI 0x1000 @@ -31,6 +36,10 @@ #define PRODUCT_TAP_WIN_COMPONENT_ID "tap0901" #endif +namespace osd { + +namespace { + // Ethernet minimum frame length static constexpr int ETHERNET_MIN_FRAME = 64; @@ -43,7 +52,7 @@ public: } virtual ~taptun_module() { } - virtual int init(const osd_options &options); + virtual int init(osd_interface &osd, const osd_options &options); virtual void exit(); virtual bool probe() { return true; } @@ -51,16 +60,18 @@ public: -class netdev_tap : public osd_netdev +class netdev_tap : public osd_network_device { public: - netdev_tap(const char *name, class device_network_interface *ifdev, int rate); + netdev_tap(const char *name, network_handler &ifdev); ~netdev_tap(); int send(uint8_t *buf, int len) override; - void set_mac(const char *mac) override; + void set_mac(const uint8_t *mac) override; + protected: int recv_dev(uint8_t **buf) override; + private: #if defined(_WIN32) HANDLE m_handle = INVALID_HANDLE_VALUE; @@ -74,8 +85,8 @@ private: uint8_t m_buf[2048]; }; -netdev_tap::netdev_tap(const char *name, class device_network_interface *ifdev, int rate) - : osd_netdev(ifdev, rate) +netdev_tap::netdev_tap(const char *name, network_handler &ifdev) + : osd_network_device(ifdev) { #ifdef __linux__ struct ifreq ifr; @@ -134,7 +145,7 @@ netdev_tap::~netdev_tap() #endif } -void netdev_tap::set_mac(const char *mac) +void netdev_tap::set_mac(const uint8_t *mac) { memcpy(m_mac, mac, 6); } @@ -286,16 +297,21 @@ static std::vector<std::wstring> get_tap_adapters() // check if the ComponentId value indicates a TAP-Windows adapter if (RegQueryValueExW(unit_key, L"ComponentId", NULL, &data_type, LPBYTE(component_id), &component_id_len) == ERROR_SUCCESS - && data_type == REG_SZ - && safe_string(component_id, component_id_len) == L"" PRODUCT_TAP_WIN_COMPONENT_ID) + && data_type == REG_SZ) { - WCHAR net_cfg_instance_id[MAX_PATH]; - DWORD net_cfg_instance_id_len = sizeof(net_cfg_instance_id); - - // add the adapter to the result - if (RegQueryValueExW(unit_key, L"NetCfgInstanceId", NULL, &data_type, LPBYTE(net_cfg_instance_id), &net_cfg_instance_id_len) == ERROR_SUCCESS - && data_type == REG_SZ) - result.push_back(safe_string(net_cfg_instance_id, net_cfg_instance_id_len)); + std::wstring const value(safe_string(component_id, component_id_len)); + + // some older versions may not set the "root\" prefix + if (value == L"root\\" PRODUCT_TAP_WIN_COMPONENT_ID || value == L"" PRODUCT_TAP_WIN_COMPONENT_ID) + { + WCHAR net_cfg_instance_id[MAX_PATH]; + DWORD net_cfg_instance_id_len = sizeof(net_cfg_instance_id); + + // add the adapter to the result + if (RegQueryValueExW(unit_key, L"NetCfgInstanceId", NULL, &data_type, LPBYTE(net_cfg_instance_id), &net_cfg_instance_id_len) == ERROR_SUCCESS + && data_type == REG_SZ) + result.push_back(safe_string(net_cfg_instance_id, net_cfg_instance_id_len)); + } } RegCloseKey(unit_key); @@ -326,7 +342,7 @@ int netdev_tap::recv_dev(uint8_t **buf) // are in promiscuous mode or got a packet with our mac. do { len = read(m_fd, m_buf, sizeof(m_buf)); - } while((len > 0) && memcmp(get_mac(), m_buf, 6) && !get_promisc() && !(m_buf[0] & 1)); + } while((len > 0) && memcmp(&get_mac()[0], m_buf, 6) && !get_promisc() && !(m_buf[0] & 1)); if (len > 0) len = finalise_frame(m_buf, len); @@ -338,11 +354,11 @@ int netdev_tap::recv_dev(uint8_t **buf) static CREATE_NETDEV(create_tap) { - auto *dev = new netdev_tap(ifname, ifdev, rate); - return dynamic_cast<osd_netdev *>(dev); + auto *dev = new netdev_tap(ifname, ifdev); + return dynamic_cast<osd_network_device *>(dev); } -int taptun_module::init(const osd_options &options) +int taptun_module::init(osd_interface &osd, const osd_options &options) { #if defined(_WIN32) for (std::wstring &id : get_tap_adapters()) @@ -358,13 +374,16 @@ void taptun_module::exit() clear_netdev(); } +} // anonymous namespace + +} // namespace osd + #else - #include "modules/osdmodule.h" - #include "netdev_module.h" - MODULE_NOT_SUPPORTED(taptun_module, OSD_NETDEV_PROVIDER, "taptun") +namespace osd { namespace { MODULE_NOT_SUPPORTED(taptun_module, OSD_NETDEV_PROVIDER, "taptun") } } + #endif -MODULE_DEFINITION(NETDEV_TAPTUN, taptun_module) +MODULE_DEFINITION(NETDEV_TAPTUN, osd::taptun_module) |