示例#1
0
        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;
        }
示例#2
0
        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.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);
        }