#module	NICE "SRH X1.0-000"
#pragma builtins

/*
** COPYRIGHT (c) 1992 BY
** DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASSACHUSETTS.
** ALL RIGHTS RESERVED.
**
** THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED
** ONLY  IN  ACCORDANCE  OF  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
** INCLUSION OF THE ABOVE COPYRIGHT NOTICE. THIS SOFTWARE OR  ANY  OTHER
** COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
** OTHER PERSON.  NO TITLE TO AND  OWNERSHIP OF THE  SOFTWARE IS  HEREBY
** TRANSFERRED.
**
** THE INFORMATION IN THIS SOFTWARE IS  SUBJECT TO CHANGE WITHOUT NOTICE
** AND  SHOULD  NOT  BE  CONSTRUED  AS A COMMITMENT BY DIGITAL EQUIPMENT
** CORPORATION.
**
** DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE  OR  RELIABILITY OF ITS
** SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DIGITAL.
*/

/*
**++
**  Facility:
**
**	Examples
**
**  Version: V1.0
**
**  Abstract:
**
**	Example of working with the DECnet NICE protocol.
**
**  Author:
**	Steve Hoffman
**
**  Creation Date:  1-Jan-1990
**
**  Modification History:
**--
*/

/*
**  This procedure chats with the Network Management Layer of DECnet
**  to retrieve node information.  It was intended as a learning tool
**  and test program.  No claims are made for performance or style.
**
**  The NICE protocol is a DECnet/VAX Phase IV/IV+ protocol and is
**  not particularly useful under DECnet/VAX Phase V (DECnet/OSI)
**
**  For the technical details of the NICE protocol, see the DECnet
**  Network Management Functional Specification, order AA-X437A-TK.
*/
#include    iodef
#include    ssdef
#include    stsdef
#include    descrip

#define	NICE$K_NICEVER	    4
#define	NICE$K_DECECO	    0
#define	NICE$K_USERECO	    0

/*
**  Return status codes -- found in first byte.
*/
#define NICE$K_SUCCESS	    ((unsigned char) 1)
#define NICE$K_ACCEPTED	    ((unsigned char) 2)
#define NICE$K_PARTIAL	    ((unsigned char) 3)
#define NICE$K_NOPRIV	    ((unsigned char) -3)
#define NICE$K_UNRECCOMP    ((unsigned char) -8)
#define NICE$K_FILEOPENERR  ((unsigned char) -13)
#define NICE$K_OPFAILED	    ((unsigned char) -25)
#define NICE$K_DONE	    ((unsigned char) -128)
#define NICE$K_NOSTATUS	    ((unsigned short int) ~0)

/*
**  entity type number
*/
#define NICE$K_EN_NODE	    0
#define NICE$K_EN_LINE	    1
#define NICE$K_EN_LOGGING   2
#define NICE$K_EN_CIRCUIT   3
#define NICE$K_EN_MODULE    4
#define NICE$K_EN_AREA	    5

/*
**  Node identification stuff
**  if >0 then it's the start of the ASCIC nodename string.
*/
#define	NICE$K_NI_SIGNIFICANT	((unsigned char) -5)
#define	NICE$K_NI_ADJACENT	((unsigned char) -4)
#define	NICE$K_NI_LOOP		((unsigned char) -3)
#define	NICE$K_NI_ACTIVE	((unsigned char) -2)
#define	NICE$K_NI_KNOWN		((unsigned char) -1)
#define	NICE$K_NI_ADDRESS	((unsigned char) 0)
#define	NICE$M_NI_EXECUTOR	((unsigned char) 1 << 7)


/*
** Change Option bitmasks
*/
#define	NICE$M_CO_PERMANENT     (1 << 7)    /* volative if zero */
#define	NICE$M_CO_CLEARPURGE	(1 << 6)    /* set/define if zero */
#define	NICE$M_CO_ENTITYTYPE    (0x07)	    /* entity type field */

/*
**  NICE operation codes.
*/	    
#define NICE$K_OP_WRITE	19
#define NICE$K_OP_READ	20

