Something that I thought would be interesting is to dabble a little bit with some obscure network programming, however something simple. This is the Network Time Protocol(NTP). NTP has been around for year, before 1985 and is a way to synchronize time across computers in networks. There have been 4 version of this protocol with the latest indepth RFC being 5905 and RFC 7822 with some updates. RFC 5905 established the standard for NTPv4. There is also a more light weight subset of NTP called SNTP, however, here I will focus on implementing a simple client using NTPv4.
Datastructure
The primary packet for NTPv4 has one form. It consists of 48 bytes of data. Or atleat, those are the parts of the packet we care about. There are some option fields, however, we will not concern ourselves with this. The information for What we care about as far as the packet in RFC 5905 is located in page 17, chapter 7.
First is LI, which is short for Leap Indicator. This will indicate if there anything we need to concern ourselves about a leap second being added or subtracted. This takes up the first 2 bits. Next is VN, Version Number, encoded in 3 bits. After that is the Mode for 3 bits. When handling this packet, the first 3 fields will fit in an unsigned 8-bit integer, in rust as a u8.
In the rest of the packet, 1 byte can be treated as a u8 in Rust and 4 bytes as a u32.
Stratum is an interesting concept in NTP. The Stratum Model has to deal with the hierarchy of machines. Levels 0-15 indicate the device’s distance from the reference clock. Stratum 0 can be directly connected to something such as an atomix clock. Stratum 0 devices are usually connected directly to a Startum 1 device that will serve as the time server to distribute time to Stratum 2 clients or servers and so on. Generally, it can be assumed that the higher the stratum number is, the less stability and accuracy there is. Any stratum greater than 15 is rejected.
Poll is the max interval between messages, this is taken in log2 seconds.
Precision represents the precision of the system clock, also in log2 seconds.
For the remaining fields, I suggest taking a look at RFC 5905.
main.rs
Here is the main source file of the program. In the program we create an NtpDataPacket which is a structure I made to represent the packet for NTP. I also have some constants set for the host and port of one of the many publically availiable NTP servers. NTP uses UDP, so a UDPSocket is created, binded and connected. Next, write to the socket, which will be covered later in this article. Read the response, parse and display. Pretty straight forward and simple. THe magic for the protocol happens in the ntp_client.rs file.
lib.rs
Really simple lib.rs file
ntp_client.rs
NtpDataPacket
This is a structure that defines the packet for the protocol. Some of the fields are described some above. This structure is 48 bytes in total. To keep communication simple, an array of 48 bytes (u8) is created, sent and we received it. A function is created in the implementation block of NtpDataPacket to parse the 48 byte block received into the data structure for better handling. Using bitwise logical operators to ensure that data is placed appropraitely.
What is required by the packet
In the send function, before sending the 48 byte packet, the first byte is set to value 0x1b. This is 0b00011011. This will set the appropriate LI, VN and Mode bits.
The version and mode need to be set in order to get a response.
Above is the output from the server. The primary field toget the time is the tx_ts_seconds field. However, converting that will not give the current time. 3877306283 yields a date and time of November 12th, 2092 5:31:23 AM. So, how do we get the correct time? We substract 70 years worth of seconds from the time. This is 1900 to the UNIX epoch of Janruary 1st, 1970. 70 years in seconds is 2208988800. This will give us the proper time.