From fcc75e49e1b50dc99b17200da99bb758384b3fde Mon Sep 17 00:00:00 2001 From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Sun, 2 May 2021 14:40:39 +0100 Subject: [PATCH] Include the IP or the master in the shm -- the user might wish to check that it's in the timing peer list. --- nqptp-clock-sources.c | 3 ++- nqptp-message-handlers.c | 4 +++- nqptp-shm-structures.h | 3 ++- nqptp.c | 10 +++++++++- nqptp.h | 2 +- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/nqptp-clock-sources.c b/nqptp-clock-sources.c index c32d424..009732a 100644 --- a/nqptp-clock-sources.c +++ b/nqptp-clock-sources.c @@ -205,7 +205,7 @@ void update_master() { if (old_master != -1) { // but there was a master clock, so remove it debug(1,"shm interface -- remove master clock designation"); - update_master_clock_info(0, 0, 0); + update_master_clock_info(0, NULL, 0, 0); } if (timing_peer_count == 0) debug(2, "No timing peer list found"); @@ -217,6 +217,7 @@ void update_master() { // master_clock_index = best_so_far; if (old_master != best_so_far) { update_master_clock_info(clocks_private[best_so_far].clock_id, + &clocks_private[best_so_far].ip, clocks_private[best_so_far].local_time, clocks_private[best_so_far].local_to_source_time_offset); } diff --git a/nqptp-message-handlers.c b/nqptp-message-handlers.c index 21cbaa4..448f860 100644 --- a/nqptp-message-handlers.c +++ b/nqptp-message-handlers.c @@ -494,7 +494,9 @@ void handle_delay_resp(char *buf, __attribute__((unused)) ssize_t recv_len, if (old_flags != clock_private_info->flags) { update_master(); } else if ((clock_private_info->flags & (1 << clock_is_master)) != 0) { - update_master_clock_info(clock_private_info->clock_id, clock_private_info->local_time, + update_master_clock_info(clock_private_info->clock_id, + &clock_private_info->ip, + clock_private_info->local_time, clock_private_info->local_to_source_time_offset); } diff --git a/nqptp-shm-structures.h b/nqptp-shm-structures.h index f30f989..50d9e9a 100644 --- a/nqptp-shm-structures.h +++ b/nqptp-shm-structures.h @@ -22,7 +22,7 @@ #define STORAGE_ID "/nqptp" #define MAX_CLOCKS 32 -#define NQPTP_SHM_STRUCTURES_VERSION 3 +#define NQPTP_SHM_STRUCTURES_VERSION 4 #define NQPTP_CONTROL_PORT 9000 // the control port will accept a UDP packet with the first letter being: @@ -40,6 +40,7 @@ struct shm_structure { uint16_t version; // check this is equal to NQPTP_SHM_STRUCTURES_VERSION uint32_t flags; // unused uint64_t master_clock_id; // the current master clock + char master_clock_ip[64]; // where it's coming from uint64_t local_time; // the time when the offset was calculated uint64_t local_to_master_time_offset; // add this to the local time to get master clock time }; diff --git a/nqptp.c b/nqptp.c index 04c5fb6..8db0faa 100644 --- a/nqptp.c +++ b/nqptp.c @@ -59,6 +59,10 @@ #include #include +#ifndef FIELD_SIZEOF +#define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f)) +#endif + // 8 samples per second #define BUFLEN 4096 // Max length of buffer @@ -71,7 +75,7 @@ int master_clock_index = -1; struct shm_structure *shared_memory = NULL; // this is where public clock info is available int epoll_fd; -void update_master_clock_info(uint64_t master_clock_id, uint64_t local_time, +void update_master_clock_info(uint64_t master_clock_id, const char *ip, uint64_t local_time, uint64_t local_to_master_offset) { if (shared_memory->master_clock_id != master_clock_id) debug(1, "Master clock is: %" PRIx64 ".", master_clock_id); @@ -79,6 +83,10 @@ void update_master_clock_info(uint64_t master_clock_id, uint64_t local_time, if (rc != 0) warn("Can't acquire mutex to update master clock!"); shared_memory->master_clock_id = master_clock_id; + if (ip != NULL) + strncpy((char *)&shared_memory->master_clock_ip, ip, FIELD_SIZEOF(struct shm_structure, master_clock_ip) - 1); + else + shared_memory->master_clock_ip[0] = '\0'; shared_memory->local_time = local_time; shared_memory->local_to_master_time_offset = local_to_master_offset; rc = pthread_mutex_unlock(&shared_memory->shm_mutex); diff --git a/nqptp.h b/nqptp.h index 0e0d1d5..c1da9eb 100644 --- a/nqptp.h +++ b/nqptp.h @@ -31,7 +31,7 @@ extern int master_clock_index; extern struct shm_structure *shared_memory; -void update_master_clock_info(uint64_t master_clock_id, uint64_t local_time, +void update_master_clock_info(uint64_t master_clock_id, const char *ip, uint64_t local_time, uint64_t local_to_master_offset); #endif \ No newline at end of file -- 2.47.2