Subversion Repositories Kolibri OS

Rev

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

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