//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; }
/// <summary> /// SearchExtSynchronous /// </summary> /// <param name="basedn"></param> /// <param name="scope"></param> /// <param name="filter"></param> /// <param name="search_attrs"></param> /// <param name="attrsonly"></param> /// <param name="ServerControls"></param> /// <param name="ClientControls"></param> /// <param name="timeout"></param> /// <param name="SizeLimit"></param> /// <param name="ret"> Return value to show up the correct pop up to the user while querying</param> /// <returns>LdapMessage</returns> public int SearchExtSynchronous( string basedn, LdapAPI.LDAPSCOPE scope, string filter, string[] search_attrs, int attrsonly, IntPtr[] ServerControls, IntPtr[] ClientControls, LdapTimeVal timeout, int SizeLimit, out LdapMessage message ) { message = null; int ret = 0; lock (lockThis_searchExtsyn) { ret = _ldapHandle.Ldap_Search_Ext_S( basedn, (int)scope, filter, search_attrs, attrsonly, ServerControls, ClientControls, timeout, SizeLimit, out message); } ret = handleSearchError("SearchExtSynchronous", basedn, ret); return ret; }
public int Ldap_Search_Ext( IntPtr ld, string basedn, int scope, string filter, string[] attrs, int attrsonly, LDAPControl[] ServerControls, LDAPControl[] ClientControls, int TimeLimit, int SizeLimit, out LdapMessage ldapMessage ) { int ret = -1; ldapMessage = null; IntPtr umldapMessage = new IntPtr(); LdapTimeVal tm = new LdapTimeVal(5, 6); Monitor.Enter(Ldap_ApiCallSync); try { Logger.Log(String.Format("Calling Ldap_Search_Ext(basedn={0}, scope={1}, filter={2})", basedn, scope, filter), Logger.ldapLogLevel); unsafe { ret = Ldap_Rebind_S(); if (ret != 0) { Logger.Log(ErrorCodes.LDAPString(ret), Logger.ldapLogLevel); ldapMessage = null; return ret; } ret = LdapAPI.ldap_search_s(_ld, basedn, scope, filter, attrs, attrsonly, out umldapMessage); if (ret != 0) { Logger.Log(ErrorCodes.LDAPString(ret), Logger.ldapLogLevel); ldapMessage = null; return ret; } if (umldapMessage == IntPtr.Zero) { ldapMessage = null; return ret; } ldapMessage = new LdapMessage(this, umldapMessage); return ret; } } catch (Exception ex) { Logger.LogException("LdapHandle.Ldap_Search_Ext", ex); return ret; } finally { Monitor.Exit(Ldap_ApiCallSync); } }
public int Ldap_Search_Ext_S( string basedn, int scope, string filter, string[] attrs, int attrsonly, //LDAPControl[] ServerControls, IntPtr[] ServerControls, IntPtr[] ClientControls, LdapTimeVal timeout, int SizeLimit, out LdapMessage ldapMessage ) { IntPtr umldapMessage = new IntPtr(); // IntPtr[] umServerControls = null; //IntPtr umClientControls = IntPtr.Zero; IntPtr umtimeout = IntPtr.Zero; /* if (ServerControls != null && ServerControls.Length > 0) { umServerControls = new IntPtr[ServerControls.Length]; for (int i = 0; i < ServerControls.Length; i++) { umServerControls[i] = ServerControls[i].ConvertToUM(); } }*/ //if (ClientControls != null && ClientControls.Length > 0) //{ // umClientControls = new IntPtr[ClientControls.Length]; // for (int i = 0; i < ClientControls.Length; i++) // { // umClientControls[i] = ClientControls[i].ConvertToUM(); // } //} if (timeout != null) umtimeout = timeout.ConvertToUM(); int ret = -1; ldapMessage = null; Monitor.Enter(Ldap_ApiCallSync); try { Logger.Log(String.Format("Calling Ldap_Search_Ext_S(basedn={0}, scope={1}, filter={2})", basedn, scope, filter), Logger.ldapLogLevel); /* int ret = LdapApi.ldap_search_ext_s( _ld, basedn, scope, filter, attrs, attrsonly, umServerControls, umClientControls, umtimeout, SizeLimit, out umldapMessage);*/ unsafe { ret = Ldap_Rebind_S(); if (ret != 0) { Logger.Log(ErrorCodes.LDAPString(ret), Logger.ldapLogLevel); ldapMessage = null; return ret; } ret = LdapAPI.ldap_search_ext_s(_ld, basedn, scope, filter, attrs, 0, ServerControls, ClientControls, umtimeout, SizeLimit, out umldapMessage); } Logger.Log(String.Format("LdapHandle.Ldap_Search_Ext_S: ret={0}", ret), Logger.ldapLogLevel); if (ret != 0) { Logger.Log(ErrorCodes.LDAPString(ret), Logger.ldapLogLevel); } if (umldapMessage == IntPtr.Zero) { ldapMessage = null; return ret; } ldapMessage = new LdapMessage(this, umldapMessage); return ret; } catch (Exception ex) { Logger.LogException("LdapHandle.Ldap_Search_Ext_S", ex); return ret; } finally { Monitor.Exit(Ldap_ApiCallSync); } }
public int Ldap_Search_ST( string basedn, int scope, string filter, string[] attrs, int attrsonly, LdapTimeVal tv, out LdapMessage ldapMessage ) { DateTime beforeSearch = DateTime.Now; DateTime afterSearch = DateTime.Now; int ret = -1; ldapMessage = null; Monitor.Enter(Ldap_ApiCallSync); try { unsafe { IntPtr umldapMessage = new IntPtr(); IntPtr timeout = tv.ConvertToUM(); Logger.Log(String.Format("Calling Ldap_Search_ST(basedn={0}, scope={1}, filter={2})", basedn, scope, filter), Logger.ldapLogLevel); ret = Ldap_Rebind_S(); if (ret != 0) { Logger.Log(ErrorCodes.LDAPString(ret), Logger.ldapLogLevel); ldapMessage = null; return ret; } ret = LdapAPI.ldap_search_st(_ld, basedn, scope, filter, attrs, attrsonly, timeout, out umldapMessage); if (ret != 0) { Logger.Log(ErrorCodes.LDAPString(ret), Logger.ldapLogLevel); ldapMessage = null; return ret; } if (umldapMessage == IntPtr.Zero) { ldapMessage = null; return ret; } ldapMessage = new LdapMessage(this, umldapMessage); return ret; } } catch (Exception ex) { Logger.LogException("LdapHandle.Ldap_Search_ST", ex); return ret; } finally { afterSearch = DateTime.Now; Logger.Log(String.Format("Ldap_Search_ST complete. Time elapsed: {0}", afterSearch - beforeSearch), Logger.ldapLogLevel); Monitor.Exit(Ldap_ApiCallSync); } }