Subversion Repositories Kolibri OS

Rev

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