Subversion Repositories Kolibri OS

Rev

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