View. You can use inet_pton() to try to parse the string first as IPv4 ( AF_INET ), then IPv6 ( AF_INET6 ). The return code will let you know if this function was successful, and therefore the line contains the address of the requested type.
For example:
#include <arpa/inet.h> #include <stdio.h> static int ip_version(const char *src) { char buf[16]; if (inet_pton(AF_INET, src, buf)) { return 4; } else if (inet_pton(AF_INET6, src, buf)) { return 6; } return -1; } int main(int argc, char *argv[]) { for (int i = 1; i < argc; ++i) { printf("%s\t%d\n", argv[i], ip_version(argv[i])); } return 0; }
llasram
source share