1: /*
   2:  * Copyright (c) 1983 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if defined(LIBC_SCCS) && !defined(lint)
   8: static char sccsid[] = "@(#)gethostnamadr.c	5.5 (Berkeley) 3/9/86";
   9: #endif LIBC_SCCS and not lint
  10: 
  11: #include <stdio.h>
  12: #include <sys/types.h>
  13: #include <netdb.h>
  14: #include <sys/file.h>
  15: #include <ndbm.h>
  16: #include <ctype.h>
  17: 
  18: #define MAXALIASES  20
  19: #define MAXADDRS    10
  20: 
  21: static struct hostent host;
  22: static char hostbuf[256];
  23: static char *host_aliases[MAXALIASES];
  24: static char *host_addrs[MAXADDRS];
  25: 
  26: int h_errno;
  27: 
  28: /*
  29:  * The following is shared with gethostent.c
  30:  */
  31: extern  char *_host_file;
  32: DBM *_host_db = (DBM *)NULL;
  33: int _host_stayopen; /* set by sethostent(), cleared by endhostent() */
  34: 
  35: static struct hostent *
  36: fetchhost(key)
  37:     datum key;
  38: {
  39:         register char *cp, *tp, **ap;
  40:     int naliases, naddrs;
  41: 
  42:         if (key.dptr == 0)
  43:                 return ((struct hostent *)NULL);
  44:     key = dbm_fetch(_host_db, key);
  45:     if (key.dptr == 0)
  46:                 return ((struct hostent *)NULL);
  47:     cp = key.dptr;
  48:         tp = hostbuf;
  49:     host.h_name = tp;
  50:     while (*tp++ = *cp++)
  51:         ;
  52:     bcopy(cp, (char *)&naliases, sizeof(int));
  53:     cp += sizeof (int);
  54:     for (ap = host_aliases; naliases > 0; naliases--) {
  55:         *ap++ = tp;
  56:         while (*tp++ = *cp++)
  57:             ;
  58:     }
  59:     *ap = (char *)NULL;
  60:     host.h_aliases = host_aliases;
  61:     bcopy(cp, (char *)&host.h_addrtype, sizeof (int));
  62:     cp += sizeof (int);
  63:     bcopy(cp, (char *)&host.h_length, sizeof (int));
  64:     cp += sizeof (int);
  65:     host.h_addr_list = host_addrs;
  66:     naddrs = (key.dsize - (cp - key.dptr)) / host.h_length;
  67:     if (naddrs > MAXADDRS)
  68:         naddrs = MAXADDRS;
  69:     for (ap = host_addrs; naddrs; naddrs--) {
  70:         *ap++ = tp;
  71:         bcopy(cp, tp, host.h_length);
  72:         cp += host.h_length;
  73:         tp += host.h_length;
  74:     }
  75:     *ap = (char *)NULL;
  76:         return (&host);
  77: }
  78: 
  79: struct hostent *
  80: gethostbyname(nam)
  81:     register char *nam;
  82: {
  83:     register struct hostent *hp;
  84:     register char **cp;
  85:         datum key;
  86:     char lowname[128];
  87:     register char *lp = lowname;
  88: 
  89:     while (*nam)
  90:         if (isupper(*nam))
  91:             *lp++ = tolower(*nam++);
  92:         else
  93:             *lp++ = *nam++;
  94:     *lp = '\0';
  95: 
  96:     if ((_host_db == (DBM *)NULL)
  97:       && ((_host_db = dbm_open(_host_file, O_RDONLY)) == (DBM *)NULL)) {
  98:         sethostent(_host_stayopen);
  99:         while (hp = gethostent()) {
 100:             if (strcmp(hp->h_name, lowname) == 0)
 101:                 break;
 102:             for (cp = hp->h_aliases; cp != 0 && *cp != 0; cp++)
 103:                 if (strcmp(*cp, lowname) == 0)
 104:                     goto found;
 105:         }
 106:     found:
 107:         if (!_host_stayopen)
 108:             endhostent();
 109:         return (hp);
 110:     }
 111:         key.dptr = lowname;
 112:         key.dsize = strlen(lowname);
 113:     hp = fetchhost(key);
 114:     if (!_host_stayopen) {
 115:         dbm_close(_host_db);
 116:         _host_db = (DBM *)NULL;
 117:     }
 118:     if ( hp == NULL)
 119:         h_errno = HOST_NOT_FOUND;
 120:         return (hp);
 121: }
 122: 
 123: struct hostent *
 124: gethostbyaddr(addr, length, type)
 125:     char *addr;
 126:     register int length;
 127:     register int type;
 128: {
 129:     register struct hostent *hp;
 130:         datum key;
 131: 
 132:     if ((_host_db == (DBM *)NULL)
 133:       && ((_host_db = dbm_open(_host_file, O_RDONLY)) == (DBM *)NULL)) {
 134:         sethostent(_host_stayopen);
 135:         while (hp = gethostent()) {
 136:             if (hp->h_addrtype == type && hp->h_length == length
 137:                 && bcmp(hp->h_addr, addr, length) == 0)
 138:                 break;
 139:         }
 140:         if (!_host_stayopen)
 141:             endhostent();
 142:         if ( hp == NULL)
 143:             h_errno = HOST_NOT_FOUND;
 144:         return (hp);
 145:     }
 146:         key.dptr = addr;
 147:         key.dsize = length;
 148:     hp = fetchhost(key);
 149:     if (!_host_stayopen) {
 150:         dbm_close(_host_db);
 151:         _host_db = (DBM *)NULL;
 152:     }
 153:     if ( hp == NULL)
 154:         h_errno = HOST_NOT_FOUND;
 155:         return (hp);
 156: }

Defined functions

fetchhost defined in line 35; used 2 times
gethostbyaddr defined in line 123; never used
gethostbyname defined in line 79; never used

Defined variables

_host_stayopen defined in line 33; used 6 times
h_errno defined in line 26; used 3 times
host defined in line 21; used 10 times
host_addrs defined in line 24; used 2 times
host_aliases defined in line 23; used 2 times
hostbuf defined in line 22; used 1 times
  • in line 48
sccsid defined in line 8; never used

Defined macros

MAXADDRS defined in line 19; used 3 times
MAXALIASES defined in line 18; used 1 times
  • in line 23
Last modified: 1989-10-07
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 4068
Valid CSS Valid XHTML 1.0 Strict