/* Copyright 1986 by the Massachusetts Institute of Technology */
/* See permission and disclaimer notice in file notice.h */
#include	"notice.h"

/* CGW control protocol ... the other side of gadmin ... */

/*
 *------------------------------------------------------------------
 *
 * $Source: /achilles/u1/jis/gw/cgw/gw/in/RCS/gw_control.c,v $
 * $Revision: 1.3 $
 * $Date: 87/10/06 18:34:58 $
 * $State: Exp $
 * $Author: jon $
 * $Locker: jon $
 *
 * $Log:	gw_control.c,v $
 * Revision 1.3  87/10/06  18:34:58  jon
 * ifdef out non-working functions
 * 
 * Revision 1.2  87/10/06  18:12:42  jon
 * Supports old form of REBOOT/RESTART (i.e. no version string).
 * 
 * Revision 1.1  87/09/18  04:35:09  jon
 * Initial revision
 * 
 * 
 *------------------------------------------------------------------
 */

#ifndef lint
static char *rcsid_gwcontrolc = "$Header: gw_control.c,v 1.3 87/10/06 18:34:58 jon Locked $";
#endif	lint

#include	<types.h>
#include	<sys.h>
#include	"../src/const.h"
#include	"../src/param.h"
#include	"../src/defs.h"
#include	"../src/macs.h"
#include	"../src/net.h"
#include	"../src/ext.h"
#include	"in.h"
#include	"inparam.h"
#include	"inext.h"
#include	"inpro.h"
#include        "inudp.h"
#include	"inrte.h"
#include        "gw_control.h"

gw_control (iob)
     iorb  *iob;
{
    reg	struct netinfo	*data;
    reg	gw_control_header *cpkt;
    reg iprte		*ipr;
    unsl                req;
    inpkt		*ipkt;
    udppkt		*upkt;
    inaddr		*iaddr;	/* address of the gateway that sent this
				 * packet */

    ipkt = mkin(iob->i_addr);
    iaddr = &ipkt->i_src;
    cpkt = (gw_control_header *)((byte *)ipkt + sizeof(inpkt) + sizeof(udppkt));

    req = swabl (cpkt->request);
    
    if ((req != REBOOT) && (req != RESTART)) /* don't break old gwrst, sigh */
      if (strcmp (cpkt->version, GW_CONTROL_PROTOCOL_VERSION_1)) {
	niflog(L_ERRU)
	  dolog("gw_control: Bad packet version string (%s) from %d.%d.%d.%d\n",
		cpkt->version, mksina(iaddr));
	freebuf(iob);
	return;
      } 

    niflog(L_TRCO)
      dolog("Control message %d from %d.%d.%d.%d\n", 
	    req, mksina(iaddr));

    switch (req) {
       case REBOOT:
       case RESTART:
         md_reboot(req, cpkt->password);
	 break;
	 /* bulit proof with sanity length checks later */
       case DUMP_ROUTES:
	 {
#ifdef ndef
	   reg iprte *ipr, **iprp;
	   inaddr iaddr;
	   char *addr_str;
	   
	   addr_str = (char *) (cpkt + sizeof (gw_control_header));
	   iaddr.i_long = getdmch(addr_str);
	   if (iaddr.i_long != 0) {
	     if ((ipr = ipr_lookup(iaddr.i_long)) != NULL) {
	       ipr_prrte(devp, ipr);
	       return;
	     }
	fprintf(devp, "No route to %d.%d.%d.%d, use default\n",
		mksina(&iaddr));
	     fprintf(devp, defdmp, indefgi->n_net, mksina(&indefga));
	   }
#endif
	 }
	 freebuf(iob);
       }
  }

