Subversion Repositories Kolibri OS

Rev

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

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