home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / bootpd-2.zip / READFILE.C < prev    next >
C/C++ Source or Header  |  1995-09-04  |  48KB  |  2,102 lines

  1. /************************************************************************
  2.           Copyright 1988, 1991 by Carnegie Mellon University
  3.  
  4.                           All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute this software and its
  7. documentation for any purpose and without fee is hereby granted, provided
  8. that the above copyright notice appear in all copies and that both that
  9. copyright notice and this permission notice appear in supporting
  10. documentation, and that the name of Carnegie Mellon University not be used
  11. in advertising or publicity pertaining to distribution of the software
  12. without specific, written prior permission.
  13.  
  14. CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15. SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16. IN NO EVENT SHALL CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  17. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  18. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  19. ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20. SOFTWARE.
  21. ************************************************************************/
  22.  
  23. /*
  24.  * bootpd configuration file reading code.
  25.  *
  26.  * The routines in this file deal with reading, interpreting, and storing
  27.  * the information found in the bootpd configuration file (usually
  28.  * /etc/bootptab).
  29.  */
  30.  
  31.  
  32. #include <sys/errno.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <sys/file.h>
  36. #include <sys/time.h>
  37. #include <netinet/in.h>
  38.  
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <ctype.h>
  43. #include <assert.h>
  44. #include <syslog.h>
  45.  
  46. #ifndef USE_BFUNCS
  47. #include <memory.h>
  48. /* Yes, memcpy is OK here (no overlapped copies). */
  49. #define    bcopy(a,b,c)    memcpy(b,a,c)
  50. #define    bzero(p,l)    memset(p,0,l)
  51. #define    bcmp(a,b,c)    memcmp(a,b,c)
  52. #endif
  53.  
  54. #include "bootp.h"
  55. #include "hash.h"
  56. #include "hwaddr.h"
  57. #include "lookup.h"
  58. #include "readfile.h"
  59. #include "report.h"
  60. #include "tzone.h"
  61. #include "bootpd.h"
  62.  
  63. #define HASHTABLESIZE        257    /* Hash table size (prime) */
  64.  
  65. /* Non-standard hardware address type (see bootp.h) */
  66. #define HTYPE_DIRECT    0
  67.  
  68. /* Error codes returned by eval_symbol: */
  69. #define SUCCESS              0
  70. #define E_END_OF_ENTRY        (-1)
  71. #define E_SYNTAX_ERROR        (-2)
  72. #define E_UNKNOWN_SYMBOL    (-3)
  73. #define E_BAD_IPADDR        (-4)
  74. #define E_BAD_HWADDR        (-5)
  75. #define E_BAD_LONGWORD        (-6)
  76. #define E_BAD_HWATYPE        (-7)
  77. #define E_BAD_PATHNAME        (-8)
  78. #define E_BAD_VALUE         (-9)
  79.  
  80. /* Tag idendities. */
  81. #define SYM_NULL          0
  82. #define SYM_BOOTFILE          1
  83. #define SYM_COOKIE_SERVER      2
  84. #define SYM_DOMAIN_SERVER      3
  85. #define SYM_GATEWAY          4
  86. #define SYM_HWADDR          5
  87. #define SYM_HOMEDIR          6
  88. #define SYM_HTYPE          7
  89. #define SYM_IMPRESS_SERVER      8
  90. #define SYM_IPADDR          9
  91. #define SYM_LOG_SERVER         10
  92. #define SYM_LPR_SERVER         11
  93. #define SYM_NAME_SERVER         12
  94. #define SYM_RLP_SERVER         13
  95. #define SYM_SUBNET_MASK         14
  96. #define SYM_TIME_OFFSET         15
  97. #define SYM_TIME_SERVER         16
  98. #define SYM_VENDOR_MAGIC     17
  99. #define SYM_SIMILAR_ENTRY     18
  100. #define SYM_NAME_SWITCH         19
  101. #define SYM_BOOTSIZE         20
  102. #define SYM_BOOT_SERVER         22
  103. #define SYM_TFTPDIR         23
  104. #define SYM_DUMP_FILE         24
  105. #define SYM_DOMAIN_NAME          25
  106. #define SYM_SWAP_SERVER          26
  107. #define SYM_ROOT_PATH            27
  108. #define SYM_EXTEN_FILE           28
  109. #define SYM_REPLY_ADDR           29
  110. #define SYM_NIS_DOMAIN           30    /* RFC 1533 */
  111. #define SYM_NIS_SERVER           31    /* RFC 1533 */
  112. #define SYM_NTP_SERVER           32    /* RFC 1533 */
  113. #define SYM_EXEC_FILE         33    /* YORK_EX_OPTION */
  114. #define SYM_MSG_SIZE          34
  115. #define SYM_MIN_WAIT         35
  116. #define SYM_DHCP_LEASE         36    /* RFC 1533 PeP hic facet */
  117. #define SYM_DHCP_IPRENEW     37
  118. #define SYM_DHCP_IPREBIND     38
  119. /* XXX - Add new tags here */
  120.  
  121. #define OP_ADDITION          1    /* Operations on tags */
  122. #define OP_DELETION          2
  123. #define OP_BOOLEAN          3
  124.  
  125. #define MAXINADDRS         16    /* Max size of an IP address list */
  126. #define MAXBUFLEN        256    /* Max temp buffer space */
  127. #define MAXENTRYLEN           2048    /* Max size of an entire entry */
  128.  
  129.  
  130.  
  131. /*
  132.  * Structure used to map a configuration-file symbol (such as "ds") to a
  133.  * unique integer.
  134.  */
  135.  
  136. struct symbolmap {
  137.     char *symbol;
  138.     int symbolcode;
  139. };
  140.  
  141.  
  142. struct htypename {
  143.     char *name;
  144.     byte htype;
  145. };
  146.  
  147.  
  148. PRIVATE int nhosts;                /* Number of hosts (/w hw or IP address) */
  149. PRIVATE int nentries;            /* Total number of entries */
  150. PRIVATE int32 modtime = 0;        /* Last modification time of bootptab */
  151. PRIVATE char *current_hostname;    /* Name of the current entry. */
  152. PRIVATE char current_tagname[8];
  153.  
  154. /*
  155.  * List of symbolic names used in the bootptab file.  The order and actual
  156.  * values of the symbol codes (SYM_. . .) are unimportant, but they must
  157.  * all be unique.
  158.  */
  159.  
  160. PRIVATE struct symbolmap symbol_list[] = {
  161.     {"bf", SYM_BOOTFILE},
  162.     {"bs", SYM_BOOTSIZE},
  163.     {"cs", SYM_COOKIE_SERVER},
  164.     {"df", SYM_DUMP_FILE},
  165.     {"dn", SYM_DOMAIN_NAME},
  166.     {"ds", SYM_DOMAIN_SERVER},
  167. #ifdef DHCP
  168.     {"dl", SYM_DHCP_LEASE},        /* PeP hic facet */
  169. #endif
  170.     {"ef", SYM_EXTEN_FILE},
  171.     {"ex", SYM_EXEC_FILE},        /* YORK_EX_OPTION */
  172.     {"gw", SYM_GATEWAY},
  173.     {"ha", SYM_HWADDR},
  174.     {"hd", SYM_HOMEDIR},
  175.     {"hn", SYM_NAME_SWITCH},
  176.     {"ht", SYM_HTYPE},
  177.     {"im", SYM_IMPRESS_SERVER},
  178.     {"ip", SYM_IPADDR},
  179.     {"lg", SYM_LOG_SERVER},
  180.     {"lp", SYM_LPR_SERVER},
  181.     {"ms", SYM_MSG_SIZE},
  182.     {"mw", SYM_MIN_WAIT},
  183.     {"ns", SYM_NAME_SERVER},
  184.     {"nt", SYM_NTP_SERVER},
  185.     {"ra", SYM_REPLY_ADDR},
  186.     {"rl", SYM_RLP_SERVER},
  187.     {"rp", SYM_ROOT_PATH},
  188.     {"sa", SYM_BOOT_SERVER},
  189.     {"sm", SYM_SUBNET_MASK},
  190.     {"sw", SYM_SWAP_SERVER},
  191.     {"tc", SYM_SIMILAR_ENTRY},
  192.     {"td", SYM_TFTPDIR},
  193.     {"to", SYM_TIME_OFFSET},
  194.     {"ts", SYM_TIME_SERVER},
  195.     {"vm", SYM_VENDOR_MAGIC},
  196.     {"yd", SYM_NIS_DOMAIN},
  197.     {"ys", SYM_NIS_SERVER},
  198.     /* XXX - Add new tags here */
  199. };
  200.  
  201.  
  202. /*
  203.  * List of symbolic names for hardware types.  Name translates into
  204.  * hardware type code listed with it.  Names must begin with a letter
  205.  * and must be all lowercase.  This is searched linearly, so put
  206.  * commonly-used entries near the beginning.
  207.  */
  208.  
  209. PRIVATE struct htypename htnamemap[] = {
  210.     {"ethernet", HTYPE_ETHERNET},
  211.     {"ethernet3", HTYPE_EXP_ETHERNET},
  212.     {"ether", HTYPE_ETHERNET},
  213.     {"ether3", HTYPE_EXP_ETHERNET},
  214.     {"ieee802", HTYPE_IEEE802},
  215.     {"tr", HTYPE_IEEE802},
  216.     {"token-ring", HTYPE_IEEE802},
  217.     {"pronet", HTYPE_PRONET},
  218.     {"chaos", HTYPE_CHAOS},
  219.     {"arcnet", HTYPE_ARCNET},
  220.     {"ax.25", HTYPE_AX25},
  221.     {"direct", HTYPE_DIRECT},
  222.     {"serial", HTYPE_DIRECT},
  223.     {"slip", HTYPE_DIRECT},
  224.     {"ppp", HTYPE_DIRECT}
  225. };
  226.  
  227.  
  228.  
  229. /*
  230.  * Externals and forward declarations.
  231.  */
  232.  
  233. #ifdef    __STDC__
  234. #define P(args) args
  235. #else
  236. #define P(args) ()
  237. #endif
  238.  
  239. extern boolean iplookcmp();
  240. boolean nmcmp P((hash_datum *, hash_datum *));
  241.  
  242. PRIVATE void
  243.     adjust P((char **));
  244. PRIVATE void
  245.     del_string P((struct shared_string *));
  246. PRIVATE void
  247.     del_bindata P((struct shared_bindata *));
  248. PRIVATE void
  249.     del_iplist P((struct in_addr_list *));
  250. PRIVATE void
  251.     eat_whitespace P((char **));
  252. PRIVATE int
  253.     eval_symbol P((char **, struct host *));
  254. PRIVATE void
  255.     fill_defaults P((struct host *, char **));
  256. PRIVATE void
  257.     free_host P((hash_datum *));
  258. PRIVATE struct in_addr_list *
  259.     get_addresses P((char **));
  260. PRIVATE struct shared_string *
  261.     get_shared_string P((char **));
  262. PRIVATE char *
  263.     get_string P((char **, char *, u_int *));
  264. PRIVATE u_int32
  265.     get_u_long P((char **));
  266. PRIVATE boolean
  267.     goodname P((char *));
  268. PRIVATE boolean
  269.     hwinscmp P((hash_datum *, hash_datum *));
  270. PRIVATE int
  271.     interp_byte P((char **, byte *));
  272. PRIVATE void
  273.     makelower P((char *));
  274. PRIVATE boolean
  275.         nullcmp P((hash_datum *, hash_datum *));
  276. PRIVATE int
  277.     process_entry P((struct host *, char *));
  278. PRIVATE int
  279.     process_generic P((char **, struct shared_bindata **, u_int));
  280. PRIVATE byte *
  281.     prs_haddr P((char **, u_int));
  282. PRIVATE int
  283.     prs_inetaddr P((char **, u_int32 *));
  284. PRIVATE void
  285.     read_entry P((FILE *, char *, u_int *));
  286. PRIVATE char *
  287.     smalloc P((u_int));
  288.  
  289. #undef P
  290.  
  291.  
  292. /*
  293.  * Vendor magic cookies for CMU and RFC1048
  294.  */
  295. u_char vm_cmu[4] = VM_CMU;
  296. u_char vm_rfc1048[4] = VM_RFC1048;
  297.  
  298. /*
  299.  * Main hash tables
  300.  */
  301. hash_tbl *hwhashtable;
  302. hash_tbl *iphashtable;
  303. hash_tbl *nmhashtable;
  304.  
  305. /*
  306.  * Allocate hash tables for hardware address, ip address, and hostname
  307.  * (shared by bootpd and bootpef)
  308.  */
  309. void
  310. rdtab_init()
  311. {
  312.     hwhashtable = hash_Init(HASHTABLESIZE);
  313.     iphashtable = hash_Init(HASHTABLESIZE);
  314.     nmhashtable = hash_Init(HASHTABLESIZE);
  315.     if (!(hwhashtable && iphashtable && nmhashtable)) {
  316.         report(LOG_ERR, "Unable to allocate hash tables.");
  317.         exit(1);
  318.     }
  319. }
  320.  
  321.  
  322. /*
  323.  * Read bootptab database file.  Avoid rereading the file if the
  324.  * write date hasn't changed since the last time we read it.
  325.  */
  326.  
  327. void
  328. readtab(force)
  329.     int force;
  330. {
  331.     struct host *hp;
  332.     FILE *fp;
  333.     struct stat st;
  334.     unsigned hashcode, buflen;
  335.     static char buffer[MAXENTRYLEN];
  336.  
  337.     /*
  338.      * Check the last modification time.
  339.      */
  340.     if (stat(bootptab, &st) < 0) {
  341.         report(LOG_ERR, "stat on \"%s\": %s",
  342.                bootptab, get_errmsg());
  343.         return;
  344.     }
  345. #ifdef DEBUG
  346.     if (debug > 3) {
  347.         char timestr[28];
  348.         strcpy(timestr, ctime(&(st.st_mtime)));
  349.         /* zap the newline */
  350.         timestr[24] = '\0';
  351.         report(LOG_INFO, "bootptab mtime: %s",
  352.                timestr);
  353.     }
  354. #endif
  355.     if ((force == 0) &&
  356.         (st.st_mtime == modtime) &&
  357.         st.st_nlink) {
  358.         /*
  359.          * hasn't been modified or deleted yet.
  360.          */
  361.         return;
  362.     }
  363.     if (debug)
  364.         report(LOG_INFO, "reading %s\"%s\"",
  365.                (modtime != 0L) ? "new " : "",
  366.                bootptab);
  367.  
  368.     /*
  369.      * Open bootptab file.
  370.      */
  371.     if ((fp = fopen(bootptab, "r")) == NULL) {
  372.         report(LOG_ERR, "error opening \"%s\": %s", bootptab, get_errmsg());
  373.         return;
  374.     }
  375.     /*
  376.      * Record file modification time.
  377.      */
  378.     if (fstat(fileno(fp), &st) < 0) {
  379.         report(LOG_ERR, "fstat: %s", get_errmsg());
  380.         fclose(fp);
  381.         return;
  382.     }
  383.     modtime = st.st_mtime;
  384.  
  385.     /*
  386.      * Entirely erase all hash tables.
  387.      */
  388.     hash_Reset(hwhashtable, free_host);
  389.     hash_Reset(iphashtable, free_host);
  390.     hash_Reset(nmhashtable, free_host);
  391.  
  392.     nhosts = 0;
  393.     nentries = 0;
  394.     while (TRUE) {
  395.         buflen = sizeof(buffer);
  396.         read_entry(fp, buffer, &buflen);
  397.         if (buflen == 0) {        /* More entries? */
  398.             break;
  399.         }
  400.         hp = (struct host *) smalloc(sizeof(struct host));
  401.         bzero((char *) hp, sizeof(*hp));
  402.         /* the link count it zero */
  403.  
  404.         /*
  405.          * Get individual info
  406.          */
  407.         if (process_entry(hp, buffer) < 0) {
  408.             hp->linkcount = 1;
  409.             free_host((hash_datum *) hp);
  410.             continue;
  411.         }
  412.         /*
  413.          * If this is not a dummy entry, and the IP or HW
  414.          * address is not yet set, try to get them here.
  415.          * Dummy entries have . as first char of name.
  416.          */
  417.         if (goodname(hp->hostname->string)) {
  418.             char *hn = hp->hostname->string;
  419.             u_int32 value;
  420.             if (hp->flags.iaddr == 0) {
  421.                 if (lookup_ipa(hn, &value)) {
  422.                     report(LOG_ERR, "can not get IP addr for %s", hn);
  423.                     report(LOG_ERR, "(dummy names should start with '.')");
  424.                 } else {
  425.                     hp->iaddr.s_addr = value;
  426.                     hp->flags.iaddr = TRUE;
  427.                 }
  428.             }
  429.             /* Set default subnet mask. */
  430.             if (hp->flags.subnet_mask == 0) {
  431.                 if (lookup_netmask(hp->iaddr.s_addr, &value)) {
  432.                     report(LOG_ERR, "can not get netmask for %s", hn);
  433.                 } else {
  434.                     hp->subnet_mask.s_addr = value;
  435.                     hp->flags.subnet_mask = TRUE;
  436.                 }
  437.             }
  438.         }
  439.         if (hp->flags.iaddr) {
  440.             nhosts++;
  441.         }
  442.         /* Register by HW addr if known. */
  443.         if (hp->flags.htype && hp->flags.haddr) {
  444.             /* We will either insert it or free it. */
  445.             hp->linkcount++;
  446.             hashcode = hash_HashFunction(hp->haddr, haddrlength(hp->htype));
  447.             if (hash_Insert(hwhashtable, hashcode, hwinscmp, hp, hp) < 0) {
  448.                 report(LOG_NOTICE, "duplicate %s address: %s",
  449.                        netname(hp->htype),
  450.                        haddrtoa(hp->haddr, haddrlength(hp->htype)));
  451.                 free_host((hash_datum *) hp);
  452.                 continue;
  453.             }
  454.         }
  455.         /* Register by IP addr if known. */
  456.         if (hp->flags.iaddr) {
  457.             hashcode = hash_HashFunction((u_char *) & (hp->iaddr.s_addr), 4);
  458.             if (hash_Insert(iphashtable, hashcode, nullcmp, hp, hp) < 0) {
  459.                 report(LOG_ERR,
  460.                        "hash_Insert() failed on IP address insertion");
  461.             } else {
  462.                 /* Just inserted the host struct in a new hash list. */
  463.                 hp->linkcount++;
  464.             }
  465.         }
  466.         /* Register by Name (always known) */
  467.         hashcode = hash_HashFunction((u_char *) hp->hostname->string,
  468.                                      strlen(hp->hostname->string));
  469.         if (hash_Insert(nmhashtable, hashcode, nullcmp,
  470.                         hp->hostname->string, hp) < 0) {
  471.             report(LOG_ERR,
  472.                  "hash_Insert() failed on insertion of hostname: \"%s\"",
  473.                    hp->hostname->string);
  474.         } else {
  475.             /* Just inserted the host struct in a new hash list. */
  476.             hp->linkcount++;
  477.         }
  478.  
  479.         nentries++;
  480.     }
  481.  
  482.     fclose(fp);
  483.     if (debug)
  484.         report(LOG_INFO, "read %d entries (%d hosts) from \"%s\"",
  485.                nentries, nhosts, bootptab);
  486.     return;
  487. }
  488.  
  489.  
  490.  
  491. /*
  492.  * Read an entire host entry from the file pointed to by "fp" and insert it
  493.  * into the memory pointed to by "buffer".  Leading whitespace and comments
  494.  * starting with "#" are ignored (removed).  Backslashes (\) always quote
  495.  * the next character except that newlines preceeded by a backslash cause
  496.  * line-continuation onto the next line.  The entry is terminated by a
  497.  * newline character which is not preceeded by a backslash.  Sequences
  498.  * surrounded by double quotes are taken literally (including newlines, but
  499.  * not backslashes).
  500.  *
  501.  * The "bufsiz" parameter points to an unsigned int which specifies the
  502.  * maximum permitted buffer size.  Upon return, this value will be replaced
  503.  * with the actual length of the entry (not including the null terminator).
  504.  *
  505.  * This code is a little scary. . . .  I don't like using gotos in C
  506.  * either, but I first wrote this as an FSM diagram and gotos seemed like
  507.  * the easiest way to implement it.  Maybe later I'll clean it up.
  508.  */
  509.  
  510. PRIVATE void
  511. read_entry(fp, buffer, bufsiz)
  512.     FILE *fp;
  513.     char *buffer;
  514.     unsigned *bufsiz;
  515. {
  516.     int c, length;
  517.  
  518.     length = 0;
  519.  
  520.     /*
  521.      * Eat whitespace, blank lines, and comment lines.
  522.      */
  523.   top:
  524.     c = fgetc(fp);
  525.     if (c < 0) {
  526.         goto done;                /* Exit if end-of-file */
  527.     }
  528.     if (isspace(c)) {
  529.         goto top;                /* Skip over whitespace */
  530.     }
  531.     if (c == '#') {
  532.         while (TRUE) {            /* Eat comments after # */
  533.             c = fgetc(fp);
  534.             if (c < 0) {
  535.                 goto done;        /* Exit if end-of-file */
  536.             }
  537.             if (c == '\n') {
  538.                 goto top;        /* Try to read the next line */
  539.             }
  540.         }
  541.     }
  542.     ungetc(c, fp);                /* Other character, push it back to reprocess it */
  543.  
  544.  
  545.     /*
  546.      * Now we're actually reading a data entry.  Get each character and
  547.      * assemble it into the data buffer, processing special characters like
  548.      * double quotes (") and backslashes (\).
  549.      */
  550.  
  551.   mainloop:
  552.     c = fgetc(fp);
  553.     switch (c) {
  554.     case EOF:
  555.     case '\n':
  556.         goto done;                /* Exit on EOF or newline */
  557.     case '\\':
  558.         c = fgetc(fp);            /* Backslash, read a new character */
  559.         if (c < 0) {
  560.             goto done;            /* Exit on EOF */
  561.         }
  562.         *buffer++ = c;            /* Store the literal character */
  563.         length++;
  564.         if (length < *bufsiz - 1) {
  565.             goto mainloop;
  566.         } else {
  567.             goto done;
  568.         }
  569.     case '"':
  570.         *buffer++ = '"';        /* Store double-quote */
  571.         length++;
  572.         if (length >= *bufsiz - 1) {
  573.             goto done;
  574.         }
  575.         while (TRUE) {            /* Special quote processing loop */
  576.             c = fgetc(fp);
  577.             switch (c) {
  578.             case EOF:
  579.                 goto done;        /* Exit on EOF . . . */
  580.             case '"':
  581.                 *buffer++ = '"';/* Store matching quote */
  582.                 length++;
  583.                 if (length < *bufsiz - 1) {
  584.                     goto mainloop;    /* And continue main loop */
  585.                 } else {
  586.                     goto done;
  587.                 }
  588.             case '\\':
  589.                 if ((c = fgetc(fp)) < 0) {    /* Backslash */
  590.                     goto done;    /* EOF. . . .*/
  591.                 }                /* else fall through */
  592.             default:
  593.                 *buffer++ = c;    /* Other character, store it */
  594.                 length++;
  595.                 if (length >= *bufsiz - 1) {
  596.                     goto done;
  597.                 }
  598.             }
  599.         }
  600.     case ':':
  601.         *buffer++ = c;            /* Store colons */
  602.         length++;
  603.         if (length >= *bufsiz - 1) {
  604.             goto done;
  605.         }
  606.         do {                    /* But remove whitespace after them */
  607.             c = fgetc(fp);
  608.             if ((c < 0) || (c == '\n')) {
  609.                 goto done;
  610.             }
  611.         } while (isspace(c));    /* Skip whitespace */
  612.  
  613.         if (c == '\\') {        /* Backslash quotes next character */
  614.             c = fgetc(fp);
  615.             if (c < 0) {
  616.                 goto done;
  617.             }
  618.             if (c == '\n') {
  619.                 goto top;        /* Backslash-newline continuation */
  620.             }
  621.         }
  622.         /* fall through if "other" character */
  623.     default:
  624.         *buffer++ = c;            /* Store other characters */
  625.         length++;
  626.         if (length >= *bufsiz - 1) {
  627.             goto done;
  628.         }
  629.     }
  630.     goto mainloop;                /* Keep going */
  631.  
  632.   done:
  633.     *buffer = '\0';                /* Terminate string */
  634.     *bufsiz = length;            /* Tell the caller its length */
  635. }
  636.  
  637.  
  638.  
  639. /*
  640.  * Parse out all the various tags and parameters in the host entry pointed
  641.  * to by "src".  Stuff all the data into the appropriate fields of the
  642.  * host structure pointed to by "host".  If there is any problem with the
  643.  * entry, an error message is reported via report(), no further processing
  644.  * is done, and -1 is returned.  Successful calls return 0.
  645.  *
  646.  * (Some errors probably shouldn't be so completely fatal. . . .)
  647.  */
  648.  
  649. PRIVATE int
  650. process_entry(host, src)
  651.     struct host *host;
  652.     char *src;
  653. {
  654.     int retval;
  655.     char *msg;
  656.  
  657.     if (!host || *src == '\0') {
  658.         return -1;
  659.     }
  660.     host->hostname = get_shared_string(&src);
  661. #if 0
  662.     /* Be more liberal for the benefit of dummy tag names. */
  663.     if (!goodname(host->hostname->string)) {
  664.         report(LOG_ERR, "bad hostname: \"%s\"", host->hostname->string);
  665.         del_string(host->hostname);
  666.         return -1;
  667.     }
  668. #endif
  669.     current_hostname = host->hostname->string;
  670.     adjust(&src);
  671.     while (TRUE) {
  672.         retval = eval_symbol(&src, host);
  673.         if (retval == SUCCESS) {
  674.             adjust(&src);
  675.             continue;
  676.         }
  677.         if (retval == E_END_OF_ENTRY) {
  678.             /* The default subnet mask is set in readtab() */
  679.             return 0;
  680.         }
  681.         /* Some kind of error. */
  682.         switch (retval) {
  683.         case E_SYNTAX_ERROR:
  684.             msg = "bad syntax";
  685.             break;
  686.         case E_UNKNOWN_SYMBOL:
  687.             msg = "unknown symbol";
  688.             break;
  689.         case E_BAD_IPADDR:
  690.             msg = "bad INET address";
  691.             break;
  692.         case E_BAD_HWADDR:
  693.             msg = "bad hardware address";
  694.             break;
  695.         case E_BAD_LONGWORD:
  696.             msg = "bad longword value";
  697.             break;
  698.         case E_BAD_HWATYPE:
  699.             msg = "bad HW address type";
  700.             break;
  701.         case E_BAD_PATHNAME:
  702.             msg = "bad pathname (need leading '/')";
  703.         case E_BAD_VALUE:
  704.             msg = "bad value";
  705.         default:
  706.             msg = "unkown error";
  707.             break;
  708.         }                        /* switch */
  709.         report(LOG_ERR, "in entry named \"%s\", symbol \"%s\": %s",
  710.                current_hostname, current_tagname, msg);
  711.         return -1;
  712.     }
  713. }
  714.  
  715.  
  716. /*
  717.  * Macros for use in the function below:
  718.  */
  719.  
  720. /* Parse one INET address stored directly in MEMBER. */
  721. #define PARSE_IA1(MEMBER) do \
  722. { \
  723.     if (optype == OP_BOOLEAN) \
  724.         return E_SYNTAX_ERROR; \
  725.     hp->flags.MEMBER = FALSE; \
  726.     if (optype == OP_ADDITION) { \
  727.         if (prs_inetaddr(symbol, &value) < 0) \
  728.             return E_BAD_IPADDR; \
  729.         hp->MEMBER.s_addr = value; \
  730.         hp->flags.MEMBER = TRUE; \
  731.     } \
  732. } while (0)
  733.  
  734. /* Parse a list of INET addresses pointed to by MEMBER */
  735. #define PARSE_IAL(MEMBER) do \
  736. { \
  737.     if (optype == OP_BOOLEAN) \
  738.         return E_SYNTAX_ERROR; \
  739.     if (hp->flags.MEMBER) { \
  740.         hp->flags.MEMBER = FALSE; \
  741.         assert(hp->MEMBER); \
  742.         del_iplist(hp->MEMBER); \
  743.         hp->MEMBER = NULL; \
  744.     } \
  745.     if (optype == OP_ADDITION) { \
  746.         hp->MEMBER = get_addresses(symbol); \
  747.         if (hp->MEMBER == NULL) \
  748.             return E_SYNTAX_ERROR; \
  749.         hp->flags.MEMBER = TRUE; \
  750.     } \
  751. } while (0)
  752.  
  753. /* Parse a shared string pointed to by MEMBER */
  754. #define PARSE_STR(MEMBER) do \
  755. { \
  756.     if (optype == OP_BOOLEAN) \
  757.         return E_SYNTAX_ERROR; \
  758.     if (hp->flags.MEMBER) { \
  759.         hp->flags.MEMBER = FALSE; \
  760.         assert(hp->MEMBER); \
  761.         del_string(hp->MEMBER); \
  762.         hp->MEMBER = NULL; \
  763.     } \
  764.     if (optype == OP_ADDITION) { \
  765.         hp->MEMBER = get_shared_string(symbol); \
  766.         if (hp->MEMBER == NULL) \
  767.             return E_SYNTAX_ERROR; \
  768.         hp->flags.MEMBER = TRUE; \
  769.     } \
  770. } while (0)
  771.  
  772. /* Parse an unsigned integer value for MEMBER */
  773. #define PARSE_UINT(MEMBER) do \
  774. { \
  775.     if (optype == OP_BOOLEAN) \
  776.         return E_SYNTAX_ERROR; \
  777.     hp->flags.MEMBER = FALSE; \
  778.     if (optype == OP_ADDITION) { \
  779.         value = get_u_long(symbol); \
  780.         hp->MEMBER = value; \
  781.         hp->flags.MEMBER = TRUE; \
  782.     } \
  783. } while (0)
  784.  
  785. /*
  786.  * Evaluate the two-character tag symbol pointed to by "symbol" and place
  787.  * the data in the structure pointed to by "hp".  The pointer pointed to
  788.  * by "symbol" is updated to point past the source string (but may not
  789.  * point to the next tag entry).
  790.  *
  791.  * Obviously, this need a few more comments. . . .
  792.  */
  793. PRIVATE int
  794. eval_symbol(symbol, hp)
  795.     char **symbol;
  796.     struct host *hp;
  797. {
  798.     char tmpstr[MAXSTRINGLEN];
  799.     byte *tmphaddr;
  800.     struct symbolmap *symbolptr;
  801.     u_int32 value;
  802.     int32 timeoff;
  803.     int i, numsymbols;
  804.     unsigned len;
  805.     int optype;                    /* Indicates boolean, addition, or deletion */
  806.  
  807.     eat_whitespace(symbol);
  808.  
  809.     /* Make sure this is set before returning. */
  810.     current_tagname[0] = (*symbol)[0];
  811.     current_tagname[1] = (*symbol)[1];
  812.     current_tagname[2] = 0;
  813.  
  814.     if ((*symbol)[0] == '\0') {
  815.         return E_END_OF_ENTRY;
  816.     }
  817.     if ((*symbol)[0] == ':') {
  818.         return SUCCESS;
  819.     }
  820.     if ((*symbol)[0] == 'T') {    /* generic symbol */
  821.         (*symbol)++;
  822.         value = get_u_long(symbol);
  823.         sprintf(current_tagname, "T%d", (int)value);
  824.         eat_whitespace(symbol);
  825.         if ((*symbol)[0] != '=') {
  826.             return E_SYNTAX_ERROR;
  827.         }
  828.         (*symbol)++;
  829.         if (!(hp->generic)) {
  830.             hp->generic = (struct shared_bindata *)
  831.                 smalloc(sizeof(struct shared_bindata));
  832.         }
  833.         if (process_generic(symbol, &(hp->generic), (byte) (value & 0xFF)))
  834.             return E_SYNTAX_ERROR;
  835.         hp->flags.generic = TRUE;
  836.         return SUCCESS;
  837.     }
  838.     /*
  839.      * Determine the type of operation to be done on this symbol
  840.      */
  841.     switch ((*symbol)[2]) {
  842.     case '=':
  843.         optype = OP_ADDITION;
  844.         break;
  845.     case '@':
  846.         optype = OP_DELETION;
  847.         break;
  848.     case ':':
  849.     case '\0':
  850.         optype = OP_BOOLEAN;
  851.         break;
  852.     default:
  853.         return E_SYNTAX_ERROR;
  854.     }
  855.  
  856.     symbolptr = symbol_list;
  857.     numsymbols = sizeof(symbol_list) / sizeof(struct symbolmap);
  858.     for (i = 0; i < numsymbols; i++) {
  859.         if (((symbolptr->symbol)[0] == (*symbol)[0]) &&
  860.             ((symbolptr->symbol)[1] == (*symbol)[1])) {
  861.             break;
  862.         }
  863.         symbolptr++;
  864.     }
  865.     if (i >= numsymbols) {
  866.         return E_UNKNOWN_SYMBOL;
  867.     }
  868.     /*
  869.      * Skip past the = or @ character (to point to the data) if this
  870.      * isn't a boolean operation.  For boolean operations, just skip
  871.      * over the two-character tag symbol (and nothing else. . . .).
  872.      */
  873.     (*symbol) += (optype == OP_BOOLEAN) ? 2 : 3;
  874.  
  875.     eat_whitespace(symbol);
  876.  
  877.     /* The cases below are in order by symbolcode value. */
  878.     switch (symbolptr->symbolcode) {
  879.  
  880.     case SYM_BOOTFILE:
  881.         PARSE_STR(bootfile);
  882.         break;
  883.  
  884.     case SYM_COOKIE_SERVER:
  885.         PARSE_IAL(cookie_server);
  886.         break;
  887.  
  888.     case SYM_DOMAIN_SERVER:
  889.         PARSE_IAL(domain_server);
  890.         break;
  891.  
  892.     case SYM_GATEWAY:
  893.         PARSE_IAL(gateway);
  894.         break;
  895.  
  896.     case SYM_HWADDR:
  897.         if (optype == OP_BOOLEAN)
  898.             return E_SYNTAX_ERROR;
  899.         hp->flags.haddr = FALSE;
  900.         if (optype == OP_ADDITION) {
  901.             /* Default the HW type to Ethernet */
  902.             if (hp->flags.htype == 0) {
  903.                 hp->flags.htype = TRUE;
  904.                 hp->htype = HTYPE_ETHERNET;
  905.             }
  906.             tmphaddr = prs_haddr(symbol, hp->htype);
  907.             if (!tmphaddr)
  908.                 return E_BAD_HWADDR;
  909.             bcopy(tmphaddr, hp->haddr, haddrlength(hp->htype));
  910.             hp->flags.haddr = TRUE;
  911.         }
  912.         break;
  913.  
  914.     case SYM_HOMEDIR:
  915.         PARSE_STR(homedir);
  916.         break;
  917.  
  918.     case SYM_HTYPE:
  919.         if (optype == OP_BOOLEAN)
  920.             return E_SYNTAX_ERROR;
  921.         hp->flags.htype = FALSE;
  922.         if (optype == OP_ADDITION) {
  923.             value = 0L;            /* Assume an illegal value */
  924.             eat_whitespace(symbol);
  925.             if (isdigit(**symbol)) {
  926.                 value = get_u_long(symbol);
  927.             } else {
  928.                 len = sizeof(tmpstr);
  929.                 (void) get_string(symbol, tmpstr, &len);
  930.                 makelower(tmpstr);
  931.                 numsymbols = sizeof(htnamemap) /
  932.                     sizeof(struct htypename);
  933.                 for (i = 0; i < numsymbols; i++) {
  934.                     if (!strcmp(htnamemap[i].name, tmpstr)) {
  935.                         break;
  936.                     }
  937.                 }
  938.                 if (i < numsymbols) {
  939.                     value = htnamemap[i].htype;
  940.                 }
  941.             }
  942.             if (value >= hwinfocnt) {
  943.                 return E_BAD_HWATYPE;
  944.             }
  945.             hp->htype = (byte) (value & 0xFF);
  946.             hp->flags.htype = TRUE;
  947.         }
  948.         break;
  949.  
  950.     case SYM_IMPRESS_SERVER:
  951.         PARSE_IAL(impress_server);
  952.         break;
  953.  
  954.     case SYM_IPADDR:
  955.         PARSE_IA1(iaddr);
  956.         break;
  957.  
  958.     case SYM_LOG_SERVER:
  959.         PARSE_IAL(log_server);
  960.         break;
  961.  
  962.     case SYM_LPR_SERVER:
  963.         PARSE_IAL(lpr_server);
  964.         break;
  965.  
  966.     case SYM_NAME_SERVER:
  967.         PARSE_IAL(name_server);
  968.         break;
  969.  
  970.     case SYM_RLP_SERVER:
  971.         PARSE_IAL(rlp_server);
  972.         break;
  973.  
  974.     case SYM_SUBNET_MASK:
  975.         PARSE_IA1(subnet_mask);
  976.         break;
  977.  
  978.     case SYM_TIME_OFFSET:
  979.         if (optype == OP_BOOLEAN)
  980.             return E_SYNTAX_ERROR;
  981.         hp->flags.time_offset = FALSE;
  982.         if (optype == OP_ADDITION) {
  983.             len = sizeof(tmpstr);
  984.             (void) get_string(symbol, tmpstr, &len);
  985.             if (!strncmp(tmpstr, "auto", 4)) {
  986.                 hp->time_offset = secondswest;
  987.             } else {
  988.                 if (sscanf(tmpstr, "%d", (int*)&timeoff) != 1)
  989.                     return E_BAD_LONGWORD;
  990.                 hp->time_offset = timeoff;
  991.             }
  992.             hp->flags.time_offset = TRUE;
  993.         }
  994.         break;
  995.  
  996.     case SYM_TIME_SERVER:
  997.         PARSE_IAL(time_server);
  998.         break;
  999.  
  1000.     case SYM_VENDOR_MAGIC:
  1001.         if (optype == OP_BOOLEAN)
  1002.             return E_SYNTAX_ERROR;
  1003.         hp->flags.vm_cookie = FALSE;
  1004.         if (optype == OP_ADDITION) {
  1005.             if (strncmp(*symbol, "auto", 4)) {
  1006.                 /* The string is not "auto" */
  1007.                 if (!strncmp(*symbol, "rfc", 3)) {
  1008.                     bcopy(vm_rfc1048, hp->vm_cookie, 4);
  1009.                 } else if (!strncmp(*symbol, "cmu", 3)) {
  1010.                     bcopy(vm_cmu, hp->vm_cookie, 4);
  1011.                 } else {
  1012.                     if (!isdigit(**symbol))
  1013.                         return E_BAD_IPADDR;
  1014.                     if (prs_inetaddr(symbol, &value) < 0)
  1015.                         return E_BAD_IPADDR;
  1016.                     bcopy(&value, hp->vm_cookie, 4);
  1017.                 }
  1018.                 hp->flags.vm_cookie = TRUE;
  1019.             }
  1020.         }
  1021.         break;
  1022.  
  1023.     case SYM_SIMILAR_ENTRY:
  1024.         switch (optype) {
  1025.         case OP_ADDITION:
  1026.             fill_defaults(hp, symbol);
  1027.             break;
  1028.         default:
  1029.             return E_SYNTAX_ERROR;
  1030.         }
  1031.         break;
  1032.  
  1033.     case SYM_NAME_SWITCH:
  1034.         switch (optype) {
  1035.         case OP_ADDITION:
  1036.             return E_SYNTAX_ERROR;
  1037.         case OP_DELETION:
  1038.             hp->flags.send_name = FALSE;
  1039.             hp->flags.name_switch = FALSE;
  1040.             break;
  1041.         case OP_BOOLEAN:
  1042.             hp->flags.send_name = TRUE;
  1043.             hp->flags.name_switch = TRUE;
  1044.             break;
  1045.         }
  1046.         break;
  1047.  
  1048.     case SYM_BOOTSIZE:
  1049.         switch (optype) {
  1050.         case OP_ADDITION:
  1051.             if (!strncmp(*symbol, "auto", 4)) {
  1052.                 hp->flags.bootsize = TRUE;
  1053.                 hp->flags.bootsize_auto = TRUE;
  1054.             } else {
  1055.                 hp->bootsize = (unsigned int) get_u_long(symbol);
  1056.                 hp->flags.bootsize = TRUE;
  1057.                 hp->flags.bootsize_auto = FALSE;
  1058.             }
  1059.             break;
  1060.         case OP_DELETION:
  1061.             hp->flags.bootsize = FALSE;
  1062.             break;
  1063.         case OP_BOOLEAN:
  1064.             hp->flags.bootsize = TRUE;
  1065.             hp->flags.bootsize_auto = TRUE;
  1066.             break;
  1067.         }
  1068.         break;
  1069.  
  1070.     case SYM_BOOT_SERVER:
  1071.         PARSE_IA1(bootserver);
  1072.         break;
  1073.  
  1074.     case SYM_TFTPDIR:
  1075.         PARSE_STR(tftpdir);
  1076.         if ((hp->tftpdir != NULL) &&
  1077.             (hp->tftpdir->string[0] != '/'))
  1078.             return E_BAD_PATHNAME;
  1079.         break;
  1080.  
  1081.     case SYM_DUMP_FILE:
  1082.         PARSE_STR(dump_file);
  1083.         break;
  1084.  
  1085.     case SYM_DOMAIN_NAME:
  1086.         PARSE_STR(domain_name);
  1087.         break;
  1088.  
  1089.     case SYM_SWAP_SERVER:
  1090.         PARSE_IA1(swap_server);
  1091.         break;
  1092.  
  1093.     case SYM_ROOT_PATH:
  1094.         PARSE_STR(root_path);
  1095.         break;
  1096.  
  1097.     case SYM_EXTEN_FILE:
  1098.         PARSE_STR(exten_file);
  1099.         break;
  1100.  
  1101.     case SYM_REPLY_ADDR:
  1102.         PARSE_IA1(reply_addr);
  1103.         break;
  1104.  
  1105.     case SYM_NIS_DOMAIN:
  1106.         PARSE_STR(nis_domain);
  1107.         break;
  1108.  
  1109.     case SYM_NIS_SERVER:
  1110.         PARSE_IAL(nis_server);
  1111.         break;
  1112.  
  1113.     case SYM_NTP_SERVER:
  1114.         PARSE_IAL(ntp_server);
  1115.         break;
  1116.  
  1117. #ifdef    YORK_EX_OPTION
  1118.     case SYM_EXEC_FILE:
  1119.         PARSE_STR(exec_file);
  1120.         break;
  1121. #endif
  1122.  
  1123.     case SYM_MSG_SIZE:
  1124.         PARSE_UINT(msg_size);
  1125.         if (hp->msg_size < BP_MINPKTSZ ||
  1126.             hp->msg_size > MAX_MSG_SIZE)
  1127.             return E_BAD_VALUE;
  1128.         break;
  1129.  
  1130.     case SYM_MIN_WAIT:
  1131.         PARSE_UINT(min_wait);
  1132.         break;
  1133.  
  1134. #ifdef DHCP        
  1135.     /* PeP hic facet. */            
  1136.     case SYM_DHCP_LEASE:
  1137.         PARSE_UINT(dhcp_lease);
  1138.         break;
  1139.  
  1140.         /* XXX - Add new DHCP tags here */
  1141. #endif
  1142.  
  1143.     /* XXX - Add new tags here */
  1144.  
  1145.     default:
  1146.         return E_UNKNOWN_SYMBOL;
  1147.  
  1148.     }                            /* switch symbolcode */
  1149.  
  1150.     return SUCCESS;
  1151. }
  1152. #undef    PARSE_IA1
  1153. #undef    PARSE_IAL
  1154. #undef    PARSE_STR
  1155.  
  1156.  
  1157.  
  1158.  
  1159. /*
  1160.  * Read a string from the buffer indirectly pointed to through "src" and
  1161.  * move it into the buffer pointed to by "dest".  A pointer to the maximum
  1162.  * allowable length of the string (including null-terminator) is passed as
  1163.  * "length".  The actual length of the string which was read is returned in
  1164.  * the unsigned integer pointed to by "length".  This value is the same as
  1165.  * that which would be returned by applying the strlen() function on the
  1166.  * destination string (i.e the terminating null is not counted as a
  1167.  * character).  Trailing whitespace is removed from the string.  For
  1168.  * convenience, the function returns the new value of "dest".
  1169.  *
  1170.  * The string is read until the maximum number of characters, an unquoted
  1171.  * colon (:), or a null character is read.  The return string in "dest" is
  1172.  * null-terminated.
  1173.  */
  1174.  
  1175. PRIVATE char *
  1176. get_string(src, dest, length)
  1177.     char **src, *dest;
  1178.     unsigned *length;
  1179. {
  1180.     int n, len, quoteflag;
  1181.  
  1182.     quoteflag = FALSE;
  1183.     n = 0;
  1184.     len = *length - 1;
  1185.     while ((n < len) && (**src)) {
  1186.         if (!quoteflag && (**src == ':')) {
  1187.             break;
  1188.         }
  1189.         if (**src == '"') {
  1190.             (*src)++;
  1191.             quoteflag = !quoteflag;
  1192.             continue;
  1193.         }
  1194.         if (**src == '\\') {
  1195.             (*src)++;
  1196.             if (!**src) {
  1197.                 break;
  1198.             }
  1199.         }
  1200.         *dest++ = *(*src)++;
  1201.         n++;
  1202.     }
  1203.  
  1204.     /*
  1205.      * Remove that troublesome trailing whitespace. . .
  1206.      */
  1207.     while ((n > 0) && isspace(dest[-1])) {
  1208.         dest--;
  1209.         n--;
  1210.     }
  1211.  
  1212.     *dest = '\0';
  1213.     *length = n;
  1214.     return dest;
  1215. }
  1216.  
  1217.  
  1218.  
  1219. /*
  1220.  * Read the string indirectly pointed to by "src", update the caller's
  1221.  * pointer, and return a pointer to a malloc'ed shared_string structure
  1222.  * containing the string.
  1223.  *
  1224.  * The string is read using the same rules as get_string() above.
  1225.  */
  1226.  
  1227. PRIVATE struct shared_string *
  1228. get_shared_string(src)
  1229.     char **src;
  1230. {
  1231.     char retstring[MAXSTRINGLEN];
  1232.     struct shared_string *s;
  1233.     unsigned length;
  1234.  
  1235.     length = sizeof(retstring);
  1236.     (void) get_string(src, retstring, &length);
  1237.  
  1238.     s = (struct shared_string *) smalloc(sizeof(struct shared_string)
  1239.                                          + length);
  1240.     s->linkcount = 1;
  1241.     strcpy(s->string, retstring);
  1242.  
  1243.     return s;
  1244. }
  1245.  
  1246.  
  1247.  
  1248. /*
  1249.  * Load RFC1048 generic information directly into a memory buffer.
  1250.  *
  1251.  * "src" indirectly points to the ASCII representation of the generic data.
  1252.  * "dest" points to a string structure which is updated to point to a new
  1253.  * string with the new data appended to the old string.  The old string is
  1254.  * freed.
  1255.  *
  1256.  * The given tag value is inserted with the new data.
  1257.  *
  1258.  * The data may be represented as either a stream of hexadecimal numbers
  1259.  * representing bytes (any or all bytes may optionally start with '0x' and
  1260.  * be separated with periods ".") or as a quoted string of ASCII
  1261.  * characters (the quotes are required).
  1262.  */
  1263.  
  1264. PRIVATE int
  1265. process_generic(src, dest, tagvalue)
  1266.     char **src;
  1267.     struct shared_bindata **dest;
  1268.     u_int tagvalue;
  1269. {
  1270.     byte tmpbuf[MAXBUFLEN];
  1271.     byte *str;
  1272.     struct shared_bindata *bdata;
  1273.     u_int newlength, oldlength;
  1274.  
  1275.     str = tmpbuf;
  1276.     *str++ = (tagvalue & 0xFF);    /* Store tag value */
  1277.     str++;                        /* Skip over length field */
  1278.     if ((*src)[0] == '"') {        /* ASCII data */
  1279.         newlength = sizeof(tmpbuf) - 2;    /* Set maximum allowed length */
  1280.         (void) get_string(src, (char *) str, &newlength);
  1281.         newlength++;            /* null terminator */
  1282.     } else {                    /* Numeric data */
  1283.         newlength = 0;
  1284.         while (newlength < sizeof(tmpbuf) - 2) {
  1285.             if (interp_byte(src, str++) < 0)
  1286.                 break;
  1287.             newlength++;
  1288.             if (**src == '.') {
  1289.                 (*src)++;
  1290.             }
  1291.         }
  1292.     }
  1293.     if ((*src)[0] != ':')
  1294.         return -1;
  1295.  
  1296.     tmpbuf[1] = (newlength & 0xFF);
  1297.     oldlength = ((*dest)->length);
  1298.     bdata = (struct shared_bindata *) smalloc(sizeof(struct shared_bindata)
  1299.                                             + oldlength + newlength + 1);
  1300.     if (oldlength > 0) {
  1301.         bcopy((*dest)->data, bdata->data, oldlength);
  1302.     }
  1303.     bcopy(tmpbuf, bdata->data + oldlength, newlength + 2);
  1304.     bdata->length = oldlength + newlength + 2;
  1305.     bdata->linkcount = 1;
  1306.     if (*dest) {
  1307.         del_bindata(*dest);
  1308.     }
  1309.     *dest = bdata;
  1310.     return 0;
  1311. }
  1312.  
  1313.  
  1314.  
  1315. /*
  1316.  * Verify that the given string makes sense as a hostname (according to
  1317.  * Appendix 1, page 29 of RFC882).
  1318.  *
  1319.  * Return TRUE for good names, FALSE otherwise.
  1320.  */
  1321.  
  1322. PRIVATE boolean
  1323. goodname(hostname)
  1324.     register char *hostname;
  1325. {
  1326.     do {
  1327.         if (!isalpha(*hostname++)) {    /* First character must be a letter */
  1328.             return FALSE;
  1329.         }
  1330.         while (isalnum(*hostname) ||
  1331.                (*hostname == '-') ||
  1332.                (*hostname == '_') )
  1333.         {
  1334.             hostname++;            /* Alphanumeric or a hyphen */
  1335.         }
  1336.         if (!isalnum(hostname[-1])) {    /* Last must be alphanumeric */
  1337.             return FALSE;
  1338.         }
  1339.         if (*hostname == '\0') {/* Done? */
  1340.             return TRUE;
  1341.         }
  1342.     } while (*hostname++ == '.');    /* Dot, loop for next label */
  1343.  
  1344.     return FALSE;                /* If it's not a dot, lose */
  1345. }
  1346.  
  1347.  
  1348.  
  1349. /*
  1350.  * Null compare function -- always returns FALSE so an element is always
  1351.  * inserted into a hash table (i.e. there is never a collision with an
  1352.  * existing element).
  1353.  */
  1354.  
  1355. PRIVATE boolean
  1356. nullcmp(d1, d2)
  1357.     hash_datum *d1, *d2;
  1358. {
  1359.     return FALSE;
  1360. }
  1361.  
  1362.  
  1363. /*
  1364.  * Function for comparing a string with the hostname field of a host
  1365.  * structure.
  1366.  */
  1367.  
  1368. boolean
  1369. nmcmp(d1, d2)
  1370.     hash_datum *d1, *d2;
  1371. {
  1372.     char *name = (char *) d1;    /* XXX - OK? */
  1373.     struct host *hp = (struct host *) d2;
  1374.  
  1375.     return !strcmp(name, hp->hostname->string);
  1376. }
  1377.  
  1378.  
  1379. /*
  1380.  * Compare function to determine whether two hardware addresses are
  1381.  * equivalent.  Returns TRUE if "host1" and "host2" are equivalent, FALSE
  1382.  * otherwise.
  1383.  *
  1384.  * If the hardware addresses of "host1" and "host2" are identical, but
  1385.  * they are on different IP subnets, this function returns FALSE.
  1386.  *
  1387.  * This function is used when inserting elements into the hardware address
  1388.  * hash table.
  1389.  */
  1390.  
  1391. PRIVATE boolean
  1392. hwinscmp(d1, d2)
  1393.     hash_datum *d1, *d2;
  1394. {
  1395.     struct host *host1 = (struct host *) d1;
  1396.     struct host *host2 = (struct host *) d2;
  1397.  
  1398.     if (host1->htype != host2->htype) {
  1399.         return FALSE;
  1400.     }
  1401.     if (bcmp(host1->haddr, host2->haddr, haddrlength(host1->htype))) {
  1402.         return FALSE;
  1403.     }
  1404.     /* XXX - Is the subnet_mask field set yet? */
  1405.     if ((host1->subnet_mask.s_addr) == (host2->subnet_mask.s_addr)) {
  1406.         if (((host1->iaddr.s_addr) & (host1->subnet_mask.s_addr)) !=
  1407.             ((host2->iaddr.s_addr) & (host2->subnet_mask.s_addr)))
  1408.         {
  1409.             return FALSE;
  1410.         }
  1411.     }
  1412.     return TRUE;
  1413. }
  1414.  
  1415.  
  1416. /*
  1417.  * Macros for use in the function below:
  1418.  */
  1419.  
  1420. #define DUP_COPY(MEMBER) do \
  1421. { \
  1422.     if (!hp->flags.MEMBER) { \
  1423.         if ((hp->flags.MEMBER = hp2->flags.MEMBER) != 0) { \
  1424.             hp->MEMBER = hp2->MEMBER; \
  1425.         } \
  1426.     } \
  1427. } while (0)
  1428.  
  1429. #define DUP_LINK(MEMBER) do \
  1430. { \
  1431.     if (!hp->flags.MEMBER) { \
  1432.         if ((hp->flags.MEMBER = hp2->flags.MEMBER) != 0) { \
  1433.             assert(hp2->MEMBER); \
  1434.             hp->MEMBER = hp2->MEMBER; \
  1435.             (hp->MEMBER->linkcount)++; \
  1436.         } \
  1437.     } \
  1438. } while (0)
  1439.  
  1440. /*
  1441.  * Process the "similar entry" symbol.
  1442.  *
  1443.  * The host specified as the value of the "tc" symbol is used as a template
  1444.  * for the current host entry.  Symbol values not explicitly set in the
  1445.  * current host entry are inferred from the template entry.
  1446.  */
  1447. PRIVATE void
  1448. fill_defaults(hp, src)
  1449.     struct host *hp;
  1450.     char **src;
  1451. {
  1452.     unsigned int tlen, hashcode;
  1453.     struct host *hp2;
  1454.     char tstring[MAXSTRINGLEN];
  1455.  
  1456.     tlen = sizeof(tstring);
  1457.     (void) get_string(src, tstring, &tlen);
  1458.     hashcode = hash_HashFunction((u_char *) tstring, tlen);
  1459.     hp2 = (struct host *) hash_Lookup(nmhashtable, hashcode, nmcmp, tstring);
  1460.  
  1461.     if (hp2 == NULL) {
  1462.         report(LOG_ERR, "can't find tc=\"%s\"", tstring);
  1463.         return;
  1464.     }
  1465.     DUP_LINK(bootfile);
  1466.     DUP_LINK(cookie_server);
  1467.     DUP_LINK(domain_server);
  1468.     DUP_LINK(gateway);
  1469.     /* haddr not copied */
  1470.     DUP_LINK(homedir);
  1471.     DUP_COPY(htype);
  1472.  
  1473.     DUP_LINK(impress_server);
  1474.     /* iaddr not copied */
  1475.     DUP_LINK(log_server);
  1476.     DUP_LINK(lpr_server);
  1477.     DUP_LINK(name_server);
  1478.     DUP_LINK(rlp_server);
  1479.  
  1480.     DUP_COPY(subnet_mask);
  1481.     DUP_COPY(time_offset);
  1482.     DUP_LINK(time_server);
  1483.  
  1484.     if (!hp->flags.vm_cookie) {
  1485.         if ((hp->flags.vm_cookie = hp2->flags.vm_cookie)) {
  1486.             bcopy(hp2->vm_cookie, hp->vm_cookie, 4);
  1487.         }
  1488.     }
  1489.     if (!hp->flags.name_switch) {
  1490.         if ((hp->flags.name_switch = hp2->flags.name_switch)) {
  1491.             hp->flags.send_name = hp2->flags.send_name;
  1492.         }
  1493.     }
  1494.     if (!hp->flags.bootsize) {
  1495.         if ((hp->flags.bootsize = hp2->flags.bootsize)) {
  1496.             hp->flags.bootsize_auto = hp2->flags.bootsize_auto;
  1497.             hp->bootsize = hp2->bootsize;
  1498.         }
  1499.     }
  1500.     DUP_COPY(bootserver);
  1501.  
  1502.     DUP_LINK(tftpdir);
  1503.     DUP_LINK(dump_file);
  1504.     DUP_LINK(domain_name);
  1505.  
  1506.     DUP_COPY(swap_server);
  1507.     DUP_LINK(root_path);
  1508.     DUP_LINK(exten_file);
  1509.  
  1510.     DUP_COPY(reply_addr);
  1511.  
  1512.     DUP_LINK(nis_domain);
  1513.     DUP_LINK(nis_server);
  1514.     DUP_LINK(ntp_server);
  1515.  
  1516. #ifdef    YORK_EX_OPTION
  1517.     DUP_LINK(exec_file);
  1518. #endif
  1519.  
  1520.     DUP_COPY(msg_size);
  1521.     DUP_COPY(min_wait);
  1522.  
  1523.     /* XXX - Add new tags here */
  1524.     DUP_COPY(dhcp_lease); /* PeP hic facet */
  1525.  
  1526.     DUP_LINK(generic);
  1527.  
  1528. }
  1529. #undef    DUP_COPY
  1530. #undef    DUP_LINK
  1531.  
  1532.  
  1533.  
  1534. /*
  1535.  * This function adjusts the caller's pointer to point just past the
  1536.  * first-encountered colon.  If it runs into a null character, it leaves
  1537.  * the pointer pointing to it.
  1538.  */
  1539.  
  1540. PRIVATE void
  1541. adjust(s)
  1542.     char **s;
  1543. {
  1544.     register char *t;
  1545.  
  1546.     t = *s;
  1547.     while (*t && (*t != ':')) {
  1548.         t++;
  1549.     }
  1550.     if (*t) {
  1551.         t++;
  1552.     }
  1553.     *s = t;
  1554. }
  1555.  
  1556.  
  1557.  
  1558.  
  1559. /*
  1560.  * This function adjusts the caller's pointer to point to the first
  1561.  * non-whitespace character.  If it runs into a null character, it leaves
  1562.  * the pointer pointing to it.
  1563.  */
  1564.  
  1565. PRIVATE void
  1566. eat_whitespace(s)
  1567.     char **s;
  1568. {
  1569.     register char *t;
  1570.  
  1571.     t = *s;
  1572.     while (*t && isspace(*t)) {
  1573.         t++;
  1574.     }
  1575.     *s = t;
  1576. }
  1577.  
  1578.  
  1579.  
  1580. /*
  1581.  * This function converts the given string to all lowercase.
  1582.  */
  1583.  
  1584. PRIVATE void
  1585. makelower(s)
  1586.     char *s;
  1587. {
  1588.     while (*s) {
  1589.         if (isupper(*s)) {
  1590.             *s = tolower(*s);
  1591.         }
  1592.         s++;
  1593.     }
  1594. }
  1595.  
  1596.  
  1597.  
  1598. /*
  1599.  *
  1600.  *    N O T E :
  1601.  *
  1602.  *    In many of the functions which follow, a parameter such as "src" or
  1603.  *    "symbol" is passed as a pointer to a pointer to something.  This is
  1604.  *    done for the purpose of letting the called function update the
  1605.  *    caller's copy of the parameter (i.e. to effect call-by-reference
  1606.  *    parameter passing).  The value of the actual parameter is only used
  1607.  *    to locate the real parameter of interest and then update this indirect
  1608.  *    parameter.
  1609.  *
  1610.  *    I'm sure somebody out there won't like this. . . .
  1611.  *  (Yea, because it usually makes code slower... -gwr)
  1612.  *
  1613.  */
  1614.  
  1615.  
  1616.  
  1617. /*
  1618.  * "src" points to a character pointer which points to an ASCII string of
  1619.  * whitespace-separated IP addresses.  A pointer to an in_addr_list
  1620.  * structure containing the list of addresses is returned.  NULL is
  1621.  * returned if no addresses were found at all.  The pointer pointed to by
  1622.  * "src" is updated to point to the first non-address (illegal) character.
  1623.  */
  1624.  
  1625. PRIVATE struct in_addr_list *
  1626. get_addresses(src)
  1627.     char **src;
  1628. {
  1629.     struct in_addr tmpaddrlist[MAXINADDRS];
  1630.     struct in_addr *address1, *address2;
  1631.     struct in_addr_list *result;
  1632.     unsigned addrcount, totalsize;
  1633.  
  1634.     address1 = tmpaddrlist;
  1635.     for (addrcount = 0; addrcount < MAXINADDRS; addrcount++) {
  1636.         while (isspace(**src) || (**src == ',')) {
  1637.             (*src)++;
  1638.         }
  1639.         if (!**src) {            /* Quit if nothing more */
  1640.             break;
  1641.         }
  1642.         if (prs_inetaddr(src, &(address1->s_addr)) < 0) {
  1643.             break;
  1644.         }
  1645.         address1++;                /* Point to next address slot */
  1646.     }
  1647.     if (addrcount < 1) {
  1648.         result = NULL;
  1649.     } else {
  1650.         totalsize = sizeof(struct in_addr_list)
  1651.         +            (addrcount - 1) * sizeof(struct in_addr);
  1652.         result = (struct in_addr_list *) smalloc(totalsize);
  1653.         result->linkcount = 1;
  1654.         result->addrcount = addrcount;
  1655.         address1 = tmpaddrlist;
  1656.         address2 = result->addr;
  1657.         for (; addrcount > 0; addrcount--) {
  1658.             address2->s_addr = address1->s_addr;
  1659.             address1++;
  1660.             address2++;
  1661.         }
  1662.     }
  1663.     return result;
  1664. }
  1665.  
  1666.  
  1667.  
  1668. /*
  1669.  * prs_inetaddr(src, result)
  1670.  *
  1671.  * "src" is a value-result parameter; the pointer it points to is updated
  1672.  * to point to the next data position.   "result" points to an unsigned long
  1673.  * in which an address is returned.
  1674.  *
  1675.  * This function parses the IP address string in ASCII "dot notation" pointed
  1676.  * to by (*src) and places the result (in network byte order) in the unsigned
  1677.  * long pointed to by "result".  For malformed addresses, -1 is returned,
  1678.  * (*src) points to the first illegal character, and the unsigned long pointed
  1679.  * to by "result" is unchanged.  Successful calls return 0.
  1680.  */
  1681.  
  1682. PRIVATE int
  1683. prs_inetaddr(src, result)
  1684.     char **src;
  1685.     u_int32 *result;
  1686. {
  1687.     char tmpstr[MAXSTRINGLEN];
  1688.     register u_int32 value;
  1689.     u_int32 parts[4], *pp;
  1690.     int n;
  1691.     char *s, *t;
  1692.  
  1693.     /* Leading alpha char causes IP addr lookup. */
  1694.     if (isalpha(**src)) {
  1695.         /* Lookup IP address. */
  1696.         s = *src;
  1697.         t = tmpstr;
  1698.         while ((isalnum(*s) || (*s == '.') ||
  1699.                 (*s == '-') || (*s == '_') ) &&
  1700.                (t < &tmpstr[MAXSTRINGLEN - 1]) )
  1701.             *t++ = *s++;
  1702.         *t = '\0';
  1703.         *src = s;
  1704.  
  1705.         n = lookup_ipa(tmpstr, result);
  1706.         if (n < 0)
  1707.             report(LOG_ERR, "can not get IP addr for %s", tmpstr);
  1708.         return n;
  1709.     }
  1710.  
  1711.     /*
  1712.      * Parse an address in Internet format:
  1713.      *    a.b.c.d
  1714.      *    a.b.c    (with c treated as 16-bits)
  1715.      *    a.b    (with b treated as 24 bits)
  1716.      */
  1717.     pp = parts;
  1718.   loop:
  1719.     /* If it's not a digit, return error. */
  1720.     if (!isdigit(**src))
  1721.         return -1;
  1722.     *pp++ = get_u_long(src);
  1723.     if (**src == '.') {
  1724.         if (pp < (parts + 4)) {
  1725.             (*src)++;
  1726.             goto loop;
  1727.         }
  1728.         return (-1);
  1729.     }
  1730. #if 0
  1731.     /* This is handled by the caller. */
  1732.     if (**src && !(isspace(**src) || (**src == ':'))) {
  1733.         return (-1);
  1734.     }
  1735. #endif
  1736.  
  1737.     /*
  1738.      * Construct the address according to
  1739.      * the number of parts specified.
  1740.      */
  1741.     n = pp - parts;
  1742.     switch (n) {
  1743.     case 1:                    /* a -- 32 bits */
  1744.         value = parts[0];
  1745.         break;
  1746.     case 2:                    /* a.b -- 8.24 bits */
  1747.         value = (parts[0] << 24) | (parts[1] & 0xFFFFFF);
  1748.         break;
  1749.     case 3:                    /* a.b.c -- 8.8.16 bits */
  1750.         value = (parts[0] << 24) | ((parts[1] & 0xFF) << 16) |
  1751.             (parts[2] & 0xFFFF);
  1752.         break;
  1753.     case 4:                    /* a.b.c.d -- 8.8.8.8 bits */
  1754.         value = (parts[0] << 24) | ((parts[1] & 0xFF) << 16) |
  1755.             ((parts[2] & 0xFF) << 8) | (parts[3] & 0xFF);
  1756.         break;
  1757.     default:
  1758.         return (-1);
  1759.     }
  1760.     *result = htonl(value);
  1761.     return (0);
  1762. }
  1763.  
  1764.  
  1765.  
  1766. /*
  1767.  * "src" points to a pointer which in turn points to a hexadecimal ASCII
  1768.  * string.  This string is interpreted as a hardware address and returned
  1769.  * as a pointer to the actual hardware address, represented as an array of
  1770.  * bytes.
  1771.  *
  1772.  * The ASCII string must have the proper number of digits for the specified
  1773.  * hardware type (e.g. twelve digits for a 48-bit Ethernet address).
  1774.  * Two-digit sequences (bytes) may be separated with periods (.)  and/or
  1775.  * prefixed with '0x' for readability, but this is not required.
  1776.  *
  1777.  * For bad addresses, the pointer which "src" points to is updated to point
  1778.  * to the start of the first two-digit sequence which was bad, and the
  1779.  * function returns a NULL pointer.
  1780.  */
  1781.  
  1782. PRIVATE byte *
  1783. prs_haddr(src, htype)
  1784.     char **src;
  1785.     u_int htype;
  1786. {
  1787.     static byte haddr[MAXHADDRLEN];
  1788.     byte *hap;
  1789.     char tmpstr[MAXSTRINGLEN];
  1790.     u_int tmplen;
  1791.     unsigned hal;
  1792.     char *p;
  1793.  
  1794.     hal = haddrlength(htype);    /* Get length of this address type */
  1795.     if (hal <= 0) {
  1796.         report(LOG_ERR, "Invalid addr type for HW addr parse");
  1797.         return NULL;
  1798.     }
  1799.     tmplen = sizeof(tmpstr);
  1800.     get_string(src, tmpstr, &tmplen);
  1801.     p = tmpstr;
  1802.  
  1803.     /* If it's a valid host name, try to lookup the HW address. */
  1804.     if (goodname(p)) {
  1805.         /* Lookup Hardware Address for hostname. */
  1806.         if ((hap = lookup_hwa(p, htype)) != NULL)
  1807.             return hap; /* success */
  1808.         report(LOG_ERR, "Add 0x prefix if hex value starts with A-F");
  1809.         /* OK, assume it must be numeric. */
  1810.     }
  1811.  
  1812.     hap = haddr;
  1813.     while (hap < haddr + hal) {
  1814.         if ((*p == '.') || (*p == ':'))
  1815.             p++;
  1816.         if (interp_byte(&p, hap++) < 0) {
  1817.             return NULL;
  1818.         }
  1819.     }
  1820.     return haddr;
  1821. }
  1822.  
  1823.  
  1824.  
  1825. /*
  1826.  * "src" is a pointer to a character pointer which in turn points to a
  1827.  * hexadecimal ASCII representation of a byte.  This byte is read, the
  1828.  * character pointer is updated, and the result is deposited into the
  1829.  * byte pointed to by "retbyte".
  1830.  *
  1831.  * The usual '0x' notation is allowed but not required.  The number must be
  1832.  * a two digit hexadecimal number.  If the number is invalid, "src" and
  1833.  * "retbyte" are left untouched and -1 is returned as the function value.
  1834.  * Successful calls return 0.
  1835.  */
  1836.  
  1837. PRIVATE int
  1838. interp_byte(src, retbyte)
  1839.     char **src;
  1840.     byte *retbyte;
  1841. {
  1842.     int v;
  1843.  
  1844.     if ((*src)[0] == '0' &&
  1845.         ((*src)[1] == 'x' ||
  1846.          (*src)[1] == 'X')) {
  1847.         (*src) += 2;            /* allow 0x for hex, but don't require it */
  1848.     }
  1849.     if (!isxdigit((*src)[0]) || !isxdigit((*src)[1])) {
  1850.         return -1;
  1851.     }
  1852.     if (sscanf(*src, "%2x", &v) != 1) {
  1853.         return -1;
  1854.     }
  1855.     (*src) += 2;
  1856.     *retbyte = (byte) (v & 0xFF);
  1857.     return 0;
  1858. }
  1859.  
  1860.  
  1861.  
  1862. /*
  1863.  * The parameter "src" points to a character pointer which points to an
  1864.  * ASCII string representation of an unsigned number.  The number is
  1865.  * returned as an unsigned long and the character pointer is updated to
  1866.  * point to the first illegal character.
  1867.  */
  1868.  
  1869. PRIVATE u_int32
  1870. get_u_long(src)
  1871.     char **src;
  1872. {
  1873.     register u_int32 value, base;
  1874.     char c;
  1875.  
  1876.     /*
  1877.      * Collect number up to first illegal character.  Values are specified
  1878.      * as for C:  0x=hex, 0=octal, other=decimal.
  1879.      */
  1880.     value = 0;
  1881.     base = 10;
  1882.     if (**src == '0') {
  1883.         base = 8;
  1884.         (*src)++;
  1885.     }
  1886.     if (**src == 'x' || **src == 'X') {
  1887.         base = 16;
  1888.         (*src)++;
  1889.     }
  1890.     while ((c = **src)) {
  1891.         if (isdigit(c)) {
  1892.             value = (value * base) + (c - '0');
  1893.             (*src)++;
  1894.             continue;
  1895.         }
  1896.         if (base == 16 && isxdigit(c)) {
  1897.             value = (value << 4) + ((c & ~32) + 10 - 'A');
  1898.             (*src)++;
  1899.             continue;
  1900.         }
  1901.         break;
  1902.     }
  1903.     return value;
  1904. }
  1905.  
  1906.  
  1907.  
  1908. /*
  1909.  * Routines for deletion of data associated with the main data structure.
  1910.  */
  1911.  
  1912.  
  1913. /*
  1914.  * Frees the entire host data structure given.  Does nothing if the passed
  1915.  * pointer is NULL.
  1916.  */
  1917.  
  1918. PRIVATE void
  1919. free_host(hmp)
  1920.     hash_datum *hmp;
  1921. {
  1922.     struct host *hostptr = (struct host *) hmp;
  1923.     if (hostptr == NULL)
  1924.         return;
  1925.     assert(hostptr->linkcount > 0);
  1926.     if (--(hostptr->linkcount))
  1927.         return;                    /* Still has references */
  1928.     del_iplist(hostptr->cookie_server);
  1929.     del_iplist(hostptr->domain_server);
  1930.     del_iplist(hostptr->gateway);
  1931.     del_iplist(hostptr->impress_server);
  1932.     del_iplist(hostptr->log_server);
  1933.     del_iplist(hostptr->lpr_server);
  1934.     del_iplist(hostptr->name_server);
  1935.     del_iplist(hostptr->rlp_server);
  1936.     del_iplist(hostptr->time_server);
  1937.     del_iplist(hostptr->nis_server);
  1938.     del_iplist(hostptr->ntp_server);
  1939.  
  1940.     /*
  1941.      * XXX - Add new tags here
  1942.      * (if the value is an IP list)
  1943.      */
  1944.  
  1945.     del_string(hostptr->hostname);
  1946.     del_string(hostptr->homedir);
  1947.     del_string(hostptr->bootfile);
  1948.     del_string(hostptr->tftpdir);
  1949.     del_string(hostptr->root_path);
  1950.     del_string(hostptr->domain_name);
  1951.     del_string(hostptr->dump_file);
  1952.     del_string(hostptr->exten_file);
  1953.     del_string(hostptr->nis_domain);
  1954.  
  1955. #ifdef    YORK_EX_OPTION
  1956.     del_string(hostptr->exec_file);
  1957. #endif
  1958.  
  1959.     /*
  1960.      * XXX - Add new tags here
  1961.      * (if it is a shared string)
  1962.      */
  1963.  
  1964.     del_bindata(hostptr->generic);
  1965.     free((char *) hostptr);
  1966. }
  1967.  
  1968.  
  1969.  
  1970. /*
  1971.  * Decrements the linkcount on the given IP address data structure.  If the
  1972.  * linkcount goes to zero, the memory associated with the data is freed.
  1973.  */
  1974.  
  1975. PRIVATE void
  1976. del_iplist(iplist)
  1977.     struct in_addr_list *iplist;
  1978. {
  1979.     if (iplist) {
  1980.         if (!(--(iplist->linkcount))) {
  1981.             free((char *) iplist);
  1982.         }
  1983.     }
  1984. }
  1985.  
  1986.  
  1987.  
  1988. /*
  1989.  * Decrements the linkcount on a string data structure.  If the count
  1990.  * goes to zero, the memory associated with the string is freed.  Does
  1991.  * nothing if the passed pointer is NULL.
  1992.  */
  1993.  
  1994. PRIVATE void
  1995. del_string(stringptr)
  1996.     struct shared_string *stringptr;
  1997. {
  1998.     if (stringptr) {
  1999.         if (!(--(stringptr->linkcount))) {
  2000.             free((char *) stringptr);
  2001.         }
  2002.     }
  2003. }
  2004.  
  2005.  
  2006.  
  2007. /*
  2008.  * Decrements the linkcount on a shared_bindata data structure.  If the
  2009.  * count goes to zero, the memory associated with the data is freed.  Does
  2010.  * nothing if the passed pointer is NULL.
  2011.  */
  2012.  
  2013. PRIVATE void
  2014. del_bindata(dataptr)
  2015.     struct shared_bindata *dataptr;
  2016. {
  2017.     if (dataptr) {
  2018.         if (!(--(dataptr->linkcount))) {
  2019.             free((char *) dataptr);
  2020.         }
  2021.     }
  2022. }
  2023.  
  2024.  
  2025.  
  2026.  
  2027. /* smalloc()  --  safe malloc()
  2028.  *
  2029.  * Always returns a valid pointer (if it returns at all).  The allocated
  2030.  * memory is initialized to all zeros.  If malloc() returns an error, a
  2031.  * message is printed using the report() function and the program aborts
  2032.  * with a status of 1.
  2033.  */
  2034.  
  2035. PRIVATE char *
  2036. smalloc(nbytes)
  2037.     unsigned nbytes;
  2038. {
  2039.     char *retvalue;
  2040.  
  2041.     retvalue = malloc(nbytes);
  2042.     if (!retvalue) {
  2043.         report(LOG_ERR, "malloc() failure -- exiting");
  2044.         exit(1);
  2045.     }
  2046.     bzero(retvalue, nbytes);
  2047.     return retvalue;
  2048. }
  2049.  
  2050.  
  2051. /*
  2052.  * Compare function to determine whether two hardware addresses are
  2053.  * equivalent.  Returns TRUE if "host1" and "host2" are equivalent, FALSE
  2054.  * otherwise.
  2055.  *
  2056.  * This function is used when retrieving elements from the hardware address
  2057.  * hash table.
  2058.  */
  2059.  
  2060. boolean
  2061. hwlookcmp(d1, d2)
  2062.     hash_datum *d1, *d2;
  2063. {
  2064.     struct host *host1 = (struct host *) d1;
  2065.     struct host *host2 = (struct host *) d2;
  2066.  
  2067.     if (host1->htype != host2->htype) {
  2068.         return FALSE;
  2069.     }
  2070.     if (bcmp(host1->haddr, host2->haddr, haddrlength(host1->htype))) {
  2071.         return FALSE;
  2072.     }
  2073.     return TRUE;
  2074. }
  2075.  
  2076.  
  2077. /*
  2078.  * Compare function for doing IP address hash table lookup.
  2079.  */
  2080.  
  2081. boolean
  2082. iplookcmp(d1, d2)
  2083.     hash_datum *d1, *d2;
  2084. {
  2085.     struct host *host1 = (struct host *) d1;
  2086.     struct host *host2 = (struct host *) d2;
  2087.  
  2088.     return (host1->iaddr.s_addr == host2->iaddr.s_addr);
  2089. }
  2090.  
  2091. /*
  2092.  * Local Variables:
  2093.  * tab-width: 4
  2094.  * c-indent-level: 4
  2095.  * c-argdecl-indent: 4
  2096.  * c-continued-statement-offset: 4
  2097.  * c-continued-brace-offset: -4
  2098.  * c-label-offset: -4
  2099.  * c-brace-offset: 0
  2100.  * End:
  2101.  */
  2102.