private static void SendMDNSNameReply() { if (Adapter.IPAddress == null || Networking.Adapter.Name == string.Empty) { return; } prefix.Overwrite(0, new byte[6] { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb }); prefix.Overwrite(6, Adapter.MacAddress); // Source MAC Address prefix.Overwrite(26, Adapter.IPAddress); // Source IP Address lock (lockObj) { // Set the all-important name and IP address -- that's the whole purpose here...! suffix.Overwrite(10, Adapter.IPAddress); var result = Utility.CombineArrays(prefix, Utility.CombineArrays(DNS.EncodeDnsName(Networking.Adapter.Name + ".local"), suffix)); result.Overwrite(16, ((ushort)(result.Length - 14)).ToBytes()); // Set IPv4 message size result.Overwrite(38, ((ushort)(result.Length - 34)).ToBytes()); // Set UDP message size result.Overwrite(24, new byte[] { 0x00, 0x00 }); // clear header checksum, so that calc excludes the checksum itself result.Overwrite(24, result.InternetChecksum(20, 14)); // header checksum result.Overwrite(40, new byte[] { 0x00, 0x00 }); // clear UDP Checksum Debug.WriteLine("Sending MDNS name message! "); Adapter.nic.SendFrame(result); // Send the packet out into the ether....! } }
/// <summary> /// /// </summary> private static void SendLLMNRNameReply(string name, byte[] tranID, byte[] destinationMac, byte[] destinationIP, byte[] destinationPort) { if (Adapter.IPAddress == null || Networking.Adapter.Name == string.Empty) { return; } lock (lockObj) { prefix.Overwrite(0, destinationMac); prefix.Overwrite(6, Adapter.MacAddress); // Source MAC Address prefix.Overwrite(26, Adapter.IPAddress); // Source IP Address prefix.Overwrite(30, destinationIP); prefix.Overwrite(36, destinationPort); prefix.Overwrite(42, tranID); var suffix = new byte[name.Length * 2 + 22]; var byteName = Utility.CombineArrays(DNS.EncodeDnsName(name), new byte[4] { 0x00, 0x01, 0x00, 0x01 }); suffix.Overwrite(0, Utility.CombineArrays(byteName, byteName)); suffix.Overwrite(suffix.Length - 7, new byte[1] { 0x1e }); // Time To Live (30 seconds) suffix.Overwrite(suffix.Length - 5, new byte[1] { 0x04 }); // IP Address length suffix.Overwrite(suffix.Length - 4, Adapter.IPAddress); var result = Utility.CombineArrays(prefix, suffix); result.Overwrite(16, ((ushort)(result.Length - 14)).ToBytes()); // Set IPv4 message size result.Overwrite(38, ((ushort)(result.Length - 34)).ToBytes()); // Set UDP message size result.Overwrite(24, new byte[] { 0x00, 0x00 }); // clear header checksum, so that calc excludes the checksum itself result.Overwrite(24, result.InternetChecksum(20, 14)); // header checksum result.Overwrite(40, new byte[] { 0x00, 0x00 }); // clear UDP Checksum Adapter.nic.SendFrame(result); // Send the packet out into the ether....! Debug.WriteLine("LLMNR Response sent"); } }
/// <summary> /// Seperate send method to share a lock so that we don't mix up packets... /// </summary> /// <param name="packetType"></param> internal static void SendMessage(byte[] packetType) { lock (MagicCookie) { scratch.Overwrite(0, Adapter.BroadcastMAC); // Destination MAC Address scratch.Overwrite(6, Adapter.MacAddress); // Source MAC Address scratch.Overwrite(30, Adapter.BroadcastIPAddress); // Destiantion IP Address scratch.Overwrite(54, Adapter.BlankIPAddress); // Source IP Address scratch.Overwrite(70, Adapter.MacAddress); // Source MAC Address Again inside the DHCP section scratch.Overwrite(284, packetType); byte[] options = new byte[13 + (Adapter.Name == string.Empty ? 0 : Adapter.Name.Length + 2)]; options.Overwrite(0, Adapter.MacAddress); transactionID = transactionID ?? Extensions.GetRandomBytes(4); // Make up some transaction ID if (packetType == DHCP.Discover) { // Debug.WriteLine("Composing Discover Message"); PendingIpAddress = null; //scratch.Overwrite(306, suffix); // write the Discover suffix options.Overwrite(6, new byte[6] { 0x33, 0x04, 0x00, 0x76, 0xa7, 0x00 }); // Request an IP lease time of 90 days } else if (packetType == DHCP.Request && PendingIpAddress != null && Adapter.Gateway != null) { //Debug.WriteLine("Composing Request Message"); options.Overwrite(6, new byte[2] { 0x32, 0x04 }); // Set the option prefix for IP Address options.Overwrite(8, PendingIpAddress); // Set the option Value options.Overwrite(12, new byte[2] { 0x36, 0x04 }); // Set the option prefix for Gateway options.Overwrite(14, Adapter.Gateway); // Set the option value if (Adapter.GatewayMac != null && Adapter.Gateway != null && Adapter.IPAddress != null) { // This is for the renewals //TODO: Test to make sure this works! //scratch.Overwrite(0, Adapter.GatewayMac); //scratch.Overwrite(30, Adapter.Gateway); scratch.Overwrite(54, Adapter.IPAddress); } } else { Debug.WriteLine("Odd DHCP situation... should we be concerned?"); return; } if (Adapter.Name != string.Empty) { // Add Hostname option to Discover and Request messages options.Overwrite(options.Length - (Adapter.Name.Length + 3), new byte[1] { 0x0c }); options.Overwrite(options.Length - (Adapter.Name.Length + 2), DNS.EncodeDnsName(Adapter.Name)); } options.Overwrite(options.Length - 1, new byte[1] { 0xFF }); // End of option section marker scratch.Overwrite(46, transactionID); // Write the transaction ID var result = Utility.CombineArrays(scratch, options); result.Overwrite(16, ((ushort)(result.Length - 14)).ToBytes()); // Set IPv4 message size result.Overwrite(38, ((ushort)(result.Length - 34)).ToBytes()); // Set UDP message size result.Overwrite(24, new byte[] { 0x00, 0x00 }); // clear header checksum, so that calc excludes the checksum itself result.Overwrite(24, result.InternetChecksum(20, 14)); // header checksum result.Overwrite(40, new byte[] { 0x00, 0x00 }); // clear UDP Checksum Adapter.nic.SendFrame(result); // Send the packet out into the ether....! } }