Skip to content

Commit 23cb896

Browse files
committed
feat: add Avahi Zeroconf backend
1 parent 3a99415 commit 23cb896

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

config_schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@
169169
"type": "string",
170170
"description": "The Zeroconf implementation to use",
171171
"enum": [
172-
"builtin"
172+
"builtin",
173+
"avahi"
173174
],
174175
"default": "builtin"
175176
},

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/devgianlu/go-librespot
33
go 1.22.2
44

55
require (
6+
github.com/OpenPrinting/go-avahi v0.0.0-20250813163007-dd9db1c4a6e9
67
github.com/cenkalti/backoff/v4 v4.2.1
78
github.com/devgianlu/shannon v0.0.0-20230613115856-82ec90b7fa7e
89
github.com/godbus/dbus/v5 v5.1.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/OpenPrinting/go-avahi v0.0.0-20250813163007-dd9db1c4a6e9 h1:sYwgzNSkvqBnhfhS5THhHdSYt+8aleQBGMLObOOg5vM=
2+
github.com/OpenPrinting/go-avahi v0.0.0-20250813163007-dd9db1c4a6e9/go.mod h1:1vYkalHi1N1ZQ+Wt7KX7WK8fTS09iwrZnuQjwpsUMCU=
13
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
24
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
35
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=

zeroconf/discovery/avahi.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//go:build linux
2+
3+
package discovery
4+
5+
import (
6+
"fmt"
7+
"net"
8+
9+
"github.com/OpenPrinting/go-avahi"
10+
)
11+
12+
func init() {
13+
discoveryServices["avahi"] = &avahiDiscoveryService{}
14+
}
15+
16+
type avahiDiscoveryService struct {
17+
client *avahi.Client
18+
group *avahi.EntryGroup
19+
}
20+
21+
func (s *avahiDiscoveryService) Register(name, service, domain string, port int, txt []string, ifaces []net.Interface) (err error) {
22+
if len(ifaces) > 0 {
23+
return fmt.Errorf("avahi discovery does not support specifying interfaces")
24+
}
25+
26+
s.client, err = avahi.NewClient(avahi.ClientLoopbackWorkarounds)
27+
if err != nil {
28+
return fmt.Errorf("failed to create Avahi client: %w", err)
29+
}
30+
31+
s.group, err = avahi.NewEntryGroup(s.client)
32+
if err != nil {
33+
return fmt.Errorf("failed to create Avahi entry group: %w", err)
34+
}
35+
36+
if err = s.group.AddService(&avahi.EntryGroupService{
37+
IfIdx: avahi.IfIndexUnspec,
38+
Proto: avahi.ProtocolUnspec,
39+
InstanceName: name,
40+
SvcType: service,
41+
Domain: domain,
42+
Port: port,
43+
Txt: txt,
44+
}, 0); err != nil {
45+
return fmt.Errorf("failed to add service to Avahi entry group: %w", err)
46+
}
47+
48+
if err = s.group.Commit(); err != nil {
49+
return fmt.Errorf("failed to commit Avahi entry group: %w", err)
50+
}
51+
52+
return nil
53+
}
54+
55+
func (s *avahiDiscoveryService) Shutdown() {
56+
s.group.Close()
57+
s.client.Close()
58+
}

0 commit comments

Comments
 (0)