Receiving None Standard Layer 2 Packets in .NET

427 Views Asked by At

I'm trying to receive Layer 2 packets in .NET. I've tried to use Sockets in RAW mode with no avail.

Example WireShark Packet:

 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              ............

Here's one of the things I've tried so far:

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
0

There are 0 best solutions below