Skip to content

Commit

Permalink
handle unannouncment with PTR only, from apple stack
Browse files Browse the repository at this point in the history
  • Loading branch information
betamos committed Feb 8, 2025
1 parent 333e52d commit d559d61
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
19 changes: 16 additions & 3 deletions dns-sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func answerTo(records, knowns []dns.RR, question dns.Question) (answers, extras
}

// Returns any services from the msg that matches the provided search type.
// This includes unannouncements
func servicesFromRecords(msg *dns.Msg) (services []*Service) {
// TODO: Support meta-queries
var (
Expand All @@ -222,23 +223,35 @@ func servicesFromRecords(msg *dns.Msg) (services []*Service) {
// Note that stable sort is necessary to preserve order of A and AAAA records
slices.SortStableFunc(answers, byRecordType)

// Create a svc record if it doesn't exist
ensureSvc := func(name string) *Service {
if svc = m[name]; svc != nil {
return svc
}
svc, err := parseServicePath(name)
if err != nil {
return nil
}
m[name] = svc
return svc
}

for _, answer := range answers {
switch rr := answer.(type) {
// Phase 1: create services
case *dns.SRV:

// pointer to service path, e.g. `My Printer._http._tcp.`
if svc, _ = parseServicePath(rr.Hdr.Name); svc == nil {
if svc = ensureSvc(rr.Hdr.Name); svc == nil {
continue
}
svc.Hostname = rr.Target
svc.Port = rr.Port
svc.ttl = time.Second * time.Duration(rr.Hdr.Ttl)
m[rr.Hdr.Name] = svc

// Phase 2: populate subtypes and text
case *dns.PTR:
if svc = m[rr.Ptr]; svc == nil {
if svc = ensureSvc(rr.Ptr); svc == nil {
continue
}
// parse type from query, e.g. `_printer._sub._http._tcp.local.`
Expand Down
4 changes: 1 addition & 3 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,14 @@ func (s *Service) String() string {
return fmt.Sprintf("%v.%v.%v", s.Name, s.Type.Name, s.Type.Domain)
}

// Checks that the name and type are valid. The rest can be nullish when unannouncing.
func (s *Service) Validate() error {
if err := s.Type.Validate(); err != nil {
return err
}
if s.Name == "" {
return errors.New("no name specified")
}
if s.Hostname == "" {
return errors.New("no hostname specified")
}
return nil
}

Expand Down

0 comments on commit d559d61

Please sign in to comment.