From 5133c506356387874c26cd5dfbf459f9def11115 Mon Sep 17 00:00:00 2001
From: hidnplayr <hidnplayr@kolibrios.org>
Date: Fri, 27 Jul 2012 22:14:12 +0000
Subject: [PATCH] TCP_send now sends correct data under all conditions

git-svn-id: svn://kolibrios.org@2893 a494cfbc-eb01-0410-851d-a64ba20cac60
---
 kernel/branches/net/network/socket.inc     | 33 +++++++++++-----------
 kernel/branches/net/network/tcp_output.inc |  4 ++-
 2 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/kernel/branches/net/network/socket.inc b/kernel/branches/net/network/socket.inc
index f26f467fac..77c201cbf7 100644
--- a/kernel/branches/net/network/socket.inc
+++ b/kernel/branches/net/network/socket.inc
@@ -784,6 +784,7 @@ SOCKET_receive_tcp:
         mov     ecx, esi
         mov     edi, edx
         add     eax, STREAM_SOCKET.rcv
+        xor     edx, edx
         call    SOCKET_ring_read
         call    SOCKET_ring_free
 
@@ -1272,26 +1273,30 @@ SOCKET_ring_write:
 ;
 ; SOCKET_ring_read
 ;
-; reads the data, BUT DOES NOT CLEAR IT FROM MEMORY YET
-;
-;  IN:  eax = ptr to ring struct
-;       ecx = buffer size
+;  IN:  eax = ring struct ptr
+;       ecx = bytes to read
+;       edx = offset
 ;       edi = ptr to buffer
 ;
-;  OUT: ecx = number of bytes read
+;  OUT: ecx = number of bytes read (0 on error)
 ;
 ;-----------------------------------------------------------------
 align 4
 SOCKET_ring_read:
 
-        DEBUGF  1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u\n", eax, edi, ecx
+        DEBUGF  1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx
 
-        cmp     ecx, [eax + RING_BUFFER.size]
+        mov     esi, [eax + RING_BUFFER.read_ptr]
+        add     esi, edx                                        ; esi = start_ptr + offset
+
+        neg     edx
+        add     edx, [eax + RING_BUFFER.size]                   ; edx = snd.size - offset
+        jle     .no_data_at_all
+
+        cmp     ecx, edx
         ja      .less_data
 
   .copy:
-        mov     esi, [eax + RING_BUFFER.read_ptr]
-
         DEBUGF  2,"SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
         push    ecx
         shr     ecx, 1
@@ -1307,20 +1312,16 @@ SOCKET_ring_read:
         rep     movsd
   .nd:
         pop     ecx
+        ret
 
   .no_data_at_all:
+        DEBUGF  1,"SOCKET_ring_read_tcp: no data at all!\n", eax
         ret
 
   .less_data:
-        mov     ecx, [eax + RING_BUFFER.size]
-        cmp     ecx, 0
-        jb      .error
+        mov     ecx, edx
         jmp     .copy
 
-  .error:
-        DEBUGF  1,"SOCKET_ring_read: ringbuff=%x error!", eax
-        xor     ecx, ecx
-        ret
 
 ;-----------------------------------------------------------------
 ;
diff --git a/kernel/branches/net/network/tcp_output.inc b/kernel/branches/net/network/tcp_output.inc
index d2735327db..8ca907560f 100644
--- a/kernel/branches/net/network/tcp_output.inc
+++ b/kernel/branches/net/network/tcp_output.inc
@@ -456,11 +456,13 @@ TCP_send:
 ; edi = ptr to buffer
 
         mov     eax, [esp + 12]                 ; get socket ptr
-        add     [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number
 
         push    edx
         test    ecx, ecx
         jz      .nodata
+        mov     edx, [eax + TCP_SOCKET.SND_NXT]
+        add     [eax + TCP_SOCKET.SND_NXT], ecx ; update sequence number
+        sub     edx, [eax + TCP_SOCKET.SND_UNA]
         add     eax, STREAM_SOCKET.snd
         call    SOCKET_ring_read
   .nodata: