diff options
Diffstat (limited to 'src/regress/lib/libc/asr/bin/getaddrinfo.c')
-rw-r--r-- | src/regress/lib/libc/asr/bin/getaddrinfo.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/regress/lib/libc/asr/bin/getaddrinfo.c b/src/regress/lib/libc/asr/bin/getaddrinfo.c new file mode 100644 index 0000000000..8c4abf361e --- /dev/null +++ b/src/regress/lib/libc/asr/bin/getaddrinfo.c | |||
@@ -0,0 +1,131 @@ | |||
1 | /* $OpenBSD: getaddrinfo.c,v 1.1.1.1 2012/07/13 17:49:54 eric Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | #include <sys/types.h> | ||
18 | #include <sys/socket.h> | ||
19 | |||
20 | #include <netinet/in.h> | ||
21 | |||
22 | #include <err.h> | ||
23 | #include <errno.h> | ||
24 | #include <getopt.h> | ||
25 | #include <netdb.h> | ||
26 | #include <stdio.h> | ||
27 | #include <stdlib.h> | ||
28 | #include <string.h> | ||
29 | |||
30 | #include "common.h" | ||
31 | |||
32 | static void | ||
33 | usage(void) | ||
34 | { | ||
35 | extern const char * __progname; | ||
36 | |||
37 | fprintf(stderr, "usage: %s [-CHSPe] [-f family] [-p proto] " | ||
38 | "[-s servname]\n [-t socktype] <host...>\n", __progname); | ||
39 | exit(1); | ||
40 | } | ||
41 | |||
42 | int | ||
43 | main(int argc, char *argv[]) | ||
44 | { | ||
45 | struct addrinfo *ai, *res, hints; | ||
46 | char *servname = NULL, *host; | ||
47 | int i, ch; | ||
48 | |||
49 | memset(&hints, 0, sizeof hints); | ||
50 | |||
51 | while((ch = getopt(argc, argv, "CFHPSef:p:s:t:")) != -1) { | ||
52 | switch(ch) { | ||
53 | case 'C': | ||
54 | hints.ai_flags |= AI_CANONNAME; | ||
55 | break; | ||
56 | case 'F': | ||
57 | hints.ai_flags |= AI_FQDN; | ||
58 | break; | ||
59 | case 'H': | ||
60 | hints.ai_flags |= AI_NUMERICHOST; | ||
61 | break; | ||
62 | case 'P': | ||
63 | hints.ai_flags |= AI_PASSIVE; | ||
64 | break; | ||
65 | case 'S': | ||
66 | hints.ai_flags |= AI_NUMERICSERV; | ||
67 | break; | ||
68 | case 'e': | ||
69 | long_err += 1; | ||
70 | break; | ||
71 | case 'f': | ||
72 | if (!strcmp(optarg, "inet")) | ||
73 | hints.ai_family = AF_INET; | ||
74 | else if (!strcmp(optarg, "inet6")) | ||
75 | hints.ai_family = AF_INET6; | ||
76 | else | ||
77 | usage(); | ||
78 | break; | ||
79 | case 'p': | ||
80 | if (!strcmp(optarg, "udp")) | ||
81 | hints.ai_protocol = IPPROTO_UDP; | ||
82 | else if (!strcmp(optarg, "tcp")) | ||
83 | hints.ai_protocol = IPPROTO_TCP; | ||
84 | else | ||
85 | usage(); | ||
86 | break; | ||
87 | case 's': | ||
88 | servname = optarg; | ||
89 | break; | ||
90 | case 't': | ||
91 | if (!strcmp(optarg, "stream")) | ||
92 | hints.ai_socktype = SOCK_STREAM; | ||
93 | else if (!strcmp(optarg, "dgram")) | ||
94 | hints.ai_socktype = SOCK_DGRAM; | ||
95 | else if (!strcmp(optarg, "raw")) | ||
96 | hints.ai_socktype = SOCK_RAW; | ||
97 | else | ||
98 | usage(); | ||
99 | break; | ||
100 | default: | ||
101 | usage(); | ||
102 | /* NOTREACHED */ | ||
103 | } | ||
104 | } | ||
105 | argc -= optind; | ||
106 | argv += optind; | ||
107 | |||
108 | for(i = 0; i < argc; i++) { | ||
109 | |||
110 | if (i) | ||
111 | printf("\n"); | ||
112 | printf("===> \"%s\"\n", argv[i]); | ||
113 | host = gethostarg(argv[i]); | ||
114 | |||
115 | errno = 0; | ||
116 | h_errno = 0; | ||
117 | gai_errno = 0; | ||
118 | rrset_errno = 0; | ||
119 | |||
120 | gai_errno = getaddrinfo(host, servname, &hints, &ai); | ||
121 | |||
122 | print_errors(); | ||
123 | if (gai_errno == 0) { | ||
124 | for (res = ai; res; res = res->ai_next) | ||
125 | print_addrinfo(res); | ||
126 | freeaddrinfo(ai); | ||
127 | } | ||
128 | } | ||
129 | |||
130 | return (0); | ||
131 | } | ||