Subversion Repositories Kolibri OS

Rev

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

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