File: synctime.c

package info (click to toggle)
curl 8.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,016 kB
  • sloc: ansic: 202,975; perl: 20,695; python: 10,293; sh: 6,684; makefile: 1,529; pascal: 239; cpp: 174
file content (360 lines) | stat: -rw-r--r-- 12,085 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 * SPDX-License-Identifier: curl
 *
 ***************************************************************************/
/* <DESC>
 * Set your system time from a remote HTTP server's Date: header.
 * </DESC>
 */
/* This example code only builds as-is on Windows.
 *
 * Synchronising your computer clock via Internet time server usually relies
 * on DAYTIME, TIME, or NTP protocols. These protocols provide good accurate
 * time synchronization but it does not work well through a firewall/proxy.
 * Some adjustment has to be made to the firewall/proxy for these protocols to
 * work properly.
 *
 * There is an indirect method. Since most webserver provide server time in
 * their HTTP header, therefore you could synchronise your computer clock
 * using HTTP protocol which has no problem with firewall/proxy.
 *
 * For this software to work, you should take note of these items.
 * 1. Your firewall/proxy must allow your computer to surf Internet.
 * 2. Webserver system time must in sync with the NTP time server,
 *    or at least provide an accurate time keeping.
 * 3. Webserver HTTP header does not provide the milliseconds units,
 *    so there is no way to get an accurate time.
 * 4. This software could only provide an accuracy of +- a few seconds,
 *    as Round-Trip delay time is not taken into consideration.
 *    Compensation of network, firewall/proxy delay cannot be simply divide
 *    the Round-Trip delay time by half.
 * 5. Win32 SetSystemTime() API sets your computer clock according to
 *    GMT/UTC time. Therefore your computer timezone must be properly set.
 * 6. Webserver data should not be cached by the proxy server. Some
 *    webserver provide Cache-Control to prevent caching.
 *
 * Usage:
 * This software synchronises your computer clock only when you issue
 * it with --synctime. By default, it only display the webserver's clock.
 */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen(), gmtime(),
                                   localtime(), sscanf() */
#endif
#endif

#include <stdio.h>
#include <string.h>
#include <time.h>

#include <curl/curl.h>

#ifdef _WIN32
#if (defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0602)) || \
  defined(WINAPI_FAMILY)
#  include <winapifamily.h>
#  if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \
     !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
#    define CURL_WINDOWS_UWP
#  endif
#include <windows.h>
#endif
#endif

#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif

#define SYNCTIME_UA "synctime/1.0"

struct conf {
  char http_proxy[256];
  char proxy_user[256];
  char timeserver[256];
};

static int ShowAllHeader;
static int AutoSyncTime;
#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
static SYSTEMTIME SYSTime;
static SYSTEMTIME LOCALTime;

static const char *DayStr[] = {
  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char *MthStr[] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
#endif

#define HTTP_COMMAND_HEAD 0
#define HTTP_COMMAND_GET  1

static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *stream)
{
  fwrite(ptr, size, nmemb, stream);
  return nmemb * size;
}

/* Remember: do not assume headers are passed on null terminated! */
static size_t SyncTime_CURL_WriteHeader(void *ptr, size_t size, size_t nmemb,
                                        void *stream)
{
  (void)stream;

  if(ShowAllHeader == 1)
    fprintf(stderr, "%.*s", (int)nmemb, (char *)ptr);

  if((nmemb >= 5) && !strncmp((char *)ptr, "Date:", 5)) {
    if(ShowAllHeader == 0)
      fprintf(stderr, "HTTP Server. %.*s", (int)nmemb, (char *)ptr);

    if(AutoSyncTime == 1) {
#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
      char TmpStr1[26], TmpStr2[26];
      int RetVal = 0;
      char *field = ptr;
      *TmpStr1 = 0;
      *TmpStr2 = 0;
      if(nmemb && (field[nmemb] == '\n')) {
        field[nmemb] = 0; /* null terminated */
        RetVal = sscanf(field, "Date: %25s %hu %25s %hu %hu:%hu:%hu",
                        TmpStr1, &SYSTime.wDay, TmpStr2, &SYSTime.wYear,
                        &SYSTime.wHour, &SYSTime.wMinute,
                        &SYSTime.wSecond);
      }

      if(RetVal == 7) {
        int i;
        SYSTime.wMilliseconds = 500;  /* adjust to midpoint, 0.5 sec */
        for(i = 0; i < 12; i++) {
          if(strcmp(MthStr[i], TmpStr2) == 0) {
            SYSTime.wMonth = (WORD)(i + 1);
            break;
          }
        }
        AutoSyncTime = 3;  /* Computer clock is adjusted */
      }
      else {
        AutoSyncTime = 0;  /* Error in sscanf() fields conversion */
      }
#endif
    }
  }

  if((nmemb >= 12) && !strncmp((char *)ptr, "X-Cache: HIT", 12)) {
    fprintf(stderr, "ERROR: HTTP Server data is cached."
            " Server Date is no longer valid.\n");
    AutoSyncTime = 0;
  }
  return nmemb * size;
}

