Keine Beschreibung

DatabaseWorker.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // DatabaseWorker.cs
  3. //
  4. // Author:
  5. // Bryan Allred <bryan.allred@gmail.com>
  6. //
  7. // Copyright (c) 2011 Bryan Allred
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Collections.Generic;
  28. using System.ComponentModel;
  29. using System.Diagnostics;
  30. using System.IO;
  31. using SharpPcap;
  32. using SharpPcap.LibPcap;
  33. using WhiteNoise.Domain.Abstract;
  34. using WhiteNoise.Domain.Entities;
  35. namespace WhiteNoise.Storage
  36. {
  37. /// <summary>
  38. /// Database worker.
  39. /// </summary>
  40. public class DatabaseWorker : BackgroundWorker
  41. {
  42. /// <summary>
  43. /// The packet repository.
  44. /// </summary>
  45. private static IPacketRepository _repository;
  46. /// <summary>
  47. /// Gets or sets the devices.
  48. /// </summary>
  49. /// <value>
  50. /// The devices.
  51. /// </value>
  52. private ICollection<ICaptureDevice> _deviceFiles { get; set; }
  53. /// <summary>
  54. /// Gets or sets the version.
  55. /// </summary>
  56. /// <value>
  57. /// The version.
  58. /// </value>
  59. public string Version { get; private set; }
  60. /// <summary>
  61. /// Initializes a new instance of the <see cref="WhiteNoise.Storage.DatabaseWorker"/> class.
  62. /// </summary>
  63. /// <param name='repository'>
  64. /// Repository.
  65. /// </param>
  66. public DatabaseWorker(IPacketRepository repository)
  67. {
  68. _repository = repository;
  69. this._deviceFiles = new List<ICaptureDevice>();
  70. this.Version = SharpPcap.Version.VersionString;
  71. this.WorkerReportsProgress = true;
  72. this.WorkerSupportsCancellation = true;
  73. }
  74. /// <summary>
  75. /// Captures the device file.
  76. /// </summary>
  77. /// <param name='file'>
  78. /// File.
  79. /// </param>
  80. public void CaptureDeviceFile(string file)
  81. {
  82. this.CaptureDeviceFile(new FileInfo(file));
  83. }
  84. /// <summary>
  85. /// Captures the device file.
  86. /// </summary>
  87. /// <param name='file'>
  88. /// File.
  89. /// </param>
  90. public void CaptureDeviceFile(FileInfo file)
  91. {
  92. if (file.Exists)
  93. {
  94. ICaptureDevice device = null;
  95. try
  96. {
  97. // Listen to the device file.
  98. device = new CaptureFileReaderDevice(file.FullName);
  99. // Add event handler(s).
  100. device.OnPacketArrival += new PacketArrivalEventHandler(Device_OnPacketArrival);
  101. // Open device file and start capturing.
  102. device.Open();
  103. device.StartCapture();
  104. }
  105. catch (Exception ex)
  106. {
  107. Debug.WriteLine(ex.Message);
  108. }
  109. finally
  110. {
  111. // Clean-up.
  112. if (device != null && device.Started)
  113. {
  114. this._deviceFiles.Add(device);
  115. }
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// Stop all listening devices.
  121. /// </summary>
  122. public void Stop()
  123. {
  124. foreach (ICaptureDevice device in this._deviceFiles)
  125. {
  126. if (device.Started)
  127. {
  128. device.StopCapture();
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Raises the do work event.
  134. /// </summary>
  135. /// <param name='e'>
  136. /// Work event arguments.
  137. /// </param>
  138. protected override void OnDoWork (DoWorkEventArgs e)
  139. {
  140. // TODO: Add the working portion (possibly pull from previous areas).
  141. base.OnDoWork(e);
  142. }
  143. /// <summary>
  144. /// Device the on packet arrival.
  145. /// </summary>
  146. /// <param name='sender'>
  147. /// Sender.
  148. /// </param>
  149. /// <param name='e'>
  150. /// Capture event arguments.
  151. /// </param>
  152. private static void Device_OnPacketArrival(object sender, CaptureEventArgs e)
  153. {
  154. Debug.WriteLine("Received {0} bytes.", e.Packet.Data.Length);
  155. try
  156. {
  157. // Dump to the file.
  158. _repository.Add(new WhiteNoise.Domain.Entities.Packet()
  159. {
  160. Type = e.Packet.LinkLayerType.ToString(),
  161. Data = e.Packet.Data
  162. });
  163. }
  164. catch (Exception ex)
  165. {
  166. // Let the developer know!!!
  167. Debug.WriteLine(ex.Message);
  168. }
  169. finally
  170. {
  171. // Clean up.
  172. }
  173. }
  174. }
  175. }