diff --git a/cmd/ntpal/control.go b/cmd/ntpal/control.go index 9ccc642..f7a083b 100644 --- a/cmd/ntpal/control.go +++ b/cmd/ntpal/control.go @@ -73,7 +73,6 @@ func fetchInfoCommand(m ntpalUIModel) tea.Cmd { assocCall := client.Go("NTPalRPCServer.FetchAssociations", 0, &associations, nil) var system *ntp.System systemCall := client.Go("NTPalRPCServer.FetchSystem", 0, &system, nil) - err := (<-assocCall.Done).Error if err != nil { fmt.Printf("Error getting info from daemon: %v\n", err) @@ -173,7 +172,7 @@ func (m ntpalUIModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { {"Hostname", m.association.Hostname}, {"IP", m.association.Srcaddr.IP.String()}, {"Offset (ms)", ui.TableFloat(m.association.Offset * 1e3)}, - {"Poll", (time.Duration(ntp.Log2ToDouble(int8(math.Max(float64(m.association.Poll), float64(m.association.Hpoll))))) * time.Second).String()}, + {"Poll", (time.Duration(ntp.Log2ToDouble(int8(math.Min(float64(m.association.Poll), float64(m.association.Hpoll))))) * time.Second).String()}, {"Reach", strconv.FormatUint(uint64(m.association.Reach), 2)}, {"Root delay", ui.TableFloat(m.association.Rootdelay)}, {"Root dispersion", ui.TableFloat(m.association.Rootdisp)}, diff --git a/cmd/ntpal/daemon.go b/cmd/ntpal/daemon.go index 1ce118d..9c20fd4 100644 --- a/cmd/ntpal/daemon.go +++ b/cmd/ntpal/daemon.go @@ -1,8 +1,8 @@ package main import ( + "errors" "fmt" - "log" "os" "syscall" @@ -17,6 +17,8 @@ var ( LogPath = fmt.Sprintf("/var/log/%s.log", daemonName) ) +var ErrNotRunning = errors.New("daemon ntpald is not running") + var daemonCtx = &daemon.Context{ PidFileName: PidPath, PidFilePerm: 0644, @@ -27,14 +29,19 @@ var daemonCtx = &daemon.Context{ Args: append([]string{daemonName}, os.Args[1:]...), } -func killDaemon() { +func killDaemon() error { daemon, err := daemonCtx.Search() - if err != nil { - log.Fatalf("Error finding daemon: %v", err) + if daemon == nil { + if err == nil { + err = ErrNotRunning + } + return err } err = syscall.Kill(daemon.Pid, syscall.SIGTERM) if err != nil { - log.Fatal("Couldn't stop ntpal daemon.") + return err } + + return nil } diff --git a/cmd/ntpal/main.go b/cmd/ntpal/main.go index 5c6a91c..92a45a4 100644 --- a/cmd/ntpal/main.go +++ b/cmd/ntpal/main.go @@ -14,8 +14,6 @@ import ( const defaultConfigPath = "/etc/ntp.conf" const defaultDriftPath = "/etc/ntp.drift" -var socketPath = fmt.Sprintf("/var/%s.sock", daemonName) - const queryMessages = 5 func main() { @@ -46,10 +44,18 @@ func main() { host = "0.0.0.0" } - system := ntpal.NewSystem(host, port, config, drift, socketPath) + system := ntpal.NewSystem(host, port, config, drift, SocketPath) if query != "" { handleQueryCommand(system, query, queryMessages) + } else if stop { + err := killDaemon() + if err == nil { + fmt.Println("Stopped ntpald") + } else { + fmt.Printf("Error stopping ntpald: %v\n", err) + os.Exit(1) + } } else { process, _ := daemonCtx.Search() @@ -72,6 +78,7 @@ func main() { // Parent process if process != nil { fmt.Printf("Daemon process (ntpald) started successfully with PID: %d\n", process.Pid) + // TODO: Launch control UI here, but need to sleep or connect with backoff return } diff --git a/internal/ntp/network.go b/internal/ntp/network.go index c082630..732c796 100644 --- a/internal/ntp/network.go +++ b/internal/ntp/network.go @@ -8,15 +8,8 @@ import ( ) type ReceivePacket struct { - Srcaddr *net.UDPAddr /* source (remote) address */ - Dstaddr *net.UDPAddr /* destination (local) address */ - Leap byte /* leap indicator */ - Version byte /* version number */ - Mode Mode /* mode */ - Keyid int32 /* key ID */ - Mac Digest /* message digest */ - Dst TimestampEncoded /* destination timestamp */ - ntpFieldsEncoded + Dst TimestampEncoded /* destination timestamp */ + TransmitPacket } type TransmitPacket struct { @@ -25,12 +18,12 @@ type TransmitPacket struct { Leap byte /* leap indicator */ Version byte /* version number */ Mode Mode /* mode */ - Keyid int /* key ID */ + Keyid int32 /* key ID */ Dgst Digest /* message digest */ - ntpFieldsEncoded + NtpFieldsEncoded } -type ntpFieldsEncoded struct { +type NtpFieldsEncoded struct { Stratum byte /* stratum */ Poll int8 /* poll interval */ Precision int8 /* precision */ @@ -48,7 +41,7 @@ func EncodeTransmitPacket(packet TransmitPacket) []byte { var buffer bytes.Buffer binary.Write(&buffer, binary.BigEndian, firstByte) - binary.Write(&buffer, binary.BigEndian, &packet.ntpFieldsEncoded) + binary.Write(&buffer, binary.BigEndian, &packet.NtpFieldsEncoded) return buffer.Bytes() } @@ -75,17 +68,19 @@ func DecodeRecvPacket(encoded []byte, clientAddr net.Addr, con net.PacketConn) ( version := (firstByte >> 3) & 0b111 mode := firstByte & 0b111 - fieldsEncoded := ntpFieldsEncoded{} + fieldsEncoded := NtpFieldsEncoded{} if err := binary.Read(reader, binary.BigEndian, &fieldsEncoded); err != nil { return nil, err } return &ReceivePacket{ - Srcaddr: clientUDPAddr, - Dstaddr: localUDPAddr, - Leap: leap, - Version: version, - Mode: Mode(mode), - ntpFieldsEncoded: fieldsEncoded, + TransmitPacket: TransmitPacket{ + Srcaddr: clientUDPAddr, + Dstaddr: localUDPAddr, + Leap: leap, + Version: version, + Mode: Mode(mode), + NtpFieldsEncoded: fieldsEncoded, + }, }, nil } diff --git a/pkg/ntpal/ntpal.go b/pkg/ntpal/ntpal.go index 49ae503..9eaf407 100644 --- a/pkg/ntpal/ntpal.go +++ b/pkg/ntpal/ntpal.go @@ -318,10 +318,12 @@ func (system *NTPalSystem) setupAssociations(associationConfigs []serverAssociat Hpoll: int8(associationConfig.minpoll), Hostname: associationConfig.hostname, ReceivePacket: ntp.ReceivePacket{ - Srcaddr: associationConfig.address, - Dstaddr: system.address, - Version: byte(associationConfig.version), - Keyid: int32(associationConfig.key), + TransmitPacket: ntp.TransmitPacket{ + Srcaddr: associationConfig.address, + Dstaddr: system.address, + Version: byte(associationConfig.version), + Keyid: int32(associationConfig.key), + }, }, }, } diff --git a/pkg/ntpal/query.go b/pkg/ntpal/query.go index dbdbb56..9fc1dd6 100644 --- a/pkg/ntpal/query.go +++ b/pkg/ntpal/query.go @@ -36,10 +36,12 @@ func (system *NTPalSystem) Query(address string, messages int) (*QueryResult, er Association: ntp.Association{ Hpoll: 0, ReceivePacket: ntp.ReceivePacket{ - Srcaddr: addr, - Dstaddr: system.address, - Version: VERSION, - Keyid: 0, + TransmitPacket: ntp.TransmitPacket{ + Srcaddr: addr, + Dstaddr: system.address, + Version: VERSION, + Keyid: 0, + }, }, }, }