1*1fceb383Ssemery /* 2*1fceb383Ssemery * CDDL HEADER START 3*1fceb383Ssemery * 4*1fceb383Ssemery * The contents of this file are subject to the terms of the 5*1fceb383Ssemery * Common Development and Distribution License (the "License"). 6*1fceb383Ssemery * You may not use this file except in compliance with the License. 7*1fceb383Ssemery * 8*1fceb383Ssemery * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*1fceb383Ssemery * or http://www.opensolaris.org/os/licensing. 10*1fceb383Ssemery * See the License for the specific language governing permissions 11*1fceb383Ssemery * and limitations under the License. 12*1fceb383Ssemery * 13*1fceb383Ssemery * When distributing Covered Code, include this CDDL HEADER in each 14*1fceb383Ssemery * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*1fceb383Ssemery * If applicable, add the following below this CDDL HEADER, with the 16*1fceb383Ssemery * fields enclosed by brackets "[]" replaced with your own identifying 17*1fceb383Ssemery * information: Portions Copyright [yyyy] [name of copyright owner] 18*1fceb383Ssemery * 19*1fceb383Ssemery * CDDL HEADER END 20*1fceb383Ssemery */ 21*1fceb383Ssemery 22*1fceb383Ssemery /* 23*1fceb383Ssemery * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24*1fceb383Ssemery * Use is subject to license terms. 25*1fceb383Ssemery */ 26*1fceb383Ssemery 27*1fceb383Ssemery #pragma ident "%Z%%M% %I% %E% SMI" 28*1fceb383Ssemery 29*1fceb383Ssemery #include <sys/types.h> 30*1fceb383Ssemery #include <netinet/in.h> 31*1fceb383Ssemery #include <arpa/nameser.h> 32*1fceb383Ssemery #include <resolv.h> 33*1fceb383Ssemery #include <netdb.h> 34*1fceb383Ssemery #include <limits.h> 35*1fceb383Ssemery #include <stdlib.h> 36*1fceb383Ssemery #include <string.h> 37*1fceb383Ssemery #include <unistd.h> 38*1fceb383Ssemery #include <ctype.h> 39*1fceb383Ssemery 40*1fceb383Ssemery int 41*1fceb383Ssemery /* ARGSUSED */ 42*1fceb383Ssemery main(int argc, char **argv) 43*1fceb383Ssemery { 44*1fceb383Ssemery unsigned char answer[NS_MAXMSG], *ansp = NULL, *end; 45*1fceb383Ssemery int len = 0, anslen, hostlen, nq, na, type, class; 46*1fceb383Ssemery char hostname[MAXHOSTNAMELEN], *cp; 47*1fceb383Ssemery struct __res_state stat; 48*1fceb383Ssemery int found = 0; 49*1fceb383Ssemery HEADER *h; 50*1fceb383Ssemery 51*1fceb383Ssemery if (argc != 1) 52*1fceb383Ssemery exit(1); 53*1fceb383Ssemery 54*1fceb383Ssemery if (gethostname(hostname, MAXHOSTNAMELEN) != 0) 55*1fceb383Ssemery exit(1); 56*1fceb383Ssemery 57*1fceb383Ssemery (void) memset(&stat, 0, sizeof (stat)); 58*1fceb383Ssemery 59*1fceb383Ssemery if (res_ninit(&stat) == -1) 60*1fceb383Ssemery exit(1); 61*1fceb383Ssemery 62*1fceb383Ssemery anslen = sizeof (answer); 63*1fceb383Ssemery len = res_nsearch(&stat, hostname, C_IN, T_A, answer, anslen); 64*1fceb383Ssemery 65*1fceb383Ssemery if (len < sizeof (HEADER)) 66*1fceb383Ssemery exit(1); 67*1fceb383Ssemery 68*1fceb383Ssemery ansp = answer; 69*1fceb383Ssemery end = ansp + anslen; 70*1fceb383Ssemery 71*1fceb383Ssemery /* LINTED */ 72*1fceb383Ssemery h = (HEADER *)answer; 73*1fceb383Ssemery nq = ntohs(h->qdcount); 74*1fceb383Ssemery na = ntohs(h->ancount); 75*1fceb383Ssemery ansp += HFIXEDSZ; 76*1fceb383Ssemery 77*1fceb383Ssemery if (nq != 1 || na < 1) 78*1fceb383Ssemery exit(1); 79*1fceb383Ssemery 80*1fceb383Ssemery hostlen = sizeof (hostname); 81*1fceb383Ssemery len = dn_expand(answer, end, ansp, hostname, hostlen); 82*1fceb383Ssemery if (len < 0) 83*1fceb383Ssemery exit(1); 84*1fceb383Ssemery 85*1fceb383Ssemery ansp += len + QFIXEDSZ; 86*1fceb383Ssemery 87*1fceb383Ssemery if (ansp > end) 88*1fceb383Ssemery exit(1); 89*1fceb383Ssemery 90*1fceb383Ssemery while (na-- > 0 && ansp < end) { 91*1fceb383Ssemery len = dn_expand(answer, end, ansp, hostname, hostlen); 92*1fceb383Ssemery 93*1fceb383Ssemery if (len < 0) 94*1fceb383Ssemery continue; 95*1fceb383Ssemery ansp += len; /* hostname */ 96*1fceb383Ssemery type = ns_get16(ansp); 97*1fceb383Ssemery ansp += INT16SZ; /* type */ 98*1fceb383Ssemery class = ns_get16(ansp); 99*1fceb383Ssemery ansp += INT16SZ; /* class */ 100*1fceb383Ssemery ansp += INT32SZ; /* ttl */ 101*1fceb383Ssemery len = ns_get16(ansp); 102*1fceb383Ssemery ansp += INT16SZ; /* size */ 103*1fceb383Ssemery ansp += len; 104*1fceb383Ssemery if (type == T_A && class == C_IN) { 105*1fceb383Ssemery found = 1; 106*1fceb383Ssemery break; 107*1fceb383Ssemery } 108*1fceb383Ssemery } 109*1fceb383Ssemery 110*1fceb383Ssemery if (found != 1) 111*1fceb383Ssemery exit(1); 112*1fceb383Ssemery 113*1fceb383Ssemery for (cp = hostname; *cp; cp++) { 114*1fceb383Ssemery *cp = tolower(*cp); 115*1fceb383Ssemery } 116*1fceb383Ssemery 117*1fceb383Ssemery (void) printf("%s\n", hostname); 118*1fceb383Ssemery 119*1fceb383Ssemery return (0); 120*1fceb383Ssemery } 121