From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Sat, 30 Oct 2021 17:28:08 +0000 (+0100) Subject: add ip family stuff to the clock records and the port sockets X-Git-Tag: 1.2~95 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=65fea24b2f43816e51990494d626507f2bba3e67;p=thirdparty%2Fnqptp.git add ip family stuff to the clock records and the port sockets --- diff --git a/nqptp-clock-sources.c b/nqptp-clock-sources.c index c894300..4354c15 100644 --- a/nqptp-clock-sources.c +++ b/nqptp-clock-sources.c @@ -71,13 +71,29 @@ int create_clock_source_record(char *sender_string, } if (found == 1) { - response = i; - memset(&clocks_private_info[i], 0, sizeof(clock_source_private_data)); - strncpy((char *)&clocks_private_info[i].ip, sender_string, - FIELD_SIZEOF(clock_source_private_data, ip) - 1); - clocks_private_info[i].vacant_samples = MAX_TIMING_SAMPLES; - clocks_private_info[i].in_use = 1; - debug(2, "create record for ip: %s.", &clocks_private_info[i].ip); + int family = 0; + int ret; + // check its ipv4/6 family + struct addrinfo hint, *res = NULL; + memset(&hint, '\0', sizeof hint); + hint.ai_family = PF_UNSPEC; + hint.ai_flags = AI_NUMERICHOST; + + ret = getaddrinfo(sender_string, NULL, &hint, &res); + if (getaddrinfo(sender_string, NULL, &hint, &res) == 0) { + family = res->ai_family; + freeaddrinfo(res); + response = i; + memset(&clocks_private_info[i], 0, sizeof(clock_source_private_data)); + strncpy((char *)&clocks_private_info[i].ip, sender_string, + FIELD_SIZEOF(clock_source_private_data, ip) - 1); + clocks_private_info[i].family = family; + clocks_private_info[i].vacant_samples = MAX_TIMING_SAMPLES; + clocks_private_info[i].in_use = 1; + debug(1, "create record for ip: %s, family: %d.", &clocks_private_info[i].ip, clocks_private_info[i].family); + } else { + die("cannot getaddrinfo for ip: %s.", &clocks_private_info[i].ip); + } } else { die("Clock tables full!"); } diff --git a/nqptp-clock-sources.h b/nqptp-clock-sources.h index 28b5b09..1a8cd66 100644 --- a/nqptp-clock-sources.h +++ b/nqptp-clock-sources.h @@ -42,6 +42,7 @@ typedef struct { // information about each clock source typedef struct { char ip[64]; // 64 is nicely aligned and bigger than INET6_ADDRSTRLEN (46) + int family; // AF_INET or AF_INET6 uint64_t clock_id; uint64_t local_time; // the local time when the offset was calculated uint64_t source_time; diff --git a/nqptp-utilities.c b/nqptp-utilities.c index aa3d2d3..3663486 100644 --- a/nqptp-utilities.c +++ b/nqptp-utilities.c @@ -96,6 +96,7 @@ void open_sockets_at_port(uint16_t port, sockets_open_bundle *sockets_open_stuff debug(2, "listening on %s port %d.", p->ai_family == AF_INET6 ? "IPv6" : "IPv4", port); sockets_open_stuff->sockets[sockets_open_stuff->sockets_open].number = fd; sockets_open_stuff->sockets[sockets_open_stuff->sockets_open].port = port; + sockets_open_stuff->sockets[sockets_open_stuff->sockets_open].family = p->ai_family; sockets_open_stuff->sockets_open++; } } diff --git a/nqptp-utilities.h b/nqptp-utilities.h index 66367a8..3d2e933 100644 --- a/nqptp-utilities.h +++ b/nqptp-utilities.h @@ -30,6 +30,7 @@ typedef struct { int number; uint16_t port; + int family; // AF_INET or AF_INET6 } socket_info; typedef struct {