Skip to content

Commit

Permalink
支持在未适配客户端的架构上编译出可用的服务端
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Aug 11, 2024
1 parent ec5993b commit 0015eeb
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 40 deletions.
17 changes: 10 additions & 7 deletions src/core/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
#ifndef CANDY_CORE_COMMON_H
#define CANDY_CORE_COMMON_H

#if defined(__linux__) || defined(__linux)
#include <Poco/Platform.h>

#if POCO_OS == POCO_OS_LINUX
#include <netdb.h>
#define CANDY_SYSTEM "linux"
#endif

#if defined(__APPLE__) || defined(__MACH__)
#elif POCO_OS == POCO_OS_MAC_OS_X
#include <netdb.h>
#define CANDY_SYSTEM "macos"
#endif

#if defined(_WIN32) || defined(_WIN64)
#elif POCO_OS == POCO_OS_ANDROID
#include <netdb.h>
#define CANDY_SYSTEM "android"
#elif POCO_OS == POCO_OS_WINDOWS_NT
#include <winsock2.h>
#include <ws2tcpip.h>
#define CANDY_SYSTEM "windows"
#else
#define CANDY_SYSTEM "unknown"
#endif

#ifndef CANDY_VERSION
Expand Down
3 changes: 2 additions & 1 deletion src/main/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "utility/argparse.h"
#include "utility/random.h"
#include "utility/time.h"
#include <Poco/Platform.h>
#include <Poco/String.h>
#include <atomic>
#include <bit>
Expand Down Expand Up @@ -117,7 +118,7 @@ void parseConfig(std::string cfgFile, arguments &args) {
}
}

#if defined(_WIN32) || defined(_WIN64)
#if POCO_OS == POCO_OS_WINDOWS_NT

