Subversion Repositories Kolibri OS

Rev

Rev 2998 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2012. 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: 3143 $
  19.  
  20.  
  21. struct  SOCKET
  22.  
  23.         NextPtr                 dd ? ; pointer to next socket in list
  24.         PrevPtr                 dd ? ; pointer to previous socket in list
  25.         Number                  dd ? ; socket number
  26.  
  27.         mutex                   MUTEX
  28.  
  29.         PID                     dd ? ; application process id
  30.         Domain                  dd ? ; INET/UNIX/..
  31.         Type                    dd ? ; RAW/STREAM/DGRAP
  32.         Protocol                dd ? ; ICMP/IPv4/ARP/TCP/UDP
  33.         errorcode               dd ?
  34.         device                  dd ?
  35.  
  36.         options                 dd ?
  37.         state                   dd ?
  38.         backlog                 dw ? ; how many incomming connections that can be queued
  39.  
  40.         snd_proc                dd ?
  41.         rcv_proc                dd ?
  42.  
  43. ends
  44.  
  45. struct  IP_SOCKET               SOCKET
  46.  
  47.         LocalIP                 rd 4 ; network byte order
  48.         RemoteIP                rd 4 ; network byte order
  49.  
  50. ends
  51.  
  52. struct  TCP_SOCKET              IP_SOCKET
  53.  
  54.         LocalPort               dw ? ; network byte order
  55.         RemotePort              dw ? ; network byte order
  56.  
  57.         t_state                 dd ? ; TCB state
  58.         t_rxtshift              db ?
  59.                                 rb 3 ; align
  60.         t_rxtcur                dd ?
  61.         t_dupacks               dd ?
  62.         t_maxseg                dd ?
  63.         t_force                 dd ?
  64.         t_flags                 dd ?
  65.  
  66. ;---------------
  67. ; RFC783 page 21
  68.  
  69. ; send sequence
  70.         SND_UNA                 dd ? ; sequence number of unack'ed sent Packets
  71.         SND_NXT                 dd ? ; next send sequence number to use
  72.         SND_UP                  dd ? ; urgent pointer
  73.         SND_WL1                 dd ? ; window minus one
  74.         SND_WL2                 dd ? ;
  75.         ISS                     dd ? ; initial send sequence number
  76.         SND_WND                 dd ? ; send window
  77.  
  78. ; receive sequence
  79.         RCV_WND                 dd ? ; receive window
  80.         RCV_NXT                 dd ? ; next receive sequence number to use
  81.         RCV_UP                  dd ? ; urgent pointer
  82.         IRS                     dd ? ; initial receive sequence number
  83.  
  84. ;---------------------
  85. ; Additional variables
  86.  
  87. ; receive variables
  88.         RCV_ADV                 dd ?
  89.  
  90. ; retransmit variables
  91.         SND_MAX                 dd ?
  92.  
  93. ; congestion control
  94.         SND_CWND                dd ?
  95.         SND_SSTHRESH            dd ?
  96.  
  97. ;----------------------
  98. ; Transmit timing stuff
  99.         t_idle                  dd ?
  100.         t_rtt                   dd ?
  101.         t_rtseq                 dd ?
  102.         t_srtt                  dd ?
  103.         t_rttvar                dd ?
  104.         t_rttmin                dd ?
  105.         max_sndwnd              dd ?
  106.  
  107. ;-----------------
  108. ; Out-of-band data
  109.         t_oobflags              dd ?
  110.         t_iobc                  dd ?
  111.         t_softerror             dd ?
  112.  
  113.  
  114. ;---------
  115. ; RFC 1323                              ; the order of next 4 elements may not change
  116.  
  117.         SND_SCALE               db ?
  118.         RCV_SCALE               db ?
  119.         requested_s_scale       db ?
  120.         request_r_scale         db ?
  121.  
  122.         ts_recent               dd ?    ; a copy of the most-recent valid timestamp from the other end
  123.         ts_recent_age           dd ?
  124.         last_ack_sent           dd ?
  125.  
  126.  
  127. ;-------
  128. ; Timers
  129.         timer_retransmission    dd ? ; rexmt
  130.         timer_persist           dd ?
  131.         timer_keepalive         dd ? ; keepalive/syn timeout
  132.         timer_timed_wait        dd ? ; also used as 2msl timer
  133.  
  134. ; extra
  135.  
  136.         ts_ecr                  dd ? ; timestamp echo reply
  137.         ts_val                  dd ?
  138.         temp_bits               db ?
  139.  
  140. ends
  141.  
  142. struct  UDP_SOCKET              IP_SOCKET
  143.  
  144.         LocalPort               dw ? ; network byte order
  145.         RemotePort              dw ? ; network byte order
  146.         firstpacket             db ?
  147.  
  148. ends
  149.  
  150.  
  151. struct  ICMP_SOCKET             IP_SOCKET
  152.  
  153.         Identifier              dw ?
  154.  
  155. ends
  156.  
  157.  
  158. struct  RING_BUFFER
  159.  
  160.         start_ptr               dd ? ; Pointer to start of buffer
  161.         end_ptr                 dd ? ; pointer to end of buffer
  162.         read_ptr                dd ? ; Read pointer
  163.         write_ptr               dd ? ; Write pointer
  164.         size                    dd ? ; Number of bytes buffered
  165.  
  166. ends
  167.  
  168. struct  STREAM_SOCKET           TCP_SOCKET
  169.  
  170.         rcv                     RING_BUFFER
  171.         snd                     RING_BUFFER
  172.  
  173. ends
  174.  
  175. struct  socket_queue_entry
  176.  
  177.         data_ptr                dd ?
  178.         buf_ptr                 dd ?
  179.         data_size               dd ?
  180.  
  181. ends
  182.  
  183.  
  184. SOCKETBUFFSIZE          = 4096     ; in bytes
  185.  
  186. SOCKET_QUEUE_SIZE       = 10       ; maximum number ofincoming packets queued for 1 socket
  187. ; the incoming packet queue for sockets is placed in the socket struct itself, at this location from start
  188. SOCKET_QUEUE_LOCATION   = (SOCKETBUFFSIZE - SOCKET_QUEUE_SIZE*sizeof.socket_queue_entry - sizeof.queue)
  189.  
  190. uglobal
  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. endg
  196.  
  197.  
  198. ;-----------------------------------------------------------------
  199. ;
  200. ; SOCKET_init
  201. ;
  202. ;-----------------------------------------------------------------
  203. macro   SOCKET_init {
  204.  
  205.         xor     eax, eax
  206.         mov     edi, net_sockets
  207.         mov     ecx, 5
  208.         rep     stosd
  209.  
  210.        @@:
  211.         pseudo_random eax
  212.         cmp     ax, MIN_EPHEMERAL_PORT
  213.         jb      @r
  214.         cmp     ax, MAX_EPHEMERAL_PORT
  215.         ja      @r
  216.         xchg    al, ah
  217.         mov     [last_UDP_port], ax
  218.  
  219.        @@:
  220.         pseudo_random eax
  221.         cmp     ax, MIN_EPHEMERAL_PORT
  222.         jb      @r
  223.         cmp     ax, MAX_EPHEMERAL_PORT
  224.         ja      @r
  225.         xchg    al, ah
  226.         mov     [last_TCP_port], ax
  227.  
  228. }
  229.  
  230.  
  231. ;-----------------------------------------------------------------
  232. ;
  233. ; SOCKET_block
  234. ;
  235. ;-----------------------------------------------------------------
  236. macro   SOCKET_block socket, loop, done {
  237.  
  238.         test    [socket + SOCKET.options], SO_BLOCK     ; Is this a blocking socket?
  239.         jz      done                                    ; No, return immediately
  240.  
  241.         pusha
  242.         mov     eax, EVENT_NETWORK
  243.         mov     ebx, 1337                   ; UID: ????
  244.         call    wait_event
  245.         popa
  246.  
  247.         jmp     loop
  248.  
  249. }
  250.  
  251.  
  252. ;-----------------------------------------------------------------
  253. ;
  254. ; Socket API (function 74)
  255. ;
  256. ;-----------------------------------------------------------------
  257. align 4
  258. sys_socket:
  259.  
  260.         cmp     ebx, 255
  261.         jz      SOCKET_debug
  262.  
  263.         cmp     ebx, .number
  264.         ja      s_error
  265.         jmp     dword [.table + 4*ebx]
  266.  
  267.   .table:
  268.         dd      SOCKET_open     ; 0
  269.         dd      SOCKET_close    ; 1
  270.         dd      SOCKET_bind     ; 2
  271.         dd      SOCKET_listen   ; 3
  272.         dd      SOCKET_connect  ; 4
  273.         dd      SOCKET_accept   ; 5
  274.         dd      SOCKET_send     ; 6
  275.         dd      SOCKET_receive  ; 7
  276.         dd      SOCKET_set_opt  ; 8
  277.         dd      SOCKET_get_opt  ; 9
  278.   .number = ($ - .table) / 4 - 1
  279.  
  280. s_error:
  281.         DEBUGF  1,"socket error\n"
  282.         mov     dword [esp+32], -1
  283.  
  284.         ret
  285.  
  286.  
  287.  
  288. ;-----------------------------------------------------------------
  289. ;
  290. ; SOCKET_open
  291. ;
  292. ;  IN:  domain in ecx
  293. ;       type in edx
  294. ;       protocol in esi
  295. ;  OUT: eax is socket num, -1 on error
  296. ;
  297. ;-----------------------------------------------------------------
  298. align 4
  299. SOCKET_open:
  300.  
  301.         DEBUGF  1,"SOCKET_open: domain=%u type=%u protocol=%x\n", ecx, edx, esi
  302.  
  303.         push    ecx edx esi
  304.         call    SOCKET_alloc
  305.         pop     esi edx ecx
  306.         jz      s_error
  307.  
  308.         mov     [esp+32], edi                   ; return socketnumber
  309.  
  310.         mov     [eax + SOCKET.Domain], ecx
  311.         mov     [eax + SOCKET.Type], edx
  312.         mov     [eax + SOCKET.Protocol], esi
  313.  
  314.         cmp     ecx, AF_INET4
  315.         jne     .no_inet4
  316.  
  317.         cmp     edx, SOCK_DGRAM
  318.         je      .udp
  319.  
  320.         cmp     edx, SOCK_STREAM
  321.         je      .tcp
  322.  
  323.         cmp     edx, SOCK_RAW
  324.         je      .raw
  325.  
  326.   .no_inet4:
  327.         cmp     ecx, AF_PPP
  328.         jne     .no_ppp
  329.  
  330.         cmp     esi, PPP_PROTO_ETHERNET
  331.         je      .pppoe
  332.  
  333.   .no_ppp:
  334.         ret
  335.  
  336. align 4
  337.   .raw:
  338.         test    esi, esi       ; IP_PROTO_IP
  339.         jz      .ip
  340.  
  341.         cmp     esi, IP_PROTO_ICMP
  342.         je      .icmp
  343.  
  344.         cmp     esi, IP_PROTO_UDP
  345.         je      .udp
  346.  
  347.         cmp     esi, IP_PROTO_TCP
  348.         je      .tcp
  349.  
  350.         ret
  351.  
  352. align 4
  353.   .udp:
  354.         mov     [eax + SOCKET.Protocol], IP_PROTO_UDP
  355.         mov     [eax + SOCKET.snd_proc], SOCKET_send_udp
  356.         mov     [eax + SOCKET.rcv_proc], SOCKET_receive_dgram
  357.         ret
  358.  
  359. align 4
  360.   .tcp:
  361.         mov     [eax + SOCKET.Protocol], IP_PROTO_TCP
  362.         mov     [eax + SOCKET.snd_proc], SOCKET_send_tcp
  363.         mov     [eax + SOCKET.rcv_proc], SOCKET_receive_tcp
  364.  
  365.         TCP_init_socket eax
  366.         ret
  367.  
  368.  
  369. align 4
  370.   .ip:
  371.         mov     [eax + SOCKET.snd_proc], SOCKET_send_ip
  372.         mov     [eax + SOCKET.rcv_proc], SOCKET_receive_dgram
  373.         ret
  374.  
  375.  
  376. align 4
  377.   .icmp:
  378.         mov     [eax + SOCKET.snd_proc], SOCKET_send_icmp
  379.         mov     [eax + SOCKET.rcv_proc], SOCKET_receive_dgram
  380.         ret
  381.  
  382. align 4
  383.   .pppoe:
  384.         push    eax
  385.         init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
  386.         pop     eax
  387.  
  388.         mov     [eax + SOCKET.snd_proc], SOCKET_send_pppoe
  389.         mov     [eax + SOCKET.rcv_proc], SOCKET_receive_dgram
  390.         ret
  391.  
  392.  
  393. ;-----------------------------------------------------------------
  394. ;
  395. ; SOCKET_bind
  396. ;
  397. ;  IN:  socket number in ecx
  398. ;       pointer to sockaddr struct in edx
  399. ;       length of that struct in esi
  400. ;  OUT: 0 on success
  401. ;
  402. ;-----------------------------------------------------------------
  403. align 4
  404. SOCKET_bind:
  405.  
  406.         DEBUGF  1,"SOCKET_bind: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
  407.  
  408.         call    SOCKET_num_to_ptr
  409.         jz      s_error
  410.  
  411.         cmp     esi, 2
  412.         jb      s_error
  413.  
  414.         cmp     word [edx], AF_INET4
  415.         je      .af_inet4
  416.  
  417.         cmp     word [edx], AF_UNIX
  418.         je      .af_unix
  419.  
  420.         jmp     s_error
  421.  
  422.   .af_unix:
  423.         ; TODO: write code here
  424.  
  425.         mov     dword [esp+32], 0
  426.         ret
  427.  
  428.   .af_inet4:
  429.  
  430.         cmp     esi, 6
  431.         jb      s_error
  432.  
  433.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  434.         je      .udp
  435.  
  436.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  437.         je      .tcp
  438.  
  439.         jmp     s_error
  440.  
  441.   .tcp:
  442.   .udp:
  443.  
  444.         mov     ebx, [edx + 4]                  ; First, fill in the IP
  445.         test    ebx, ebx                        ; If IP is 0, use default
  446.         jnz     @f
  447.         mov     ebx, [NET_DEFAULT]
  448.         mov     ebx, [IP_LIST + 4*ebx]
  449.        @@:
  450.         mov     [eax + IP_SOCKET.LocalIP], ebx
  451.  
  452.         mov     bx, [edx + 2]                   ; Now fill in the local port if it's still available
  453.         call    SOCKET_check_port
  454.         jz      s_error                         ; ZF is set by socket_check_port, on error
  455.  
  456.         DEBUGF  1,"SOCKET_bind: local ip=%u.%u.%u.%u\n",\
  457.         [eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\
  458.         [eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1
  459.  
  460.         mov     dword [esp+32], 0
  461.         ret
  462.  
  463.  
  464.  
  465.  
  466. ;-----------------------------------------------------------------
  467. ;
  468. ; SOCKET_connect
  469. ;
  470. ;  IN:  socket number in ecx
  471. ;       pointer to sockaddr struct in edx
  472. ;       length of that struct in esi
  473. ;  OUT: 0 on success
  474. ;
  475. ;-----------------------------------------------------------------
  476. align 4
  477. SOCKET_connect:
  478.  
  479.         DEBUGF  1,"SOCKET_connect: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
  480.  
  481.         call    SOCKET_num_to_ptr
  482.         jz      s_error
  483.  
  484.         cmp     esi, 8
  485.         jb      s_error
  486.  
  487.         cmp     word [edx], AF_INET4
  488.         je      .af_inet4
  489.  
  490.         jmp     s_error
  491.  
  492.   .af_inet4:
  493.         cmp     [eax + IP_SOCKET.LocalIP], 0
  494.         jne     @f
  495.         push    [IP_LIST]
  496.         pop     [eax + IP_SOCKET.LocalIP]
  497.        @@:
  498.  
  499.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  500.         je      .udp
  501.  
  502.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  503.         je      .tcp
  504.  
  505.         cmp     [eax + SOCKET.Protocol], IP_PROTO_IP
  506.         je      .ip
  507.  
  508.         cmp     [eax + SOCKET.Protocol], IP_PROTO_ICMP
  509.         je      .ip
  510.  
  511.         jmp     s_error
  512.  
  513. align 4
  514.   .udp:
  515.         pusha
  516.         lea     ecx, [eax + SOCKET.mutex]
  517.         call    mutex_lock
  518.         popa
  519.  
  520.         pushw   [edx + 2]
  521.         pop     [eax + UDP_SOCKET.RemotePort]
  522.  
  523.         pushd   [edx + 4]
  524.         pop     [eax + IP_SOCKET.RemoteIP]
  525.  
  526.         cmp     [eax + UDP_SOCKET.LocalPort], 0
  527.         jne     @f
  528.         call    SOCKET_find_port
  529.        @@:
  530.  
  531.         mov     [eax + UDP_SOCKET.firstpacket], 0
  532.  
  533.         push    eax
  534.         init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
  535.         pop     eax
  536.  
  537.         lea     ecx, [eax + SOCKET.mutex]
  538.         call    mutex_unlock
  539.  
  540.         mov     dword [esp+32], 0
  541.         ret
  542.  
  543. align 4
  544.   .tcp:
  545.         pusha
  546.         lea     ecx, [eax + SOCKET.mutex]
  547.         call    mutex_lock
  548.         popa
  549.  
  550.         pushw   [edx + 2]
  551.         pop     [eax + TCP_SOCKET.RemotePort]
  552.  
  553.         pushd   [edx + 4]
  554.         pop     [eax + IP_SOCKET.RemoteIP]
  555.  
  556.         cmp     [eax + TCP_SOCKET.LocalPort], 0
  557.         jne     @f
  558.         call    SOCKET_find_port
  559.        @@:
  560.  
  561.         mov     [eax + TCP_SOCKET.timer_persist], 0
  562.         mov     [eax + TCP_SOCKET.t_state], TCPS_SYN_SENT
  563.  
  564.         push    [TCP_sequence_num]
  565.         add     [TCP_sequence_num], 6400
  566.         pop     [eax + TCP_SOCKET.ISS]
  567.         mov     [eax + TCP_SOCKET.timer_keepalive], TCP_time_keep_init
  568.  
  569.  
  570.         TCP_sendseqinit eax
  571.  
  572. ;        mov     [ebx + TCP_SOCKET.timer_retransmission],   ;; todo: create macro to set retransmission timer
  573.  
  574.         mov     ebx, eax
  575.  
  576.         lea     eax, [ebx + STREAM_SOCKET.snd]
  577.         call    SOCKET_ring_create
  578.  
  579.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  580.         call    SOCKET_ring_create
  581.  
  582.         pusha
  583.         lea     ecx, [ebx + SOCKET.mutex]
  584.         call    mutex_unlock
  585.         popa
  586.  
  587.         mov     eax, ebx
  588.         call    TCP_output
  589.  
  590. ;;; TODO: wait for successfull connection if blocking socket
  591.  
  592.         mov     dword [esp+32], 0
  593.         ret
  594.  
  595. align 4
  596.   .ip:
  597.         pusha
  598.         lea     ecx, [eax + SOCKET.mutex]
  599.         call    mutex_lock
  600.         popa
  601.  
  602.         pushd   [edx + 4]
  603.         pop     [eax + IP_SOCKET.RemoteIP]
  604.  
  605.         push    eax
  606.         init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
  607.         pop     eax
  608.  
  609.         lea     ecx, [eax + SOCKET.mutex]
  610.         call    mutex_unlock
  611.  
  612.         mov     dword [esp+32], 0
  613.         ret
  614.  
  615.  
  616. ;-----------------------------------------------------------------
  617. ;
  618. ; SOCKET_listen
  619. ;
  620. ;  IN:  socket number in ecx
  621. ;       backlog in edx
  622. ;  OUT: eax is socket num, -1 on error
  623. ;
  624. ;-----------------------------------------------------------------
  625. align 4
  626. SOCKET_listen:
  627.  
  628.         DEBUGF  1,"SOCKET_listen: socknum=%u backlog=%u\n", ecx, edx
  629.  
  630.         call    SOCKET_num_to_ptr
  631.         jz      s_error
  632.  
  633.         cmp     [eax + SOCKET.Domain], AF_INET4
  634.         jne     s_error
  635.  
  636.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  637.         jne     s_error
  638.  
  639.         cmp     [eax + TCP_SOCKET.LocalPort], 0
  640.         je      s_error
  641.  
  642.         cmp     [eax + IP_SOCKET.LocalIP], 0
  643.         jne     @f
  644.         push    [IP_LIST]
  645.         pop     [eax + IP_SOCKET.LocalIP]
  646.        @@:
  647.  
  648.         cmp     edx, MAX_backlog
  649.         jbe     @f
  650.         mov     edx, MAX_backlog
  651.        @@:
  652.  
  653.         mov     [eax + SOCKET.backlog], dx
  654.         or      [eax + SOCKET.options], SO_ACCEPTCON
  655.         mov     [eax + TCP_SOCKET.t_state], TCPS_LISTEN
  656.         mov     [eax + TCP_SOCKET.timer_keepalive], 0           ; disable keepalive timer
  657.  
  658.         push    eax
  659.         init_queue (eax + SOCKET_QUEUE_LOCATION)                ; Set up sockets queue
  660.         pop     eax
  661.  
  662.         mov     dword [esp+32], 0
  663.  
  664.         ret
  665.  
  666.  
  667. ;-----------------------------------------------------------------
  668. ;
  669. ; SOCKET_accept
  670. ;
  671. ;  IN:  socket number in ecx
  672. ;       addr in edx
  673. ;       addrlen in esi
  674. ;  OUT: eax is socket num, -1 on error
  675. ;
  676. ;-----------------------------------------------------------------
  677. align 4
  678. SOCKET_accept:
  679.  
  680.         DEBUGF  1,"SOCKET_accept: socknum=%u sockaddr=%x length=%u\n", ecx, edx, esi
  681.  
  682.         call    SOCKET_num_to_ptr
  683.         jz      s_error
  684.  
  685.         test    [eax + SOCKET.options], SO_ACCEPTCON
  686.         jz      s_error
  687.  
  688.         cmp     [eax + SOCKET.Domain], AF_INET4
  689.         jne     s_error
  690.  
  691.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  692.         jne     s_error
  693.  
  694.   .loop:
  695.         get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .block
  696.  
  697. ; Ok, we got a socket ptr
  698.         mov     eax, [esi]
  699.  
  700. ; Change PID to that of the current process
  701.         mov     ebx, [TASK_BASE]
  702.         mov     ebx, [ebx + TASKDATA.pid]
  703.         mov     [eax + SOCKET.PID], ebx
  704.  
  705. ; Convert it to a socket number
  706.         call    SOCKET_ptr_to_num
  707.         jz      s_error
  708. ; and return it to caller
  709.         mov     [esp+32], eax
  710.         ret
  711.  
  712.   .block:
  713.         SOCKET_block    eax, .loop, s_error
  714.  
  715. ;-----------------------------------------------------------------
  716. ;
  717. ; SOCKET_close
  718. ;
  719. ;  IN:  socket number in ecx
  720. ;  OUT: eax is socket num, -1 on error
  721. ;
  722. ;-----------------------------------------------------------------
  723. align 4
  724. SOCKET_close:
  725.  
  726.         DEBUGF  1,"SOCKET_close: %u\n", ecx
  727.  
  728.         call    SOCKET_num_to_ptr
  729.         jz      s_error
  730.  
  731.         cmp     [eax + SOCKET.Domain], AF_INET4
  732.         jne     s_error
  733.  
  734.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  735.         je      .free
  736.  
  737.         cmp     [eax + SOCKET.Protocol], IP_PROTO_ICMP
  738.         je      .free
  739.  
  740.         cmp     [eax + SOCKET.Protocol], IP_PROTO_IP
  741.         je      .free
  742.  
  743.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  744.         je      .tcp
  745.  
  746.         jmp     s_error
  747.  
  748.   .tcp:
  749.         cmp     [eax + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED    ; state must be LISTEN, SYN_SENT or CLOSED
  750.         jb      .free
  751.  
  752.         call    TCP_usrclosed
  753.         call    TCP_output      ;;;; Fixme: is this nescessary??
  754.  
  755. ;;; TODO: wait for successfull termination if blocking socket
  756.         mov     dword [esp+32], 0
  757.  
  758.         ret
  759.  
  760.   .free:
  761.         call    SOCKET_free
  762.         mov     dword [esp+32], 0
  763.  
  764.         ret
  765.  
  766.  
  767. ;-----------------------------------------------------------------
  768. ;
  769. ; SOCKET_receive
  770. ;
  771. ;  IN:  socket number in ecx
  772. ;       addr to buffer in edx
  773. ;       length of buffer in esi
  774. ;       flags in edi
  775. ;  OUT: eax is number of bytes copied, -1 on error
  776. ;
  777. ;-----------------------------------------------------------------
  778. align 4
  779. SOCKET_receive:
  780.  
  781.         DEBUGF  1,"SOCKET_receive: socknum=%u bufaddr=%x buflength=%u flags=%x\n", ecx, edx, esi, edi
  782.  
  783.         call    SOCKET_num_to_ptr
  784.         jz      s_error
  785.  
  786.         jmp     [eax + SOCKET.rcv_proc]
  787.  
  788.  
  789. align 4
  790. SOCKET_receive_dgram:
  791.  
  792.         DEBUGF  1,"SOCKET_receive: DGRAM\n"
  793.  
  794.         mov     ebx, esi
  795.         mov     edi, edx                                        ; addr to buffer
  796.  
  797.   .loop:
  798.         get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, .block      ; destroys esi and ecx
  799.  
  800.         mov     ecx, [esi + socket_queue_entry.data_size]
  801.         DEBUGF  1,"SOCKET_receive: %u bytes data\n", ecx
  802.  
  803.         cmp     ecx, ebx
  804.         ja      .too_small
  805.  
  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  1,"SOCKET_receive: Source buffer=%x real addr=%x\n", [esp], esi
  809.         mov     [esp+32+4], ecx                                 ; return number of bytes copied
  810.  
  811. ; copy the data
  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    kernel_free                                     ; remove the packet
  826.         ret
  827.  
  828.   .too_small:
  829.  
  830.         DEBUGF  1,"SOCKET_receive: Buffer too small\n"
  831.         jmp     s_error
  832.  
  833.   .block:
  834.         SOCKET_block    eax, .loop, s_error
  835.  
  836.  
  837. align 4
  838. SOCKET_receive_tcp:
  839.  
  840.         DEBUGF  1,"SOCKET_receive: TCP\n"
  841.  
  842.         mov     ecx, esi
  843.         mov     edi, edx
  844.         xor     edx, edx
  845.         add     eax, STREAM_SOCKET.rcv
  846.   .loop:
  847.         cmp     [eax + RING_BUFFER.size], 0
  848.         je      .block
  849.         call    SOCKET_ring_read
  850.         call    SOCKET_ring_free
  851.  
  852.         mov     [esp+32], ecx                                   ; return number of bytes copied
  853.         ret
  854.  
  855.   .block:
  856.         SOCKET_block    (eax - STREAM_SOCKET.rcv), .loop, s_error
  857.  
  858.  
  859. ;-----------------------------------------------------------------
  860. ;
  861. ; SOCKET_send
  862. ;
  863. ;
  864. ;  IN:  socket number in ecx
  865. ;       pointer to data in edx
  866. ;       datalength in esi
  867. ;       flags in edi
  868. ;  OUT: -1 on error
  869. ;
  870. ;-----------------------------------------------------------------
  871. align 4
  872. SOCKET_send:
  873.  
  874.         DEBUGF  1,"SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi
  875.  
  876.         call    SOCKET_num_to_ptr
  877.         jz      s_error
  878.  
  879.         mov     ecx, esi
  880.         mov     esi, edx
  881.  
  882.         jmp     [eax + SOCKET.snd_proc]
  883.  
  884.  
  885. align 4
  886. SOCKET_send_udp:
  887.  
  888.         DEBUGF  1,"SOCKET_send: UDP\n"
  889.  
  890.         mov     [esp+32], ecx
  891.         call    UDP_output
  892.         cmp     eax, -1
  893.         je      s_error
  894.         ret
  895.  
  896.  
  897. align 4
  898. SOCKET_send_tcp:
  899.  
  900.         DEBUGF  1,"SOCKET_send: TCP\n"
  901.  
  902.         push    eax
  903.         add     eax, STREAM_SOCKET.snd
  904.         call    SOCKET_ring_write
  905.         pop     eax
  906.  
  907.         mov     [esp+32], ecx
  908.  
  909.         call    TCP_output
  910.         ret
  911.  
  912.  
  913. align 4
  914. SOCKET_send_ip:
  915.  
  916.         DEBUGF  1,"SOCKET_send: IPv4\n"
  917.  
  918.         mov     [esp+32], ecx
  919.         call    IPv4_output_raw
  920.         cmp     eax, -1
  921.         je      s_error
  922.         ret
  923.  
  924.  
  925. align 4
  926. SOCKET_send_icmp:
  927.  
  928.         DEBUGF  1,"SOCKET_send: ICMP\n"
  929.  
  930.         mov     [esp+32], ecx
  931.         call    ICMP_output_raw
  932.         cmp     eax, -1
  933.         je      s_error
  934.         ret
  935.  
  936.  
  937. align 4
  938. SOCKET_send_pppoe:
  939.  
  940.         DEBUGF  1,"SOCKET_send: PPPoE\n"
  941.  
  942.         mov     [esp+32], ecx
  943.         mov     ebx, [eax + SOCKET.device]
  944.  
  945.         call    PPPoE_discovery_output
  946.         cmp     eax, -1
  947.         je      s_error
  948.         ret
  949.  
  950.  
  951.  
  952.  
  953. ;-----------------------------------------------------------------
  954. ;
  955. ; SOCKET_get_options
  956. ;
  957. ;  IN:  ecx = socket number
  958. ;       edx = pointer to the options:
  959. ;               dd      level, optname, optval, optlen
  960. ;  OUT: -1 on error
  961. ;
  962. ; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
  963. ; TODO: find best way to notify that send()'ed data were acknowledged
  964. ; Also pseudo-optname -3 is valid and returns socket state, one of TCPS_*.
  965. ;
  966. ;-----------------------------------------------------------------
  967. align 4
  968. SOCKET_get_opt:
  969.  
  970.         DEBUGF  1,"SOCKET_get_opt\n"
  971.  
  972.         call    SOCKET_num_to_ptr
  973.         jz      s_error
  974.  
  975.         cmp     dword [edx], IP_PROTO_TCP
  976.         jne     s_error
  977.         cmp     dword [edx+4], -2
  978.         je      @f
  979.         cmp     dword [edx+4], -3
  980.         jne     s_error
  981. @@:
  982. ;        mov     eax, [edx+12]
  983. ;        test    eax, eax
  984. ;        jz      .fail
  985. ;        cmp     dword [eax], 4
  986. ;        mov     dword [eax], 4
  987. ;        jb      .fail
  988. ;        stdcall net_socket_num_to_addr, ecx
  989. ;        test    eax, eax
  990. ;        jz      .fail
  991. ;        ; todo: check that eax is really TCP socket
  992. ;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
  993. ;        cmp     dword [edx+4], -2
  994. ;        jz      @f
  995. ;        mov     ecx, [eax + TCP_SOCKET.state]
  996. @@:
  997.         mov     eax, [edx+8]
  998.         test    eax, eax
  999.         jz      @f
  1000.         mov     [eax], ecx
  1001. @@:
  1002.         mov     dword [esp+32], 0
  1003.         ret
  1004.  
  1005.  
  1006.  
  1007. ;-----------------------------------------------------------------
  1008. ;
  1009. ; SOCKET_set_options
  1010. ;
  1011. ;  IN:  ecx = socket number
  1012. ;       edx = pointer to the options:
  1013. ;               dd      level, optname, optlen, optval
  1014. ;  OUT: -1 on error
  1015. ;
  1016. ;-----------------------------------------------------------------
  1017. align 4
  1018. SOCKET_set_opt:
  1019.  
  1020.         DEBUGF  1,"SOCKET_set_opt\n"
  1021.  
  1022.         call    SOCKET_num_to_ptr
  1023.         jz      s_error
  1024.  
  1025.         cmp     dword [edx], SOL_SOCKET
  1026.         jne     s_error
  1027.  
  1028.         cmp     dword [edx+4], SO_BINDTODEVICE
  1029.         je      .bind
  1030.  
  1031.         cmp     dword [edx+4], SO_BLOCK
  1032.         je      .block
  1033.  
  1034.         jmp     s_error
  1035.  
  1036.   .bind:
  1037.         cmp     dword [edx+8], 0
  1038.         je      .unbind
  1039.  
  1040.         movzx   edx, byte [edx + 9]
  1041.         cmp     edx, MAX_NET_DEVICES
  1042.         ja      s_error
  1043.  
  1044.         mov     edx, [NET_DRV_LIST + 4*edx]
  1045.         test    edx, edx
  1046.         jz      s_error
  1047.         mov     [eax + SOCKET.device], edx
  1048.  
  1049.         DEBUGF  1,"SOCKET_set_opt: Bound socket %x to device %x\n",eax, edx
  1050.  
  1051.         mov     dword [esp+32], 0       ; success!
  1052.         ret
  1053.  
  1054.   .unbind:
  1055.         mov     [eax + SOCKET.device], 0
  1056.  
  1057.         mov     dword [esp+32], 0       ; success!
  1058.         ret
  1059.  
  1060.   .block:
  1061.         cmp     dword [edx+8], 0
  1062.         je      .unblock
  1063.  
  1064.         or      [eax + SOCKET.options], SO_BLOCK
  1065.  
  1066.         mov     dword [esp+32], 0       ; success!
  1067.         ret
  1068.  
  1069.   .unblock:
  1070.         and     [eax + SOCKET.options], not SO_BLOCK
  1071.  
  1072.         mov     dword [esp+32], 0       ; success!
  1073.         ret
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079. ;-----------------------------------------------------------------
  1080. ;
  1081. ; SOCKET_debug
  1082. ;
  1083. ;  Copies socket variables to application buffer
  1084. ;
  1085. ;  IN:  ecx = socket number
  1086. ;       edx = pointer to buffer
  1087. ;
  1088. ;  OUT: -1 on error
  1089. ;-----------------------------------------------------------------
  1090. align 4
  1091. SOCKET_debug:
  1092.  
  1093.         DEBUGF  1,"SOCKET_debug\n"
  1094.  
  1095.         call    SOCKET_num_to_ptr
  1096.         jz      s_error
  1097.  
  1098.         mov     esi, eax
  1099.         mov     edi, edx
  1100.         mov     ecx, SOCKETBUFFSIZE/4
  1101.         rep     movsd
  1102.  
  1103.         mov     dword [esp+32], 0
  1104.         ret
  1105.  
  1106.  
  1107. ;-----------------------------------------------------------------
  1108. ;
  1109. ; SOCKET_find_port
  1110. ;
  1111. ; Fills in the local port number for TCP and UDP sockets
  1112. ; This procedure always works because the number of sockets is
  1113. ; limited to a smaller number then the number of possible ports
  1114. ;
  1115. ;  IN:  eax = socket pointer
  1116. ;  OUT: /
  1117. ;
  1118. ;-----------------------------------------------------------------
  1119. align 4
  1120. SOCKET_find_port:
  1121.  
  1122.         DEBUGF  1,"SOCKET_find_port\n"
  1123.  
  1124.         push    ebx esi ecx
  1125.  
  1126.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  1127.         je      .udp
  1128.  
  1129.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  1130.         je      .tcp
  1131.  
  1132.         pop     ecx esi ebx
  1133.         ret
  1134.  
  1135.   .udp:
  1136.         mov     bx, [last_UDP_port]
  1137.         call    .findit
  1138.         mov     [last_UDP_port], bx
  1139.  
  1140.         pop     ecx esi ebx
  1141.         ret
  1142.  
  1143.   .tcp:
  1144.         mov     bx, [last_TCP_port]
  1145.         call    .findit
  1146.         mov     [last_TCP_port], bx
  1147.  
  1148.         pop     ecx esi ebx
  1149.         ret
  1150.  
  1151.  
  1152.   .restart:
  1153.         mov     bx, MIN_EPHEMERAL_PORT_N
  1154.   .findit:
  1155.         cmp     bx, MAX_EPHEMERAL_PORT_N
  1156.         je      .restart
  1157.  
  1158.         add     bh, 1
  1159.         adc     bl, 0
  1160.  
  1161.         call    SOCKET_check_port
  1162.         jz      .findit
  1163.         ret
  1164.  
  1165.  
  1166.  
  1167. ;-----------------------------------------------------------------
  1168. ;
  1169. ; SOCKET_check_port (to be used with AF_INET only!)
  1170. ;
  1171. ; Checks if a local port number is unused
  1172. ; If the proposed port number is unused, it is filled in in the socket structure
  1173. ;
  1174. ;  IN:  eax = socket ptr (to find out if its a TCP/UDP socket)
  1175. ;        bx = proposed socket number (network byte order)
  1176. ;
  1177. ;  OUT:  ZF = set on error
  1178. ;
  1179. ;-----------------------------------------------------------------
  1180. align 4
  1181. SOCKET_check_port:
  1182.  
  1183.         DEBUGF  1,"SOCKET_check_port: "
  1184.  
  1185.         mov     ecx, [eax + SOCKET.Protocol]
  1186.         mov     edx, [eax + IP_SOCKET.LocalIP]
  1187.         mov     esi, net_sockets
  1188.  
  1189.   .next_socket:
  1190.         mov     esi, [esi + SOCKET.NextPtr]
  1191.         or      esi, esi
  1192.         jz      .port_ok
  1193.  
  1194.         cmp     [esi + SOCKET.Protocol], ecx
  1195.         jne     .next_socket
  1196.  
  1197.         cmp     [esi + IP_SOCKET.LocalIP], edx
  1198.         jne     .next_socket
  1199.  
  1200.         cmp     [esi + UDP_SOCKET.LocalPort], bx
  1201.         jne     .next_socket
  1202.  
  1203.         DEBUGF  1,"local port %x already in use\n", bx  ; FIXME: find a way to print big endian values with debugf
  1204.         ret
  1205.  
  1206.   .port_ok:
  1207.         DEBUGF  1,"local port %x is free\n", bx         ; FIXME: find a way to print big endian values with debugf
  1208.         mov     [eax + UDP_SOCKET.LocalPort], bx
  1209.         or      bx, bx                                  ; clear the zero-flag
  1210.         ret
  1211.  
  1212.  
  1213.  
  1214. ;-----------------------------------------------------------------
  1215. ;
  1216. ; SOCKET_input
  1217. ;
  1218. ; Updates a (stateless) socket with received data
  1219. ;
  1220. ; Note: the mutex should already be set !
  1221. ;
  1222. ;  IN:  eax = socket ptr
  1223. ;       ecx = data size
  1224. ;       esi = ptr to data
  1225. ;       [esp] = ptr to buf
  1226. ;       [esp + 4] = buf size
  1227. ;
  1228. ;  OUT: /
  1229. ;
  1230. ;-----------------------------------------------------------------
  1231. align 4
  1232. SOCKET_input:
  1233.  
  1234.         DEBUGF  1,"SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
  1235.  
  1236.         mov     [esp+4], ecx
  1237.         push    esi
  1238.         mov     esi, esp
  1239.  
  1240.         add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, sizeof.socket_queue_entry, SOCKET_input.full
  1241.  
  1242.         DEBUGF  1,"SOCKET_input: queued packet successfully\n"
  1243.         add     esp, sizeof.socket_queue_entry
  1244.  
  1245.         pusha
  1246.         lea     ecx, [eax + SOCKET.mutex]
  1247.         call    mutex_unlock
  1248.         popa
  1249.  
  1250.         jmp     SOCKET_notify_owner
  1251.  
  1252.   .full:
  1253.         DEBUGF  2,"SOCKET_input: socket %x is full!\n", eax
  1254.  
  1255.         pusha
  1256.         lea     ecx, [eax + SOCKET.mutex]
  1257.         call    mutex_unlock
  1258.         popa
  1259.  
  1260.         call    kernel_free
  1261.         add     esp, 8
  1262.  
  1263.         ret
  1264.  
  1265.  
  1266. ;--------------------------
  1267. ;
  1268. ; eax = ptr to ring struct (just a buffer of the right size)
  1269. ;
  1270. align 4
  1271. SOCKET_ring_create:
  1272.  
  1273.         push    esi
  1274.         mov     esi, eax
  1275.  
  1276.         push    edx
  1277.         stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW
  1278.         pop     edx
  1279.  
  1280.         DEBUGF  1,"SOCKET_ring_created: %x\n", eax
  1281.         mov     [esi + RING_BUFFER.start_ptr], eax
  1282.         mov     [esi + RING_BUFFER.write_ptr], eax
  1283.         mov     [esi + RING_BUFFER.read_ptr], eax
  1284.         mov     [esi + RING_BUFFER.size], 0
  1285.         add     eax, SOCKET_MAXDATA
  1286.         mov     [esi + RING_BUFFER.end_ptr], eax
  1287.         mov     eax, esi
  1288.         pop     esi
  1289.  
  1290.         ret
  1291.  
  1292. ;-----------------------------------------------------------------
  1293. ;
  1294. ; SOCKET_ring_write
  1295. ;
  1296. ; Adds data to a stream socket, and updates write pointer and size
  1297. ;
  1298. ;  IN:  eax = ptr to ring struct
  1299. ;       ecx = data size
  1300. ;       esi = ptr to data
  1301. ;
  1302. ;  OUT: ecx = number of bytes stored
  1303. ;
  1304. ;-----------------------------------------------------------------
  1305. align 4
  1306. SOCKET_ring_write:
  1307.  
  1308.         DEBUGF  1,"SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
  1309.  
  1310.         add     [eax + RING_BUFFER.size], ecx
  1311.         jc      .way_too_large
  1312.         cmp     [eax + RING_BUFFER.size], SOCKET_MAXDATA
  1313.         ja      .too_large
  1314.  
  1315.   .copy:
  1316.         mov     edi, [eax + RING_BUFFER.write_ptr]
  1317.         DEBUGF  2,"SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
  1318.  
  1319.         push    ecx
  1320.         shr     ecx, 1
  1321.         jnc     .nb
  1322.         movsb
  1323.   .nb:
  1324.         shr     ecx, 1
  1325.         jnc     .nw
  1326.         movsw
  1327.   .nw:
  1328.         test    ecx, ecx
  1329.         jz      .nd
  1330.         rep     movsd
  1331.   .nd:
  1332.         pop     ecx
  1333.  
  1334.         cmp     edi, [eax + RING_BUFFER.end_ptr]
  1335.         jae     .wrap
  1336.         mov     [eax + RING_BUFFER.write_ptr], edi
  1337.  
  1338.         ret
  1339.  
  1340.   .wrap:
  1341.         sub     edi, SOCKET_MAXDATA
  1342.         mov     [eax + RING_BUFFER.write_ptr], edi
  1343.  
  1344.         ret
  1345.  
  1346.   .too_large:                                                           ; update size, we will fill buffer completely
  1347.         sub     [eax + RING_BUFFER.size], SOCKET_MAXDATA
  1348.         sub     ecx, [eax + RING_BUFFER.size]
  1349.         mov     [eax + RING_BUFFER.size], SOCKET_MAXDATA
  1350.         ja      .copy
  1351.  
  1352.   .full:
  1353.         DEBUGF  2,"SOCKET_ring_write: ring buffer is full!\n"
  1354.         xor     ecx, ecx
  1355.         ret
  1356.  
  1357.   .way_too_large:
  1358.         sub     [eax + RING_BUFFER.size], ecx
  1359.         mov     ecx, SOCKET_MAXDATA
  1360.         sub     ecx, [eax + RING_BUFFER.size]
  1361.         ja      .copy
  1362.         jmp     .full
  1363.  
  1364.  
  1365.  
  1366. ;-----------------------------------------------------------------
  1367. ;
  1368. ; SOCKET_ring_read
  1369. ;
  1370. ;  IN:  eax = ring struct ptr
  1371. ;       ecx = bytes to read
  1372. ;       edx = offset
  1373. ;       edi = ptr to buffer start
  1374. ;
  1375. ;  OUT: eax = unchanged
  1376. ;       ecx = number of bytes read (0 on error)
  1377. ;       edx = destroyed
  1378. ;       esi = destroyed
  1379. ;       edi = ptr to buffer end
  1380. ;
  1381. ;-----------------------------------------------------------------
  1382. align 4
  1383. SOCKET_ring_read:
  1384.  
  1385.         DEBUGF  1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx
  1386.  
  1387.         mov     esi, [eax + RING_BUFFER.read_ptr]
  1388.         add     esi, edx                                        ; esi = start_ptr + offset
  1389.  
  1390.         neg     edx
  1391.         add     edx, [eax + RING_BUFFER.size]                   ; edx = snd.size - offset
  1392.         jle     .no_data_at_all
  1393.  
  1394.         cmp     ecx, edx
  1395.         ja      .less_data
  1396.  
  1397.   .copy:
  1398.         DEBUGF  2,"SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
  1399.         push    ecx
  1400.         shr     ecx, 1
  1401.         jnc     .nb
  1402.         movsb
  1403.   .nb:
  1404.         shr     ecx, 1
  1405.         jnc     .nw
  1406.         movsw
  1407.   .nw:
  1408.         test    ecx, ecx
  1409.         jz      .nd
  1410.         rep     movsd
  1411.   .nd:
  1412.         pop     ecx
  1413.         ret
  1414.  
  1415.   .no_data_at_all:
  1416.         DEBUGF  1,"SOCKET_ring_read: no data at all!\n"
  1417.         xor     ecx, ecx
  1418.         ret
  1419.  
  1420.   .less_data:
  1421.         mov     ecx, edx
  1422.         jmp     .copy
  1423.  
  1424.  
  1425. ;-----------------------------------------------------------------
  1426. ;
  1427. ; SOCKET_ring_free
  1428. ;
  1429. ; Free's some bytes from the ringbuffer
  1430. ;
  1431. ;  IN:  eax = ptr to ring struct
  1432. ;       ecx = data size
  1433. ;
  1434. ;  OUT: ecx = number of bytes free-ed
  1435. ;
  1436. ;-----------------------------------------------------------------
  1437. align 4
  1438. SOCKET_ring_free:
  1439.  
  1440.         DEBUGF  1,"SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
  1441.  
  1442.         sub     [eax + RING_BUFFER.size], ecx
  1443.         jb      .error
  1444.         add     [eax + RING_BUFFER.read_ptr], ecx
  1445.  
  1446.         mov     edx, [eax + RING_BUFFER.end_ptr]
  1447.         cmp     [eax + RING_BUFFER.read_ptr], edx
  1448.         jb      @f
  1449.         sub     [eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA
  1450.        @@:
  1451.         ret
  1452.  
  1453.   .error:       ; we could free all available bytes, but that would be stupid, i guess..
  1454.         DEBUGF  1,"SOCKET_ring_free: buffer=%x error!\n", eax
  1455.         add     [eax + RING_BUFFER.size], ecx
  1456.         xor     ecx, ecx
  1457.         ret
  1458.  
  1459.  
  1460. ;-----------------------------------------------------------------
  1461. ;
  1462. ; SOCKET_notify_owner
  1463. ;
  1464. ; notify's the owner of a socket that something happened
  1465. ;
  1466. ;  IN:  eax = socket ptr
  1467. ;  OUT: /
  1468. ;
  1469. ;-----------------------------------------------------------------
  1470. align 4
  1471. SOCKET_notify_owner:
  1472.  
  1473.         DEBUGF  1,"SOCKET_notify_owner: %x\n", eax
  1474.  
  1475.         call    SOCKET_check
  1476.         jz      .error
  1477.  
  1478.         push    eax ecx esi
  1479.  
  1480. ; socket exists, now try to flag an event to the application
  1481.  
  1482.         mov     eax, [eax + SOCKET.PID]
  1483.         test    eax, eax
  1484.         jz      .error2
  1485.         mov     ecx, 1
  1486.         mov     esi, TASK_DATA + TASKDATA.pid
  1487.  
  1488.        .next_pid:
  1489.         cmp     [esi], eax
  1490.         je      .found_pid
  1491.         inc     ecx
  1492.         add     esi, 0x20
  1493.         cmp     ecx, [TASK_COUNT]
  1494.         jbe     .next_pid
  1495.  
  1496. ; PID not found, TODO: close socket!
  1497.  
  1498.         jmp     .error2
  1499.  
  1500.        .found_pid:
  1501.         shl     ecx, 8
  1502.         or      [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK
  1503.         mov     [check_idle_semaphore], 200
  1504.  
  1505.         DEBUGF  1,"SOCKET_notify_owner: succes!\n"
  1506.  
  1507.   .error2:
  1508.         pop     esi ecx eax
  1509.  
  1510.   .error:
  1511.  
  1512.         ret
  1513.  
  1514.  
  1515. ;--------------------------------------------------------------------
  1516. ;
  1517. ; SOCKET_alloc
  1518. ;
  1519. ; Allocate memory for socket data and put new socket into the list
  1520. ; Newly created socket is initialized with calling PID and number and
  1521. ; put into beginning of list (which is a fastest way).
  1522. ;
  1523. ; IN:  /
  1524. ; OUT: eax = 0 on error, socket ptr otherwise
  1525. ;      edi = socket number
  1526. ;       ZF = cleared on error
  1527. ;
  1528. ;--------------------------------------------------------------------
  1529. align 4
  1530. SOCKET_alloc:
  1531.  
  1532.         push    ebx
  1533.  
  1534.         stdcall kernel_alloc, SOCKETBUFFSIZE
  1535.         DEBUGF  1, "SOCKET_alloc: ptr=%x\n", eax
  1536.         or      eax, eax
  1537.         jz      .exit
  1538.  
  1539. ; zero-initialize allocated memory
  1540.         push    eax
  1541.         mov     edi, eax
  1542.         mov     ecx, SOCKETBUFFSIZE / 4
  1543.         xor     eax, eax
  1544.         rep     stosd
  1545.         pop     eax
  1546.  
  1547. ; set send-and receive procedures to return -1
  1548.         mov     [eax + SOCKET.snd_proc], s_error
  1549.         mov     [eax + SOCKET.rcv_proc], s_error
  1550.  
  1551. ; find first free socket number and use it
  1552.         mov     edi, [last_socket_num]
  1553.   .next_socket_number:
  1554.         inc     edi
  1555.         jz      .next_socket_number     ; avoid socket nr 0
  1556.         cmp     edi, -1
  1557.         je      .next_socket_number     ; avoid socket nr -1
  1558.         mov     ebx, net_sockets
  1559.   .next_socket:
  1560.         mov     ebx, [ebx + SOCKET.NextPtr]
  1561.         test    ebx, ebx
  1562.         jz      .last_socket
  1563.  
  1564.         cmp     [ebx + SOCKET.Number], edi
  1565.         jne     .next_socket
  1566.         jmp     .next_socket_number
  1567.  
  1568.   .last_socket:
  1569.         mov     [last_socket_num], edi
  1570.         mov     [eax + SOCKET.Number], edi
  1571.         DEBUGF  1, "SOCKET_alloc: number=%u\n", edi
  1572.  
  1573. ; Fill in PID
  1574.         mov     ebx, [TASK_BASE]
  1575.         mov     ebx, [ebx + TASKDATA.pid]
  1576.         mov     [eax + SOCKET.PID], ebx
  1577.  
  1578. ; init mutex
  1579.         pusha
  1580.         lea     ecx, [eax + SOCKET.mutex]
  1581.         call    mutex_init
  1582.         popa
  1583.  
  1584. ; add socket to the list by re-arranging some pointers
  1585.         mov     ebx, [net_sockets + SOCKET.NextPtr]
  1586.  
  1587.         mov     [eax + SOCKET.PrevPtr], net_sockets
  1588.         mov     [eax + SOCKET.NextPtr], ebx
  1589.  
  1590.         test    ebx, ebx
  1591.         jz      @f
  1592.  
  1593.         pusha
  1594.         lea     ecx, [ebx + SOCKET.mutex]
  1595.         call    mutex_lock
  1596.         popa
  1597.  
  1598.         mov     [ebx + SOCKET.PrevPtr], eax
  1599.  
  1600.         pusha
  1601.         lea     ecx, [ebx + SOCKET.mutex]
  1602.         call    mutex_unlock
  1603.         popa
  1604.        @@:
  1605.  
  1606.         mov     [net_sockets + SOCKET.NextPtr], eax
  1607.         or      eax, eax                ; used to clear zero flag
  1608.   .exit:
  1609.         pop     ebx
  1610.  
  1611.         ret
  1612.  
  1613.  
  1614. ;----------------------------------------------------
  1615. ;
  1616. ; SOCKET_free
  1617. ;
  1618. ; Free socket data memory and remove socket from the list
  1619. ;
  1620. ; IN:  eax = socket ptr
  1621. ; OUT: /
  1622. ;
  1623. ;----------------------------------------------------
  1624. align 4
  1625. SOCKET_free:
  1626.  
  1627.         DEBUGF  1, "SOCKET_free: %x\n", eax
  1628.  
  1629.         call    SOCKET_check
  1630.         jz      .error
  1631.  
  1632.         push    ebx
  1633.  
  1634.         pusha
  1635.         lea     ecx, [eax + SOCKET.mutex]
  1636.         call    mutex_lock
  1637.         popa
  1638.  
  1639.         cmp     [eax + SOCKET.Domain], AF_INET4
  1640.         jnz     .no_tcp
  1641.  
  1642.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  1643.         jnz     .no_tcp
  1644.  
  1645.         mov     ebx, eax
  1646.         stdcall kernel_free, [ebx + STREAM_SOCKET.rcv.start_ptr]
  1647.         stdcall kernel_free, [ebx + STREAM_SOCKET.snd.start_ptr]
  1648.         mov     eax, ebx
  1649.   .no_tcp:
  1650.  
  1651.         push    eax                             ; this will be passed to kernel_free
  1652.         mov     ebx, [eax + SOCKET.NextPtr]
  1653.         mov     eax, [eax + SOCKET.PrevPtr]
  1654.  
  1655.         DEBUGF  1, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
  1656.  
  1657.         test    eax, eax
  1658.         jz      @f
  1659.         mov     [eax + SOCKET.NextPtr], ebx
  1660.        @@:
  1661.  
  1662.         test    ebx, ebx
  1663.         jz      @f
  1664.         mov     [ebx + SOCKET.PrevPtr], eax
  1665.        @@:
  1666.  
  1667.         call    kernel_free
  1668.         pop     ebx
  1669.  
  1670.         DEBUGF  1, "SOCKET_free: success!\n"
  1671.  
  1672.   .error:
  1673.         ret
  1674.  
  1675. ;------------------------------------
  1676. ;
  1677. ; SOCKET_fork
  1678. ;
  1679. ; Create a child socket
  1680. ;
  1681. ; IN:  socket nr in ebx
  1682. ; OUT: child socket nr in eax
  1683. ;
  1684. ;-----------------------------------
  1685. align 4
  1686. SOCKET_fork:
  1687.  
  1688.         DEBUGF  1,"SOCKET_fork: %x\n", ebx
  1689.  
  1690. ; Exit if backlog queue is full
  1691.         mov     eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
  1692.         cmp     ax, [ebx + SOCKET.backlog]
  1693.         jae     .fail
  1694.  
  1695. ; Allocate new socket
  1696.         push    ebx
  1697.         call    SOCKET_alloc
  1698.         pop     ebx
  1699.         jz      .fail
  1700.  
  1701.         push    eax
  1702.         mov     esi, esp
  1703.         add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
  1704.         pop     eax
  1705.  
  1706. ; Copy structure from current socket to new
  1707. ; We start at PID to preserve the socket num, and the 2 pointers at beginning of socket
  1708.         lea     esi, [ebx + SOCKET.PID]
  1709.         lea     edi, [eax + SOCKET.PID]
  1710.         mov     ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
  1711.         rep     movsd
  1712.  
  1713.         and     [eax + SOCKET.options], not SO_ACCEPTCON
  1714.  
  1715.         ret
  1716.  
  1717.   .fail2:
  1718.         add     esp, 4+4+4
  1719.   .fail:
  1720.         DEBUGF  1,"SOCKET_fork: failed\n"
  1721.         xor     eax, eax
  1722.         ret
  1723.  
  1724.  
  1725. ;---------------------------------------------------
  1726. ;
  1727. ; SOCKET_num_to_ptr
  1728. ;
  1729. ; Get socket structure address by its number
  1730. ;
  1731. ; IN:  ecx = socket number
  1732. ; OUT: eax = 0 on error, socket ptr otherwise
  1733. ;       ZF = set on error
  1734. ;
  1735. ;---------------------------------------------------
  1736. align 4
  1737. SOCKET_num_to_ptr:
  1738.  
  1739.         DEBUGF  1,"SOCKET_num_to_ptr: num=%u ", ecx
  1740.  
  1741.         mov     eax, net_sockets
  1742.  
  1743.   .next_socket:
  1744.         mov     eax, [eax + SOCKET.NextPtr]
  1745.         or      eax, eax
  1746.         jz      .error
  1747.         cmp     [eax + SOCKET.Number], ecx
  1748.         jne     .next_socket
  1749.  
  1750.         test    eax, eax
  1751.  
  1752.         DEBUGF  1,"ptr=%x\n", eax
  1753.         ret
  1754.  
  1755.   .error:
  1756.         DEBUGF  1,"not found\n", eax
  1757.         ret
  1758.  
  1759.  
  1760. ;---------------------------------------------------
  1761. ;
  1762. ; SOCKET_ptr_to_num
  1763. ;
  1764. ; Get socket number by its address
  1765. ;
  1766. ; IN:  eax = socket ptr
  1767. ; OUT: eax = 0 on error, socket num otherwise
  1768. ;       ZF = set on error
  1769. ;
  1770. ;---------------------------------------------------
  1771. align 4
  1772. SOCKET_ptr_to_num:
  1773.  
  1774.         DEBUGF  1,"SOCKET_ptr_to_num: ptr=%x ", eax
  1775.  
  1776.         call    SOCKET_check
  1777.         jz      .error
  1778.  
  1779.         mov     eax, [eax + SOCKET.Number]
  1780.  
  1781.         DEBUGF  1,"num=%u\n", eax
  1782.         ret
  1783.  
  1784.   .error:
  1785.         DEBUGF  1,"not found\n", eax
  1786.         ret
  1787.  
  1788.  
  1789. ;---------------------------------------------------
  1790. ;
  1791. ; SOCKET_check
  1792. ;
  1793. ; checks if the given value is really a socket ptr
  1794. ;
  1795. ; IN:  eax = socket ptr
  1796. ; OUT: eax = 0 on error, unchanged otherwise
  1797. ;       ZF = set on error
  1798. ;
  1799. ;---------------------------------------------------
  1800. align 4
  1801. SOCKET_check:
  1802.  
  1803.         DEBUGF  1,"SOCKET_check: %x\n", eax
  1804.  
  1805.         push    ebx
  1806.         mov     ebx, net_sockets
  1807.  
  1808.   .next_socket:
  1809.         mov     ebx, [ebx + SOCKET.NextPtr]
  1810.         or      ebx, ebx
  1811.         jz      .done
  1812.         cmp     ebx, eax
  1813.         jnz     .next_socket
  1814.  
  1815.   .done:
  1816.         mov     eax, ebx
  1817.         test    eax, eax
  1818.         pop     ebx
  1819.  
  1820.         ret
  1821.  
  1822.  
  1823.  
  1824. ;---------------------------------------------------
  1825. ;
  1826. ; SOCKET_check_owner
  1827. ;
  1828. ; checks if the caller application owns the socket
  1829. ;
  1830. ; IN:  eax = socket ptr
  1831. ; OUT:  ZF = true/false
  1832. ;
  1833. ;---------------------------------------------------
  1834. align 4
  1835. SOCKET_check_owner:
  1836.  
  1837.         DEBUGF  1,"SOCKET_check_owner: %x\n", eax
  1838.  
  1839.         push    ebx
  1840.         mov     ebx, [TASK_BASE]
  1841.         mov     ebx, [ecx + TASKDATA.pid]
  1842.         cmp     [eax + SOCKET.PID], ebx
  1843.         pop      ebx
  1844.  
  1845.         ret
  1846.  
  1847.  
  1848.  
  1849.  
  1850. ;------------------------------------------------------
  1851. ;
  1852. ; SOCKET_process_end
  1853. ;
  1854. ; Kernel calls this function when a certain process ends
  1855. ; This function will check if the process had any open sockets
  1856. ; And update them accordingly
  1857. ;
  1858. ; IN:  edx = pid
  1859. ; OUT: /
  1860. ;
  1861. ;------------------------------------------------------
  1862. align 4
  1863. SOCKET_process_end:
  1864.  
  1865.         DEBUGF  1,"SOCKET_process_end: %x\n", edx
  1866.  
  1867.         push    ebx
  1868.         mov     ebx, net_sockets
  1869.  
  1870.   .next_socket:
  1871.         mov     ebx, [ebx + SOCKET.NextPtr]
  1872.   .test_socket:
  1873.         test    ebx, ebx
  1874.         jz      .done
  1875.  
  1876.         cmp     [ebx + SOCKET.PID], edx
  1877.         jne     .next_socket
  1878.  
  1879.         DEBUGF  1,"SOCKET_process_end: killing socket %x\n", ebx
  1880.  
  1881.         mov     [ebx + SOCKET.PID], 0
  1882.  
  1883.         cmp     [ebx + SOCKET.Protocol], IP_PROTO_TCP
  1884.         je      .tcp
  1885.  
  1886. ; The socket is stateless, just kill it right away!
  1887.  
  1888.         mov     eax, ebx
  1889.         mov     ebx, [ebx + SOCKET.NextPtr]
  1890.         call    SOCKET_free
  1891.         jmp     .test_socket
  1892.  
  1893.   .tcp:
  1894.         push    [ebx + SOCKET.NextPtr]
  1895.         mov     eax, ebx
  1896.         call    TCP_disconnect
  1897.         pop     ebx
  1898.         jmp     .test_socket
  1899.  
  1900.   .done:
  1901.         pop     ebx
  1902.  
  1903.         ret
  1904.  
  1905.  
  1906.  
  1907.  
  1908. ;-----------------------------------------------------------------
  1909. ;
  1910. ; SOCKET_is_connecting
  1911. ;
  1912. ;  IN:  eax = socket ptr
  1913. ;  OUT: /
  1914. ;
  1915. ;-----------------------------------------------------------------
  1916.  
  1917. align 4
  1918. SOCKET_is_connecting:
  1919.  
  1920.         DEBUGF  1,"SOCKET_is_connecting: %x\n", eax
  1921.  
  1922.         and     [eax + SOCKET.options], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING)
  1923.         or      [eax + SOCKET.options], SS_ISCONNECTING
  1924.  
  1925.         jmp     SOCKET_notify_owner
  1926.  
  1927.  
  1928.  
  1929. ;-----------------------------------------------------------------
  1930. ;
  1931. ; SOCKET_is_connected
  1932. ;
  1933. ;  IN:  eax = socket ptr
  1934. ;  OUT: /
  1935. ;
  1936. ;-----------------------------------------------------------------
  1937.  
  1938. align 4
  1939. SOCKET_is_connected:
  1940.  
  1941.         DEBUGF  1,"SOCKET_is_connected: %x\n", eax
  1942.  
  1943.         and     [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
  1944.         or      [eax + SOCKET.options], SS_ISCONNECTED
  1945.  
  1946.         jmp     SOCKET_notify_owner
  1947.  
  1948.  
  1949.  
  1950.  
  1951. ;-----------------------------------------------------------------
  1952. ;
  1953. ; SOCKET_is_disconnecting
  1954. ;
  1955. ;  IN:  eax = socket ptr
  1956. ;  OUT: /
  1957. ;
  1958. ;-----------------------------------------------------------------
  1959.  
  1960. align 4
  1961. SOCKET_is_disconnecting:
  1962.  
  1963.         DEBUGF  1,"SOCKET_is_disconnecting: %x\n", eax
  1964.  
  1965.         and     [eax + SOCKET.options], not (SS_ISCONNECTING)
  1966.         or      [eax + SOCKET.options], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE
  1967.  
  1968.         jmp     SOCKET_notify_owner
  1969.  
  1970.  
  1971.  
  1972. ;-----------------------------------------------------------------
  1973. ;
  1974. ; SOCKET_is_disconnected
  1975. ;
  1976. ;  IN:  eax = socket ptr
  1977. ;  OUT: /
  1978. ;
  1979. ;-----------------------------------------------------------------
  1980.  
  1981. align 4
  1982. SOCKET_is_disconnected:
  1983.  
  1984.         DEBUGF  1,"SOCKET_is_disconnected: %x\n", eax
  1985.  
  1986.         and     [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING)
  1987.         or      [eax + SOCKET.options], SS_CANTRCVMORE + SS_CANTSENDMORE
  1988.  
  1989.         jmp     SOCKET_notify_owner
  1990.  
  1991.  
  1992. ;-----------------------------------------------------------------
  1993. ;
  1994. ; SOCKET_cant_recv_more
  1995. ;
  1996. ;  IN:  eax = socket ptr
  1997. ;  OUT: /
  1998. ;
  1999. ;-----------------------------------------------------------------
  2000.  
  2001. align 4
  2002. SOCKET_cant_recv_more:
  2003.  
  2004.         DEBUGF  1,"SOCKET_cant_recv_more: %x\n", eax
  2005.  
  2006.         or      [eax + SOCKET.options], SS_CANTRCVMORE
  2007.  
  2008.         ret
  2009.  
  2010.  
  2011.  
  2012. ;-----------------------------------------------------------------
  2013. ;
  2014. ; SOCKET_cant_send_more
  2015. ;
  2016. ;  IN:  eax = socket ptr
  2017. ;  OUT: /
  2018. ;
  2019. ;-----------------------------------------------------------------
  2020.  
  2021. align 4
  2022. SOCKET_cant_send_more:
  2023.  
  2024.         DEBUGF  1,"SOCKET_cant_send_more: %x\n", eax
  2025.  
  2026.         or      [eax + SOCKET.options], SS_CANTSENDMORE
  2027.  
  2028.         ret