www.pudn.com > dsread.zip > READADDR.C
/**************************************************************************** ** File: READADDR.C ** ** Desc: Reads NetWork Address of given object ** ** ** DISCLAIMER ** ** Novell, Inc. makes no representations or warranties with respect to ** any NetWare software, and specifically disclaims any express or ** implied warranties of merchantability, title, or fitness for a ** particular purpose. ** ** Distribution of any NetWare software is forbidden without the ** express written consent of Novell, Inc. Further, Novell reserves ** the right to discontinue distribution of any NetWare software. ** ** Novell is not responsible for lost profits or revenue, loss of use ** of the software, loss of data, costs of re-creating lost data, the ** cost of any substitute equipment or program, or claims by any party ** other than you. Novell strongly recommends a backup be made before ** any software is installed. Technical support for this software ** may be provided at the discretion of Novell. ** ** Programmers: ** ** Ini Who Firm ** --- ----------------- ------------------------------------------------ ** CRG Calvin Gaisford Novell Developer Support. ** ** History: ** ** When Who What ** -------- --- --------------------------------------------------------- ** 10-21-94 CRG First code. */ /*************************************************************************** ** Libraries Linked in for .exe ** ** NWCALLS.LIB ** NWLOCALE.LIB ** NWNET.LIB ** */ /*************************************************************************** ** #defines */ #define FAILURE 0xFF #define SUCCESS 0x00 /**************************************************************************** ** Include headers, macros, etc. */ /*----------------------------------------------------------------------- ** ANSI */ #include#include #include #include /*----------------------------------------------------------------------- ** NetWare */ #include #include #include /*----------------------------------------------------------------------- ** Program Global storage */ struct { NWDSContextHandle context; }gs; extern unsigned _stklen=8000; /*************************************************************************** ** Function Prototypes */ void Proc(void); NWDSCCODE ReadNetAddr(char *objectName); void FatalError(int errorCode); void InitDS(void); int UninitDS(void); /*************************************************************************** ** Main Program */ void main(void) { InitDS(); Proc(); UninitDS(); } /*************************************************************************** ** This will prompt users for a login name. ** It will then pass that name to ReadNetAddr. */ void Proc(void) { NWDSCCODE ccode; char objectName[MAX_DN_CHARS]; printf("Enter Object Name: "); gets(objectName); strcat(objectName,"\x0"); ccode = ReadNetAddr(objectName); if(ccode) { printf("ReadNetAddr failed with error: %X\n",ccode); exit(0); } } /*************************************************************************** ** This reads the attribute "Network Address" for the object passed in. */ NWDSCCODE ReadNetAddr(char *objectName) { NWDSCCODE ccode; NWDS_ITERATION iterHandle=-1L; NWDS_BUFFER *inBuf, *outBuf; NWCOUNT totalAttrs, totalValues, x; NWSYNTAX_ID syntaxID; uint32 attrValSize; char attrname[MAX_DN_CHARS]; void *attrValue; ccode = NWDSAllocBuf(DEFAULT_MESSAGE_LEN, &inBuf); if(ccode <0) { printf("Error NWDSAllocBuf\n"); goto ReadSurnameExit0; } ccode = NWDSAllocBuf(MAX_DN_CHARS, &outBuf); if(ccode <0) { printf("Error NWDSAllocBuf\n"); goto ReadSurnameExit1; } ccode = NWDSInitBuf(gs.context,DSV_READ,inBuf); if(ccode <0) { printf("Error NWDSInitBuf\n"); goto ReadSurnameExit2; } ccode = NWDSPutAttrName(gs.context,inBuf,"Network Address"); /* We only want the */ if(ccode <0) /* attribute surname */ { printf("Error NWDSPutAttrName\n"); goto ReadSurnameExit2; } do { ccode = NWDSRead(gs.context, objectName, /* name of Object to read (IN) */ DS_ATTRIBUTE_VALUES, /* read name and value (IN) */ FALSE, /* only attributes specified (IN) */ inBuf, /* buffer holding "surname" (IN) */ &iterHandle, /* read until all values (OUT) */ outBuf); /* where the values go to(OUT) */ if(ccode < 0) { printf("Error readAclValues:NWDSRead %X\n",ccode); goto ReadSurnameExit2; } ccode = NWDSGetAttrCount(gs.context, outBuf, &totalAttrs); if(ccode < 0) { printf("Error NWDSGetAttrCount\n"); goto ReadSurnameExit2; } if((NWCOUNT)0 == totalAttrs) { goto ReadSurnameExit2; } else { ccode = NWDSGetAttrName(gs.context, outBuf, /* values are in here (IN) */ attrname, /* better be surname (OUT) */ &totalValues, /* number of values (OUT) */ &syntaxID); /* how to read the value (OUT) */ if(syntaxID != SYN_NET_ADDRESS) /* this is the syntax for surname */ { printf("Error NWDSGetAttrName, bad syntax\n"); goto ReadSurnameExit2; } if(ccode <0) { printf("Error NWDSGetAttrName\n"); goto ReadSurnameExit2; } /* This loop is here because objects can contain more than one NetWork address. */ for(x=totalValues;x;x--) { ccode = NWDSComputeAttrValSize(gs.context, /* context handle (IN) */ outBuf, /* buffer with info (IN) */ syntaxID, /* syntax ID (IN) */ &attrValSize); /* size to allocate (OUT) */ if (ccode) { printf("Error NWDSComputeAttrValSize %X",ccode); goto ReadSurnameExit2; } if (NULL == (attrValue = malloc((int)attrValSize)) ) { printf("Error malloc acl\n"); goto ReadSurnameExit2; } ccode = NWDSGetAttrVal(gs.context, /* context handle (IN) */ outBuf, /* buffer with info (IN) */ syntaxID, /* syntax ID (IN) */ attrValue); /* value of attrib (OUT) */ if(ccode <0) { printf("Error NWDSGetAttrVal\n"); goto ReadSurnameExit2; } 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]); free(attrValue); } } } while(iterHandle != NO_MORE_ITERATIONS); ReadSurnameExit2: NWDSFreeBuf(inBuf); ReadSurnameExit1: NWDSFreeBuf(outBuf); ReadSurnameExit0: return (ccode); } /*************************************************************************** ** FatalError is the common exit point for errors. */ void FatalError(int errorCode) { exit(errorCode); } /*************************************************************************** ** Establish context. These are the standard initializations for DS calls */ void InitDS(void) { NWDSCCODE dsCcode; LCONV lconvInfo; dsCcode=NWCallsInit(NULL,NULL); if(dsCcode) /* initialize allowing to call nwcalls functions */ { printf("FatalError during NWCallsInit %X\n",dsCcode); FatalError(FAILURE); } NWLlocaleconv(&lconvInfo); dsCcode = NWInitUnicodeTables(lconvInfo.country_id, lconvInfo.code_page); if(dsCcode) { printf("NWInitUnicodeTables() returned: %X\n", dsCcode); FatalError(FAILURE); } gs.context=NWDSCreateContext(); if(gs.context) { printf("FatalError during NWDSCreateContext %X\n",gs.context); FatalError(FAILURE); } } /*************************************************************************** ** Release context and clean up. */ int UninitDS() { NWDSCCODE dsCcode; dsCcode=NWDSFreeContext(gs.context); if(dsCcode) { printf("FatalError during NWDSFreeContext %X\n",dsCcode); FatalError (FAILURE); } dsCcode=NWFreeUnicodeTables(); if(dsCcode) { printf("Error during NWFreeUnicodeTables()\n"); FatalError (FAILURE); } return(0); }