Subversion Repositories Kolibri OS

Rev

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