From 08faca03b76ea5d6f6064ec8ba7b1c8ebe2e3989 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Thu, 10 Apr 2014 17:59:08 +0200 Subject: [PATCH] ntp: close client socket when offline --- ntp_core.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ntp_core.c b/ntp_core.c index 8933c49..4cb176f 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -263,6 +263,10 @@ start_initial_timeout(NCR_Instance inst) /* Mark source active */ SRC_SetActive(inst->source); + + /* Open client socket */ + if (inst->mode == MODE_CLIENT) + inst->local_addr.sock_fd = NIO_GetClientSocket(&inst->remote_addr); } /* ================================================== */ @@ -281,6 +285,10 @@ take_offline(NCR_Instance inst) /* And inactive */ SRC_UnsetActive(inst->source); + + /* Close client socket */ + if (inst->mode == MODE_CLIENT) + NIO_CloseClientSocket(inst->local_addr.sock_fd); } /* ================================================== */ @@ -297,7 +305,7 @@ NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourcePar switch (type) { case NTP_SERVER: - result->local_addr.sock_fd = NIO_GetClientSocket(remote_addr); + /* Client socket will be obtained when timer is started */ result->mode = MODE_CLIENT; break; case NTP_PEER: @@ -384,20 +392,14 @@ NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourcePar void NCR_DestroyInstance(NCR_Instance instance) { + if (instance->opmode != MD_OFFLINE) + take_offline(instance); + /* This will destroy the source instance inside the structure, which will cause reselection if this was the synchronising source etc. */ SRC_DestroyInstance(instance->source); - /* Cancel any pending timeouts */ - if (instance->timer_running) { - SCH_RemoveTimeout(instance->timeout_id); - instance->timer_running = 0; - } - - if (instance->mode == MODE_CLIENT) - NIO_CloseClientSocket(instance->local_addr.sock_fd); - /* Free the data structure */ Free(instance); }