By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Xenophobic packet analyzer that only analyze its own frames


New member
Jun 27, 2016
Good morning!

I'm trying to code my own packet analyser in VB.NET, it works by pressing a start button and chosing the connection to put into a datagridbox.

Yet I have two issues, one of form and the other of substance. It seems that my packet anlayzer is xenophobic, it doesn't accept other frames than his own one, and he doesn't want to store more than one frame at a given time.
  • On the substance I can't access to other frames than those which came from my computer to my computer despite of the fact that an Ethernet camera is wired to my computer through an Ethernet cable
  • On the form, all my frames disappears when a new one (which creates a new line) appear to my array.

Can you help me finding the criminal that impedes those frames to come and to live? :)

Here is my code:

Imports System.Net
Imports System.Net.Sockets
Public Class Form1
    Dim mycomputerconnections() As Net.NetworkInformation.NetworkInterface 'I don't know what it is...
    Dim monip As IPAddress
    Dim socket As New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)
    Dim buffer(4096) As Byte
    Dim destinationport As UInteger
    Dim sourceport As UInteger
    Dim typez As String
    Dim sizediff As Size
    Dim formloaded As Boolean = False
    Private ipfrom As IPAddress
    Private ipto As IPAddress
    Private FilterIP As Boolean = False
    Private stringz As String = ""
    Private started As Boolean = False
    Private Sub DémarrageAquisitionToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DémarrageAquisitionToolStripMenuItem.Click
    End Sub
    Private Sub QuitterToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles QuitterToolStripMenuItem.Click
    End Sub
    Private Sub affichage_trame_ip_TextChanged(sender As Object, e As EventArgs) Handles affichage_trame_ip.TextChanged
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        sizediff.Height = Me.Height - DataGridView1.Height
        sizediff.Width = Me.Width - DataGridView1.Width
        formloaded = True
        mycomputerconnections = Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
        For i = 0 To mycomputerconnections.Length - 1
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        'Récupération de l'adresse IP et appel de la méthode d'association de la socket avec un point de terminaison local (un port).
        For i = 0 To mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses.Count - 1 'what foes GetIPProperties mean?
            If mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
                monip = mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address
            End If
    End Sub
    Private Sub BindSocket()
            socket.Bind(New IPEndPoint(monip, 0))
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, True)
            Dim byin() As Byte = {1, 0, 0, 0}
            Dim byout() As Byte = {1, 0, 0, 0}
            socket.Blocking = False
            ReDim buffer(socket.ReceiveBufferSize)
            socket.IOControl(IOControlCode.ReceiveAll, byin, byout) 'what are OptionIn and OptionOut?
            'Pour que le socket commence à recevoir, il faut un buffer, une règle de comportement, une méthode de réception et savoir si on le fait à partir d'un socket déjà constitué
            socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, New AsyncCallback(AddressOf Reception), Nothing)
            ComboBox1.Enabled = False
        Catch ex As Exception
            ComboBox1.BackColor = Color.Red
        End Try
    End Sub
    'Méthode de réception
    Private Sub Reception(ar As IAsyncResult)
        'On cherche d'abord la taille du paquet
        If started = True Then
            Dim readlength As UInteger = BitConverter.ToUInt16(Byteswap(buffer, 2), 0)
            sourceport = BitConverter.ToUInt16(Byteswap(buffer, 22), 0) 'Yet, I want to take the one related to the data, not the others...
            destinationport = BitConverter.ToUInt16(Byteswap(buffer, 24), 0)
            'Ici on cherche à savoir quel protocole a été utilisé.
            If buffer(9) = 6 Then
                typez = "TCP"
            ElseIf buffer(9) = 11
                typez = "UDP"
            ElseIf buffer(9) = 1
                typez = "ICMP"
                typez = "???"
            End If
            'On cherche les IP de provenance et de destination
            ipfrom = New IPAddress(BitConverter.ToUInt16(buffer, 12))
            ipto = New IPAddress(BitConverter.ToUInt16(buffer, 16))
            'Si c'est un pacquet de/pour moi
            If (ipfrom.Equals(monip) = True Or (ipto.Equals(monip) = True)) And ipto.Equals(ipfrom) = False Then
                'Si on a pas mis le filtre
                If FilterIP = False Then
                    'On remplit stringz qui stocke le contenue du buffer
                    For i = 26 To readlength - 1
                        'If Char.IsLetterOrDigit(Chr(buffer(i))) = True Then
                        stringz = stringz & Chr(buffer(i))
                        'stringz = stringz & "."
                        'End If
                    'On met des données dans DataGriedView, et dans la mesure ou c'est un thread différent, on l'invoque.
                    DataGridView1.Invoke(New MethodInvoker(AddressOf DGVMisAJour))
                End If
            End If
        End If
        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, New AsyncCallback(AddressOf Reception), Nothing)
    End Sub
    Private Function Byteswap(buffer() As Byte, index As UInteger)
        Dim result(1) As Byte
        result(0) = buffer(index + 1)
        result(1) = buffer(index)
        Return result
    End Function
    'Mise à jour de la table
    Private Sub DGVMisAJour()
        'suppression de ligne s'il y en a trop/ NE DEVRAIT T'ON PAS LES STOCKER PLUTÔT?
        If DataGridView1.RowCount > 50 Then
        End If
        'On rajoute une ligne
        'On rajoute l'ip de provenance dans la première case
        DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(0).Value = ipfrom.ToString & ":" & sourceport
        'On rajoute l'ip de destination dans la deuxième case
        DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(1).Value = ipto.ToString & ":" & destinationport 'From 
        'On rajoute le protocole utilisé 
        DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(2).Value = typez
        'On rajoute le contenu
        DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(3).Value = stringz
        stringz = ""
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If started = True Then
            Button1.Text = "Start"
            started = False
            Button1.Text = "Stop"
            started = True
        End If
    End Sub
    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If formloaded = True Then
            DataGridView1.Size = Me.Size - sizediff
        End If
    End Sub
End Class

Here is its design:

View attachment 139135