diff --git a/internal/mdns/mdns.go b/internal/mdns/mdns.go index b882b93a..dbbe43ee 100644 --- a/internal/mdns/mdns.go +++ b/internal/mdns/mdns.go @@ -146,14 +146,17 @@ func (m *MDNS) start(allowRestart bool) error { return nil } +// Start starts the mDNS server func (m *MDNS) Start() error { return m.start(false) } +// Restart restarts the mDNS server func (m *MDNS) Restart() error { return m.start(true) } +// Stop stops the mDNS server func (m *MDNS) Stop() error { m.lock.Lock() defer m.lock.Unlock() @@ -165,26 +168,46 @@ func (m *MDNS) Stop() error { return m.conn.Close() } -func (m *MDNS) SetLocalNames(localNames []string, always bool) error { - if reflect.DeepEqual(m.localNames, localNames) && !always { - return nil +func (m *MDNS) setLocalNames(localNames []string) { + m.lock.Lock() + defer m.lock.Unlock() + + if reflect.DeepEqual(m.localNames, localNames) { + return } m.localNames = localNames - _ = m.Restart() - - return nil + return } -func (m *MDNS) SetListenOptions(listenOptions *MDNSListenOptions) error { +func (m *MDNS) setListenOptions(listenOptions *MDNSListenOptions) { + m.lock.Lock() + defer m.lock.Unlock() + if m.listenOptions != nil && m.listenOptions.IPv4 == listenOptions.IPv4 && m.listenOptions.IPv6 == listenOptions.IPv6 { - return nil + return } m.listenOptions = listenOptions - _ = m.Restart() - - return nil +} + +// SetLocalNames sets the local names and restarts the mDNS server +func (m *MDNS) SetLocalNames(localNames []string) error { + m.setLocalNames(localNames) + return m.Restart() +} + +// SetListenOptions sets the listen options and restarts the mDNS server +func (m *MDNS) SetListenOptions(listenOptions *MDNSListenOptions) error { + m.setListenOptions(listenOptions) + return m.Restart() +} + +// SetOptions sets the local names and listen options and restarts the mDNS server +func (m *MDNS) SetOptions(options *MDNSOptions) error { + m.setLocalNames(options.LocalNames) + m.setListenOptions(options.ListenOptions) + return m.Restart() } diff --git a/mdns.go b/mdns.go index 8e251e1b..c197d16f 100644 --- a/mdns.go +++ b/mdns.go @@ -1,23 +1,23 @@ package kvm import ( + "fmt" + "github.com/jetkvm/kvm/internal/mdns" ) var mDNS *mdns.MDNS func initMdns() error { + options := getMdnsOptions() + if options == nil { + return fmt.Errorf("failed to get mDNS options") + } + m, err := mdns.NewMDNS(&mdns.MDNSOptions{ - Logger: logger, - LocalNames: []string{ - "jetkvm", "jetkvm.local", - // networkManager.GetHostname(), - // networkManager.GetFQDN(), - }, - ListenOptions: &mdns.MDNSListenOptions{ - IPv4: config.NetworkConfig.MDNSMode.String != "disabled", - IPv6: config.NetworkConfig.MDNSMode.String != "disabled", - }, + Logger: logger, + LocalNames: options.LocalNames, + ListenOptions: options.ListenOptions, }) if err != nil { return err diff --git a/network.go b/network.go index b4d3ad37..b957df8c 100644 --- a/network.go +++ b/network.go @@ -32,19 +32,47 @@ func toRpcNetworkSettings(config *types.NetworkConfig) *RpcNetworkSettings { } } +func getMdnsOptions() *mdns.MDNSOptions { + if networkManager == nil { + return nil + } + + var ipv4, ipv6 bool + switch config.NetworkConfig.MDNSMode.String { + case "auto": + ipv4 = true + ipv6 = true + case "ipv4_only": + ipv4 = true + case "ipv6_only": + ipv6 = true + } + + return &mdns.MDNSOptions{ + LocalNames: []string{ + networkManager.Hostname(), + networkManager.FQDN(), + }, + ListenOptions: &mdns.MDNSListenOptions{ + IPv4: ipv4, + IPv6: ipv6, + }, + } +} + func restartMdns() { if mDNS == nil { return } - _ = mDNS.SetListenOptions(&mdns.MDNSListenOptions{ - IPv4: config.NetworkConfig.MDNSMode.String != "disabled", - IPv6: config.NetworkConfig.MDNSMode.String != "disabled", - }) - _ = mDNS.SetLocalNames([]string{ - networkManager.Hostname(), - networkManager.FQDN(), - }, true) + options := getMdnsOptions() + if options == nil { + return + } + + if err := mDNS.SetOptions(options); err != nil { + networkLogger.Error().Err(err).Msg("failed to restart mDNS") + } } func triggerTimeSyncOnNetworkStateChange() { @@ -115,6 +143,7 @@ func initNetwork() error { nc := config.NetworkConfig nm := nmlite.NewNetworkManager(context.Background(), networkLogger) + networkLogger.Info().Interface("networkConfig", nc).Str("hostname", nc.Hostname.String).Str("domain", nc.Domain.String).Msg("initializing network manager") _ = setHostname(nm, nc.Hostname.String, nc.Domain.String) nm.SetOnInterfaceStateChange(networkStateChanged) if err := nm.AddInterface(NetIfName, nc); err != nil { diff --git a/pkg/nmlite/hostname.go b/pkg/nmlite/hostname.go index a75aaf4f..146aad0b 100644 --- a/pkg/nmlite/hostname.go +++ b/pkg/nmlite/hostname.go @@ -83,7 +83,7 @@ func (hm *ResolvConfManager) getDomain() string { } } - return "" + return "local" } func (hm *ResolvConfManager) reconcileHostname() error { diff --git a/pkg/nmlite/manager.go b/pkg/nmlite/manager.go index 92847176..d8a0ddd8 100644 --- a/pkg/nmlite/manager.go +++ b/pkg/nmlite/manager.go @@ -163,7 +163,10 @@ func (nm *NetworkManager) GetInterfaceState(iface string) (*types.InterfaceState return nil, err } - return im.GetState(), nil + state := im.GetState() + state.Hostname = nm.Hostname() + + return state, nil } // GetInterfaceConfig returns the current configuration of a specific interface