Subversion Repositories Kolibri OS

Rev

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