Exemplo n.º 1
0
        public void ConnectTest()
        {
            const ushort id        = 100;
            var          requestIp = new Ip("127.0.0.1");
            var          serverIp  = new Ip("127.0.0.1");
            var          mac       = new Mac("11-22-33-44-55-66");
            var          maskIp    = new Ip("255.255.255.0");
            var          gwIp      = new Ip("255.255.255.0");
            var          dnsIp0    = new Ip("255.255.255.0");
            var          dnsIp1    = new Ip("255.255.255.0");
            var          sp        = new PacketDhcp(id, requestIp, serverIp, mac, DhcpType.Discover, 3600, maskIp, gwIp, dnsIp0, dnsIp1, "");

            var bytes = sp.GetBuffer();

            bytes[0] = 1;//Opecode = 2->1

            var rp = Access(bytes);

            Assert.AreEqual(rp.Type, DhcpType.Offer);
        }
Exemplo n.º 2
0
        public void RequestTest(string requestIpStr, string macStr, DhcpType ans)
        {
            const ushort id        = 100;
            var          requestIp = new Ip(requestIpStr);
            var          serverIp  = new Ip("127.0.0.1");
            var          mac       = new Mac(macStr);
            var          maskIp    = new Ip("255.255.255.0");
            var          gwIp      = new Ip("0.0.0.0");
            var          dnsIp0    = new Ip("0.0.0.0");
            var          dnsIp1    = new Ip("0.0.0.0");
            var          sp        = new PacketDhcp(id, requestIp, serverIp, mac, DhcpType.Discover, 3600, maskIp, gwIp, dnsIp0, dnsIp1, "");

            var bytes = sp.GetBuffer();

            bytes[0] = 1;//Opecode = 2->1

            var rp = Access(bytes);

            Assert.AreEqual(rp.Type, ans);
        }
Exemplo n.º 3
0
        PacketDhcp Access(byte [] buf)
        {
            //クライアントソケット生成、及び送信
            var cl = new UdpClient(68);

            cl.Connect((new Ip(IpKind.V4Localhost)).IPAddress, 67); //クライアントのポートが67でないとサーバが応答しない
            cl.Send(buf, buf.Length);

            //受信
            var ep      = new IPEndPoint(0, 0);
            var recvBuf = cl.Receive(ref ep);

            if (recvBuf.Length == 0)
            {
                Assert.Fail();//受信データが無い場合
            }
            var rp = new PacketDhcp();

            rp.Read(recvBuf);

            cl.Close();
            return(rp);
        }