From b58e642a76801673529aace790f1e8e6fc297195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98rjan=20Malde?= Date: Fri, 3 Feb 2023 12:50:11 +0000 Subject: [PATCH] remove dependency on libbsd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ørjan Malde --- configure.ac | 4 +--- src/Makefile.am | 2 +- src/ntp.c | 23 ++++++++++++++--------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/configure.ac b/configure.ac index 37c8404..8d542dd 100644 --- a/configure.ac +++ b/configure.ac @@ -36,8 +36,6 @@ AC_CHECK_LIB([util], [malloc]) AC_CHECK_HEADERS([arpa/inet.h fcntl.h float.h limits.h netdb.h \ netinet/in.h stdlib.h string.h sys/param.h sys/socket.h \ sys/time.h unistd.h util.h]) -AC_CHECK_HEADERS([bsd/stdlib.h], [], \ - [echo "ERROR: previous header not found."; exit -1]) # Checks for typedefs, structures, and compiler characteristics. AC_C_CONST @@ -51,7 +49,7 @@ AC_STRUCT_TM AC_FUNC_MALLOC AC_FUNC_SELECT_ARGTYPES AC_FUNC_STRFTIME -AC_CHECK_FUNCS([gettimeofday memset select socket]) +AC_CHECK_FUNCS([arc4random gettimeofday memset select socket]) # Check for uname prog AC_PATH_PROG([UNAME], [uname], diff --git a/src/Makefile.am b/src/Makefile.am index 63c6d4e..0be381a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,7 +9,7 @@ bin_PROGRAMS = rdate rdate_SOURCES = rdate.c -rdate_LDADD = librdate.a -lbsd +rdate_LDADD = librdate.a noinst_LIBRARIES = librdate.a diff --git a/src/ntp.c b/src/ntp.c index 2a27b91..547ec5f 100644 --- a/src/ntp.c +++ b/src/ntp.c @@ -59,7 +59,6 @@ #include #include "ntpleaps.h" -#include /* * NTP definitions. Note that these assume 8-bit bytes - sigh. There @@ -101,10 +100,10 @@ #define MILLION_D 1.0e6 /* Must be equal to MILLION_L */ struct ntp_data { - u_char status; - u_char version; - u_char mode; - u_char stratum; + uint8_t status; + uint8_t version; + uint8_t mode; + uint8_t stratum; double receive; double transmit; double current; @@ -119,7 +118,7 @@ void ntp_client(const char *, int, struct timeval *, struct timeval *, int, int, int sync_ntp(int, const struct sockaddr *, socklen_t, double *, double *, int); int write_packet(int, struct ntp_data *); int read_packet(int, struct ntp_data *, double *, double *); -void unpack_ntp(struct ntp_data *, u_char *); +void unpack_ntp(struct ntp_data *, uint8_t *); double current_time(double); void create_timeval(double, struct timeval *, struct timeval *); @@ -277,14 +276,20 @@ sync_ntp(int fd, const struct sockaddr *peer, socklen_t addrlen, int write_packet(int fd, struct ntp_data *data) { - u_char packet[NTP_PACKET_MIN]; + uint8_t packet[NTP_PACKET_MIN]; ssize_t length; memset(packet, 0, sizeof(packet)); packet[0] = (NTP_VERSION << 3) | (NTP_MODE_CLIENT); +#ifndef HAVE_ARC4RANDOM + uint64_t tmp; + getentropy(&tmp, 8); + data->xmitck = tmp; +#else data->xmitck = (uint64_t)arc4random() << 32 | arc4random(); +#endif /* * Send out a random 64-bit number as our transmit time. The NTP @@ -325,7 +330,7 @@ write_packet(int fd, struct ntp_data *data) int read_packet(int fd, struct ntp_data *data, double *off, double *error) { - u_char receive[NTP_PACKET_MAX]; + uint8_t receive[NTP_PACKET_MAX]; struct timeval tv; double x, y; int length, r; @@ -421,7 +426,7 @@ read_packet(int fd, struct ntp_data *data, double *off, double *error) * to SNTP. */ void -unpack_ntp(struct ntp_data *data, u_char *packet) +unpack_ntp(struct ntp_data *data, uint8_t *packet) { int i; double d;