コード例 #1
0
ファイル: DnsDataServer.cs プロジェクト: wflk/canape
        public override void Run(IDataAdapter adapter, Logger logger)
        {
            DataFrame frame = adapter.Read();

            while (frame != null)
            {
                try
                {
                    DNSPacket packet = DNSPacket.FromArray(frame.ToArray());
                    List <DNSPacket.DNSRRBase> aRecords = new List <DNSPacket.DNSRRBase>();

                    foreach (DNSPacket.DNSQuestion question in packet.Questions)
                    {
                        if ((question.QClass == DNSPacket.DNSClass.IN) || (question.QClass == DNSPacket.DNSClass.AnyClass))
                        {
                            if ((question.QType == DNSPacket.DNSType.A) || (question.QType == DNSPacket.DNSType.AllRecords))
                            {
                                if (Config.ResponseAddress != IPAddress.Any)
                                {
                                    DNSPacket.ADNSRR addr = new DNSPacket.ADNSRR();

                                    addr.Address = new IPAddress(Config.ResponseAddress.GetAddressBytes());

                                    addr.TimeToLive = Config.TimeToLive;
                                    addr.Type       = DNSPacket.DNSType.A;
                                    addr.Class      = DNSPacket.DNSClass.IN;
                                    addr.Name       = question.QName;

                                    aRecords.Add(addr);
                                }
                            }

                            if ((question.QType == DNSPacket.DNSType.AAAA) || (question.QType == DNSPacket.DNSType.AllRecords))
                            {
                                if (Config.ResponseAddress6 != IPAddress.IPv6Any)
                                {
                                    DNSPacket.AAAADNSRR addr = new DNSPacket.AAAADNSRR();

                                    addr.Address = new IPAddress(Config.ResponseAddress6.GetAddressBytes());

                                    addr.TimeToLive = Config.TimeToLive;
                                    addr.Type       = DNSPacket.DNSType.AAAA;
                                    addr.Class      = DNSPacket.DNSClass.IN;
                                    addr.Name       = question.QName;

                                    aRecords.Add(addr);
                                }
                            }

                            if ((question.QType == DNSPacket.DNSType.PTR) || (question.QType == DNSPacket.DNSType.AllRecords))
                            {
                                if (!String.IsNullOrEmpty(Config.ReverseDns) && ((question.QName.EndsWith(".in-addr.arpa.") || question.QName.EndsWith(".ip6.arpa."))))
                                {
                                    DNSPacket.PTRDNSRR addr = new DNSPacket.PTRDNSRR();

                                    addr.Ptr   = Config.ReverseDns;
                                    addr.Type  = DNSPacket.DNSType.PTR;
                                    addr.Class = DNSPacket.DNSClass.IN;
                                    addr.Name  = question.QName;

                                    aRecords.Add(addr);
                                }
                            }
                        }
                    }

                    packet.Query = true;
                    packet.RecursionAvailable = true;
                    if (aRecords.Count > 0)
                    {
                        packet.Answers           = aRecords.ToArray();
                        packet.AuthoritiveAnswer = true;
                        packet.ResponseCode      = DNSPacket.DNSRCode.NoError;
                    }
                    else
                    {
                        packet.ResponseCode = DNSPacket.DNSRCode.Refused;
                    }

                    adapter.Write(new DataFrame(packet.ToArray()));
                }
                catch (ArgumentException ex)
                {
                    logger.LogException(ex);
                }

                frame = adapter.Read();
            }
        }
コード例 #2
0
ファイル: DNSParser.cs プロジェクト: wflk/canape
        /// <summary>
        ///
        /// </summary>
        /// <param name="data"></param>
        /// <param name="root"></param>
        /// <param name="logger"></param>
        public void FromArray(byte[] data, DataKey root, Logger logger)
        {
            DNSPacket packet = DNSPacket.FromArray(data);

            packet.ToDataKey(root);
        }