1 /* 2 * Copyright 2021 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <arpa/inet.h> 18 #include <stdio.h> 19 20 int main(int argc, char* argv[]) 21 { 22 if (argc < 1) 23 { 24 return 1; 25 } 26 if (argc != 2) 27 { 28 fprintf(stderr, "Usage: %s <ip address>\n", argv[0]); 29 return 1; 30 } 31 32 union 33 { 34 struct in_addr in; 35 struct in6_addr in6; 36 } buf; 37 int af = AF_INET6; 38 if (inet_pton(af, argv[1], &buf) != 1) 39 { 40 af = AF_INET; 41 if (inet_pton(af, argv[1], &buf) != 1) 42 { 43 fprintf(stderr, "Invalid IP Address: %s\n", argv[1]); 44 return 2; 45 } 46 } 47 48 char str[INET6_ADDRSTRLEN]; 49 if (inet_ntop(af, &buf, str, sizeof(str)) == NULL) 50 { 51 return 1; 52 } 53 54 printf("%s\n", str); 55 return 0; 56 } 57