public SIPConnection(SIPChannel channel, Socket sipSocket, IPEndPoint remoteEndPoint, SIPProtocolsEnum connectionProtocol, SIPConnectionsEnum connectionType)
 {
     LastTransmission = DateTime.Now;
     m_owningChannel = channel;
     SIPSocket = sipSocket;
     RemoteEndPoint = remoteEndPoint;
     ConnectionProtocol = connectionProtocol;
     ConnectionType = connectionType;
 }
        public event SIPMessageReceivedDelegate SIPMessageReceived; // Event for new SIP requests or responses becoming available.

        public SIPStreamConnection(Socket streamSocket, IPEndPoint remoteEndPoint, SIPProtocolsEnum connectionProtocol, SIPConnectionsEnum connectionType)
        {
            StreamSocket       = streamSocket;
            LastTransmission   = DateTime.Now;
            RemoteEndPoint     = remoteEndPoint;
            ConnectionProtocol = connectionProtocol;
            ConnectionType     = connectionType;

            if (ConnectionProtocol == SIPProtocolsEnum.tcp)
            {
                RecvSocketArgs = new SocketAsyncEventArgs();
                RecvSocketArgs.SetBuffer(new byte[2 * MaxSIPTCPMessageSize], 0, 2 * MaxSIPTCPMessageSize);
            }
        }
        /// <summary>
        /// Records the crucial stream connection properties and initialises the required buffers.
        /// </summary>
        /// <param name="streamSocket">The local socket the stream is using.</param>
        /// <param name="remoteEndPoint">The remote network end point of this connection.</param>
        /// <param name="connectionProtocol">Whether the stream is TCP or TLS.</param>
        public SIPStreamConnection(Socket streamSocket, IPEndPoint remoteEndPoint, SIPProtocolsEnum connectionProtocol)
        {
            StreamSocket       = streamSocket;
            LastTransmission   = DateTime.Now;
            RemoteEndPoint     = remoteEndPoint;
            ConnectionProtocol = connectionProtocol;
            ConnectionID       = Crypto.GetRandomInt(CONNECTION_ID_LENGTH).ToString();

            if (ConnectionProtocol == SIPProtocolsEnum.tcp)
            {
                RecvSocketArgs = new SocketAsyncEventArgs();
                RecvSocketArgs.SetBuffer(new byte[2 * MaxSIPTCPMessageSize], 0, 2 * MaxSIPTCPMessageSize);
            }
        }
Beispiel #4
0
 /// <summary>
 /// Helper method to extract the appropriate IP address from a DNS lookup result.
 /// The query may have returned an AAAA or A record. This method checks which
 /// and extracts the IP address accordingly.
 /// </summary>
 /// <param name="addrRecord">The DNS lookup result.</param>
 /// <param name="port">The port for the IP end point.</param>
 /// <returns>An IP end point or null.</returns>
 private static SIPEndPoint GetFromLookupResult(SIPProtocolsEnum protocol, DnsResourceRecord addrRecord, int port)
 {
     if (addrRecord is AaaaRecord)
     {
         return(new SIPEndPoint(protocol, (addrRecord as AaaaRecord).Address, port));
     }
     else if (addrRecord is ARecord)
     {
         return(new SIPEndPoint(protocol, (addrRecord as ARecord).Address, port));
     }
     else
     {
         return(null);
     }
 }
        public static void DNSSRVRecordLookup(SIPSchemesEnum scheme, SIPProtocolsEnum protocol, string host, bool async,
                                              ref SIPDNSLookupResult lookupResult)
        {
            SIPServicesEnum reqdNAPTRService = SIPServicesEnum.none;

            if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.udp)
            {
                reqdNAPTRService = SIPServicesEnum.sipudp;
            }
            else if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.tcp)
            {
                reqdNAPTRService = SIPServicesEnum.siptcp;
            }
            else if (scheme == SIPSchemesEnum.sips && protocol == SIPProtocolsEnum.tcp)
            {
                reqdNAPTRService = SIPServicesEnum.sipstcp;
            }
            else if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.tls)
            {
                reqdNAPTRService = SIPServicesEnum.siptls;
            }

            // If there are NAPTR records available see if there is a matching one for the SIP scheme and protocol required.
            SIPDNSServiceResult naptrService = null;

            if (lookupResult.SIPNAPTRResults != null && lookupResult.SIPNAPTRResults.Count > 0)
            {
                if (reqdNAPTRService != SIPServicesEnum.none &&
                    lookupResult.SIPNAPTRResults.ContainsKey(reqdNAPTRService))
                {
                    naptrService = lookupResult.SIPNAPTRResults[reqdNAPTRService];
                }
            }

            // Construct the SRV target to lookup depending on whether an NAPTR record was available or not.
            string srvLookup = null;

            if (naptrService != null)
            {
                srvLookup = naptrService.Data;
            }
            else
            {
                srvLookup = "_" + scheme.ToString() + "._" + protocol.ToString() + "." + host;
            }
        }
Beispiel #6
0
        /// <summary>
        /// Creates a SIP channel to listen for and send SIP messages over TCP.
        /// </summary>
        /// <param name="endPoint">The IP end point to listen on and send from.</param>
        /// <param name="protocol">Whether the channel is being used with TCP or TLS (TLS channels get upgraded once connected).</param>
        /// <param name="canListen">Indicates whether the channel is capable of listening for new client connections.
        /// A TLS channel without a certificate cannot listen.</param>
        public SIPTCPChannel(IPEndPoint endPoint, SIPProtocolsEnum protocol, bool canListen = true) : base()
        {
            if (endPoint == null)
            {
                throw new ArgumentNullException("endPoint", "The end point must be specified when creating a SIPTCPChannel.");
            }

            ListeningIPAddress = endPoint.Address;
            Port        = endPoint.Port;
            SIPProtocol = protocol;
            IsReliable  = true;

            if (canListen)
            {
                StartListening(endPoint);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Creates a SIP channel to listen for and send SIP messages over TCP.
        /// </summary>
        /// <param name="endPoint">The IP end point to send from and optionally listen on.</param>
        /// <param name="protocol">Whether the channel is being used with TCP or TLS (TLS channels get upgraded once connected).</param>
        /// <param name="sipBodyEncoding"></param>
        /// <param name="canListen">Indicates whether the channel is capable of listening for new client connections.
        /// A TLS channel without a certificate cannot listen.</param>
        /// <param name="sipEncoding"></param>
        public SIPTCPChannel(
            IPEndPoint endPoint,
            SIPProtocolsEnum protocol,
            Encoding sipEncoding,
            Encoding sipBodyEncoding,
            bool canListen,
            bool useDualMode) : base(sipEncoding, sipBodyEncoding)
        {
            if (endPoint == null)
            {
                throw new ArgumentNullException("endPoint", "The end point must be specified when creating a SIPTCPChannel.");
            }

            SIPProtocol = protocol;
            IsReliable  = true;

            m_channelSocket = new Socket(endPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            m_channelSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            m_channelSocket.LingerState = new LingerOption(true, 0);

            if (useDualMode && endPoint.AddressFamily == AddressFamily.InterNetworkV6)
            {
                m_isDualMode             = true;
                m_channelSocket.DualMode = true;
            }

            m_channelSocket.Bind(endPoint);

            Port = (m_channelSocket.LocalEndPoint as IPEndPoint).Port;
            ListeningIPAddress = (m_channelSocket.LocalEndPoint as IPEndPoint).Address;

            logger.LogInformation($"SIP {ProtDescr} Channel created for {ListeningSIPEndPoint}.");

            if (canListen)
            {
                m_channelSocket.Listen(MAX_TCP_CONNECTIONS);

                Task.Factory.StartNew(AcceptConnections, TaskCreationOptions.LongRunning);
                Task.Factory.StartNew(PruneConnections, TaskCreationOptions.LongRunning);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Attempts to resolve a hostname.
        /// </summary>
        /// <param name="host">The hostname to resolve.</param>
        /// <param name="port">The service port to use in the end pint result (not used for the lookup).</param>
        /// <param name="queryType">The lookup query type, either A or AAAA.</param>
        /// <returns>If successful an IPEndPoint or null if not.</returns>
        private static SIPEndPoint HostQuery(SIPProtocolsEnum protocol, string host, int port, QueryType queryType)
        {
            try
            {
                var addrRecord = _lookupClient.Query(host, queryType).Answers.FirstOrDefault();
                if (addrRecord != null)
                {
                    return(GetFromLookupResult(protocol, addrRecord, port));
                }
            }
            catch (Exception excp)
            {
                logger.LogWarning($"SIP DNS lookup failure for {host} and query {queryType}. {excp.Message}");
            }

            if (queryType == QueryType.AAAA)
            {
                return(HostQuery(protocol, host, port, QueryType.A));
            }

            return(null);
        }
Beispiel #9
0
        /// <summary>
        /// Gets the default SIP port for the protocol.
        /// </summary>
        /// <param name="protocol">The transport layer protocol to get the port for.</param>
        /// <returns>The default port to use.</returns>
        public static int GetDefaultPort(SIPProtocolsEnum protocol)
        {
            switch (protocol)
            {
            case SIPProtocolsEnum.udp:
                return(DEFAULT_SIP_PORT);

            case SIPProtocolsEnum.tcp:
                return(DEFAULT_SIP_PORT);

            case SIPProtocolsEnum.tls:
                return(DEFAULT_SIP_TLS_PORT);

            case SIPProtocolsEnum.ws:
                return(DEFAULT_SIP_WEBSOCKET_PORT);

            case SIPProtocolsEnum.wss:
                return(DEFAULT_SIPS_WEBSOCKET_PORT);

            default:
                throw new ApplicationException($"Protocol {protocol} was not recognised in GetDefaultPort.");
            }
        }
Beispiel #10
0
 public void SIPTransportRequestReceived(SIPProtocolsEnum protocol, IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, SIPRequest sipRequest)
 {
     if (sipRequest.Method == SIPMethodsEnum.BYE)
     {
         // Send an Ok response.
         SIPResponse okResponse = GetResponse(sipRequest.Header, SIPResponseStatusCodesEnum.Ok);
         m_sipTransport.SendResponseFrom(localEndPoint, remoteEndPoint, protocol, okResponse);
     }
     else if (sipRequest.Method == SIPMethodsEnum.NOTIFY)
     {
         // Send an not supported response.
         //SIPResponse notSupportedResponse = GetResponse(sipRequest.Header, SIPResponseStatusCodesEnum.MethodNotAllowed);
         //SIPTransport.SendResponseFrom(localEndPoint, remoteEndPoint, notSupportedResponse);
         // Send an Ok response.
         SIPResponse okResponse = GetResponse(sipRequest.Header, SIPResponseStatusCodesEnum.Ok);
         okResponse.Header.Contact = null;
         m_sipTransport.SendResponseFrom(localEndPoint, remoteEndPoint, protocol, okResponse);
     }
     else
     {
         m_lastRequest = sipRequest;
         m_waitForSIPRequest.Set();
     }
 }
 public SIPEndPoint(SIPProtocolsEnum protocol, IPAddress address, int port)
 {
     Protocol = protocol;
     Address = address;
     Port = port;
 }
        private static IEnumerable <SIPEndPoint> GetSIPEndPoints(string sipSocketString, SIPProtocolsEnum sipProtocol, int overridePort)
        {
            if (sipSocketString == null)
            {
                return(null);
            }

            int port;

            if (overridePort > 0)
            {
                port = overridePort;
            }
            else
            {
                port = IPSocket.ParsePortFromSocket(sipSocketString);
                if (port == 0)
                {
                    port = (sipProtocol == SIPProtocolsEnum.tls) ? m_defaultSIPTLSPort : m_defaultSIPPort;
                }
            }

            if (sipSocketString.StartsWith(m_allIPAddresses))
            {
                return(m_localIPAddresses.Select(x => new SIPEndPoint(sipProtocol, new IPEndPoint(x, port))));
            }

            var ipAddress = IPAddress.Parse(IPSocket.ParseHostFromSocket(sipSocketString));

            return(new List <SIPEndPoint> {
                new SIPEndPoint(sipProtocol, new IPEndPoint(ipAddress, port))
            });
        }
Beispiel #13
0
 /// <summary>
 /// Instantiates a new SIP end point from a network end point. Non specified properties
 /// will be set to their defaults.
 /// </summary>
 public SIPEndPoint(SIPProtocolsEnum protocol, IPAddress address, int port) :
     this(protocol, address, port, null, null)
 {
 }
Beispiel #14
0
 public SIPEndPoint(SIPProtocolsEnum protocol, IPAddress address, int port)
 {
     Protocol = protocol;
     Address  = address;
     Port     = (port == 0) ? (Protocol == SIPProtocolsEnum.tls) ? m_defaultSIPTLSPort : m_defaultSIPPort : port;
 }
        private static List<SIPEndPoint> GetSIPEndPoints(string sipSocketString, SIPProtocolsEnum sipProtocol)
        {
            if (sipSocketString == null)
            {
                return null;
            }
            else
            {
                int port = IPSocket.ParsePortFromSocket(sipSocketString);
                if (port == 0)
                {
                    port = (sipProtocol == SIPProtocolsEnum.tls) ? m_defaultSIPTLSPort : m_defaultSIPPort;
                }

                if (sipSocketString.StartsWith(m_allIPAddresses))
                {
                    List<SIPEndPoint> sipEndPoints = new List<SIPEndPoint>();

                    foreach (IPAddress ipAddress in m_localIPAddresses)
                    {
                        sipEndPoints.Add(new SIPEndPoint(sipProtocol, new IPEndPoint(ipAddress, port)));
                    }

                    return sipEndPoints;
                }
                else
                {
                    IPAddress ipAddress = IPAddress.Parse(IPSocket.ParseHostFromSocket(sipSocketString));
                    return new List<SIPEndPoint>() { new SIPEndPoint(sipProtocol, new IPEndPoint(ipAddress, port)) };
                }
            }
        }
        public static void DNSSRVRecordLookup(SIPSchemesEnum scheme, SIPProtocolsEnum protocol, string host, bool async, ref SIPDNSLookupResult lookupResult)
        {
            SIPServicesEnum reqdNAPTRService = SIPServicesEnum.none;

            if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.udp)
            {
                reqdNAPTRService = SIPServicesEnum.sipudp;
            }
            else if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.tcp)
            {
                reqdNAPTRService = SIPServicesEnum.siptcp;
            }
            else if (scheme == SIPSchemesEnum.sips && protocol == SIPProtocolsEnum.tcp)
            {
                reqdNAPTRService = SIPServicesEnum.sipstcp;
            }
            else if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.tls)
            {
                reqdNAPTRService = SIPServicesEnum.siptls;
            }

            // If there are NAPTR records available see if there is a matching one for the SIP scheme and protocol required.
            SIPDNSServiceResult naptrService = null;

            if (lookupResult.SIPNAPTRResults != null && lookupResult.SIPNAPTRResults.Count > 0)
            {
                if (reqdNAPTRService != SIPServicesEnum.none && lookupResult.SIPNAPTRResults.ContainsKey(reqdNAPTRService))
                {
                    naptrService = lookupResult.SIPNAPTRResults[reqdNAPTRService];
                }
            }

            // Construct the SRV target to lookup depending on whether an NAPTR record was available or not.
            string srvLookup = null;

            if (naptrService != null)
            {
                srvLookup = naptrService.Data;
            }
            else
            {
                srvLookup = "_" + scheme.ToString() + "._" + protocol.ToString() + "." + host;
            }

            SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup requested for " + srvLookup + ".", null));

            DNSResponse srvRecordResponse = DNSManager.Lookup(srvLookup, DNSQType.SRV, DNS_LOOKUP_TIMEOUT, null, true, async);

            if (srvRecordResponse == null && async)
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup pending for " + srvLookup + ".", null));
                lookupResult.Pending = true;
            }
            else if (srvRecordResponse.Timedout)
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup timed out for " + srvLookup + ".", null));
                lookupResult.SRVTimedoutAt = DateTime.Now;
            }
            else if (srvRecordResponse.Error != null)
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup for " + srvLookup + " returned error of " + lookupResult.LookupError + ".", null));
            }
            else if (srvRecordResponse.Error == null && srvRecordResponse.RecordSRV != null && srvRecordResponse.RecordSRV.Length > 0)
            {
                foreach (RecordSRV srvRecord in srvRecordResponse.RecordSRV)
                {
                    SIPDNSServiceResult sipSRVResult = new SIPDNSServiceResult(reqdNAPTRService, srvRecord.Priority, srvRecord.Weight, srvRecord.RR.TTL, srvRecord.Target, srvRecord.Port, DateTime.Now);
                    lookupResult.AddSRVResult(sipSRVResult);
                    SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record found for " + srvLookup + ", result " + srvRecord.Target + " " + srvRecord.Port + ".", null));
                }
            }
            else
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS no SRV records found for " + srvLookup + ".", null));
            }
        }
Beispiel #17
0
        private static IEnumerable <SIPEndPoint> GetSIPEndPoints(string sipSocketString, SIPProtocolsEnum sipProtocol, int overridePort)
        {
            if (sipSocketString == null)
            {
                return(null);
            }

            int port;

            if (overridePort > 0)
            {
                port = overridePort;
            }
            else
            {
                port = IPSocket.ParsePortFromSocket(sipSocketString);
                if (port == 0)
                {
                    port = (sipProtocol == SIPProtocolsEnum.tls) ? m_defaultSIPTLSPort : m_defaultSIPPort;
                }
            }

            if (sipSocketString.StartsWith(ALL_LOCAL_IPADDRESSES_KEY))
            {
                return(new List <SIPEndPoint> {
                    new SIPEndPoint(sipProtocol, new IPEndPoint(IPAddress.Any, port))
                });
            }
            else
            {
                var ipAddress = IPAddress.Parse(IPSocket.ParseHostFromSocket(sipSocketString));
                return(new List <SIPEndPoint> {
                    new SIPEndPoint(sipProtocol, new IPEndPoint(ipAddress, port))
                });
            }
        }
Beispiel #18
0
 public SIPEndPoint(SIPProtocolsEnum protocol, IPEndPoint endPoint) :
     this(protocol, endPoint, null, null)
 {
 }
Beispiel #19
0
        public static void DNSSRVRecordLookup(SIPSchemesEnum scheme, SIPProtocolsEnum protocol, string host, bool async, ref SIPDNSLookupResult lookupResult)
        {
            SIPServicesEnum reqdNAPTRService = SIPServicesEnum.none;

            if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.udp)
            {
                reqdNAPTRService = SIPServicesEnum.sipudp;
            }
            else if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.tcp)
            {
                reqdNAPTRService = SIPServicesEnum.siptcp;
            }
            else if (scheme == SIPSchemesEnum.sips && protocol == SIPProtocolsEnum.tcp)
            {
                reqdNAPTRService = SIPServicesEnum.sipstcp;
            }
            //rj2 2018-10-17: this looks wrong, but the www says: if sips then protocol is _tcp not _tls
            else if (scheme == SIPSchemesEnum.sips && protocol == SIPProtocolsEnum.tls)
            {
                reqdNAPTRService = SIPServicesEnum.sipstcp;
            }
            else if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.tls)
            {
                reqdNAPTRService = SIPServicesEnum.siptls;
            }

            // If there are NAPTR records available see if there is a matching one for the SIP scheme and protocol required.
            // this works best (only works) if protocol in sip-uri matches sip-service of NAPTRRecord
            SIPDNSServiceResult naptrService = null;

            if (lookupResult.SIPNAPTRResults != null && lookupResult.SIPNAPTRResults.Count > 0)
            {
                if (reqdNAPTRService != SIPServicesEnum.none && lookupResult.SIPNAPTRResults.ContainsKey(reqdNAPTRService))
                {
                    naptrService = lookupResult.SIPNAPTRResults[reqdNAPTRService];
                }
            }

            // Construct the SRV target to lookup depending on whether an NAPTR record was available or not.
            string srvLookup = null;

            if (naptrService != null)
            {
                srvLookup = naptrService.Data;
            }
            else
            {
                if (scheme == SIPSchemesEnum.sips)
                {
                    srvLookup = SIPDNSConstants.SRV_SIPS_TCP_QUERY_PREFIX + host;
                }
                else
                {
                    srvLookup = "_" + scheme.ToString() + "._" + protocol.ToString() + "." + host;
                }
            }

            SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup requested for " + srvLookup + ".", null));

            DNSResponse srvRecordResponse = DNSManager.Lookup(srvLookup, QType.SRV, DNS_LOOKUP_TIMEOUT, null, true, async);

            if (srvRecordResponse == null && async)
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup pending for " + srvLookup + ".", null));
                lookupResult.Pending = true;
            }
            else if (srvRecordResponse.Timedout)
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup timed out for " + srvLookup + ".", null));
                lookupResult.SRVTimedoutAt = DateTime.Now;
            }
            else if (!string.IsNullOrWhiteSpace(srvRecordResponse.Error))
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup for " + srvLookup + " returned error of " + lookupResult.LookupError + ".", null));
            }
            else if (string.IsNullOrWhiteSpace(srvRecordResponse.Error) && srvRecordResponse.RecordSRV != null && srvRecordResponse.RecordSRV.Length > 0)
            {
                foreach (RecordSRV srvRecord in srvRecordResponse.RecordSRV)
                {
                    SIPDNSServiceResult sipSRVResult = new SIPDNSServiceResult(reqdNAPTRService, srvRecord.PRIORITY, srvRecord.WEIGHT, srvRecord.RR.TTL, srvRecord.TARGET, srvRecord.PORT, DateTime.Now);
                    lookupResult.AddSRVResult(sipSRVResult);
                    SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record found for " + srvLookup + ", result " + srvRecord.TARGET + " " + srvRecord.PORT + ".", null));
                }
            }
            else
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS no SRV records found for " + srvLookup + ".", null));
            }
        }
 public LocalSIPSocket(string socket, SIPProtocolsEnum protocol)
 {
     Socket = socket;
     Protocol = protocol;
 }
        private bool IsLocalSIPSocket(string socket, SIPProtocolsEnum protocol)
        {
            foreach (LocalSIPSocket sipSocket in m_sipSockets)
            {
                if (sipSocket.Socket == socket && sipSocket.Protocol == protocol)
                {
                    return true;
                }
            }

            return false;
        }
Beispiel #22
0
 public SIPEndPoint(SIPProtocolsEnum protocol, IPEndPoint endPoint, string channelID, string connectionID) :
     this(protocol, endPoint.Address, endPoint.Port, channelID, connectionID)
 {
 }
Beispiel #23
0
        public SIPURI(string user, string host, string paramsAndHeaders, SIPSchemesEnum scheme, SIPProtocolsEnum protocol)
        {
            User = user;
            Host = host;
            ParseParamsAndHeaders(paramsAndHeaders);
            Scheme = scheme;

            if (protocol != SIPProtocolsEnum.udp)
            {
                Parameters.Set(m_uriParamTransportKey, protocol.ToString());
            }
        }
 public SIPEndPoint GetDefaultSIPEndPoint(SIPProtocolsEnum protocol)
 {
     return m_sipTransport.GetDefaultSIPEndPoint(protocol);
 }
Beispiel #25
0
 /// <summary>
 /// Instantiates a new SIP end point from a network end point. Non specified properties
 /// will be set to their defaults.
 /// </summary>
 public SIPEndPoint(SIPProtocolsEnum protocol, IPAddress address, int port)
 {
     Protocol = protocol;
     Address  = address;
     Port     = port;
 }
 public SIPTCPChannel(IPEndPoint endPoint, SIPProtocolsEnum protocol)
 {
     m_localSIPEndPoint = new SIPEndPoint(protocol, endPoint);
     m_isReliable       = true;
     Initialise();
 }
