Browse Source

Fix Windows build: POSIX dependencies (netdb.h, fork, setsid, SIGHUP, etc.)

- Wrap POSIX headers (netdb.h, ifaddrs.h, net/if.h) in #ifndef _WIN32 in config_parser.c
- Add Windows stub for get_netif_index() - returns 0 (no interface binding)
- Wrap daemonize() function for Windows - returns 0 (no fork/setsid support)
- Replace setlinebuf() with setvbuf() for Windows compatibility
- Wrap SIGHUP signal handler in #ifndef _WIN32
- Add explicit poll() declaration for Windows in platform_compat.h
- All 22 tests pass on Linux
nodeinfo-routing-update
Evgeny 8 months ago
parent
commit
605a928350
  1. 2
      lib/platform_compat.h
  2. 8
      src/config_parser.c
  3. 13
      src/utun.c

2
lib/platform_compat.h

@ -103,6 +103,8 @@
// nanosleep already defined in pthread_time.h on MSYS2 // nanosleep already defined in pthread_time.h on MSYS2
// poll() and pollfd already defined in winsock2.h on MSYS2 // poll() and pollfd already defined in winsock2.h on MSYS2
// Explicitly declare poll() to avoid implicit declaration warning
int poll(struct pollfd *fds, unsigned long nfds, int timeout);
// sa_family_t might not be defined on some Windows setups // sa_family_t might not be defined on some Windows setups
#ifndef sa_family_t #ifndef sa_family_t

8
src/config_parser.c

@ -9,9 +9,11 @@
#include <ctype.h> #include <ctype.h>
#include "../lib/platform_compat.h" #include "../lib/platform_compat.h"
#include <errno.h> #include <errno.h>
#ifndef _WIN32
#include <netdb.h> #include <netdb.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <net/if.h> #include <net/if.h>
#endif
#define MAX_LINE_LEN 1024 #define MAX_LINE_LEN 1024
#define INITIAL_ARRAY_CAPACITY 8 #define INITIAL_ARRAY_CAPACITY 8
@ -173,7 +175,13 @@ static int parse_address_and_port(const char *str, struct sockaddr_storage *sock
static uint32_t get_netif_index(const char *ifname) { static uint32_t get_netif_index(const char *ifname) {
if (!ifname || strlen(ifname) == 0) return 0; if (!ifname || strlen(ifname) == 0) return 0;
#ifdef _WIN32
// Windows doesn't have if_nametoindex, return 0 to indicate no specific interface
(void)ifname;
return 0;
#else
return if_nametoindex(ifname); return if_nametoindex(ifname);
#endif
} }
static struct CFG_CLIENT_LINK* create_client_link(struct CFG_SERVER* local_srv, const char *remote_addr) { static struct CFG_CLIENT_LINK* create_client_link(struct CFG_SERVER* local_srv, const char *remote_addr) {

13
src/utun.c

@ -159,6 +159,10 @@ static void remove_pidfile(const char *pidfile) {
// Daemonize process // Daemonize process
static int daemonize(void) { static int daemonize(void) {
#ifdef _WIN32
// Windows doesn't support fork/setsid, just return success
return 0;
#else
pid_t pid = fork(); pid_t pid = fork();
if (pid < 0) { if (pid < 0) {
perror("fork"); perror("fork");
@ -196,6 +200,7 @@ static int daemonize(void) {
} }
return 0; return 0;
#endif
} }
// Open log file // Open log file
@ -208,8 +213,12 @@ static FILE* open_logfile(const char *logfile) {
return stderr; return stderr;
} }
// Set line buffering // Set line buffering (Windows uses _IOLBF via setvbuf)
#ifdef _WIN32
setvbuf(fp, NULL, _IOLBF, 0);
#else
setlinebuf(fp); setlinebuf(fp);
#endif
return fp; return fp;
} }
@ -283,7 +292,9 @@ int main(int argc, char *argv[]) {
// Setup signal handlers // Setup signal handlers
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler); signal(SIGTERM, signal_handler);
#ifndef _WIN32
signal(SIGHUP, signal_handler); signal(SIGHUP, signal_handler);
#endif
// Daemonize if not in foreground // Daemonize if not in foreground
if (!args.foreground) { if (!args.foreground) {

Loading…
Cancel
Save