"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "htpdate.c" between
htpdate-1.3.4.tar.gz and htpdate-1.3.5.tar.gz

About: HTPdate (HTTP Time Protocol) synchronizes a computer’s time with web servers as reference time source (working through proxy servers).

htpdate.c  (htpdate-1.3.4):htpdate.c  (htpdate-1.3.5)
/* /*
htpdate v1.3.4 htpdate v1.3.5
Eddy Vervest <eddy@vervest.org> Eddy Vervest <eddy@vervest.org>
http://www.vervest.org/htp http://www.vervest.org/htp
Synchronize local system with time offered by remote web servers Synchronize local system with time offered by remote web servers
This program works with the timestamps return by web servers, This program works with the timestamps return by web servers,
formatted as specified by HTTP/1.1 (RFC 2616, RFC 1123). formatted as specified by HTTP/1.1 (RFC 2616, RFC 1123).
Example usage: Example usage:
skipping to change at line 63 skipping to change at line 63
#include <float.h> #include <float.h>
#if defined __NetBSD__ || defined __FreeBSD__ || defined __APPLE__ #if defined __NetBSD__ || defined __FreeBSD__ || defined __APPLE__
#define adjtimex ntp_adjtime #define adjtimex ntp_adjtime
#endif #endif
#ifdef ENABLE_HTTPS #ifdef ENABLE_HTTPS
#include <openssl/ssl.h> #include <openssl/ssl.h>
#endif #endif
#define VERSION "1.3.4" #define VERSION "1.3.5"
#define MAX_HTTP_HOSTS 16 /* 16 web servers */ #define MAX_HTTP_HOSTS 16 /* 16 web servers */
#define DEFAULT_HTTP_PORT "80" #define DEFAULT_HTTP_PORT "80"
#define DEFAULT_PROXY_PORT "8080" #define DEFAULT_PROXY_PORT "8080"
#define DEFAULT_IP_VERSION PF_UNSPEC /* IPv6 and IPv4 */ #define DEFAULT_IP_VERSION PF_UNSPEC /* IPv6 and IPv4 */
#define DEFAULT_HTTP_VERSION "1" /* HTTP/1.1 */ #define DEFAULT_HTTP_VERSION "1" /* HTTP/1.1 */
#define DEFAULT_TIME_LIMIT 31536000 /* 1 year */ #define DEFAULT_TIME_LIMIT 31536000 /* 1 year */
#define NO_TIME_LIMIT -1 #define NO_TIME_LIMIT -1
#define ERR_TIMESTAMP DBL_MAX /* Err fetching date in getHTT Pdate */ #define ERR_TIMESTAMP DBL_MAX /* Err fetching date in getHTT Pdate */
#define DEFAULT_PRECISION 4 /* 4 request per host */ #define DEFAULT_PRECISION 4 /* 4 request per host */
#define DEFAULT_MIN_SLEEP 1800 /* 30 minutes */ #define DEFAULT_MIN_SLEEP 1800 /* 30 minutes */
skipping to change at line 397 skipping to change at line 397
nanosleep(&sleepspec, NULL); nanosleep(&sleepspec, NULL);
/* Send HEAD request */ /* Send HEAD request */
#ifdef ENABLE_HTTPS #ifdef ENABLE_HTTPS
if (scheme) if (scheme)
rc = sendHEADTLS(conn, headrequest, buffer); rc = sendHEADTLS(conn, headrequest, buffer);
else else
#endif #endif
rc = sendHEAD(server_s, headrequest, buffer); rc = sendHEAD(server_s, headrequest, buffer);
if (!rc) if (!rc) {
printlog(1, "error from %s:%s", host, port ); printlog(1, "error from %s:%s", host, port );
offset = LLONG_MAX;
if (rc) { break;
} else {
clock_gettime(CLOCK_REALTIME, &now); clock_gettime(CLOCK_REALTIME, &now);
/* rtt contains round trip time in nanoseconds */ /* rtt contains round trip time in nanoseconds */
rtt = (now.tv_sec - rtt) * 1e9 + now.tv_nsec - when; rtt = (now.tv_sec - rtt) * 1e9 + now.tv_nsec - when;
/* Look for the line that contains [dD]ate: */ /* Look for the line that contains [dD]ate: */
if ((pdate = strcasestr(buffer, "date: ")) != NULL && strlen(pdate) >= 35) { if ((pdate = strcasestr(buffer, "date: ")) != NULL && strlen(pdate) >= 35) {
if (debug > 2) printlog(0, "%s", buffer); if (debug > 2) printlog(0, "%s", buffer);
char remote_time[25] = {'\0'}; char remote_time[25] = {'\0'};
skipping to change at line 429 skipping to change at line 430
} else { } else {
first_offset=offset; first_offset=offset;
} }
prev_offset=offset; prev_offset=offset;
/* Print host, raw timestamp, round trip time */ /* Print host, raw timestamp, round trip time */
if (debug) if (debug)
printlog(0, "%-25s %s, %s (%li ms) => %li", host, port, printlog(0, "%-25s %s, %s (%li ms) => %li", host, port,
remote_time, rtt / (long)1e6, offset); remote_time, rtt / (long)1e6, offset);
} else { } else {
printlog(1, "%s no timestamp", host); printlog(1, "%s no timestamp", host);
return(ERR_TIMESTAMP); offset = LLONG_MAX;
break;
} }
} /* bytes received */ } /* bytes received */
precision--; precision--;
when += nap; when += nap;
} while (precision >= 1); } while (precision >= 1);
close(server_s); close(server_s);
#ifdef ENABLE_HTTPS #ifdef ENABLE_HTTPS
if (scheme) SSL_shutdown(conn); if (scheme) SSL_shutdown(conn);
SSL_CTX_free(tls_ctx); SSL_CTX_free(tls_ctx);
SSL_free(conn); SSL_free(conn);
#endif #endif
/* Rounding */ /* Rounding */
if (debug) printlog(0, "when: %ld, nap: %ld", when, nap); if (debug) printlog(0, "when: %ld, nap: %ld", when, nap);
if (offset == LLONG_MAX) return(ERR_TIMESTAMP);
if (when + nap == 1e9 && offset == 0) return 0; if (when + nap == 1e9 && offset == 0) return 0;
/* Return the time delta between web server time (timevalue) /* Return the time delta between web server time (timevalue)
and system time (now) and system time (now)
*/ */
if (first_offset < 0) { if (first_offset < 0) {
return(-first_offset + (1e9L-when)/(double)1e9L); return(-first_offset + (1e9L-when)/(double)1e9L);
} else { } else {
return(-first_offset + 1 - when/(double)1e9L); return(-first_offset + 1 - when/(double)1e9L);
} }
} }
static int setstatus(int precision) {
struct timex txc = {0};
txc.modes = MOD_STATUS | (1000000 >> precision) | MOD_MAXERROR;
txc.status &= ~STA_UNSYNC;
printlog(0, "Set clock synchronized");
return(adjtimex(&txc));
}
static int setclock(double timedelta, int setmode) { static int setclock(double timedelta, int setmode) {
struct timespec now; struct timespec now;
struct timeval timeofday; struct timeval timeofday;
char buffer[32] = {'\0'}; char buffer[32] = {'\0'};
if (timedelta == 0) { if (timedelta == 0) {
printlog(0, "No time correction needed"); printlog(0, "No time correction needed");
return(0); return(0);
} }
skipping to change at line 532 skipping to change at line 545
tmx.freq = sign(tmx.freq) * MAX_DRIFT; tmx.freq = sign(tmx.freq) * MAX_DRIFT;
printlog(0, "Set frequency: %li", tmx.freq); printlog(0, "Set frequency: %li", tmx.freq);
tmx.modes = MOD_FREQUENCY; tmx.modes = MOD_FREQUENCY;
/* Become root */ /* Become root */
swuid(0); swuid(0);
return(adjtimex(&tmx)); return(adjtimex(&tmx));
} }
static int htpdate_adjtimex(double drift, char *driftfile) { static int htpdate_adjtimex(double drift, char *driftfile, float confidence) {
struct timex tmx; struct timex tmx;
long freq; long freq;
FILE *fp; FILE *fp;
/* Read current clock frequency */ /* Read current clock frequency */
tmx.modes = 0; tmx.modes = 0;
adjtimex(&tmx); adjtimex(&tmx);
/* Calculate new frequency */ /* Calculate new frequency */
freq = (long)(65536e6 * drift); freq = (long)(65536e6 * drift);
/* Weighted average of current and new frequency */ /* Weighted average of current and new frequency */
tmx.freq = tmx.freq + (freq >> 3); tmx.freq = tmx.freq + freq * confidence;
if ((tmx.freq < -MAX_DRIFT) || (tmx.freq > MAX_DRIFT)) if ((tmx.freq < -MAX_DRIFT) || (tmx.freq > MAX_DRIFT))
tmx.freq = sign(tmx.freq) * MAX_DRIFT; tmx.freq = sign(tmx.freq) * MAX_DRIFT;
printlog(0, "Set frequency %li", tmx.freq); printlog(0, "Set frequency %li", tmx.freq);
tmx.modes = MOD_FREQUENCY; tmx.modes = MOD_FREQUENCY;
if (driftfile) { if (driftfile) {
fp = fopen(driftfile, "w"); fp = fopen(driftfile, "w");
if (fp != NULL) { if (fp != NULL) {
printlog(0, "Update %s", driftfile); printlog(0, "Update %s", driftfile);
skipping to change at line 570 skipping to change at line 583
} }
} }
/* Become root */ /* Become root */
swuid(0); swuid(0);
return(adjtimex(&tmx)); return(adjtimex(&tmx));
} }
static void showhelp() { static void showhelp() {
puts("htpdate version "VERSION"\n\ puts("htpdate version "VERSION"\n\
Usage: htpdate [-046acdhlnqstvxD] [-f driftfile] [-i pidfile] [-m minpoll]\n\ Usage: htpdate [-046acdhlnqstvxDF] [-f driftfile] [-i pidfile] [-m minpoll]\n\
[-M maxpoll] [-p precision] [-P <proxyserver>[:port]]\n\ [-M maxpoll] [-p precision] [-P <proxyserver>[:port]]\n\
[-u user[:group]] <URL> ...\n\n\ [-u user[:group]] <URL> ...\n\n\
-0 HTTP/1.0 request\n\ -0 HTTP/1.0 request\n\
-4 Force IPv4 name resolution only\n\ -4 Force IPv4 name resolution only\n\
-6 Force IPv6 name resolution only\n\ -6 Force IPv6 name resolution only\n\
-a adjust time smoothly\n\ -a adjust time smoothly\n\
-c verify server certificate\n\ -c verify server certificate\n\
-d debug mode\n\ -d debug mode\n\
-D daemon mode\n\ -D daemon mode\n\
-f drift/frequency file\n\ -f drift/frequency file\n\
skipping to change at line 780 skipping to change at line 793
printf("Unknown group %s\n", group); printf("Unknown group %s\n", group);
exit(1); exit(1);
} }
} }
break; break;
case 'v': /* print version */ case 'v': /* print version */
printf("htpdate version %s\n", VERSION); printf("htpdate version %s\n", VERSION);
exit(0); exit(0);
case 'x': /* adjust time and clock frequency */ case 'x': /* adjust time and clock frequency */
setmode = 3; setmode = 3;
if (maxsleep > 14400) maxsleep = 14400;
if (precision < 7) precision = 7;
break; break;
case 'D': /* run as daemon */ case 'D': /* run as daemon */
daemonize = 1; daemonize = 1;
logmode = 1; logmode = 1;
break; break;
case 'F': /* run daemon in foreground, don't fork */ case 'F': /* run daemon in foreground, don't fork */
foreground = 1; foreground = 1;
break; break;
case 'M': /* maximum poll interval */ case 'M': /* maximum poll interval */
if ((maxsleep = atoi(optarg)) <= 0) { if ((maxsleep = atoi(optarg)) <= 0) {
skipping to change at line 905 skipping to change at line 920
for (i = 0; i < validtimes; i++) { for (i = 0; i < validtimes; i++) {
if ((timedelta[i]-mean) < .5 && (timedelta[i]-mean) > -.5) { if ((timedelta[i]-mean) < .5 && (timedelta[i]-mean) > -.5) {
sumtimes += timedelta[i]; sumtimes += timedelta[i];
goodtimes++; goodtimes++;
} }
} }
/* Check if we have at least one valid response */ /* Check if we have at least one valid response */
if (goodtimes) { if (goodtimes) {
timeavg = sumtimes / goodtimes; timeavg = sumtimes / goodtimes / 2;
if (debug > 1) if (debug > 1)
printlog(0, "#: %d, mean: %.3f, average: %.3f", goodtimes, mean, timeavg); printlog(0, "#: %d, mean: %.3f, average: %.3f", goodtimes, mean, timeavg);
/* Do I really need to change the time? */ /* Do I really need to change the time? */
if (sumtimes || !(daemonize || foreground)) { if (sumtimes || !(daemonize || foreground)) {
if (setclock(timeavg, setmode) < 0) if (setclock(timeavg, setmode) < 0)
printlog(1, "Time change failed"); printlog(1, "Time change failed");
/* Drop root privileges again */ /* Drop root privileges again */
skipping to change at line 928 skipping to change at line 943
if (daemonize || foreground) { if (daemonize || foreground) {
if (starttime) { if (starttime) {
/* Calculate systematic clock drift */ /* Calculate systematic clock drift */
drift = timeavg / (time(NULL) - starttime); drift = timeavg / (time(NULL) - starttime);
printlog(0, "Drift %.2f PPM, %.2f s/day", drift*1e6, dri ft*86400); printlog(0, "Drift %.2f PPM, %.2f s/day", drift*1e6, dri ft*86400);
/* Adjust system clock */ /* Adjust system clock */
if (setmode == 3) { if (setmode == 3) {
starttime = time(NULL); starttime = time(NULL);
/* Adjust the clock frequency */ /* Adjust the clock frequency */
if (htpdate_adjtimex(drift, driftfile) < 0) if (htpdate_adjtimex(drift, driftfile, (float)sleept ime / (float)maxsleep) < 0)
printlog(1, "Frequency change failed"); printlog(1, "Frequency change failed");
/* Drop root privileges again */ /* Drop root privileges again */
if (sw_uid) swuid(sw_uid); if (sw_uid) swuid(sw_uid);
} }
} else { } else {
starttime = time(NULL); starttime = time(NULL);
} }
/* Decrease polling interval to minimum */ /* Decrease polling interval to minimum */
sleeptime = minsleep; sleeptime = minsleep;
/* Sleep for 30 minutes after a time adjust or set */ /* Sleep for 30 minutes after a time adjust or set */
sleep(DEFAULT_MIN_SLEEP); sleep(DEFAULT_MIN_SLEEP);
} }
} else { } else {
/* Increase polling interval */ /* Increase polling interval */
if (sleeptime < maxsleep) sleeptime <<= 1; if (sleeptime < maxsleep) sleeptime <<= 1;
if (setmode == 3) setstatus(precision);
} }
if (daemonize || foreground) { if (daemonize || foreground) {
printlog(0, "sleep for %ld s", sleeptime); printlog(0, "sleep for %ld s", sleeptime);
sleep(sleeptime); sleep(sleeptime);
} }
} else { } else {
printlog(1, "No server suitable for synchronization found"); printlog(1, "No server suitable for synchronization found");
/* Sleep for minsleep to avoid flooding */ /* Sleep for minsleep to avoid flooding */
 End of changes. 14 change blocks. 
11 lines changed or deleted 27 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)