Exemplo n.º 1
0
        /// <summary>把消息写入到数据流中</summary>
        /// <param name="stream">数据流</param>
        /// <param name="context">上下文</param>
        public virtual Boolean Write(Stream stream, Object context)
        {
            stream.WriteByte(Version);
            stream.Write(Token.GetBytes());
            stream.Write((Byte)Command);
            stream.Write(Mac.GetBytes());

            Payload?.CopyTo(stream);

            return(true);
        }
Exemplo n.º 2
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);
        }