// sntpsync console version, by rojer, 2002 #include "stdafx.h" #include #include #include int main(int argc, char* argv[]) { WORD wVersionRequested = MAKEWORD(2, 2); WSADATA wsaData; int err; SOCKET s; char *ntps; struct sockaddr_in sa; int len; SYSTEMTIME new_time = {0, 0, 0, 0, 0, 0, 0, 0}; DWORD sec1900; FILETIME ft; ULONGLONG nanosec1900; ULARGE_INTEGER ftlint; fd_set sset = { 1 }; int timeout = 3; TIMEVAL timeout_tv = {timeout, 0}; hostent *h; struct { DWORD bf; DWORD root_delay; DWORD root_dispersion; DWORD ref_id; DWORD ref_tstamp_hi; DWORD ref_tstamp_lo; DWORD orig_tstamp_hi; DWORD orig_tstamp_lo; DWORD rcv_tstamp_hi; DWORD rcv_tstamp_lo; DWORD trans_tstamp_hi; DWORD trans_tstamp_lo; } ntp_message; printf("\nsntpsync - simple ntp (sntp, rfc1769) time synchronizer.\n"); if (argc < 2 || argc > 3) { printf("usage: sntpsync server [timeout]\ndefault timeout is 3 seconds.\n"); }; printf("(c) rojer, 2002\n\n"); if (argc < 2 || argc > 3) return 1; ntps = argv[1]; if (argc == 3) timeout = atoi(argv[2]); if (timeout > 0) timeout_tv.tv_sec = timeout; if (err = WSAStartup(wVersionRequested, &wsaData)) { printf("Winsock init error: %d\n", err); return 1; } if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) { printf("Socket error: %d\n", WSAGetLastError()); return 1; } sa.sin_family = AF_INET; sa.sin_port = htons(123); if ((sa.sin_addr.S_un.S_addr = inet_addr(ntps)) == INADDR_NONE) { if ((h = gethostbyname(ntps)) == NULL) { printf("gethostbyname: could not resolve %s: %d\n", ntps, WSAGetLastError()); return 1; } sa.sin_addr.S_un.S_addr = *((DWORD *)h->h_addr); } /* rfc1769 recommends these values for simple queries */ ntp_message.bf = htonl(0x0b000000); ntp_message.root_delay = 0; ntp_message.root_dispersion = 0; ntp_message.ref_id = 0; ntp_message.ref_tstamp_hi = 0; ntp_message.ref_tstamp_lo = 0; ntp_message.orig_tstamp_hi = 0; ntp_message.orig_tstamp_lo = 0; ntp_message.rcv_tstamp_hi = 0; ntp_message.rcv_tstamp_lo = 0; ntp_message.trans_tstamp_hi = 0; ntp_message.trans_tstamp_lo = 0; if (sendto(s, (const char *) &ntp_message, sizeof(ntp_message), 0, (struct sockaddr *) &sa, sizeof(sa)) == SOCKET_ERROR) { printf("Socket write error: %d\n", WSAGetLastError()); return 1; }; len = sizeof(sa); sset.fd_array[0] = s; err = select(0, &sset, NULL, NULL, &timeout_tv); if (err == 0) { printf("select: socket read timeout\n"); return 1; } if (err == SOCKET_ERROR) { printf("select: socket error: %d\n", WSAGetLastError()); return 1; } if (recvfrom(s, (char *) &ntp_message, sizeof(ntp_message), 0, (struct sockaddr *) &sa, &len) == SOCKET_ERROR) { printf("Socket read error: %d\n", WSAGetLastError()); return 1; }; sec1900 = ntohl(ntp_message.trans_tstamp_hi); if (((ntohl(ntp_message.bf) >> 30) == 3) || (sec1900 == 0)) { printf("%s: time not synchronized.\n", ntps); return 1; } new_time.wYear = 1900; new_time.wDay = 1; new_time.wMonth = 1; new_time.wHour = new_time.wMinute = new_time.wSecond = new_time.wMilliseconds = 0; SystemTimeToFileTime(&new_time, &ft); nanosec1900 = (ULONGLONG)sec1900 * (ULONGLONG)10000000; ftlint.LowPart = ft.dwLowDateTime; ftlint.HighPart = ft.dwHighDateTime; ftlint.QuadPart += nanosec1900; ft.dwLowDateTime = ftlint.LowPart; ft.dwHighDateTime = ftlint.HighPart; FileTimeToSystemTime(&ft, &new_time); printf("new time: %d/%d/%d %d:%d.%d UTC\n", new_time.wDay, new_time.wMonth, new_time.wYear, new_time.wHour, new_time.wMinute, new_time.wSecond); if (SetSystemTime(&new_time) == 0) { printf("setsystemtime: error %d\n", GetLastError()); return 1; } printf("time adjusted successfully.\n"); return 0; }