Subversion Repositories Kolibri OS

Rev

Rev 2621 | Blame | Last modification | View Log | Download | RSS feed

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