/* dapwait.c - DAP: Deal with incoming activity */

#ifndef	lint
static char *rcsid = "$Header: /var/lib/cvsd/repos/CSRG/43BSDReno/contrib/isode-beta/dsap/net/dapwait.c,v 1.1.1.1 2018/04/24 16:12:56 root Exp $";
#endif

/* 
 * $Header: /var/lib/cvsd/repos/CSRG/43BSDReno/contrib/isode-beta/dsap/net/dapwait.c,v 1.1.1.1 2018/04/24 16:12:56 root Exp $
 *
 *
 * $Log: dapwait.c,v $
 * Revision 1.1.1.1  2018/04/24 16:12:56  root
 * BSD 4.3reno
 *
 * Revision 7.0  90/07/26  14:45:31  mrose
 * *** empty log message ***
 * 
 */

/*
 *				  NOTICE
 *
 *    Acquisition, use, and distribution of this module and related
 *    materials are subject to the restrictions of a license agreement.
 *    Consult the Preface in the User's Manual for the full terms of
 *    this agreement.
 *
 */


/* LINTLIBRARY */

#include "logger.h"
#include "quipu/util.h"
#include "quipu/dap2.h"


extern LLog	* log_dsap;

#ifdef PDU_DUMP
#define DUMP_ARG 	"arg"
#define DUMP_RES 	"res"
#define DUMP_ERR 	"err"
#endif

/*
* Wait routine for a DAP initiator.
*/

/* ARGSUSED */

int	  DapInitWaitRequest (sd, secs, di)
int			  sd;
int			  secs;
struct DAPindication	* di;
{
    int	  result;
    struct RoSAPindication	  roi_s;
    struct RoSAPindication	* roi = &(roi_s);

    DLOG (log_dsap,LLOG_TRACE,( "DapInitWaitRequest()"));

    result = RoWaitRequest(sd, secs, roi);

    if (result == NOTOK)
    {
	return (ros2daplose (di, "RoBindWaitRequest", &(roi->roi_preject)));
    }

    switch(roi->roi_type)
    {
	case ROI_INVOKE:
	    LLOG (log_dsap, LLOG_EXCEPTIONS, ("DapInitWaitRequest: Invocation received"));
	    DRejectRequest (sd, ROS_IP_UNRECOG, roi->roi_invoke.rox_id);
	    return (daplose (di, DP_ROS, NULLCP, "DAP initiator cannot accept invokes"));

	case ROI_RESULT:
	    return (DapDecodeResult (sd, &(roi->roi_result), di));

	case ROI_ERROR:
	    return (DapDecodeError (sd, &(roi->roi_error), di));

	case ROI_UREJECT:
	    LLOG (log_dsap, LLOG_EXCEPTIONS, ("DapInitWaitRequest: Operation (%d) user rejected (%d)", roi->roi_ureject.rou_id, roi->roi_ureject.rou_reason));
		return (ros2dapreject(di, "ROI_UREJECT", &(roi->roi_ureject)));

	case ROI_PREJECT:
		LLOG (log_dsap, LLOG_EXCEPTIONS, ("DapInitWaitRequest: Operation (%d) provider rejected", roi->roi_preject.rop_id));
		return (ros2daplose (di, "ROI_PREJECT", &(roi->roi_preject)));

	case ROI_FINISH:
	    LLOG (log_dsap, LLOG_EXCEPTIONS, ("DapInitWaitRequest: Unbind request received"));
	    return (daplose (di, DP_ROS, NULLCP, "DAP initiator cannot accept unbind requests"));

	default:
	    LLOG (log_dsap,LLOG_EXCEPTIONS,( "Unknown indication type : %d", roi->roi_type));
	    return (daplose (di, DP_ROS, NULLCP, "DapInitWaitRequest(): RoWaitRequest error"));
        }
	/* NOT REACHED */
}

int	  DapDecodeResult (sd, ror, di)
int			  sd;
struct RoSAPresult	* ror;
struct DAPindication	* di;
{
    int			  success;
    PE			  pe = ror->ror_result;
    struct DSResult	* res = &(di->di_result.dr_res);

    di->di_type = DI_RESULT;
    di->di_result.dr_id = ror->ror_id;

#ifdef PDU_DUMP
	    pdu_dump (pe,DUMP_RES,ror->ror_op);
#endif

#ifdef	HEAVY_DEBUG
	    pdu_res_log (pe, ror->ror_op);
#endif

