Subversion Repositories Kolibri OS

Rev

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