internal void RemovePending(LdapRequestMessage message) { if (_pending.TryRemove(message.Id, out var request)) { request.Dispose(); } }
private async Task OnRequestAsync(LdapRequestMessage message, LdapClientConnection connection) { if (!await connection.TryAddPendingAsync(message).ConfigureAwait(false)) { return; } try { if (message is LdapAbandonRequest abandon) { connection.ContinueRead(); if (CriticalControlsSupported(abandon.Controls)) { connection.AbandonRequest(abandon.MessageId); } } else if (message is LdapBindRequest bind) { connection.ContinueRead(); try { await connection.BeginBindAsync().ConfigureAwait(false); var response = await BindRequestAsync(bind, connection).ConfigureAwait(false); await WriteAsync(response, connection).ConfigureAwait(false); } catch (Exception ex) { await WriteAsync(bind.Response(ResultCode.Other, ex.Message), connection).ConfigureAwait(false); } finally { connection.FinishBind(); } } else if (message is LdapUnbindRequest) { connection.ContinueRead(); UnbindRequest(connection); } else if (message is LdapSearchRequest search) { connection.ContinueRead(); await SearchRequestAsync(search, connection).ConfigureAwait(false); } else if (message is LdapExtendedRequest extended) { await ExtendedRequestAsync(extended, connection).ConfigureAwait(false); } else { connection.ContinueRead(); throw new NotImplementedException(); } } finally { connection.RemovePending(message); } }
private static async Task WriteLdapMessage(Socket socket, LdapRequestMessage ldap, CancellationToken cancellationToken) { var asn = ldap.GetAsn(); using (var asnwriter = new AsnWriter(AsnEncodingRules.BER)) { asn.Encode(asnwriter); var bytes = asnwriter.Encode(); await socket.SendAsync(bytes, SocketFlags.None, cancellationToken); } }
internal async Task <bool> TryAddPendingAsync(LdapRequestMessage message) { await _bindLock.WaitAsync(CancellationToken).ConfigureAwait(false); try { return(_pending.TryAdd(message.Id, new LdapRequest(message, CancellationToken))); } finally { _bindLock.Release(); } }
internal bool Add(LdapRequestMessage message) { if (message is LdapSearchResultEntry entry) { _entries.Add(entry); return(true); } else if (message is LdapSearchResultReference reference) { _references.Add(reference); return(true); } else if (message is LdapSearchResultDone) { return(false); } else { throw new LdapProtocolException($"unexpected response type '{message.GetType().FullName}'"); } }
protected virtual LdapRequestMessage OnSendToClient(Guid clientId, LdapRequestMessage message) { return(message); }
private static LdapRequestMessage ReadLdapMessage(ReadOnlyMemory <byte> buffer) { var message = Asn1Serializer.Deserialize(buffer); return(LdapRequestMessage.Create(message)); }
public static LdapRequestMessage ReadMessage(ReadOnlyMemory <byte> source) { var message = Asn1Serializer.Deserialize(source); return(LdapRequestMessage.Create(message)); }
public static byte[] WriteMessage(LdapRequestMessage message) { return(Asn1Serializer.Serialize(message.GetAsn())); }
public LdapRequest(LdapRequestMessage request, CancellationToken cancellationToken) { Request = request; _cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); }