bool netStartup() {
WSADATA data;
Expand Down
3 changes: 2 additions & 1 deletion src/peer/posix.cc → src/peer/unix.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: MIT
#if defined(__linux__) || defined(__linux) || defined(__APPLE__) || defined(__MACH__)
#include <Poco/Platform.h>
#if defined(POCO_OS_FAMILY_UNIX)

#include "peer/peer.h"
#include "utility/address.h"
Expand Down
4 changes: 3 additions & 1 deletion src/peer/windows.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: MIT
#if defined(_WIN32) || defined(_WIN64)
#include <Poco/Platform.h>

#if POCO_OS == POCO_OS_WINDOWS_NT

#include "peer/peer.h"
#include "utility/address.h"
Expand Down
7 changes: 7 additions & 0 deletions src/tun/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ endif()
if (${CANDY_STATIC_OPENSSL})
target_link_libraries(tun PRIVATE ${OPENSSL_LIB_CRYPTO} ${OPENSSL_LIB_SSL})
endif()

if (${CANDY_STATIC_POCO})
target_link_libraries(tun PRIVATE Poco::Foundation)
else()
find_package(Poco REQUIRED Net)
target_link_libraries(tun PRIVATE Poco::Foundation)
endif()
3 changes: 2 additions & 1 deletion src/tun/linux.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: MIT
#if defined(__linux__) || defined(__linux)
#include <Poco/Platform.h>
#if POCO_OS == POCO_OS_LINUX

#include "tun/tun.h"
#include "utility/address.h"
Expand Down
47 changes: 24 additions & 23 deletions src/tun/darwin.cc → src/tun/macos.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: MIT
#if defined(__APPLE__) || defined(__MACH__)
#include <Poco/Platform.h>
#if POCO_OS == POCO_OS_MAC_OS_X

#include "tun/tun.h"
#include "utility/address.h"
Expand All @@ -22,7 +23,7 @@
#include <unistd.h>

namespace {
class DarwinTun {
class MacTun {
public:
int setName(const std::string &name) {
this->name = name.empty() ? "candy" : "candy-" + name;
Expand Down Expand Up @@ -257,30 +258,30 @@ class DarwinTun {
namespace Candy {

Tun::Tun() {
this->impl = std::make_shared<DarwinTun>();
this->impl = std::make_shared<MacTun>();
}

Tun::~Tun() {
this->impl.reset();
}

int Tun::setName(const std::string &name) {
std::shared_ptr<DarwinTun> tun;
std::shared_ptr<MacTun> tun;

tun = std::any_cast<std::shared_ptr<DarwinTun>>(this->impl);
tun = std::any_cast<std::shared_ptr<MacTun>>(this->impl);
tun->setName(name);
return 0;
}

int Tun::setAddress(const std::string &cidr) {
std::shared_ptr<DarwinTun> tun;
std::shared_ptr<MacTun> tun;
Address address;

if (address.cidrUpdate(cidr)) {
return -1;
}
spdlog::info("client address: {}", address.getCidr());
tun = std::any_cast<std::shared_ptr<DarwinTun>>(this->impl);
tun = std::any_cast<std::shared_ptr<MacTun>>(this->impl);
if (tun->setIP(address.getIp())) {
return -1;
}
Expand All @@ -291,56 +292,56 @@ int Tun::setAddress(const std::string &cidr) {
}

uint32_t Tun::getIP() {
std::shared_ptr<DarwinTun> tun;
tun = std::any_cast<std::shared_ptr<DarwinTun>>(this->impl);
std::shared_ptr<MacTun> tun;
tun = std::any_cast<std::shared_ptr<MacTun>>(this->impl);
return tun->getIP();
}

int Tun::setMTU(int mtu) {
std::shared_ptr<DarwinTun> tun;
tun = std::any_cast<std::shared_ptr<DarwinTun>>(this->impl);
std::shared_ptr<MacTun> tun;
tun = std::any_cast<std::shared_ptr<MacTun>>(this->impl);
if (tun->setMTU(mtu)) {
return -1;
}
return 0;
}

int Tun::setTimeout(int timeout) {
std::shared_ptr<DarwinTun> tun;
tun = std::any_cast<std::shared_ptr<DarwinTun>>(this->impl);
std::shared_ptr<MacTun> tun;
tun = std::any_cast<std::shared_ptr<MacTun>>(this->impl);
if (tun->setTimeout(timeout)) {
return -1;
}
return 0;
}

int Tun::up() {
std::shared_ptr<DarwinTun> tun;
tun = std::any_cast<std::shared_ptr<DarwinTun>>(this->impl);
std::shared_ptr<MacTun> tun;
tun = std::any_cast<std::shared_ptr<MacTun>>(this->impl);
return tun->up();
}

int Tun::down() {
std::shared_ptr<DarwinTun> tun;
tun = std::any_cast<std::shared_ptr<DarwinTun>>(this->impl);
std::shared_ptr<MacTun> tun;
tun = std::any_cast<std::shared_ptr<MacTun>>(this->impl);
return tun->down();
}

int Tun::read(std::string &buffer) {
std::shared_ptr<DarwinTun> tun;
tun = std::any_cast<std::shared_ptr<DarwinTun>>(this->impl);
std::shared_ptr<MacTun> tun;
tun = std::any_cast<std::shared_ptr<MacTun>>(this->impl);
return tun->read(buffer);
}

int Tun::write(const std::string &buffer) {
std::shared_ptr<DarwinTun> tun;
tun = std::any_cast<std::shared_ptr<DarwinTun>>(this->impl);
std::shared_ptr<MacTun> tun;
tun = std::any_cast<std::shared_ptr<MacTun>>(this->impl);
return tun->write(buffer);
}

int Tun::setSysRtTable(uint32_t dst, uint32_t mask, uint32_t nexthop) {
std::shared_ptr<DarwinTun> tun;
tun = std::any_cast<std::shared_ptr<DarwinTun>>(this->impl);
std::shared_ptr<MacTun> tun;
tun = std::any_cast<std::shared_ptr<MacTun>>(this->impl);
return tun->setSysRtTable(dst, mask, nexthop);
}

Expand Down
54 changes: 54 additions & 0 deletions src/tun/tun.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <Poco/Platform.h>
#if POCO_OS != POCO_OS_LINUX && POCO_OS != POCO_OS_MAC_OS_X && POCO_OS != POCO_OS_WINDOWS_NT

#include "tun/tun.h"

namespace Candy {

Tun::Tun() {}

Tun::~Tun() {}

int Tun::setName(const std::string &name) {
return -1;
}

int Tun::setAddress(const std::string &cidr) {
return -1;
}

uint32_t Tun::getIP() {
return -1;
}

int Tun::setMTU(int mtu) {
return -1;
}

int Tun::setTimeout(int timeout) {
return -1;
}

int Tun::up() {
return -1;
}

int Tun::down() {
return -1;
}

int Tun::read(std::string &buffer) {
return -1;
}

int Tun::write(const std::string &buffer) {
return -1;
}

int Tun::setSysRtTable(uint32_t dst, uint32_t mask, uint32_t nexthop) {
return -1;
}

} // namespace Candy

#endif
3 changes: 2 additions & 1 deletion src/tun/windows.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: MIT
#if defined(_WIN32) || defined(_WIN64)
#include <Poco/Platform.h>
#if POCO_OS == POCO_OS_WINDOWS_NT

#include "tun/tun.h"
#include "utility/address.h"
Expand Down
7 changes: 7 additions & 0 deletions src/utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ else()
include_directories(${DEPS_INCLUDEDIR})
target_link_libraries(utility PRIVATE ${DEPS_LIBRARIES})
endif()

if (${CANDY_STATIC_POCO})
target_link_libraries(utility PRIVATE Poco::Foundation)
else()
find_package(Poco REQUIRED Net)
target_link_libraries(utility PRIVATE Poco::Foundation)
endif()
8 changes: 4 additions & 4 deletions src/utility/address.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: MIT
#include "utility/address.h"
#include "utility/byteswap.h"
#include <Poco/Platform.h>
#include <spdlog/spdlog.h>

#if defined(__linux__) || defined(__linux)
#if defined(POCO_OS_FAMILY_UNIX)
#include <arpa/inet.h>
#elif defined(__APPLE__) || defined(__MACH__)
#include <arpa/inet.h>
#elif defined(_WIN32) || defined(_WIN64)
#include <sys/socket.h>
#elif defined(POCO_OS_FAMILY_WINDOWS)
#include <ws2tcpip.h>
#endif

Expand Down
6 changes: 5 additions & 1 deletion src/utility/time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "utility/time.h"
#include "utility/address.h"
#include "utility/byteswap.h"
#include <Poco/Platform.h>
#include <bit>
#include <chrono>
#include <limits>
Expand Down Expand Up @@ -37,9 +38,12 @@ struct ntp_packet {

} // namespace

#if defined(__linux__) || defined(__linux) || defined(__APPLE__) || defined(__MACH__)
#if defined(POCO_OS_FAMILY_UNIX)
#include <netdb.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

static int64_t ntpTime() {
Expand Down

0 comments on commit 0015eeb

Please sign in to comment.