mirror of
https://github.com/debauchee/barrier.git
synced 2026-02-12 22:55:53 +08:00
Compare commits
4 Commits
smartzeroc
...
remotes/fo
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a5eae46fe | ||
|
|
f65189190c | ||
|
|
3747ea51fd | ||
|
|
ecb61f41dc |
@@ -163,12 +163,11 @@ if (UNIX)
|
|||||||
include_directories("/usr/local/include" "/usr/local/include/avahi-compat-libdns_sd")
|
include_directories("/usr/local/include" "/usr/local/include/avahi-compat-libdns_sd")
|
||||||
link_directories("/usr/local/lib")
|
link_directories("/usr/local/lib")
|
||||||
endif()
|
endif()
|
||||||
|
if (${PKG_CONFIG_FOUND})
|
||||||
if (${PKG_CONFIG_FOUND})
|
pkg_check_modules (AVAHI_COMPAT REQUIRED avahi-compat-libdns_sd)
|
||||||
pkg_check_modules (AVAHI_COMPAT REQUIRED avahi-compat-libdns_sd)
|
include_directories (BEFORE SYSTEM ${AVAHI_COMPAT_INCLUDE_DIRS})
|
||||||
include_directories (BEFORE SYSTEM ${AVAHI_COMPAT_INCLUDE_DIRS})
|
set (CMAKE_REQUIRED_INCLUDES "${CMAKE_REQUIRED_INCLUDES};${AVAHI_COMPAT_INCLUDE_DIRS}")
|
||||||
set (CMAKE_REQUIRED_INCLUDES "${CMAKE_REQUIRED_INCLUDES};${AVAHI_COMPAT_INCLUDE_DIRS}")
|
endif ()
|
||||||
endif ()
|
|
||||||
|
|
||||||
set (XKBlib "X11/Xlib.h;X11/XKBlib.h")
|
set (XKBlib "X11/Xlib.h;X11/XKBlib.h")
|
||||||
set (CMAKE_EXTRA_INCLUDE_FILES "${XKBlib};X11/extensions/Xrandr.h")
|
set (CMAKE_EXTRA_INCLUDE_FILES "${XKBlib};X11/extensions/Xrandr.h")
|
||||||
@@ -309,28 +308,13 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
|
|||||||
${OPENSSL_ROOT}/lib/ssleay32.lib
|
${OPENSSL_ROOT}/lib/ssleay32.lib
|
||||||
)
|
)
|
||||||
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
||||||
|
set (OPENSSL_ROOT /usr/local/opt/openssl)
|
||||||
|
|
||||||
if (IS_DIRECTORY /opt/local)
|
include_directories (BEFORE SYSTEM ${OPENSSL_ROOT}/include)
|
||||||
# macports
|
set (OPENSSL_LIBS
|
||||||
set (OPENSSL_ROOT /opt/local)
|
${OPENSSL_ROOT}/lib/libssl.a
|
||||||
|
${OPENSSL_ROOT}/lib/libcrypto.a
|
||||||
set (OPENSSL_LIBS
|
)
|
||||||
${OPENSSL_ROOT}/lib/libssl.a
|
|
||||||
${OPENSSL_ROOT}/lib/libcrypto.a
|
|
||||||
z
|
|
||||||
)
|
|
||||||
elseif (IS_DIRECTORY /usr/local/opt/openssl)
|
|
||||||
# brew
|
|
||||||
set (OPENSSL_ROOT /usr/local/opt/openssl)
|
|
||||||
|
|
||||||
include_directories (BEFORE SYSTEM ${OPENSSL_ROOT}/include)
|
|
||||||
|
|
||||||
set (OPENSSL_LIBS
|
|
||||||
${OPENSSL_ROOT}/lib/libssl.a
|
|
||||||
${OPENSSL_ROOT}/lib/libcrypto.a
|
|
||||||
)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
|
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
|
||||||
set (OPENSSL_LIBS ssl crypto)
|
set (OPENSSL_LIBS ssl crypto)
|
||||||
else()
|
else()
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ Barrier is KVM software forked from Symless's synergy 1.9 codebase. Synergy was
|
|||||||
|
|
||||||
### What's different?
|
### What's different?
|
||||||
|
|
||||||
Whereas synergy has moved beyond its goals from the 1.x era, barrier aims to maintain that simplicity. Barrier will let you use your keyboard and mouse from machine A to control machine B (or more). It's that simple.
|
Whereas synergy has moved beyond its goals from the 1.x era, barrier aims to maintain that simplicity. Barrier will let you use your keyboard and mouse from machine A to control machine B (or more). That's it.
|
||||||
|
|
||||||
### Project goals
|
### Project goals
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
REM defaults - override them by creating a build_env.bat file
|
REM defaults - override them by creating a build_env.bat file
|
||||||
set B_BUILD_TYPE=Debug
|
set B_BUILD_TYPE=Debug
|
||||||
set B_QT_ROOT=C:\Qt
|
set B_QT_ROOT=C:\Qt
|
||||||
set B_QT_VER=5.11.1
|
set B_QT_VER=5.6.3
|
||||||
set B_QT_MSVC=msvc2017_64
|
set B_QT_MSVC=msvc2015_64
|
||||||
set B_BONJOUR=C:\Program Files\Bonjour SDK
|
set B_BONJOUR=C:\Program Files\Bonjour SDK
|
||||||
|
|
||||||
set savedir=%cd%
|
set savedir=%cd%
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ if [ "$(uname)" = "Darwin" ]; then
|
|||||||
# OSX needs a lot of extra help, poor thing
|
# OSX needs a lot of extra help, poor thing
|
||||||
# run the osx_environment.sh script to fix paths
|
# run the osx_environment.sh script to fix paths
|
||||||
. ./osx_environment.sh
|
. ./osx_environment.sh
|
||||||
B_CMAKE_FLAGS="-DCMAKE_OSX_SYSROOT=$(xcode-select --print-path)/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -DCMAKE_OSX_DEPLOYMENT_TARGET=10.9 $B_CMAKE_FLAGS"
|
B_CMAKE_FLAGS="-DCMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -DCMAKE_OSX_DEPLOYMENT_TARGET=10.9 $B_CMAKE_FLAGS"
|
||||||
fi
|
fi
|
||||||
# allow local customizations to build environment
|
# allow local customizations to build environment
|
||||||
[ -r ./build_env.sh ] && . ./build_env.sh
|
[ -r ./build_env.sh ] && . ./build_env.sh
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
cmake_minimum_required (VERSION 3.4)
|
cmake_minimum_required (VERSION 3.4)
|
||||||
|
|
||||||
set (BARRIER_VERSION_MAJOR 2)
|
set (BARRIER_VERSION_MAJOR 2)
|
||||||
set (BARRIER_VERSION_MINOR 2)
|
set (BARRIER_VERSION_MINOR 1)
|
||||||
set (BARRIER_VERSION_PATCH 0)
|
set (BARRIER_VERSION_PATCH 0)
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -1,37 +1,16 @@
|
|||||||
#!/bin/bash
|
#!/bin/sh
|
||||||
|
|
||||||
if [ ! $BARRIER_BUILD_ENV ]; then
|
if [ ! $BARRIER_BUILD_ENV ]; then
|
||||||
|
|
||||||
printf "Modifying environment for Barrier build..."
|
printf "Modifying environment for Barrier build..."
|
||||||
|
|
||||||
if command -v port; then
|
QT_PATH=$(brew --prefix qt)
|
||||||
printf "Detected Macports"
|
OPENSSL_PATH=$(brew --prefix openssl)
|
||||||
|
|
||||||
if [ ! -d /opt/local/lib/cmake/Qt5 ]; then
|
|
||||||
printf "Please install qt5-qtbase port"
|
|
||||||
fi
|
|
||||||
export BARRIER_BUILD_MACPORTS=1
|
|
||||||
export CMAKE_PREFIX_PATH="/opt/local/lib/cmake/Qt5:$CMAKE_PREFIX_PATH"
|
|
||||||
export LD_LIBRARY_PATH="/opt/local/lib:$LD_LIBRARY_PATH"
|
|
||||||
export CPATH="/opt/local/include:$CPATH"
|
|
||||||
export PKG_CONFIG_PATH="/opt/local/libexec/qt5/lib/pkgconfig:$PKG_CONFIG_PATH"
|
|
||||||
|
|
||||||
elif command -v brew; then
|
|
||||||
printf "Detected Homebrew"
|
|
||||||
QT_PATH=$(brew --prefix qt)
|
|
||||||
OPENSSL_PATH=$(brew --prefix openssl)
|
|
||||||
|
|
||||||
export BARRIER_BUILD_BREW=1
|
|
||||||
export CMAKE_PREFIX_PATH="$QT_PATH:$CMAKE_PREFIX_PATH"
|
|
||||||
export LD_LIBRARY_PATH="$OPENSSL_PATH/lib:$LD_LIBRARY_PATH"
|
|
||||||
export CPATH="$OPENSSL_PATH/include:$CPATH"
|
|
||||||
export PKG_CONFIG_PATH="$OPENSSL_PATH/lib/pkgconfig:$PKG_CONFIG_PATH"
|
|
||||||
|
|
||||||
else
|
|
||||||
printf "Neither Homebrew nor Macports is installed. Can't get dependency paths"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
export CMAKE_PREFIX_PATH="$QT_PATH:$CMAKE_PREFIX_PATH"
|
||||||
|
export LD_LIBRARY_PATH="$OPENSSL_PATH/lib:$LD_LIBRARY_PATH"
|
||||||
|
export CPATH="$OPENSSL_PATH/include:$CPATH"
|
||||||
|
export PKG_CONFIG_PATH="$OPENSSL_PATH/lib/pkgconfig:$PKG_CONFIG_PATH"
|
||||||
export BARRIER_BUILD_ENV=1
|
export BARRIER_BUILD_ENV=1
|
||||||
|
|
||||||
printf "done\n"
|
printf "done\n"
|
||||||
|
|||||||
@@ -6,25 +6,17 @@ set (CMAKE_AUTORCC ON)
|
|||||||
set (CMAKE_AUTOUIC ON)
|
set (CMAKE_AUTOUIC ON)
|
||||||
set (CMAKE_INCLUDE_CURRENT_DIR ON)
|
set (CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||||
|
|
||||||
|
file (GLOB GUI_SOURCE_FILES src/*.cpp src/*.h)
|
||||||
|
file (GLOB GUI_UI_FILES src/*.ui)
|
||||||
|
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
set (resources res/win/Barrier.rc)
|
set (GUI_RC_FILES res/win/Barrier.rc)
|
||||||
set (arch "win32")
|
|
||||||
else()
|
|
||||||
set (arch "unix")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
file (GLOB sources src/*.cpp src/${arch}/*.cpp)
|
|
||||||
file (GLOB headers src/*.h src/${arch}/*.h)
|
|
||||||
file (GLOB designs src/*.ui)
|
|
||||||
|
|
||||||
if (BARRIER_ADD_HEADERS)
|
|
||||||
list (APPEND sources ${headers})
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_executable (barrier WIN32
|
add_executable (barrier WIN32
|
||||||
${sources}
|
${GUI_SOURCE_FILES}
|
||||||
${designs}
|
${GUI_UI_FILES}
|
||||||
${resources}
|
${GUI_RC_FILES}
|
||||||
res/Barrier.qrc
|
res/Barrier.qrc
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,7 +32,7 @@ if (WIN32)
|
|||||||
HINTS ENV BONJOUR_SDK_HOME
|
HINTS ENV BONJOUR_SDK_HOME
|
||||||
PATH_SUFFIXES "Lib/x64")
|
PATH_SUFFIXES "Lib/x64")
|
||||||
set_target_properties (barrier PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMT")
|
set_target_properties (barrier PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMT")
|
||||||
target_link_libraries (barrier ${DNSSD_LIB} Ws2_32.lib Iphlpapi.lib)
|
target_link_libraries (barrier ${DNSSD_LIB})
|
||||||
elseif (APPLE)
|
elseif (APPLE)
|
||||||
find_library(APPSERVICES_LIB ApplicationServices)
|
find_library(APPSERVICES_LIB ApplicationServices)
|
||||||
target_link_libraries(barrier ${APPSERVICES_LIB})
|
target_link_libraries(barrier ${APPSERVICES_LIB})
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace Debauchee
|
|
||||||
{
|
|
||||||
|
|
||||||
std::string default_interface_ip();
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -20,7 +20,6 @@
|
|||||||
#include "MainWindow.h"
|
#include "MainWindow.h"
|
||||||
#include "ZeroconfRegister.h"
|
#include "ZeroconfRegister.h"
|
||||||
#include "ZeroconfBrowser.h"
|
#include "ZeroconfBrowser.h"
|
||||||
#include "DefaultInterfaceIP.h"
|
|
||||||
|
|
||||||
#include <QtNetwork>
|
#include <QtNetwork>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
@@ -35,6 +34,12 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static const QStringList preferedIPAddress(
|
||||||
|
QStringList() <<
|
||||||
|
"192.168." <<
|
||||||
|
"10." <<
|
||||||
|
"172.");
|
||||||
|
|
||||||
const char* ZeroconfService:: m_ServerServiceName = "_barrierServerZeroconf._tcp";
|
const char* ZeroconfService:: m_ServerServiceName = "_barrierServerZeroconf._tcp";
|
||||||
const char* ZeroconfService:: m_ClientServiceName = "_barrierClientZeroconf._tcp";
|
const char* ZeroconfService:: m_ClientServiceName = "_barrierClientZeroconf._tcp";
|
||||||
|
|
||||||
@@ -119,6 +124,27 @@ void ZeroconfService::errorHandle(DNSServiceErrorType errorCode)
|
|||||||
tr("Error code: %1.").arg(errorCode));
|
tr("Error code: %1.").arg(errorCode));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString ZeroconfService::getLocalIPAddresses()
|
||||||
|
{
|
||||||
|
QStringList addresses;
|
||||||
|
foreach (const QHostAddress& address, QNetworkInterface::allAddresses()) {
|
||||||
|
if (address.protocol() == QAbstractSocket::IPv4Protocol &&
|
||||||
|
address != QHostAddress(QHostAddress::LocalHost)) {
|
||||||
|
addresses.append(address.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (const QString& preferedIP, preferedIPAddress) {
|
||||||
|
foreach (const QString& address, addresses) {
|
||||||
|
if (address.startsWith(preferedIP)) {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
bool ZeroconfService::registerService(bool server)
|
bool ZeroconfService::registerService(bool server)
|
||||||
{
|
{
|
||||||
bool result = true;
|
bool result = true;
|
||||||
@@ -133,7 +159,7 @@ bool ZeroconfService::registerService(bool server)
|
|||||||
else {
|
else {
|
||||||
m_pZeroconfRegister = new ZeroconfRegister(this);
|
m_pZeroconfRegister = new ZeroconfRegister(this);
|
||||||
if (server) {
|
if (server) {
|
||||||
QString localIP = QString::fromStdString(Debauchee::default_interface_ip());
|
QString localIP = getLocalIPAddresses();
|
||||||
if (localIP.isEmpty()) {
|
if (localIP.isEmpty()) {
|
||||||
QMessageBox::warning(m_pMainWindow, tr("Barrier"),
|
QMessageBox::warning(m_pMainWindow, tr("Barrier"),
|
||||||
tr("Failed to get local IP address. "
|
tr("Failed to get local IP address. "
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ private slots:
|
|||||||
void errorHandle(DNSServiceErrorType errorCode);
|
void errorHandle(DNSServiceErrorType errorCode);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QString getLocalIPAddresses();
|
||||||
bool registerService(bool server);
|
bool registerService(bool server);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -67,13 +67,6 @@ int main(int argc, char* argv[])
|
|||||||
/* Workaround for QTBUG-40332 - "High ping when QNetworkAccessManager is instantiated" */
|
/* Workaround for QTBUG-40332 - "High ping when QNetworkAccessManager is instantiated" */
|
||||||
::setenv ("QT_BEARER_POLL_TIMEOUT", "-1", 1);
|
::setenv ("QT_BEARER_POLL_TIMEOUT", "-1", 1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if _WIN32
|
|
||||||
// winsock needs to be initialized for DefaultInterfaceIP
|
|
||||||
WSADATA wd;
|
|
||||||
WSAStartup(MAKEWORD(2, 0), &wd);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
QCoreApplication::setOrganizationName("Debauchee");
|
QCoreApplication::setOrganizationName("Debauchee");
|
||||||
QCoreApplication::setOrganizationDomain("github.com");
|
QCoreApplication::setOrganizationDomain("github.com");
|
||||||
QCoreApplication::setApplicationName("Barrier");
|
QCoreApplication::setApplicationName("Barrier");
|
||||||
|
|||||||
@@ -1,79 +0,0 @@
|
|||||||
#include "IfAddrsResource.h"
|
|
||||||
#include "SocketResource.h"
|
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
|
|
||||||
#ifdef __linux__
|
|
||||||
#include <linux/wireless.h>
|
|
||||||
#else
|
|
||||||
#include <net/if.h>
|
|
||||||
#include <net/if_media.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace Debauchee
|
|
||||||
{
|
|
||||||
|
|
||||||
static bool is_wireless(const char * ifname, const SocketResource& sock)
|
|
||||||
{
|
|
||||||
#ifdef __linux__
|
|
||||||
struct iwreq req;
|
|
||||||
::memset(&req, 0, sizeof(struct iwreq));
|
|
||||||
::strncpy(req.ifr_name, ifname, IFNAMSIZ - 1);
|
|
||||||
return ioctl(sock, SIOCGIWMODE, &req) >= 0;
|
|
||||||
#else
|
|
||||||
struct ifmediareq req;
|
|
||||||
::memset(&req, 0, sizeof(struct ifmediareq));
|
|
||||||
::strncpy(req.ifm_name, ifname, IFNAMSIZ - 1);
|
|
||||||
if (ioctl(sock, SIOCGIFMEDIA, &req) >= 0 &&
|
|
||||||
(req.ifm_status & IFM_AVALID)
|
|
||||||
) {
|
|
||||||
return IFM_TYPE(req.ifm_active) == IFM_IEEE80211;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool is_wireless(const char * ifname)
|
|
||||||
{
|
|
||||||
if (ifname) {
|
|
||||||
SocketResource sock(AF_INET, SOCK_DGRAM, 0);
|
|
||||||
if (sock.is_valid()) {
|
|
||||||
return is_wireless(ifname, sock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string default_interface_ip()
|
|
||||||
{
|
|
||||||
std::string wirelessAddress;
|
|
||||||
IfAddrsResource ifa;
|
|
||||||
if (ifa.is_valid()) {
|
|
||||||
for (struct ifaddrs * next = ifa; next; next = next->ifa_next) {
|
|
||||||
auto sain = (struct sockaddr_in *)next->ifa_addr;
|
|
||||||
if (!sain || sain->sin_family != AF_INET ||
|
|
||||||
!(next->ifa_flags & IFF_RUNNING) ||
|
|
||||||
(next->ifa_flags & IFF_LOOPBACK)
|
|
||||||
) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
std::string address = inet_ntoa(sain->sin_addr);
|
|
||||||
// take first wired address right away
|
|
||||||
if (!is_wireless(next->ifa_name)) {
|
|
||||||
return address;
|
|
||||||
}
|
|
||||||
// save first wireless address to be used if we don't find a wired one
|
|
||||||
if (wirelessAddress.empty()) {
|
|
||||||
wirelessAddress = address;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return wirelessAddress;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#include <sys/types.h>
|
|
||||||
#include <ifaddrs.h>
|
|
||||||
|
|
||||||
namespace Debauchee
|
|
||||||
{
|
|
||||||
|
|
||||||
class IfAddrsResource
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit IfAddrsResource() :
|
|
||||||
_valid(getifaddrs(&_ifa) == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
~IfAddrsResource()
|
|
||||||
{
|
|
||||||
if (_valid) {
|
|
||||||
freeifaddrs(_ifa);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_valid() const { return _valid; }
|
|
||||||
|
|
||||||
operator struct ifaddrs * () const { return _ifa; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool _valid;
|
|
||||||
struct ifaddrs * _ifa;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
namespace Debauchee
|
|
||||||
{
|
|
||||||
|
|
||||||
class SocketResource
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit SocketResource(int domain, int type, int protocol) :
|
|
||||||
_fd(socket(domain, type, protocol))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
~SocketResource()
|
|
||||||
{
|
|
||||||
if (is_valid()) {
|
|
||||||
close(_fd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_valid() const { return _fd >= 0; }
|
|
||||||
|
|
||||||
operator int() const { return _fd; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
int _fd;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
// REQUIRES: Ws2_32.lib, Iphlpapi.lib
|
|
||||||
// REQUIRES: WSAStartup(..)
|
|
||||||
|
|
||||||
// see comments regarding WSAAddressToStringA in address_string(..)
|
|
||||||
#define _WINSOCK_DEPRECATED_NO_WARNINGS
|
|
||||||
|
|
||||||
#include <WinSock2.h>
|
|
||||||
#include <iphlpapi.h>
|
|
||||||
#include <string>
|
|
||||||
#include "HeapResource.h"
|
|
||||||
|
|
||||||
namespace Debauchee
|
|
||||||
{
|
|
||||||
|
|
||||||
static ULONG get_addresses(IP_ADAPTER_ADDRESSES * addresses, PULONG sz)
|
|
||||||
{
|
|
||||||
return GetAdaptersAddresses(AF_INET,
|
|
||||||
GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
|
|
||||||
NULL, addresses, sz);
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string address_string(const SOCKET_ADDRESS& address)
|
|
||||||
{
|
|
||||||
// planning to use this with Qt but QString does not have a c'tor
|
|
||||||
// for wide strings so we need to force the ANSI API here
|
|
||||||
DWORD szStrAddress = 1023;
|
|
||||||
char strAddress[1024];
|
|
||||||
if (WSAAddressToStringA(address.lpSockaddr, address.iSockaddrLength, NULL, strAddress, &szStrAddress)) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return strAddress;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string first_unicast_address(IP_ADAPTER_UNICAST_ADDRESS * unicast)
|
|
||||||
{
|
|
||||||
while (unicast) {
|
|
||||||
auto address = address_string(unicast->Address);
|
|
||||||
if (!address.empty()) {
|
|
||||||
return address;
|
|
||||||
}
|
|
||||||
unicast = unicast->Next;
|
|
||||||
}
|
|
||||||
return std::string();
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool is_useable_interface(const IP_ADAPTER_ADDRESSES * ifa)
|
|
||||||
{
|
|
||||||
return
|
|
||||||
// is an ethernet or wireless interface AND
|
|
||||||
(ifa->IfType == IF_TYPE_ETHERNET_CSMACD || ifa->IfType == IF_TYPE_IEEE80211) &&
|
|
||||||
// is up
|
|
||||||
ifa->OperStatus == IfOperStatusUp;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string default_ip_address(const IP_ADAPTER_ADDRESSES * next)
|
|
||||||
{
|
|
||||||
std::string wirelessAddress;
|
|
||||||
for ( ; next; next = next->Next) {
|
|
||||||
if (!is_useable_interface(next)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
std::string address = first_unicast_address(next->FirstUnicastAddress);
|
|
||||||
if (address.empty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// take first wired address right away
|
|
||||||
if (next->IfType == IF_TYPE_ETHERNET_CSMACD) {
|
|
||||||
return address;
|
|
||||||
}
|
|
||||||
// save first wireless address to be used if we don't find a wired one
|
|
||||||
if (wirelessAddress.empty()) {
|
|
||||||
wirelessAddress = address;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return wirelessAddress;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string default_interface_ip()
|
|
||||||
{
|
|
||||||
const ULONG DefaultSzAddresses = 15 * 1024; // 15k recommended by msdn
|
|
||||||
ULONG szAddresses = DefaultSzAddresses;
|
|
||||||
HeapResource<IP_ADAPTER_ADDRESSES> addresses(GetProcessHeap(), 0, DefaultSzAddresses);
|
|
||||||
if (addresses.is_valid()) {
|
|
||||||
ULONG result;
|
|
||||||
while (ERROR_BUFFER_OVERFLOW == (result = get_addresses(addresses, &szAddresses))) {
|
|
||||||
// add more space in case more adapters have shown up
|
|
||||||
szAddresses += DefaultSzAddresses;
|
|
||||||
swap(addresses, HeapResource<IP_ADAPTER_ADDRESSES>(GetProcessHeap(), 0, szAddresses));
|
|
||||||
if (!addresses.is_valid()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (result == ERROR_SUCCESS) {
|
|
||||||
return default_ip_address(addresses);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return std::string();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
namespace Debauchee
|
|
||||||
{
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
class HeapResource
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit HeapResource(HANDLE heap, DWORD flags, SIZE_T sz) :
|
|
||||||
_heap(heap),
|
|
||||||
_flags(flags),
|
|
||||||
_mem((T*)HeapAlloc(heap, flags, sz))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
HeapResource(HeapResource<T>&& other)
|
|
||||||
: _mem(NULL)
|
|
||||||
{
|
|
||||||
swap(*this, other);
|
|
||||||
}
|
|
||||||
|
|
||||||
~HeapResource()
|
|
||||||
{
|
|
||||||
if (is_valid()) {
|
|
||||||
HeapFree(_heap, _flags, _mem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
friend void swap(HeapResource<T>& first, HeapResource<T>& second)
|
|
||||||
{
|
|
||||||
using std::swap;
|
|
||||||
swap(first._heap, second._heap);
|
|
||||||
swap(first._flags, second._flags);
|
|
||||||
swap(first._mem, second._mem);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_valid() const { return _mem != NULL; }
|
|
||||||
|
|
||||||
operator T*() const { return _mem; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
HANDLE _heap;
|
|
||||||
DWORD _flags;
|
|
||||||
T * _mem;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user