Beispiel #27
0
 public override bool IsProtocolSupported(SIPProtocolsEnum protocol)
 {
     return(true);
 }
Beispiel #28
0
        public static void DNSSRVRecordLookup(SIPSchemesEnum scheme, SIPProtocolsEnum protocol, string host, bool async, ref SIPDNSLookupResult lookupResult)
        {
            SIPServicesEnum reqdNAPTRService = SIPServicesEnum.none;
            if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.udp)
            {
                reqdNAPTRService = SIPServicesEnum.sipudp;
            }
            else if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.tcp)
            {
                reqdNAPTRService = SIPServicesEnum.siptcp;
            }
            else if (scheme == SIPSchemesEnum.sips && protocol == SIPProtocolsEnum.tcp)
            {
                reqdNAPTRService = SIPServicesEnum.sipstcp;
            }
            else if (scheme == SIPSchemesEnum.sip && protocol == SIPProtocolsEnum.tls)
            {
                reqdNAPTRService = SIPServicesEnum.siptls;
            }

            // If there are NAPTR records available see if there is a matching one for the SIP scheme and protocol required.
            SIPDNSServiceResult naptrService = null;
            if (lookupResult.SIPNAPTRResults != null && lookupResult.SIPNAPTRResults.Count > 0)
            {
                if (reqdNAPTRService != SIPServicesEnum.none && lookupResult.SIPNAPTRResults.ContainsKey(reqdNAPTRService))
                {
                    naptrService = lookupResult.SIPNAPTRResults[reqdNAPTRService];
                }
            }

            // Construct the SRV target to lookup depending on whether an NAPTR record was available or not.
            string srvLookup = null;
            if (naptrService != null)
            {
                srvLookup = naptrService.Data;
            }
            else
            {
                srvLookup = "_" + scheme.ToString() + "._" + protocol.ToString() + "." + host;
            }

            SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup requested for " + srvLookup + ".", null));

            DNSResponse srvRecordResponse = DNSManager.Lookup(srvLookup, DNSQType.SRV, DNS_LOOKUP_TIMEOUT, null, true, async);
            if (srvRecordResponse == null && async)
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup pending for " + srvLookup + ".", null));
                lookupResult.Pending = true;
            }
            else if (srvRecordResponse.Timedout)
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup timed out for " + srvLookup + ".", null));
                lookupResult.SRVTimedoutAt = DateTime.Now;
            }
            else if (srvRecordResponse.Error != null)
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record lookup for " + srvLookup + " returned error of " + lookupResult.LookupError + ".", null));
            }
            else if (srvRecordResponse.Error == null && srvRecordResponse.RecordSRV != null && srvRecordResponse.RecordSRV.Length > 0)
            {
                foreach (RecordSRV srvRecord in srvRecordResponse.RecordSRV)
                {
                    SIPDNSServiceResult sipSRVResult = new SIPDNSServiceResult(reqdNAPTRService, srvRecord.Priority, srvRecord.Weight, srvRecord.RR.TTL, srvRecord.Target, srvRecord.Port, DateTime.Now);
                    lookupResult.AddSRVResult(sipSRVResult);
                    SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS SRV record found for " + srvLookup + ", result " + srvRecord.Target + " " + srvRecord.Port + ".", null));
                }
            }
            else
            {
                SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS no SRV records found for " + srvLookup + ".", null));
            }
        }
Beispiel #29
0
        public SIPEndPoint GetDefaultSIPEndPoint(SIPProtocolsEnum protocol)
        {
            foreach (SIPChannel sipChannel in m_sipChannels.Values)
            {
                if (sipChannel.SIPChannelEndPoint.Protocol == protocol)
                {
                    return sipChannel.SIPChannelEndPoint;
                }
            }

            return null;
        }
 /// <summary>
 /// Checks whether the specified protocol is supported.
 /// </summary>
 /// <param name="protocol">The protocol to check.</param>
 /// <returns>True if supported, false if not.</returns>
 public override bool IsProtocolSupported(SIPProtocolsEnum protocol)
 {
     // We can establish client web sockets to both ws and wss servers.
     return(protocol == SIPProtocolsEnum.ws || protocol == SIPProtocolsEnum.wss);
 }
