private void PrepareAndBindUdpSocket(DnsClientEndpointInfo endpointInfo, Socket udpClient)
 {
     if (endpointInfo.IsMulticast)
     {
         udpClient.Bind(new IPEndPoint(endpointInfo.LocalAddress, 0));
     }
     else
     {
         udpClient.Connect(endpointInfo.ServerAddress, _port);
     }
 }
Esempio n. 2
0
        private async Task <QueryResponse> QuerySingleResponseByUdpAsync(DnsClientEndpointInfo endpointInfo, byte[] messageData, int messageLength, CancellationToken token)
        {
            try
            {
                if (endpointInfo.IsMulticast)
                {
                    using (UdpClient udpClient = new UdpClient(new IPEndPoint(endpointInfo.LocalAddress, 0)))
                    {
                        IPEndPoint serverEndpoint = new IPEndPoint(endpointInfo.ServerAddress, _port);
                        await udpClient.SendAsync(messageData, messageLength, serverEndpoint);

                        udpClient.Client.SendTimeout    = QueryTimeout;
                        udpClient.Client.ReceiveTimeout = QueryTimeout;

                        UdpReceiveResult response = await udpClient.ReceiveAsync(QueryTimeout, token);

                        return(new QueryResponse(response.Buffer, response.RemoteEndPoint.Address));
                    }
                }
                else
                {
                    using (UdpClient udpClient = new UdpClient(endpointInfo.LocalAddress.AddressFamily))
                    {
#if !NETSTANDARD
                        udpClient.Connect(endpointInfo.ServerAddress, _port);
#endif

                        udpClient.Client.SendTimeout    = QueryTimeout;
                        udpClient.Client.ReceiveTimeout = QueryTimeout;

#if NETSTANDARD
                        await udpClient.SendAsync(messageData, messageLength, new IPEndPoint(endpointInfo.ServerAddress, _port));
#else
                        await udpClient.SendAsync(messageData, messageLength);
#endif

                        UdpReceiveResult response = await udpClient.ReceiveAsync(QueryTimeout, token);

                        return(new QueryResponse(response.Buffer, response.RemoteEndPoint.Address));
                    }
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("Error on dns query: " + e);
                return(null);
            }
        }
        private async Task SendMessageParallelAsync <TMessage>(DnsClientEndpointInfo endpointInfo, TMessage message, byte[] messageData, int messageLength, DnsServer.SelectTsigKey tsigKeySelector, byte[] tsigOriginalMac, BlockingCollection <TMessage> results, CancellationToken token)
            where TMessage : DnsMessageBase, new()
        {
            using (UdpClient udpClient = new UdpClient(new IPEndPoint(endpointInfo.LocalAddress, 0)))
            {
                IPEndPoint serverEndpoint = new IPEndPoint(endpointInfo.ServerAddress, _port);
                await udpClient.SendAsync(messageData, messageLength, serverEndpoint);

                udpClient.Client.SendTimeout    = QueryTimeout;
                udpClient.Client.ReceiveTimeout = QueryTimeout;

                while (true)
                {
                    TMessage         result;
                    UdpReceiveResult response = await udpClient.ReceiveAsync(Int32.MaxValue, token);

                    try
                    {
                        result = DnsMessageBase.Parse <TMessage>(response.Buffer, tsigKeySelector, tsigOriginalMac);
                    }
                    catch (Exception e)
                    {
                        Trace.TraceError("Error on dns query: " + e);
                        continue;
                    }

                    if (!ValidateResponse(message, result))
                    {
                        continue;
                    }

                    if (result.ReturnCode == ReturnCode.ServerFailure)
                    {
                        continue;
                    }

                    results.Add(result, token);

                    if (token.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }
        }
Esempio n. 4
0
        private void UdpBeginSend <TMessage>(DnsClientAsyncState <TMessage> state)
            where TMessage : DnsMessageBase, new()
        {
            if (state.EndpointInfoIndex == state.EndpointInfos.Count)
            {
                state.UdpClient   = null;
                state.UdpEndpoint = null;
                state.SetCompleted();
                return;
            }

            try
            {
                DnsClientEndpointInfo endpointInfo = state.EndpointInfos[state.EndpointInfoIndex];

                state.UdpEndpoint = new IPEndPoint(endpointInfo.ServerAddress, _port);

                state.UdpClient = new System.Net.Sockets.Socket(state.UdpEndpoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);

                PrepareAndBindUdpSocket(endpointInfo, state.UdpClient);

                state.TimedOut      = false;
                state.TimeRemaining = QueryTimeout;

                IAsyncResult asyncResult = state.UdpClient.BeginSendTo(state.QueryData, 0, state.QueryLength, SocketFlags.None, state.UdpEndpoint, UdpSendCompleted <TMessage>, state);
                state.Timer = new Timer(UdpTimedOut <TMessage>, asyncResult, state.TimeRemaining, Timeout.Infinite);
            }
            catch (Exception e)
            {
                Trace.TraceError("Error on dns query: " + e);

                try
                {
                    state.UdpClient.Close();
                    state.Timer.Dispose();
                }
                catch {}

                state.EndpointInfoIndex++;
                UdpBeginSend(state);
            }
        }
Esempio n. 5
0
        private async Task <QueryResponse> QuerySingleResponseByUdpAsync(DnsClientEndpointInfo endpointInfo, byte[] messageData, int messageLength, CancellationToken token)
        {
            try
            {
                if (endpointInfo.IsMulticast)
                {
                    using (var udpClient = new UdpClient(new IPEndPoint(endpointInfo.LocalAddress, 0)))
                    {
                        var serverEndpoint = new IPEndPoint(endpointInfo.ServerAddress, _port);
                        await udpClient.SendAsync(messageData, messageLength, serverEndpoint);

                        udpClient.Client.SendTimeout    = QueryTimeout;
                        udpClient.Client.ReceiveTimeout = QueryTimeout;

                        var response = await udpClient.ReceiveAsync(QueryTimeout, token);

                        return(new QueryResponse(response.Buffer, response.RemoteEndPoint.Address));
                    }
                }

                using (var udpClient = new UdpClient(endpointInfo.LocalAddress.AddressFamily))
                {
                    udpClient.Connect(endpointInfo.ServerAddress, _port);

                    udpClient.Client.SendTimeout    = QueryTimeout;
                    udpClient.Client.ReceiveTimeout = QueryTimeout;

                    await udpClient.SendAsync(messageData, messageLength);

                    var response = await udpClient.ReceiveAsync(QueryTimeout, token);

                    return(response.Buffer == null || response.RemoteEndPoint?.Address == null ? null : new QueryResponse(response.Buffer, response.RemoteEndPoint.Address));
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("Error on dns query: " + e);
                return(null);
            }
        }
        private byte[] QueryByUdp(DnsClientEndpointInfo endpointInfo, byte[] messageData, int messageLength, out IPAddress responderAddress)
        {
            using (var udpClient = new Socket(endpointInfo.LocalAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp))
            {
                try
                {
                    udpClient.ReceiveTimeout = QueryTimeout;

                    PrepareAndBindUdpSocket(endpointInfo, udpClient);

                    EndPoint serverEndpoint = new IPEndPoint(endpointInfo.ServerAddress, _port);

                    udpClient.SendTo(messageData, messageLength, SocketFlags.None, serverEndpoint);

                    if (endpointInfo.IsMulticast)
                    {
                        serverEndpoint = new IPEndPoint(udpClient.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, _port);
                    }

                    byte[] buffer = new byte[65535];
                    int    length = udpClient.ReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref serverEndpoint);

                    responderAddress = ((IPEndPoint)serverEndpoint).Address;

                    byte[] res = new byte[length];
                    Buffer.BlockCopy(buffer, 0, res, 0, length);
                    return(res);
                }
                catch (Exception e)
                {
                    Trace.TraceError("Error on dns query: " + e);
                    responderAddress = default(IPAddress);
                    return(null);
                }
            }
        }