Subversion Repositories Kolibri OS

Rev

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