/* chokecall.c this does the syscall to enables the module and loads 
the defaults into the module.
$Id: chokecall.c,v 1.4 1999/04/28 19:58:53 jeremiah Exp jeremiah $
$Log: chokecall.c,v $
Revision 1.4  1999/04/28 19:58:53  jeremiah
Chnaged name of module

Revision 1.3  1999/04/28 19:47:58  jeremiah
broke code up into seperate sections as well as spun some off into choke_error.h. Also commented code

Revision 1.2  1999/04/28 02:59:05  jeremiah
Inital creation

*/

#include "choke_error.h"



void main(int argc, char * argv[]){
  
  char * module_name = "choke_module.ko";
  char * module_path_name = "/home/jeremiah/networking/choke_module.ko";
  int sysctl_oid[4];

  /* set the sysctl oid to  net.inet.ip.chokeing */
  sysctl_oid[0] = CTL_NET;
  sysctl_oid[1] = PF_INET;
  sysctl_oid[2] = 0;
  sysctl_oid[3] = 242;
  
 
  if (argc < 2){
    print_usage();
  }
  if(!strcmp(argv[1],"load")) {
    load(module_path_name,sysctl_oid);
  }
  else if (!strcmp(argv[1],"unload")){
    unload(module_name,sysctl_oid);
  }
  else {
    print_usage();
  }
    
}

/* this loads the module and then calls sysctl to let the system know that it's been enabled. This exits on any error to insure system stability */
void load(char * module_name,int * sysctl_oid){
  int error = 0;
  int fileid;
  
  /* this function loads the module */
  fileid = kldload(module_name);
  if (fileid < 0){
    err(1, "can't load %s", module_name);
    exit(-1);
  }
  else {
    printf("Loaded %s, id=%d\n", module_name, fileid);
  }
  fileid = 1;
  /* this sets net.inet.ip.chokeing to 1 */ 
  error = sysctl(sysctl_oid, 4, NULL,NULL, &fileid, sizeof(int));
  if (error < 0)
    print_error();
}

/*this calls sysctl to let the system know that the module has been disabled. It then unloads the module. It exit's on any error, to insure system stability */

void unload(char * module_name, int * sysctl_oid){
  int fileid = 0; 
  int error;
 
  /* this sets net.inet.ip.chokeing to 0 */ 
  error = sysctl(sysctl_oid, 4, NULL,NULL, &fileid, sizeof(int));
  if (error < 0)
    print_error();
  /* this function finds the module_id */
  if ((fileid = kldfind(module_name)) < 0) {  
    err(1, "can't find file %s", module_name);  
    exit(-1);  
  } 
  /* this unloads the module_id */
  if (kldunload(fileid) < 0) {  
    err(1, "can't unload file");  
    exit(-1);  
  }  
}
