ncsid: Import from gBMC
This is the initial code drop from gBMC.
Google-Bug-Id: 179618516
Upstream: 1e71af914bc8c54d8b91d0a1cf377e2696713c2f
Change-Id: Ic653e8271dacd205e04f2bc713071ef2ec5936a4
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/ncsid/src/normalize_ip.c b/ncsid/src/normalize_ip.c
new file mode 100644
index 0000000..107d29f
--- /dev/null
+++ b/ncsid/src/normalize_ip.c
@@ -0,0 +1,40 @@
+#include <arpa/inet.h>
+#include <stdio.h>
+
+int main(int argc, char* argv[])
+{
+ if (argc < 1)
+ {
+ return 1;
+ }
+ if (argc != 2)
+ {
+ fprintf(stderr, "Usage: %s <ip address>\n", argv[0]);
+ return 1;
+ }
+
+ union
+ {
+ struct in_addr in;
+ struct in6_addr in6;
+ } buf;
+ int af = AF_INET6;
+ if (inet_pton(af, argv[1], &buf) != 1)
+ {
+ af = AF_INET;
+ if (inet_pton(af, argv[1], &buf) != 1)
+ {
+ fprintf(stderr, "Invalid IP Address: %s\n", argv[1]);
+ return 2;
+ }
+ }
+
+ char str[INET6_ADDRSTRLEN];
+ if (inet_ntop(af, &buf, str, sizeof(str)) == NULL)
+ {
+ return 1;
+ }
+
+ printf("%s\n", str);
+ return 0;
+}