public static SIPDNSLookupResult DNSARecordLookup(string host, int port, bool async, SIPURI uri) { SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS A record lookup requested for " + host + ".", null)); SIPDNSLookupResult result = new SIPDNSLookupResult(uri); DNSResponse aRecordResponse = DNSManager.Lookup(host, DNSQType.A, DNS_A_RECORD_LOOKUP_TIMEOUT, null, true, async); if (aRecordResponse == null && async) { result.Pending = true; } else if (aRecordResponse.Timedout) { result.ATimedoutAt = DateTime.Now; } else if (aRecordResponse.Error != null) { result.LookupError = aRecordResponse.Error; } else if (aRecordResponse.RecordsA == null || aRecordResponse.RecordsA.Length == 0) { SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS no A records found for " + host + ".", null)); result.LookupError = "No A records found for " + host + "."; } else { SIPURI sipURI = result.URI; foreach (RecordA aRecord in aRecordResponse.RecordsA) { SIPDNSLookupEndPoint sipLookupEndPoint = new SIPDNSLookupEndPoint(new SIPEndPoint(sipURI.Protocol, new IPEndPoint(aRecord.Address, port)), aRecord.RR.TTL); result.AddLookupResult(sipLookupEndPoint); SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS A record found for " + host + ", result " + sipLookupEndPoint.LookupEndPoint.ToString() + ".", null)); } } return(result); }
public static SIPDNSLookupResult ResolveSIPService(SIPURI sipURI, bool async) { try { if (sipURI == null) { throw new ArgumentNullException("sipURI", "Cannot resolve SIP service on a null URI."); } string host = sipURI.Host; int port = (sipURI.Scheme == SIPSchemesEnum.sip) ? m_defaultSIPPort : m_defaultSIPSPort; bool explicitPort = false; if (sipURI.Host.IndexOf(':') != -1) { host = sipURI.Host.Split(':')[0]; Int32.TryParse(sipURI.Host.Split(':')[1], out port); explicitPort = true; } if (Regex.Match(host, @"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$").Success) { // Target is an IP address, no DNS lookup required. IPAddress hostIP = IPAddress.Parse(host); SIPDNSLookupEndPoint sipLookupEndPoint = new SIPDNSLookupEndPoint(new SIPEndPoint(sipURI.Protocol, new IPEndPoint(hostIP, port)), 0); SIPDNSLookupResult result = new SIPDNSLookupResult(sipURI); result.AddLookupResult(sipLookupEndPoint); return(result); } else if (explicitPort) { // Target is a hostname with an explicit port, DNS lookup for A or AAAA record. return(DNSARecordLookup(host, port, async, sipURI)); } else { // Target is a hostname with no explicit port, use the whole NAPTR->SRV->A lookup procedure. SIPDNSLookupResult sipLookupResult = new SIPDNSLookupResult(sipURI); // Do without the NAPTR lookup for the time being. Very few organisations appear to use them and it can cost up to 2.5s to get a failed resolution. /*SIPMonitorLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Unknown, SIPMonitorEventTypesEnum.DNS, "SIP DNS full lookup requested for " + sipURI.ToString() + ".", null)); * DNSNAPTRRecordLookup(host, async, ref sipLookupResult); * if (sipLookupResult.Pending) * { * if (!m_inProgressSIPServiceLookups.Contains(sipURI.ToString())) * { * m_inProgressSIPServiceLookups.Add(sipURI.ToString()); * ThreadPool.QueueUserWorkItem(delegate { ResolveSIPService(sipURI, false); }); * } * return sipLookupResult; * }*/ DNSSRVRecordLookup(sipURI.Scheme, sipURI.Protocol, host, async, ref sipLookupResult); if (sipLookupResult.Pending) { //logger.Debug("SIPDNSManager SRV lookup for " + host + " is pending."); return(sipLookupResult); } else { //logger.Debug("SIPDNSManager SRV lookup for " + host + " is final."); SIPDNSServiceResult nextSRVRecord = sipLookupResult.GetNextUnusedSRV(); int lookupPort = (nextSRVRecord != null) ? nextSRVRecord.Port : port; return(DNSARecordLookup(nextSRVRecord, host, lookupPort, async, sipLookupResult.URI)); } } } catch (Exception excp) { logger.Error("Exception SIPDNSManager ResolveSIPService (" + sipURI.ToString() + "). " + excp.Message); m_inProgressSIPServiceLookups.Remove(sipURI.ToString()); return(new SIPDNSLookupResult(sipURI, excp.Message)); } }