In my company, we develop a network product, which acts like a sniffer.
In order to capture all IPV4 packets, we follow the next procedure:
new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Udp);
socket.Bind(new IPEndPoint(address, 0));
)socket.IOControl(IOControlCode.ReceiveAll, new byte[]{ 1, 0, 0, 0 }, null);
And this is everything we need in order to capture all IPV4 packets from a network interface. the rest is a simple read operation inside a loop. On Windows, the process has to be executed with admin permissions.
Now, we also want to support IPV6. It's supposed to be easy, but we can't come up with a working solution. Even if nothing throws an exception, the socket is still "stuck" on the read operation. nothing is never received there.
In order to capture IPV6 packets, we modify the above procedure just a bit
Socket(AddressFamily.InterNetworkV6, SocketType.Raw, ProtocolType.IPv6)
(note: using ProtocolType.Udp
doesn't help either)socket.Bind(new IPEndPoint(IPAddress.Parse("fe80::dd37:936f:d87:b70c"), 0));
). Note: adding a scope-id (like fe80::dd37:936f:d87:b70c%9
) doesn't help either.Another things I've tried:
socket.DualMode = true;
between (1) and (2). socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
between (1) and (2).I test this by opening Chrome - Chrome fires an MDNS packet both for IPV6 and IPV4 for its chrome-cast feature. Wireshark (which uses a kernel driver), sees this packet and displays it correctly. My code doesn't unblock from the read operation.
I use Windows 10 that supports IPV6.
An ideas how to read all IPV6 packets from the network interface like in IPV4? Thanks.
The solution is to use ProtocolType.IP instead of ProtocolType.IPv6.