From 473b89c963c600ee7d1c446ee7e7648787f4c38c Mon Sep 17 00:00:00 2001 From: Hamish Coleman Date: Wed, 28 Jun 2023 23:24:49 +0100 Subject: [PATCH] Simplify build system by using standard macro Most environments have predefined macros that identify the environment to the source code. If we use these macros instead of defining our own then there is one less parameter difference to keep track of with different builds cf: http://web.archive.org/web/20191012035921/http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system https://sourceforge.net/p/predef/wiki/OperatingSystems/ --- Makefile | 2 +- include/n2n.h | 14 ++++----- include/n2n_define.h | 2 +- include/n2n_typedefs.h | 14 ++++----- include/n2n_wire.h | 6 ++-- include/random_numbers.h | 2 +- src/edge.c | 58 ++++++++++++++++++------------------ src/edge_management.c | 2 +- src/edge_utils.c | 32 ++++++++++---------- src/edge_utils_win32.c | 2 +- src/example_edge_embed.c | 2 +- src/example_sn_embed.c | 2 +- src/management.c | 2 +- src/management.h | 2 +- src/n2n.c | 14 ++++----- src/network_traffic_filter.c | 2 +- src/random_numbers.c | 2 +- src/sn_management.c | 2 +- src/sn_utils.c | 14 ++++----- src/supernode.c | 26 ++++++++-------- src/wire.c | 2 +- tools/n2n-decode.c | 6 ++-- tools/n2n-portfwd.c | 14 ++++----- tools/n2n-route.c | 28 ++++++++--------- win32/getopt.c | 6 ++-- 25 files changed, 129 insertions(+), 129 deletions(-) diff --git a/Makefile b/Makefile index 8e4f86a..efa9fc4 100644 --- a/Makefile +++ b/Makefile @@ -169,7 +169,7 @@ LDLIBS+=-lsocket -lnsl endif ifeq ($(CONFIG_TARGET),mingw) -CFLAGS+=-I. -I./win32 -DWIN32 +CFLAGS+=-I. -I./win32 LDLIBS+=$(abspath win32/n2n_win32.a) LDLIBS+=-lnetapi32 -lws2_32 -liphlpapi N2N_DEPS+=win32/n2n_win32.a diff --git a/include/n2n.h b/include/n2n.h index f41f2a4..f86805f 100644 --- a/include/n2n.h +++ b/include/n2n.h @@ -42,12 +42,12 @@ #include "config.h" /* Visual C++ */ /* Moved here to define _CRT_SECURE_NO_WARNINGS before all the including takes place */ -#ifdef WIN32 +#ifdef _WIN32 #define N2N_CAN_NAME_IFACE 1 #undef N2N_HAVE_DAEMON #undef N2N_HAVE_TCP /* as explained on https://github.com/ntop/n2n/pull/627#issuecomment-782093706 */ #undef N2N_HAVE_SETUID -#endif /* WIN32 */ +#endif /* _WIN32 */ #include @@ -55,7 +55,7 @@ #include "n2n_define.h" #include "n2n_typedefs.h" -#ifdef WIN32 +#ifdef _WIN32 #include /* for tcp */ #include /* for privilege check in tools/n2n-route */ #include /* for privilege check in tools/n2n-route */ @@ -63,9 +63,9 @@ #include /* for privilege check in tools/n2n-route */ #include "wintap.h" #define SHUT_RDWR SD_BOTH /* for tcp */ -#endif /* #ifdef WIN32 */ +#endif /* #ifdef _WIN32 */ -#ifndef WIN32 +#ifndef _WIN32 #include // for in_addr (ptr only), in_addr_t #include #include // for uint8_t, uint64_t, uint32_t, uint16_t @@ -89,7 +89,7 @@ #include #include #endif -#endif /* #ifndef WIN32 */ +#endif /* #ifndef _WIN32 */ @@ -129,7 +129,7 @@ void _traceEvent (int eventTraceLevel, char* file, int line, char * format, ...) /* Tuntap API */ int tuntap_open (struct tuntap_dev *device, char *dev, const char *address_mode, char *device_ip, char *device_mask, const char * device_mac, int mtu -#ifdef WIN32 +#ifdef _WIN32 , int metric #endif ); diff --git a/include/n2n_define.h b/include/n2n_define.h index 27113b9..12c5dc9 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -190,7 +190,7 @@ enum skip_add {SN_ADD = 0, SN_ADD_SKIP = 1, SN_ADD_ADDED = 2}; #define N2N_MULTICAST_PORT 1968 #define N2N_MULTICAST_GROUP "224.0.0.68" -#ifdef WIN32 +#ifdef _WIN32 #define N2N_IFNAMSIZ 64 #else #define N2N_IFNAMSIZ 16 /* 15 chars * NULL */ diff --git a/include/n2n_typedefs.h b/include/n2n_typedefs.h index 4951aa4..0b34f32 100644 --- a/include/n2n_typedefs.h +++ b/include/n2n_typedefs.h @@ -21,7 +21,7 @@ #include #include // for uint8_t and friends -#ifndef WIN32 +#ifndef _WIN32 #include // for in_addr_t #include // for sockaddr #endif @@ -84,7 +84,7 @@ typedef unsigned long in_addr_t; #endif #endif -#ifdef WIN32 +#ifdef _WIN32 #ifndef __LITTLE_ENDIAN__ #define __LITTLE_ENDIAN__ 1 #endif @@ -231,7 +231,7 @@ typedef char dec_ip_bit_str_t[N2N_NETMASK_STR_SIZE + 4]; typedef char devstr_t[N2N_IFNAMSIZ]; -#ifndef WIN32 +#ifndef _WIN32 typedef struct tuntap_dev { int fd; int if_idx; @@ -243,9 +243,9 @@ typedef struct tuntap_dev { } tuntap_dev; #define SOCKET int -#else /* #ifndef WIN32 */ +#else /* #ifndef _WIN32 */ typedef u_short sa_family_t; -#endif /* #ifndef WIN32 */ +#endif /* #ifndef _WIN32 */ typedef struct speck_context_t he_context_t; @@ -574,7 +574,7 @@ typedef struct n2n_tuntap_priv_config { int mtu; int metric; uint8_t daemon; -#ifndef WIN32 +#ifndef _WIN32 uid_t userid; gid_t groupid; #endif @@ -849,7 +849,7 @@ typedef struct n2n_sn { int mgmt_sock; /* management socket. */ n2n_ip_subnet_t min_auto_ip_net; /* Address range of auto_ip service. */ n2n_ip_subnet_t max_auto_ip_net; /* Address range of auto_ip service. */ -#ifndef WIN32 +#ifndef _WIN32 uid_t userid; gid_t groupid; #endif diff --git a/include/n2n_wire.h b/include/n2n_wire.h index 3e23132..0151bc9 100644 --- a/include/n2n_wire.h +++ b/include/n2n_wire.h @@ -25,12 +25,12 @@ #include #endif -#if defined(WIN32) +#ifdef _WIN32 #include "n2n_win32.h" -#else /* #if defined(WIN32) */ +#else /* #ifdef _WIN32 */ #include #include /* AF_INET and AF_INET6 */ -#endif /* #if defined(WIN32) */ +#endif /* #ifdef _WIN32 */ #include "sn_selection.h" diff --git a/include/random_numbers.h b/include/random_numbers.h index 2c7eba4..bd7c6ce 100644 --- a/include/random_numbers.h +++ b/include/random_numbers.h @@ -38,7 +38,7 @@ #include /* _rdrand64_step, rdseed4_step */ #endif -#if defined (WIN32) +#ifdef _WIN32 #include // HCTYPTPROV, Crypt*-functions #endif diff --git a/src/edge.c b/src/edge.c index 5e76ab5..ae9115b 100644 --- a/src/edge.c +++ b/src/edge.c @@ -42,7 +42,7 @@ #include "speck.h" // for speck_init, speck_context_t #include "uthash.h" // for UT_hash_handle, HASH_ADD, HASH_C... -#ifdef WIN32 +#ifdef _WIN32 #include #include #else @@ -230,12 +230,12 @@ static void help (int level) { "[-J ] " "[-P ] " "[-R ] " -#ifdef WIN32 +#ifdef _WIN32 "\n " "[-x ] " #endif "\n\n local options " -#ifndef WIN32 +#ifndef _WIN32 "[-f] " #endif "[-t ] " @@ -243,7 +243,7 @@ static void help (int level) { "\n " "[-v] " "[-V] " -#ifndef WIN32 +#ifndef _WIN32 "\n " "[-u ] " "[-g ] " @@ -266,7 +266,7 @@ static void help (int level) { "\n [-E] accept multicast MAC addresses" "\n [--select-rtt] select supernode by round trip time" "\n [--select-mac] select supernode by MAC address" -#ifndef WIN32 +#ifndef _WIN32 "\n [-f] do not fork but run in foreground" #endif "\n [-v] make more verbose, repeat as required" @@ -351,14 +351,14 @@ static void help (int level) { printf(" | rule format: 'src_ip/n:[s_port,e_port],...\n" " | |on same| ...dst_ip/n:[s_port,e_port],...\n" " | | line | ...TCP+/-,UDP+/-,ICMP+/-'\n"); -#ifdef WIN32 +#ifdef _WIN32 printf(" -x | set TAP interface metric, defaults to 0 (auto),\n" " | e.g. set to 1 for better multiplayer game detection\n"); #endif printf ("\n"); printf (" LOCAL OPTIONS\n"); printf (" -------------\n\n"); -#ifndef WIN32 +#ifndef _WIN32 printf(" -f | do not fork and run as a daemon, rather run in foreground\n"); #endif printf(" -t | management UDP port, for multiple edges on a machine,\n" @@ -367,7 +367,7 @@ static void help (int level) { " ...password | \n", N2N_MGMT_PASSWORD); printf(" -v | make more verbose, repeat as required\n"); printf(" -V | make less verbose, repeat as required\n"); -#ifndef WIN32 +#ifndef _WIN32 printf(" -u | numeric user ID to use when privileges are dropped\n"); printf(" -g | numeric group ID to use when privileges are dropped\n"); #endif @@ -378,7 +378,7 @@ static void help (int level) { printf(" N2N_COMMUNITY | community name (ASCII), overwritten by '-c ...'\n"); printf(" N2N_PASSWORD | password (ASCII) for user-password authentication,\n" " | overwritten by '-J ...'\n"); -#ifdef WIN32 +#ifdef _WIN32 printf ("\n"); printf (" AVAILABLE TAP ADAPTERS\n"); printf (" ----------------------\n\n"); @@ -490,7 +490,7 @@ static int setOption (int optkey, char *optargument, n2n_tuntap_priv_config_t *e break; } -#ifndef WIN32 +#ifndef _WIN32 case 'u': /* unprivileged uid */ { ec->userid = atoi(optargument); break; @@ -502,12 +502,12 @@ static int setOption (int optkey, char *optargument, n2n_tuntap_priv_config_t *e } #endif -#ifndef WIN32 +#ifndef _WIN32 case 'f' : /* do not fork as daemon */ { ec->daemon = 0; break; } -#endif /* #ifndef WIN32 */ +#endif /* #ifndef _WIN32 */ case 'm' : /* TUNTAP MAC address */ { strncpy(ec->device_mac, optargument, N2N_MACNAMSIZ); @@ -785,7 +785,7 @@ static int setOption (int optkey, char *optargument, n2n_tuntap_priv_config_t *e } break; } -#ifdef WIN32 +#ifdef _WIN32 case 'x': { conf->metric = atoi(optargument); ec->metric = atoi(optargument); @@ -831,7 +831,7 @@ static int loadFromCLI (int argc, char *argv[], n2n_edge_conf_t *conf, n2n_tunta #ifdef __linux__ "T:" #endif -#ifdef WIN32 +#ifdef _WIN32 "x:" #endif , @@ -912,7 +912,7 @@ static int loadFromFile (const char *path, n2n_edge_conf_t *conf, n2n_tuntap_pri /* ************************************** */ -#ifndef WIN32 +#ifndef _WIN32 static void daemonize () { int childpid; @@ -961,8 +961,8 @@ static void daemonize () { static bool keep_on_running = true; -#if defined(__linux__) || defined(WIN32) -#ifdef WIN32 +#if defined(__linux__) || defined(_WIN32) +#ifdef _WIN32 BOOL WINAPI term_handler(DWORD sig) #else static void term_handler(int sig) @@ -979,7 +979,7 @@ BOOL WINAPI term_handler(DWORD sig) } keep_on_running = false; -#ifdef WIN32 +#ifdef _WIN32 switch (sig) { case CTRL_CLOSE_EVENT: case CTRL_LOGOFF_EVENT: @@ -990,7 +990,7 @@ BOOL WINAPI term_handler(DWORD sig) return(TRUE); #endif } -#endif /* defined(__linux__) || defined(WIN32) */ +#endif /* defined(__linux__) || defined(_WIN32) */ /* *************************************************** */ @@ -1015,13 +1015,13 @@ int main (int argc, char* argv[]) { uint8_t pktbuf[N2N_SN_PKTBUF_SIZE + sizeof(uint16_t)]; /* buffer + prepended buffer length in case of tcp */ -#ifndef WIN32 +#ifndef _WIN32 struct passwd *pw = NULL; #endif #ifdef HAVE_LIBCAP cap_t caps; #endif -#ifdef WIN32 +#ifdef _WIN32 initWin32(); #endif @@ -1031,7 +1031,7 @@ int main (int argc, char* argv[]) { ec.mtu = DEFAULT_MTU; ec.daemon = 1; /* By default run in daemon mode. */ -#ifndef WIN32 +#ifndef _WIN32 if(((pw = getpwnam("n2n")) != NULL) || ((pw = getpwnam("nobody")) != NULL)) { ec.userid = pw->pw_uid; @@ -1039,7 +1039,7 @@ int main (int argc, char* argv[]) { } #endif -#ifdef WIN32 +#ifdef _WIN32 ec.tuntap_dev_name[0] = '\0'; ec.metric = 0; #else @@ -1055,7 +1055,7 @@ int main (int argc, char* argv[]) { rc = loadFromCLI(argc, argv, &conf, &ec); else -#ifdef WIN32 +#ifdef _WIN32 // load from current directory rc = loadFromFile("edge.conf", &conf, &ec); #else @@ -1119,7 +1119,7 @@ int main (int argc, char* argv[]) { /* Random seed */ n2n_srand (n2n_seed()); -#ifndef WIN32 +#ifndef _WIN32 /* If running suid root then we need to setuid before using the force. */ if(setuid(0) != 0) traceEvent(TRACE_ERROR, "unable to become root [%u/%s]", errno, strerror(errno)); @@ -1247,7 +1247,7 @@ int main (int argc, char* argv[]) { if(tuntap_open(&tuntap, eee->tuntap_priv_conf.tuntap_dev_name, eee->tuntap_priv_conf.ip_mode, eee->tuntap_priv_conf.ip_addr, eee->tuntap_priv_conf.netmask, eee->tuntap_priv_conf.device_mac, eee->tuntap_priv_conf.mtu -#ifdef WIN32 +#ifdef _WIN32 , eee->tuntap_priv_conf.metric #endif ) < 0) @@ -1298,7 +1298,7 @@ int main (int argc, char* argv[]) { eee->sn_wait = 1; eee->last_register_req = 0; -#ifndef WIN32 +#ifndef _WIN32 if(eee->tuntap_priv_conf.daemon) { setUseSyslog(1); /* traceEvent output now goes to syslog. */ daemonize(); @@ -1340,7 +1340,7 @@ int main (int argc, char* argv[]) { signal(SIGTERM, term_handler); signal(SIGINT, term_handler); #endif -#ifdef WIN32 +#ifdef _WIN32 SetConsoleCtrlHandler(term_handler, TRUE); #endif @@ -1365,7 +1365,7 @@ int main (int argc, char* argv[]) { tuntap_close(&eee->device); edge_term(eee); -#ifdef WIN32 +#ifdef _WIN32 destroyWin32(); #endif diff --git a/src/edge_management.c b/src/edge_management.c index ebf2cfa..4884525 100644 --- a/src/edge_management.c +++ b/src/edge_management.c @@ -35,7 +35,7 @@ #include "strbuf.h" // for strbuf_t, STRBUF_INIT #include "uthash.h" // for UT_hash_handle, HASH_ITER -#ifdef WIN32 +#ifdef _WIN32 #include #include #else diff --git a/src/edge_utils.c b/src/edge_utils.c index f256272..c10711e 100644 --- a/src/edge_utils.c +++ b/src/edge_utils.c @@ -40,7 +40,7 @@ #include "speck.h" // for speck_128_decrypt, speck_128_enc... #include "uthash.h" // for UT_hash_handle, HASH_COUNT, HASH... -#ifdef WIN32 +#ifdef _WIN32 #include #include #include "edge_utils_win32.h" @@ -307,7 +307,7 @@ int supernode_connect (n2n_edge_t *eee) { // set tcp socket to O_NONBLOCK so connect does not hang // requires checking the socket for readiness before sending and receving if(eee->conf.connect_tcp) { -#ifdef WIN32 +#ifdef _WIN32 u_long value = 1; ioctlsocket(eee->sock, FIONBIO, &value); #else @@ -705,7 +705,7 @@ static void register_with_new_peer (n2n_edge_t *eee, */ if(eee->conf.register_ttl == 1) { /* We are DMZ host or port is directly accessible. Just let peer to send back the ack */ -#ifndef WIN32 +#ifndef _WIN32 } else if(eee->conf.register_ttl > 1) { /* Setting register_ttl usually implies that the edge knows the internal net topology * clearly, we can apply aggressive port prediction to support incoming Symmetric NAT @@ -1086,7 +1086,7 @@ static ssize_t sendto_fd (n2n_edge_t *eee, const void *buf, traceEvent(level, "sendto(%s) failed (%d) %s", sock_to_cstr(sockbuf, n2ndest), errno, errstr); -#ifdef WIN32 +#ifdef _WIN32 traceEvent(level, "WSAGetLastError(): %u", WSAGetLastError()); #endif @@ -1182,7 +1182,7 @@ static void check_join_multicast_group (n2n_edge_t *eee) { if(!eee->multicast_joined) { struct ip_mreq mreq; mreq.imr_multiaddr.s_addr = inet_addr(N2N_MULTICAST_GROUP); -#ifdef WIN32 +#ifdef _WIN32 dec_ip_str_t ip_addr; get_best_interface_ip(eee, &ip_addr); mreq.imr_interface.s_addr = inet_addr(ip_addr); @@ -1194,7 +1194,7 @@ static void check_join_multicast_group (n2n_edge_t *eee) { traceEvent(TRACE_WARNING, "failed to bind to local multicast group %s:%u [errno %u]", N2N_MULTICAST_GROUP, N2N_MULTICAST_PORT, errno); -#ifdef WIN32 +#ifdef _WIN32 traceEvent(TRACE_WARNING, "WSAGetLastError(): %u", WSAGetLastError()); #endif } else { @@ -1851,7 +1851,7 @@ static int handle_PACKET (n2n_edge_t * eee, #if 0 -#ifndef WIN32 +#ifndef _WIN32 static char *get_ip_from_arp (dec_ip_str_t buf, const n2n_mac_t req_mac) { @@ -2183,7 +2183,7 @@ void edge_read_from_tap (n2n_edge_t * eee) { tuntap_close(&(eee->device)); tuntap_open(&(eee->device), eee->tuntap_priv_conf.tuntap_dev_name, eee->tuntap_priv_conf.ip_mode, eee->tuntap_priv_conf.ip_addr, eee->tuntap_priv_conf.netmask, eee->tuntap_priv_conf.device_mac, eee->tuntap_priv_conf.mtu -#ifdef WIN32 +#ifdef _WIN32 ,eee->tuntap_priv_conf.metric #endif ); @@ -2795,14 +2795,14 @@ int fetch_and_eventually_process_data (n2n_edge_t *eee, SOCKET sock, sender_sock, &ss_size); if((bread < 0) -#ifdef WIN32 +#ifdef _WIN32 && (WSAGetLastError() != WSAECONNRESET) #endif ) { /* For UDP bread of zero just means no data (unlike TCP). */ /* The fd is no good now. Maybe we lost our interface. */ traceEvent(TRACE_ERROR, "recvfrom() failed %d errno %d (%s)", bread, errno, strerror(errno)); -#ifdef WIN32 +#ifdef _WIN32 traceEvent(TRACE_ERROR, "WSAGetLastError(): %u", WSAGetLastError()); #endif return -1; @@ -2821,7 +2821,7 @@ int fetch_and_eventually_process_data (n2n_edge_t *eee, SOCKET sock, sender_sock, &ss_size); if((bread <= 0) && (errno)) { traceEvent(TRACE_ERROR, "recvfrom() failed %d errno %d (%s)", bread, errno, strerror(errno)); -#ifdef WIN32 +#ifdef _WIN32 traceEvent(TRACE_ERROR, "WSAGetLastError(): %u", WSAGetLastError()); #endif supernode_disconnect(eee); @@ -2889,7 +2889,7 @@ int run_edge_loop (n2n_edge_t *eee) { uint16_t position = 0; uint8_t pktbuf[N2N_PKT_BUF_SIZE + sizeof(uint16_t)]; /* buffer + prepended buffer length in case of tcp */ -#ifdef WIN32 +#ifdef _WIN32 struct tunread_arg arg; arg.eee = eee; HANDLE tun_read_thread = startTunReadThread(&arg); @@ -2929,7 +2929,7 @@ int run_edge_loop (n2n_edge_t *eee) { } #endif -#ifndef WIN32 +#ifndef _WIN32 FD_SET(eee->device.fd, &socket_mask); max_sock = max(max_sock, eee->device.fd); #endif @@ -2989,7 +2989,7 @@ int run_edge_loop (n2n_edge_t *eee) { break; } -#ifndef WIN32 +#ifndef _WIN32 if(FD_ISSET(eee->device.fd, &socket_mask)) { // read an ethernet frame from the TAP socket; write on the IP socket edge_read_from_tap(eee); @@ -3055,7 +3055,7 @@ int run_edge_loop (n2n_edge_t *eee) { send_unregister_super(eee); -#ifdef WIN32 +#ifdef _WIN32 WaitForSingleObject(tun_read_thread, INFINITE); #endif @@ -3296,7 +3296,7 @@ int quick_edge_init (char *device_name, char *community_name, if(tuntap_open(&tuntap, device_name, "static", local_ip_address, "255.255.255.0", device_mac, DEFAULT_MTU -#ifdef WIN32 +#ifdef _WIN32 , 0 #endif ) < 0) diff --git a/src/edge_utils_win32.c b/src/edge_utils_win32.c index b65c44c..5f5ea82 100644 --- a/src/edge_utils_win32.c +++ b/src/edge_utils_win32.c @@ -16,7 +16,7 @@ * */ -#ifdef WIN32 +#ifdef _WIN32 #include diff --git a/src/example_edge_embed.c b/src/example_edge_embed.c index 2b32269..c9d2919 100644 --- a/src/example_edge_embed.c +++ b/src/example_edge_embed.c @@ -59,7 +59,7 @@ int main() { "255.255.255.0", // Netmask to use "DE:AD:BE:EF:01:10", // Set mac address DEFAULT_MTU // MTU to use -#ifdef WIN32 +#ifdef _WIN32 , 0 #endif ) < 0) diff --git a/src/example_sn_embed.c b/src/example_sn_embed.c index cdd16ed..7050e40 100644 --- a/src/example_sn_embed.c +++ b/src/example_sn_embed.c @@ -21,7 +21,7 @@ #include // for exit #include "n2n.h" // for n2n_sn_t, open_socket, run_sn_loop, sn_init -#ifdef WIN32 +#ifdef _WIN32 #include #else #include // for INADDR_ANY, INADDR_LOOPBACK diff --git a/src/management.c b/src/management.c index 6f914b5..0aa73b3 100644 --- a/src/management.c +++ b/src/management.c @@ -12,7 +12,7 @@ #include "management.h" #include "n2n.h" // for TRACE_DEBUG, traceEvent -#ifndef WIN32 +#ifndef _WIN32 #include // for getnameinfo, NI_NUMERICHOST, NI_NUMERICSERV #include // for sendto, sockaddr #endif diff --git a/src/management.h b/src/management.h index 53592bd..3377a98 100644 --- a/src/management.h +++ b/src/management.h @@ -16,7 +16,7 @@ #include "n2n_define.h" // for n2n_event_topic #include "strbuf.h" -#ifdef WIN32 +#ifdef _WIN32 #include #else #include // for sockaddr, sockaddr_storage, socklen_t diff --git a/src/n2n.c b/src/n2n.c index 026a120..bf9df3c 100644 --- a/src/n2n.c +++ b/src/n2n.c @@ -34,7 +34,7 @@ #include #endif -#ifdef WIN32 +#ifdef _WIN32 #include #include #include @@ -60,7 +60,7 @@ SOCKET open_socket (int local_port, in_addr_t address, int type /* 0 = UDP, TCP return(-1); } -#ifndef WIN32 +#ifndef _WIN32 /* fcntl(sock_fd, F_SETFL, O_NONBLOCK); */ #endif @@ -83,7 +83,7 @@ SOCKET open_socket (int local_port, in_addr_t address, int type /* 0 = UDP, TCP static int traceLevel = 2 /* NORMAL */; static int useSyslog = 0; -#ifndef WIN32 +#ifndef _WIN32 static int syslog_opened = 0; #endif static FILE *traceFile = NULL; @@ -113,7 +113,7 @@ void closeTraceFile () { if((traceFile != NULL) && (traceFile != stdout)) { fclose(traceFile); } -#ifndef WIN32 +#ifndef _WIN32 if(useSyslog && syslog_opened) { closelog(); syslog_opened = 0; @@ -161,7 +161,7 @@ void _traceEvent (int eventTraceLevel, char* file, int line, char * format, ...) buf[strlen(buf) - 1] = '\0'; } -#ifndef WIN32 +#ifndef _WIN32 if(useSyslog) { if(!syslog_opened) { openlog("n2n", LOG_PID, LOG_DAEMON); @@ -181,7 +181,7 @@ void _traceEvent (int eventTraceLevel, char* file, int line, char * format, ...) snprintf(out_buf, sizeof(out_buf), "%s [%s:%d] %s%s", theDate, &file[i], line, extra_msg, buf); fprintf(traceFile, "%s\n", out_buf); fflush(traceFile); -#ifndef WIN32 +#ifndef _WIN32 } #endif } @@ -844,7 +844,7 @@ int memxor (uint8_t *destination, const uint8_t *source, size_t len) { /* *********************************************** */ -#if defined(WIN32) +#ifdef _WIN32 int gettimeofday (struct timeval *tp, void *tzp) { time_t clock; diff --git a/src/network_traffic_filter.c b/src/network_traffic_filter.c index 4ba0d0b..ec840a0 100644 --- a/src/network_traffic_filter.c +++ b/src/network_traffic_filter.c @@ -25,7 +25,7 @@ #include "network_traffic_filter.h" // for create_network_traffic_filter #include "uthash.h" // for UT_hash_handle, HASH_ITER, HASH_DEL -#ifdef WIN32 +#ifdef _WIN32 #include #include #else diff --git a/src/random_numbers.c b/src/random_numbers.c index ed6ca8c..67a1b98 100644 --- a/src/random_numbers.c +++ b/src/random_numbers.c @@ -178,7 +178,7 @@ uint64_t n2n_seed (void) { #endif #endif -#ifdef WIN32 +#ifdef _WIN32 HCRYPTPROV crypto_provider; CryptAcquireContext (&crypto_provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); diff --git a/src/sn_management.c b/src/sn_management.c index 2ef8f41..7b3abe4 100644 --- a/src/sn_management.c +++ b/src/sn_management.c @@ -35,7 +35,7 @@ #include "strbuf.h" // for strbuf_t, STRBUF_INIT #include "uthash.h" // for UT_hash_handle, HASH_ITER, HASH_COUNT -#ifdef WIN32 +#ifdef _WIN32 #include #else #include // for sendto, socklen_t diff --git a/src/sn_utils.c b/src/sn_utils.c index 7f8e806..f6092fe 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -40,7 +40,7 @@ #include "speck.h" // for speck_128_encrypt, speck_context_t #include "uthash.h" // for UT_hash_handle, HASH_ITER, HASH_DEL -#ifdef WIN32 +#ifdef _WIN32 #include #include #else @@ -512,7 +512,7 @@ static ssize_t sendto_fd (n2n_sn_t *sss, if((sent <= 0) && (errno)) { char * c = strerror(errno); traceEvent(TRACE_ERROR, "sendto failed (%d) %s", errno, c); -#ifdef WIN32 +#ifdef _WIN32 traceEvent(TRACE_ERROR, "WSAGetLastError(): %u", WSAGetLastError()); #endif // if the erroneous connection is tcp, i.e. not the regular sock... @@ -768,7 +768,7 @@ int sn_init_defaults (n2n_sn_t *sss) { char *tmp_string; -#ifdef WIN32 +#ifdef _WIN32 initWin32(); #endif @@ -904,7 +904,7 @@ void sn_term (n2n_sn_t *sss) { if(sss->community_file) free(sss->community_file); -#ifdef WIN32 +#ifdef _WIN32 destroyWin32(); #endif } @@ -2642,14 +2642,14 @@ int run_sn_loop (n2n_sn_t *sss) { sender_sock, &ss_size); if((bread < 0) -#ifdef WIN32 +#ifdef _WIN32 && (WSAGetLastError() != WSAECONNRESET) #endif ) { /* For UDP bread of zero just means no data (unlike TCP). */ /* The fd is no good now. Maybe we lost our interface. */ traceEvent(TRACE_ERROR, "recvfrom() failed %d errno %d (%s)", bread, errno, strerror(errno)); -#ifdef WIN32 +#ifdef _WIN32 traceEvent(TRACE_ERROR, "WSAGetLastError(): %u", WSAGetLastError()); #endif *sss->keep_running = false; @@ -2688,7 +2688,7 @@ int run_sn_loop (n2n_sn_t *sss) { if(bread <= 0) { traceEvent(TRACE_INFO, "closing tcp connection to [%s]", sock_to_cstr(sockbuf, (n2n_sock_t*)sender_sock)); traceEvent(TRACE_DEBUG, "recvfrom() returns %d and sees errno %d (%s)", bread, errno, strerror(errno)); -#ifdef WIN32 +#ifdef _WIN32 traceEvent(TRACE_DEBUG, "WSAGetLastError(): %u", WSAGetLastError()); #endif close_tcp_connection(sss, conn); diff --git a/src/supernode.c b/src/supernode.c index 2f60416..8d39a4c 100644 --- a/src/supernode.c +++ b/src/supernode.c @@ -35,7 +35,7 @@ #include "pearson.h" // for pearson_hash_64 #include "uthash.h" // for UT_hash_handle, HASH_ITER, HASH_ADD_STR -#ifdef WIN32 +#ifdef _WIN32 #include #include #else @@ -107,7 +107,7 @@ static void help (int level) { "\n " "[--management-password ] " "[-v] " -#ifndef WIN32 +#ifndef _WIN32 "\n " "[-u ]" "[-g ]" @@ -168,7 +168,7 @@ static void help (int level) { printf(" --management_... | management port password, defaults to '%s'\n" " ...password | \n", N2N_MGMT_PASSWORD); printf(" -v | make more verbose, repeat as required\n"); -#ifndef WIN32 +#ifndef _WIN32 printf(" -u | numeric user ID to use when privileges are dropped\n"); printf(" -g | numeric group ID to use when privileges are dropped\n"); #endif @@ -324,7 +324,7 @@ static int setOption (int optkey, char *_optarg, n2n_sn_t *sss) { break; } -#ifndef WIN32 +#ifndef _WIN32 case 'u': /* unprivileged uid */ sss->userid = atoi(_optarg); break; @@ -425,7 +425,7 @@ static int loadFromCLI (int argc, char * const argv[], n2n_sn_t *sss) { #if defined(N2N_HAVE_DAEMON) "f" #endif -#ifndef WIN32 +#ifndef _WIN32 "u:g:" #endif , @@ -567,8 +567,8 @@ static void dump_registrations (int signo) { static bool keep_running = true; -#if defined(__linux__) || defined(WIN32) -#ifdef WIN32 +#if defined(__linux__) || defined(_WIN32) +#ifdef _WIN32 BOOL WINAPI term_handler (DWORD sig) #else static void term_handler(int sig) @@ -585,11 +585,11 @@ BOOL WINAPI term_handler (DWORD sig) } keep_running = false; -#ifdef WIN32 +#ifdef _WIN32 return(TRUE); #endif } -#endif /* defined(__linux__) || defined(WIN32) */ +#endif /* defined(__linux__) || defined(_WIN32) */ /* *************************************************** */ @@ -597,7 +597,7 @@ BOOL WINAPI term_handler (DWORD sig) int main (int argc, char * const argv[]) { int rc; -#ifndef WIN32 +#ifndef _WIN32 struct passwd *pw = NULL; #endif struct peer_info *scan, *tmp; @@ -615,7 +615,7 @@ int main (int argc, char * const argv[]) { rc = loadFromCLI(argc, argv, &sss_node); } else -#ifdef WIN32 +#ifdef _WIN32 // load from current directory rc = loadFromFile("supernode.conf", &sss_node); #else @@ -689,7 +689,7 @@ int main (int argc, char * const argv[]) { HASH_ITER(hh, sss_node.federation->edges, scan, tmp) scan->socket_fd = sss_node.sock; -#ifndef WIN32 +#ifndef _WIN32 /* * If no uid/gid is specified on the commandline, use the uid/gid of the * first found out of user "n2n" or "nobody" @@ -734,7 +734,7 @@ int main (int argc, char * const argv[]) { signal(SIGINT, term_handler); signal(SIGHUP, dump_registrations); #endif -#ifdef WIN32 +#ifdef _WIN32 SetConsoleCtrlHandler(term_handler, TRUE); #endif diff --git a/src/wire.c b/src/wire.c index 485020f..995efe8 100644 --- a/src/wire.c +++ b/src/wire.c @@ -33,7 +33,7 @@ #include "n2n.h" // for n2n_sock_t, n2n_common_t, n2n_auth_t, n2n_RE... #include "n2n_wire.h" // for decode_PACKET, decode_PEER_INFO, decode_QUER... -#ifdef WIN32 +#ifdef _WIN32 #include #include #else diff --git a/tools/n2n-decode.c b/tools/n2n-decode.c index cf5844b..c11211c 100644 --- a/tools/n2n-decode.c +++ b/tools/n2n-decode.c @@ -58,7 +58,7 @@ static void help() { /* *************************************************** */ -#ifdef WIN32 +#ifdef _WIN32 BOOL WINAPI term_handler(DWORD sig) #else static void term_handler(int sig) @@ -75,7 +75,7 @@ static void term_handler(int sig) } running = 0; -#ifdef WIN32 +#ifdef _WIN32 return(TRUE); #endif } @@ -349,7 +349,7 @@ int main(int argc, char* argv[]) { return(5); } -#ifdef WIN32 +#ifdef _WIN32 SetConsoleCtrlHandler(term_handler, TRUE); #else signal(SIGTERM, term_handler); diff --git a/tools/n2n-portfwd.c b/tools/n2n-portfwd.c index 1b6a7e9..dce283a 100644 --- a/tools/n2n-portfwd.c +++ b/tools/n2n-portfwd.c @@ -33,7 +33,7 @@ #include "n2n_port_mapping.h" // for n2n_del_port_mapping, n2n_set_port_map... #include "random_numbers.h" // for n2n_rand, n2n_seed, n2n_srand -#ifdef WIN32 +#ifdef _WIN32 #include #include #else @@ -49,7 +49,7 @@ #define INFO_INTERVAL 5 // REVISIT: may become obsolete -#ifdef WIN32 +#ifdef _WIN32 #ifndef STDIN_FILENO #define STDIN_FILENO _fileno(stdin) #endif @@ -75,13 +75,13 @@ void set_term_handler(const void *handler) { signal(SIGTERM, handler); signal(SIGINT, handler); #endif -#ifdef WIN32 +#ifdef _WIN32 SetConsoleCtrlHandler(handler, TRUE); #endif } -#ifdef WIN32 +#ifdef _WIN32 BOOL WINAPI term_handler (DWORD sig) { #else static void term_handler (int sig) { @@ -98,7 +98,7 @@ static void term_handler (int sig) { } keep_running = false; -#ifdef WIN32 +#ifdef _WIN32 return TRUE; #endif } @@ -113,7 +113,7 @@ SOCKET connect_to_management_port (n2n_portfwd_conf_t *ppp) { SOCKET ret; struct sockaddr_in sock_addr; -#if defined(WIN32) +#ifdef _WIN32 // Windows requires a call to WSAStartup() before it can work with sockets WORD wVersionRequested; WSADATA wsaData; @@ -184,7 +184,7 @@ int get_port_from_json (uint16_t *port, json_object_t *json, char *key, int tag, // PLATFORM-DEPENDANT CODE -#if !defined(WIN32) +#ifndef _WIN32 // taken from https://web.archive.org/web/20170407122137/http://cc.byexamples.com/2007/04/08/non-blocking-user-input-in-loop-without-ncurses/ int _kbhit () { diff --git a/tools/n2n-route.c b/tools/n2n-route.c index aeaa49d..fa63fa2 100644 --- a/tools/n2n-route.c +++ b/tools/n2n-route.c @@ -38,7 +38,7 @@ #include // for RTA_DATA, rtmsg, RTA_GATEWAY, RTA_NEXT #endif -#ifdef WIN32 +#ifdef _WIN32 #include #include #else @@ -51,7 +51,7 @@ #include // for send, socket, AF_INET, recv, connect #endif -#if defined (__linux__) || defined(WIN32) /* currently, Linux and Windows only */ +#if defined (__linux__) || defined(_WIN32) /* currently, Linux and Windows only */ #define WITH_ADDRESS 1 @@ -74,7 +74,7 @@ #define AUTO_DETECT 1 // REVISIT: may become obsolete -#ifdef WIN32 +#ifdef _WIN32 #ifndef STDIN_FILENO #define STDIN_FILENO _fileno(stdin) #endif @@ -117,7 +117,7 @@ int is_privileged (void) { return euid == 0; -#elif defined(WIN32) +#elif defined(_WIN32) // taken from https://stackoverflow.com/a/10553065 int result; DWORD rc; @@ -148,13 +148,13 @@ void set_term_handler(const void *handler) { signal(SIGPIPE, SIG_IGN); signal(SIGTERM, handler); signal(SIGINT, handler); -#elif defined(WIN32) +#elif defined(_WIN32) SetConsoleCtrlHandler(handler, TRUE); #endif } -#if !defined(WIN32) +#ifndef _WIN32 static void term_handler (int sig) { #else BOOL WINAPI term_handler (DWORD sig) { @@ -171,7 +171,7 @@ BOOL WINAPI term_handler (DWORD sig) { } keep_running = false; -#if defined(WIN32) +#ifdef _WIN32 return TRUE; #endif } @@ -317,7 +317,7 @@ find_default_gateway_end: closesocket(sock); return ret; -#elif defined(WIN32) +#elif defined(_WIN32) // taken from (and modified) // https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-createipforwardentry @@ -376,7 +376,7 @@ find_default_gateway_end: // PLATFORM-DEPENDANT CODE -#if defined(WIN32) +#ifdef _WIN32 DWORD get_interface_index (struct in_addr addr) { // taken from (and modified) // https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-createipforwardentry @@ -496,7 +496,7 @@ void handle_route (n2n_route_t* in_route, int verb) { closesocket(sock); -#elif defined(WIN32) +#elif defined(_WIN32) // REVISIT: use 'CreateIpForwardEntry()' and 'DeleteIpForwardEntry()' [iphlpapi.h] char c_net_addr[32]; char c_gateway[32]; @@ -573,7 +573,7 @@ SOCKET connect_to_management_port (n2n_route_conf_t *rrr) { SOCKET ret; struct sockaddr_in sock_addr; -#if defined(WIN32) +#ifdef _WIN32 // Windows requires a call to WSAStartup() before it can work with sockets WORD wVersionRequested; WSADATA wsaData; @@ -643,7 +643,7 @@ int get_addr_from_json (struct in_addr *addr, json_object_t *json, char *key, in // PLATFORM-DEPENDANT CODE -#if !defined(WIN32) +#ifndef _WIN32 // taken from https://web.archive.org/web/20170407122137/http://cc.byexamples.com/2007/04/08/non-blocking-user-input-in-loop-without-ncurses/ int _kbhit () { @@ -1088,7 +1088,7 @@ end_route_tool: } -#else /* if defined(__linux__) || defined(WIN32) -- currently, Linux and Windows only */ +#else /* if defined(__linux__) || defined(_WIN32) -- currently, Linux and Windows only */ int main (int argc, char* argv[]) { @@ -1100,4 +1100,4 @@ int main (int argc, char* argv[]) { } -#endif /* if defined (__linux__) || defined(WIN32) -- currently, Linux and Windows only */ +#endif /* if defined (__linux__) || defined(_WIN32) -- currently, Linux and Windows only */ diff --git a/win32/getopt.c b/win32/getopt.c index f34b53a..3e51545 100644 --- a/win32/getopt.c +++ b/win32/getopt.c @@ -58,7 +58,7 @@ #endif #include -#ifdef WIN32 +#ifdef _WIN32 #include #endif @@ -171,7 +171,7 @@ static char *nextchar; for unrecognized options. */ #ifndef DARWIN -#ifdef WIN32 +#ifdef _WIN32 int opterr = 0; #else int opterr = 1; @@ -231,7 +231,7 @@ static char *posixly_correct; # define my_index strchr #else -#ifndef WIN32 +#ifndef _WIN32 # if HAVE_STRING_H # include # else