8

I'm trying to figure out what I need to send (client) in the NTP request package to retrieve a NTP package from the server. I'm working with the LWIP on Cortex M3, Stellaris LM3S6965

I understand that I will recieve a UDP header and then the NTP protocol with the different timestamps the remove the latency. I probable need to make an UDP header but what do I need to add as data?

wireshark image: enter image description here

I hope you guys can help me.

artless noise
  • 21,212
  • 6
  • 68
  • 105
Sharpless512
  • 3,062
  • 5
  • 35
  • 60

3 Answers3

6

The client request packet is the same as the server reply packet - just set the MODE bits in the first word to 3 (Client) to be sure.

Send the whole 48 byte packet to the server, it will reply with the same.

The simplest packet would be 0x1B followed by 47 zeroes. (Version = 3, mode = 3)

Tomuo
  • 141
  • 1
  • 6
  • Correct answer, after sending such format to NTP server, I **received some** reply (previously the "TimeOutException" was thrown). Perhaps you could also tell how to **extract** time from the received packet? That is, from the **48 bytes** received, how to **print** time as "YYYY:MM:DD:HH:MM:SS" (or as some other format)? Which bits correspond to what? – mercury0114 Jan 31 '16 at 18:29
3

This is for starters: http://www.eecis.udel.edu/~mills/ntp/html/warp.html

Check this out in case you haven't yet: https://www.rfc-editor.org/rfc/rfc5905

Then look at this: http://wiki.wireshark.org/NTP and check out the sample pcap files that they have uploaded.

I am not sure if this helped, but I hope so.

Community
  • 1
  • 1
ipinak
  • 5,739
  • 3
  • 23
  • 41
0

I have coded an Arduino to connect to an NTP server using this code here,

http://www.instructables.com/id/Arduino-Internet-Time-Client/step2/Code/

Look at the method called getTimeAndDate, and sendNTPpacket.

That is the packet that is sent. This is setting up a buffer and shows binary (0b) and hex (0x) being set up in the 48 character buffer. The address is the NTP time server,

memset(packetBuffer, 0, NTP_PACKET_SIZE);
packetBuffer[0] = 0b11100011;
packetBuffer[1] = 0;
packetBuffer[2] = 6;
packetBuffer[3] = 0xEC;
packetBuffer[12]  = 49;
packetBuffer[13]  = 0x4E;
packetBuffer[14]  = 49;
packetBuffer[15]  = 52;                 
Udp.beginPacket(address, 123);
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.endPacket();

Here is what happens to the received packet,

Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer
unsigned long highWord, lowWord, epoch;
highWord = word(packetBuffer[40], packetBuffer[41]);
lowWord = word(packetBuffer[42], packetBuffer[43]); 
epoch = highWord << 16 | lowWord;
epoch = epoch - 2208988800 + timeZoneOffset;
flag=1;
setTime(epoch);

setTime is part of the arduino time library, so the epoch should be the number of seconds since Jan 1, 1900 as suggested here (search for epoch),

https://en.wikipedia.org/wiki/Network_Time_Protocol

But in case you want a C# version, I found this here, compiled the code under the excepted answer and it works. This will likely make more sense to you, and does show the use of epoch 1/1/1900.

How to Query an NTP Server using C#?

Can easily see the similarity.

Community
  • 1
  • 1
peter
  • 13,009
  • 22
  • 82
  • 142