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

Language C# Lines 80
MD5 Hash 4d0d9ae91c583e04753e7bfa013f3a8a
Repository https://github.com/thecc4re/sipsorcery-mono.git View Raw File
 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using SIPSorcery.Sys;
using log4net;

namespace SIPSorcery.Net
{
    public class ICMPListener
    {
        private readonly static ILog logger = AppState.logger;

        private Socket m_icmpListener;
        private bool m_stop;

        public ICMPListener()
        {
            m_icmpListener = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
            //m_icmpListener.Blocking = true;
        }

        public void Start(object state)
        {
            try
            {
                //m_icmpListener.Bind(new IPEndPoint(IPAddress.Any, 0));

                byte[] buffer = new byte[4096];
                EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

                logger.Debug("ICMPListener receive thread starting.");

                while (!m_stop)
                {
                    int bytesRead = m_icmpListener.ReceiveFrom(buffer, ref remoteEndPoint);
                    logger.Debug("ICMPListener received " + bytesRead + " from " + remoteEndPoint.ToString());
                    //m_icmpListener.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, this.ReceiveRawPacket, buffer);
                }

                logger.Debug("ICMPListener receive thread stopped.");
            }
            catch (Exception excp)
            {
                logger.Error("Exception ICMPListener Start. " + excp.Message);
                throw;
            }
        }

        private void ReceiveRawPacket(IAsyncResult ar)
        {
            try
            {
                byte[] buffer = (byte[])ar.AsyncState;
                int bytesRead = m_icmpListener.EndReceive(ar);
                logger.Debug("ICMPListener received " + bytesRead + ".");
            }
            catch (Exception excp)
            {
                logger.Error("Exception ReceiveRawPacket. " + excp.Message);
            }
        }

        public void Stop()
        {
            try
            {
                m_stop = true;
                m_icmpListener.Shutdown(SocketShutdown.Receive);
            }
            catch (Exception excp)
            {
                logger.Error("Exception ICMPListener Stop. " + excp.Message);
            }
        }
    }
}
Back to Top