Subversion Repositories Kolibri OS

Rev

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