home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume21 / coda / part01.Z / part01.bin / hostdb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-08  |  4.2 KB  |  219 lines

  1. /*
  2. **  Copyright 1989 BBN Systems and Technologies Corporation.
  3. **  All Rights Reserved.
  4. **  This is free software, and may be distributed under the terms of the
  5. **  GNU Public License; see the file COPYING for more details.
  6. **
  7. **  An implementation of the 4.2BSD hostname lookup routines.
  8. **  Not based on licensed code.
  9. */
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #ifdef    XENIX
  13. #include <netdb.h>
  14. #define HAVE_HOSTENT
  15. /* Stupid, just plain stupid... */
  16. #undef NULL
  17. #define NULL        0
  18. #endif    /* XENIX */
  19. #ifdef    RCSID
  20. static char RCS[] =
  21.     "$Header: hostdb.c,v 2.0 90/03/23 14:41:30 rsalz Exp $";
  22. #endif    /* RCSID */
  23.  
  24. /* Constants and shorthands. */
  25. #define TRUE        1
  26. #define FALSE        0
  27. #define MAXALIASES    10
  28. #define HOSTFILENAME    "/etc/hosts"
  29. #define ADDRLEN        4
  30.  
  31. #define WHITE(c)    ((c) == ' ' || (c) == '\t')
  32.  
  33. #ifndef    AF_INET
  34. #define AF_INET        2
  35. #endif    /* AF_INET */
  36.  
  37.  
  38. #ifndef    HAVE_HOSTENT
  39. struct hostent {
  40.     char    *h_name;        /* The host's official name    */
  41.     char    **h_aliases;        /* A/K/A list            */
  42.     int        h_addrtype;        /* Address type            */
  43.     int        h_length;        /* Size of the address        */
  44.     char    *h_addr;        /* Pointer to the address    */
  45. };
  46. #endif    /* HAVE_HOSTENT */
  47.  
  48.  
  49. static FILE    *HostFile = NULL;    /* The open /etc/hosts file    */
  50.  
  51.  
  52. extern char    *strchr();
  53.  
  54.  
  55. /*
  56. **  Close the host file.
  57. */
  58. void
  59. endhostent()
  60. {
  61.     if (HostFile) {
  62.     (void)fclose(HostFile);
  63.     HostFile = NULL;
  64.     }
  65. }
  66.  
  67.  
  68. /*
  69. **  Read the next line from the host file.
  70. */
  71. static struct hostent *
  72. gethostent()
  73. {
  74.     static struct hostent    E;
  75.     static char            buff[200];
  76.     static char            *aliases[MAXALIASES];
  77.     static char            addr[ADDRLEN];
  78.     register int        i;
  79.     register char        *q;
  80.     register char        *p;
  81.  
  82.     /* Open the file if necessary, reset state. */
  83.     if (HostFile == NULL) {
  84.     if ((HostFile = fopen(HOSTFILENAME, "r")) == NULL)
  85.         return NULL;
  86.     E.h_addrtype = AF_INET;
  87.     E.h_length = ADDRLEN;
  88.     E.h_addr = addr;
  89.         E.h_aliases = aliases;
  90.     }
  91.  
  92.     /* Loop until we get a good line. */
  93.     for ( ; ; ) {
  94.     if (fgets(buff, sizeof buff, HostFile) == NULL)
  95.         return NULL;
  96.  
  97.     /* Kill the comment and newline character, skip blank lines. */
  98.     if (p = strchr(buff, '#'))
  99.         *p = '\0';
  100.     if (p = strchr(buff, '\n'))
  101.         *p = '\0';
  102.     if (buff[0] == '\0')
  103.         continue;
  104.  
  105.     /* Parse the first field as a dotted quad. */
  106.     for (q = p = buff, i = 0; i < ADDRLEN; i++) {
  107.         while (*p && *p != '.' && !WHITE(*p))
  108.         p++;
  109.         if (*p == '\0')
  110.         break;
  111.         *p = '\0';
  112.         addr[i] = atoi(q);
  113.         q = ++p;
  114.     }
  115.     if (i == ADDRLEN)
  116.         break;
  117.     }
  118.  
  119.     /* Skip whitespace, get the second field as the full name. */
  120.     while (*q && WHITE(*q))
  121.     q++;
  122.     for (E.h_name = q; *q && !WHITE(*q); )
  123.     q++;
  124.     if (*q)
  125.     *q++ = '\0';
  126.  
  127.     /* Parse any aliases. */
  128.     for (i = 0; i < MAXALIASES - 1 && *q; i++) {
  129.         while (*q && WHITE(*q))
  130.         q++;
  131.     for (aliases[i] = q; *q && !WHITE(*q); )
  132.         q++;
  133.         if (*q)
  134.         *q++ = '\0';
  135.     }
  136.     aliases[i] = NULL;
  137.  
  138.     return &E;
  139. }
  140.  
  141.  
  142. /*
  143. **  Compare two strings, ignoring case.
  144. */
  145. static int
  146. samehostname(n1, n2)
  147.     register char    *n1;
  148.     register char    *n2;
  149. {
  150.     register char    c1;
  151.     register char    c2;
  152.  
  153.     for ( ; ; ) {
  154.     if ((c1 = *n1++) == (c2 = *n2++)) {
  155.         if (c1 || c2)
  156.         continue;
  157.         return TRUE;
  158.     }
  159.     if (islower(c1))
  160.         c1 = toupper(c1);
  161.     if (islower(c2))
  162.         c2 = toupper(c2);
  163.     if (c1 != c2)
  164.         return FALSE;
  165.     }
  166. }
  167.  
  168.  
  169. /*
  170. **  Given a name, find the hostent structure.
  171. */
  172. struct hostent *
  173. gethostbyname(name)
  174.     register char        *name;
  175. {
  176.     register struct hostent    *E;
  177.     register int        i;
  178.  
  179.     while (E = gethostent()) {
  180.     if (samehostname(name, E->h_name))
  181.         break;
  182.     for (i = 0; E->h_aliases[i]; i++)
  183.         if (samehostname(name, E->h_aliases[i]))
  184.         break;
  185.     if (E->h_aliases[i])
  186.         break;
  187.     }
  188.     endhostent();
  189.  
  190.     return E;
  191. }
  192.  
  193.  
  194. #if    0
  195. /*
  196. **  Given an address, find the hostent structure.
  197. */
  198. struct hostent *
  199. gethostbyaddr(p, length, type)
  200.     register char        *p;
  201.     int                length;
  202.     int                type;
  203. {
  204.     register struct hostent    *E;
  205.  
  206.     /* We only handle Internet addresses here. */
  207.     if (length != ADDRLEN || type != AF_INET)
  208.     return NULL;
  209.  
  210.     while (E = gethostent())
  211.     if (he->h_addr[0] == p[0] && he->h_addr[1] == p[1]
  212.      && he->h_addr[2] == p[2] && he->h_addr[3] == p[3])
  213.         break;
  214.     endhostent();
  215.  
  216.     return E;
  217. }
  218. #endif    /* 0 */
  219.