Subversion Repositories Kolibri OS

Rev

Rev 9917 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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