aboutsummaryrefslogtreecommitdiff
path: root/Inveigh/Sniffer/Sniffer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Inveigh/Sniffer/Sniffer.cs')
-rw-r--r--Inveigh/Sniffer/Sniffer.cs38
1 files changed, 30 insertions, 8 deletions
diff --git a/Inveigh/Sniffer/Sniffer.cs b/Inveigh/Sniffer/Sniffer.cs
index 36106cf..de8fa77 100644
--- a/Inveigh/Sniffer/Sniffer.cs
+++ b/Inveigh/Sniffer/Sniffer.cs
@@ -21,6 +21,8 @@ namespace Inveigh
{
class Sniffer
{
+ public static bool isRunning = false;
+
public static void Start(string protocol, string snifferIP, bool isIPV6)
{
byte[] snifferIn = new byte[4] { 1, 0, 0, 0 };
@@ -31,6 +33,7 @@ namespace Inveigh
IPEndPoint snifferIPEndPoint;
EndPoint snifferEndPoint;
AddressFamily addressFamily = AddressFamily.InterNetwork;
+ IAsyncResult ipAsync;
if (isIPV6)
{
@@ -87,9 +90,10 @@ namespace Inveigh
throw;
}
- int packetLength;
+ int packetLength = 0;
+ isRunning = true;
- while (Program.isRunning)
+ while (isRunning)
{
try
@@ -98,17 +102,35 @@ namespace Inveigh
SocketFlags socketFlags = SocketFlags.None;
try
- {
- packetLength = snifferSocket.ReceiveMessageFrom(snifferBuffer, 0, snifferBuffer.Length, ref socketFlags, ref snifferEndPoint, out packetInformation);
- snifferData = new byte[packetLength];
- Buffer.BlockCopy(snifferBuffer, 0, snifferData, 0, packetLength);
+ {
+ ipAsync = snifferSocket.BeginReceiveMessageFrom(snifferBuffer, 0, snifferBuffer.Length, socketFlags, ref snifferEndPoint, null, null);
+
+ do
+ {
+ Thread.Sleep(10);
+
+ if (!isRunning)
+ {
+ break;
+ }
+
+ }
+ while (!ipAsync.IsCompleted);
+
+ if (isRunning)
+ {
+ packetLength = snifferSocket.EndReceiveMessageFrom(ipAsync, ref socketFlags, ref snifferEndPoint, out packetInformation);
+ snifferData = new byte[packetLength];
+ Buffer.BlockCopy(snifferBuffer, 0, snifferData, 0, packetLength);
+ }
+
}
catch
{
packetLength = 0;
}
-
- if (packetLength > 0)
+
+ if (packetLength > 0 && isRunning)
{
IPHeader ipHeader = new IPHeader();
MemoryStream memoryStream = new MemoryStream(snifferData, 0, packetLength);