Subversion Repositories Kolibri OS

Rev

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

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