Subversion Repositories Kolibri OS

Rev

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

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