Subversion Repositories Kolibri OS

Rev

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

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