Subversion Repositories Kolibri OS

Rev

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