add ipv4 frame tests; fix protocol setting

This commit is contained in:
soypat
2025-05-13 21:36:24 -03:00
parent 4b3db8fefd
commit 411d1b3c28
4 changed files with 125 additions and 7 deletions
+3 -1
View File
@@ -61,7 +61,8 @@ func (sb *StackBasic) Recv(frame []byte) error {
totalLen := ifrm.TotalLength()
for i := range sb.handlers {
h := &sb.handlers[i]
if h.proto == ifrm.Protocol() {
proto := ifrm.Protocol()
if h.proto == proto {
return h.recv(frame[:totalLen], off)
}
}
@@ -92,6 +93,7 @@ func (sb *StackBasic) Handle(frame []byte) (int, error) {
ifrm.SetTotalLength(uint16(totalLen))
ifrm.SetFlags(dontFrag)
ifrm.SetTTL(64)
ifrm.SetProtocol(h.proto)
ifrm.SetCRC(ifrm.CalculateHeaderCRC())
if ifrm.Protocol() == lneto.IPProtoTCP {
tfrm, _ := tcp.NewFrame(ifrm.Payload())
+11 -4
View File
@@ -14,9 +14,11 @@ func TestBasicStack(t *testing.T) {
var connCl, connSv TCPConn
setupClientServer(t, rng, &sbCl, &sbSv, &connCl, &connSv)
var buf [2048]byte
nextToSend := &sbCl
nextToRecv := &sbSv
exchangeAndExpectStates := func(clState, svState tcp.State) {
t.Helper()
expectExchange(t, &sbCl, &sbSv, buf[:])
expectExchange(t, nextToSend, nextToRecv, buf[:])
gotCl := connCl.State()
gotSv := connSv.State()
if gotCl != clState {
@@ -25,20 +27,25 @@ func TestBasicStack(t *testing.T) {
if gotSv != svState {
t.Errorf("want server state %s, got %s", svState, gotSv)
}
nextToSend, nextToRecv = nextToRecv, nextToSend
}
exchangeAndExpectStates(tcp.StateSynSent, tcp.StateListen)
exchangeAndExpectStates(tcp.StateSynSent, tcp.StateSynRcvd) // Client sends over first SYN and server receives it.
exchangeAndExpectStates(tcp.StateEstablished, tcp.StateSynRcvd) // server sends back SYNACK, establishing connection on client side.
exchangeAndExpectStates(tcp.StateEstablished, tcp.StateEstablished) // Client sends ACK, establishing connection in full.
}
func expectExchange(t *testing.T, from, to *StackBasic, buf []byte) {
t.Helper()
n, err := from.Handle(buf)
if err != nil {
t.Error(err)
t.Error("expectExchange:Handle:", err)
} else if n == 0 {
t.Error("expected data exchange")
return
}
err = to.Recv(buf[:n])
if err != nil {
t.Error(err)
t.Error("expectExchange:Recv:", err)
}
}