1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
9 * This example code only builds as-is on Windows.
11 * While Unix/Linux user, you do not need this software.
12 * You can achieve the same result as synctime using curl, awk and date.
13 * Set proxy as according to your network, but beware of proxy Cache-Control.
15 * To set your system clock, root access is required.
16 * # date -s "`curl -sI http://nist.time.gov/timezone.cgi?UTC/s/0 \
17 * | awk -F': ' '/Date: / {print $2}'`"
19 * To view remote webserver date and time.
20 * $ curl -sI http://nist.time.gov/timezone.cgi?UTC/s/0 \
21 * | awk -F': ' '/Date: / {print $2}'
23 * Synchronising your computer clock via Internet time server usually relies
24 * on DAYTIME, TIME, or NTP protocols. These protocols provide good accurate
25 * time synchronisation but it does not work very well through a
26 * firewall/proxy. Some adjustment has to be made to the firewall/proxy for
27 * these protocols to work properly.
29 * There is an indirect method. Since most webserver provide server time in
30 * their HTTP header, therefore you could synchronise your computer clock
31 * using HTTP protocol which has no problem with firewall/proxy.
33 * For this software to work, you should take note of these items.
34 * 1. Your firewall/proxy must allow your computer to surf internet.
35 * 2. Webserver system time must in sync with the NTP time server,
36 * or at least provide an accurate time keeping.
37 * 3. Webserver HTTP header does not provide the milliseconds units,
38 * so there is no way to get very accurate time.
39 * 4. This software could only provide an accuracy of +- a few seconds,
40 * as Round-Trip delay time is not taken into consideration.
41 * Compensation of network, firewall/proxy delay cannot be simply divide
42 * the Round-Trip delay time by half.
43 * 5. Win32 SetSystemTime() API will set your computer clock according to
44 * GMT/UTC time. Therefore your computer timezone must be properly set.
45 * 6. Webserver data should not be cached by the proxy server. Some
46 * webserver provide Cache-Control to prevent caching.
49 * http://tf.nist.gov/timefreq/service/its.htm
50 * http://tf.nist.gov/timefreq/service/firewall.htm
53 * This software will synchronise your computer clock only when you issue
54 * it with --synctime. By default, it only display the webserver's clock.
56 * Written by: Frank (contributed to libcurl)
58 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
59 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
60 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
62 * IN NO EVENT SHALL THE AUTHOR OF THIS SOFTWARE BE LIABLE FOR
63 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
64 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
65 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
66 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
76 #include <curl/curl.h>
79 #define MAX_STRING 256
80 #define MAX_STRING1 MAX_STRING+1
84 char http_proxy[MAX_STRING1];
85 char proxy_user[MAX_STRING1];
86 char timeserver[MAX_STRING1];
89 const char DefaultTimeServer[4][MAX_STRING1] =
91 "http://nist.time.gov/timezone.cgi?UTC/s/0",
92 "http://www.google.com/",
93 "http://www.worldtimeserver.com/current_time_in_UTC.aspx",
94 "http://www.worldtime.com/cgi-bin/wt.cgi"
97 const char *DayStr[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
98 const char *MthStr[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
99 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
104 SYSTEMTIME LOCALTime;
106 #define HTTP_COMMAND_HEAD 0
107 #define HTTP_COMMAND_GET 1
110 size_t SyncTime_CURL_WriteOutput(void *ptr, size_t size, size_t nmemb,
113 fwrite(ptr, size, nmemb, stream);
117 size_t SyncTime_CURL_WriteHeader(void *ptr, size_t size, size_t nmemb,
121 char TmpStr1[26], TmpStr2[26];
123 if (ShowAllHeader == 1)
124 fprintf(stderr, "%s", (char *)(ptr));
126 if (strncmp((char *)(ptr), "Date:", 5) == 0) {
127 if (ShowAllHeader == 0)
128 fprintf(stderr, "HTTP Server. %s", (char *)(ptr));
130 if (AutoSyncTime == 1) {
133 if (strlen((char *)(ptr)) > 50) /* Can prevent buffer overflow to
137 RetVal = sscanf ((char *)(ptr), "Date: %s %d %s %d %d:%d:%d",
138 TmpStr1, &SYSTime.wDay, TmpStr2, &SYSTime.wYear,
139 &SYSTime.wHour, &SYSTime.wMinute, &SYSTime.wSecond);
143 SYSTime.wMilliseconds = 500; /* adjust to midpoint, 0.5 sec */
144 for (i=0; i<12; i++) {
145 if (strcmp(MthStr[i], TmpStr2) == 0) {
146 SYSTime.wMonth = i+1;
150 AutoSyncTime = 3; /* Computer clock will be adjusted */
153 AutoSyncTime = 0; /* Error in sscanf() fields conversion */
159 if (strncmp((char *)(ptr), "X-Cache: HIT", 12) == 0) {
160 fprintf(stderr, "ERROR: HTTP Server data is cached."
161 " Server Date is no longer valid.\n");
167 void SyncTime_CURL_Init(CURL *curl, char *proxy_port,
168 char *proxy_user_password)
170 if (strlen(proxy_port) > 0)
171 curl_easy_setopt(curl, CURLOPT_PROXY, proxy_port);
173 if (strlen(proxy_user_password) > 0)
174 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_user_password);
176 /* Trick Webserver by claiming that you are using Microsoft WinXP SP2, IE6 */
177 curl_easy_setopt(curl, CURLOPT_USERAGENT,
178 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
179 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, *SyncTime_CURL_WriteOutput);
180 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, *SyncTime_CURL_WriteHeader);
183 int SyncTime_CURL_Fetch(CURL *curl, char *URL_Str, char *OutFileName,
190 if (HttpGetBody == HTTP_COMMAND_HEAD)
191 curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
193 outfile = fopen(OutFileName, "wb");
194 curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
197 curl_easy_setopt(curl, CURLOPT_URL, URL_Str);
198 res = curl_easy_perform(curl);
201 return res; /* (CURLE_OK) */
206 fprintf(stderr, "SYNCTIME: Synchronising computer clock with time server"
207 " using HTTP protocol.\n");
208 fprintf(stderr, "Usage : SYNCTIME [Option]\n");
209 fprintf(stderr, "Options :\n");
210 fprintf(stderr, " --server=WEBSERVER Use this time server instead"
212 fprintf(stderr, " --showall Show all HTTP header.\n");
213 fprintf(stderr, " --synctime Synchronising computer clock"
214 " with time server.\n");
215 fprintf(stderr, " --proxy-user=USER[:PASS] Set proxy username and"
217 fprintf(stderr, " --proxy=HOST[:PORT] Use HTTP proxy on given"
219 fprintf(stderr, " --help Print this help.\n");
220 fprintf(stderr, "\n");
224 int conf_init(conf_t *conf)
228 *conf->http_proxy = 0;
229 for (i=0; i<MAX_STRING1; i++)
230 conf->proxy_user[i] = 0; /* Clean up password from memory */
231 *conf->timeserver = 0;
235 int main(int argc, char *argv[])
245 double tzonediffFloat;
252 ShowAllHeader = 0; /* Do not show HTTP Header */
253 AutoSyncTime = 0; /* Do not synchronise computer clock */
254 RetValue = 0; /* Successful Exit */
258 while (OptionIndex < argc) {
259 if (strncmp(argv[OptionIndex], "--server=", 9) == 0)
260 snprintf(conf->timeserver, MAX_STRING, "%s", &argv[OptionIndex][9]);
262 if (strcmp(argv[OptionIndex], "--showall") == 0)
265 if (strcmp(argv[OptionIndex], "--synctime") == 0)
268 if (strncmp(argv[OptionIndex], "--proxy-user=", 13) == 0)
269 snprintf(conf->proxy_user, MAX_STRING, "%s", &argv[OptionIndex][13]);
271 if (strncmp(argv[OptionIndex], "--proxy=", 8) == 0)
272 snprintf(conf->http_proxy, MAX_STRING, "%s", &argv[OptionIndex][8]);
274 if ((strcmp(argv[OptionIndex], "--help") == 0) ||
275 (strcmp(argv[OptionIndex], "/?") == 0)) {
283 if (*conf->timeserver == 0) /* Use default server for time information */
284 snprintf(conf->timeserver, MAX_STRING, "%s", DefaultTimeServer[0]);
286 /* Init CURL before usage */
287 curl_global_init(CURL_GLOBAL_ALL);
288 curl = curl_easy_init();
290 SyncTime_CURL_Init(curl, conf->http_proxy, conf->proxy_user);
292 /* Calculating time diff between GMT and localtime */
295 tt_local = mktime(lt);
297 tt_gmt = mktime(gmt);
298 tzonediffFloat = difftime(tt_local, tt_gmt);
299 tzonediffWord = (int)(tzonediffFloat/3600.0);
301 if ((double)(tzonediffWord * 3600) == tzonediffFloat)
302 snprintf(tzoneBuf, 15, "%+03d'00'", tzonediffWord);
304 snprintf(tzoneBuf, 15, "%+03d'30'", tzonediffWord);
306 /* Get current system time and local time */
307 GetSystemTime(&SYSTime);
308 GetLocalTime(&LOCALTime);
309 snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
310 DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
311 MthStr[LOCALTime.wMonth-1], LOCALTime.wYear,
312 LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
313 LOCALTime.wMilliseconds);
315 fprintf(stderr, "Fetch: %s\n\n", conf->timeserver);
316 fprintf(stderr, "Before HTTP. Date: %s%s\n\n", timeBuf, tzoneBuf);
318 /* HTTP HEAD command to the Webserver */
319 SyncTime_CURL_Fetch(curl, conf->timeserver, "index.htm",
322 GetLocalTime(&LOCALTime);
323 snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
324 DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
325 MthStr[LOCALTime.wMonth-1], LOCALTime.wYear,
326 LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
327 LOCALTime.wMilliseconds);
328 fprintf(stderr, "\nAfter HTTP. Date: %s%s\n", timeBuf, tzoneBuf);
330 if (AutoSyncTime == 3) {
331 /* Synchronising computer clock */
332 if (!SetSystemTime(&SYSTime)) { /* Set system time */
333 fprintf(stderr, "ERROR: Unable to set system time.\n");
337 /* Successfully re-adjusted computer clock */
338 GetLocalTime(&LOCALTime);
339 snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
340 DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
341 MthStr[LOCALTime.wMonth-1], LOCALTime.wYear,
342 LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
343 LOCALTime.wMilliseconds);
344 fprintf(stderr, "\nNew System's Date: %s%s\n", timeBuf, tzoneBuf);
348 /* Cleanup before exit */
350 curl_easy_cleanup(curl);