mirror of
https://github.com/soypat/lneto.git
synced 2026-08-20 22:49:11 +00:00
Error rewrites (#43)
* pcap: reuse Frame memory * slog: reduce heap allocations of addresses; also prevent heap alloc of dhcp options in pcap * dns: heapless improvement; add StackAsync buffer for more heapless operation; start thinking of errors * errors: begin standardise errors in lneto * errors: finish standardization of errors * fix merge issues * add more lneto errors to rest of package * format errors.go
This commit is contained in:
+3
-2
@@ -1,8 +1,9 @@
|
||||
package ntp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/soypat/lneto"
|
||||
)
|
||||
|
||||
type state uint8
|
||||
@@ -100,7 +101,7 @@ func (c *Client) Demux(carrierData []byte, frameOffset int) error {
|
||||
xmt := frm.TransmitTime()
|
||||
orig := frm.OriginTime()
|
||||
if xmt == orig || orig != c.t[0] {
|
||||
return errors.New("bogus NTP packet")
|
||||
return lneto.ErrPacketDrop
|
||||
}
|
||||
|
||||
txelapsed := c.now().Sub(c.start)
|
||||
|
||||
+6
-5
@@ -3,11 +3,12 @@ package ntp
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"math"
|
||||
"math/bits"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/soypat/lneto"
|
||||
)
|
||||
|
||||
// NTP Global Parameters.
|
||||
@@ -26,7 +27,7 @@ const (
|
||||
|
||||
func NewFrame(buf []byte) (Frame, error) {
|
||||
if len(buf) < SizeHeader {
|
||||
return Frame{buf: nil}, errors.New("NTP frame too short")
|
||||
return Frame{buf: nil}, lneto.ErrShortBuffer
|
||||
}
|
||||
return Frame{buf: buf}, nil
|
||||
}
|
||||
@@ -189,12 +190,12 @@ func TimestampFromUint64(ts uint64) Timestamp {
|
||||
func TimestampFromTime(t time.Time) (Timestamp, error) {
|
||||
t = t.UTC()
|
||||
if t.Before(baseTime) {
|
||||
return Timestamp{}, errors.New("ntp.TimestampFromTime: time is before baseTime")
|
||||
return Timestamp{}, lneto.ErrUnsupported
|
||||
}
|
||||
off := t.Sub(baseTime)
|
||||
sec := uint64(off / time.Second)
|
||||
if sec > math.MaxUint32 {
|
||||
return Timestamp{}, errors.New("ntp.TimestampFromTime: time is too large")
|
||||
return Timestamp{}, lneto.ErrUnsupported
|
||||
}
|
||||
fra := uint64(off%time.Second) * math.MaxUint32 / uint64(time.Second)
|
||||
return Timestamp{
|
||||
@@ -254,7 +255,7 @@ func (d Date) Time() (time.Time, error) {
|
||||
}
|
||||
hi, seclo := bits.Mul64(uint64(sec), uint64(time.Second))
|
||||
if hi != 0 || seclo > math.MaxInt64-uint64(time.Second)-1 {
|
||||
return time.Time{}, errors.New("ntp.Date.Time overflow")
|
||||
return time.Time{}, lneto.ErrUnsupported
|
||||
}
|
||||
off := time.Duration(seclo)
|
||||
off += time.Second * time.Duration(d.frac>>32) / math.MaxUint32
|
||||
|
||||
Reference in New Issue
Block a user