Exemplo n.º 1
0
        public OneLease(Ip ip, Mac mac)
        {
            MacAppointment = true;//MAC指定あり
            Init();

            Ip = ip;
            Mac = mac;
        }
Exemplo n.º 2
0
 //DISCOVER����
 public Ip Discover(Ip requestIp, uint id, Mac mac)
 {
     int i = SearchDiscover(requestIp, id, mac);
     if (i != -1) {
         ar[i].SetReserve(id, mac);
         // ���N�G�X�g���ꂽIP�ȊO���������ꂽ�ꍇ�����
         return ar[i].Ip;
     }
     return null;
 }
Exemplo n.º 3
0
 public void Mac_macStr_で初期化してtoStringで確かめる(string macStr)
 {
     //setUp
     var sut = new Mac(macStr);
     var expected = macStr.ToLower();
     //exercise
     var actual = sut.ToString().ToLower();
     //verify
     Assert.That(actual, Is.EqualTo(expected));
 }
Exemplo n.º 4
0
 public void Equalのテスト12_34_56_78_9A_BCと比較する(string macStr,bool expected)
 {
     //setUp
     var sut = new Mac("12-34-56-78-9A-BC");
     Mac target = null;
     if (macStr != null) {
         target = new Mac(macStr);
     }
     //exercise
     bool actual = sut.Equals(target);
     //verify
     Assert.That(actual, Is.EqualTo(expected));
 }
Exemplo n.º 5
0
 //ToString()を戻すためのコンストラクタ
 public OneLease(string str)
 {
     string[] tmp = str.Split('\t');
     if (tmp.Length != 5) {
         Init();
         return;
     }
     Ip = new Ip(tmp[0]);
     DbStatus = (DhcpDbStatus)(Convert.ToInt32(tmp[1]));
     long ticks = (Convert.ToInt64(tmp[2]));
     Dt = new DateTime(ticks);
     Mac = new Mac(tmp[3]);
     MacAppointment = Convert.ToBoolean(tmp[4]);
 }
Exemplo n.º 6
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.º 7
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.º 8
0
        //public Lease(Logger logger, string fileName, Ip startIp, Ip endIp, int leaseTime, Dat2 macAcl) {
        public Lease(string fileName, Ip startIp, Ip endIp, int leaseTime, Dat macAcl)
        {
            //this.logger = logger;
            this.fileName = fileName;
            this.leaseTime = leaseTime;
            uint start = startIp.AddrV4;
            uint end = endIp.AddrV4;
            int count = 2048;//�ő�ێ���

            for (uint i = start; i <= end && count > 0; i++) {
                Ip ip = new Ip(i);
                ar.Add(new OneLease(ip));//MAC�w��Ȃ�
                count--;
            }

            foreach (var o in macAcl) {
                if (o.Enable) {//�L���ȃf�[�^������Ώۂɂ���
                    string macStr = o.StrList[0];//MAC�A�h���X(99-99-99-99-99-99)
                    Mac mac = new Mac(macStr);
                    Ip ip = new Ip(o.StrList[1]);//IP�A�h���X
                    if (ip.ToString() == "255.255.255.255") {
                        ar.Add(new OneLease(ip, mac));//MAC�w�肠��őS���lj�
                    } else {

                        // ��{�ݒ�͈̔͂̃e�[�u�������
                        bool find = false;
                        for (int i = 0; i < ar.Count; i++) {
                            if (ar[i].Ip == ip) {
                                ar[i] = new OneLease(ip, mac);//MAC�w�肠��ɕύX
                                find = true;
                                break;
                            }
                        }
                        if (!find) { // ��{�ݒ�͈̔͊O�̏ꍇ
                            ar.Add(new OneLease(ip, mac));//MAC�w�肠��Ƃ��Ēlj�
                        }
                    }
                }
            }
            // ���[�X���f�[�^�̓ǂݍ���
            Read();
        }
Exemplo n.º 9
0
        // ���[�X���̃f�[�^�̓ǂݍ���
        void Read()
        {
            if (!File.Exists(fileName))
                return;
            using (StreamReader sr = new StreamReader(fileName, Encoding.ASCII)) {
                while (true) {
                    string str = sr.ReadLine();
                    if (str == null)
                        break;
                    string[] tmp = str.Split('\t');
                    if (tmp.Length == 4) {
                        try {
                            Ip ip = new Ip(tmp[0]);
                            DateTime dt = new DateTime(Convert.ToInt64(tmp[1]));
                            uint id = Convert.ToUInt32(tmp[2]);
                            Mac mac = new Mac(tmp[3]);
                            for (int i = 0; i < ar.Count; i++) {
                                if (ar[i].Ip == ip) {
                                    if (ar[i].MacAppointment && ar[i].Mac != mac) {
                                        break;
                                    }
                                    ar[i].SetUsed(id, mac, dt);
                                    break;
                                }
                            }
                        } catch {

                        }

                    }
                }
                sr.Close();
            }
        }
