2 * Based on code found at https://dev.openwrt.org/ticket/4876 .
3 * Extended by Jo-Philipp Wich <jow@openwrt.org> for use in OpenWrt.
5 * You may use this program under the terms of the GPLv2 license.
10 #include <sys/types.h>
11 #include <sys/socket.h>
13 #include <arpa/inet.h>
14 #include <netinet/in.h>
20 static void abort_query(int sig)
25 static void show_usage(void)
28 printf(" resolveip -h\n");
29 printf(" resolveip [-t timeout] hostname\n");
30 printf(" resolveip -4 [-t timeout] hostname\n");
31 printf(" resolveip -6 [-t timeout] hostname\n");
35 int main(int argc, char **argv)
39 char ipaddr[INET6_ADDRSTRLEN];
41 struct addrinfo *res, *rp;
42 struct sigaction sa = { .sa_handler = &abort_query };
43 struct addrinfo hints = {
44 .ai_family = AF_UNSPEC,
45 .ai_socktype = SOCK_STREAM,
46 .ai_protocol = IPPROTO_TCP,
50 while ((opt = getopt(argc, argv, "46t:h")) > -1)
55 hints.ai_family = AF_INET;
59 hints.ai_family = AF_INET6;
63 timeout = atoi(optarg);
77 sigaction(SIGALRM, &sa, NULL);
80 if (getaddrinfo(argv[optind], NULL, &hints, &res))
83 for (rp = res; rp != NULL; rp = rp->ai_next)
85 addr = (rp->ai_family == AF_INET)
86 ? (void *)&((struct sockaddr_in *)rp->ai_addr)->sin_addr
87 : (void *)&((struct sockaddr_in6 *)rp->ai_addr)->sin6_addr
90 if (!inet_ntop(rp->ai_family, addr, ipaddr, INET6_ADDRSTRLEN - 1))
93 printf("%s\n", ipaddr);