Subversion Repositories Kolibri OS

Rev

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

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