public void パケット解釈_str0() { //exercise var sut = new PacketDns(TestUtil.HexStream2Bytes(Str0)); //verify Assert.That(sut.GetId(), Is.EqualTo((short)0x0003)); Assert.That(sut.GetCount(RrKind.QD), Is.EqualTo(1)); Assert.That(sut.GetCount(RrKind.AN), Is.EqualTo(0)); Assert.That(sut.GetCount(RrKind.NS), Is.EqualTo(4)); Assert.That(sut.GetCount(RrKind.AR), Is.EqualTo(4)); Assert.That(sut.GetRcode(), Is.EqualTo((short)0)); Assert.That(sut.GetAa(), Is.EqualTo(false)); Assert.That(sut.GetRd(), Is.EqualTo(true)); Assert.That(sut.GetDnsType(), Is.EqualTo(DnsType.Aaaa)); Assert.That(sut.GetRequestName(), Is.EqualTo("www.google.com.")); Assert.That(sut.GetRr(RrKind.QD, 0).ToString(), Is.EqualTo((new RrQuery("www.google.com.", DnsType.Aaaa)).ToString())); Assert.That(sut.GetRr(RrKind.NS, 0).ToString(), Is.EqualTo((new RrNs("google.com.", 83400, "ns4.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.NS, 1).ToString(), Is.EqualTo((new RrNs("google.com.", 83400, "ns2.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.NS, 2).ToString(), Is.EqualTo((new RrNs("google.com.", 83400, "ns3.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.NS, 3).ToString(), Is.EqualTo((new RrNs("google.com.", 83400, "ns1.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.AR, 0).ToString(), Is.EqualTo((new RrA("ns1.google.com.", 83465, new Ip("216.239.32.10"))).ToString())); Assert.That(sut.GetRr(RrKind.AR, 1).ToString(), Is.EqualTo((new RrA("ns2.google.com.", 83563, new Ip("216.239.34.10"))).ToString())); Assert.That(sut.GetRr(RrKind.AR, 2).ToString(), Is.EqualTo((new RrA("ns3.google.com.", 83465, new Ip("216.239.36.10"))).ToString())); Assert.That(sut.GetRr(RrKind.AR, 3).ToString(), Is.EqualTo((new RrA("ns4.google.com.", 83465, new Ip("216.239.38.10"))).ToString())); }
public void パケット解釈_str1() { //exercise var sut = new PacketDns(TestUtil.HexStream2Bytes(Str1)); //verify Assert.That(sut.GetId(), Is.EqualTo((short)0x0002)); Assert.That(sut.GetCount(RrKind.QD), Is.EqualTo(1)); Assert.That(sut.GetCount(RrKind.AN), Is.EqualTo(5)); Assert.That(sut.GetCount(RrKind.NS), Is.EqualTo(4)); Assert.That(sut.GetCount(RrKind.AR), Is.EqualTo(7)); Assert.That(sut.GetRcode(), Is.EqualTo((short)0)); Assert.That(sut.GetAa(), Is.EqualTo(false)); Assert.That(sut.GetRd(), Is.EqualTo(true)); Assert.That(sut.GetDnsType(), Is.EqualTo(DnsType.Mx)); Assert.That(sut.GetRequestName(), Is.EqualTo("gmail.com.")); Assert.That(sut.GetRr(RrKind.QD, 0).ToString(), Is.EqualTo((new RrQuery("gmail.com.", DnsType.Mx)).ToString())); Assert.That(sut.GetRr(RrKind.AN, 0).ToString(), Is.EqualTo((new RrMx("gmail.com.", 3427, 10, "alt1.gmail-smtp-in.l.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.AN, 1).ToString(), Is.EqualTo((new RrMx("gmail.com.", 3427, 20, "alt2.gmail-smtp-in.l.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.AN, 2).ToString(), Is.EqualTo((new RrMx("gmail.com.", 3427, 30, "alt3.gmail-smtp-in.l.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.AN, 3).ToString(), Is.EqualTo((new RrMx("gmail.com.", 3427, 40, "alt4.gmail-smtp-in.l.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.AN, 4).ToString(), Is.EqualTo((new RrMx("gmail.com.", 3427, 5, "gmail-smtp-in.l.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.NS, 0).ToString(), Is.EqualTo((new RrNs("gmail.com.", 54413, "ns4.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.NS, 1).ToString(), Is.EqualTo((new RrNs("gmail.com.", 54413, "ns2.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.NS, 2).ToString(), Is.EqualTo((new RrNs("gmail.com.", 54413, "ns1.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.NS, 3).ToString(), Is.EqualTo((new RrNs("gmail.com.", 54413, "ns3.google.com.")).ToString())); Assert.That(sut.GetRr(RrKind.AR, 0).ToString(), Is.EqualTo((new RrA("gmail-smtp-in.l.google.com.", 295, new Ip("74.125.25.27"))).ToString())); Assert.That(sut.GetRr(RrKind.AR, 1).ToString(), Is.EqualTo((new RrA("alt2.gmail-smtp-in.l.google.com.", 60, new Ip("74.125.140.27"))).ToString())); Assert.That(sut.GetRr(RrKind.AR, 2).ToString(), Is.EqualTo((new RrA("alt4.gmail-smtp-in.l.google.com.", 149, new Ip("74.125.131.27"))).ToString())); Assert.That(sut.GetRr(RrKind.AR, 3).ToString(), Is.EqualTo((new RrA("ns1.google.com.", 54584, new Ip("216.239.32.10"))).ToString())); Assert.That(sut.GetRr(RrKind.AR, 4).ToString(), Is.EqualTo((new RrA("ns2.google.com.", 54578, new Ip("216.239.34.10"))).ToString())); Assert.That(sut.GetRr(RrKind.AR, 5).ToString(), Is.EqualTo((new RrA("ns3.google.com.", 54641, new Ip("216.239.36.10"))).ToString())); Assert.That(sut.GetRr(RrKind.AR, 6).ToString(), Is.EqualTo((new RrA("ns4.google.com.", 54520, new Ip("216.239.38.10"))).ToString())); }
// 共通メソッド // リクエスト送信して、サーバから返ったデータを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); }
// 共通メソッド //リソースレコードのToString() private string Print(PacketDns p, RrKind rrKind, int n) { var o = p.GetRr(rrKind, n); if (rrKind == RrKind.QD) { return(o.ToString()); } return(Print(o)); }
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())); }
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())); }
public void パケット解釈_str2() { //exercise var sut = new PacketDns(TestUtil.HexStream2Bytes(Str2)); //verify Assert.That(sut.GetId(), Is.EqualTo((short)0x0004)); Assert.That(sut.GetCount(RrKind.QD), Is.EqualTo(1)); Assert.That(sut.GetCount(RrKind.AN), Is.EqualTo(1)); Assert.That(sut.GetCount(RrKind.NS), Is.EqualTo(2)); Assert.That(sut.GetCount(RrKind.AR), Is.EqualTo(2)); Assert.That(sut.GetRcode(), Is.EqualTo((short)0)); Assert.That(sut.GetAa(), Is.EqualTo(false)); Assert.That(sut.GetRd(), Is.EqualTo(true)); Assert.That(sut.GetDnsType(), Is.EqualTo(DnsType.Soa)); Assert.That(sut.GetRequestName(), Is.EqualTo("nifty.com.")); Assert.That(sut.GetRr(RrKind.QD, 0).ToString(), Is.EqualTo((new RrQuery("nifty.com.", DnsType.Soa)).ToString())); Assert.That(sut.GetRr(RrKind.AN, 0).ToString(), Is.EqualTo((new RrSoa("nifty.com.", 0x616, "ons0.nifty.ad.jp", "hostmaster.nifty.ad.jp", 0x0bfe4128, 0xe10, 0x384, 0x36ee80, 0x384)).ToString())); Assert.That(sut.GetRr(RrKind.NS, 0).ToString(), Is.EqualTo((new RrNs("nifty.com.", 0x6d2, "ons0.nifty.ad.jp.")).ToString())); Assert.That(sut.GetRr(RrKind.NS, 1).ToString(), Is.EqualTo((new RrNs("nifty.com.", 0x6d2, "ons1.nifty.ad.jp.")).ToString())); Assert.That(sut.GetRr(RrKind.AR, 0).ToString(), Is.EqualTo((new RrA("ons0.nifty.ad.jp.", 0x712, new Ip("202.248.37.77"))).ToString())); Assert.That(sut.GetRr(RrKind.AR, 1).ToString(), Is.EqualTo((new RrA("ons1.nifty.ad.jp.", 0x6da, new Ip("202.248.20.156"))).ToString())); }
//共通メソッド //リソースレコードの数を表示する private static string Print(PacketDns p) { return(string.Format("QD={0} AN={1} NS={2} AR={3}", p.GetCount(RrKind.QD), p.GetCount(RrKind.AN), p.GetCount(RrKind.NS), p.GetCount(RrKind.AR))); }