    switch(res->result_type = ror->ror_op)
    {
    case    OP_READ :
	success = decode_DAS_ReadResult(pe,1,NULLIP,NULLVP,&(res->res_rd));
	break;
    case    OP_COMPARE :
	success = decode_DAS_CompareResult(pe,1,NULLIP,NULLVP,&(res->res_cm));
	break;
    case    OP_ABANDON :
	success = decode_DAS_AbandonResult(pe,1,NULLIP,NULLVP,NULLCP);
	break;
    case    OP_LIST :
	success = decode_DAS_ListResult(pe,1,NULLIP,NULLVP,&(res->res_ls));
	break;
    case    OP_SEARCH :
	success = decode_DAS_SearchResult(pe,1,NULLIP,NULLVP,&(res->res_sr));
	break;
    case    OP_ADDENTRY :
	success = decode_DAS_AddEntryResult(pe,1,NULLIP,NULLVP,NULLCP);
	break;
    case    OP_REMOVEENTRY :
	success = decode_DAS_RemoveEntryResult(pe,1,NULLIP,NULLVP,NULLCP);
	break;
    case    OP_MODIFYENTRY :
	success = decode_DAS_ModifyEntryResult(pe,1,NULLIP,NULLVP,NULLCP);
	break;
    case    OP_MODIFYRDN :
	success = decode_DAS_ModifyRDNResult(pe,1,NULLIP,NULLVP,NULLCP);
	break;

    default:
	LLOG(log_dsap, LLOG_EXCEPTIONS, ("DapDecodeResult: op id %d unknown!", ror->ror_op));
	DRejectRequest (sd, ROS_RRP_UNRECOG, ror->ror_id);
	return (daplose (di, DP_RESULT, NULLCP, "Unknown operation identifier"));
    }

    if (success == NOTOK)
    {
	LLOG (log_dsap, LLOG_EXCEPTIONS, ("DapDecodeResult: Unable to parse argument"));
	DRejectRequest (sd, ROS_RRP_MISTYPED, ror->ror_id);
	return (daplose (di, DP_RESULT, NULLCP, "Undecodable argument"));
    }

    return(success);
}

int	  DapDecodeError (sd, roe, di)
int			  sd;
struct RoSAPerror	* roe;
struct DAPindication	* di;
{
    int			  success;
    PE			  pe = roe->roe_param;
    struct DSError	* err = &(di->di_error.de_err);

#ifdef PDU_DUMP
	    pdu_dump (pe,DUMP_ERR,roe->roe_id);
#endif

    di->di_type = DI_ERROR;
    di->di_error.de_id = roe->roe_id;

    switch(err->dse_type = roe->roe_error)
    {
    case    DSE_ABANDON_FAILED :
	success = decode_DAS_AbandonFailedParm(pe,1,NULLIP,NULLVP,&(err->dse_un.dse_un_abandon_fail));
	break;
    case    DSE_ATTRIBUTEERROR :
	success = decode_DAS_AttributeErrorParm(pe,1,NULLIP,NULLVP,&(err->dse_un.dse_un_attribute));
	break;
    case    DSE_NAMEERROR :
	success = decode_DAS_NameErrorParm(pe,1,NULLIP,NULLVP,&(err->dse_un.dse_un_name));
	break;
    case    DSE_REFERRAL :
	success = decode_DAS_ReferralParm(pe,1,NULLIP,NULLVP,&(err->dse_un.dse_un_referral));
	break;
    case    DSE_SECURITYERROR :
	success = decode_DAS_SecurityErrorParm(pe,1,NULLIP,NULLVP,&(err->dse_un.dse_un_security));
	break;
    case    DSE_SERVICEERROR :
	success = decode_DAS_ServiceErrorParm(pe,1,NULLIP,NULLVP,&(err->dse_un.dse_un_service));
	break;
    case    DSE_UPDATEERROR :
	success = decode_DAS_UpdateErrorParm(pe,1,NULLIP,NULLVP,&(err->dse_un.dse_un_update));
	break;
    case    DSE_ABANDONED :
	success = ((pe == NULLPE) ? OK : NOTOK);
	break;
    case	DSE_DSAREFERRAL :
	success = decode_DO_DSAReferralParm(pe, 1, NULLIP, NULLVP, &(err->dse_un.dse_un_referral));
	break;

    default:
	LLOG(log_dsap, LLOG_EXCEPTIONS, ("DapDecodeError: op id %d unknown!", roe->roe_error));
	DRejectRequest (sd, ROS_REP_UNRECOG, roe->roe_id);
	return (daplose (di, DP_ERROR, NULLCP, "Unknown operation identifier"));
    }

    if (success == NOTOK)
    {
	LLOG (log_dsap, LLOG_EXCEPTIONS, ("DapDecodeError: Unable to parse argument"));
	DRejectRequest (sd, ROS_RRP_MISTYPED, roe->roe_id);
	return (daplose (di, DP_ERROR, NULLCP, "Undecodable argument"));
    }

    return(success);
}

