add maximum segment size option on TCP connection init

This commit is contained in:
soypat
2025-05-30 21:33:12 -03:00
parent 0f4478cc32
commit 6c65764070
2 changed files with 51 additions and 10 deletions
+12 -4
View File
@@ -30,8 +30,9 @@ type Handler struct {
// connid is a conenction counter that is incremented each time a new
// connection is established via Open calls. This disambiguate's whether
// Read and Write calls belong to the current connection.
connid uint16
closing bool
connid uint16
optcodec OptionCodec
closing bool
}
func (h *Handler) SetLoggers(handler, scb *slog.Logger) {
@@ -206,10 +207,14 @@ func (h *Handler) Send(b []byte) (int, error) {
if err != nil {
return 0, err
}
offset := uint8(5)
var segment Segment
if h.AwaitingSynSend() {
// Handling init syn segment.
segment = ClientSynSegment(h.scb.ISS(), h.scb.RecvWindow())
h.optcodec.PutOption16(b[sizeHeaderTCP:], OptMaxSegmentSize, uint16(len(b)))
offset++
} else {
var ok bool
available := min(h.bufTx.Buffered(), len(b)-sizeHeaderTCP)
@@ -225,6 +230,9 @@ func (h *Handler) Send(b []byte) (int, error) {
} else if n != int(segment.DATALEN) {
panic("expected n == available")
}
} else if segment.Flags == synack {
h.optcodec.PutOption16(b[sizeHeaderTCP:], OptMaxSegmentSize, uint16(len(b)))
offset++
}
}
prevState := h.scb.State()
@@ -236,9 +244,9 @@ func (h *Handler) Send(b []byte) (int, error) {
}
tfrm.SetSourcePort(h.localPort)
tfrm.SetDestinationPort(h.remotePort)
tfrm.SetSegment(segment, 5) // No TCP options.
tfrm.SetSegment(segment, offset) // No TCP options.
tfrm.SetUrgentPtr(0)
return sizeHeaderTCP + int(segment.DATALEN), nil
return int(offset)*4 + int(segment.DATALEN), nil
}
// Free returns the amount of space free in the transmit buffer. A call to [Handler.Write] with a larger buffer will fail.