Beispiel #1
0
        // 共通メソッド
        // リクエスト送信して、サーバから返ったデータをDNSパケットとしてデコードする
        // レスポンスが無い場合は、1秒でタイムアウトしてnullを返す
        // rd = 再帰要求
        private PacketDns lookup(DnsType dnsType, string name, bool rd = false)
        {
            //乱数で識別子生成
            var id = (ushort)(new Random()).Next(100);
            //送信パケット生成
            var sp = new PacketDns(id, false, false, rd, false);

            //質問フィールド追加
            sp.AddRr(RrKind.QD, new RrQuery(name, dnsType));
            //クライアントソケット生成、及び送信
            var cl = new SockUdp(new Kernel(), new Ip(IpKind.V4Localhost), 53, null, sp.GetBytes());
            //受信
            //byte[] recvBuf = cl.Recv(1000);
            var recvBuf = cl.Recv(3);

            if (recvBuf.Length == 0)
            {
                //受信データが無い場合
                return(null);
            }
            //System.out.println(string.Format("lookup(%s,\"%s\") recv().Length=%d", dnsType, name, recvBuf.Length));
            //デコード
            var p = new PacketDns(recvBuf);

            //System.out.println(print(p));
            return(p);
        }
Beispiel #2
0
        public void パケット生成_MX()
        {
            //setUp

            //exercise
            //パケットの生成
            const ushort id  = (ushort)0xf00f;
            const bool   qr  = true;  //応答
            const bool   rd  = false; //再帰要求(有効)
            const bool   aa  = true;  //権限(あり)
            const bool   ra  = true;
            var          sut = new PacketDns(id, qr, aa, rd, ra);

            sut.AddRr(RrKind.QD, new RrQuery("google.com.", DnsType.Mx));
            sut.AddRr(RrKind.AN, new RrMx("google.com.", 0xa7d, 10, "smtp.google.com."));
            sut.AddRr(RrKind.NS, new RrNs("google.com.", 0xa7d, "ns3.google.com."));
            //生成したパケットのバイト配列で、再度パケットクラスを生成する
            byte[] b = sut.GetBytes();
            var    p = new PacketDns(b);

            //verify
            Assert.That(p.GetAa(), Is.EqualTo(true));
            Assert.That(p.GetId(), Is.EqualTo((ushort)0xf00f));
            Assert.That(p.GetDnsType(), Is.EqualTo(DnsType.Mx));
            Assert.That(p.GetCount(RrKind.QD), Is.EqualTo(1));
            Assert.That(p.GetCount(RrKind.AN), Is.EqualTo(1));
            Assert.That(p.GetCount(RrKind.NS), Is.EqualTo(1));
            Assert.That(p.GetCount(RrKind.AR), Is.EqualTo(0));
            Assert.That(p.GetRcode(), Is.EqualTo((short)0));

            //DEBUG
            //OneRr r = p.GetRr(RrKind.AN, 0);


            Assert.That(p.GetRr(RrKind.AN, 0).ToString(), Is.EqualTo(new RrMx("google.com.", 0xa7d, 10, "smtp.google.com.").ToString()));
            Assert.That(p.GetRr(RrKind.NS, 0).ToString(), Is.EqualTo(new RrNs("google.com.", 0xa7d, "ns3.google.com.").ToString()));
            //		Assert.That(p.GetRr(RrKind.NS, 1).ToString(), Is.EqualTo(new RrNs("google.com.", 0xa7d, "ns1.google.com.").ToString()));
            //		Assert.That(p.GetRr(RrKind.NS, 2).ToString(), Is.EqualTo(new RrNs("google.com.", 0xa7d, "ns4.google.com.").ToString()));
            //		Assert.That(p.GetRr(RrKind.NS, 3).ToString(), Is.EqualTo(new RrNs("google.com.", 0xa7d, "ns2.google.com.").ToString()));
            //
            //		Assert.That(p.GetRr(RrKind.AR, 0).ToString(), Is.EqualTo(new RrA("ns1.google.com.", 0xb5e, new Ip("216.239.32.10")).ToString()));
            //		Assert.That(p.GetRr(RrKind.AR, 1).ToString(), Is.EqualTo(new RrA("ns2.google.com.", 0xbde, new Ip("216.239.34.10")).ToString()));
            //		Assert.That(p.GetRr(RrKind.AR, 2).ToString(), Is.EqualTo(new RrA("ns3.google.com.", 0xaf5, new Ip("216.239.36.10")).ToString()));
            //		Assert.That(p.GetRr(RrKind.AR, 3).ToString(), Is.EqualTo(new RrA("ns4.google.com.", 0xab3, new Ip("216.239.38.10")).ToString()));
        }
