/// <summary> /// Read a NTP time stamp from the packet at offset /// </summary> /// <param name="Packet">Packet to pull timestamp from</param> /// <param name="Offset">Offset from start of packet</param> /// <returns>Timestamp that was read</returns> private NtpTimeStamp ReadTimeStampSequenctial(byte[] Packet, ref uint Offset) { NtpTimeStamp time = new NtpTimeStamp(); time.Seconds = (uint)ReadFieldSequential(Packet, ref Offset, 4); time.Fraction = (uint)ReadFieldSequential(Packet, ref Offset, 4); return(time); }
/// <summary> /// Build a client NTP packet /// </summary> /// <param name="TransmitTimestamp">Timestamp to put in the packet</param> /// <returns>Packet to be sent</returns> private byte[] BuildNtpClientPacket(NtpTimeStamp TransmitTimestamp) { byte[] packet = new byte[48]; for (int i = 0; i < packet.Length; i++) { packet[i] = 0; } packet[0] = 0x23; WriteUint(packet, 40, 4, TransmitTimestamp.Seconds); // Transmit Timestamp WriteUint(packet, 44, 4, TransmitTimestamp.Fraction); return(packet); }
/// <summary> /// Convert a NTP timestamp to a Windows NT file time /// </summary> /// <param name="NtpTime">NtpTimeStamp</param> /// <returns>Filetime</returns> public static ulong NtpTimeStampToFileTime(NtpTimeStamp NtpTime) { BigInteger epochDelta = (BigInteger)((NtpEopch - FileTimeEopch).TotalSeconds); epochDelta *= OneHundredNsInOneSecond; BigInteger seconds = new BigInteger(); BigInteger fraction = new BigInteger(); seconds = NtpTime.Seconds; fraction = NtpTime.Fraction; seconds *= OneHundredNsInOneSecond; fraction *= OneHundredNsInOneSecond; fraction /= MaxUintPlusOne; return((ulong)(seconds + fraction + epochDelta)); }
/// <summary> /// Convert a Windows NT file time to a NTP timestamp /// </summary> /// <param name="time">Time in 100ns</param> /// <returns>Ntp timestamp</returns> public static NtpTimeStamp FileTimeToNtpTimeStamp(ulong Time) { BigInteger fileTime = Time; BigInteger epochDelta = (BigInteger)((NtpEopch - FileTimeEopch).TotalSeconds); NtpTimeStamp ntpTime = new NtpTimeStamp(); BigInteger seconds = new BigInteger(); BigInteger fraction = new BigInteger(); epochDelta *= OneHundredNsInOneSecond; fileTime -= epochDelta; seconds = fileTime / OneHundredNsInOneSecond; fraction = fileTime % OneHundredNsInOneSecond; fraction *= MaxUintPlusOne; fraction /= OneHundredNsInOneSecond; ntpTime.Seconds = (uint)seconds; ntpTime.Fraction = (uint)fraction; return(ntpTime); }