Subversion Repositories Kolibri OS

Rev

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