Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
1159 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2362 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved.    ;;
1159 hidnplayr 4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
1529 hidnplayr 6
;;    Written by hidnplayr@kolibrios.org,                          ;;
7
;;     and Clevermouse.                                            ;;
1159 hidnplayr 8
;;                                                                 ;;
1529 hidnplayr 9
;;       Based on code by mike.dld                                 ;;
1159 hidnplayr 10
;;                                                                 ;;
1529 hidnplayr 11
;;         GNU GENERAL PUBLIC LICENSE                              ;;
12
;;          Version 2, June 1991                                   ;;
13
;;                                                                 ;;
1159 hidnplayr 14
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15
 
1206 hidnplayr 16
$Revision: 2572 $
1159 hidnplayr 17
 
1773 hidnplayr 18
 
2402 hidnplayr 19
struct  SOCKET
1249 hidnplayr 20
 
2402 hidnplayr 21
        NextPtr                 dd ? ; pointer to next socket in list
22
        PrevPtr                 dd ? ; pointer to previous socket in list
23
        Number                  dd ? ; socket number
1249 hidnplayr 24
 
2402 hidnplayr 25
        mutex                   MUTEX
1249 hidnplayr 26
 
2402 hidnplayr 27
        PID                     dd ? ; application process id
28
        Domain                  dd ? ; INET/UNIX/..
29
        Type                    dd ? ; RAW/STREAM/DGRAP
30
        Protocol                dd ? ; ICMP/IPv4/ARP/TCP/UDP
31
        errorcode               dd ?
1249 hidnplayr 32
 
2402 hidnplayr 33
        options                 dd ?
34
        state                   dd ?
35
        backlog                 dw ? ; how many incomming connections that can be queued
1249 hidnplayr 36
 
2402 hidnplayr 37
        snd_proc                dd ?
38
        rcv_proc                dd ?
1536 hidnplayr 39
 
2305 hidnplayr 40
ends
1249 hidnplayr 41
 
2402 hidnplayr 42
struct  IP_SOCKET               SOCKET
1254 hidnplayr 43
 
2402 hidnplayr 44
        LocalIP                 rd 4
45
        RemoteIP                rd 4
1318 hidnplayr 46
 
2305 hidnplayr 47
ends
1159 hidnplayr 48
 
2402 hidnplayr 49
struct  TCP_SOCKET              IP_SOCKET
1249 hidnplayr 50
 
2402 hidnplayr 51
        LocalPort               dw ?
52
        RemotePort              dw ?
1514 hidnplayr 53
 
2402 hidnplayr 54
        t_state                 dd ? ; TCB state
55
        t_rxtshift              dd ?
56
        t_rxtcur                dd ?
57
        t_dupacks               dd ?
58
        t_maxseg                dd ?
59
        t_force                 dd ?
60
        t_flags                 dd ?
1514 hidnplayr 61
 
62
;---------------
63
; RFC783 page 21
64
 
65
; send sequence
2402 hidnplayr 66
        SND_UNA                 dd ? ; sequence number of unack'ed sent Packets
67
        SND_NXT                 dd ? ; next send sequence number to use
68
        SND_UP                  dd ?
69
        SND_WL1                 dd ? ; window minus one
70
        SND_WL2                 dd ? ;
71
        ISS                     dd ? ; initial send sequence number
72
        SND_WND                 dd ? ; send window
1514 hidnplayr 73
 
74
; receive sequence
2402 hidnplayr 75
        RCV_WND                 dw ? ; receive window
76
        RCV_NXT                 dd ? ; next receive sequence number to use
77
        RCV_UP                  dd ?
78
        IRS                     dd ? ; initial receive sequence number
1514 hidnplayr 79
 
80
;---------------------
81
; Additional variables
82
 
83
; receive variables
2402 hidnplayr 84
        RCV_ADV                 dd ?
1514 hidnplayr 85
 
86
; retransmit variables
2402 hidnplayr 87
        SND_MAX                 dd ?
1514 hidnplayr 88
 
89
; congestion control
2402 hidnplayr 90
        SND_CWND                dd ?
91
        SND_SSTHRESH            dd ?
1514 hidnplayr 92
 
93
;----------------------
94
; Transmit timing stuff
2402 hidnplayr 95
        t_idle                  dd ?
96
        t_rtt                   dd ?
97
        t_rtseq                 dd ?
98
        t_srtt                  dd ?
99
        t_rttvar                dd ?
100
        t_rttmin                dd ?
101
        max_sndwnd              dd ?
1514 hidnplayr 102
 
103
;-----------------
104
; Out-of-band data
2402 hidnplayr 105
        t_oobflags              dd ?
106
        t_iobc                  dd ?
107
        t_softerror             dd ?
1514 hidnplayr 108
 
109
 
110
;---------
2310 hidnplayr 111
; RFC 1323                           ; the order of next 4 elements may not change
1514 hidnplayr 112
 
2402 hidnplayr 113
        SND_SCALE               db ?
114
        RCV_SCALE               db ?
115
        requested_s_scale       db ?
116
        request_r_scale         db ?
1514 hidnplayr 117
 
2402 hidnplayr 118
        ts_recent               dd ?
119
        ts_recent_age           dd ?
120
        last_ack_sent           dd ?
1519 hidnplayr 121
 
2310 hidnplayr 122
 
1519 hidnplayr 123
;-------
124
; Timers
2402 hidnplayr 125
        timer_retransmission    dw ? ; rexmt
126
        timer_persist           dw ?
127
        timer_keepalive         dw ? ; keepalive/syn timeout
128
        timer_timed_wait        dw ? ; also used as 2msl timer
1519 hidnplayr 129
 
2305 hidnplayr 130
ends
1249 hidnplayr 131
 
2402 hidnplayr 132
struct  UDP_SOCKET              IP_SOCKET
1249 hidnplayr 133
 
2402 hidnplayr 134
        LocalPort               dw ?
135
        RemotePort              dw ?
136
        firstpacket             db ?
1249 hidnplayr 137
 
2305 hidnplayr 138
ends
1249 hidnplayr 139
 
140
 
2402 hidnplayr 141
struct  ICMP_SOCKET             IP_SOCKET
1249 hidnplayr 142
 
2402 hidnplayr 143
        Identifier              dw ?
1514 hidnplayr 144
 
2305 hidnplayr 145
ends
1514 hidnplayr 146
 
147
 
2402 hidnplayr 148
struct  RING_BUFFER
2311 hidnplayr 149
 
2402 hidnplayr 150
        start_ptr               dd ? ; Pointer to start of buffer
151
        end_ptr                 dd ? ; pointer to end of buffer
152
        read_ptr                dd ? ; Read pointer
153
        write_ptr               dd ? ; Write pointer
154
        size                    dd ? ; Number of bytes buffered
2311 hidnplayr 155
 
2305 hidnplayr 156
ends
1514 hidnplayr 157
 
2402 hidnplayr 158
struct  STREAM_SOCKET           TCP_SOCKET
1529 hidnplayr 159
 
2402 hidnplayr 160
        rcv                     RING_BUFFER
161
        snd                     RING_BUFFER
1529 hidnplayr 162
 
2305 hidnplayr 163
ends
1529 hidnplayr 164
 
2402 hidnplayr 165
struct  socket_queue_entry
1529 hidnplayr 166
 
2402 hidnplayr 167
        data_ptr                dd ?
168
        buf_ptr                 dd ?
169
        data_size               dd ?
1529 hidnplayr 170
 
1274 hidnplayr 171
ends
172
 
1514 hidnplayr 173
 
2402 hidnplayr 174
SOCKETBUFFSIZE          equ 4096     ; in bytes
1514 hidnplayr 175
 
2402 hidnplayr 176
SOCKET_QUEUE_SIZE       equ 10       ; maximum number ofincoming packets queued for 1 socket
1514 hidnplayr 177
; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start
2402 hidnplayr 178
SOCKET_QUEUE_LOCATION   equ (SOCKETBUFFSIZE - SOCKET_QUEUE_SIZE*sizeof.socket_queue_entry - sizeof.queue)
1249 hidnplayr 179
 
1159 hidnplayr 180
uglobal
2402 hidnplayr 181
        net_sockets     rd 4
182
        last_socket_num dd ?
183
        last_UDP_port   dw ? ; These values give the number of the last used ephemeral port
184
        last_TCP_port   dw ? ;
1159 hidnplayr 185
endg
186
 
187
 
1257 hidnplayr 188
;-----------------------------------------------------------------
1159 hidnplayr 189
;
190
; SOCKET_init
191
;
1257 hidnplayr 192
;-----------------------------------------------------------------
2402 hidnplayr 193
macro   SOCKET_init {
1159 hidnplayr 194
 
2402 hidnplayr 195
        xor     eax, eax
196
        mov     edi, net_sockets
197
        mov     ecx, 5
198
        rep     stosd
1159 hidnplayr 199
 
1529 hidnplayr 200
       @@:
2402 hidnplayr 201
        pseudo_random eax
202
        cmp     ax, MIN_EPHEMERAL_PORT
203
        jb      @r
204
        cmp     ax, MAX_EPHEMERAL_PORT
205
        ja      @r
206
        mov     [last_UDP_port], ax
1529 hidnplayr 207
 
208
       @@:
2402 hidnplayr 209
        pseudo_random eax
210
        cmp     ax, MIN_EPHEMERAL_PORT
211
        jb      @r
212
        cmp     ax, MAX_EPHEMERAL_PORT
213
        ja      @r
214
        mov     [last_TCP_port], ax
1529 hidnplayr 215
 
216
}
217
 
218
 
1257 hidnplayr 219
;-----------------------------------------------------------------
1159 hidnplayr 220
;
221
; Socket API (function 74)
222
;
1257 hidnplayr 223
;-----------------------------------------------------------------
2301 hidnplayr 224
align 16
225
sock_sysfn_table:
2402 hidnplayr 226
        dd      SOCKET_open     ; 0
227
        dd      SOCKET_close    ; 1
