From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Mon, 15 Mar 2021 09:14:52 +0000 (+0000) Subject: include a linked list for recording each ptp source. Untested X-Git-Tag: 1.1-dev~113 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8012f0ee082a853bae8dfbe2ddc530da59a8835a;p=thirdparty%2Fnqptp.git include a linked list for recording each ptp source. Untested --- diff --git a/nqptp.c b/nqptp.c index 54ca06b..a9cde57 100644 --- a/nqptp.c +++ b/nqptp.c @@ -4,7 +4,7 @@ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 3. + * the Free Software Foundation, version 2. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -60,8 +60,13 @@ enum messageType { Reserved_F }; +typedef struct { + char *ip; //ipv4 or ipv6 + uint64_t t1,t2,t3,t4; + struct ptpSource *next; +} ptpSource; + #define BUFLEN 4096 // Max length of buffer -#define PORT 320 // The port on which to listen for incoming data struct sockaddr_in si_me_319, si_me_320, si_other; @@ -99,6 +104,39 @@ uint64_t get_time_now() { return timespec_to_ns(&tn); } +ptpSource* findOrCreateSource(ptpSource** list, char *ip) { + ptpSource* response; + ptpSource** insertion_point = list; // in case the list is empty + ptpSource* crawler = *list; + if (crawler == NULL) { + insertion_point = list; + } else { + while ((crawler->next != NULL) && (strcasecmp(ip,crawler->ip)!= 0)) { + crawler = crawler->next; + } + if (strcasecmp(ip,crawler->ip)== 0) { + // found, so no insertion + insertion_point = NULL; + else + // not found, so we are on the last item. Add a new one on to the end. + insertion_point = &crawler->next; + } + } + // here, if the insertion point is null, then + // the record is pointer to by crawler + // otherwise, add a new record at the insertion point + if (insertion_point == NULL) { + response = crawler; + } else { + response = (ptpSource*)malloc(sizeof(ptpSource)); + if (response != NULL) { + memset((void *)response,0,sizeof(ptpSource)); + response->ip = strdup(ip); + } + } + return response; +} + void print_buffer(char *buf, size_t buf_len) { uint64_t time_now = get_time_now(); if (time_then == 0) { @@ -157,6 +195,8 @@ int main(void) { unsigned int slen = sizeof(si_other); ssize_t recv_len; + ptpSource *clocks = NULL; // a one-way linked list + char buf[BUFLEN]; int status; @@ -284,6 +324,8 @@ int main(void) { -1) { die("recvfrom() 319"); } else if (recv_len >= (ssize_t)sizeof(struct ptp_common_message_header)) { + // find or create a ptpSource record for this one. + ptpSource *the_clock = findOrCreateSource(clocks); // print_buffer(buf, recv_len); switch (buf[0] & 0xF) { case Sync: { // if it's a sync