/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_ISODEBind_addr[] = "@(#)ISODEbind_ad.c	1.16 11/13/92 10:49:12  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	10/11/90

	ROUTINE:	ISODEBind_addr

PURPOSE:
	Establish an association between two entities.  The currently 
 	supported platforms are all in this source file and are #ifdef
	to include the code.
	


IN:

OUT:


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:
	Added sending_PPDUs field to instance record (ls 920414)


*/

#ifdef __RO_PLATFORM_ISODE

#include <netdb.h>
#include <sys/types.h>

#if defined(ultrix)
#include <sys/file.h>
#else
#include <sys/fcntl.h>
#endif


#include <elros/instance.h>
#include <elros/binds.h>
#include <elros/specifics.h>

#ifdef __RO_TASKING

#include <elros/tasking.h>
extern int __ROv_ISODEListenFileDesc;		/* Defined in tasking.c */

#else

     int __ROv_ISODEListenFileDesc;

#endif

#include <isode/tsap.h>
#include <isode/pepsy/ACS-types.h>
#define ACSE
#include <isode/acpkt.h>


# define ISODE_ADDR(s)( (s)->un.isode_addr )
# define ISODE_INSTADDR(i)( (i)->subsystem->specific.isode )
# define INITIATOR_ACSEBER_PCID		3
# define INITIATOR_INTERFSPECIFIC_PCID	1
# define  __RO_UDVEC_MAXIMUM		30

int __ROv_ISODE_initialized = 0;
int __ROv_ISODE_listening = 0;


int __ROr_ISODEBind_addr( inst, rules, addr_struct, timeout, udata)
   
   __RO_Instance_Record	*inst;
   __RO_Presentation_rules rules;
   __RO_Address		*addr_struct;
   int			timeout;
   User_data		*udata;

  {

   int			fd;


   inst->subsystem->platform = __ROm_platform_ISODE;
   inst->subsystem->role = addr_struct->role;
   inst->subsystem->timeout = timeout;

   /* Reset to always send PPDU wrapping for ISODE */
   inst->sending_PPDUs = 1;

	/* log the addresses at layer 6 */
   if (__ROr_Logging6on(inst))
   {
      char buffer[512];
      sprintf(buffer,"BIND: platform=ISODE, role=%s",
		addr_struct->role==Initiator ? "INITIATOR" :
		addr_struct->role==Responder ? "RESPONDER" :
		"***UNKNOWN***");
      if (__ROr_log6comment(inst,buffer))
         return(EXCEPTION);
      sprintf(buffer,"BIND: calling_address=%s",
		paddr2str(ISODE_ADDR(addr_struct).calling,NULLNA));
      if (__ROr_log6comment(inst,buffer))
         return(EXCEPTION);
      sprintf(buffer,"BIND: called_address=%s",
		paddr2str(ISODE_ADDR(addr_struct).called,NULLNA));
      if (__ROr_log6comment(inst,buffer))
         return(EXCEPTION);
   }

   if (__ROr_STDinit_bind(inst,rules,__ROr_ISODEsend_data,__ROr_ISODEreceive_data))
      return(EXCEPTION);

   if( !__ROv_ISODE_initialized )
   {
      isodetailor(0,0); /* Initialize the ts_communities and ts_stacks */
      __ROv_ISODE_initialized = 1;
   }


   switch ( addr_struct->role )
   {

      case Initiator:
         if (__ROr_ISODEBind_addr_INITIATOR(inst,addr_struct,udata))
            return(EXCEPTION);
         break;
      case Responder:
         if (__ROr_ISODEBind_addr_RESPONDER(inst,addr_struct,udata))
            return(EXCEPTION);
         break;
      default:
         __RO_RETURN_RUNTIME( inst, __ROx_INTERNAL_MUTUAL );
   } 
	
	/* Allocate the scatter gatter array for sending.  */
   if( (ISODE_INSTADDR(inst).uvarray = 
       (struct udvec *)malloc( sizeof(struct udvec)*__RO_UDVEC_MAXIMUM)) == 0)
   {
      __RO_RETURN_RUNTIME( inst, __ROx_BIND_OUTOFMEM );
   }
   memset( ISODE_INSTADDR(inst).uvarray,
	   0,
	   (sizeof(struct udvec)*__RO_UDVEC_MAXIMUM ));

   ISODE_INSTADDR(inst).uvindex = 0;
   ISODE_INSTADDR(inst).uvmax = __RO_UDVEC_MAXIMUM;
 
   return(SUCCESS);
}

