public static NATKeepAliveMessage ParseNATKeepAliveMessage(byte[] buffer)
        {
            if (buffer != null && buffer.Length == 20)
            {
                byte[] sendToAddrBuffer = new byte[4];
                Buffer.BlockCopy(buffer, 0, sendToAddrBuffer, 0, 4);
                IPAddress sendToAddress = new IPAddress(sendToAddrBuffer);
                int sendToPort = BitConverter.ToInt32(buffer, 4);

                int proxyProtocol = BitConverter.ToInt32(buffer, 8);
                byte[] proxyFromAddrBuffer = new byte[4];
                Buffer.BlockCopy(buffer, 12, proxyFromAddrBuffer, 0, 4);
                int sendFromPort = BitConverter.ToInt32(buffer, 16);
                SIPEndPoint proxySendFrom = new SIPEndPoint((SIPProtocolsEnum)proxyProtocol, new IPEndPoint(new IPAddress(proxyFromAddrBuffer), sendFromPort));


                //SIPProtocolsEnum protocol = SIPProtocolsType.GetProtocolTypeFromId(BitConverter.ToInt32(buffer, 16));
                //SIPProtocolsEnum protocol = SIPProtocolsEnum.udp;

                NATKeepAliveMessage natKeepAliveMsg = new NATKeepAliveMessage(proxySendFrom, new IPEndPoint(sendToAddress, sendToPort));
                return natKeepAliveMsg;
            }
            else
            {
                return null;
            }
        }
Ejemplo n.º 2
0
        public static NATKeepAliveMessage ParseNATKeepAliveMessage(byte[] buffer)
        {
            if (buffer != null && buffer.Length == 20)
            {
                byte[] sendToAddrBuffer = new byte[4];
                Buffer.BlockCopy(buffer, 0, sendToAddrBuffer, 0, 4);
                IPAddress sendToAddress = new IPAddress(sendToAddrBuffer);
                int       sendToPort    = BitConverter.ToInt32(buffer, 4);

                int    proxyProtocol       = BitConverter.ToInt32(buffer, 8);
                byte[] proxyFromAddrBuffer = new byte[4];
                Buffer.BlockCopy(buffer, 12, proxyFromAddrBuffer, 0, 4);
                int         sendFromPort  = BitConverter.ToInt32(buffer, 16);
                SIPEndPoint proxySendFrom = new SIPEndPoint((SIPProtocolsEnum)proxyProtocol, new IPEndPoint(new IPAddress(proxyFromAddrBuffer), sendFromPort));


                //SIPProtocolsEnum protocol = SIPProtocolsType.GetProtocolTypeFromId(BitConverter.ToInt32(buffer, 16));
                //SIPProtocolsEnum protocol = SIPProtocolsEnum.udp;

                NATKeepAliveMessage natKeepAliveMsg = new NATKeepAliveMessage(proxySendFrom, new IPEndPoint(sendToAddress, sendToPort));
                return(natKeepAliveMsg);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        private void NatKeepAliveChannelMessageReceived(SIPChannel sipChannel, SIPEndPoint remoteEndPoint, byte[] buffer)
        {
            try
            {
                NATKeepAliveMessage keepAliveMessage = NATKeepAliveMessage.ParseNATKeepAliveMessage(buffer);

                if (keepAliveMessage != null)
                {
                    if (keepAliveMessage.LocalSIPEndPoint.Protocol == SIPProtocolsEnum.udp)
                    {
                        FireSIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.NATKeepAlive, SIPMonitorEventTypesEnum.NATKeepAliveRelay, "Relaying NAT keep-alive from proxy socket " + keepAliveMessage.LocalSIPEndPoint + " to " + keepAliveMessage.RemoteEndPoint + ".", null));
                        m_sipTransport.SendRaw(keepAliveMessage.LocalSIPEndPoint, new SIPEndPoint(keepAliveMessage.RemoteEndPoint), m_sendBuffer);
                    }
                    else
                    {
                        // For connection oriented protocols check whether a connection exists. NAT keep alives shouldn't cause a connection to be initiated.
                        SIPChannel sendFromChannel = m_sipTransport.FindSIPChannel(keepAliveMessage.LocalSIPEndPoint);
                        if (sendFromChannel != null && sendFromChannel.IsConnectionEstablished(keepAliveMessage.RemoteEndPoint))
                        {
                            FireSIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.NATKeepAlive, SIPMonitorEventTypesEnum.NATKeepAliveRelay, "Relaying NAT keep-alive from proxy socket " + keepAliveMessage.LocalSIPEndPoint + " to " + keepAliveMessage.RemoteEndPoint + ".", null));
                            m_sipTransport.SendRaw(keepAliveMessage.LocalSIPEndPoint, new SIPEndPoint(keepAliveMessage.RemoteEndPoint), m_sendBuffer);
                        }
                        else
                        {
                            FireSIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.NATKeepAlive, SIPMonitorEventTypesEnum.NATKeepAliveRelay, "No established connection was found to relay NAT keep-alive from proxy socket " + keepAliveMessage.LocalSIPEndPoint + " to " + keepAliveMessage.RemoteEndPoint + ".", null));
                        }
                    }
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception NatKeepAliveChannelMessageReceived. " + excp.Message);
            }
        }
