"Fossies" - the Fresh Open Source Software Archive

Member "arpwatch-NG1.7/dns.c" (8 Feb 2005, 3753 Bytes) of archive /linux/misc/old/arpwatch-NG1.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "dns.c": 2.1a15_vs_1.7.

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