diff options
-rw-r--r-- | pkg/ipstack/ipstack.go | 34 | ||||
-rwxr-xr-x | vhost | bin | 3179481 -> 3191030 bytes | |||
-rwxr-xr-x | vrouter | bin | 3146532 -> 3183009 bytes |
3 files changed, 21 insertions, 13 deletions
diff --git a/pkg/ipstack/ipstack.go b/pkg/ipstack/ipstack.go index ba55869..cf2e9d9 100644 --- a/pkg/ipstack/ipstack.go +++ b/pkg/ipstack/ipstack.go @@ -1249,9 +1249,10 @@ type SocketKey struct { } type RecvBuffer struct { - recvNext uint32 - lbr uint32 - buffer []byte + recvNext uint32 + lbr uint32 + buffer []byte + windowSize uint16 } type SendBuffer struct { @@ -1386,9 +1387,10 @@ func VConnect(ip string, port uint16) (*VTCPConn, error) { buffer: make([]byte, MAX_WINDOW_SIZE), }, RecvBuffer: &RecvBuffer{ - recvNext: 0, - lbr: 0, - buffer: make([]byte, MAX_WINDOW_SIZE), + recvNext: 0, + lbr: 0, + buffer: make([]byte, MAX_WINDOW_SIZE), + windowSize: uint16(MAX_WINDOW_SIZE), }, } @@ -1507,21 +1509,25 @@ func (c *VTCPConn) VWrite(payload []byte) (int, error) { return 0, errors.Errorf("error VWrite: payload is larger than the window size") } + availableSendSpace := MAX_WINDOW_SIZE - c.SendBuffer.nxt - 1 - c.SendBuffer.lbw + // check if the payload is larger than the available window size - if len(payload) > int(MAX_WINDOW_SIZE-(c.SendBuffer.nxt-1-c.SendBuffer.lbw)) { - return 0, errors.Errorf("error VWrite: payload is larger than the available window size") + fmt.Println("space in send buffer", availableSendSpace, "recbuf next", c.RecvBuffer.recvNext, "lbr", c.RecvBuffer.lbr) + if len(payload) > int(availableSendSpace) { + return 0, errors.Errorf("error VWrite: payload is larger than the available space in send buffer") } + // check if the payload is larger than the available window size + availableWindowSize := c.RecvBuffer.windowSize - uint16(c.RecvBuffer.recvNext-1-c.RecvBuffer.lbr) // make the header - advertisedWindow := MAX_WINDOW_SIZE - (c.SendBuffer.nxt - 1 - c.SendBuffer.lbw) tcpHdr := &header.TCPFields{ SrcPort: c.LocalPort, DstPort: c.RemotePort, - SeqNum: c.SendBuffer.nxt, - AckNum: c.SendBuffer.una, + SeqNum: c.SendBuffer.nxt, + AckNum: startingSeqNum c.SendBuffer.una, DataOffset: 20, Flags: header.TCPFlagAck, - WindowSize: uint16(advertisedWindow), + WindowSize: availableWindowSize, Checksum: 0, UrgentPointer: 0, } @@ -1574,8 +1580,10 @@ func (c *VTCPConn) VRead(numBytesToRead int) (int, string, error) { toReturn := string(socketEntry.Conn.RecvBuffer.buffer[c.RecvBuffer.lbr : c.RecvBuffer.lbr+diff]) // update the last byte read c.RecvBuffer.lbr += diff + // increase the window size by bytes read + c.RecvBuffer.windowSize += uint16(diff) // return the data - return numBytesToRead, toReturn, nil + return int(diff), toReturn, nil } return 0, "", nil Binary files differBinary files differ |