ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

ntpclient.c
Go to the documentation of this file.
00001 /*
00002  * PROJECT:     ReactOS Timedate Control Panel
00003  * LICENSE:     GPL - See COPYING in the top level directory
00004  * FILE:        dll/cpl/timedate/ntpclient.c
00005  * PURPOSE:     Queries the NTP server
00006  * COPYRIGHT:   Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
00007  *
00008  */
00009 
00010 #include <timedate.h>
00011 
00012 #define TIMEOUT 4000 /* 4 second timeout */
00013 
00014 typedef struct _INFO
00015 {
00016     SOCKET Sock;
00017     SOCKADDR_IN myAddr;
00018     SOCKADDR_IN ntpAddr;
00019     NTPPACKET SendPacket;
00020     NTPPACKET RecvPacket;
00021 } INFO, *PINFO;
00022 
00023 
00024 static BOOL
00025 InitConnection(PINFO pInfo,
00026                LPSTR lpAddress)
00027 {
00028     WSADATA wsaData;
00029     HOSTENT *he;
00030     INT Ret;
00031 
00032     Ret = WSAStartup(MAKEWORD(2, 2),
00033                      &wsaData);
00034     if (Ret != 0)
00035         return FALSE;
00036 
00037     pInfo->Sock = socket(AF_INET,
00038                          SOCK_DGRAM,
00039                          0);
00040     if (pInfo->Sock == INVALID_SOCKET)
00041         return FALSE;
00042 
00043     /* Setup server info */
00044     he = gethostbyname(lpAddress);
00045     if (he != NULL)
00046     {
00047         /* Setup server socket info */
00048         ZeroMemory(&pInfo->ntpAddr, sizeof(SOCKADDR_IN));
00049         pInfo->ntpAddr.sin_family = AF_INET; // he->h_addrtype;
00050         pInfo->ntpAddr.sin_port = htons(NTPPORT);
00051         pInfo->ntpAddr.sin_addr = *((struct in_addr *)he->h_addr);
00052     }
00053     else
00054         return FALSE;
00055 
00056     return TRUE;
00057 }
00058 
00059 
00060 static VOID
00061 DestroyConnection(VOID)
00062 {
00063     WSACleanup();
00064 }
00065 
00066 
00067 static BOOL
00068 GetTransmitTime(PTIMEPACKET ptp)
00069 {
00070     return TRUE;
00071 }
00072 
00073 
00074 /* Send some data to wake the server up */
00075 static BOOL
00076 SendData(PINFO pInfo)
00077 {
00078     TIMEPACKET tp = { 0, 0 };
00079     INT Ret;
00080 
00081     ZeroMemory(&pInfo->SendPacket, sizeof(pInfo->SendPacket));
00082     pInfo->SendPacket.LiVnMode = 27;
00083     if (!GetTransmitTime(&tp))
00084         return FALSE;
00085     pInfo->SendPacket.TransmitTimestamp = tp;
00086 
00087     Ret = sendto(pInfo->Sock,
00088                  (char *)&pInfo->SendPacket,
00089                  sizeof(pInfo->SendPacket),
00090                  0,
00091                  (SOCKADDR *)&pInfo->ntpAddr,
00092                  sizeof(SOCKADDR_IN));
00093 
00094     if (Ret == SOCKET_ERROR)
00095         return FALSE;
00096 
00097     return TRUE;
00098 }
00099 
00100 
00101 static ULONG
00102 RecieveData(PINFO pInfo)
00103 {
00104     TIMEVAL timeVal;
00105     FD_SET readFDS;
00106     INT Ret;
00107     ULONG ulTime = 0;
00108 
00109     /* Monitor socket for incomming connections */
00110     FD_ZERO(&readFDS);
00111     FD_SET(pInfo->Sock, &readFDS);
00112 
00113     /* Set timeout values */
00114     timeVal.tv_sec  = TIMEOUT / 1000;
00115     timeVal.tv_usec = TIMEOUT % 1000;
00116 
00117     /* Check for data on the socket for TIMEOUT millisecs */
00118     Ret = select(0, &readFDS, NULL, NULL, &timeVal);
00119 
00120     if ((Ret != SOCKET_ERROR) && (Ret != 0))
00121     {
00122 
00123         Ret = recvfrom(pInfo->Sock,
00124                        (char *)&pInfo->RecvPacket,
00125                        sizeof(pInfo->RecvPacket),
00126                        0,
00127                        NULL,
00128                        NULL);
00129         if (Ret != SOCKET_ERROR)
00130             ulTime = ntohl(ulTime);
00131     }
00132 
00133     return ulTime;
00134 }
00135 
00136 
00137 ULONG
00138 GetServerTime(LPWSTR lpAddress)
00139 {
00140     PINFO pInfo;
00141     LPSTR lpAddr;
00142     DWORD dwSize = wcslen(lpAddress) + 1;
00143     ULONG ulTime = 0;
00144 
00145     pInfo = (PINFO)HeapAlloc(GetProcessHeap(),
00146                              0,
00147                              sizeof(INFO));
00148     lpAddr = (LPSTR)HeapAlloc(GetProcessHeap(),
00149                               0,
00150                               dwSize);
00151 
00152     if (pInfo && lpAddr)
00153     {
00154         if (WideCharToMultiByte(CP_ACP,
00155                                 0,
00156                                 lpAddress,
00157                                 -1,
00158                                 lpAddr,
00159                                 dwSize,
00160                                 NULL,
00161                                 NULL))
00162         {
00163             if (InitConnection(pInfo, lpAddr))
00164             {
00165                 if (SendData(pInfo))
00166                 {
00167                     ulTime = RecieveData(pInfo);
00168                 }
00169             }
00170 
00171             DestroyConnection();
00172         }
00173     }
00174 
00175     if (pInfo)
00176         HeapFree(GetProcessHeap(), 0, pInfo);
00177     if (lpAddr)
00178         HeapFree(GetProcessHeap(), 0, lpAddr);
00179 
00180     return ulTime;
00181 }

Generated on Mon May 28 2012 04:20:39 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.