add stringer methods

This commit is contained in:
soypat
2025-02-20 23:41:27 -03:00
parent c9d1c4f239
commit d996040d92
6 changed files with 43 additions and 4 deletions
+7
View File
@@ -73,6 +73,13 @@ func (seg Segment) isFirstSYN() bool {
return seg.Flags == FlagSYN && seg.ACK == 0 && seg.DATALEN == 0 && seg.WND > 0
}
func (seg Segment) String() string {
if seg.DATALEN == 0 {
return fmt.Sprintf("SEG %s ACK=%d SEQ=%d WND=%d", seg.Flags, seg.ACK, seg.SEQ, seg.WND)
}
return fmt.Sprintf("SEG %s ACK=%d SEQ=%d WND=%d DATALEN=%d", seg.Flags, seg.ACK, seg.SEQ, seg.WND, seg.DATALEN)
}
// ClientSynSegment is a the first packet sent over a TCP connection to a server. Typically the client
// will call ClientSynSegment to generate a new SYN packet to send over to the server to initiate communications:
//
+3 -1
View File
@@ -174,8 +174,10 @@ func (frm Frame) ClearHeader() {
}
func (tfrm Frame) String() string {
src := tfrm.SourcePort()
dst := tfrm.DestinationPort()
seg := tfrm.Segment(len(tfrm.Payload()))
return fmt.Sprintf("%+v", seg)
return fmt.Sprintf("TCP :%d -> :%d %s", src, dst, seg.String())
}
//
+8 -3
View File
@@ -11,7 +11,8 @@ import (
)
var (
errMismatchedPort = errors.New("mismatched port")
errMismatchedSrcPort = errors.New("source port mismatch")
errMismatchedDstPort = errors.New("destination port mismatch")
)
// Handler is a low level TCP handling data structure. It implements logic
@@ -120,13 +121,14 @@ func (h *Handler) Recv(b []byte) error {
if err != nil {
return err
}
remotePort := tfrm.SourcePort()
if h.remotePort != 0 && remotePort != h.remotePort {
return errMismatchedPort
return errMismatchedSrcPort
}
dstPort := tfrm.DestinationPort()
if h.localPort != dstPort {
return errMismatchedPort
return errMismatchedDstPort
}
payload := tfrm.Payload()
if len(payload) > h.bufRx.Free() {
@@ -160,6 +162,9 @@ func (h *Handler) Recv(b []byte) error {
h.debug("tcp.Handler:rx-remoteport-set", slog.Uint64("port", uint64(h.localPort)), slog.Uint64("remoteport", uint64(remotePort)))
h.remotePort = remotePort
}
if h.logenabled(internal.LevelTrace) {
h.trace("tcp.Handler:rx-done", slog.Uint64("port", uint64(h.localPort)), slog.Uint64("remoteport", uint64(remotePort)), slog.String("seg", segIncoming.String()))
}
return nil
}