- correctly get local NTP server info

- fix some bugs in the socket code

svn path=/trunk/; revision=26598
This commit is contained in:
Ged Murphy 2007-04-30 19:05:04 +00:00
parent 710ef6922b
commit 69bd4cc6a6
5 changed files with 144 additions and 91 deletions

View file

@ -13,7 +13,7 @@
static WNDPROC pOldWndProc = NULL; static WNDPROC pOldWndProc = NULL;
static BOOL BOOL
SystemSetLocalTime(LPSYSTEMTIME lpSystemTime) SystemSetLocalTime(LPSYSTEMTIME lpSystemTime)
{ {
HANDLE hToken; HANDLE hToken;

View file

@ -136,15 +136,13 @@ SetNTPServer(HWND hwnd)
/* get the domain name from the registry */ /* get the domain name from the registry */
static BOOL static BOOL
GetNTPServerAddress(LPSTR* lpAddress) GetNTPServerAddress(LPWSTR* lpAddress)
{ {
HKEY hKey; HKEY hKey;
WCHAR szSel[4]; WCHAR szSel[4];
LPWSTR buf = NULL;
DWORD dwSize; DWORD dwSize;
LONG Ret; LONG Ret;
*lpAddress = NULL;
Ret = RegOpenKeyExW(HKEY_LOCAL_MACHINE, Ret = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers", L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
0, 0,
@ -172,14 +170,14 @@ GetNTPServerAddress(LPSTR* lpAddress)
szSel, szSel,
NULL, NULL,
NULL, NULL,
NULL, //(LPBYTE)buf NULL,
&dwSize); &dwSize);
if (Ret == ERROR_MORE_DATA) if (Ret == ERROR_SUCCESS)
{ {
buf = (LPWSTR) HeapAlloc(GetProcessHeap(), (*lpAddress) = (LPWSTR) HeapAlloc(GetProcessHeap(),
0, 0,
dwSize); dwSize);
if (buf == NULL) if ((*lpAddress) == NULL)
{ {
Ret = ERROR_NOT_ENOUGH_MEMORY; Ret = ERROR_NOT_ENOUGH_MEMORY;
goto fail; goto fail;
@ -189,7 +187,7 @@ GetNTPServerAddress(LPSTR* lpAddress)
szSel, szSel,
NULL, NULL,
NULL, NULL,
(LPBYTE)buf, (LPBYTE)*lpAddress,
&dwSize); &dwSize);
if (Ret != ERROR_SUCCESS) if (Ret != ERROR_SUCCESS)
goto fail; goto fail;
@ -198,38 +196,13 @@ GetNTPServerAddress(LPSTR* lpAddress)
else else
goto fail; goto fail;
/* We still allocate same amount of space for ASCII storage,
* as some chars may use several bytes */
*lpAddress = (LPSTR) HeapAlloc(GetProcessHeap(),
0,
sizeof(dwSize));
if (*lpAddress == NULL)
goto fail;
if (! WideCharToMultiByte(CP_ACP,
0,
buf,
sizeof(dwSize),
*lpAddress,
sizeof(dwSize),
NULL,
NULL))
{
Ret = GetLastError();
goto fail;
}
RegCloseKey(hKey); RegCloseKey(hKey);
HeapFree(GetProcessHeap(),
0,
buf);
return TRUE; return TRUE;
fail: fail:
DisplayWin32Error(Ret); DisplayWin32Error(Ret);
if (hKey) RegCloseKey(hKey); if (hKey) RegCloseKey(hKey);
HeapFree(GetProcessHeap(), 0, buf);
HeapFree(GetProcessHeap(), 0, *lpAddress); HeapFree(GetProcessHeap(), 0, *lpAddress);
return FALSE; return FALSE;
@ -240,26 +213,18 @@ fail:
static ULONG static ULONG
GetTimeFromServer(VOID) GetTimeFromServer(VOID)
{ {
LPSTR lpAddress = NULL; LPWSTR lpAddress = NULL;
ULONG ulTime = 0; ULONG ulTime = 0;
if (! GetNTPServerAddress(&lpAddress)) if (GetNTPServerAddress(&lpAddress))
return 0;
if (InitializeConnection(lpAddress))
{ {
if (SendData()) ulTime = GetServerTime(lpAddress);
{
ulTime = RecieveData(); HeapFree(GetProcessHeap(),
} 0,
lpAddress);
} }
DestroyConnection();
HeapFree(GetProcessHeap(),
0,
lpAddress);
return ulTime; return ulTime;
} }
@ -303,11 +268,7 @@ UpdateSystemTime(ULONG ulTime)
return; return;
} }
/*FIXME: Should we be calling SystemSetLocalTime if (!SystemSetLocalTime(&stNew))
in order to maintain system privldges?
I thought SetSystemTime already dealt
with this */
if (! SetSystemTime(&stNew))
DisplayWin32Error(GetLastError()); DisplayWin32Error(GetLastError());
} }
@ -364,7 +325,7 @@ GetSyncSetting(HWND hwnd)
static VOID static VOID
InitializeDialog(HWND hwnd) OnInitDialog(HWND hwnd)
{ {
GetSyncSetting(hwnd); GetSyncSetting(hwnd);
@ -385,7 +346,7 @@ InetTimePageProc(HWND hwndDlg,
{ {
case WM_INITDIALOG: case WM_INITDIALOG:
{ {
InitializeDialog(hwndDlg); OnInitDialog(hwndDlg);
} }
break; break;

View file

@ -176,7 +176,7 @@ MonthCalUpdate(IN PMONTHCALWND infoPtr)
pDayEnd = pDay + MonthLength; pDayEnd = pDay + MonthLength;
while (pDay != pDayEnd) while (pDay != pDayEnd)
{ {
*(pDay++) = ++d; *(pDay++) = (BYTE)++d;
} }
/* repaint the control */ /* repaint the control */

View file

@ -7,15 +7,23 @@
* *
*/ */
#include <timedate.h> #include "timedate.h"
#define TIMEOUT 4000 /* 4 second timeout */ #define TIMEOUT 4000 /* 4 second timeout */
SOCKET Sock; typedef struct _INFO
SOCKADDR_IN myAddr, ntpAddr; {
SOCKET Sock;
SOCKADDR_IN myAddr;
SOCKADDR_IN ntpAddr;
NTPPACKET SendPacket;
NTPPACKET RecvPacket;
} INFO, *PINFO;
BOOL
InitializeConnection(LPSTR lpAddress) static BOOL
InitConnection(PINFO pInfo,
LPSTR lpAddress)
{ {
WSADATA wsaData; WSADATA wsaData;
HOSTENT *he; HOSTENT *he;
@ -26,10 +34,10 @@ InitializeConnection(LPSTR lpAddress)
if (Ret != 0) if (Ret != 0)
return FALSE; return FALSE;
Sock = socket(AF_INET, pInfo->Sock = socket(AF_INET,
SOCK_DGRAM, SOCK_DGRAM,
0); 0);
if (Sock == INVALID_SOCKET) if (pInfo->Sock == INVALID_SOCKET)
return FALSE; return FALSE;
/* setup server info */ /* setup server info */
@ -37,10 +45,10 @@ InitializeConnection(LPSTR lpAddress)
if (he != NULL) if (he != NULL)
{ {
/* setup server socket info */ /* setup server socket info */
ZeroMemory(&ntpAddr, sizeof(SOCKADDR_IN)); ZeroMemory(&pInfo->ntpAddr, sizeof(SOCKADDR_IN));
ntpAddr.sin_family = AF_INET; //he->h_addrtype; pInfo->ntpAddr.sin_family = AF_INET; //he->h_addrtype;
ntpAddr.sin_port = htons(NTPPORT); pInfo->ntpAddr.sin_port = htons(NTPPORT);
ntpAddr.sin_addr = *((struct in_addr *)he->h_addr); pInfo->ntpAddr.sin_addr = *((struct in_addr *)he->h_addr);
} }
else else
return FALSE; return FALSE;
@ -48,24 +56,40 @@ InitializeConnection(LPSTR lpAddress)
return TRUE; return TRUE;
} }
VOID
static VOID
DestroyConnection() DestroyConnection()
{ {
WSACleanup(); WSACleanup();
} }
/* send some data to wake the server up */
BOOL static BOOL
SendData() GetTransmitTime(PTIMEPACKET ptp)
{ {
CHAR Packet[] = "";
return TRUE;
}
/* send some data to wake the server up */
static BOOL
SendData(PINFO pInfo)
{
TIMEPACKET tp = {0,};
INT Ret; INT Ret;
Ret = sendto(Sock, ZeroMemory(&pInfo->SendPacket, sizeof(pInfo->SendPacket));
Packet, pInfo->SendPacket.LiVnMode = 27;
sizeof(Packet), if (!GetTransmitTime(&tp))
return FALSE;
pInfo->SendPacket.TransmitTimestamp = tp;
Ret = sendto(pInfo->Sock,
(char *)&pInfo->SendPacket,
sizeof(pInfo->SendPacket),
0, 0,
(SOCKADDR *)&ntpAddr, (SOCKADDR *)&pInfo->ntpAddr,
sizeof(SOCKADDR_IN)); sizeof(SOCKADDR_IN));
if (Ret == SOCKET_ERROR) if (Ret == SOCKET_ERROR)
@ -75,8 +99,8 @@ SendData()
} }
ULONG static ULONG
RecieveData(VOID) RecieveData(PINFO pInfo)
{ {
TIMEVAL timeVal; TIMEVAL timeVal;
FD_SET readFDS; FD_SET readFDS;
@ -85,7 +109,7 @@ RecieveData(VOID)
/* monitor socket for incomming connections */ /* monitor socket for incomming connections */
FD_ZERO(&readFDS); FD_ZERO(&readFDS);
FD_SET(Sock, &readFDS); FD_SET(pInfo->Sock, &readFDS);
/* set timeout values */ /* set timeout values */
timeVal.tv_sec = TIMEOUT / 1000; timeVal.tv_sec = TIMEOUT / 1000;
@ -96,9 +120,10 @@ RecieveData(VOID)
if ((Ret != SOCKET_ERROR) && (Ret != 0)) if ((Ret != SOCKET_ERROR) && (Ret != 0))
{ {
Ret = recvfrom(Sock,
(char *)&ulTime, Ret = recvfrom(pInfo->Sock,
4, (char *)&pInfo->RecvPacket,
sizeof(pInfo->RecvPacket),
0, 0,
NULL, NULL,
NULL); NULL);
@ -108,3 +133,49 @@ RecieveData(VOID)
return ulTime; return ulTime;
} }
ULONG
GetServerTime(LPWSTR lpAddress)
{
PINFO pInfo;
LPSTR lpAddr;
DWORD dwSize = wcslen(lpAddress) + 1;
ULONG ulTime = 0;
pInfo = (PINFO)HeapAlloc(GetProcessHeap(),
0,
sizeof(INFO));
lpAddr = (LPSTR)HeapAlloc(GetProcessHeap(),
0,
dwSize);
if (pInfo && lpAddr)
{
if (WideCharToMultiByte(CP_ACP,
0,
lpAddress,
-1,
lpAddr,
dwSize,
NULL,
NULL))
{
if (InitConnection(pInfo, lpAddr))
{
if (SendData(pInfo))
{
ulTime = RecieveData(pInfo);
}
}
DestroyConnection();
}
HeapFree(GetProcessHeap(), 0, pInfo);
HeapFree(GetProcessHeap(), 0, lpAddr);
}
return ulTime;
}

View file

@ -31,6 +31,7 @@ extern HINSTANCE hApplet;
/* dateandtime.c */ /* dateandtime.c */
INT_PTR CALLBACK DateTimePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); INT_PTR CALLBACK DateTimePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL SystemSetLocalTime(LPSYSTEMTIME lpSystemTime);
/* timezone.c */ /* timezone.c */
@ -56,10 +57,30 @@ VOID UnregisterClockControl(VOID);
/* ntpclient.c */ /* ntpclient.c */
BOOL InitializeConnection(CHAR *szIpAddr); // NTP timestamp
VOID DestroyConnection(VOID); typedef struct _TIMEPACKET
BOOL SendData(VOID); {
ULONG RecieveData(VOID); DWORD dwInteger;
DWORD dwFractional;
} TIMEPACKET, *PTIMEPACKET;
// NTP packet
typedef struct _NTPPACKET
{
BYTE LiVnMode;
BYTE Stratum;
char Poll;
char Precision;
long RootDelay;
long RootDispersion;
char ReferenceID[4];
TIMEPACKET ReferenceTimestamp;
TIMEPACKET OriginateTimestamp;
TIMEPACKET ReceiveTimestamp;
TIMEPACKET TransmitTimestamp;
}NTPPACKET, *PNTPPACKET;
ULONG GetServerTime(LPWSTR lpAddress);
/* monthcal.c */ /* monthcal.c */