Hacker News new | past | comments | ask | show | jobs | submit login
A Tiny NTP client (seriot.ch)
46 points by beefburger on July 20, 2015 | hide | past | favorite | 7 comments



This is actually a tiny `sntp` client:

  14. Simple Network Time Protocol (SNTP)
    Primary  servers  and  clients  complying  with a  subset  of  NTP,  called
    the  Simple  Network Time  Protocol  (SNTPv4)  [RFC4330],  do not  need  to
    implement the  mitigation algorithms described  in Section 9  and following
    sections. SNTP  is intended  for  primary servers  equipped  with a  single
    reference  clock, as  well as  for clients  with a  single upstream  server
    and  no  dependent clients. The  fully  developed  NTPv4 implementation  is
    intended for secondary servers with  multiple upstream servers and multiple
    downstream  servers or  clients. Other than  these considerations,  NTP and
    SNTP servers and clients are completely interoperable and can be intermixed
    in NTP subnets.
https://tools.ietf.org/html/rfc5905#section-14


Usually an SNTP client will add the latency/2 to get the correct time - I.E. if a packet takes 350 msec to return, then you'll want to add 175 msec from the wall-clock-time in the packet to get the "current wall clock time".


Here's a portable version for those of us who do not use Bash:

  a=$(printf '\xb%-47.s' \
  |nc -uw1 ntp.metas.ch 123 \
  |exec xxd -s40 -l4 -p) 
  b=$(printf %d\\n 0x"$a")
  c=$((b-2208988800))
  date -r$c
There is a way to do the xxd step using original netcat. Included with nc was a short program called nc-data. It converts from btoa and atob for shoveling data to nc.

Slower than xxd but still useful. It also shows octal, decimal and byte number.

Another exercise might be a one-liner for the TAICLOCK protocol: http://cr.yp.to/proto/taiclock.txt (Alternative to SNTP with smaller packets and support for leap seconds.)


Less than well known: you can also use the timestamp of an ICMP packet to set your clock. The "clockdiff" command, for example, can be used to find the difference.


Simple alternative to quickly set the current time: use the date header of any webserver:

    date -s "`curl -I http://www.kernel.org |grep 'Date:'|awk -F': ' '{print $2}'`"



didn't quite work for me, but was close. This worked:

date -d @$((16#`printf "\xb%-47.s"|nc -uw1 ntp.metas.ch 123|xxd -s40 -l4 -p`-2208988800))




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: