home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / dsread.exe / READADDR.C < prev    next >
C/C++ Source or Header  |  1995-04-12  |  9KB  |  323 lines

  1. /****************************************************************************
  2. **    File:    READADDR.C
  3. **
  4. **    Desc:    Reads NetWork Address of given object
  5. **
  6. **
  7. **        DISCLAIMER
  8. **
  9. **    Novell, Inc. makes no representations or warranties with respect to
  10. **    any NetWare software, and specifically disclaims any express or
  11. **    implied warranties of merchantability, title, or fitness for a
  12. **    particular purpose.
  13. **
  14. **    Distribution of any NetWare software is forbidden without the
  15. **    express written consent of Novell, Inc.  Further, Novell reserves
  16. **    the right to discontinue distribution of any NetWare software.
  17. **
  18. **    Novell is not responsible for lost profits or revenue, loss of use
  19. **    of the software, loss of data, costs of re-creating lost data, the
  20. **    cost of any substitute equipment or program, or claims by any party
  21. **    other than you.  Novell strongly recommends a backup be made before
  22. **    any software is installed.   Technical support for this software
  23. **    may be provided at the discretion of Novell.
  24. **
  25. **    Programmers:
  26. **
  27. **        Ini    Who                                Firm
  28. **        --- ----------------- ------------------------------------------------
  29. **        CRG    Calvin Gaisford        Novell Developer Support.
  30. **
  31. **    History:
  32. **
  33. **        When         Who What
  34. **        -------- --- ---------------------------------------------------------
  35. **        10-21-94 CRG First code.
  36. */
  37.  
  38.  
  39.  
  40. /***************************************************************************
  41. **  Libraries Linked in for .exe
  42. **
  43. **  NWCALLS.LIB
  44. **  NWLOCALE.LIB
  45. **  NWNET.LIB
  46. **
  47. */
  48.  
  49.  
  50. /***************************************************************************
  51. **  #defines
  52. */
  53. #define FAILURE  0xFF
  54. #define SUCCESS  0x00
  55.  
  56.  
  57. /****************************************************************************
  58. **    Include headers, macros, etc.
  59. */
  60.  
  61.     /*-----------------------------------------------------------------------
  62.     **    ANSI
  63.     */
  64.     #include <stdio.h>
  65.     #include <stdlib.h>
  66.     #include <string.h>
  67.   #include <conio.h>
  68.  
  69.  
  70.     /*-----------------------------------------------------------------------
  71.     **    NetWare
  72.     */
  73.     #include <nwnet.h>
  74.     #include <nwcalls.h>
  75.     #include <nwlocale.h>
  76.  
  77.     /*-----------------------------------------------------------------------
  78.     **    Program Global storage
  79.     */
  80.     struct
  81.     {
  82.         NWDSContextHandle context;
  83.     }gs;
  84.  
  85.     extern unsigned _stklen=8000;
  86.  
  87.  
  88. /***************************************************************************
  89. **    Function Prototypes
  90. */
  91. void Proc(void);
  92. NWDSCCODE ReadNetAddr(char *objectName);
  93. void FatalError(int errorCode);
  94. void InitDS(void);
  95. int  UninitDS(void);
  96.  
  97.  
  98. /***************************************************************************
  99. **    Main Program
  100. */
  101. void main(void)
  102. {
  103.     InitDS();
  104.     Proc();
  105.     UninitDS();
  106. }
  107.  
  108.  
  109. /***************************************************************************
  110. **    This will prompt users for a login name.
  111. **  It will then pass that name to ReadNetAddr.
  112. */
  113. void Proc(void)
  114. {
  115.     NWDSCCODE            ccode;
  116.     char                    objectName[MAX_DN_CHARS];
  117.  
  118.     printf("Enter Object Name: ");
  119.     gets(objectName);
  120.     strcat(objectName,"\x0");
  121.  
  122.     ccode = ReadNetAddr(objectName);
  123.     if(ccode)
  124.     {
  125.         printf("ReadNetAddr failed with error: %X\n",ccode);
  126.         exit(0);
  127.     }
  128. }
  129.  
  130.  
  131. /***************************************************************************
  132. **    This reads the attribute "Network Address" for the object passed in.
  133. */
  134. NWDSCCODE ReadNetAddr(char *objectName)
  135. {
  136.     NWDSCCODE                    ccode;
  137.     NWDS_ITERATION         iterHandle=-1L;
  138.     NWDS_BUFFER                *inBuf, *outBuf;
  139.     NWCOUNT                        totalAttrs, totalValues, x;
  140.     NWSYNTAX_ID                syntaxID;
  141.     uint32                        attrValSize;
  142.     char                            attrname[MAX_DN_CHARS];
  143.     void                            *attrValue;
  144.  
  145.     ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &inBuf);
  146.     if(ccode <0)
  147.     {
  148.         printf("Error NWDSAllocBuf\n");
  149.         goto ReadSurnameExit0;
  150.     }
  151.  
  152.     ccode = NWDSAllocBuf(MAX_DN_CHARS, &outBuf);
  153.     if(ccode <0)
  154.     {
  155.         printf("Error NWDSAllocBuf\n");
  156.         goto ReadSurnameExit1;
  157.     }
  158.  
  159.     ccode = NWDSInitBuf(gs.context,DSV_READ,inBuf);
  160.     if(ccode <0)
  161.     {
  162.         printf("Error NWDSInitBuf\n");
  163.         goto ReadSurnameExit2;
  164.     }
  165.  
  166.     ccode = NWDSPutAttrName(gs.context,inBuf,"Network Address");      /* We only want the  */
  167.     if(ccode <0)                                               /* attribute surname */
  168.     {
  169.         printf("Error NWDSPutAttrName\n");
  170.         goto ReadSurnameExit2;
  171.     }
  172.     do
  173.     {
  174.         ccode = NWDSRead(gs.context,
  175.                     objectName,           /* name of Object to read (IN)    */
  176.                     DS_ATTRIBUTE_VALUES,    /* read name and value (IN)       */
  177.                     FALSE,                                /* only attributes specified (IN) */
  178.                     inBuf,                                /* buffer holding "surname" (IN)      */
  179.                     &iterHandle,                    /* read until all values (OUT)    */
  180.                     outBuf);                            /* where the values go to(OUT)    */
  181.         if(ccode < 0)
  182.         {
  183.             printf("Error readAclValues:NWDSRead %X\n",ccode);
  184.             goto ReadSurnameExit2;
  185.         }
  186.         ccode = NWDSGetAttrCount(gs.context, outBuf, &totalAttrs);
  187.         if(ccode < 0)
  188.         {
  189.             printf("Error NWDSGetAttrCount\n");
  190.             goto ReadSurnameExit2;
  191.         }
  192.         if((NWCOUNT)0 == totalAttrs)
  193.         {
  194.             goto ReadSurnameExit2;
  195.         }
  196.         else
  197.         {
  198.             ccode = NWDSGetAttrName(gs.context,
  199.                                             outBuf,                        /* values are in here (IN)     */
  200.                                             attrname,                    /* better be surname (OUT)         */
  201.                                             &totalValues,     /* number of values (OUT)      */
  202.                                             &syntaxID);                /* how to read the value (OUT) */
  203.  
  204.             if(syntaxID != SYN_NET_ADDRESS)    /* this is the syntax for surname */
  205.             {
  206.                 printf("Error NWDSGetAttrName, bad syntax\n");
  207.                 goto ReadSurnameExit2;
  208.             }
  209.             if(ccode <0)
  210.             {
  211.                 printf("Error NWDSGetAttrName\n");
  212.                 goto ReadSurnameExit2;
  213.             }
  214.  
  215.             /*        This loop is here because objects can contain more than one
  216.                         NetWork address. */
  217.  
  218.             for(x=totalValues;x;x--)
  219.             {
  220.                 ccode = NWDSComputeAttrValSize(gs.context,  /* context handle (IN)    */
  221.                                             outBuf,                    /* buffer with info (IN)  */
  222.                                             syntaxID,                  /* syntax ID (IN)         */
  223.                                             &attrValSize);             /* size to allocate (OUT) */
  224.                 if (ccode)
  225.                 {
  226.                     printf("Error NWDSComputeAttrValSize %X",ccode);
  227.                     goto ReadSurnameExit2;
  228.                 }
  229.  
  230.                 if (NULL == (attrValue = malloc((int)attrValSize)) )
  231.                 {
  232.                     printf("Error malloc acl\n");
  233.                     goto ReadSurnameExit2;
  234.                 }
  235.                 ccode = NWDSGetAttrVal(gs.context,                    /* context handle (IN)   */
  236.                                             outBuf,                   /* buffer with info (IN) */
  237.                                             syntaxID,                 /* syntax ID (IN)        */
  238.                                             attrValue);              /* value of attrib (OUT) */
  239.                 if(ccode <0)
  240.                 {
  241.                     printf("Error NWDSGetAttrVal\n");
  242.                     goto ReadSurnameExit2;
  243.                 }
  244.  
  245.                 printf("Address Type: %lu\nAddress Length: %lu\nAddress :%02X%02X%02X%02X:%02X%02X%02X%02X%02X%02X\n", ((Net_Address_T *)attrValue)->addressType, ((Net_Address_T *)attrValue)->addressLength, ((Net_Address_T *)attrValue)->address[0],((Net_Address_T *)attrValue)->address[1],((Net_Address_T *)attrValue)->address[2],((Net_Address_T *)attrValue)->address[3],((Net_Address_T *)attrValue)->address[4],((Net_Address_T *)attrValue)->address[5],((Net_Address_T *)attrValue)->address[6],((Net_Address_T *)attrValue)->address[7],((Net_Address_T *)attrValue)->address[8],((Net_Address_T *)attrValue)->address[9]);
  246.  
  247.                 free(attrValue);
  248.             }
  249.         }
  250.     }
  251.     while(iterHandle != NO_MORE_ITERATIONS);
  252.  
  253. ReadSurnameExit2:
  254.     NWDSFreeBuf(inBuf);
  255. ReadSurnameExit1:
  256.     NWDSFreeBuf(outBuf);
  257. ReadSurnameExit0:
  258.     return (ccode);
  259. }
  260.  
  261.  
  262. /***************************************************************************
  263. **    FatalError is the common exit point for errors.
  264. */
  265. void FatalError(int errorCode)
  266. {
  267.         exit(errorCode);
  268. }
  269.  
  270.  
  271. /***************************************************************************
  272. **    Establish context.  These are the standard initializations for DS calls
  273. */
  274. void InitDS(void)
  275. {
  276.     NWDSCCODE dsCcode;
  277.     LCONV            lconvInfo;
  278.  
  279.     dsCcode=NWCallsInit(NULL,NULL);
  280.     if(dsCcode)          /* initialize allowing to call nwcalls functions */
  281.     {
  282.         printf("FatalError during NWCallsInit %X\n",dsCcode);
  283.         FatalError(FAILURE);
  284.     }
  285.     NWLlocaleconv(&lconvInfo);
  286.     dsCcode = NWInitUnicodeTables(lconvInfo.country_id, lconvInfo.code_page);
  287.     if(dsCcode)
  288.     {
  289.         printf("NWInitUnicodeTables() returned: %X\n", dsCcode);
  290.         FatalError(FAILURE);
  291.     }
  292.     gs.context=NWDSCreateContext();
  293.     if(gs.context)
  294.     {
  295.         printf("FatalError during NWDSCreateContext %X\n",gs.context);
  296.         FatalError(FAILURE);
  297.     }
  298. }
  299.  
  300.  
  301. /***************************************************************************
  302. **    Release context and clean up.
  303. */
  304. int UninitDS()
  305. {
  306.     NWDSCCODE dsCcode;
  307.  
  308.     dsCcode=NWDSFreeContext(gs.context);
  309.     if(dsCcode)
  310.     {
  311.         printf("FatalError during NWDSFreeContext %X\n",dsCcode);
  312.         FatalError (FAILURE);
  313.     }
  314.     dsCcode=NWFreeUnicodeTables();
  315.     if(dsCcode)
  316.     {
  317.         printf("Error during NWFreeUnicodeTables()\n");
  318.         FatalError (FAILURE);
  319.     }
  320.     return(0);
  321. }
  322.  
  323.