2 * PROJECT: ReactOS Timedate Control Panel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/cpl/timedate/ntpclient.c
5 * PURPOSE: Queries the NTP server
6 * COPYRIGHT: Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
12 #define TIMEOUT 4000 /* 4 second timeout */
25 InitConnection(PINFO pInfo
,
32 Ret
= WSAStartup(MAKEWORD(2, 2),
37 pInfo
->Sock
= socket(AF_INET
,
40 if (pInfo
->Sock
== INVALID_SOCKET
)
43 /* Setup server info */
44 he
= gethostbyname(lpAddress
);
47 /* Setup server socket info */
48 ZeroMemory(&pInfo
->ntpAddr
, sizeof(SOCKADDR_IN
));
49 pInfo
->ntpAddr
.sin_family
= AF_INET
; // he->h_addrtype;
50 pInfo
->ntpAddr
.sin_port
= htons(NTPPORT
);
51 pInfo
->ntpAddr
.sin_addr
= *((struct in_addr
*)he
->h_addr
);
61 DestroyConnection(VOID
)
68 GetTransmitTime(PTIMEPACKET ptp
)
74 /* Send some data to wake the server up */
78 TIMEPACKET tp
= { 0, 0 };
81 ZeroMemory(&pInfo
->SendPacket
, sizeof(pInfo
->SendPacket
));
82 pInfo
->SendPacket
.LiVnMode
= 27;
83 if (!GetTransmitTime(&tp
))
85 pInfo
->SendPacket
.TransmitTimestamp
= tp
;
87 Ret
= sendto(pInfo
->Sock
,
88 (char *)&pInfo
->SendPacket
,
89 sizeof(pInfo
->SendPacket
),
91 (SOCKADDR
*)&pInfo
->ntpAddr
,
94 if (Ret
== SOCKET_ERROR
)
102 ReceiveData(PINFO pInfo
)
109 /* Monitor socket for incomming connections */
111 FD_SET(pInfo
->Sock
, &readFDS
);
113 /* Set timeout values */
114 timeVal
.tv_sec
= TIMEOUT
/ 1000;
115 timeVal
.tv_usec
= TIMEOUT
% 1000;
117 /* Check for data on the socket for TIMEOUT millisecs */
118 Ret
= select(0, &readFDS
, NULL
, NULL
, &timeVal
);
120 if ((Ret
!= SOCKET_ERROR
) && (Ret
!= 0))
123 Ret
= recvfrom(pInfo
->Sock
,
124 (char *)&pInfo
->RecvPacket
,
125 sizeof(pInfo
->RecvPacket
),
129 if (Ret
!= SOCKET_ERROR
)
130 ulTime
= ntohl(ulTime
);
138 GetServerTime(LPWSTR lpAddress
)
142 DWORD dwSize
= wcslen(lpAddress
) + 1;
145 pInfo
= (PINFO
)HeapAlloc(GetProcessHeap(),
148 lpAddr
= (LPSTR
)HeapAlloc(GetProcessHeap(),
154 if (WideCharToMultiByte(CP_ACP
,
163 if (InitConnection(pInfo
, lpAddr
))
167 ulTime
= ReceiveData(pInfo
);
176 HeapFree(GetProcessHeap(), 0, pInfo
);
178 HeapFree(GetProcessHeap(), 0, lpAddr
);