Subversion Repositories Kolibri OS

Rev

Rev 7680 | Rev 8026 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7680 Rev 7974
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2004-2019. All rights reserved.    ;;
3
;; Copyright (C) KolibriOS team 2004-2020. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
5
;;                                                                 ;;
6
;;  Part of the TCP/IP network stack for KolibriOS                 ;;
6
;;  Part of the TCP/IP network stack for KolibriOS                 ;;
7
;;                                                                 ;;
7
;;                                                                 ;;
8
;;   Written by hidnplayr@kolibrios.org,                           ;;
8
;;   Written by hidnplayr@kolibrios.org,                           ;;
9
;;     and Clevermouse.                                            ;;
9
;;     and Clevermouse.                                            ;;
10
;;                                                                 ;;
10
;;                                                                 ;;
11
;;       Based on code by mike.dld                                 ;;
11
;;       Based on code by mike.dld                                 ;;
12
;;                                                                 ;;
12
;;                                                                 ;;
13
;;         GNU GENERAL PUBLIC LICENSE                              ;;
13
;;         GNU GENERAL PUBLIC LICENSE                              ;;
14
;;          Version 2, June 1991                                   ;;
14
;;          Version 2, June 1991                                   ;;
15
;;                                                                 ;;
15
;;                                                                 ;;
16
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17
 
17
 
18
$Revision: 7680 $
18
$Revision: 7974 $
19
 
19
 
20
struct  SOCKET
20
struct  SOCKET
21
 
21
 
22
        NextPtr                 dd ?    ; pointer to next socket in list
22
        NextPtr                 dd ?    ; pointer to next socket in list
23
        PrevPtr                 dd ?    ; pointer to previous socket in list
23
        PrevPtr                 dd ?    ; pointer to previous socket in list
24
        Number                  dd ?    ; socket number
24
        Number                  dd ?    ; socket number
25
 
25
 
26
        mutex                   MUTEX
26
        mutex                   MUTEX
27
 
27
 
28
        PID                     dd ?    ; process ID
28
        PID                     dd ?    ; process ID
29
        TID                     dd ?    ; thread ID
29
        TID                     dd ?    ; thread ID
30
        Domain                  dd ?    ; INET4/INET6/LOCAL/..
30
        Domain                  dd ?    ; INET4/INET6/LOCAL/..
31
        Type                    dd ?    ; RAW/STREAM/DGRAM
31
        Type                    dd ?    ; RAW/STREAM/DGRAM
32
        Protocol                dd ?    ; UDP/TCP/ARP/ICMP
32
        Protocol                dd ?    ; UDP/TCP/ARP/ICMP
33
        errorcode               dd ?
33
        errorcode               dd ?
34
        device                  dd ?    ; device pointer, paired socket pointer if it's a local socket
34
        device                  dd ?    ; device pointer, paired socket pointer if it's a local socket
35
 
35
 
36
        options                 dd ?
36
        options                 dd ?
37
        state                   dd ?
37
        state                   dd ?
38
        backlog                 dw ?    ; number of incoming connections that can be queued
38
        backlog                 dw ?    ; number of incoming connections that can be queued
39
 
39
 
40
        snd_proc                dd ?
40
        snd_proc                dd ?
41
        rcv_proc                dd ?
41
        rcv_proc                dd ?
42
        connect_proc            dd ?
42
        connect_proc            dd ?
43
 
43
 
44
ends
44
ends
45
 
45
 
46
struct  IP_SOCKET               SOCKET
46
struct  IP_SOCKET               SOCKET
47
 
47
 
48
        LocalIP                 rd 4    ; network byte order
48
        LocalIP                 rd 4    ; network byte order
49
        RemoteIP                rd 4    ; network byte order
49
        RemoteIP                rd 4    ; network byte order
50
        ttl                     db ?
50
        ttl                     db ?
51
                                rb 3    ; align
51
                                rb 3    ; align
52
 
52
 
53
ends
53
ends
54
 
54
 
55
struct  TCP_SOCKET              IP_SOCKET
55
struct  TCP_SOCKET              IP_SOCKET
56
 
56
 
57
        LocalPort               dw ?    ; network byte order
57
        LocalPort               dw ?    ; network byte order
58
        RemotePort              dw ?    ; network byte order
58
        RemotePort              dw ?    ; network byte order
59
 
59
 
60
        t_state                 dd ?    ; TCB state
60
        t_state                 dd ?    ; TCB state
61
        t_rxtshift              db ?
61
        t_rxtshift              db ?
62
                                rb 3    ; align
62
                                rb 3    ; align
63
        t_rxtcur                dd ?
63
        t_rxtcur                dd ?
64
        t_dupacks               dd ?
64
        t_dupacks               dd ?
65
        t_maxseg                dd ?
65
        t_maxseg                dd ?
66
        t_flags                 dd ?
66
        t_flags                 dd ?
67
 
67
 
68
;---------------
68
;---------------
69
; RFC783 page 21
69
; RFC783 page 21
70
 
70
 
71
; send sequence
71
; send sequence
72
        SND_UNA                 dd ?    ; sequence number of unack'ed sent Packets
72
        SND_UNA                 dd ?    ; sequence number of unack'ed sent Packets
73
        SND_NXT                 dd ?    ; next send sequence number to use
73
        SND_NXT                 dd ?    ; next send sequence number to use
74
        SND_UP                  dd ?    ; urgent pointer
74
        SND_UP                  dd ?    ; urgent pointer
75
        SND_WL1                 dd ?    ; the sequence number of the last segment used to update the send window
75
        SND_WL1                 dd ?    ; the sequence number of the last segment used to update the send window
76
        SND_WL2                 dd ?    ; the acknowledgment number of the last segment used to update the send window
76
        SND_WL2                 dd ?    ; the acknowledgment number of the last segment used to update the send window
77
        ISS                     dd ?    ; initial send sequence number
77
        ISS                     dd ?    ; initial send sequence number
78
        SND_WND                 dd ?    ; send window
78
        SND_WND                 dd ?    ; send window
79
 
79
 
80
; receive sequence
80
; receive sequence
81
        RCV_WND                 dd ?    ; receive window
81
        RCV_WND                 dd ?    ; receive window
82
        RCV_NXT                 dd ?    ; next receive sequence number to use
82
        RCV_NXT                 dd ?    ; next receive sequence number to use
83
        RCV_UP                  dd ?    ; urgent pointer
83
        RCV_UP                  dd ?    ; urgent pointer
84
        IRS                     dd ?    ; initial receive sequence number
84
        IRS                     dd ?    ; initial receive sequence number
85
 
85
 
86
;---------------------
86
;---------------------
87
; Additional variables
87
; Additional variables
88
 
88
 
89
; receive variables
89
; receive variables
90
        RCV_ADV                 dd ?
90
        RCV_ADV                 dd ?
91
 
91
 
92
; retransmit variables
92
; retransmit variables
93
        SND_MAX                 dd ?
93
        SND_MAX                 dd ?
94
 
94
 
95
; congestion control
95
; congestion control
96
        SND_CWND                dd ?    ; congestion window
96
        SND_CWND                dd ?    ; congestion window
97
        SND_SSTHRESH            dd ?    ; slow start threshold
97
        SND_SSTHRESH            dd ?    ; slow start threshold
98
 
98
 
99
;----------------------
99
;----------------------
100
; Transmit timing stuff
100
; Transmit timing stuff
101
        t_idle                  dd ?
101
        t_idle                  dd ?
102
        t_rtt                   dd ?    ; round trip time
102
        t_rtt                   dd ?    ; round trip time
103
        t_rtseq                 dd ?
103
        t_rtseq                 dd ?
104
        t_srtt                  dd ?    ; smoothed round trip time
104
        t_srtt                  dd ?    ; smoothed round trip time
105
        t_rttvar                dd ?
105
        t_rttvar                dd ?
106
        t_rttmin                dd ?
106
        t_rttmin                dd ?
107
        max_sndwnd              dd ?
107
        max_sndwnd              dd ?
108
 
108
 
109
;-----------------
109
;-----------------
110
; Out-of-band data
110
; Out-of-band data
111
        t_oobflags              dd ?
111
        t_oobflags              dd ?
112
        t_iobc                  dd ?
112
        t_iobc                  dd ?
113
        t_softerror             dd ?
113
        t_softerror             dd ?
114
 
114
 
115
 
115
 
116
;---------
116
;---------
117
; RFC 1323                              ; the order of next 4 elements may not change
117
; RFC 1323                              ; the order of next 4 elements may not change
118
 
118
 
119
        SND_SCALE               db ?
119
        SND_SCALE               db ?
120
        RCV_SCALE               db ?
120
        RCV_SCALE               db ?
121
        requested_s_scale       db ?
121
        requested_s_scale       db ?
122
        request_r_scale         db ?
122
        request_r_scale         db ?
123
 
123
 
124
        ts_recent               dd ?    ; a copy of the most-recent valid timestamp from the other end
124
        ts_recent               dd ?    ; a copy of the most-recent valid timestamp from the other end
125
        ts_recent_age           dd ?
125
        ts_recent_age           dd ?
126
        last_ack_sent           dd ?
126
        last_ack_sent           dd ?
127
 
127
 
128
 
128
 
129
;-------
129
;-------
130
; Timers
130
; Timers
131
        timer_flags             dd ?
131
        timer_flags             dd ?
132
        timer_retransmission    dd ?    ; rexmt
132
        timer_retransmission    dd ?    ; rexmt
133
        timer_persist           dd ?
133
        timer_persist           dd ?
134
        timer_keepalive         dd ?    ; keepalive/syn timeout
134
        timer_keepalive         dd ?    ; keepalive/syn timeout
135
        timer_timed_wait        dd ?    ; also used as 2msl timer
135
        timer_timed_wait        dd ?    ; also used as 2msl timer
136
        timer_connect           dd ?
136
        timer_connect           dd ?
137
 
137
 
138
; extra
138
; extra
139
 
139
 
140
        ts_ecr                  dd ?    ; timestamp echo reply
140
        ts_ecr                  dd ?    ; timestamp echo reply
141
        ts_val                  dd ?
141
        ts_val                  dd ?
142
 
142
 
143
        seg_next                dd ?    ; re-assembly queue
143
        seg_next                dd ?    ; re-assembly queue
144
 
144
 
145
ends
145
ends
146
 
146
 
147
struct  UDP_SOCKET              IP_SOCKET
147
struct  UDP_SOCKET              IP_SOCKET
148
 
148
 
149
        LocalPort               dw ?    ; in network byte order
149
        LocalPort               dw ?    ; in network byte order
150
        RemotePort              dw ?    ; in network byte order
150
        RemotePort              dw ?    ; in network byte order
151
 
151
 
152
ends
152
ends
153
 
153
 
154
struct  RING_BUFFER
154
struct  RING_BUFFER
155
 
155
 
156
        mutex                   MUTEX
156
        mutex                   MUTEX
157
        start_ptr               dd ?    ; Pointer to start of buffer
157
        start_ptr               dd ?    ; Pointer to start of buffer
158
        end_ptr                 dd ?    ; pointer to end of buffer
158
        end_ptr                 dd ?    ; pointer to end of buffer
159
        read_ptr                dd ?    ; Read pointer
159
        read_ptr                dd ?    ; Read pointer
160
        write_ptr               dd ?    ; Write pointer
160
        write_ptr               dd ?    ; Write pointer
161
        size                    dd ?    ; Number of bytes buffered
161
        size                    dd ?    ; Number of bytes buffered
162
 
162
 
163
ends
163
ends
164
 
164
 
165
struct  STREAM_SOCKET           TCP_SOCKET
165
struct  STREAM_SOCKET           TCP_SOCKET
166
 
166
 
167
        rcv                     RING_BUFFER
167
        rcv                     RING_BUFFER
168
        snd                     RING_BUFFER
168
        snd                     RING_BUFFER
169
 
169
 
170
ends
170
ends
171
 
171
 
172
struct  socket_queue_entry
172
struct  socket_queue_entry
173
 
173
 
174
        data_ptr                dd ?
174
        data_ptr                dd ?
175
        data_size               dd ?
175
        data_size               dd ?
176
        buf_ptr                 dd ?
176
        buf_ptr                 dd ?
177
 
177
 
178
ends
178
ends
179
 
179
 
180
struct  socket_options
180
struct  socket_options
181
 
181
 
182
        level                   dd ?
182
        level                   dd ?
183
        optname                 dd ?
183
        optname                 dd ?
184
        optlen                  dd ?
184
        optlen                  dd ?
185
        optval                  dd ?
185
        optval                  dd ?
186
 
186
 
187
ends
187
ends
188
 
188
 
189
SOCKET_STRUCT_SIZE      = 4096          ; in bytes
189
SOCKET_STRUCT_SIZE      = 4096          ; in bytes
190
 
190
 
191
SOCKET_QUEUE_SIZE       = 10            ; maximum number of incoming packets queued for 1 socket
191
SOCKET_QUEUE_SIZE       = 10            ; maximum number of incoming packets queued for 1 socket
192
; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start
192
; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start
193
SOCKET_QUEUE_LOCATION   = (SOCKET_STRUCT_SIZE - SOCKET_QUEUE_SIZE*sizeof.socket_queue_entry - sizeof.queue)
193
SOCKET_QUEUE_LOCATION   = (SOCKET_STRUCT_SIZE - SOCKET_QUEUE_SIZE*sizeof.socket_queue_entry - sizeof.queue)
194
 
194
 
195
uglobal
195
uglobal
196
align 4
196
align 4
197
 
197
 
198
        net_sockets     rd 4
198
        net_sockets     rd 4
199
        last_socket_num dd ?
199
        last_socket_num dd ?
200
        last_UDP_port   dw ?            ; last used ephemeral port
200
        last_UDP_port   dw ?            ; last used ephemeral port
201
        last_TCP_port   dw ?            ;
201
        last_TCP_port   dw ?            ;
202
        socket_mutex    MUTEX
202
        socket_mutex    MUTEX
203
 
203
 
204
endg
204
endg
205
 
205
 
206
 
206
 
207
;-----------------------------------------------------------------;
207
;-----------------------------------------------------------------;
208
;                                                                 ;
208
;                                                                 ;
209
; socket_init                                                     ;
209
; socket_init                                                     ;
210
;                                                                 ;
210
;                                                                 ;
211
;-----------------------------------------------------------------;
211
;-----------------------------------------------------------------;
212
macro   socket_init {
212
macro   socket_init {
213
 
213
 
214
        xor     eax, eax
214
        xor     eax, eax
215
        mov     edi, net_sockets
215
        mov     edi, net_sockets
216
        mov     ecx, 5
216
        mov     ecx, 5
217
        rep stosd
217
        rep stosd
218
 
218
 
219
       @@:
219
       @@:
220
        pseudo_random eax
220
        pseudo_random eax
221
        cmp     ax, EPHEMERAL_PORT_MIN
221
        cmp     ax, EPHEMERAL_PORT_MIN
222
        jb      @r
222
        jb      @r
223
        cmp     ax, EPHEMERAL_PORT_MAX
223
        cmp     ax, EPHEMERAL_PORT_MAX
224
        ja      @r
224
        ja      @r
225
        xchg    al, ah
225
        xchg    al, ah
226
        mov     [last_UDP_port], ax
226
        mov     [last_UDP_port], ax
227
 
227
 
228
       @@:
228
       @@:
229
        pseudo_random eax
229
        pseudo_random eax
230
        cmp     ax, EPHEMERAL_PORT_MIN
230
        cmp     ax, EPHEMERAL_PORT_MIN
231
        jb      @r
231
        jb      @r
232
        cmp     ax, EPHEMERAL_PORT_MAX
232
        cmp     ax, EPHEMERAL_PORT_MAX
233
        ja      @r
233
        ja      @r
234
        xchg    al, ah
234
        xchg    al, ah
235
        mov     [last_TCP_port], ax
235
        mov     [last_TCP_port], ax
236
 
236
 
237
        mov     ecx, socket_mutex
237
        mov     ecx, socket_mutex
238
        call    mutex_init
238
        call    mutex_init
239
 
239
 
240
}
240
}
241
 
241
 
242
;-----------------------------------------------------------------;
242
;-----------------------------------------------------------------;
243
;                                                                 ;
243
;                                                                 ;
244
; Sockets API (system function 75)                                ;
244
; Sockets API (system function 75)                                ;
245
;                                                                 ;
245
;                                                                 ;
246
;-----------------------------------------------------------------;
246
;-----------------------------------------------------------------;
247
align 4
247
align 4
248
sys_socket:
248
sys_socket:
249
 
249
 
250
        mov     dword[esp+20], 0        ; Set error code to 0
250
        mov     dword[esp+20], 0        ; Set error code to 0
251
 
251
 
252
        cmp     ebx, 255
252
        cmp     ebx, 255
253
        jz      socket_debug
253
        jz      socket_debug
254
 
254
 
255
        cmp     ebx, .number
255
        cmp     ebx, .number
256
        ja      .error
256
        ja      .error
257
        jmp     dword [.table + 4*ebx]
257
        jmp     dword [.table + 4*ebx]
258
 
258
 
259
  .table:
259
  .table:
260
        dd      socket_open             ; 0
260
        dd      socket_open             ; 0
261
        dd      socket_close            ; 1
261
        dd      socket_close            ; 1
262
        dd      socket_bind             ; 2
262
        dd      socket_bind             ; 2
263
        dd      socket_listen           ; 3
263
        dd      socket_listen           ; 3
264
        dd      socket_connect          ; 4
264
        dd      socket_connect          ; 4
265
        dd      socket_accept           ; 5
265
        dd      socket_accept           ; 5
266
        dd      socket_send             ; 6
266
        dd      socket_send             ; 6
267
        dd      socket_receive          ; 7
267
        dd      socket_receive          ; 7
268
        dd      socket_set_opt          ; 8
268
        dd      socket_set_opt          ; 8
269
        dd      socket_get_opt          ; 9
269
        dd      socket_get_opt          ; 9
270
        dd      socket_pair             ; 10
270
        dd      socket_pair             ; 10
271
  .number = ($ - .table) / 4 - 1
271
  .number = ($ - .table) / 4 - 1
272
 
272
 
273
  .error:
273
  .error:
274
        mov     dword[esp+32], -1
274
        mov     dword[esp+32], -1
275
        mov     dword[esp+20], EINVAL
275
        mov     dword[esp+20], EINVAL
276
 
276
 
277
        ret
277
        ret
278
 
278
 
279
;-----------------------------------------------------------------;
279
;-----------------------------------------------------------------;
280
;                                                                 ;
280
;                                                                 ;
281
; socket_open: Create a new socket.                               ;
281
; socket_open: Create a new socket.                               ;
282
;                                                                 ;
282
;                                                                 ;
283
;   IN: ecx = domain                                              ;
283
;   IN: ecx = domain                                              ;
284
;       edx = type                                                ;
284
;       edx = type                                                ;
285
;       esi = protocol                                            ;
285
;       esi = protocol                                            ;
286
;                                                                 ;
286
;                                                                 ;
287
;  OUT: eax = socket number                                       ;
287
;  OUT: eax = socket number                                       ;
288
;       eax = -1 on error                                         ;
288
;       eax = -1 on error                                         ;
289
;       ebx = errorcode on error                                  ;
289
;       ebx = errorcode on error                                  ;
290
;                                                                 ;
290
;                                                                 ;
291
;-----------------------------------------------------------------;
291
;-----------------------------------------------------------------;
292
align 4
292
align 4
293
socket_open:
293
socket_open:
294
 
294
 
295
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_open: domain=%u type=%u protocol=%x ", ecx, edx, esi
295
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_open: domain=%u type=%u protocol=%x\n", ecx, edx, esi
296
 
296
 
297
        push    ecx edx esi
297
        push    ecx edx esi
298
        call    socket_alloc
298
        call    socket_alloc
299
        pop     esi edx ecx
299
        pop     esi edx ecx
300
        test    eax, eax
300
        test    eax, eax
301
        jz      .nobuffs
301
        jz      .nobuffs
302
 
302
 
303
        mov     [esp+32], edi           ; return socketnumber
303
        mov     [esp+32], edi           ; return socketnumber
304
        DEBUGF  DEBUG_NETWORK_VERBOSE, "socknum=%u\n", edi
304
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_open: socknum=%u\n", edi
305
 
305
 
306
        test    edx, SO_NONBLOCK
306
        test    edx, SO_NONBLOCK
307
        jz      @f
307
        jz      @f
308
        or      [eax + SOCKET.options], SO_NONBLOCK
