Subversion Repositories Kolibri OS

Rev

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

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