Subversion Repositories Kolibri OS

Rev

Rev 5976 | Rev 6912 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3545 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
5363 yogev_ezra 3
;; Copyright (C) KolibriOS team 2004-2015. All rights reserved.    ;;
3545 hidnplayr 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
 
4850 mario79 17
$Revision: 6011 $
3545 hidnplayr 18
 
4850 mario79 19
 
5976 hidnplayr 20
;-----------------------------------------------------------------;
21
;                                                                 ;
6011 hidnplayr 22
; tcp_usrclosed                                                   ;
5976 hidnplayr 23
;                                                                 ;
24
;  IN:  eax = socket ptr                                          ;
25
;                                                                 ;
26
;  OUT: /                                                         ;
27
;                                                                 ;
28
;-----------------------------------------------------------------;
3545 hidnplayr 29
align 4
6011 hidnplayr 30
tcp_usrclosed:
3545 hidnplayr 31
 
3556 hidnplayr 32
        DEBUGF  DEBUG_NETWORK_VERBOSE, "TCP_usrclosed: %x\n", eax
3545 hidnplayr 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
6011 hidnplayr 54
        call    tcp_close
3545 hidnplayr 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:
6011 hidnplayr 69
        call    socket_is_disconnected
3545 hidnplayr 70
  .ret:
71
        pop     ebx
72
        ret
73
 
74
 
5976 hidnplayr 75
;-----------------------------------------------------------------;
76
;                                                                 ;
6011 hidnplayr 77
; tcp_connect                                                     ;
5976 hidnplayr 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
;-----------------------------------------------------------------;
4030 hidnplayr 86
align 4
6011 hidnplayr 87
tcp_connect:
3545 hidnplayr 88
 
4030 hidnplayr 89
        test    [eax + SOCKET.state], SS_ISCONNECTED
90
        jnz     .eisconn
3545 hidnplayr 91
 
5442 hidnplayr 92
        inc     [TCPS_connattempt]                      ; update stats
93
 
4035 hidnplayr 94
        push    eax edx
4030 hidnplayr 95
        lea     ecx, [eax + SOCKET.mutex]
96
        call    mutex_lock
4035 hidnplayr 97
        pop     edx eax
4030 hidnplayr 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
6011 hidnplayr 115
        call    socket_find_port
4030 hidnplayr 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
 
6011 hidnplayr 127
        tcp_sendseqinit eax
4030 hidnplayr 128
 
129
        mov     ebx, eax
130
        lea     eax, [ebx + STREAM_SOCKET.snd]
6011 hidnplayr 131
        call    socket_ring_create
4030 hidnplayr 132
        test    eax, eax
133
        jz      .nomem
134
 
135
        lea     eax, [ebx + STREAM_SOCKET.rcv]
6011 hidnplayr 136
        call    socket_ring_create
4030 hidnplayr 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
 
6011 hidnplayr 145
        call    socket_is_connecting
4030 hidnplayr 146
 
147
; Now send the SYN packet to remote end
148
        push    eax
6011 hidnplayr 149
        call    tcp_output
4030 hidnplayr 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
 
6011 hidnplayr 186
        call    socket_block
4030 hidnplayr 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
6011 hidnplayr 193
        call    socket_notify
4030 hidnplayr 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
3545 hidnplayr 207
        ret