Tcp rst handling (#40)

* claude suggests a way forward

* add timing to capture printer

* add pcap.Flags

* fix ICMP CRC calculation and add test

* bugfix: still send data on half-close state(close-wait)

* fix pcap test
This commit is contained in:
Pat Whittingslow
2026-02-23 13:50:13 +01:00
committed by GitHub
parent 08423d0dba
commit 7d323aae19
10 changed files with 386 additions and 46 deletions
+30
View File
@@ -3,12 +3,19 @@ package xnet
import (
"io"
"strconv"
"time"
"github.com/soypat/lneto/internet/pcap"
)
type CapturePrinterConfig struct {
NamespaceWidth int
// TimePrecision if non-zero is used to print timestamp
// at which the packet was received. By default the amount of
// seconds since configuration is printed.
TimePrecision int
// Now returns the current time.
Now func() time.Time
}
// CapturePrinter prints internet packets using the [pcap.PacketBreakdown] and [pcap.Formatter] types.
@@ -20,9 +27,18 @@ type CapturePrinter struct {
fmtPcapBuf []byte
// minimum length of namespace on print.
namespaceminwidth int
timeprec int
origin time.Time
now func() time.Time
}
func (stack *CapturePrinter) Configure(writer io.Writer, cfg CapturePrinterConfig) error {
stack.timeprec = cfg.TimePrecision
stack.now = cfg.Now
if stack.printTimestamps() {
stack.origin = cfg.Now()
}
stack.namespaceminwidth = cfg.NamespaceWidth
stack.write = writer.Write
return nil
@@ -36,9 +52,19 @@ func (stack *CapturePrinter) Formatter() *pcap.Formatter {
func (stack *CapturePrinter) PrintPacket(prefix string, pkt []byte) {
fmtbuf := stack.fmtPcapBuf[:0]
useTimestamps := stack.printTimestamps()
var captime time.Time
if useTimestamps {
captime = stack.now()
}
var err error
stack.frms, err = stack.cap.CaptureEthernet(stack.frms[:0], pkt, 0)
if err == nil {
if useTimestamps {
diff := captime.Sub(stack.origin)
fmtbuf = strconv.AppendFloat(fmtbuf, diff.Seconds(), 'f', stack.timeprec, 32)
fmtbuf = append(fmtbuf, ' ')
}
fmtbuf = append(fmtbuf, prefix...)
// Ensure minimum width of packet length display for less jitter in log viewline.
prevlen := len(prefix)
@@ -60,3 +86,7 @@ func (stack *CapturePrinter) PrintPacket(prefix string, pkt []byte) {
stack.write(fmtbuf)
stack.fmtPcapBuf = fmtbuf[:0] // Reuse buffer if allocated at larger size.
}
func (stack *CapturePrinter) printTimestamps() bool {
return stack.timeprec > 0 && stack.now != nil
}