Subversion Repositories Kolibri OS

Rev

Rev 1763 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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