Subversion Repositories Kolibri OS

Rev

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