Feature: Transfer a Linux host to another master
The Linux client ignored COMMAND_ASSIGN_MASTER, so a host could not be moved to a different controller the way the Windows client allows. The payload is host:port:days:shine[:transport]; the parsing, record encoding and address application live in a shared header so a macOS client can reuse them unchanged. An assignment only rewrites the connection target. The running session stays up and the next reconnect - a dropped link or a restart - goes to the new master, matching the Windows behaviour. The target and its expiry are stored in a single "transfer" config key, while the address the process started with is kept in memory only: the config's own master/port entries are never overwritten, so restarting mid-transfer still has the original to fall back to. When the expiry passes the client restores that address, clears the key and reconnects. Malformed payloads and zero-day assignments are rejected outright. The way back depends on an expiry time, so honouring a permanent move would strand a host on an address it cannot leave. Give the POSIX connect() a timeout as well (non-blocking connect plus poll, 10 seconds). A blocking connect waiting for SYN retransmission froze the main loop for about two minutes when the port was filtered, delaying both heartbeats and the expiry restore. The transfer state is initialised after daemonize() for the same reason it is logged: logging before the double fork can leave the child deadlocked on the logger mutex. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
0b22d5e870
commit
3b3aed50e5
4 files changed
+346
-6
No files matched your search
+56
-3
@@ -11,6 +11,9 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h> // For struct sockaddr_in
|
||||
#include <unistd.h> // For close()
|
||||
#include <fcntl.h> // For fcntl():非阻塞 connect
|
||||
#include <poll.h> // For poll():连接超时
|
||||
#include <errno.h> // For EINPROGRESS/EINTR
|
||||
#include <cstring> // For memset()
|
||||
inline int WSAGetLastError()
|
||||
{
|
||||
@@ -387,6 +390,55 @@ BOOL ConnectWithTimeout(SOCKET sock, SOCKADDR *addr, int timeout_sec=5)
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
#else
|
||||
// POSIX 版超时连接。阻塞 connect() 在 SYN 被静默丢弃时(端口被防火墙 DROP、运营商
|
||||
// 黑洞、出口 ACL 丢包)不回 RST 也不回 ICMP,要等内核重传耗尽(Linux 默认
|
||||
// tcp_syn_retries=6,约 127 秒)才失败。这段时间里主循环完全停摆:到期检查、心跳、
|
||||
// g_bExit 都轮不到执行,「主机转移到期自动回原主控」这类兜底会迟到两分多钟——
|
||||
// 2026-11-07 在 Ubuntu 24.04 上用被公司出口过滤的 6543 端口实测。
|
||||
// 非阻塞 connect + poll,超时即按普通连接失败返回,让上层 5 秒后重试。
|
||||
BOOL ConnectWithTimeout(SOCKET sock, const struct sockaddr* addr, int timeout_sec=10)
|
||||
{
|
||||
int flags = fcntl(sock, F_GETFL, 0);
|
||||
if (flags < 0 || fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
|
||||
Mprintf("[ERROR] connect: fcntl failed, errno=%d\n", errno);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
BOOL ok = FALSE;
|
||||
if (connect(sock, addr, sizeof(struct sockaddr_in)) == 0) {
|
||||
ok = TRUE; // 回环/同机端口,立即成功
|
||||
} else if (errno == EINPROGRESS || errno == EINTR) {
|
||||
struct pollfd pfd;
|
||||
pfd.fd = sock;
|
||||
pfd.events = POLLOUT;
|
||||
pfd.revents = 0;
|
||||
int rc;
|
||||
do {
|
||||
rc = poll(&pfd, 1, timeout_sec * 1000);
|
||||
} while (rc < 0 && errno == EINTR); // 信号打断不算失败
|
||||
// 连接失败时 revents 不保证带 POLLOUT(可能只有 POLLERR/POLLHUP),
|
||||
// 三种都当「有结果」处理,再统一从 SO_ERROR 读真正的结果。
|
||||
if (rc > 0 && (pfd.revents & (POLLOUT | POLLERR | POLLHUP))) {
|
||||
// 可写只代表「连接有结果」,失败也要在这里读出来
|
||||
int err = 0;
|
||||
socklen_t len = sizeof(err);
|
||||
ok = (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len) == 0 && err == 0);
|
||||
if (!ok) Mprintf("[ERROR] connect: %d\n", err);
|
||||
} else if (rc == 0) {
|
||||
Mprintf("[ERROR] connect timeout after %ds\n", timeout_sec);
|
||||
} else {
|
||||
Mprintf("[ERROR] connect: poll failed, errno=%d\n", errno);
|
||||
}
|
||||
} else {
|
||||
Mprintf("[ERROR] connect: errno=%d\n", errno);
|
||||
}
|
||||
|
||||
// 无论成败都恢复原阻塞标志:失败后 socket 会被关闭,成功时后续 recv/TLS 握手
|
||||
// 必须回到阻塞语义,否则接收线程会在 EAGAIN 上空转。
|
||||
fcntl(sock, F_SETFL, flags);
|
||||
return ok;
|
||||
}
|
||||
#endif
|
||||
|
||||
BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
@@ -435,9 +487,10 @@ BOOL IOCPClient::ConnectServer(const char* szServerIP, unsigned short uPort)
|
||||
return false;
|
||||
}
|
||||
|
||||
// 连接到服务器
|
||||
if (connect(m_sClientSocket, (struct sockaddr*)&m_ServerAddr, sizeof(m_ServerAddr)) == -1) {
|
||||
Mprintf("Connection failed\n");
|
||||
// 连接到服务器(带超时:阻塞 connect 在 SYN 被丢弃时要等内核重传耗尽,
|
||||
// 期间主循环的心跳与到期检查全都停摆,见 ConnectWithTimeout 注释)
|
||||
if (!ConnectWithTimeout(m_sClientSocket, (struct sockaddr*)&m_ServerAddr)) {
|
||||
Mprintf("Connection failed: %s:%d\n", m_sCurIP.c_str(), (int)port);
|
||||
close(m_sClientSocket);
|
||||
m_sClientSocket = -1; // 标记套接字无效
|
||||
return false;
|
||||
|
||||
Reference in new issue
Block a user