From e2ec33712eb5e983a2ed1a5bbd9e9e247848d194 Mon Sep 17 00:00:00 2001 From: hidnplayr Date: Sun, 15 Dec 2013 21:27:22 +0000 Subject: [PATCH] TCP: correctly close connection. git-svn-id: svn://kolibrios.org@4366 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/trunk/network/socket.inc | 20 ++++++----------- kernel/trunk/network/tcp_input.inc | 12 +++++----- kernel/trunk/network/tcp_output.inc | 2 +- kernel/trunk/network/tcp_subr.inc | 33 +++++++++++++++++++++++++-- kernel/trunk/network/tcp_usreq.inc | 35 +---------------------------- 5 files changed, 45 insertions(+), 57 deletions(-) diff --git a/kernel/trunk/network/socket.inc b/kernel/trunk/network/socket.inc index 8435f59ed9..d93041c0d5 100644 --- a/kernel/trunk/network/socket.inc +++ b/kernel/trunk/network/socket.inc @@ -714,6 +714,11 @@ SOCKET_close: call TCP_usrclosed + test eax, eax + jz @f + call TCP_output ; If connection is not closed yet, send the FIN + @@: + ret @@ -2196,7 +2201,7 @@ SOCKET_check_owner: ; ; Kernel calls this function when a certain process ends ; This function will check if the process had any open sockets -; And update them accordingly +; And update them accordingly (clean up) ; ; IN: edx = pid ; OUT: / @@ -2242,7 +2247,7 @@ SOCKET_process_end: cmp [eax + SOCKET.Protocol], IP_PROTO_TCP jne .free - call TCP_close + call TCP_disconnect jmp .closed .free: @@ -2347,20 +2352,9 @@ SOCKET_is_disconnected: and [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING) or [eax + SOCKET.state], SS_CANTRCVMORE + SS_CANTSENDMORE - cmp [eax + SOCKET.Protocol], IP_PROTO_TCP - je .tcp - - cmp [eax + SOCKET.Protocol], IP_PROTO_UDP - je .udp jmp SOCKET_notify - .tcp: - .udp: - mov [eax + UDP_SOCKET.LocalPort], 0 ; UDP and TCP structs store localport at the same offset - mov [eax + UDP_SOCKET.RemotePort], 0 - - jmp SOCKET_notify ;----------------------------------------------------------------- diff --git a/kernel/trunk/network/tcp_input.inc b/kernel/trunk/network/tcp_input.inc index 1042136aa5..773119412e 100644 --- a/kernel/trunk/network/tcp_input.inc +++ b/kernel/trunk/network/tcp_input.inc @@ -567,12 +567,13 @@ endl DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_input: Header prediction failed\n" ; Calculate receive window size - push edx - mov eax, SOCKETBUFFSIZE + mov eax, SOCKET_MAXDATA sub eax, [ebx + STREAM_SOCKET.rcv.size] + DEBUGF DEBUG_NETWORK_VERBOSE, "Space in receive buffer=%d\n", eax mov edx, [ebx + TCP_SOCKET.RCV_ADV] sub edx, [ebx + TCP_SOCKET.RCV_NXT] + DEBUGF DEBUG_NETWORK_VERBOSE, "Current advertised window=%d\n", edx cmp eax, edx jg @f mov eax, edx @@ -1189,12 +1190,9 @@ endl call mutex_unlock pop ebx - push ebx mov eax, ebx - call TCP_disconnect - pop ebx - - jmp .destroy_new_socket + call TCP_close + jmp .drop_no_socket .ack_tw: mov [ebx + TCP_SOCKET.timer_timed_wait], 2 * TCP_time_MSL diff --git a/kernel/trunk/network/tcp_output.inc b/kernel/trunk/network/tcp_output.inc index 43c4f6bc0a..1ae7e9266f 100644 --- a/kernel/trunk/network/tcp_output.inc +++ b/kernel/trunk/network/tcp_output.inc @@ -32,7 +32,7 @@ locals temp_bits db ? endl - DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: socket=%x\n", eax + DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_output: socket=%x state=%u\n", eax, [eax + TCP_SOCKET.t_state] push eax lea ecx, [eax + SOCKET.mutex] diff --git a/kernel/trunk/network/tcp_subr.inc b/kernel/trunk/network/tcp_subr.inc index 78cb57b4bb..c191d3046f 100644 --- a/kernel/trunk/network/tcp_subr.inc +++ b/kernel/trunk/network/tcp_subr.inc @@ -171,8 +171,35 @@ TCP_drop: +;------------------------- +; +; TCP_disconnect +; +; IN: eax = socket ptr +; OUT: eax = socket ptr / 0 +; +;------------------------- +align 4 +TCP_disconnect: + DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_disconnect: %x\n", eax + cmp [eax + TCP_SOCKET.t_state], TCPS_ESTABLISHED + jb TCP_close ; Connection not yet synchronised, just get rid of the socket + +; TODO: implement LINGER + + call SOCKET_is_disconnecting + call TCP_usrclosed + + test eax, eax + jz @f + push eax + call TCP_output + pop eax + @@: + + ret ;------------------------- @@ -180,7 +207,7 @@ TCP_drop: ; TCP_close ; ; IN: eax = socket ptr -; OUT: eax = socket ptr +; OUT: / ; ;------------------------- align 4 @@ -192,7 +219,9 @@ TCP_close: ;;; TODO: update slow start threshold call SOCKET_is_disconnected -;; call SOCKET_free + call SOCKET_free + + xor eax, eax ret diff --git a/kernel/trunk/network/tcp_usreq.inc b/kernel/trunk/network/tcp_usreq.inc index c1b45975f3..d990748b27 100644 --- a/kernel/trunk/network/tcp_usreq.inc +++ b/kernel/trunk/network/tcp_usreq.inc @@ -1,6 +1,6 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; -;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;; +;; Copyright (C) KolibriOS team 2004-2013. All rights reserved. ;; ;; Distributed under terms of the GNU General Public License ;; ;; ;; ;; Part of the TCP/IP network stack for KolibriOS ;; @@ -57,7 +57,6 @@ TCP_usrclosed: .wait1: mov [eax + TCP_SOCKET.t_state], TCPS_FIN_WAIT_1 - ; TODO: set timer? pop ebx ret @@ -68,7 +67,6 @@ TCP_usrclosed: .disc: call SOCKET_is_disconnected - ; TODO: set timer? .ret: pop ebx ret @@ -202,35 +200,4 @@ TCP_connect: stdcall cancel_timer_hs, [eax + TCP_SOCKET.timer_connect] xor eax, eax - ret - - - - -;------------------------- -; -; TCP_disconnect -; -; IN: eax = socket ptr -; OUT: eax = socket ptr -; -;------------------------- -align 4 -TCP_disconnect: - - DEBUGF DEBUG_NETWORK_VERBOSE, "TCP_disconnect: %x\n", eax - - cmp [eax + TCP_SOCKET.t_state], TCPS_ESTABLISHED - jb TCP_close - - -; TODO: implement LINGER ? - - call SOCKET_is_disconnecting - call TCP_usrclosed - - push eax - call TCP_output - pop eax - ret \ No newline at end of file