Exemplo n.º 10
0
 void Init()
 {
     DbStatus = DhcpDbStatus.Unused;
     Id = 0;
     Dt = new DateTime(0);
     if (!MacAppointment)//MAC指定無しの場合、MACも初期化する
         Mac = new Mac("ff-ff-ff-ff-ff-ff");
 }
Exemplo n.º 11
0
 //USEDの設定
 public void SetUsed(uint id, Mac mac, DateTime dt)
 {
     DbStatus = DhcpDbStatus.Used;
     Id = id;
     Mac = mac;
     Dt = dt;
 }
Exemplo n.º 12
0
 void Log(SockUdp sockUdp,int messageNo, Mac mac,Ip ip,DhcpType type)
 {
     string macStr = mac.ToString();
     foreach (var m in _macAcl) {
         if (m.StrList[0].ToUpper() == mac.ToString()) {
             macStr = string.Format("{0}({1})",mac,m.StrList[2]);
             break;
         }
     }
     Logger.Set(LogKind.Detail, sockUdp, messageNo, string.Format("{0} {1} {2}", macStr, (ip == null) ? "0.0.0.0" : ip.ToString(), type.ToString()));
 }
Exemplo n.º 13
0
 public void ToStringTest(string macStr)
 {
     var target = new Mac(macStr);
     Assert.AreEqual(target.ToString(), macStr.ToUpper());
 }
Exemplo n.º 14
0
 //MAC�w��݂̂̏ꍇ�A�f�[�^�x�[�X�ɑ��݂��邩�ǂ�����m�F����
 public bool SearchMac(Mac mac)
 {
     for (int i = 0; i < ar.Count; i++) {
         if (ar[i].MacAppointment && ar[i].Mac == mac)
             return true;
     }
     return false;
 }
Exemplo n.º 15
0
        //REQUEST����
        public Ip Request(Ip requestIp, uint id, Mac mac)
        {
            int i = SearchRequest(requestIp, id);

            if (i != -1) {

                //����MAC�ł��łɎg�p���̂�̂�����Δj������
                for (int n = 0; n < ar.Count; n++) {
                    if (n == i)
                        continue;
                    if (ar[n].Mac == mac)
                        ar[n].SetUnuse();
                }

                ar[i].SetUsed(id, mac, DateTime.Now.AddSeconds(leaseTime));
                Save();// ���[�X���̃f�[�^��ۑ�

                // ���N�G�X�g���ꂽIP�ȊO���������ꂽ�ꍇ�����
                return ar[i].Ip;
            }
            return null;
        }
Exemplo n.º 16
0
 //RELEASE����
 public Ip Release(Mac mac)
 {
     // ���Y�f�[�^�x�[�X�̌���
     for (int i = 0; i < ar.Count; i++) {
         if (ar[i].Mac == mac) {
             ar[i].SetUnuse();
             Save();// ���[�X���̃f�[�^��ۑ�
             return ar[i].Ip;
         }
     }
     return null;
 }
Exemplo n.º 17
0
        //���M�p�P�b�g�p�̃R���X�g���N�^
        public PacketDhcp(uint id,Ip requestIp,Ip serverIp,Mac mac,DhcpType dhcpType,int leaseTime,Ip maskIp,Ip gwIp,Ip dnsIp0,Ip dnsIp1,string wpadUrl)
        {
            //public PacketDhcp(uint id,Ip requestIp,Ip serverIp,Mac mac,DHCP_TYPE dhcpType) {

            //Dmy init
            _headerDhcp.HopCount = 0;
            _headerDhcp.NumberOfSecounds = 0;
            _headerDhcp.Flags = 0;
            _headerDhcp.ClientIp = 0;
            _headerDhcp.GatewayIp = 0;

            _headerDhcp.ClientHwAddr = new byte[16];
            _headerDhcp.ServerHostName = new String((char)0,64);
            _headerDhcp.BootFile = new String((char)0, 128);

            _headerDhcp.Opcode = 0x02;// �����p�P�b�g
            _headerDhcp.HwType = 0x01;
            _headerDhcp.HwAddrLen = 0x06;
            _headerDhcp.TransactionId = id;
            _headerDhcp.MagicCookie = 0x63538263;
            if(requestIp!=null)
                _headerDhcp.YourIp = Util.htonl(requestIp.AddrV4);
            _headerDhcp.ServerIp = Util.htonl(serverIp.AddrV4);
            Buffer.BlockCopy(mac.GetBytes(),0,_headerDhcp.ClientHwAddr,0,6);

            //�I�v�V����
            _option = new byte[1];
            _option[0] = 0xFF;//�I�[�|�C���^��Z�b�g

            byte[] buf;//�I�v�V�����lj����̃e���|����
            //if (dhcpType != DHCP_TYPE.INFRM) {
            if (dhcpType == DhcpType.Ack || dhcpType == DhcpType.Offer) {
            // ERR if (dhcpType == DHCP_TYPE.OFFER) {
                //leaseTime
                double d = leaseTime/0x2;
                uint i = (uint)d;
                buf = BitConverter.GetBytes(Util.htonl(i));
                SetOptions(0x3a,buf);//Renewal Time

                d = leaseTime*0.85;
                i = (uint)d;
                buf = BitConverter.GetBytes(Util.htonl(i));
                SetOptions(0x3b,buf);//Rebinding Time

                i = (uint)leaseTime;
                buf = BitConverter.GetBytes(Util.htonl(i));
                SetOptions(0x33,buf);//Lease Time

                //serverIp
                byte [] dat = BitConverter.GetBytes(Util.htonl(serverIp.AddrV4));
                SetOptions(0x36,dat);

                //maskIp
                buf = BitConverter.GetBytes(Util.htonl(maskIp.AddrV4));
                SetOptions(0x01,buf);

                //gwIp
                buf = BitConverter.GetBytes(Util.htonl(gwIp.AddrV4));
                SetOptions(0x03, buf);

                //dnsIp0
                buf = new byte[0];
                if(dnsIp0!=null){
                    buf = BitConverter.GetBytes(Util.htonl(dnsIp0.AddrV4));
                }
                if(dnsIp1!=null){
                    byte [] tmp = BitConverter.GetBytes(Util.htonl(dnsIp1.AddrV4));
                    buf = Bytes.Create(buf,tmp);
                }
                if(buf.Length!=0){
                    SetOptions(0x06,buf);
                }
                //wpad
                if(wpadUrl != null) {
                    byte[] tmp = Encoding.ASCII.GetBytes(wpadUrl);
                    SetOptions(252,tmp);
                }

            }

            buf = new byte[] { 0 };
            switch (dhcpType) {
                case DhcpType.Discover:
                    buf[0] = 0x01;
                    break;
                case DhcpType.Offer:
                    buf[0] = 0x02;
                    break;
                case DhcpType.Request:
                    buf[0] = 0x03;
                    break;
                case DhcpType.Decline:
                    buf[0] = 0x04;
                    break;
                case DhcpType.Ack:
                    buf[0] = 0x05;
                    break;
                case DhcpType.Nak:
                    buf[0] = 0x06;
                    break;
                case DhcpType.Release:
                    buf[0] = 0x07;
                    break;
                case DhcpType.Infrm:
                    buf[0] = 0x08;
                    break;

            }
            SetOptions(0x35, buf);
        }
Exemplo n.º 18
0
 //RESERVEの設定
 public void SetReserve(uint id, Mac mac)
 {
     DbStatus = DhcpDbStatus.Reserve;
     Id = id;
     Dt = DateTime.Now.AddSeconds(5);//5秒間有効
     Mac = mac;
 }
Exemplo n.º 19
0
        int SearchDiscover(Ip ip, uint id, Mac mac)
        {
            //���ł�DISCOVER��󂯂ă��U�[�u��Ԃ̃f�[�^������ꍇ�́A����������Ԃ�
            for (int i = 0; i < ar.Count; i++) {
                if (ar[i].DbStatus == DhcpDbStatus.Reserve && ar[i].Id == id) {
                    return i;
                }
            }

            //MAC�w��̃f�[�^��D�悵�Č�������
            for (int i = 0; i < ar.Count; i++) {
                if (ar[i].MacAppointment && ar[i].Mac == mac) {
                    if (ar[i].Ip.ToString() == "255.255.255.255") {
                        goto next;//
                    }
                    return i;
                }
            }

            // ����MAC�̃f�[�^������΁A�����̃f�[�^��j�����ă��[�X�ΏۂƂ���
            for (int i = 0; i < ar.Count; i++) {
                if (ar[i].Mac == mac) {
                    ar[i].SetUnuse();// �ˑ��f�[�^��N���A
                    return i;
                }
            }
            //�v���h�o�������Ă���ꍇ�́A���[�X�Ώۂɂ���
            for (int i = 0; i < ar.Count; i++) {
                if (!ar[i].MacAppointment && ar[i].DbStatus == DhcpDbStatus.Unused && ar[i].Ip == ip) {
                    return i;
                }
            }
            next:
            //IP�͂Ȃ�ł�����̂ŋ󂢂Ă����̂�Ώۂɂ���
            for (int i = 0; i < ar.Count; i++) {
                ar[i].Refresh();//���Ԓ��߂��Ă���f�[�^�͏���������
                if (!ar[i].MacAppointment && ar[i].DbStatus == DhcpDbStatus.Unused) {
                    return i;
                }
            }
            return -1;
        }