ReactOS 0.4.16-dev-319-g6cf4263
ntpclient.c File Reference
#include "w32time.h"
#include <winsock2.h>
Include dependency graph for ntpclient.c:

Go to the source code of this file.

Classes

struct  _INFO
 

Macros

#define TIMEOUT   4000 /* 4 second timeout */
 

Typedefs

typedef struct _INFO INFO
 
typedef struct _INFOPINFO
 

Functions

static BOOL InitConnection (PINFO pInfo, LPSTR lpAddress)
 
static VOID DestroyConnection (VOID)
 
static BOOL GetTransmitTime (PTIMEPACKET ptp)
 
static BOOL SendData (PINFO pInfo)
 
static ULONG ReceiveData (PINFO pInfo)
 
ULONG GetServerTime (LPWSTR lpAddress)
 

Macro Definition Documentation

◆ TIMEOUT

#define TIMEOUT   4000 /* 4 second timeout */

Definition at line 12 of file ntpclient.c.

Typedef Documentation

◆ INFO

typedef struct _INFO INFO

◆ PINFO

typedef struct _INFO * PINFO

Function Documentation

◆ DestroyConnection()

static VOID DestroyConnection ( VOID  )
static

Definition at line 61 of file ntpclient.c.

62{
63 WSACleanup();
64}
int PASCAL FAR WSACleanup(void)
Definition: startup.c:60

Referenced by GetServerTime().

◆ GetServerTime()

ULONG GetServerTime ( LPWSTR  lpAddress)

Definition at line 138 of file ntpclient.c.

139{
140 PINFO pInfo;
141 LPSTR lpAddr;
142 DWORD dwSize = wcslen(lpAddress) + 1;
143 ULONG ulTime = 0;
144
145 pInfo = (PINFO)HeapAlloc(GetProcessHeap(),
146 0,
147 sizeof(INFO));
148 lpAddr = (LPSTR)HeapAlloc(GetProcessHeap(),
149 0,
150 dwSize);
151
152 if (pInfo && lpAddr)
153 {
155 0,
156 lpAddress,
157 -1,
158 lpAddr,
159 dwSize,
160 NULL,
161 NULL))
162 {
163 if (InitConnection(pInfo, lpAddr))
164 {
165 if (SendData(pInfo))
166 {
167 ulTime = ReceiveData(pInfo);
168 }
169 }
170
172 }
173 }
174
175 if (pInfo)
176 HeapFree(GetProcessHeap(), 0, pInfo);
177 if (lpAddr)
178 HeapFree(GetProcessHeap(), 0, lpAddr);
179
180 return ulTime;
181}
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define CP_ACP
Definition: compat.h:109
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define WideCharToMultiByte
Definition: compat.h:111
unsigned long DWORD
Definition: ntddk_ex.h:95
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
struct _INFO INFO
static BOOL SendData(PINFO pInfo)
Definition: ntpclient.c:76
static VOID DestroyConnection(VOID)
Definition: ntpclient.c:61
struct _INFO * PINFO
static BOOL InitConnection(PINFO pInfo, LPSTR lpAddress)
Definition: ntpclient.c:25
static ULONG ReceiveData(PINFO pInfo)
Definition: ntpclient.c:102
Definition: precomp.h:84
uint32_t ULONG
Definition: typedefs.h:59
char * LPSTR
Definition: xmlstorage.h:182

Referenced by SetTime().

◆ GetTransmitTime()

static BOOL GetTransmitTime ( PTIMEPACKET  ptp)
static

Definition at line 68 of file ntpclient.c.

69{
70 return TRUE;
71}
#define TRUE
Definition: types.h:120

Referenced by SendData().

◆ InitConnection()

static BOOL InitConnection ( PINFO  pInfo,
LPSTR  lpAddress 
)
static

Definition at line 25 of file ntpclient.c.

