Subversion Repositories Kolibri OS

Rev

Rev 1716 | Go to most recent revision | 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: 1733 $
  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.         TCP_sendseqinit eax
  517.  
  518. ;        mov     [ebx + TCP_SOCKET.timer_retransmission],   ;; todo: create macro to set retransmission timer
  519.  
  520.         mov     ebx, eax
  521.  
  522.         lea     eax, [ebx + STREAM_SOCKET.snd]
  523.         call    SOCKET_ring_create
  524.  
  525.         lea     eax, [ebx + STREAM_SOCKET.rcv]
  526.         call    SOCKET_ring_create
  527.  
  528.         mov     [ebx + SOCKET.lock], 0
  529.  
  530.         mov     eax, ebx
  531.         call    TCP_output
  532.  
  533.         mov     dword [esp+32], 0
  534.         ret
  535.  
  536. align 4
  537.   .ip:
  538.         lea     ebx, [eax + SOCKET.lock]
  539.         call    wait_mutex
  540.  
  541.         push    dword [edx + 4]
  542.         pop     dword [eax + IP_SOCKET.RemoteIP]
  543.  
  544.         push    eax
  545.         init_queue (eax + SOCKET_QUEUE_LOCATION)        ; Set up data receiving queue
  546.         pop     eax
  547.  
  548.         mov     [eax + SOCKET.lock], 0
  549.         mov     dword [esp+32], 0
  550.         ret
  551.  
  552.  
  553. ;-----------------------------------------------------------------
  554. ;
  555. ; SOCKET_listen
  556. ;
  557. ;  IN:  socket number in ecx
  558. ;       backlog in edx
  559. ;  OUT: eax is socket num, -1 on error
  560. ;
  561. ;-----------------------------------------------------------------
  562. align 4
  563. SOCKET_listen:
  564.  
  565.         DEBUGF  1,"SOCKET_listen: socknum: %u backlog: %u\n", ecx, edx
  566.  
  567.         call    SOCKET_num_to_ptr
  568.         jz      s_error
  569.  
  570.         cmp     word [eax + SOCKET.Domain], AF_INET4
  571.         jne     s_error
  572.  
  573.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  574.         jne     s_error
  575.  
  576.         cmp     [eax + TCP_SOCKET.LocalPort], 0
  577.         je      s_error
  578.  
  579.         cmp     [eax + IP_SOCKET.LocalIP], 0
  580.         jne     @f
  581.         push    [IP_LIST]
  582.         pop     [eax + IP_SOCKET.LocalIP]
  583.        @@:
  584.  
  585.         cmp     edx, MAX_backlog
  586.         jle     @f
  587.         mov     edx, MAX_backlog
  588.        @@:
  589.  
  590.         mov     [eax + SOCKET.backlog], dx
  591.         or      [eax + SOCKET.options], SO_ACCEPTCON
  592.         mov     [eax + TCP_SOCKET.t_state], TCB_LISTEN
  593.  
  594.         push    eax
  595.         init_queue (eax + SOCKET_QUEUE_LOCATION)                ; Set up sockets queue
  596.         pop     eax
  597.  
  598.         mov     dword [esp+32], 0
  599.  
  600.         ret
  601.  
  602.  
  603. ;-----------------------------------------------------------------
  604. ;
  605. ; SOCKET_accept
  606. ;
  607. ;  IN:  socket number in ecx
  608. ;       addr in edx
  609. ;       addrlen in esi
  610. ;  OUT: eax is socket num, -1 on error
  611. ;
  612. ;-----------------------------------------------------------------
  613. align 4
  614. SOCKET_accept:
  615.  
  616.         DEBUGF  1,"SOCKET_accept: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
  617.  
  618.         call    SOCKET_num_to_ptr
  619.         jz      s_error
  620.  
  621.         test    [eax + SOCKET.options], SO_ACCEPTCON
  622.         jz      s_error
  623.  
  624.         cmp     word [eax + SOCKET.Domain], AF_INET4
  625.         jne     s_error
  626.  
  627.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  628.         jne     s_error
  629.  
  630.         get_from_queue (eax + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, s_error
  631.  
  632.         mov     eax, [esi]
  633.         call    SOCKET_ptr_to_num
  634.         jz      s_error
  635.         mov     dword [esp+32], eax
  636.         ret
  637.  
  638.  
  639. ;-----------------------------------------------------------------
  640. ;
  641. ; SOCKET_close
  642. ;
  643. ;  IN:  socket number in ecx
  644. ;  OUT: eax is socket num, -1 on error
  645. ;
  646. ;-----------------------------------------------------------------
  647. align 4
  648. SOCKET_close:
  649.  
  650.         DEBUGF  1,"SOCKET_close: socknum: %u\n", ecx
  651.  
  652.         call    SOCKET_num_to_ptr
  653.         jz      s_error
  654.  
  655.         cmp     [eax + SOCKET.Domain], AF_INET4
  656.         jne     s_error
  657.  
  658.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  659.         je      .free
  660.  
  661.         cmp     [eax + SOCKET.Protocol], IP_PROTO_ICMP
  662.         je      .free
  663.  
  664.         cmp     [eax + SOCKET.Protocol], IP_PROTO_IP
  665.         je      .free
  666.  
  667.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  668.         je      .tcp
  669.  
  670.         jmp     s_error
  671.  
  672.   .tcp:
  673.         cmp     [eax + TCP_SOCKET.t_state], TCB_SYN_RECEIVED    ; state must be LISTEN, SYN_SENT or CLOSED
  674.         jl      .free
  675.  
  676.         call    TCP_output
  677.         mov     dword [esp+32], 0
  678.  
  679.         ret
  680.  
  681.   .free:
  682.         call    SOCKET_free
  683.         mov     dword [esp+32], 0
  684.  
  685.         ret
  686.  
  687.  
  688. ;-----------------------------------------------------------------
  689. ;
  690. ; SOCKET_receive
  691. ;
  692. ;  IN:  socket number in ecx
  693. ;       addr to buffer in edx
  694. ;       length of buffer in esi
  695. ;       flags in edi
  696. ;  OUT: eax is number of bytes copied, -1 on error
  697. ;
  698. ;-----------------------------------------------------------------
  699. align 4
  700. SOCKET_receive:
  701.  
  702.         DEBUGF  1,"SOCKET_receive: socknum: %u bufaddr: %x, buflength: %u, flags: %x, ", ecx, edx, esi, edi
  703.  
  704.         call    SOCKET_num_to_ptr
  705.         jz      s_error
  706.  
  707.         jmp     [eax + SOCKET.rcv_proc]
  708.  
  709.  
  710. align 4
  711. SOCKET_receive_dgram:
  712.  
  713.         DEBUGF  1,"SOCKET_receive: DGRAM\n"
  714.  
  715.         mov     ebx, esi
  716.         mov     edi, edx                                        ; addr to buffer
  717.  
  718.         get_from_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, socket_queue_entry.size, s_error       ; destroys esi and ecx
  719.  
  720.         mov     ecx, [esi + socket_queue_entry.data_size]
  721.         DEBUGF  1,"Got %u bytes of data\n", ecx
  722.  
  723.         cmp     ecx, ebx
  724.         jg      .too_small
  725.  
  726.         push    [esi + socket_queue_entry.buf_ptr]              ; save the buffer addr so we can clear it later
  727.         mov     esi, [esi + socket_queue_entry.data_ptr]
  728.         DEBUGF  1,"Source buffer: %x, real addr: %x\n", [esp], esi
  729.         mov     dword[esp+32+4], ecx                            ; return number of bytes copied
  730.  
  731. ; copy the data
  732.         shr     ecx, 1
  733.         jnc     .nb
  734.         movsb
  735.   .nb:
  736.         shr     ecx, 1
  737.         jnc     .nw
  738.         movsw
  739.   .nw:
  740.         test    ecx, ecx
  741.         jz      .nd
  742.         rep     movsd
  743.   .nd:
  744.  
  745.         call    kernel_free                                     ; remove the packet
  746.         ret
  747.  
  748.   .too_small:
  749.  
  750.         DEBUGF  1,"Buffer too small...\n"
  751.         jmp     s_error
  752.  
  753. align 4
  754. SOCKET_receive_tcp:
  755.  
  756.         DEBUGF  1,"SOCKET_receive: TCP\n"
  757.  
  758.         mov     ecx, esi
  759.         mov     edi, edx
  760.         add     eax, STREAM_SOCKET.rcv
  761.         call    SOCKET_ring_read
  762.         call    SOCKET_ring_free
  763.  
  764.         mov     dword[esp+32], ecx                              ; return number of bytes copied
  765.  
  766.         ret
  767.  
  768.  
  769. ;-----------------------------------------------------------------
  770. ;
  771. ; SOCKET_send
  772. ;
  773. ;
  774. ;  IN:  socket number in ecx
  775. ;       pointer to data in edx
  776. ;       datalength in esi
  777. ;       flags in edi
  778. ;  OUT: -1 on error
  779. ;
  780. ;-----------------------------------------------------------------
  781. align 4
  782. SOCKET_send:
  783.  
  784.         DEBUGF  1,"SOCKET_send: socknum: %u data ptr: %x, length: %u, flags: %x, ", ecx, edx, esi, edi
  785.  
  786.         call    SOCKET_num_to_ptr
  787.         jz      s_error
  788.  
  789.         jmp     [eax + SOCKET.snd_proc]
  790.  
  791.  
  792. align 4
  793. SOCKET_send_udp:
  794.  
  795.         DEBUGF  1,"SOCKET_send: UDP\n"
  796.  
  797.         mov     ecx, esi
  798.         mov     esi, edx
  799.  
  800.         call    UDP_output
  801.  
  802.         mov     dword [esp+32], 0
  803.         ret
  804.  
  805.  
  806. align 4
  807. SOCKET_send_tcp:
  808.  
  809.         DEBUGF  1,"SOCKET_send: TCP\n"
  810.  
  811.         push    eax
  812.         mov     ecx, esi
  813.         mov     esi, edx
  814.         add     eax, STREAM_SOCKET.snd
  815.         call    SOCKET_ring_write
  816.         pop     eax
  817.  
  818.         call    TCP_output
  819.  
  820.         mov     [esp+32], eax
  821.         ret
  822.  
  823.  
  824. align 4
  825. SOCKET_send_ip:
  826.  
  827.         DEBUGF  1,"type: IP\n"
  828.  
  829.         mov     ecx, esi
  830.         mov     esi, edx
  831.  
  832.         call    IPv4_output_raw
  833.  
  834.         mov     dword [esp+32], eax
  835.         ret
  836.  
  837. align 4
  838. SOCKET_send_icmp:
  839.  
  840.         DEBUGF  1,"SOCKET_send: ICMP\n"
  841.  
  842.         mov     ecx, esi
  843.         call    ICMP_output_raw
  844.  
  845.         mov     dword [esp+32], 0
  846.         ret
  847.  
  848.  
  849.  
  850.  
  851. ;-----------------------------------------------------------------
  852. ;
  853. ; SOCKET_get_options
  854. ;
  855. ;  IN:  ecx = socket number
  856. ;       edx = pointer to the options:
  857. ;               dd      level, optname, optval, optlen
  858. ;  OUT: -1 on error
  859. ;
  860. ; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
  861. ; TODO: find best way to notify that send()'ed data were acknowledged
  862. ; Also pseudo-optname -3 is valid and returns socket state, one of TCB_*.
  863. ;
  864. ;-----------------------------------------------------------------
  865. align 4
  866. SOCKET_get_opt:
  867.  
  868.         DEBUGF  1,"SOCKET_get_opt\n"
  869.  
  870.         call    SOCKET_num_to_ptr
  871.         jz      s_error
  872.  
  873.         cmp     dword [edx], IP_PROTO_TCP
  874.         jnz     s_error
  875.         cmp     dword [edx+4], -2
  876.         jz      @f
  877.         cmp     dword [edx+4], -3
  878.         jnz     s_error
  879. @@:
  880. ;        mov     eax, [edx+12]
  881. ;        test    eax, eax
  882. ;        jz      .fail
  883. ;        cmp     dword [eax], 4
  884. ;        mov     dword [eax], 4
  885. ;        jb      .fail
  886. ;        stdcall net_socket_num_to_addr, ecx
  887. ;        test    eax, eax
  888. ;        jz      .fail
  889. ;        ; todo: check that eax is really TCP socket
  890. ;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
  891. ;        cmp     dword [edx+4], -2
  892. ;        jz      @f
  893. ;        mov     ecx, [eax + TCP_SOCKET.state]
  894. @@:
  895.         mov     eax, [edx+8]
  896.         test    eax, eax
  897.         jz      @f
  898.         mov     [eax], ecx
  899. @@:
  900.         mov     dword [esp+32], 0
  901.         ret
  902.  
  903.  
  904.  
  905.  
  906. align 4
  907. SOCKET_set_opt:
  908.  
  909.         ret
  910.  
  911.  
  912.  
  913. ;-----------------------------------------------------------------
  914. ;
  915. ; SOCKET_debug
  916. ;
  917. ;  Copies socket variables to application buffer
  918. ;
  919. ;  IN:  ecx = socket number
  920. ;       edx = pointer to buffer
  921. ;
  922. ;  OUT: -1 on error
  923. ;-----------------------------------------------------------------
  924. align 4
  925. SOCKET_debug:
  926.  
  927.         DEBUGF  1,"socket_debug\n"
  928.  
  929.         call    SOCKET_num_to_ptr
  930.         jz      s_error
  931.  
  932.         mov     esi, eax
  933.         mov     edi, edx
  934.         mov     ecx, SOCKETBUFFSIZE/4
  935.         rep     movsd
  936.  
  937.         mov     dword [esp+32], 0
  938.         ret
  939.  
  940.  
  941. ;-----------------------------------------------------------------
  942. ;
  943. ; SOCKET_find_port
  944. ;
  945. ; Fills in the local port number for TCP and UDP sockets
  946. ; This procedure always works because the number of sockets is
  947. ; limited to a smaller number then the number of possible ports
  948. ;
  949. ;  IN:  eax = socket pointer
  950. ;  OUT: /
  951. ;
  952. ;-----------------------------------------------------------------
  953. align 4
  954. SOCKET_find_port:
  955.  
  956.         DEBUGF  1,"SOCKET_find_port\n"
  957.  
  958.         push    ebx esi ecx
  959.  
  960.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  961.         je      .udp
  962.  
  963.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  964.         je      .tcp
  965.  
  966.         jmp     .error
  967.  
  968.   .done:
  969.         mov     [eax + UDP_SOCKET.LocalPort], bx
  970.   .error:
  971.         pop     ecx esi ebx
  972.         ret
  973.  
  974.   .udp:
  975.         mov     bx, [last_UDP_port]
  976.         call    .findit
  977.         mov     [last_UDP_port], bx
  978.         jmp     .done
  979.  
  980.   .tcp:
  981.         mov     bx, [last_TCP_port]
  982.         call    .findit
  983.         mov     [last_TCP_port], bx
  984.         jmp     .done
  985.  
  986.  
  987.   .restart:
  988.         mov     bx, MIN_EPHEMERAL_PORT
  989.   .findit:
  990.         inc     bx
  991.  
  992.         cmp     bx, MAX_EPHEMERAL_PORT
  993.         jz      .restart
  994.  
  995.         call    SOCKET_check_port
  996.         jz      .findit
  997.  
  998.         ret
  999.  
  1000.  
  1001.  
  1002. ;-----------------------------------------------------------------
  1003. ;
  1004. ; SOCKET_check_port (to be used with AF_INET only!)
  1005. ;
  1006. ; Checks if a local port number is unused
  1007. ; If the proposed port number is unused, it is filled in in the socket structure
  1008. ;
  1009. ;  IN:  eax = socket ptr (to find out if its a TCP/UDP socket)
  1010. ;        bx = proposed socket number
  1011. ;
  1012. ;  OUT:  ZF = cleared on error
  1013. ;
  1014. ;-----------------------------------------------------------------
  1015. align 4
  1016. SOCKET_check_port:
  1017.  
  1018.         DEBUGF  1,"SOCKET_check_port\n"
  1019.  
  1020.         mov     ecx, [eax + SOCKET.Protocol]
  1021.         mov     esi, net_sockets
  1022.  
  1023.   .next_socket:
  1024.         mov     esi, [esi + SOCKET.NextPtr]
  1025.         or      esi, esi
  1026.         jz      .port_ok
  1027.  
  1028.         cmp     [esi + SOCKET.Protocol], ecx
  1029.         jne     .next_socket
  1030.  
  1031.         cmp     [esi + UDP_SOCKET.LocalPort], bx
  1032.         jne     .next_socket
  1033.  
  1034.         DEBUGF  1,"local port %u already in use\n", bx
  1035.         ret
  1036.  
  1037.   .port_ok:
  1038.         mov     [eax + UDP_SOCKET.LocalPort], bx
  1039.         or      bx, bx                                  ; set the zero-flag
  1040.  
  1041.         ret
  1042.  
  1043.  
  1044.  
  1045. ;-----------------------------------------------------------------
  1046. ;
  1047. ; SOCKET_input
  1048. ;
  1049. ; Updates a (stateless) socket with received data
  1050. ;
  1051. ; Note: the mutex should already be set !
  1052. ;
  1053. ;  IN:  eax = socket ptr
  1054. ;       ebx = pointer to device struct
  1055. ;       ecx = data size
  1056. ;       esi = ptr to data
  1057. ;       [esp] = ptr to buf
  1058. ;       [esp + 4] = buf size
  1059. ;
  1060. ;  OUT: /
  1061. ;
  1062. ;-----------------------------------------------------------------
  1063. align 4
  1064. SOCKET_input:
  1065.  
  1066.         DEBUGF  1,"SOCKET_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
  1067.  
  1068.         mov     dword[esp+4], ecx
  1069.         push    esi
  1070.         mov     esi, esp
  1071.  
  1072.         add_to_queue (eax + SOCKET_QUEUE_LOCATION), SOCKET_QUEUE_SIZE, socket_queue_entry.size, SOCKET_input.full
  1073.  
  1074.         DEBUGF  1,"SOCKET_input: queued packet successfully\n"
  1075.         add     esp, socket_queue_entry.size
  1076.         mov     [eax + SOCKET.lock], 0
  1077.         jmp     SOCKET_notify_owner
  1078.  
  1079.   .full:
  1080.         DEBUGF  2,"SOCKET_input: socket %x is full!\n", eax
  1081.         mov     [eax + SOCKET.lock], 0
  1082.         call    kernel_free
  1083.         add     esp, 8
  1084.  
  1085.         ret
  1086.  
  1087.  
  1088. ;--------------------------
  1089. ;
  1090. ; eax = ptr to ring struct (just a buffer of the right size)
  1091. ;
  1092. align 4
  1093. SOCKET_ring_create:
  1094.  
  1095.         push    esi
  1096.         mov     esi, eax
  1097.  
  1098.         push    edx
  1099.         stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW
  1100.         pop     edx
  1101.  
  1102.         DEBUGF  1,"SOCKET_ring_created: %x\n", eax
  1103.         mov     [esi + RING_BUFFER.start_ptr], eax
  1104.         mov     [esi + RING_BUFFER.write_ptr], eax
  1105.         mov     [esi + RING_BUFFER.read_ptr], eax
  1106.         mov     [esi + RING_BUFFER.size], 0
  1107.         add     eax,  SOCKET_MAXDATA
  1108.         mov     [esi + RING_BUFFER.end_ptr], eax
  1109.         mov     eax, esi
  1110.         pop     esi
  1111.  
  1112.         ret
  1113.  
  1114. ;-----------------------------------------------------------------
  1115. ;
  1116. ; SOCKET_ring_write
  1117. ;
  1118. ; Adds data to a stream socket, and updates write pointer and size
  1119. ;
  1120. ;  IN:  eax = ptr to ring struct
  1121. ;       ecx = data size
  1122. ;       esi = ptr to data
  1123. ;
  1124. ;  OUT: ecx = number of bytes stored
  1125. ;
  1126. ;-----------------------------------------------------------------
  1127. align 4
  1128. SOCKET_ring_write:
  1129.  
  1130.         DEBUGF  1,"SOCKET_ring_write: ringbuff=%x ptr=%x size=%u\n", eax, esi, ecx
  1131.  
  1132.         add     [eax + RING_BUFFER.size], ecx
  1133.         cmp     [eax + RING_BUFFER.size], SOCKET_MAXDATA
  1134.         jg      .too_large
  1135.  
  1136.   .copy:
  1137.         mov     edi, [eax + RING_BUFFER.write_ptr]
  1138.         DEBUGF  2,"SOCKET_ring_write: %u bytes from %x to %x\n", ecx, esi, edi
  1139.  
  1140.         push    ecx
  1141.         shr     ecx, 1
  1142.         jnc     .nb
  1143.         movsb
  1144.   .nb:
  1145.         shr     ecx, 1
  1146.         jnc     .nw
  1147.         movsw
  1148.   .nw:
  1149.         test    ecx, ecx
  1150.         jz      .nd
  1151.         rep     movsd
  1152.   .nd:
  1153.         pop     ecx
  1154.  
  1155.         cmp     edi, [eax + RING_BUFFER.end_ptr]
  1156.         jge     .wrap
  1157.         mov     [eax + RING_BUFFER.write_ptr], edi
  1158.  
  1159.         ret
  1160.  
  1161.   .wrap:
  1162.         sub     edi, SOCKET_MAXDATA
  1163.         mov     [eax + RING_BUFFER.write_ptr], edi
  1164.  
  1165.         ret
  1166.  
  1167.   .too_large:
  1168.         mov     ecx, SOCKET_MAXDATA                             ; calculate number of bytes available in buffer
  1169.         sub     ecx, [eax + RING_BUFFER.size]
  1170.         jge     .full
  1171.  
  1172.         mov     [eax + RING_BUFFER.size], SOCKET_MAXDATA        ; update size, we will fill buffer completely
  1173.         jmp     .copy
  1174.  
  1175.   .full:
  1176.         DEBUGF  2,"SOCKET_ring_write: ring buffer is full!\n"
  1177.         xor     ecx, ecx
  1178.         ret
  1179.  
  1180.  
  1181. ;-----------------------------------------------------------------
  1182. ;
  1183. ; SOCKET_ring_read
  1184. ;
  1185. ; reads the data, BUT DOES NOT CLEAR IT FROM MEMORY YET
  1186. ;
  1187. ;  IN:  eax = ptr to ring struct
  1188. ;       ecx = buffer size
  1189. ;       edi = ptr to buffer
  1190. ;
  1191. ;  OUT: ecx = number of bytes read
  1192. ;
  1193. ;-----------------------------------------------------------------
  1194. align 4
  1195. SOCKET_ring_read:
  1196.  
  1197.         DEBUGF  1,"SOCKET_ring_read: ringbuff=%x ptr=%x size=%u\n", eax, edi, ecx
  1198.  
  1199.         cmp     ecx, [eax + RING_BUFFER.size]
  1200.         jg      .less_data
  1201.  
  1202.   .copy:
  1203.         mov     esi, [eax + RING_BUFFER.read_ptr]
  1204.  
  1205.         DEBUGF  2,"SOCKET_ring_read: %u bytes from %x to %x\n", ecx, esi, edi
  1206.         push    ecx
  1207.         shr     ecx, 1
  1208.         jnc     .nb
  1209.         movsb
  1210.   .nb:
  1211.         shr     ecx, 1
  1212.         jnc     .nw
  1213.         movsw
  1214.   .nw:
  1215.         test    ecx, ecx
  1216.         jz      .nd
  1217.         rep     movsd
  1218.   .nd:
  1219.         pop     ecx
  1220.  
  1221. ;  .no_data_at_all:
  1222.         ret
  1223.  
  1224.   .less_data:
  1225.         mov     ecx, [eax + RING_BUFFER.size]
  1226.         test    ecx, ecx
  1227. ;        jz      .no_data_at_all
  1228.         jmp     .copy
  1229.  
  1230.  
  1231. ;-----------------------------------------------------------------
  1232. ;
  1233. ; SOCKET_ring_free
  1234. ;
  1235. ; Free's some bytes from the ringbuffer
  1236. ;
  1237. ;  IN:  eax = ptr to ring struct
  1238. ;       ecx = data size
  1239. ;
  1240. ;  OUT: ecx = number of bytes free-ed
  1241. ;
  1242. ;-----------------------------------------------------------------
  1243. align 4
  1244. SOCKET_ring_free:
  1245.  
  1246.         DEBUGF  1,"SOCKET_ring_free: %u bytes from ring %x\n", ecx, eax
  1247.  
  1248.         sub     [eax + RING_BUFFER.size], ecx
  1249.         jl      .sumthinwong
  1250.         add     [eax + RING_BUFFER.read_ptr], ecx
  1251.  
  1252.         mov     edx, [eax + RING_BUFFER.end_ptr]
  1253.         cmp     [eax + RING_BUFFER.read_ptr], edx
  1254.         jl      @f
  1255.         sub     [eax + RING_BUFFER.read_ptr], SOCKET_MAXDATA
  1256.        @@:
  1257.         ret
  1258.  
  1259.   .sumthinwong:                ; we could free all available bytes, but that would be stupid, i guess..
  1260.         add     [eax + RING_BUFFER.size], ecx
  1261.         xor     ecx, ecx
  1262.         ret
  1263.  
  1264.  
  1265. ;-----------------------------------------------------------------
  1266. ;
  1267. ; SOCKET_notify_owner
  1268. ;
  1269. ; notify's the owner of a socket that something happened
  1270. ;
  1271. ;  IN:  eax = socket ptr
  1272. ;  OUT: /
  1273. ;
  1274. ;-----------------------------------------------------------------
  1275. align 4
  1276. SOCKET_notify_owner:
  1277.  
  1278.         DEBUGF  1,"SOCKET_notify_owner: %x\n", eax
  1279.  
  1280.         call    SOCKET_check
  1281.         jz      .error
  1282.  
  1283.         push    eax ecx esi
  1284.  
  1285. ; socket exists, now try to flag an event to the application
  1286.  
  1287.         mov     eax, [eax + SOCKET.PID]
  1288.         mov     ecx, 1
  1289.         mov     esi, TASK_DATA + TASKDATA.pid
  1290.  
  1291.        .next_pid:
  1292.         cmp     [esi], eax
  1293.         je      .found_pid
  1294.         inc     ecx
  1295.         add     esi, 0x20
  1296.         cmp     ecx, [TASK_COUNT]
  1297.         jbe     .next_pid
  1298.  
  1299. ; PID not found, TODO: close socket!
  1300.  
  1301.         jmp     .error2
  1302.  
  1303.        .found_pid:
  1304.         shl     ecx, 8
  1305.         or      [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK
  1306.         mov     [check_idle_semaphore], 200
  1307.  
  1308.         DEBUGF  1,"SOCKET_notify_owner: succes!\n"
  1309.  
  1310.   .error2:
  1311.         pop     esi ecx eax
  1312.  
  1313.   .error:
  1314.  
  1315.         ret
  1316.  
  1317.  
  1318. ;--------------------------------------------------------------------
  1319. ;
  1320. ; SOCKET_alloc
  1321. ;
  1322. ; Allocate memory for socket data and put new socket into the list
  1323. ; Newly created socket is initialized with calling PID and number and
  1324. ; put into beginning of list (which is a fastest way).
  1325. ;
  1326. ; IN:  /
  1327. ; OUT: eax = 0 on error, socket ptr otherwise
  1328. ;      edi = socket number
  1329. ;       ZF = cleared on error
  1330. ;
  1331. ;--------------------------------------------------------------------
  1332. align 4
  1333. SOCKET_alloc:
  1334.  
  1335.         push    ecx ebx
  1336.  
  1337.         stdcall kernel_alloc, SOCKETBUFFSIZE
  1338.         DEBUGF  1, "SOCKET_alloc: ptr=%x\n", eax
  1339.         or      eax, eax
  1340.         jz      .exit
  1341.  
  1342. ; zero-initialize allocated memory
  1343.         push    eax edi
  1344.         mov     edi, eax
  1345.         mov     ecx, SOCKETBUFFSIZE / 4
  1346.         xor     eax, eax
  1347.         rep     stosd
  1348.         pop     edi eax
  1349.  
  1350. ; set send-and receive procedures to return -1
  1351.         mov     [eax + SOCKET.snd_proc], s_error
  1352.         mov     [eax + SOCKET.rcv_proc], s_error
  1353.  
  1354. ; find first free socket number and use it
  1355.         mov     ecx, [last_socket_num]
  1356.   .next_socket_number:
  1357.         inc     ecx
  1358.         jz      .next_socket_number     ; avoid socket nr 0
  1359.         cmp     ecx, -1
  1360.         je      .next_socket_number     ; avoid socket nr -1
  1361.         mov     ebx, net_sockets
  1362.   .next_socket:
  1363.         mov     ebx, [ebx + SOCKET.NextPtr]
  1364.         test    ebx, ebx
  1365.         jz      .last_socket
  1366.  
  1367.         cmp     [ebx + SOCKET.Number], ecx
  1368.         jne     .next_socket
  1369.         jmp     .next_socket_number
  1370.  
  1371.   .last_socket:
  1372.         mov     [last_socket_num], ecx
  1373.         mov     [eax + SOCKET.Number], ecx
  1374.         DEBUGF  1, "SOCKET_alloc: number=%u\n", ecx
  1375.         mov     edi, ecx
  1376.  
  1377. ; Fill in PID
  1378.         mov     ebx, [TASK_BASE]
  1379.         mov     ebx, [ebx + TASKDATA.pid]
  1380.         mov     [eax + SOCKET.PID], ebx
  1381.  
  1382. ; add socket to the list by re-arranging some pointers
  1383.         mov     ebx, [net_sockets + SOCKET.NextPtr]
  1384.  
  1385.         mov     [eax + SOCKET.PrevPtr], net_sockets
  1386.         mov     [eax + SOCKET.NextPtr], ebx
  1387.  
  1388.         test    ebx, ebx
  1389.         jz      @f
  1390.         add     ebx, SOCKET.lock        ; lock the next socket
  1391.         call    wait_mutex
  1392.         sub     ebx, SOCKET.lock
  1393.         mov     [ebx + SOCKET.PrevPtr], eax
  1394.         mov     [ebx + SOCKET.lock], 0  ; and unlock it again
  1395.        @@:
  1396.  
  1397.         mov     [net_sockets + SOCKET.NextPtr], eax
  1398.         or      eax, eax                ; used to clear zero flag
  1399.   .exit:
  1400.         pop     ebx ecx
  1401.  
  1402.         ret
  1403.  
  1404.  
  1405. ;----------------------------------------------------
  1406. ;
  1407. ; SOCKET_free
  1408. ;
  1409. ; Free socket data memory and remove socket from the list
  1410. ;
  1411. ; IN:  eax = socket ptr
  1412. ; OUT: /
  1413. ;
  1414. ;----------------------------------------------------
  1415. align 4
  1416. SOCKET_free:
  1417.  
  1418.         DEBUGF  1, "SOCKET_free: %x\n", eax
  1419.  
  1420.         call    SOCKET_check
  1421.         jz      .error
  1422.  
  1423.         push    ebx
  1424.         lea     ebx, [eax + SOCKET.lock]
  1425.         call    wait_mutex
  1426.  
  1427.         DEBUGF  1, "SOCKET_free: freeing socket..\n"
  1428.  
  1429.         cmp     [eax + SOCKET.Domain], AF_INET4
  1430.         jnz     .no_tcp
  1431.  
  1432.         cmp     [eax + SOCKET.Protocol], IP_PROTO_TCP
  1433.         jnz     .no_tcp
  1434.  
  1435.         mov     ebx, eax
  1436.         stdcall kernel_free, [ebx + STREAM_SOCKET.rcv + RING_BUFFER.start_ptr]
  1437.         stdcall kernel_free, [ebx + STREAM_SOCKET.snd + RING_BUFFER.start_ptr]
  1438.         mov     eax, ebx
  1439.   .no_tcp:
  1440.  
  1441.         push    eax                             ; this will be passed to kernel_free
  1442.         mov     ebx, [eax + SOCKET.NextPtr]
  1443.         mov     eax, [eax + SOCKET.PrevPtr]
  1444.  
  1445.         DEBUGF  1, "SOCKET_free: linking socket %x to socket %x\n", eax, ebx
  1446.  
  1447.         test    eax, eax
  1448.         jz      @f
  1449.         mov     [eax + SOCKET.NextPtr], ebx
  1450.        @@:
  1451.  
  1452.         test    ebx, ebx
  1453.         jz      @f
  1454.         mov     [ebx + SOCKET.PrevPtr], eax
  1455.        @@:
  1456.  
  1457.         call    kernel_free
  1458.         pop     ebx
  1459.  
  1460.         DEBUGF  1, "SOCKET_free: success!\n"
  1461.  
  1462.   .error:
  1463.         ret
  1464.  
  1465. ;------------------------------------
  1466. ;
  1467. ; SOCKET_fork
  1468. ;
  1469. ; Create a child socket
  1470. ;
  1471. ; IN:  socket nr in ebx
  1472. ; OUT: child socket nr in eax
  1473. ;
  1474. ;-----------------------------------
  1475. align 4
  1476. SOCKET_fork:
  1477.  
  1478.         DEBUGF  1,"SOCKET_fork: %x\n", ebx
  1479.  
  1480. ; Exit if backlog queue is full
  1481.         mov     eax, [ebx + SOCKET_QUEUE_LOCATION + queue.size]
  1482.         cmp     ax, [ebx + SOCKET.backlog]
  1483.         jge     .fail
  1484.  
  1485. ; Allocate new socket
  1486.         call    SOCKET_alloc
  1487.         jz      .fail
  1488.  
  1489.         push    esi ecx edi
  1490.         push    eax
  1491.         mov     esi, esp
  1492.         add_to_queue (ebx + SOCKET_QUEUE_LOCATION), MAX_backlog, 4, .fail2
  1493.         pop     eax
  1494.  
  1495. ; Copy structure from current socket to new
  1496. ; We start at PID to preserve the socket num, and the 2 pointers at beginning of socket
  1497.         lea     esi, [ebx + SOCKET.PID]
  1498.         lea     edi, [eax + SOCKET.PID]
  1499.         mov     ecx, (SOCKET_QUEUE_LOCATION - SOCKET.PID + 3)/4
  1500.         rep     movsd
  1501.  
  1502.         and     [eax + SOCKET.options], not SO_ACCEPTCON
  1503.  
  1504.         call    SOCKET_notify_owner
  1505.         pop     edi ecx esi
  1506.  
  1507.         ret
  1508.  
  1509.   .fail2:
  1510.         add     esp, 4+4+4
  1511.   .fail:
  1512.         DEBUGF  1,"SOCKET_fork: failed\n"
  1513.         xor     eax, eax
  1514.         ret
  1515.  
  1516.  
  1517. ;---------------------------------------------------
  1518. ;
  1519. ; SOCKET_num_to_ptr
  1520. ;
  1521. ; Get socket structure address by its number
  1522. ;
  1523. ; IN:  ecx = socket number
  1524. ; OUT: eax = 0 on error, socket ptr otherwise
  1525. ;       ZF = set on error
  1526. ;
  1527. ;---------------------------------------------------
  1528. align 4
  1529. SOCKET_num_to_ptr:
  1530.  
  1531.         DEBUGF  1,"SOCKET_num_to_ptr: %u ", ecx
  1532.  
  1533.         mov     eax, net_sockets
  1534.  
  1535.   .next_socket:
  1536.         mov     eax, [eax + SOCKET.NextPtr]
  1537.         or      eax, eax
  1538.         jz      .error
  1539.         cmp     [eax + SOCKET.Number], ecx
  1540.         jne     .next_socket
  1541.  
  1542.         test    eax, eax
  1543.  
  1544.         DEBUGF  1,"(%x)\n", eax
  1545.   .error:
  1546.         ret
  1547.  
  1548.  
  1549. ;---------------------------------------------------
  1550. ;
  1551. ; SOCKET_ptr_to_num
  1552. ;
  1553. ; Get socket number by its address
  1554. ;
  1555. ; IN:  eax = socket ptr
  1556. ; OUT: eax = 0 on error, socket num otherwise
  1557. ;       ZF = set on error
  1558. ;
  1559. ;---------------------------------------------------
  1560. align 4
  1561. SOCKET_ptr_to_num:
  1562.  
  1563.         DEBUGF  1,"SOCKET_ptr_to_num: %x ", eax
  1564.  
  1565.         call    SOCKET_check
  1566.         jz      .error
  1567.  
  1568.         mov     eax, [eax + SOCKET.Number]
  1569.  
  1570.         DEBUGF  1,"(%u)\n", eax
  1571.  
  1572.   .error:
  1573.         ret
  1574.  
  1575.  
  1576. ;---------------------------------------------------
  1577. ;
  1578. ; SOCKET_check
  1579. ;
  1580. ; checks if the given value is really a socket ptr
  1581. ;
  1582. ; IN:  eax = socket ptr
  1583. ; OUT: eax = 0 on error, unchanged otherwise
  1584. ;       ZF = set on error
  1585. ;
  1586. ;---------------------------------------------------
  1587. align 4
  1588. SOCKET_check:
  1589.  
  1590.         DEBUGF  1,"SOCKET_check: %x\n", eax
  1591.  
  1592.         push    ebx
  1593.         mov     ebx, net_sockets
  1594.  
  1595.   .next_socket:
  1596.         mov     ebx, [ebx + SOCKET.NextPtr]
  1597.         or      ebx, ebx
  1598.         jz      .done
  1599.         cmp     ebx, eax
  1600.         jnz     .next_socket
  1601.  
  1602.   .done:
  1603.         mov     eax, ebx
  1604.         test    eax, eax
  1605.         pop     ebx
  1606.  
  1607.         ret
  1608.  
  1609.  
  1610.  
  1611. ;---------------------------------------------------
  1612. ;
  1613. ; SOCKET_check_owner
  1614. ;
  1615. ; checks if the caller application owns the socket
  1616. ;
  1617. ; IN:  eax = socket ptr
  1618. ; OUT:  ZF = true/false
  1619. ;
  1620. ;---------------------------------------------------
  1621. align 4
  1622. SOCKET_check_owner:
  1623.  
  1624.         DEBUGF  1,"SOCKET_check_owner: %x\n", eax
  1625.  
  1626.         push    ebx
  1627.         mov     ebx, [TASK_BASE]
  1628.         mov     ebx, [ecx + TASKDATA.pid]
  1629.         cmp     [eax + SOCKET.PID], ebx
  1630.         pop      ebx
  1631.  
  1632.         ret
  1633.  
  1634.  
  1635.  
  1636.  
  1637. ;---------------------------------------------------
  1638. ;
  1639. ; SOCKET_process_end
  1640. ;
  1641. ; Kernel calls this function when a certain process ends
  1642. ; This function will check if the process had any open sockets
  1643. ; And update them accordingly
  1644. ;
  1645. ; IN:  eax = pid
  1646. ; OUT: /
  1647. ;
  1648. ;------------------------------------------------------
  1649. align 4
  1650. SOCKET_process_end:
  1651.  
  1652.         DEBUGF  1,"SOCKET_process_end: %x\n", eax
  1653.  
  1654.         push    ebx
  1655.         mov     ebx, net_sockets
  1656.  
  1657.   .next_socket:
  1658.  
  1659.         mov     ebx, [ebx + SOCKET.NextPtr]
  1660.   .test_socket:
  1661.         test    ebx, ebx
  1662.         jz      .done
  1663.  
  1664.         cmp     [ebx + SOCKET.PID], eax
  1665.         jne     .next_socket
  1666.  
  1667.         DEBUGF  1,"closing socket %x", eax, ebx
  1668.  
  1669.         mov     [ebx + SOCKET.PID], 0
  1670.  
  1671.         cmp     [ebx + SOCKET.Protocol], IP_PROTO_UDP
  1672.         je      .udp
  1673.  
  1674.         cmp     [ebx + SOCKET.Protocol], IP_PROTO_TCP
  1675.         je      .tcp
  1676.  
  1677.         jmp     .next_socket    ; kill all sockets for given PID
  1678.  
  1679.   .udp:
  1680.         mov     eax, ebx
  1681.         mov     ebx, [ebx + SOCKET.NextPtr]
  1682.         call    SOCKET_free
  1683.         jmp     .test_socket
  1684.  
  1685.   .tcp:
  1686.  
  1687.         jmp     .next_socket
  1688.  
  1689.   .done:
  1690.         pop     ebx
  1691.  
  1692.         ret
  1693.