308
        or      [eax + SOCKET.options], SO_NONBLOCK
309
        and     edx, not SO_NONBLOCK
309
        and     edx, not SO_NONBLOCK
310
  @@:
310
  @@:
311
 
311
 
312
        mov     [eax + SOCKET.Domain], ecx
312
        mov     [eax + SOCKET.Domain], ecx
313
        mov     [eax + SOCKET.Type], edx
313
        mov     [eax + SOCKET.Type], edx
314
        mov     [eax + SOCKET.Protocol], esi
314
        mov     [eax + SOCKET.Protocol], esi
315
        mov     [eax + SOCKET.connect_proc], connect_notsupp
315
        mov     [eax + SOCKET.connect_proc], connect_notsupp
316
 
316
 
317
        cmp     ecx, AF_INET4
317
        cmp     ecx, AF_INET4
318
        jne     .no_inet4
318
        jne     .no_inet4
319
 
319
 
320
        mov     [eax + IP_SOCKET.ttl], 128
320
        mov     [eax + IP_SOCKET.ttl], 128
321
 
321
 
322
        cmp     edx, SOCK_DGRAM
322
        cmp     edx, SOCK_DGRAM
323
        je      .udp
323
        je      .udp
324
 
324
 
325
        cmp     edx, SOCK_STREAM
325
        cmp     edx, SOCK_STREAM
326
        je      .tcp
326
        je      .tcp
327
 
327
 
328
        cmp     edx, SOCK_RAW
328
        cmp     edx, SOCK_RAW
329
        je      .raw
329
        je      .raw
330
 
330
 
331
  .no_inet4:
331
  .no_inet4:
332
        cmp     ecx, AF_PPP
332
        cmp     ecx, AF_PPP
333
        jne     .no_ppp
333
        jne     .no_ppp
334
 
334
 
335
        cmp     esi, PPP_PROTO_ETHERNET
335
        cmp     esi, PPP_PROTO_ETHERNET
336
        je      .pppoe
336
        je      .pppoe
337
 
337
 
338
  .no_ppp:
338
  .no_ppp:
339
  .unsupported:
339
  .unsupported:
340
        push    eax
340
        push    eax
341
        call    socket_free
341
        call    socket_free
342
        pop     eax
342
        pop     eax
343
        mov     dword[esp+20], EOPNOTSUPP
343
        mov     dword[esp+20], EOPNOTSUPP
344
        mov     dword[esp+32], -1
344
        mov     dword[esp+32], -1
345
        ret
345
        ret
346
 
346
 
347
  .nobuffs:
347
  .nobuffs:
348
        mov     dword[esp+20], ENOBUFS
348
        mov     dword[esp+20], ENOBUFS
349
        mov     dword[esp+32], -1
349
        mov     dword[esp+32], -1
350
        ret
350
        ret
351
 
351
 
352
  .raw:
352
  .raw:
353
        test    esi, esi        ; IP_PROTO_IP
353
        test    esi, esi        ; IP_PROTO_IP
354
        jz      .raw_ip
354
        jz      .raw_ip
355
 
355
 
356
        cmp     esi, IP_PROTO_ICMP
356
        cmp     esi, IP_PROTO_ICMP
357
        je      .raw_icmp
357
        je      .raw_icmp
358
 
358
 
359
        jmp     .unsupported
359
        jmp     .unsupported
360
 
360
 
361
align 4
361
align 4
362
  .udp:
362
  .udp:
363
        push    eax
363
        push    eax
364
        init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
364
        init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
365
        pop     eax
365
        pop     eax
366
 
366
 
367
        mov     [eax + SOCKET.Protocol], IP_PROTO_UDP
367
        mov     [eax + SOCKET.Protocol], IP_PROTO_UDP
368
        mov     [eax + SOCKET.snd_proc], socket_send_udp
368
        mov     [eax + SOCKET.snd_proc], socket_send_udp
369
        mov     [eax + SOCKET.rcv_proc], socket_receive_dgram
369
        mov     [eax + SOCKET.rcv_proc], socket_receive_dgram
370
        mov     [eax + SOCKET.connect_proc], udp_connect
370
        mov     [eax + SOCKET.connect_proc], udp_connect
371
        ret
371
        ret
372
 
372
 
373
align 4
373
align 4
374
  .tcp:
374
  .tcp:
375
        mov     [eax + SOCKET.Protocol], IP_PROTO_TCP
375
        mov     [eax + SOCKET.Protocol], IP_PROTO_TCP
376
        mov     [eax + SOCKET.snd_proc], socket_send_tcp
376
        mov     [eax + SOCKET.snd_proc], socket_send_tcp
377
        mov     [eax + SOCKET.rcv_proc], socket_receive_stream
377
        mov     [eax + SOCKET.rcv_proc], socket_receive_tcp
378
        mov     [eax + SOCKET.connect_proc], tcp_connect
378
        mov     [eax + SOCKET.connect_proc], tcp_connect
379
 
379
 
380
        tcp_init_socket eax
380
        tcp_init_socket eax
381
        ret
381
        ret
382
 
382
 
383
 
383
 
384
align 4
384
align 4
385
  .raw_ip:
385
  .raw_ip:
386
        push    eax
386
        push    eax
387
        init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
387
        init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
388
        pop     eax
388
        pop     eax
389
 
389
 
390
        mov     [eax + SOCKET.snd_proc], socket_send_ip
390
        mov     [eax + SOCKET.snd_proc], socket_send_ip
391
        mov     [eax + SOCKET.rcv_proc], socket_receive_dgram
391
        mov     [eax + SOCKET.rcv_proc], socket_receive_dgram
392
        mov     [eax + SOCKET.connect_proc], ipv4_connect
392
        mov     [eax + SOCKET.connect_proc], ipv4_connect
393
        ret
393
        ret
394
 
394
 
395
 
395
 
396
align 4
396
align 4
397
  .raw_icmp:
397
  .raw_icmp:
398
        push    eax
398
        push    eax
399
        init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
399
        init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
400
        pop     eax
400
        pop     eax
401
 
401
 
402
        mov     [eax + SOCKET.snd_proc], socket_send_icmp
402
        mov     [eax + SOCKET.snd_proc], socket_send_icmp
403
        mov     [eax + SOCKET.rcv_proc], socket_receive_dgram
403
        mov     [eax + SOCKET.rcv_proc], socket_receive_dgram
404
        mov     [eax + SOCKET.connect_proc], ipv4_connect
404
        mov     [eax + SOCKET.connect_proc], ipv4_connect
405
        ret
405
        ret
406
 
406
 
407
align 4
407
align 4
408
  .pppoe:
408
  .pppoe:
409
        push    eax
409
        push    eax
410
        init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
410
        init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
411
        pop     eax
411
        pop     eax
412
 
412
 
413
        mov     [eax + SOCKET.snd_proc], socket_send_pppoe
413
        mov     [eax + SOCKET.snd_proc], socket_send_pppoe
414
        mov     [eax + SOCKET.rcv_proc], socket_receive_dgram
414
        mov     [eax + SOCKET.rcv_proc], socket_receive_dgram
415
        ret
415
        ret
416
 
416
 
417
 
417
 
418
;-----------------------------------------------------------------;
418
;-----------------------------------------------------------------;
419
;                                                                 ;
419
;                                                                 ;
420
; socket_bind: Bind to a local port.                              ;
420
; socket_bind: Bind to a local port.                              ;
421
;                                                                 ;
421
;                                                                 ;
422
;   IN: ecx = socket number                                       ;
422
;   IN: ecx = socket number                                       ;
423
;       edx = pointer to sockaddr struct                          ;
423
;       edx = pointer to sockaddr struct                          ;
424
;       esi = length of sockaddr struct                           ;
424
;       esi = length of sockaddr struct                           ;
425
;                                                                 ;
425
;                                                                 ;
426
;  OUT: eax = 0 on success                                        ;
426
;  OUT: eax = 0 on success                                        ;
427
;       eax = -1 on error                                         ;
427
;       eax = -1 on error                                         ;
428
;       ebx = errorcode on error                                  ;
428
;       ebx = errorcode on error                                  ;
429
;                                                                 ;
429
;                                                                 ;
430
;-----------------------------------------------------------------;
430
;-----------------------------------------------------------------;
431
align 4
431
align 4
432
socket_bind:
432
socket_bind:
433
 
433
 
434
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_bind: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
434
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_bind: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
435
 
435
 
436
        call    socket_num_to_ptr
436
        call    socket_num_to_ptr
437
        test    eax, eax
437
        test    eax, eax
438
        jz      .invalid
438
        jz      .invalid
439
 
439
 
440
        cmp     esi, 2
440
        cmp     esi, 2
441
        jb      .invalid
441
        jb      .invalid
442
 
442
 
443
        cmp     [eax + UDP_SOCKET.LocalPort], 0 ; Socket can only be bound once
443
        cmp     [eax + UDP_SOCKET.LocalPort], 0 ; Socket can only be bound once
444
        jnz     .invalid
444
        jnz     .invalid
445
 
445
 
446
        cmp     word[edx], AF_INET4
446
        cmp     word[edx], AF_INET4
447
        je      .af_inet4
447
        je      .af_inet4
448
 
448
 
449
        cmp     word[edx], AF_LOCAL
449
        cmp     word[edx], AF_LOCAL
450
        je      .af_local
450
        je      .af_local
451
 
451
 
452
  .notsupp:
452
  .notsupp:
453
        mov     dword[esp+20], EOPNOTSUPP
453
        mov     dword[esp+20], EOPNOTSUPP
454
        mov     dword[esp+32], -1
454
        mov     dword[esp+32], -1
455
        ret
455
        ret
456
 
456
 
457
  .invalid:
457
  .invalid:
458
        mov     dword[esp+20], EINVAL
458
        mov     dword[esp+20], EINVAL
459
        mov     dword[esp+32], -1
459
        mov     dword[esp+32], -1
460
        ret
460
        ret
461
 
461
 
462
  .af_local:
462
  .af_local:
463
        ; TODO: write code here
463
        ; TODO: write code here
464
        mov     dword[esp+32], 0
464
        mov     dword[esp+32], 0
465
        ret
465
        ret
466
 
466
 
467
  .af_inet4:
467
  .af_inet4:
468
        cmp     esi, 6
468
        cmp     esi, 6
469
        jb      .invalid
469
        jb      .invalid
470
 
470
 
471
        cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
471
        cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
472
        je      .udp
472
        je      .udp
473
 
473
 
474
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
474
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
475
        je      .tcp
475
        je      .tcp
476
 
476
 
477
        jmp     .notsupp
477
        jmp     .notsupp
478
 
478
 
479
  .tcp:
479
  .tcp:
480
  .udp:
480
  .udp:
481
        pushd   [edx + 4]                       ; First, fill in the IP
481
        pushd   [edx + 4]                       ; First, fill in the IP
482
        popd    [eax + IP_SOCKET.LocalIP]
482
        popd    [eax + IP_SOCKET.LocalIP]
483
 
483
 
484
        mov     bx, [edx + 2]                   ; Did caller specify a local port?
484
        mov     bx, [edx + 2]                   ; Did caller specify a local port?
485
        test    bx, bx
485
        test    bx, bx
486
        jnz     .just_check
486
        jnz     .just_check
487
        call    socket_find_port                ; Nope, find an ephemeral one
487
        call    socket_find_port                ; Nope, find an ephemeral one
488
        jmp     .done
488
        jmp     .done
489
 
489
 
490
  .just_check:
490
  .just_check:
491
        call    socket_check_port               ; Yes, check if it's still available
491
        call    socket_check_port               ; Yes, check if it's still available
492
        jz      .addrinuse                      ; ZF is set by socket_check_port on error
492
        jz      .addrinuse                      ; ZF is set by socket_check_port on error
493
 
493
 
494
  .done:
494
  .done:
495
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_bind: local ip=%u.%u.%u.%u\n",\
495
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_bind: local ip=%u.%u.%u.%u\n",\
496
        [eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\
496
        [eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\
497
        [eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1
497
        [eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1
498
 
498
 
499
        mov     dword[esp+32], 0
499
        mov     dword[esp+32], 0
500
        ret
500
        ret
501
 
501
 
502
  .addrinuse:
502
  .addrinuse:
503
        mov     dword[esp+32], -1
503
        mov     dword[esp+32], -1
504
        mov     dword[esp+20], EADDRINUSE
504
        mov     dword[esp+20], EADDRINUSE
505
        ret
505
        ret
506
 
506
 
507
 
507
 
508
 
508
 
509
 
509
 
510
;-----------------------------------------------------------------;
510
;-----------------------------------------------------------------;
511
;                                                                 ;
511
;                                                                 ;
512
; socket_connect: Connect to the remote host.                     ;
512
; socket_connect: Connect to the remote host.                     ;
513
;                                                                 ;
513
;                                                                 ;
514
;   IN: ecx = socket number                                       ;
514
;   IN: ecx = socket number                                       ;
515
;       edx = pointer to sockaddr struct                          ;
515
;       edx = pointer to sockaddr struct                          ;
516
;       esi = length of sockaddr struct                           ;
516
;       esi = length of sockaddr struct                           ;
517
;                                                                 ;
517
;                                                                 ;
518
;  OUT: eax = 0 on success                                        ;
518
;  OUT: eax = 0 on success                                        ;
519
;       eax = -1 on error                                         ;
519
;       eax = -1 on error                                         ;
520
;       ebx = errorcode on error                                  ;
520
;       ebx = errorcode on error                                  ;
521
;                                                                 ;
521
;                                                                 ;
522
;-----------------------------------------------------------------;
522
;-----------------------------------------------------------------;
523
align 4
523
align 4
524
socket_connect:
524
socket_connect:
525
 
525
 
526
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_connect: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
526
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_connect: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
527
 
527
 
528
        call    socket_num_to_ptr
528
        call    socket_num_to_ptr
529
        test    eax, eax
529
        test    eax, eax
530
        jz      .invalid
530
        jz      .invalid
531
 
531
 
532
        cmp     esi, 8
532
        cmp     esi, 8
533
        jb      .invalid
533
        jb      .invalid
534
 
534
 
535
        cmp     [eax + SOCKET.state], SS_ISCONNECTING
535
        cmp     [eax + SOCKET.state], SS_ISCONNECTING
536
        je      .already
536
        je      .already
537
 
537
 
538
        test    [eax + SOCKET.options], SO_ACCEPTCON
538
        test    [eax + SOCKET.options], SO_ACCEPTCON
539
        jnz     .notsupp
539
        jnz     .notsupp
540
 
540
 
541
        call    [eax + SOCKET.connect_proc]
541
        call    [eax + SOCKET.connect_proc]
542
 
542
 
543
        mov     dword[esp+20], ebx
543
        mov     dword[esp+20], ebx
544
        mov     dword[esp+32], eax
544
        mov     dword[esp+32], eax
545
        ret
545
        ret
546
 
546
 
547
 
547
 
548
  .notsupp:
548
  .notsupp:
549
        mov     dword[esp+20], EOPNOTSUPP
549
        mov     dword[esp+20], EOPNOTSUPP
550
        mov     dword[esp+32], -1
550
        mov     dword[esp+32], -1
551
        ret
551
        ret
552
 
552
 
553
  .invalid:
553
  .invalid:
554
        mov     dword[esp+20], EINVAL
554
        mov     dword[esp+20], EINVAL
555
        mov     dword[esp+32], -1
555
        mov     dword[esp+32], -1
556
        ret
556
        ret
557
 
557
 
558
  .already:
558
  .already:
559
        mov     dword[esp+20], EALREADY
559
        mov     dword[esp+20], EALREADY
560
        mov     dword[esp+32], -1
560
        mov     dword[esp+32], -1
561
        ret
561
        ret
562
 
562
 
563
 
563
 
564
connect_notsupp:
564
connect_notsupp:
565
        xor     eax, eax
565
        xor     eax, eax
566
        dec     eax
566
        dec     eax
567
        mov     ebx, EOPNOTSUPP
567
        mov     ebx, EOPNOTSUPP
568
        ret
568
        ret
569
 
569
 
570
 
570
 
571
;-----------------------------------------------------------------;
571
;-----------------------------------------------------------------;
572
;                                                                 ;
572
;                                                                 ;
573
; socket_listen: Listen for incoming connections.                 ;
573
; socket_listen: Listen for incoming connections.                 ;
574
;                                                                 ;
574
;                                                                 ;
575
;   IN: ecx = socket number                                       ;
575
;   IN: ecx = socket number                                       ;
576
;       edx = backlog in edx                                      ;
576
;       edx = backlog in edx                                      ;
577
;                                                                 ;
577
;                                                                 ;
578
;  OUT: eax = 0 on success                                        ;
578
;  OUT: eax = 0 on success                                        ;
579
;       eax = -1 on error                                         ;
579
;       eax = -1 on error                                         ;
580
;       ebx = errorcode on error                                  ;
580
;       ebx = errorcode on error                                  ;
581
;                                                                 ;
581
;                                                                 ;
582
;-----------------------------------------------------------------;
582
;-----------------------------------------------------------------;
583
align 4
583
align 4
584
socket_listen:
584
socket_listen:
585
 
585
 
586
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_listen: socknum=%u backlog=%u\n", ecx, edx
586
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_listen: socknum=%u backlog=%u\n", ecx, edx
587
 
587
 
588
        call    socket_num_to_ptr
588
        call    socket_num_to_ptr
589
        test    eax, eax
589
        test    eax, eax
590
        jz      .invalid
590
        jz      .invalid
591
 
591
 
592
        cmp     [eax + SOCKET.Domain], AF_INET4
592
        cmp     [eax + SOCKET.Domain], AF_INET4
593
        jne     .notsupp
593
        jne     .notsupp
594
 
594
 
595
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
595
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
596
        jne     .invalid
596
        jne     .invalid
597
 
597
 
598
        cmp     [eax + TCP_SOCKET.LocalPort], 0
598
        cmp     [eax + TCP_SOCKET.LocalPort], 0
599
        je      .already
599
        je      .already
600
 
600
 
601
        cmp     [eax + IP_SOCKET.LocalIP], 0
601
        cmp     [eax + IP_SOCKET.LocalIP], 0
602
        jne     @f
602
        jne     @f
603
        push    [IPv4_address + 4]           ;;; fixme!!!!
603
        push    [IPv4_address + 4]           ;;; fixme!!!!
604
        pop     [eax + IP_SOCKET.LocalIP]
604
        pop     [eax + IP_SOCKET.LocalIP]
605
       @@:
605
       @@:
606
 
606
 
607
        cmp     edx, MAX_backlog
607
        cmp     edx, MAX_backlog
608
        jbe     @f
608
        jbe     @f
609
        mov     edx, MAX_backlog
609
        mov     edx, MAX_backlog
610
       @@:
610
       @@:
611
 
611
 
612
        mov     [eax + SOCKET.backlog], dx
612
        mov     [eax + SOCKET.backlog], dx
613
        or      [eax + SOCKET.options], SO_ACCEPTCON
613
        or      [eax + SOCKET.options], SO_ACCEPTCON
614
        mov     [eax + TCP_SOCKET.t_state], TCPS_LISTEN
614
        mov     [eax + TCP_SOCKET.t_state], TCPS_LISTEN
615
        mov     [eax + TCP_SOCKET.timer_keepalive], 0           ; disable keepalive timer
615
        mov     [eax + TCP_SOCKET.timer_keepalive], 0           ; disable keepalive timer
616
 
616
 
617
        push    eax
617
        push    eax
618
        init_queue (eax + SOCKET_QUEUE_LOCATION)                ; Set up sockets queue
618
        init_queue (eax + SOCKET_QUEUE_LOCATION)                ; Set up sockets queue
619
        pop     eax
619
        pop     eax
620
 
620
 
621
        mov     dword[esp+32], 0
621
        mov     dword[esp+32], 0
622
        ret
622
        ret
623
 
623
 
624
  .notsupp:
624
  .notsupp:
625
        mov     dword[esp+20], EOPNOTSUPP
625
        mov     dword[esp+20], EOPNOTSUPP
626
        mov     dword[esp+32], -1
626
        mov     dword[esp+32], -1
627
        ret
627
        ret
628
 
628
 
629
  .invalid:
629
  .invalid:
630
        mov     dword[esp+20], EINVAL
630
        mov     dword[esp+20], EINVAL
631
        mov     dword[esp+32], -1
631
        mov     dword[esp+32], -1
632
        ret
632
        ret
633
 
633
 
634
  .already:
634
  .already:
635
        mov     dword[esp+20], EALREADY
635
        mov     dword[esp+20], EALREADY
636
        mov     dword[esp+32], -1
636
        mov     dword[esp+32], -1
637
        ret
637
        ret
638
 
638
 
639
 
639
 
640
;-----------------------------------------------------------------;
640
;-----------------------------------------------------------------;
641
;                                                                 ;
641
;                                                                 ;
642
; socket_accept: Accept an incoming connection.                   ;
642
; socket_accept: Accept an incoming connection.                   ;
643
;                                                                 ;
643
;                                                                 ;
644
;   IN: ecx = socket number (of listening socket)                 ;
644
;   IN: ecx = socket number (of listening socket)                 ;
645
;       edx = ptr to sockaddr struct                              ;
645
;       edx = ptr to sockaddr struct                              ;
646
;       esi = length of sockaddr struct                           ;
646
;       esi = length of sockaddr struct                           ;
647
;                                                                 ;
647
;                                                                 ;
648
;  OUT: eax = newly created socket num                            ;
648
;  OUT: eax = newly created socket num                            ;
649
;       eax = -1 on error                                         ;
649
;       eax = -1 on error                                         ;
650
;       ebx = errorcode on error                                  ;
650
;       ebx = errorcode on error                                  ;
651
;                                                                 ;
651
;                                                                 ;
652
;-----------------------------------------------------------------;
652
;-----------------------------------------------------------------;
653
align 4
653
align 4
654
socket_accept:
654
socket_accept:
655
 
655
 
656
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_accept: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
656
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_accept: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
657
 
657
 
658
        call    socket_num_to_ptr
658
        call    socket_num_to_ptr
659
        test    eax, eax
659
        test    eax, eax
660
        jz      .invalid
660
        jz      .invalid
661
 
661
 
662
        test    [eax + SOCKET.options], SO_ACCEPTCON
662
        test    [eax + SOCKET.options], SO_ACCEPTCON
663
        jz      .invalid
663
        jz      .invalid
664
 
664
 
665
        cmp     [eax + SOCKET.Domain], AF_INET4
665
        cmp     [eax + SOCKET.Domain], AF_INET4
666
        jne     .notsupp
666
        jne     .notsupp
667
 
667
 
668
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
668
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
669
        jne     .invalid
669
        jne     .invalid
670
 
670
 
671
  .loop:
671
  .loop:
672
        get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .block
672
        get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .block
673
 
673
 
674
; Ok, we got a socket ptr
674
; Ok, we got a socket ptr
675
        mov     eax, [esi]
675
        mov     eax, [esi]
676
 
676
 
677
; Verify that it is (still) a valid socket
677
; Verify that it is (still) a valid socket
678
        call    socket_check
678
        call    socket_check
679
        jz      .invalid
679
        jz      .invalid
680
 
680
 
681
; Change sockets thread owner ID to that of the current thread
681
; Change sockets thread owner ID to that of the current thread
682
        mov     ebx, [TASK_BASE]
682
        mov     ebx, [TASK_BASE]
683
        mov     ebx, [ebx + TASKDATA.pid]
683
        mov     ebx, [ebx + TASKDATA.pid]
684
        mov     [eax + SOCKET.TID], ebx
684
        mov     [eax + SOCKET.TID], ebx
685
 
685
 
686
; Return socket number to caller
686
; Return socket number to caller
687
        mov     eax, [eax + SOCKET.Number]
687
        mov     eax, [eax + SOCKET.Number]
688
        mov     [esp+32], eax
688
        mov     [esp+32], eax
689
        ret
689
        ret
690
 
690
 
691
  .block:
691
  .block:
692
        test    [eax + SOCKET.options], SO_NONBLOCK
692
        test    [eax + SOCKET.options], SO_NONBLOCK
693
        jnz     .wouldblock
693
        jnz     .wouldblock
694
 
694
 
695
        call    socket_block
695
        call    socket_block
696
        jmp     .loop
696
        jmp     .loop
697
 
697
 
698
  .wouldblock:
698
  .wouldblock:
699
        mov     dword[esp+20], EWOULDBLOCK
699
        mov     dword[esp+20], EWOULDBLOCK
700
        mov     dword[esp+32], -1
700
        mov     dword[esp+32], -1
701
        ret
701
        ret
702
 
702
 
703
  .invalid:
703
  .invalid:
704
        mov     dword[esp+20], EINVAL
704
        mov     dword[esp+20], EINVAL
705
        mov     dword[esp+32], -1
705
        mov     dword[esp+32], -1
706
        ret
706
        ret
707
 
707
 
708
  .notsupp:
708
  .notsupp:
709
        mov     dword[esp+20], EOPNOTSUPP
709
        mov     dword[esp+20], EOPNOTSUPP
710
        mov     dword[esp+32], -1
710
        mov     dword[esp+32], -1
711
        ret
711
        ret
712
 
712
 
713
;-----------------------------------------------------------------;
713
;-----------------------------------------------------------------;
714
;                                                                 ;
714
;                                                                 ;
715
; socket_close: Close the socket (and connection).                ;
715
; socket_close: Close the socket (and connection).                ;
716
;                                                                 ;
716
;                                                                 ;
717
;   IN: ecx = socket number                                       ;
717
;   IN: ecx = socket number                                       ;
718
;                                                                 ;
718
;                                                                 ;
719
;  OUT: eax = 0 on success                                        ;
719
;  OUT: eax = 0 on success                                        ;
720
;       eax = -1 on error                                         ;
720
;       eax = -1 on error                                         ;
721
;       ebx = errorcode on error                                  ;
721
;       ebx = errorcode on error                                  ;
722
;                                                                 ;
722
;                                                                 ;
723
;-----------------------------------------------------------------;
723
;-----------------------------------------------------------------;
724
align 4
724
align 4
725
socket_close:
725
socket_close:
726
 
726
 
727
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_close: socknum=%u\n", ecx
727
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_close: socknum=%u\n", ecx
728
 
728
 
729
        call    socket_num_to_ptr
729
        call    socket_num_to_ptr
730
        test    eax, eax
730
        test    eax, eax
731
        jz      .invalid
731
        jz      .invalid
732
 
732
 
733
        mov     dword[esp+32], 0                                ; The socket exists, so we will succeed in closing it.
733
        mov     dword[esp+32], 0                                ; The socket exists, so we will succeed in closing it.
734
 
734
 
735
        or      [eax + SOCKET.options], SO_NONBLOCK             ; Mark the socket as non blocking, we dont want it to block any longer!
735
        or      [eax + SOCKET.options], SO_NONBLOCK             ; Mark the socket as non blocking, we dont want it to block any longer!
736
 
736
 
737
        test    [eax + SOCKET.state], SS_BLOCKED                ; Is the socket still in blocked state?
737
        test    [eax + SOCKET.state], SS_BLOCKED                ; Is the socket still in blocked state?
738
        jz      @f
738
        jz      @f
739
        call    socket_notify                                   ; Unblock it.
739
        call    socket_notify                                   ; Unblock it.
740
  @@:
740
  @@:
741
 
741
 
742
        cmp     [eax + SOCKET.Domain], AF_INET4
742
        cmp     [eax + SOCKET.Domain], AF_INET4
743
        jne     .free
743
        jne     .free
744
 
744
 
745
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
745
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
746
        je      .tcp
746
        je      .tcp
747
 
747
 
748
  .free:
748
  .free:
749
        call    socket_free
749
        call    socket_free
750
        ret
750
        ret
751
 
751
 
752
  .tcp:
752
  .tcp:
753
        call    tcp_usrclosed
753
        call    tcp_usrclosed
754
 
754
 
755
        test    eax, eax
755
        test    eax, eax
756
        jz      @f
756
        jz      @f
757
        call    tcp_output                                      ; If connection is not closed yet, send the FIN
757
        call    tcp_output                                      ; If connection is not closed yet, send the FIN
758
  @@:
758
  @@:
759
        ret
759
        ret
760
 
760
 
761
 
761
 
762
  .invalid:
762
  .invalid:
763
        mov     dword[esp+20], EINVAL
763
        mov     dword[esp+20], EINVAL
764
        mov     dword[esp+32], -1
764
        mov     dword[esp+32], -1
765
        ret
765
        ret
766
 
766
 
767
 
767
 
768
;-----------------------------------------------------------------;
768
;-----------------------------------------------------------------;
769
;                                                                 ;
769
;                                                                 ;
770
; socket_receive: Receive some data from the remote end.          ;
770
; socket_receive: Receive some data from the remote end.          ;
771
;                                                                 ;
771
;                                                                 ;
772
;   IN: ecx = socket number                                       ;
772
;   IN: ecx = socket number                                       ;
773
;       edx = addr to application buffer                          ;
773
;       edx = addr to application buffer                          ;
774
;       edx = length of application buffer                        ;
774
;       edx = length of application buffer                        ;
775
;       edi = flags                                               ;
775
;       edi = flags                                               ;
776
;                                                                 ;
776
;                                                                 ;
777
;  OUT: eax = number of bytes copied                              ;
777
;  OUT: eax = number of bytes copied                              ;
778
;       eax = -1 on error                                         ;
778
;       eax = -1 on error                                         ;
779
;       eax = 0 when socket has been closed by the remote end     ;
779
;       eax = 0 when socket has been closed by the remote end     ;
780
;       ebx = errorcode on error                                  ;
780
;       ebx = errorcode on error                                  ;
781
;                                                                 ;
781
;                                                                 ;
782
;-----------------------------------------------------------------;
782
;-----------------------------------------------------------------;
783
align 4
783
align 4
784
socket_receive:
784
socket_receive:
785
 
785
 
786
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: socknum=%u bufaddr=%x buflength=%u flags=%x\n", ecx, edx, esi, edi
786
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: socknum=%u bufaddr=%x buflength=%u flags=%x\n", ecx, edx, esi, edi
787
 
787
 
788
        call    socket_num_to_ptr
788
        call    socket_num_to_ptr
789
        test    eax, eax
789
        test    eax, eax
790
        jz      .invalid
790
        jz      .invalid
791
 
791
 
792
  .loop:
792
  .loop:
793
        push    edi
793
        push    edi
794
        call    [eax + SOCKET.rcv_proc]
794
        call    [eax + SOCKET.rcv_proc]
795
        pop     edi
795
        pop     edi
796
 
796
 
797
        test    [eax + SOCKET.state], SS_CANTRCVMORE
797
        test    [eax + SOCKET.state], SS_CANTRCVMORE
798
        jnz     .last_data
798
        jnz     .last_data
799
 
799
 
800
        cmp     ebx, EWOULDBLOCK
800
        cmp     ebx, EWOULDBLOCK
801
        jne     .return
801
        jne     .return
802
 
802
 
803
        test    edi, MSG_DONTWAIT
803
        test    edi, MSG_DONTWAIT
804
        jnz     .return_err
804
        jnz     .return_err
805
 
805
 
806
        test    [eax + SOCKET.options], SO_NONBLOCK
806
        test    [eax + SOCKET.options], SO_NONBLOCK
807
        jnz     .return_err
807
        jnz     .return_err
808
 
808
 
809
        call    socket_block
809
        call    socket_block
810
        jmp     .loop
810
        jmp     .loop
811
 
811
 
812
 
812
 
813
  .invalid:
813
  .invalid:
814
        push    EINVAL
814
        push    EINVAL
815
        pop     ebx
815
        pop     ebx
816
  .return_err:
816
  .return_err:
817
        mov     ecx, -1
817
        mov     ecx, -1
818
  .return:
818
  .return:
819
        mov     [esp+20], ebx
819
        mov     [esp+20], ebx
820
        mov     [esp+32], ecx
820
        mov     [esp+32], ecx
821
        ret
821
        ret
822
 
822
 
823
  .last_data:
823
  .last_data:
824
        test    ecx, ecx
824
        test    ecx, ecx
825
        jz      .return
825
        jz      .return
826
        call    socket_notify                                   ; Call me again!
826
        call    socket_notify                                   ; Call me again!
827
        jmp     .return
827
        jmp     .return
828
 
828
 
829
 
829
 
830
 
830
 
831
 
831
 
832
align 4
832
align 4
833
socket_receive_dgram:
833
socket_receive_dgram:
834
 
834
 
835
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: DGRAM\n"
835
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: DGRAM\n"
836
 
836
 
837
        test    edi, MSG_PEEK
837
        test    edi, MSG_PEEK
838
        jnz     .peek
838
        jnz     .peek
839
 
839
 
840
        mov     ebx, esi                                        ; buffer length
840
        mov     ebx, esi                                        ; buffer length
841
 
841
 
842
        get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .wouldblock ; sets esi only on success.
842
        get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .wouldblock ; sets esi only on success.
843
        mov     ecx, [esi + socket_queue_entry.data_size]
843
        mov     ecx, [esi + socket_queue_entry.data_size]
844
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: %u bytes data\n", ecx
844
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: %u bytes data\n", ecx
845
 
845
 
846
        cmp     ecx, ebx                                        ; If data segment does not fit in applications buffer, abort
846
        cmp     ecx, ebx                                        ; If data segment does not fit in applications buffer, abort
847
        ja      .too_small
847
        ja      .too_small
848
 
848
 
849
        push    eax ecx
849
        push    eax ecx
850
        push    [esi + socket_queue_entry.buf_ptr]              ; save the buffer addr so we can clear it later
850
        push    [esi + socket_queue_entry.buf_ptr]              ; save the buffer addr so we can clear it later
851
        mov     esi, [esi + socket_queue_entry.data_ptr]
851
        mov     esi, [esi + socket_queue_entry.data_ptr]
852
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: Source buffer=%x real addr=%x\n", [esp], esi
852
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: Source buffer=%x real addr=%x\n", [esp], esi
853
 
853
 
854
; copy the data from kernel buffer to application buffer
854
; copy the data from kernel buffer to application buffer
855
        mov     edi, edx                                        ; bufferaddr
855
        mov     edi, edx                                        ; bufferaddr
856
        shr     ecx, 1
856
        shr     ecx, 1
857
        jnc     .nb
857
        jnc     .nb
858
        movsb
858
        movsb
859
  .nb:
859
  .nb:
860
        shr     ecx, 1
860
        shr     ecx, 1
861
        jnc     .nw
861
        jnc     .nw
862
        movsw
862
        movsw
863
  .nw:
863
  .nw:
864
        test    ecx, ecx
864
        test    ecx, ecx
865
        jz      .nd
865
        jz      .nd
866
        rep movsd
866
        rep movsd
867
  .nd:
867
  .nd:
868
 
868
 
869
        call    net_buff_free
869
        call    net_buff_free
870
        pop     ecx eax                                         ; return number of bytes copied to application
870
        pop     ecx eax                                         ; return number of bytes copied to application
871
        cmp     [eax + SOCKET_QUEUE_LOCATION + queue.size], 0
871
        cmp     [eax + SOCKET_QUEUE_LOCATION + queue.size], 0
872
        je      @f
872
        je      @f
873
        call    socket_notify                                   ; Queue another network event
873
        call    socket_notify                                   ; Queue another network event
874
  @@:
874
  @@:
875
        xor     ebx, ebx                                        ; errorcode = 0 (no error)
875
        xor     ebx, ebx                                        ; errorcode = 0 (no error)
876
        ret
876
        ret
877
 
877
 
878
  .too_small:
878
  .too_small:
879
        mov     ecx, -1
879
        mov     ecx, -1
880
        push    EMSGSIZE
880
        push    EMSGSIZE
881
        pop     ebx
881
        pop     ebx
882
        ret
882
        ret
883
 
883
 
884
  .wouldblock:
884
  .wouldblock:
885
        push    EWOULDBLOCK
885
        push    EWOULDBLOCK
886
        pop     ebx
886
        pop     ebx
887
        ret
887
        ret
888
 
888
 
889
  .peek:
889
  .peek:
890
        xor     ebx, ebx
890
        xor     ebx, ebx
891
        xor     ecx, ecx
891
        xor     ecx, ecx
892
        cmp     [eax + SOCKET_QUEUE_LOCATION + queue.size], 0
892
        cmp     [eax + SOCKET_QUEUE_LOCATION + queue.size], 0
893
        je      @f
893
        je      @f
894
        mov     esi, [eax + SOCKET_QUEUE_LOCATION + queue.r_ptr]
894
        mov     esi, [eax + SOCKET_QUEUE_LOCATION + queue.r_ptr]
895
        mov     ecx, [esi + socket_queue_entry.data_size]
895
        mov     ecx, [esi + socket_queue_entry.data_size]
896
  @@:
896
  @@:
897
        ret
897
        ret
-
 
898
 
-
 
899
align 4
-
 
900
socket_receive_tcp:
-
 
901
 
-
 
902
        call    socket_receive_stream
-
 
903
 
-
 
904
        test    ecx, ecx
-
 
905
        jz      @f
-
 
906
        push    eax ebx ecx
-
 
907
        call    tcp_output
-
 
908
        pop     ecx ebx eax
-
 
909
  @@:
-
 
910
 
-
 
911
        ret
898
 
912
 
899
 
913
 
900
align 4
914
align 4
901
socket_receive_local:
915
socket_receive_local:
902
 
916
 
903
        ; does this socket have a PID yet?
917
        ; does this socket have a PID yet?
904
        cmp     [eax + SOCKET.PID], 0
918
        cmp     [eax + SOCKET.PID], 0
905
        jne     @f
919
        jne     @f
906
 
920
 
907
        ; Change PID to that of current process
921
        ; Change PID to that of current process
908
        mov     ebx, [TASK_BASE]
922
        mov     ebx, [TASK_BASE]
909
        mov     ebx, [ebx + TASKDATA.pid]
923
        mov     ebx, [ebx + TASKDATA.pid]
910
        mov     [eax + SOCKET.PID], ebx
924
        mov     [eax + SOCKET.PID], ebx
911
        mov     [eax + SOCKET.TID], ebx                         ; currently TID = PID in kolibrios :(
925
        mov     [eax + SOCKET.TID], ebx                         ; currently TID = PID in kolibrios :(
912
      @@:
926
      @@:
913
 
927
 
914
        mov     [eax + SOCKET.rcv_proc], socket_receive_stream
928
        mov     [eax + SOCKET.rcv_proc], socket_receive_stream
915
 
929
 
916
; ... continue to SOCKET_receive_stream
930
; ... continue to SOCKET_receive_stream
917
 
931
 
918
align 4
932
align 4
919
socket_receive_stream:
933
socket_receive_stream:
920
 
934
 
921
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: STREAM\n"
935
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: STREAM\n"
922
 
936
 
923
        cmp     [eax + STREAM_SOCKET.rcv.size], 0
937
        cmp     [eax + STREAM_SOCKET.rcv.size], 0
924
        je      .wouldblock
938
        je      .wouldblock
925
 
939
 
926
        test    edi, MSG_PEEK
940
        test    edi, MSG_PEEK
927
        jnz     .peek
941
        jnz     .peek
928
 
942
 
929
        mov     ecx, esi
943
        mov     ecx, esi
930
        mov     edi, edx
944
        mov     edi, edx
931
        xor     edx, edx
945
        xor     edx, edx
932
 
946
 
933
        push    eax
947
        push    eax
934
        add     eax, STREAM_SOCKET.rcv
948
        add     eax, STREAM_SOCKET.rcv
935
        call    socket_ring_read                                ; copy data from kernel buffer to application buffer
949
        call    socket_ring_read                                ; copy data from kernel buffer to application buffer
936
        call    socket_ring_free                                ; free read memory
950
        call    socket_ring_free                                ; free read memory
937
        pop     eax
951
        pop     eax
938
 
952
 
939
        cmp     [eax + STREAM_SOCKET.rcv.size], 0
953
        cmp     [eax + STREAM_SOCKET.rcv.size], 0
940
        jne     .more_data
954
        jne     .more_data
941
        xor     ebx, ebx                                        ; errorcode = 0 (no error)
955
        xor     ebx, ebx                                        ; errorcode = 0 (no error)
942
        ret
956
        ret
943
 
957
 
944
  .more_data:
958
  .more_data:
945
        call    socket_notify                                   ; Queue another network event
959
        call    socket_notify                                   ; Queue another network event
946
        xor     ebx, ebx                                        ; errorcode = 0 (no error)
960
        xor     ebx, ebx                                        ; errorcode = 0 (no error)
947
        ret
961
        ret
948
 
962
 
949
  .wouldblock:
963
  .wouldblock:
950
        push    EWOULDBLOCK
964
        push    EWOULDBLOCK
951
        pop     ebx
965
        pop     ebx
952
        xor     ecx, ecx
966
        xor     ecx, ecx
953
        ret
967
        ret
954
 
968
 
955
  .peek:
969
  .peek:
956
        mov     ecx, [eax + STREAM_SOCKET.rcv.size]
970
        mov     ecx, [eax + STREAM_SOCKET.rcv.size]
957
        xor     ebx, ebx
971
        xor     ebx, ebx
958
        ret
972
        ret
959
 
973
 
960
 
974
 
961
;-----------------------------------------------------------------;
975
;-----------------------------------------------------------------;
962
;                                                                 ;
976
;                                                                 ;
963
; socket_send: Send some data to the remote end.                  ;
977
; socket_send: Send some data to the remote end.                  ;
964
;                                                                 ;
978
;                                                                 ;
965
;   IN: ecx = socket number                                       ;
979
;   IN: ecx = socket number                                       ;
966
;       edx = pointer to data                                     ;
980
;       edx = pointer to data                                     ;
967
;       esi = data length                                         ;
981
;       esi = data length                                         ;
968
;       edi = flags                                               ;
982
;       edi = flags                                               ;
969
;                                                                 ;
983
;                                                                 ;
970
;  OUT: eax = number of bytes sent                                ;
984
;  OUT: eax = number of bytes sent                                ;
971
;       eax = -1 on error                                         ;
985
;       eax = -1 on error                                         ;
972
;       ebx = errorcode on error                                  ;
986
;       ebx = errorcode on error                                  ;
973
;                                                                 ;
987
;                                                                 ;
974
;-----------------------------------------------------------------;
988
;-----------------------------------------------------------------;
975
align 4
989
align 4
976
socket_send:
990
socket_send:
977
 
991
 
978
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi
992
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi
979
 
993
 
980
        call    socket_num_to_ptr
994
        call    socket_num_to_ptr
981
        test    eax, eax
995
        test    eax, eax
982
        jz      .invalid
996
        jz      .invalid
983
 
997
 
984
        mov     ecx, esi
998
        mov     ecx, esi
985
        mov     esi, edx
999
        mov     esi, edx
986
 
1000
 
987
        jmp     [eax + SOCKET.snd_proc]
1001
        jmp     [eax + SOCKET.snd_proc]
988
 
1002
 
989
  .invalid:
1003
  .invalid:
990
        mov     dword[esp+20], EINVAL
1004
        mov     dword[esp+20], EINVAL
991
        mov     dword[esp+32], -1
1005
        mov     dword[esp+32], -1
992
        ret
1006
        ret
993
 
1007
 
994
 
1008
 
995
align 4
1009
align 4
996
socket_send_udp:
1010
socket_send_udp:
997
 
1011
 
998
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: UDP\n"
1012
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: UDP\n"
999
 
1013
 
1000
        mov     [esp+32], ecx
1014
        mov     [esp+32], ecx
1001
        call    udp_output
1015
        call    udp_output
1002
        cmp     eax, -1
1016
        cmp     eax, -1
1003
        je      .error
1017
        je      .error
1004
        ret
1018
        ret
1005
 
1019
 
1006
  .error:
1020
  .error:
1007
        mov     dword[esp+32], -1
1021
        mov     dword[esp+32], -1
1008
        mov     dword[esp+20], EMSGSIZE ; FIXME: UDP_output should return error codes!
1022
        mov     dword[esp+20], EMSGSIZE ; FIXME: UDP_output should return error codes!
1009
        ret
1023
        ret
1010
 
1024
 
1011
 
1025
 
1012
align 4
1026
align 4
1013
socket_send_tcp:
1027
socket_send_tcp:
1014
 
1028
 
1015
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: TCP\n"
1029
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: TCP\n"
1016
 
1030
 
1017
        push    eax
1031
        push    eax
1018
        add     eax, STREAM_SOCKET.snd
1032
        add     eax, STREAM_SOCKET.snd
1019
        call    socket_ring_write
1033
        call    socket_ring_write
1020
        pop     eax
1034
        pop     eax
1021
 
1035
 
1022
        mov     [esp+32], ecx
1036
        mov     [esp+32], ecx
1023
        mov     [eax + SOCKET.errorcode], 0
1037
        mov     [eax + SOCKET.errorcode], 0
1024
        push    eax
1038
        push    eax
1025
        call    tcp_output              ; FIXME: this doesnt look pretty, does it?
1039
        call    tcp_output              ; FIXME: this doesnt look pretty, does it?
1026
        pop     eax
1040
        pop     eax
1027
        mov     eax, [eax + SOCKET.errorcode]
1041
        mov     eax, [eax + SOCKET.errorcode]
1028
        mov     [esp+20], eax
1042
        mov     [esp+20], eax
1029
        ret
1043
        ret
1030
 
1044
 
1031
 
1045
 
1032
align 4
1046
align 4
1033
socket_send_ip:
1047
socket_send_ip:
1034
 
1048
 
1035
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: IPv4\n"
1049
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: IPv4\n"
1036
 
1050
 
1037
        mov     [esp+32], ecx
1051
        mov     [esp+32], ecx
1038
        call    ipv4_output_raw
1052
        call    ipv4_output_raw
1039
        cmp     eax, -1
1053
        cmp     eax, -1
1040
        je      .error
1054
        je      .error
1041
        ret
1055
        ret
1042
 
1056
 
1043
  .error:
1057
  .error:
1044
        mov     dword[esp+32], eax
1058
        mov     dword[esp+32], eax
1045
        mov     dword[esp+20], ebx
1059
        mov     dword[esp+20], ebx
1046
        ret
1060
        ret
1047
 
1061
 
1048
 
1062
 
1049
align 4
1063
align 4
1050
socket_send_icmp:
1064
socket_send_icmp:
1051
 
1065
 
1052
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: ICMP\n"
1066
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: ICMP\n"
1053
 
1067
 
1054
        mov     [esp+32], ecx
1068
        mov     [esp+32], ecx
1055
        call    icmp_output_raw
1069
        call    icmp_output_raw
1056
        cmp     eax, -1
1070
        cmp     eax, -1
1057
        je      .error
1071
        je      .error
1058
        ret
1072
        ret
1059
 
1073
 
1060
  .error:
1074
  .error:
1061
        mov     dword[esp+32], eax
1075
        mov     dword[esp+32], eax
1062
        mov     dword[esp+20], ebx
1076
        mov     dword[esp+20], ebx
1063
        ret
1077
        ret
1064
 
1078
 
1065
 
1079
 
1066
align 4
1080
align 4
1067
socket_send_pppoe:
1081
socket_send_pppoe:
1068
 
1082
 
1069
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: PPPoE\n"
1083
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: PPPoE\n"
1070
 
1084
 
1071
        mov     [esp+32], ecx
1085
        mov     [esp+32], ecx
1072
        mov     ebx, [eax + SOCKET.device]
1086
        mov     ebx, [eax + SOCKET.device]
1073
 
1087
 
1074
        call    pppoe_discovery_output  ; FIXME: errorcodes
1088
        call    pppoe_discovery_output  ; FIXME: errorcodes
1075
        cmp     eax, -1
1089
        cmp     eax, -1
1076
        je      .error
1090
        je      .error
1077
        ret
1091
        ret
1078
 
1092
 
1079
  .error:
1093
  .error:
1080
        mov     dword[esp+32], -1
1094
        mov     dword[esp+32], -1
1081
        mov     dword[esp+20], EMSGSIZE
1095
        mov     dword[esp+20], EMSGSIZE
1082
        ret
1096
        ret
1083
 
1097
 
1084
 
1098
 
1085
 
1099
 
1086
align 4
1100
align 4
1087
socket_send_local:
1101
socket_send_local:
1088
 
1102
 
1089
        ; does this socket have a PID yet?
1103
        ; does this socket have a PID yet?
1090
        cmp     [eax + SOCKET.PID], 0
1104
        cmp     [eax + SOCKET.PID], 0
1091
        jne     @f
1105
        jne     @f
1092
 
1106
 
1093
        ; Change PID to that of current process
1107
        ; Change PID to that of current process
1094
        mov     ebx, [TASK_BASE]
1108
        mov     ebx, [TASK_BASE]
1095
        mov     ebx, [ebx + TASKDATA.pid]
1109
        mov     ebx, [ebx + TASKDATA.pid]
1096
        mov     [eax + SOCKET.PID], ebx
1110
        mov     [eax + SOCKET.PID], ebx
1097
        mov     [eax + SOCKET.TID], ebx         ; currently TID = PID in kolibrios :(
1111
        mov     [eax + SOCKET.TID], ebx         ; currently TID = PID in kolibrios :(
1098
      @@:
1112
      @@:
1099
        mov     [eax + SOCKET.snd_proc], socket_send_local_initialized
1113
        mov     [eax + SOCKET.snd_proc], socket_send_local_initialized
1100
 
1114
 
1101
align 4
1115
align 4
1102
socket_send_local_initialized:
1116
socket_send_local_initialized:
1103
 
1117
 
1104
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: LOCAL\n"
1118
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: LOCAL\n"
1105
 
1119
 
1106
        ; get the other side's socket and check if it still exists
1120
        ; get the other side's socket and check if it still exists
1107
        mov     eax, [eax + SOCKET.device]
1121
        mov     eax, [eax + SOCKET.device]
1108
        call    socket_check
1122
        call    socket_check
1109
        jz      .invalid
1123
        jz      .invalid
1110
 
1124
 
1111
        ; allright, shove in the data!
1125
        ; allright, shove in the data!
1112
        push    eax
1126
        push    eax
1113
        add     eax, STREAM_SOCKET.rcv
1127
        add     eax, STREAM_SOCKET.rcv
1114
        call    socket_ring_write
1128
        call    socket_ring_write
1115
        pop     eax
1129
        pop     eax
1116
 
1130
 
1117
        ; return the number of written bytes (or errorcode) to application
1131
        ; return the number of written bytes (or errorcode) to application
1118
        mov     [esp+32], ecx
1132
        mov     [esp+32], ecx
1119
 
1133
 
1120
        ; and notify the other end
1134
        ; and notify the other end
1121
        call    socket_notify
1135
        call    socket_notify
1122
 
1136
 
1123
        ret
1137
        ret
1124
 
1138
 
1125
  .invalid:
1139
  .invalid:
1126
        mov     dword[esp+32], -1
1140
        mov     dword[esp+32], -1
1127
        mov     dword[esp+20], EINVAL
1141
        mov     dword[esp+20], EINVAL
1128
        ret
1142
        ret
1129
 
1143
 
1130
 
1144
 
1131
;-----------------------------------------------------------------;
1145
;-----------------------------------------------------------------;
1132
;                                                                 ;
1146
;                                                                 ;
1133
; socket_get_opt: Read a socket option                            ;
1147
; socket_get_opt: Read a socket option                            ;
1134
;                                                                 ;
1148
;                                                                 ;
1135
;   IN: ecx = socket number                                       ;
1149
;   IN: ecx = socket number                                       ;
1136
;       edx = pointer to socket options struct                    ;
1150
;       edx = pointer to socket options struct                    ;
1137
;                                                                 ;
1151
;                                                                 ;
1138
;  OUT: eax = 0 on success                                        ;
1152
;  OUT: eax = 0 on success                                        ;
1139
;       eax = -1 on error                                         ;
1153
;       eax = -1 on error                                         ;
1140
;       ebx = errorcode on error                                  ;
1154
;       ebx = errorcode on error                                  ;
1141
;                                                                 ;
1155
;                                                                 ;
1142
;-----------------------------------------------------------------;
1156
;-----------------------------------------------------------------;
1143
align 4
1157
align 4
1144
socket_get_opt:
1158
socket_get_opt:
1145
 
1159
 
1146
; FIXME:
1160
; FIXME:
1147
; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
1161
; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
1148
; TODO: find best way to notify that send()'ed data were acknowledged
1162
; TODO: find best way to notify that send()'ed data were acknowledged
1149
; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*.
1163
; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*.
1150
 
1164
 
1151
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_get_opt\n"
1165
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_get_opt\n"
1152
 
1166
 
1153
        call    socket_num_to_ptr
1167
        call    socket_num_to_ptr
1154
        test    eax, eax
1168
        test    eax, eax
1155
        jz      .invalid
1169
        jz      .invalid
1156
 
1170
 
1157
        cmp     dword [edx], IP_PROTO_TCP
1171
        cmp     dword [edx], IP_PROTO_TCP
1158
        jne     .invalid
1172
        jne     .invalid
1159
        cmp     dword [edx+4], -2
1173
        cmp     dword [edx+4], -2
1160
        je      @f
1174
        je      @f
1161
        cmp     dword [edx+4], -3
1175
        cmp     dword [edx+4], -3
1162
        jne     .invalid
1176
        jne     .invalid
1163
@@:
1177
@@:
1164
;        mov     eax, [edx+12]
1178
;        mov     eax, [edx+12]
1165
;        test    eax, eax
1179
;        test    eax, eax
1166
;        jz      .fail
1180
;        jz      .fail
1167
;        cmp     dword [eax], 4
1181
;        cmp     dword [eax], 4
1168
;        mov     dword [eax], 4
1182
;        mov     dword [eax], 4
1169
;        jb      .fail
1183
;        jb      .fail
1170
;        stdcall net_socket_num_to_addr, ecx
1184
;        stdcall net_socket_num_to_addr, ecx
1171
;        test    eax, eax
1185
;        test    eax, eax
1172
;        jz      .fail
1186
;        jz      .fail
1173
;        ; todo: check that eax is really TCP socket
1187
;        ; todo: check that eax is really TCP socket
1174
;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
1188
;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
1175
;        cmp     dword [edx+4], -2
1189
;        cmp     dword [edx+4], -2
1176
;        jz      @f
1190
;        jz      @f
1177
;        mov     ecx, [eax + TCP_SOCKET.state]
1191
;        mov     ecx, [eax + TCP_SOCKET.state]
1178
@@:
1192
@@:
1179
        mov     eax, [edx+8]
1193
        mov     eax, [edx+8]
1180
        test    eax, eax
1194
        test    eax, eax
1181
        jz      @f
1195
        jz      @f
1182
        mov     [eax], ecx
1196
        mov     [eax], ecx
1183
@@:
1197
@@:
1184
        mov     dword [esp+32], 0
1198
        mov     dword [esp+32], 0
1185
        ret
1199
        ret
1186
 
1200
 
1187
  .invalid:
1201
  .invalid:
1188
        mov     dword[esp+32], -1
1202
        mov     dword[esp+32], -1
1189
        mov     dword[esp+20], EINVAL
1203
        mov     dword[esp+20], EINVAL
1190
        ret
1204
        ret
1191
 
1205
 
1192
 
1206
 
1193
;-----------------------------------------------------------------;
1207
;-----------------------------------------------------------------;
1194
;                                                                 ;
1208
;                                                                 ;
1195
; socket_set_options: Set a socket option.                        ;
1209
; socket_set_options: Set a socket option.                        ;
1196
;                                                                 ;
1210
;                                                                 ;
1197
;   IN: ecx = socket number                                       ;
1211
;   IN: ecx = socket number                                       ;
1198
;       edx = pointer to socket options struct                    ;
1212
;       edx = pointer to socket options struct                    ;
1199
;                                                                 ;
1213
;                                                                 ;
1200
;  OUT: eax = 0 on success                                        ;
1214
;  OUT: eax = 0 on success                                        ;
1201
;       eax = -1 on error                                         ;
1215
;       eax = -1 on error                                         ;
1202
;       ebx = errorcode on error                                  ;
1216
;       ebx = errorcode on error                                  ;
1203
;                                                                 ;
1217
;                                                                 ;
1204
;-----------------------------------------------------------------;
1218
;-----------------------------------------------------------------;
1205
align 4
1219
align 4
1206
socket_set_opt:
1220
socket_set_opt:
1207
 
1221
 
1208
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt\n"
1222
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt\n"
1209
 
1223
 
1210
        call    socket_num_to_ptr
1224
        call    socket_num_to_ptr
1211
        test    eax, eax
1225
        test    eax, eax
1212
        jz      .invalid
1226
        jz      .invalid
1213
 
1227
 
1214
        cmp     [edx + socket_options.level], IP_PROTO_IP
1228
        cmp     [edx + socket_options.level], IP_PROTO_IP
1215
        je      .ip
1229
        je      .ip
1216
        cmp     [edx + socket_options.level], SOL_SOCKET
1230
        cmp     [edx + socket_options.level], SOL_SOCKET
1217
        jne     .invalid
1231
        jne     .invalid
1218
 
1232
 
1219
  .socket:
1233
  .socket:
1220
        cmp     [edx + socket_options.optname], SO_BINDTODEVICE
1234
        cmp     [edx + socket_options.optname], SO_BINDTODEVICE
1221
        jne     .invalid
1235
        jne     .invalid
1222
 
1236
 
1223
  .bind:
1237
  .bind:
1224
        cmp     [edx + socket_options.optlen], 0
1238
        cmp     [edx + socket_options.optlen], 0
1225
        je      .unbind
1239
        je      .unbind
1226
 
1240
 
1227
        movzx   edx, byte[edx + socket_options.optval]
1241
        movzx   edx, byte[edx + socket_options.optval]
1228
        cmp     edx, NET_DEVICES_MAX
1242
        cmp     edx, NET_DEVICES_MAX
1229
        ja      .invalid
1243
        ja      .invalid
1230
 
1244
 
1231
        mov     edx, [net_device_list + 4*edx]
1245
        mov     edx, [net_device_list + 4*edx]
1232
        test    edx, edx
1246
        test    edx, edx
1233
        jz      .already
1247
        jz      .already
1234
        mov     [eax + SOCKET.device], edx
1248
        mov     [eax + SOCKET.device], edx
1235
 
1249
 
1236
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt: Bound socket %x to device %x\n", eax, edx
1250
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt: Bound socket %x to device %x\n", eax, edx
1237
 
1251
 
1238
        mov     dword[esp+32], 0        ; success!
1252
        mov     dword[esp+32], 0        ; success!
1239
        ret
1253
        ret
1240
 
1254
 
1241
  .unbind:
1255
  .unbind:
1242
        mov     [eax + SOCKET.device], 0
1256
        mov     [eax + SOCKET.device], 0
1243
 
1257
 
1244
        mov     dword[esp+32], 0        ; success!
1258
        mov     dword[esp+32], 0        ; success!
1245
        ret
1259
        ret
1246
 
1260
 
1247
  .ip:
1261
  .ip:
1248
        cmp     [edx + socket_options.optname], IP_TTL
1262
        cmp     [edx + socket_options.optname], IP_TTL
1249
        jne     .invalid
1263
        jne     .invalid
1250
 
1264
 
1251
  .ttl:
1265
  .ttl:
1252
        mov     bl, byte[edx + socket_options.optval]
1266
        mov     bl, byte[edx + socket_options.optval]
1253
        mov     [eax + IP_SOCKET.ttl], bl
1267
        mov     [eax + IP_SOCKET.ttl], bl
1254
 
1268
 
1255
        mov     dword[esp+32], 0        ; success!
1269
        mov     dword[esp+32], 0        ; success!
1256
        ret
1270
        ret
1257
 
1271
 
1258
  .already:
1272
  .already:
1259
        mov     dword[esp+20], EALREADY
1273
        mov     dword[esp+20], EALREADY
1260
        mov     dword[esp+32], -1
1274
        mov     dword[esp+32], -1
1261
        ret
1275
        ret
1262
 
1276
 
1263
  .invalid:
1277
  .invalid:
1264
        mov     dword[esp+20], EINVAL
1278
        mov     dword[esp+20], EINVAL
1265
        mov     dword[esp+32], -1
1279
        mov     dword[esp+32], -1
1266
        ret
1280
        ret
1267
 
1281
 
1268
 
1282
 
1269
 
1283
 
1270
 
1284
 
1271
;-----------------------------------------------------------------;
1285
;-----------------------------------------------------------------;
1272
;                                                                 ;
1286
;                                                                 ;
1273
; socket_pair: Allocate a pair of linked local sockets.           ;
1287
; socket_pair: Allocate a pair of linked local sockets.           ;
1274
;                                                                 ;
1288
;                                                                 ;
1275
;  IN: /                                                          ;
1289
;  IN: /                                                          ;
1276
;                                                                 ;
1290
;                                                                 ;
1277
; OUT: eax = socket1 num on success                               ;
1291
; OUT: eax = socket1 num on success                               ;
1278
;      eax = -1 on error                                          ;
1292
;      eax = -1 on error                                          ;
1279
;      ebx = socket2 num on success                               ;
1293
;      ebx = socket2 num on success                               ;
1280
;      ebx = errorcode on error                                   ;
1294
;      ebx = errorcode on error                                   ;
1281
;                                                                 ;
1295
;                                                                 ;
1282
;-----------------------------------------------------------------;
1296
;-----------------------------------------------------------------;
1283
align 4
1297
align 4
1284
socket_pair:
1298
socket_pair:
1285
 
1299
 
1286
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_pair\n"
1300
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_pair\n"
1287
 
1301
 
1288
        call    socket_alloc
1302
        call    socket_alloc
1289
        test    eax, eax
1303
        test    eax, eax
1290
        jz      .nomem1
1304
        jz      .nomem1
1291
        mov     [esp+32], edi   ; application's eax
1305
        mov     [esp+32], edi   ; application's eax
1292
 
1306
 
1293
        mov     [eax + SOCKET.Domain], AF_LOCAL
1307
        mov     [eax + SOCKET.Domain], AF_LOCAL
1294
        mov     [eax + SOCKET.Type], SOCK_STREAM
1308
        mov     [eax + SOCKET.Type], SOCK_STREAM
1295
        mov     [eax + SOCKET.Protocol], 0              ;;; CHECKME
1309
        mov     [eax + SOCKET.Protocol], 0              ;;; CHECKME
1296
        mov     [eax + SOCKET.snd_proc], socket_send_local
1310
        mov     [eax + SOCKET.snd_proc], socket_send_local
1297
        mov     [eax + SOCKET.rcv_proc], socket_receive_local
1311
        mov     [eax + SOCKET.rcv_proc], socket_receive_local
1298
        mov     [eax + SOCKET.PID], 0
1312
        mov     [eax + SOCKET.PID], 0
1299
        mov     ebx, eax
1313
        mov     ebx, eax
1300
 
1314
 
1301
        call    socket_alloc
1315
        call    socket_alloc
1302
        test    eax, eax
1316
        test    eax, eax
1303
        jz      .nomem2
1317
        jz      .nomem2
1304
        mov     [esp+20], edi   ; application's ebx
1318
        mov     [esp+20], edi   ; application's ebx
1305
 
1319
 
1306
        mov     [eax + SOCKET.Domain], AF_LOCAL
1320
        mov     [eax + SOCKET.Domain], AF_LOCAL
1307
        mov     [eax + SOCKET.Type], SOCK_STREAM
1321
        mov     [eax + SOCKET.Type], SOCK_STREAM
1308
        mov     [eax + SOCKET.Protocol], 0              ;;; CHECKME
1322
        mov     [eax + SOCKET.Protocol], 0              ;;; CHECKME
1309
        mov     [eax + SOCKET.snd_proc], socket_send_local
1323
        mov     [eax + SOCKET.snd_proc], socket_send_local
1310
        mov     [eax + SOCKET.rcv_proc], socket_receive_local
1324
        mov     [eax + SOCKET.rcv_proc], socket_receive_local
1311
        mov     [eax + SOCKET.PID], 0
1325
        mov     [eax + SOCKET.PID], 0
1312
 
1326
 
1313
        ; Link the two sockets to eachother
1327
        ; Link the two sockets to eachother
1314
        mov     [eax + SOCKET.device], ebx
1328
        mov     [eax + SOCKET.device], ebx
1315
        mov     [ebx + SOCKET.device], eax
1329
        mov     [ebx + SOCKET.device], eax
1316
 
1330
 
1317
        lea     eax, [eax + STREAM_SOCKET.rcv]
1331
        lea     eax, [eax + STREAM_SOCKET.rcv]
1318
        call    socket_ring_create
1332
        call    socket_ring_create
1319
        test    eax, eax
1333
        test    eax, eax
1320
        jz      .nomem2
1334
        jz      .nomem2
1321
 
1335
 
1322
        lea     eax, [ebx + STREAM_SOCKET.rcv]
1336
        lea     eax, [ebx + STREAM_SOCKET.rcv]
1323
        call    socket_ring_create
1337
        call    socket_ring_create
1324
        test    eax, eax
1338
        test    eax, eax
1325
        jz      .nomem2
1339
        jz      .nomem2
1326
 
1340
 
1327
        ret
1341
        ret
1328
 
1342
 
1329
  .nomem2:
1343
  .nomem2:
1330
        mov     eax, [esp+20]
1344
        mov     eax, [esp+20]
1331
        call    socket_free
1345
        call    socket_free
1332
 
1346
 
1333
  .nomem1:
1347
  .nomem1:
1334
        mov     eax, [esp+32]
1348
        mov     eax, [esp+32]
1335
        call    socket_free
1349
        call    socket_free
1336
 
1350
 
1337
        mov     dword[esp+32], -1
1351
        mov     dword[esp+32], -1
1338
        mov     dword[esp+20], ENOMEM
1352
        mov     dword[esp+20], ENOMEM
1339
        ret
1353
        ret
1340
 
1354
 
1341
 
1355
 
1342
 
1356
 
1343
;-----------------------------------------------------------------;
1357
;-----------------------------------------------------------------;
1344
;                                                                 ;
1358
;                                                                 ;
1345
; socket_debug: Copy socket variables to application buffer.      ;
1359
; socket_debug: Copy socket variables to application buffer.      ;
1346
;                                                                 ;
1360
;                                                                 ;
1347
;   IN: ecx = socket number                                       ;
1361
;   IN: ecx = socket number                                       ;
1348
;       edx = pointer to application buffer                       ;
1362
;       edx = pointer to application buffer                       ;
1349
;                                                                 ;
1363
;                                                                 ;
1350
;  OUT: eax = 0 on success                                        ;
1364
;  OUT: eax = 0 on success                                        ;
1351
;       eax = -1 on error                                         ;
1365
;       eax = -1 on error                                         ;
1352
;       ebx = errorcode on error                                  ;
1366
;       ebx = errorcode on error                                  ;
1353
;                                                                 ;
1367
;                                                                 ;
1354
;-----------------------------------------------------------------;
1368
;-----------------------------------------------------------------;
1355
align 4
1369
align 4
1356
socket_debug:
1370
socket_debug:
1357
 
1371
 
1358
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_debug\n"
1372
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_debug\n"
1359
 
1373
 
1360
        mov     edi, edx
1374
        mov     edi, edx
1361
 
1375
 
1362
        test    ecx, ecx
1376
        test    ecx, ecx
1363
        jz      .returnall
1377
        jz      .returnall
1364
 
1378
 
1365
        call    socket_num_to_ptr
1379
        call    socket_num_to_ptr
1366
        test    eax, eax
1380
        test    eax, eax
1367
        jz      .invalid
1381
        jz      .invalid
1368
 
1382
 
1369
        mov     esi, eax
1383
        mov     esi, eax
1370
        mov     ecx, SOCKET_STRUCT_SIZE/4
1384
        mov     ecx, SOCKET_STRUCT_SIZE/4
1371
        rep movsd
1385
        rep movsd
1372
 
1386
 
1373
        mov     dword[esp+32], 0
1387
        mov     dword[esp+32], 0
1374
        ret
1388
        ret
1375
 
1389
 
1376
  .returnall:
1390
  .returnall:
1377
        mov     ebx, net_sockets
1391
        mov     ebx, net_sockets
1378
  .next_socket:
1392
  .next_socket:
1379
        mov     ebx, [ebx + SOCKET.NextPtr]
1393
        mov     ebx, [ebx + SOCKET.NextPtr]
1380
        test    ebx, ebx
1394
        test    ebx, ebx
1381
        jz      .done
1395
        jz      .done
1382
        mov     eax, [ebx + SOCKET.Number]
1396
        mov     eax, [ebx + SOCKET.Number]
1383
        stosd
1397
        stosd
1384
        jmp     .next_socket
1398
        jmp     .next_socket
1385
  .done:
1399
  .done:
1386
        xor     eax, eax
1400
        xor     eax, eax
1387
        stosd
1401
        stosd
1388
        mov     dword[esp+32], eax
1402
        mov     dword[esp+32], eax
1389
        ret
1403
        ret
1390
 
1404
 
1391
  .invalid:
1405
  .invalid:
1392
        mov     dword[esp+32], -1
1406
        mov     dword[esp+32], -1
1393
        mov     dword[esp+20], EINVAL
1407
        mov     dword[esp+20], EINVAL
1394
        ret
1408
        ret
1395
 
1409
 
1396
 
1410
 
1397
;-----------------------------------------------------------------;
1411
;-----------------------------------------------------------------;
1398
;   ____                                                 ____     ;
1412
;   ____                                                 ____     ;
1399
;   \  /              End of sockets API                 \  /     ;
1413
;   \  /              End of sockets API                 \  /     ;
1400
;    \/                                                   \/      ;
1414
;    \/                                                   \/      ;
1401
;    ()        Internally used functions follow           ()      ;
1415
;    ()        Internally used functions follow           ()      ;
1402
;                                                                 ;
1416
;                                                                 ;
1403
;-----------------------------------------------------------------;
1417
;-----------------------------------------------------------------;
1404
 
1418
 
1405
 
1419
 
1406
;-----------------------------------------------------------------;
1420
;-----------------------------------------------------------------;
1407
;                                                                 ;
1421
;                                                                 ;
1408
; socket_find_port:                                               ;
1422
; socket_find_port:                                               ;
1409
; Fill in the local port number for TCP and UDP sockets           ;
1423
; Fill in the local port number for TCP and UDP sockets           ;
1410
; This procedure always works because the number of sockets is    ;
1424
; This procedure always works because the number of sockets is    ;
1411
; limited to a smaller number then the number of possible ports   ;
1425
; limited to a smaller number then the number of possible ports   ;
1412
;                                                                 ;
1426
;                                                                 ;
1413
;  IN:  eax = socket pointer                                      ;
1427
;  IN:  eax = socket pointer                                      ;
1414
;                                                                 ;
1428
;                                                                 ;
1415
;  OUT: /                                                         ;
1429
;  OUT: /                                                         ;
1416
;                                                                 ;
1430
;                                                                 ;
1417
;-----------------------------------------------------------------;
1431
;-----------------------------------------------------------------;
1418
align 4
1432
align 4
1419
socket_find_port:
1433
socket_find_port:
1420
 
1434
 
1421
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_find_port\n"
1435
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_find_port\n"
1422
 
1436
 
1423
        push    ebx esi ecx
1437
        push    ebx esi ecx
1424
 
1438
 
1425
        cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
1439
        cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
1426
        je      .udp
1440
        je      .udp
1427
 
1441
 
1428
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
1442
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
1429
        je      .tcp
1443
        je      .tcp
1430
 
1444
 
1431
        pop     ecx esi ebx
1445
        pop     ecx esi ebx
1432
        ret
1446
        ret
1433
 
1447
 
1434
  .udp:
1448
  .udp:
1435
        mov     bx, [last_UDP_port]
1449
        mov     bx, [last_UDP_port]
1436
        call    .findit
1450
        call    .findit
1437
        mov     [last_UDP_port], bx
1451
        mov     [last_UDP_port], bx
1438
 
1452
 
1439
        pop     ecx esi ebx
1453
        pop     ecx esi ebx
1440
        ret
1454
        ret
1441
 
1455
 
1442
  .tcp:
1456
  .tcp:
1443
        mov     bx, [last_TCP_port]
1457
        mov     bx, [last_TCP_port]
1444
        call    .findit
1458
        call    .findit
1445
        mov     [last_TCP_port], bx
1459
        mov     [last_TCP_port], bx
1446
 
1460
 
1447
        pop     ecx esi ebx
1461
        pop     ecx esi ebx
1448
        ret
1462
        ret
1449
 
1463
 
1450
 
1464
 
1451
  .restart:
1465
  .restart:
1452
        mov     bx, MIN_EPHEMERAL_PORT_N
1466
        mov     bx, MIN_EPHEMERAL_PORT_N
1453
  .findit:
1467
  .findit:
1454
        cmp     bx, MAX_EPHEMERAL_PORT_N
1468
        cmp     bx, MAX_EPHEMERAL_PORT_N
1455
        je      .restart
1469
        je      .restart
1456
 
1470
 
1457
        add     bh, 1
1471
        add     bh, 1
1458
        adc     bl, 0
1472
        adc     bl, 0
1459
 
1473
 
1460
        call    socket_check_port
1474
        call    socket_check_port
1461
        jz      .findit
1475
        jz      .findit
1462
        ret
1476
        ret
1463
 
1477
 
1464
 
1478
 
1465
 
1479
 
1466
;-----------------------------------------------------------------;
1480
;-----------------------------------------------------------------;
1467
;                                                                 ;
1481
;                                                                 ;
1468
; socket_check_port: (to be used with AF_INET only!)              ;
1482
; socket_check_port: (to be used with AF_INET only!)              ;
1469
; Checks if a local port number is unused                         ;
1483
; Checks if a local port number is unused                         ;
1470
; If the proposed port number is unused, it is filled in in the   ;
1484
; If the proposed port number is unused, it is filled in in the   ;
1471
; socket structure.                                               ;
1485
; socket structure.                                               ;
1472
;                                                                 ;
1486
;                                                                 ;
1473
;   IN: eax = socket ptr                                          ;
1487
;   IN: eax = socket ptr                                          ;
1474
;       bx = proposed socket number (network byte order)          ;
1488
;       bx = proposed socket number (network byte order)          ;
1475
;                                                                 ;
1489
;                                                                 ;
1476
;  OUT: ZF = set on error                                         ;
1490
;  OUT: ZF = set on error                                         ;
1477
;                                                                 ;
1491
;                                                                 ;
1478
;-----------------------------------------------------------------;
1492
;-----------------------------------------------------------------;
1479
align 4
1493
align 4
1480
socket_check_port:
1494
socket_check_port:
1481
 
1495
 
1482
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check_port: "
1496
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check_port: "
1483
 
1497
 
1484
        pusha
1498
        pusha
1485
        mov     ecx, socket_mutex
1499
        mov     ecx, socket_mutex
1486
        call    mutex_lock
1500
        call    mutex_lock
1487
        popa
1501
        popa
1488
 
1502
 
1489
        mov     ecx, [eax + SOCKET.Protocol]
1503
        mov     ecx, [eax + SOCKET.Protocol]
1490
        mov     edx, [eax + IP_SOCKET.LocalIP]
1504
        mov     edx, [eax + IP_SOCKET.LocalIP]
1491
        mov     esi, net_sockets
1505
        mov     esi, net_sockets
1492
 
1506
 
1493
  .next_socket:
1507
  .next_socket:
1494
        mov     esi, [esi + SOCKET.NextPtr]
1508
        mov     esi, [esi + SOCKET.NextPtr]
1495
        or      esi, esi
1509
        or      esi, esi
1496
        jz      .port_ok
1510
        jz      .port_ok
1497
 
1511
 
1498
        cmp     [esi + SOCKET.Protocol], ecx
1512
        cmp     [esi + SOCKET.Protocol], ecx
1499
        jne     .next_socket
1513
        jne     .next_socket
1500
 
1514
 
1501
        cmp     [esi + IP_SOCKET.LocalIP], edx
1515
        cmp     [esi + IP_SOCKET.LocalIP], edx
1502
        jne     .next_socket
1516
        jne     .next_socket
1503
 
1517
 
1504
        cmp     [esi + UDP_SOCKET.LocalPort], bx
1518
        cmp     [esi + UDP_SOCKET.LocalPort], bx
1505
        jne     .next_socket
1519
        jne     .next_socket
1506
 
1520
 
1507
        pusha
1521
        pusha
1508
        mov     ecx, socket_mutex
1522
        mov     ecx, socket_mutex
1509
        call    mutex_unlock
1523
        call    mutex_unlock
1510
        popa
1524
        popa
1511
 
1525
 
1512
        DEBUGF  DEBUG_NETWORK_VERBOSE, "local port %x already in use\n", bx  ; FIXME: find a way to print big endian values with debugf
1526
        DEBUGF  DEBUG_NETWORK_VERBOSE, "local port %x already in use\n", bx  ; FIXME: find a way to print big endian values with debugf
1513
        ret
1527
        ret
1514
 
1528
 
1515
  .port_ok:
1529
  .port_ok:
1516
        pusha
1530
        pusha
1517
        mov     ecx, socket_mutex
1531
        mov     ecx, socket_mutex
1518
        call    mutex_unlock
1532
        call    mutex_unlock
1519
        popa
1533
        popa
1520
 
1534
 
1521
        DEBUGF  DEBUG_NETWORK_VERBOSE, "local port %x is free\n", bx         ; FIXME: find a way to print big endian values with debugf
1535
        DEBUGF  DEBUG_NETWORK_VERBOSE, "local port %x is free\n", bx         ; FIXME: find a way to print big endian values with debugf
1522
        mov     [eax + UDP_SOCKET.LocalPort], bx
1536
        mov     [eax + UDP_SOCKET.LocalPort], bx
1523
        or      bx, bx                                  ; clear the zero-flag
1537
        or      bx, bx                                  ; clear the zero-flag
1524
        ret
1538
        ret
1525
 
1539
 
1526
 
1540
 
1527
 
1541
 
1528
;-----------------------------------------------------------------;
1542
;-----------------------------------------------------------------;
1529
;                                                                 ;
1543
;                                                                 ;
1530
; socket_input: Update a (stateless) socket with received data.   ;
1544
; socket_input: Update a (stateless) socket with received data.   ;
1531
;                                                                 ;
1545
;                                                                 ;
1532
; Note: The socket's mutex should already be set !                ;
1546
; Note: The socket's mutex should already be set !                ;
1533
;                                                                 ;
1547
;                                                                 ;
1534
;   IN: eax = socket ptr                                          ;
1548
;   IN: eax = socket ptr                                          ;
1535
;       ecx = data size                                           ;
1549
;       ecx = data size                                           ;
1536
;       esi = ptr to data                                         ;
1550
;       esi = ptr to data                                         ;
1537
;       [esp] = ptr to buf                                        ;
1551
;       [esp] = ptr to buf                                        ;
1538
;                                                                 ;
1552
;                                                                 ;
1539
;  OUT: /                                                         ;
1553
;  OUT: /                                                         ;
1540
;                                                                 ;
1554
;                                                                 ;
1541
;-----------------------------------------------------------------;
1555
;-----------------------------------------------------------------;
1542
align 4
1556
align 4
1543
socket_input:
1557
socket_input:
1544
 
1558
 
1545
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
1559
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
1546
 
1560
 
1547
        push    ecx
1561
        push    ecx
1548
        push    esi
1562
        push    esi
1549
        mov     esi, esp
1563
        mov     esi, esp
1550
 
1564
 
1551
        add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .full
1565
        add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .full
1552
 
1566
 
1553
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_input: success\n"
1567
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_input: success\n"
1554
        add     esp, sizeof.socket_queue_entry
1568
        add     esp, sizeof.socket_queue_entry
1555
 
1569
 
1556
        pusha
1570
        pusha
1557
        lea     ecx, [eax + SOCKET.mutex]
1571
        lea     ecx, [eax + SOCKET.mutex]
1558
        call    mutex_unlock
1572
        call    mutex_unlock
1559
        popa
1573
        popa
1560
 
1574
 
1561
        jmp     socket_notify
1575
        jmp     socket_notify
1562
 
1576
 
1563
  .full:
1577
  .full:
1564
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_input: socket %x is full!\n", eax
1578
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_input: socket %x is full!\n", eax
1565
 
1579
 
1566
        pusha
1580
        pusha
1567
        lea     ecx, [eax + SOCKET.mutex]
1581
        lea     ecx, [eax + SOCKET.mutex]
1568
        call    mutex_unlock
1582
        call    mutex_unlock
1569
        popa
1583
        popa
1570
 
1584
 
1571
        add     esp, 8
1585
        add     esp, 8
1572
        call    net_buff_free
1586
        call    net_buff_free
1573
        ret
1587
        ret
1574
 
1588
 
1575
 
1589
 
1576
;-----------------------------------------------------------------;
1590
;-----------------------------------------------------------------;
1577
;                                                                 ;
1591
;                                                                 ;
1578
; socket_ring_create: Create a ringbuffer for sockets.            ;
1592
; socket_ring_create: Create a ringbuffer for sockets.            ;
1579
;                                                                 ;
1593
;                                                                 ;
1580
;   IN: eax = ptr to ring struct                                  ;
1594
;   IN: eax = ptr to ring struct                                  ;
1581
;                                                                 ;
1595
;                                                                 ;
1582
;  OUT: eax = 0 on error                                          ;
1596
;  OUT: eax = 0 on error                                          ;
1583
;       eax = start ptr                                           ;
1597
;       eax = start ptr                                           ;
1584
;                                                                 ;
1598
;                                                                 ;
1585
;-----------------------------------------------------------------;
1599
;-----------------------------------------------------------------;
1586
align 4
1600
align 4
1587
socket_ring_create:
1601
socket_ring_create:
1588
 
1602
 
1589
        push    esi
1603
        push    esi
1590
        mov     esi, eax
1604
        mov     esi, eax
1591
 
1605
 
1592
        push    edx
1606
        push    edx
1593
        stdcall create_ring_buffer, SOCKET_BUFFER_SIZE, PG_SWR
1607
        stdcall create_ring_buffer, SOCKET_BUFFER_SIZE, PG_SWR
1594
        pop     edx
1608
        pop     edx
1595
        test    eax, eax
1609
        test    eax, eax
1596
        jz      .fail
1610
        jz      .fail
1597
 
1611
 
1598
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_create: %x\n", eax
1612
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_create: %x\n", eax
1599
 
1613
 
1600
        pusha
1614
        pusha
1601
        lea     ecx, [esi + RING_BUFFER.mutex]
1615
        lea     ecx, [esi + RING_BUFFER.mutex]
1602
        call    mutex_init
1616
        call    mutex_init
1603
        popa
1617
        popa
1604
 
1618
 
1605
        mov     [esi + RING_BUFFER.start_ptr], eax
1619
        mov     [esi + RING_BUFFER.start_ptr], eax
1606
        mov     [esi + RING_BUFFER.write_ptr], eax
1620
        mov     [esi + RING_BUFFER.write_ptr], eax
1607
        mov     [esi + RING_BUFFER.read_ptr], eax
1621
        mov     [esi + RING_BUFFER.read_ptr], eax
1608
        mov     [esi + RING_BUFFER.size], 0
1622
        mov     [esi + RING_BUFFER.size], 0
1609
        add     eax, SOCKET_BUFFER_SIZE
1623
        add     eax, SOCKET_BUFFER_SIZE
1610
        mov     [esi + RING_BUFFER.end_ptr], eax
1624
        mov     [esi + RING_BUFFER.end_ptr], eax
1611
        mov     eax, esi
1625
        mov     eax, esi
1612
 
1626
 
1613
        pop     esi
1627
        pop     esi
1614
        ret
1628
        ret
1615
 
1629
 
1616
  .fail:
1630
  .fail:
1617
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ring_create: Out of memory!\n"
1631
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ring_create: Out of memory!\n"
1618
        pop     esi
1632
        pop     esi
1619
        ret
1633
        ret
1620
 
1634
 
1621
;-----------------------------------------------------------------;
1635
;-----------------------------------------------------------------;
1622
;                                                                 ;
1636
;                                                                 ;
1623
; socket_ring_write: Write data to ring buffer.                   ;
1637
; socket_ring_write: Write data to ring buffer.                   ;
1624
;                                                                 ;
1638
;                                                                 ;
1625
;   IN: eax = ptr to ring struct                                  ;
1639
;   IN: eax = ptr to ring struct                                  ;
1626
;       ecx = data size                                           ;
1640
;       ecx = data size                                           ;
1627
;       esi = ptr to data                                         ;
1641
;       esi = ptr to data                                         ;
1628
;                                                                 ;
1642
;                                                                 ;
1629
;  OUT: ecx = number of bytes stored                              ;
1643
;  OUT: ecx = number of bytes stored                              ;
1630
;                                                                 ;
1644
;                                                                 ;
1631
;-----------------------------------------------------------------;
1645
;-----------------------------------------------------------------;
1632
align 4
1646
align 4
1633
socket_ring_write:
1647
socket_ring_write:
1634
 
1648
 
1635
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
1649
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
1636
 
1650
 
1637
; lock mutex
1651
; lock mutex
1638
        pusha
1652
        pusha
1639
        lea     ecx, [eax + RING_BUFFER.mutex]
1653
        lea     ecx, [eax + RING_BUFFER.mutex]
1640
        call    mutex_lock                                      ; TODO: check what registers this function actually destroys
1654
        call    mutex_lock                                      ; TODO: check what registers this function actually destroys
1641
        popa
1655
        popa
1642
 
1656
 
1643
; calculate available size
1657
; calculate available size
1644
        mov     edi, SOCKET_BUFFER_SIZE
1658
        mov     edi, SOCKET_BUFFER_SIZE
1645
        sub     edi, [eax + RING_BUFFER.size]                   ; available buffer size in edi
1659
        sub     edi, [eax + RING_BUFFER.size]                   ; available buffer size in edi
1646
        cmp     ecx, edi
1660
        cmp     ecx, edi
1647
        jbe     .copy
1661
        jbe     .copy
1648
        mov     ecx, edi
1662
        mov     ecx, edi
1649
  .copy:
1663
  .copy:
1650
        mov     edi, [eax + RING_BUFFER.write_ptr]
1664
        mov     edi, [eax + RING_BUFFER.write_ptr]
1651
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
1665
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
1652
 
1666
 
1653
; update write ptr
1667
; update write ptr
1654
        push    edi
1668
        push    edi
1655
        add     edi, ecx
1669
        add     edi, ecx
1656
        cmp     edi, [eax + RING_BUFFER.end_ptr]
1670
        cmp     edi, [eax + RING_BUFFER.end_ptr]
1657
        jb      @f
1671
        jb      @f
1658
        sub     edi, SOCKET_BUFFER_SIZE                         ; WRAP
1672
        sub     edi, SOCKET_BUFFER_SIZE                         ; WRAP
1659
  @@:
1673
  @@:
1660
        mov     [eax + RING_BUFFER.write_ptr], edi
1674
        mov     [eax + RING_BUFFER.write_ptr], edi
1661
        pop     edi
1675
        pop     edi
1662
 
1676
 
1663
; update size
1677
; update size
1664
        add     [eax + RING_BUFFER.size], ecx
1678
        add     [eax + RING_BUFFER.size], ecx
1665
 
1679
 
1666
; copy the data
1680
; copy the data
1667
        push    ecx
1681
        push    ecx
1668
        shr     ecx, 1
1682
        shr     ecx, 1
1669
        jnc     .nb
1683
        jnc     .nb
1670
        movsb
1684
        movsb
1671
  .nb:
1685
  .nb:
1672
        shr     ecx, 1
1686
        shr     ecx, 1
1673
        jnc     .nw
1687
        jnc     .nw
1674
        movsw
1688
        movsw
1675
  .nw:
1689
  .nw:
1676
        test    ecx, ecx
1690
        test    ecx, ecx
1677
        jz      .nd
1691
        jz      .nd
1678
        rep movsd
1692
        rep movsd
1679
  .nd:
1693
  .nd:
1680
        pop     ecx
1694
        pop     ecx
1681
 
1695
 
1682
; unlock mutex
1696
; unlock mutex
1683
        pusha
1697
        pusha
1684
        lea     ecx, [eax + RING_BUFFER.mutex]
1698
        lea     ecx, [eax + RING_BUFFER.mutex]
1685
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1699
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1686
        popa
1700
        popa
1687
 
1701
 
1688
        ret
1702
        ret
1689
 
1703
 
1690
;-----------------------------------------------------------------;
1704
;-----------------------------------------------------------------;
1691
;                                                                 ;
1705
;                                                                 ;
1692
; socket_ring_read: Read from ring buffer                         ;
1706
; socket_ring_read: Read from ring buffer                         ;
1693
;                                                                 ;
1707
;                                                                 ;
1694
;   IN: eax = ring struct ptr                                     ;
1708
;   IN: eax = ring struct ptr                                     ;
1695
;       ecx = bytes to read                                       ;
1709
;       ecx = bytes to read                                       ;
1696
;       edx = offset                                              ;
1710
;       edx = offset                                              ;
1697
;       edi = ptr to buffer start                                 ;
1711
;       edi = ptr to buffer start                                 ;
1698
;                                                                 ;
1712
;                                                                 ;
1699
;  OUT: eax = unchanged                                           ;
1713
;  OUT: eax = unchanged                                           ;
1700
;       ecx = number of bytes read (0 on error)                   ;
1714
;       ecx = number of bytes read (0 on error)                   ;
1701
;       edx = destroyed                                           ;
1715
;       edx = destroyed                                           ;
1702
;       esi = destroyed                                           ;
1716
;       esi = destroyed                                           ;
1703
;       edi = ptr to buffer end                                   ;
1717
;       edi = ptr to buffer end                                   ;
1704
;                                                                 ;
1718
;                                                                 ;
1705
;-----------------------------------------------------------------;
1719
;-----------------------------------------------------------------;
1706
align 4
1720
align 4
1707
socket_ring_read:
1721
socket_ring_read:
1708
 
1722
 
1709
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx
1723
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx
1710
 
1724
 
1711
        pusha
1725
        pusha
1712
        lea     ecx, [eax + RING_BUFFER.mutex]
1726
        lea     ecx, [eax + RING_BUFFER.mutex]
1713
        call    mutex_lock                                      ; TODO: check what registers this function actually destroys
1727
        call    mutex_lock                                      ; TODO: check what registers this function actually destroys
1714
        popa
1728
        popa
1715
 
1729
 
1716
        mov     esi, [eax + RING_BUFFER.read_ptr]
1730
        mov     esi, [eax + RING_BUFFER.read_ptr]
1717
        add     esi, edx                                        ; esi = start_ptr + offset
1731
        add     esi, edx                                        ; esi = start_ptr + offset
1718
 
1732
 
1719
        neg     edx
1733
        neg     edx
1720
        add     edx, [eax + RING_BUFFER.size]                   ; edx = snd.size - offset
1734
        add     edx, [eax + RING_BUFFER.size]                   ; edx = snd.size - offset
1721
        jle     .no_data_at_all
1735
        jle     .no_data_at_all
1722
 
1736
 
1723
        pusha
1737
        pusha
1724
        lea     ecx, [eax + RING_BUFFER.mutex]
1738
        lea     ecx, [eax + RING_BUFFER.mutex]
1725
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1739
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1726
        popa
1740
        popa
1727
 
1741
 
1728
        cmp     ecx, edx
1742
        cmp     ecx, edx
1729
        ja      .less_data
1743
        ja      .less_data
1730
 
1744
 
1731
  .copy:
1745
  .copy:
1732
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
1746
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
1733
        push    ecx
1747
        push    ecx
1734
        shr     ecx, 1
1748
        shr     ecx, 1
1735
        jnc     .nb
1749
        jnc     .nb
1736
        movsb
1750
        movsb
1737
  .nb:
1751
  .nb:
1738
        shr     ecx, 1
1752
        shr     ecx, 1
1739
        jnc     .nw
1753
        jnc     .nw
1740
        movsw
1754
        movsw
1741
  .nw:
1755
  .nw:
1742
        test    ecx, ecx
1756
        test    ecx, ecx
1743
        jz      .nd
1757
        jz      .nd
1744
        rep movsd
1758
        rep movsd
1745
  .nd:
1759
  .nd:
1746
        pop     ecx
1760
        pop     ecx
1747
        ret
1761
        ret
1748
 
1762
 
1749
  .no_data_at_all:
1763
  .no_data_at_all:
1750
        pusha
1764
        pusha
1751
        lea     ecx, [eax + RING_BUFFER.mutex]
1765
        lea     ecx, [eax + RING_BUFFER.mutex]
1752
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1766
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1753
        popa
1767
        popa
1754
 
1768
 
1755
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ring_read: no data at all!\n"
1769
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ring_read: no data at all!\n"
1756
        xor     ecx, ecx
1770
        xor     ecx, ecx
1757
        ret
1771
        ret
1758
 
1772
 
1759
  .less_data:
1773
  .less_data:
1760
        mov     ecx, edx
1774
        mov     ecx, edx
1761
        jmp     .copy
1775
        jmp     .copy
1762
 
1776
 
1763
 
1777
 
1764
;-----------------------------------------------------------------;
1778
;-----------------------------------------------------------------;
1765
;                                                                 ;
1779
;                                                                 ;
1766
; socket_ring_free: Free data from a ringbuffer.                  ;
1780
; socket_ring_free: Free data from a ringbuffer.                  ;
1767
;                                                                 ;
1781
;                                                                 ;
1768
;   IN: eax = ptr to ring struct                                  ;
1782
;   IN: eax = ptr to ring struct                                  ;
1769
;       ecx = data size                                           ;
1783
;       ecx = data size                                           ;
1770
;                                                                 ;
1784
;                                                                 ;
1771
;  OUT: ecx = number of freed bytes                               ;
1785
;  OUT: ecx = number of freed bytes                               ;
1772
;                                                                 ;
1786
;                                                                 ;
1773
;-----------------------------------------------------------------;
1787
;-----------------------------------------------------------------;
1774
align 4
1788
align 4
1775
socket_ring_free:
1789
socket_ring_free:
1776
 
1790
 
1777
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
1791
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
1778
 
1792
 
1779
        push    eax ecx
1793
        push    eax ecx
1780
        lea     ecx, [eax + RING_BUFFER.mutex]
1794
        lea     ecx, [eax + RING_BUFFER.mutex]
1781
        call    mutex_lock                                      ; TODO: check what registers this function actually destroys
1795
        call    mutex_lock                                      ; TODO: check what registers this function actually destroys
1782
        pop     ecx eax
1796
        pop     ecx eax
1783
 
1797
 
1784
        sub     [eax + RING_BUFFER.size], ecx
1798
        sub     [eax + RING_BUFFER.size], ecx
1785
        jb      .error
1799
        jb      .error
1786
        add     [eax + RING_BUFFER.read_ptr], ecx
1800
        add     [eax + RING_BUFFER.read_ptr], ecx
1787
 
1801
 
1788
        mov     edx, [eax + RING_BUFFER.end_ptr]
1802
        mov     edx, [eax + RING_BUFFER.end_ptr]
1789
        cmp     [eax + RING_BUFFER.read_ptr], edx
1803
        cmp     [eax + RING_BUFFER.read_ptr], edx
1790
        jb      @f
1804
        jb      @f
1791
        sub     [eax + RING_BUFFER.read_ptr], SOCKET_BUFFER_SIZE
1805
        sub     [eax + RING_BUFFER.read_ptr], SOCKET_BUFFER_SIZE
1792
       @@:
1806
       @@:
1793
 
1807
 
1794
        push    eax ecx
1808
        push    eax ecx
1795
        lea     ecx, [eax + RING_BUFFER.mutex]                  ; TODO: check what registers this function actually destroys
1809
        lea     ecx, [eax + RING_BUFFER.mutex]                  ; TODO: check what registers this function actually destroys
1796
        call    mutex_unlock
1810
        call    mutex_unlock
1797
        pop     ecx eax
1811
        pop     ecx eax
1798
 
1812
 
1799
        ret
1813
        ret
1800
 
1814
 
1801
  .error:       ; we could free all available bytes, but that would be stupid, i guess..
1815
  .error:       ; we could free all available bytes, but that would be stupid, i guess..
1802
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ring_free: buffer=%x error!\n", eax
1816
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ring_free: buffer=%x error!\n", eax
1803
        add     [eax + RING_BUFFER.size], ecx
1817
        add     [eax + RING_BUFFER.size], ecx
1804
 
1818
 
1805
        push    eax
1819
        push    eax
1806
        lea     ecx, [eax + RING_BUFFER.mutex]
1820
        lea     ecx, [eax + RING_BUFFER.mutex]
1807
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1821
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1808
        pop     eax
1822
        pop     eax
1809
 
1823
 
1810
        xor     ecx, ecx
1824
        xor     ecx, ecx
1811
        ret
1825
        ret
1812
 
1826
 
1813
 
1827
 
1814
;-----------------------------------------------------------------;
1828
;-----------------------------------------------------------------;
1815
;                                                                 ;
1829
;                                                                 ;
1816
; socket_block: Suspend the thread attached to a socket.          ;
1830
; socket_block: Suspend the thread attached to a socket.          ;
1817
;                                                                 ;
1831
;                                                                 ;
1818
;   IN: eax = socket ptr                                          ;
1832
;   IN: eax = socket ptr                                          ;
1819
;                                                                 ;
1833
;                                                                 ;
1820
;  OUT: eax = unchanged                                           ;
1834
;  OUT: eax = unchanged                                           ;
1821
;                                                                 ;
1835
;                                                                 ;
1822
;-----------------------------------------------------------------;
1836
;-----------------------------------------------------------------;
1823
align 4
1837
align 4
1824
socket_block:
1838
socket_block:
1825
 
1839
 
1826
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: %x\n", eax
1840
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: %x\n", eax
1827
 
1841
 
1828
        push    eax
1842
        push    eax
1829
 
1843
 
1830
        pushf
1844
        pushf
1831
        cli
1845
        cli
1832
 
-
 
1833
        ; Set the 'socket is blocked' flag
1846
        ; Set the 'socket is blocked' flag
1834
        or      [eax + SOCKET.state], SS_BLOCKED
1847
        or      [eax + SOCKET.state], SS_BLOCKED
1835
 
1848
 
1836
        ; Suspend the thread
1849
        ; Suspend the thread
1837
        push    edx
1850
        push    edx
1838
        mov     edx, [TASK_BASE]
1851
        mov     edx, [TASK_BASE]
1839
        mov     [edx + TASKDATA.state], 1               ; Suspended
1852
        mov     [edx + TASKDATA.state], TSTATE_RUN_SUSPENDED
1840
 
1853
 
1841
        ; Remember the thread ID so we can wake it up again
1854
        ; Remember the thread ID so we can wake it up again
1842
        mov     edx, [edx + TASKDATA.pid]
1855
        mov     edx, [edx + TASKDATA.pid]
1843
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: suspending thread: %u\n", edx
1856
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: suspending thread: %u\n", edx
1844
        mov     [eax + SOCKET.TID], edx
1857
        mov     [eax + SOCKET.TID], edx
1845
        pop     edx
1858
        pop     edx
1846
        popf
1859
        popf
1847
 
1860
 
1848
        call    change_task
1861
        call    change_task
1849
        pop     eax
1862
        pop     eax
1850
 
1863
 
1851
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: continuing\n"
1864
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: continuing\n"
1852
 
1865
 
1853
        ret
1866
        ret
1854
 
1867
 
1855
 
1868
 
1856
;-----------------------------------------------------------------;
1869
;-----------------------------------------------------------------;
1857
;                                                                 ;
1870
;                                                                 ;
1858
; socket_notify: Wake up socket owner thread.                     ;
1871
; socket_notify: Wake up socket owner thread.                     ;
1859
;                                                                 ;
1872
;                                                                 ;
1860
;   IN: eax = socket ptr                                          ;
1873
;   IN: eax = socket ptr                                          ;
1861
;                                                                 ;
1874
;                                                                 ;
1862
;  OUT: eax = unchanged                                           ;
1875
;  OUT: eax = unchanged                                           ;
1863
;                                                                 ;
1876
;                                                                 ;
1864
;-----------------------------------------------------------------;
1877
;-----------------------------------------------------------------;
1865
align 4
1878
align 4
1866
socket_notify:
1879
socket_notify:
1867
 
1880
 
1868
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: %x\n", eax
1881
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: %x\n", eax
1869
 
1882
 
1870
        call    socket_check
1883
        call    socket_check
1871
        jz      .error
1884
        jz      .error
1872
 
1885
 
1873
; Find the associated thread's TASK_DATA
1886
; Find the associated thread's TASK_DATA
1874
        push    ebx ecx esi
1887
        push    ebx ecx esi
1875
        mov     ebx, [eax + SOCKET.TID]
1888
        mov     ebx, [eax + SOCKET.TID]
1876
        test    ebx, ebx
1889
        test    ebx, ebx
1877
        jz      .error2
1890
        jz      .error2
1878
        xor     ecx, ecx
1891
        xor     ecx, ecx
1879
        inc     ecx
1892
        inc     ecx
1880
        mov     esi, TASK_DATA
1893
        mov     esi, TASK_DATA
1881
  .next:
1894
  .next:
1882
        cmp     [esi + TASKDATA.pid], ebx
1895
        cmp     [esi + TASKDATA.pid], ebx
1883
        je      .found
1896
        je      .found
1884
        inc     ecx
1897
        inc     ecx
1885
        add     esi, 0x20
1898
        add     esi, sizeof.TASKDATA
1886
        cmp     ecx, [TASK_COUNT]
1899
        cmp     ecx, [TASK_COUNT]
1887
        jbe     .next
1900
        jbe     .next
1888
 
1901
 
1889
  .error2:
1902
  .error2:
1890
; PID not found, TODO: close socket!
1903
; PID not found, TODO: close socket!
1891
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_notify: error finding thread 0x%x !\n", ebx
1904
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_notify: error finding thread 0x%x !\n", ebx
1892
        pop     esi ecx ebx
1905
        pop     esi ecx ebx
1893
        ret
1906
        ret
1894
 
1907
 
1895
  .error:
1908
  .error:
1896
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_notify: invalid socket ptr: 0x%x !\n", eax
1909
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_notify: invalid socket ptr: 0x%x !\n", eax
1897
        ret
1910
        ret
1898
 
1911
 
1899
  .found:
1912
  .found:
1900
        test    [eax + SOCKET.state], SS_BLOCKED
1913
        test    [eax + SOCKET.state], SS_BLOCKED
1901
        jnz     .un_block
1914
        jnz     .un_block
1902
 
1915
 
1903
; Socket and thread exists and socket is of non blocking type.
1916
; Socket and thread exists and socket is of non blocking type.
1904
; We'll try to flag an event to the thread.
1917
; We'll try to flag an event to the thread.
1905
        shl     ecx, 8
1918
        shl     ecx, 8
1906
        or      [SLOT_BASE + ecx + APPDATA.event_mask], EVENT_NETWORK
1919
        or      [SLOT_BASE + ecx + APPDATA.event_mask], EVENT_NETWORK
1907
 
1920
 
1908
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: poking thread %u!\n", ebx
1921
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: poking thread %u!\n", ebx
1909
        pop     esi ecx ebx
1922
        pop     esi ecx ebx
1910
        ret
1923
        ret
1911
 
1924
 
1912
 
1925
 
1913
  .un_block:
1926
  .un_block:
1914
; Socket and thread exists and socket is of blocking type
1927
; Socket and thread exists and socket is of blocking type
1915
; We'll try to unblock it.
1928
; We'll try to unblock it.
1916
        and     [eax + SOCKET.state], not SS_BLOCKED    ; Clear the 'socket is blocked' flag
1929
        and     [eax + SOCKET.state], not SS_BLOCKED    ; Clear the 'socket is blocked' flag
1917
        mov     [esi + TASKDATA.state], 0               ; Run the thread
1930
        mov     [esi + TASKDATA.state], TSTATE_RUNNING  ; Run the thread
1918
 
1931
 
1919
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: Unblocked socket!\n"
1932
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: Unblocked socket!\n"
1920
        pop     esi ecx ebx
1933
        pop     esi ecx ebx
1921
        ret
1934
        ret
1922
 
1935
 
1923
 
1936
 
1924
;-----------------------------------------------------------------;
1937
;-----------------------------------------------------------------;
1925
;                                                                 ;
1938
;                                                                 ;
1926
; socket_alloc: Allocate memory for socket and put new socket     ;
1939
; socket_alloc: Allocate memory for socket and put new socket     ;
1927
; into the list. Newly created socket is initialized with calling ;
1940
; into the list. Newly created socket is initialized with calling ;
1928
; PID and given a socket number.                                  ;
1941
; PID and given a socket number.                                  ;
1929
;                                                                 ;
1942
;                                                                 ;
1930
;  IN:  /                                                         ;
1943
;  IN:  /                                                         ;
1931
;                                                                 ;
1944
;                                                                 ;
1932
; OUT:  eax = socket ptr on success                               ;
1945
; OUT:  eax = socket ptr on success                               ;
1933
;       eax = 0 on error                                          ;
1946
;       eax = 0 on error                                          ;
1934
;       edi = socket number on success                            ;
1947
;       edi = socket number on success                            ;
1935
;                                                                 ;
1948
;                                                                 ;
1936
;-----------------------------------------------------------------;
1949
;-----------------------------------------------------------------;
1937
align 4
1950
align 4
1938
socket_alloc:
1951
socket_alloc:
1939
 
1952
 
1940
        push    ebx
1953
        push    ebx
1941
 
1954
 
1942
        stdcall kernel_alloc, SOCKET_STRUCT_SIZE
1955
        stdcall kernel_alloc, SOCKET_STRUCT_SIZE
1943
        or      eax, eax
1956
        or      eax, eax
1944
        jz      .nomem
1957
        jz      .nomem
1945
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: ptr=%x\n", eax
1958
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: ptr=%x\n", eax
1946
 
1959
 
1947
; zero-initialize allocated memory
1960
; zero-initialize allocated memory
1948
        push    eax
1961
        push    eax
1949
        mov     edi, eax
1962
        mov     edi, eax
1950
        mov     ecx, SOCKET_STRUCT_SIZE / 4
1963
        mov     ecx, SOCKET_STRUCT_SIZE / 4
1951
        xor     eax, eax
1964
        xor     eax, eax
1952
        rep stosd
1965
        rep stosd
1953
        pop     eax
1966
        pop     eax
1954
 
1967
 
1955
; set send-and receive procedures to return -1
1968
; set send-and receive procedures to return -1
1956
        mov     [eax + SOCKET.snd_proc], .not_yet
1969
        mov     [eax + SOCKET.snd_proc], .not_yet
1957
        mov     [eax + SOCKET.rcv_proc], .not_yet
1970
        mov     [eax + SOCKET.rcv_proc], .not_yet
1958
 
1971
 
1959
        pusha
1972
        pusha
1960
        mov     ecx, socket_mutex
1973
        mov     ecx, socket_mutex
1961
        call    mutex_lock
1974
        call    mutex_lock
1962
        popa
1975
        popa
1963
 
1976
 
1964
; find first free socket number and use it
1977
; find first free socket number and use it
1965
        mov     edi, [last_socket_num]
1978
        mov     edi, [last_socket_num]
1966
  .next_socket_number:
1979
  .next_socket_number:
1967
        inc     edi
1980
        inc     edi
1968
        jz      .next_socket_number     ; avoid socket nr 0
1981
        jz      .next_socket_number     ; avoid socket nr 0
1969
        cmp     edi, -1
1982
        cmp     edi, -1
1970
        je      .next_socket_number     ; avoid socket nr -1
1983
        je      .next_socket_number     ; avoid socket nr -1
1971
        mov     ebx, net_sockets
1984
        mov     ebx, net_sockets
1972
  .next_socket:
1985
  .next_socket:
1973
        mov     ebx, [ebx + SOCKET.NextPtr]
1986
        mov     ebx, [ebx + SOCKET.NextPtr]
1974
        test    ebx, ebx
1987
        test    ebx, ebx
1975
        jz      .last_socket
1988
        jz      .last_socket
1976
 
1989
 
1977
        cmp     [ebx + SOCKET.Number], edi
1990
        cmp     [ebx + SOCKET.Number], edi
1978
        jne     .next_socket
1991
        jne     .next_socket
1979
        jmp     .next_socket_number
1992
        jmp     .next_socket_number
1980
 
1993
 
1981
  .last_socket:
1994
  .last_socket:
1982
        mov     [last_socket_num], edi
1995
        mov     [last_socket_num], edi
1983
        mov     [eax + SOCKET.Number], edi
1996
        mov     [eax + SOCKET.Number], edi
1984
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: number=%u\n", edi
1997
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: number=%u\n", edi
1985
 
1998
 
1986
; Fill in PID
1999
; Fill in PID
1987
        mov     ebx, [TASK_BASE]
2000
        mov     ebx, [TASK_BASE]
1988
        mov     ebx, [ebx + TASKDATA.pid]
2001
        mov     ebx, [ebx + TASKDATA.pid]
1989
        mov     [eax + SOCKET.PID], ebx
2002
        mov     [eax + SOCKET.PID], ebx
1990
        mov     [eax + SOCKET.TID], ebx         ; currently TID = PID in kolibrios :(
2003
        mov     [eax + SOCKET.TID], ebx         ; currently TID = PID in kolibrios :(
1991
 
2004
 
1992
; init mutex
2005
; init mutex
1993
        pusha
2006
        pusha
1994
        lea     ecx, [eax + SOCKET.mutex]
2007
        lea     ecx, [eax + SOCKET.mutex]
1995
        call    mutex_init
2008
        call    mutex_init
1996
        popa
2009
        popa
1997
 
2010
 
1998
; add socket to the list by re-arranging some pointers
2011
; add socket to the list by re-arranging some pointers
1999
        mov     ebx, [net_sockets + SOCKET.NextPtr]
2012
        mov     ebx, [net_sockets + SOCKET.NextPtr]
2000
 
2013
 
2001
        mov     [eax + SOCKET.PrevPtr], net_sockets
2014
        mov     [eax + SOCKET.PrevPtr], net_sockets
2002
        mov     [eax + SOCKET.NextPtr], ebx
2015
        mov     [eax + SOCKET.NextPtr], ebx
2003
 
2016
 
2004
        test    ebx, ebx
2017
        test    ebx, ebx
2005
        jz      @f
2018
        jz      @f
2006
 
2019
 
2007
        pusha
2020
        pusha
2008
        lea     ecx, [ebx + SOCKET.mutex]
2021
        lea     ecx, [ebx + SOCKET.mutex]
2009
        call    mutex_lock
2022
        call    mutex_lock
2010
        popa
2023
        popa
2011
 
2024
 
2012
        mov     [ebx + SOCKET.PrevPtr], eax
2025
        mov     [ebx + SOCKET.PrevPtr], eax
2013
 
2026
 
2014
        pusha
2027
        pusha
2015
        lea     ecx, [ebx + SOCKET.mutex]
2028
        lea     ecx, [ebx + SOCKET.mutex]
2016
        call    mutex_unlock
2029
        call    mutex_unlock
2017
        popa
2030
        popa
2018
       @@:
2031
       @@:
2019
 
2032
 
2020
        mov     [net_sockets + SOCKET.NextPtr], eax
2033
        mov     [net_sockets + SOCKET.NextPtr], eax
2021
 
2034
 
2022
        pusha
2035
        pusha
2023
        mov     ecx, socket_mutex
2036
        mov     ecx, socket_mutex
2024
        call    mutex_unlock
2037
        call    mutex_unlock
2025
        popa
2038
        popa
2026
        pop     ebx
2039
        pop     ebx
2027
 
2040
 
2028
        ret
2041
        ret
2029
 
2042
 
2030
  .nomem:
2043
  .nomem:
2031
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_alloc: Out of memory!\n"
2044
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_alloc: Out of memory!\n"
2032
        pop     ebx
2045
        pop     ebx
2033
        ret
2046
        ret
2034
 
2047
 
2035
  .not_yet:
2048
  .not_yet:
2036
        mov     dword[esp+20], ENOTCONN
2049
        mov     dword[esp+20], ENOTCONN
2037
        mov     dword[esp+32], -1
2050
        mov     dword[esp+32], -1
2038
        ret
2051
        ret
2039
 
2052
 
2040
 
2053
 
2041
;-----------------------------------------------------------------;
2054
;-----------------------------------------------------------------;
2042
;                                                                 ;
2055
;                                                                 ;
2043
; socket_free: Free socket data memory and remove socket from     ;
2056
; socket_free: Free socket data memory and remove socket from     ;
2044
; the list. Caller should lock and unlock socket_mutex.           ;
2057
; the list. Caller should lock and unlock socket_mutex.           ;
2045
;                                                                 ;
2058
;                                                                 ;
2046
;  IN:  eax = socket ptr                                          ;
2059
;  IN:  eax = socket ptr                                          ;
2047
;                                                                 ;
2060
;                                                                 ;
2048
; OUT:  /                                                         ;
2061
; OUT:  /                                                         ;
2049
;                                                                 ;
2062
;                                                                 ;
2050
;-----------------------------------------------------------------;
2063
;-----------------------------------------------------------------;
2051
align 4
2064
align 4
2052
socket_free:
2065
socket_free:
2053
 
2066
 
2054
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: %x\n", eax
2067
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: %x\n", eax
2055
 
2068
 
2056
        call    socket_check
2069
        call    socket_check
2057
        jz      .error
2070
        jz      .error
2058
 
2071
 
2059
        push    ebx
2072
        push    ebx
2060
 
2073
 
2061
        pusha
2074
        pusha
2062
        lea     ecx, [eax + SOCKET.mutex]
2075
        lea     ecx, [eax + SOCKET.mutex]
2063
        call    mutex_lock
2076
        call    mutex_lock
2064
        popa
2077
        popa
2065
 
2078
 
2066
        cmp     [eax + SOCKET.Type], SOCK_STREAM
2079
        cmp     [eax + SOCKET.Type], SOCK_STREAM
2067
        jne     .no_stream
2080
        jne     .no_stream
2068
 
2081
 
2069
        mov     ebx, eax
2082
        mov     ebx, eax
2070
        cmp     [eax + STREAM_SOCKET.rcv.start_ptr], 0
2083
        cmp     [eax + STREAM_SOCKET.rcv.start_ptr], 0
2071
        je      @f
2084
        je      @f
2072
        stdcall free_kernel_space, [eax + STREAM_SOCKET.rcv.start_ptr]
2085
        stdcall free_kernel_space, [eax + STREAM_SOCKET.rcv.start_ptr]
2073
  @@:
2086
  @@:
2074
        cmp     [ebx + STREAM_SOCKET.snd.start_ptr], 0
2087
        cmp     [ebx + STREAM_SOCKET.snd.start_ptr], 0
2075
        je      @f
2088
        je      @f
2076
        stdcall free_kernel_space, [ebx + STREAM_SOCKET.snd.start_ptr]
2089
        stdcall free_kernel_space, [ebx + STREAM_SOCKET.snd.start_ptr]
2077
  @@:
2090
  @@:
2078
        mov     eax, ebx
2091
        mov     eax, ebx
2079
  .no_stream:
2092
  .no_stream:
2080
 
2093
 
2081
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: freeing socket %x\n", eax
2094
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: freeing socket %x\n", eax
2082
        push    eax                             ; this will be passed to kernel_free
2095
        push    eax                             ; this will be passed to kernel_free
2083
        mov     ebx, [eax + SOCKET.NextPtr]
2096
        mov     ebx, [eax + SOCKET.NextPtr]
2084
        mov     eax, [eax + SOCKET.PrevPtr]
2097
        mov     eax, [eax + SOCKET.PrevPtr]
2085
 
2098
 
2086
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
2099
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
2087
 
2100
 
2088
        test    eax, eax
2101
        test    eax, eax
2089
        jz      @f
2102
        jz      @f
2090
        mov     [eax + SOCKET.NextPtr], ebx
2103
        mov     [eax + SOCKET.NextPtr], ebx
2091
       @@:
2104
       @@:
2092
 
2105
 
2093
        test    ebx, ebx
2106
        test    ebx, ebx
2094
        jz      @f
2107
        jz      @f
2095
        mov     [ebx + SOCKET.PrevPtr], eax
2108
        mov     [ebx + SOCKET.PrevPtr], eax
2096
       @@:
2109
       @@:
2097
 
2110
 
2098
        call    kernel_free
2111
        call    kernel_free
2099
        pop     ebx
2112
        pop     ebx
2100
 
2113
 
2101
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: success!\n"
2114
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: success!\n"
2102
 
2115
 
2103
  .error:
2116
  .error:
2104
        ret
2117
        ret
2105
 
2118
 
2106
  .error1:
2119
  .error1:
2107
        pop     ebx
2120
        pop     ebx
2108
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_free: error!\n"
2121
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_free: error!\n"
2109
        DEBUGF  DEBUG_NETWORK_ERROR, "socket ptr=0x%x caller=0x%x\n", eax, [esp]
2122
        DEBUGF  DEBUG_NETWORK_ERROR, "socket ptr=0x%x caller=0x%x\n", eax, [esp]
2110
        ret
2123
        ret
2111
 
2124
 
2112
;-----------------------------------------------------------------;
2125
;-----------------------------------------------------------------;
2113
;                                                                 ;
2126
;                                                                 ;
2114
; socket_fork: Create a child socket.                             ;
2127
; socket_fork: Create a child socket.                             ;
2115
;                                                                 ;
2128
;                                                                 ;
2116
;  IN:  ebx = socket number                                       ;
2129
;  IN:  ebx = socket number                                       ;
2117
;                                                                 ;
2130
;                                                                 ;
2118
; OUT:  eax = child socket number on success                      ;
2131
; OUT:  eax = child socket number on success                      ;
2119
;       eax = 0 on error                                          ;
2132
;       eax = 0 on error                                          ;
2120
;                                                                 ;
2133
;                                                                 ;
2121
;-----------------------------------------------------------------;
2134
;-----------------------------------------------------------------;
2122
align 4
2135
align 4
2123
socket_fork:
2136
socket_fork:
2124
 
2137
 
2125
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_fork: %x\n", ebx
2138
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_fork: %x\n", ebx
2126
 
2139
 
2127
; Exit if backlog queue is full
2140
; Exit if backlog queue is full
2128
        mov     eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
2141
        mov     eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
2129
        cmp     ax, [ebx + SOCKET.backlog]
2142
        cmp     ax, [ebx + SOCKET.backlog]
2130
        jae     .fail
2143
        jae     .fail
2131
 
2144
 
2132
; Allocate new socket
2145
; Allocate new socket
2133
        push    ebx
2146
        push    ebx
2134
        call    socket_alloc
2147
        call    socket_alloc
2135
        pop     ebx
2148
        pop     ebx
2136
        test    eax, eax
2149
        test    eax, eax
2137
        jz      .fail
2150
        jz      .fail
2138
 
2151
 
2139
        push    eax
2152
        push    eax
2140
        mov     esi, esp
2153
        mov     esi, esp
2141
        add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
2154
        add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
2142
        pop     eax
2155
        pop     eax
2143
 
2156
 
2144
; Copy structure from current socket to new
2157
; Copy structure from current socket to new
2145
; We start at PID to preserve the socket num, 2 pointers and mutex
2158
; We start at PID to preserve the socket num, 2 pointers and mutex
2146
; TID will be filled in later
2159
; TID will be filled in later
2147
        lea     esi, [ebx + SOCKET.PID]
2160
        lea     esi, [ebx + SOCKET.PID]
2148
        lea     edi, [eax + SOCKET.PID]
2161
        lea     edi, [eax + SOCKET.PID]
2149
        mov     ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
2162
        mov     ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
2150
        rep movsd
2163
        rep movsd
2151
 
2164
 
2152
        and     [eax + SOCKET.options], not SO_ACCEPTCON
2165
        and     [eax + SOCKET.options], not SO_ACCEPTCON
2153
 
2166
 
2154
; Notify owner of parent socket
2167
; Notify owner of parent socket
2155
        push    eax
2168
        push    eax
2156
        mov     eax, ebx
2169
        mov     eax, ebx
2157
        call    socket_notify
2170
        call    socket_notify
2158
        pop     eax
2171
        pop     eax
2159
 
2172
 
2160
        ret
2173
        ret
2161
 
2174
 
2162
  .fail2:
2175
  .fail2:
2163
        add     esp, 4+4+4
2176
        add     esp, 4+4+4
2164
  .fail:
2177
  .fail:
2165
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_fork: failed\n"
2178
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_fork: failed\n"
2166
        xor     eax, eax
2179
        xor     eax, eax
2167
        ret
2180
        ret
2168
 
2181
 
2169
 
2182
 
2170
;-----------------------------------------------------------------;
2183
;-----------------------------------------------------------------;
2171
;                                                                 ;
2184
;                                                                 ;
2172
; socket_num_to_ptr: Get socket structure address by its number.  ;
2185
; socket_num_to_ptr: Get socket structure address by its number.  ;
2173
;                                                                 ;
2186
;                                                                 ;
2174
;  IN:  ecx = socket number                                       ;
2187
;  IN:  ecx = socket number                                       ;
2175
;                                                                 ;
2188
;                                                                 ;
2176
; OUT:  eax = socket ptr                                          ;
2189
; OUT:  eax = socket ptr                                          ;
2177
;       eax = 0 on error                                          ;
2190
;       eax = 0 on error                                          ;
2178
;                                                                 ;
2191
;                                                                 ;
2179
;-----------------------------------------------------------------;
2192
;-----------------------------------------------------------------;
2180
align 4
2193
align 4
2181
socket_num_to_ptr:
2194
socket_num_to_ptr:
2182
 
2195
 
2183
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_num_to_ptr: num=%u ", ecx
2196
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_num_to_ptr: num=%u ", ecx
2184
 
2197
 
2185
        pusha
2198
        pusha
2186
        mov     ecx, socket_mutex
2199
        mov     ecx, socket_mutex
2187
        call    mutex_lock
2200
        call    mutex_lock
2188
        popa
2201
        popa
2189
 
2202
 
2190
        mov     eax, net_sockets
2203
        mov     eax, net_sockets
2191
  .next_socket:
2204
  .next_socket:
2192
        mov     eax, [eax + SOCKET.NextPtr]
2205
        mov     eax, [eax + SOCKET.NextPtr]
2193
        test    eax, eax
2206
        test    eax, eax
2194
        jz      .error
2207
        jz      .error
2195
        cmp     [eax + SOCKET.Number], ecx
2208
        cmp     [eax + SOCKET.Number], ecx
2196
        jne     .next_socket
2209
        jne     .next_socket
2197
 
2210
 
2198
        pusha
2211
        pusha
2199
        mov     ecx, socket_mutex
2212
        mov     ecx, socket_mutex
2200
        call    mutex_unlock
2213
        call    mutex_unlock
2201
        popa
2214
        popa
2202
 
2215
 
2203
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ptr=%x\n", eax
2216
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ptr=%x\n", eax
2204
        ret
2217
        ret
2205
 
2218
 
2206
  .error:
2219
  .error:
2207
        pusha
2220
        pusha
2208
        mov     ecx, socket_mutex
2221
        mov     ecx, socket_mutex
2209
        call    mutex_unlock
2222
        call    mutex_unlock
2210
        popa
2223
        popa
2211
 
2224
 
2212
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: socket %u not found!\n", eax
2225
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: socket %u not found!\n", eax
2213
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: caller = 0x%x\n", [esp]
2226
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: caller = 0x%x\n", [esp]
2214
        ret
2227
        ret
2215
 
2228
 
2216
 
2229
 
2217
;-----------------------------------------------------------------;
2230
;-----------------------------------------------------------------;
2218
;                                                                 ;
2231
;                                                                 ;
2219
; socket_ptr_to_num: Get socket number by its address.            ;
2232
; socket_ptr_to_num: Get socket number by its address.            ;
2220
;                                                                 ;
2233
;                                                                 ;
2221
;  IN:  eax = socket ptr                                          ;
2234
;  IN:  eax = socket ptr                                          ;
2222
;                                                                 ;
2235
;                                                                 ;
2223
; OUT:  eax = socket number                                       ;
2236
; OUT:  eax = socket number                                       ;
2224
;       eax = 0 on error                                          ;
2237
;       eax = 0 on error                                          ;
2225
;       ZF = set on error                                         ;
2238
;       ZF = set on error                                         ;
2226
;                                                                 ;
2239
;                                                                 ;
2227
;-----------------------------------------------------------------;
2240
;-----------------------------------------------------------------;
2228
align 4
2241
align 4
2229
socket_ptr_to_num:
2242
socket_ptr_to_num:
2230
 
2243
 
2231
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ptr_to_num: ptr=%x ", eax
2244
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ptr_to_num: ptr=%x ", eax
2232
 
2245
 
2233
        call    socket_check
2246
        call    socket_check
2234
        jz      .error
2247
        jz      .error
2235
 
2248
 
2236
        mov     eax, [eax + SOCKET.Number]
2249
        mov     eax, [eax + SOCKET.Number]
2237
 
2250
 
2238
        DEBUGF  DEBUG_NETWORK_VERBOSE, "num=%u\n", eax
2251
        DEBUGF  DEBUG_NETWORK_VERBOSE, "num=%u\n", eax
2239
        ret
2252
        ret
2240
 
2253
 
2241
  .error:
2254
  .error:
2242
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ptr_to_num: not found\n", eax
2255
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ptr_to_num: not found\n", eax
2243
        ret
2256
        ret
2244
 
2257
 
2245
 
2258
 
2246
;-----------------------------------------------------------------;
2259
;-----------------------------------------------------------------;
2247
;                                                                 ;
2260
;                                                                 ;
2248
; socket_check: Checks if the given ptr is really a socket ptr.   ;
2261
; socket_check: Checks if the given ptr is really a socket ptr.   ;
2249
;                                                                 ;
2262
;                                                                 ;
2250
;  IN:  eax = socket ptr                                          ;
2263
;  IN:  eax = socket ptr                                          ;
2251
;                                                                 ;
2264
;                                                                 ;
2252
; OUT:  eax = 0 on error                                          ;
2265
; OUT:  eax = 0 on error                                          ;
2253
;       ZF = set on error                                         ;
2266
;       ZF = set on error                                         ;
2254
;                                                                 ;
2267
;                                                                 ;
2255
;-----------------------------------------------------------------;
2268
;-----------------------------------------------------------------;
2256
align 4
2269
align 4
2257
socket_check:
2270
socket_check:
2258
 
2271
 
2259
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check: %x\n", eax
2272
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check: %x\n", eax
2260
 
2273
 
2261
        test    eax, eax
2274
        test    eax, eax
2262
        jz      .error
2275
        jz      .error
2263
        push    ebx
2276
        push    ebx
2264
        mov     ebx, net_sockets
2277
        mov     ebx, net_sockets
2265
 
2278
 
2266
  .next_socket:
2279
  .next_socket:
2267
        mov     ebx, [ebx + SOCKET.NextPtr]
2280
        mov     ebx, [ebx + SOCKET.NextPtr]
2268
        or      ebx, ebx
2281
        or      ebx, ebx
2269
        jz      .done
2282
        jz      .done
2270
        cmp     ebx, eax
2283
        cmp     ebx, eax
2271
        jnz     .next_socket
2284
        jnz     .next_socket
2272
 
2285
 
2273
  .done:
2286
  .done:
2274
        mov     eax, ebx
2287
        mov     eax, ebx
2275
        test    eax, eax
2288
        test    eax, eax
2276
        pop     ebx
2289
        pop     ebx
2277
        ret
2290
        ret
2278
 
2291
 
2279
  .error:
2292
  .error:
2280
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_check: called with argument 0\n"
2293
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_check: called with argument 0\n"
2281
        DEBUGF  DEBUG_NETWORK_ERROR, "stack: 0x%x, 0x%x, 0x%x\n", [esp], [esp+4], [esp+8]
2294
        DEBUGF  DEBUG_NETWORK_ERROR, "stack: 0x%x, 0x%x, 0x%x\n", [esp], [esp+4], [esp+8]
2282
        ret
2295
        ret
2283
 
2296
 
2284
 
2297
 
2285
 
2298
 
2286
;-----------------------------------------------------------------;
2299
;-----------------------------------------------------------------;
2287
;                                                                 ;
2300
;                                                                 ;
2288
; socket_check_owner: Check if the caller app owns the socket.    ;
2301
; socket_check_owner: Check if the caller app owns the socket.    ;
2289
;                                                                 ;
2302
;                                                                 ;
2290
;  IN:  eax = socket ptr                                          ;
2303
;  IN:  eax = socket ptr                                          ;
2291
;                                                                 ;
2304
;                                                                 ;
2292
; OUT:  ZF = true/false                                           ;
2305
; OUT:  ZF = true/false                                           ;
2293
;                                                                 ;
2306
;                                                                 ;
2294
;-----------------------------------------------------------------;
2307
;-----------------------------------------------------------------;
2295
align 4
2308
align 4
2296
socket_check_owner:
2309
socket_check_owner:
2297
 
2310
 
2298
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check_owner: %x\n", eax
2311
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check_owner: %x\n", eax
2299
 
2312
 
2300
        push    ebx
2313
        push    ebx
2301
        mov     ebx, [TASK_BASE]
2314
        mov     ebx, [TASK_BASE]
2302
        mov     ebx, [ebx + TASKDATA.pid]
2315
        mov     ebx, [ebx + TASKDATA.pid]
2303
        cmp     [eax + SOCKET.PID], ebx
2316
        cmp     [eax + SOCKET.PID], ebx
2304
        pop     ebx
2317
        pop     ebx
2305
 
2318
 
2306
        ret
2319
        ret
2307
 
2320
 
2308
 
2321
 
2309
 
2322
 
2310
 
2323
 
2311
;-----------------------------------------------------------------;
2324
;-----------------------------------------------------------------;
2312
;                                                                 ;
2325
;                                                                 ;
2313
; socket_process_end: Kernel calls this function when a certain   ;
2326
; socket_process_end: Kernel calls this function when a certain   ;
2314
; process ends. This function will check if the process had any   ;
2327
; process ends. This function will check if the process had any   ;
2315
; open sockets and update them accordingly (clean up).            ;
2328
; open sockets and update them accordingly (clean up).            ;
2316
;                                                                 ;
2329
;                                                                 ;
2317
;  IN:  edx = pid                                                 ;
2330
;  IN:  edx = pid                                                 ;
2318
;                                                                 ;
2331
;                                                                 ;
2319
; OUT:  /                                                         ;
2332
; OUT:  /                                                         ;
2320
;                                                                 ;
2333
;                                                                 ;
2321
;-----------------------------------------------------------------;
2334
;-----------------------------------------------------------------;
2322
align 4
2335
align 4
2323
socket_process_end:
2336
socket_process_end:
2324
 
2337
 
2325
        ret     ; FIXME
2338
        ret     ; FIXME
2326
 
2339
 
2327
        cmp     [net_sockets + SOCKET.NextPtr], 0       ; Are there any active sockets at all?
2340
        cmp     [net_sockets + SOCKET.NextPtr], 0       ; Are there any active sockets at all?
2328
        je      .quickret                               ; nope, exit immediately
2341
        je      .quickret                               ; nope, exit immediately
2329
 
2342
 
2330
; TODO: run the following code in another thread, to avoid deadlock
2343
; TODO: run the following code in another thread, to avoid deadlock
2331
 
2344
 
2332
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: %x\n", edx
2345
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: %x\n", edx
2333
 
2346
 
2334
        pusha
2347
        pusha
2335
        mov     ecx, socket_mutex
2348
        mov     ecx, socket_mutex
2336
        call    mutex_lock
2349
        call    mutex_lock
2337
        popa
2350
        popa
2338
 
2351
 
2339
        push    ebx
2352
        push    ebx
2340
        mov     ebx, net_sockets
2353
        mov     ebx, net_sockets
2341
 
2354
 
2342
  .next_socket:
2355
  .next_socket:
2343
        mov     ebx, [ebx + SOCKET.NextPtr]
2356
        mov     ebx, [ebx + SOCKET.NextPtr]
2344
  .next_socket_test:
2357
  .next_socket_test:
2345
        test    ebx, ebx
2358
        test    ebx, ebx
2346
        jz      .done
2359
        jz      .done
2347
 
2360
 
2348
        cmp     [ebx + SOCKET.PID], edx
2361
        cmp     [ebx + SOCKET.PID], edx
2349
        jne     .next_socket
2362
        jne     .next_socket
2350
 
2363
 
2351
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: killing socket %x\n", ebx
2364
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: killing socket %x\n", ebx
2352
 
2365
 
2353
        mov     [ebx + SOCKET.PID], 0
2366
        mov     [ebx + SOCKET.PID], 0
2354
        mov     eax, ebx
2367
        mov     eax, ebx
2355
        mov     ebx, [ebx + SOCKET.NextPtr]
2368
        mov     ebx, [ebx + SOCKET.NextPtr]
2356
 
2369
 
2357
        pusha
2370
        pusha
2358
        cmp     [eax + SOCKET.Domain], AF_INET4
2371
        cmp     [eax + SOCKET.Domain], AF_INET4
2359
        jne     .free
2372
        jne     .free
2360
 
2373
 
2361
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
2374
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
2362
        jne     .free
2375
        jne     .free
2363
 
2376
 
2364
        call    tcp_disconnect
2377
        call    tcp_disconnect
2365
        jmp     .closed
2378
        jmp     .closed
2366
 
2379
 
2367
  .free:
2380
  .free:
2368
        call    socket_free
2381
        call    socket_free
2369
 
2382
 
2370
  .closed:
2383
  .closed:
2371
        popa
2384
        popa
2372
        jmp     .next_socket_test
2385
        jmp     .next_socket_test
2373
 
2386
 
2374
  .done:
2387
  .done:
2375
        pop     ebx
2388
        pop     ebx
2376
 
2389
 
2377
        pusha
2390
        pusha
2378
        mov     ecx, socket_mutex
2391
        mov     ecx, socket_mutex
2379
        call    mutex_unlock
2392
        call    mutex_unlock
2380
        popa
2393
        popa
2381
 
2394
 
2382
  .quickret:
2395
  .quickret:
2383
        ret
2396
        ret
2384
 
2397
 
2385
 
2398
 
2386
 
2399
 
2387
 
2400
 
2388
;-----------------------------------------------------------------;
2401
;-----------------------------------------------------------------;
2389
;                                                                 ;
2402
;                                                                 ;
2390
; socket_is_connecting: Update socket state.                      ;
2403
; socket_is_connecting: Update socket state.                      ;
2391
;                                                                 ;
2404
;                                                                 ;
2392
;  IN:  eax = socket ptr                                          ;
2405
;  IN:  eax = socket ptr                                          ;
2393
;                                                                 ;
2406
;                                                                 ;
2394
;  OUT: /                                                         ;
2407
;  OUT: /                                                         ;
2395
;                                                                 ;
2408
;                                                                 ;
2396
;-----------------------------------------------------------------;
2409
;-----------------------------------------------------------------;
2397
align 4
2410
align 4
2398
socket_is_connecting:
2411
socket_is_connecting:
2399
 
2412
 
2400
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_connecting: %x\n", eax
2413
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_connecting: %x\n", eax
2401
 
2414
 
2402
        and     [eax + SOCKET.state], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING)
2415
        and     [eax + SOCKET.state], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING)
2403
        or      [eax + SOCKET.state], SS_ISCONNECTING
2416
        or      [eax + SOCKET.state], SS_ISCONNECTING
2404
        ret
2417
        ret
2405
 
2418
 
2406
 
2419
 
2407
 
2420
 
2408
;-----------------------------------------------------------------;
2421
;-----------------------------------------------------------------;
2409
;                                                                 ;
2422
;                                                                 ;
2410
; socket_is_connected: Update socket state.                       ;
2423
; socket_is_connected: Update socket state.                       ;
2411
;                                                                 ;
2424
;                                                                 ;
2412
;  IN:  eax = socket ptr                                          ;
2425
;  IN:  eax = socket ptr                                          ;
2413
;                                                                 ;
2426
;                                                                 ;
2414
;  OUT: /                                                         ;
2427
;  OUT: /                                                         ;
2415
;                                                                 ;
2428
;                                                                 ;
2416
;-----------------------------------------------------------------;
2429
;-----------------------------------------------------------------;
2417
align 4
2430
align 4
2418
socket_is_connected:
2431
socket_is_connected:
2419
 
2432
 
2420
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_connected: %x\n", eax
2433
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_connected: %x\n", eax
2421
 
2434
 
2422
        and     [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
2435
        and     [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
2423
        or      [eax + SOCKET.state], SS_ISCONNECTED
2436
        or      [eax + SOCKET.state], SS_ISCONNECTED
2424
        jmp     socket_notify
2437
        jmp     socket_notify
2425
 
2438
 
2426
 
2439
 
2427
 
2440
 
2428
 
2441
 
2429
;-----------------------------------------------------------------;
2442
;-----------------------------------------------------------------;
2430
;                                                                 ;
2443
;                                                                 ;
2431
; socket_is_disconnecting: Update socket state.                   ;
2444
; socket_is_disconnecting: Update socket state.                   ;
2432
;                                                                 ;
2445
;                                                                 ;
2433
;  IN:  eax = socket ptr                                          ;
2446
;  IN:  eax = socket ptr                                          ;
2434
;                                                                 ;
2447
;                                                                 ;
2435
;  OUT: /                                                         ;
2448
;  OUT: /                                                         ;
2436
;                                                                 ;
2449
;                                                                 ;
2437
;-----------------------------------------------------------------;
2450
;-----------------------------------------------------------------;
2438
align 4
2451
align 4
2439
socket_is_disconnecting:
2452
socket_is_disconnecting:
2440
 
2453
 
2441
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnecting: %x\n", eax
2454
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnecting: %x\n", eax
2442
 
2455
 
2443
        and     [eax + SOCKET.state], not (SS_ISCONNECTING)
2456
        and     [eax + SOCKET.state], not (SS_ISCONNECTING)
2444
        or      [eax + SOCKET.state], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE
2457
        or      [eax + SOCKET.state], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE
2445
        jmp     socket_notify
2458
        jmp     socket_notify
2446
 
2459
 
2447
 
2460
 
2448
 
2461
 
2449
;-----------------------------------------------------------------;
2462
;-----------------------------------------------------------------;
2450
;                                                                 ;
2463
;                                                                 ;
2451
; socket_is_disconnected: Update socket state.                    ;
2464
; socket_is_disconnected: Update socket state.                    ;
2452
;                                                                 ;
2465
;                                                                 ;
2453
;  IN:  eax = socket ptr                                          ;
2466
;  IN:  eax = socket ptr                                          ;
2454
;                                                                 ;
2467
;                                                                 ;
2455
;  OUT: /                                                         ;
2468
;  OUT: /                                                         ;
2456
;                                                                 ;
2469
;                                                                 ;
2457
;-----------------------------------------------------------------;
2470
;-----------------------------------------------------------------;
2458
align 4
2471
align 4
2459
socket_is_disconnected:
2472
socket_is_disconnected:
2460
 
2473
 
2461
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnected: %x\n", eax
2474
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnected: %x\n", eax
2462
 
2475
 
2463
        and     [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING)
2476
        and     [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING)
2464
        or      [eax + SOCKET.state], SS_CANTRCVMORE + SS_CANTSENDMORE
2477
        or      [eax + SOCKET.state], SS_CANTRCVMORE + SS_CANTSENDMORE
2465
        jmp     socket_notify
2478
        jmp     socket_notify
2466
 
2479
 
2467
 
2480
 
2468
 
2481
 
2469
;-----------------------------------------------------------------;
2482
;-----------------------------------------------------------------;
2470
;                                                                 ;
2483
;                                                                 ;
2471
; socket_cant_recv_more: Update socket state.                     ;
2484
; socket_cant_recv_more: Update socket state.                     ;
2472
;                                                                 ;
2485
;                                                                 ;
2473
;  IN:  eax = socket ptr                                          ;
2486
;  IN:  eax = socket ptr                                          ;
2474
;                                                                 ;
2487
;                                                                 ;
2475
;  OUT: /                                                         ;
2488
;  OUT: /                                                         ;
2476
;                                                                 ;
2489
;                                                                 ;
2477
;-----------------------------------------------------------------;
2490
;-----------------------------------------------------------------;
2478
align 4
2491
align 4
2479
socket_cant_recv_more:
2492
socket_cant_recv_more:
2480
 
2493
 
2481
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_cant_recv_more: %x\n", eax
2494
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_cant_recv_more: %x\n", eax
2482
 
2495
 
2483
        or      [eax + SOCKET.state], SS_CANTRCVMORE
2496
        or      [eax + SOCKET.state], SS_CANTRCVMORE
2484
        jmp     socket_notify
2497
        jmp     socket_notify
2485
 
2498
 
2486
 
2499
 
2487
 
2500
 
2488
;-----------------------------------------------------------------;
2501
;-----------------------------------------------------------------;
2489
;                                                                 ;
2502
;                                                                 ;
2490
; socket_cant_send_more: Update socket state.                     ;
2503
; socket_cant_send_more: Update socket state.                     ;
2491
;                                                                 ;
2504
;                                                                 ;
2492
;  IN:  eax = socket ptr                                          ;
2505
;  IN:  eax = socket ptr                                          ;
2493
;                                                                 ;
2506
;                                                                 ;
2494
;  OUT: /                                                         ;
2507
;  OUT: /                                                         ;
2495
;                                                                 ;
2508
;                                                                 ;
2496
;-----------------------------------------------------------------;
2509
;-----------------------------------------------------------------;
2497
align 4
2510
align 4
2498
socket_cant_send_more:
2511
socket_cant_send_more:
2499
 
2512
 
2500
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_cant_send_more: %x\n", eax
2513
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_cant_send_more: %x\n", eax
2501
 
2514
 
2502
        or      [eax + SOCKET.state], SS_CANTSENDMORE
2515
        or      [eax + SOCKET.state], SS_CANTSENDMORE
2503
        mov     [eax + SOCKET.snd_proc], .notconn
2516
        mov     [eax + SOCKET.snd_proc], .notconn
2504
        jmp     socket_notify
2517
        jmp     socket_notify
2505
 
2518
 
2506
  .notconn:
2519
  .notconn:
2507
        mov     dword[esp+20], ENOTCONN
2520
        mov     dword[esp+20], ENOTCONN
2508
        mov     dword[esp+32], -1
2521
        mov     dword[esp+32], -1
2509
        ret
2522
        ret