/*
 * This is the main file.
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Tom Coppeto
 * MIT Network Services
 * 8 August 1990
 *
 *    $Source: /afs/.net/tools/src/xinterface/RCS/main.c,v $
 *    $Author: tom $
 *    $Locker: tom $
 *    $Log: main.c,v $
 * Revision 1.6  1994/07/25  10:47:41  tom
 * *** empty log message ***
 *
 * Revision 1.5  91/02/21  13:44:51  tom
 * allows for hostnames to begin with numbers
 * improved vendor type checking
 * 
 * Revision 1.4  90/10/29  09:18:01  tom
 * *** empty log message ***
 * 
 * Revision 1.3  90/08/19  16:18:57  tom
 * *** empty log message ***
 * 
 * Revision 1.2  90/08/15  14:08:16  tom
 * use setenv to reste app dir
 * 
 * Revision 1.1  90/08/15  01:13:07  tom
 * Initial revision
 * 
 */

#ifndef lint
static char *rcsid = "$Header: /afs/.net/tools/src/xinterface/RCS/main.c,v 1.6 1994/07/25 10:47:41 tom Exp tom $";
#endif

#include "xport.h"
#include <mit-copyright.h>

char communityB[100];

#ifndef XAPPLRESDIR
#define XAPPLRESDIR "/usr/lib/X11/app-defaults/"
#endif


main(argc, argv)
     int  argc;
     char **argv;
{
  struct hostent *hp = (struct hostent *) NULL;
  unsigned long laddr = 0;
  char **prog;
  char *c;
  int len;
  char buf[BUFSIZ];

#ifdef sgi
  sprintf(buf, "XAPPLRESDIR=%s", XAPPLRESDIR);
  putenv(buf);
#else /* sgi */
  setenv("XAPPLRESDIR", XAPPLRESDIR, 1);
#endif /* sgi */

  prog = argv;
  while(*++argv)
    {      
      if((strlen(*argv) > 1) && !strncmp(*argv, "-help", strlen(*argv)))
	usage(*prog);

      if((strlen(*argv) > 1) && !strncmp(*argv, "-community", strlen(*argv)))
	{
	  if(*++argv == (char *) NULL)
	    usage(*prog);
	  len = strlen(*argv);
	  strncpy(communityB, *argv, len);
	  community = communityB;
	  if(len > 1)
	    **argv = 'x';
	  continue;
	}

      if((*argv[0] >= '0') && (*argv[0] <= '9'))
	if((laddr = inet_addr(*argv)) > 0)
	  hp = gethostbyaddr((char *) &laddr, 4, AF_INET);
	  
      if(!hp)
	{
	  hp =  gethostbyname(*argv);
	  if(!hp && !laddr)
	    {
	      fprintf(stderr, "Cannot resolve name \"%s\".\n", *argv);
	      exit(1);
	    }
	  else
	    if(hp)
	      laddr = 0;
	}
    }
  
  if(!hp && !laddr)
    usage(*prog);

  if(laddr > 0)
    addr.s_addr = laddr;
  else
    bcopy(hp->h_addr, &(addr.s_addr), hp->h_length);
  
  if((c = (char *) malloc((strlen(community) + 1) * sizeof(char))) 
     == (char *) NULL)
    exit(1);
  strcpy(c, community);
  community = c;

  if(get_config() != SUCCESS)
    {
      fprintf(stderr, "unable to get configuration...\n");
      exit(1);
    }

  draw_screen(argc, prog);
}
 




usage(prog)
     char *prog;
{
  fprintf(stderr, "usage: %s [-c <community name>] [agent]\n", prog);
  exit(1);
}



/*
 * Function:    get_config()
 * Description: gets the "static configuration" of the box.
 */


