static void Request(DHCPRequest dhcpRequest, IPAddress targetIp, string interfaceIp) { try { var type = dhcpRequest.GetMsgType(); var replyOptions = new DHCPReplyOptions(); replyOptions.SubnetMask = IPAddress.Parse("255.255.255.0"); replyOptions.ServerIdentifier = IPAddress.Parse(interfaceIp); if (type == DHCPMsgType.DHCPDISCOVER) { Console.WriteLine($"Received discovery, sending offer!"); dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPOFFER, targetIp, replyOptions); } else if (type == DHCPMsgType.DHCPREQUEST) { Console.WriteLine($"Received request, sending ack!"); dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPACK, targetIp, replyOptions); } else { Console.WriteLine($"Received unknown DHCP type {type}"); } } catch (Exception ex) { Console.WriteLine(ex); } }
static void informHandler(DHCPRequest dhcpRequest) { String mac = Util.bytesToMac(dhcpRequest.GetChaddr()); //TODO: implement this method sendNotify(DhcpNotify.NotifyType.INFORM, String.Format("RTDhcpServer receives DHCP inform message from {0}", mac)); }
static void declineHandler(DHCPRequest dhcpRequest) { String mac = Util.bytesToMac(dhcpRequest.GetChaddr()); pool.release(mac); pool.storeDb(); sendNotify(DhcpNotify.NotifyType.DECLINE, String.Format("RTDhcpServer receives DHCP decline from {0}", mac)); }
static void discoverHandler(DHCPRequest dhcpRequest) { DHCPMsgType type = dhcpRequest.GetMsgType(); String mac = Util.bytesToMac(dhcpRequest.GetChaddr()); IPAddress ip = pool.offerIP(mac, dhcpRequest.GetRequestedIP()); DHCPReplyOptions options = defaultOptions.clone(); options.ServerIdentifier = localAddress; dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPOFFER, ip, defaultOptions); sendNotify(DhcpNotify.NotifyType.DISCOVER, String.Format("RTDhcpServer receives DHCP discover message from {0}, offer {1}", mac, ip)); }
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)); } } }
static void Request(DHCPRequest dhcpRequest) { try { var type = dhcpRequest.GetMsgType(); var mac = ByteArrayToString(dhcpRequest.GetChaddr()); // IP for client IPAddress ip; if (!leases.TryGetValue(mac, out ip)) { ip = new IPAddress(new byte[] { 192, 168, 1, nextIP++ }); leases[mac] = ip; } Console.WriteLine(type.ToString() + " request from " + mac + ", it will be " + ip.ToString()); var options = dhcpRequest.GetAllOptions(); Console.Write("Options:"); foreach (DHCPOption option in options.Keys) { Console.WriteLine(option.ToString() + ": " + ByteArrayToString(options[option])); } // Lets show some request info var requestedOptions = dhcpRequest.GetRequestedOptionsList(); if (requestedOptions != null) { Console.Write("Requested options:"); foreach (DHCPOption option in requestedOptions) { Console.Write(" " + option.ToString()); } Console.WriteLine(); } // Option 82 info var relayInfoN = dhcpRequest.GetRelayInfo(); if (relayInfoN != null) { var relayInfo = (RelayInfo)relayInfoN; if (relayInfo.AgentCircuitID != null) { Console.WriteLine("Relay agent circuit ID: " + ByteArrayToString(relayInfo.AgentCircuitID)); } if (relayInfo.AgentRemoteID != null) { Console.WriteLine("Relay agent remote ID: " + ByteArrayToString(relayInfo.AgentRemoteID)); } } Console.WriteLine(); var replyOptions = new DHCPReplyOptions(); // Options should be filled with valid data. Only requested options will be sent. replyOptions.SubnetMask = IPAddress.Parse("255.255.255.0"); replyOptions.DomainName = "SharpDHCPServer"; replyOptions.ServerIdentifier = IPAddress.Parse("10.0.0.1"); replyOptions.RouterIP = IPAddress.Parse("10.0.0.1"); replyOptions.DomainNameServers = new IPAddress[] { IPAddress.Parse("192.168.100.2"), IPAddress.Parse("192.168.100.3") }; // Some static routes replyOptions.StaticRoutes = new NetworkRoute[] { new NetworkRoute(IPAddress.Parse("10.0.0.0"), IPAddress.Parse("255.0.0.0"), IPAddress.Parse("10.0.0.1")), new NetworkRoute(IPAddress.Parse("192.168.0.0"), IPAddress.Parse("255.255.0.0"), IPAddress.Parse("10.0.0.1")), new NetworkRoute(IPAddress.Parse("172.16.0.0"), IPAddress.Parse("255.240.0.0"), IPAddress.Parse("10.0.0.1")), new NetworkRoute(IPAddress.Parse("80.252.130.248"), IPAddress.Parse("255.255.255.248"), IPAddress.Parse("10.0.0.1")), new NetworkRoute(IPAddress.Parse("80.252.128.88"), IPAddress.Parse("255.255.255.248"), IPAddress.Parse("10.0.0.1")), }; // Lets send reply to client! if (type == DHCPMsgType.DHCPDISCOVER) { dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPOFFER, ip, replyOptions); } if (type == DHCPMsgType.DHCPREQUEST) { dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPACK, ip, replyOptions); } } catch (Exception ex) { Console.WriteLine(ex); } }