Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.48 KB | None | 0 0
  1. Option Explicit On
  2.  
  3. Imports System.IO
  4. Imports System.Net
  5. Imports System.Net.Sockets
  6.  
  7. Public Class Form1
  8.  
  9.     Dim _socket As Socket
  10.     Dim bindEndpoint As IPEndPoint
  11.     Private byteData As Byte() = New Byte(4095) {}
  12.     Dim bContinueCapturing As Boolean = False
  13.  
  14.  
  15.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  16.         bContinueCapturing = True
  17.         '_socket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
  18.     End Sub
  19.  
  20.     Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
  21.         _socket.Close()
  22.     End Sub
  23.  
  24.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  25.         _socket = New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)
  26.         bindEndpoint = New IPEndPoint(IPAddress.Parse(Dns.GetHostByName(Dns.GetHostName).AddressList(0).ToString), 0)
  27.         _socket.Bind(bindEndpoint)
  28.         _socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, True)
  29.  
  30.         Dim byTrue As Byte() = New Byte(3) {1, 0, 0, 0}
  31.         Dim byOut As Byte() = New Byte(3) {1, 0, 0, 0}
  32.         _socket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut)
  33.     End Sub
  34.  
  35.     Private Sub OnReceive(ByVal ar As IAsyncResult)
  36.         Try
  37.             If bContinueCapturing Then
  38.                 Dim nReceived As Integer = _socket.EndReceive(ar)
  39.  
  40.                 ParseData(byteData, nReceived)
  41.  
  42.                 byteData = New Byte(4095) {}
  43.                 _socket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
  44.             End If
  45.         Catch ex As ObjectDisposedException
  46.             Trace.WriteLine("OnReceive->ObjectDisposedException: " & ex.Message)
  47.         Catch ex As Exception
  48.             Trace.WriteLine("OnReceive->Exception: " & ex.Message)
  49.         End Try
  50.     End Sub
  51.  
  52.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  53.         bContinueCapturing = False
  54.     End Sub
  55.  
  56.     Private Sub ParseData(ByVal byteData As Byte(), ByVal nReceived As Integer)
  57.  
  58.         Dim ipHeader As New IPHeader(byteData, nReceived)
  59.  
  60.         Trace.WriteLine(ipHeader.SourceAddress.ToString & " => " & ipHeader.DestinationAddress.ToString)
  61.  
  62.         Select Case ipHeader.ProtocolType
  63.             Case Protocol.TCP
  64.                 Dim tcpHeader As New TCPHeader(ipHeader.Data, ipHeader.MessageLength)
  65.  
  66.                 If tcpHeader.DestinationPort = "53" OrElse tcpHeader.SourcePort = "53" Then
  67.                     Dim dnsHeader As New DNSHeader(tcpHeader.Data, tcpHeader.MessageLength)
  68.                     ' DNS
  69.                 End If
  70.             Case Protocol.UDP
  71.                 Dim udpHeader As New UDPHeader(ipHeader.Data, CInt(ipHeader.MessageLength))
  72.  
  73.                 If udpHeader.DestinationPort = "53" OrElse udpHeader.SourcePort = "53" Then
  74.                     ' DNS
  75.                     Dim dnsHeader As New DNSHeader(udpHeader.Data, udpHeader.Length)
  76.                 End If
  77.             Case Protocol.Unknown
  78.                 ' TODO
  79.         End Select
  80.     End Sub
  81.  
  82.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  83.         Dim encoding As New System.Text.UTF8Encoding()
  84.         _socket.SendTo(encoding.GetBytes("abcabcabc"), bindEndpoint)
  85.     End Sub
  86. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement