Subversion Repositories Kolibri OS

Rev

Rev 9993 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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