228
        dd      SOCKET_bind     ; 2
229
        dd      SOCKET_listen   ; 3
230
        dd      SOCKET_connect  ; 4
231
        dd      SOCKET_accept   ; 5
232
        dd      SOCKET_send     ; 6
233
        dd      SOCKET_receive  ; 7
234
        dd      SOCKET_set_opt  ; 8
235
        dd      SOCKET_get_opt  ; 9
2301 hidnplayr 236
 
237
SOCKET_SYSFUNCS = ($ - sock_sysfn_table)/4
238
 
239
 
1159 hidnplayr 240
align 4
241
sys_socket:
2402 hidnplayr 242
        cmp     ebx, SOCKET_SYSFUNCS-1
243
        ja      @f
244
        jmp     dword [sock_sysfn_table + 4*ebx]
1529 hidnplayr 245
       @@:
2402 hidnplayr 246
        cmp     ebx, 255
247
        jz      SOCKET_debug
1159 hidnplayr 248
 
1529 hidnplayr 249
s_error:
2402 hidnplayr 250
        DEBUGF  1,"socket error\n"
251
        mov     dword [esp+32], -1
1529 hidnplayr 252
 
2402 hidnplayr 253
        ret
1529 hidnplayr 254
 
1159 hidnplayr 255
 
2301 hidnplayr 256
 
1257 hidnplayr 257
;-----------------------------------------------------------------
1159 hidnplayr 258
;
259
; SOCKET_open
260
;
261
;  IN:  domain in ecx
262
;       type in edx
1196 hidnplayr 263
;       protocol in esi
1159 hidnplayr 264
;  OUT: eax is socket num, -1 on error
265
;
1257 hidnplayr 266
;-----------------------------------------------------------------
1206 hidnplayr 267
align 4
1514 hidnplayr 268
SOCKET_open:
1159 hidnplayr 269
 
2402 hidnplayr 270
        DEBUGF  1,"SOCKET_open: domain: %u, type: %u protocol: %x\n", ecx, edx, esi
1159 hidnplayr 271
 
2402 hidnplayr 272
        push    ecx edx esi
273
        call    SOCKET_alloc
274
        pop     esi edx ecx
275
        jz      s_error
1159 hidnplayr 276
 
2402 hidnplayr 277
        mov     [esp+32], edi                   ; return socketnumber
1542 hidnplayr 278
 
2402 hidnplayr 279
        mov     [eax + SOCKET.Domain], ecx
280
        mov     [eax + SOCKET.Type], edx
281
        mov     [eax + SOCKET.Protocol], esi
1159 hidnplayr 282
 
2402 hidnplayr 283
        cmp     ecx, AF_INET4
284
        jne     .no_inet4
1529 hidnplayr 285
 
2402 hidnplayr 286
        cmp     edx, SOCK_DGRAM
287
        je      .udp
1536 hidnplayr 288
 
2402 hidnplayr 289
        cmp     edx, SOCK_STREAM
290
        je      .tcp
1529 hidnplayr 291
 
2402 hidnplayr 292
        cmp     edx, SOCK_RAW
293
        je      .raw
1541 hidnplayr 294
 
1536 hidnplayr 295
  .no_inet4:
2402 hidnplayr 296
        ret
1536 hidnplayr 297
 
1542 hidnplayr 298
align 4
299
  .raw:
2402 hidnplayr 300
        test    esi, esi       ; IP_PROTO_IP
301
        jz      .ip
1529 hidnplayr 302
 
2402 hidnplayr 303
        cmp     esi, IP_PROTO_ICMP
304
        je      .icmp
1529 hidnplayr 305
 
2402 hidnplayr 306
        cmp     esi, IP_PROTO_UDP
307
        je      .udp
1533 hidnplayr 308
 
2402 hidnplayr 309
        cmp     esi, IP_PROTO_TCP
310
        je      .tcp
1536 hidnplayr 311
 
2402 hidnplayr 312
        ret
1159 hidnplayr 313
 
1542 hidnplayr 314
align 4
1536 hidnplayr 315
  .udp:
2402 hidnplayr 316
        mov     [eax + SOCKET.Protocol], IP_PROTO_UDP
317
        mov     [eax + SOCKET.snd_proc], SOCKET_send_udp
318
        mov     [eax + SOCKET.rcv_proc], SOCKET_receive_dgram
319
        ret
1529 hidnplayr 320
 
1542 hidnplayr 321
align 4
322
  .tcp:
2402 hidnplayr 323
        mov     [eax + SOCKET.Protocol], IP_PROTO_TCP
324
        mov     [eax + SOCKET.snd_proc], SOCKET_send_tcp
325
        mov     [eax + SOCKET.rcv_proc], SOCKET_receive_tcp
1838 hidnplayr 326
 
2402 hidnplayr 327
        mov     [eax + TCP_SOCKET.t_maxseg], 1480       ;;;;; FIXME
328
        ret
1529 hidnplayr 329
 
330
 
1543 hidnplayr 331
align 4
332
  .ip:
2402 hidnplayr 333
        mov     [eax + SOCKET.snd_proc], SOCKET_send_ip
334
        mov     [eax + SOCKET.rcv_proc], SOCKET_receive_dgram
335
        ret
1541 hidnplayr 336
 
1542 hidnplayr 337
 
338
align 4
1541 hidnplayr 339
  .icmp:
2402 hidnplayr 340
        mov     [eax + SOCKET.snd_proc], SOCKET_send_icmp
341
        mov     [eax + SOCKET.rcv_proc], SOCKET_receive_dgram
342
        ret
1541 hidnplayr 343
 
344
 
345
 
1257 hidnplayr 346
;-----------------------------------------------------------------
1159 hidnplayr 347
;
348
; SOCKET_bind
349
;
350
;  IN:  socket number in ecx
351
;       pointer to sockaddr struct in edx
352
;       length of that struct in esi
353
;  OUT: 0 on success
354
;
1257 hidnplayr 355
;-----------------------------------------------------------------
1206 hidnplayr 356
align 4
1514 hidnplayr 357
SOCKET_bind:
1159 hidnplayr 358
 
2402 hidnplayr 359
        DEBUGF  1,"socket_bind: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
1159 hidnplayr 360
 
2402 hidnplayr 361
        call    SOCKET_num_to_ptr
362
        jz      s_error
1159 hidnplayr 363
 
2402 hidnplayr 364
        cmp     esi, 2
365
        jb      s_error
1159 hidnplayr 366
 
2402 hidnplayr 367
        cmp     word [edx], AF_INET4
368
        je      .af_inet4
1159 hidnplayr 369
 
2402 hidnplayr 370
        cmp     word [edx], AF_UNIX
371
        je      .af_unix
1249 hidnplayr 372
 
2402 hidnplayr 373
        jmp     s_error
1249 hidnplayr 374
 
375
  .af_unix:
2402 hidnplayr 376
        ; TODO: write code here
1249 hidnplayr 377
 
2402 hidnplayr 378
        mov     dword [esp+32], 0
379
        ret
1249 hidnplayr 380
 
1159 hidnplayr 381
  .af_inet4:
382
 
2402 hidnplayr 383
        DEBUGF  1,"af_inet4\n"
1514 hidnplayr 384
 
2402 hidnplayr 385
        cmp     esi, 6
386
        jb      s_error
1159 hidnplayr 387
 
2402 hidnplayr 388
        pushw   [edx + 2]
389
        pop     [eax + UDP_SOCKET.LocalPort]
1159 hidnplayr 390
 
2402 hidnplayr 391
        pushd   [edx + 4]
392
        pop     [eax + IP_SOCKET.LocalIP]
1159 hidnplayr 393
 
2402 hidnplayr 394
        DEBUGF  1,"local ip: %u.%u.%u.%u\n",\
