home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / unix / unixlib_1 / !UnixLib37_netlib_netlib_c_net < prev    next >
Encoding:
Text File  |  1996-07-28  |  4.2 KB  |  238 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source$
  4.  * $Date$
  5.  * $Revision$
  6.  * $State$
  7.  * $Author$
  8.  *
  9.  * $Log$
  10.  ***************************************************************************/
  11.  
  12. static const char rcs_id[] = "$Id$";
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. #include <arpa/inet.h>
  19. #include <netdb.h>
  20. #include <netinet/in.h>
  21. #include <sys/socket.h>
  22.  
  23. #include <socketlib.h>
  24.  
  25. /*
  26.  * File handle for the nets file
  27.  */
  28. static FILE *netfile = NULL;
  29.  
  30. /*
  31.  * Keep the file open between calls to these routines?
  32.  */
  33. static int keepopen = 0;
  34.  
  35. /*
  36.  * Local routines
  37.  */
  38. static int __setnetent (int allowrewind);
  39. static struct netent *__getnetent (void);
  40.  
  41. /*
  42.  * Open and rewind the nets file
  43.  */
  44. int
  45. setnetent (int stayopen)
  46. {
  47.   /* Record whether the file should be kept open */
  48.   keepopen = stayopen;
  49.  
  50.   return __setnetent (1);
  51. }
  52.  
  53. /*
  54.  * Do the real work of opening/rewinding the nets file
  55.  */
  56. static int
  57. __setnetent (int allowrewind)
  58. {
  59.   /* Open or rewind the file as necessary */
  60.   if (netfile)
  61.     {
  62.       if (allowrewind)
  63.     rewind (netfile);
  64.     }
  65.   else
  66.     {
  67.       netfile = fopen ("InetDBase:Networks", "r");
  68.     }
  69.  
  70.   return (netfile == NULL) ? -1 : 0;
  71. }
  72.  
  73. /*
  74.  * Fetch the next entry from the nets file
  75.  */
  76. struct netent *
  77. getnetent ()
  78. {
  79.   struct netent *net;
  80.  
  81.   /* Open the file if necessary */
  82.   if (netfile == NULL)
  83.     if (__setnetent (0) == -1)
  84.       return NULL;
  85.  
  86.   /* Do the actual read */
  87.   net = __getnetent ();
  88.  
  89.   /* Close the file unless the user has prohibited it */
  90.   if (!keepopen)
  91.     endnetent ();
  92.  
  93.   return net;
  94. }
  95.  
  96. /*
  97.  * Do the real work of getting an entry from the file
  98.  */
  99. struct netent *
  100. __getnetent ()
  101. {
  102.   static struct netent net =
  103.   {
  104.     NULL, NULL, AF_INET, 0
  105.   };
  106.  
  107.   char **item;
  108.   char *line;
  109.   char *element;
  110.   int aliases;
  111.  
  112.   /* Free up any memory in use */
  113.   if (net.n_name)
  114.     {
  115.       for (item = net.n_aliases; *item; item++)
  116.     free (*item);
  117.       free (net.n_name);
  118.       free (net.n_aliases);
  119.  
  120.       net.n_name = NULL;
  121.     }
  122.  
  123.   /* Read a line from the file */
  124.   if ((line = __socketlib_readline (netfile)) == NULL)
  125.     return NULL;
  126.  
  127.   /* Extract the offical network name from the line */
  128.   element = strtok (line, " \t");
  129.   net.n_name = strdup (element);
  130.  
  131.   /* Extract the address from the line */
  132.   element = strtok (NULL, " \t");
  133.   net.n_net = inet_network (element);
  134.  
  135.   /* Initialialise the alias list */
  136.   net.n_aliases = malloc (sizeof (char *));
  137.   net.n_aliases[0] = NULL;
  138.   aliases = 1;
  139.  
  140.   /* Extract the aliases */
  141.   while ((element = strtok (NULL, " \t")) != NULL)
  142.     {
  143.       aliases += 1;
  144.       net.n_aliases = realloc (net.n_aliases, aliases * sizeof (char *));
  145.       net.n_aliases[aliases - 2] = strdup (element);
  146.       net.n_aliases[aliases - 1] = NULL;
  147.     }
  148.  
  149.   return &net;
  150. }
  151.  
  152. /*
  153.  * Close the nets file
  154.  */
  155. int
  156. endnetent ()
  157. {
  158.   int status = 0;
  159.  
  160.   /* If its open, close it */
  161.   if (netfile)
  162.     {
  163.       status = fclose (netfile);
  164.       netfile = 0;
  165.     }
  166.  
  167.   return status;
  168. }
  169.  
  170. /*
  171.  * Search the nets file for a given net name
  172.  */
  173. struct netent *
  174. getnetbyname (const char *name)
  175. {
  176.   struct netent *net;
  177.   char **alias;
  178.  
  179.   /* Open/rewind the file */
  180.   if (__setnetent (1) == -1)
  181.     return NULL;
  182.  
  183.   /* Look through the file for a match */
  184.   while ((net = __getnetent ()) != NULL)
  185.     {
  186.  
  187.       /* Does the offical name match? */
  188.       if (strcmp (net->n_name, name) == 0)
  189.     break;
  190.  
  191.       /* Do any of the aliases match? */
  192.       for (alias = net->n_aliases; *alias; alias++)
  193.     {
  194.       if (strcmp (*alias, name) == 0)
  195.         break;
  196.     }
  197.  
  198.       /* Did any of the aliases match? */
  199.       if (*alias)
  200.     break;
  201.     }
  202.  
  203.   /* Close the file unless the user has prohibited it */
  204.   if (!keepopen)
  205.     endnetent ();
  206.  
  207.   return net;
  208. }
  209.  
  210. /*
  211.  * Search the nets file for a given address
  212.  */
  213. struct netent *
  214. getnetbyaddr (long netaddr, int type)
  215. {
  216.   struct netent *net;
  217.  
  218.   /* Open/rewind the file */
  219.   if (__setnetent (1) == -1)
  220.     return NULL;
  221.  
  222.   /* Look through the file for a match */
  223.   while ((net = __getnetent ()) != NULL)
  224.     {
  225.  
  226.       /* If the type and length match, we've found it */
  227.       if ((net->n_addrtype == type) && (net->n_net == netaddr))
  228.     break;
  229.  
  230.     }
  231.  
  232.   /* Close the file unless the user has prohibited it */
  233.   if (!keepopen)
  234.     endnetent ();
  235.  
  236.   return net;
  237. }
  238.