7442b49150f4fba6942724e38247c58d6f9fbf5a
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>
14 #define TIMEOUT 4000 /* 4 second timeout */
27 InitConnection(PINFO pInfo
,
34 Ret
= WSAStartup(MAKEWORD(2, 2),
39 pInfo
->Sock
= socket(AF_INET
,
42 if (pInfo
->Sock
== INVALID_SOCKET
)
45 /* Setup server info */
46 he
= gethostbyname(lpAddress
);
49 /* Setup server socket info */
50 ZeroMemory(&pInfo
->ntpAddr
, sizeof(SOCKADDR_IN
));
51 pInfo
->ntpAddr
.sin_family
= AF_INET
; // he->h_addrtype;
52 pInfo
->ntpAddr
.sin_port
= htons(NTPPORT
);
53 pInfo
->ntpAddr
.sin_addr
= *((struct in_addr
*)he
->h_addr
);
63 DestroyConnection(VOID
)
70 GetTransmitTime(PTIMEPACKET ptp
)
76 /* Send some data to wake the server up */
80 TIMEPACKET tp
= { 0, 0 };
83 ZeroMemory(&pInfo
->SendPacket
, sizeof(pInfo
->SendPacket
));
84 pInfo
->SendPacket
.LiVnMode
= 27;
85 if (!GetTransmitTime(&tp
))
87 pInfo
->SendPacket
.TransmitTimestamp
= tp
;
89 Ret
= sendto(pInfo
->Sock
,
90 (char *)&pInfo
->SendPacket
,
91 sizeof(pInfo
->SendPacket
),
93 (SOCKADDR
*)&pInfo
->ntpAddr
,
96 if (Ret
== SOCKET_ERROR
)
104 ReceiveData(PINFO pInfo
)
111 /* Monitor socket for incoming connections */
113 FD_SET(pInfo
->Sock
, &readFDS
);
115 /* Set timeout values */
116 timeVal
.tv_sec
= TIMEOUT
/ 1000;
117 timeVal
.tv_usec
= TIMEOUT
% 1000;
119 /* Check for data on the socket for TIMEOUT millisecs */
120 Ret
= select(0, &readFDS
, NULL
, NULL
, &timeVal
);
122 if ((Ret
!= SOCKET_ERROR
) && (Ret
!= 0))
125 Ret
= recvfrom(pInfo
->Sock
,
126 (char *)&pInfo
->RecvPacket
,
127 sizeof(pInfo
->RecvPacket
),
131 if (Ret
!= SOCKET_ERROR
)
132 ulTime
= ntohl(ulTime
);
140 GetServerTime(LPWSTR lpAddress
)
144 DWORD dwSize
= wcslen(lpAddress
) + 1;
147 pInfo
= (PINFO
)HeapAlloc(GetProcessHeap(),
150 lpAddr
= (LPSTR
)HeapAlloc(GetProcessHeap(),
156 if (WideCharToMultiByte(CP_ACP
,
165 if (InitConnection(pInfo
, lpAddr
))
169 ulTime
= ReceiveData(pInfo
);
178 HeapFree(GetProcessHeap(), 0, pInfo
);
180 HeapFree(GetProcessHeap(), 0, lpAddr
);