From 69efea9cc568b3906340ddf45df66db811d4f204 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 23 Oct 2019 01:25:22 -0500 Subject: Use adjtimex instead of adjtime (bionic hasn't got adjtime). And check in more multicast support that's been sitting in the tree, I don't have a test enviornment for it anymore but somebody wanted this... --- lib/portability.h | 1 - toys/net/sntp.c | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/portability.h b/lib/portability.h index c2af278b..fb4e0383 100644 --- a/lib/portability.h +++ b/lib/portability.h @@ -297,7 +297,6 @@ static inline char *get_sched_policy_name(int policy) {return "unknown";} // stub it out for now. #ifdef __ANDROID_NDK__ #define __android_log_write(a, b, c) (0) -#define adjtime(x, y) (0) #endif #ifndef SYSLOG_NAMES diff --git a/toys/net/sntp.c b/toys/net/sntp.c index b1f3685f..b58abc10 100644 --- a/toys/net/sntp.c +++ b/toys/net/sntp.c @@ -6,13 +6,13 @@ modes: oneshot display, oneshot set, persist, serve, multi -USE_SNTP(NEWTOY(sntp, "M:m:Sp:asdDqr#<4>17=10[!as]", TOYFLAG_USR|TOYFLAG_BIN)) +USE_SNTP(NEWTOY(sntp, ">1M :m :Sp:t#<0=1>16asdDqr#<4>17=10[!as]", TOYFLAG_USR|TOYFLAG_BIN)) config SNTP bool "sntp" default y help - usage: sntp [-saSdDqm] [-r SHIFT] [-m ADDRESS] [-p PORT] [SERVER] + usage: sntp [-saSdDq] [-r SHIFT] [-mM[ADDRESS]] [-p PORT] [SERVER] Simple Network Time Protocol client. Query SERVER and display time. @@ -20,8 +20,9 @@ config SNTP -s Set system clock suddenly -a Adjust system clock gradually -S Serve time instead of querying (bind to SERVER address if specified) - -m Wait for updates from multicast ADDRESS (RFC 4330 says use 224.0.1.1) - -M Multicast server on ADDRESS + -m Wait for updates from multicast ADDRESS (RFC 4330 default 224.0.1.1) + -M Multicast server on ADDRESS (deault 224.0.0.1) + -t TTL (multicast only, default 1) -d Daemonize (run in background re-querying ) -D Daemonize but stay in foreground: re-query time every 1000 seconds -r Retry shift (every 1< GLOBALS( - long r; + long r, t; char *p, *m, *M; ) @@ -76,8 +78,9 @@ void sntp_main(void) union socksaddr sa; int fd, tries = 0; + if (FLAG(M)) toys.optflags |= FLAG_S; if (!(FLAG(S)||FLAG(m)) && !*toys.optargs) - error_exit("Need -Sm or SERVER address"); + error_exit("Need -SMm or SERVER address"); // Lookup address and open server or client UDP socket if (!TT.p || !*TT.p) TT.p = "123"; @@ -87,15 +90,19 @@ void sntp_main(void) if (FLAG(d) && daemon(0, 0)) perror_exit("daemonize"); // Act as server if necessary - if (FLAG(S)|FLAG(m)) { + if (FLAG(S)||FLAG(m)) { fd = xbindany(ai); - if (TT.m) { + if (TT.m || TT.M) { struct ip_mreq group; + int t = 0; // subscribe to multicast group memset(&group, 0, sizeof(group)); - group.imr_multiaddr.s_addr = inet_addr(TT.m); + group.imr_multiaddr.s_addr = inet_addr(TT.m ? TT.m : TT.M); xsetsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &group, sizeof(group)); + xsetsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &t, 4); + t = TT.t; + xsetsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &t, 4); } } else fd = xsocket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP); @@ -107,13 +114,13 @@ void sntp_main(void) for (;;) { now = millitime(); - // Figure out if we're in server and multicast modes don't poll + // If we're in server or multicast client mode, don't poll if (FLAG(m) || FLAG(S)) then = -1; // daemon and oneshot modes send a packet each time through outer loop else { then = now + 3000; - if (FLAG(d) || FLAG(D)) then = now + (1<