Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TTL server option #23

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,36 @@ const (

var defaultTTL uint32 = 3200

type serverOpts struct {
ttl uint32
}

func applyServerOpts(options ...ServerOption) serverOpts {
// Apply default configuration and load supplied options.
var conf = serverOpts{
ttl: defaultTTL,
}
for _, o := range options {
if o != nil {
o(&conf)
}
}
return conf
}

// ServerOption fills the option struct.
type ServerOption func(*serverOpts)

// TTL sets the TTL for DNS replies.
func TTL(ttl uint32) ServerOption {
return func(o *serverOpts) {
o.ttl = ttl
}
}

// Register a service by given arguments. This call will take the system's hostname
// and lookup IP by that hostname.
func Register(instance, service, domain string, port int, text []string, ifaces []net.Interface) (*Server, error) {
func Register(instance, service, domain string, port int, text []string, ifaces []net.Interface, opts ...ServerOption) (*Server, error) {
entry := newServiceEntry(instance, service, domain)
entry.Port = port
entry.Text = text
Expand Down Expand Up @@ -68,7 +95,7 @@ func Register(instance, service, domain string, port int, text []string, ifaces
return nil, fmt.Errorf("could not determine host IP addresses")
}

s, err := newServer(ifaces)
s, err := newServer(ifaces, applyServerOpts(opts...))
if err != nil {
return nil, err
}
Expand All @@ -81,7 +108,7 @@ func Register(instance, service, domain string, port int, text []string, ifaces

// RegisterProxy registers a service proxy. This call will skip the hostname/IP lookup and
// will use the provided values.
func RegisterProxy(instance, service, domain string, port int, host string, ips []string, text []string, ifaces []net.Interface) (*Server, error) {
func RegisterProxy(instance, service, domain string, port int, host string, ips []string, text []string, ifaces []net.Interface, opts ...ServerOption) (*Server, error) {
entry := newServiceEntry(instance, service, domain)
entry.Port = port
entry.Text = text
Expand Down Expand Up @@ -124,7 +151,7 @@ func RegisterProxy(instance, service, domain string, port int, host string, ips
ifaces = listMulticastInterfaces()
}

s, err := newServer(ifaces)
s, err := newServer(ifaces, applyServerOpts(opts...))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -154,7 +181,7 @@ type Server struct {
}

// Constructs server structure
func newServer(ifaces []net.Interface) (*Server, error) {
func newServer(ifaces []net.Interface, opts serverOpts) (*Server, error) {
ipv4conn, err4 := joinUdp4Multicast(ifaces)
if err4 != nil {
log.Printf("[zeroconf] no suitable IPv4 interface: %s", err4.Error())
Expand All @@ -172,7 +199,7 @@ func newServer(ifaces []net.Interface) (*Server, error) {
ipv4conn: ipv4conn,
ipv6conn: ipv6conn,
ifaces: ifaces,
ttl: defaultTTL,
ttl: opts.ttl,
shouldShutdown: make(chan struct{}),
}

Expand All @@ -199,6 +226,8 @@ func (s *Server) SetText(text []string) {
}

// TTL sets the TTL for DNS replies
//
// Deprecated: This method is racy. Use the TTL server option instead.
func (s *Server) TTL(ttl uint32) {
s.ttl = ttl
}
Expand Down