Not sure why I get error code 10022 (Invalid Argument) when setting socket options in my C # sniffer

I am writing a batch sniffer as an exercise in learning .Net 4 development in C #. My goal is to sniff IP packets coming in and out on my computer.

My problem is that I get error code 10022, an invalid argument, when I call SetSocketOption. I do not see where my argument is wrong. I have some admins on my computer, but maybe this is not enough for me. This is my working computer and the IT department is pretty strict. With that said, if it were a permissions issue, I would expect another exception.

I'm not sure what my next step should be to debug this problem. Anyone have an idea?

Here is the code:

public Sniffer()
{
    try
    {
        socket = new Socket(
            AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);

        IPAddress[] ipAddresses = Dns.GetHostEntry(
            Dns.GetHostName()).AddressList;

        socket.Bind(new IPEndPoint(ipAddresses[0], 0));

        socket.SetSocketOption(
            SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);

        byte[] inputData = new byte[4] { 1, 0, 0, 0 };
        byte[] outValue = new byte[4];

        socket.IOControl(IOControlCode.ReceiveAll, inputData, outValue);
    }
    catch (SocketException ex)
    {
        string ErrorMessage = ex.Message;
    }
}
+3
2

Microsoft Windows , , XP SP2, - 2000- .

TCP/IP MSDN.

+1

WSAEINVAL 10022

.

(, setsockopt). , , accept , .

: http://msdn.microsoft.com/en-us/library/ms740668(v=vs.85).aspx

-2

Source: https://habr.com/ru/post/1795294/


All Articles