Subversion Repositories Kolibri OS

Rev

Rev 7678 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3545 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
7678 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2019. 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: 7679 $
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
 
4035 hidnplayr 92
        push    eax edx
4030 hidnplayr 93
        lea     ecx, [eax + SOCKET.mutex]
94
        call    mutex_lock
6912 hidnplayr 95
 
96
        mov     ebx, eax
97
        lea     eax, [ebx + STREAM_SOCKET.snd]
98
        call    socket_ring_create
99
        test    eax, eax
100
        jz      .nomem
101
 
102
        lea     eax, [ebx + STREAM_SOCKET.rcv]
103
        call    socket_ring_create
104
        test    eax, eax
105
        jz      .nomem
4035 hidnplayr 106
        pop     edx eax
4030 hidnplayr 107
 
108
; Fill in remote port and IP
109
        pushw   [edx + 2]
110
        pop     [eax + TCP_SOCKET.RemotePort]
111
 
112
        pushd   [edx + 4]
6912 hidnplayr 113
        pop     [eax + TCP_SOCKET.RemoteIP]
4030 hidnplayr 114
 
6912 hidnplayr 115
; Find route to host
116
        pusha
117
        push    eax
118
        mov     ebx, [eax + TCP_SOCKET.device]
119
        mov     edx, [eax + TCP_SOCKET.LocalIP]
120
        mov     eax, [eax + TCP_SOCKET.RemoteIP]
121
        call    ipv4_route
122
        test    eax, eax
123
        jz      .enoroute
124
        pop     eax
7679 hidnplayr 125
        mov     ebx, [net_device_list + edi]
6912 hidnplayr 126
        mov     [eax + TCP_SOCKET.device], ebx
127
        mov     [eax + TCP_SOCKET.LocalIP], edx
128
        popa
129
 
4030 hidnplayr 130
; Find a local port, if user didnt define one
131
        cmp     [eax + TCP_SOCKET.LocalPort], 0
132
        jne     @f
6011 hidnplayr 133
        call    socket_find_port
4030 hidnplayr 134
       @@:
135
 
6912 hidnplayr 136
; Compute window scaling factor
137
        push    ecx
138
        xor     ecx, ecx
139
        mov     ebx, TCP_max_win
140
  @@:
141
        cmp     ebx, SOCKET_BUFFER_SIZE
142
        ja      @f
143
        shl     ebx, 1
144
        inc     ecx
145
        cmp     ecx, TCP_max_winshift
146
        jb      @r
147
  @@:
148
        mov     [eax + TCP_SOCKET.request_r_scale], cl
149
        pop     ecx
150
 
151
        call    socket_is_connecting
152
        inc     [TCPS_connattempt]
153
 
4030 hidnplayr 154
        mov     [eax + TCP_SOCKET.timer_persist], 0
155
        mov     [eax + TCP_SOCKET.t_state], TCPS_SYN_SENT
156
 
6912 hidnplayr 157
        mov     [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_init
158
 
4030 hidnplayr 159
        push    [TCP_sequence_num]
6912 hidnplayr 160
        add     [TCP_sequence_num], TCP_ISSINCR/2
4030 hidnplayr 161
        pop     [eax + TCP_SOCKET.ISS]
162
 
6011 hidnplayr 163
        tcp_sendseqinit eax
4030 hidnplayr 164
 
6912 hidnplayr 165
        push    eax
166
        lea     ecx, [eax + SOCKET.mutex]
4030 hidnplayr 167
        call    mutex_unlock
168
        pop     eax
169
 
170
; Now send the SYN packet to remote end
171
        push    eax
6011 hidnplayr 172
        call    tcp_output
4030 hidnplayr 173
        pop     eax
174
 
175
        test    [eax + SOCKET.options], SO_NONBLOCK
176
        jz      .waitforit
177
 
178
        xor     eax, eax
179
        dec     eax
180
        mov     ebx, EINPROGRESS
181
        ret
182
 
183
  .nomem:
6912 hidnplayr 184
        pop     edx eax
4030 hidnplayr 185
        xor     eax, eax
186
        dec     eax
187
        mov     ebx, ENOMEM
188
        ret
189
 
190
  .eisconn:
191
        xor     eax, eax
192
        dec     eax
193
        mov     ebx, EISCONN
194
        ret
195
 
6912 hidnplayr 196
  .enoroute:
197
        pop     eax
198
        popa
199
        xor     eax, eax
200
        dec     eax
201
        mov     ebx, EADDRNOTAVAIL
202
        ret
203
 
4030 hidnplayr 204
  .waitforit:
205
        push    eax
206
        stdcall timer_hs, TCP_time_connect, 0, .timeout, eax
207
        pop     ebx
208
        mov     [ebx + TCP_SOCKET.timer_connect], eax
209
        mov     eax, ebx
210
 
211
  .loop:
212
        cmp     [eax + SOCKET.errorcode], 0
213
        jne     .fail
214
        cmp     [eax + TCP_SOCKET.t_state], TCPS_ESTABLISHED
215
        je      .established
216
 
6011 hidnplayr 217
        call    socket_block
4030 hidnplayr 218
        jmp     .loop
219
 
220
  .timeout:
221
        mov     eax, [esp+4]
222
        mov     [eax + SOCKET.errorcode], ETIMEDOUT
223
        and     [eax + SOCKET.state], not SS_ISCONNECTING
6011 hidnplayr 224
        call    socket_notify
4030 hidnplayr 225
        ret     4
226
 
227
  .fail:
228
        mov     ebx, [eax + SOCKET.errorcode]
229
        mov     [eax + SOCKET.errorcode], 0                     ; Clear the error, we only need to send it to the caller once
230
        xor     eax, eax
231
        dec     eax
232
        ret
233
 
234
  .established:
235
        stdcall cancel_timer_hs, [eax + TCP_SOCKET.timer_connect]
236
        xor     eax, eax
3545 hidnplayr 237
        ret