Advertisement
Robomatics

Packet Scanner

Apr 23rd, 2013
11,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.36 KB | None | 0 0
  1. Imports System.Net
  2. Imports System.Net.Sockets
  3.  
  4. Public Class Form1
  5.  
  6.     Dim socketz As New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)
  7.     Dim bytedata(4096) As Byte
  8.     Dim myip As IPAddress
  9.     Dim started As Boolean = True
  10.     Dim sizediff As Size
  11.     Dim formloaded As Boolean = False
  12.     Dim FilterIPAddress As New IPAddress(0)
  13.     Dim FilterIP As Boolean
  14.     Dim mycomputerconnections() As Net.NetworkInformation.NetworkInterface
  15.  
  16.     'DGV Update stuff
  17.     Dim stringz As String = ""
  18.     Dim Typez As String = ""
  19.     Dim ipfrom As IPAddress
  20.     Dim ipto As IPAddress
  21.     Dim destinationport As UInteger
  22.     Dim sourceport As UInteger
  23.  
  24.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  25.  
  26.         sizediff.Height = Me.Height - DGV.Height
  27.         sizediff.Width = Me.Width - DGV.Width
  28.         formloaded = True
  29.  
  30.         mycomputerconnections = Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
  31.  
  32.         For i = 0 To mycomputerconnections.Length - 1
  33.             ComboBox1.Items.Add(mycomputerconnections(i).Name)
  34.         Next
  35.  
  36.     End Sub
  37.  
  38.     Private Sub OnReceive(ByVal asyncresult As IAsyncResult)
  39.  
  40.         If started = True Then
  41.             'Get Length of packet (including header)
  42.             Dim readlength As UInteger = BitConverter.ToUInt16(Byteswap(bytedata, 2), 0)
  43.             sourceport = BitConverter.ToUInt16(Byteswap(bytedata, 22), 0)
  44.             destinationport = BitConverter.ToUInt16(Byteswap(bytedata, 24), 0)
  45.  
  46.             'Get Protocol Type
  47.             If bytedata(9) = 6 Then
  48.                 Typez = "TCP"
  49.             ElseIf bytedata(9) = 17 Then
  50.                 Typez = "UDP"
  51.             Else
  52.                 Typez = "???"
  53.             End If
  54.  
  55.             'Get IP from and to
  56.             ipfrom = New IPAddress(BitConverter.ToUInt32(bytedata, 12))
  57.             ipto = New IPAddress(BitConverter.ToUInt32(bytedata, 16))
  58.  
  59.             'If this is a packet to/from me and not from myself then...
  60.             If (ipfrom.Equals(myip) = True Or ipto.Equals(myip) = True) And ipto.Equals(ipfrom) = False Then
  61.                 If FilterIP = False Or (FilterIP = True And (FilterIPAddress.Equals(ipfrom) Or FilterIPAddress.Equals(ipto))) Then
  62.  
  63.                     'Fix data
  64.                     stringz = ""
  65.                     For i = 26 To readlength - 1
  66.                         If Char.IsLetterOrDigit(Chr(bytedata(i))) = True Then
  67.                             stringz = stringz & Chr(bytedata(i))
  68.                         Else
  69.                             stringz = stringz & "."
  70.                         End If
  71.                     Next
  72.  
  73.                     'Put data to DataGridView, since it's on a different thread now, invoke it
  74.                     DGV.Invoke(New MethodInvoker(AddressOf DGVUpdate))
  75.  
  76.                 End If
  77.             End If
  78.  
  79.         End If
  80.  
  81.         'Restart the Receiving
  82.         socketz.BeginReceive(bytedata, 0, bytedata.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
  83.     End Sub
  84.  
  85.     Private Sub DGVUpdate()
  86.  
  87.         'Remove rows if there are too many
  88.         If DGV.Rows.Count > 50 Then
  89.             DGV.Rows.RemoveAt(0)
  90.         End If
  91.  
  92.         DGV.Rows.Add()
  93.         DGV.Rows(DGV.Rows.Count - 1).Cells(0).Value = ipfrom.ToString & ":" & sourceport 'From Column, size at 125
  94.         DGV.Rows(DGV.Rows.Count - 1).Cells(1).Value = ipto.ToString & ":" & destinationport 'To Column, size at 125
  95.         DGV.Rows(DGV.Rows.Count - 1).Cells(2).Value = Typez 'Type Column, size at 50
  96.         DGV.Rows(DGV.Rows.Count - 1).Cells(3).Value = stringz 'Data column, size mode set to fill
  97.  
  98.     End Sub
  99.  
  100.     Private Function Byteswap(ByVal bytez() As Byte, ByVal index As UInteger)
  101.         Dim result(1) As Byte
  102.         result(0) = bytez(index + 1)
  103.         result(1) = bytez(index)
  104.         Return result
  105.     End Function
  106.  
  107.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  108.         If started = True Then
  109.             Button1.Text = "Start"
  110.             started = False
  111.         Else
  112.             Button1.Text = "Stop"
  113.             started = True
  114.         End If
  115.     End Sub
  116.  
  117.     Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
  118.         If formloaded = True Then
  119.             DGV.Size = Me.Size - sizediff
  120.         End If
  121.     End Sub
  122.  
  123.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  124.  
  125.         Try
  126.             If TextBox1.Text <> "" And TextBox1.Text IsNot Nothing Then
  127.                 FilterIPAddress = IPAddress.Parse(TextBox1.Text)
  128.                 FilterIP = True
  129.                 TextBox1.BackColor = Color.LimeGreen
  130.             Else
  131.                 FilterIP = False
  132.                 TextBox1.BackColor = Color.White
  133.             End If
  134.         Catch ex As Exception
  135.             FilterIP = False
  136.             TextBox1.BackColor = Color.White
  137.         End Try
  138.  
  139.     End Sub
  140.  
  141.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  142.  
  143.         For i = 0 To mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses.Count - 1
  144.  
  145.             If mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
  146.  
  147.                 myip = mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address
  148.  
  149.                 BindSocket()
  150.  
  151.             End If
  152.  
  153.         Next
  154.  
  155.     End Sub
  156.  
  157.     Private Sub BindSocket()
  158.  
  159.         Try
  160.             socketz.Bind(New IPEndPoint(myip, 0))
  161.             socketz.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, True)
  162.             Dim bytrue() As Byte = {1, 0, 0, 0}
  163.             Dim byout() As Byte = {1, 0, 0, 0}
  164.             socketz.IOControl(IOControlCode.ReceiveAll, bytrue, byout)
  165.             socketz.Blocking = False
  166.             ReDim bytedata(socketz.ReceiveBufferSize)
  167.             socketz.BeginReceive(bytedata, 0, bytedata.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
  168.             ComboBox1.Enabled = False
  169.         Catch ex As Exception
  170.             ComboBox1.BackColor = Color.Red
  171.         End Try
  172.  
  173.     End Sub
  174.  
  175. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement