]> Pileus Git - ~andy/fetchmail/blob - contrib/gai.c
Make compatible with FreeBSD.
[~andy/fetchmail] / contrib / gai.c
1 /*
2  * File:   gai.c
3  * Author: Matthias Andree
4  *
5  * Created on 3. Februar 2013, 15:03
6  * A short file to call getaddrinfo with the same arguments as checkalias.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netdb.h>
16
17 /*
18  *
19  */
20 int main(int argc, char** argv) {
21     struct addrinfo hints;
22     struct addrinfo *res;
23
24     if (argc != 2 || 0 == strcmp("-h", argv[1])) {
25         fprintf(stderr, "Usage: %s hostname\n", argv[0]);
26         exit(EXIT_FAILURE);
27     }
28
29     memset(&hints, 0, sizeof hints);
30     hints.ai_family=AF_UNSPEC;
31     hints.ai_protocol=PF_UNSPEC;
32     hints.ai_socktype=SOCK_STREAM;
33     hints.ai_flags=AI_CANONNAME;
34
35     int result = getaddrinfo(argv[1], NULL, &hints, &res);
36     if (result) {
37         fprintf(stderr, "getaddrinfo(\"%s\", ...AI_CANONNAME...) failed: %d (%s)\n", argv[1], result, gai_strerror(result));
38         exit(EXIT_FAILURE);
39     }
40
41     freeaddrinfo(res);
42     return (EXIT_SUCCESS);
43 }
44