System.Net.Sockets for reading IP packets?
-
I'm writing an application in C# that needs to sniff all IP packets coming into a certain server. I've tried to create a socket, bind, listening for connections, but I'm getting an exception when i try to listen on the created socket. It says "the operation is not supported on the type of object". I tried changing how I defined the IPEndPoint, and the SocketType and ProtocolType enumerations when creating the socket. Does anybody know the correct setup of the socket in order to inspect IP packets? Not at the TCP layer, but lower in OSI model. Is Here's the code. Thx, Tom try { // Build local end point IPAddress localAddr = IPAddress.Parse(Constants.LOCALHOST); IPEndPoint endpoint = new IPEndPoint(localAddr, Constants.PORT); // create new IP socket. server = new Socket(AddressFamily.InterNetwork,SocketType.Raw,ProtocolType.IP); // SetupSocket(server); // Bind the socket to the local IP addr and port server.Bind(endpoint); // Start listening for connections, allowing for max # to be queued. server.Listen(Constants.MAX_CONNECTION_QUEUE); // loop until the service is stopped. while (ServiceStarted) { // Wait asynchronously for connections to be accepted. server.BeginAccept(new AsyncCallback(this.AcceptCallback), server); // yield if (ServiceStarted) { Thread.Sleep(new TimeSpan(0, 0, 5)); } . . . } } catch(SocketException e) { throw e; }
-
I'm writing an application in C# that needs to sniff all IP packets coming into a certain server. I've tried to create a socket, bind, listening for connections, but I'm getting an exception when i try to listen on the created socket. It says "the operation is not supported on the type of object". I tried changing how I defined the IPEndPoint, and the SocketType and ProtocolType enumerations when creating the socket. Does anybody know the correct setup of the socket in order to inspect IP packets? Not at the TCP layer, but lower in OSI model. Is Here's the code. Thx, Tom try { // Build local end point IPAddress localAddr = IPAddress.Parse(Constants.LOCALHOST); IPEndPoint endpoint = new IPEndPoint(localAddr, Constants.PORT); // create new IP socket. server = new Socket(AddressFamily.InterNetwork,SocketType.Raw,ProtocolType.IP); // SetupSocket(server); // Bind the socket to the local IP addr and port server.Bind(endpoint); // Start listening for connections, allowing for max # to be queued. server.Listen(Constants.MAX_CONNECTION_QUEUE); // loop until the service is stopped. while (ServiceStarted) { // Wait asynchronously for connections to be accepted. server.BeginAccept(new AsyncCallback(this.AcceptCallback), server); // yield if (ServiceStarted) { Thread.Sleep(new TimeSpan(0, 0, 5)); } . . . } } catch(SocketException e) { throw e; }
AFAIK, what you are attempting to do can't be done using the .NET API as it provides only application level operations. Regards Senthil _____________________________ My Blog | My Articles | WinMacro