PageRenderTime 32ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/sipsorcery-core/SIPSorcery.Net/ICMP/ICMPListener.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 79 lines | 66 code | 10 blank | 3 comment | 1 complexity | 4d0d9ae91c583e04753e7bfa013f3a8a MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using SIPSorcery.Sys;
  8. using log4net;
  9. namespace SIPSorcery.Net
  10. {
  11. public class ICMPListener
  12. {
  13. private readonly static ILog logger = AppState.logger;
  14. private Socket m_icmpListener;
  15. private bool m_stop;
  16. public ICMPListener()
  17. {
  18. m_icmpListener = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
  19. //m_icmpListener.Blocking = true;
  20. }
  21. public void Start(object state)
  22. {
  23. try
  24. {
  25. //m_icmpListener.Bind(new IPEndPoint(IPAddress.Any, 0));
  26. byte[] buffer = new byte[4096];
  27. EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
  28. logger.Debug("ICMPListener receive thread starting.");
  29. while (!m_stop)
  30. {
  31. int bytesRead = m_icmpListener.ReceiveFrom(buffer, ref remoteEndPoint);
  32. logger.Debug("ICMPListener received " + bytesRead + " from " + remoteEndPoint.ToString());
  33. //m_icmpListener.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, this.ReceiveRawPacket, buffer);
  34. }
  35. logger.Debug("ICMPListener receive thread stopped.");
  36. }
  37. catch (Exception excp)
  38. {
  39. logger.Error("Exception ICMPListener Start. " + excp.Message);
  40. throw;
  41. }
  42. }
  43. private void ReceiveRawPacket(IAsyncResult ar)
  44. {
  45. try
  46. {
  47. byte[] buffer = (byte[])ar.AsyncState;
  48. int bytesRead = m_icmpListener.EndReceive(ar);
  49. logger.Debug("ICMPListener received " + bytesRead + ".");
  50. }
  51. catch (Exception excp)
  52. {
  53. logger.Error("Exception ReceiveRawPacket. " + excp.Message);
  54. }
  55. }
  56. public void Stop()
  57. {
  58. try
  59. {
  60. m_stop = true;
  61. m_icmpListener.Shutdown(SocketShutdown.Receive);
  62. }
  63. catch (Exception excp)
  64. {
  65. logger.Error("Exception ICMPListener Stop. " + excp.Message);
  66. }
  67. }
  68. }
  69. }