中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > 临时文章
VB.net进阶:VB.net下的Sniffer
作者:未知 时间:2004-12-02 12:12 出处:Blog 责编:chinaitpower
              摘要:暂无

原来的C#代码在这里:
http://www.techblog.idv.tw/Blog/zion/archive/2004/09/14/148.aspx

改写后的代码分成两部分: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

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有