Exemple #1
0
        public static async Task <DnsServer> Create(string ip, int port, IRecordResolver recordResolver, IAuthority areWeAuthority, IExceptionHandler onError)
        {
            var networkInterface = Network.Interfaces.FirstOrDefault(x => x.IpAddress == ip);

            var udpReceiver = new UdpSocketReceiver();

            var observerUdpReceiver = await udpReceiver.ObservableUnicastListener(
                port : port,
                communicationInterface : networkInterface,
                allowMultipleBindToSamePort : false);

            var subscriberUpdReceiver = observerUdpReceiver.Subscribe(
                async udpMsg =>
            {
                var req      = Reader.ReadRequest(new MemoryStream(udpMsg.ByteData));
                var response = new Response(req);

                foreach (var q in req)
                {
                    var record = recordResolver.Resolve(q.Class, q.Type, q.Name);
                    if (record == null)
                    {
                        continue;
                    }

                    if (areWeAuthority?.AreWeAuthority(q.Name) ?? false)
                    {
                        response.Authorities.Add(record);
                    }
                    else
                    {
                        response.Answers.Add(record);
                    }
                }
                await udpReceiver.SendToAsync(Writer.Serialize(response), udpMsg.RemoteAddress, int.Parse(udpMsg.RemotePort));
            },
                x => onError?.OnError(x));

            return(new UDPServer
            {
                udpReceiver = udpReceiver,
                observerUdpReceiver = observerUdpReceiver,
                subscriberUpdReceiver = subscriberUpdReceiver,
                RecordResolver = recordResolver
            });
        }