我正在尝试在 .NET 中接收第 2 层数据包。我尝试在 RAW 模式下使用套接字但无济于事。
WireShark 数据包示例:
0000 ff ff ff ff ff ff 00 b0 d0 04 82 ae 12 fd bb 00 ................
0010 01 1c 00 02 00 b0 d0 04 82 ae 01 af af af af af ................
0020 af af af af af af af af af af af af af af af af ................
0030 af af af af af af af af af af af af ............
到目前为止,这是我尝试过的一件事:
Private LocalIP As System.Net.IPAddress
Private RAW_TcpSocket As System.Net.Sockets.Socket
Private RAW_IPEndPoint As System.Net.IPEndPoint
Private STR_TcpSocket As System.Net.Sockets.Socket
Private STR_IPEndPoint As System.Net.IPEndPoint
Private Sub frmMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Try
Me.LocalIP = New System.Net.IPAddress(New Byte() {192, 168, 200, 106})
Me.RAW_IPEndPoint = New System.Net.IPEndPoint(Me.LocalIP, 0)
Me.RAW_TcpSocket = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Raw, Net.Sockets.ProtocolType.IP)
Dim inValue As Byte() = New Byte(3) {1, 0, 0, 0}
Dim outValue As Byte() = New Byte(3) {0, 0, 0, 0}
With Me.RAW_TcpSocket
Dim RecBuf(512) As System.Byte
.Bind(Me.RAW_IPEndPoint)
'.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.TypeOfService, True)
.IOControl(IOControlCode.ReceiveAll, inValue, outValue)
.BeginReceive(RecBuf, 0, RecBuf.Length, SocketFlags.None, AddressOf Me.RAW_TcpReceive, RecBuf)
End With
Catch ex As Exception
Debug.WriteLine(ex)
End Try
End Sub
Private Sub RAW_TcpReceive(ByVal ar As System.IAsyncResult)
Dim RecBuf As System.Byte() = CType(ar.AsyncState, System.Byte())
SetText(vbCrLf)
For Each tmpByte As Byte In RecBuf
SetText("0x" & tmpByte.ToString("X") & Space(2))
Next
Beep()
'MsgBox("RAW SOCKET RECEIVED DATA")
End Sub