From cbadfe5128d7ee61f2d8c503c78bd0e9139a5b24 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Thu, 3 Jun 2021 21:05:33 +0200 Subject: [PATCH] dhcp4: ensure MQTT topic names are printable (for mosquitto_sub) --- cmd/dhcp4d/dhcp4d.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cmd/dhcp4d/dhcp4d.go b/cmd/dhcp4d/dhcp4d.go index dbecc4c..ae705a4 100644 --- a/cmd/dhcp4d/dhcp4d.go +++ b/cmd/dhcp4d/dhcp4d.go @@ -34,6 +34,7 @@ import ( "sync" "syscall" "time" + "unicode" "github.com/gokrazy/gokrazy" "github.com/google/renameio" @@ -419,6 +420,15 @@ func newSrv(permDir string) (*srv, error) { // MQTT requires valid UTF-8 and some brokers don’t cope well with // invalid UTF-8: https://github.com/fhmq/hmq/issues/104 identifier := strings.ToValidUTF8(latest.Hostname, "") + // Some MQTT clients (e.g. mosquitto_pub) don’t cope well with topic + // names containing non-printable characters (see also + // https://twitter.com/zekjur/status/1347295676909158400): + identifier = strings.Map(func(r rune) rune { + if unicode.IsPrint(r) { + return r + } + return -1 + }, identifier) if identifier == "" { identifier = latest.HardwareAddr }