From 004986310d2a744f354f83489c3d215004f4f26a Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 11 Sep 2019 18:35:29 +0200 Subject: [PATCH] ntp: skip loop test if no server socket is open If there is no socket that could receive a request from a client or peer, we know that nothing can be synchronized to us and no loop is possible. --- ntp_core.c | 4 ++-- ntp_io.c | 8 ++++++++ ntp_io.h | 3 +++ test/unit/ntp_core.c | 1 + 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ntp_core.c b/ntp_core.c index 7266e2e..4eb3674 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -1474,9 +1474,9 @@ check_sync_loop(NCR_Instance inst, NTP_Packet *message, NTP_Local_Address *local NTP_Leap leap_status; uint32_t our_ref_id; - /* Check if our server may be enabled, i.e. a client or peer can actually + /* Check if a server socket is open, i.e. a client or peer can actually be synchronised to us */ - if (REF_GetMode() != REF_ModeNormal) + if (!NIO_IsServerSocketOpen()) return 1; /* Check if the source indicates that it is synchronised to our address diff --git a/ntp_io.c b/ntp_io.c index f947237..4d9699d 100644 --- a/ntp_io.c +++ b/ntp_io.c @@ -329,6 +329,14 @@ NIO_IsServerSocket(int sock_fd) /* ================================================== */ +int +NIO_IsServerSocketOpen(void) +{ + return server_sock_fd4 != INVALID_SOCK_FD || server_sock_fd6 != INVALID_SOCK_FD; +} + +/* ================================================== */ + int NIO_IsServerConnectable(NTP_Remote_Address *remote_addr) { diff --git a/ntp_io.h b/ntp_io.h index 628f736..d52d56f 100644 --- a/ntp_io.h +++ b/ntp_io.h @@ -53,6 +53,9 @@ extern void NIO_CloseServerSocket(int sock_fd); /* Function to check if socket is a server socket */ extern int NIO_IsServerSocket(int sock_fd); +/* Function to check if a server socket is currently open */ +extern int NIO_IsServerSocketOpen(void); + /* Function to check if client packets can be sent to a server */ extern int NIO_IsServerConnectable(NTP_Remote_Address *remote_addr); diff --git a/test/unit/ntp_core.c b/test/unit/ntp_core.c index 18992ac..9cfff5f 100644 --- a/test/unit/ntp_core.c +++ b/test/unit/ntp_core.c @@ -39,6 +39,7 @@ static int req_length, res_length; #define NIO_OpenClientSocket(addr) ((addr)->ip_addr.family != IPADDR_UNSPEC ? 101 : 0) #define NIO_CloseClientSocket(fd) assert(fd == 101) #define NIO_IsServerSocket(fd) (fd == 100) +#define NIO_IsServerSocketOpen() 1 #define NIO_SendPacket(msg, to, from, len, process_tx) (memcpy(&req_buffer, msg, len), req_length = len, 1) #define SCH_AddTimeoutByDelay(delay, handler, arg) (1 ? 102 : (handler(arg), 1)) #define SCH_AddTimeoutInClass(delay, separation, randomness, class, handler, arg) \