home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / install / dns.c < prev    next >
C/C++ Source or Header  |  1997-09-17  |  2KB  |  102 lines

  1. #include <alloca.h>
  2. #include <sys/socket.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <resolv.h>
  6. #include <arpa/nameser.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. /* This is dumb, but glibc doesn't like to do hostname lookups w/o libc.so */
  11.  
  12. union dns_response{
  13.     HEADER hdr;
  14.     u_char buf[PACKETSZ];
  15. } ;
  16.  
  17. static int doQuery(char * query, int queryType,
  18.            char ** domainName, struct in_addr * ipNum) {
  19.     int rc, len;
  20.     u_char * data, * end;
  21.     char name[MAXDNAME];
  22.     union dns_response response;
  23.  
  24.     _res.retry = 1;
  25.  
  26.     len = res_search(query, C_IN, queryType, (void *) &response, 
  27.             sizeof(response));
  28.     if (len <= 0) return -1;
  29.  
  30.     if (ntohs(response.hdr.rcode) != NOERROR) return -1;
  31.     if (ntohs(response.hdr.ancount) != 1) return -1;
  32.  
  33.     data = response.buf + sizeof(HEADER);
  34.     end = response.buf + len;
  35.     
  36.     /* skip the question */
  37.     data += dn_skipname(data, end) + QFIXEDSZ;
  38.  
  39.     /* skip the domain name portion of the RR record */
  40.     data += dn_skipname(data, end);
  41.  
  42.     /* skip the fixed RR data */
  43.     data += RRFIXEDSZ;
  44.     
  45.     if (queryType == T_PTR && domainName) {
  46.     if ((rc = dn_expand(response.buf, end, data, name, sizeof(name))) <= 0) 
  47.         return -1;
  48.  
  49.     if (domainName) {
  50.         *domainName = malloc(strlen(name) + 1);
  51.         strcpy(*domainName, name);
  52.     }
  53.     } 
  54.  
  55.     if (ipNum && queryType == T_A) {
  56.     memcpy(ipNum, data, sizeof(*ipNum));
  57.     }
  58.  
  59.  
  60.     return 0;
  61. }
  62.  
  63. char * mygethostbyaddr(char * ipnum) {
  64.     int rc;
  65.     char * result;
  66.     char * strbuf;
  67.     char * chptr;
  68.     char * splits[4];
  69.     int i;
  70.  
  71.     _res.retry = 1;
  72.  
  73.     strbuf = alloca(strlen(ipnum) + 1);
  74.     strcpy(strbuf, ipnum);
  75.  
  76.     ipnum = alloca(strlen(strbuf) + 20);
  77.  
  78.     for (i = 0; i < 4; i++) {
  79.     chptr = strbuf;
  80.     while (*chptr && *chptr != '.') chptr++;
  81.     *chptr = '\0';
  82.  
  83.     if (chptr - strbuf > 3) return NULL;
  84.     splits[i] = strbuf;
  85.     strbuf = chptr + 1;
  86.     }
  87.  
  88.     sprintf(ipnum, "%s.%s.%s.%s.in-addr.arpa", splits[3], splits[2],
  89.         splits[1], splits[0]);
  90.  
  91.     rc = doQuery(ipnum, T_PTR, &result, NULL);
  92.  
  93.     if (rc) 
  94.     return NULL;
  95.     else
  96.     return result;
  97. }
  98.  
  99. int mygethostbyname(char * name, struct in_addr * addr) {
  100.     return doQuery(name, T_A, NULL, addr);
  101. }
  102.