Beispiel #31
0
        //public SIPConnection(SIPChannel channel, Socket sipSocket, IPEndPoint remoteEndPoint, SIPProtocolsEnum connectionProtocol, SIPConnectionsEnum connectionType)
        //{
        //    LastTransmission = DateTime.Now;
        //    m_owningChannel = channel;
        //    SIPSocket = sipSocket;
        //    RemoteEndPoint = remoteEndPoint;
        //    ConnectionProtocol = connectionProtocol;
        //    ConnectionType = connectionType;
        //}

        public SIPConnection(SIPChannel channel, TcpClient tcpClient, Stream sipStream, IPEndPoint remoteEndPoint, SIPProtocolsEnum connectionProtocol, SIPConnectionsEnum connectionType)
        {
            LastTransmission   = DateTime.Now;
            m_owningChannel    = channel;
            _tcpClient         = tcpClient;
            SIPStream          = sipStream;
            RemoteEndPoint     = remoteEndPoint;
            ConnectionProtocol = connectionProtocol;
            ConnectionType     = connectionType;
        }
 public SIPEndPoint(SIPProtocolsEnum protocol, IPEndPoint endPoint)
 {
     Protocol = protocol;
     Address  = endPoint.Address;
     Port     = endPoint.Port;
 }
Beispiel #33
0
 public void SIPTransportRequestReceived(SIPProtocolsEnum protocol, IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, SIPRequest sipRequest)
 {
     if (sipRequest.Method == SIPMethodsEnum.BYE)
     {
         // Send an Ok response.
         SIPResponse okResponse = GetResponse(sipRequest.Header, SIPResponseStatusCodesEnum.Ok);
         m_sipTransport.SendResponseFrom(localEndPoint, remoteEndPoint, protocol, okResponse);
     }
     else if (sipRequest.Method == SIPMethodsEnum.NOTIFY)
     {
         // Send an not supported response.
         //SIPResponse notSupportedResponse = GetResponse(sipRequest.Header, SIPResponseStatusCodesEnum.MethodNotAllowed);
         //SIPTransport.SendResponseFrom(localEndPoint, remoteEndPoint, notSupportedResponse);
         // Send an Ok response.
         SIPResponse okResponse = GetResponse(sipRequest.Header, SIPResponseStatusCodesEnum.Ok);
         okResponse.Header.Contact = null;
         m_sipTransport.SendResponseFrom(localEndPoint, remoteEndPoint, protocol, okResponse);
     }
     else
     {
         m_lastRequest = sipRequest;
         m_waitForSIPRequest.Set();
     }
 }
Beispiel #34
0
 public SIPEndPoint(SIPProtocolsEnum protocol, IPEndPoint endPoint)
 {
     Protocol = protocol;
     Address  = endPoint.Address;
     Port     = (endPoint.Port == 0) ? (Protocol == SIPProtocolsEnum.tls) ? m_defaultSIPTLSPort : m_defaultSIPPort : endPoint.Port;
 }
        public static SIPEndPoint ParseSIPEndPoint(string sipEndPointStr)
        {
            try
            {
                if (sipEndPointStr.IsNullOrBlank())
                {
                    return(null);
                }

                if (sipEndPointStr.StartsWith("udp") || sipEndPointStr.StartsWith("tcp") || sipEndPointStr.StartsWith("tls"))
                {
                    return(ParseSerialisedSIPEndPoint(sipEndPointStr));
                }

                string           ipAddress = null;
                int              port      = 0;
                SIPProtocolsEnum protocol  = SIPProtocolsEnum.udp;

                if (sipEndPointStr.StartsWith("sip:"))
                {
                    sipEndPointStr = sipEndPointStr.Substring(4);
                }
                else if (sipEndPointStr.StartsWith("sips:"))
                {
                    sipEndPointStr = sipEndPointStr.Substring(5);
                    protocol       = SIPProtocolsEnum.tls;
                }

                int colonIndex     = sipEndPointStr.IndexOf(':');
                int semiColonIndex = sipEndPointStr.IndexOf(';');
                if (colonIndex == -1 && semiColonIndex == -1)
                {
                    ipAddress = sipEndPointStr;
                }
                else if (colonIndex != -1 && semiColonIndex == -1)
                {
                    ipAddress = sipEndPointStr.Substring(0, colonIndex);
                    port      = Convert.ToInt32(sipEndPointStr.Substring(colonIndex + 1));
                }
                else
                {
                    if (colonIndex != -1 && colonIndex < semiColonIndex)
                    {
                        ipAddress = sipEndPointStr.Substring(0, colonIndex);
                        port      = Convert.ToInt32(sipEndPointStr.Substring(colonIndex + 1, semiColonIndex - colonIndex - 1));
                    }
                    else
                    {
                        ipAddress = sipEndPointStr.Substring(0, semiColonIndex);
                    }

                    if (protocol != SIPProtocolsEnum.tls)
                    {
                        sipEndPointStr = sipEndPointStr.Substring(semiColonIndex + 1);
                        int transportIndex = sipEndPointStr.ToLower().IndexOf(m_transportParameterKey + "=");
                        if (transportIndex != -1)
                        {
                            sipEndPointStr = sipEndPointStr.Substring(transportIndex + 10);
                            semiColonIndex = sipEndPointStr.IndexOf(';');
                            if (semiColonIndex != -1)
                            {
                                protocol = SIPProtocolsType.GetProtocolType(sipEndPointStr.Substring(0, semiColonIndex));
                            }
                            else
                            {
                                protocol = SIPProtocolsType.GetProtocolType(sipEndPointStr);
                            }
                        }
                    }
                }

                if (port == 0)
                {
                    port = (protocol == SIPProtocolsEnum.tls) ? m_defaultSIPTLSPort : m_defaultSIPPort;
                }

                return(new SIPEndPoint(protocol, IPAddress.Parse(ipAddress), port));
            }
            catch //(Exception excp)
            {
                //logger.Error("Exception ParseSIPEndPoint (sipEndPointStr=" + sipEndPointStr + "). " + excp.Message);
                throw;
            }
        }
