Subversion Repositories Kolibri OS

Rev

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

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