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())); }
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); }
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); }
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())); }
//���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); }
//���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); }