public void Start(IPAddress ipAddress, string replyIP, string replyIPv6) { UDPListener listener = new UDPListener(AddressFamily.InterNetwork); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 53); if (String.Equals(ipAddress.AddressFamily.ToString(), "InterNetworkV6")) { listener = new UDPListener(AddressFamily.InterNetworkV6); } listener.Client.Bind(ipEndPoint); while (true) { try { byte[] receiveBuffer = listener.Receive(ref ipEndPoint); ProcessRequest(receiveBuffer, listener, ipEndPoint, replyIP, replyIPv6); } catch (Exception ex) { OutputError(ex); } } }
protected override void ProcessRequest(byte[] data, UDPListener udpListener, IPEndPoint ipEndPoint, string replyIP, string replyIPv6) { string clientIP = ipEndPoint.Address.ToString(); MDNSPacket packet = new MDNSPacket(data); if (packet.Header.IsQuery()) { if (Check(packet.Question.Name, packet.Question.QuestionType, packet.Question.Type, clientIP, out string message)) { if (packet.Question.QuestionType.Equals("QM") && !this.UnicastOnly && string.Equals(ipEndPoint.Address.AddressFamily.ToString(), "InterNetwork")) { ipEndPoint.Address = IPAddress.Parse("224.0.0.251"); } else if (packet.Question.QuestionType.Equals("QM") && !this.UnicastOnly && string.Equals(ipEndPoint.Address.AddressFamily.ToString(), "InterNetworkV6")) { ipEndPoint.Address = IPAddress.Parse("ff02::fb"); } byte[] buffer = packet.GetBytes(this.TTL, replyIP, replyIPv6); SendTo(buffer, udpListener, ipEndPoint); } Output("mDNS", clientIP, packet.Question.Name, packet.Question.QuestionType, packet.Question.Type, message); } }
public new void Start(IPAddress ipAddress, string replyIP, string replyIPv6) { UDPListener listener = new UDPListener(AddressFamily.InterNetwork); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 5353); if (string.Equals(ipAddress.AddressFamily.ToString(), "InterNetwork")) { listener.JoinMulticastGroup(IPAddress.Parse("224.0.0.251"), ipAddress); } else { listener = new UDPListener(AddressFamily.InterNetworkV6); listener.JoinMulticastGroup(IPAddress.Parse("ff02::fb")); } listener.Client.Bind(ipEndPoint); while (true) { try { byte[] receiveBuffer = listener.Receive(ref ipEndPoint); ProcessRequest(receiveBuffer, listener, ipEndPoint, replyIP, replyIPv6); } catch (Exception ex) { OutputError(ex); } } }
protected void ProcessRequest(byte[] data, UDPListener udpListener, IPEndPoint ipEndPoint, string replyIP) { string clientIP = ipEndPoint.Address.ToString(); NetBIOSNSPacket packet = new NetBIOSNSPacket(data); if (packet.Header.IsQuery()) { if (Check(packet.Question.Name, packet.Question.Type, clientIP, out string message)) { byte[] buffer = packet.GetBytes(this.TTL, replyIP); SendTo(buffer, udpListener, ipEndPoint); } Output("NBNS", clientIP, packet.Question.Name, packet.Question.Type, message); } }
public void Start(IPAddress ipAddress, string replyIP) { UDPListener listener = new UDPListener(AddressFamily.InterNetwork); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 137); listener.Client.Bind(ipEndPoint); while (true) { try { byte[] receiveBuffer = listener.Receive(ref ipEndPoint); ProcessRequest(receiveBuffer, listener, ipEndPoint, replyIP); } catch (Exception ex) { OutputError(ex); } } }
public void Start(IPAddress ipAddress, string mac, string dnsIPv6) { UDPListener listener = new UDPListener(AddressFamily.InterNetworkV6); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 547); listener.JoinMulticastGroup(IPAddress.Parse("ff02::1:2")); listener.Client.Bind(ipEndPoint); while (true) { try { byte[] receiveBuffer = listener.Receive(ref ipEndPoint); ProcessRequest(receiveBuffer, listener, ipEndPoint, mac, dnsIPv6); } catch (Exception ex) { Console.WriteLine(ex); } } }
protected virtual void ProcessRequest(byte[] data, UDPListener udpListener, IPEndPoint ipEndPoint, string replyIP, string replyIPv6) { string clientIP = ipEndPoint.Address.ToString(); DNSPacket packet = new DNSPacket(data) { Host = this.Host, TTL = this.TTL }; if (packet.Header.IsQuery()) { if (!packet.Header.IsDynamicUpdateRequest()) { if (Check(packet.Question.Name, packet.Question.Type, clientIP, out string message)) { byte[] buffer; buffer = packet.GetBytes(this.TTL, this.Serial, replyIP, replyIPv6); SendTo(buffer, udpListener, ipEndPoint); } Output("DNS", clientIP, packet.Question.Name, packet.Question.Type, message); } else { byte[] flags = new byte[2] { 0xa8, 0x05 }; byte[] dnsPayload = new byte[data.Length - 2]; System.Buffer.BlockCopy(data, 2, dnsPayload, 0, dnsPayload.Length); MemoryStream dnsMemoryStream = new MemoryStream(); dnsMemoryStream.Write(data, 0, data.Length); dnsMemoryStream.Position = 2; dnsMemoryStream.Write(flags, 0, 2); SendTo(dnsMemoryStream.ToArray(), udpListener, ipEndPoint); } } }
protected virtual void ProcessRequest(byte[] data, UDPListener udpListener, IPEndPoint ipEndPoint, string listenerMAC, string dnsIPv6) { string clientIP = ipEndPoint.Address.ToString(); DHCPv6Packet packet = new DHCPv6Packet(data); if (packet.Message?.MsgType == 1 || packet.Message?.MsgType == 3 || packet.Message?.MsgType == 5) { bool isMicrosoft = false; if (packet.Option16?.EnterpriseNumber == 311) { isMicrosoft = true; } byte msgType = 0; string leaseIP = ""; switch (packet.Message.MsgType) { case 1: msgType = 2; break; case 3: { byte[] renewIP = new DHCPv6Option5(packet.Option3.IANAOptions).IPv6Address; leaseIP = new IPAddress(renewIP).ToString(); msgType = 7; } break; case 5: { byte[] renewIP = new DHCPv6Option5(packet.Option3.IANAOptions).IPv6Address; leaseIP = new IPAddress(renewIP).ToString(); msgType = 7; } break; } DHCPv6DUIDLL duid = new DHCPv6DUIDLL(packet.Option1.DUID); byte[] clientMACData = new DHCPv6DUIDLL(packet.Option1.DUID).LinkLayerAddress; if (duid.DUIDType == 1) { clientMACData = new DHCPv6DUIDLLT(packet.Option1.DUID).LinkLayerAddress; } string clientMAC = BitConverter.ToString(clientMACData).Replace("-", ":"); string clientHostName = ""; if (!String.IsNullOrEmpty(packet.Option39?.DomainName)) { clientHostName = packet.Option39.DomainName; } if (Check(clientMAC, clientHostName, listenerMAC, isMicrosoft, out string message)) { if (msgType == 2) { leaseIP = "fe80::" + this.Prefix + ":" + this.Index; this.Index++; } byte[] buffer = new DHCPv6Packet().GetBytes(msgType, leaseIP, listenerMAC, dnsIPv6, this.DNSSuffix, this.Lifetime, packet); SendTo(buffer, udpListener, ipEndPoint); } Output(packet.Message.MsgType, leaseIP, clientIP, clientMAC, clientHostName, message); } }
protected virtual void SendTo(byte[] data, UDPListener udpListener, IPEndPoint ipEndPoint) { udpListener.Client.SendTo(data, ipEndPoint); }