#define RUNT$S_BUFFER	512
#define	RUNT$S_NCBDATA	17
#define	RUNT$S_CHANNEL	2
main()
    {
    unsigned char *src_node = "VOX";
    unsigned char *trg_node = "XDELTA";
    unsigned char *nodename = "XDELTA";
    unsigned char *nice = calloc( 1, RUNT$S_BUFFER );
    unsigned char *ncb	= calloc( 1, RUNT$S_BUFFER );
    unsigned char *index;
    unsigned long int node16;
    unsigned long int area6;
    unsigned long int number10;
    unsigned long int retstat;
    struct dsc$descriptor dncb =
	{
	0,
	DSC$K_DTYPE_T, DSC$K_CLASS_S,
	ncb
	};
    unsigned short int src_chan;
    unsigned short int trg_chan;
    unsigned short int iosb[4];

    /*
    **	Build the NCB
    */
    strcpy( ncb, src_node );
    strcat( ncb, "::\"NML=/" );

    dncb.dsc$w_length = strlen( ncb );
    ncb[dncb.dsc$w_length++] = 0;
    ncb[dncb.dsc$w_length++] = 0;

    ncb[dncb.dsc$w_length++] = 3;
    ncb[dncb.dsc$w_length++] = NICE$K_NICEVER;
    ncb[dncb.dsc$w_length++] = NICE$K_DECECO;
    ncb[dncb.dsc$w_length++] = NICE$K_USERECO;
    dncb.dsc$w_length = strlen( ncb ) + RUNT$S_CHANNEL + RUNT$S_NCBDATA;
    ncb[dncb.dsc$w_length++] = '\"';

    /*
    **	Open a channel to the network...
    */
    retstat = SYS$ASSIGN( &dncb, &src_chan, 0, 0 );
    if ( !$VMS_STATUS_SUCCESS( retstat ))
	return( retstat );

    /*
    **	Build a NICE "show node" request
    */
    dncb.dsc$w_length = 0;
    ncb[dncb.dsc$w_length++] = NICE$K_OP_READ;
    ncb[dncb.dsc$w_length++] = 0x0;
    ncb[dncb.dsc$w_length++] = strlen( nodename );
    strcpy( &(ncb[dncb.dsc$w_length]), nodename );
    dncb.dsc$w_length += strlen( nodename );

    /*
    **	Write the NCB message and read the response.  (We are
    **	rather cavalier about the $QIOW and the IOSB status.)
    */
    retstat = SYS$QIOW( 0, src_chan, IO$_WRITEVBLK, iosb, 0, 0,
	ncb, dncb.dsc$w_length, 0, 0, 0, 0 );

    retstat = SYS$QIOW( 0, src_chan, IO$_READVBLK, iosb, 0, 0,
	ncb, RUNT$S_BUFFER, 0, 0, 0, 0 );

    if ( *ncb != NICE$K_ACCEPTED )
	    return( SS$_ABORT );

    retstat = SYS$QIOW( 0, src_chan, IO$_READVBLK, iosb, 0, 0,
	ncb, RUNT$S_BUFFER, 0, 0, 0, 0 );

    /*
    **	Did we get it back?
    */
    if ( *ncb != NICE$K_SUCCESS )
	    return( SS$_ABORT );

    /*
    **	Is the status reasonable?
    */
    if ( *(unsigned short int *) (ncb + 1 ) != NICE$K_NOSTATUS )
	return( SS$_ABORT );

    /*
    **	Now that we've got the reply, parse it.
    */
    if ( *(unsigned char *)(ncb + 3 ) == NICE$K_NI_ADDRESS )
	{
	node16 = *(short*)(ncb + 4);
	area6 = node16 >> 10;
	number10 = node16 & 0x03ff;
	printf("Node _%s:: is at address %d.%d.\n",
	    nodename, area6, number10 );
	}
    else
	return( SS$_ABORT );

    if ( *(unsigned char *)(ncb + 6 ) & NICE$M_NI_EXECUTOR )
	printf("(executor)\n" );

    retstat = SYS$QIOW( 0, src_chan, IO$_READVBLK, iosb, 0, 0,
	ncb, RUNT$S_BUFFER, 0, 0, 0, 0 );

    if ( *ncb != NICE$K_DONE )
	    return( SS$_ABORT );

    /*
    **	All done... Clean up and exit...
    */
    retstat = SYS$DASSGN( src_chan );

    return( SS$_NORMAL );

    }

/*
** **************************************************************************
**
**  To get the sort of output included below, define the logical name
**  NML$LOG to be some hex value -- such as "FF" -- before calling NCP.
**
**	Network Management Listener (NML) logging:
** 
**         Bit 0 = all NICE messages.
**         Bit 1 = all permanent data base I/O.
**         Bit 2 = endparse state transitions.
**         Bit 3 = loopback I/O.
**         Bit 4 = ACP I/O
**         Bit 5 = MOP I/o
**         Bit 6 = Service operations.
**         Bit 7 = logs things related to event processing.
**
**         Bits 8-15 = reserved (no purpose yet).
** 
**         Bits 16-21 dump an entire permanent data base:
**
**         Bit 16 = dump node data base.
**         Bit 17 = dump line data base.
**         Bit 18 = dump logging data base.
**         Bit 19 = dump object data base.
**         Bit 20 = dump circuit data base.
**         Bit 21 = dump module data base.
** 
** **************************************************************************
** 
**  The following is the result of an NCP "SET NODE 5.775 NAME XDELTA"
**  command...  As seen from the perspective of NML.  
** 
** NICE message received by NML (length = 14 bytes)
** 00004154 4C454458 0601F417 07008013 	.....ô..XDELTA..
** 
** 				 13	write
**                                80	volatile (item class node)
**                              00		node address code
**                         17 07		the address
**                     01F4	        data ID (500, nodename)
**                   06			ASCIC nodename count
**      4154 4C454458		        "XDELTA"
** 0000					trash
** 
** NICE message transmitted by NML (length = 4 bytes)
** 00000000 00000000 00000000 00FFFF02 	................
** 
**                                  02 	accepted
**                              FFFF	no error deatil
** 00000000 00000000 00000000 00		trash
** 
** NICE message transmitted by NML (length = 4 bytes)
** 00000000 00000000 00000000 00FFFF01 	................
** 
** 				 01 	success
** 			     FFFF	no error detail
** 00000000 00000000 00000000 00		trash
** 
** NICE message transmitted by NML (length = 1 bytes)
** 00000000 00000000 00000000 00000080     ................
** 
**                                  80	all done
** 00000000 00000000 00000000 000000	trash
** 
** **************************************************************************
** 
**   The following is the (partial) result of an NCP "SHOW KNOW NODE"
**   command...  Again, as seen from the perspective of NML.
** 
** 
** NICE message received  (length = 3 bytes)
** 00000000 00000000 00000000 00FF0014 	................
** 				 14	show
**                                00	show nodes
**                              FF		show known nodes
** 00000000 00000000 00000000 00
** 
** NICE message transmitted  (length = 4 bytes)
** 00000000 00000000 00000000 00FFFF02 	................
**                                  02	command accepted
**                              FFFF	status
** 00000000 00000000 00000000 00		extra
** 
** NICE message transmitted  (length = 48 bytes)
** 00640081 0000584F 56831445 00FFFF01 	....E..VOX....d.
** 2E355620 5841562D 74656E43 45441E40 	@.DECnet-VAX V5.
** 2032542D 312E3556 20534D56 20202C30 	0,  VMS V5.1-T2 
** 
**                                  01	partial response
**                              FFFF	status
**                            00		address code
**                       1445		address value
** 		    83		        count - bit seven means executor
**              584F 56                    node name
** 00640081 0000                           baggage
** 2E355620 5841562D 74656E43 45441E40     baggage
** 2032542D 312E3556 20534D56 20202C30	baggage
** 
** NICE message transmitted  (length = 34 bytes)
** 40033631 3054454E 45060401 00FFFF01 	.......ENET016.@
** 414C0440 140F02C2 033E302D 414E4205 	.BNA-0>.Â...@.LA
** 00000000 00000000 00000000 00003552 	R5..............
** 
**                                  01	partial
**                              FFFF	status
**                            00		node address code
**                       0401		node address value
**       31 3054454E 4506                  ASCIC nodename
** 400336					baggage
** 414C0440 140F02C2 033E302D 414E4205 	more baggage
** 00000000 00000000 00000000 00003552 	yet more baggage
** 
** NICE message transmitted  (length = 34 bytes)
** 40033632 3054454E 45060402 00FFFF01 	.......ENET026.@
** 414C0440 140F02C2 033E302D 414E4205 	.BNA-0>.Â...@.LA
** 00000000 00000000 00000000 00003552 	R5..............
** 
** ....
** 
** NICE message transmitted  (length = 1 bytes)
** 00000000 00000000 00000000 00000080 	................
** 
**                                  80	all done...
** 
** 
** Known Node Volatile Summary as of  1-DEC-1988 00:00:31
**  
** Executor node = 5.69 (VOX)
**  
** State                    = on
** Identification           = DECnet-VAX V5.0,  VMS V5.1-T2
** 
**  
**     Node           State      Active  Delay   Circuit     Next node
**                               Links
**  
**  1.1 (ENET01)                                 BNA-0         5.15 (LAR5)
**  1.2 (ENET02)                                 BNA-0         5.15 (LAR5)
** 
** 	ad naseum...
** 
** 
** **************************************************************************
** 
**  The result of a "PURGE NODE ENET01 ALL" command...
** 
** NICE message received  (length = 9 bytes)
** 00000000 00000031 3054454E 4506C013 	.À.ENET01.......
** 				    13 	
**                                C0	clear/purge permanent
**                31 3054454E 4506	ascic nodename
** 00000000 000000
** 
** NICE message transmitted  (length = 4 bytes)
** 00000000 00000000 00000000 00FFFF02 	................
** 
** NICE message transmitted  (length = 52 bytes)
** 45444345 522D492D 4C4D4E25 27FFFF01 	...'%NML-I-RECDE
** 6E652065 73616261 74614420 2C54454C 	LET, Database en
** 4E450604 01646574 656C6564 20797274 	try deleted...EN
** 00000000 00000000 00000000 31305445 	ET01............
** 
** NICE message transmitted  (length = 1 bytes)
** 00000000 00000000 00000000 00000080 	................
** 
** **************************************************************************
** 
**   "DEFINE NODE 1.1 NAME ENET01"
** 
** NICE message received  (length = 14 bytes)
** 00003130 54454E45 0601F404 01008013 	.....ô..ENET01..
** 
** NICE message transmitted  (length = 4 bytes)
** 00000000 00000000 00000000 00FFFF02 	................
** 
** NICE message transmitted  (length = 4 bytes)
** 00000000 00000000 00000000 00FFFF01 	................
** 
** NICE message transmitted  (length = 1 bytes)
** 00000000 00000000 00000000 00000080 	................
** 
** **************************************************************************
** 
** "PURGE NODE 1.1 ALL"
** 
** NICE message received  (length = 5 bytes)
** 00000000 00000000 00000004 0100C013 	.À..............
** 
** NICE message transmitted  (length = 4 bytes)
** 00000000 00000000 00000000 00FFFF02 	................
** 
** NICE message transmitted  (length = 52 bytes)
** 45444345 522D492D 4C4D4E25 27FFFF01 	...'%NML-I-RECDE
** 6E652065 73616261 74614420 2C54454C 	LET, Database en
** 4E450604 01646574 656C6564 20797274 	try deleted...EN
** 00000000 00000000 00000000 31305445 	ET01............
** 
** NICE message transmitted  (length = 1 bytes)
** 00000000 00000000 00000000 00000080 	................
** 
** **************************************************************************
** 
** "PURGE NODE ENET01 ALL"
** 
** NICE message received  (length = 9 bytes)
** 00000000 00000031 3054454E 4506C013 	.À.ENET01.......
** 
** NICE message transmitted  (length = 4 bytes)
** 00000000 00000000 00000000 00FFFF02 	................
** 
** NICE message transmitted  (length = 4 bytes)
** 00000000 00000000 00000000 000000F8 	ø...............
** 
**                                  F8 	Unrecognized component, Node
**                            000000
** 
** NICE message transmitted  (length = 1 bytes)
** 00000000 00000000 00000000 00000080 	................
** 
** **************************************************************************
** 
** "DEFINE NODE 1.1 NAME ENET01"
** 
** NICE message received  (length = 14 bytes)
** 00003130 54454E45 0601F404 01008013 	.....ô..ENET01..
** 
** NICE message transmitted  (length = 4 bytes)
** 00000000 00000000 00000000 00FFFF02 	................
** 
** NICE message transmitted  (length = 4 bytes)
** 00000000 00000000 00000000 00FFFF01 	................
** 
** NICE message transmitted  (length = 1 bytes)
** 00000000 00000000 00000000 00000080 	................
** 
** **************************************************************************
*/
