using System; using System.IO; using System.Net; using System.Net.NetworkInformation;…
- User
using System; using System.IO; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Collections.Generic; using System.Diagnostics;
namespace UDPPacketPortList { class Program { static HashSet seenDestPorts = new HashSet();
static void Main(string[] args) { Console.WriteLine("=== UDP Destination Port Capture Tool ==="); // Get the IP address of the active network adapter string ip = GetActiveIPAddress(); if (ip == null) { Console.WriteLine("No active network adapter found. Exiting..."); return; } Console.WriteLine($"Active IP Address: {ip}"); // Find the process ID of "HD-Player.exe" int pid = GetProcessIdByName("HD-Player"); if (pid == 0) { Console.WriteLine("'HD-Player.exe' is not running. Exiting..."); return; } Console.WriteLine($"'HD-Player.exe' Process ID: {pid}"); // Start capturing UDP ports and data sizes for this application try { Console.WriteLine("Starting UDP destination port capture for 'HD-Player.exe'..."); CaptureUDPPorts(ip, pid); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } // Get the active IP address of the system static string GetActiveIPAddress() { foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) { if (adapter.OperationalStatus == OperationalStatus.Up && adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback) { foreach (UnicastIPAddressInformation ipInfo in adapter.GetIPProperties().UnicastAddresses) { if (ipInfo.Address.AddressFamily == AddressFamily.InterNetwork) { return ipInfo.Address.ToString(); } } } } return null; } // Get the Process ID for a given process name static int GetProcessIdByName(string processName) { try { Process[] processes = Process.GetProcessesByName(processName); if (processes.Length > 0) { return processes[0].Id; // Return the first match } else { return 0; // Process not found } } catch (Exception ex) { Console.WriteLine($"Error retrieving process ID: {ex.Message}"); return 0; } } // Capture UDP ports and data for the given process ID static void CaptureUDPPorts(string ip, int targetPid) { try { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(ip), 0); sock.Bind(ipEndPoint); sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true); byte[] inValue = { 1, 0, 0, 0 }; byte[] outValue = { 1, 1, 1, 1 }; sock.IOControl(IOControlCode.ReceiveAll, inValue, outValue); byte[] buffer = new byte[65000]; while (true) { try { int received = sock.Receive(buffer); MemoryStream ms = new MemoryStream(buffer, 0, received); BinaryReader reader = new BinaryReader(ms); ParseUDPPorts(reader, received, targetPid); } catch (Exception ex) { Console.WriteLine($"Packet parsing error: {ex.Message}"); } } } catch (Exception ex) { Console.WriteLine($"Socket error: {ex.Message}"); } } // Parse UDP ports and check if they belong to the "HD-Player.exe" process static void ParseUDPPorts(BinaryReader reader, int receivedBytes, int targetPid) { // Skip IP header reader.BaseStream.Seek(20, SeekOrigin.Begin); // Parse UDP header ushort sourcePort = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16()); ushort destPort = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16()); // Get the total length from UDP header ushort udpLength = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16()); // Check if the destination port is already displayed if (!seenDestPorts.Contains(destPort)) { seenDestPorts.Add(destPort); // Calculate the data size by subtracting the UDP header length (8 bytes) int dataSize = udpLength - 8; // Display the destination port and data size Console.WriteLine($"Destination Port: {destPort}, Data Size: {dataSize} bytes"); } } } - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.