static int serve(struct ntpd_param *param) { int ret; struct ntphdr rep; struct timespec ts; assert(param != NULL); while (param->running) { ret = recv(param->sock, &rep, sizeof rep, 0); if (ret == -1) { perror("ntpd: recv() failure"); return -errno; } else if (ret < sizeof rep) { continue; } else if (ntp_mode_client(&rep) || !ntp_valid_stratum(&rep)) { continue; } param->replied = 1; param->poll = rep.poll; ret = ntp_time(&rep, &ts); if (ret != 0) { return ret; } settimeofday(&ts, NULL); } return 0; }
fc::time_point_sec now() { if( simulated_time ) return fc::time_point() + fc::seconds(simulated_time + adjusted_time_sec); auto ntp = ntp_time(); if( ntp ) return *ntp + fc::seconds( adjusted_time_sec ); else return fc::time_point::now() + fc::seconds( adjusted_time_sec ); }
fc::time_point_sec now() { if( simulated_time ) return fc::time_point() + fc::seconds( simulated_time + adjusted_time_sec ); fc::optional<fc::time_point> current_ntp_time = ntp_time(); if( current_ntp_time.valid() ) return *current_ntp_time + fc::seconds( adjusted_time_sec ); else return fc::time_point::now() + fc::seconds( adjusted_time_sec ); }
static int ntpdate_process(const struct ntphdr *rep, in_addr_t addr, int only_query) { int ret; struct timespec ts; assert(rep != NULL); if (!ntp_valid_stratum(rep)) { printf("ntpdate_process: error: invalid stratum (%s)\n", ntp_stratum_error(rep)); return 0; /* error: incorrect packet */ } if (only_query) { /* show result */ struct timespec offset, delay; getnsofday(&ts, NULL); ret = ntp_offset(rep, &ts, &offset); if (ret != 0) { return ret; } ret = ntp_delay(rep, &ts, &delay); if (ret != 0) { return ret; } printf("server %s, stratum %hhd, offset %ld.%.6ld, delay %ld.%.6ld\n", inet_ntoa(*(struct in_addr *)&addr), rep->stratum, offset.tv_sec, offset.tv_nsec / NSEC_PER_USEC, delay.tv_sec, delay.tv_nsec / NSEC_PER_USEC); printf("[%s] adjust time server %s offset %ld.%.6ld sec\n", ctime(&ts.tv_sec), inet_ntoa(*(struct in_addr *)&addr), offset.tv_sec, offset.tv_nsec / NSEC_PER_USEC); } else { /* setup new time */ ret = ntp_time(rep, &ts); if (ret != 0) { return ret; } settimeofday(&ts, NULL); } return 0; }
fc::microseconds ntp_error() { fc::optional<fc::time_point> current_ntp_time = ntp_time(); FC_ASSERT( current_ntp_time, "We don't have NTP time!" ); return *current_ntp_time - fc::time_point::now(); }
double ntp_error() { FC_ASSERT(ntp_time(), "We don't have NTP time, we can't calculate error"); return ( *ntp_time() - fc::time_point::now() ).count() / double( 1000000 ); }