Subversion Repositories Kolibri OS

Rev

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

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