NTP Request Packet

2019-06-19 14:46发布

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.

3条回答
Lonely孤独者°
2楼-- · 2019-06-19 15:14

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

Check this out in case you haven't yet: http://tools.ietf.org/html/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.

查看更多
时光不老,我们不散
3楼-- · 2019-06-19 15:27

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.

查看更多
Fickle 薄情
4楼-- · 2019-06-19 15:35

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)

查看更多
登录 后发表回答