/* Copyright (c) 2011 by Juliusz Chroboczek Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* A refclock for ntpd and chrony that determines the time over HTTP. Useful when everything except HTTP is firewalled. Although HTTP has a granularity of 1s, this can achieve a precision of under 100ms after averaging and filtering. With chrony, say something like refclock SHM 0 refid HTTP delay 1 */ #include #include #include #include #include #include #include #include #include #include #if defined(__GNUC__) && (__GNUC__ >= 3) #define BARRIER() do {__sync_synchronize();} while(0) #else #warn "Cannot do memory barriers on this compiler." #define BARRIER() do{ /* nothing*/ } while(0) #endif #define NTPD_BASE 0x4e545030 struct shmTime { int mode; int count; time_t clockTimeStampSec; int clockTimeStampUSec; time_t receiveTimeStampSec; int receiveTimeStampUSec; int leap; int precision; int nsamples; int valid; int pad[10]; }; static long tm = -1; static long interval = 60; static char *url = "http://www.google.com/"; static int verbose = 0; static size_t discard_handler(void *contents, size_t size, size_t nmemb, void *closure) { return size * nmemb; } static size_t header_handler(void *contents, size_t size, size_t nmemb, void *closure) { size_t sz = size * nmemb; if(sz > 5 && strncasecmp(contents, "date:", 5) == 0) { char copy[95]; size_t n = sz >= 99 ? 94 : sz - 5; memcpy(copy, contents + 5, n); copy[n] = '\0'; tm = curl_getdate(copy, NULL); } return sz; } static void usage() { fprintf(stderr, "http-refclock [-v] [-i interval] [-u unit] [url]\n"); exit(1); } int main(int argc, char **argv) { int opt, shmid, rc; int unit = 0, perms = 0600; struct passwd *pw; volatile struct shmTime *p; while(1) { opt = getopt(argc, argv, "vi:u:"); if(opt < 0) break; switch(opt) { case 'v': verbose = 1; break; case 'i': interval = atoi(optarg); if(interval < 1) usage(); break; case 'u': unit = atoi(optarg); if(unit < 0 || unit > 3) usage(); break; default: usage(); } } if(optind < argc) url = argv[optind++]; if(optind < argc) usage(); shmid = shmget((key_t)(NTPD_BASE + unit), sizeof(struct shmTime), (int)(IPC_CREAT | perms)); if(shmid < 0) { perror("shmget"); exit(1); } p = shmat(shmid, 0, 0); if((int)(long)p == -1) { perror("shmat"); exit(1); } pw = getpwnam("nobody"); if(pw == NULL) { perror("getpwnam(nobody)"); exit(1); } rc = setuid(pw->pw_uid); if(rc < 0) { perror("setuid(nobody)"); exit(1); } memset((void*)p, 0, sizeof(struct shmTime)); p->mode = 1; p->precision = -1; p->nsamples = 5; while(1) { CURL *curl; CURLcode res; struct timeval tv1, tv2; curl = curl_easy_init(); if(curl == NULL) { perror("curl_easy_init"); exit(1); } curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_NOBODY, 1); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, discard_handler); curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_handler); tm = -1; gettimeofday(&tv1, NULL); res = curl_easy_perform(curl); gettimeofday(&tv2, NULL); if(res == CURLE_OK && tm > 0) { struct timeval tv; double connect_time; res = curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &connect_time); if(res != CURLE_OK) connect_time = 0.0; tv1.tv_sec += (int)connect_time; tv1.tv_usec += (int)((connect_time - (int)connect_time) * 1000000); while(tv1.tv_usec > 1000000) { tv1.tv_sec++; tv1.tv_usec -= 1000000; } if(tv1.tv_sec > tv2.tv_sec || (tv1.tv_sec == tv2.tv_sec && tv1.tv_usec > tv2.tv_usec)) { tv1 = tv2; } tv.tv_sec = tv1.tv_sec + (tv2.tv_sec - tv1.tv_sec) / 2; tv.tv_usec = tv1.tv_usec + (tv2.tv_usec - tv1.tv_usec) / 2; while(tv.tv_usec > 1000000) { tv.tv_sec++; tv.tv_usec -= 1000000; } if(verbose) { double offset = tv.tv_sec - tm + (tv.tv_usec / 1000000.0) - 0.5; printf("%ld: offset %lf, connect time %lf\n", tv.tv_sec, offset, connect_time); } p->valid = 0; BARRIER(); p->count++; p->clockTimeStampSec = tm; p->clockTimeStampUSec = 500000; p->receiveTimeStampSec = tv.tv_sec; p->receiveTimeStampUSec = tv.tv_usec; p->count++; BARRIER(); p->valid = 1; } curl_easy_cleanup(curl); /* We want our samples to be uniformly distributed in the sub-second range, so we cannot just use sleep. */ { struct timeval tv; tv.tv_sec = interval / 2 + random() % interval; tv.tv_usec = random() % 1000000; select(0, NULL, NULL, NULL, &tv); } } return 0; }