ar71xx: add diag support for the DB120 board
[openwrt.git] / package / resolveip / src / resolveip.c
1 /*
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.
4 *
5 * You may use this program under the terms of the GPLv2 license.
6 */
7
8 #include <string.h>
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <netdb.h>
13 #include <arpa/inet.h>
14 #include <netinet/in.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <signal.h>
18
19
20 static void abort_query(int sig)
21 {
22 exit(1);
23 }
24
25 static void show_usage(void)
26 {
27 printf("Usage:\n");
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");
32 exit(255);
33 }
34
35 int main(int argc, char **argv)
36 {
37 int timeout = 3;
38 char opt;
39 char ipaddr[INET6_ADDRSTRLEN];
40 void *addr;
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,
47 .ai_flags = 0
48 };
49
50 while ((opt = getopt(argc, argv, "46t:h")) > -1)
51 {
52 switch (opt)
53 {
54 case '4':
55 hints.ai_family = AF_INET;
56 break;
57
58 case '6':
59 hints.ai_family = AF_INET6;
60 break;
61
62 case 't':
63 timeout = atoi(optarg);
64 if (timeout <= 0)
65 show_usage();
66 break;
67
68 case 'h':
69 show_usage();
70 break;
71 }
72 }
73
74 if (!argv[optind])
75 show_usage();
76
77 sigaction(SIGALRM, &sa, NULL);
78 alarm(timeout);
79
80 if (getaddrinfo(argv[optind], NULL, &hints, &res))
81 exit(2);
82
83 for (rp = res; rp != NULL; rp = rp->ai_next)
84 {
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
88 ;
89
90 if (!inet_ntop(rp->ai_family, addr, ipaddr, INET6_ADDRSTRLEN - 1))
91 exit(3);
92
93 printf("%s\n", ipaddr);
94 }
95
96 freeaddrinfo(res);
97 exit(0);
98 }
This page took 0.044063 seconds and 5 git commands to generate.