public LDAPControl(Berval berVal, string oid, char isCritical) { ldapControl.oid = oid; ldapControl.ldctl_iscritical = isCritical; ldapControl.ptrberVal = IntPtr.Zero; // ldapControl.berVal = berVal; ldapControl.ptrberVal = Marshal.AllocHGlobal(Marshal.SizeOf(berVal)); Marshal.StructureToPtr(berVal, ldapControl.ptrberVal, false); }
public LdapValue[] GetBervals(IntPtr ldapEntry, string attrName) { Logger.Log(string.Format("Calling GetBervals(ldapEntry{0},attrName{1})", ldapEntry, attrName), Logger.ldapTracingLevel); Monitor.Enter(Ldap_ApiCallSync); try { unsafe { if (_ldapHandle != null && _ldapHandle.Handle != IntPtr.Zero) { IntPtr ppBervals = LdapAPI.ldap_get_values_len(_ldapHandle.Handle, ldapEntry, attrName); if (IntPtr.Zero.Equals(ppBervals)) { return(null); } int berCount = LdapAPI.ldap_count_values_len(LdapAPI.ldap_get_values_len(_ldapHandle.Handle, ldapEntry, attrName)); Logger.Log(String.Format("Result of ldap_get_values_len(berCount)", berCount), Logger.ldapTracingLevel); if (berCount == 0) { return(null); } LdapValue[] ldapValues = new LdapValue[berCount]; for (int i = 0; i < berCount; i++) { IntPtr pBerval = Marshal.ReadIntPtr(ppBervals, i * IntPtr.Size); if (IntPtr.Zero.Equals(pBerval)) { ldapValues[i] = null; } else { Berval tmpBer = (Berval)Marshal.PtrToStructure(pBerval, typeof(Berval)); if (tmpBer.bv_len == 0) { ldapValues[i] = null; } else { byte[] tmpBytes = new byte[tmpBer.bv_len]; for (int j = 0; j < (int)tmpBer.bv_len; j++) { tmpBytes[j] = Marshal.ReadByte(tmpBer.bv_val, j); } ldapValues[i] = new LdapValue(ADSType.ADSTYPE_DN_STRING, tmpBytes); ldapValues[i].stringData = BitConverter.ToString(tmpBytes); } } } return(ldapValues); } else { return(null); } } } catch (Exception ex) { Logger.LogException("LdapMessage.GetAttributeNames", ex); return(null); } finally { Monitor.Exit(Ldap_ApiCallSync); } }
//Function to query the object list based on paging public int PagedSearchSynchronous( string ObjectDN, LdapAPI.LDAPSCOPE scope, string Query, string[] AttributeList, out List<LdapEntry> ldapEntries) { ldapEntries = new List<LdapEntry>(); //Set up LDAP_OPT_SERVER_CONTROLS option so that server can respond with message that contains page control information LdapTimeVal timeout = new LdapTimeVal(600, 0); IntPtr page_cookiePtr = IntPtr.Zero; Berval cookie = new Berval(0, IntPtr.Zero); IntPtr[] serverCtrls = new IntPtr[2]; IntPtr pagedControlPtr = IntPtr.Zero; IntPtr serverLdapControl = IntPtr.Zero; int errcodep = 0; ulong pageCount = 0; int PageSize = 1000; int ret = -1; //int ret = Setserver_PageControloption(); Logger.Log("Before start using paging, enable paging control return " + ret); LdapMessage ldapMessage; do { serverCtrls = new IntPtr[2]; if (PageSize > 0) { //Create page control, the initial page control is created using a cookie with Berval of {0,null} //if the page size is set to be larger than the default page size in server, //the actual page size will still be the default page size set in server. int pagedRet = Ldap_Create_Page_Control((int)PageSize, page_cookiePtr, 'T', out pagedControlPtr); Logger.Log("CreatePageControl return is " + pagedRet); if (pagedControlPtr == IntPtr.Zero) { Logger.Log("The created pagedcontrol is null"); } serverCtrls[0] = pagedControlPtr; serverCtrls[1] = IntPtr.Zero; ret = SearchExtSynchronous( ObjectDN, scope, Query, AttributeList, 0, serverCtrls, null, timeout, 0, out ldapMessage); if (ldapMessage == null) { return ret; } if (serverLdapControl != IntPtr.Zero) { LdapAPI.ldap_control_free(serverLdapControl); serverLdapControl = IntPtr.Zero; ; } ret = Ldap_Parse_Result(ldapMessage.MessagePtr, out errcodep, out serverLdapControl); if (serverLdapControl == IntPtr.Zero) { Logger.Log(" LdapApi.ldap_parse_result" + ret.ToString()); return ret; } if (page_cookiePtr != IntPtr.Zero) { LdapAPI.ber_free(page_cookiePtr, 0); page_cookiePtr = IntPtr.Zero; } ret = Ldap_Parse_Page_Control(serverLdapControl, out pageCount, out page_cookiePtr); if (ret != 0) { Logger.Log("ParsePageControl" + ret.ToString()); return ret; } if (serverLdapControl != IntPtr.Zero) { LdapAPI.ldap_controls_free(serverLdapControl); serverLdapControl = IntPtr.Zero; } if (pagedControlPtr != IntPtr.Zero) { LdapAPI.ldap_control_free(pagedControlPtr); pagedControlPtr = IntPtr.Zero; } } else { ret = SearchExtSynchronous( ObjectDN, scope, Query, AttributeList, 0, null, null, timeout, 0, out ldapMessage); if (ret != 0) { return ret; } } //process the entries returned in the current page (in ldapMessage) List<LdapEntry> onePageldapEntries = (ldapMessage != null ? ldapMessage.Ldap_Get_Entries() : null); if (onePageldapEntries != null) { foreach (LdapEntry entry in onePageldapEntries) { ldapEntries.Add(entry); } } if (page_cookiePtr != IntPtr.Zero) { cookie = (Berval)Marshal.PtrToStructure(page_cookiePtr, typeof(Berval)); Logger.Log("cookie.bv_len is " + cookie.bv_len + "cookie.bv_val is " + cookie.bv_val); } } while (PageSize > 0 && page_cookiePtr != IntPtr.Zero && cookie.bv_val != IntPtr.Zero && cookie.bv_len > 0); ServerCtrls[0] = pagedControlPtr; ServerCtrls[1] = IntPtr.Zero; return ret; }
public int setserver_pageControloption() { //set server control LDAP_OPT_SERVER_CONTROLS Berval berVal = new Berval(0, IntPtr.Zero); LDAPControl c = new LDAPControl(berVal, LdapMessage.LDAP_CONTROL_PAGEDRESULTS, 'F'); IntPtr[] ctrls = new IntPtr[2]; ctrls[0] = c.ConvertToUM(); ctrls[1] = IntPtr.Zero; IntPtr arrayCtrls = IntPtr.Zero; // Allocate memory to store the array of pointers int sizeOfIntPtr = Marshal.SizeOf(typeof(IntPtr)); arrayCtrls = Marshal.AllocCoTaskMem(sizeOfIntPtr * ctrls.Length); // Stuff the pointers into the array for (int counter = 0; counter < ctrls.Length; counter++) Marshal.WriteIntPtr(arrayCtrls, counter * sizeOfIntPtr, ctrls[counter]); int ret = Ldap_Set_Option(LDAP_OPT_SERVER_CONTROLS, arrayCtrls); return ret; }
/// <summary> /// Return a pointer to BerElement /// </summary> /// <param name="berVal">The BerVal</param> /// <returns>The BerElement to return</returns> public IntPtr Ber_Init(Berval berVal) { IntPtr ptrberVal = IntPtr.Zero; ptrberVal = Marshal.AllocHGlobal(Marshal.SizeOf(berVal)); Marshal.StructureToPtr(berVal, ptrberVal, false); return LdapAPI.ber_init(out ptrberVal); }