【发布时间】:2011-09-14 03:43:16
【问题描述】:
我正在尝试读取启用了标头的原始套接字上的传入数据包。查看其他项目,例如 CodeProject 上的 MJsniffer,我已经能够创建自己的代码来读取我想要的所有内容。问题:我只检索传出的信息。这是我初始化原始套接字的代码,此时处理代码无关紧要..
// Resolve the host name or IP address to am IPHostEntry instance
IPHostEntry hIPHostEntry = Dns.GetHostEntry( Dns.GetHostName());
// Initialize a new instance of the Socket class.
Socket hSocket = new Socket( AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Unspecified ); // IP is possible.
// Bind the socket to each resolved IP address.
foreach ( IPAddress hIPAddress in hIPHostEntry.AddressList ) try { hSocket.Bind( new IPEndPoint( hIPAddress, 0 )); } catch( Exception ) { continue; }
// Configure the incoming socket to accept all the required information.
hSocket.SetSocketOption( SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true );
// Configure the incoming socket to receive all the required information.
hSocket.IOControl( IOControlCode.ReceiveAll, BitConverter.GetBytes( 1 ), BitConverter.GetBytes( 1 ));
// Return the configured socket.
return hSocket;
在 Windows 7 64 位上运行,我拥有完全的管理权限,如何修改此代码以获取能够读取传入数据包的套接字?传出很好,我也需要,但我也绝对需要传入数据包。
P.S: 我不想强迫用户安装 WinPcap。我不想捕捉或欺骗或任何东西,只是阅读,这应该是可能的..
【问题讨论】: