Subversion Repositories Kolibri OS

Rev

Rev 6907 | Rev 6916 | 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: 6908 $
  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  DEBUG_NETWORK_VERBOSE, "SOCKET_open: domain=%u type=%u protocol=%x ", 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.  
  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.  
  734.         mov     dword[esp+32], 0                                ; The socket exists, so we will succeed in closing it.
  735.  
  736.         or      [eax + SOCKET.options], SO_NONBLOCK             ; Mark the socket as non blocking, we dont want it to block any longer!
  737.  
  738.         test    [eax + SOCKET.state], SS_BLOCKED                ; Is the socket still in blocked state?
  739.         jz      @f
  740.         call    socket_notify                                   ; Unblock it.
  741.   @@:
  742.  
  743.         cmp     [eax + SOCKET.Domain], AF_INET4
  744.         jne     .free
  745.  
  746.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  747.         je      .tcp
  748.  
  749.   .free:
  750.         call    socket_free
  751.         ret
  752.  
  753.   .tcp:
  754.         call    tcp_usrclosed
  755.  
  756.         test    eax, eax
  757.         jz      @f
  758.         call    tcp_output                                      ; If connection is not closed yet, send the FIN
  759.   @@:
  760.         ret
  761.  
  762.  
  763.   .invalid:
  764.         mov     dword[esp+20], EINVAL
  765.         mov     dword[esp+32], -1
  766.         ret
  767.  
  768.  
  769. ;-----------------------------------------------------------------;
  770. ;                                                                 ;
  771. ; socket_receive: Receive some data from the remote end.          ;
  772. ;                                                                 ;
  773. ;   IN: ecx = socket number                                       ;
  774. ;       edx = addr to application buffer                          ;
  775. ;       edx = length of application buffer                        ;
  776. ;       edi = flags                                               ;
  777. ;                                                                 ;
  778. ;  OUT: eax = number of bytes copied                              ;
  779. ;       eax = -1 on error                                         ;
  780. ;       eax = 0 when socket has been closed by the remote end     ;
  781. ;       ebx = errorcode on error                                  ;
  782. ;                                                                 ;
  783. ;-----------------------------------------------------------------;
  784. align 4
  785. socket_receive:
  786.  
  787.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: socknum=%u bufaddr=%x buflength=%u flags=%x\n", ecx, edx, esi, edi
  788.  
  789.         call    socket_num_to_ptr
  790.         test    eax, eax
  791.         jz      .invalid
  792.  
  793.   .loop:
  794.         push    edi
  795.         call    [eax + SOCKET.rcv_proc]
  796.         pop     edi
  797.  
  798.         test    [eax + SOCKET.state], SS_CANTRCVMORE
  799.         jnz     .last_data
  800.  
  801.         cmp     ebx, EWOULDBLOCK
  802.         jne     .return
  803.  
  804.         test    edi, MSG_DONTWAIT
  805.         jnz     .return_err
  806.  
  807.         test    [eax + SOCKET.options], SO_NONBLOCK
  808.         jnz     .return_err
  809.  
  810.         call    socket_block
  811.         jmp     .loop
  812.  
  813.  
  814.   .invalid:
  815.         push    EINVAL
  816.         pop     ebx
  817.   .return_err:
  818.         mov     ecx, -1
  819.   .return:
  820.         mov     [esp+20], ebx
  821.         mov     [esp+32], ecx
  822.         ret
  823.  
  824.   .last_data:
  825.         test    ecx, ecx
  826.         jz      .return
  827.         call    socket_notify                                   ; Call me again!
  828.         jmp     .return
  829.  
  830.  
  831.  
  832.  
  833. align 4
  834. socket_receive_dgram:
  835.  
  836.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: DGRAM\n"
  837.  
  838.         test    edi, MSG_PEEK
  839.         jnz     .peek
  840.  
  841.         mov     ebx, esi                                        ; buffer length
  842.  
  843.         get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .wouldblock ; sets esi only on success.
  844.         mov     ecx, [esi + socket_queue_entry.data_size]
  845.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: %u bytes data\n", ecx
  846.  
  847.         cmp     ecx, ebx                                        ; If data segment does not fit in applications buffer, abort
  848.         ja      .too_small
  849.  
  850.         push    eax ecx
  851.         push    [esi + socket_queue_entry.buf_ptr]              ; save the buffer addr so we can clear it later
  852.         mov     esi, [esi + socket_queue_entry.data_ptr]
  853.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: Source buffer=%x real addr=%x\n", [esp], esi
  854.  
  855. ; copy the data from kernel buffer to application buffer
  856.         mov     edi, edx                                        ; bufferaddr
  857.         shr     ecx, 1
  858.         jnc     .nb
  859.         movsb
  860.   .nb:
  861.         shr     ecx, 1
  862.         jnc     .nw
  863.         movsw
  864.   .nw:
  865.         test    ecx, ecx
  866.         jz      .nd
  867.         rep movsd
  868.   .nd:
  869.  
  870.         call    net_buff_free
  871.         pop     ecx eax                                         ; return number of bytes copied to application
  872.         cmp     [eax + SOCKET_QUEUE_LOCATION + queue.size], 0
  873.         je      @f
  874.         call    socket_notify                                   ; Queue another network event
  875.   @@:
  876.         xor     ebx, ebx                                        ; errorcode = 0 (no error)
  877.         ret
  878.  
  879.   .too_small:
  880.         mov     ecx, -1
  881.         push    EMSGSIZE
  882.         pop     ebx
  883.         ret
  884.  
  885.   .wouldblock:
  886.         push    EWOULDBLOCK
  887.         pop     ebx
  888.         ret
  889.  
  890.   .peek:
  891.         xor     ebx, ebx
  892.         xor     ecx, ecx
  893.         cmp     [eax + SOCKET_QUEUE_LOCATION + queue.size], 0
  894.         je      @f
  895.         mov     esi, [eax + SOCKET_QUEUE_LOCATION + queue.r_ptr]
  896.         mov     ecx, [esi + socket_queue_entry.data_size]
  897.   @@:
  898.         ret
  899.  
  900.  
  901. align 4
  902. socket_receive_local:
  903.  
  904.         ; does this socket have a PID yet?
  905.         cmp     [eax + SOCKET.PID], 0
  906.         jne     @f
  907.  
  908.         ; Change PID to that of current process
  909.         mov     ebx, [TASK_BASE]
  910.         mov     ebx, [ebx + TASKDATA.pid]
  911.         mov     [eax + SOCKET.PID], ebx
  912.         mov     [eax + SOCKET.TID], ebx                         ; currently TID = PID in kolibrios :(
  913.       @@:
  914.  
  915.         mov     [eax + SOCKET.rcv_proc], socket_receive_stream
  916.  
  917. ; ... continue to SOCKET_receive_stream
  918.  
  919. align 4
  920. socket_receive_stream:
  921.  
  922.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_receive: STREAM\n"
  923.  
  924.         cmp     [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0
  925.         je      .wouldblock
  926.  
  927.         test    edi, MSG_PEEK
  928.         jnz     .peek
  929.  
  930.         mov     ecx, esi
  931.         mov     edi, edx
  932.         xor     edx, edx
  933.  
  934.         push    eax
  935.         add     eax, STREAM_SOCKET.rcv
  936.         call    socket_ring_read                                ; copy data from kernel buffer to application buffer
  937.         call    socket_ring_free                                ; free read memory
  938.         pop     eax
  939.  
  940.         cmp     [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0
  941.         jne     .more_data
  942.         xor     ebx, ebx                                        ; errorcode = 0 (no error)
  943.         ret
  944.  
  945.   .more_data:
  946.         call    socket_notify                                   ; Queue another network event
  947.         xor     ebx, ebx                                        ; errorcode = 0 (no error)
  948.         ret
  949.  
  950.   .wouldblock:
  951.         push    EWOULDBLOCK
  952.         pop     ebx
  953.         xor     ecx, ecx
  954.         ret
  955.  
  956.   .peek:
  957.         mov     ecx, [eax + STREAM_SOCKET.rcv + RING_BUFFER.size]
  958.         xor     ebx, ebx
  959.         ret
  960.  
  961.  
  962. ;-----------------------------------------------------------------;
  963. ;                                                                 ;
  964. ; socket_send: Send some data to the remote end.                  ;
  965. ;                                                                 ;
  966. ;   IN: ecx = socket number                                       ;
  967. ;       edx = pointer to data                                     ;
  968. ;       esi = data length                                         ;
  969. ;       edi = flags                                               ;
  970. ;                                                                 ;
  971. ;  OUT: eax = number of bytes sent                                ;
  972. ;       eax = -1 on error                                         ;
  973. ;       ebx = errorcode on error                                  ;
  974. ;                                                                 ;
  975. ;-----------------------------------------------------------------;
  976. align 4
  977. socket_send:
  978.  
  979.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi
  980.  
  981.         call    socket_num_to_ptr
  982.         test    eax, eax
  983.         jz      .invalid
  984.  
  985.         mov     ecx, esi
  986.         mov     esi, edx
  987.  
  988.         jmp     [eax + SOCKET.snd_proc]
  989.  
  990.   .invalid:
  991.         mov     dword[esp+20], EINVAL
  992.         mov     dword[esp+32], -1
  993.         ret
  994.  
  995.  
  996. align 4
  997. socket_send_udp:
  998.  
  999.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: UDP\n"
  1000.  
  1001.         mov     [esp+32], ecx
  1002.         call    udp_output
  1003.         cmp     eax, -1
  1004.         je      .error
  1005.         ret
  1006.  
  1007.   .error:
  1008.         mov     dword[esp+32], -1
  1009.         mov     dword[esp+20], EMSGSIZE ; FIXME: UDP_output should return error codes!
  1010.         ret
  1011.  
  1012.  
  1013. align 4
  1014. socket_send_tcp:
  1015.  
  1016.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: TCP\n"
  1017.  
  1018.         push    eax
  1019.         add     eax, STREAM_SOCKET.snd
  1020.         call    socket_ring_write
  1021.         pop     eax
  1022.  
  1023.         mov     [esp+32], ecx
  1024.         mov     [eax + SOCKET.errorcode], 0
  1025.         push    eax
  1026.         call    tcp_output              ; FIXME: this doesnt look pretty, does it?
  1027.         pop     eax
  1028.         mov     eax, [eax + SOCKET.errorcode]
  1029.         mov     [esp+20], eax
  1030.         ret
  1031.  
  1032.  
  1033. align 4
  1034. socket_send_ip:
  1035.  
  1036.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: IPv4\n"
  1037.  
  1038.         mov     [esp+32], ecx
  1039.         call    ipv4_output_raw
  1040.         cmp     eax, -1
  1041.         je      .error
  1042.         ret
  1043.  
  1044.   .error:
  1045.         mov     dword[esp+32], eax
  1046.         mov     dword[esp+20], ebx
  1047.         ret
  1048.  
  1049.  
  1050. align 4
  1051. socket_send_icmp:
  1052.  
  1053.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: ICMP\n"
  1054.  
  1055.         mov     [esp+32], ecx
  1056.         call    icmp_output_raw
  1057.         cmp     eax, -1
  1058.         je      .error
  1059.         ret
  1060.  
  1061.   .error:
  1062.         mov     dword[esp+32], eax
  1063.         mov     dword[esp+20], ebx
  1064.         ret
  1065.  
  1066.  
  1067. align 4
  1068. socket_send_pppoe:
  1069.  
  1070.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: PPPoE\n"
  1071.  
  1072.         mov     [esp+32], ecx
  1073.         mov     ebx, [eax + SOCKET.device]
  1074.  
  1075.         call    pppoe_discovery_output  ; FIXME: errorcodes
  1076.         cmp     eax, -1
  1077.         je      .error
  1078.         ret
  1079.  
  1080.   .error:
  1081.         mov     dword[esp+32], -1
  1082.         mov     dword[esp+20], EMSGSIZE
  1083.         ret
  1084.  
  1085.  
  1086.  
  1087. align 4
  1088. socket_send_local:
  1089.  
  1090.         ; does this socket have a PID yet?
  1091.         cmp     [eax + SOCKET.PID], 0
  1092.         jne     @f
  1093.  
  1094.         ; Change PID to that of current process
  1095.         mov     ebx, [TASK_BASE]
  1096.         mov     ebx, [ebx + TASKDATA.pid]
  1097.         mov     [eax + SOCKET.PID], ebx
  1098.         mov     [eax + SOCKET.TID], ebx         ; currently TID = PID in kolibrios :(
  1099.       @@:
  1100.         mov     [eax + SOCKET.snd_proc], socket_send_local_initialized
  1101.  
  1102. align 4
  1103. socket_send_local_initialized:
  1104.  
  1105.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_send: LOCAL\n"
  1106.  
  1107.         ; get the other side's socket and check if it still exists
  1108.         mov     eax, [eax + SOCKET.device]
  1109.         call    socket_check
  1110.         jz      .invalid
  1111.  
  1112.         ; allright, shove in the data!
  1113.         push    eax
  1114.         add     eax, STREAM_SOCKET.rcv
  1115.         call    socket_ring_write
  1116.         pop     eax
  1117.  
  1118.         ; return the number of written bytes (or errorcode) to application
  1119.         mov     [esp+32], ecx
  1120.  
  1121.         ; and notify the other end
  1122.         call    socket_notify
  1123.  
  1124.         ret
  1125.  
  1126.   .invalid:
  1127.         mov     dword[esp+32], -1
  1128.         mov     dword[esp+20], EINVAL
  1129.         ret
  1130.  
  1131.  
  1132. ;-----------------------------------------------------------------;
  1133. ;                                                                 ;
  1134. ; socket_get_opt: Read a socket option                            ;
  1135. ;                                                                 ;
  1136. ;   IN: ecx = socket number                                       ;
  1137. ;       edx = pointer to socket options struct                    ;
  1138. ;                                                                 ;
  1139. ;  OUT: eax = 0 on success                                        ;
  1140. ;       eax = -1 on error                                         ;
  1141. ;       ebx = errorcode on error                                  ;
  1142. ;                                                                 ;
  1143. ;-----------------------------------------------------------------;
  1144. align 4
  1145. socket_get_opt:
  1146.  
  1147. ; FIXME:
  1148. ; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
  1149. ; TODO: find best way to notify that send()'ed data were acknowledged
  1150. ; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*.
  1151.  
  1152.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_get_opt\n"
  1153.  
  1154.         call    socket_num_to_ptr
  1155.         test    eax, eax
  1156.         jz      .invalid
  1157.  
  1158.         cmp     dword [edx], IP_PROTO_TCP
  1159.         jne     .invalid
  1160.         cmp     dword [edx+4], -2
  1161.         je      @f
  1162.         cmp     dword [edx+4], -3
  1163.         jne     .invalid
  1164. @@:
  1165. ;        mov     eax, [edx+12]
  1166. ;        test    eax, eax
  1167. ;        jz      .fail
  1168. ;        cmp     dword [eax], 4
  1169. ;        mov     dword [eax], 4
  1170. ;        jb      .fail
  1171. ;        stdcall net_socket_num_to_addr, ecx
  1172. ;        test    eax, eax
  1173. ;        jz      .fail
  1174. ;        ; todo: check that eax is really TCP socket
  1175. ;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
  1176. ;        cmp     dword [edx+4], -2
  1177. ;        jz      @f
  1178. ;        mov     ecx, [eax + TCP_SOCKET.state]
  1179. @@:
  1180.         mov     eax, [edx+8]
  1181.         test    eax, eax
  1182.         jz      @f
  1183.         mov     [eax], ecx
  1184. @@:
  1185.         mov     dword [esp+32], 0
  1186.         ret
  1187.  
  1188.   .invalid:
  1189.         mov     dword[esp+32], -1
  1190.         mov     dword[esp+20], EINVAL
  1191.         ret
  1192.  
  1193.  
  1194. ;-----------------------------------------------------------------;
  1195. ;                                                                 ;
  1196. ; socket_set_options: Set a socket option.                        ;
  1197. ;                                                                 ;
  1198. ;   IN: ecx = socket number                                       ;
  1199. ;       edx = pointer to socket options struct                    ;
  1200. ;                                                                 ;
  1201. ;  OUT: eax = 0 on success                                        ;
  1202. ;       eax = -1 on error                                         ;
  1203. ;       ebx = errorcode on error                                  ;
  1204. ;                                                                 ;
  1205. ;-----------------------------------------------------------------;
  1206. align 4
  1207. socket_set_opt:
  1208.  
  1209.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt\n"
  1210.  
  1211.         call    socket_num_to_ptr
  1212.         test    eax, eax
  1213.         jz      .invalid
  1214.  
  1215.         cmp     [edx + socket_options.level], IP_PROTO_IP
  1216.         je      .ip
  1217.         cmp     [edx + socket_options.level], SOL_SOCKET
  1218.         jne     .invalid
  1219.  
  1220.   .socket:
  1221.         cmp     [edx + socket_options.optname], SO_BINDTODEVICE
  1222.         jne     .invalid
  1223.  
  1224.   .bind:
  1225.         cmp     [edx + socket_options.optlen], 0
  1226.         je      .unbind
  1227.  
  1228.         movzx   edx, byte[edx + socket_options.optval]
  1229.         cmp     edx, NET_DEVICES_MAX
  1230.         ja      .invalid
  1231.  
  1232.         mov     edx, [NET_DRV_LIST + 4*edx]
  1233.         test    edx, edx
  1234.         jz      .already
  1235.         mov     [eax + SOCKET.device], edx
  1236.  
  1237.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_set_opt: Bound socket %x to device %x\n", eax, edx
  1238.  
  1239.         mov     dword[esp+32], 0        ; success!
  1240.         ret
  1241.  
  1242.   .unbind:
  1243.         mov     [eax + SOCKET.device], 0
  1244.  
  1245.         mov     dword[esp+32], 0        ; success!
  1246.         ret
  1247.  
  1248.   .ip:
  1249.         cmp     [edx + socket_options.optname], IP_TTL
  1250.         jne     .invalid
  1251.  
  1252.   .ttl:
  1253.         mov     bl, byte[edx + socket_options.optval]
  1254.         mov     [eax + IP_SOCKET.ttl], bl
  1255.  
  1256.         mov     dword[esp+32], 0        ; success!
  1257.         ret
  1258.  
  1259.   .already:
  1260.         mov     dword[esp+20], EALREADY
  1261.         mov     dword[esp+32], -1
  1262.         ret
  1263.  
  1264.   .invalid:
  1265.         mov     dword[esp+20], EINVAL
  1266.         mov     dword[esp+32], -1
  1267.         ret
  1268.  
  1269.  
  1270.  
  1271.  
  1272. ;-----------------------------------------------------------------;
  1273. ;                                                                 ;
  1274. ; socket_pair: Allocate a pair of linked local sockets.           ;
  1275. ;                                                                 ;
  1276. ;  IN: /                                                          ;
  1277. ;                                                                 ;
  1278. ; OUT: eax = socket1 num on success                               ;
  1279. ;      eax = -1 on error                                          ;
  1280. ;      ebx = socket2 num on success                               ;
  1281. ;      ebx = errorcode on error                                   ;
  1282. ;                                                                 ;
  1283. ;-----------------------------------------------------------------;
  1284. align 4
  1285. socket_pair:
  1286.  
  1287.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_pair\n"
  1288.  
  1289.         call    socket_alloc
  1290.         test    eax, eax
  1291.         jz      .nomem1
  1292.         mov     [esp+32], edi   ; application's eax
  1293.  
  1294.         mov     [eax + SOCKET.Domain], AF_LOCAL
  1295.         mov     [eax + SOCKET.Type], SOCK_STREAM
  1296.         mov     [eax + SOCKET.Protocol], 0              ;;; CHECKME
  1297.         mov     [eax + SOCKET.snd_proc], socket_send_local
  1298.         mov     [eax + SOCKET.rcv_proc], socket_receive_local
  1299.         mov     [eax + SOCKET.PID], 0
  1300.         mov     ebx, eax
  1301.  
  1302.         call    socket_alloc
  1303.         test    eax, eax
  1304.         jz      .nomem2
  1305.         mov     [esp+20], edi   ; application's ebx
  1306.  
  1307.         mov     [eax + SOCKET.Domain], AF_LOCAL
  1308.         mov     [eax + SOCKET.Type], SOCK_STREAM
  1309.         mov     [eax + SOCKET.Protocol], 0              ;;; CHECKME
  1310.         mov     [eax + SOCKET.snd_proc], socket_send_local
  1311.         mov     [eax + SOCKET.rcv_proc], socket_receive_local
  1312.         mov     [eax + SOCKET.PID], 0
  1313.  
  1314.         ; Link the two sockets to eachother
  1315.         mov     [eax + SOCKET.device], ebx
  1316.         mov     [ebx + SOCKET.device], eax
  1317.  
  1318.         lea     eax, [eax + STREAM_SOCKET.rcv]
  1319.         call    socket_ring_create
  1320.         test    eax, eax
  1321.         jz      .nomem2
  1322.  
  1323.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  1324.         call    socket_ring_create
  1325.         test    eax, eax
  1326.         jz      .nomem2
  1327.  
  1328.         ret
  1329.  
  1330.   .nomem2:
  1331.         mov     eax, [esp+20]
  1332.         call    socket_free
  1333.  
  1334.   .nomem1:
  1335.         mov     eax, [esp+32]
  1336.         call    socket_free
  1337.  
  1338.         mov     dword[esp+32], -1
  1339.         mov     dword[esp+20], ENOMEM
  1340.         ret
  1341.  
  1342.  
  1343.  
  1344. ;-----------------------------------------------------------------;
  1345. ;                                                                 ;
  1346. ; socket_debug: Copy socket variables to application buffer.      ;
  1347. ;                                                                 ;
  1348. ;   IN: ecx = socket number                                       ;
  1349. ;       edx = pointer to application buffer                       ;
  1350. ;                                                                 ;
  1351. ;  OUT: eax = 0 on success                                        ;
  1352. ;       eax = -1 on error                                         ;
  1353. ;       ebx = errorcode on error                                  ;
  1354. ;                                                                 ;
  1355. ;-----------------------------------------------------------------;
  1356. align 4
  1357. socket_debug:
  1358.  
  1359.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_debug\n"
  1360.  
  1361.         mov     edi, edx
  1362.  
  1363.         test    ecx, ecx
  1364.         jz      .returnall
  1365.  
  1366.         call    socket_num_to_ptr
  1367.         test    eax, eax
  1368.         jz      .invalid
  1369.  
  1370.         mov     esi, eax
  1371.         mov     ecx, SOCKET_STRUCT_SIZE/4
  1372.         rep movsd
  1373.  
  1374.         mov     dword[esp+32], 0
  1375.         ret
  1376.  
  1377.   .returnall:
  1378.         mov     ebx, net_sockets
  1379.   .next_socket:
  1380.         mov     ebx, [ebx + SOCKET.NextPtr]
  1381.         test    ebx, ebx
  1382.         jz      .done
  1383.         mov     eax, [ebx + SOCKET.Number]
  1384.         stosd
  1385.         jmp     .next_socket
  1386.   .done:
  1387.         xor     eax, eax
  1388.         stosd
  1389.         mov     dword[esp+32], eax
  1390.         ret
  1391.  
  1392.   .invalid:
  1393.         mov     dword[esp+32], -1
  1394.         mov     dword[esp+20], EINVAL
  1395.         ret
  1396.  
  1397.  
  1398. ;-----------------------------------------------------------------;
  1399. ;   ____                                                 ____     ;
  1400. ;   \  /              End of sockets API                 \  /     ;
  1401. ;    \/                                                   \/      ;
  1402. ;    ()        Internally used functions follow           ()      ;
  1403. ;                                                                 ;
  1404. ;-----------------------------------------------------------------;
  1405.  
  1406.  
  1407. ;-----------------------------------------------------------------;
  1408. ;                                                                 ;
  1409. ; socket_find_port:                                               ;
  1410. ; Fill in the local port number for TCP and UDP sockets           ;
  1411. ; This procedure always works because the number of sockets is    ;
  1412. ; limited to a smaller number then the number of possible ports   ;
  1413. ;                                                                 ;
  1414. ;  IN:  eax = socket pointer                                      ;
  1415. ;                                                                 ;
  1416. ;  OUT: /                                                         ;
  1417. ;                                                                 ;
  1418. ;-----------------------------------------------------------------;
  1419. align 4
  1420. socket_find_port:
  1421.  
  1422.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_find_port\n"
  1423.  
  1424.         push    ebx esi ecx
  1425.  
  1426.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  1427.         je      .udp
  1428.  
  1429.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  1430.         je      .tcp
  1431.  
  1432.         pop     ecx esi ebx
  1433.         ret
  1434.  
  1435.   .udp:
  1436.         mov     bx, [last_UDP_port]
  1437.         call    .findit
  1438.         mov     [last_UDP_port], bx
  1439.  
  1440.         pop     ecx esi ebx
  1441.         ret
  1442.  
  1443.   .tcp:
  1444.         mov     bx, [last_TCP_port]
  1445.         call    .findit
  1446.         mov     [last_TCP_port], bx
  1447.  
  1448.         pop     ecx esi ebx
  1449.         ret
  1450.  
  1451.  
  1452.   .restart:
  1453.         mov     bx, MIN_EPHEMERAL_PORT_N
  1454.   .findit:
  1455.         cmp     bx, MAX_EPHEMERAL_PORT_N
  1456.         je      .restart
  1457.  
  1458.         add     bh, 1
  1459.         adc     bl, 0
  1460.  
  1461.         call    socket_check_port
  1462.         jz      .findit
  1463.         ret
  1464.  
  1465.  
  1466.  
  1467. ;-----------------------------------------------------------------;
  1468. ;                                                                 ;
  1469. ; socket_check_port: (to be used with AF_INET only!)              ;
  1470. ; Checks if a local port number is unused                         ;
  1471. ; If the proposed port number is unused, it is filled in in the   ;
  1472. ; socket structure.                                               ;
  1473. ;                                                                 ;
  1474. ;   IN: eax = socket ptr                                          ;
  1475. ;       bx = proposed socket number (network byte order)          ;
  1476. ;                                                                 ;
  1477. ;  OUT: ZF = set on error                                         ;
  1478. ;                                                                 ;
  1479. ;-----------------------------------------------------------------;
  1480. align 4
  1481. socket_check_port:
  1482.  
  1483.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check_port: "
  1484.  
  1485.         pusha
  1486.         mov     ecx, socket_mutex
  1487.         call    mutex_lock
  1488.         popa
  1489.  
  1490.         mov     ecx, [eax + SOCKET.Protocol]
  1491.         mov     edx, [eax + IP_SOCKET.LocalIP]
  1492.         mov     esi, net_sockets
  1493.  
  1494.   .next_socket:
  1495.         mov     esi, [esi + SOCKET.NextPtr]
  1496.         or      esi, esi
  1497.         jz      .port_ok
  1498.  
  1499.         cmp     [esi + SOCKET.Protocol], ecx
  1500.         jne     .next_socket
  1501.  
  1502.         cmp     [esi + IP_SOCKET.LocalIP], edx
  1503.         jne     .next_socket
  1504.  
  1505.         cmp     [esi + UDP_SOCKET.LocalPort], bx
  1506.         jne     .next_socket
  1507.  
  1508.         pusha
  1509.         mov     ecx, socket_mutex
  1510.         call    mutex_unlock
  1511.         popa
  1512.  
  1513.         DEBUGF  DEBUG_NETWORK_VERBOSE, "local port %x already in use\n", bx  ; FIXME: find a way to print big endian values with debugf
  1514.         ret
  1515.  
  1516.   .port_ok:
  1517.         pusha
  1518.         mov     ecx, socket_mutex
  1519.         call    mutex_unlock
  1520.         popa
  1521.  
  1522.         DEBUGF  DEBUG_NETWORK_VERBOSE, "local port %x is free\n", bx         ; FIXME: find a way to print big endian values with debugf
  1523.         mov     [eax + UDP_SOCKET.LocalPort], bx
  1524.         or      bx, bx                                  ; clear the zero-flag
  1525.         ret
  1526.  
  1527.  
  1528.  
  1529. ;-----------------------------------------------------------------;
  1530. ;                                                                 ;
  1531. ; socket_input: Update a (stateless) socket with received data.   ;
  1532. ;                                                                 ;
  1533. ; Note: The socket's mutex should already be set !                ;
  1534. ;                                                                 ;
  1535. ;   IN: eax = socket ptr                                          ;
  1536. ;       ecx = data size                                           ;
  1537. ;       esi = ptr to data                                         ;
  1538. ;       [esp] = ptr to buf                                        ;
  1539. ;                                                                 ;
  1540. ;  OUT: /                                                         ;
  1541. ;                                                                 ;
  1542. ;-----------------------------------------------------------------;
  1543. align 4
  1544. socket_input:
  1545.  
  1546.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
  1547.  
  1548.         push    ecx
  1549.         push    esi
  1550.         mov     esi, esp
  1551.  
  1552.         add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .full
  1553.  
  1554.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_input: success\n"
  1555.         add     esp, sizeof.socket_queue_entry
  1556.  
  1557.         pusha
  1558.         lea     ecx, [eax + SOCKET.mutex]
  1559.         call    mutex_unlock
  1560.         popa
  1561.  
  1562.         jmp     socket_notify
  1563.  
  1564.   .full:
  1565.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_input: socket %x is full!\n", eax
  1566.  
  1567.         pusha
  1568.         lea     ecx, [eax + SOCKET.mutex]
  1569.         call    mutex_unlock
  1570.         popa
  1571.  
  1572.         add     esp, 8
  1573.         call    net_buff_free
  1574.         ret
  1575.  
  1576.  
  1577. ;-----------------------------------------------------------------;
  1578. ;                                                                 ;
  1579. ; socket_ring_create: Create a ringbuffer for sockets.            ;
  1580. ;                                                                 ;
  1581. ;   IN: eax = ptr to ring struct                                  ;
  1582. ;                                                                 ;
  1583. ;  OUT: eax = 0 on error                                          ;
  1584. ;       eax = start ptr                                           ;
  1585. ;                                                                 ;
  1586. ;-----------------------------------------------------------------;
  1587. align 4
  1588. socket_ring_create:
  1589.  
  1590.         push    esi
  1591.         mov     esi, eax
  1592.  
  1593.         push    edx
  1594.         stdcall create_ring_buffer, SOCKET_BUFFER_SIZE, PG_SWR
  1595.         pop     edx
  1596.         test    eax, eax
  1597.         jz      .fail
  1598.  
  1599.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_create: %x\n", eax
  1600.  
  1601.         pusha
  1602.         lea     ecx, [esi + RING_BUFFER.mutex]
  1603.         call    mutex_init
  1604.         popa
  1605.  
  1606.         mov     [esi + RING_BUFFER.start_ptr], eax
  1607.         mov     [esi + RING_BUFFER.write_ptr], eax
  1608.         mov     [esi + RING_BUFFER.read_ptr], eax
  1609.         mov     [esi + RING_BUFFER.size], 0
  1610.         add     eax, SOCKET_BUFFER_SIZE
  1611.         mov     [esi + RING_BUFFER.end_ptr], eax
  1612.         mov     eax, esi
  1613.  
  1614.         pop     esi
  1615.         ret
  1616.  
  1617.   .fail:
  1618.         DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ring_create: Out of memory!\n"
  1619.         pop     esi
  1620.         ret
  1621.  
  1622. ;-----------------------------------------------------------------;
  1623. ;                                                                 ;
  1624. ; socket_ring_write: Write data to ring buffer.                   ;
  1625. ;                                                                 ;
  1626. ;   IN: eax = ptr to ring struct                                  ;
  1627. ;       ecx = data size                                           ;
  1628. ;       esi = ptr to data                                         ;
  1629. ;                                                                 ;
  1630. ;  OUT: ecx = number of bytes stored                              ;
  1631. ;                                                                 ;
  1632. ;-----------------------------------------------------------------;
  1633. align 4
  1634. socket_ring_write:
  1635.  
  1636.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
  1637.  
  1638. ; lock mutex
  1639.         pusha
  1640.         lea     ecx, [eax + RING_BUFFER.mutex]
  1641.         call    mutex_lock                                      ; TODO: check what registers this function actually destroys
  1642.         popa
  1643.  
  1644. ; calculate available size
  1645.         mov     edi, SOCKET_BUFFER_SIZE
  1646.         sub     edi, [eax + RING_BUFFER.size]                   ; available buffer size in edi
  1647.         cmp     ecx, edi
  1648.         jbe     .copy
  1649.         mov     ecx, edi
  1650.   .copy:
  1651.         mov     edi, [eax + RING_BUFFER.write_ptr]
  1652.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
  1653.  
  1654. ; update write ptr
  1655.         push    edi
  1656.         add     edi, ecx
  1657.         cmp     edi, [eax + RING_BUFFER.end_ptr]
  1658.         jb      @f
  1659.         sub     edi, SOCKET_BUFFER_SIZE                         ; WRAP
  1660.   @@:
  1661.  
  1662.         mov     [eax + RING_BUFFER.write_ptr], edi
  1663.         pop     edi
  1664.  
  1665. ; update size
  1666.         add     [eax + RING_BUFFER.size], ecx
  1667.  
  1668. ; copy the data
  1669.         push    ecx
  1670.         shr     ecx, 1
  1671.         jnc     .nb
  1672.         movsb
  1673.   .nb:
  1674.         shr     ecx, 1
  1675.         jnc     .nw
  1676.         movsw
  1677.   .nw:
  1678.         test    ecx, ecx
  1679.         jz      .nd
  1680.         rep movsd
  1681.   .nd:
  1682.         pop     ecx
  1683.  
  1684. ; unlock mutex
  1685.         pusha
  1686.         lea     ecx, [eax + RING_BUFFER.mutex]
  1687.         call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
  1688.         popa
  1689.  
  1690.         ret
  1691.  
  1692. ;-----------------------------------------------------------------;
  1693. ;                                                                 ;
  1694. ; socket_ring_read: Read from ring buffer                         ;
  1695. ;                                                                 ;
  1696. ;   IN: eax = ring struct ptr                                     ;
  1697. ;       ecx = bytes to read                                       ;
  1698. ;       edx = offset                                              ;
  1699. ;       edi = ptr to buffer start                                 ;
  1700. ;                                                                 ;
  1701. ;  OUT: eax = unchanged                                           ;
  1702. ;       ecx = number of bytes read (0 on error)                   ;
  1703. ;       edx = destroyed                                           ;
  1704. ;       esi = destroyed                                           ;
  1705. ;       edi = ptr to buffer end                                   ;
  1706. ;                                                                 ;
  1707. ;-----------------------------------------------------------------;
  1708. align 4
  1709. socket_ring_read:
  1710.  
  1711.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx
  1712.  
  1713.         pusha
  1714.         lea     ecx, [eax + RING_BUFFER.mutex]
  1715.         call    mutex_lock                                      ; TODO: check what registers this function actually destroys
  1716.         popa
  1717.  
  1718.         mov     esi, [eax + RING_BUFFER.read_ptr]
  1719.         add     esi, edx                                        ; esi = start_ptr + offset
  1720.  
  1721.         neg     edx
  1722.         add     edx, [eax + RING_BUFFER.size]                   ; edx = snd.size - offset
  1723.         jle     .no_data_at_all
  1724.  
  1725.         pusha
  1726.         lea     ecx, [eax + RING_BUFFER.mutex]
  1727.         call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
  1728.         popa
  1729.  
  1730.         cmp     ecx, edx
  1731.         ja      .less_data
  1732.  
  1733.   .copy:
  1734.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
  1735.         push    ecx
  1736.         shr     ecx, 1
  1737.         jnc     .nb
  1738.         movsb
  1739.   .nb:
  1740.         shr     ecx, 1
  1741.         jnc     .nw
  1742.         movsw
  1743.   .nw:
  1744.         test    ecx, ecx
  1745.         jz      .nd
  1746.         rep movsd
  1747.   .nd:
  1748.         pop     ecx
  1749.         ret
  1750.  
  1751.   .no_data_at_all:
  1752.         pusha
  1753.         lea     ecx, [eax + RING_BUFFER.mutex]
  1754.         call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
  1755.         popa
  1756.  
  1757.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_read: no data at all!\n"
  1758.         xor     ecx, ecx
  1759.         ret
  1760.  
  1761.   .less_data:
  1762.         mov     ecx, edx
  1763.         jmp     .copy
  1764.  
  1765.  
  1766. ;-----------------------------------------------------------------;
  1767. ;                                                                 ;
  1768. ; socket_ring_free: Free data from a ringbuffer.                  ;
  1769. ;                                                                 ;
  1770. ;   IN: eax = ptr to ring struct                                  ;
  1771. ;       ecx = data size                                           ;
  1772. ;                                                                 ;
  1773. ;  OUT: ecx = number of freed bytes                               ;
  1774. ;                                                                 ;
  1775. ;-----------------------------------------------------------------;
  1776. align 4
  1777. socket_ring_free:
  1778.  
  1779.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
  1780.  
  1781.         push    eax ecx
  1782.         lea     ecx, [eax + RING_BUFFER.mutex]
  1783.         call    mutex_lock                                      ; TODO: check what registers this function actually destroys
  1784.         pop     ecx eax
  1785.  
  1786.         sub     [eax + RING_BUFFER.size], ecx
  1787.         jb      .error
  1788.         add     [eax + RING_BUFFER.read_ptr], ecx
  1789.  
  1790.         mov     edx, [eax + RING_BUFFER.end_ptr]
  1791.         cmp     [eax + RING_BUFFER.read_ptr], edx
  1792.         jb      @f
  1793.         sub     [eax + RING_BUFFER.read_ptr], SOCKET_BUFFER_SIZE
  1794.        @@:
  1795.  
  1796.         push    eax ecx
  1797.         lea     ecx, [eax + RING_BUFFER.mutex]                  ; TODO: check what registers this function actually destroys
  1798.         call    mutex_unlock
  1799.         pop     ecx eax
  1800.  
  1801.         ret
  1802.  
  1803.   .error:       ; we could free all available bytes, but that would be stupid, i guess..
  1804.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ring_free: buffer=%x error!\n", eax
  1805.         add     [eax + RING_BUFFER.size], ecx
  1806.  
  1807.         push    eax
  1808.         lea     ecx, [eax + RING_BUFFER.mutex]
  1809.         call    mutex_unlock                                    ; TODO: check what registers this function actually destroys
  1810.         pop     eax
  1811.  
  1812.         xor     ecx, ecx
  1813.         ret
  1814.  
  1815.  
  1816. ;-----------------------------------------------------------------;
  1817. ;                                                                 ;
  1818. ; socket_block: Suspend the thread attached to a socket.          ;
  1819. ;                                                                 ;
  1820. ;   IN: eax = socket ptr                                          ;
  1821. ;                                                                 ;
  1822. ;  OUT: eax = unchanged                                           ;
  1823. ;                                                                 ;
  1824. ;-----------------------------------------------------------------;
  1825. align 4
  1826. socket_block:
  1827.  
  1828.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: %x\n", eax
  1829.  
  1830.         push    eax
  1831.  
  1832.         pushf
  1833.         cli
  1834.  
  1835.         ; Set the 'socket is blocked' flag
  1836.         or      [eax + SOCKET.state], SS_BLOCKED
  1837.  
  1838.         ; Suspend the thread
  1839.         push    edx
  1840.         mov     edx, [TASK_BASE]
  1841.         mov     [edx + TASKDATA.state], 1               ; Suspended
  1842.  
  1843.         ; Remember the thread ID so we can wake it up again
  1844.         mov     edx, [edx + TASKDATA.pid]
  1845.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: suspending thread: %u\n", edx
  1846.         mov     [eax + SOCKET.TID], edx
  1847.         pop     edx
  1848.         popf
  1849.  
  1850.         call    change_task
  1851.         pop     eax
  1852.  
  1853.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_block: continuing\n"
  1854.  
  1855.         ret
  1856.  
  1857.  
  1858. ;-----------------------------------------------------------------;
  1859. ;                                                                 ;
  1860. ; socket_notify: Wake up socket owner thread.                     ;
  1861. ;                                                                 ;
  1862. ;   IN: eax = socket ptr                                          ;
  1863. ;                                                                 ;
  1864. ;  OUT: eax = unchanged                                           ;
  1865. ;                                                                 ;
  1866. ;-----------------------------------------------------------------;
  1867. align 4
  1868. socket_notify:
  1869.  
  1870.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: %x\n", eax
  1871.  
  1872.         call    socket_check
  1873.         jz      .error
  1874.  
  1875. ; Find the associated thread's TASK_DATA
  1876.         push    ebx ecx esi
  1877.         mov     ebx, [eax + SOCKET.TID]
  1878.         test    ebx, ebx
  1879.         jz      .error2
  1880.         xor     ecx, ecx
  1881.         inc     ecx
  1882.         mov     esi, TASK_DATA
  1883.   .next:
  1884.         cmp     [esi + TASKDATA.pid], ebx
  1885.         je      .found
  1886.         inc     ecx
  1887.         add     esi, 0x20
  1888.         cmp     ecx, [TASK_COUNT]
  1889.         jbe     .next
  1890.  
  1891.   .error2:
  1892. ; PID not found, TODO: close socket!
  1893.         DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_notify: error finding thread 0x%x !\n", ebx
  1894.         pop     esi ecx ebx
  1895.         ret
  1896.  
  1897.   .error:
  1898.         DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_notify: invalid socket ptr: 0x%x !\n", eax
  1899.         ret
  1900.  
  1901.   .found:
  1902.         test    [eax + SOCKET.state], SS_BLOCKED
  1903.         jnz     .un_block
  1904.  
  1905. ; Socket and thread exists and socket is of non blocking type.
  1906. ; We'll try to flag an event to the thread.
  1907.         shl     ecx, 8
  1908.         or      [SLOT_BASE + ecx + APPDATA.event_mask], EVENT_NETWORK
  1909.  
  1910.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: poking thread %u!\n", ebx
  1911.         pop     esi ecx ebx
  1912.         ret
  1913.  
  1914.  
  1915.   .un_block:
  1916. ; Socket and thread exists and socket is of blocking type
  1917. ; We'll try to unblock it.
  1918.         and     [eax + SOCKET.state], not SS_BLOCKED    ; Clear the 'socket is blocked' flag
  1919.         mov     [esi + TASKDATA.state], 0               ; Run the thread
  1920.  
  1921.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_notify: Unblocked socket!\n"
  1922.         pop     esi ecx ebx
  1923.         ret
  1924.  
  1925.  
  1926. ;-----------------------------------------------------------------;
  1927. ;                                                                 ;
  1928. ; socket_alloc: Allocate memory for socket and put new socket     ;
  1929. ; into the list. Newly created socket is initialized with calling ;
  1930. ; PID and given a socket number.                                  ;
  1931. ;                                                                 ;
  1932. ;  IN:  /                                                         ;
  1933. ;                                                                 ;
  1934. ; OUT:  eax = socket ptr on success                               ;
  1935. ;       eax = 0 on error                                          ;
  1936. ;       edi = socket number on success                            ;
  1937. ;                                                                 ;
  1938. ;-----------------------------------------------------------------;
  1939. align 4
  1940. socket_alloc:
  1941.  
  1942.         push    ebx
  1943.  
  1944.         stdcall kernel_alloc, SOCKET_STRUCT_SIZE
  1945.         or      eax, eax
  1946.         jz      .nomem
  1947.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: ptr=%x\n", eax
  1948.  
  1949. ; zero-initialize allocated memory
  1950.         push    eax
  1951.         mov     edi, eax
  1952.         mov     ecx, SOCKET_STRUCT_SIZE / 4
  1953.         xor     eax, eax
  1954.         rep stosd
  1955.         pop     eax
  1956.  
  1957. ; set send-and receive procedures to return -1
  1958.         mov     [eax + SOCKET.snd_proc], .not_yet
  1959.         mov     [eax + SOCKET.rcv_proc], .not_yet
  1960.  
  1961.         pusha
  1962.         mov     ecx, socket_mutex
  1963.         call    mutex_lock
  1964.         popa
  1965.  
  1966. ; find first free socket number and use it
  1967.         mov     edi, [last_socket_num]
  1968.   .next_socket_number:
  1969.         inc     edi
  1970.         jz      .next_socket_number     ; avoid socket nr 0
  1971.         cmp     edi, -1
  1972.         je      .next_socket_number     ; avoid socket nr -1
  1973.         mov     ebx, net_sockets
  1974.   .next_socket:
  1975.         mov     ebx, [ebx + SOCKET.NextPtr]
  1976.         test    ebx, ebx
  1977.         jz      .last_socket
  1978.  
  1979.         cmp     [ebx + SOCKET.Number], edi
  1980.         jne     .next_socket
  1981.         jmp     .next_socket_number
  1982.  
  1983.   .last_socket:
  1984.         mov     [last_socket_num], edi
  1985.         mov     [eax + SOCKET.Number], edi
  1986.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_alloc: number=%u\n", edi
  1987.  
  1988. ; Fill in PID
  1989.         mov     ebx, [TASK_BASE]
  1990.         mov     ebx, [ebx + TASKDATA.pid]
  1991.         mov     [eax + SOCKET.PID], ebx
  1992.         mov     [eax + SOCKET.TID], ebx         ; currently TID = PID in kolibrios :(
  1993.  
  1994. ; init mutex
  1995.         pusha
  1996.         lea     ecx, [eax + SOCKET.mutex]
  1997.         call    mutex_init
  1998.         popa
  1999.  
  2000. ; add socket to the list by re-arranging some pointers
  2001.         mov     ebx, [net_sockets + SOCKET.NextPtr]
  2002.  
  2003.         mov     [eax + SOCKET.PrevPtr], net_sockets
  2004.         mov     [eax + SOCKET.NextPtr], ebx
  2005.  
  2006.         test    ebx, ebx
  2007.         jz      @f
  2008.  
  2009.         pusha
  2010.         lea     ecx, [ebx + SOCKET.mutex]
  2011.         call    mutex_lock
  2012.         popa
  2013.  
  2014.         mov     [ebx + SOCKET.PrevPtr], eax
  2015.  
  2016.         pusha
  2017.         lea     ecx, [ebx + SOCKET.mutex]
  2018.         call    mutex_unlock
  2019.         popa
  2020.        @@:
  2021.  
  2022.         mov     [net_sockets + SOCKET.NextPtr], eax
  2023.  
  2024.         pusha
  2025.         mov     ecx, socket_mutex
  2026.         call    mutex_unlock
  2027.         popa
  2028.         pop     ebx
  2029.  
  2030.         ret
  2031.  
  2032.   .nomem:
  2033.         DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_alloc: Out of memory!\n"
  2034.         pop     ebx
  2035.         ret
  2036.  
  2037.   .not_yet:
  2038.         mov     dword[esp+20], ENOTCONN
  2039.         mov     dword[esp+32], -1
  2040.         ret
  2041.  
  2042.  
  2043. ;-----------------------------------------------------------------;
  2044. ;                                                                 ;
  2045. ; socket_free: Free socket data memory and remove socket from     ;
  2046. ; the list. Caller should lock and unlock socket_mutex.           ;
  2047. ;                                                                 ;
  2048. ;  IN:  eax = socket ptr                                          ;
  2049. ;                                                                 ;
  2050. ; OUT:  /                                                         ;
  2051. ;                                                                 ;
  2052. ;-----------------------------------------------------------------;
  2053. align 4
  2054. socket_free:
  2055.  
  2056.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: %x\n", eax
  2057.  
  2058.         call    socket_check
  2059.         jz      .error
  2060.  
  2061.         push    ebx
  2062.  
  2063.         pusha
  2064.         lea     ecx, [eax + SOCKET.mutex]
  2065.         call    mutex_lock
  2066.         popa
  2067.  
  2068.         cmp     [eax + SOCKET.Type], SOCK_STREAM
  2069.         jne     .no_stream
  2070.  
  2071.         mov     ebx, eax
  2072.         cmp     [eax + STREAM_SOCKET.rcv.start_ptr], 0
  2073.         je      @f
  2074.         stdcall free_kernel_space, [eax + STREAM_SOCKET.rcv.start_ptr]
  2075.   @@:
  2076.         cmp     [ebx + STREAM_SOCKET.snd.start_ptr], 0
  2077.         je      @f
  2078.         stdcall free_kernel_space, [ebx + STREAM_SOCKET.snd.start_ptr]
  2079.   @@:
  2080.         mov     eax, ebx
  2081.   .no_stream:
  2082.  
  2083.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: freeing socket %x\n", eax
  2084.         push    eax                             ; this will be passed to kernel_free
  2085.         mov     ebx, [eax + SOCKET.NextPtr]
  2086.         mov     eax, [eax + SOCKET.PrevPtr]
  2087.  
  2088.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
  2089.  
  2090.         test    eax, eax
  2091.         jz      @f
  2092.         mov     [eax + SOCKET.NextPtr], ebx
  2093.        @@:
  2094.  
  2095.         test    ebx, ebx
  2096.         jz      @f
  2097.         mov     [ebx + SOCKET.PrevPtr], eax
  2098.        @@:
  2099.  
  2100.         call    kernel_free
  2101.         pop     ebx
  2102.  
  2103.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_free: success!\n"
  2104.  
  2105.   .error:
  2106.         ret
  2107.  
  2108.   .error1:
  2109.         pop     ebx
  2110.         DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_free: error!\n"
  2111.         DEBUGF  DEBUG_NETWORK_ERROR, "socket ptr=0x%x caller=0x%x\n", eax, [esp]
  2112.         ret
  2113.  
  2114. ;-----------------------------------------------------------------;
  2115. ;                                                                 ;
  2116. ; socket_fork: Create a child socket.                             ;
  2117. ;                                                                 ;
  2118. ;  IN:  ebx = socket number                                       ;
  2119. ;                                                                 ;
  2120. ; OUT:  eax = child socket number on success                      ;
  2121. ;       eax = 0 on error                                          ;
  2122. ;                                                                 ;
  2123. ;-----------------------------------------------------------------;
  2124. align 4
  2125. socket_fork:
  2126.  
  2127.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_fork: %x\n", ebx
  2128.  
  2129. ; Exit if backlog queue is full
  2130.         mov     eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
  2131.         cmp     ax, [ebx + SOCKET.backlog]
  2132.         jae     .fail
  2133.  
  2134. ; Allocate new socket
  2135.         push    ebx
  2136.         call    socket_alloc
  2137.         pop     ebx
  2138.         test    eax, eax
  2139.         jz      .fail
  2140.  
  2141.         push    eax
  2142.         mov     esi, esp
  2143.         add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
  2144.         pop     eax
  2145.  
  2146. ; Copy structure from current socket to new
  2147. ; We start at PID to preserve the socket num, 2 pointers and mutex
  2148. ; TID will be filled in later
  2149.         lea     esi, [ebx + SOCKET.PID]
  2150.         lea     edi, [eax + SOCKET.PID]
  2151.         mov     ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
  2152.         rep movsd
  2153.  
  2154.         and     [eax + SOCKET.options], not SO_ACCEPTCON
  2155.  
  2156. ; Notify owner of parent socket
  2157.         push    eax
  2158.         mov     eax, ebx
  2159.         call    socket_notify
  2160.         pop     eax
  2161.  
  2162.         ret
  2163.  
  2164.   .fail2:
  2165.         add     esp, 4+4+4
  2166.   .fail:
  2167.         DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_fork: failed\n"
  2168.         xor     eax, eax
  2169.         ret
  2170.  
  2171.  
  2172. ;-----------------------------------------------------------------;
  2173. ;                                                                 ;
  2174. ; socket_num_to_ptr: Get socket structure address by its number.  ;
  2175. ;                                                                 ;
  2176. ;  IN:  ecx = socket number                                       ;
  2177. ;                                                                 ;
  2178. ; OUT:  eax = socket ptr                                          ;
  2179. ;       eax = 0 on error                                          ;
  2180. ;                                                                 ;
  2181. ;-----------------------------------------------------------------;
  2182. align 4
  2183. socket_num_to_ptr:
  2184.  
  2185.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_num_to_ptr: num=%u ", ecx
  2186.  
  2187.         pusha
  2188.         mov     ecx, socket_mutex
  2189.         call    mutex_lock
  2190.         popa
  2191.  
  2192.         mov     eax, net_sockets
  2193.   .next_socket:
  2194.         mov     eax, [eax + SOCKET.NextPtr]
  2195.         test    eax, eax
  2196.         jz      .error
  2197.         cmp     [eax + SOCKET.Number], ecx
  2198.         jne     .next_socket
  2199.  
  2200.         pusha
  2201.         mov     ecx, socket_mutex
  2202.         call    mutex_unlock
  2203.         popa
  2204.  
  2205.         DEBUGF  DEBUG_NETWORK_VERBOSE, "ptr=%x\n", eax
  2206.         ret
  2207.  
  2208.   .error:
  2209.         pusha
  2210.         mov     ecx, socket_mutex
  2211.         call    mutex_unlock
  2212.         popa
  2213.  
  2214.         DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: socket %u not found!\n", eax
  2215.         DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: caller = 0x%x\n", [esp]
  2216.         ret
  2217.  
  2218.  
  2219. ;-----------------------------------------------------------------;
  2220. ;                                                                 ;
  2221. ; socket_ptr_to_num: Get socket number by its address.            ;
  2222. ;                                                                 ;
  2223. ;  IN:  eax = socket ptr                                          ;
  2224. ;                                                                 ;
  2225. ; OUT:  eax = socket number                                       ;
  2226. ;       eax = 0 on error                                          ;
  2227. ;       ZF = set on error                                         ;
  2228. ;                                                                 ;
  2229. ;-----------------------------------------------------------------;
  2230. align 4
  2231. socket_ptr_to_num:
  2232.  
  2233.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_ptr_to_num: ptr=%x ", eax
  2234.  
  2235.         call    socket_check
  2236.         jz      .error
  2237.  
  2238.         mov     eax, [eax + SOCKET.Number]
  2239.  
  2240.         DEBUGF  DEBUG_NETWORK_VERBOSE, "num=%u\n", eax
  2241.         ret
  2242.  
  2243.   .error:
  2244.         DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_ptr_to_num: not found\n", eax
  2245.         ret
  2246.  
  2247.  
  2248. ;-----------------------------------------------------------------;
  2249. ;                                                                 ;
  2250. ; socket_check: Checks if the given ptr is really a socket ptr.   ;
  2251. ;                                                                 ;
  2252. ;  IN:  eax = socket ptr                                          ;
  2253. ;                                                                 ;
  2254. ; OUT:  eax = 0 on error                                          ;
  2255. ;       ZF = set on error                                         ;
  2256. ;                                                                 ;
  2257. ;-----------------------------------------------------------------;
  2258. align 4
  2259. socket_check:
  2260.  
  2261.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check: %x\n", eax
  2262.  
  2263.         test    eax, eax
  2264.         jz      .error
  2265.         push    ebx
  2266.         mov     ebx, net_sockets
  2267.  
  2268.   .next_socket:
  2269.         mov     ebx, [ebx + SOCKET.NextPtr]
  2270.         or      ebx, ebx
  2271.         jz      .done
  2272.         cmp     ebx, eax
  2273.         jnz     .next_socket
  2274.  
  2275.   .done:
  2276.         mov     eax, ebx
  2277.         test    eax, eax
  2278.         pop     ebx
  2279.         ret
  2280.  
  2281.   .error:
  2282.         DEBUGF  DEBUG_NETWORK_ERROR, "SOCKET_check: called with argument 0\n"
  2283.         DEBUGF  DEBUG_NETWORK_ERROR, "stack: 0x%x, 0x%x, 0x%x\n", [esp], [esp+4], [esp+8]
  2284.         ret
  2285.  
  2286.  
  2287.  
  2288. ;-----------------------------------------------------------------;
  2289. ;                                                                 ;
  2290. ; socket_check_owner: Check if the caller app owns the socket.    ;
  2291. ;                                                                 ;
  2292. ;  IN:  eax = socket ptr                                          ;
  2293. ;                                                                 ;
  2294. ; OUT:  ZF = true/false                                           ;
  2295. ;                                                                 ;
  2296. ;-----------------------------------------------------------------;
  2297. align 4
  2298. socket_check_owner:
  2299.  
  2300.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_check_owner: %x\n", eax
  2301.  
  2302.         push    ebx
  2303.         mov     ebx, [TASK_BASE]
  2304.         mov     ebx, [ebx + TASKDATA.pid]
  2305.         cmp     [eax + SOCKET.PID], ebx
  2306.         pop     ebx
  2307.  
  2308.         ret
  2309.  
  2310.  
  2311.  
  2312.  
  2313. ;-----------------------------------------------------------------;
  2314. ;                                                                 ;
  2315. ; socket_process_end: Kernel calls this function when a certain   ;
  2316. ; process ends. This function will check if the process had any   ;
  2317. ; open sockets and update them accordingly (clean up).            ;
  2318. ;                                                                 ;
  2319. ;  IN:  edx = pid                                                 ;
  2320. ;                                                                 ;
  2321. ; OUT:  /                                                         ;
  2322. ;                                                                 ;
  2323. ;-----------------------------------------------------------------;
  2324. align 4
  2325. socket_process_end:
  2326.  
  2327.         ret     ; FIXME
  2328.  
  2329.         cmp     [net_sockets + SOCKET.NextPtr], 0       ; Are there any active sockets at all?
  2330.         je      .quickret                               ; nope, exit immediately
  2331.  
  2332. ; TODO: run the following code in another thread, to avoid deadlock
  2333.  
  2334.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: %x\n", edx
  2335.  
  2336.         pusha
  2337.         mov     ecx, socket_mutex
  2338.         call    mutex_lock
  2339.         popa
  2340.  
  2341.         push    ebx
  2342.         mov     ebx, net_sockets
  2343.  
  2344.   .next_socket:
  2345.         mov     ebx, [ebx + SOCKET.NextPtr]
  2346.   .next_socket_test:
  2347.         test    ebx, ebx
  2348.         jz      .done
  2349.  
  2350.         cmp     [ebx + SOCKET.PID], edx
  2351.         jne     .next_socket
  2352.  
  2353.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_process_end: killing socket %x\n", ebx
  2354.  
  2355.         mov     [ebx + SOCKET.PID], 0
  2356.         mov     eax, ebx
  2357.         mov     ebx, [ebx + SOCKET.NextPtr]
  2358.  
  2359.         pusha
  2360.         cmp     [eax + SOCKET.Domain], AF_INET4
  2361.         jne     .free
  2362.  
  2363.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  2364.         jne     .free
  2365.  
  2366.         call    tcp_disconnect
  2367.         jmp     .closed
  2368.  
  2369.   .free:
  2370.         call    socket_free
  2371.  
  2372.   .closed:
  2373.         popa
  2374.         jmp     .next_socket_test
  2375.  
  2376.   .done:
  2377.         pop     ebx
  2378.  
  2379.         pusha
  2380.         mov     ecx, socket_mutex
  2381.         call    mutex_unlock
  2382.         popa
  2383.  
  2384.   .quickret:
  2385.         ret
  2386.  
  2387.  
  2388.  
  2389.  
  2390. ;-----------------------------------------------------------------;
  2391. ;                                                                 ;
  2392. ; socket_is_connecting: Update socket state.                      ;
  2393. ;                                                                 ;
  2394. ;  IN:  eax = socket ptr                                          ;
  2395. ;                                                                 ;
  2396. ;  OUT: /                                                         ;
  2397. ;                                                                 ;
  2398. ;-----------------------------------------------------------------;
  2399. align 4
  2400. socket_is_connecting:
  2401.  
  2402.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_connecting: %x\n", eax
  2403.  
  2404.         and     [eax + SOCKET.state], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING)
  2405.         or      [eax + SOCKET.state], SS_ISCONNECTING
  2406.         ret
  2407.  
  2408.  
  2409.  
  2410. ;-----------------------------------------------------------------;
  2411. ;                                                                 ;
  2412. ; socket_is_connected: Update socket state.                       ;
  2413. ;                                                                 ;
  2414. ;  IN:  eax = socket ptr                                          ;
  2415. ;                                                                 ;
  2416. ;  OUT: /                                                         ;
  2417. ;                                                                 ;
  2418. ;-----------------------------------------------------------------;
  2419. align 4
  2420. socket_is_connected:
  2421.  
  2422.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_connected: %x\n", eax
  2423.  
  2424.         and     [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
  2425.         or      [eax + SOCKET.state], SS_ISCONNECTED
  2426.         jmp     socket_notify
  2427.  
  2428.  
  2429.  
  2430.  
  2431. ;-----------------------------------------------------------------;
  2432. ;                                                                 ;
  2433. ; socket_is_disconnecting: Update socket state.                   ;
  2434. ;                                                                 ;
  2435. ;  IN:  eax = socket ptr                                          ;
  2436. ;                                                                 ;
  2437. ;  OUT: /                                                         ;
  2438. ;                                                                 ;
  2439. ;-----------------------------------------------------------------;
  2440. align 4
  2441. socket_is_disconnecting:
  2442.  
  2443.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnecting: %x\n", eax
  2444.  
  2445.         and     [eax + SOCKET.state], not (SS_ISCONNECTING)
  2446.         or      [eax + SOCKET.state], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE
  2447.         jmp     socket_notify
  2448.  
  2449.  
  2450.  
  2451. ;-----------------------------------------------------------------;
  2452. ;                                                                 ;
  2453. ; socket_is_disconnected: Update socket state.                    ;
  2454. ;                                                                 ;
  2455. ;  IN:  eax = socket ptr                                          ;
  2456. ;                                                                 ;
  2457. ;  OUT: /                                                         ;
  2458. ;                                                                 ;
  2459. ;-----------------------------------------------------------------;
  2460. align 4
  2461. socket_is_disconnected:
  2462.  
  2463.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_is_disconnected: %x\n", eax
  2464.  
  2465.         and     [eax + SOCKET.state], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING)
  2466.         or      [eax + SOCKET.state], SS_CANTRCVMORE + SS_CANTSENDMORE
  2467.         jmp     socket_notify
  2468.  
  2469.  
  2470.  
  2471. ;-----------------------------------------------------------------;
  2472. ;                                                                 ;
  2473. ; socket_cant_recv_more: Update socket state.                     ;
  2474. ;                                                                 ;
  2475. ;  IN:  eax = socket ptr                                          ;
  2476. ;                                                                 ;
  2477. ;  OUT: /                                                         ;
  2478. ;                                                                 ;
  2479. ;-----------------------------------------------------------------;
  2480. align 4
  2481. socket_cant_recv_more:
  2482.  
  2483.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_cant_recv_more: %x\n", eax
  2484.  
  2485.         or      [eax + SOCKET.state], SS_CANTRCVMORE
  2486.         jmp     socket_notify
  2487.  
  2488.  
  2489.  
  2490. ;-----------------------------------------------------------------;
  2491. ;                                                                 ;
  2492. ; socket_cant_send_more: Update socket state.                     ;
  2493. ;                                                                 ;
  2494. ;  IN:  eax = socket ptr                                          ;
  2495. ;                                                                 ;
  2496. ;  OUT: /                                                         ;
  2497. ;                                                                 ;
  2498. ;-----------------------------------------------------------------;
  2499. align 4
  2500. socket_cant_send_more:
  2501.  
  2502.         DEBUGF  DEBUG_NETWORK_VERBOSE, "SOCKET_cant_send_more: %x\n", eax
  2503.  
  2504.         or      [eax + SOCKET.state], SS_CANTSENDMORE
  2505.         mov     [eax + SOCKET.snd_proc], .notconn
  2506.         jmp     socket_notify
  2507.  
  2508.   .notconn:
  2509.         mov     dword[esp+20], ENOTCONN
  2510.         mov     dword[esp+32], -1
  2511.         ret
  2512.