Beispiel #36
0
        /// <summary>
        /// Returns the first SIPChannel found for the requested protocol.
        /// </summary>
        /// <param name="protocol"></param>
        /// <returns></returns>
        private SIPChannel GetDefaultChannel(SIPProtocolsEnum protocol)
        {
            // Channels that are not on a loopback address take priority.
            foreach (SIPChannel sipChannel in m_sipChannels.Values)
            {
                if (sipChannel.SIPChannelEndPoint.Protocol == protocol && !IPAddress.IsLoopback(sipChannel.SIPChannelEndPoint.Address))
                {
                    return sipChannel;
                }
            }
            foreach (SIPChannel sipChannel in m_sipChannels.Values)
            {
                if (sipChannel.SIPChannelEndPoint.Protocol == protocol)
                {
                    return sipChannel;
                }
            }

            logger.Warn("No default SIP channel could be found for " + protocol + ".");
            return null;
        }
 public SIPEndPoint(SIPProtocolsEnum protocol, IPAddress address, int port)
 {
     Protocol = protocol;
     Address = address;
     Port = (port == 0) ? (Protocol == SIPProtocolsEnum.tls) ? m_defaultSIPTLSPort : m_defaultSIPPort : port;
 }
Beispiel #38
0
        public SIPEndPoint GetDefaultTransportContact(SIPProtocolsEnum protocol)
        {
            SIPChannel defaultChannel = GetDefaultChannel(protocol);

            if (defaultChannel != null)
            {
                return defaultChannel.SIPChannelEndPoint;
            }
            else
            {
                return null;
            }
        }
 public SIPEndPoint(SIPProtocolsEnum protocol, IPEndPoint endPoint)
 {
     Protocol = protocol;
     Address = endPoint.Address;
     Port = endPoint.Port;
 }
Beispiel #40
0
 /// <summary>
 /// Returns true if the channel supports the requested transport layer protocol.
 /// </summary>
 public abstract bool IsProtocolSupported(SIPProtocolsEnum protocol);
 public void PushRoute(IPEndPoint socket, SIPSchemesEnum scheme, SIPProtocolsEnum protcol)
 {
     m_sipRoutes.Insert(0, new SIPRoute(scheme + ":" + socket.ToString(), true));
 }
        public SIPURI(string user, string host, string paramsAndHeaders, SIPSchemesEnum scheme, SIPProtocolsEnum protocol)
        {
            User = user;
            Host = host;
            ParseParamsAndHeaders(paramsAndHeaders);
            Scheme = scheme;

            if (protocol != SIPProtocolsEnum.udp && scheme != SIPSchemesEnum.sips)
            {
                Parameters.Set(m_uriParamTransportKey, protocol.ToString());
            }
        }
 public SIPViaHeader(IPEndPoint contactEndPoint, string branch, SIPProtocolsEnum protocol) :
     this(contactEndPoint.Address.ToString(), contactEndPoint.Port, branch, protocol)
 { }
 public SIPViaHeader(string contactIPAddress, int contactPort, string branch, SIPProtocolsEnum protocol)
 {
     Version = SIPConstants.SIP_FULLVERSION_STRING;
     Transport = protocol;
     Host = contactIPAddress;
     Port = contactPort;
     Branch = branch;
     ViaParameters.Set(m_rportKey, null);
 }
 public SIPEndPoint GetDefaultSIPEndPoint(SIPProtocolsEnum protocol)
 {
     return(m_sipTransport.GetDefaultSIPEndPoint(protocol));
 }
 /// <summary>
 /// Checks whether the specified protocol is supported.
 /// </summary>
 /// <param name="protocol">The protocol to check.</param>
 /// <returns>True if supported, false if not.</returns>
 public override bool IsProtocolSupported(SIPProtocolsEnum protocol)
 {
     return(protocol == SIPProtocolsEnum.udp);
 }
Beispiel #47
0
 public SIPEndPoint(SIPProtocolsEnum protocol, IPEndPoint endPoint)
 {
     Protocol = protocol;
     Address  = endPoint.Address;
     Port     = (endPoint.Port == 0) ? SIPConstants.GetDefaultPort(Protocol) : endPoint.Port;
 }