diff options
Diffstat (limited to 'src/regress/lib/libc/asr/bin/gethostnamadr.c')
-rw-r--r-- | src/regress/lib/libc/asr/bin/gethostnamadr.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/regress/lib/libc/asr/bin/gethostnamadr.c b/src/regress/lib/libc/asr/bin/gethostnamadr.c new file mode 100644 index 0000000000..b1b3e331a7 --- /dev/null +++ b/src/regress/lib/libc/asr/bin/gethostnamadr.c | |||
@@ -0,0 +1,113 @@ | |||
1 | /* $OpenBSD: gethostnamadr.c,v 1.2 2012/07/29 19:51:36 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 | #include <netinet/in.h> | ||
20 | #include <arpa/inet.h> | ||
21 | |||
22 | #include <err.h> | ||
23 | #include <errno.h> | ||
24 | #include <getopt.h> | ||
25 | #include <stdio.h> | ||
26 | #include <stdlib.h> | ||
27 | #include <string.h> | ||
28 | |||
29 | #include "common.h" | ||
30 | |||
31 | static void | ||
32 | usage(void) | ||
33 | { | ||
34 | extern const char * __progname; | ||
35 | |||
36 | fprintf(stderr, "usage: %s [-46e] <host...>\n", __progname); | ||
37 | exit(1); | ||
38 | } | ||
39 | |||
40 | int | ||
41 | main(int argc, char *argv[]) | ||
42 | { | ||
43 | int i, ch, aflag, family = AF_INET; | ||
44 | struct hostent *h; | ||
45 | char *host; | ||
46 | char addr[16]; | ||
47 | int addrlen; | ||
48 | |||
49 | aflag = 0; | ||
50 | while((ch = getopt(argc, argv, "46ae")) != -1) { | ||
51 | switch(ch) { | ||
52 | case '4': | ||
53 | family = AF_INET; | ||
54 | break; | ||
55 | case '6': | ||
56 | family = AF_INET6; | ||
57 | break; | ||
58 | case 'a': | ||
59 | aflag = 1; | ||
60 | break; | ||
61 | case 'e': | ||
62 | long_err += 1; | ||
63 | break; | ||
64 | default: | ||
65 | usage(); | ||
66 | /* NOTREACHED */ | ||
67 | } | ||
68 | } | ||
69 | argc -= optind; | ||
70 | argv += optind; | ||
71 | |||
72 | for(i = 0; i < argc; i++) { | ||
73 | |||
74 | if (i) | ||
75 | printf("\n"); | ||
76 | printf("===> \"%s\"\n", argv[i]); | ||
77 | host = gethostarg(argv[i]); | ||
78 | |||
79 | if (aflag && addr_from_str(addr, &family, &addrlen, host) == -1) | ||
80 | errx(1, "bad address"); | ||
81 | |||
82 | errno = 0; | ||
83 | h_errno = 0; | ||
84 | gai_errno = 0; | ||
85 | rrset_errno = 0; | ||
86 | |||
87 | if (aflag == 0) | ||
88 | h = gethostbyname2(host, family); | ||
89 | else | ||
90 | h = gethostbyaddr(addr, addrlen, family); | ||
91 | if (h) | ||
92 | print_hostent(h); | ||
93 | print_errors(); | ||
94 | } | ||
95 | |||
96 | return (0); | ||
97 | } | ||
98 | |||
99 | int | ||
100 | addr_from_str(char *addr, int *family, int *len, const char *src) | ||
101 | { | ||
102 | if (inet_pton(AF_INET6, src, addr) == 1) { | ||
103 | *family = AF_INET6; | ||
104 | *len = 16; | ||
105 | return (0); | ||
106 | } | ||
107 | if (inet_pton(AF_INET, src, addr) == 1) { | ||
108 | *family = AF_INET; | ||
109 | *len = 4; | ||
110 | return (0); | ||
111 | } | ||
112 | return (-1); | ||
113 | } | ||