Beispiel #3
0
        public void パケット生成_A_NS()
        {
            //setUp

            //exercise
            //パケットの生成
            const ushort id  = 0x0005;
            const bool   qr  = true;  //応答
            const bool   rd  = true;  //再帰要求(有効)
            const bool   aa  = false; //権限(なし)
            const bool   ra  = true;
            var          sut = new PacketDns(id, qr, aa, rd, ra);

            sut.AddRr(RrKind.QD, new RrQuery("www.google.com.", DnsType.Aaaa));
            sut.AddRr(RrKind.NS, new RrNs("google.com.", 0xa7d, "ns3.google.com."));
            sut.AddRr(RrKind.NS, new RrNs("google.com.", 0xa7d, "ns1.google.com."));
            sut.AddRr(RrKind.NS, new RrNs("google.com.", 0xa7d, "ns4.google.com."));
            sut.AddRr(RrKind.NS, new RrNs("google.com.", 0xa7d, "ns2.google.com."));
            sut.AddRr(RrKind.AR, new RrA("ns1.google.com.", 0xb5e, new Ip("216.239.32.10")));
            sut.AddRr(RrKind.AR, new RrA("ns2.google.com.", 0xbde, new Ip("216.239.34.10")));
            sut.AddRr(RrKind.AR, new RrA("ns3.google.com.", 0xaf5, new Ip("216.239.36.10")));
            sut.AddRr(RrKind.AR, new RrA("ns4.google.com.", 0xab3, new Ip("216.239.38.10")));
            //生成したパケットのバイト配列で、再度パケットクラスを生成する
            var p = new PacketDns(sut.GetBytes());

            //verify
            Assert.That(p.GetAa(), Is.EqualTo(false));
            Assert.That(p.GetId(), Is.EqualTo((short)0x0005));
            Assert.That(p.GetDnsType(), Is.EqualTo(DnsType.Aaaa));
            Assert.That(p.GetCount(RrKind.QD), Is.EqualTo(1));
            Assert.That(p.GetCount(RrKind.AN), Is.EqualTo(0));
            Assert.That(p.GetCount(RrKind.NS), Is.EqualTo(4));
            Assert.That(p.GetCount(RrKind.AR), Is.EqualTo(4));
            Assert.That(p.GetRcode(), Is.EqualTo((short)0));
            Assert.That(p.GetRr(RrKind.NS, 0).ToString(), Is.EqualTo(new RrNs("google.com.", 0xa7d, "ns3.google.com.").ToString()));
            Assert.That(p.GetRr(RrKind.NS, 1).ToString(), Is.EqualTo(new RrNs("google.com.", 0xa7d, "ns1.google.com.").ToString()));
            Assert.That(p.GetRr(RrKind.NS, 2).ToString(), Is.EqualTo(new RrNs("google.com.", 0xa7d, "ns4.google.com.").ToString()));
            Assert.That(p.GetRr(RrKind.NS, 3).ToString(), Is.EqualTo(new RrNs("google.com.", 0xa7d, "ns2.google.com.").ToString()));

            Assert.That(p.GetRr(RrKind.AR, 0).ToString(), Is.EqualTo(new RrA("ns1.google.com.", 0xb5e, new Ip("216.239.32.10")).ToString()));
            Assert.That(p.GetRr(RrKind.AR, 1).ToString(), Is.EqualTo(new RrA("ns2.google.com.", 0xbde, new Ip("216.239.34.10")).ToString()));
            Assert.That(p.GetRr(RrKind.AR, 2).ToString(), Is.EqualTo(new RrA("ns3.google.com.", 0xaf5, new Ip("216.239.36.10")).ToString()));
            Assert.That(p.GetRr(RrKind.AR, 3).ToString(), Is.EqualTo(new RrA("ns4.google.com.", 0xab3, new Ip("216.239.38.10")).ToString()));
        }