Subversion Repositories Kolibri OS

Rev

Rev 3259 | 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: 3261 $
  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_local:
  832.  
  833.         ; does this socket have a PID yet?
  834.         cmp     [eax + SOCKET.PID], 0
  835.         jne     @f
  836.  
  837.         ; Change PID to that of current process
  838.         mov     ebx, [TASK_BASE]
  839.         mov     ebx, [ebx + TASKDATA.pid]
  840.         mov     [eax + SOCKET.PID], ebx
  841.       @@:
  842.  
  843.         mov     [eax + SOCKET.rcv_proc], SOCKET_receive_stream
  844.  
  845. align 4
  846. SOCKET_receive_stream:
  847.  
  848.         DEBUGF  1,"SOCKET_receive: STREAM\n"
  849.  
  850.         mov     ecx, esi
  851.         mov     edi, edx
  852.         xor     edx, edx
  853.   .loop:
  854.         cmp     [eax + STREAM_SOCKET.rcv + RING_BUFFER.size], 0
  855.         je      .block
  856.         add     eax, STREAM_SOCKET.rcv
  857.         call    SOCKET_ring_read
  858.         call    SOCKET_ring_free
  859.  
  860.         mov     [esp+32], ecx                                   ; return number of bytes copied
  861.         ret
  862.  
  863.   .block:
  864.         test    [eax + SOCKET.options], SO_NONBLOCK
  865.         jnz     s_error
  866.  
  867.         call    SOCKET_block
  868.         jmp     .loop
  869.  
  870.  
  871. ;-----------------------------------------------------------------
  872. ;
  873. ; SOCKET_send
  874. ;
  875. ;
  876. ;  IN:  socket number in ecx
  877. ;       pointer to data in edx
  878. ;       datalength in esi
  879. ;       flags in edi
  880. ;  OUT: -1 on error
  881. ;
  882. ;-----------------------------------------------------------------
  883. align 4
  884. SOCKET_send:
  885.  
  886.         DEBUGF  2,"SOCKET_send: socknum=%u data ptr=%x length=%u flags=%x\n", ecx, edx, esi, edi
  887.  
  888.         call    SOCKET_num_to_ptr
  889.         jz      s_error
  890.  
  891.         mov     ecx, esi
  892.         mov     esi, edx
  893.  
  894.         jmp     [eax + SOCKET.snd_proc]
  895.  
  896.  
  897. align 4
  898. SOCKET_send_udp:
  899.  
  900.         DEBUGF  1,"SOCKET_send: UDP\n"
  901.  
  902.         mov     [esp+32], ecx
  903.         call    UDP_output
  904.         cmp     eax, -1
  905.         je      s_error
  906.         ret
  907.  
  908.  
  909. align 4
  910. SOCKET_send_tcp:
  911.  
  912.         DEBUGF  1,"SOCKET_send: TCP\n"
  913.  
  914.         push    eax
  915.         add     eax, STREAM_SOCKET.snd
  916.         call    SOCKET_ring_write
  917.         pop     eax
  918.  
  919.         mov     [esp+32], ecx
  920.  
  921.         call    TCP_output
  922.         ret
  923.  
  924.  
  925. align 4
  926. SOCKET_send_ip:
  927.  
  928.         DEBUGF  1,"SOCKET_send: IPv4\n"
  929.  
  930.         mov     [esp+32], ecx
  931.         call    IPv4_output_raw
  932.         cmp     eax, -1
  933.         je      s_error
  934.         ret
  935.  
  936.  
  937. align 4
  938. SOCKET_send_icmp:
  939.  
  940.         DEBUGF  1,"SOCKET_send: ICMP\n"
  941.  
  942.         mov     [esp+32], ecx
  943.         call    ICMP_output_raw
  944.         cmp     eax, -1
  945.         je      s_error
  946.         ret
  947.  
  948.  
  949. align 4
  950. SOCKET_send_pppoe:
  951.  
  952.         DEBUGF  1,"SOCKET_send: PPPoE\n"
  953.  
  954.         mov     [esp+32], ecx
  955.         mov     ebx, [eax + SOCKET.device]
  956.  
  957.         call    PPPoE_discovery_output
  958.         cmp     eax, -1
  959.         je      s_error
  960.         ret
  961.  
  962.  
  963.  
  964. align 4
  965. SOCKET_send_local:
  966.  
  967.         ; does this socket have a PID yet?
  968.         cmp     [eax + SOCKET.PID], 0
  969.         jne     @f
  970.  
  971.         ; Change PID to that of current process
  972.         mov     ebx, [TASK_BASE]
  973.         mov     ebx, [ebx + TASKDATA.pid]
  974.         mov     [eax + SOCKET.PID], ebx
  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.         mov     [esi + RING_BUFFER.start_ptr], eax
  1409.         mov     [esi + RING_BUFFER.write_ptr], eax
  1410.         mov     [esi + RING_BUFFER.read_ptr], eax
  1411.         mov     [esi + RING_BUFFER.size], 0
  1412.         add     eax, SOCKET_MAXDATA
  1413.         mov     [esi + RING_BUFFER.end_ptr], eax
  1414.         mov     eax, esi
  1415.         pop     esi
  1416.  
  1417.         ret
  1418.  
  1419. ;-----------------------------------------------------------------
  1420. ;
  1421. ; SOCKET_ring_write
  1422. ;
  1423. ; Adds data to a stream socket, and updates write pointer and size
  1424. ;
  1425. ;  IN:  eax = ptr to ring struct
  1426. ;       ecx = data size
  1427. ;       esi = ptr to data
  1428. ;
  1429. ;  OUT: ecx = number of bytes stored
  1430. ;
  1431. ;-----------------------------------------------------------------
  1432. align 4
  1433. SOCKET_ring_write:
  1434.  
  1435.         DEBUGF  1,"SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
  1436.  
  1437.         add     [eax + RING_BUFFER.size], ecx
  1438.         jc      .way_too_large
  1439.         cmp     [eax + RING_BUFFER.size], SOCKET_MAXDATA
  1440.         ja      .too_large
  1441.  
  1442.   .copy:
  1443.         mov     edi, [eax + RING_BUFFER.write_ptr]
  1444.         DEBUGF  2,"SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
  1445.  
  1446.         push    ecx
  1447.         shr     ecx, 1
  1448.         jnc     .nb
  1449.         movsb
  1450.   .nb:
  1451.         shr     ecx, 1
  1452.         jnc     .nw
  1453.         movsw
  1454.   .nw:
  1455.         test    ecx, ecx
  1456.         jz      .nd
  1457.         rep     movsd
  1458.   .nd:
  1459.         pop     ecx
  1460.  
  1461.         cmp     edi, [eax + RING_BUFFER.end_ptr]
  1462.         jae     .wrap
  1463.         mov     [eax + RING_BUFFER.write_ptr], edi
  1464.  
  1465.         ret
  1466.  
  1467.   .wrap:
  1468.         sub     edi, SOCKET_MAXDATA
  1469.         mov     [eax + RING_BUFFER.write_ptr], edi
  1470.  
  1471.         ret
  1472.  
  1473.   .too_large:                                                           ; update size, we will fill buffer completely
  1474.         sub     [eax + RING_BUFFER.size], SOCKET_MAXDATA
  1475.         sub     ecx, [eax + RING_BUFFER.size]
  1476.         mov     [eax + RING_BUFFER.size], SOCKET_MAXDATA
  1477.         ja      .copy
  1478.  
  1479.   .full:
  1480.         DEBUGF  2,"SOCKET_ring_write: ring buffer is full!\n"
  1481.         xor     ecx, ecx
  1482.         ret
  1483.  
  1484.   .way_too_large:
  1485.         sub     [eax + RING_BUFFER.size], ecx
  1486.         mov     ecx, SOCKET_MAXDATA
  1487.         sub     ecx, [eax + RING_BUFFER.size]
  1488.         ja      .copy
  1489.         jmp     .full
  1490.  
  1491.  
  1492.  
  1493. ;-----------------------------------------------------------------
  1494. ;
  1495. ; SOCKET_ring_read
  1496. ;
  1497. ;  IN:  eax = ring struct ptr
  1498. ;       ecx = bytes to read
  1499. ;       edx = offset
  1500. ;       edi = ptr to buffer start
  1501. ;
  1502. ;  OUT: eax = unchanged
  1503. ;       ecx = number of bytes read (0 on error)
  1504. ;       edx = destroyed
  1505. ;       esi = destroyed
  1506. ;       edi = ptr to buffer end
  1507. ;
  1508. ;-----------------------------------------------------------------
  1509. align 4
  1510. SOCKET_ring_read:
  1511.  
  1512.         DEBUGF  1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u offset=%x\n", eax, edi, ecx, edx
  1513.  
  1514.         mov     esi, [eax + RING_BUFFER.read_ptr]
  1515.         add     esi, edx                                        ; esi = start_ptr + offset
  1516.  
  1517.         neg     edx
  1518.         add     edx, [eax + RING_BUFFER.size]                   ; edx = snd.size - offset
  1519.         jle     .no_data_at_all
  1520.  
  1521.         cmp     ecx, edx
  1522.         ja      .less_data
  1523.  
  1524.   .copy:
  1525.         DEBUGF  2,"SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
  1526.         push    ecx
  1527.         shr     ecx, 1
  1528.         jnc     .nb
  1529.         movsb
  1530.   .nb:
  1531.         shr     ecx, 1
  1532.         jnc     .nw
  1533.         movsw
  1534.   .nw:
  1535.         test    ecx, ecx
  1536.         jz      .nd
  1537.         rep     movsd
  1538.   .nd:
  1539.         pop     ecx
  1540.         ret
  1541.  
  1542.   .no_data_at_all:
  1543.         DEBUGF  1,"SOCKET_ring_read: no data at all!\n"
  1544.         xor     ecx, ecx
  1545.         ret
  1546.  
  1547.   .less_data:
  1548.         mov     ecx, edx
  1549.         jmp     .copy
  1550.  
  1551.  
  1552. ;-----------------------------------------------------------------
  1553. ;
  1554. ; SOCKET_ring_free
  1555. ;
  1556. ; Free's some bytes from the ringbuffer
  1557. ;
  1558. ;  IN:  eax = ptr to ring struct
  1559. ;       ecx = data size
  1560. ;
  1561. ;  OUT: ecx = number of bytes free-ed
  1562. ;
  1563. ;-----------------------------------------------------------------
  1564. align 4
  1565. SOCKET_ring_free:
  1566.  
  1567.         DEBUGF  1,"SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
  1568.  
  1569.         sub     [eax + RING_BUFFER.size], ecx
  1570.         jb      .error
  1571.         add     [eax + RING_BUFFER.read_ptr], ecx
  1572.  
  1573.         mov     edx, [eax + RING_BUFFER.end_ptr]
  1574.         cmp     [eax + RING_BUFFER.read_ptr], edx
  1575.         jb      @f
  1576.         sub     [eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA
  1577.        @@:
  1578.         ret
  1579.  
  1580.   .error:       ; we could free all available bytes, but that would be stupid, i guess..
  1581.         DEBUGF  1,"SOCKET_ring_free: buffer=%x error!\n", eax
  1582.         add     [eax + RING_BUFFER.size], ecx
  1583.         xor     ecx, ecx
  1584.         ret
  1585.  
  1586.  
  1587. ;-----------------------------------------------------------------
  1588. ;
  1589. ; SOCKET_block
  1590. ;
  1591. ; Suspends the thread attached to a socket
  1592. ;
  1593. ;  IN:  eax = socket ptr
  1594. ;  OUT: /
  1595. ;
  1596. ;-----------------------------------------------------------------
  1597. align 4
  1598. SOCKET_block:
  1599.  
  1600.         DEBUGF  1,"SOCKET_block: %x\n", eax
  1601.  
  1602.         pushf
  1603.         cli
  1604.  
  1605.         ; Set the 'socket is blocked' flag
  1606.         or      [eax + SOCKET.state], SS_BLOCKED
  1607.  
  1608.         ; Suspend the thread
  1609.         push    edx
  1610.         mov     edx, [TASK_BASE]
  1611.         DEBUGF  1,"SOCKET_block: suspending PID: %u\n", [edx + TASKDATA.pid]
  1612.         mov     [edx + TASKDATA.state], 1               ; Suspended
  1613.         pop     edx
  1614.  
  1615.         call    change_task
  1616.         popf
  1617.  
  1618.         DEBUGF  1,"SOCKET_block: continueing\n"
  1619.  
  1620.         ret
  1621.  
  1622.  
  1623. ;-----------------------------------------------------------------
  1624. ;
  1625. ; SOCKET_notify
  1626. ;
  1627. ; notify's the owner of a socket that something happened
  1628. ;
  1629. ;  IN:  eax = socket ptr
  1630. ;  OUT: /
  1631. ;
  1632. ;-----------------------------------------------------------------
  1633. align 4
  1634. SOCKET_notify:
  1635.  
  1636.         DEBUGF  1,"SOCKET_notify: %x\n", eax
  1637.  
  1638.         call    SOCKET_check
  1639.         jz      .error
  1640.  
  1641.         test    [eax + SOCKET.state], SS_BLOCKED
  1642.         jnz     .unblock
  1643.  
  1644.         test    [eax + SOCKET.options], SO_NONBLOCK
  1645.         jz      .error
  1646.  
  1647.         push    eax ecx esi
  1648.  
  1649. ; socket exists and is of non blocking type.
  1650. ; We'll try to flag an event to the thread
  1651.  
  1652.         mov     eax, [eax + SOCKET.PID]
  1653.         test    eax, eax
  1654.         jz      .done
  1655.         mov     ecx, 1
  1656.         mov     esi, TASK_DATA + TASKDATA.pid
  1657.  
  1658.   .next_pid:
  1659.         cmp     [esi], eax
  1660.         je      .found_pid
  1661.         inc     ecx
  1662.         add     esi, 0x20
  1663.         cmp     ecx, [TASK_COUNT]
  1664.         jbe     .next_pid
  1665. ; PID not found, TODO: close socket!
  1666.         jmp     .done
  1667.  
  1668.   .found_pid:
  1669.         shl     ecx, 8
  1670.         or      [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK
  1671.         mov     [check_idle_semaphore], 200                             ; What does this mean??
  1672.  
  1673.         DEBUGF  1,"SOCKET_notify: Raised a network event!\n"
  1674.  
  1675.         jmp     .done
  1676.  
  1677.   .unblock:
  1678.         push    eax ecx esi
  1679.         ; Clear the 'socket is blocked' flag
  1680.         and     [eax + SOCKET.state], not SS_BLOCKED
  1681.  
  1682.         ; Find the thread's TASK_DATA
  1683.         mov     eax, [eax + SOCKET.PID]
  1684.         test    eax, eax
  1685.         jz      .error
  1686.         xor     ecx, ecx
  1687.         inc     ecx
  1688.         mov     esi, TASK_DATA
  1689.   .next:
  1690.         cmp     [esi + TASKDATA.pid], eax
  1691.         je      .found
  1692.         inc     ecx
  1693.         add     esi, 0x20
  1694.         cmp     ecx, [TASK_COUNT]
  1695.         jbe     .next
  1696.         jmp     .error
  1697.   .found:
  1698.  
  1699.         ; Run the thread
  1700.         mov     [esi + TASKDATA.state], 0       ; Running
  1701.         DEBUGF  1,"SOCKET_notify: Unblocked socket!\n"
  1702.  
  1703.   .done:
  1704.         pop     esi ecx eax
  1705.  
  1706.   .error:
  1707.         ret
  1708.  
  1709.  
  1710. ;--------------------------------------------------------------------
  1711. ;
  1712. ; SOCKET_alloc
  1713. ;
  1714. ; Allocate memory for socket data and put new socket into the list
  1715. ; Newly created socket is initialized with calling PID and number and
  1716. ; put into beginning of list (which is a fastest way).
  1717. ;
  1718. ; IN:  /
  1719. ; OUT: eax = 0 on error, socket ptr otherwise
  1720. ;      edi = socket number
  1721. ;       ZF = cleared on error
  1722. ;
  1723. ;--------------------------------------------------------------------
  1724. align 4
  1725. SOCKET_alloc:
  1726.  
  1727.         push    ebx
  1728.  
  1729.         stdcall kernel_alloc, SOCKETBUFFSIZE
  1730.         DEBUGF  1, "SOCKET_alloc: ptr=%x\n", eax
  1731.         or      eax, eax
  1732.         jz      .exit
  1733.  
  1734. ; zero-initialize allocated memory
  1735.         push    eax
  1736.         mov     edi, eax
  1737.         mov     ecx, SOCKETBUFFSIZE / 4
  1738.         xor     eax, eax
  1739.         rep     stosd
  1740.         pop     eax
  1741.  
  1742. ; set send-and receive procedures to return -1
  1743.         mov     [eax + SOCKET.snd_proc], s_error
  1744.         mov     [eax + SOCKET.rcv_proc], s_error
  1745.  
  1746. ; find first free socket number and use it
  1747.         mov     edi, [last_socket_num]
  1748.   .next_socket_number:
  1749.         inc     edi
  1750.         jz      .next_socket_number     ; avoid socket nr 0
  1751.         cmp     edi, -1
  1752.         je      .next_socket_number     ; avoid socket nr -1
  1753.         mov     ebx, net_sockets
  1754.   .next_socket:
  1755.         mov     ebx, [ebx + SOCKET.NextPtr]
  1756.         test    ebx, ebx
  1757.         jz      .last_socket
  1758.  
  1759.         cmp     [ebx + SOCKET.Number], edi
  1760.         jne     .next_socket
  1761.         jmp     .next_socket_number
  1762.  
  1763.   .last_socket:
  1764.         mov     [last_socket_num], edi
  1765.         mov     [eax + SOCKET.Number], edi
  1766.         DEBUGF  1, "SOCKET_alloc: number=%u\n", edi
  1767.  
  1768. ; Fill in PID
  1769.         mov     ebx, [TASK_BASE]
  1770.         mov     ebx, [ebx + TASKDATA.pid]
  1771.         mov     [eax + SOCKET.PID], ebx
  1772.  
  1773. ; init mutex
  1774.         pusha
  1775.         lea     ecx, [eax + SOCKET.mutex]
  1776.         call    mutex_init
  1777.         popa
  1778.  
  1779. ; add socket to the list by re-arranging some pointers
  1780.         mov     ebx, [net_sockets + SOCKET.NextPtr]
  1781.  
  1782.         mov     [eax + SOCKET.PrevPtr], net_sockets
  1783.         mov     [eax + SOCKET.NextPtr], ebx
  1784.  
  1785.         test    ebx, ebx
  1786.         jz      @f
  1787.  
  1788.         pusha
  1789.         lea     ecx, [ebx + SOCKET.mutex]
  1790.         call    mutex_lock
  1791.         popa
  1792.  
  1793.         mov     [ebx + SOCKET.PrevPtr], eax
  1794.  
  1795.         pusha
  1796.         lea     ecx, [ebx + SOCKET.mutex]
  1797.         call    mutex_unlock
  1798.         popa
  1799.        @@:
  1800.  
  1801.         mov     [net_sockets + SOCKET.NextPtr], eax
  1802.         or      eax, eax                ; used to clear zero flag
  1803.   .exit:
  1804.         pop     ebx
  1805.  
  1806.         ret
  1807.  
  1808.  
  1809. ;----------------------------------------------------
  1810. ;
  1811. ; SOCKET_free
  1812. ;
  1813. ; Free socket data memory and remove socket from the list
  1814. ;
  1815. ; IN:  eax = socket ptr
  1816. ; OUT: /
  1817. ;
  1818. ;----------------------------------------------------
  1819. align 4
  1820. SOCKET_free:
  1821.  
  1822.         DEBUGF  1, "SOCKET_free: %x\n", eax
  1823.  
  1824.         call    SOCKET_check
  1825.         jz      .error
  1826.  
  1827.         push    ebx
  1828.  
  1829.         pusha
  1830.         lea     ecx, [eax + SOCKET.mutex]
  1831.         call    mutex_lock
  1832.         popa
  1833.  
  1834.         cmp     [eax + SOCKET.Domain], AF_INET4
  1835.         jnz     .no_tcp
  1836.  
  1837.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  1838.         jnz     .no_tcp
  1839.  
  1840.         mov     ebx, eax
  1841.         stdcall kernel_free, [ebx + STREAM_SOCKET.rcv.start_ptr]
  1842.         stdcall kernel_free, [ebx + STREAM_SOCKET.snd.start_ptr]
  1843.         mov     eax, ebx
  1844.   .no_tcp:
  1845.  
  1846.         push    eax                             ; this will be passed to kernel_free
  1847.         mov     ebx, [eax + SOCKET.NextPtr]
  1848.         mov     eax, [eax + SOCKET.PrevPtr]
  1849.  
  1850.         DEBUGF  1, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
  1851.  
  1852.         test    eax, eax
  1853.         jz      @f
  1854.         mov     [eax + SOCKET.NextPtr], ebx
  1855.        @@:
  1856.  
  1857.         test    ebx, ebx
  1858.         jz      @f
  1859.         mov     [ebx + SOCKET.PrevPtr], eax
  1860.        @@:
  1861.  
  1862.         call    kernel_free
  1863.         pop     ebx
  1864.  
  1865.         DEBUGF  1, "SOCKET_free: success!\n"
  1866.  
  1867.   .error:
  1868.         ret
  1869.  
  1870. ;------------------------------------
  1871. ;
  1872. ; SOCKET_fork
  1873. ;
  1874. ; Create a child socket
  1875. ;
  1876. ; IN:  socket nr in ebx
  1877. ; OUT: child socket nr in eax
  1878. ;
  1879. ;-----------------------------------
  1880. align 4
  1881. SOCKET_fork:
  1882.  
  1883.         DEBUGF  1,"SOCKET_fork: %x\n", ebx
  1884.  
  1885. ; Exit if backlog queue is full
  1886.         mov     eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
  1887.         cmp     ax, [ebx + SOCKET.backlog]
  1888.         jae     .fail
  1889.  
  1890. ; Allocate new socket
  1891.         push    ebx
  1892.         call    SOCKET_alloc
  1893.         pop     ebx
  1894.         jz      .fail
  1895.  
  1896.         push    eax
  1897.         mov     esi, esp
  1898.         add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
  1899.         pop     eax
  1900.  
  1901. ; Copy structure from current socket to new
  1902. ; We start at PID to preserve the socket num, and the 2 pointers at beginning of socket
  1903.         lea     esi, [ebx + SOCKET.PID]
  1904.         lea     edi, [eax + SOCKET.PID]
  1905.         mov     ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
  1906.         rep     movsd
  1907.  
  1908.         and     [eax + SOCKET.options], not SO_ACCEPTCON
  1909.  
  1910.         ret
  1911.  
  1912.   .fail2:
  1913.         add     esp, 4+4+4
  1914.   .fail:
  1915.         DEBUGF  1,"SOCKET_fork: failed\n"
  1916.         xor     eax, eax
  1917.         ret
  1918.  
  1919.  
  1920. ;---------------------------------------------------
  1921. ;
  1922. ; SOCKET_num_to_ptr
  1923. ;
  1924. ; Get socket structure address by its number
  1925. ;
  1926. ; IN:  ecx = socket number
  1927. ; OUT: eax = 0 on error, socket ptr otherwise
  1928. ;       ZF = set on error
  1929. ;
  1930. ;---------------------------------------------------
  1931. align 4
  1932. SOCKET_num_to_ptr:
  1933.  
  1934.         DEBUGF  1,"SOCKET_num_to_ptr: num=%u ", ecx
  1935.  
  1936.         mov     eax, net_sockets
  1937.  
  1938.   .next_socket:
  1939.         mov     eax, [eax + SOCKET.NextPtr]
  1940.         or      eax, eax
  1941.         jz      .error
  1942.         cmp     [eax + SOCKET.Number], ecx
  1943.         jne     .next_socket
  1944.  
  1945.         test    eax, eax
  1946.  
  1947.         DEBUGF  1,"ptr=%x\n", eax
  1948.         ret
  1949.  
  1950.   .error:
  1951.         DEBUGF  1,"not found\n", eax
  1952.         ret
  1953.  
  1954.  
  1955. ;---------------------------------------------------
  1956. ;
  1957. ; SOCKET_ptr_to_num
  1958. ;
  1959. ; Get socket number by its address
  1960. ;
  1961. ; IN:  eax = socket ptr
  1962. ; OUT: eax = 0 on error, socket num otherwise
  1963. ;       ZF = set on error
  1964. ;
  1965. ;---------------------------------------------------
  1966. align 4
  1967. SOCKET_ptr_to_num:
  1968.  
  1969.         DEBUGF  1,"SOCKET_ptr_to_num: ptr=%x ", eax
  1970.  
  1971.         call    SOCKET_check
  1972.         jz      .error
  1973.  
  1974.         mov     eax, [eax + SOCKET.Number]
  1975.  
  1976.         DEBUGF  1,"num=%u\n", eax
  1977.         ret
  1978.  
  1979.   .error:
  1980.         DEBUGF  1,"not found\n", eax
  1981.         ret
  1982.  
  1983.  
  1984. ;---------------------------------------------------
  1985. ;
  1986. ; SOCKET_check
  1987. ;
  1988. ; checks if the given value is really a socket ptr
  1989. ;
  1990. ; IN:  eax = socket ptr
  1991. ; OUT: eax = 0 on error, unchanged otherwise
  1992. ;       ZF = set on error
  1993. ;
  1994. ;---------------------------------------------------
  1995. align 4
  1996. SOCKET_check:
  1997.  
  1998.         DEBUGF  1,"SOCKET_check: %x\n", eax
  1999.  
  2000.         push    ebx
  2001.         mov     ebx, net_sockets
  2002.  
  2003.   .next_socket:
  2004.         mov     ebx, [ebx + SOCKET.NextPtr]
  2005.         or      ebx, ebx
  2006.         jz      .done
  2007.         cmp     ebx, eax
  2008.         jnz     .next_socket
  2009.  
  2010.   .done:
  2011.         mov     eax, ebx
  2012.         test    eax, eax
  2013.         pop     ebx
  2014.  
  2015.         ret
  2016.  
  2017.  
  2018.  
  2019. ;---------------------------------------------------
  2020. ;
  2021. ; SOCKET_check_owner
  2022. ;
  2023. ; checks if the caller application owns the socket
  2024. ;
  2025. ; IN:  eax = socket ptr
  2026. ; OUT:  ZF = true/false
  2027. ;
  2028. ;---------------------------------------------------
  2029. align 4
  2030. SOCKET_check_owner:
  2031.  
  2032.         DEBUGF  1,"SOCKET_check_owner: %x\n", eax
  2033.  
  2034.         push    ebx
  2035.         mov     ebx, [TASK_BASE]
  2036.         mov     ebx, [ebx + TASKDATA.pid]
  2037.         cmp     [eax + SOCKET.PID], ebx
  2038.         pop      ebx
  2039.  
  2040.         ret
  2041.  
  2042.  
  2043.  
  2044.  
  2045. ;------------------------------------------------------
  2046. ;
  2047. ; SOCKET_process_end
  2048. ;
  2049. ; Kernel calls this function when a certain process ends
  2050. ; This function will check if the process had any open sockets
  2051. ; And update them accordingly
  2052. ;
  2053. ; IN:  edx = pid
  2054. ; OUT: /
  2055. ;
  2056. ;------------------------------------------------------
  2057. align 4
  2058. SOCKET_process_end:
  2059.  
  2060.         DEBUGF  1,"SOCKET_process_end: %x\n", edx
  2061.  
  2062.         push    ebx
  2063.         mov     ebx, net_sockets
  2064.  
  2065.   .next_socket:
  2066.         mov     ebx, [ebx + SOCKET.NextPtr]
  2067.   .test_socket:
  2068.         test    ebx, ebx
  2069.         jz      .done
  2070.  
  2071.         cmp     [ebx + SOCKET.PID], edx
  2072.         jne     .next_socket
  2073.  
  2074.         DEBUGF  1,"SOCKET_process_end: killing socket %x\n", ebx
  2075.  
  2076.         mov     [ebx + SOCKET.PID], 0
  2077.  
  2078.         cmp     [ebx + SOCKET.Protocol], IP_PROTO_TCP
  2079.         je      .tcp
  2080.  
  2081. ; The socket is stateless, just kill it right away!
  2082.  
  2083.         mov     eax, ebx
  2084.         mov     ebx, [ebx + SOCKET.NextPtr]
  2085.         call    SOCKET_free
  2086.         jmp     .test_socket
  2087.  
  2088.   .tcp:
  2089.         push    [ebx + SOCKET.NextPtr]
  2090.         mov     eax, ebx
  2091.         call    TCP_disconnect
  2092.         pop     ebx
  2093.         jmp     .test_socket
  2094.  
  2095.   .done:
  2096.         pop     ebx
  2097.  
  2098.         ret
  2099.  
  2100.  
  2101.  
  2102.  
  2103. ;-----------------------------------------------------------------
  2104. ;
  2105. ; SOCKET_is_connecting
  2106. ;
  2107. ;  IN:  eax = socket ptr
  2108. ;  OUT: /
  2109. ;
  2110. ;-----------------------------------------------------------------
  2111.  
  2112. align 4
  2113. SOCKET_is_connecting:
  2114.  
  2115.         DEBUGF  1,"SOCKET_is_connecting: %x\n", eax
  2116.  
  2117.         and     [eax + SOCKET.options], not (SS_ISCONNECTED + SS_ISDISCONNECTING + SS_ISCONFIRMING)
  2118.         or      [eax + SOCKET.options], SS_ISCONNECTING
  2119.  
  2120.         jmp     SOCKET_notify
  2121.  
  2122.  
  2123.  
  2124. ;-----------------------------------------------------------------
  2125. ;
  2126. ; SOCKET_is_connected
  2127. ;
  2128. ;  IN:  eax = socket ptr
  2129. ;  OUT: /
  2130. ;
  2131. ;-----------------------------------------------------------------
  2132.  
  2133. align 4
  2134. SOCKET_is_connected:
  2135.  
  2136.         DEBUGF  1,"SOCKET_is_connected: %x\n", eax
  2137.  
  2138.         and     [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISDISCONNECTING + SS_ISCONFIRMING)
  2139.         or      [eax + SOCKET.options], SS_ISCONNECTED
  2140.  
  2141.         jmp     SOCKET_notify
  2142.  
  2143.  
  2144.  
  2145.  
  2146. ;-----------------------------------------------------------------
  2147. ;
  2148. ; SOCKET_is_disconnecting
  2149. ;
  2150. ;  IN:  eax = socket ptr
  2151. ;  OUT: /
  2152. ;
  2153. ;-----------------------------------------------------------------
  2154.  
  2155. align 4
  2156. SOCKET_is_disconnecting:
  2157.  
  2158.         DEBUGF  1,"SOCKET_is_disconnecting: %x\n", eax
  2159.  
  2160.         and     [eax + SOCKET.options], not (SS_ISCONNECTING)
  2161.         or      [eax + SOCKET.options], SS_ISDISCONNECTING + SS_CANTRCVMORE + SS_CANTSENDMORE
  2162.  
  2163.         jmp     SOCKET_notify
  2164.  
  2165.  
  2166.  
  2167. ;-----------------------------------------------------------------
  2168. ;
  2169. ; SOCKET_is_disconnected
  2170. ;
  2171. ;  IN:  eax = socket ptr
  2172. ;  OUT: /
  2173. ;
  2174. ;-----------------------------------------------------------------
  2175.  
  2176. align 4
  2177. SOCKET_is_disconnected:
  2178.  
  2179.         DEBUGF  1,"SOCKET_is_disconnected: %x\n", eax
  2180.  
  2181.         and     [eax + SOCKET.options], not (SS_ISCONNECTING + SS_ISCONNECTED + SS_ISDISCONNECTING)
  2182.         or      [eax + SOCKET.options], SS_CANTRCVMORE + SS_CANTSENDMORE
  2183.  
  2184.         jmp     SOCKET_notify
  2185.  
  2186.  
  2187. ;-----------------------------------------------------------------
  2188. ;
  2189. ; SOCKET_cant_recv_more
  2190. ;
  2191. ;  IN:  eax = socket ptr
  2192. ;  OUT: /
  2193. ;
  2194. ;-----------------------------------------------------------------
  2195.  
  2196. align 4
  2197. SOCKET_cant_recv_more:
  2198.  
  2199.         DEBUGF  1,"SOCKET_cant_recv_more: %x\n", eax
  2200.  
  2201.         or      [eax + SOCKET.options], SS_CANTRCVMORE
  2202.  
  2203.         ret
  2204.  
  2205.  
  2206.  
  2207. ;-----------------------------------------------------------------
  2208. ;
  2209. ; SOCKET_cant_send_more
  2210. ;
  2211. ;  IN:  eax = socket ptr
  2212. ;  OUT: /
  2213. ;
  2214. ;-----------------------------------------------------------------
  2215.  
  2216. align 4
  2217. SOCKET_cant_send_more:
  2218.  
  2219.         DEBUGF  1,"SOCKET_cant_send_more: %x\n", eax
  2220.  
  2221.         or      [eax + SOCKET.options], SS_CANTSENDMORE
  2222.  
  2223.         ret