Subversion Repositories Kolibri OS

Rev

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

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