Subversion Repositories Kolibri OS

Rev

Rev 3406 | 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: 3459 $
  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     ebx, edi
  852.         mov     ecx, esi
  853.         mov     edi, edx
  854.         xor     edx, edx
  855.  
  856.         test    ebx, MSG_DONTWAIT
  857.         jnz     .dontwait
  858.   .loop:
  859.         cmp     [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0
  860.         je      .block
  861.   .dontwait:
  862.         test    ebx, MSG_PEEK
  863.         jnz     .peek
  864.  
  865.         add     eax, STREAM_SOCKET.rcv
  866.         call    SOCKET_ring_read
  867.         call    SOCKET_ring_free
  868.  
  869.         mov     [esp+32], ecx                                   ; return number of bytes copied
  870.         ret
  871.  
  872.   .peek:
  873.         mov     ecx, [eax + STREAM_SOCKET.rcv + RING_BUFFER.size]
  874.         mov     [esp+32], ecx                                   ; return number of bytes available
  875.         ret
  876.  
  877.   .block:
  878.         test    [eax + SOCKET.options], SO_NONBLOCK
  879.         jnz     s_error
  880.  
  881.         call    SOCKET_block
  882.         jmp     .loop
  883.  
  884.  
  885. ;-----------------------------------------------------------------
  886. ;
  887. ; SOCKET_send
  888. ;
  889. ;
  890. ;  IN:  socket number in ecx
  891. ;       pointer to data in edx
  892. ;       datalength in esi
  893. ;       flags in edi
  894. ;  OUT: -1 on error
  895. ;
  896. ;-----------------------------------------------------------------
  897. align 4
  898. SOCKET_send:
  899.  
  900.         DEBUGF  2,"SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi
  901.  
  902.         call    SOCKET_num_to_ptr
  903.         jz      s_error
  904.  
  905.         mov     ecx, esi
  906.         mov     esi, edx
  907.  
  908.         jmp     [eax + SOCKET.snd_proc]
  909.  
  910.  
  911. align 4
  912. SOCKET_send_udp:
  913.  
  914.         DEBUGF  1,"SOCKET_send: UDP\n"
  915.  
  916.         mov     [esp+32], ecx
  917.         call    UDP_output
  918.         cmp     eax, -1
  919.         je      s_error
  920.         ret
  921.  
  922.  
  923. align 4
  924. SOCKET_send_tcp:
  925.  
  926.         DEBUGF  1,"SOCKET_send: TCP\n"
  927.  
  928.         push    eax
  929.         add     eax, STREAM_SOCKET.snd
  930.         call    SOCKET_ring_write
  931.         pop     eax
  932.  
  933.         mov     [esp+32], ecx
  934.  
  935.         call    TCP_output
  936.         ret
  937.  
  938.  
  939. align 4
  940. SOCKET_send_ip:
  941.  
  942.         DEBUGF  1,"SOCKET_send: IPv4\n"
  943.  
  944.         mov     [esp+32], ecx
  945.         call    IPv4_output_raw
  946.         cmp     eax, -1
  947.         je      s_error
  948.         ret
  949.  
  950.  
  951. align 4
  952. SOCKET_send_icmp:
  953.  
  954.         DEBUGF  1,"SOCKET_send: ICMP\n"
  955.  
  956.         mov     [esp+32], ecx
  957.         call    ICMP_output_raw
  958.         cmp     eax, -1
  959.         je      s_error
  960.         ret
  961.  
  962.  
  963. align 4
  964. SOCKET_send_pppoe:
  965.  
  966.         DEBUGF  1,"SOCKET_send: PPPoE\n"
  967.  
  968.         mov     [esp+32], ecx
  969.         mov     ebx, [eax + SOCKET.device]
  970.  
  971.         call    PPPoE_discovery_output
  972.         cmp     eax, -1
  973.         je      s_error
  974.         ret
  975.  
  976.  
  977.  
  978. align 4
  979. SOCKET_send_local:
  980.  
  981.         ; does this socket have a PID yet?
  982.         cmp     [eax + SOCKET.PID], 0
  983.         jne     @f
  984.  
  985.         ; Change PID to that of current process
  986.         mov     ebx, [TASK_BASE]
  987.         mov     ebx, [ebx + TASKDATA.pid]
  988.         mov     [eax + SOCKET.PID], ebx
  989.         mov     [eax + SOCKET.TID], ebx         ; currently TID = PID in kolibrios :(
  990.       @@:
  991.         mov     [eax + SOCKET.snd_proc], SOCKET_send_local_
  992.  
  993. align 4
  994. SOCKET_send_local_:
  995.  
  996.         DEBUGF  1,"SOCKET_send: LOCAL\n"
  997.  
  998.         ; get the other side's socket and check if it still exists
  999.         mov     eax, [eax + SOCKET.device]
  1000.         call    SOCKET_check
  1001.         jz      s_error
  1002.  
  1003.         ; allright, shove in the data!
  1004.         push    eax
  1005.         add     eax, STREAM_SOCKET.rcv
  1006.         call    SOCKET_ring_write
  1007.         pop     eax
  1008.  
  1009.         ; return the number of written bytes (or errorcode) to application
  1010.         mov     [esp+32], ecx
  1011.  
  1012.         ; and notify the other end
  1013.         call    SOCKET_notify
  1014.  
  1015.         ret
  1016.  
  1017.  
  1018. ;-----------------------------------------------------------------
  1019. ;
  1020. ; SOCKET_get_options
  1021. ;
  1022. ;  IN:  ecx = socket number
  1023. ;       edx = pointer to the options:
  1024. ;               dd      level, optname, optval, optlen
  1025. ;  OUT: -1 on error
  1026. ;
  1027. ; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
  1028. ; TODO: find best way to notify that send()'ed data were acknowledged
  1029. ; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*.
  1030. ;
  1031. ;-----------------------------------------------------------------
  1032. align 4
  1033. SOCKET_get_opt:
  1034.  
  1035.         DEBUGF  2,"SOCKET_get_opt\n"
  1036.  
  1037.         call    SOCKET_num_to_ptr
  1038.         jz      s_error
  1039.  
  1040.         cmp     dword [edx], IP_PROTO_TCP
  1041.         jne     s_error
  1042.         cmp     dword [edx+4], -2
  1043.         je      @f
  1044.         cmp     dword [edx+4], -3
  1045.         jne     s_error
  1046. @@:
  1047. ;        mov     eax, [edx+12]
  1048. ;        test    eax, eax
  1049. ;        jz      .fail
  1050. ;        cmp     dword [eax], 4
  1051. ;        mov     dword [eax], 4
  1052. ;        jb      .fail
  1053. ;        stdcall net_socket_num_to_addr, ecx
  1054. ;        test    eax, eax
  1055. ;        jz      .fail
  1056. ;        ; todo: check that eax is really TCP socket
  1057. ;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
  1058. ;        cmp     dword [edx+4], -2
  1059. ;        jz      @f
  1060. ;        mov     ecx, [eax + TCP_SOCKET.state]
  1061. @@:
  1062.         mov     eax, [edx+8]
  1063.         test    eax, eax
  1064.         jz      @f
  1065.         mov     [eax], ecx
  1066. @@:
  1067.         mov     dword [esp+32], 0
  1068.         ret
  1069.  
  1070.  
  1071.  
  1072. ;-----------------------------------------------------------------
  1073. ;
  1074. ; SOCKET_set_options
  1075. ;
  1076. ;  IN:  ecx = socket number
  1077. ;       edx = pointer to the options:
  1078. ;               dd      level, optname, optlen, optval
  1079. ;  OUT: -1 on error
  1080. ;
  1081. ;-----------------------------------------------------------------
  1082. align 4
  1083. SOCKET_set_opt:
  1084.  
  1085.         DEBUGF  2,"SOCKET_set_opt\n"
  1086.  
  1087.         call    SOCKET_num_to_ptr
  1088.         jz      s_error
  1089.  
  1090.         cmp     dword [edx], SOL_SOCKET
  1091.         jne     s_error
  1092.  
  1093.         cmp     dword [edx+4], SO_BINDTODEVICE
  1094.         je      .bind
  1095.  
  1096.         cmp     dword [edx+4], SO_BLOCK
  1097.         je      .block
  1098.  
  1099.         jmp     s_error
  1100.  
  1101.   .bind:
  1102.         cmp     dword [edx+8], 0
  1103.         je      .unbind
  1104.  
  1105.         movzx   edx, byte [edx + 9]
  1106.         cmp     edx, MAX_NET_DEVICES
  1107.         ja      s_error
  1108.  
  1109.         mov     edx, [NET_DRV_LIST + 4*edx]
  1110.         test    edx, edx
  1111.         jz      s_error
  1112.         mov     [eax + SOCKET.device], edx
  1113.  
  1114.         DEBUGF  1,"SOCKET_set_opt: Bound socket %x to device %x\n",eax, edx
  1115.  
  1116.         mov     dword [esp+32], 0       ; success!
  1117.         ret
  1118.  
  1119.   .unbind:
  1120.         mov     [eax + SOCKET.device], 0
  1121.  
  1122.         mov     dword [esp+32], 0       ; success!
  1123.         ret
  1124.  
  1125.   .block:
  1126.         cmp     dword [edx+8], 0
  1127.         je      .unblock
  1128.  
  1129.         and     [eax + SOCKET.options], not SO_NONBLOCK
  1130.  
  1131.         mov     dword [esp+32], 0       ; success!
  1132.         ret
  1133.  
  1134.   .unblock:
  1135.         or      [eax + SOCKET.options], SO_NONBLOCK
  1136.  
  1137.         mov     dword [esp+32], 0       ; success!
  1138.         ret
  1139.  
  1140.  
  1141.  
  1142. ;-----------------------------------------------------------------
  1143. ;
  1144. ; SOCKET_pair
  1145. ;
  1146. ; Allocates a pair of linked LOCAL domain sockets
  1147. ;
  1148. ; IN: /
  1149. ; OUT: eax is socket1 num, -1 on error
  1150. ;      ebx is socket2 num
  1151. ;
  1152. ;-----------------------------------------------------------------
  1153. align 4
  1154. SOCKET_pair:
  1155.  
  1156.         DEBUGF  2,"SOCKET_pair\n"
  1157.  
  1158.         call    SOCKET_alloc
  1159.         jz      s_error
  1160.         mov     [esp+32], edi   ; application's eax
  1161.  
  1162.         mov     [eax + SOCKET.Domain], AF_LOCAL
  1163.         mov     [eax + SOCKET.Type], SOCK_STREAM
  1164.         mov     [eax + SOCKET.Protocol], 0              ;;; CHECKME
  1165.         mov     [eax + SOCKET.snd_proc], SOCKET_send_local
  1166.         mov     [eax + SOCKET.rcv_proc], SOCKET_receive_local
  1167.         mov     [eax + SOCKET.PID], 0
  1168.         mov     ebx, eax
  1169.  
  1170.         call    SOCKET_alloc
  1171.         jz      .error
  1172.         mov     [esp+24], edi   ; application's ebx
  1173.  
  1174.         mov     [eax + SOCKET.Domain], AF_LOCAL
  1175.         mov     [eax + SOCKET.Type], SOCK_STREAM
  1176.         mov     [eax + SOCKET.Protocol], 0              ;;; CHECKME
  1177.         mov     [eax + SOCKET.snd_proc], SOCKET_send_local
  1178.         mov     [eax + SOCKET.rcv_proc], SOCKET_receive_local
  1179.         mov     [eax + SOCKET.PID], 0
  1180.  
  1181.         ; Link the two sockets to eachother
  1182.         mov     [eax + SOCKET.device], ebx
  1183.         mov     [ebx + SOCKET.device], eax
  1184.  
  1185.         lea     eax, [eax + STREAM_SOCKET.rcv]
  1186.         call    SOCKET_ring_create
  1187.  
  1188.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  1189.         call    SOCKET_ring_create
  1190.         pop     eax
  1191.  
  1192.         ret
  1193.  
  1194.   .error:
  1195.         mov     eax, ebx
  1196.         call    SOCKET_free
  1197.         jmp     s_error
  1198.  
  1199.  
  1200.  
  1201. ;-----------------------------------------------------------------
  1202. ;
  1203. ; SOCKET_debug
  1204. ;
  1205. ;  Copies socket variables to application buffer
  1206. ;
  1207. ;  IN:  ecx = socket number
  1208. ;       edx = pointer to buffer
  1209. ;
  1210. ;  OUT: -1 on error
  1211. ;-----------------------------------------------------------------
  1212. align 4
  1213. SOCKET_debug:
  1214.  
  1215.         DEBUGF  1,"SOCKET_debug\n"
  1216.  
  1217.         mov     edi, edx
  1218.  
  1219.         test    ecx, ecx
  1220.         jz      .returnall
  1221.  
  1222.         call    SOCKET_num_to_ptr
  1223.         jz      s_error
  1224.  
  1225.         mov     esi, eax
  1226.         mov     ecx, SOCKETBUFFSIZE/4
  1227.         rep     movsd
  1228.  
  1229.         mov     dword [esp+32], 0
  1230.         ret
  1231.  
  1232.   .returnall:
  1233.         mov     ebx, net_sockets
  1234.   .next_socket:
  1235.         mov     ebx, [ebx + SOCKET.NextPtr]
  1236.         test    ebx, ebx
  1237.         jz      .done
  1238.         mov     eax, [ebx + SOCKET.Number]
  1239.         stosd
  1240.         jmp     .next_socket
  1241.   .done:
  1242.         xor     eax, eax
  1243.         stosd
  1244.  
  1245.         mov     dword [esp+32], 0
  1246.         ret
  1247.  
  1248.  
  1249. ;-----------------------------------------------------------------
  1250. ;
  1251. ; SOCKET_find_port
  1252. ;
  1253. ; Fills in the local port number for TCP and UDP sockets
  1254. ; This procedure always works because the number of sockets is
  1255. ; limited to a smaller number then the number of possible ports
  1256. ;
  1257. ;  IN:  eax = socket pointer
  1258. ;  OUT: /
  1259. ;
  1260. ;-----------------------------------------------------------------
  1261. align 4
  1262. SOCKET_find_port:
  1263.  
  1264.         DEBUGF  2,"SOCKET_find_port\n"
  1265.  
  1266.         push    ebx esi ecx
  1267.  
  1268.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  1269.         je      .udp
  1270.  
  1271.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  1272.         je      .tcp
  1273.  
  1274.         pop     ecx esi ebx
  1275.         ret
  1276.  
  1277.   .udp:
  1278.         mov     bx, [last_UDP_port]
  1279.         call    .findit
  1280.         mov     [last_UDP_port], bx
  1281.  
  1282.         pop     ecx esi ebx
  1283.         ret
  1284.  
  1285.   .tcp:
  1286.         mov     bx, [last_TCP_port]
  1287.         call    .findit
  1288.         mov     [last_TCP_port], bx
  1289.  
  1290.         pop     ecx esi ebx
  1291.         ret
  1292.  
  1293.  
  1294.   .restart:
  1295.         mov     bx, MIN_EPHEMERAL_PORT_N
  1296.   .findit:
  1297.         cmp     bx, MAX_EPHEMERAL_PORT_N
  1298.         je      .restart
  1299.  
  1300.         add     bh, 1
  1301.         adc     bl, 0
  1302.  
  1303.         call    SOCKET_check_port
  1304.         jz      .findit
  1305.         ret
  1306.  
  1307.  
  1308.  
  1309. ;-----------------------------------------------------------------
  1310. ;
  1311. ; SOCKET_check_port (to be used with AF_INET only!)
  1312. ;
  1313. ; Checks if a local port number is unused
  1314. ; If the proposed port number is unused, it is filled in in the socket structure
  1315. ;
  1316. ;  IN:  eax = socket ptr (to find out if its a TCP/UDP socket)
  1317. ;        bx = proposed socket number (network byte order)
  1318. ;
  1319. ;  OUT:  ZF = set on error
  1320. ;
  1321. ;-----------------------------------------------------------------
  1322. align 4
  1323. SOCKET_check_port:
  1324.  
  1325.         DEBUGF  2,"SOCKET_check_port: "
  1326.  
  1327.         mov     ecx, [eax + SOCKET.Protocol]
  1328.         mov     edx, [eax + IP_SOCKET.LocalIP]
  1329.         mov     esi, net_sockets
  1330.  
  1331.   .next_socket:
  1332.         mov     esi, [esi + SOCKET.NextPtr]
  1333.         or      esi, esi
  1334.         jz      .port_ok
  1335.  
  1336.         cmp     [esi + SOCKET.Protocol], ecx
  1337.         jne     .next_socket
  1338.  
  1339.         cmp     [esi + IP_SOCKET.LocalIP], edx
  1340.         jne     .next_socket
  1341.  
  1342.         cmp     [esi + UDP_SOCKET.LocalPort], bx
  1343.         jne     .next_socket
  1344.  
  1345.         DEBUGF  2,"local port %x already in use\n", bx  ; FIXME: find a way to print big endian values with debugf
  1346.         ret
  1347.  
  1348.   .port_ok:
  1349.         DEBUGF  2,"local port %x is free\n", bx         ; FIXME: find a way to print big endian values with debugf
  1350.         mov     [eax + UDP_SOCKET.LocalPort], bx
  1351.         or      bx, bx                                  ; clear the zero-flag
  1352.         ret
  1353.  
  1354.  
  1355.  
  1356. ;-----------------------------------------------------------------
  1357. ;
  1358. ; SOCKET_input
  1359. ;
  1360. ; Updates a (stateless) socket with received data
  1361. ;
  1362. ; Note: the mutex should already be set !
  1363. ;
  1364. ;  IN:  eax = socket ptr
  1365. ;       ecx = data size
  1366. ;       esi = ptr to data
  1367. ;       [esp] = ptr to buf
  1368. ;       [esp + 4] = buf size
  1369. ;
  1370. ;  OUT: /
  1371. ;
  1372. ;-----------------------------------------------------------------
  1373. align 4
  1374. SOCKET_input:
  1375.  
  1376.         DEBUGF  2,"SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
  1377.  
  1378.         mov     [esp+4], ecx
  1379.         push    esi
  1380.         mov     esi, esp
  1381.  
  1382.         add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, SOCKET_input.full
  1383.  
  1384.         DEBUGF  1,"SOCKET_input: success\n"
  1385.         add     esp, sizeof.socket_queue_entry
  1386.  
  1387.         pusha
  1388.         lea     ecx, [eax + SOCKET.mutex]
  1389.         call    mutex_unlock
  1390.         popa
  1391.  
  1392.         jmp     SOCKET_notify
  1393.  
  1394.   .full:
  1395.         DEBUGF  2,"SOCKET_input: socket %x is full!\n", eax
  1396.  
  1397.         pusha
  1398.         lea     ecx, [eax + SOCKET.mutex]
  1399.         call    mutex_unlock
  1400.         popa
  1401.  
  1402.         call    kernel_free
  1403.         add     esp, 8
  1404.  
  1405.         ret
  1406.  
  1407.  
  1408. ;--------------------------
  1409. ;
  1410. ; eax = ptr to ring struct (just a buffer of the right size)
  1411. ;
  1412. align 4
  1413. SOCKET_ring_create:
  1414.  
  1415.         push    esi
  1416.         mov     esi, eax
  1417.  
  1418.         push    edx
  1419.         stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW
  1420.         pop     edx
  1421.  
  1422.         DEBUGF  1,"SOCKET_ring_created: %x\n", eax
  1423.  
  1424.         pusha
  1425.         lea     ecx, [esi + RING_BUFFER.mutex]
  1426.         call    mutex_init
  1427.         popa
  1428.  
  1429.         mov     [esi + RING_BUFFER.start_ptr], eax
  1430.         mov     [esi + RING_BUFFER.write_ptr], eax
  1431.         mov     [esi + RING_BUFFER.read_ptr], eax
  1432.         mov     [esi + RING_BUFFER.size], 0
  1433.         add     eax, SOCKET_MAXDATA
  1434.         mov     [esi + RING_BUFFER.end_ptr], eax
  1435.         mov     eax, esi
  1436.         pop     esi
  1437.  
  1438.         ret
  1439.  
  1440. ;-----------------------------------------------------------------
  1441. ;
  1442. ; SOCKET_ring_write
  1443. ;
  1444. ; Adds data to a stream socket, and updates write pointer and size
  1445. ;
  1446. ;  IN:  eax = ptr to ring struct
  1447. ;       ecx = data size
  1448. ;       esi = ptr to data
  1449. ;
  1450. ;  OUT: ecx = number of bytes stored
  1451. ;
  1452. ;-----------------------------------------------------------------
  1453. align 4
  1454. SOCKET_ring_write:
  1455.  
  1456.         DEBUGF  1,"SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
  1457.  
  1458. ; lock mutex
  1459.         pusha
  1460.         lea     ecx, [eax + RING_BUFFER.mutex]
  1461.         call    mutex_lock                                      ; TODO: check what registers this function actually destroys
  1462.         popa
  1463.  
  1464. ; calculate available size
  1465.         mov     edi, SOCKET_MAXDATA
  1466.         sub     edi, [eax + RING_BUFFER.size]                   ; available buffer size in edi
  1467.         cmp     ecx, edi
  1468.         jbe     .copy
  1469.         mov     ecx, edi
  1470.   .copy:
  1471.         mov     edi, [eax + RING_BUFFER.write_ptr]
  1472.         DEBUGF  2,"SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
  1473.  
  1474. ; update write ptr
  1475.         push    edi
  1476.         add     edi, ecx
  1477.         cmp     edi, [eax + RING_BUFFER.end_ptr]
  1478.         jb      @f
  1479.         sub     edi, SOCKET_MAXDATA                             ; WRAP
  1480.   @@:
  1481.         mov     [eax + RING_BUFFER.write_ptr], edi
  1482.         pop     edi
  1483.  
  1484. ; update size
  1485.         add     [eax + RING_BUFFER.size], ecx
  1486.  
  1487. ; copy the data
  1488.         push    ecx
  1489.         shr     ecx, 1
  1490.         jnc     .nb
  1491.         movsb
  1492.   .nb:
  1493.         shr     ecx, 1
  1494.         jnc     .nw
  1495.         movsw
  1496.   .nw:
  1497.         test    ecx, ecx
  1498.         jz      .nd
  1499.         rep     movsd
  1500.   .nd:
  1501.         pop     ecx
  1502.  
  1503. ; unlock mutex
  1504.         push    eax ecx
  1505.         lea     ecx, [eax + RING_BUFFER.mutex]
  1506.         call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
  1507.         pop     ecx eax
  1508.  
  1509.         ret
  1510.  
  1511. ;-----------------------------------------------------------------
  1512. ;
  1513. ; SOCKET_ring_read
  1514. ;
  1515. ;  IN:  eax = ring struct ptr
  1516. ;       ecx = bytes to read
  1517. ;       edx = offset
  1518. ;       edi = ptr to buffer start
  1519. ;
  1520. ;  OUT: eax = unchanged
  1521. ;       ecx = number of bytes read (0 on error)
  1522. ;       edx = destroyed
  1523. ;       esi = destroyed
  1524. ;       edi = ptr to buffer end
  1525. ;
  1526. ;-----------------------------------------------------------------
  1527. align 4
  1528. SOCKET_ring_read:
  1529.  
  1530.         DEBUGF  1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx
  1531.  
  1532.         pusha
  1533.         lea     ecx, [eax + RING_BUFFER.mutex]
  1534.         call    mutex_lock                                      ; TODO: check what registers this function actually destroys
  1535.         popa
  1536.  
  1537.         mov     esi, [eax + RING_BUFFER.read_ptr]
  1538.         add     esi, edx                                        ; esi = start_ptr + offset
  1539.  
  1540.         neg     edx
  1541.         add     edx, [eax + RING_BUFFER.size]                   ; edx = snd.size - offset
  1542.         jle     .no_data_at_all
  1543.  
  1544.         pusha
  1545.         lea     ecx, [eax + RING_BUFFER.mutex]
  1546.         call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
  1547.         popa
  1548.  
  1549.         cmp     ecx, edx
  1550.         ja      .less_data
  1551.  
  1552.   .copy:
  1553.         DEBUGF  2,"SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
  1554.         push    ecx
  1555.         shr     ecx, 1
  1556.         jnc     .nb
  1557.         movsb
  1558.   .nb:
  1559.         shr     ecx, 1
  1560.         jnc     .nw
  1561.         movsw
  1562.   .nw:
  1563.         test    ecx, ecx
  1564.         jz      .nd
  1565.         rep     movsd
  1566.   .nd:
  1567.         pop     ecx
  1568.         ret
  1569.  
  1570.   .no_data_at_all:
  1571.         pusha
  1572.         lea     ecx, [eax + RING_BUFFER.mutex]
  1573.         call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
  1574.         popa
  1575.  
  1576.         DEBUGF  1,"SOCKET_ring_read: no data at all!\n"
  1577.         xor     ecx, ecx
  1578.         ret
  1579.  
  1580.   .less_data:
  1581.         mov     ecx, edx
  1582.         jmp     .copy
  1583.  
  1584.  
  1585. ;-----------------------------------------------------------------
  1586. ;
  1587. ; SOCKET_ring_free
  1588. ;
  1589. ; Free's some bytes from the ringbuffer
  1590. ;
  1591. ;  IN:  eax = ptr to ring struct
  1592. ;       ecx = data size
  1593. ;
  1594. ;  OUT: ecx = number of bytes free-ed
  1595. ;
  1596. ;-----------------------------------------------------------------
  1597. align 4
  1598. SOCKET_ring_free:
  1599.  
  1600.         DEBUGF  1,"SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
  1601.  
  1602.         push    eax ecx
  1603.         lea     ecx, [eax + RING_BUFFER.mutex]
  1604.         call    mutex_lock                                      ; TODO: check what registers this function actually destroys
  1605.         pop     ecx eax
  1606.  
  1607.         sub     [eax + RING_BUFFER.size], ecx
  1608.         jb      .error
  1609.         add     [eax + RING_BUFFER.read_ptr], ecx
  1610.  
  1611.         mov     edx, [eax + RING_BUFFER.end_ptr]
  1612.         cmp     [eax + RING_BUFFER.read_ptr], edx
  1613.         jb      @f
  1614.         sub     [eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA
  1615.        @@:
  1616.  
  1617.         push    eax ecx
  1618.         lea     ecx, [eax + RING_BUFFER.mutex]                  ; TODO: check what registers this function actually destroys
  1619.         call    mutex_unlock
  1620.         pop     ecx eax
  1621.  
  1622.         ret
  1623.  
  1624.   .error:       ; we could free all available bytes, but that would be stupid, i guess..
  1625.         DEBUGF  1,"SOCKET_ring_free: buffer=%x error!\n", eax
  1626.         add     [eax + RING_BUFFER.size], ecx
  1627.  
  1628.         push    eax
  1629.         lea     ecx, [eax + RING_BUFFER.mutex]
  1630.         call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
  1631.         pop     eax
  1632.  
  1633.         xor     ecx, ecx
  1634.         ret
  1635.  
  1636.  
  1637. ;-----------------------------------------------------------------
  1638. ;
  1639. ; SOCKET_block
  1640. ;
  1641. ; Suspends the thread attached to a socket
  1642. ;
  1643. ;  IN:  eax = socket ptr
  1644. ;  OUT: /
  1645. ;
  1646. ;-----------------------------------------------------------------
  1647. align 4
  1648. SOCKET_block:
  1649.  
  1650.         DEBUGF  1,"SOCKET_block: %x\n", eax
  1651.  
  1652.         pushf
  1653.         cli
  1654.  
  1655.         ; Set the 'socket is blocked' flag
  1656.         or      [eax + SOCKET.state], SS_BLOCKED
  1657.  
  1658.         ; Suspend the thread
  1659.         push    edx
  1660.         mov     edx, [TASK_BASE]
  1661.         mov     [edx + TASKDATA.state], 1               ; Suspended
  1662.  
  1663.         ; Remember the thread ID so we can wake it up again
  1664.         mov     edx, [edx + TASKDATA.pid]
  1665.         DEBUGF  1,"SOCKET_block: suspending thread: %u\n", edx
  1666.         mov     [eax + SOCKET.TID], edx
  1667.         pop     edx
  1668.  
  1669.         call    change_task
  1670.         popf
  1671.  
  1672.         DEBUGF  1,"SOCKET_block: continueing\n"
  1673.  
  1674.         ret
  1675.  
  1676.  
  1677. ;-----------------------------------------------------------------
  1678. ;
  1679. ; SOCKET_notify
  1680. ;
  1681. ; notify's the owner of a socket that something happened
  1682. ;
  1683. ;  IN:  eax = socket ptr
  1684. ;  OUT: /
  1685. ;
  1686. ;-----------------------------------------------------------------
  1687. align 4
  1688. SOCKET_notify:
  1689.  
  1690.         DEBUGF  1,"SOCKET_notify: %x\n", eax
  1691.  
  1692.         call    SOCKET_check
  1693.         jz      .error
  1694.  
  1695.         test    [eax + SOCKET.state], SS_BLOCKED
  1696.         jnz     .unblock
  1697.  
  1698.         test    [eax + SOCKET.options], SO_NONBLOCK
  1699.         jz      .error
  1700.  
  1701.         push    eax ecx esi
  1702.  
  1703. ; socket exists and is of non blocking type.
  1704. ; We'll try to flag an event to the thread
  1705.  
  1706.         mov     eax, [eax + SOCKET.TID]
  1707.         test    eax, eax
  1708.         jz      .done
  1709.         mov     ecx, 1
  1710.         mov     esi, TASK_DATA + TASKDATA.pid
  1711.  
  1712.   .next_pid:
  1713.         cmp     [esi], eax
  1714.         je      .found_pid
  1715.         inc     ecx
  1716.         add     esi, 0x20
  1717.         cmp     ecx, [TASK_COUNT]
  1718.         jbe     .next_pid
  1719. ; PID not found, TODO: close socket!
  1720.         jmp     .done
  1721.  
  1722.   .found_pid:
  1723.         shl     ecx, 8
  1724.         or      [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK
  1725.         mov     [check_idle_semaphore], 200                             ; What does this mean??
  1726.  
  1727.         DEBUGF  1,"SOCKET_notify: Raised a network event!\n"
  1728.  
  1729.         jmp     .done
  1730.  
  1731.   .unblock:
  1732.         push    eax ecx esi
  1733.         ; Clear the 'socket is blocked' flag
  1734.         and     [eax + SOCKET.state], not SS_BLOCKED
  1735.  
  1736.         ; Find the thread's TASK_DATA
  1737.         mov     eax, [eax + SOCKET.TID]
  1738.         test    eax, eax
  1739.         jz      .error
  1740.         xor     ecx, ecx
  1741.         inc     ecx
  1742.         mov     esi, TASK_DATA
  1743.   .next:
  1744.         cmp     [esi + TASKDATA.pid], eax
  1745.         je      .found
  1746.         inc     ecx
  1747.         add     esi, 0x20
  1748.         cmp     ecx, [TASK_COUNT]
  1749.         jbe     .next
  1750.         jmp     .error
  1751.   .found:
  1752.  
  1753.         ; Run the thread
  1754.         mov     [esi + TASKDATA.state], 0       ; Running
  1755.         DEBUGF  1,"SOCKET_notify: Unblocked socket!\n"
  1756.  
  1757.   .done:
  1758.         pop     esi ecx eax
  1759.  
  1760.   .error:
  1761.         ret
  1762.  
  1763.  
  1764. ;--------------------------------------------------------------------
  1765. ;
  1766. ; SOCKET_alloc
  1767. ;
  1768. ; Allocate memory for socket data and put new socket into the list
  1769. ; Newly created socket is initialized with calling PID and number and
  1770. ; put into beginning of list (which is a fastest way).
  1771. ;
  1772. ; IN:  /
  1773. ; OUT: eax = 0 on error, socket ptr otherwise
  1774. ;      edi = socket number
  1775. ;       ZF = cleared on error
  1776. ;
  1777. ;--------------------------------------------------------------------
  1778. align 4
  1779. SOCKET_alloc:
  1780.  
  1781.         push    ebx
  1782.  
  1783.         stdcall kernel_alloc, SOCKETBUFFSIZE
  1784.         DEBUGF  1, "SOCKET_alloc: ptr=%x\n", eax
  1785.         or      eax, eax
  1786.         jz      .exit
  1787.  
  1788. ; zero-initialize allocated memory
  1789.         push    eax
  1790.         mov     edi, eax
  1791.         mov     ecx, SOCKETBUFFSIZE / 4
  1792.         xor     eax, eax
  1793.         rep     stosd
  1794.         pop     eax
  1795.  
  1796. ; set send-and receive procedures to return -1
  1797.         mov     [eax + SOCKET.snd_proc], s_error
  1798.         mov     [eax + SOCKET.rcv_proc], s_error
  1799.  
  1800. ; find first free socket number and use it
  1801.         mov     edi, [last_socket_num]
  1802.   .next_socket_number:
  1803.         inc     edi
  1804.         jz      .next_socket_number     ; avoid socket nr 0
  1805.         cmp     edi, -1
  1806.         je      .next_socket_number     ; avoid socket nr -1
  1807.         mov     ebx, net_sockets
  1808.   .next_socket:
  1809.         mov     ebx, [ebx + SOCKET.NextPtr]
  1810.         test    ebx, ebx
  1811.         jz      .last_socket
  1812.  
  1813.         cmp     [ebx + SOCKET.Number], edi
  1814.         jne     .next_socket
  1815.         jmp     .next_socket_number
  1816.  
  1817.   .last_socket:
  1818.         mov     [last_socket_num], edi
  1819.         mov     [eax + SOCKET.Number], edi
  1820.         DEBUGF  1, "SOCKET_alloc: number=%u\n", edi
  1821.  
  1822. ; Fill in PID
  1823.         mov     ebx, [TASK_BASE]
  1824.         mov     ebx, [ebx + TASKDATA.pid]
  1825.         mov     [eax + SOCKET.PID], ebx
  1826.         mov     [eax + SOCKET.TID], ebx         ; currently TID = PID in kolibrios :(
  1827.  
  1828. ; init mutex
  1829.         pusha
  1830.         lea     ecx, [eax + SOCKET.mutex]
  1831.         call    mutex_init
  1832.         popa
  1833.  
  1834. ; add socket to the list by re-arranging some pointers
  1835.         mov     ebx, [net_sockets + SOCKET.NextPtr]
  1836.  
  1837.         mov     [eax + SOCKET.PrevPtr], net_sockets
  1838.         mov     [eax + SOCKET.NextPtr], ebx
  1839.  
  1840.         test    ebx, ebx
  1841.         jz      @f
  1842.  
  1843.         pusha
  1844.         lea     ecx, [ebx + SOCKET.mutex]
  1845.         call    mutex_lock
  1846.         popa
  1847.  
  1848.         mov     [ebx + SOCKET.PrevPtr], eax
  1849.  
  1850.         pusha
  1851.         lea     ecx, [ebx + SOCKET.mutex]
  1852.         call    mutex_unlock
  1853.         popa
  1854.        @@:
  1855.  
  1856.         mov     [net_sockets + SOCKET.NextPtr], eax
  1857.         or      eax, eax                ; used to clear zero flag
  1858.   .exit:
  1859.         pop     ebx
  1860.  
  1861.         ret
  1862.  
  1863.  
  1864. ;----------------------------------------------------
  1865. ;
  1866. ; SOCKET_free
  1867. ;
  1868. ; Free socket data memory and remove socket from the list
  1869. ;
  1870. ; IN:  eax = socket ptr
  1871. ; OUT: /
  1872. ;
  1873. ;----------------------------------------------------
  1874. align 4
  1875. SOCKET_free:
  1876.  
  1877.         DEBUGF  1, "SOCKET_free: %x\n", eax
  1878.  
  1879.         call    SOCKET_check
  1880.         jz      .error
  1881.  
  1882.         push    ebx
  1883.  
  1884.         pusha
  1885.         lea     ecx, [eax + SOCKET.mutex]
  1886.         call    mutex_lock
  1887.         popa
  1888.  
  1889.         cmp     [eax + SOCKET.Domain], AF_INET4
  1890.         jnz     .no_tcp
  1891.  
  1892.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  1893.         jnz     .no_tcp
  1894.  
  1895.         mov     ebx, eax
  1896.         stdcall kernel_free, [ebx + STREAM_SOCKET.rcv.start_ptr]
  1897.         stdcall kernel_free, [ebx + STREAM_SOCKET.snd.start_ptr]
  1898.         mov     eax, ebx
  1899.   .no_tcp:
  1900.  
  1901.         push    eax                             ; this will be passed to kernel_free
  1902.         mov     ebx, [eax + SOCKET.NextPtr]
  1903.         mov     eax, [eax + SOCKET.PrevPtr]
  1904.  
  1905.         DEBUGF  1, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
  1906.  
  1907.         test    eax, eax
  1908.         jz      @f
  1909.         mov     [eax + SOCKET.NextPtr], ebx
  1910.        @@:
  1911.  
  1912.         test    ebx, ebx
  1913.         jz      @f
  1914.         mov     [ebx + SOCKET.PrevPtr], eax
  1915.        @@:
  1916.  
  1917.         call    kernel_free
  1918.         pop     ebx
  1919.  
  1920.         DEBUGF  1, "SOCKET_free: success!\n"
  1921.  
  1922.   .error:
  1923.         ret
  1924.  
  1925. ;------------------------------------
  1926. ;
  1927. ; SOCKET_fork
  1928. ;
  1929. ; Create a child socket
  1930. ;
  1931. ; IN:  socket nr in ebx
  1932. ; OUT: child socket nr in eax
  1933. ;
  1934. ;-----------------------------------
  1935. align 4
  1936. SOCKET_fork:
  1937.  
  1938.         DEBUGF  1,"SOCKET_fork: %x\n", ebx
  1939.  
  1940. ; Exit if backlog queue is full
  1941.         mov     eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
  1942.         cmp     ax, [ebx + SOCKET.backlog]
  1943.         jae     .fail
  1944.  
  1945. ; Allocate new socket
  1946.         push    ebx
  1947.         call    SOCKET_alloc
  1948.         pop     ebx
  1949.         jz      .fail
  1950.  
  1951.         push    eax
  1952.         mov     esi, esp
  1953.         add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
  1954.         pop     eax
  1955.  
  1956. ; Copy structure from current socket to new
  1957. ; We start at PID to preserve the socket num, and the 2 pointers at beginning of socket
  1958.         lea     esi, [ebx + SOCKET.PID]
  1959.         lea     edi, [eax + SOCKET.PID]
  1960.         mov     ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
  1961.         rep     movsd
  1962.  
  1963.         and     [eax + SOCKET.options], not SO_ACCEPTCON
  1964.  
  1965.         ret
  1966.  
  1967.   .fail2:
  1968.         add     esp, 4+4+4
  1969.   .fail:
  1970.         DEBUGF  1,"SOCKET_fork: failed\n"
  1971.         xor     eax, eax
  1972.         ret
  1973.  
  1974.  
  1975. ;---------------------------------------------------
  1976. ;
  1977. ; SOCKET_num_to_ptr
  1978. ;
  1979. ; Get socket structure address by its number
  1980. ;
  1981. ; IN:  ecx = socket number
  1982. ; OUT: eax = 0 on error, socket ptr otherwise
  1983. ;       ZF = set on error
  1984. ;
  1985. ;---------------------------------------------------
  1986. align 4
  1987. SOCKET_num_to_ptr:
  1988.  
  1989.         DEBUGF  1,"SOCKET_num_to_ptr: num=%u ", ecx
  1990.  
  1991.         mov     eax, net_sockets
  1992.  
  1993.   .next_socket:
  1994.         mov     eax, [eax + SOCKET.NextPtr]
  1995.         or      eax, eax
  1996.         jz      .error
  1997.         cmp     [eax + SOCKET.Number], ecx
  1998.         jne     .next_socket
  1999.  
  2000.         test    eax, eax
  2001.  
  2002.         DEBUGF  1,"ptr=%x\n", eax
  2003.         ret
  2004.  
  2005.   .error:
  2006.         DEBUGF  1,"not found\n", eax
  2007.         ret
  2008.  
  2009.  
  2010. ;---------------------------------------------------
  2011. ;
  2012. ; SOCKET_ptr_to_num
  2013. ;
  2014. ; Get socket number by its address
  2015. ;
  2016. ; IN:  eax = socket ptr
  2017. ; OUT: eax = 0 on error, socket num otherwise
  2018. ;       ZF = set on error
  2019. ;
  2020. ;---------------------------------------------------
  2021. align 4
  2022. SOCKET_ptr_to_num:
  2023.  
  2024.         DEBUGF  1,"SOCKET_ptr_to_num: ptr=%x ", eax
  2025.  
  2026.         call    SOCKET_check
  2027.         jz      .error
  2028.  
  2029.         mov     eax, [eax + SOCKET.Number]
  2030.  
  2031.         DEBUGF  1,"num=%u\n", eax
  2032.         ret
  2033.  
  2034.   .error:
  2035.         DEBUGF  1,"not found\n", eax
  2036.         ret
  2037.  
  2038.  
  2039. ;---------------------------------------------------
  2040. ;
  2041. ; SOCKET_check
  2042. ;
  2043. ; checks if the given value is really a socket ptr
  2044. ;
  2045. ; IN:  eax = socket ptr
  2046. ; OUT: eax = 0 on error, unchanged otherwise
  2047. ;       ZF = set on error
  2048. ;
  2049. ;---------------------------------------------------
  2050. align 4
  2051. SOCKET_check:
  2052.  
  2053.         DEBUGF  1,"SOCKET_check: %x\n", eax
  2054.  
  2055.         push    ebx
  2056.         mov     ebx, net_sockets
  2057.  
  2058.   .next_socket:
  2059.         mov     ebx, [ebx + SOCKET.NextPtr]
  2060.         or      ebx, ebx
  2061.         jz      .done
  2062.         cmp     ebx, eax
  2063.         jnz     .next_socket
  2064.  
  2065.   .done:
  2066.         mov     eax, ebx
  2067.         test    eax, eax
  2068.         pop     ebx
  2069.  
  2070.         ret
  2071.  
  2072.  
  2073.  
  2074. ;---------------------------------------------------
  2075. ;
  2076. ; SOCKET_check_owner
  2077. ;
  2078. ; checks if the caller application owns the socket
  2079. ;
  2080. ; IN:  eax = socket ptr
  2081. ; OUT:  ZF = true/false
  2082. ;
  2083. ;---------------------------------------------------
  2084. align 4
  2085. SOCKET_check_owner:
  2086.  
  2087.         DEBUGF  1,"SOCKET_check_owner: %x\n", eax
  2088.  
  2089.         push    ebx
  2090.         mov     ebx, [TASK_BASE]
  2091.         mov     ebx, [ebx + TASKDATA.pid]
  2092.         cmp     [eax + SOCKET.PID], ebx
  2093.         pop      ebx
  2094.  
  2095.         ret
  2096.  
  2097.  
  2098.  
  2099.  
  2100. ;------------------------------------------------------
  2101. ;
  2102. ; SOCKET_process_end
  2103. ;
  2104. ; Kernel calls this function when a certain process ends
  2105. ; This function will check if the process had any open sockets
  2106. ; And update them accordingly
  2107. ;
  2108. ; IN:  edx = pid
  2109. ; OUT: /
  2110. ;
  2111. ;------------------------------------------------------
  2112. align 4
  2113. SOCKET_process_end:
  2114.  
  2115.         DEBUGF  1, "SOCKET_process_end: %x\n", edx
  2116.  
  2117.         push    ebx
  2118.         mov     ebx, net_sockets
  2119.  
  2120.   .next_socket:
  2121.         mov     ebx, [ebx + SOCKET.NextPtr]
  2122.   .next_socket_test:
  2123.         test    ebx, ebx
  2124.         jz      .done
  2125.  
  2126.         cmp     [ebx + SOCKET.PID], edx
  2127.         jne     .next_socket
  2128.  
  2129.         DEBUGF  1, "SOCKET_process_end: killing socket %x\n", ebx
  2130.  
  2131.         mov     [ebx + SOCKET.PID], 0
  2132.         mov     eax, ebx
  2133.         mov     ebx, [ebx + SOCKET.NextPtr]
  2134.         pusha
  2135.         call    SOCKET_close.socket
  2136.         popa
  2137.         jmp     .next_socket_test
  2138.  
  2139.   .done:
  2140.         pop     ebx
  2141.  
  2142.         ret
  2143.  
  2144.  
  2145.  
  2146.  
  2147. ;-----------------------------------------------------------------
  2148. ;
  2149. ; SOCKET_is_connecting
  2150. ;
  2151. ;  IN:  eax = socket ptr
  2152. ;  OUT: /
  2153. ;
  2154. ;-----------------------------------------------------------------
  2155.  
  2156. align 4
  2157. SOCKET_is_connecting:
  2158.  
  2159.         DEBUGF  1,"SOCKET_is_connecting: %x\n", eax
  2160.  
  2161.         and     [eax + SOCKET.options], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING)
  2162.         or      [eax + SOCKET.options], SS_ISCONNECTING
  2163.  
  2164.         jmp     SOCKET_notify
  2165.  
  2166.  
  2167.  
  2168. ;-----------------------------------------------------------------
  2169. ;
  2170. ; SOCKET_is_connected
  2171. ;
  2172. ;  IN:  eax = socket ptr
  2173. ;  OUT: /
  2174. ;
  2175. ;-----------------------------------------------------------------
  2176.  
  2177. align 4
  2178. SOCKET_is_connected:
  2179.  
  2180.         DEBUGF  1,"SOCKET_is_connected: %x\n", eax
  2181.  
  2182.         and     [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
  2183.         or      [eax + SOCKET.options], SS_ISCONNECTED
  2184.  
  2185.         jmp     SOCKET_notify
  2186.  
  2187.  
  2188.  
  2189.  
  2190. ;-----------------------------------------------------------------
  2191. ;
  2192. ; SOCKET_is_disconnecting
  2193. ;
  2194. ;  IN:  eax = socket ptr
  2195. ;  OUT: /
  2196. ;
  2197. ;-----------------------------------------------------------------
  2198.  
  2199. align 4
  2200. SOCKET_is_disconnecting:
  2201.  
  2202.         DEBUGF  1,"SOCKET_is_disconnecting: %x\n", eax
  2203.  
  2204.         and     [eax + SOCKET.options], not (SS_ISCONNECTING)
  2205.         or      [eax + SOCKET.options], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE
  2206.  
  2207.         jmp     SOCKET_notify
  2208.  
  2209.  
  2210.  
  2211. ;-----------------------------------------------------------------
  2212. ;
  2213. ; SOCKET_is_disconnected
  2214. ;
  2215. ;  IN:  eax = socket ptr
  2216. ;  OUT: /
  2217. ;
  2218. ;-----------------------------------------------------------------
  2219.  
  2220. align 4
  2221. SOCKET_is_disconnected:
  2222.  
  2223.         DEBUGF  1,"SOCKET_is_disconnected: %x\n", eax
  2224.  
  2225.         and     [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING)
  2226.         or      [eax + SOCKET.options], SS_CANTRCVMORE + SS_CANTSENDMORE
  2227.  
  2228.         jmp     SOCKET_notify
  2229.  
  2230.  
  2231. ;-----------------------------------------------------------------
  2232. ;
  2233. ; SOCKET_cant_recv_more
  2234. ;
  2235. ;  IN:  eax = socket ptr
  2236. ;  OUT: /
  2237. ;
  2238. ;-----------------------------------------------------------------
  2239.  
  2240. align 4
  2241. SOCKET_cant_recv_more:
  2242.  
  2243.         DEBUGF  1,"SOCKET_cant_recv_more: %x\n", eax
  2244.  
  2245.         or      [eax + SOCKET.options], SS_CANTRCVMORE
  2246.  
  2247.         ret
  2248.  
  2249.  
  2250.  
  2251. ;-----------------------------------------------------------------
  2252. ;
  2253. ; SOCKET_cant_send_more
  2254. ;
  2255. ;  IN:  eax = socket ptr
  2256. ;  OUT: /
  2257. ;
  2258. ;-----------------------------------------------------------------
  2259.  
  2260. align 4
  2261. SOCKET_cant_send_more:
  2262.  
  2263.         DEBUGF  1,"SOCKET_cant_send_more: %x\n", eax
  2264.  
  2265.         or      [eax + SOCKET.options], SS_CANTSENDMORE
  2266.  
  2267.         ret