VB.net進階:VB.net下的Sniffer

來源:互聯網
上載者:User
改寫後的代碼分成兩部分:Receiver,用來偵聽;PacketInfo,對資料包進行簡單的解析。
資料包的內容是通過Receiver的DataReceived事件返回來的。
每個函數都不長,容易看懂,注釋我就……咳咳。




Imports System.Net
Imports System.Net.Sockets
Imports System.Threading

Public Class Receiver

Dim buffer As Byte()
Dim mvarBufferLength As Integer = 4096
Dim sck As Socket
Dim thrReceive As Thread
Dim mvarStopOne As Boolean = False

Public Event DataReceived(ByVal data As Byte(), ByVal Length As Integer)

Sub New()
ReDim buffer(mvarBufferLength)

sck = New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)
sck.Blocking = False
sck.Bind(New IPEndPoint(Dns.GetHostByName(Dns.GetHostName).AddressList(0), 0))

If Not SetSockoption() Then Throw New Exception("Unable to setup socket options")
End Sub

Public Property BufferLength() As Integer
Get
Return mvarBufferLength
End Get
Set(ByVal Value As Integer)
If Not thrReceive Is Nothing Then
If thrReceive.ThreadState = ThreadState.Running Then Throw New Exception("Receiving thread is running. Call StopReceive() first.")
End If
ReDim buffer(Value)
mvarBufferLength = Value
End Set
End Property

Public Property StopEveryOnePackage() As Boolean '指定是否接受一個資料包後就退出。用於測試。
Get
Return mvarStopOne
End Get
Set(ByVal Value As Boolean)
mvarStopOne = Value
End Set
End Property

Public Sub StartReceive()
StopReceive()
thrReceive = New Thread(AddressOf subReceive)
thrReceive.Start()
End Sub

Public Sub StopReceive()
Try
thrReceive.Abort()
Catch ex As Exception
End Try
End Sub

Private Sub subReceive()
Dim i As Integer, ar As IAsyncResult
Dim b As Byte()
While True
ar = sck.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, Nothing, Me)
i = sck.EndReceive(ar)
ReDim b(i)
Array.Copy(buffer, 0, b, 0, i)
RaiseEvent DataReceived(b, i)
Thread.CurrentThread.Sleep(10)
If Me.StopEveryOnePackage Then Exit While
End While
End Sub

Private Function SetSockoption() As Boolean
Try
sck.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1)
Dim IN_() As Byte = {1, 0, 0, 0}
Dim OUT_(4) As Byte
Dim SIO_RCVALL As Long = &H98000001
sck.IOControl(SIO_RCVALL, IN_, OUT_)
If (BitConverter.ToInt32(OUT_, 0) <> 0) Then Return False
Catch ex As SocketException
Return False
End Try
Return True
End Function
End Class



------------------------------------------------------------------------------------------




Imports System.Net

Public Class PacketInfo
Dim data As Byte()

Sub New(ByVal PacketData As Byte())
data = PacketData
End Sub

Public ReadOnly Property Protocal() As System.Net.Sockets.ProtocolType
Get
Select Case GetProtocal()
Case 17
Return Net.Sockets.ProtocolType.Udp
Case 6
Return Net.Sockets.ProtocolType.Tcp
Case 1
Return Net.Sockets.ProtocolType.Icmp
Case Else
Return Net.Sockets.ProtocolType.Unknown
End Select
End Get
End Property

Public ReadOnly Property Sender() As IPEndPoint
Get
If Me.Protocal = Sockets.ProtocolType.Unknown Then Return Nothing
Return GetSenderIPEndPoint()
End Get
End Property

Public ReadOnly Property Receiver() As IPEndPoint
Get
If Me.Protocal = Sockets.ProtocolType.Unknown Then Return Nothing
Return GetreceiverIPEndPoint()
End Get
End Property

Public ReadOnly Property PacketData() As Byte()
Get
If Me.Protocal = Sockets.ProtocolType.Unknown Then Return Nothing
Return GetData()
End Get
End Property



Private Function GetProtocal() As Integer
Return data(9)
End Function

Private Function GetSenderIPEndPoint() As IPEndPoint
Return New IPEndPoint(GetSenderAddress, GetSenderPort)
End Function

Private Function GetreceiverIPEndPoint() As IPEndPoint
Return New IPEndPoint(GetReceiverAddress, getReceiverPort)
End Function

Private Function GetSenderAddress() As IPAddress
Return GetAddress(12)
End Function

Private Function GetSenderPort() As Integer
Return GetPort(20)
End Function

Private Function GetReceiverAddress() As IPAddress
Return GetAddress(16)
End Function

Private Function getReceiverPort() As Integer
Return GetPort(21)
End Function

Private Function GetAddress(ByVal StartIndex As Integer) As IPAddress
Dim b(3) As Byte
Array.Copy(data, StartIndex, b, 0, 4)
Return IPAddress.Parse(String.Format("{0}.{1}.{2}.{3}", b(0), b(1), b(2), b(3)))
End Function

Private Function GetPort(ByVal StartIndex As Integer) As Integer
Return data(StartIndex) * 256 + data(StartIndex + 1)
End Function

Private Function GetData() As Byte()
Dim b As Byte()
Dim HeaderLength As Integer

Select Case Me.Protocal
Case Sockets.ProtocolType.Tcp
HeaderLength = 40
Case Sockets.ProtocolType.Udp
HeaderLength = 28
End Select
ReDim b(data.Length - HeaderLength)
Array.Copy(data, HeaderLength, b, 0, data.Length - HeaderLength)
Return b
End Function

End Class





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.