static void SyncTime_CURL_Init(CURL *curl, const char *proxy_port,
                               const char *proxy_user_password)
{
  if(strlen(proxy_port) > 0)
    curl_easy_setopt(curl, CURLOPT_PROXY, proxy_port);

  if(strlen(proxy_user_password) > 0)
    curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_user_password);

  curl_easy_setopt(curl, CURLOPT_USERAGENT, SYNCTIME_UA);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, SyncTime_CURL_WriteHeader);
}

static CURLcode SyncTime_CURL_Fetch(CURL *curl, const char *URL_Str,
                                    const char *OutFileName, int HttpGetBody)
{
  FILE *outfile;
  CURLcode result;

  outfile = NULL;
  if(HttpGetBody == HTTP_COMMAND_HEAD)
    curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
  else {
    outfile = fopen(OutFileName, "wb");
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
  }

  curl_easy_setopt(curl, CURLOPT_URL, URL_Str);
  result = curl_easy_perform(curl);
  if(outfile)
    fclose(outfile);
  return result; /* CURLE_OK */
}

static void showUsage(void)
{
  fprintf(stderr, "synctime: Synchronising computer clock with time server"
          " using HTTP protocol.\n");
  fprintf(stderr, "Usage   : synctime [Option]\n");
  fprintf(stderr, "Options :\n");
  fprintf(stderr, " --server=WEBSERVER        Use this time server instead"
          " of default.\n");
  fprintf(stderr, " --showall                 Show all HTTP header.\n");
  fprintf(stderr, " --synctime                Synchronising computer clock"
          " with time server.\n");
  fprintf(stderr, " --proxy-user=USER[:PASS]  Set proxy username and"
          " password.\n");
  fprintf(stderr, " --proxy=HOST[:PORT]       Use HTTP proxy on given"
          " port.\n");
  fprintf(stderr, " --help                    Print this help.\n");
  fprintf(stderr, "\n");
  return;
}

int main(int argc, char *argv[])
{
  CURLcode result;
  CURL *curl;
  struct conf conf;
  int RetValue;

  ShowAllHeader = 0;  /* Do not show HTTP Header */
  AutoSyncTime = 0;   /* Do not synchronise computer clock */
  RetValue = 0;       /* Successful Exit */

  memset(&conf, 0, sizeof(conf));

  if(argc > 1) {
    int OptionIndex = 1;
    while(OptionIndex < argc) {
      if(strncmp(argv[OptionIndex], "--server=", 9) == 0)
        snprintf(conf.timeserver, sizeof(conf.timeserver) - 1, "%s",
                 &argv[OptionIndex][9]);

      if(strcmp(argv[OptionIndex], "--showall") == 0)
        ShowAllHeader = 1;

      if(strcmp(argv[OptionIndex], "--synctime") == 0)
        AutoSyncTime = 1;

      if(strncmp(argv[OptionIndex], "--proxy-user=", 13) == 0)
        snprintf(conf.proxy_user, sizeof(conf.proxy_user) - 1, "%s",
                 &argv[OptionIndex][13]);

      if(strncmp(argv[OptionIndex], "--proxy=", 8) == 0)
        snprintf(conf.http_proxy, sizeof(conf.http_proxy) - 1, "%s",
                 &argv[OptionIndex][8]);

      if((strcmp(argv[OptionIndex], "--help") == 0) ||
         (strcmp(argv[OptionIndex], "/?") == 0)) {
        showUsage();
        return 0;
      }
      OptionIndex++;
    }
  }

  if(*conf.timeserver == 0)  /* Use default server for time information */
    snprintf(conf.timeserver, sizeof(conf.timeserver) - 1, "%s",
             "https://www.ntp.org/");

  /* Init CURL before usage */
  result = curl_global_init(CURL_GLOBAL_ALL);
  if(result)
    return (int)result;

  curl = curl_easy_init();
  if(curl) {
    struct tm *lt;
    struct tm *gmt;
    time_t tt;
    time_t tt_local;
    time_t tt_gmt;
    double tzonediffFloat;
    int tzonediffWord;
    char timeBuf[61];
    char tzoneBuf[16];

    SyncTime_CURL_Init(curl, conf.http_proxy, conf.proxy_user);

    /* Calculating time diff between GMT and localtime */
    tt       = time(0);
    lt       = localtime(&tt);
    tt_local = mktime(lt);
    gmt      = gmtime(&tt);
    tt_gmt   = mktime(gmt);
    tzonediffFloat = difftime(tt_local, tt_gmt);
    tzonediffWord = (int)(tzonediffFloat / 3600.0);

    if(tzonediffWord == (int)(tzonediffFloat / 3600.0))
      snprintf(tzoneBuf, sizeof(tzoneBuf), "%+03d'00'", tzonediffWord);
    else
      snprintf(tzoneBuf, sizeof(tzoneBuf), "%+03d'30'", tzonediffWord);

#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
    /* Get current system time and local time */
    GetSystemTime(&SYSTime);
    GetLocalTime(&LOCALTime);
    snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
             DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
             MthStr[LOCALTime.wMonth - 1], LOCALTime.wYear, LOCALTime.wHour,
             LOCALTime.wMinute, LOCALTime.wSecond, LOCALTime.wMilliseconds);
#endif

    fprintf(stderr, "Fetch: %s\n\n", conf.timeserver);
    fprintf(stderr, "Before HTTP. Date: %s%s\n\n", timeBuf, tzoneBuf);

    /* HTTP HEAD command to the Webserver */
    SyncTime_CURL_Fetch(curl, conf.timeserver, "index.htm", HTTP_COMMAND_HEAD);

#if defined(_WIN32) && !defined(CURL_WINDOWS_UWP)
    GetLocalTime(&LOCALTime);
    snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
             DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
             MthStr[LOCALTime.wMonth - 1], LOCALTime.wYear, LOCALTime.wHour,
             LOCALTime.wMinute, LOCALTime.wSecond, LOCALTime.wMilliseconds);
    fprintf(stderr, "\nAfter  HTTP. Date: %s%s\n", timeBuf, tzoneBuf);

    if(AutoSyncTime == 3) {
      /* Synchronising computer clock */
      if(!SetSystemTime(&SYSTime)) {  /* Set system time */
        fprintf(stderr, "ERROR: Unable to set system time.\n");
        RetValue = 1;
      }
      else {
        /* Successfully re-adjusted computer clock */
        GetLocalTime(&LOCALTime);
        snprintf(timeBuf, 60, "%s, %02d %s %04d %02d:%02d:%02d.%03d, ",
                 DayStr[LOCALTime.wDayOfWeek], LOCALTime.wDay,
                 MthStr[LOCALTime.wMonth - 1], LOCALTime.wYear,
                 LOCALTime.wHour, LOCALTime.wMinute, LOCALTime.wSecond,
                 LOCALTime.wMilliseconds);
        fprintf(stderr, "\nNew System's Date: %s%s\n", timeBuf, tzoneBuf);
      }
    }
#endif

    /* Cleanup before exit */
    memset(&conf, 0, sizeof(conf));
    curl_easy_cleanup(curl);
  }

  curl_global_cleanup();

  return RetValue;
}