blob: f58dbbd0a7502875fe0d8c0baffc7c26414ade16 [file] [log] [blame]
From 842142c16d03cb47f712bc396e28d56ee33e589e Mon Sep 17 00:00:00 2001
From: Avery Pennarun <apenwarr@gmail.com>
Date: Wed, 9 Apr 2014 01:38:08 -0400
Subject: [PATCH] sntpclock: add IPv6 support.
---
sntpclock.c | 49 ++++++++++++++++++++++++++++++++++++-------------
1 file changed, 36 insertions(+), 13 deletions(-)
diff --git a/sntpclock.c b/sntpclock.c
index 5680e05..63c5644 100644
--- a/sntpclock.c
+++ b/sntpclock.c
@@ -1,3 +1,4 @@
+#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>
@@ -92,7 +93,11 @@ void die_usage()
char *host;
struct ip_address ipremote;
+struct in6_addr ip6remote;
struct sockaddr_in sa;
+struct sockaddr_in6 sa6;
+struct sockaddr *sap;
+socklen_t sap_len;
int s;
unsigned char query[48];
@@ -143,18 +148,36 @@ char **argv;
host = argv[1];
if (!host) die_usage();
if (!str_diff(host,"0")) host = "127.0.0.1";
- if (host[ip_scan(host,&ipremote)]) die_usage();
-
- s = socket(AF_INET,SOCK_DGRAM,0);
- if (s == -1)
- strerr_die2sys(111,FATAL,"unable to create socket: ");
-
- byte_zero(&sa,sizeof(sa));
- byte_copy(&sa.sin_addr,4,&ipremote);
- x = (char *) &sa.sin_port;
- x[0] = 0;
- x[1] = 123; /* NTP */
- sa.sin_family = AF_INET;
+ if (inet_pton(AF_INET6, host, &ip6remote)) {
+ s = socket(AF_INET6,SOCK_DGRAM,0);
+ if (s == -1)
+ strerr_die2sys(111,FATAL,"unable to create socket6: ");
+
+ byte_zero(&sa6,sizeof(sa6));
+ byte_copy(&sa6.sin6_addr,16,&ip6remote);
+ x = (char *) &sa6.sin6_port;
+ x[0] = 0;
+ x[1] = 123; /* NTP */
+ sa6.sin6_family = AF_INET6;
+ sap = (struct sockaddr *)&sa6;
+ sap_len = sizeof(sa6);
+ } else if (!host[ip_scan(host,&ipremote)]) {
+ s = socket(AF_INET,SOCK_DGRAM,0);
+ if (s == -1)
+ strerr_die2sys(111,FATAL,"unable to create socket: ");
+
+ byte_zero(&sa,sizeof(sa));
+ byte_copy(&sa.sin_addr,4,&ipremote);
+ x = (char *) &sa.sin_port;
+ x[0] = 0;
+ x[1] = 123; /* NTP */
+ sa.sin_family = AF_INET;
+ sap = (struct sockaddr *)&sa;
+ sap_len = sizeof(sa);
+ } else {
+ strerr_die2sys(111,FATAL, "foooo!");
+ die_usage();
+ }
for (loop = 0;loop < 10;++loop) {
byte_zero(query,sizeof query);
@@ -175,7 +198,7 @@ char **argv;
query[46] = u;
taia_now(&ta0);
- if (sendto(s,query,sizeof query,0,(struct sockaddr *) &sa,sizeof sa) == -1)
+ if (sendto(s,query,sizeof query,0,sap,sap_len) == -1)
strerr_die2sys(111,FATAL,"unable to send request: ");
FD_ZERO(&rfds);
FD_SET(s,&rfds);
--
1.9.1.423.g4596e3a