/* 
 * xrpc_print.c
 *
 * x-kernel v3.3
 *
 * Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 */

/*
 * xrpc_print.c  -- debugging support for Sun RPC
 *
 *	contributed by Rice University 7/11/90
 *
 * $Revision: 1.1 $
 * $Date: 1996/01/30 22:44:47 $
 */
 
/*
 * only include all this stuff if compiling a DEBUGGING kernel.
 */

#ifdef XK_DEBUG

#include "xkernel.h"
#include "xrpc.h"
#include "ip.h"
#include "udp.h"
#include "sunrpc.h"
#include "sunrpc_i.h"
#define NL() printf("\n")

/* rpc_print - print an rpc header */

void pauthstat();

/* print out message type */
static void
ptype(t)
    enum msg_type t;
{
  if (t == CALL) { 
    printf("CALL");
  } else if (t == REPLY) {
    printf("REPLY");
  } else {
    printf("ptype: Invalid type\n");
  }
}

/* print out reply status */ 
static void
prstat(t)
    enum reply_stat t;
{
  if (t == MSG_ACCEPTED) { 
    printf("MSG_ACCEPTED");
  } else if (t == MSG_DENIED) {
    printf("MSG_DENIED");
  } else {
    printf("prstat: Invalid reply status\n");
  }
}

/* print out accept status */ 
static void
pastat(t)
    enum accept_stat t;
{
  switch (t) {
    case SUCCESS:
      printf("SUCCESS");
      break;
    case PROG_UNAVAIL:
      printf("PROG_UNAVAIL");
      break;
    case PROG_MISMATCH:
      printf("PROG_MISMATCH");
      break;
    case PROC_UNAVAIL:
      printf("PROC_UNAVAIL");
      break;
    case GARBAGE_ARGS:
      printf("GARBAGE_ARGS");
      break;
    case SYSTEM_ERR:
      printf("SYSTEM_ERR");
      break;
    default:
      printf("pastat: invalid accept status");
      break;
  }
}

/* print out reject status */ 
static void
prejstat(t)
    enum reject_stat t;
{
  if (t == RPC_MISMATCH) { 
    printf("RPC_MISMATCH");
  } else if (t == AUTH_ERROR) {
    printf("AUTH_ERROR");
  } else {
    printf("prejstat: Invalid reject status\n");
  }
}

/* print accepted_reply */
static void
pareply(t)
    struct accepted_reply t;
{
  printf("ar_verf = opaque \n"); 
  printf("accept_stat = ");
  pastat(t.ar_stat);
  NL();

  if (t.ar_stat == PROG_MISMATCH) {
    printf("low = %d high = %d \n",t.ar_vers.low,t.ar_vers.high);
  }
} 

/* print  rejected reply */
static void
prejreply(t)
    struct rejected_reply t;
{
  printf("rejected_stat  ="); 
  prejstat(t.rj_stat);
  NL();

  if (t.rj_stat == RPC_MISMATCH) {
    printf("low = %d high = %d \n",t.rj_vers.low,t.rj_vers.high);
  } else  if (t.rj_stat == AUTH_ERROR) {
    printf("RJ_why  ="); 
    pauthstat(t.rj_why);
    NL();
 }
}
/* print reply body*/
static void
preply(t)
    struct reply_body t;
{
  printf("rejected_stat  ="); 
  prstat(t.rp_stat);
  NL();

  if (t.rp_stat == MSG_ACCEPTED) {
    pareply(t.rp_acpt);
  } else if (t.rp_stat == MSG_DENIED) {
    prejreply(t.rp_rjct);
  }
}

/* print call body*/
static void
pcall(t)
    struct call_body t;
{
  printf("rpcvers = %d \n",t.cb_rpcvers);
  printf("prog = %d \n",t.cb_prog);
  printf("vers = %d \n",t.cb_vers);
  printf("proc = %d \n",t.cb_proc);
  printf("cred = opaque \n");
  printf("verf = opaque \n");
}

/* print rpc header */
void
prpchdr(t, s)
    struct rpc_msg t;
    char *s;
{
  printf("%s:\n", s);
  printf("rm_xid = %d\n", t.rm_xid);
  printf("direction = ");
  ptype(t.rm_direction);
  NL();
  if (t.rm_direction == CALL) {
    pcall(t.rm_call);
  } else if (t.rm_direction == REPLY) {
    preply(t.rm_reply);
  }
}

/* print authentication stuff */

/* print auth status */
void
pauthstat(t)
    enum auth_stat t;
{
  switch(t) {
    case AUTH_OK:
      printf("OK");
      break;
    case AUTH_BADCRED:
      printf("BADCRED");
      break;
    case AUTH_REJECTEDCRED:
      printf("REJECTCRED");
      break;
    case AUTH_BADVERF:
      printf("BADVERF");
      break;
    case AUTH_REJECTEDVERF:
      printf("REJECTVERF");
      break;
    case AUTH_INVALIDRESP:
      printf("INVALIDRESP");
      break;
    case AUTH_FAILED:
      printf("AUTH_FAILED");
      break;
    default:
      printf("pauthstat: invalid auth status\n");
      break;
  }
  printf(" ( %d )",  (int) t);
}


void
sunrpcPrActiveKey(key, s)
    ActiveKey *key;
    char *s;
{
    printf("%s: vers=%d prog=%d lls=%x\n",
	   s, key->p.vers, key->p.prog, key->lls);
    xIfTrace(sunrpcp, 8) {
	int i;
	
	for( i=0; i < sizeof(ActiveKey); i++) {
	    printf("%x ", ((u_char *)key)[i]);
	}
	printf("\n");
    }
}


void
sunrpcPrPassiveKey(key, s)
    PassiveKey *key;
    char *s;
{
    printf("%s: vers=%d prog=%d port=%d\n",
	   s, key->p.vers, key->p.prog, key->port);
    xIfTrace(sunrpcp, 8) {
	int i;
	
	for( i=0; i < sizeof(PassiveKey); i++) {
	    printf("%x ", ((u_char *)key)[i]);
	}
	printf("\n");
    }
}


#endif /* XK_DEBUG */
