void DisplayPacket(DHCPPacket packet) // hien thi goi tin vua nhan ra man hinh { // Hien thi goi tin dhcp vua nhan duoc len man hinh //rtb_DHCPMessage.Text += packet.ToText() + "\r\n"; List <byte[]> Option = packet.optionsplit(); string DHCPType = ""; for (int i = 0; i < Option.Count(); i++) { if (Option[i][0] == 53) { if (Option[i][2] == 1) { DHCPType = "DHCP Discover"; } if (Option[i][2] == 2) { DHCPType = "DHCP Offer"; } if (Option[i][2] == 3) { DHCPType = "DHCP Request"; } if (Option[i][2] == 4) { DHCPType = "DHCP Decline"; } if (Option[i][2] == 5) { DHCPType = "DHCP ACK"; } if (Option[i][2] == 6) { DHCPType = "DHCP NACK"; } if (Option[i][2] == 7) { DHCPType = "DHCP Release"; } if (Option[i][2] == 8) { DHCPType = "DHCP Inform"; } } } // Display dhcp messeage //rtbMess.Text += packet.ToText() + "\r\n"; // d.ToText() la ham tra ve mot string tu DHCPPacket ListViewItem type = new ListViewItem(DHCPType); lv_Message.Items.Add(type); ListViewItem.ListViewSubItem time = new ListViewItem.ListViewSubItem(type, DateTime.Now.ToString()); type.SubItems.Add(time); }
private void btnExtendIP_Click(object sender, EventArgs e) // Extend button { if (haveip) { Send_DHCPRequest(offer_saved, DHCPServer_IP, 2); } else { FileStream fs; try { fs = new FileStream("IP", FileMode.Open); byte[] load = new byte[376]; fs.Read(load, 0, 376); fs.Close(); offer_saved.BytesToDHCPPacket(load); List <byte[]> Option = offer_saved.optionsplit(); for (int j = 0; j < Option.Count(); j++) { if (Option[j][0] == 54) // server dhcp minh da chon { for (int z = 0; z < 4; z++) { DHCPServer_IP[z] = Option[j][z + 2]; } break; } } Send_DHCPRequest(offer_saved, DHCPServer_IP, 3); } catch (Exception z) { MessageBox.Show(z.Message); return; } } }
void HandlePacket(DHCPPacket packet) { // Xu li khi nhan goi DHCP tu server byte[] p = new byte[6]; bool d = xid.Contains(ByteArrayToString(packet.xid)); if (!d) // neu xid khong thuoc cac xid da gui { return; } List <byte[]> Option = packet.optionsplit(); for (int i = 0; i < Option.Count(); i++) { if (Option[i][0] == 53) // dhcp messeage type { if (Option[i][2] == 2) // xac dinh dhcp offer { if (!DHCPServer_IP.SequenceEqual(new byte[] { 0, 0, 0, 0 })) { return; // if we already have choose dhcp server, so not doing anything } for (int j = 0; j < Option.Count(); j++) { if (Option[j][0] == 54) // server dhcp minh da chon { for (int z = 0; z < 4; z++) { DHCPServer_IP[z] = Option[j][z + 2]; } offer_saved = packet; break; } } Send_DHCPRequest(packet, DHCPServer_IP, 1); // gui goi tin request gom goi dhcp offer va dhcp server da chon } else if (Option[i][2] == 5) // Xac dinh day la goi dhcp ack { FileStream fs; try { fs = new FileStream("IP", FileMode.Create); byte[] save = offer_saved.DHCPPacketToBytes(); fs.Write(save, 0, save.Length); fs.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Can not save offer_packet"); } ip = new IPAddress(packet.yiaddr); for (int j = 0; j < Option.Count(); j++) { if (Option[j][0] == 1) { subnetmask = new IPAddress(new byte[] { Option[j][2], Option[j][3], Option[j][4], Option[j][5] }); } if (Option[j][0] == 3) { defaultgateway = new IPAddress(new byte[] { Option[j][2], Option[j][3], Option[j][4], Option[j][5] }); } if (Option[j][0] == 6) { dns = new IPAddress(new byte[] { Option[j][2], Option[j][3], Option[j][4], Option[j][5] }); } if (Option[j][0] == 51) { Int64 epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; Int64 leasetime = BitConverter.ToInt32(new byte[] { Option[j][2], Option[j][3], Option[j][4], Option[j][5] }, 0); time = epoch + leasetime; t1 = epoch + (int)(leasetime * 0.5); t2 = epoch + (int)(leasetime * 0.875); } } haveip = true; } else // Goi tin DHCP Nak { for (int j = 0; j < Option.Count(); j++) { if (Option[j][0] == 54) // server dhcp minh da chon { for (int z = 0; z < 4; z++) { if (DHCPServer_IP[z] != Option[j][z + 2]) // Neu khong phai server dhcp da chon { return; } } Send_DHCPDiscover(); // Chuyen ve trang thai requesting return; } } } return; } } }
void HandlePacket(DHCPPacket packet) { // Phan biet cac goi tin request co phai selecting hay khong bool flag1 = false; // Xu li goi tin dhcp List <byte[]> Option = packet.optionsplit(); for (int i = 0; i < Option.Count(); i++) // dhcp server identify khi goi request_new hoac release { if (Option[i][0] == 54) { if (!network_config.dhcpserver.Equals(new IPAddress(new byte[] { Option[i][2], Option[i][3], Option[i][4], Option[i][5] }))) { return; } flag1 = true; // Neu la goi tin request thi se o trang thai selecting break; } } for (int i = 0; i < Option.Count(); i++) { if (Option[i][0] == 53) { if (Option[i][2] == 1) // xac dinh dhcp discover { xid.Add(ByteArrayToString(packet.xid)); byte[] b_MacAddress = new byte[packet.hlen]; for (int j = 0; j < packet.hlen; j++) { b_MacAddress[j] = packet.chaddr[j]; } string s_MacAddress = ByteArrayToString(b_MacAddress); for (int z = 0; z < table.Count(); z++) { if (s_MacAddress == table[z].macaddress) { Send_DHCPOffer(packet, IPAddress.Parse(table[z].ip)); return; } } IPAddress IP = allocate(); if (IP.Equals(IPAddress.Parse("0.0.0.0"))) // if out of IP { return; } Send_DHCPOffer(packet, IP); } else if (Option[i][2] == 3) // Xac dinh dhcp request { byte[] p = new byte[6]; if (!xid.Contains(ByteArrayToString(packet.xid))) // neu xid khong thuoc cac xid dang giap tiep { return; } IPAddress IP; IP = new IPAddress(packet.ciaddr); // request o dang rebound, ip -> ciaddr if (packet.ciaddr.SequenceEqual(new byte[] { 0, 0, 0, 0 })) // chua co ip -> dhcp dang o dang new { for (int j = 0; j < Option.Count(); j++) { if (Option[j][0] == 50) // request IP address { IP = new IPAddress(new byte[] { Option[j][2], Option[j][3], Option[j][4], Option[j][5] }); break; } } } byte[] b_MacAddress = new byte[packet.hlen]; for (int j = 0; j < packet.hlen; j++) { b_MacAddress[j] = packet.chaddr[j]; } string s_MacAddress = ByteArrayToString(b_MacAddress); Item item = new Item(); item.macaddress = s_MacAddress; item.ip = IP.ToString(); Int64 epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; // setup item item.time = epoch + network_config.leasetime; bool flag2 = false; for (int j = 0; j < table.Count(); j++) { if (table[j].ip == item.ip) { if (table[j].macaddress == item.macaddress) // goi tin dang xin gia han { table.RemoveAt(j); // xoa goi cu flag2 = true; } else { Send_DHCPNak(packet); return; } } } if (flag1 || flag2) // La goi tin dhcp request seleting thi xac nhan, neu la cac goi tin renew, reboot thi phai co san IP trong bang { table.Add(item); // add goi moi vo Send_DHCPAck(packet, IP); // send goi ack } } else // goi dhcp release { IPAddress IP = new IPAddress(packet.ciaddr); byte[] b_MacAddress = new byte[packet.hlen]; for (int j = 0; j < packet.hlen; j++) { b_MacAddress[j] = packet.chaddr[j]; } string s_mac_address = ByteArrayToString(b_MacAddress); for (int j = 0; j < table.Count(); j++) // Duyet ban va xoa di ip { if (table[j].ip == IP.ToString() || table[j].macaddress == s_mac_address) { table.RemoveAt(j); j--; } } } break; } } }