Subversion Repositories Kolibri OS

Rev

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

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