private void retriveDb() { if (!Directory.Exists(DB_PATH)) { Directory.CreateDirectory(DB_PATH); } if (File.Exists(DB_FILE)) { String[] lines = File.ReadAllLines(DB_FILE); foreach (String line in lines) { try { IpPoolItem item = IpPoolItem.valueOf(line); item.status = IpPoolItem.Status.ACKED; lease.Add(item.mac, item); } catch { Util.log("retrieve dhcp data fail: " + line); } } Util.log("retrieve dhcp data, count " + lease.Count); } else { Util.log("no dhcp data file"); File.WriteAllText(DB_FILE, ""); } }
public static IpPoolItem valueOf(String s) { String[] ss = s.Split(';'); IpPoolItem item = new IpPoolItem(ss[0], IPAddress.Parse(ss[1]), long.Parse(ss[2])); return(item); }
public IpPoolItem getPoolItem(String mac) { IpPoolItem item = null; if (!lease.TryGetValue(mac, out item)) { return(null); } return(item); }
public IPAddress ackIP(String mac) { IpPoolItem item = null; if (!lease.TryGetValue(mac, out item)) //already allocate address for this mac { return(null); } item.status = IpPoolItem.Status.ACKED; return(item.ip); }
public void release(String mac) { IpPoolItem item = null; if (lease.TryGetValue(mac, out item)) { lease.Remove(mac); item.clearTick(); item.mac = null; item.status = IpPoolItem.Status.NULL; released.Enqueue(item); //storeDb(); } }
static void requestHandler(DHCPRequest dhcpRequest) { String mac = Util.bytesToMac(dhcpRequest.GetChaddr()); IPAddress si = new IPAddress(dhcpRequest.GetOptionData(DHCPOption.ServerIdentifier)); if (si != null && si.Equals(localAddress)) //client select this server { IPAddress requestIp = new IPAddress(dhcpRequest.GetOptionData(DHCPOption.RequestedIPAddress)); IPAddress ip = pool.ackIP(mac); if (ip != null) { pool.storeDb(); dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPACK, ip, defaultOptions); sendNotify(DhcpNotify.NotifyType.REQUEST, String.Format("RTDhcpServer receives DHCP request message from {0}, ack {1}", mac, ip)); } /* * IPAddress ip = pool.getIP(mac); * if (requestIp!=null && ip.Equals(requestIp)) * { * pool.storeDb(); * dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPACK, ip, options); * sendNotify(DhcpNotify.NotifyType.REQUEST, String.Format("RTDhcpServer receives DHCP request message from {0}, ack {1}", mac, ip)); * } else * { * dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPNAK, requestIp, options); * pool.release(mac); * sendNotify(DhcpNotify.NotifyType.REQUEST, String.Format("RTDhcpServer receives DHCP request message from {0}, nack {1}", mac, requestIp)); * } */ } else { IpPoolItem item = pool.getPoolItem(mac); if (item != null && item.status == IpPoolItem.Status.ACKED) //reuse previous allocated address { IPAddress ip = item.ip; pool.storeDb(); dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPACK, ip, defaultOptions); sendNotify(DhcpNotify.NotifyType.REQUEST, String.Format("RTDhcpServer receives DHCP request(reuse) message from {0}, ack {1}", mac, ip)); } else //not selected by client { pool.release(mac); sendNotify(DhcpNotify.NotifyType.REQUEST, String.Format("RTDhcpServer receives DHCP request message from {0}, not selected by this client", mac)); } } }
public IPAddress offerIP(String mac, IPAddress preferIp) { IpPoolItem item = null; if (!lease.TryGetValue(mac, out item)) //already allocate address for this mac { if (preferIp != null) { if (!containsIp(preferIp)) { item = new IpPoolItem(preferIp); } } if (item == null && released.Count > 0) //is there released address to be reused { lock (released) { item = released.Dequeue(); } } if (item == null) //find an available address { IPAddress ip = null; ip = getAvailableAddress(); item = new IpPoolItem(ip); } item.mac = mac; item.setTick(); item.status = IpPoolItem.Status.OFFERED; lease[mac] = item; //storeDb(); } return(item.ip); }