#ifndef lint
static char *RCSid = "$Header: /afs/.net.mit.edu/tools/src/rprst/RCS/rprst.c,v 1.2 92/01/12 20:51:19 tom Exp $";
#endif

#include <stdio.h>
#include <strings.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <arpa/snmp/snmp_hs.h>

/* definitions */
#define DFL_SID			"public" /* default session id */
#define DFL_RETRY		1

static objident cvted = { 13,
			    1, 3, 6, 1, 4, 1, 52, 1, 2, 1, 1, 18, 0};


main(argc,argv)
int argc;
char *argv[];
{ 
  int i;				/* indices and counters */
  setreq setmsg;			/* the set message */
  struct in_addr netent;		/* address of entity to be altered */
  struct hostent *hp;			/* in case symbolic host specified */
  char *s_sid;		                /* sids for sending, receiving */
  short rc;				/* return code */
  int retries;				/* number of retries */
  char *malloc();
  long atol();

  retries = DFL_RETRY;
  s_sid = DFL_SID;
  if(argc < 2)
    { 
      fprintf(stderr,"usage: %s -c <community> -r <count> repeater\n", argv[0]);
      exit(1);
    }
  /* process options */
  while(argv[1][0] == '-')
    switch(argv[1][1]) 
      {
      case 'c':				/* session id */
	s_sid = argv[2];
	argc -= 2; argv += 2;		/* move on */
	break;
      case 'r':
	retries = atoi(argv[2]);
	argc -=2; argv += 2;
	break;
      default:				/* unknown option */
	fprintf(stderr,"snmpset: unknown option %s\n",argv[1]);
	argv++; argc--;			/* move past bad option */
	break;
      }
  
  /* get network entity address */
  if(argc < 2)
    { 
      fprintf(stderr,"snmpset: no net entity to alter\n");
      exit(1);
    }

  if(isalpha(argv[1][0]))		/* if symbolic hostname specified */
    if((hp = gethostbyname(argv[1])) == NULL)
      { 
	fprintf(stderr,"snmpset: could not get address for %s\n",argv[1]);
	exit(1);
      }
    else					/* could get host address */
      bcopy((char *)hp->h_addr,(char *)&netent.s_addr,hp->h_length);
  else					/* an IP address was specified */
    netent.s_addr = inet_addr(argv[1]);
  argc--; argv++;			/* move on */

  setmsg.reqid = getpid() ^ time((long *)NULL);
  setmsg.errstat = setmsg.errindex = 0;
  setmsg.varlist.len = 1;
  oidcpy(&(setmsg.varlist.elem[0].name),&(cvted));
  setmsg.varlist.elem[0].name.ncmp = cvted.ncmp;
  setmsg.varlist.elem[0].val.type = INT;
  setmsg.varlist.elem[0].val.value.intgr = 0;
  printf("sending.");
  fflush(stdout);
  for (i = 0; i < retries; i++)
    if((rc = snmpsend(SET,setmsg.reqid,&netent,"snmp",(char *)&setmsg,
		      s_sid,strlen(s_sid),(long)10)) < 0)
      { 
	error(rc,stderr);
	exit(1);
      }
    else
      {
	printf(".");
	fflush(stdout);
      }
  printf(" done.\n");

  exit(0);
}
