Subversion Repositories Kolibri OS

Rev

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