mirror of
https://github.com/reactos/reactos.git
synced 2024-12-28 18:15:11 +00:00
- correctly get local NTP server info
- fix some bugs in the socket code svn path=/trunk/; revision=26598
This commit is contained in:
parent
710ef6922b
commit
69bd4cc6a6
5 changed files with 144 additions and 91 deletions
|
@ -13,7 +13,7 @@
|
|||
|
||||
static WNDPROC pOldWndProc = NULL;
|
||||
|
||||
static BOOL
|
||||
BOOL
|
||||
SystemSetLocalTime(LPSYSTEMTIME lpSystemTime)
|
||||
{
|
||||
HANDLE hToken;
|
||||
|
|
|
@ -136,15 +136,13 @@ SetNTPServer(HWND hwnd)
|
|||
|
||||
/* get the domain name from the registry */
|
||||
static BOOL
|
||||
GetNTPServerAddress(LPSTR* lpAddress)
|
||||
GetNTPServerAddress(LPWSTR* lpAddress)
|
||||
{
|
||||
HKEY hKey;
|
||||
WCHAR szSel[4];
|
||||
LPWSTR buf = NULL;
|
||||
DWORD dwSize;
|
||||
LONG Ret;
|
||||
|
||||
*lpAddress = NULL;
|
||||
Ret = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
|
||||
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
|
||||
0,
|
||||
|
@ -172,14 +170,14 @@ GetNTPServerAddress(LPSTR* lpAddress)
|
|||
szSel,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL, //(LPBYTE)buf
|
||||
NULL,
|
||||
&dwSize);
|
||||
if (Ret == ERROR_MORE_DATA)
|
||||
if (Ret == ERROR_SUCCESS)
|
||||
{
|
||||
buf = (LPWSTR) HeapAlloc(GetProcessHeap(),
|
||||
0,
|
||||
dwSize);
|
||||
if (buf == NULL)
|
||||
(*lpAddress) = (LPWSTR) HeapAlloc(GetProcessHeap(),
|
||||
0,
|
||||
dwSize);
|
||||
if ((*lpAddress) == NULL)
|
||||
{
|
||||
Ret = ERROR_NOT_ENOUGH_MEMORY;
|
||||
goto fail;
|
||||
|
@ -189,7 +187,7 @@ GetNTPServerAddress(LPSTR* lpAddress)
|
|||
szSel,
|
||||
NULL,
|
||||
NULL,
|
||||
(LPBYTE)buf,
|
||||
(LPBYTE)*lpAddress,
|
||||
&dwSize);
|
||||
if (Ret != ERROR_SUCCESS)
|
||||
goto fail;
|
||||
|
@ -198,38 +196,13 @@ GetNTPServerAddress(LPSTR* lpAddress)
|
|||
else
|
||||
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);
|
||||
HeapFree(GetProcessHeap(),
|
||||
0,
|
||||
buf);
|
||||
|
||||
return TRUE;
|
||||
|
||||
fail:
|
||||
DisplayWin32Error(Ret);
|
||||
if (hKey) RegCloseKey(hKey);
|
||||
HeapFree(GetProcessHeap(), 0, buf);
|
||||
HeapFree(GetProcessHeap(), 0, *lpAddress);
|
||||
return FALSE;
|
||||
|
||||
|
@ -240,26 +213,18 @@ fail:
|
|||
static ULONG
|
||||
GetTimeFromServer(VOID)
|
||||
{
|
||||
LPSTR lpAddress = NULL;
|
||||
LPWSTR lpAddress = NULL;
|
||||
ULONG ulTime = 0;
|
||||
|
||||
if (! GetNTPServerAddress(&lpAddress))
|
||||
return 0;
|
||||
|
||||
if (InitializeConnection(lpAddress))
|
||||
if (GetNTPServerAddress(&lpAddress))
|
||||
{
|
||||
if (SendData())
|
||||
{
|
||||
ulTime = RecieveData();
|
||||
}
|
||||
ulTime = GetServerTime(lpAddress);
|
||||
|
||||
HeapFree(GetProcessHeap(),
|
||||
0,
|
||||
lpAddress);
|
||||
}
|
||||
|
||||
DestroyConnection();
|
||||
|
||||
HeapFree(GetProcessHeap(),
|
||||
0,
|
||||
lpAddress);
|
||||
|
||||
return ulTime;
|
||||
}
|
||||
|
||||
|
@ -303,11 +268,7 @@ UpdateSystemTime(ULONG ulTime)
|
|||
return;
|
||||
}
|
||||
|
||||
/*FIXME: Should we be calling SystemSetLocalTime
|
||||
in order to maintain system privldges?
|
||||
I thought SetSystemTime already dealt
|
||||
with this */
|
||||
if (! SetSystemTime(&stNew))
|
||||
if (!SystemSetLocalTime(&stNew))
|
||||
DisplayWin32Error(GetLastError());
|
||||
|
||||
}
|
||||
|
@ -364,7 +325,7 @@ GetSyncSetting(HWND hwnd)
|
|||
|
||||
|
||||
static VOID
|
||||
InitializeDialog(HWND hwnd)
|
||||
OnInitDialog(HWND hwnd)
|
||||
{
|
||||
GetSyncSetting(hwnd);
|
||||
|
||||
|
@ -385,7 +346,7 @@ InetTimePageProc(HWND hwndDlg,
|
|||
{
|
||||
case WM_INITDIALOG:
|
||||
{
|
||||
InitializeDialog(hwndDlg);
|
||||
OnInitDialog(hwndDlg);
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
|
@ -176,7 +176,7 @@ MonthCalUpdate(IN PMONTHCALWND infoPtr)
|
|||
pDayEnd = pDay + MonthLength;
|
||||
while (pDay != pDayEnd)
|
||||
{
|
||||
*(pDay++) = ++d;
|
||||
*(pDay++) = (BYTE)++d;
|
||||
}
|
||||
|
||||
/* repaint the control */
|
||||
|
|
|
@ -7,15 +7,23 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include <timedate.h>
|
||||
#include "timedate.h"
|
||||
|
||||
#define TIMEOUT 4000 /* 4 second timeout */
|
||||
|
||||
SOCKET Sock;
|
||||
SOCKADDR_IN myAddr, ntpAddr;
|
||||
typedef struct _INFO
|
||||
{
|
||||
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;
|
||||
HOSTENT *he;
|
||||
|
@ -26,10 +34,10 @@ InitializeConnection(LPSTR lpAddress)
|
|||
if (Ret != 0)
|
||||
return FALSE;
|
||||
|
||||
Sock = socket(AF_INET,
|
||||
SOCK_DGRAM,
|
||||
0);
|
||||
if (Sock == INVALID_SOCKET)
|
||||
pInfo->Sock = socket(AF_INET,
|
||||
SOCK_DGRAM,
|
||||
0);
|
||||
if (pInfo->Sock == INVALID_SOCKET)
|
||||
return FALSE;
|
||||
|
||||
/* setup server info */
|
||||
|
@ -37,10 +45,10 @@ InitializeConnection(LPSTR lpAddress)
|
|||
if (he != NULL)
|
||||
{
|
||||
/* setup server socket info */
|
||||
ZeroMemory(&ntpAddr, sizeof(SOCKADDR_IN));
|
||||
ntpAddr.sin_family = AF_INET; //he->h_addrtype;
|
||||
ntpAddr.sin_port = htons(NTPPORT);
|
||||
ntpAddr.sin_addr = *((struct in_addr *)he->h_addr);
|
||||
ZeroMemory(&pInfo->ntpAddr, sizeof(SOCKADDR_IN));
|
||||
pInfo->ntpAddr.sin_family = AF_INET; //he->h_addrtype;
|
||||
pInfo->ntpAddr.sin_port = htons(NTPPORT);
|
||||
pInfo->ntpAddr.sin_addr = *((struct in_addr *)he->h_addr);
|
||||
}
|
||||
else
|
||||
return FALSE;
|
||||
|
@ -48,24 +56,40 @@ InitializeConnection(LPSTR lpAddress)
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
VOID
|
||||
|
||||
static VOID
|
||||
DestroyConnection()
|
||||
{
|
||||
WSACleanup();
|
||||
}
|
||||
|
||||
/* send some data to wake the server up */
|
||||
BOOL
|
||||
SendData()
|
||||
|
||||
static BOOL
|
||||
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;
|
||||
|
||||
Ret = sendto(Sock,
|
||||
Packet,
|
||||
sizeof(Packet),
|
||||
ZeroMemory(&pInfo->SendPacket, sizeof(pInfo->SendPacket));
|
||||
pInfo->SendPacket.LiVnMode = 27;
|
||||
if (!GetTransmitTime(&tp))
|
||||
return FALSE;
|
||||
pInfo->SendPacket.TransmitTimestamp = tp;
|
||||
|
||||
Ret = sendto(pInfo->Sock,
|
||||
(char *)&pInfo->SendPacket,
|
||||
sizeof(pInfo->SendPacket),
|
||||
0,
|
||||
(SOCKADDR *)&ntpAddr,
|
||||
(SOCKADDR *)&pInfo->ntpAddr,
|
||||
sizeof(SOCKADDR_IN));
|
||||
|
||||
if (Ret == SOCKET_ERROR)
|
||||
|
@ -75,8 +99,8 @@ SendData()
|
|||
}
|
||||
|
||||
|
||||
ULONG
|
||||
RecieveData(VOID)
|
||||
static ULONG
|
||||
RecieveData(PINFO pInfo)
|
||||
{
|
||||
TIMEVAL timeVal;
|
||||
FD_SET readFDS;
|
||||
|
@ -85,7 +109,7 @@ RecieveData(VOID)
|
|||
|
||||
/* monitor socket for incomming connections */
|
||||
FD_ZERO(&readFDS);
|
||||
FD_SET(Sock, &readFDS);
|
||||
FD_SET(pInfo->Sock, &readFDS);
|
||||
|
||||
/* set timeout values */
|
||||
timeVal.tv_sec = TIMEOUT / 1000;
|
||||
|
@ -96,9 +120,10 @@ RecieveData(VOID)
|
|||
|
||||
if ((Ret != SOCKET_ERROR) && (Ret != 0))
|
||||
{
|
||||
Ret = recvfrom(Sock,
|
||||
(char *)&ulTime,
|
||||
4,
|
||||
|
||||
Ret = recvfrom(pInfo->Sock,
|
||||
(char *)&pInfo->RecvPacket,
|
||||
sizeof(pInfo->RecvPacket),
|
||||
0,
|
||||
NULL,
|
||||
NULL);
|
||||
|
@ -108,3 +133,49 @@ RecieveData(VOID)
|
|||
|
||||
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;
|
||||
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ extern HINSTANCE hApplet;
|
|||
|
||||
/* dateandtime.c */
|
||||
INT_PTR CALLBACK DateTimePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
|
||||
BOOL SystemSetLocalTime(LPSYSTEMTIME lpSystemTime);
|
||||
|
||||
|
||||
/* timezone.c */
|
||||
|
@ -56,10 +57,30 @@ VOID UnregisterClockControl(VOID);
|
|||
|
||||
|
||||
/* ntpclient.c */
|
||||
BOOL InitializeConnection(CHAR *szIpAddr);
|
||||
VOID DestroyConnection(VOID);
|
||||
BOOL SendData(VOID);
|
||||
ULONG RecieveData(VOID);
|
||||
// NTP timestamp
|
||||
typedef struct _TIMEPACKET
|
||||
{
|
||||
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 */
|
||||
|
|
Loading…
Reference in a new issue