Author: gedmurphy Date: Mon Apr 30 23:05:04 2007 New Revision: 26598
URL: http://svn.reactos.org/svn/reactos?rev=26598&view=rev Log: - correctly get local NTP server info - fix some bugs in the socket code
Modified: trunk/reactos/dll/cpl/timedate/dateandtime.c trunk/reactos/dll/cpl/timedate/internettime.c trunk/reactos/dll/cpl/timedate/monthcal.c trunk/reactos/dll/cpl/timedate/ntpclient.c trunk/reactos/dll/cpl/timedate/timedate.h
Modified: trunk/reactos/dll/cpl/timedate/dateandtime.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/cpl/timedate/dateandtim... ============================================================================== --- trunk/reactos/dll/cpl/timedate/dateandtime.c (original) +++ trunk/reactos/dll/cpl/timedate/dateandtime.c Mon Apr 30 23:05:04 2007 @@ -13,7 +13,7 @@
static WNDPROC pOldWndProc = NULL;
-static BOOL +BOOL SystemSetLocalTime(LPSYSTEMTIME lpSystemTime) { HANDLE hToken;
Modified: trunk/reactos/dll/cpl/timedate/internettime.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/cpl/timedate/internetti... ============================================================================== --- trunk/reactos/dll/cpl/timedate/internettime.c (original) +++ trunk/reactos/dll/cpl/timedate/internettime.c Mon Apr 30 23:05:04 2007 @@ -136,15 +136,13 @@
/* 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 @@ szSel, NULL, NULL, - NULL, //(LPBYTE)buf + NULL, &dwSize); - if (Ret == ERROR_MORE_DATA) - { - buf = (LPWSTR) HeapAlloc(GetProcessHeap(), - 0, - dwSize); - if (buf == NULL) + if (Ret == ERROR_SUCCESS) + { + (*lpAddress) = (LPWSTR) HeapAlloc(GetProcessHeap(), + 0, + dwSize); + if ((*lpAddress) == NULL) { Ret = ERROR_NOT_ENOUGH_MEMORY; goto fail; @@ -189,7 +187,7 @@ szSel, NULL, NULL, - (LPBYTE)buf, + (LPBYTE)*lpAddress, &dwSize); if (Ret != ERROR_SUCCESS) goto fail; @@ -198,38 +196,13 @@ 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,25 +213,17 @@ static ULONG GetTimeFromServer(VOID) { - LPSTR lpAddress = NULL; + LPWSTR lpAddress = NULL; ULONG ulTime = 0;
- if (! GetNTPServerAddress(&lpAddress)) - return 0; - - if (InitializeConnection(lpAddress)) - { - if (SendData()) - { - ulTime = RecieveData(); - } - } - - DestroyConnection(); - - HeapFree(GetProcessHeap(), - 0, - lpAddress); + if (GetNTPServerAddress(&lpAddress)) + { + ulTime = GetServerTime(lpAddress); + + HeapFree(GetProcessHeap(), + 0, + lpAddress); + }
return ulTime; } @@ -303,11 +268,7 @@ 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 @@
static VOID -InitializeDialog(HWND hwnd) +OnInitDialog(HWND hwnd) { GetSyncSetting(hwnd);
@@ -385,7 +346,7 @@ { case WM_INITDIALOG: { - InitializeDialog(hwndDlg); + OnInitDialog(hwndDlg); } break;
Modified: trunk/reactos/dll/cpl/timedate/monthcal.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/cpl/timedate/monthcal.c... ============================================================================== --- trunk/reactos/dll/cpl/timedate/monthcal.c (original) +++ trunk/reactos/dll/cpl/timedate/monthcal.c Mon Apr 30 23:05:04 2007 @@ -176,7 +176,7 @@ pDayEnd = pDay + MonthLength; while (pDay != pDayEnd) { - *(pDay++) = ++d; + *(pDay++) = (BYTE)++d; }
/* repaint the control */
Modified: trunk/reactos/dll/cpl/timedate/ntpclient.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/cpl/timedate/ntpclient.... ============================================================================== --- trunk/reactos/dll/cpl/timedate/ntpclient.c (original) +++ trunk/reactos/dll/cpl/timedate/ntpclient.c Mon Apr 30 23:05:04 2007 @@ -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 @@ 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 @@ 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 @@ return TRUE; }
-VOID + +static VOID DestroyConnection() { WSACleanup(); }
+ +static BOOL +GetTransmitTime(PTIMEPACKET ptp) +{ + + return TRUE; +} + + /* send some data to wake the server up */ -BOOL -SendData() +static BOOL +SendData(PINFO pInfo) { - CHAR Packet[] = ""; + 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 @@ }
-ULONG -RecieveData(VOID) +static ULONG +RecieveData(PINFO pInfo) { TIMEVAL timeVal; FD_SET readFDS; @@ -85,7 +109,7 @@
/* 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 @@
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 @@
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; + +}
Modified: trunk/reactos/dll/cpl/timedate/timedate.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/cpl/timedate/timedate.h... ============================================================================== --- trunk/reactos/dll/cpl/timedate/timedate.h (original) +++ trunk/reactos/dll/cpl/timedate/timedate.h Mon Apr 30 23:05:04 2007 @@ -31,6 +31,7 @@
/* dateandtime.c */ INT_PTR CALLBACK DateTimePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); +BOOL SystemSetLocalTime(LPSYSTEMTIME lpSystemTime);
/* timezone.c */ @@ -56,10 +57,30 @@
/* 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 */