Subversion Repositories Kolibri OS

Rev

Rev 6907 | Rev 6916 | 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: 6908 $
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
 
6908 ashmew2 296
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_open: domain=%u type=%u protocol=%x ", 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
6908 ashmew2 695
 
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
3545 hidnplayr 733
 
3658 hidnplayr 734
        mov     dword[esp+32], 0                                ; The socket exists, so we will succeed in closing it.
3545 hidnplayr 735
 
736
        or      [eax + SOCKET.options], SO_NONBLOCK             ; Mark the socket as non blocking, we dont want it to block any longer!
737
 
738
        test    [eax + SOCKET.state], SS_BLOCKED                ; Is the socket still in blocked state?
739
        jz      @f
6011 hidnplayr 740
        call    socket_notify                                   ; Unblock it.
3545 hidnplayr 741
  @@:
742
 
743
        cmp     [eax + SOCKET.Domain], AF_INET4
744
        jne     .free
745
 
746
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
747
        je      .tcp
748
 
749
  .free:
6011 hidnplayr 750
        call    socket_free
3545 hidnplayr 751
        ret
752
 
753
  .tcp:
6011 hidnplayr 754
        call    tcp_usrclosed
3545 hidnplayr 755
 
4366 hidnplayr 756
        test    eax, eax
757
        jz      @f
6011 hidnplayr 758
        call    tcp_output                                      ; If connection is not closed yet, send the FIN
4366 hidnplayr 759
  @@:
3545 hidnplayr 760
        ret
761
 
762
 
3658 hidnplayr 763
  .invalid:
3673 hidnplayr 764
        mov     dword[esp+20], EINVAL
3658 hidnplayr 765
        mov     dword[esp+32], -1
766
        ret
767
 
768
 
5976 hidnplayr 769
;-----------------------------------------------------------------;
770
;                                                                 ;
6011 hidnplayr 771
; socket_receive: Receive some data from the remote end.          ;
5976 hidnplayr 772
;                                                                 ;
773
;   IN: ecx = socket number                                       ;
774
;       edx = addr to application buffer                          ;
775
;       edx = length of application buffer                        ;
776
;       edi = flags                                               ;
777
;                                                                 ;
778
;  OUT: eax = number of bytes copied                              ;
779
;       eax = -1 on error                                         ;
780
;       eax = 0 when socket has been closed by the remote end     ;
781
;       ebx = errorcode on error                                  ;
782
;                                                                 ;
783
;-----------------------------------------------------------------;
3545 hidnplayr 784
align 4
6011 hidnplayr 785
socket_receive:
3545 hidnplayr 786
 
3556 hidnplayr 787
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: socknum=%u bufaddr=%x buflength=%u flags=%x\n", ecx, edx, esi, edi
3545 hidnplayr 788
 
6011 hidnplayr 789
        call    socket_num_to_ptr
5969 hidnplayr 790
        test    eax, eax
3658 hidnplayr 791
        jz      .invalid
3545 hidnplayr 792
 
3704 hidnplayr 793
  .loop:
794
        push    edi
3565 hidnplayr 795
        call    [eax + SOCKET.rcv_proc]
3704 hidnplayr 796
        pop     edi
3545 hidnplayr 797
 
4219 hidnplayr 798
        test    [eax + SOCKET.state], SS_CANTRCVMORE
5364 hidnplayr 799
        jnz     .last_data
4219 hidnplayr 800
 
3704 hidnplayr 801
        cmp     ebx, EWOULDBLOCK
802
        jne     .return
803
 
804
        test    edi, MSG_DONTWAIT
805
        jnz     .return_err
806
 
5976 hidnplayr 807
        test    [eax + SOCKET.options], SO_NONBLOCK
808
        jnz     .return_err
6908 ashmew2 809
 
6011 hidnplayr 810
        call    socket_block
3704 hidnplayr 811
        jmp     .loop
812
 
813
 
814
  .invalid:
815
        push    EINVAL
816
        pop     ebx
817
  .return_err:
4219 hidnplayr 818
        mov     ecx, -1
3704 hidnplayr 819
  .return:
3673 hidnplayr 820
        mov     [esp+20], ebx
4219 hidnplayr 821
        mov     [esp+32], ecx
3565 hidnplayr 822
        ret
3545 hidnplayr 823
 
5364 hidnplayr 824
  .last_data:
825
        test    ecx, ecx
826
        jz      .return
6011 hidnplayr 827
        call    socket_notify                                   ; Call me again!
5364 hidnplayr 828
        jmp     .return
3565 hidnplayr 829
 
3658 hidnplayr 830
 
3704 hidnplayr 831
 
832
 
3545 hidnplayr 833
align 4
6011 hidnplayr 834
socket_receive_dgram:
3704 hidnplayr 835
 
3556 hidnplayr 836
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: DGRAM\n"
3545 hidnplayr 837
 
5976 hidnplayr 838
        test    edi, MSG_PEEK
839
        jnz     .peek
3545 hidnplayr 840
 
5976 hidnplayr 841
        mov     ebx, esi                                        ; buffer length
842
 
3704 hidnplayr 843
        get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .wouldblock ; sets esi only on success.
3545 hidnplayr 844
        mov     ecx, [esi + socket_queue_entry.data_size]
3556 hidnplayr 845
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: %u bytes data\n", ecx
3545 hidnplayr 846
 
3704 hidnplayr 847
        cmp     ecx, ebx                                        ; If data segment does not fit in applications buffer, abort
3545 hidnplayr 848
        ja      .too_small
849
 
4219 hidnplayr 850
        push    eax ecx
3545 hidnplayr 851
        push    [esi + socket_queue_entry.buf_ptr]              ; save the buffer addr so we can clear it later
852
        mov     esi, [esi + socket_queue_entry.data_ptr]
3556 hidnplayr 853
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: Source buffer=%x real addr=%x\n", [esp], esi
3545 hidnplayr 854
 
3704 hidnplayr 855
; copy the data from kernel buffer to application buffer
856
        mov     edi, edx                                        ; bufferaddr
3545 hidnplayr 857
        shr     ecx, 1
858
        jnc     .nb
859
        movsb
860
  .nb:
861
        shr     ecx, 1
862
        jnc     .nw
863
        movsw
864
  .nw:
865
        test    ecx, ecx
866
        jz      .nd
3711 clevermous 867
        rep movsd
3545 hidnplayr 868
  .nd:
869
 
6011 hidnplayr 870
        call    net_buff_free
4219 hidnplayr 871
        pop     ecx eax                                         ; return number of bytes copied to application
5976 hidnplayr 872
        cmp     [eax + SOCKET_QUEUE_LOCATION + queue.size], 0
873
        je      @f
6011 hidnplayr 874
        call    socket_notify                                   ; Queue another network event
5976 hidnplayr 875
  @@:
876
        xor     ebx, ebx                                        ; errorcode = 0 (no error)
3545 hidnplayr 877
        ret
878
 
879
  .too_small:
4219 hidnplayr 880
        mov     ecx, -1
3704 hidnplayr 881
        push    EMSGSIZE
882
        pop     ebx
3565 hidnplayr 883
        ret
3545 hidnplayr 884
 
3658 hidnplayr 885
  .wouldblock:
3704 hidnplayr 886
        push    EWOULDBLOCK
887
        pop     ebx
3658 hidnplayr 888
        ret
3545 hidnplayr 889
 
5976 hidnplayr 890
  .peek:
891
        xor     ebx, ebx
892
        xor     ecx, ecx
893
        cmp     [eax + SOCKET_QUEUE_LOCATION + queue.size], 0
894
        je      @f
895
        mov     esi, [eax + SOCKET_QUEUE_LOCATION + queue.r_ptr]
896
        mov     ecx, [esi + socket_queue_entry.data_size]
897
  @@:
898
        ret
3658 hidnplayr 899
 
3704 hidnplayr 900
 
3545 hidnplayr 901
align 4
6011 hidnplayr 902
socket_receive_local:
3545 hidnplayr 903
 
904
        ; does this socket have a PID yet?
905
        cmp     [eax + SOCKET.PID], 0
906
        jne     @f
907
 
908
        ; Change PID to that of current process
909
        mov     ebx, [TASK_BASE]
910
        mov     ebx, [ebx + TASKDATA.pid]
911
        mov     [eax + SOCKET.PID], ebx
