private void UdpReceiveCompleted <TMessage>(IAsyncResult ar) where TMessage : DnsMessageBase, new() { DnsClientAsyncState <TMessage> state = (DnsClientAsyncState <TMessage>)ar.AsyncState; if (state.Timer != null) { state.Timer.Dispose(); } if (state.TimedOut) { state.EndpointInfoIndex++; UdpBeginSend(state); } else { try { int length = state.UdpClient.EndReceiveFrom(ar, ref state.UdpEndpoint); byte[] responseData = new byte[length]; Buffer.BlockCopy(state.Buffer, 0, responseData, 0, length); TMessage response = DnsMessageBase.Parse <TMessage>(responseData, state.TSigKeySelector, state.TSigOriginalMac); if (AreMultipleResponsesAllowedInParallelMode) { if (ValidateResponse(state.Query, response)) { if (response.IsTcpResendingRequested) { TcpBeginConnect <TMessage>(state.CreateTcpCloneWithoutCallback(), ((IPEndPoint)state.UdpEndpoint).Address); } else { state.Responses.Add(response); } } state.Buffer = new byte[65535]; if (state.EndpointInfos[state.EndpointInfoIndex].IsMulticast) { state.UdpEndpoint = new IPEndPoint(state.UdpClient.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, _port); } IAsyncResult asyncResult = state.UdpClient.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, ref state.UdpEndpoint, UdpReceiveCompleted <TMessage>, state); state.Timer = new Timer(UdpTimedOut <TMessage>, asyncResult, state.TimeRemaining, Timeout.Infinite); } else { state.UdpClient.Close(); state.UdpClient = null; state.UdpEndpoint = null; if (!ValidateResponse(state.Query, response) || (response.ReturnCode == ReturnCode.ServerFailure)) { state.EndpointInfoIndex++; UdpBeginSend(state); } else { if (response.IsTcpResendingRequested) { TcpBeginConnect <TMessage>(state, ((IPEndPoint)state.UdpEndpoint).Address); } else { state.Responses.Add(response); state.SetCompleted(); } } } } catch (Exception e) { Trace.TraceError("Error on dns query: " + e); try { state.UdpClient.Close(); state.Timer.Dispose(); } catch {} state.EndpointInfoIndex++; UdpBeginSend(state); } } }
protected TMessage SendMessage <TMessage>(TMessage message) where TMessage : DnsMessageBase, new() { int messageLength; byte[] messageData; DnsServer.SelectTsigKey tsigKeySelector; byte[] tsigOriginalMac; PrepareMessage(message, out messageLength, out messageData, out tsigKeySelector, out tsigOriginalMac); bool sendByTcp = ((messageLength > MaximumQueryMessageSize) || message.IsTcpUsingRequested); var endpointInfos = GetEndpointInfos <TMessage>(); for (int i = 0; i < endpointInfos.Count; i++) { TcpClient tcpClient = null; NetworkStream tcpStream = null; try { var endpointInfo = endpointInfos[i]; IPAddress responderAddress; byte[] resultData = sendByTcp ? QueryByTcp(endpointInfo.ServerAddress, messageData, messageLength, ref tcpClient, ref tcpStream, out responderAddress) : QueryByUdp(endpointInfo, messageData, messageLength, out responderAddress); if (resultData != null) { TMessage result; try { result = DnsMessageBase.Parse <TMessage>(resultData, tsigKeySelector, tsigOriginalMac); } catch (Exception e) { Trace.TraceError("Error on dns query: " + e); continue; } if (!ValidateResponse(message, result)) { continue; } if ((result.ReturnCode == ReturnCode.ServerFailure) && (i != endpointInfos.Count - 1)) { continue; } if (result.IsTcpResendingRequested) { resultData = QueryByTcp(responderAddress, messageData, messageLength, ref tcpClient, ref tcpStream, out responderAddress); if (resultData != null) { TMessage tcpResult; try { tcpResult = DnsMessageBase.Parse <TMessage>(resultData, tsigKeySelector, tsigOriginalMac); } catch (Exception e) { Trace.TraceError("Error on dns query: " + e); continue; } if (tcpResult.ReturnCode == ReturnCode.ServerFailure) { if (i != endpointInfos.Count - 1) { continue; } } else { result = tcpResult; } } } bool isTcpNextMessageWaiting = result.IsTcpNextMessageWaiting(false); bool isSucessfullFinished = true; while (isTcpNextMessageWaiting) { resultData = QueryByTcp(responderAddress, null, 0, ref tcpClient, ref tcpStream, out responderAddress); if (resultData != null) { TMessage tcpResult; try { tcpResult = DnsMessageBase.Parse <TMessage>(resultData, tsigKeySelector, tsigOriginalMac); } catch (Exception e) { Trace.TraceError("Error on dns query: " + e); isSucessfullFinished = false; break; } if (tcpResult.ReturnCode == ReturnCode.ServerFailure) { isSucessfullFinished = false; break; } else { result.AnswerRecords.AddRange(tcpResult.AnswerRecords); isTcpNextMessageWaiting = tcpResult.IsTcpNextMessageWaiting(true); } } else { isSucessfullFinished = false; break; } } if (isSucessfullFinished) { return(result); } } } finally { try { if (tcpStream != null) { tcpStream.Dispose(); } if (tcpClient != null) { tcpClient.Close(); } } catch {} } } return(null); }
private void TcpReceiveCompleted <TMessage>(IAsyncResult ar) where TMessage : DnsMessageBase, new() { DnsClientAsyncState <TMessage> state = (DnsClientAsyncState <TMessage>)ar.AsyncState; if (state.Timer != null) { state.Timer.Dispose(); } if (state.TimedOut) { state.EndpointInfoIndex++; TcpBeginConnect(state); } else { try { state.TcpBytesToReceive -= state.TcpStream.EndRead(ar); if (state.TcpBytesToReceive > 0) { IAsyncResult asyncResult = state.TcpStream.BeginRead(state.Buffer, state.Buffer.Length - state.TcpBytesToReceive, state.TcpBytesToReceive, TcpReceiveCompleted <TMessage>, state); state.Timer = new Timer(TcpTimedOut <TMessage>, asyncResult, state.TimeRemaining, Timeout.Infinite); } else { byte[] buffer = state.Buffer; state.Buffer = null; TMessage response = DnsMessageBase.Parse <TMessage>(buffer, state.TSigKeySelector, state.TSigOriginalMac); if (!ValidateResponse(state.Query, response) || (response.ReturnCode == ReturnCode.ServerFailure)) { state.EndpointInfoIndex++; state.PartialMessage = null; state.TcpStream.Close(); state.TcpClient.Close(); state.TcpStream = null; state.TcpClient = null; TcpBeginConnect(state); } else { bool isSubsequentResponseMessage = (state.PartialMessage != null); if (isSubsequentResponseMessage) { state.PartialMessage.AnswerRecords.AddRange(response.AnswerRecords); } else { state.PartialMessage = response; } if (response.IsTcpNextMessageWaiting(isSubsequentResponseMessage)) { state.TcpBytesToReceive = 2; state.Buffer = new byte[2]; IAsyncResult asyncResult = state.TcpStream.BeginRead(state.Buffer, 0, 2, TcpReceiveLengthCompleted <TMessage>, state); state.Timer = new Timer(TcpTimedOut <TMessage>, asyncResult, state.TimeRemaining, Timeout.Infinite); } else { state.TcpStream.Close(); state.TcpClient.Close(); state.TcpStream = null; state.TcpClient = null; state.Responses.Add(state.PartialMessage); state.SetCompleted(); } } } } catch (Exception e) { Trace.TraceError("Error on dns query: " + e); try { state.TcpClient.Close(); state.Timer.Dispose(); } catch {} state.EndpointInfoIndex++; TcpBeginConnect(state); } } }