1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/*
* Copyright (c) 1992, 1993, 1994, 1995, 1996, 1998, 2000
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
static const char rcsid[] =
"@(#) $Id: dns.c,v 1.15 2000/09/30 23:40:40 leres Exp $ (LBL)";
#endif
/*
* dns - domain name system routines
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/inet.h>
#include <ctype.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <netdb.h>
#include <resolv.h>
#include <string.h>
#include <stdio.h>
#include "gnuc.h"
#ifdef HAVE_OS_PROTO_H
#include "os-proto.h"
#endif
#include "arpwatch.h"
#include "dns.h"
#ifdef HAVE_DN_SKIPNAME
#ifndef BUFSIZ
#define BUFSIZ 1024
#endif
static char hostbuf[BUFSIZ+1];
#if PACKETSZ > 1024
#define MAXPACKET PACKETSZ
#else
#define MAXPACKET 1024
#endif
typedef union {
HEADER hdr;
u_char buf[MAXPACKET];
} querybuf;
#endif
int
gethinfo(register char *hostname, register char *cpu, register int cpulen,
register char *os, register int oslen)
{
#ifdef HAVE_DN_SKIPNAME
register querybuf *qb;
register u_char *cp, *eom;
register char *bp;
register int n;
register HEADER *hp;
register int type, class, buflen, ancount, qdcount;
querybuf qbuf;
qb = &qbuf;
n = res_query(hostname, C_IN, T_HINFO, qb->buf, sizeof(qb->buf));
if (n < 0)
return (0);
eom = qb->buf + n;
/*
* find first satisfactory answer
*/
hp = &qb->hdr;
ancount = ntohs(hp->ancount);
qdcount = ntohs(hp->qdcount);
bp = hostbuf;
buflen = sizeof(hostbuf);
cp = qb->buf + sizeof(HEADER);
if (qdcount) {
cp += dn_skipname(cp, eom) + QFIXEDSZ;
while (--qdcount > 0)
cp += dn_skipname(cp, eom) + QFIXEDSZ;
}
while (--ancount >= 0 && cp < eom) {
if ((n = dn_expand((u_char *)qb->buf, (u_char *)eom,
(u_char *)cp, (u_char *)bp, buflen)) < 0)
break;
cp += n;
type = ns_get16(cp);
cp += sizeof(u_short);
class = ns_get16(cp);
cp += sizeof(u_short) + sizeof(u_int32_t);
n = ns_get16(cp);
cp += sizeof(u_short);
if (type == T_HINFO) {
/* Unpack */
n = *cp++;
if (n > cpulen - 1)
return (0);
BCOPY(cp, cpu, n);
cp += n;
cpu[n] = '\0';
n = *cp++;
if (n > oslen - 1)
return (0);
BCOPY(cp, os, n);
os[n] = '\0';
return (1);
}
/* Skip unexpected junk */
cp += n;
}
#endif
return (0);
}
/* Return the cannonical name of the host */
char *
gethname(u_int32_t a)
{
register int32_t options;
register struct hostent *hp;
options = _res.options;
_res.options |= RES_AAONLY;
_res.options &= ~(RES_DEFNAMES | RES_DNSRCH);
hp = gethostbyaddr((char *)&a, sizeof(a), AF_INET);
_res.options = options;
if (hp == NULL)
return (intoa(a));
return (hp->h_name);
}
/* Return the simple name of the host */
char *
getsname(register u_int32_t a)
{
register char *s, *cp;
s = gethname(a);
if (!isdigit((int)*s)) {
cp = strchr(s, '.');
if (cp != NULL)
*cp = '\0';
}
return (s);
}
|