From 2bd2946e9f51f843c58db2052cb92b2b0d1cd0df Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Fri, 10 Apr 2020 11:23:12 -0400 Subject: [PATCH] Add time sync option to copernicus Since we are dealing in TLS certificates, the easiest thing is to have copernicus sync time. It is not as robust as ntp, but it'll do as a stopgap until the real time utilities kick in. --- .../confluent/discovery/protocols/ssdp.py | 3 +- misc/copernicus.c | 41 +++++++++++++------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/confluent_server/confluent/discovery/protocols/ssdp.py b/confluent_server/confluent/discovery/protocols/ssdp.py index 4ceb1ae1..d6d8de18 100644 --- a/confluent_server/confluent/discovery/protocols/ssdp.py +++ b/confluent_server/confluent/discovery/protocols/ssdp.py @@ -33,6 +33,7 @@ import confluent.util as util import confluent.log as log import eventlet.green.select as select import eventlet.green.socket as socket +import time try: from eventlet.green.urllib.request import urlopen except (ImportError, AssertionError): @@ -172,7 +173,7 @@ def snoop(handler, byehandler=None, protocol=None, uuidlookup=None): node = uuidlookup(curruuid) if not node: break - reply = 'HTTP/1.1 200 OK\r\nNODENAME: {0}'.format(node) + reply = 'HTTP/1.1 200 OK\r\nNODENAME: {0}\r\nCURRTIME: {1}\r\n'.format(node, int(time.time())) if not isinstance(reply, bytes): reply = reply.encode('utf8') s.sendto(reply, peer) diff --git a/misc/copernicus.c b/misc/copernicus.c index b727de2a..658b5fa6 100644 --- a/misc/copernicus.c +++ b/misc/copernicus.c @@ -76,6 +76,7 @@ int main(int argc, char* argv[]) { int ifidx, offset; fd_set rfds; struct timeval tv; + int settime = 0; socklen_t dstsize, dst4size; dstsize = sizeof(dst); dst4size = sizeof(dst4); @@ -102,10 +103,6 @@ int main(int argc, char* argv[]) { inet_pton(AF_INET, "239.255.255.250", &dst4.sin_addr); strncpy(msg, "M-SEARCH * HTTP/1.1\r\nST: urn:xcat.org:service:confluent:", 1024); offset = strnlen(msg, 1024); - if (argc > 1) { - snprintf(msg + offset, 1024 - offset, "/node=%s", argv[1]); - offset = strnlen(msg, 1024); - } add_uuid(msg + offset, 1024 - offset); offset = strnlen(msg, 1024); add_macs(msg + offset, 1024 - offset); @@ -142,8 +139,8 @@ int main(int argc, char* argv[]) { FD_ZERO(&rfds); FD_SET(n4, &rfds); FD_SET(ns, &rfds); - tv.tv_sec = 10; - tv.tv_usec = 0; + tv.tv_sec = 2; + tv.tv_usec = 500000; ifidx = select(FD_SETSIZE, &rfds, NULL, NULL, &tv); while (ifidx) { if (ifidx == -1) perror("Unable to select"); @@ -154,15 +151,23 @@ int main(int argc, char* argv[]) { recvfrom(n4, msg, 1000, 0, (struct sockaddr *)&dst4, &dst4size); if (nodenameidx = strstr(msg, "NODENAME: ")) { nodenameidx += 10; - strncpy(nodename, nodenameidx, 1024); - nodenameidx = strstr(nodenameidx, "\r"); + strncpy(nodename, nodenameidx, 1024); + nodenameidx = strstr(nodename, "\r"); if (nodenameidx) { nodenameidx[0] = 0; } if (strncmp(lastnodename, nodename, 1024) != 0) { printf("NODENAME: %s\n", nodename); strncpy(lastnodename, nodename, 1024); } } - memset(msg, 0, 1024); + if (nodenameidx = strstr(msg, "CURRTIME: ")) { + nodenameidx += 10; + strncpy(nodename, nodenameidx, 1024); + if (nodenameidx = strstr(nodename, "\r")) { + nodenameidx[0] = 0; + } + settime = strtol(nodename, NULL, 10); + } + memset(msg, 0, 1024); inet_ntop(dst4.sin_family, &dst4.sin_addr, msg, dst4size); /* Take measure from printing out the same ip twice in a row */ if (strncmp(lastmsg, msg, 1024) != 0) { @@ -177,14 +182,22 @@ int main(int argc, char* argv[]) { recvfrom(ns, msg, 1000, 0, (struct sockaddr *)&dst, &dstsize); if (nodenameidx = strstr(msg, "NODENAME: ")) { nodenameidx += 10; - strncpy(nodename, nodenameidx, 1024); - nodenameidx = strstr(nodenameidx, "\r"); + strncpy(nodename, nodenameidx, 1024); + nodenameidx = strstr(nodename, "\r"); if (nodenameidx) { nodenameidx[0] = 0; } if (strncmp(lastnodename, nodename, 1024) != 0) { printf("NODENAME: %s\n", nodename); strncpy(lastnodename, nodename, 1024); } } + if (nodenameidx = strstr(msg, "CURRTIME: ")) { + nodenameidx += 10; + strncpy(nodename, nodenameidx, 1024); + if (nodenameidx = strstr(nodename, "\r")) { + nodenameidx[0] = 0; + } + settime = strtol(nodename, NULL, 10); + } memset(msg, 0, 1024); inet_ntop(dst.sin6_family, &dst.sin6_addr, msg, dstsize); if (strncmp(last6msg, msg, 1024) != 0) { @@ -198,8 +211,12 @@ int main(int argc, char* argv[]) { } } } + if (settime && argc > 1 && strcmp(argv[1], "-t") == 0) { + tv.tv_sec = settime; + settimeofday(&tv, NULL); + settime = 0; + } tv.tv_sec = 0; - tv.tv_usec = 500000; FD_SET(n4, &rfds); FD_SET(ns, &rfds); ifidx = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);