author | Markus Bröker<broeker.markus@googlemail.com> |
Sun, 10 Feb 2019 13:17:01 +0100 | |
changeset 173 | 374a86886bc5 |
parent 154 | 142730cfecee |
permissions | -rw-r--r-- |
0 | 1 |
/** |
77 | 2 |
* dnsresolve.c |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
*/ |
5 |
||
6 |
#include <stdio.h> |
|
7 |
#include <stdlib.h> |
|
8 |
#include <string.h> |
|
154
142730cfecee
proper includes for getaddrinfo and inet_ntop
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
153
diff
changeset
|
9 |
#include <sys/types.h> |
142730cfecee
proper includes for getaddrinfo and inet_ntop
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
153
diff
changeset
|
10 |
#include <sys/socket.h> |
0 | 11 |
#include <arpa/inet.h> |
12 |
#include <netdb.h> |
|
154
142730cfecee
proper includes for getaddrinfo and inet_ntop
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
153
diff
changeset
|
13 |
#include <netinet/in.h> |
0 | 14 |
|
15 |
int main (int argc, char **argv) |
|
16 |
{ |
|
153 | 17 |
struct addrinfo hints; |
18 |
struct addrinfo *result, *rp; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
19 |
|
153 | 20 |
void *addr = NULL; |
21 |
char ip[INET6_ADDRSTRLEN]; |
|
0 | 22 |
|
23 |
if (argc != 2) { |
|
61
4b4c97f179da
Lazy BNF / EBNF Help Messages
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
48
diff
changeset
|
24 |
fprintf (stderr, "Usage: %s <hostname>\n", argv[0]); |
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
27
diff
changeset
|
25 |
return EXIT_FAILURE; |
0 | 26 |
} |
27 |
||
153 | 28 |
memset (&hints, 0, sizeof (hints)); |
29 |
hints.ai_family = AF_UNSPEC; |
|
30 |
hints.ai_socktype = SOCK_STREAM; |
|
31 |
||
32 |
if (getaddrinfo (argv[1], NULL, &hints, &result) != 0) { |
|
33 |
perror ("getaddrinfo"); |
|
34 |
return EXIT_FAILURE; |
|
35 |
} |
|
0 | 36 |
|
153 | 37 |
for (rp = result; rp != NULL; rp = rp->ai_next) { |
38 |
if (rp->ai_family == AF_INET6) { |
|
39 |
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)rp->ai_addr; |
|
40 |
addr = &(ipv6->sin6_addr); |
|
41 |
} else if (rp->ai_family == AF_INET) { |
|
42 |
struct sockaddr_in *ipv4 = (struct sockaddr_in *)rp->ai_addr; |
|
43 |
addr = &(ipv4->sin_addr); |
|
44 |
} |
|
45 |
if (addr != NULL) { |
|
46 |
if (inet_ntop (rp->ai_family, addr, ip, sizeof (ip)) != NULL) |
|
47 |
printf ("%s: %s\n", argv[1], ip); |
|
0 | 48 |
} |
49 |
} |
|
50 |
||
153 | 51 |
if (result != NULL) |
52 |
freeaddrinfo (result); |
|
53 |
||
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
54 |
return EXIT_SUCCESS; |
0 | 55 |
} |