4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
42 * Private resolver of target and type with arguments:
43 * klooukp [ target [ RR_type ] ]
45 * Utitilizes DNS lookups to discover domain and realm information. This CLI
46 * is used primarily by kdcmgr(1M) and kclient(1M).
51 main(int argc
, char **argv
)
53 unsigned char answer
[NS_MAXMSG
], *ansp
= NULL
, *end
, a
, b
, c
, d
;
54 int len
= 0, anslen
, hostlen
, nq
, na
, type
, class;
55 int ttl
, priority
, weight
, port
, size
;
56 char name
[NS_MAXDNAME
], *cp
, *typestr
= NULL
;
57 char nbuf
[INET6_ADDRSTRLEN
];
58 struct __res_state stat
;
67 if (gethostname(name
, MAXHOSTNAMELEN
) != 0)
70 (void) strncpy(name
, (char *)argv
[1], NS_MAXDNAME
);
86 (void) sscanf(name
, "%hhd.%hhd.%hhd.%hhd",
88 (void) sprintf(name
, "%d.%d.%d.%d.in-addr.arpa",
100 (void) memset(&stat
, 0, sizeof (stat
));
102 if (res_ninit(&stat
) == -1)
105 anslen
= sizeof (answer
);
106 len
= res_nsearch(&stat
, name
, C_IN
, rr_type
, answer
, anslen
);
108 if (len
< sizeof (HEADER
)) {
117 h
= (HEADER
*)answer
;
118 nq
= ntohs(h
->qdcount
);
119 na
= ntohs(h
->ancount
);
122 if (nq
!= 1 || na
< 1) {
127 hostlen
= sizeof (name
);
128 len
= dn_expand(answer
, end
, ansp
, name
, hostlen
);
134 ansp
+= len
+ QFIXEDSZ
;
141 while (na
-- > 0 && ansp
< end
) {
143 len
= dn_expand(answer
, end
, ansp
, name
, hostlen
);
147 ansp
+= len
; /* name */
148 NS_GET16(type
, ansp
); /* type */
149 NS_GET16(class, ansp
); /* class */
150 NS_GET32(ttl
, ansp
); /* ttl */
151 NS_GET16(size
, ansp
); /* size */
153 if ((ansp
+ size
) > end
) {
158 NS_GET16(priority
, ansp
);
159 NS_GET16(weight
, ansp
);
160 NS_GET16(port
, ansp
);
161 len
= dn_expand(answer
, end
, ansp
, name
, hostlen
);
166 for (cp
= name
; *cp
; cp
++) {
169 (void) printf("%s %d\n", name
, port
);
170 } else if (typestr
&& *typestr
== 'I') {
171 (void) inet_ntop(AF_INET
, (void *)ansp
, nbuf
,
174 (void) printf("%s\n", nbuf
);
175 } else if (type
== T_PTR
) {
176 len
= dn_expand(answer
, end
, ansp
, name
, hostlen
);
183 if (type
== rr_type
&& class == C_IN
) {
185 if (type
!= T_SRV
&& !(typestr
&& *typestr
== 'I'))
195 for (cp
= name
; *cp
; cp
++) {
199 if (type
!= T_SRV
&& !(typestr
&& *typestr
== 'I'))
200 (void) printf("%s\n", name
);