diff options
author | Anton Kling <anton@kling.gg> | 2024-07-03 18:32:04 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-07-03 18:32:04 +0200 |
commit | 6ec139d3ef7c1d2a52bb786779dd1914f125eda4 (patch) | |
tree | 30c75cd402e0c3ca6bcb5ac3d9226cf394b6721a /userland/minibox/utilities/rdate.c | |
parent | 4e7918753175dbd8fc38bc7c5b176517e1dbef2f (diff) |
rdate: Add a very basic implementation rdate
Also adds sha1sum.c file which I forgot in a previous commit
Diffstat (limited to 'userland/minibox/utilities/rdate.c')
-rw-r--r-- | userland/minibox/utilities/rdate.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/userland/minibox/utilities/rdate.c b/userland/minibox/utilities/rdate.c new file mode 100644 index 0000000..11398c8 --- /dev/null +++ b/userland/minibox/utilities/rdate.c @@ -0,0 +1,50 @@ +#include <arpa/inet.h> +#include <assert.h> +#include <fcntl.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <sys/socket.h> + +int rdate_main(int argc, char **argv) { + int fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) { + perror("socket"); + return 1; + } + + struct addrinfo *result = NULL; + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 0; + hints.ai_canonname = NULL; + hints.ai_addr = NULL; + hints.ai_next = NULL; + + int rc = getaddrinfo("time-d-g.nist.gov", "37", &hints, &result); + if (-1 == rc) { + fprintf(stderr, "Error in getaddrinfo()\n"); + return 1; + } + + if (connect(fd, (struct sockaddr *)result->ai_addr, result->ai_addrlen) < 0) { + perror("connect"); + return 1; + } + + uint32_t t; + read(fd, &t, sizeof(t)); + t = ntohl(t); + + close(fd); + + int64_t unix_time = (t - 2208988800) * 1000; + int clock_fd = open("/dev/clock", O_RDWR); + int64_t current; + read(clock_fd, ¤t, sizeof(int64_t)); + write(clock_fd, &unix_time, sizeof(int64_t)); + int64_t delta = (current / 1000) - (unix_time / 1000); + printf("delta: %d\n", delta); + return 0; +} |