5

I'm searching for a working code in c/c++ that gets the time and date from the server(ntp.belnet.be). It works with UDP and uses port 123.

Can someone help?

//sending pakket
memset(&sntp_msg_header, 0, sizeof sntp_msg_header);
sntp_msg_header.flags = 27;
sntp_msg_header.originate_timestamp_secs = time(NULL);

// Get data in rxmsg
...
...

// print time
timeval = ntohl(rxmsg.transmit_timestamp_secs) - ((70ul * 365ul + 17ul) * 86400ul);
printf("%s", ctime(&timeval));

This is what i have so far. But i can't get the correct data out of it. I hope this is more information.

If found this:

import socket
import struct
import sys
import time

TIME1970 = 2208988800L      # Thanks to F.Lundh

client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
data = '\x1b' + 47 * '\0'
client.sendto( data, ( sys.argv[1], 123 ))
data, address = client.recvfrom( 1024 )
if data:
    print 'Response received from:', address
    t = struct.unpack( '!12I', data )[10]
    t -= TIME1970
    print '\tTime=%s' % time.ctime(t)

But it is in python. Can someone change this to c++ or is there a converter for this??

user1765216
  • 111
  • 2
  • 2
  • 8

2 Answers2

7

This is what I use. It isn't very elegant, but it should be enough to get the idea across. The basic idea is to have a structure that matches what the network sends and receives. The reversed endianness was initially unobvious. It's worth mentioning here that this pretty much does nothing useful for error conditions. You'd want to fix that. :)

#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() {
        ReverseEndianInt(seconds);
        ReverseEndianInt(fraction);
    }

    /**
     * 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()
    {
        return (seconds - ((70 * 365 + 17) * 86400))&0x7fffffff;
    }
};

/**
 * 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() {
        ref.ReverseEndian();
        orig.ReverseEndian();
        rx.ReverseEndian();
        tx.ReverseEndian();
    }

    /**
     * Recieve an NTPMessage.
     * Overwrites this object with values from the recieved packet.
     */
    int recv(int sock)
    {
        int ret = ::recv(sock, (char*)this, sizeof(*this), 0);
        ReverseEndian();
        return ret;
    }

    /**
     * Send an NTPMessage.
     */
    int 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;
    }

    /**
     * Zero all the values.
     */
    void clear()
    {
        memset(this, 0, sizeof(*this));
    }
};

You'd use it like this, perhaps in 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();

There's lots of ways to get the ip and port, but getaddrinfo does a decent job on Windows:

int dns_lookup(const char *host, sockaddr_in *out)
{
    struct addrinfo *result;
    int ret = getaddrinfo(host, "ntp", NULL, &result);
    for (struct addrinfo *p = result; p; p = p->ai_next)
    {
        if (p->ai_family != AF_INET)
            continue;

        memcpy(out, p->ai_addr, sizeof(*out));
    }
    freeaddrinfo(result);
}

Naturally, this solution is about as portable as a beached whale, but it Works For Me™.

alficles
  • 1,471
  • 1
  • 15
  • 24
  • You can use [`ntohl`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms740069(v=vs.85).aspx) to do the little-endian conversion, it's portable. – Mark Ransom Nov 27 '12 at 18:13
  • 1
    True, though that wouldn't solve the portability concerns about the size of unsigned int or the precise nature of the bit-field or padding. Still, probably good enough. At the end of the day, casting an object to a char* and writing over it is gonna be a little "special". :) (The struct ordering might be more specified than I remember, but I think it's allowed to pad or reorder the bit-fields.) – alficles Nov 27 '12 at 19:21
  • 1
    @alficles Is there an updated version of this code? I'm trying to resolve an issue with `rec()` not working in another question I opened up: http://stackoverflow.com/questions/20804145/basic-ntp-client-in-windows-in-visual-c – Cloud Dec 27 '13 at 20:48
0

Sockets in C/++ are somewhat less pleasant than in most other languages, especially interpreted and the newer compiled ones.

Why not use the official implementation from

Of course, NTP requires MinGW, but there are some libraries out there.

Windows also has NTP built-in since the server 2000 version. I can't verify it, but it should work at least on systems since Vista.

dualed
  • 10,262
  • 1
  • 26
  • 29
  • I need to use the ntp.belnet.be server. So i need to apply that to my code. The problem i'm having is reading the pakket that i receive from the server. – user1765216 Oct 22 '12 at 12:58
  • @user1765216 Unless you think you can do it better than the official NTP implementation, I see no sense in re-implementing something that already exists. NTP usually has no preset server whatsoever, so using your belnet server should not be an issue. – dualed Oct 22 '12 at 13:03
  • I still don't get it. How can i use this to create a c++ commandline program that just gives me the time and date of the server?? – user1765216 Oct 22 '12 at 13:16
  • 1
    @user1765216 I updated the answer. Why didn't you just write from the beginning this is what you wanted... – dualed Oct 22 '12 at 14:23
  • The thing i want to do is make a c++ commandline program working with sockets. I need to make a connection with sntp to the ntp.belnet.be server so i can receive a package from the server that gives me the time and date. I thought it was clear from the question? – user1765216 Oct 22 '12 at 14:56
  • @user1765216 No, it is not clear. From your question title I assume that your focus lies in getting the time and date, not in writing the application. So I post you links to the source code of ntp and a library which implements it. (Just for the record: You could have looked at the source code of either and taken what you needed to write your program) Then you say you only want a commandline program that prints the time and date, so I add a link to the w32tm docs. Now I think you only want us to write your homework. – dualed Oct 22 '12 at 21:13