|
分類:[VB.NET/VB2005 以降]
2010/04/29(Thu) 20:17:59 編集(投稿者) 2010/04/29(Thu) 20:16:47 編集(投稿者)
VB.NETを使用したパケットキャプチャを作ろうと思っています。 最終的にはログの内容から特定の処理を実行させたいと考えています。
あれこれ調べ、ようやくパケットを見れるところまで行きましたが、自分のPCからの送信パケットしか取得出来ず、受信パケットが取得出来ません。わかる方いましたら教えてください。
環境:VB2008(Visualstudio2008 Professional Edition) OS:Windows7Ultimate(64bit)
現在の不具合:送信パケットしか取得出来ない。同じパケットなのに何度もReceptionCallback関数が呼ばれる(こちらは重要度低です)
以下にコードを書きます。 ※参考URL http://bbs.wankuma.com/index.cgi?mode=al2&namber=25740&KLOG=47
---------------------------------------------------------------- Imports System Imports System.Net Imports System.Net.Sockets Imports System.Threading Imports System.Text
Public Class Form1 Const BufferSize As Integer = 4096 Private Socket As Socket Private Buffer(BufferSize) As Byte
Public Sub BeginReceive() Socket = New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP) Socket.Blocking = False
Dim HostIP() As System.Net.IPAddress Dim HostIPAdress As String HostIP = System.Net.Dns.Resolve(System.Net.Dns.GetHostName()).AddressList HostIPAdress = HostIP(HostIP.GetLowerBound(0)).ToString
Socket.Bind(New IPEndPoint(IPAddress.Parse(HostIPAdress), 0)) Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AcceptConnection, True) Socket.IOControl(IOControlCode.ReceiveAll, New Byte() {1, 0, 0, 0}, New Byte() {0, 0, 0, 0}) Socket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, New AsyncCallback(AddressOf ReceptionCallback), Nothing) End Sub
Private Sub ReceptionCallback(ByVal ar As IAsyncResult) Dim size As Integer = Socket.EndReceive(ar) Dim currentstate = ar.AsyncState
' --------------------------------------------- ' IPヘッダ部分 Dim ipHeader As New StringBuilder() For Each b As Byte In Buffer Dim s As String = b.ToString("X") Dim str2 As String = Convert.ToString(b, 2) Dim str3 As String = [String].Format("{0:D8}", Convert.ToInt32(str2)) ipHeader.Append(str3) Next Dim version As Integer = Convert.ToInt32(ipHeader.ToString().Substring(0, 4), 2) Dim headerLength As Integer = Convert.ToInt32(ipHeader.ToString().Substring(3, 4), 2) Dim serviceType As Integer = Convert.ToInt32(ipHeader.ToString().Substring(8, 8), 2) Dim dataGramLength As Integer = Convert.ToInt32(ipHeader.ToString().Substring(16, 16), 2) Dim id As Integer = Convert.ToInt32(ipHeader.ToString().Substring(32, 16), 2) Dim flag As Integer = Convert.ToInt32(ipHeader.ToString().Substring(48, 3), 2) Dim flagmentOffset As Integer = Convert.ToInt32(ipHeader.ToString().Substring(51, 13), 2) Dim ttl As Integer = Convert.ToInt32(ipHeader.ToString().Substring(64, 8), 2) Dim protocolNo As Integer = Convert.ToInt32(ipHeader.ToString().Substring(72, 8), 2) Dim checkSum As Integer = Convert.ToInt32(ipHeader.ToString().Substring(80, 16), 2) Dim offset As Integer = 0 Dim ipFrom1 As Integer = Convert.ToInt32(ipHeader.ToString().Substring(96 + offset, 8), 2) Dim ipFrom2 As Integer = Convert.ToInt32(ipHeader.ToString().Substring(104 + offset, 8), 2) Dim ipFrom3 As Integer = Convert.ToInt32(ipHeader.ToString().Substring(112 + offset, 8), 2) Dim ipFrom4 As Integer = Convert.ToInt32(ipHeader.ToString().Substring(120 + offset, 8), 2) Dim ipTo1 As Integer = Convert.ToInt32(ipHeader.ToString().Substring(128 + offset, 8), 2) Dim ipTo2 As Integer = Convert.ToInt32(ipHeader.ToString().Substring(136 + offset, 8), 2) Dim ipTo3 As Integer = Convert.ToInt32(ipHeader.ToString().Substring(144 + offset, 8), 2) Dim ipTo4 As Integer = Convert.ToInt32(ipHeader.ToString().Substring(152 + offset, 8), 2) Dim ipFrom As New StringBuilder() ipFrom.Append(ipFrom1) ipFrom.Append(".") ipFrom.Append(ipFrom2) ipFrom.Append(".") ipFrom.Append(ipFrom3) ipFrom.Append(".") ipFrom.Append(ipFrom4) Dim ipTo As New StringBuilder() ipTo.Append(ipTo1) ipTo.Append(".") ipTo.Append(ipTo2) ipTo.Append(".") ipTo.Append(ipTo3) ipTo.Append(".") ipTo.Append(ipTo4)
' --------------------------------------------- ' メイン処理 ' ---------------------------------------------
' ---------------------------------------------
Socket.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, New AsyncCallback(AddressOf ReceptionCallback), Nothing) End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click BeginReceive() End Sub End Class
|