private async Task <DnsMessageBase> ProcessMessageAsync(DnsMessageBase query, ProtocolType protocolType, IPEndPoint remoteEndpoint) { QueryReceivedEventArgs eventArgs = new QueryReceivedEventArgs(query, protocolType, remoteEndpoint); await QueryReceived.RaiseAsync(this, eventArgs); return(eventArgs.Response); }
/// <summary> /// Called by the MulticastClient when a DNS message is received. /// </summary> /// <param name="sender"> /// The <see cref="MulticastClient"/> that got the message. /// </param> /// <param name="result"> /// The received message <see cref="UdpReceiveResult"/>. /// </param> /// <remarks> /// Decodes the <paramref name="result"/> and then raises /// either the <see cref="QueryReceived"/> or <see cref="AnswerReceived"/> event. /// <para> /// Multicast DNS messages received with an OPCODE or RCODE other than zero /// are silently ignored. /// </para> /// <para> /// If the message cannot be decoded, then the <see cref="MalformedMessage"/> /// event is raised. /// </para> /// </remarks> public void OnDnsMessage(object sender, UdpReceiveResult result) { // If recently received, then ignore. // A message has been recently received, if its content, as well as the remote endpoint(!) match, otherwise messages from different senders can be considered equal. var receivedMessage = new byte[result.Buffer.Length + result.RemoteEndPoint.Address.GetAddressBytes().Length]; result.Buffer.CopyTo(receivedMessage, 0); result.RemoteEndPoint.Address.GetAddressBytes().CopyTo(receivedMessage, result.Buffer.Length); if (IgnoreDuplicateMessages && !receivedMessages.TryAdd(receivedMessage)) { // Not continuing here because it looks like a duplicate message return; } var msg = new Message(); try { msg.Read(result.Buffer, 0, result.Buffer.Length); } catch (Exception e) { log.Warn("Received malformed message", e); MalformedMessage?.Invoke(this, result.Buffer); return; // eat the exception } if (msg.Opcode != MessageOperation.Query || msg.Status != MessageStatus.NoError) { return; } // Dispatch the message. try { if (msg.IsQuery && msg.Questions.Count > 0) { QueryReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } else if (msg.IsResponse && msg.Answers.Count > 0) { AnswerReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } } catch (Exception e) { log.Error("Receive handler failed", e); // eat the exception } }
/// <summary> /// Called by the MulticastClient when a DNS message is received. /// </summary> /// <param name="sender"> /// The <see cref="MulticastClient"/> that got the message. /// </param> /// <param name="result"> /// The received message <see cref="UdpReceiveResult"/>. /// </param> /// <remarks> /// Decodes the <paramref name="result"/> and then raises /// either the <see cref="QueryReceived"/> or <see cref="AnswerReceived"/> event. /// <para> /// Multicast DNS messages received with an OPCODE or RCODE other than zero /// are silently ignored. /// </para> /// <para> /// If the message cannot be decoded, then the <see cref="MalformedMessage"/> /// event is raised. /// </para> /// </remarks> public void OnDnsMessage(object sender, UdpReceiveResult result) { // If recently received, then ignore. if (IgnoreDuplicateMessages && !_receivedMessages.TryAdd(result.Buffer)) { return; } if (GetLinkLocalAddresses().Any(a => a == result.RemoteEndPoint.Address)) { return; } var msg = new Message(); try { msg.Read(result.Buffer, 0, result.Buffer.Length); } catch (Exception e) { Log.LogWarning("Received malformed message", e); MalformedMessage?.Invoke(this, result.Buffer); return; // eat the exception } if (msg.Opcode != MessageOperation.Query || msg.Status != MessageStatus.NoError) { return; } // Dispatch the message. try { if (msg.IsQuery && msg.Questions.Count > 0) { QueryReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } else if (msg.IsResponse && msg.Answers.Count > 0) { AnswerReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } } catch (Exception e) { Log.LogError("Receive handler failed", e); // eat the exception } }
/// <summary> /// Called by the MulticastClient when a DNS message is received. /// </summary> /// <param name="sender"> /// The <see cref="MulticastClient"/> that got the message. /// </param> /// <param name="result"> /// The received message <see cref="UdpReceiveResult"/>. /// </param> /// <remarks> /// Decodes the <paramref name="result"/> and then raises /// either the <see cref="QueryReceived"/> or <see cref="AnswerReceived"/> event. /// <para> /// Multicast DNS messages received with an OPCODE or RCODE other than zero /// are silently ignored. /// </para> /// <para> /// If the message cannot be decoded, then the <see cref="MalformedMessage"/> /// event is raised. /// </para> /// </remarks> public void OnDnsMessage(object sender, UdpReceiveResult result) { // If recently received, then ignore. if (IgnoreDuplicateMessages && !receivedMessages.TryAdd(result.Buffer)) { return; } var msg = new Message(); try { msg.Read(result.Buffer, 0, result.Buffer.Length); } catch (Exception e) { System.Diagnostics.Trace.TraceWarning($"Received malformed message : {e.GetBaseException().Message}"); MalformedMessage?.Invoke(this, result.Buffer); return; // eat the exception } if (msg.Opcode != MessageOperation.Query || msg.Status != MessageStatus.NoError) { return; } // Dispatch the message. try { if (msg.IsQuery && msg.Questions.Count > 0) { QueryReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } else if (msg.IsResponse && msg.Answers.Count > 0) { AnswerReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } } catch (Exception e) { System.Diagnostics.Trace.TraceError($"Receive handler failed {e.GetBaseException().Message}"); // eat the exception } }
private async Task <DnsMessageBase> ProcessMessageAsync(DnsMessageBase query, ProtocolType protocolType, IPEndPoint remoteEndpoint) { if (query.TSigOptions != null) { switch (query.TSigOptions.ValidationResult) { case ReturnCode.BadKey: case ReturnCode.BadSig: query.IsQuery = false; query.ReturnCode = ReturnCode.NotAuthoritive; query.TSigOptions.Error = query.TSigOptions.ValidationResult; query.TSigOptions.KeyData = null; #pragma warning disable 4014 InvalidSignedMessageReceived.RaiseAsync(this, new InvalidSignedMessageEventArgs(query, protocolType, remoteEndpoint)); #pragma warning restore 4014 return(query); case ReturnCode.BadTime: query.IsQuery = false; query.ReturnCode = ReturnCode.NotAuthoritive; query.TSigOptions.Error = query.TSigOptions.ValidationResult; query.TSigOptions.OtherData = new byte[6]; int tmp = 0; TSigRecord.EncodeDateTime(query.TSigOptions.OtherData, ref tmp, DateTime.Now); #pragma warning disable 4014 InvalidSignedMessageReceived.RaiseAsync(this, new InvalidSignedMessageEventArgs(query, protocolType, remoteEndpoint)); #pragma warning restore 4014 return(query); } } QueryReceivedEventArgs eventArgs = new QueryReceivedEventArgs(query, protocolType, remoteEndpoint); await QueryReceived.RaiseAsync(this, eventArgs); return(eventArgs.Response); }
/// <summary> /// Called by the listener when a DNS message is received. /// </summary> /// <param name="datagram"> /// The received message. /// </param> /// <param name="length"> /// The length of the messages. /// </param> /// <remarks> /// Decodes the <paramref name="datagram"/> and then raises /// either the <see cref="QueryReceived"/> or <see cref="AnswerReceived"/> event. /// <para> /// Multicast DNS messages received with an OPCODE or RCODE other than zero /// are silently ignored. /// </para> /// </remarks> void OnDnsMessage(byte[] datagram, int length) { var msg = new Message(); try { msg.Read(datagram, 0, length); } catch (Exception e) { log.Warn("Received malformed message", e); return; // eat the exception } if (msg.Opcode != MessageOperation.Query || msg.Status != MessageStatus.NoError) { return; } // Dispatch the message. try { if (msg.IsQuery && msg.Questions.Count > 0) { QueryReceived?.Invoke(this, new MessageEventArgs { Message = msg }); } else if (msg.IsResponse && msg.Answers.Count > 0) { AnswerReceived?.Invoke(this, new MessageEventArgs { Message = msg }); } } catch (Exception e) { log.Error("Receive handler failed", e); // eat the exception } }