Skip to content

Commit

Permalink
Fix getting poll in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLester committed Jul 15, 2023
1 parent bac1fc8 commit a8cfb78
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 38 deletions.
3 changes: 1 addition & 2 deletions cmd/ntpal/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)},
Expand Down
17 changes: 12 additions & 5 deletions cmd/ntpal/daemon.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"errors"
"fmt"
"log"
"os"
"syscall"

Expand All @@ -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,
Expand All @@ -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
}
13 changes: 10 additions & 3 deletions cmd/ntpal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()

Expand All @@ -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
}

Expand Down
35 changes: 15 additions & 20 deletions internal/ntp/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 */
Expand All @@ -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()
}

Expand All @@ -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
}
10 changes: 6 additions & 4 deletions pkg/ntpal/ntpal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},
},
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/ntpal/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
}
Expand Down

0 comments on commit a8cfb78

Please sign in to comment.