ReactOS  0.4.13-dev-479-gec9c8fd
ntpclient.c
Go to the documentation of this file.
1 /*
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>
7  *
8  */
9 
10 #include "timedate.h"
11 
12 #include <winsock2.h>
13 
14 #define TIMEOUT 4000 /* 4 second timeout */
15 
16 typedef struct _INFO
17 {
23 } INFO, *PINFO;
24 
25 
26 static BOOL
28  LPSTR lpAddress)
29 {
30  WSADATA wsaData;
31  HOSTENT *he;
32  INT Ret;
33 
34  Ret = WSAStartup(MAKEWORD(2, 2),
35  &wsaData);
36  if (Ret != 0)
37  return FALSE;
38 
39  pInfo->Sock = socket(AF_INET,
40  SOCK_DGRAM,
41  0);
42  if (pInfo->Sock == INVALID_SOCKET)
43  return FALSE;
44 
45  /* Setup server info */
46  he = gethostbyname(lpAddress);
47  if (he != NULL)
48  {
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);
54  }
55  else
56  return FALSE;
57 
58  return TRUE;
59 }
60 
61 
62 static VOID
64 {
65  WSACleanup();
66 }
67 
68 
69 static BOOL
71 {
72  return TRUE;
73 }
74 
75 
76 /* Send some data to wake the server up */
77 static BOOL
79 {
80  TIMEPACKET tp = { 0, 0 };
81  INT Ret;
82 
83  ZeroMemory(&pInfo->SendPacket, sizeof(pInfo->SendPacket));
84  pInfo->SendPacket.LiVnMode = 27;
85  if (!GetTransmitTime(&tp))
86  return FALSE;
88 
89  Ret = sendto(pInfo->Sock,
90  (char *)&pInfo->SendPacket,
91  sizeof(pInfo->SendPacket),
92  0,
93  (SOCKADDR *)&pInfo->ntpAddr,
94  sizeof(SOCKADDR_IN));
95 
96  if (Ret == SOCKET_ERROR)
97  return FALSE;
98 
99  return TRUE;
100 }
101 
102 
103 static ULONG
105 {
106  TIMEVAL timeVal;
107  FD_SET readFDS;
108  INT Ret;
109  ULONG ulTime = 0;
110 
111  /* Monitor socket for incoming connections */
112  FD_ZERO(&readFDS);
113  FD_SET(pInfo->Sock, &readFDS);
114 
115  /* Set timeout values */
116  timeVal.tv_sec = TIMEOUT / 1000;
117  timeVal.tv_usec = TIMEOUT % 1000;
118 
119  /* Check for data on the socket for TIMEOUT millisecs */
120  Ret = select(0, &readFDS, NULL, NULL, &timeVal);
121 
122  if ((Ret != SOCKET_ERROR) && (Ret != 0))
123  {
124  Ret = recvfrom(pInfo->Sock,
125  (char *)&pInfo->RecvPacket,
126  sizeof(pInfo->RecvPacket),
127  0,
128  NULL,
129  NULL);
130 
131  if (Ret != SOCKET_ERROR)
132  ulTime = ntohl(pInfo->RecvPacket.TransmitTimestamp.dwInteger);
133  }
134 
135  return ulTime;
136 }
137 
138 
139 ULONG
141 {
142  PINFO pInfo;
143  LPSTR lpAddr;
144  DWORD dwSize = wcslen(lpAddress) + 1;
145  ULONG ulTime = 0;
146 
147  pInfo = (PINFO)HeapAlloc(GetProcessHeap(),
148  0,
149  sizeof(INFO));
150  lpAddr = (LPSTR)HeapAlloc(GetProcessHeap(),
151  0,
152  dwSize);
153 
154  if (pInfo && lpAddr)
155  {
157  0,
158  lpAddress,
159  -1,
160  lpAddr,
161  dwSize,
162  NULL,
163  NULL))
164  {
165  if (InitConnection(pInfo, lpAddr))
166  {
167  if (SendData(pInfo))
168  {
169  ulTime = ReceiveData(pInfo);
170  }
171  }
172 
174  }
175  }
176 
177  if (pInfo)
178  HeapFree(GetProcessHeap(), 0, pInfo);
179  if (lpAddr)
180  HeapFree(GetProcessHeap(), 0, lpAddr);
181 
182  return ulTime;
183 }
Definition: winsock.h:66
#define SOCKET_ERROR
Definition: winsock.h:333
INT WSAAPI recvfrom(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags, OUT LPSOCKADDR from, IN OUT INT FAR *fromlen)
Definition: recv.c:87
#define TRUE
Definition: types.h:120
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
BYTE LiVnMode
Definition: timedate.h:81
u_short sin_port
Definition: winsock.h:511
#define WideCharToMultiByte
Definition: compat.h:101
NTPPACKET SendPacket
Definition: ntpclient.c:21
#define MAKEWORD(a, b)
Definition: typedefs.h:247
#define CP_ACP
Definition: compat.h:99
unsigned long tv_sec
Definition: linux.h:1738
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
SOCKADDR_IN myAddr
Definition: ntpclient.c:19
#define ZeroMemory
Definition: winbase.h:1635
_In_ UINT64 _In_ UINT64 _In_ UINT64 _In_opt_ traverse_ptr * tp
Definition: btrfs.c:2694
#define FD_ZERO(set)
Definition: winsock.h:96
char * LPSTR
Definition: xmlstorage.h:182
static BOOL InitConnection(PINFO pInfo, LPSTR lpAddress)
Definition: ntpclient.c:27
#define FD_SET(fd, set)
Definition: winsock.h:89
int32_t INT
Definition: typedefs.h:56
static VOID DestroyConnection(VOID)
Definition: ntpclient.c:63
static ULONG ReceiveData(PINFO pInfo)
Definition: ntpclient.c:104
unsigned int BOOL
Definition: ntddk_ex.h:94
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
smooth NULL
Definition: ftsmooth.c:416
static BOOL GetTransmitTime(PTIMEPACKET ptp)
Definition: ntpclient.c:70
unsigned long tv_usec
Definition: linux.h:1739
#define TIMEOUT
Definition: ntpclient.c:14
PHOSTENT WSAAPI gethostbyname(IN const char FAR *name)
Definition: getxbyxx.c:221
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define INVALID_SOCKET
Definition: winsock.h:332
Definition: tcpip.h:125
unsigned long DWORD
Definition: ntddk_ex.h:95
SOCKADDR_IN ntpAddr
Definition: ntpclient.c:20
TIMEPACKET TransmitTimestamp
Definition: timedate.h:91
#define NTPPORT
Definition: timedate.h:26
struct in_addr sin_addr
Definition: winsock.h:512
ULONG GetServerTime(LPWSTR lpAddress)
Definition: ntpclient.c:140
SOCKET Sock
Definition: ntpclient.c:18
Definition: precomp.h:83
DWORD dwInteger
Definition: timedate.h:74
NTPPACKET RecvPacket
Definition: ntpclient.c:22
INT WSAAPI sendto(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags, IN CONST struct sockaddr *to, IN INT tolen)
Definition: send.c:82
static BOOL SendData(PINFO pInfo)
Definition: ntpclient.c:78
unsigned int ULONG
Definition: retypes.h:1
#define AF_INET
Definition: tcpip.h:117
UINT_PTR SOCKET
Definition: winsock.h:47
struct _INFO * PINFO
struct _INFO INFO
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define htons(x)
Definition: module.h:213
#define SOCK_DGRAM
Definition: winsock.h:336
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
short sin_family
Definition: winsock.h:510
#define HeapFree(x, y, z)
Definition: compat.h:394
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
#define ntohl(x)
Definition: module.h:203