Subversion Repositories Kolibri OS

Rev

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