Subversion Repositories Kolibri OS

Rev

Rev 5984 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2015. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  Part of the TCP/IP network stack for KolibriOS                 ;;
  7. ;;                                                                 ;;
  8. ;;   Written by hidnplayr@kolibrios.org                            ;;
  9. ;;                                                                 ;;
  10. ;;    Based on the code of 4.4BSD                                  ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. $Revision: 6011 $
  18.  
  19.  
  20. ;-----------------------------------------------------------------;
  21. ;                                                                 ;
  22. ; tcp_usrclosed                                                   ;
  23. ;                                                                 ;
  24. ;  IN:  eax = socket ptr                                          ;
  25. ;                                                                 ;
  26. ;  OUT: /                                                         ;
  27. ;                                                                 ;
  28. ;-----------------------------------------------------------------;
  29. align 4
  30. tcp_usrclosed:
  31.  
  32.         DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_usrclosed: %x\n", eax
  33.  
  34.         push    ebx
  35.         mov     ebx, [eax + TCP_SOCKET.t_state]
  36.         mov     ebx, dword [.switch + ebx*4]
  37.         jmp     ebx
  38.  
  39.   .switch:
  40.         dd      .close                  ; TCPS_CLOSED
  41.         dd      .close                  ; TCPS_LISTEN
  42.         dd      .close                  ; TCPS_SYN_SENT
  43.         dd      .wait1                  ; TCPS_SYN_RECEIVED
  44.         dd      .wait1                  ; TCPS_ESTABLISHED
  45.         dd      .last_ack               ; TCPS_CLOSE_WAIT
  46.         dd      .ret                    ; TCPS_FIN_WAIT_1
  47.         dd      .ret                    ; TCPS_CLOSING
  48.         dd      .ret                    ; TCPS_LAST_ACK
  49.         dd      .disc                   ; TCPS_FIN_WAIT_2
  50.         dd      .disc                   ; TCPS_TIMED_WAIT
  51.  
  52.   .close:
  53.         mov     [eax + TCP_SOCKET.t_state], TCPS_CLOSED
  54.         call    tcp_close
  55.         pop     ebx
  56.         ret
  57.  
  58.   .wait1:
  59.         mov     [eax + TCP_SOCKET.t_state], TCPS_FIN_WAIT_1
  60.         pop     ebx
  61.         ret
  62.  
  63.   .last_ack:
  64.         mov     [eax + TCP_SOCKET.t_state], TCPS_LAST_ACK
  65.         pop     ebx
  66.         ret
  67.  
  68.   .disc:
  69.         call    socket_is_disconnected
  70.   .ret:
  71.         pop     ebx
  72.         ret
  73.  
  74.  
  75. ;-----------------------------------------------------------------;
  76. ;                                                                 ;
  77. ; tcp_connect                                                     ;
  78. ;                                                                 ;
  79. ;  IN:  eax = socket ptr                                          ;
  80. ;                                                                 ;
  81. ;  OUT: eax = 0 on success                                        ;
  82. ;       eax = -1 on error                                         ;
  83. ;       ebx = error code on error                                 ;
  84. ;                                                                 ;
  85. ;-----------------------------------------------------------------;
  86. align 4
  87. tcp_connect:
  88.  
  89.         test    [eax + SOCKET.state], SS_ISCONNECTED
  90.         jnz     .eisconn
  91.  
  92.         inc     [TCPS_connattempt]                      ; update stats
  93.  
  94.         push    eax edx
  95.         lea     ecx, [eax + SOCKET.mutex]
  96.         call    mutex_lock
  97.         pop     edx eax
  98.  
  99. ; Fill in local IP
  100.         cmp     [eax + IP_SOCKET.LocalIP], 0
  101.         jne     @f
  102.         push    [IP_LIST + 4]                                   ; FIXME: use correct local IP
  103.         pop     [eax + IP_SOCKET.LocalIP]
  104.  
  105. ; Fill in remote port and IP
  106.         pushw   [edx + 2]
  107.         pop     [eax + TCP_SOCKET.RemotePort]
  108.  
  109.         pushd   [edx + 4]
  110.         pop     [eax + IP_SOCKET.RemoteIP]
  111.  
  112. ; Find a local port, if user didnt define one
  113.         cmp     [eax + TCP_SOCKET.LocalPort], 0
  114.         jne     @f
  115.         call    socket_find_port
  116.        @@:
  117.  
  118. ; Start the TCP sequence
  119.         mov     [eax + TCP_SOCKET.timer_persist], 0
  120.         mov     [eax + TCP_SOCKET.t_state], TCPS_SYN_SENT
  121.  
  122.         push    [TCP_sequence_num]
  123.         add     [TCP_sequence_num], 6400
  124.         pop     [eax + TCP_SOCKET.ISS]
  125.         mov     [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_init
  126.  
  127.         tcp_sendseqinit eax
  128.  
  129.         mov     ebx, eax
  130.         lea     eax, [ebx + STREAM_SOCKET.snd]
  131.         call    socket_ring_create
  132.         test    eax, eax
  133.         jz      .nomem
  134.  
  135.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  136.         call    socket_ring_create
  137.         test    eax, eax
  138.         jz      .nomem
  139.  
  140.         push    ebx
  141.         lea     ecx, [ebx + SOCKET.mutex]
  142.         call    mutex_unlock
  143.         pop     eax
  144.  
  145.         call    socket_is_connecting
  146.  
  147. ; Now send the SYN packet to remote end
  148.         push    eax
  149.         call    tcp_output
  150.         pop     eax
  151.  
  152.   .block:
  153.         test    [eax + SOCKET.options], SO_NONBLOCK
  154.         jz      .waitforit
  155.  
  156.         xor     eax, eax
  157.         dec     eax
  158.         mov     ebx, EINPROGRESS
  159.         ret
  160.  
  161.   .nomem:
  162.         xor     eax, eax
  163.         dec     eax
  164.         mov     ebx, ENOMEM
  165.         ret
  166.  
  167.   .eisconn:
  168.         xor     eax, eax
  169.         dec     eax
  170.         mov     ebx, EISCONN
  171.         ret
  172.  
  173.   .waitforit:
  174.         push    eax
  175.         stdcall timer_hs, TCP_time_connect, 0, .timeout, eax
  176.         pop     ebx
  177.         mov     [ebx + TCP_SOCKET.timer_connect], eax
  178.         mov     eax, ebx
  179.  
  180.   .loop:
  181.         cmp     [eax + SOCKET.errorcode], 0
  182.         jne     .fail
  183.         cmp     [eax + TCP_SOCKET.t_state], TCPS_ESTABLISHED
  184.         je      .established
  185.  
  186.         call    socket_block
  187.         jmp     .loop
  188.  
  189.   .timeout:
  190.         mov     eax, [esp+4]
  191.         mov     [eax + SOCKET.errorcode], ETIMEDOUT
  192.         and     [eax + SOCKET.state], not SS_ISCONNECTING
  193.         call    socket_notify
  194.         ret     4
  195.  
  196.   .fail:
  197.         mov     ebx, [eax + SOCKET.errorcode]
  198.         mov     [eax + SOCKET.errorcode], 0                     ; Clear the error, we only need to send it to the caller once
  199.         xor     eax, eax
  200.         dec     eax
  201.         ret
  202.  
  203.   .established:
  204.         stdcall cancel_timer_hs, [eax + TCP_SOCKET.timer_connect]
  205.  
  206.         xor     eax, eax
  207.         ret