mirror of
https://github.com/soypat/lneto.git
synced 2026-08-21 06:59:04 +00:00
add ipv4 frame tests; fix protocol setting
This commit is contained in:
@@ -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())
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user