1

Currently I have a C# program that queries my NTP server (CentOS 7)

public static DateTime GetNetworkTime()
{
    const string ntpServer = "192.168.10.48";
    var ntpData = new byte[48];
    ntpData[0] = 0x1B; //LeapIndicator = 0 (no warning), VersionNum = 3 (IPv4 only), Mode = 3 (Client Mode)

    var addresses = Dns.GetHostEntry(ntpServer).AddressList;
    var ipEndPoint = new IPEndPoint(addresses[0], 123);
    var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

    socket.Connect(ipEndPoint);
    socket.Send(ntpData);
    socket.Receive(ntpData);
    socket.Close();

    ulong intPart = (ulong)ntpData[40] << 24 | (ulong)ntpData[41] << 16 | (ulong)ntpData[42] << 8 | (ulong)ntpData[43];
    ulong fractPart = (ulong)ntpData[44] << 24 | (ulong)ntpData[45] << 16 | (ulong)ntpData[46] << 8 | (ulong)ntpData[47];

    var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
    var networkDateTime = (new DateTime(1900, 1, 1)).AddMilliseconds((long)milliseconds);

    return networkDateTime;
}

this works, then after sometime it will return an error:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

it continues to work on another PC. I'm thinking ntpd has a built-in firewall that is blocking the socket connection. I have firewalld offline. Also, changing the ntp server to like asia.pool.ntp.org seems to work.

I also currently removed all restricts. Is there any way to find the cause of this?

svick
  • 137
  • 7
Joe
  • 111
  • 2
  • First of all `firefalld` has different kind of zones, that you have to understand. You can still install `iptables-services` package and try again. But don't forget to **disable firewalld**. Command to check you ntp: `ntpdate -q {yourntpaddr}` – obohovyk Sep 11 '15 at 09:27
  • `server 192.168.10.48, stratum 3, offset -0.000022, delay 0.02568` i don't have `iptables-services` installed. and `firewalld` is not running. – Joe Sep 11 '15 at 09:52
  • What does `tcpdump` (or for more clicky, wireshark) show when the client connects? E.g. `tcpdump -ennl -i eth0 port 123 and host thatclient` or such. – thrig Sep 11 '15 at 14:26
  • Have you read "Network Time Protocol Version 4: Protocol and Algorithms Specification" https://tools.ietf.org/html/rfc5905 ? – waltinator Sep 11 '15 at 17:43
  • Can you query your NTP server via the commandline? I'm wondering if your question is code-centric or NTP-server centric, and figuring *that* out may help steer your question to better audience (SO for coding, or here if there's an NTP config issue). – Jeff Schaller Sep 13 '15 at 20:44

0 Answers0