int
get_config()
{
  struct hostent *hp = (struct hostent *) NULL;
  extern getreq reply;
  unsigned long laddr = 0;
  objident *obj[SNMPMAXVARS];
  objident save;
  caddr_t *dat;
  caddr_t datt;
  char addr[16];
  int *frp;
  int i = 0;
  int o;

  nints = 0;
  bzero(interface, sizeof(struct _interface) * MAX_INTS);

  o = 0;
  obj[o++] = &vSysDescr;
  obj[o++] = &vIfNumber;
  obj[o++] = (objident *) NULL;

  if((dat = make_lots_o_snmp_queries(obj)) == (caddr_t *) NULL)
    return(ERROR);

  o = 0;
  sysdesc = strcpy(malloc(strlen(dat[o]) + 1), dat[o]);
  strip_word(sysdesc, "copyright");
  do_vendor(sysdesc);

  ++o;
  bcopy(dat[o], &nints, sizeof(nints));

  for(i = 0; i < nints; i++)
    {
      interface[i].index = i;
      vIfDescr.cmp[vIfDescr.ncmp-1] = i + 1;
      vIfType.cmp[vIfType.ncmp-1]   = i + 1;
      vIfMtu.cmp[vIfMtu.ncmp-1]     = i + 1;
      vIfSpeed.cmp[vIfSpeed.ncmp-1] = i + 1;

      o = 0;
      obj[o++] = &vIfDescr;
      obj[o++] = &vIfType;
      obj[o++] = &vIfMtu;
      obj[o++] = &vIfSpeed;
      obj[o++] = (objident *) NULL;

      if((dat = make_lots_o_snmp_queries(obj)) == (caddr_t *) NULL)
	return(ERROR);
      
      o = 0;
      interface[i].descr = strcpy(malloc(strlen(dat[o]) + 1), (char *) dat[o]);
      o++;
      bcopy(dat[o++], &(interface[i].type),  sizeof(interface[i].type));
      bcopy(dat[o++], &(interface[i].mtu),   sizeof(interface[i].type));
      bcopy(dat[o++], &(interface[i].speed), sizeof(interface[i].speed));
    }

  datt = make_snmp_next(&vIAIfIndex);
  while(dat)
    {
      if(oidncmp(&vIAIfIndex, &(reply.varlist.elem[0].name), 10) != 0)
	 break;
      frp = (int *) datt;
      if((*frp > 0) && (*frp <= nints))
	{
	  interface[*frp - 1].addr[0] = 
	    reply.varlist.elem[0].name.cmp[reply.varlist.elem[0].name.ncmp-4];
	  interface[*frp - 1].addr[1] = 
	    reply.varlist.elem[0].name.cmp[reply.varlist.elem[0].name.ncmp-3];
	  interface[*frp - 1].addr[2] = 
	    reply.varlist.elem[0].name.cmp[reply.varlist.elem[0].name.ncmp-2];
	  interface[*frp - 1].addr[3] = 
	    reply.varlist.elem[0].name.cmp[reply.varlist.elem[0].name.ncmp-1];
	  sprintf(addr, "%d.%d.%d.%d", interface[*frp - 1].addr[0],
		  interface[*frp - 1].addr[1], interface[*frp - 1].addr[2],
		  interface[*frp - 1].addr[3]);
	  if((laddr = inet_addr(addr)) > 0)
	    if((hp = gethostbyaddr((char *) &laddr, 4, AF_INET)))
	      interface[*frp - 1].name = strcpy(malloc(strlen(hp->h_name)+1), 
						hp->h_name);
	}
      bcopy(&(reply.varlist.elem[0].name), &save, sizeof(save));
      datt = make_snmp_next(&save);
    }
  do_update_status();
  return(SUCCESS);
}


do_vendor(desc)
     char *desc;
{
  int l;
  int i = 0;
  char *c;

  while(vendors[i].id)
    {
      l = strlen(vendors[i].name);
      c = desc;      
      while(*c)
	{
	  if(strncasecmp(c, vendors[i].name, l) == 0)
	    {
	      vendor = vendors[i].id;
	      return;
	    }
	  ++c;
	}
      ++i;
    }
}
