Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
fixed dns-sd instance name escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
farshidtz committed Mar 7, 2021
1 parent 4a5dd20 commit b469a46
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
13 changes: 10 additions & 3 deletions discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ import (
"github.com/linksmart/thing-directory/catalog"
)

// escape special characters as recommended by https://tools.ietf.org/html/rfc6763#section-4.3
func escapeDNSSDServiceInstance(instance string) (escaped string) {
// replace \ by \\
escaped = strings.ReplaceAll(instance, "\\", "\\\\")
// replace . by \.
escaped = strings.ReplaceAll(escaped, ".", "\\.")
return escaped
}

// register as a DNS-SD Service
func registerDNSSDService(conf *Config) (func(), error) {
// escape special characters (https://tools.ietf.org/html/rfc6763#section-4.3)
instance := strings.ReplaceAll(conf.DNSSD.Publish.Instance, ".", "\\.")
instance = strings.ReplaceAll(conf.DNSSD.Publish.Instance, "\\", "\\\\")
instance := escapeDNSSDServiceInstance(conf.DNSSD.Publish.Instance)

log.Printf("DNS-SD: registering as \"%s.%s.%s\", subtype: %s",
instance, catalog.DNSSDServiceType, conf.DNSSD.Publish.Domain, catalog.DNSSDServiceSubtype)
Expand Down
31 changes: 31 additions & 0 deletions discovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import "testing"

func TestEscapeDNSSDServiceInstance(t *testing.T) {
t.Run("no escaping", func(t *testing.T) {
instance := "thing-directory"
escaped := escapeDNSSDServiceInstance(instance)
if escaped != instance {
t.Fatalf("Unexpected escaping of %s to %s", instance, escaped)
}
})

t.Run("escape dot", func(t *testing.T) {
instance := "thing.directory" // from thing.directory
expected := "thing\\.directory" // to thing\.directory
escaped := escapeDNSSDServiceInstance(instance)
if escaped != expected {
t.Fatalf("Escaped value for %s is %s. Expected %s", instance, escaped, expected)
}
})

t.Run("escape backslash", func(t *testing.T) {
instance := "thing\\directory" // from thing\directory
expected := "thing\\\\directory" // to thing\\directory
escaped := escapeDNSSDServiceInstance(instance)
if escaped != expected {
t.Fatalf("Escaped value for %s is %s. Expected %s", instance, escaped, expected)
}
})
}

0 comments on commit b469a46

Please sign in to comment.