int __ROr_ISODEBind_addr_INITIATOR(inst,addr_struct,udata)

   __RO_Instance_Record	*inst;
   __RO_Address		*addr_struct;
   User_data		*udata;

{
   struct PSAPctxlist 	presentation_contextlist;
   OID temp_oid1,temp_oid2,temp_oid3;
   OID 			default_contextname;
   int 			presentation_requirements;
   int 			session_requirements;
   long 		initial_serial_number;
   int 			initial_token_settings;
   PE 			user_data;
   int 			user_data_length;
   struct SSAPref 	*session_connection_identifier;
   struct QOStype 	*quality_of_service;
   int			async_connection;
   int status;
   struct PSAPconnect 	presentation_connection_state;
   struct PSAPindication presentation_indication;
#ifdef __RO_TASKING
   unsigned long	absolute_timeout;
#endif


				/* Remote address cannot be NULL */
   if( ISODE_ADDR(addr_struct).called == NULLPA )
      __RO_RETURN_RUNTIME( inst, __ROx_BIND_ADDRESSNULL )
   else
      ISODE_INSTADDR(inst).responder_address = 
		*(ISODE_ADDR(addr_struct).called);
	

		/*
		 * Check the users IP address against that
		 * of the machine.
		 */
   if( ISODE_ADDR(addr_struct).calling != NULLPA )
   {
      struct hostent *userip;

      userip = (struct hostent *)gethostbystring((char *)(ISODE_ADDR(addr_struct).calling->pa_addr.sa_addr.ta_addrs[0].na_domain));

      if(!__ROr_verify_IPAddress(*(u_long *)(userip->h_addr)
#ifdef CRAY
                                 >>32
#endif
                                 ))
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_BADIPADDR );
   }
	
		/* If calling is null, a NULLPA will be 
		   be passed to the connect request and
	 	   a local address will be generated */
   if( ISODE_ADDR(addr_struct).calling != NULLPA )
      ISODE_INSTADDR(inst).initiator_address = 
			*(ISODE_ADDR(addr_struct).calling);
		
	
   presentation_contextlist.pc_nctx = 2;

   inst->ISO_Contexts.specific_pcid = INITIATOR_INTERFSPECIFIC_PCID;
   presentation_contextlist.pc_ctx[0].pc_id = 
			inst->ISO_Contexts.specific_pcid;

		/* This is the oid for the presentation
		   context name.  It appears to identify
		   and asn.1 specification. */

   presentation_contextlist.pc_ctx[0].pc_asn = temp_oid1 =
		oid_cpy( str2oid(inst->ISO_Contexts.prsnt_context_name) );	
   presentation_contextlist.pc_ctx[0].pc_atn = NULLOID;


   inst->ISO_Contexts.acse_pcid = INITIATOR_ACSEBER_PCID;
   presentation_contextlist.pc_ctx[1].pc_id = 
		inst->ISO_Contexts.acse_pcid; 

		/* These are the OIDs for the 
		   ACSE and BER transfer syntaxes. */
   presentation_contextlist.pc_ctx[1].pc_asn = temp_oid2 =
		oid_cpy(str2oid("2.2.1.0.1"));

   presentation_contextlist.pc_ctx[1].pc_atn = temp_oid3 =
		oid_cpy(str2oid("2.1.1"));

   default_contextname = NULLOID; 
   presentation_requirements = 0;
   session_requirements = SR_DUPLEX;
   initial_serial_number = SERIAL_NONE;
 

	   initial_token_settings = 0;

		/* Set up the inital token settings.	
		   Full duplex is all we support. dotokens()
		   is an ISODE routine that calls dotoken. */
#define dotoken(REQUIRES,SHIFT,BIT,TYPE)                                \
        if (session_requirements & REQUIRES)                            \
           initial_token_settings |= ST_INIT_VALUE << SHIFT;
   dotokens();
#undef dotoken

   session_connection_identifier = addr2ref(PLocalHostName());

				/* Establish the user-data for 
				   the ACSE Request APDU */
   {
      struct type_ACS_AARQ__apdu aarq_apdu;
      OID temp_oid;
      int result;
      struct AcSAPindication aci;
      struct assocblk acb;
      int nelem = 1;
 
      memset(&aarq_apdu,0,sizeof(aarq_apdu));
      memset(&acb,0,sizeof(acb));
 
			/* This is the "RIGHT" oid -- we 
			   need to get this from the user */
      aarq_apdu.application__context__name = temp_oid =
		oid_cpy(str2oid(inst->ISO_Contexts.appl_context_name));

      if (udata && udata->initiator_user_data) {
	 aarq_apdu.user__information = info2apdu(&acb,&aci,udata->initiator_user_data,nelem);
      }
       
      user_data = NULLPE;
      result = encode_ACS_AARQ__apdu(&user_data,1,0,NULLCP,&aarq_apdu);

      if(aarq_apdu.user__information)
	free_ACS_Association__information(aarq_apdu.user__information);
      if(result == NOTOK)
         __RO_RETURN_RUNTIME(inst, __ROx_BIND_ACSEREQUESTFAILED);
      user_data->pe_context = INITIATOR_ACSEBER_PCID;
      user_data_length = 1;

      oid_free(temp_oid);
   }

   quality_of_service = NULLQOS;


#ifdef __RO_TASKING
   async_connection = 1;
   absolute_timeout = __ROr_STDget_absolute_timeout(inst);
#else
   async_connection = 0;
#endif
				/* Now, OPEN the connection */
 
   status = PAsynConnRequest(
	           ( ISODE_ADDR(addr_struct).calling == NULL ?
			(NULLPA) : (&ISODE_INSTADDR(inst).initiator_address) ),
                   &ISODE_INSTADDR(inst).responder_address,
	           &presentation_contextlist,
	           default_contextname,
	           presentation_requirements,
	           session_requirements,
	           initial_serial_number,
	           initial_token_settings,
	           session_connection_identifier,
	           &user_data,user_data_length,
	           quality_of_service,
	           &presentation_connection_state,
	           &presentation_indication,
	           async_connection);
#ifdef __RO_TASKING
   do
   {
      int direction;

      if( status == NOTOK )
         __RO_RETURN_RUNTIME(inst,__ROx_BIND_ISODECONNECTFAILED);

      if( status == DONE )
         break;

      if( status == CONNECTING_1 )
      {
         direction = __ROm_WRITEDIR;
         if( __ROr_Scan( presentation_connection_state.pc_sd, 
					direction, inst ))
            return(EXCEPTION);
      }
      else if( status == CONNECTING_2 )
      {
         direction = __ROm_READDIR;
         if(__ROr_Scan( presentation_connection_state.pc_sd,
			    direction, inst ))
            return(EXCEPTION);
      }


      Alarm( absolute_timeout );
      SLEEP;
		
      if( TIME > absolute_timeout)
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_INITIATOR_TIMEOUT );

		/*  Clear socket structures in case of spurious wake */
      __ROr_ClearSocket( presentation_connection_state.pc_sd, __ROm_READDIR );
      __ROr_ClearSocket( presentation_connection_state.pc_sd, __ROm_WRITEDIR );

   } while ( (status = PAsynRetryRequest( 
				  presentation_connection_state.pc_sd,
				  &presentation_connection_state,
				  &presentation_indication )) != DONE
			      && status != NOTOK ); 
#endif /* __RO_TASKING */

   /*
    * Decode the application control data unit.
    */
   {
       struct type_ACS_ACSE__apdu *pdu;
       struct type_ACS_AARE__apdu *aare;
       struct AcSAPindication aci;
       struct assocblk acb;
       int error, nelem;

       error = decode_ACS_ACSE__apdu(presentation_connection_state.pc_info[0],
                                        1, NULLIP, NULLVP, &pdu);
       aare = pdu->un.aare;
       addr_struct->un.isode_addr.acc.acc_result = aare->result;

       /*
        * If user gave us a buffer for user data, extract user data, if there
        * is any.
        */
       if (udata && udata->responder_user_data && aare->user__information) {

           udata->responder_user_data_present = 1;
           memset(&acb,0,sizeof(acb));
           if (apdu2info(&acb,&aci,aare->user__information,udata->responder_user_data,&nelem) == NOTOK) {
              free_ACS_ACSE__apdu(pdu);
	      __RO_RETURN_RUNTIME( inst, __ROx_BIND_APDUCONVERSIONFAILED);
           }
       } else
        if (udata)
           udata->responder_user_data_present = 0;
       free_ACS_ACSE__apdu(pdu);
   }


				 
   pe_free(user_data);
   oid_free(temp_oid1);
   oid_free(temp_oid2);
   oid_free(temp_oid3);

   if (presentation_connection_state.pc_result != PC_ACCEPT)
      __RO_RETURN_RUNTIME(inst,__ROx_BIND_ISODECONNECTSTATE);
 
   ISODE_INSTADDR(inst).association =
	               presentation_connection_state.pc_sd;
   PCFREE(&presentation_connection_state);
		/*
		 * Enable Asynchronous writes
		 */
#ifdef __RO_TASKING
   {
      struct TSAPdisconnect tdisconnect;

      if( TSetQueuesOK( ISODE_INSTADDR(inst).association, 1, 
			        &tdisconnect ) == NOTOK )
      {
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_TSETQFAILED );
      }
   }
#endif
   return(SUCCESS);
}

int __ROr_ISODEBind_addr_RESPONDER(inst,addr_struct,udata)

   __RO_Instance_Record	*inst;
   __RO_Address		*addr_struct;
   User_data		*udata;

{
   struct PSAPstart 	presentation_start;
   struct TSAPdisconnect tdisconnect;
   int			status;		
   int			reject_reason;
   int 			session_requirements;
   long 		initial_serial_number;
   int 			initial_token_settings;
   PE 			user_data;
   int 			user_data_length;
   struct PSAPindication presentation_indication;
   unsigned long	absolute_timeout;

	 
   if( ISODE_ADDR(addr_struct).calling != NULLPA )
      __RO_RETURN_RUNTIME( inst, __ROx_BIND_ADDRESSNOTNULL );
   if( ISODE_ADDR(addr_struct).called == NULLPA )
      __RO_RETURN_RUNTIME( inst, __ROx_BIND_ADDRESSNULL )
   else
      ISODE_INSTADDR(inst).responder_address =
		 *(ISODE_ADDR(addr_struct).called);


		/* Initialize status for the response to PC_ACCEPT */
   status = PC_ACCEPT;

		/*
		 * Check the users IP address against that
		 * of the machine.
		 */
   {
      struct hostent *userip;

      userip = (struct hostent *)gethostbystring((char *)(ISODE_ADDR(addr_struct).called->pa_addr.sa_addr.ta_addrs[0].na_domain));


      if(!__ROr_verify_IPAddress(*(u_long *)(userip->h_addr)
#ifdef CRAY
                                 >> 32
#endif
                                 ))
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_BADIPADDR);
   }

   if ( !__ROv_ISODE_listening )
   {
		/* Find out what the listen socket number will be */
      __ROv_ISODEListenFileDesc = open( "/dev/null", O_RDWR );
      if( __ROv_ISODEListenFileDesc < 0 )
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_LISTENFAILED );
      close( __ROv_ISODEListenFileDesc );

		/* Listen on the socket */
      if( TNetListen ( &(ISODE_INSTADDR(inst).responder_address.pa_addr.sa_addr), &tdisconnect) == NOTOK  )
      {
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_LISTENFAILED );
      }
      __ROv_ISODE_listening = 1;
   }


   absolute_timeout = __ROr_STDget_absolute_timeout(inst);

	    /* 
	     * Accept a connection on the socket
	     * we have to loop until the argument vector 
	     * is filled in for Pinit.
	     * When tasking, the routine Hwait performs the TNetAccept
	     * calls and wakes the waiting tasks.
	     */
#ifdef __RO_TASKING
   {
      if(__ROr_Scan( __ROv_ISODEListenFileDesc, __ROm_READDIR, inst ))
         return(EXCEPTION);

      do
      {
         Alarm(absolute_timeout);
         SLEEP;

         if( TIME >= absolute_timeout )
            __RO_RETURN_RUNTIME( inst, __ROx_BIND_RESPONDER_TIMEOUT );

      } while (ISODE_INSTADDR(inst).vectorlength <= 0);

      if( PInit( ISODE_INSTADDR(inst).vectorlength,
		       ISODE_INSTADDR(inst).argvector, &presentation_start, 
		       &presentation_indication ) == NOTOK )
      {
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_ISOINITFAILED );
      }

      ISODE_INSTADDR(inst).association = presentation_start.ps_sd;

		/*
		 * Enable Asynchronous writes
		 */
      if( TSetQueuesOK( ISODE_INSTADDR(inst).association, 1, 
			     &tdisconnect ) == NOTOK )
      {
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_TSETQFAILED );
      }
   }

#else /* NO__RO_TASKING */
   {
      int numfds = 0;
      int accept_timeout = __ROr_STDget_timeout(inst);
      char *argvec[__ROc_ARGVECSIZE];
      int veclen;

      while(1)
      {
         int x;
         int connection_desc;
         struct TSAPaddr taddr;

         if( (x = TNetAcceptAux( &veclen,
		                argvec,
				&connection_desc,
				&taddr,
			        numfds,
			        NULLFD,
			        NULLFD,
			        NULLFD,
			        accept_timeout,
			        &tdisconnect )) == NOTOK ) 
         {
            __RO_RETURN_RUNTIME( inst, __ROx_BIND_ACCEPTFAILED );
         }

		/* 
		 * If the vector length is <= 0, no new connection 
		 * was established.  If it is due to a timeout we
		 * need to give up, otherwise loop around and wait
		 * for a connection
  		 */
         if( veclen <= 0 )
         {
		/*
		 * Recalculate the timeout for a blocking accept
		 */
	    accept_timeout = (absolute_timeout - TIME);

		/* If there is no more time left, then get out,
		   note that this can partially accept a connection
		   and then time out. */
            if( accept_timeout < 0)
               __RO_RETURN_RUNTIME(inst, __ROx_BIND_RESPONDER_TIMEOUT);
			
            continue;
         }
         else
            break;
      }
		


      if( PInit( veclen, argvec, &presentation_start, 
		       &presentation_indication ) == NOTOK )
      {
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_ISOINITFAILED );
      }

      inst->subsystem->specific.isode.association = presentation_start.ps_sd;
   }
#endif /* __RO_TASKING */


	    /*
	     * Decode the user data in the argument vector to get
	     * the defined context set and application context 
	     * name (oid).
	     */
   {
      struct type_ACS_ACSE__apdu *pdu;
      struct type_ACS_AARQ__apdu *aarq;
      struct PSAPcontext prsnt_ctx;
      struct AcSAPindication aci;
      struct assocblk acb;
      int error, i, nelem;


      error = decode_ACS_ACSE__apdu( presentation_start.ps_info[0],
		        		1, NULLIP, NULLVP, &pdu );
      aarq = pdu->un.aarq;	

		/*
		 * Check the application context name received against the
		 * name in the instance.
		 */
      if( oid_cmp( aarq->application__context__name, 
	  str2oid(inst->ISO_Contexts.appl_context_name) ) )
      {
         status = PC_REJECTED;
         reject_reason = __ROx_BIND_BADAPPLCTXNAME ;
      }

      /*
       * If user gave us a buffer for user data, extract the user data,
       * if there is any, and call the user-supplied function handing it
       * the user data.
       */
      if (udata && udata->initiator_user_data && aarq->user__information) {
         memset(&acb,0,sizeof(acb));
         if (apdu2info(&acb,&aci,aarq->user__information,udata->initiator_user_data,&nelem) == NOTOK) {
             free_ACS_ACSE__apdu(pdu);
	     __RO_RETURN_RUNTIME( inst, __ROx_BIND_APDUCONVERSIONFAILED);
         }
         if (udata->callback)
             (*udata->callback)(udata->initiator_user_data,udata->responder_user_data);
      }

      for( i = 0; i < presentation_start.ps_ctxlist.pc_nctx; i++ )
      {
         presentation_start.ps_ctxlist.pc_ctx[i].pc_result = PC_REJECTED;
         prsnt_ctx = presentation_start.ps_ctxlist.pc_ctx[i];

		/*
		 * If asn oid and atn oid are asn_ber save 
		 * the context handle in the instance and
	 	 * accept the context
		 */
         if(!oid_cmp(prsnt_ctx.pc_asn, str2oid("2.2.1.0.1") ))
         {
            if(!oid_cmp(prsnt_ctx.pc_atn,NULLOID) )
            {
               inst->ISO_Contexts.acse_pcid = prsnt_ctx.pc_id;
               presentation_start.ps_ctxlist.pc_ctx[i].pc_result = PC_ACCEPT;
            }
         }
			   

		/*
		 * If asn oid and atn oid are the applications save 
		 * the context handle in the instance and
	 	 * accept the context
		 */
         if(!oid_cmp(prsnt_ctx.pc_asn, 
		      str2oid(inst->ISO_Contexts.prsnt_context_name)))
         {
            if(!oid_cmp(prsnt_ctx.pc_atn, NULLOID) )
            {
               inst->ISO_Contexts.specific_pcid = prsnt_ctx.pc_id;
               presentation_start.ps_ctxlist.pc_ctx[i].pc_result = PC_ACCEPT;
            }
         }
      }
      free_ACS_ACSE__apdu(pdu);
   }


		/*
		 *  Check session requirements and set them to FULL_DUPLEX 
		 */
   session_requirements = 0;
   session_requirements = SR_DUPLEX;

		/* Set up the inital token settings.	
		   Full duplex is all we support. dotokens()
		   is an ISODE routine that calls dotoken. */

#define dotoken(REQUIRES,SHIFT,BIT,TYPE)                                \
        if (session_requirements & REQUIRES)                            \
           initial_token_settings |= ST_INIT_VALUE << SHIFT;
   dotokens();
#undef dotoken

   initial_token_settings = 0;

		/*
		 *  Encode the user data AARE__apdu for the response
		 */
   {
      struct type_ACS_AARE__apdu apdu;
      struct type_ACS_Associate__source__diagnostic diag;
      struct AcSAPindication aci;
      struct assocblk acb;
      int error;
      OID temp_oid;
      int nelem = 1;


      user_data = NULLPE;
      bzero( &apdu, sizeof(apdu) );
      memset(&acb,0,sizeof(acb));

      apdu.application__context__name = temp_oid = 
		oid_cpy(str2oid(inst->ISO_Contexts.appl_context_name));
      apdu.result = ACS_ACCEPT;

      apdu.result__source__diagnostic = &diag;
      apdu.result__source__diagnostic->offset = 
		type_ACS_Associate__source__diagnostic_acse__service__user;	
      apdu.result__source__diagnostic->un.acse__service__user =
		ACS_USER_NULL - ACS_USER_BASE;

      if (udata && udata->responder_user_data)
	  apdu.user__information = info2apdu(&acb,&aci,udata->responder_user_data,nelem);

      error = encode_ACS_AARE__apdu(&user_data, 1, 0, NULLCP, &apdu );
      if (apdu.user__information)
	  free_ACS_Association__information(apdu.user__information);
      user_data->pe_context = INITIATOR_ACSEBER_PCID;
      user_data_length = 1;
      if( error == NOTOK )
         __RO_RETURN_RUNTIME(inst, __ROx_BIND_AAREFAILED );

      oid_free(temp_oid);
   }

	     
	/*
	 *  Respond to the Initiator
	 */
   if( status == PC_ACCEPT )
   {
      if( PConnResponse( presentation_start.ps_sd,
		       PC_ACCEPT,
		       NULLPA,
		       NULLPC,
		       PC_ACCEPT, 
		       0,
		       session_requirements, 
		       SERIAL_NONE,
		       initial_token_settings,
		       &presentation_start.ps_connect,
		       &user_data,
		       user_data_length,
		       &presentation_indication ) == NOTOK )
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_CONNRESPFAILED )
      else
      {
#ifdef __RO_TASKING
         if( __ROr_ISODEwait_for_send(inst) )
            return(EXCEPTION);
#endif
      }
      pe_free(user_data);
      PSFREE(&presentation_start);
      return(SUCCESS);
   }
   else
   {
      if( PConnResponse( presentation_start.ps_sd,
		       PC_REJECTED,
		       NULLPA,
		       &presentation_start.ps_ctxlist,
		       presentation_start.ps_defctxresult, 
		       0,
		       0, 
		       SERIAL_NONE,
		       0,
		       &presentation_start.ps_connect,
		       NULLPEP,
		       0,
		       &presentation_indication ) == NOTOK )
         __RO_RETURN_RUNTIME( inst, __ROx_BIND_CONNRESPFAILED )
      else
      {
#ifdef __RO_TASKING
         if( __ROr_ISODEwait_for_send(inst) )
            return(EXCEPTION);
#endif
      }
      pe_free(user_data);
      PSFREE(&presentation_start);
      __RO_RETURN_RUNTIME( inst, reject_reason );
   }
}
#endif


