summaryrefslogtreecommitdiffstats
path: root/libntp/decodenetnum.c
diff options
context:
space:
mode:
Diffstat (limited to 'libntp/decodenetnum.c')
-rw-r--r--libntp/decodenetnum.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/libntp/decodenetnum.c b/libntp/decodenetnum.c
new file mode 100644
index 0000000..4accf4a
--- /dev/null
+++ b/libntp/decodenetnum.c
@@ -0,0 +1,40 @@
+/*
+ * decodenetnum - return a net number (this is crude, but careful)
+ */
+#include <sys/types.h>
+#include <ctype.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+#include "ntp_stdlib.h"
+
+int
+decodenetnum(
+ const char *num,
+ struct sockaddr_storage *netnum
+ )
+{
+ struct addrinfo hints, *ai = NULL;
+ register int err, i;
+ register const char *cp;
+ char name[80];
+
+ cp = num;
+
+ if (*cp == '[') {
+ cp++;
+ for (i = 0; *cp != ']'; cp++, i++)
+ name[i] = *cp;
+ name[i] = '\0';
+ num = name;
+ }
+ memset(&hints, 0, sizeof(struct addrinfo));
+ hints.ai_flags = AI_NUMERICHOST;
+ err = getaddrinfo(num, NULL, &hints, &ai);
+ if (err != 0)
+ return 0;
+ memcpy(netnum, (struct sockaddr_storage *)ai->ai_addr, ai->ai_addrlen);
+ freeaddrinfo(ai);
+ return 1;
+}
OpenPOWER on IntegriCloud