Subversion Repositories Kolibri OS

Rev

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