/*
 *------------------------------------------------------------------
 *
 * $Source: $
 * $Revision: 1.1 $
 * $Date: 87/06/24 14:09:52 $
 * $State: Exp $
 * $Author: jon $
 * $Locker: jon $
 *
 * $Log:	rvd.c,v $
 * 
 *------------------------------------------------------------------
 */

#ifndef lint
static char *rcsid_do_request_c = "$Header: infwd.c,v 1.3 87/06/24 14:11:49 jon Exp $";
#endif	lint

#include <stdio.h>
#include "ss.h"

#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include "gadmin.h"

#include "sys/include/types.h"
#include "gw/in/inudp.h"
#include "gw/in/gw_control.h"

do_request (gii, gateway_name, gateway_addr,
	    request, get_password, request_data, request_data_len)
     gadmin_invocation_info *gii;
     char *gateway_name;
     u_long gateway_addr;
     int get_password;
     int request;
     char *request_data;
     int request_data_len;
{
  char buffer[2048], msg[32], *password, *getpass();
  gw_control_header *pkt;
  static int request_number = 1;
  struct sockaddr_in sin;
  int cc;

  pkt = (gw_control_header *) buffer;

  if (get_password) {
    password = getpass ("Password:");
    if (*password == NULL) return;
    strncpy (pkt->password, password, 8);
  }

  pkt->request = htonl (request);

  strncpy (pkt->version, GW_CONTROL_PROTOCOL_VERSION_1, 8);
  pkt->request_number = request_number++;

  if (request_data)
    bcopy (request_data, buffer + sizeof(gw_control_header), request_data_len);

  bzero((char *)&sin, sizeof (sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons (GW_CONTROL);
  sin.sin_addr = *(struct in_addr *) &(gateway_addr);
  
  cc = sendto (gii->gw_send_socket, buffer,
	       sizeof (gw_control_header) + request_data_len,
	       0 /* flags */, &sin, sizeof (sin));
  if (cc < 0) {
    (void) sprintf (msg, "Sending to %s.", 
		    host_string (gateway_name, gateway_addr));
    ss_perror (gii->sci_idx, errno, msg);
    return;
  }
}
