1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
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
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
ComboBox1.Items.Add(mycomputerconnections(i).Name)
Next
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
BindSocket()
End If
Next
End Sub
Private Sub BindSocket()
Try
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"
Else
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))
'Else
'stringz = stringz & "."
'End If
Next
'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
DataGridView1.Rows.RemoveAt(0)
End If
'On rajoute une ligne
DataGridView1.Rows.Add()
'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
Else
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 |
Partager