3704 hidnplayr 912
        mov     [eax + SOCKET.TID], ebx                         ; currently TID = PID in kolibrios :(
3545 hidnplayr 913
      @@:
914
 
6011 hidnplayr 915
        mov     [eax + SOCKET.rcv_proc], socket_receive_stream
3545 hidnplayr 916
 
3704 hidnplayr 917
; ... continue to SOCKET_receive_stream
918
 
3545 hidnplayr 919
align 4
6011 hidnplayr 920
socket_receive_stream:
3545 hidnplayr 921
 
3556 hidnplayr 922
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: STREAM\n"
3545 hidnplayr 923
 
3704 hidnplayr 924
        cmp     [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0
925
        je      .wouldblock
926
 
927
        test    edi, MSG_PEEK
928
        jnz     .peek
929
 
3545 hidnplayr 930
        mov     ecx, esi
931
        mov     edi, edx
932
        xor     edx, edx
933
 
4219 hidnplayr 934
        push    eax
3545 hidnplayr 935
        add     eax, STREAM_SOCKET.rcv
6011 hidnplayr 936
        call    socket_ring_read                                ; copy data from kernel buffer to application buffer
937
        call    socket_ring_free                                ; free read memory
4219 hidnplayr 938
        pop     eax
3545 hidnplayr 939
 
5364 hidnplayr 940
        cmp     [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0
941
        jne     .more_data
3704 hidnplayr 942
        xor     ebx, ebx                                        ; errorcode = 0 (no error)
3545 hidnplayr 943
        ret
944
 
5364 hidnplayr 945
  .more_data:
6011 hidnplayr 946
        call    socket_notify                                   ; Queue another network event
5364 hidnplayr 947
        xor     ebx, ebx                                        ; errorcode = 0 (no error)
948
        ret
949
 
3704 hidnplayr 950
  .wouldblock:
951
        push    EWOULDBLOCK
952
        pop     ebx
4219 hidnplayr 953
        xor     ecx, ecx
3704 hidnplayr 954
        ret
955
 
3545 hidnplayr 956
  .peek:
4219 hidnplayr 957
        mov     ecx, [eax + STREAM_SOCKET.rcv + RING_BUFFER.size]
3704 hidnplayr 958
        xor     ebx, ebx
3545 hidnplayr 959
        ret
960
 
961
 
5976 hidnplayr 962
;-----------------------------------------------------------------;
963
;                                                                 ;
6011 hidnplayr 964
; socket_send: Send some data to the remote end.                  ;
5976 hidnplayr 965
;                                                                 ;
966
;   IN: ecx = socket number                                       ;
967
;       edx = pointer to data                                     ;
968
;       esi = data length                                         ;
969
;       edi = flags                                               ;
970
;                                                                 ;
971
;  OUT: eax = number of bytes sent                                ;
972
;       eax = -1 on error                                         ;
973
;       ebx = errorcode on error                                  ;
974
;                                                                 ;
975
;-----------------------------------------------------------------;
3545 hidnplayr 976
align 4
6011 hidnplayr 977
socket_send:
3545 hidnplayr 978
 
3556 hidnplayr 979
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi
3545 hidnplayr 980
 
6011 hidnplayr 981
        call    socket_num_to_ptr
5969 hidnplayr 982
        test    eax, eax
3658 hidnplayr 983
        jz      .invalid
3545 hidnplayr 984
 
985
        mov     ecx, esi
986
        mov     esi, edx
987
 
988
        jmp     [eax + SOCKET.snd_proc]
989
 
3658 hidnplayr 990
  .invalid:
3673 hidnplayr 991
        mov     dword[esp+20], EINVAL
3658 hidnplayr 992
        mov     dword[esp+32], -1
993
        ret
3545 hidnplayr 994
 
3658 hidnplayr 995
 
3545 hidnplayr 996
align 4
6011 hidnplayr 997
socket_send_udp:
3545 hidnplayr 998
 
3556 hidnplayr 999
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: UDP\n"
3545 hidnplayr 1000
 
1001
        mov     [esp+32], ecx
6011 hidnplayr 1002
        call    udp_output
3545 hidnplayr 1003
        cmp     eax, -1
3658 hidnplayr 1004
        je      .error
3545 hidnplayr 1005
        ret
1006
 
3658 hidnplayr 1007
  .error:
1008
        mov     dword[esp+32], -1
3673 hidnplayr 1009
        mov     dword[esp+20], EMSGSIZE ; FIXME: UDP_output should return error codes!
3658 hidnplayr 1010
        ret
3545 hidnplayr 1011
 
3658 hidnplayr 1012
 
3545 hidnplayr 1013
align 4
6011 hidnplayr 1014
socket_send_tcp:
3545 hidnplayr 1015
 
3556 hidnplayr 1016
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: TCP\n"
3545 hidnplayr 1017
 
1018
        push    eax
1019
        add     eax, STREAM_SOCKET.snd
6011 hidnplayr 1020
        call    socket_ring_write
3545 hidnplayr 1021
        pop     eax
1022
 
1023
        mov     [esp+32], ecx
3658 hidnplayr 1024
        mov     [eax + SOCKET.errorcode], 0
1025
        push    eax
6011 hidnplayr 1026
        call    tcp_output              ; FIXME: this doesnt look pretty, does it?
3658 hidnplayr 1027
        pop     eax
1028
        mov     eax, [eax + SOCKET.errorcode]
3673 hidnplayr 1029
        mov     [esp+20], eax
3545 hidnplayr 1030
        ret
1031
 
1032
 
1033
align 4
6011 hidnplayr 1034
socket_send_ip:
3545 hidnplayr 1035
 
3556 hidnplayr 1036
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: IPv4\n"
3545 hidnplayr 1037
 
1038
        mov     [esp+32], ecx
6011 hidnplayr 1039
        call    ipv4_output_raw
3545 hidnplayr 1040
        cmp     eax, -1
3658 hidnplayr 1041
        je      .error
3545 hidnplayr 1042
        ret
1043
 
3658 hidnplayr 1044
  .error:
5842 hidnplayr 1045
        mov     dword[esp+32], eax
1046
        mov     dword[esp+20], ebx
3658 hidnplayr 1047
        ret
3545 hidnplayr 1048
 
3658 hidnplayr 1049
 
3545 hidnplayr 1050
align 4
6011 hidnplayr 1051
socket_send_icmp:
3545 hidnplayr 1052
 
3556 hidnplayr 1053
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: ICMP\n"
3545 hidnplayr 1054
 
1055
        mov     [esp+32], ecx
6011 hidnplayr 1056
        call    icmp_output_raw
3545 hidnplayr 1057
        cmp     eax, -1
3658 hidnplayr 1058
        je      .error
3545 hidnplayr 1059
        ret
1060
 
3658 hidnplayr 1061
  .error:
5842 hidnplayr 1062
        mov     dword[esp+32], eax
1063
        mov     dword[esp+20], ebx
3658 hidnplayr 1064
        ret
3545 hidnplayr 1065
 
3658 hidnplayr 1066
 
3545 hidnplayr 1067
align 4
6011 hidnplayr 1068
socket_send_pppoe:
3545 hidnplayr 1069
 
3556 hidnplayr 1070
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: PPPoE\n"
3545 hidnplayr 1071
 
1072
        mov     [esp+32], ecx
1073
        mov     ebx, [eax + SOCKET.device]
1074
 
6011 hidnplayr 1075
        call    pppoe_discovery_output  ; FIXME: errorcodes
3545 hidnplayr 1076
        cmp     eax, -1
3658 hidnplayr 1077
        je      .error
3545 hidnplayr 1078
        ret
1079
 
3658 hidnplayr 1080
  .error:
1081
        mov     dword[esp+32], -1
3673 hidnplayr 1082
        mov     dword[esp+20], EMSGSIZE
3658 hidnplayr 1083
        ret
3545 hidnplayr 1084
 
1085
 
3658 hidnplayr 1086
 
3545 hidnplayr 1087
align 4
6011 hidnplayr 1088
socket_send_local:
3545 hidnplayr 1089
 
1090
        ; does this socket have a PID yet?
1091
        cmp     [eax + SOCKET.PID], 0
1092
        jne     @f
1093
 
1094
        ; Change PID to that of current process
1095
        mov     ebx, [TASK_BASE]
1096
        mov     ebx, [ebx + TASKDATA.pid]
1097
        mov     [eax + SOCKET.PID], ebx
1098
        mov     [eax + SOCKET.TID], ebx         ; currently TID = PID in kolibrios :(
1099
      @@:
6011 hidnplayr 1100
        mov     [eax + SOCKET.snd_proc], socket_send_local_initialized
3545 hidnplayr 1101
 
1102
align 4
6011 hidnplayr 1103
socket_send_local_initialized:
3545 hidnplayr 1104
 
3556 hidnplayr 1105
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: LOCAL\n"
3545 hidnplayr 1106
 
1107
        ; get the other side's socket and check if it still exists
1108
        mov     eax, [eax + SOCKET.device]
6011 hidnplayr 1109
        call    socket_check
3658 hidnplayr 1110
        jz      .invalid
3545 hidnplayr 1111
 
1112
        ; allright, shove in the data!
1113
        push    eax
1114
        add     eax, STREAM_SOCKET.rcv
6011 hidnplayr 1115
        call    socket_ring_write
3545 hidnplayr 1116
        pop     eax
1117
 
1118
        ; return the number of written bytes (or errorcode) to application
1119
        mov     [esp+32], ecx
1120
 
1121
        ; and notify the other end
6011 hidnplayr 1122
        call    socket_notify
3545 hidnplayr 1123
 
1124
        ret
1125
 
3658 hidnplayr 1126
  .invalid:
1127
        mov     dword[esp+32], -1
3673 hidnplayr 1128
        mov     dword[esp+20], EINVAL
3658 hidnplayr 1129
        ret
3545 hidnplayr 1130
 
3658 hidnplayr 1131
 
5976 hidnplayr 1132
;-----------------------------------------------------------------;
1133
;                                                                 ;
6011 hidnplayr 1134
; socket_get_opt: Read a socket option                            ;
5976 hidnplayr 1135
;                                                                 ;
1136
;   IN: ecx = socket number                                       ;
1137
;       edx = pointer to socket options struct                    ;
1138
;                                                                 ;
1139
;  OUT: eax = 0 on success                                        ;
1140
;       eax = -1 on error                                         ;
1141
;       ebx = errorcode on error                                  ;
1142
;                                                                 ;
1143
;-----------------------------------------------------------------;
1144
align 4
6011 hidnplayr 1145
socket_get_opt:
5976 hidnplayr 1146
 
1147
; FIXME:
3545 hidnplayr 1148
; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
1149
; TODO: find best way to notify that send()'ed data were acknowledged
1150
; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*.
1151
 
3556 hidnplayr 1152
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_get_opt\n"
3545 hidnplayr 1153
 
6011 hidnplayr 1154
        call    socket_num_to_ptr
5969 hidnplayr 1155
        test    eax, eax
3658 hidnplayr 1156
        jz      .invalid
3545 hidnplayr 1157
 
1158
        cmp     dword [edx], IP_PROTO_TCP
3658 hidnplayr 1159
        jne     .invalid
3545 hidnplayr 1160
        cmp     dword [edx+4], -2
1161
        je      @f
1162
        cmp     dword [edx+4], -3
3658 hidnplayr 1163
        jne     .invalid
3545 hidnplayr 1164
@@:
1165
;        mov     eax, [edx+12]
1166
;        test    eax, eax
1167
;        jz      .fail
1168
;        cmp     dword [eax], 4
1169
;        mov     dword [eax], 4
1170
;        jb      .fail
1171
;        stdcall net_socket_num_to_addr, ecx
1172
;        test    eax, eax
1173
;        jz      .fail
1174
;        ; todo: check that eax is really TCP socket
1175
;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
1176
;        cmp     dword [edx+4], -2
1177
;        jz      @f
1178
;        mov     ecx, [eax + TCP_SOCKET.state]
1179
@@:
1180
        mov     eax, [edx+8]
1181
        test    eax, eax
1182
        jz      @f
1183
        mov     [eax], ecx
1184
@@:
1185
        mov     dword [esp+32], 0
1186
        ret
1187
 
3658 hidnplayr 1188
  .invalid:
1189
        mov     dword[esp+32], -1
3673 hidnplayr 1190
        mov     dword[esp+20], EINVAL
3658 hidnplayr 1191
        ret
3545 hidnplayr 1192
 
1193
 
5976 hidnplayr 1194
;-----------------------------------------------------------------;
1195
;                                                                 ;
6011 hidnplayr 1196
; socket_set_options: Set a socket option.                        ;
5976 hidnplayr 1197
;                                                                 ;
1198
;   IN: ecx = socket number                                       ;
1199
;       edx = pointer to socket options struct                    ;
1200
;                                                                 ;
1201
;  OUT: eax = 0 on success                                        ;
1202
;       eax = -1 on error                                         ;
1203
;       ebx = errorcode on error                                  ;
1204
;                                                                 ;
1205
;-----------------------------------------------------------------;
3545 hidnplayr 1206
align 4
6011 hidnplayr 1207
socket_set_opt:
3545 hidnplayr 1208
 
3556 hidnplayr 1209
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt\n"
3545 hidnplayr 1210
 
6011 hidnplayr 1211
        call    socket_num_to_ptr
5969 hidnplayr 1212
        test    eax, eax
3658 hidnplayr 1213
        jz      .invalid
3545 hidnplayr 1214
 
5842 hidnplayr 1215
        cmp     [edx + socket_options.level], IP_PROTO_IP
1216
        je      .ip
1217
        cmp     [edx + socket_options.level], SOL_SOCKET
3658 hidnplayr 1218
        jne     .invalid
3545 hidnplayr 1219
 
5842 hidnplayr 1220
  .socket:
1221
        cmp     [edx + socket_options.optname], SO_BINDTODEVICE
1222
        jne     .invalid
3545 hidnplayr 1223
 
1224
  .bind:
5842 hidnplayr 1225
        cmp     [edx + socket_options.optlen], 0
3545 hidnplayr 1226
        je      .unbind
1227
 
5842 hidnplayr 1228
        movzx   edx, byte[edx + socket_options.optval]
3600 hidnplayr 1229
        cmp     edx, NET_DEVICES_MAX
3658 hidnplayr 1230
        ja      .invalid
3545 hidnplayr 1231
 
1232
        mov     edx, [NET_DRV_LIST + 4*edx]
1233
        test    edx, edx
3658 hidnplayr 1234
        jz      .already
3545 hidnplayr 1235
        mov     [eax + SOCKET.device], edx
1236
 
5584 hidnplayr 1237
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt: Bound socket %x to device %x\n", eax, edx
3545 hidnplayr 1238
 
3658 hidnplayr 1239
        mov     dword[esp+32], 0        ; success!
3545 hidnplayr 1240
        ret
1241
 
1242
  .unbind:
1243
        mov     [eax + SOCKET.device], 0
1244
 
3658 hidnplayr 1245
        mov     dword[esp+32], 0        ; success!
3545 hidnplayr 1246
        ret
1247
 
5842 hidnplayr 1248
  .ip:
1249
        cmp     [edx + socket_options.optname], IP_TTL
1250
        jne     .invalid
1251
 
1252
  .ttl:
1253
        mov     bl, byte[edx + socket_options.optval]
1254
        mov     [eax + IP_SOCKET.ttl], bl
1255
 
1256
        mov     dword[esp+32], 0        ; success!
1257
        ret
1258
 
3658 hidnplayr 1259
  .already:
3673 hidnplayr 1260
        mov     dword[esp+20], EALREADY
3658 hidnplayr 1261
        mov     dword[esp+32], -1
1262
        ret
3545 hidnplayr 1263
 
5842 hidnplayr 1264
  .invalid:
1265
        mov     dword[esp+20], EINVAL
1266
        mov     dword[esp+32], -1
1267
        ret
3545 hidnplayr 1268
 
3658 hidnplayr 1269
 
1270
 
5842 hidnplayr 1271
 
5976 hidnplayr 1272
;-----------------------------------------------------------------;
1273
;                                                                 ;
6011 hidnplayr 1274
; socket_pair: Allocate a pair of linked local sockets.           ;
5976 hidnplayr 1275
;                                                                 ;
1276
;  IN: /                                                          ;
1277
;                                                                 ;
1278
; OUT: eax = socket1 num on success                               ;
1279
;      eax = -1 on error                                          ;
1280
;      ebx = socket2 num on success                               ;
1281
;      ebx = errorcode on error                                   ;
1282
;                                                                 ;
1283
;-----------------------------------------------------------------;
3545 hidnplayr 1284
align 4
6011 hidnplayr 1285
socket_pair:
3545 hidnplayr 1286
 
3556 hidnplayr 1287
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_pair\n"
3545 hidnplayr 1288
 
6011 hidnplayr 1289
        call    socket_alloc
4535 hidnplayr 1290
        test    eax, eax
3658 hidnplayr 1291
        jz      .nomem1
3545 hidnplayr 1292
        mov     [esp+32], edi   ; application's eax
1293
 
1294
        mov     [eax + SOCKET.Domain], AF_LOCAL
1295
        mov     [eax + SOCKET.Type], SOCK_STREAM
1296
        mov     [eax + SOCKET.Protocol], 0              ;;; CHECKME
6011 hidnplayr 1297
        mov     [eax + SOCKET.snd_proc], socket_send_local
1298
        mov     [eax + SOCKET.rcv_proc], socket_receive_local
3545 hidnplayr 1299
        mov     [eax + SOCKET.PID], 0
1300
        mov     ebx, eax
1301
 
6011 hidnplayr 1302
        call    socket_alloc
4535 hidnplayr 1303
        test    eax, eax
3658 hidnplayr 1304
        jz      .nomem2
3673 hidnplayr 1305
        mov     [esp+20], edi   ; application's ebx
3545 hidnplayr 1306
 
1307
        mov     [eax + SOCKET.Domain], AF_LOCAL
1308
        mov     [eax + SOCKET.Type], SOCK_STREAM
1309
        mov     [eax + SOCKET.Protocol], 0              ;;; CHECKME
6011 hidnplayr 1310
        mov     [eax + SOCKET.snd_proc], socket_send_local
1311
        mov     [eax + SOCKET.rcv_proc], socket_receive_local
3545 hidnplayr 1312
        mov     [eax + SOCKET.PID], 0
1313
 
1314
        ; Link the two sockets to eachother
1315
        mov     [eax + SOCKET.device], ebx
1316
        mov     [ebx + SOCKET.device], eax
1317
 
1318
        lea     eax, [eax + STREAM_SOCKET.rcv]
6011 hidnplayr 1319
        call    socket_ring_create
5155 hidnplayr 1320
        test    eax, eax
5969 hidnplayr 1321
        jz      .nomem2
3545 hidnplayr 1322
 
1323
        lea     eax, [ebx + STREAM_SOCKET.rcv]
6011 hidnplayr 1324
        call    socket_ring_create
5155 hidnplayr 1325
        test    eax, eax
1326
        jz      .nomem2
3545 hidnplayr 1327
 
1328
        ret
1329
 
3658 hidnplayr 1330
  .nomem2:
5969 hidnplayr 1331
        mov     eax, [esp+20]
6011 hidnplayr 1332
        call    socket_free
5969 hidnplayr 1333
 
3658 hidnplayr 1334
  .nomem1:
5969 hidnplayr 1335
        mov     eax, [esp+32]
6011 hidnplayr 1336
        call    socket_free
5969 hidnplayr 1337
 
3658 hidnplayr 1338
        mov     dword[esp+32], -1
5969 hidnplayr 1339
        mov     dword[esp+20], ENOMEM
3658 hidnplayr 1340
        ret
3545 hidnplayr 1341
 
1342
 
1343
 
5976 hidnplayr 1344
;-----------------------------------------------------------------;
1345
;                                                                 ;
6011 hidnplayr 1346
; socket_debug: Copy socket variables to application buffer.      ;
5976 hidnplayr 1347
;                                                                 ;
1348
;   IN: ecx = socket number                                       ;
1349
;       edx = pointer to application buffer                       ;
1350
;                                                                 ;
1351
;  OUT: eax = 0 on success                                        ;
1352
;       eax = -1 on error                                         ;
1353
;       ebx = errorcode on error                                  ;
1354
;                                                                 ;
1355
;-----------------------------------------------------------------;
3545 hidnplayr 1356
align 4
6011 hidnplayr 1357
socket_debug:
3545 hidnplayr 1358
 
3556 hidnplayr 1359
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_debug\n"
3545 hidnplayr 1360
 
1361
        mov     edi, edx
1362
 
1363
        test    ecx, ecx
1364
        jz      .returnall
1365
 
6011 hidnplayr 1366
        call    socket_num_to_ptr
5969 hidnplayr 1367
        test    eax, eax
3658 hidnplayr 1368
        jz      .invalid
3545 hidnplayr 1369
 
1370
        mov     esi, eax
6413 hidnplayr 1371
        mov     ecx, SOCKET_STRUCT_SIZE/4
3711 clevermous 1372
        rep movsd
3545 hidnplayr 1373
 
3658 hidnplayr 1374
        mov     dword[esp+32], 0
3545 hidnplayr 1375
        ret
1376
 
1377
  .returnall:
1378
        mov     ebx, net_sockets
1379
  .next_socket:
1380
        mov     ebx, [ebx + SOCKET.NextPtr]
1381
        test    ebx, ebx
1382
        jz      .done
1383
        mov     eax, [ebx + SOCKET.Number]
1384
        stosd
1385
        jmp     .next_socket
1386
  .done:
1387
        xor     eax, eax
1388
        stosd
3658 hidnplayr 1389
        mov     dword[esp+32], eax
1390
        ret
3545 hidnplayr 1391
 
3658 hidnplayr 1392
  .invalid:
1393
        mov     dword[esp+32], -1
5969 hidnplayr 1394
        mov     dword[esp+20], EINVAL
3545 hidnplayr 1395
        ret
1396
 
1397
 
5976 hidnplayr 1398
;-----------------------------------------------------------------;
1399
;   ____                                                 ____     ;
1400
;   \  /              End of sockets API                 \  /     ;
1401
;    \/                                                   \/      ;
1402
;    ()        Internally used functions follow           ()      ;
1403
;                                                                 ;
1404
;-----------------------------------------------------------------;
1405
 
1406
 
1407
;-----------------------------------------------------------------;
1408
;                                                                 ;
6011 hidnplayr 1409
; socket_find_port:                                               ;
5976 hidnplayr 1410
; Fill in the local port number for TCP and UDP sockets           ;
1411
; This procedure always works because the number of sockets is    ;
1412
; limited to a smaller number then the number of possible ports   ;
1413
;                                                                 ;
1414
;  IN:  eax = socket pointer                                      ;
1415
;                                                                 ;
1416
;  OUT: /                                                         ;
1417
;                                                                 ;
1418
;-----------------------------------------------------------------;
3545 hidnplayr 1419
align 4
6011 hidnplayr 1420
socket_find_port:
3545 hidnplayr 1421
 
3556 hidnplayr 1422
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_find_port\n"
3545 hidnplayr 1423
 
1424
        push    ebx esi ecx
1425
 
1426
        cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
1427
        je      .udp
1428
 
1429
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
1430
        je      .tcp
1431
 
1432
        pop     ecx esi ebx
1433
        ret
1434
 
1435
  .udp:
1436
        mov     bx, [last_UDP_port]
1437
        call    .findit
1438
        mov     [last_UDP_port], bx
1439
 
1440
        pop     ecx esi ebx
1441
        ret
1442
 
1443
  .tcp:
1444
        mov     bx, [last_TCP_port]
1445
        call    .findit
1446
        mov     [last_TCP_port], bx
1447
 
1448
        pop     ecx esi ebx
1449
        ret
1450
 
1451
 
1452
  .restart:
1453
        mov     bx, MIN_EPHEMERAL_PORT_N
1454
  .findit:
1455
        cmp     bx, MAX_EPHEMERAL_PORT_N
1456
        je      .restart
1457
 
1458
        add     bh, 1
1459
        adc     bl, 0
1460
 
6011 hidnplayr 1461
        call    socket_check_port
3545 hidnplayr 1462
        jz      .findit
1463
        ret
1464
 
1465
 
1466
 
5976 hidnplayr 1467
;-----------------------------------------------------------------;
1468
;                                                                 ;
6011 hidnplayr 1469
; socket_check_port: (to be used with AF_INET only!)              ;
5976 hidnplayr 1470
; Checks if a local port number is unused                         ;
1471
; If the proposed port number is unused, it is filled in in the   ;
6011 hidnplayr 1472
; socket structure.                                               ;
5976 hidnplayr 1473
;                                                                 ;
1474
;   IN: eax = socket ptr                                          ;
1475
;       bx = proposed socket number (network byte order)          ;
1476
;                                                                 ;
1477
;  OUT: ZF = set on error                                         ;
1478
;                                                                 ;
1479
;-----------------------------------------------------------------;
3545 hidnplayr 1480
align 4
6011 hidnplayr 1481
socket_check_port:
3545 hidnplayr 1482
 
3556 hidnplayr 1483
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check_port: "
3545 hidnplayr 1484
 
3647 hidnplayr 1485
        pusha
1486
        mov     ecx, socket_mutex
1487
        call    mutex_lock
1488
        popa
1489
 
3545 hidnplayr 1490
        mov     ecx, [eax + SOCKET.Protocol]
1491
        mov     edx, [eax + IP_SOCKET.LocalIP]
1492
        mov     esi, net_sockets
1493
 
1494
  .next_socket:
1495
        mov     esi, [esi + SOCKET.NextPtr]
1496
        or      esi, esi
1497
        jz      .port_ok
1498
 
1499
        cmp     [esi + SOCKET.Protocol], ecx
1500
        jne     .next_socket
1501
 
1502
        cmp     [esi + IP_SOCKET.LocalIP], edx
1503
        jne     .next_socket
1504
 
1505
        cmp     [esi + UDP_SOCKET.LocalPort], bx
1506
        jne     .next_socket
1507
 
3647 hidnplayr 1508
        pusha
1509
        mov     ecx, socket_mutex
1510
        call    mutex_unlock
1511
        popa
1512
 
3556 hidnplayr 1513
        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 1514
        ret
1515
 
1516
  .port_ok:
3647 hidnplayr 1517
        pusha
1518
        mov     ecx, socket_mutex
1519
        call    mutex_unlock
1520
        popa
1521
 
3556 hidnplayr 1522
        DEBUGF  DEBUG_NETWORK_VERBOSE, "local port %x is free\n", bx         ; FIXME: find a way to print big endian values with debugf
3545 hidnplayr 1523
        mov     [eax + UDP_SOCKET.LocalPort], bx
1524
        or      bx, bx                                  ; clear the zero-flag
1525
        ret
1526
 
1527
 
1528
 
5976 hidnplayr 1529
;-----------------------------------------------------------------;
1530
;                                                                 ;
6011 hidnplayr 1531
; socket_input: Update a (stateless) socket with received data.   ;
5976 hidnplayr 1532
;                                                                 ;
1533
; Note: The socket's mutex should already be set !                ;
1534
;                                                                 ;
1535
;   IN: eax = socket ptr                                          ;
1536
;       ecx = data size                                           ;
1537
;       esi = ptr to data                                         ;
1538
;       [esp] = ptr to buf                                        ;
1539
;                                                                 ;
1540
;  OUT: /                                                         ;
1541
;                                                                 ;
1542
;-----------------------------------------------------------------;
3545 hidnplayr 1543
align 4
6011 hidnplayr 1544
socket_input:
3545 hidnplayr 1545
 
3556 hidnplayr 1546
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
3545 hidnplayr 1547
 
5522 hidnplayr 1548
        push    ecx
3545 hidnplayr 1549
        push    esi
1550
        mov     esi, esp
1551
 
6011 hidnplayr 1552
        add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .full
3545 hidnplayr 1553
 
3556 hidnplayr 1554
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_input: success\n"
3545 hidnplayr 1555
        add     esp, sizeof.socket_queue_entry
1556
 
1557
        pusha
1558
        lea     ecx, [eax + SOCKET.mutex]
1559
        call    mutex_unlock
1560
        popa
1561
 
6011 hidnplayr 1562
        jmp     socket_notify
3545 hidnplayr 1563
 
1564
  .full:
3556 hidnplayr 1565
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket %x is full!\n", eax
3545 hidnplayr 1566
 
1567
        pusha
1568
        lea     ecx, [eax + SOCKET.mutex]
1569
        call    mutex_unlock
1570
        popa
1571
 
5842 hidnplayr 1572
        add     esp, 8
6011 hidnplayr 1573
        call    net_buff_free
3545 hidnplayr 1574
        ret
1575
 
1576
 
5976 hidnplayr 1577
;-----------------------------------------------------------------;
1578
;                                                                 ;
6011 hidnplayr 1579
; socket_ring_create: Create a ringbuffer for sockets.            ;
5976 hidnplayr 1580
;                                                                 ;
1581
;   IN: eax = ptr to ring struct                                  ;
1582
;                                                                 ;
1583
;  OUT: eax = 0 on error                                          ;
1584
;       eax = start ptr                                           ;
1585
;                                                                 ;
1586
;-----------------------------------------------------------------;
3545 hidnplayr 1587
align 4
6011 hidnplayr 1588
socket_ring_create:
3545 hidnplayr 1589
 
1590
        push    esi
1591
        mov     esi, eax
1592
 
1593
        push    edx
6413 hidnplayr 1594
        stdcall create_ring_buffer, SOCKET_BUFFER_SIZE, PG_SWR
3545 hidnplayr 1595
        pop     edx
5155 hidnplayr 1596
        test    eax, eax
1597
        jz      .fail
3545 hidnplayr 1598
 
5969 hidnplayr 1599
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_create: %x\n", eax
3545 hidnplayr 1600
 
1601
        pusha
1602
        lea     ecx, [esi + RING_BUFFER.mutex]
1603
        call    mutex_init
1604
        popa
1605
 
1606
        mov     [esi + RING_BUFFER.start_ptr], eax
1607
        mov     [esi + RING_BUFFER.write_ptr], eax
1608
        mov     [esi + RING_BUFFER.read_ptr], eax
1609
        mov     [esi + RING_BUFFER.size], 0
6413 hidnplayr 1610
        add     eax, SOCKET_BUFFER_SIZE
3545 hidnplayr 1611
        mov     [esi + RING_BUFFER.end_ptr], eax
1612
        mov     eax, esi
1613
 
5969 hidnplayr 1614
        pop     esi
1615
        ret
1616
 
5155 hidnplayr 1617
  .fail:
5969 hidnplayr 1618
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ring_create: Out of memory!\n"
5566 hidnplayr 1619
        pop     esi
3545 hidnplayr 1620
        ret
1621
 
5976 hidnplayr 1622
;-----------------------------------------------------------------;
1623
;                                                                 ;
6011 hidnplayr 1624
; socket_ring_write: Write data to ring buffer.                   ;
5976 hidnplayr 1625
;                                                                 ;
1626
;   IN: eax = ptr to ring struct                                  ;
1627
;       ecx = data size                                           ;
1628
;       esi = ptr to data                                         ;
1629
;                                                                 ;
1630
;  OUT: ecx = number of bytes stored                              ;
1631
;                                                                 ;
1632
;-----------------------------------------------------------------;
3545 hidnplayr 1633
align 4
6011 hidnplayr 1634
socket_ring_write:
3545 hidnplayr 1635
 
3556 hidnplayr 1636
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
3545 hidnplayr 1637
 
1638
; lock mutex
1639
        pusha
1640
        lea     ecx, [eax + RING_BUFFER.mutex]
1641
        call    mutex_lock                                      ; TODO: check what registers this function actually destroys
1642
        popa
1643
 
1644
; calculate available size
6413 hidnplayr 1645
        mov     edi, SOCKET_BUFFER_SIZE
3545 hidnplayr 1646
        sub     edi, [eax + RING_BUFFER.size]                   ; available buffer size in edi
1647
        cmp     ecx, edi
1648
        jbe     .copy
1649
        mov     ecx, edi
1650
  .copy:
1651
        mov     edi, [eax + RING_BUFFER.write_ptr]
3556 hidnplayr 1652
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
3545 hidnplayr 1653
 
1654
; update write ptr
1655
        push    edi
1656
        add     edi, ecx
1657
        cmp     edi, [eax + RING_BUFFER.end_ptr]
1658
        jb      @f
6413 hidnplayr 1659
        sub     edi, SOCKET_BUFFER_SIZE                         ; WRAP
3545 hidnplayr 1660
  @@:
5155 hidnplayr 1661
 
3545 hidnplayr 1662
        mov     [eax + RING_BUFFER.write_ptr], edi
1663
        pop     edi
1664
 
1665
; update size
1666
        add     [eax + RING_BUFFER.size], ecx
1667
 
1668
; copy the data
1669
        push    ecx
1670
        shr     ecx, 1
1671
        jnc     .nb
1672
        movsb
1673
  .nb:
1674
        shr     ecx, 1
1675
        jnc     .nw
1676
        movsw
1677
  .nw:
1678
        test    ecx, ecx
1679
        jz      .nd
3711 clevermous 1680
        rep movsd
3545 hidnplayr 1681
  .nd:
1682
        pop     ecx
1683
 
1684
; unlock mutex
4344 hidnplayr 1685
        pusha
3545 hidnplayr 1686
        lea     ecx, [eax + RING_BUFFER.mutex]
1687
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
4344 hidnplayr 1688
        popa
3545 hidnplayr 1689
 
1690
        ret
1691
 
5976 hidnplayr 1692
;-----------------------------------------------------------------;
1693
;                                                                 ;
6011 hidnplayr 1694
; socket_ring_read: Read from ring buffer                         ;
5976 hidnplayr 1695
;                                                                 ;
1696
;   IN: eax = ring struct ptr                                     ;
1697
;       ecx = bytes to read                                       ;
1698
;       edx = offset                                              ;
1699
;       edi = ptr to buffer start                                 ;
1700
;                                                                 ;
1701
;  OUT: eax = unchanged                                           ;
1702
;       ecx = number of bytes read (0 on error)                   ;
1703
;       edx = destroyed                                           ;
1704
;       esi = destroyed                                           ;
1705
;       edi = ptr to buffer end                                   ;
1706
;                                                                 ;
1707
;-----------------------------------------------------------------;
3545 hidnplayr 1708
align 4
6011 hidnplayr 1709
socket_ring_read:
3545 hidnplayr 1710
 
3556 hidnplayr 1711
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx
3545 hidnplayr 1712
 
1713
        pusha
1714
        lea     ecx, [eax + RING_BUFFER.mutex]
1715
        call    mutex_lock                                      ; TODO: check what registers this function actually destroys
1716
        popa
1717
 
1718
        mov     esi, [eax + RING_BUFFER.read_ptr]
1719
        add     esi, edx                                        ; esi = start_ptr + offset
1720
 
1721
        neg     edx
1722
        add     edx, [eax + RING_BUFFER.size]                   ; edx = snd.size - offset
1723
        jle     .no_data_at_all
1724
 
1725
        pusha
1726
        lea     ecx, [eax + RING_BUFFER.mutex]
1727
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1728
        popa
1729
 
1730
        cmp     ecx, edx
1731
        ja      .less_data
1732
 
1733
  .copy:
3556 hidnplayr 1734
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
3545 hidnplayr 1735
        push    ecx
1736
        shr     ecx, 1
1737
        jnc     .nb
1738
        movsb
1739
  .nb:
1740
        shr     ecx, 1
1741
        jnc     .nw
1742
        movsw
1743
  .nw:
1744
        test    ecx, ecx
1745
        jz      .nd
3711 clevermous 1746
        rep movsd
3545 hidnplayr 1747
  .nd:
1748
        pop     ecx
1749
        ret
1750
 
1751
  .no_data_at_all:
1752
        pusha
1753
        lea     ecx, [eax + RING_BUFFER.mutex]
1754
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1755
        popa
1756
 
3556 hidnplayr 1757
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: no data at all!\n"
3545 hidnplayr 1758
        xor     ecx, ecx
1759
        ret
1760
 
1761
  .less_data:
1762
        mov     ecx, edx
1763
        jmp     .copy
1764
 
1765
 
5976 hidnplayr 1766
;-----------------------------------------------------------------;
1767
;                                                                 ;
6011 hidnplayr 1768
; socket_ring_free: Free data from a ringbuffer.                  ;
5976 hidnplayr 1769
;                                                                 ;
1770
;   IN: eax = ptr to ring struct                                  ;
1771
;       ecx = data size                                           ;
1772
;                                                                 ;
1773
;  OUT: ecx = number of freed bytes                               ;
1774
;                                                                 ;
1775
;-----------------------------------------------------------------;
3545 hidnplayr 1776
align 4
6011 hidnplayr 1777
socket_ring_free:
3545 hidnplayr 1778
 
3556 hidnplayr 1779
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
3545 hidnplayr 1780
 
1781
        push    eax ecx
1782
        lea     ecx, [eax + RING_BUFFER.mutex]
1783
        call    mutex_lock                                      ; TODO: check what registers this function actually destroys
1784
        pop     ecx eax
1785
 
1786
        sub     [eax + RING_BUFFER.size], ecx
1787
        jb      .error
1788
        add     [eax + RING_BUFFER.read_ptr], ecx
1789
 
1790
        mov     edx, [eax + RING_BUFFER.end_ptr]
1791
        cmp     [eax + RING_BUFFER.read_ptr], edx
1792
        jb      @f
6413 hidnplayr 1793
        sub     [eax + RING_BUFFER.read_ptr], SOCKET_BUFFER_SIZE
3545 hidnplayr 1794
       @@:
1795
 
1796
        push    eax ecx
1797
        lea     ecx, [eax + RING_BUFFER.mutex]                  ; TODO: check what registers this function actually destroys
1798
        call    mutex_unlock
1799
        pop     ecx eax
1800
 
1801
        ret
1802
 
1803
  .error:       ; we could free all available bytes, but that would be stupid, i guess..
3556 hidnplayr 1804
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: buffer=%x error!\n", eax
3545 hidnplayr 1805
        add     [eax + RING_BUFFER.size], ecx
1806
 
1807
        push    eax
1808
        lea     ecx, [eax + RING_BUFFER.mutex]
1809
        call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
1810
        pop     eax
1811
 
1812
        xor     ecx, ecx
1813
        ret
1814
 
1815
 
5976 hidnplayr 1816
;-----------------------------------------------------------------;
1817
;                                                                 ;
6011 hidnplayr 1818
; socket_block: Suspend the thread attached to a socket.          ;
5976 hidnplayr 1819
;                                                                 ;
1820
;   IN: eax = socket ptr                                          ;
1821
;                                                                 ;
1822
;  OUT: eax = unchanged                                           ;
1823
;                                                                 ;
1824
;-----------------------------------------------------------------;
3545 hidnplayr 1825
align 4
6011 hidnplayr 1826
socket_block:
3545 hidnplayr 1827
 
6908 ashmew2 1828
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: %x\n", eax
3545 hidnplayr 1829
 
4520 hidnplayr 1830
        push    eax
1831
 
3545 hidnplayr 1832
        pushf
1833
        cli
1834
 
1835
        ; Set the 'socket is blocked' flag
1836
        or      [eax + SOCKET.state], SS_BLOCKED
1837
 
1838
        ; Suspend the thread
1839
        push    edx
1840
        mov     edx, [TASK_BASE]
1841
        mov     [edx + TASKDATA.state], 1               ; Suspended
1842
 
1843
        ; Remember the thread ID so we can wake it up again
1844
        mov     edx, [edx + TASKDATA.pid]
3556 hidnplayr 1845
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: suspending thread: %u\n", edx
3545 hidnplayr 1846
        mov     [eax + SOCKET.TID], edx
1847
        pop     edx
4520 hidnplayr 1848
        popf
3545 hidnplayr 1849
 
1850
        call    change_task
3674 hidnplayr 1851
        pop     eax
3545 hidnplayr 1852
 
6908 ashmew2 1853
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: continuing\n"
3545 hidnplayr 1854
 
1855
        ret
1856
 
1857
 
5976 hidnplayr 1858
;-----------------------------------------------------------------;
1859
;                                                                 ;
6011 hidnplayr 1860
; socket_notify: Wake up socket owner thread.                     ;
5976 hidnplayr 1861
;                                                                 ;
1862
;   IN: eax = socket ptr                                          ;
1863
;                                                                 ;
1864
;  OUT: eax = unchanged                                           ;
1865
;                                                                 ;
1866
;-----------------------------------------------------------------;
3545 hidnplayr 1867
align 4
6011 hidnplayr 1868
socket_notify:
3545 hidnplayr 1869
 
3556 hidnplayr 1870
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: %x\n", eax
3545 hidnplayr 1871
 
6011 hidnplayr 1872
        call    socket_check
3545 hidnplayr 1873
        jz      .error
1874
 
4527 hidnplayr 1875
; Find the associated thread's TASK_DATA
1876
        push    ebx ecx esi
1877
        mov     ebx, [eax + SOCKET.TID]
1878
        test    ebx, ebx
1879
        jz      .error2
1880
        xor     ecx, ecx
3545 hidnplayr 1881
        inc     ecx
4527 hidnplayr 1882
        mov     esi, TASK_DATA
1883
  .next:
1884
        cmp     [esi + TASKDATA.pid], ebx
1885
        je      .found
1886
        inc     ecx
3545 hidnplayr 1887
        add     esi, 0x20
1888
        cmp     ecx, [TASK_COUNT]
4527 hidnplayr 1889
        jbe     .next
1890
 
1891
  .error2:
3545 hidnplayr 1892
; PID not found, TODO: close socket!
4527 hidnplayr 1893
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_notify: error finding thread 0x%x !\n", ebx
1894
        pop     esi ecx ebx
1895
        ret
3545 hidnplayr 1896
 
4527 hidnplayr 1897
  .error:
4528 hidnplayr 1898
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_notify: invalid socket ptr: 0x%x !\n", eax
4527 hidnplayr 1899
        ret
1900
 
1901
  .found:
1902
        test    [eax + SOCKET.state], SS_BLOCKED
4528 hidnplayr 1903
        jnz     .un_block
4527 hidnplayr 1904
 
5976 hidnplayr 1905
; Socket and thread exists and socket is of non blocking type.
4527 hidnplayr 1906
; We'll try to flag an event to the thread.
3545 hidnplayr 1907
        shl     ecx, 8
6908 ashmew2 1908
        or      [SLOT_BASE + ecx + APPDATA.event_mask], EVENT_NETWORK
3545 hidnplayr 1909
 
6413 hidnplayr 1910
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: poking thread %u!\n", ebx
4527 hidnplayr 1911
        pop     esi ecx ebx
1912
        ret
3545 hidnplayr 1913
 
4527 hidnplayr 1914
 
4528 hidnplayr 1915
  .un_block:
5976 hidnplayr 1916
; Socket and thread exists and socket is of blocking type
4527 hidnplayr 1917
; We'll try to unblock it.
1918
        and     [eax + SOCKET.state], not SS_BLOCKED    ; Clear the 'socket is blocked' flag
1919
        mov     [esi + TASKDATA.state], 0               ; Run the thread
3545 hidnplayr 1920
 
6908 ashmew2 1921
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: Unblocked socket!\n"
4527 hidnplayr 1922
        pop     esi ecx ebx
3545 hidnplayr 1923
        ret
1924
 
1925
 
5976 hidnplayr 1926
;-----------------------------------------------------------------;
1927
;                                                                 ;
6011 hidnplayr 1928
; socket_alloc: Allocate memory for socket and put new socket     ;
1929
; into the list. Newly created socket is initialized with calling ;
1930
; PID and given a socket number.                                  ;
5976 hidnplayr 1931
;                                                                 ;
1932
;  IN:  /                                                         ;
1933
;                                                                 ;
1934
; OUT:  eax = socket ptr on success                               ;
1935
;       eax = 0 on error                                          ;
1936
;       edi = socket number on success                            ;
1937
;                                                                 ;
1938
;-----------------------------------------------------------------;
3545 hidnplayr 1939
align 4
6011 hidnplayr 1940
socket_alloc:
3545 hidnplayr 1941
 
1942
        push    ebx
1943
 
6413 hidnplayr 1944
        stdcall kernel_alloc, SOCKET_STRUCT_SIZE
5969 hidnplayr 1945
        or      eax, eax
1946
        jz      .nomem
3556 hidnplayr 1947
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: ptr=%x\n", eax
3545 hidnplayr 1948
 
1949
; zero-initialize allocated memory
1950
        push    eax
1951
        mov     edi, eax
6413 hidnplayr 1952
        mov     ecx, SOCKET_STRUCT_SIZE / 4
3545 hidnplayr 1953
        xor     eax, eax
3711 clevermous 1954
        rep stosd
3545 hidnplayr 1955
        pop     eax
1956
 
1957
; set send-and receive procedures to return -1
3658 hidnplayr 1958
        mov     [eax + SOCKET.snd_proc], .not_yet
1959
        mov     [eax + SOCKET.rcv_proc], .not_yet
3545 hidnplayr 1960
 
3647 hidnplayr 1961
        pusha
1962
        mov     ecx, socket_mutex
1963
        call    mutex_lock
1964
        popa
1965
 
3545 hidnplayr 1966
; find first free socket number and use it
1967
        mov     edi, [last_socket_num]
1968
  .next_socket_number:
1969
        inc     edi
1970
        jz      .next_socket_number     ; avoid socket nr 0
1971
        cmp     edi, -1
1972
        je      .next_socket_number     ; avoid socket nr -1
1973
        mov     ebx, net_sockets
1974
  .next_socket:
1975
        mov     ebx, [ebx + SOCKET.NextPtr]
1976
        test    ebx, ebx
1977
        jz      .last_socket
1978
 
1979
        cmp     [ebx + SOCKET.Number], edi
1980
        jne     .next_socket
1981
        jmp     .next_socket_number
1982
 
1983
  .last_socket:
1984
        mov     [last_socket_num], edi
1985
        mov     [eax + SOCKET.Number], edi
3556 hidnplayr 1986
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: number=%u\n", edi
3545 hidnplayr 1987
 
1988
; Fill in PID
1989
        mov     ebx, [TASK_BASE]
1990
        mov     ebx, [ebx + TASKDATA.pid]
1991
        mov     [eax + SOCKET.PID], ebx
1992
        mov     [eax + SOCKET.TID], ebx         ; currently TID = PID in kolibrios :(
1993
 
1994
; init mutex
1995
        pusha
1996
        lea     ecx, [eax + SOCKET.mutex]
1997
        call    mutex_init
1998
        popa
1999
 
2000
; add socket to the list by re-arranging some pointers
2001
        mov     ebx, [net_sockets + SOCKET.NextPtr]
2002
 
2003
        mov     [eax + SOCKET.PrevPtr], net_sockets
2004
        mov     [eax + SOCKET.NextPtr], ebx
2005
 
2006
        test    ebx, ebx
2007
        jz      @f
2008
 
2009
        pusha
2010
        lea     ecx, [ebx + SOCKET.mutex]
2011
        call    mutex_lock
2012
        popa
2013
 
2014
        mov     [ebx + SOCKET.PrevPtr], eax
2015
 
2016
        pusha
2017
        lea     ecx, [ebx + SOCKET.mutex]
2018
        call    mutex_unlock
2019
        popa
2020
       @@:
2021
 
2022
        mov     [net_sockets + SOCKET.NextPtr], eax
3647 hidnplayr 2023
 
2024
        pusha
2025
        mov     ecx, socket_mutex
2026
        call    mutex_unlock
2027
        popa
3545 hidnplayr 2028
        pop     ebx
2029
 
2030
        ret
2031
 
5969 hidnplayr 2032
  .nomem:
2033
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_alloc: Out of memory!\n"
2034
        pop     ebx
2035
        ret
2036
 
3658 hidnplayr 2037
  .not_yet:
3673 hidnplayr 2038
        mov     dword[esp+20], ENOTCONN
3658 hidnplayr 2039
        mov     dword[esp+32], -1
2040
        ret
3545 hidnplayr 2041
 
3658 hidnplayr 2042
 
5976 hidnplayr 2043
;-----------------------------------------------------------------;
2044
;                                                                 ;
6011 hidnplayr 2045
; socket_free: Free socket data memory and remove socket from     ;
2046
; the list. Caller should lock and unlock socket_mutex.           ;
5976 hidnplayr 2047
;                                                                 ;
2048
;  IN:  eax = socket ptr                                          ;
2049
;                                                                 ;
2050
; OUT:  /                                                         ;
2051
;                                                                 ;
2052
;-----------------------------------------------------------------;
3545 hidnplayr 2053
align 4
6011 hidnplayr 2054
socket_free:
3545 hidnplayr 2055
 
3556 hidnplayr 2056
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: %x\n", eax
3545 hidnplayr 2057
 
6011 hidnplayr 2058
        call    socket_check
3545 hidnplayr 2059
        jz      .error
2060
 
2061
        push    ebx
2062
 
2063
        pusha
2064
        lea     ecx, [eax + SOCKET.mutex]
2065
        call    mutex_lock
2066
        popa
2067
 
5969 hidnplayr 2068
        cmp     [eax + SOCKET.Type], SOCK_STREAM
2069
        jne     .no_stream
3545 hidnplayr 2070
 
2071
        mov     ebx, eax
5969 hidnplayr 2072
        cmp     [eax + STREAM_SOCKET.rcv.start_ptr], 0
2073
        je      @f
2074
        stdcall free_kernel_space, [eax + STREAM_SOCKET.rcv.start_ptr]
2075
  @@:
2076
        cmp     [ebx + STREAM_SOCKET.snd.start_ptr], 0
2077
        je      @f
2078
        stdcall free_kernel_space, [ebx + STREAM_SOCKET.snd.start_ptr]
2079
  @@:
3545 hidnplayr 2080
        mov     eax, ebx
5969 hidnplayr 2081
  .no_stream:
3545 hidnplayr 2082
 
3652 hidnplayr 2083
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: freeing socket %x\n", eax
3545 hidnplayr 2084
        push    eax                             ; this will be passed to kernel_free
2085
        mov     ebx, [eax + SOCKET.NextPtr]
2086
        mov     eax, [eax + SOCKET.PrevPtr]
2087
 
3556 hidnplayr 2088
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
3545 hidnplayr 2089
 
2090
        test    eax, eax
2091
        jz      @f
2092
        mov     [eax + SOCKET.NextPtr], ebx
2093
       @@:
2094
 
2095
        test    ebx, ebx
2096
        jz      @f
2097
        mov     [ebx + SOCKET.PrevPtr], eax
2098
       @@:
2099
 
2100
        call    kernel_free
2101
        pop     ebx
2102
 
3652 hidnplayr 2103
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: success!\n"
2104
 
2105
  .error:
3545 hidnplayr 2106
        ret
2107
 
5969 hidnplayr 2108
  .error1:
2109
        pop     ebx
5976 hidnplayr 2110
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_free: error!\n"
5969 hidnplayr 2111
        DEBUGF  DEBUG_NETWORK_ERROR, "socket ptr=0x%x caller=0x%x\n", eax, [esp]
2112
        ret
2113
 
5976 hidnplayr 2114
;-----------------------------------------------------------------;
2115
;                                                                 ;
6011 hidnplayr 2116
; socket_fork: Create a child socket.                             ;
5976 hidnplayr 2117
;                                                                 ;
2118
;  IN:  ebx = socket number                                       ;
2119
;                                                                 ;
2120
; OUT:  eax = child socket number on success                      ;
2121
;       eax = 0 on error                                          ;
2122
;                                                                 ;
2123
;-----------------------------------------------------------------;
3545 hidnplayr 2124
align 4
6011 hidnplayr 2125
socket_fork:
3545 hidnplayr 2126
 
3556 hidnplayr 2127
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_fork: %x\n", ebx
3545 hidnplayr 2128
 
2129
; Exit if backlog queue is full
2130
        mov     eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
2131
        cmp     ax, [ebx + SOCKET.backlog]
2132
        jae     .fail
2133
 
2134
; Allocate new socket
2135
        push    ebx
6011 hidnplayr 2136
        call    socket_alloc
3545 hidnplayr 2137
        pop     ebx
4535 hidnplayr 2138
        test    eax, eax
3545 hidnplayr 2139
        jz      .fail
2140
 
2141
        push    eax
2142
        mov     esi, esp
2143
        add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
2144
        pop     eax
2145
 
2146
; Copy structure from current socket to new
3817 hidnplayr 2147
; We start at PID to preserve the socket num, 2 pointers and mutex
2148
; TID will be filled in later
3545 hidnplayr 2149
        lea     esi, [ebx + SOCKET.PID]
2150
        lea     edi, [eax + SOCKET.PID]
2151
        mov     ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
3711 clevermous 2152
        rep movsd
3545 hidnplayr 2153
 
2154
        and     [eax + SOCKET.options], not SO_ACCEPTCON
2155
 
3817 hidnplayr 2156
; Notify owner of parent socket
2157
        push    eax
2158
        mov     eax, ebx
6011 hidnplayr 2159
        call    socket_notify
3817 hidnplayr 2160
        pop     eax
2161
 
3545 hidnplayr 2162
        ret
2163
 
2164
  .fail2:
2165
        add     esp, 4+4+4
2166
  .fail:
3556 hidnplayr 2167
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_fork: failed\n"
3545 hidnplayr 2168
        xor     eax, eax
2169
        ret
2170
 
2171
 
5976 hidnplayr 2172
;-----------------------------------------------------------------;
2173
;                                                                 ;
6011 hidnplayr 2174
; socket_num_to_ptr: Get socket structure address by its number.  ;
5976 hidnplayr 2175
;                                                                 ;
2176
;  IN:  ecx = socket number                                       ;
2177
;                                                                 ;
2178
; OUT:  eax = socket ptr                                          ;
2179
;       eax = 0 on error                                          ;
2180
;                                                                 ;
2181
;-----------------------------------------------------------------;
3545 hidnplayr 2182
align 4
6011 hidnplayr 2183
socket_num_to_ptr:
3545 hidnplayr 2184
 
3556 hidnplayr 2185
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_num_to_ptr: num=%u ", ecx
3545 hidnplayr 2186
 
3647 hidnplayr 2187
        pusha
2188
        mov     ecx, socket_mutex
2189
        call    mutex_lock
2190
        popa
2191
 
3545 hidnplayr 2192
        mov     eax, net_sockets
2193
  .next_socket:
2194
        mov     eax, [eax + SOCKET.NextPtr]
5969 hidnplayr 2195
        test    eax, eax
3545 hidnplayr 2196
        jz      .error
2197
        cmp     [eax + SOCKET.Number], ecx
2198
        jne     .next_socket
2199
 
3647 hidnplayr 2200
        pusha
2201
        mov     ecx, socket_mutex
2202
        call    mutex_unlock
2203
        popa
2204
 
3556 hidnplayr 2205
        DEBUGF  DEBUG_NETWORK_VERBOSE, "ptr=%x\n", eax
3545 hidnplayr 2206
        ret
2207
 
2208
  .error:
3647 hidnplayr 2209
        pusha
2210
        mov     ecx, socket_mutex
2211
        call    mutex_unlock
2212
        popa
2213
 
4574 hidnplayr 2214
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: socket %u not found!\n", eax
2215
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: caller = 0x%x\n", [esp]
3545 hidnplayr 2216
        ret
2217
 
2218
 
5976 hidnplayr 2219
;-----------------------------------------------------------------;
2220
;                                                                 ;
6011 hidnplayr 2221
; socket_ptr_to_num: Get socket number by its address.            ;
5976 hidnplayr 2222
;                                                                 ;
2223
;  IN:  eax = socket ptr                                          ;
2224
;                                                                 ;
2225
; OUT:  eax = socket number                                       ;
2226
;       eax = 0 on error                                          ;
2227
;       ZF = set on error                                         ;
2228
;                                                                 ;
2229
;-----------------------------------------------------------------;
3545 hidnplayr 2230
align 4
6011 hidnplayr 2231
socket_ptr_to_num:
3545 hidnplayr 2232
 
3556 hidnplayr 2233
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ptr_to_num: ptr=%x ", eax
3545 hidnplayr 2234
 
6011 hidnplayr 2235
        call    socket_check
3545 hidnplayr 2236
        jz      .error
2237
 
2238
        mov     eax, [eax + SOCKET.Number]
2239
 
3556 hidnplayr 2240
        DEBUGF  DEBUG_NETWORK_VERBOSE, "num=%u\n", eax
3545 hidnplayr 2241
        ret
2242
 
2243
  .error:
3556 hidnplayr 2244
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ptr_to_num: not found\n", eax
3545 hidnplayr 2245
        ret
2246
 
2247
 
5976 hidnplayr 2248
;-----------------------------------------------------------------;
2249
;                                                                 ;
6011 hidnplayr 2250
; socket_check: Checks if the given ptr is really a socket ptr.   ;
5976 hidnplayr 2251
;                                                                 ;
2252
;  IN:  eax = socket ptr                                          ;
2253
;                                                                 ;
2254
; OUT:  eax = 0 on error                                          ;
2255
;       ZF = set on error                                         ;
2256
;                                                                 ;
2257
;-----------------------------------------------------------------;
3545 hidnplayr 2258
align 4
6011 hidnplayr 2259
socket_check:
3545 hidnplayr 2260
 
3556 hidnplayr 2261
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check: %x\n", eax
3545 hidnplayr 2262
 
5969 hidnplayr 2263
        test    eax, eax
2264
        jz      .error
3545 hidnplayr 2265
        push    ebx
2266
        mov     ebx, net_sockets
2267
 
2268
  .next_socket:
2269
        mov     ebx, [ebx + SOCKET.NextPtr]
2270
        or      ebx, ebx
2271
        jz      .done
2272
        cmp     ebx, eax
2273
        jnz     .next_socket
2274
 
2275
  .done:
2276
        mov     eax, ebx
2277
        test    eax, eax
2278
        pop     ebx
5969 hidnplayr 2279
        ret
3545 hidnplayr 2280
 
5969 hidnplayr 2281
  .error:
5976 hidnplayr 2282
        DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_check: called with argument 0\n"
5969 hidnplayr 2283
        DEBUGF  DEBUG_NETWORK_ERROR, "stack: 0x%x, 0x%x, 0x%x\n", [esp], [esp+4], [esp+8]
3545 hidnplayr 2284
        ret
2285
 
2286
 
2287
 
5976 hidnplayr 2288
;-----------------------------------------------------------------;
2289
;                                                                 ;
6011 hidnplayr 2290
; socket_check_owner: Check if the caller app owns the socket.    ;
5976 hidnplayr 2291
;                                                                 ;
2292
;  IN:  eax = socket ptr                                          ;
2293
;                                                                 ;
2294
; OUT:  ZF = true/false                                           ;
2295
;                                                                 ;
2296
;-----------------------------------------------------------------;
3545 hidnplayr 2297
align 4
6011 hidnplayr 2298
socket_check_owner:
3545 hidnplayr 2299
 
3556 hidnplayr 2300
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check_owner: %x\n", eax
3545 hidnplayr 2301
 
2302
        push    ebx
2303
        mov     ebx, [TASK_BASE]
2304
        mov     ebx, [ebx + TASKDATA.pid]
2305
        cmp     [eax + SOCKET.PID], ebx
3711 clevermous 2306
        pop     ebx
3545 hidnplayr 2307
 
2308
        ret
2309
 
2310
 
2311
 
2312
 
5976 hidnplayr 2313
;-----------------------------------------------------------------;
2314
;                                                                 ;
6011 hidnplayr 2315
; socket_process_end: Kernel calls this function when a certain   ;
2316
; process ends. This function will check if the process had any   ;
2317
; open sockets and update them accordingly (clean up).            ;
5976 hidnplayr 2318
;                                                                 ;
2319
;  IN:  edx = pid                                                 ;
2320
;                                                                 ;
2321
; OUT:  /                                                         ;
2322
;                                                                 ;
2323
;-----------------------------------------------------------------;
3545 hidnplayr 2324
align 4
6011 hidnplayr 2325
socket_process_end:
3545 hidnplayr 2326
 
4436 hidnplayr 2327
        ret     ; FIXME
2328
 
4056 hidnplayr 2329
        cmp     [net_sockets + SOCKET.NextPtr], 0       ; Are there any active sockets at all?
2330
        je      .quickret                               ; nope, exit immediately
2331
 
2332
; TODO: run the following code in another thread, to avoid deadlock
2333
 
3556 hidnplayr 2334
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: %x\n", edx
3545 hidnplayr 2335
 
3647 hidnplayr 2336
        pusha
2337
        mov     ecx, socket_mutex
2338
        call    mutex_lock
2339
        popa
2340
 
3545 hidnplayr 2341
        push    ebx
2342
        mov     ebx, net_sockets
2343
 
2344
  .next_socket:
2345
        mov     ebx, [ebx + SOCKET.NextPtr]
2346
  .next_socket_test:
2347
        test    ebx, ebx
2348
        jz      .done
2349
 
2350
        cmp     [ebx + SOCKET.PID], edx
2351
        jne     .next_socket
2352
 
3556 hidnplayr 2353
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: killing socket %x\n", ebx
3545 hidnplayr 2354
 
2355
        mov     [ebx + SOCKET.PID], 0
2356
        mov     eax, ebx
2357
        mov     ebx, [ebx + SOCKET.NextPtr]
3648 hidnplayr 2358
 
3545 hidnplayr 2359
        pusha
3652 hidnplayr 2360
        cmp     [eax + SOCKET.Domain], AF_INET4
2361
        jne     .free
2362
 
2363
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
2364
        jne     .free
2365
 
6011 hidnplayr 2366
        call    tcp_disconnect
3652 hidnplayr 2367
        jmp     .closed
2368
 
2369
  .free:
6011 hidnplayr 2370
        call    socket_free
3652 hidnplayr 2371
 
2372
  .closed:
3545 hidnplayr 2373
        popa
2374
        jmp     .next_socket_test
2375
 
2376
  .done:
2377
        pop     ebx
2378
 
3647 hidnplayr 2379
        pusha
2380
        mov     ecx, socket_mutex
2381
        call    mutex_unlock
2382
        popa
2383
 
4056 hidnplayr 2384
  .quickret:
3545 hidnplayr 2385
        ret
2386
 
2387
 
2388
 
2389
 
5976 hidnplayr 2390
;-----------------------------------------------------------------;
2391
;                                                                 ;
6011 hidnplayr 2392
; socket_is_connecting: Update socket state.                      ;
5976 hidnplayr 2393
;                                                                 ;
2394
;  IN:  eax = socket ptr                                          ;
2395
;                                                                 ;
2396
;  OUT: /                                                         ;
2397
;                                                                 ;
2398
;-----------------------------------------------------------------;
3545 hidnplayr 2399
align 4
6011 hidnplayr 2400
socket_is_connecting:
3545 hidnplayr 2401
 
3556 hidnplayr 2402
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_connecting: %x\n", eax
3545 hidnplayr 2403
 
4025 hidnplayr 2404
        and     [eax + SOCKET.state], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING)
2405
        or      [eax + SOCKET.state], SS_ISCONNECTING
2406
        ret
3545 hidnplayr 2407
 
2408
 
2409
 
5976 hidnplayr 2410
;-----------------------------------------------------------------;
2411
;                                                                 ;
6011 hidnplayr 2412
; socket_is_connected: Update socket state.                       ;
5976 hidnplayr 2413
;                                                                 ;
2414
;  IN:  eax = socket ptr                                          ;
2415
;                                                                 ;
2416
;  OUT: /                                                         ;
2417
;                                                                 ;
2418
;-----------------------------------------------------------------;
3545 hidnplayr 2419
align 4
6011 hidnplayr 2420
socket_is_connected:
3545 hidnplayr 2421
 
3556 hidnplayr 2422
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_connected: %x\n", eax
3545 hidnplayr 2423
 
3674 hidnplayr 2424
        and     [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
2425
        or      [eax + SOCKET.state], SS_ISCONNECTED
6011 hidnplayr 2426
        jmp     socket_notify
3545 hidnplayr 2427
 
2428
 
2429
 
2430
 
5976 hidnplayr 2431
;-----------------------------------------------------------------;
2432
;                                                                 ;
6011 hidnplayr 2433
; socket_is_disconnecting: Update socket state.                   ;
5976 hidnplayr 2434
;                                                                 ;
2435
;  IN:  eax = socket ptr                                          ;
2436
;                                                                 ;
2437
;  OUT: /                                                         ;
2438
;                                                                 ;
2439
;-----------------------------------------------------------------;
3545 hidnplayr 2440
align 4
6011 hidnplayr 2441
socket_is_disconnecting:
3545 hidnplayr 2442
 
3556 hidnplayr 2443
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnecting: %x\n", eax
3545 hidnplayr 2444
 
3674 hidnplayr 2445
        and     [eax + SOCKET.state], not (SS_ISCONNECTING)
2446
        or      [eax + SOCKET.state], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE
6011 hidnplayr 2447
        jmp     socket_notify
3545 hidnplayr 2448
 
2449
 
2450
 
5976 hidnplayr 2451
;-----------------------------------------------------------------;
2452
;                                                                 ;
6011 hidnplayr 2453
; socket_is_disconnected: Update socket state.                    ;
5976 hidnplayr 2454
;                                                                 ;
2455
;  IN:  eax = socket ptr                                          ;
2456
;                                                                 ;
2457
;  OUT: /                                                         ;
2458
;                                                                 ;
2459
;-----------------------------------------------------------------;
3545 hidnplayr 2460
align 4
6011 hidnplayr 2461
socket_is_disconnected:
3545 hidnplayr 2462
 
3556 hidnplayr 2463
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnected: %x\n", eax
3545 hidnplayr 2464
 
3674 hidnplayr 2465
        and     [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING)
2466
        or      [eax + SOCKET.state], SS_CANTRCVMORE + SS_CANTSENDMORE
6011 hidnplayr 2467
        jmp     socket_notify
3545 hidnplayr 2468
 
2469
 
2470
 
5976 hidnplayr 2471
;-----------------------------------------------------------------;
2472
;                                                                 ;
6011 hidnplayr 2473
; socket_cant_recv_more: Update socket state.                     ;
5976 hidnplayr 2474
;                                                                 ;
2475
;  IN:  eax = socket ptr                                          ;
2476
;                                                                 ;
2477
;  OUT: /                                                         ;
2478
;                                                                 ;
2479
;-----------------------------------------------------------------;
3545 hidnplayr 2480
align 4
6011 hidnplayr 2481
socket_cant_recv_more:
3545 hidnplayr 2482
 
3556 hidnplayr 2483
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_cant_recv_more: %x\n", eax
3545 hidnplayr 2484
 
3674 hidnplayr 2485
        or      [eax + SOCKET.state], SS_CANTRCVMORE
6011 hidnplayr 2486
        jmp     socket_notify
3545 hidnplayr 2487
 
3565 hidnplayr 2488
 
3545 hidnplayr 2489
 
5976 hidnplayr 2490
;-----------------------------------------------------------------;
2491
;                                                                 ;
6011 hidnplayr 2492
; socket_cant_send_more: Update socket state.                     ;
5976 hidnplayr 2493
;                                                                 ;
2494
;  IN:  eax = socket ptr                                          ;
2495
;                                                                 ;
2496
;  OUT: /                                                         ;
2497
;                                                                 ;
2498
;-----------------------------------------------------------------;
3545 hidnplayr 2499
align 4
6011 hidnplayr 2500
socket_cant_send_more:
3545 hidnplayr 2501
 
3556 hidnplayr 2502
        DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_cant_send_more: %x\n", eax
3545 hidnplayr 2503
 
3674 hidnplayr 2504
        or      [eax + SOCKET.state], SS_CANTSENDMORE
3658 hidnplayr 2505
        mov     [eax + SOCKET.snd_proc], .notconn
6011 hidnplayr 2506
        jmp     socket_notify
3545 hidnplayr 2507
 
3658 hidnplayr 2508
  .notconn:
3673 hidnplayr 2509
        mov     dword[esp+20], ENOTCONN
3658 hidnplayr 2510
        mov     dword[esp+32], -1
5356 serge 2511
        ret