Browse Source

fix: исправлена блокировка при добавлении маршрутов через netlink

Проблема: recv() на netlink сокете блокировал бесконечно,
если ядро не отвечало на сообщение.

Решение:
- Добавлен флаг SOCK_NONBLOCK при создании сокета
- Добавлен poll() с таймаутом 1 секунда перед recv()
- Добавлен заголовок poll.h

Теперь utun не зависает при старте и продолжает инициализацию сокетов.
nodeinfo-routing-update
Evgeny 2 months ago
parent
commit
424e2c6b14
  1. 16
      src/tun_route.c

16
src/tun_route.c

@ -21,6 +21,7 @@
#include <sys/socket.h>
#include <unistd.h>
#include <net/if.h>
#include <poll.h>
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
#include <sys/socket.h>
#include <net/route.h>
@ -45,7 +46,7 @@ struct nl_req {
};
static int netlink_route(int ifindex, uint32_t network, uint8_t prefix_len, int cmd, int flags) {
int fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
int fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK, NETLINK_ROUTE);
if (fd < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to create netlink socket: %s", strerror(errno));
return -1;
@ -87,6 +88,19 @@ static int netlink_route(int ifindex, uint32_t network, uint8_t prefix_len, int
return -1;
}
// Wait for response with timeout using poll
struct pollfd pfd = { .fd = fd, .events = POLLIN };
int poll_ret = poll(&pfd, 1, 1000); // 1 second timeout
if (poll_ret < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "poll() failed: %s", strerror(errno));
close(fd);
return -1;
} else if (poll_ret == 0) {
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Timeout waiting for netlink response");
close(fd);
return -1;
}
// Receive response
char reply[4096];
ssize_t len = recv(fd, reply, sizeof(reply), 0);

Loading…
Cancel
Save