Pergunta

Good day,

I am attempting to implement a very basic NTP client just so I can query a remote NTP server (ie: pool.ntp.org) for the internet time to automatically set the time on my Windows development board on each boot.

I've refactored the code here from another user:

Get time/date from server with sntp(windows c++)

But the program hangs at the recv() operation. The DNS resolution and send() operations appear to execute without any issues.

Does anyone know where I can find a simple NTP client example in Windows (GPL is preferred, but anything would do at this point), or can they comment on why the following code block would hang from the example (I shouldn't say "hang", it just never appears to receive a response).

NTP_CLIENT.CPP

/******************************************************************************
 * Project Headers
 *****************************************************************************/
#include "stdafx.h"
#include "ntp_client.h"

/******************************************************************************
 * System Headers
 *****************************************************************************/
#include <winsock2.h>
#include <winsock.h>
#include <ws2tcpip.h>


/******************************************************************************
 * Preprocessor Directives and Macros
 *****************************************************************************/


/******************************************************************************
 * Class Member Function Definitions
 *****************************************************************************/
void Timestamp::ReverseEndian(void) {
    ReverseEndianInt(seconds);
    ReverseEndianInt(fraction);
}

time_t Timestamp::to_time_t(void) {
    return (seconds - ((70 * 365 + 17) * 86400))&0x7fffffff;
}

void NTPMessage::ReverseEndian(void) {
    ref.ReverseEndian();
    orig.ReverseEndian();
    rx.ReverseEndian();
    tx.ReverseEndian();
}

int NTPMessage::recv(int sock) {
    int ret = ::recv(sock, (char*)this, sizeof(*this), 0);
    ReverseEndian();
    return ret;
}

int NTPMessage::sendto(int sock, struct sockaddr_in* srv_addr) {
    ReverseEndian();
    int ret = ::sendto(sock, (const char*)this, sizeof(*this), 0, (sockaddr*)srv_addr, sizeof(*srv_addr));
    ReverseEndian();
    return ret;
}

void NTPMessage::clear() {
    memset(this, 0, sizeof(*this));
}

NTP_CLIENT.H

#ifndef __NTP_CLIENT_H__
#define __NTP_CLIENT_H__

#include <ctime>

#define ReverseEndianInt(x) ((x) = \
    ((x)&0xff000000) >> 24 |       \
    ((x)&0x00ff0000) >> 8  |       \
    ((x)&0x0000ff00) << 8  |       \
    ((x)&0x000000ff) << 24)

/**
 * NTP Fixed-Point Timestamp Format.
 * From [RFC 5905](http://tools.ietf.org/html/rfc5905).
 */
struct Timestamp {
    unsigned int seconds;   /**< Seconds since Jan 1, 1900. */
    unsigned int fraction;  /**< Fractional part of seconds. Integer number of 2^-32 seconds. */

    /**
     * Reverses the Endianness of the timestamp.
     * Network byte order is big endian, so it needs to be switched before
     * sending or reading.
     */
    void ReverseEndian(void);

    /**
     * Convert to time_t.
     * Returns the integer part of the timestamp in unix time_t format,
     * which is seconds since Jan 1, 1970.
     */
    time_t to_time_t(void);
};

/**
 * A Network Time Protocol Message.
 * From [RFC 5905](http://tools.ietf.org/html/rfc5905).
 */
struct NTPMessage {
    unsigned int mode :3;           /**< Mode of the message sender. 3 = Client, 4 = Server */
    unsigned int version :2;        /**< Protocol version. Should be set to 3. */
    unsigned int leap :2;           /**< Leap seconds warning. See the [RFC](http://tools.ietf.org/html/rfc5905#section-7.3) */
    unsigned char stratum;          /**< Servers between client and physical timekeeper. 1 = Server is Connected to Physical Source. 0 = Unknown. */
    unsigned char poll;             /**< Max Poll Rate. In log2 seconds. */
    unsigned char precision;        /**< Precision of the clock. In log2 seconds. */
    unsigned int sync_distance;     /**< Round-trip to reference clock. NTP Short Format. */
    unsigned int drift_rate;        /**< Dispersion to reference clock. NTP Short Format. */
    unsigned char ref_clock_id[4];  /**< Reference ID. For Stratum 1 devices, a 4-byte string. For other devices, 4-byte IP address. */
    Timestamp ref;                  /**< Reference Timestamp. The time when the system clock was last updated. */
    Timestamp orig;                 /**< Origin Timestamp. Send time of the request. Copied from the request. */
    Timestamp rx;                   /**< Recieve Timestamp. Reciept time of the request. */
    Timestamp tx;                   /**< Transmit Timestamp. Send time of the response. If only a single time is needed, use this one. */


    /**
     * Reverses the Endianness of all the timestamps.
     * Network byte order is big endian, so they need to be switched before
     * sending and after reading.
     *
     * Maintaining them in little endian makes them easier to work with
     * locally, though.
     */
    void ReverseEndian(void);

    /**
     * Recieve an NTPMessage.
     * Overwrites this object with values from the received packet.
     */
    int recv(int sock);

    /**
     * Send an NTPMessage.
     */
    int sendto(int sock, struct sockaddr_in* srv_addr);

    /**
     * Zero all the values.
     */
    void clear();
};

#endif  /* __NTP_CLIENT_H__ */

Firewall and antivirus software has been disabled on the device under test.

Thank you.

EDIT


As requested, this is the body of main:

WSADATA wsaData;
DWORD ret = WSAStartup(MAKEWORD(2,0), &wsaData);

char *host = "pool.ntp.org"; /* Don't distribute stuff pointing here, it's not polite. */
//char *host = "time.nist.gov"; /* This one's probably ok, but can get grumpy about request rates during debugging. */

NTPMessage msg;
/* Important, if you don't set the version/mode, the server will ignore you. */
msg.clear();
msg.version = 3;
msg.mode = 3 /* client */;

NTPMessage response;
response.clear();

int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
sockaddr_in srv_addr;
memset(&srv_addr, 0, sizeof(srv_addr));
dns_lookup(host, &srv_addr); /* Helper function defined below. */

msg.sendto(sock, &srv_addr);
response.recv(sock);

time_t t = response.tx.to_time_t();
char *s = ctime(&t);
printf("The time is %s.", s);

WSACleanup();

References

  1. Get time/date from server with sntp(windows c++)
Foi útil?

Solução

From Figure 8 of RFC 5905 (will need to scroll down to page 18):

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|LI | VN  |Mode |    Stratum     |     Poll      |  Precision   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

One thing I see in figure 8 is that mode, version, and leap should compose the first byte.

Change mode, version, and leap types from unsigned int to unsigned char so your bitwise variables only span 1 byte instead of 4. This will make your NTPMessage size the expected 48 bytes.

like:

struct NTPMessage {
    unsigned char mode :3;
    unsigned char version :2;
    unsigned char leap :2;
    ...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top