Subversion Repositories Kolibri OS

Rev

Rev 2573 | Blame | Last modification | View Log | Download | RSS feed

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