395
        [eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\
396
        [eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1
1159 hidnplayr 397
 
2402 hidnplayr 398
        mov     dword [esp+32], 0
399
        ret
1159 hidnplayr 400
 
401
 
402
 
403
 
1257 hidnplayr 404
;-----------------------------------------------------------------
1159 hidnplayr 405
;
406
; SOCKET_connect
407
;
408
;  IN:  socket number in ecx
409
;       pointer to sockaddr struct in edx
410
;       length of that struct in esi
411
;  OUT: 0 on success
412
;
1257 hidnplayr 413
;-----------------------------------------------------------------
1159 hidnplayr 414
align 4
1514 hidnplayr 415
SOCKET_connect:
1159 hidnplayr 416
 
2402 hidnplayr 417
        DEBUGF  1,"SOCKET_connect: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
1159 hidnplayr 418
 
2402 hidnplayr 419
        call    SOCKET_num_to_ptr
420
        jz      s_error
1159 hidnplayr 421
 
2402 hidnplayr 422
        cmp     esi, 8
423
        jb      s_error
1159 hidnplayr 424
 
2402 hidnplayr 425
        cmp     word [edx], AF_INET4
426
        je      .af_inet4
1159 hidnplayr 427
 
2402 hidnplayr 428
        jmp     s_error
1159 hidnplayr 429
 
430
  .af_inet4:
2402 hidnplayr 431
        cmp     [eax + IP_SOCKET.LocalIP], 0
432
        jne     @f
433
        push    [IP_LIST]
434
        pop     [eax + IP_SOCKET.LocalIP]
1543 hidnplayr 435
       @@:
1159 hidnplayr 436
 
2402 hidnplayr 437
        cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
438
        je      .udp
1159 hidnplayr 439
 
2402 hidnplayr 440
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
441
        je      .tcp
1159 hidnplayr 442
 
2402 hidnplayr 443
        cmp     [eax + SOCKET.Protocol], IP_PROTO_IP
444
        je      .ip
1541 hidnplayr 445
 
2402 hidnplayr 446
        cmp     [eax + SOCKET.Protocol], IP_PROTO_ICMP
447
        je      .ip
1542 hidnplayr 448
 
2402 hidnplayr 449
        jmp     s_error
1159 hidnplayr 450
 
1542 hidnplayr 451
align 4
1254 hidnplayr 452
  .udp:
2402 hidnplayr 453
        pusha
454
        lea     ecx, [eax + SOCKET.mutex]
455
        call    mutex_lock
456
        popa
1543 hidnplayr 457
 
2402 hidnplayr 458
        pushw   [edx + 2]
459
        pop     [eax + UDP_SOCKET.RemotePort]
1543 hidnplayr 460
 
2402 hidnplayr 461
        pushd   [edx + 4]
462
        pop     [eax + IP_SOCKET.RemoteIP]
1543 hidnplayr 463
 
2402 hidnplayr 464
        cmp     [eax + UDP_SOCKET.LocalPort], 0
465
        jne     @f
466
        call    SOCKET_find_port
1543 hidnplayr 467
       @@:
468
 
2402 hidnplayr 469
        mov     [eax + UDP_SOCKET.firstpacket], 0
1159 hidnplayr 470
 
2402 hidnplayr 471
        push    eax
472
        init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
473
        pop     eax
1159 hidnplayr 474
 
2402 hidnplayr 475
        lea     ecx, [eax + SOCKET.mutex]
476
        call    mutex_unlock
1159 hidnplayr 477
 
2402 hidnplayr 478
        mov     dword [esp+32], 0
479
        ret
480
 
1542 hidnplayr 481
align 4
1254 hidnplayr 482
  .tcp:
2402 hidnplayr 483
        pusha
484
        lea     ecx, [eax + SOCKET.mutex]
485
        call    mutex_lock
486
        popa
1159 hidnplayr 487
 
2402 hidnplayr 488
        pushw   [edx + 2]
489
        pop     [eax + TCP_SOCKET.RemotePort]
1159 hidnplayr 490
 
2402 hidnplayr 491
        pushd   [edx + 4]
492
        pop     [eax + IP_SOCKET.RemoteIP]
1159 hidnplayr 493
 
2402 hidnplayr 494
        cmp     [eax + TCP_SOCKET.LocalPort], 0
495
        jne     @f
496
        call    SOCKET_find_port
1543 hidnplayr 497
       @@:
1159 hidnplayr 498
 
2402 hidnplayr 499
        mov     [eax + TCP_SOCKET.timer_persist], 0
500
        mov     [eax + TCP_SOCKET.t_state], TCPS_SYN_SENT
501
        push    [TCP_sequence_num]
502
        add     [TCP_sequence_num], 6400
503
        pop     [eax + TCP_SOCKET.ISS]
504
        mov     [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_init
1519 hidnplayr 505
 
2402 hidnplayr 506
        TCP_sendseqinit eax
1529 hidnplayr 507
 
1543 hidnplayr 508
;        mov     [ebx + TCP_SOCKET.timer_retransmission],   ;; todo: create macro to set retransmission timer
1533 hidnplayr 509
 
2402 hidnplayr 510
        mov     ebx, eax
1159 hidnplayr 511
 
2402 hidnplayr 512
        lea     eax, [ebx + STREAM_SOCKET.snd]
513
        call    SOCKET_ring_create
1543 hidnplayr 514
 
2402 hidnplayr 515
        lea     eax, [ebx + STREAM_SOCKET.rcv]
516
        call    SOCKET_ring_create
1543 hidnplayr 517
 
2402 hidnplayr 518
        pusha
2403 hidnplayr 519
        lea     ecx, [eax + SOCKET.mutex]
2402 hidnplayr 520
        call    mutex_unlock
521
        popa
1733 hidnplayr 522
 
2402 hidnplayr 523
        mov     eax, ebx
524
        call    TCP_output
1733 hidnplayr 525
 
2402 hidnplayr 526
        mov     dword [esp+32], 0
527
        ret
1159 hidnplayr 528
 
1542 hidnplayr 529
align 4
530
  .ip:
2402 hidnplayr 531
        pusha
532
        lea     ecx, [eax + SOCKET.mutex]
533
        call    mutex_lock
534
        popa
1543 hidnplayr 535
 
2402 hidnplayr 536
        pushd   [edx + 4]
537
        pop     [eax + IP_SOCKET.RemoteIP]
1159 hidnplayr 538
 
2402 hidnplayr 539
        push    eax
540
        init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
541
        pop     eax
1543 hidnplayr 542
 
2402 hidnplayr 543
        lea     ecx, [eax + SOCKET.mutex]
544
        call    mutex_unlock
1541 hidnplayr 545
 
2402 hidnplayr 546
        mov     dword [esp+32], 0
547
        ret
1541 hidnplayr 548
 
2402 hidnplayr 549
 
1257 hidnplayr 550
;-----------------------------------------------------------------
1159 hidnplayr 551
;
552
; SOCKET_listen
553
;
554
;  IN:  socket number in ecx
555
;       backlog in edx
556
;  OUT: eax is socket num, -1 on error
557
;
1257 hidnplayr 558
;-----------------------------------------------------------------
1206 hidnplayr 559
align 4
1514 hidnplayr 560
SOCKET_listen:
1159 hidnplayr 561
 
2402 hidnplayr 562
        DEBUGF  1,"SOCKET_listen: socknum: %u backlog: %u\n", ecx, edx
1159 hidnplayr 563
 
2402 hidnplayr 564
        call    SOCKET_num_to_ptr
565
        jz      s_error
1159 hidnplayr 566
 
2402 hidnplayr 567
        cmp     [eax + SOCKET.Domain], AF_INET4
568
        jne     s_error
1254 hidnplayr 569
 
2402 hidnplayr 570
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
571
        jne     s_error
1254 hidnplayr 572
 
2402 hidnplayr 573
        cmp     [eax + TCP_SOCKET.LocalPort], 0
574
        je      s_error
1514 hidnplayr 575
 
2402 hidnplayr 576
        cmp     [eax + IP_SOCKET.LocalIP], 0
577
        jne     @f
578
        push    [IP_LIST]
579
        pop     [eax + IP_SOCKET.LocalIP]
1543 hidnplayr 580
       @@:
581
 
2402 hidnplayr 582
        cmp     edx, MAX_backlog
583
        jbe     @f
584
        mov     edx, MAX_backlog
1543 hidnplayr 585
       @@:
1159 hidnplayr 586
 
2402 hidnplayr 587
        mov     [eax + SOCKET.backlog], dx
588
        or      [eax + SOCKET.options], SO_ACCEPTCON
589
        mov     [eax + TCP_SOCKET.t_state], TCPS_LISTEN
1159 hidnplayr 590
 
2402 hidnplayr 591
        push    eax
592
        init_queue (eax + SOCKET_QUEUE_LOCATION)                ; Set up sockets queue
593
        pop     eax
1543 hidnplayr 594
 
2402 hidnplayr 595
        mov     dword [esp+32], 0
1514 hidnplayr 596
 
2402 hidnplayr 597
        ret
1159 hidnplayr 598
 
599
 
1257 hidnplayr 600
;-----------------------------------------------------------------
1159 hidnplayr 601
;
602
; SOCKET_accept
603
;
604
;  IN:  socket number in ecx
605
;       addr in edx
606
;       addrlen in esi
607
;  OUT: eax is socket num, -1 on error
608
;
1257 hidnplayr 609
;-----------------------------------------------------------------
1206 hidnplayr 610
align 4
1514 hidnplayr 611
SOCKET_accept:
1159 hidnplayr 612
 
2402 hidnplayr 613
        DEBUGF  1,"SOCKET_accept: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
1159 hidnplayr 614
 
2402 hidnplayr 615
        call    SOCKET_num_to_ptr
616
        jz      s_error
1159 hidnplayr 617
 
2402 hidnplayr 618
        test    [eax + SOCKET.options], SO_ACCEPTCON
619
        jz      s_error
1543 hidnplayr 620
 
2402 hidnplayr 621
        cmp     [eax + SOCKET.Domain], AF_INET4
622
        jne     s_error
1249 hidnplayr 623
 
2402 hidnplayr 624
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
625
        jne     s_error
1249 hidnplayr 626
 
2402 hidnplayr 627
        get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, s_error
2572 hidnplayr 628
        mov     eax, [esi]
1249 hidnplayr 629
 
2572 hidnplayr 630
; Change PID to that of the current process
631
        mov     ebx, [TASK_BASE]
632
        mov     ebx, [ebx + TASKDATA.pid]
633
        mov     [eax + SOCKET.PID], ebx
634
 
2402 hidnplayr 635
        call    SOCKET_ptr_to_num
636
        jz      s_error
637
        mov     [esp+32], eax
638
        ret
1514 hidnplayr 639
 
1159 hidnplayr 640
 
1257 hidnplayr 641
;-----------------------------------------------------------------
1159 hidnplayr 642
;
643
; SOCKET_close
644
;
645
;  IN:  socket number in ecx
646
;  OUT: eax is socket num, -1 on error
647
;
1257 hidnplayr 648
;-----------------------------------------------------------------
1206 hidnplayr 649
align 4
1514 hidnplayr 650
SOCKET_close:
1159 hidnplayr 651
 
2402 hidnplayr 652
        DEBUGF  1,"SOCKET_close: socknum: %u\n", ecx
1159 hidnplayr 653
 
2402 hidnplayr 654
        call    SOCKET_num_to_ptr
655
        jz      s_error
1159 hidnplayr 656
 
2402 hidnplayr 657
        cmp     [eax + SOCKET.Domain], AF_INET4
658
        jne     s_error
1159 hidnplayr 659
 
2402 hidnplayr 660
        cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
661
        je      .free
1159 hidnplayr 662
 
2402 hidnplayr 663
        cmp     [eax + SOCKET.Protocol], IP_PROTO_ICMP
664
        je      .free
1159 hidnplayr 665
 
2402 hidnplayr 666
        cmp     [eax + SOCKET.Protocol], IP_PROTO_IP
667
        je      .free
1542 hidnplayr 668
 
2402 hidnplayr 669
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
670
        je      .tcp
1159 hidnplayr 671
 
2402 hidnplayr 672
        jmp     s_error
1159 hidnplayr 673
 
674
  .tcp:
2402 hidnplayr 675
        cmp     [eax + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED    ; state must be LISTEN, SYN_SENT or CLOSED
676
        jb      .free
1318 hidnplayr 677
 
2541 hidnplayr 678
        call    TCP_drop
2402 hidnplayr 679
        mov     dword [esp+32], 0
1159 hidnplayr 680
 
2402 hidnplayr 681
        ret
1159 hidnplayr 682
 
1514 hidnplayr 683
  .free:
2402 hidnplayr 684
        call    SOCKET_free
685
        mov     dword [esp+32], 0
1159 hidnplayr 686
 
2402 hidnplayr 687
        ret
1159 hidnplayr 688
 
689
 
1257 hidnplayr 690
;-----------------------------------------------------------------
1159 hidnplayr 691
;
692
; SOCKET_receive
693
;
694
;  IN:  socket number in ecx
1249 hidnplayr 695
;       addr to buffer in edx
696
;       length of buffer in esi
1159 hidnplayr 697
;       flags in edi
698
;  OUT: eax is number of bytes copied, -1 on error
699
;
1257 hidnplayr 700
;-----------------------------------------------------------------
1206 hidnplayr 701
align 4
1514 hidnplayr 702
SOCKET_receive:
1159 hidnplayr 703
 
2402 hidnplayr 704
        DEBUGF  1,"SOCKET_receive: socknum: %u bufaddr: %x, buflength: %u, flags: %x, ", ecx, edx, esi, edi
1514 hidnplayr 705
 
2402 hidnplayr 706
        call    SOCKET_num_to_ptr
707
        jz      s_error
1159 hidnplayr 708
 
2402 hidnplayr 709
        jmp     [eax + SOCKET.rcv_proc]
1533 hidnplayr 710
 
1536 hidnplayr 711
 
712
align 4
1541 hidnplayr 713
SOCKET_receive_dgram:
1536 hidnplayr 714
 
2402 hidnplayr 715
        DEBUGF  1,"SOCKET_receive: DGRAM\n"
1536 hidnplayr 716
 
2402 hidnplayr 717
        mov     ebx, esi
718
        mov     edi, edx                                        ; addr to buffer
1159 hidnplayr 719
 
2402 hidnplayr 720
        get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, s_error       ; destroys esi and ecx
1536 hidnplayr 721
 
2402 hidnplayr 722
        mov     ecx, [esi + socket_queue_entry.data_size]
723
        DEBUGF  1,"Got %u bytes of data\n", ecx
1159 hidnplayr 724
 
2402 hidnplayr 725
        cmp     ecx, ebx
726
        ja      .too_small
1514 hidnplayr 727
 
2402 hidnplayr 728
        push    [esi + socket_queue_entry.buf_ptr]              ; save the buffer addr so we can clear it later
729
        mov     esi, [esi + socket_queue_entry.data_ptr]
730
        DEBUGF  1,"Source buffer: %x, real addr: %x\n", [esp], esi
731
        mov     [esp+32+4], ecx                                 ; return number of bytes copied
1159 hidnplayr 732
 
1514 hidnplayr 733
; copy the data
2402 hidnplayr 734
        shr     ecx, 1
735
        jnc     .nb
736
        movsb
1536 hidnplayr 737
  .nb:
2402 hidnplayr 738
        shr     ecx, 1
739
        jnc     .nw
740
        movsw
1536 hidnplayr 741
  .nw:
2402 hidnplayr 742
        test    ecx, ecx
743
        jz      .nd
744
        rep     movsd
1536 hidnplayr 745
  .nd:
1159 hidnplayr 746
 
2402 hidnplayr 747
        call    kernel_free                                     ; remove the packet
748
        ret
1514 hidnplayr 749
 
1536 hidnplayr 750
  .too_small:
1533 hidnplayr 751
 
2402 hidnplayr 752
        DEBUGF  1,"Buffer too small...\n"
753
        jmp     s_error
1536 hidnplayr 754
 
755
align 4
756
SOCKET_receive_tcp:
757
 
2402 hidnplayr 758
        DEBUGF  1,"SOCKET_receive: TCP\n"
1536 hidnplayr 759
 
2402 hidnplayr 760
        mov     ecx, esi
761
        mov     edi, edx
762
        add     eax, STREAM_SOCKET.rcv
763
        call    SOCKET_ring_read
764
        call    SOCKET_ring_free
1533 hidnplayr 765
 
2402 hidnplayr 766
        mov     [esp+32], ecx                                   ; return number of bytes copied
1533 hidnplayr 767
 
2402 hidnplayr 768
        ret
1159 hidnplayr 769
 
770
 
1257 hidnplayr 771
;-----------------------------------------------------------------
1159 hidnplayr 772
;
773
; SOCKET_send
774
;
775
;
776
;  IN:  socket number in ecx
1206 hidnplayr 777
;       pointer to data in edx
778
;       datalength in esi
1159 hidnplayr 779
;       flags in edi
780
;  OUT: -1 on error
781
;
1257 hidnplayr 782
;-----------------------------------------------------------------
1206 hidnplayr 783
align 4
1514 hidnplayr 784
SOCKET_send:
1159 hidnplayr 785
 
2402 hidnplayr 786
        DEBUGF  1,"SOCKET_send: socknum: %u data ptr: %x, length: %u, flags: %x, ", ecx, edx, esi, edi
1159 hidnplayr 787
 
2402 hidnplayr 788
        call    SOCKET_num_to_ptr
789
        jz      s_error
1159 hidnplayr 790
 
2402 hidnplayr 791
        mov     ecx, esi
792
        mov     esi, edx
1763 hidnplayr 793
 
2402 hidnplayr 794
        jmp     [eax + SOCKET.snd_proc]
1206 hidnplayr 795
 
796
 
1536 hidnplayr 797
align 4
798
SOCKET_send_udp:
1529 hidnplayr 799
 
2402 hidnplayr 800
        DEBUGF  1,"SOCKET_send: UDP\n"
1159 hidnplayr 801
 
2402 hidnplayr 802
        call    UDP_output
1159 hidnplayr 803
 
2402 hidnplayr 804
        mov     [esp+32], eax
805
        ret
1159 hidnplayr 806
 
807
 
1536 hidnplayr 808
align 4
809
SOCKET_send_tcp:
1254 hidnplayr 810
 
2402 hidnplayr 811
        DEBUGF  1,"SOCKET_send: TCP\n"
1254 hidnplayr 812
 
2402 hidnplayr 813
        push    eax
814
        add     eax, STREAM_SOCKET.snd
815
        call    SOCKET_ring_write
816
        pop     eax
1536 hidnplayr 817
 
2402 hidnplayr 818
        call    TCP_output
1254 hidnplayr 819
 
2402 hidnplayr 820
        mov     [esp+32], eax
821
        ret
1159 hidnplayr 822
 
1249 hidnplayr 823
 
1543 hidnplayr 824
align 4
825
SOCKET_send_ip:
1249 hidnplayr 826
 
2402 hidnplayr 827
        DEBUGF  1,"type: IP\n"
1543 hidnplayr 828
 
2402 hidnplayr 829
        call    IPv4_output_raw
1543 hidnplayr 830
 
2402 hidnplayr 831
        mov     [esp+32], eax
832
        ret
1543 hidnplayr 833
 
1541 hidnplayr 834
align 4
835
SOCKET_send_icmp:
1249 hidnplayr 836
 
2402 hidnplayr 837
        DEBUGF  1,"SOCKET_send: ICMP\n"
1541 hidnplayr 838
 
2402 hidnplayr 839
        call    ICMP_output_raw
1541 hidnplayr 840
 
2402 hidnplayr 841
        mov     [esp+32], eax
842
        ret
1541 hidnplayr 843
 
844
 
845
 
846
 
1257 hidnplayr 847
;-----------------------------------------------------------------
1256 clevermous 848
;
1257 hidnplayr 849
; SOCKET_get_options
1256 clevermous 850
;
1514 hidnplayr 851
;  IN:  ecx = socket number
852
;       edx = pointer to the options:
1257 hidnplayr 853
;               dd      level, optname, optval, optlen
1256 clevermous 854
;  OUT: -1 on error
855
;
856
; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
857
; TODO: find best way to notify that send()'ed data were acknowledged
1831 hidnplayr 858
; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*.
1256 clevermous 859
;
1257 hidnplayr 860
;-----------------------------------------------------------------
861
align 4
1514 hidnplayr 862
SOCKET_get_opt:
1257 hidnplayr 863
 
2402 hidnplayr 864
        DEBUGF  1,"SOCKET_get_opt\n"
1514 hidnplayr 865
 
2402 hidnplayr 866
        call    SOCKET_num_to_ptr
867
        jz      s_error
1514 hidnplayr 868
 
2402 hidnplayr 869
        cmp     dword [edx], IP_PROTO_TCP
870
        jne     s_error
871
        cmp     dword [edx+4], -2
872
        je      @f
873
        cmp     dword [edx+4], -3
874
        jne     s_error
1299 clevermous 875
@@:
1514 hidnplayr 876
;        mov     eax, [edx+12]
877
;        test    eax, eax
878
;        jz      .fail
879
;        cmp     dword [eax], 4
880
;        mov     dword [eax], 4
881
;        jb      .fail
882
;        stdcall net_socket_num_to_addr, ecx
883
;        test    eax, eax
884
;        jz      .fail
885
;        ; todo: check that eax is really TCP socket
886
;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
887
;        cmp     dword [edx+4], -2
888
;        jz      @f
889
;        mov     ecx, [eax + TCP_SOCKET.state]
1299 clevermous 890
@@:
2402 hidnplayr 891
        mov     eax, [edx+8]
892
        test    eax, eax
893
        jz      @f
894
        mov     [eax], ecx
1256 clevermous 895
@@:
2402 hidnplayr 896
        mov     dword [esp+32], 0
897
        ret
1159 hidnplayr 898
 
899
 
1529 hidnplayr 900
 
1542 hidnplayr 901
 
902
align 4
903
SOCKET_set_opt:
904
 
2402 hidnplayr 905
        ret
1542 hidnplayr 906
 
907
 
908
 
1257 hidnplayr 909
;-----------------------------------------------------------------
1206 hidnplayr 910
;
1529 hidnplayr 911
; SOCKET_debug
912
;
913
;  Copies socket variables to application buffer
914
;
915
;  IN:  ecx = socket number
916
;       edx = pointer to buffer
917
;
918
;  OUT: -1 on error
919
;-----------------------------------------------------------------
920
align 4
921
SOCKET_debug:
922
 
2402 hidnplayr 923
        DEBUGF  1,"socket_debug\n"
1529 hidnplayr 924
 
2402 hidnplayr 925
        call    SOCKET_num_to_ptr
926
        jz      s_error
1529 hidnplayr 927
 
2402 hidnplayr 928
        mov     esi, eax
929
        mov     edi, edx
930
        mov     ecx, SOCKETBUFFSIZE/4
931
        rep     movsd
1529 hidnplayr 932
 
2402 hidnplayr 933
        mov     dword [esp+32], 0
934
        ret
1529 hidnplayr 935
 
936
 
937
;-----------------------------------------------------------------
938
;
1514 hidnplayr 939
; SOCKET_find_port
1206 hidnplayr 940
;
1514 hidnplayr 941
; Fills in the local port number for TCP and UDP sockets
942
; This procedure always works because the number of sockets is
943
; limited to a smaller number then the number of possible ports
1206 hidnplayr 944
;
1514 hidnplayr 945
;  IN:  eax = socket pointer
946
;  OUT: /
1206 hidnplayr 947
;
1257 hidnplayr 948
;-----------------------------------------------------------------
1206 hidnplayr 949
align 4
1514 hidnplayr 950
SOCKET_find_port:
1159 hidnplayr 951
 
2402 hidnplayr 952
        DEBUGF  1,"SOCKET_find_port\n"
1159 hidnplayr 953
 
2402 hidnplayr 954
        push    ebx esi ecx
1514 hidnplayr 955
 
2402 hidnplayr 956
        cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
957
        je      .udp
1159 hidnplayr 958
 
2402 hidnplayr 959
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
960
        je      .tcp
1159 hidnplayr 961
 
2402 hidnplayr 962
        jmp     .error
1514 hidnplayr 963
 
964
  .done:
2402 hidnplayr 965
        mov     [eax + UDP_SOCKET.LocalPort], bx
1514 hidnplayr 966
  .error:
2402 hidnplayr 967
        pop     ecx esi ebx
968
        ret
1514 hidnplayr 969
 
1206 hidnplayr 970
  .udp:
2402 hidnplayr 971
        mov     bx, [last_UDP_port]
972
        call    .findit
973
        mov     [last_UDP_port], bx
974
        jmp     .done
1206 hidnplayr 975
 
976
  .tcp:
2402 hidnplayr 977
        mov     bx, [last_TCP_port]
978
        call    .findit
979
        mov     [last_TCP_port], bx
980
        jmp     .done
1206 hidnplayr 981
 
982
 
1514 hidnplayr 983
  .restart:
2402 hidnplayr 984
        mov     bx, MIN_EPHEMERAL_PORT
1514 hidnplayr 985
  .findit:
2402 hidnplayr 986
        inc     bx
1206 hidnplayr 987
 
2402 hidnplayr 988
        cmp     bx, MAX_EPHEMERAL_PORT
989
        jz      .restart
1206 hidnplayr 990
 
2402 hidnplayr 991
        call    SOCKET_check_port
992
        jz      .findit
1206 hidnplayr 993
 
2402 hidnplayr 994
        ret
1206 hidnplayr 995
 
1257 hidnplayr 996
 
997
 
998
;-----------------------------------------------------------------
1206 hidnplayr 999
;
1542 hidnplayr 1000
; SOCKET_check_port (to be used with AF_INET only!)
1206 hidnplayr 1001
;
1514 hidnplayr 1002
; Checks if a local port number is unused
1003
; If the proposed port number is unused, it is filled in in the socket structure
1206 hidnplayr 1004
;
1514 hidnplayr 1005
;  IN:  eax = socket ptr (to find out if its a TCP/UDP socket)
1006
;        bx = proposed socket number
1206 hidnplayr 1007
;
1514 hidnplayr 1008
;  OUT:  ZF = cleared on error
1009
;
1257 hidnplayr 1010
;-----------------------------------------------------------------
1206 hidnplayr 1011
align 4
1514 hidnplayr 1012
SOCKET_check_port:
1013
 
2402 hidnplayr 1014
        DEBUGF  1,"SOCKET_check_port\n"
1514 hidnplayr 1015
 
2402 hidnplayr 1016
        mov     ecx, [eax + SOCKET.Protocol]
1017
        mov     esi, net_sockets
1206 hidnplayr 1018
 
1019
  .next_socket:
2402 hidnplayr 1020
        mov     esi, [esi + SOCKET.NextPtr]
1021
        or      esi, esi
1022
        jz      .port_ok
1206 hidnplayr 1023
 
2402 hidnplayr 1024
        cmp     [esi + SOCKET.Protocol], ecx
1025
        jne     .next_socket
1206 hidnplayr 1026
 
2402 hidnplayr 1027
        cmp     [esi + UDP_SOCKET.LocalPort], bx
1028
        jne     .next_socket
1206 hidnplayr 1029
 
2402 hidnplayr 1030
        DEBUGF  1,"local port %u already in use\n", bx
1031
        ret
1206 hidnplayr 1032
 
1033
  .port_ok:
2402 hidnplayr 1034
        mov     [eax + UDP_SOCKET.LocalPort], bx
1035
        or      bx, bx                                  ; set the zero-flag
1514 hidnplayr 1036
 
2402 hidnplayr 1037
        ret
1206 hidnplayr 1038
 
1039
 
1257 hidnplayr 1040
 
1041
;-----------------------------------------------------------------
1206 hidnplayr 1042
;
1514 hidnplayr 1043
; SOCKET_input
1206 hidnplayr 1044
;
1536 hidnplayr 1045
; Updates a (stateless) socket with received data
1206 hidnplayr 1046
;
1514 hidnplayr 1047
; Note: the mutex should already be set !
1206 hidnplayr 1048
;
1249 hidnplayr 1049
;  IN:  eax = socket ptr
1514 hidnplayr 1050
;       ebx = pointer to device struct
1051
;       ecx = data size
1052
;       esi = ptr to data
1053
;       [esp] = ptr to buf
1054
;       [esp + 4] = buf size
1249 hidnplayr 1055
;
1514 hidnplayr 1056
;  OUT: /
1206 hidnplayr 1057
;
1257 hidnplayr 1058
;-----------------------------------------------------------------
1206 hidnplayr 1059
align 4
1514 hidnplayr 1060
SOCKET_input:
1206 hidnplayr 1061
 
2402 hidnplayr 1062
        DEBUGF  1,"SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
1206 hidnplayr 1063
 
2402 hidnplayr 1064
        mov     [esp+4], ecx
1065
        push    esi
1066
        mov     esi, esp
1514 hidnplayr 1067
 
2402 hidnplayr 1068
        add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, SOCKET_input.full
1514 hidnplayr 1069
 
2402 hidnplayr 1070
        DEBUGF  1,"SOCKET_input: queued packet successfully\n"
1071
        add     esp, sizeof.socket_queue_entry
1206 hidnplayr 1072
 
2402 hidnplayr 1073
        pusha
1074
        lea     ecx, [eax + SOCKET.mutex]
1075
        call    mutex_unlock
1076
        popa
1077
 
1078
        jmp     SOCKET_notify_owner
1079
 
1514 hidnplayr 1080
  .full:
2402 hidnplayr 1081
        DEBUGF  2,"SOCKET_input: socket %x is full!\n", eax
1206 hidnplayr 1082
 
2402 hidnplayr 1083
        pusha
1084
        lea     ecx, [eax + SOCKET.mutex]
1085
        call    mutex_unlock
1086
        popa
1514 hidnplayr 1087
 
2402 hidnplayr 1088
        call    kernel_free
1089
        add     esp, 8
1533 hidnplayr 1090
 
2402 hidnplayr 1091
        ret
1092
 
1093
 
1533 hidnplayr 1094
;--------------------------
1095
;
1096
; eax = ptr to ring struct (just a buffer of the right size)
1097
;
1098
align 4
1099
SOCKET_ring_create:
1100
 
2402 hidnplayr 1101
        push    esi
1102
        mov     esi, eax
1543 hidnplayr 1103
 
2402 hidnplayr 1104
        push    edx
1105
        stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW
1106
        pop     edx
1533 hidnplayr 1107
 
2402 hidnplayr 1108
        DEBUGF  1,"SOCKET_ring_created: %x\n", eax
1109
        mov     [esi + RING_BUFFER.start_ptr], eax
1110
        mov     [esi + RING_BUFFER.write_ptr], eax
1111
        mov     [esi + RING_BUFFER.read_ptr], eax
1112
        mov     [esi + RING_BUFFER.size], 0
1113
        add     eax,  SOCKET_MAXDATA
1114
        mov     [esi + RING_BUFFER.end_ptr], eax
1115
        mov     eax, esi
1116
        pop     esi
1533 hidnplayr 1117
 
2402 hidnplayr 1118
        ret
1533 hidnplayr 1119
 
1514 hidnplayr 1120
;-----------------------------------------------------------------
1121
;
1533 hidnplayr 1122
; SOCKET_ring_write
1529 hidnplayr 1123
;
1533 hidnplayr 1124
; Adds data to a stream socket, and updates write pointer and size
1529 hidnplayr 1125
;
1126
;  IN:  eax = ptr to ring struct
1127
;       ecx = data size
1128
;       esi = ptr to data
1129
;
1533 hidnplayr 1130
;  OUT: ecx = number of bytes stored
1529 hidnplayr 1131
;
1132
;-----------------------------------------------------------------
1133
align 4
1533 hidnplayr 1134
SOCKET_ring_write:
1529 hidnplayr 1135
 
2402 hidnplayr 1136
        DEBUGF  1,"SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
1529 hidnplayr 1137
 
2402 hidnplayr 1138
        add     [eax + RING_BUFFER.size], ecx
1139
        cmp     [eax + RING_BUFFER.size], SOCKET_MAXDATA
1140
        ja      .too_large
1529 hidnplayr 1141
 
1142
  .copy:
2402 hidnplayr 1143
        mov     edi, [eax + RING_BUFFER.write_ptr]
1144
        DEBUGF  2,"SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
1529 hidnplayr 1145
 
2402 hidnplayr 1146
        push    ecx
1147
        shr     ecx, 1
1148
        jnc     .nb
1149
        movsb
1536 hidnplayr 1150
  .nb:
2402 hidnplayr 1151
        shr     ecx, 1
1152
        jnc     .nw
1153
        movsw
1536 hidnplayr 1154
  .nw:
2402 hidnplayr 1155
        test    ecx, ecx
1156
        jz      .nd
1157
        rep     movsd
1536 hidnplayr 1158
  .nd:
2402 hidnplayr 1159
        pop     ecx
1529 hidnplayr 1160
 
2402 hidnplayr 1161
        cmp     edi, [eax + RING_BUFFER.end_ptr]
1162
        jae     .wrap
1163
        mov     [eax + RING_BUFFER.write_ptr], edi
1533 hidnplayr 1164
 
2402 hidnplayr 1165
        ret
1529 hidnplayr 1166
 
1533 hidnplayr 1167
  .wrap:
2402 hidnplayr 1168
        sub     edi, SOCKET_MAXDATA
1169
        mov     [eax + RING_BUFFER.write_ptr], edi
1533 hidnplayr 1170
 
2402 hidnplayr 1171
        ret
1533 hidnplayr 1172
 
2404 hidnplayr 1173
  .too_large:                                                           ; update size, we will fill buffer completely
1174
        sub     [eax + RING_BUFFER.size], SOCKET_MAXDATA
2402 hidnplayr 1175
        sub     ecx, [eax + RING_BUFFER.size]
2404 hidnplayr 1176
        mov     [eax + RING_BUFFER.size], SOCKET_MAXDATA
1529 hidnplayr 1177
 
2404 hidnplayr 1178
        test    ecx, ecx
1179
        jnz     .copy
1529 hidnplayr 1180
 
2402 hidnplayr 1181
        DEBUGF  2,"SOCKET_ring_write: ring buffer is full!\n"
1182
        xor     ecx, ecx
1183
        ret
1529 hidnplayr 1184
 
1185
 
1186
;-----------------------------------------------------------------
1187
;
1188
; SOCKET_ring_read
1189
;
1533 hidnplayr 1190
; reads the data, BUT DOES NOT CLEAR IT FROM MEMORY YET
1529 hidnplayr 1191
;
1192
;  IN:  eax = ptr to ring struct
1193
;       ecx = buffer size
1194
;       edi = ptr to buffer
1195
;
1533 hidnplayr 1196
;  OUT: ecx = number of bytes read
1529 hidnplayr 1197
;
1198
;-----------------------------------------------------------------
1199
align 4
1200
SOCKET_ring_read:
1201
 
2402 hidnplayr 1202
        DEBUGF  1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u\n", eax, edi, ecx
1529 hidnplayr 1203
 
2402 hidnplayr 1204
        cmp     ecx, [eax + RING_BUFFER.size]
1205
        ja      .less_data
1529 hidnplayr 1206
 
1207
  .copy:
2402 hidnplayr 1208
        mov     esi, [eax + RING_BUFFER.read_ptr]
1529 hidnplayr 1209
 
2402 hidnplayr 1210
        DEBUGF  2,"SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
1211
        push    ecx
1212
        shr     ecx, 1
1213
        jnc     .nb
1214
        movsb
1536 hidnplayr 1215
  .nb:
2402 hidnplayr 1216
        shr     ecx, 1
1217
        jnc     .nw
1218
        movsw
1536 hidnplayr 1219
  .nw:
2402 hidnplayr 1220
        test    ecx, ecx
1221
        jz      .nd
1222
        rep     movsd
1536 hidnplayr 1223
  .nd:
2402 hidnplayr 1224
        pop     ecx
1529 hidnplayr 1225
 
1830 hidnplayr 1226
  .no_data_at_all:
2402 hidnplayr 1227
        ret
1529 hidnplayr 1228
 
1533 hidnplayr 1229
  .less_data:
2402 hidnplayr 1230
        mov     ecx, [eax + RING_BUFFER.size]
1830 hidnplayr 1231
;        test    ecx, ecx
1533 hidnplayr 1232
;        jz      .no_data_at_all
2402 hidnplayr 1233
        jmp     .copy
1529 hidnplayr 1234
 
1235
 
1236
;-----------------------------------------------------------------
1237
;
1238
; SOCKET_ring_free
1239
;
1240
; Free's some bytes from the ringbuffer
1241
;
1242
;  IN:  eax = ptr to ring struct
1243
;       ecx = data size
1244
;
1245
;  OUT: ecx = number of bytes free-ed
1246
;
1247
;-----------------------------------------------------------------
1248
align 4
1249
SOCKET_ring_free:
1250
 
2402 hidnplayr 1251
        DEBUGF  1,"SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
1529 hidnplayr 1252
 
2402 hidnplayr 1253
        sub     [eax + RING_BUFFER.size], ecx
1254
        jb      .sumthinwong
1255
        add     [eax + RING_BUFFER.read_ptr], ecx
1529 hidnplayr 1256
 
2402 hidnplayr 1257
        mov     edx, [eax + RING_BUFFER.end_ptr]
1258
        cmp     [eax + RING_BUFFER.read_ptr], edx
1259
        jb      @f
1260
        sub     [eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA
1529 hidnplayr 1261
       @@:
2402 hidnplayr 1262
        ret
1529 hidnplayr 1263
 
2402 hidnplayr 1264
  .sumthinwong:                ; we could free all available bytes, but that would be stupid, i guess..
1265
        add     [eax + RING_BUFFER.size], ecx
1266
        xor     ecx, ecx
1267
        ret
1529 hidnplayr 1268
 
1269
 
1270
;-----------------------------------------------------------------
1271
;
1514 hidnplayr 1272
; SOCKET_notify_owner
1273
;
1274
; notify's the owner of a socket that something happened
1275
;
1276
;  IN:  eax = socket ptr
1277
;  OUT: /
1278
;
1279
;-----------------------------------------------------------------
1280
align 4
1281
SOCKET_notify_owner:
1282
 
2402 hidnplayr 1283
        DEBUGF  1,"SOCKET_notify_owner: %x\n", eax
1514 hidnplayr 1284
 
2402 hidnplayr 1285
        call    SOCKET_check
1286
        jz      .error
1514 hidnplayr 1287
 
2402 hidnplayr 1288
        push    eax ecx esi
1514 hidnplayr 1289
 
1290
; socket exists, now try to flag an event to the application
1291
 
2402 hidnplayr 1292
        mov     eax, [eax + SOCKET.PID]
1293
        mov     ecx, 1
1294
        mov     esi, TASK_DATA + TASKDATA.pid
1206 hidnplayr 1295
 
1296
       .next_pid:
2402 hidnplayr 1297
        cmp     [esi], eax
1298
        je      .found_pid
1299
        inc     ecx
1300
        add     esi, 0x20
1301
        cmp     ecx, [TASK_COUNT]
1302
        jbe     .next_pid
1206 hidnplayr 1303
 
1514 hidnplayr 1304
; PID not found, TODO: close socket!
1305
 
2402 hidnplayr 1306
        jmp     .error2
1514 hidnplayr 1307
 
1206 hidnplayr 1308
       .found_pid:
2402 hidnplayr 1309
        shl     ecx, 8
1310
        or      [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK
1311
        mov     [check_idle_semaphore], 200
1206 hidnplayr 1312
 
2402 hidnplayr 1313
        DEBUGF  1,"SOCKET_notify_owner: succes!\n"
1514 hidnplayr 1314
 
1315
  .error2:
2402 hidnplayr 1316
        pop     esi ecx eax
1536 hidnplayr 1317
 
1514 hidnplayr 1318
  .error:
1319
 
2402 hidnplayr 1320
        ret
1206 hidnplayr 1321
 
1322
 
1514 hidnplayr 1323
;--------------------------------------------------------------------
1324
;
1325
; SOCKET_alloc
1326
;
1159 hidnplayr 1327
; Allocate memory for socket data and put new socket into the list
1328
; Newly created socket is initialized with calling PID and number and
1329
; put into beginning of list (which is a fastest way).
1330
;
1514 hidnplayr 1331
; IN:  /
1332
; OUT: eax = 0 on error, socket ptr otherwise
1333
;      edi = socket number
1334
;       ZF = cleared on error
1159 hidnplayr 1335
;
1514 hidnplayr 1336
;--------------------------------------------------------------------
1337
align 4
1338
SOCKET_alloc:
1339
 
2402 hidnplayr 1340
        push    ebx
1514 hidnplayr 1341
 
2402 hidnplayr 1342
        stdcall kernel_alloc, SOCKETBUFFSIZE
1343
        DEBUGF  1, "SOCKET_alloc: ptr=%x\n", eax
1344
        or      eax, eax
1345
        jz      .exit
1159 hidnplayr 1346
 
1514 hidnplayr 1347
; zero-initialize allocated memory
2402 hidnplayr 1348
        push    eax
1349
        mov     edi, eax
1350
        mov     ecx, SOCKETBUFFSIZE / 4
1351
        xor     eax, eax
1352
        rep     stosd
1353
        pop     eax
1159 hidnplayr 1354
 
1536 hidnplayr 1355
; set send-and receive procedures to return -1
2402 hidnplayr 1356
        mov     [eax + SOCKET.snd_proc], s_error
1357
        mov     [eax + SOCKET.rcv_proc], s_error
1536 hidnplayr 1358
 
1514 hidnplayr 1359
; find first free socket number and use it
2402 hidnplayr 1360
        mov     edi, [last_socket_num]
1159 hidnplayr 1361
  .next_socket_number:
2402 hidnplayr 1362
        inc     edi
1363
        jz      .next_socket_number     ; avoid socket nr 0
1364
        cmp     edi, -1
1365
        je      .next_socket_number     ; avoid socket nr -1
1366
        mov     ebx, net_sockets
1159 hidnplayr 1367
  .next_socket:
2402 hidnplayr 1368
        mov     ebx, [ebx + SOCKET.NextPtr]
1369
        test    ebx, ebx
1370
        jz      .last_socket
1536 hidnplayr 1371
 
2402 hidnplayr 1372
        cmp     [ebx + SOCKET.Number], edi
1373
        jne     .next_socket
1374
        jmp     .next_socket_number
1159 hidnplayr 1375
 
1514 hidnplayr 1376
  .last_socket:
2402 hidnplayr 1377
        mov     [last_socket_num], edi
1378
        mov     [eax + SOCKET.Number], edi
1379
        DEBUGF  1, "SOCKET_alloc: number=%u\n", edi
1159 hidnplayr 1380
 
1514 hidnplayr 1381
; Fill in PID
2402 hidnplayr 1382
        mov     ebx, [TASK_BASE]
1383
        mov     ebx, [ebx + TASKDATA.pid]
1384
        mov     [eax + SOCKET.PID], ebx
1514 hidnplayr 1385
 
2403 hidnplayr 1386
; init mutex
1387
        pusha
1388
        lea     ecx, [eax + SOCKET.mutex]
1389
        call    mutex_init
1390
        popa
1391
 
1529 hidnplayr 1392
; add socket to the list by re-arranging some pointers
2402 hidnplayr 1393
        mov     ebx, [net_sockets + SOCKET.NextPtr]
1514 hidnplayr 1394
 
2402 hidnplayr 1395
        mov     [eax + SOCKET.PrevPtr], net_sockets
1396
        mov     [eax + SOCKET.NextPtr], ebx
1514 hidnplayr 1397
 
2402 hidnplayr 1398
        test    ebx, ebx
1399
        jz      @f
1400
 
1401
        pusha
1402
        lea     ecx, [ebx + SOCKET.mutex]
1403
        call    mutex_lock
1404
        popa
1405
 
1406
        mov     [ebx + SOCKET.PrevPtr], eax
1407
 
1408
        pusha
1409
        lea     ecx, [ebx + SOCKET.mutex]
1410
        call    mutex_unlock
1411
        popa
1514 hidnplayr 1412
       @@:
1413
 
2402 hidnplayr 1414
        mov     [net_sockets + SOCKET.NextPtr], eax
1415
        or      eax, eax                ; used to clear zero flag
1159 hidnplayr 1416
  .exit:
2402 hidnplayr 1417
        pop     ebx
1514 hidnplayr 1418
 
2402 hidnplayr 1419
        ret
1159 hidnplayr 1420
 
1514 hidnplayr 1421
 
1422
;----------------------------------------------------
1159 hidnplayr 1423
;
1514 hidnplayr 1424
; SOCKET_free
1159 hidnplayr 1425
;
1514 hidnplayr 1426
; Free socket data memory and remove socket from the list
1427
;
1428
; IN:  eax = socket ptr
1429
; OUT: /
1430
;
1431
;----------------------------------------------------
1432
align 4
1433
SOCKET_free:
1159 hidnplayr 1434
 
2402 hidnplayr 1435
        DEBUGF  1, "SOCKET_free: %x\n", eax
1514 hidnplayr 1436
 
2402 hidnplayr 1437
        call    SOCKET_check
1438
        jz      .error
1159 hidnplayr 1439
 
2402 hidnplayr 1440
        push    ebx
1514 hidnplayr 1441
 
2402 hidnplayr 1442
        pusha
1443
        lea     ecx, [eax + SOCKET.mutex]
1444
        call    mutex_lock
1445
        popa
1514 hidnplayr 1446
 
2402 hidnplayr 1447
        DEBUGF  1, "SOCKET_free: freeing socket..\n"
1529 hidnplayr 1448
 
2402 hidnplayr 1449
        cmp     [eax + SOCKET.Domain], AF_INET4
1450
        jnz     .no_tcp
1529 hidnplayr 1451
 
2402 hidnplayr 1452
        cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
1453
        jnz     .no_tcp
1454
 
1455
        mov     ebx, eax
1456
        stdcall kernel_free, [ebx + STREAM_SOCKET.rcv.start_ptr]
1457
        stdcall kernel_free, [ebx + STREAM_SOCKET.snd.start_ptr]
1458
        mov     eax, ebx
1536 hidnplayr 1459
  .no_tcp:
1529 hidnplayr 1460
 
2402 hidnplayr 1461
        push    eax                             ; this will be passed to kernel_free
1462
        mov     ebx, [eax + SOCKET.NextPtr]
1463
        mov     eax, [eax + SOCKET.PrevPtr]
1514 hidnplayr 1464
 
2402 hidnplayr 1465
        DEBUGF  1, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
1514 hidnplayr 1466
 
2402 hidnplayr 1467
        test    eax, eax
1468
        jz      @f
1469
        mov     [eax + SOCKET.NextPtr], ebx
1514 hidnplayr 1470
       @@:
1159 hidnplayr 1471
 
2402 hidnplayr 1472
        test    ebx, ebx
1473
        jz      @f
1474
        mov     [ebx + SOCKET.PrevPtr], eax
1514 hidnplayr 1475
       @@:
1249 hidnplayr 1476
 
2402 hidnplayr 1477
        call    kernel_free
1478
        pop     ebx
1159 hidnplayr 1479
 
2402 hidnplayr 1480
        DEBUGF  1, "SOCKET_free: success!\n"
1514 hidnplayr 1481
 
1159 hidnplayr 1482
  .error:
2402 hidnplayr 1483
        ret
1159 hidnplayr 1484
 
1543 hidnplayr 1485
;------------------------------------
1486
;
1487
; SOCKET_fork
1488
;
1489
; Create a child socket
1490
;
1533 hidnplayr 1491
; IN:  socket nr in ebx
1543 hidnplayr 1492
; OUT: child socket nr in eax
1493
;
1494
;-----------------------------------
1529 hidnplayr 1495
align 4
1496
SOCKET_fork:
1497
 
2402 hidnplayr 1498
        DEBUGF  1,"SOCKET_fork: %x\n", ebx
1529 hidnplayr 1499
 
1543 hidnplayr 1500
; Exit if backlog queue is full
2402 hidnplayr 1501
        mov     eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
1502
        cmp     ax, [ebx + SOCKET.backlog]
1503
        jae     .fail
1543 hidnplayr 1504
 
1529 hidnplayr 1505
; Allocate new socket
2402 hidnplayr 1506
        push    ebx
1507
        call    SOCKET_alloc
1508
        pop     ebx
1509
        jz      .fail
1529 hidnplayr 1510
 
2402 hidnplayr 1511
        push    eax
1512
        mov     esi, esp
1513
        add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
1514
        pop     eax
1543 hidnplayr 1515
 
1516
; Copy structure from current socket to new
1517
; We start at PID to preserve the socket num, and the 2 pointers at beginning of socket
2402 hidnplayr 1518
        lea     esi, [ebx + SOCKET.PID]
1519
        lea     edi, [eax + SOCKET.PID]
1520
        mov     ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
1521
        rep     movsd
1529 hidnplayr 1522
 
2402 hidnplayr 1523
        and     [eax + SOCKET.options], not SO_ACCEPTCON
1529 hidnplayr 1524
 
2402 hidnplayr 1525
        ret
1529 hidnplayr 1526
 
1543 hidnplayr 1527
  .fail2:
2402 hidnplayr 1528
        add     esp, 4+4+4
1543 hidnplayr 1529
  .fail:
2402 hidnplayr 1530
        DEBUGF  1,"SOCKET_fork: failed\n"
1531
        xor     eax, eax
1532
        ret
1529 hidnplayr 1533
 
1543 hidnplayr 1534
 
1514 hidnplayr 1535
;---------------------------------------------------
1536
;
1537
; SOCKET_num_to_ptr
1538
;
1159 hidnplayr 1539
; Get socket structure address by its number
1540
;
1514 hidnplayr 1541
; IN:  ecx = socket number
1533 hidnplayr 1542
; OUT: eax = 0 on error, socket ptr otherwise
1514 hidnplayr 1543
;       ZF = set on error
1159 hidnplayr 1544
;
1514 hidnplayr 1545
;---------------------------------------------------
1546
align 4
1547
SOCKET_num_to_ptr:
1159 hidnplayr 1548
 
2402 hidnplayr 1549
        DEBUGF  1,"SOCKET_num_to_ptr: %u ", ecx
1514 hidnplayr 1550
 
2402 hidnplayr 1551
        mov     eax, net_sockets
1514 hidnplayr 1552
 
1159 hidnplayr 1553
  .next_socket:
2402 hidnplayr 1554
        mov     eax, [eax + SOCKET.NextPtr]
1555
        or      eax, eax
1556
        jz      .error
1557
        cmp     [eax + SOCKET.Number], ecx
1558
        jne     .next_socket
1159 hidnplayr 1559
 
2402 hidnplayr 1560
        test    eax, eax
1159 hidnplayr 1561
 
2402 hidnplayr 1562
        DEBUGF  1,"(%x)\n", eax
1159 hidnplayr 1563
  .error:
2402 hidnplayr 1564
        ret
1159 hidnplayr 1565
 
1514 hidnplayr 1566
 
1567
;---------------------------------------------------
1159 hidnplayr 1568
;
1514 hidnplayr 1569
; SOCKET_ptr_to_num
1159 hidnplayr 1570
;
1514 hidnplayr 1571
; Get socket number by its address
1572
;
1573
; IN:  eax = socket ptr
1574
; OUT: eax = 0 on error, socket num otherwise
1575
;       ZF = set on error
1576
;
1577
;---------------------------------------------------
1578
align 4
1579
SOCKET_ptr_to_num:
1580
 
2402 hidnplayr 1581
        DEBUGF  1,"SOCKET_ptr_to_num: %x ", eax
1514 hidnplayr 1582
 
2402 hidnplayr 1583
        call    SOCKET_check
1584
        jz      .error
1159 hidnplayr 1585
 
2402 hidnplayr 1586
        mov     eax, [eax + SOCKET.Number]
1514 hidnplayr 1587
 
2402 hidnplayr 1588
        DEBUGF  1,"(%u)\n", eax
1514 hidnplayr 1589
 
1590
  .error:
2402 hidnplayr 1591
        ret
1514 hidnplayr 1592
 
1593
 
1594
;---------------------------------------------------
1595
;
1596
; SOCKET_check
1597
;
1598
; checks if the given value is really a socket ptr
1599
;
1600
; IN:  eax = socket ptr
1601
; OUT: eax = 0 on error, unchanged otherwise
1602
;       ZF = set on error
1603
;
1604
;---------------------------------------------------
1605
align 4
1606
SOCKET_check:
1607
 
2402 hidnplayr 1608
        DEBUGF  1,"SOCKET_check: %x\n", eax
1514 hidnplayr 1609
 
2402 hidnplayr 1610
        push    ebx
1611
        mov     ebx, net_sockets
1514 hidnplayr 1612
 
1159 hidnplayr 1613
  .next_socket:
2402 hidnplayr 1614
        mov     ebx, [ebx + SOCKET.NextPtr]
1615
        or      ebx, ebx
1616
        jz      .done
1617
        cmp     ebx, eax
1618
        jnz     .next_socket
1159 hidnplayr 1619
 
1514 hidnplayr 1620
  .done:
2402 hidnplayr 1621
        mov     eax, ebx
1622
        test    eax, eax
1623
        pop     ebx
1514 hidnplayr 1624
 
2402 hidnplayr 1625
        ret
1159 hidnplayr 1626
 
1514 hidnplayr 1627
 
1628
 
1629
;---------------------------------------------------
1630
;
1631
; SOCKET_check_owner
1632
;
1633
; checks if the caller application owns the socket
1634
;
1635
; IN:  eax = socket ptr
1636
; OUT:  ZF = true/false
1637
;
1638
;---------------------------------------------------
1639
align 4
1640
SOCKET_check_owner:
1641
 
2402 hidnplayr 1642
        DEBUGF  1,"SOCKET_check_owner: %x\n", eax
1514 hidnplayr 1643
 
2402 hidnplayr 1644
        push    ebx
1645
        mov     ebx, [TASK_BASE]
1646
        mov     ebx, [ecx + TASKDATA.pid]
1647
        cmp     [eax + SOCKET.PID], ebx
1648
        pop      ebx
1514 hidnplayr 1649
 
2402 hidnplayr 1650
        ret
1514 hidnplayr 1651
 
1652
 
1653
 
1654
 
1885 hidnplayr 1655
;------------------------------------------------------
1514 hidnplayr 1656
;
1657
; SOCKET_process_end
1658
;
1659
; Kernel calls this function when a certain process ends
1660
; This function will check if the process had any open sockets
1661
; And update them accordingly
1662
;
1663
; IN:  eax = pid
1664
; OUT: /
1665
;
1666
;------------------------------------------------------
1667
align 4
1668
SOCKET_process_end:
1669
 
2402 hidnplayr 1670
        DEBUGF  1,"SOCKET_process_end: %x\n", eax
1514 hidnplayr 1671
 
2402 hidnplayr 1672
        push    ebx
1673
        mov     ebx, net_sockets
1514 hidnplayr 1674
 
1675
  .next_socket:
1676
 
2402 hidnplayr 1677
        mov     ebx, [ebx + SOCKET.NextPtr]
1514 hidnplayr 1678
  .test_socket:
2402 hidnplayr 1679
        test    ebx, ebx
1680
        jz      .done
1514 hidnplayr 1681
 
2402 hidnplayr 1682
        cmp     [ebx + SOCKET.PID], eax
1683
        jne     .next_socket
1514 hidnplayr 1684
 
2402 hidnplayr 1685
        DEBUGF  1,"closing socket %x", eax, ebx
1514 hidnplayr 1686
 
2402 hidnplayr 1687
        mov     [ebx + SOCKET.PID], 0
1514 hidnplayr 1688
 
2402 hidnplayr 1689
        cmp     [ebx + SOCKET.Protocol], IP_PROTO_UDP
1690
        je      .udp
1514 hidnplayr 1691
 
2402 hidnplayr 1692
        cmp     [ebx + SOCKET.Protocol], IP_PROTO_TCP
1693
        je      .tcp
1514 hidnplayr 1694
 
2402 hidnplayr 1695
        jmp     .next_socket    ; kill all sockets for given PID
1514 hidnplayr 1696
 
1697
  .udp:
2402 hidnplayr 1698
        mov     eax, ebx
1699
        mov     ebx, [ebx + SOCKET.NextPtr]
1700
        call    SOCKET_free
1701
        jmp     .test_socket
1514 hidnplayr 1702
 
1703
  .tcp:
1704
 
2402 hidnplayr 1705
        ;;; TODO
1885 hidnplayr 1706
 
2402 hidnplayr 1707
        jmp     .next_socket
1514 hidnplayr 1708
 
1709
  .done:
2402 hidnplayr 1710
        pop     ebx
1514 hidnplayr 1711
 
2402 hidnplayr 1712
        ret
1773 hidnplayr 1713
 
1714
 
1715
 
1716
 
1885 hidnplayr 1717
;-----------------------------------------------------------------
1718
;
1719
; SOCKET_is_connecting
1720
;
1721
;  IN:  eax = socket ptr
1722
;  OUT: /
1723
;
1724
;-----------------------------------------------------------------
1773 hidnplayr 1725
 
1885 hidnplayr 1726
align 4
1727
SOCKET_is_connecting:
1773 hidnplayr 1728
 
1729
 
2402 hidnplayr 1730
        and     [eax + SOCKET.options], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING)
1731
        or      [eax + SOCKET.options], SS_ISCONNECTING
1773 hidnplayr 1732
 
2402 hidnplayr 1733
        jmp     SOCKET_notify_owner
1885 hidnplayr 1734
 
1735
 
1736
 
1773 hidnplayr 1737
;-----------------------------------------------------------------
1738
;
1885 hidnplayr 1739
; SOCKET_is_connected
1740
;
1741
;  IN:  eax = socket ptr
1742
;  OUT: /
1743
;
1744
;-----------------------------------------------------------------
1745
 
1746
align 4
1747
SOCKET_is_connected:
1748
 
1749
 
2402 hidnplayr 1750
        and     [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
1751
        or      [eax + SOCKET.options], SS_ISCONNECTED
1885 hidnplayr 1752
 
2402 hidnplayr 1753
        jmp     SOCKET_notify_owner
1885 hidnplayr 1754
 
1755
 
1756
 
1757
 
1758
;-----------------------------------------------------------------
1759
;
1773 hidnplayr 1760
; SOCKET_is_disconnecting
1761
;
1762
;  IN:  eax = socket ptr
1763
;  OUT: /
1764
;
1765
;-----------------------------------------------------------------
1766
 
1767
align 4
1768
SOCKET_is_disconnecting:
1769
 
2402 hidnplayr 1770
        and     [eax + SOCKET.options], not (SS_ISCONNECTING)
1771
        or      [eax + SOCKET.options], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE
1773 hidnplayr 1772
 
2402 hidnplayr 1773
        jmp     SOCKET_notify_owner
1773 hidnplayr 1774
 
1775
 
1776
 
1777
;-----------------------------------------------------------------
1778
;
1779
; SOCKET_is_disconnected
1780
;
1781
;  IN:  eax = socket ptr
1782
;  OUT: /
1783
;
1784
;-----------------------------------------------------------------
1785
 
1786
align 4
1787
SOCKET_is_disconnected:
1788
 
2402 hidnplayr 1789
        and     [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING)
1790
        or      [eax + SOCKET.options], SS_CANTRCVMORE + SS_CANTSENDMORE
1773 hidnplayr 1791
 
2402 hidnplayr 1792
        jmp     SOCKET_notify_owner
1774 hidnplayr 1793
 
1794
 
1795
;-----------------------------------------------------------------
1796
;
1797
; SOCKET_cant_recv_more
1798
;
1799
;  IN:  eax = socket ptr
1800
;  OUT: /
1801
;
1802
;-----------------------------------------------------------------
1803
 
1804
align 4
1805
SOCKET_cant_recv_more:
1806
 
2402 hidnplayr 1807
        or      [eax + SOCKET.options], SS_CANTRCVMORE
1885 hidnplayr 1808
 
2402 hidnplayr 1809
        ret
1831 hidnplayr 1810
 
1811
 
1812
 
1813
;-----------------------------------------------------------------
1814
;
1885 hidnplayr 1815
; SOCKET_cant_send_more
1831 hidnplayr 1816
;
1817
;  IN:  eax = socket ptr
1818
;  OUT: /
1819
;
1820
;-----------------------------------------------------------------
1821
 
1822
align 4
1885 hidnplayr 1823
SOCKET_cant_send_more:
1831 hidnplayr 1824
 
2402 hidnplayr 1825
        or      [eax + SOCKET.options], SS_CANTSENDMORE
1831 hidnplayr 1826
 
2402 hidnplayr 1827
        ret