Subversion Repositories Kolibri OS

Rev

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

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