ZeroconfHost GetOrCreateZeroconfHost(NSNetService service) { ZeroconfHost host; string hostKey = GetZeroconfHostKey(service); lock (zeroconfHostDict) { if (!zeroconfHostDict.TryGetValue(hostKey, out host)) { host = new ZeroconfHost(); host.DisplayName = service.Name; List <string> ipAddrList = new List <string>(); foreach (NSData address in service.Addresses) { Sockaddr saddr = Sockaddr.CreateSockaddr(address.Bytes); IPAddress ipAddr = Sockaddr.CreateIPAddress(saddr); if (ipAddr != null) { ipAddrList.Add(ipAddr.ToString()); } } host.IPAddresses = ipAddrList; host.Id = host.IPAddress; zeroconfHostDict[hostKey] = host; } } return(host); }
static ZeroconfHost ResponseToZeroconf(Response response, string remoteAddress) { var z = new ZeroconfHost(); // Get the Id and IP address from the A record var aRecord = response.Answers .Select(r => r.RECORD) .OfType <RecordA>() .FirstOrDefault(); if (aRecord != null) { z.Id = aRecord.RR.NAME.Split('.')[0]; z.IPAddress = aRecord.Address; } else { // Is this valid? z.Id = remoteAddress; z.IPAddress = remoteAddress; } var dispNameSet = false; foreach (var ptrRec in response.RecordsPTR) { // set the display name if needed if (!dispNameSet) { z.DisplayName = ptrRec.PTRDNAME.Split('.')[0]; dispNameSet = true; } // Get the matching service records var responseRecords = response.RecordsRR .Where(r => r.NAME == ptrRec.PTRDNAME) .Select(r => r.RECORD) .ToList(); var srvRec = responseRecords.OfType <RecordSRV>().FirstOrDefault(); if (srvRec == null) { continue; // Missing the SRV record, not valid } var svc = new Service { Name = ptrRec.RR.NAME, Port = srvRec.PORT }; // There may be 0 or more text records - property sets foreach (var txtRec in responseRecords.OfType <RecordTXT>()) { var set = new Dictionary <string, string>(); foreach (var txt in txtRec.TXT) { var split = txt.Split(new[] { '=' }, 2); if (split.Length == 1) { if (!string.IsNullOrWhiteSpace(split[0])) { set[split[0]] = null; } } else { set[split[0]] = split[1]; } } svc.AddPropertySet(set); } z.AddService(svc); } return(z); }
private static ZeroconfHost ResponseToZeroconf(Response response, string remoteAddress) { var z = new ZeroconfHost(); // Get the Id and IP address from the A record var aRecord = response.Additionals .Select(r => r.RECORD) .OfType<RecordA>() .FirstOrDefault(); if (aRecord != null) { z.Id = aRecord.RR.NAME.Split('.')[0]; z.IPAddress = aRecord.Address; } else { // Is this valid? z.Id = remoteAddress; z.IPAddress = remoteAddress; } var dispNameSet = false; var services = response.Answers .Select(r => r.RECORD) .OfType<RecordPTR>(); foreach (var ptrRec in services) { // set the display name if needed if (!dispNameSet) { z.DisplayName = ptrRec.PTRDNAME.Split('.')[0]; dispNameSet = true; } // Get the matching service records var serviceRecords = response.Additionals .Where(r => r.NAME == ptrRec.PTRDNAME) .Select(r => r.RECORD) .ToList(); var srvRec = serviceRecords.OfType<RecordSRV>().FirstOrDefault(); if (srvRec == null) continue; // Missing the SRV record, not valid var svc = new Service { Name = ptrRec.RR.NAME, Port = srvRec.PORT }; // There may be 0 or more text records - property sets foreach (var txtRec in serviceRecords.OfType<RecordTXT>()) { var set = new Dictionary<string, string>(); foreach (var txt in txtRec.TXT) { var split = txt.Split(new[] {'='}, 2); if (split.Length == 1) { if (!string.IsNullOrWhiteSpace(split[0])) set[split[0]] = null; } else { set[split[0]] = split[1]; } } svc.AddPropertySet(set); } z.AddService(svc); } return z; }
static ZeroconfHost ResponseToZeroconf(Response response, string remoteAddress, ResolveOptions options) { var z = new ZeroconfHost { IPAddresses = response.Answers .Select(r => r.RECORD) .OfType <RecordA>() .Concat(response.Additionals .Select(r => r.RECORD) .OfType <RecordA>()) .Select(aRecord => aRecord.Address) .Distinct() .ToList() }; z.Id = z.IPAddresses.FirstOrDefault() ?? remoteAddress; var dispNameSet = false; foreach (var ptrRec in response.RecordsPTR) { // set the display name if needed if (!dispNameSet && (options == null || (options != null && options.Protocols.Contains(ptrRec.RR.NAME)))) { z.DisplayName = ptrRec.PTRDNAME.Split('.')[0]; dispNameSet = true; } // Get the matching service records var responseRecords = response.RecordsRR .Where(r => r.NAME == ptrRec.PTRDNAME) .Select(r => r.RECORD) .ToList(); var srvRec = responseRecords.OfType <RecordSRV>().FirstOrDefault(); if (srvRec == null) { continue; // Missing the SRV record, not valid } var svc = new Service { Name = ptrRec.RR.NAME, Port = srvRec.PORT, Ttl = (int)srvRec.RR.TTL, }; // There may be 0 or more text records - property sets foreach (var txtRec in responseRecords.OfType <RecordTXT>()) { var set = new Dictionary <string, string>(); foreach (var txt in txtRec.TXT) { var split = txt.Split(new[] { '=' }, 2); if (split.Length == 1) { if (!string.IsNullOrWhiteSpace(split[0])) { set[split[0]] = null; } } else { set[split[0]] = split[1]; } } svc.AddPropertySet(set); } z.AddService(svc); } return(z); }
static ZeroconfHost ResponseToZeroconf(Response response, string remoteAddress) { var z = new ZeroconfHost { IPAddresses = response.Answers .Select(r => r.RECORD) .OfType <RecordA>() .Concat(response.Additionals .Select(r => r.RECORD) .OfType <RecordA>()) .Select(aRecord => aRecord.Address) .Distinct() .ToList(), }; z.Id = z.IPAddresses.FirstOrDefault() ?? remoteAddress; var dispNameSet = false; foreach (var ptrRec in response.RecordsPTR) { z.AddDomainName(ptrRec.PTRDNAME); // set the display name if needed if (!dispNameSet) { z.DisplayName = ptrRec.PTRDNAME.Split('.')[0]; dispNameSet = true; } } if (response.RecordsRR.Where(x => x.Type == Heijden.DNS.Type.SRV).FirstOrDefault()?.RECORD is RecordSRV srvRec) { var svc = new Service { Name = srvRec.RR.NAME, Port = srvRec.PORT, Ttl = (int)srvRec.RR.TTL, }; z.AddDomainName(srvRec.RR.NAME); if (!dispNameSet) { z.DisplayName = srvRec.RR.NAME.Split('.')[0]; dispNameSet = true; } z.AddService(svc); } if (response.RecordsRR.Where(x => x.Type == Heijden.DNS.Type.TXT).FirstOrDefault()?.RECORD is RecordTXT txtRec) { var txr = new TextRecord { Name = txtRec.RR.NAME, Ttl = (int)txtRec.RR.TTL, }; z.AddDomainName(txtRec.RR.NAME); if (!dispNameSet) { z.DisplayName = txtRec.RR.NAME.Split('.')[0]; dispNameSet = true; } foreach (var txt in txtRec.TXT) { var split = txt.Split(new[] { '=' }, 2); if (split.Length == 1) { if (!string.IsNullOrWhiteSpace(split[0])) { txr.AddProperty(split[0], null); } } else { txr.AddProperty(split[0], split[1].TrimEnd(new[] { '\0' })); } } z.AddTextRecord(txr); } return(z); }
void RefreshZeroconfHostDict() { // Do not walk discoveredServiceDict[] directly // If a NSNetService is in discoveredServiceDict[], it was resolved successfully before it was added List <NSNetService> nsNetServiceList = new List <NSNetService>(); lock (discoveredServiceDict) { nsNetServiceList.AddRange(discoveredServiceDict.Values); } // For each NSNetService, create a ZeroconfHost record foreach (var nsNetService in nsNetServiceList) { Debug.WriteLine($"{nameof(ReturnZeroconfHostResults)}: Name {nsNetService.Name} Type {nsNetService.Type} Domain {nsNetService.Domain} " + $"HostName {nsNetService.HostName} Port {nsNetService.Port}"); // Obtain or create ZeroconfHost ZeroconfHost host = GetOrCreateZeroconfHost(nsNetService); // Add service to ZeroconfHost record Service svc = new Service(); svc.Name = GetNsNetServiceName(nsNetService); svc.Port = (int)nsNetService.Port; // svc.Ttl = is not available NSData txtRecordData = nsNetService.GetTxtRecordData(); if (txtRecordData != null) { NSDictionary txtDict = NSNetService.DictionaryFromTxtRecord(txtRecordData); if (txtDict != null) { if (txtDict.Count > 0) { foreach (var key in txtDict.Keys) { Debug.WriteLine($"{nameof(ReturnZeroconfHostResults)}: Key {key} Value {txtDict[key].ToString()}"); } Dictionary <string, string> propertyDict = new Dictionary <string, string>(); foreach (var key in txtDict.Keys) { propertyDict[key.ToString()] = txtDict[key].ToString(); } svc.AddPropertySet(propertyDict); } else { Debug.WriteLine($"{nameof(ReturnZeroconfHostResults)}: Service.DictionaryFromTxtRecord has 0 entries"); } } else { Debug.WriteLine($"{nameof(ReturnZeroconfHostResults)}: Service.DictionaryFromTxtRecord returned null"); } } host.AddService(svc); } }