/*
 *------------------------------------------------------------------
 *
 * $Source: /u1/jis/gw/cgw/statd/src/RCS/read_config_file.c,v $
 * $Revision: 1.1 $
 * $Date: 87/06/04 17:26:38 $
 * $State: Exp $
 * $Author: jon $
 * $Locker: jon $
 *
 * $Log:	read_config_file.c,v $
 * Revision 1.1  87/06/04  17:26:38  jon
 * Initial revision
 * 
 * 
 * 
 *------------------------------------------------------------------
 */

#ifndef lint
static char *rcsid_statd_c = "$Header: read_config_file.c,v 1.1 87/06/04 17:26:38 jon Locked $";
#endif	lint

#include <stdio.h>
#include <sys/file.h>
#include <netdb.h>
#include <syslog.h>
#include "statd.h"

extern long poll_interval;

read_config_file (filename, gateways)
     char *filename;
     gateway_info *gateways;
{
  FILE *config;
  char line[1024], *primary_name;
  int i = -1;
  u_long host_addr, gethst();
  gateway_info *gw;
  char *errno_description();

  config = fopen (filename, "r");
  if (config == NULL) {
    syslog (LOG_ERR, "can't open config file (%s)", filename);
    abort ();
  }

  /* read poll interval (in minutes) */
  if (fgets (line, 512, config) == NULL) {
    syslog (LOG_ERR, "Can't read config file.");
    abort ();
  }
  else {
    poll_interval = atoi (line);
  }

  /* read gateway list */
  while (fgets (line, 512, config) != NULL) {
    line[strlen(line)-1] = NULL; /* nasty newline */

    if (++i > MAX_GATEWAYS - 1) {
      syslog (LOG_ERR, "Too many (%d) gateways.", i);
      abort (); /* should this abort? */
    }
    gw = &gateways[i];

    host_addr = gethst (line, &primary_name);
    if (host_addr == 0) {
      i--;  /* error message already logged */
      continue;
    }
    bzero((char *)&(gw->sin), sizeof (gw->sin));
    gw->sin.sin_family = AF_INET;
    gw->sin.sin_port = GWSTAT_PORT; /* modularity is a bit wrong ... */
    gw->sin.sin_addr = *(struct in_addr *) &(host_addr);

    gw->request_packets_sent = 0;
    gw->need_reply = TRUE;

    if ((gw->name = (char *) malloc (strlen (line) + 1)) == NULL) {
	  syslog (LOG_ERR, "malloc failure reading config file");
	  abort ();
	}
    strcpy (gw->name, line);

    if ((gw->primary_name = 
	 (char *) malloc (strlen (primary_name) + 1)) == NULL) {
      syslog (LOG_ERR, "malloc failure reading config file");
      abort ();
    }
    strcpy (gw->primary_name, primary_name);

    strcat (line, ".stats");
    gw->log_fd = open (line, O_RDWR | O_CREAT | O_APPEND, 0644);
    if (gw->log_fd < 0) {
      syslog (LOG_ERR, "%s. Can't open log file for %s.", 
	      errno_description (), gw->name);
      i--;
      continue;
    }

    syslog (LOG_INFO, "Adding host %s (%s)", 
	    primary_name, inet_ntoa(gw->sin.sin_addr));
  }

  if (i < 0) {
    syslog (LOG_ERR, "no valid gateways given, aborting");
    abort ();
  }
  gateways[++i].name = NULL; /* just to be safe */
  fclose (config);
}

u_long	gethst(host_str, primary_host_name)
char	*host_str, **primary_host_name;
{
  struct hostent *hstent;
  long		host;

    if ((*host_str < '0') || (*host_str > '9')) {
	if ((hstent = gethostbyname(host_str)) == NULL) {
	    syslog (LOG_ERR, "Don't know hostname %s", host_str);
	    return (0);
	}
	*primary_host_name = hstent->h_name;
	return(* (u_long *)(hstent->h_addr));
    } else {
	if ((host = inet_addr(host_str)) == NULL) {
	  syslog (LOG_ERR, "Don't know address %s", host_str);
	  return (0);
	}
	*primary_host_name = (char *) inet_ntoa ((struct in_addr *) host);
	return(host);
    }
}
