summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/asr/bin/gethostnamadr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/asr/bin/gethostnamadr.c')
-rw-r--r--src/regress/lib/libc/asr/bin/gethostnamadr.c112
1 files changed, 112 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..336c6b9db3
--- /dev/null
+++ b/src/regress/lib/libc/asr/bin/gethostnamadr.c
@@ -0,0 +1,112 @@
1/* $OpenBSD: gethostnamadr.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#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
31static void
32usage(void)
33{
34 extern const char * __progname;
35
36 fprintf(stderr, "usage: %s [-46e] <host...>\n", __progname);
37 exit(1);
38}
39
40int
41main(int argc, char *argv[])
42{
43 int i, ch, isname, family = AF_INET;
44 struct hostent *h;
45 char *host;
46 char addr[16];
47 int addraf;
48 int addrlen;
49
50 while((ch = getopt(argc, argv, "46e")) != -1) {
51 switch(ch) {
52 case '4':
53 family = AF_INET;
54 break;
55 case '6':
56 family = AF_INET6;
57 break;
58 case 'e':
59 long_err += 1;
60 break;
61 default:
62 usage();
63 /* NOTREACHED */
64 }
65 }
66 argc -= optind;
67 argv += optind;
68
69 for(i = 0; i < argc; i++) {
70
71 if (i)
72 printf("\n");
73 printf("===> \"%s\"\n", argv[i]);
74 host = gethostarg(argv[i]);
75
76 if (addr_from_str(addr, &addraf, &addrlen, argv[i]) == -1)
77 isname = 1;
78 else
79 isname = 0;
80
81 errno = 0;
82 h_errno = 0;
83 gai_errno = 0;
84 rrset_errno = 0;
85
86 if (isname)
87 h = gethostbyname2(host, family);
88 else
89 h = gethostbyaddr(addr, addrlen, addraf);
90 if (h)
91 print_hostent(h);
92 print_errors();
93 }
94
95 return (0);
96}
97
98int
99addr_from_str(char *addr, int *family, int *len, const char *src)
100{
101 if (inet_pton(AF_INET6, src, addr) == 1) {
102 *family = AF_INET6;
103 *len = 16;
104 return (0);
105 }
106 if (inet_pton(AF_INET, src, addr) == 1) {
107 *family = AF_INET;
108 *len = 4;
109 return (0);
110 }
111 return (-1);
112}