27{
28 WSADATA wsaData;
29 HOSTENT *he;
30 INT Ret;
31
32 Ret = WSAStartup(MAKEWORD(2, 2),
33 &wsaData);
34 if (Ret != 0)
35 return FALSE;
36
37 pInfo->Sock = socket(AF_INET,
39 0);
40 if (pInfo->Sock == INVALID_SOCKET)
41 return FALSE;
42
43 /* Setup server info */
44 he = gethostbyname(lpAddress);
45 if (he != NULL)
46 {
47 /* Setup server socket info */
48 ZeroMemory(&pInfo->ntpAddr, sizeof(SOCKADDR_IN));
49 pInfo->ntpAddr.sin_family = AF_INET; // he->h_addrtype;
50 pInfo->ntpAddr.sin_port = htons(NTPPORT);
51 pInfo->ntpAddr.sin_addr = *((struct in_addr *)he->h_addr);
52 }
53 else
54 return FALSE;
55
56 return TRUE;
57}
#define FALSE
Definition: types.h:117
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
#define AF_INET
Definition: tcpip.h:117
PHOSTENT WSAAPI gethostbyname(IN const char FAR *name)
Definition: getxbyxx.c:221
#define htons(x)
Definition: module.h:215
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
SOCKADDR_IN ntpAddr
Definition: ntpclient.c:18
SOCKET Sock
Definition: ntpclient.c:16
Definition: tcpip.h:126
struct in_addr sin_addr
Definition: winsock.h:512
short sin_family
Definition: winsock.h:510
u_short sin_port
Definition: winsock.h:511
#define MAKEWORD(a, b)
Definition: typedefs.h:248
int32_t INT
Definition: typedefs.h:58
#define NTPPORT
Definition: w32time.h:18
#define ZeroMemory
Definition: winbase.h:1737
#define INVALID_SOCKET
Definition: winsock.h:332
#define SOCK_DGRAM
Definition: winsock.h:336

Referenced by GetServerTime().

◆ ReceiveData()

static ULONG ReceiveData ( PINFO  pInfo)
static

Definition at line 102 of file ntpclient.c.

103{
104 TIMEVAL timeVal;
105 FD_SET readFDS;
106 INT Ret;
107 ULONG ulTime = 0;
108
109 /* Monitor socket for incoming connections */
110 FD_ZERO(&readFDS);
111 FD_SET(pInfo->Sock, &readFDS);
112
113 /* Set timeout values */
114 timeVal.tv_sec = TIMEOUT / 1000;
115 timeVal.tv_usec = TIMEOUT % 1000;
116
117 /* Check for data on the socket for TIMEOUT millisecs */
118 Ret = select(0, &readFDS, NULL, NULL, &timeVal);
119
120 if ((Ret != SOCKET_ERROR) && (Ret != 0))
121 {
122 Ret = recvfrom(pInfo->Sock,
123 (char *)&pInfo->RecvPacket,
124 sizeof(pInfo->RecvPacket),
125 0,
126 NULL,
127 NULL);
128
129 if (Ret != SOCKET_ERROR)
131 }
132
133 return ulTime;
134}
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
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
#define ntohl(x)
Definition: module.h:205
#define TIMEOUT
Definition: ntpclient.c:12
NTPPACKET RecvPacket
Definition: ntpclient.c:20
TIMEPACKET TransmitTimestamp
Definition: w32time.h:43
DWORD dwInteger
Definition: w32time.h:26
Definition: winsock.h:66
unsigned long tv_sec
Definition: linux.h:1738
unsigned long tv_usec
Definition: linux.h:1739
#define FD_ZERO(set)
Definition: winsock.h:96
#define SOCKET_ERROR
Definition: winsock.h:333
#define FD_SET(fd, set)
Definition: winsock.h:89

Referenced by GetServerTime(), KdVmKdVmExchangeData(), and KdVmSendReceive().

◆ SendData()

static BOOL SendData ( PINFO  pInfo)
static

Definition at line 76 of file ntpclient.c.

77{
78 TIMEPACKET tp = { 0, 0 };
79 INT Ret;
80
81 ZeroMemory(&pInfo->SendPacket, sizeof(pInfo->SendPacket));
82 pInfo->SendPacket.LiVnMode = 0x1b; /* 0x1b = 011 011 - version 3 , mode 3 (client) */
83 if (!GetTransmitTime(&tp))
84 return FALSE;
86
87 Ret = sendto(pInfo->Sock,
88 (char *)&pInfo->SendPacket,
89 sizeof(pInfo->SendPacket),
90 0,
91 (SOCKADDR *)&pInfo->ntpAddr,
92 sizeof(SOCKADDR_IN));
93
94 if (Ret == SOCKET_ERROR)
95 return FALSE;
96
97 return TRUE;
98}
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
_In_ uint64_t _In_ uint64_t _In_ uint64_t _In_opt_ traverse_ptr * tp
Definition: btrfs.c:2996
static BOOL GetTransmitTime(PTIMEPACKET ptp)
Definition: ntpclient.c:68
NTPPACKET SendPacket
Definition: ntpclient.c:19
BYTE LiVnMode
Definition: w32time.h:33

Referenced by GetServerTime(), KdpSendWaitContinue(), and test_IcmpSendEcho().