William A. Kennington III | 7d6fa42 | 2021-02-08 17:04:02 -0800 | [diff] [blame] | 1 | #include <arpa/inet.h> |
| 2 | #include <stdio.h> |
| 3 | |
| 4 | int main(int argc, char* argv[]) |
| 5 | { |
| 6 | if (argc < 1) |
| 7 | { |
| 8 | return 1; |
| 9 | } |
| 10 | if (argc != 2) |
| 11 | { |
| 12 | fprintf(stderr, "Usage: %s <ip address>\n", argv[0]); |
| 13 | return 1; |
| 14 | } |
| 15 | |
| 16 | union |
| 17 | { |
| 18 | struct in_addr in; |
| 19 | struct in6_addr in6; |
| 20 | } buf; |
| 21 | int af = AF_INET6; |
| 22 | if (inet_pton(af, argv[1], &buf) != 1) |
| 23 | { |
| 24 | af = AF_INET; |
| 25 | if (inet_pton(af, argv[1], &buf) != 1) |
| 26 | { |
| 27 | fprintf(stderr, "Invalid IP Address: %s\n", argv[1]); |
| 28 | return 2; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | char str[INET6_ADDRSTRLEN]; |
| 33 | if (inet_ntop(af, &buf, str, sizeof(str)) == NULL) |
| 34 | { |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | printf("%s\n", str); |
| 39 | return 0; |
| 40 | } |