Ejemplo n.º 4
0
            public void ReverseMessageTest()
            {
                Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sendToAddress             = "192.168.1.1";
                int    sendToPort                = 3455;
                string sendFromAddress           = "192.168.1.2";
                int    sendFromPort              = 3244;
                NATKeepAliveMessage keepAliveMsg = new NATKeepAliveMessage(SIPEndPoint.ParseSIPEndPoint(sendToAddress + ":" + sendToPort), new IPEndPoint(IPAddress.Parse(sendFromAddress), sendFromPort));

                byte[] buffer = keepAliveMsg.ToBuffer();
                Assert.IsTrue(buffer != null && buffer.Length == 20, "The byte buffer produced for the NATKeepAliveMessage is invalid.");

                NATKeepAliveMessage rtnMsg = NATKeepAliveMessage.ParseNATKeepAliveMessage(buffer);

                Assert.IsNotNull(rtnMsg, "The NATKeepAliveMessage could not be parsed from the buffer.");

                Assert.IsTrue(rtnMsg.RemoteEndPoint.ToString() == keepAliveMsg.RemoteEndPoint.ToString(), "The sent and returned sendto sockets were different.");
                Assert.IsTrue(rtnMsg.LocalSIPEndPoint.ToString() == keepAliveMsg.LocalSIPEndPoint.ToString(), "The sent and returned sendfrom sockets were different.");
            }
            public void ReverseMessageTest()
            {
                Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

                string sendToAddress = "192.168.1.1";
                int sendToPort = 3455;
                string sendFromAddress = "192.168.1.2";
                int sendFromPort = 3244;
                NATKeepAliveMessage keepAliveMsg = new NATKeepAliveMessage(SIPEndPoint.ParseSIPEndPoint(sendToAddress + ":" + sendToPort), new IPEndPoint(IPAddress.Parse(sendFromAddress), sendFromPort));

                byte[] buffer = keepAliveMsg.ToBuffer();
                Assert.IsTrue(buffer != null && buffer.Length == 20, "The byte buffer produced for the NATKeepAliveMessage is invalid.");

                NATKeepAliveMessage rtnMsg = NATKeepAliveMessage.ParseNATKeepAliveMessage(buffer);
                Assert.IsNotNull(rtnMsg, "The NATKeepAliveMessage could not be parsed from the buffer.");

                Assert.IsTrue(rtnMsg.RemoteEndPoint.ToString() == keepAliveMsg.RemoteEndPoint.ToString(), "The sent and returned sendto sockets were different.");
                Assert.IsTrue(rtnMsg.LocalSIPEndPoint.ToString() == keepAliveMsg.LocalSIPEndPoint.ToString(), "The sent and returned sendfrom sockets were different.");
            }
 /// <summary>
 /// This event handler is fired when the SIP Registrar wants to send a NATKeepAlive message to a user contact to make an attempt to keep the connection
 /// open through the user's NAT server. In this case the NATKeepAlive messages are forwarded onto the NATKeepAlive relay which requests the SIPTransport
 /// layer to multiplex a 4 byte null payload onto one of its sockets and send to the specified agent. A typical scenario would have the SIP Registrar
 /// firing this event every 15s to send the null payloads to each of its registered contacts.
 /// </summary>
 private void SendNATKeepAlive(NATKeepAliveMessage keepAliveMessage) {
     try {
         byte[] buffer = keepAliveMessage.ToBuffer();
         m_natKeepAliveSender.Send(buffer, buffer.Length, m_natKeepAliveRelaySocket);
     }
     catch (Exception natSendExcp) {
         logger.Warn("Exception SendNATKeepAlive " + keepAliveMessage.RemoteEndPoint + ". " + natSendExcp.Message);
     }
 }