Subversion Repositories Kolibri OS

Rev

Rev 1514 | 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. ;;     based on code by mike.dld                                   ;;
  10. ;;                                                                 ;;
  11. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  12. ;;             Version 2, June 1991                                ;;
  13. ;;                                                                 ;;
  14. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  15.  
  16. $Revision: 1519 $
  17.  
  18. virtual at 0
  19.  
  20.         SOCKET:
  21.         .NextPtr                dd ? ; pointer to next socket in list
  22.         .PrevPtr                dd ? ; pointer to previous socket in list
  23.         .Number                 dd ? ; socket number
  24.  
  25.         .lock                   dd ? ; lock mutex
  26.  
  27.         .PID                    dd ? ; application process id
  28.         .Domain                 dd ? ; INET/UNIX/..
  29.         .Type                   dd ? ; RAW/UDP/TCP/...
  30.         .Protocol               dd ? ; ICMP/IPv4/ARP/
  31.         .errorcode              dd ?
  32.  
  33.         .options                dd ?
  34.         .SO_SND.SB_CC           dd ?  ;;;;; socket options: number of bytes in socket
  35.         .SO_RCV.SB_CC           dd ?
  36.         .state                  dd ?  ;;;;;;;;;
  37.  
  38.         .end:
  39. end virtual
  40.  
  41. virtual at SOCKET.end
  42.  
  43.         IP_SOCKET:
  44.  
  45.         .LocalIP                dd ?
  46.                                 rd 3 ; for IPv6 addresses
  47.  
  48.         .RemoteIP               dd ?
  49.                                 rd 3 ; for IPv6 addresses
  50.  
  51.         .end:
  52. end virtual
  53.  
  54. virtual at SOCKET.end
  55.  
  56.         SOCKET_virtual:
  57.  
  58.         .ConnectedTo            dd ? ; Socket number of other socket this one is connected to
  59.  
  60.         .end:
  61. end virtual
  62.  
  63. virtual at IP_SOCKET.end
  64.  
  65.         TCP_SOCKET:
  66.  
  67.         .LocalPort              dw ? ; In INET byte order
  68.         .RemotePort             dw ? ; In INET byte order
  69.  
  70.         .backlog                dw ? ; Backlog
  71.         .backlog_cur            dw ? ; current size of queue for un-accept-ed connections
  72.  
  73.         .OrigRemoteIP           dd ? ; original remote IP address (used to reset to LISTEN state)
  74.         .OrigRemotePort         dw ? ; original remote port (used to reset to LISTEN state)
  75.  
  76.         .t_state                dd ? ; TCB state
  77.         .t_rxtshift             dd ?
  78.         .t_rxtcur               dd ?
  79.         .t_dupacks              dd ?
  80.         .t_maxseg               dd ?
  81.         .t_force                dd ?
  82.         .t_flags                dd ?
  83.  
  84. ;---------------
  85. ; RFC783 page 21
  86.  
  87. ; send sequence
  88.         .SND_UNA                dd ? ; sequence number of unack'ed sent Packets
  89.         .SND_NXT                dd ? ; next send sequence number to use
  90.         .SND_UP                 dd ?
  91.         .SND_WL1                dd ? ; window minus one
  92.         .SND_WL2                dd ? ;
  93.         .ISS                    dd ? ; initial send sequence number
  94.         .SND_WND                dd ? ; send window
  95.  
  96. ; receive sequence
  97.         .RCV_WND                dw ? ; receive window
  98.         .RCV_NXT                dd ? ; next receive sequence number to use
  99.         .RCV_UP                 dd ?
  100.         .IRS                    dd ? ; initial receive sequence number
  101.  
  102. ;---------------------
  103. ; Additional variables
  104.  
  105. ; receive variables
  106.         .RCV_ADV                dd ?
  107.  
  108. ; retransmit variables
  109.         .SND_MAX                dd ?
  110.  
  111. ; congestion control
  112.         .SND_CWND               dd ?
  113.         .SND_SSTHRESH           dd ?
  114.  
  115. ;----------------------
  116. ; Transmit timing stuff
  117.  
  118.         .t_idle                 dd ?
  119.         .t_rtt                  dd ?
  120.         .t_rtseq                dd ?
  121.         .t_srtt                 dd ?
  122.         .t_rttvar               dd ?
  123.         .t_rttmin               dd ?
  124.         .max_sndwnd             dd ?
  125.  
  126. ;-----------------
  127. ; Out-of-band data
  128.  
  129.         .t_oobflags             dd ?
  130.         .t_iobc                 dd ?
  131.         .t_softerror            dd ?
  132.  
  133.  
  134. ;---------
  135. ; RFC 1323
  136.  
  137.         .SND_SCALE              db ? ; Scale factor
  138.         .RCV_SCALE              db ?
  139.         .request_r_scale        db ?
  140.         .requested_s_scale      dd ?
  141.  
  142.         .ts_recent              dd ?
  143.         .ts_recent_age          dd ?
  144.         .last_ack_sent          dd ?
  145.  
  146.  
  147. ;-------
  148. ; Timers
  149.  
  150.         .timer_retransmission   dw ?    ; rexmt
  151.         .timer_ack              dw ?
  152.         .timer_persist          dw ?
  153.         .timer_keepalive        dw ?    ; keepalive/syn timeout
  154.         .timer_timed_wait       dw ?    ; also used as 2msl timer
  155.  
  156.         .end:
  157. end virtual
  158.  
  159. virtual at IP_SOCKET.end
  160.  
  161.         UDP_SOCKET:
  162.  
  163.         .LocalPort              dw ? ; In INET byte order
  164.         .RemotePort             dw ? ; In INET byte order
  165.         .firstpacket            db ?
  166.  
  167.         .end:
  168. end virtual
  169.  
  170. virtual at IP_SOCKET.end
  171.  
  172.         ICMP_SOCKET:
  173.  
  174.         .Identifier             dw ? ;
  175.  
  176.         .end:
  177. end virtual
  178.  
  179.  
  180. struct  socket_queue_entry
  181. ;        .owner          dd ?
  182.         .data_ptr       dd ?
  183.         .buf_ptr        dd ?
  184.         .data_size      dd ?
  185.         .size:
  186. ends
  187.  
  188.  
  189. MAX_backlog             equ 20       ; backlog for stream sockets
  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_UDP_port   dw ? ; These values give the number of the last used ephemeral port
  199.         last_TCP_port   dw ? ;
  200. endg
  201.  
  202.  
  203. ;-----------------------------------------------------------------
  204. ;
  205. ; SOCKET_init
  206. ;
  207. ;  -
  208. ;
  209. ;  IN:  /
  210. ;  OUT: /
  211. ;
  212. ;-----------------------------------------------------------------
  213. align 4
  214. socket_init:
  215.  
  216.         xor     eax, eax
  217.         mov     edi, net_sockets
  218.         mov     ecx, 4
  219.         rep     stosd
  220.  
  221.         mov     [last_UDP_port], MIN_EPHEMERAL_PORT
  222.         mov     [last_TCP_port], MIN_EPHEMERAL_PORT
  223.  
  224.         ret
  225.  
  226.  
  227. ;-----------------------------------------------------------------
  228. ;
  229. ; Socket API (function 74)
  230. ;
  231. ;-----------------------------------------------------------------
  232. align 4
  233. sys_socket:
  234.         cmp     ebx, 8          ; highest possible number
  235.         jg      s_error
  236.         lea     ebx, [.table + 4*ebx]
  237.         jmp     dword [ebx]
  238.  
  239. .table:
  240.         dd      SOCKET_open     ; 0
  241.         dd      SOCKET_close    ; 1
  242.         dd      SOCKET_bind     ; 2
  243.         dd      SOCKET_listen   ; 3
  244.         dd      SOCKET_connect  ; 4
  245.         dd      SOCKET_accept   ; 5
  246.         dd      SOCKET_send     ; 6
  247.         dd      SOCKET_receive  ; 7
  248.         dd      SOCKET_get_opt  ; 8
  249. ;        dd      SOCKET_set_opt  ; 9
  250.  
  251.  
  252. s_error:
  253.         DEBUGF  1,"socket error\n"
  254.         mov     dword [esp+32], -1
  255.  
  256.         ret
  257.  
  258.  
  259. ;-----------------------------------------------------------------
  260. ;
  261. ; SOCKET_open
  262. ;
  263. ;  IN:  domain in ecx
  264. ;       type in edx
  265. ;       protocol in esi
  266. ;  OUT: eax is socket num, -1 on error
  267. ;
  268. ;-----------------------------------------------------------------
  269. align 4
  270. SOCKET_open:
  271.  
  272.         DEBUGF  1,"socket_open: domain: %u, type: %u protocol: %x\n", ecx, edx, esi
  273.  
  274.         call    SOCKET_alloc
  275.         jz      s_error
  276.  
  277.         mov     [eax + SOCKET.Domain], ecx
  278.         mov     [eax + SOCKET.Type], edx
  279.         mov     [eax + SOCKET.Protocol], esi
  280.  
  281.         mov     [esp+32], edi
  282.  
  283.         ret
  284.  
  285.  
  286.  
  287. ;-----------------------------------------------------------------
  288. ;
  289. ; SOCKET_bind
  290. ;
  291. ;  IN:  socket number in ecx
  292. ;       pointer to sockaddr struct in edx
  293. ;       length of that struct in esi
  294. ;  OUT: 0 on success
  295. ;
  296. ;-----------------------------------------------------------------
  297. align 4
  298. SOCKET_bind:
  299.  
  300.         DEBUGF  1,"socket_bind: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
  301.  
  302.         call    SOCKET_num_to_ptr
  303.         jz      s_error
  304.  
  305.         cmp     esi, 2
  306.         jl      s_error
  307.  
  308.         cmp     word [edx], AF_INET4
  309.         je      .af_inet4
  310.  
  311.         cmp     word [edx], AF_UNIX
  312.         je      .af_unix
  313.  
  314.         jmp     s_error
  315.  
  316.   .af_unix:
  317.  
  318.         ; TODO: write code here
  319.  
  320.         mov     dword [esp+32], 0
  321.         ret
  322.  
  323.   .af_inet4:
  324.  
  325.         DEBUGF  1,"af_inet4\n"
  326.  
  327.         cmp     esi, 6
  328.         jl      s_error
  329.  
  330.         mov     ecx, [eax + SOCKET.Type]
  331.  
  332.         mov     bx, word [edx + 2]
  333.         test    bx, bx
  334.         jz      .find_free
  335.  
  336.         call    SOCKET_check_port
  337. ;        test    bx, bx
  338.         jz      s_error
  339.         jmp     .got_port
  340.  
  341.     .find_free:
  342.         call    SOCKET_find_port
  343. ;        test    bx, bx
  344.         jz      s_error
  345.  
  346.     .got_port:
  347.         DEBUGF  1,"using local port: %u\n", bx
  348.         mov     word [eax + UDP_SOCKET.LocalPort], bx
  349.  
  350.         mov     ebx, dword [edx + 4]
  351.         mov     dword [eax + IP_SOCKET.LocalIP], ebx
  352.  
  353.         DEBUGF  1,"local ip: %u.%u.%u.%u\n",\
  354.         [eax + IP_SOCKET.LocalIP + 0]:1,[eax + IP_SOCKET.LocalIP + 1]:1,\
  355.         [eax + IP_SOCKET.LocalIP + 2]:1,[eax + IP_SOCKET.LocalIP + 3]:1
  356.  
  357.         mov     dword [esp+32], 0
  358.         ret
  359.  
  360.  
  361.  
  362.  
  363. ;-----------------------------------------------------------------
  364. ;
  365. ; SOCKET_connect
  366. ;
  367. ;  IN:  socket number in ecx
  368. ;       pointer to sockaddr struct in edx
  369. ;       length of that struct in esi
  370. ;  OUT: 0 on success
  371. ;
  372. ;-----------------------------------------------------------------
  373. align 4
  374. SOCKET_connect:
  375.  
  376.         DEBUGF  1,"socket_connect: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
  377.  
  378.         call    SOCKET_num_to_ptr
  379.         jz      s_error
  380.  
  381.         cmp     esi, 8
  382.         jl      s_error
  383.  
  384.         cmp     word [edx], AF_INET4
  385.         je      .af_inet4
  386.  
  387.         jmp     s_error
  388.  
  389.   .af_inet4:
  390.  
  391.         cmp     [eax + SOCKET.Type], IP_PROTO_UDP
  392.         je      .udp
  393.  
  394.         cmp     [eax + SOCKET.Type], IP_PROTO_TCP
  395.         je      .tcp
  396.  
  397.         jmp     s_error
  398.  
  399.   .udp:
  400.         mov     bx , word [edx + 2]
  401.         mov     word [eax + UDP_SOCKET.RemotePort], bx
  402.         mov     [eax + UDP_SOCKET.firstpacket], 0
  403.         DEBUGF  1,"remote port: %u\n",bx
  404.  
  405.         mov     ebx, dword [edx + 4]
  406.         mov     dword [eax + IP_SOCKET.RemoteIP], ebx
  407.         DEBUGF  1,"remote ip: %u.%u.%u.%u\n",[edx+4]:1,[edx+5]:1,[edx+6]:1,[edx+7]:1
  408.  
  409.         mov     dword [esp+32], 0
  410.         ret
  411.  
  412.  
  413.   .tcp:
  414.         ; set sequence number
  415.  
  416.         mov     ebx, [TCP_sequence_num]
  417.         add     [TCP_sequence_num], 6400
  418.         mov     [eax + TCP_SOCKET.ISS], ebx
  419.  
  420.         mov     [eax + TCP_SOCKET.timer_keepalive], 120         ; 120*630ms => 75,6 seconds
  421.  
  422.         lea     ebx, [eax + SOCKET.lock]
  423.         call    wait_mutex
  424.  
  425.         ; fill in remote port and IP
  426.  
  427. ;;;;;;        mov     [eax + TCP_SOCKET.wndsizeTimer], 0     ; Reset the window timer.
  428.  
  429.         mov     bx , word [edx + 2]
  430.         mov     [eax + TCP_SOCKET.RemotePort], bx
  431.         DEBUGF  1,"remote port: %u\n", bx
  432.  
  433.         mov     ebx, dword [edx + 4]
  434.         mov     [eax + IP_SOCKET.RemoteIP], ebx
  435.  
  436.         ; check if local port and IP is ok
  437.  
  438.         cmp     [eax + IP_SOCKET.LocalIP], 0
  439.         jne     @f
  440.         push    [IP_LIST]           ;;;;; device zero = default
  441.         pop     [eax + IP_SOCKET.LocalIP]
  442.        @@:
  443.  
  444.         cmp     [eax + TCP_SOCKET.LocalPort], 0
  445.         jne     @f
  446.         call    SOCKET_find_port
  447.        @@:
  448.  
  449.         DEBUGF  1,"remote port: %u\n", [eax + TCP_SOCKET.LocalPort]:2
  450.  
  451. ;        mov     [eax + TCP_SOCKET.t_state], TCB_SYN_SENT
  452.         call    TCP_output
  453.  
  454.         mov     [eax + SOCKET.lock], 0
  455.  
  456.         mov     dword [esp+32], 0       ; success!
  457.         ret
  458.  
  459.  
  460. ;-----------------------------------------------------------------
  461. ;
  462. ; SOCKET_listen
  463. ;
  464. ;  IN:  socket number in ecx
  465. ;       backlog in edx
  466. ;  OUT: eax is socket num, -1 on error
  467. ;
  468. ;-----------------------------------------------------------------
  469. align 4
  470. SOCKET_listen:
  471.  
  472.         DEBUGF  1,"Socket_listen: socknum: %u backlog: %u\n", ecx, edx
  473.  
  474.         call    SOCKET_num_to_ptr
  475.         jz      s_error
  476.  
  477.         cmp     word [eax + SOCKET.Domain], AF_INET4
  478.         jne     s_error
  479.  
  480.         cmp     [eax + SOCKET.Type], IP_PROTO_TCP
  481.         jne     s_error
  482.  
  483.         ; TODO: check local port number
  484.  
  485.         cmp     edx, MAX_backlog
  486.         jle     .ok
  487.         mov     edx, MAX_backlog
  488.   .ok:
  489.  
  490.         mov     [eax + TCP_SOCKET.backlog], dx
  491.         mov     [eax + TCP_SOCKET.t_state], TCB_LISTEN
  492.          or     [eax + SOCKET.options], SO_ACCEPTCON
  493.  
  494.         mov     dword [esp+32], 0
  495.  
  496.         ret
  497.  
  498.  
  499. ;-----------------------------------------------------------------
  500. ;
  501. ; SOCKET_accept
  502. ;
  503. ;  IN:  socket number in ecx
  504. ;       addr in edx
  505. ;       addrlen in esi
  506. ;  OUT: eax is socket num, -1 on error
  507. ;
  508. ;-----------------------------------------------------------------
  509. align 4
  510. SOCKET_accept:
  511.  
  512.         DEBUGF  1,"Socket_accept: socknum: %u sockaddr: %x, length: %u\n", ecx, edx, esi
  513.  
  514.         call    SOCKET_num_to_ptr
  515.         jz      s_error
  516.  
  517.         cmp     word [eax + SOCKET.Domain], AF_INET4
  518.         je      .af_inet4
  519.  
  520.         jmp     s_error
  521.  
  522.   .af_inet4:
  523.  
  524.         cmp     [eax + SOCKET.Type], IP_PROTO_TCP
  525.         je      .tcp
  526.  
  527.         jmp     s_error
  528.  
  529.   .tcp:
  530.  
  531.         lea     ebx, [eax + SOCKET.lock]
  532.         call    wait_mutex
  533.  
  534.         movzx   ebx, [eax + TCP_SOCKET.backlog_cur]
  535.         test    ebx, ebx
  536.         jz      .unlock_err
  537.  
  538.         dec     [eax + TCP_SOCKET.backlog_cur]
  539.         mov     eax, [eax + TCP_SOCKET.end + (ebx-1)*4]
  540.         mov     [eax + SOCKET.lock], 0
  541.         mov     dword [esp+32], 0
  542.  
  543.         call    TCP_output      ;;;;;
  544.  
  545.         ret
  546.  
  547.   .unlock_err:
  548.         mov     [eax + SOCKET.lock], 0
  549.         jmp     s_error
  550.  
  551.  
  552. ;-----------------------------------------------------------------
  553. ;
  554. ; SOCKET_close
  555. ;
  556. ;  IN:  socket number in ecx
  557. ;  OUT: eax is socket num, -1 on error
  558. ;
  559. ;-----------------------------------------------------------------
  560. align 4
  561. SOCKET_close:
  562.  
  563.         DEBUGF  1,"socket_close: socknum: %u\n", ecx
  564.  
  565.         call    SOCKET_num_to_ptr
  566.         jz      s_error
  567.  
  568.         cmp     [eax + SOCKET.Domain], AF_INET4
  569.         jne     s_error
  570.  
  571.         cmp     [eax + SOCKET.Type], IP_PROTO_UDP
  572.         je      .free
  573.  
  574.         cmp     [eax + SOCKET.Type], IP_PROTO_ICMP
  575.         je      .free
  576.  
  577.         cmp     [eax + SOCKET.Type], IP_PROTO_TCP
  578.         je      .tcp
  579.  
  580.         jmp     s_error
  581.  
  582.   .tcp:
  583.         test    [eax + TCP_SOCKET.t_state], TCB_SYN_RECEIVED  ;;;;;;
  584.         jz      .free
  585.  
  586.         call    TCP_output
  587.  
  588.         mov     dword [esp+32], 0
  589.  
  590.         ret
  591.  
  592. ;       state must be LISTEN, SYN_SENT, CLOSED or maybe even invalid
  593. ;       so, we may destroy the socket
  594.   .free:
  595.         call    SOCKET_free
  596.         mov     dword [esp+32], 0
  597.  
  598.         ret
  599.  
  600.  
  601. ;-----------------------------------------------------------------
  602. ;
  603. ; SOCKET_receive
  604. ;
  605. ;  IN:  socket number in ecx
  606. ;       addr to buffer in edx
  607. ;       length of buffer in esi
  608. ;       flags in edi
  609. ;  OUT: eax is number of bytes copied, -1 on error
  610. ;
  611. ;-----------------------------------------------------------------
  612. align 4
  613. SOCKET_receive:
  614.  
  615.         DEBUGF  1,"socket_receive: socknum: %u bufaddr: %x, buflength: %u, flags: %x\n", ecx, edx, esi, edi
  616.  
  617.         call    SOCKET_num_to_ptr
  618.         jz      s_error
  619.  
  620.         mov     ebx, esi
  621.         get_from_queue (eax + SOCKET_QUEUE_LOCATION),\
  622.                        SOCKET_QUEUE_SIZE,\
  623.                        socket_queue_entry.size,\
  624.                        s_error
  625.                        ; destroys esi and ecx
  626.  
  627.         mov     edi, edx                                        ; addr to buffer
  628.  
  629.         mov     ecx, [esi + socket_queue_entry.data_size]
  630.         DEBUGF  1,"Got %u bytes of data\n", ecx
  631.  
  632.         cmp     ecx, ebx
  633.         jle     .large_enough
  634.         DEBUGF  1,"Buffer too small...\n"
  635.         jmp     s_error
  636.  
  637.   .large_enough:
  638.         push    [esi + socket_queue_entry.buf_ptr]             ; save the buffer addr so we can clear it later
  639.         mov     esi, [esi + socket_queue_entry.data_ptr]
  640.         DEBUGF  1,"Source buffer: %x, real addr: %x\n", [esp], esi
  641.         mov     dword[esp+32+4], ecx                            ; return number of bytes copied in ebx
  642.  
  643. ; copy the data
  644.         shr     ecx, 1
  645.         jnc     .nb
  646.         movsb
  647. .nb:    shr     ecx, 1
  648.         jnc     .nw
  649.         movsw
  650. .nw:    test    ecx, ecx
  651.         jz      .nd
  652.         rep     movsd
  653. .nd:
  654. ; remove the packet     ;;; TODO: only if it is empty!!
  655.  
  656. ;;;;        call    TCP_output  ; only if it is tcp
  657.  
  658.         call    kernel_free
  659.  
  660.         ret
  661.  
  662.  
  663. ;-----------------------------------------------------------------
  664. ;
  665. ; SOCKET_send
  666. ;
  667. ;
  668. ;  IN:  socket number in ecx
  669. ;       pointer to data in edx
  670. ;       datalength in esi
  671. ;       flags in edi
  672. ;  OUT: -1 on error
  673. ;
  674. ;-----------------------------------------------------------------
  675. align 4
  676. SOCKET_send:
  677.  
  678.         DEBUGF  1,"socket_send: socknum: %u sockaddr: %x, length: %u, flags: %x\n", ecx, edx, esi, edi
  679.  
  680.         call    SOCKET_num_to_ptr
  681.         jz      s_error
  682.  
  683.         cmp     word [eax + SOCKET.Domain], AF_INET4
  684.         je      .af_inet4
  685.  
  686.         jmp     s_error
  687.  
  688.   .af_inet4:
  689.         DEBUGF  1,"af_inet4\n"
  690.  
  691.         cmp     [eax + SOCKET.Type], IP_PROTO_TCP
  692.         je      .tcp
  693.  
  694.         cmp     [eax + SOCKET.Type], IP_PROTO_UDP
  695.         je      .udp
  696.  
  697.         jmp     s_error
  698.  
  699.   .udp:
  700.         DEBUGF  1,"type: UDP\n"
  701.  
  702. ; check if local port is valid
  703.         cmp     [eax + UDP_SOCKET.LocalPort], 0
  704.         jne     @f
  705.  
  706.         call    SOCKET_find_port
  707.         jz      s_error
  708.  
  709. ; Now, send the packet
  710.      @@:
  711.         mov     ecx, esi
  712.         mov     esi, edx
  713.  
  714.         call    UDP_output
  715.  
  716.         mov     dword [esp+32], 0
  717.         ret
  718.  
  719.   .tcp:
  720.         DEBUGF  1,"type: TCP\n"
  721.  
  722. ; check if local port is valid
  723.         cmp     [eax + TCP_SOCKET.LocalPort], 0
  724.         jne     @f
  725.  
  726.         call    SOCKET_find_port
  727.         jz      s_error
  728.  
  729.      @@:
  730. ;;;; TODO: queue the data
  731.  
  732.         call    TCP_output
  733.  
  734.         mov     [esp+32], eax
  735.         ret
  736.  
  737.  
  738.  
  739.  
  740. ;-----------------------------------------------------------------
  741. ;
  742. ; SOCKET_get_options
  743. ;
  744. ;  IN:  ecx = socket number
  745. ;       edx = pointer to the options:
  746. ;               dd      level, optname, optval, optlen
  747. ;  OUT: -1 on error
  748. ;
  749. ; At moment, uses only pseudo-optname -2 for get last_ack_number for TCP.
  750. ; TODO: find best way to notify that send()'ed data were acknowledged
  751. ; Also pseudo-optname -3 is valid and returns socket state, one of TCB_*.
  752. ;
  753. ;-----------------------------------------------------------------
  754. align 4
  755. SOCKET_get_opt:
  756.  
  757.         DEBUGF  1,"socket_get_opt\n"
  758.  
  759.         call    SOCKET_num_to_ptr
  760.         jz      s_error
  761.  
  762.         cmp     dword [edx], IP_PROTO_TCP
  763.         jnz     s_error
  764.         cmp     dword [edx+4], -2
  765.         jz      @f
  766.         cmp     dword [edx+4], -3
  767.         jnz     s_error
  768. @@:
  769. ;        mov     eax, [edx+12]
  770. ;        test    eax, eax
  771. ;        jz      .fail
  772. ;        cmp     dword [eax], 4
  773. ;        mov     dword [eax], 4
  774. ;        jb      .fail
  775. ;        stdcall net_socket_num_to_addr, ecx
  776. ;        test    eax, eax
  777. ;        jz      .fail
  778. ;        ; todo: check that eax is really TCP socket
  779. ;        mov     ecx, [eax + TCP_SOCKET.last_ack_number]
  780. ;        cmp     dword [edx+4], -2
  781. ;        jz      @f
  782. ;        mov     ecx, [eax + TCP_SOCKET.state]
  783. @@:
  784.         mov     eax, [edx+8]
  785.         test    eax, eax
  786.         jz      @f
  787.         mov     [eax], ecx
  788. @@:
  789.         mov     dword [esp+32], 0
  790.         ret
  791.  
  792.  
  793. ;-----------------------------------------------------------------
  794. ;
  795. ; SOCKET_find_port
  796. ;
  797. ; Fills in the local port number for TCP and UDP sockets
  798. ; This procedure always works because the number of sockets is
  799. ; limited to a smaller number then the number of possible ports
  800. ;
  801. ;  IN:  eax = socket pointer
  802. ;  OUT: /
  803. ;
  804. ;-----------------------------------------------------------------
  805. align 4
  806. SOCKET_find_port:
  807.  
  808.         DEBUGF  1,"socket_find_free_port\n"
  809.  
  810.         push    ebx esi ecx
  811.  
  812.         cmp     [eax + SOCKET.Type], IP_PROTO_UDP
  813.         je      .udp
  814.  
  815.         cmp     [eax + SOCKET.Type], IP_PROTO_TCP
  816.         je      .tcp
  817.  
  818.         jmp     .error
  819.  
  820.   .done:
  821.         mov     [eax + UDP_SOCKET.LocalPort], bx
  822.   .error:
  823.         pop     ecx esi ebx
  824.         ret
  825.  
  826.   .udp:
  827.         mov     bx, [last_UDP_port]
  828.         call    .findit
  829.         mov     [last_UDP_port], bx
  830.         jmp     .done
  831.  
  832.   .tcp:
  833.         mov     bx, [last_TCP_port]
  834.         call    .findit
  835.         mov     [last_TCP_port], bx
  836.         jmp     .done
  837.  
  838.  
  839.   .restart:
  840.         mov     bx, MIN_EPHEMERAL_PORT
  841.   .findit:
  842.         inc     bx
  843.  
  844.         cmp     bx, MAX_EPHEMERAL_PORT
  845.         jz      .restart
  846.  
  847.         call    SOCKET_check_port
  848.         jz      .findit
  849.  
  850.         ret
  851.  
  852.  
  853.  
  854. ;-----------------------------------------------------------------
  855. ;
  856. ; SOCKET_check_port
  857. ;
  858. ; Checks if a local port number is unused
  859. ; If the proposed port number is unused, it is filled in in the socket structure
  860. ;
  861. ;  IN:  eax = socket ptr (to find out if its a TCP/UDP socket)
  862. ;        bx = proposed socket number
  863. ;
  864. ;  OUT:  ZF = cleared on error
  865. ;
  866. ;-----------------------------------------------------------------
  867. align 4
  868. SOCKET_check_port:
  869.  
  870.         DEBUGF  1,"socket_check_port\n"
  871.  
  872.         mov     ecx, [eax + SOCKET.Type]
  873.         mov     esi, net_sockets
  874.  
  875.   .next_socket:
  876.         mov     esi, [esi + SOCKET.NextPtr]
  877.         or      esi, esi
  878.         jz      .port_ok
  879.  
  880.         cmp     [esi + SOCKET.Type], ecx
  881.         jne     .next_socket
  882.  
  883.         cmp     [esi + UDP_SOCKET.LocalPort], bx
  884.         jne     .next_socket
  885.  
  886.         DEBUGF  1,"local port %u already in use\n", bx
  887.         ret
  888.  
  889.   .port_ok:
  890.         mov     [eax + UDP_SOCKET.LocalPort], bx
  891.         or      bx, bx                                  ; set the zero-flag
  892.  
  893.         ret
  894.  
  895.  
  896.  
  897. ;-----------------------------------------------------------------
  898. ;
  899. ; SOCKET_input
  900. ;
  901. ; Updates a socket with received data
  902. ;
  903. ; Note: the mutex should already be set !
  904. ;
  905. ;  IN:  eax = socket ptr
  906. ;       ebx = pointer to device struct
  907. ;       ecx = data size
  908. ;       esi = ptr to data
  909. ;       [esp] = ptr to buf
  910. ;       [esp + 4] = buf size
  911. ;
  912. ;  OUT: /
  913. ;
  914. ;-----------------------------------------------------------------
  915. align 4
  916. SOCKET_input:
  917.  
  918.         DEBUGF  1,"socket_input: socket=%x, data=%x size=%u\n", eax, esi, ecx
  919.  
  920.         mov     dword[esp+4], ecx
  921.         push    esi
  922.         mov     esi, esp
  923.  
  924.         add_to_queue (eax + SOCKET_QUEUE_LOCATION),\
  925.                      SOCKET_QUEUE_SIZE,\
  926.                      socket_queue_entry.size,\
  927.                      SOCKET_input.full
  928.  
  929.         DEBUGF  1,"Queued packet successfully\n"
  930.         add     esp, socket_queue_entry.size
  931.         mov     [eax + SOCKET.lock], 0
  932.         jmp     SOCKET_notify_owner
  933.  
  934.   .full:
  935.         DEBUGF  2,"Socket %x is full!\n", eax
  936.         mov     [eax + SOCKET.lock], 0
  937.         call    kernel_free
  938.         add     esp, 8
  939.  
  940.         ret
  941.  
  942. ;-----------------------------------------------------------------
  943. ;
  944. ; SOCKET_notify_owner
  945. ;
  946. ; notify's the owner of a socket that something happened
  947. ;
  948. ;  IN:  eax = socket ptr
  949. ;  OUT: /
  950. ;
  951. ;-----------------------------------------------------------------
  952. align 4
  953. SOCKET_notify_owner:
  954.  
  955.         DEBUGF  1,"socket_notify_owner\n"
  956.  
  957.         call    SOCKET_check
  958.         jz      .error
  959.  
  960.         push    ecx eax esi
  961.  
  962. ; socket exists, now try to flag an event to the application
  963.  
  964.         mov     eax, [eax + SOCKET.PID]
  965.         mov     ecx, 1
  966.         mov     esi, TASK_DATA + TASKDATA.pid
  967.  
  968.        .next_pid:
  969.         cmp     [esi], eax
  970.         je      .found_pid
  971.         inc     ecx
  972.         add     esi, 0x20
  973.         cmp     ecx, [TASK_COUNT]
  974.         jbe     .next_pid
  975.  
  976. ; PID not found, TODO: close socket!
  977.  
  978.         jmp     .error2
  979.  
  980.        .found_pid:
  981.         shl     ecx, 8
  982.         or      [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK
  983.         mov     [check_idle_semaphore], 200
  984.  
  985.         DEBUGF  1,"owner notified\n"
  986.  
  987.   .error2:
  988.         pop     esi eax ecx
  989.   .error:
  990.  
  991.         ret
  992.  
  993.  
  994. ;--------------------------------------------------------------------
  995. ;
  996. ; SOCKET_alloc
  997. ;
  998. ; Allocate memory for socket data and put new socket into the list
  999. ; Newly created socket is initialized with calling PID and number and
  1000. ; put into beginning of list (which is a fastest way).
  1001. ;
  1002. ; IN:  /
  1003. ; OUT: eax = 0 on error, socket ptr otherwise
  1004. ;      edi = socket number
  1005. ;       ZF = cleared on error
  1006. ;
  1007. ;--------------------------------------------------------------------
  1008. align 4
  1009. SOCKET_alloc:
  1010.  
  1011.         push    ecx ebx
  1012.  
  1013.         stdcall kernel_alloc, SOCKETBUFFSIZE
  1014.         DEBUGF  1, "socket_alloc: %x ", eax
  1015.         or      eax, eax
  1016.         jz      .exit
  1017.  
  1018. ; zero-initialize allocated memory
  1019.         push    eax edi
  1020.         mov     edi, eax
  1021.         mov     ecx, SOCKETBUFFSIZE / 4
  1022.         xor     eax, eax
  1023.         rep     stosd
  1024.         pop     edi eax
  1025.  
  1026.         init_queue (eax + SOCKET_QUEUE_LOCATION)
  1027.  
  1028. ; find first free socket number and use it
  1029.  
  1030.         mov     ebx, net_sockets
  1031.         xor     ecx, ecx
  1032.   .next_socket_number:
  1033.         inc     ecx
  1034.   .next_socket:
  1035.         mov     ebx, [ebx + SOCKET.NextPtr]
  1036.         or      ebx, ebx
  1037.         jz      .last_socket
  1038.         cmp     [ebx + SOCKET.Number], ecx
  1039.         jne     .next_socket
  1040.         mov     ebx, net_sockets
  1041.         jmp     .next_socket_number
  1042.  
  1043.   .last_socket:
  1044.         mov     [eax + SOCKET.Number], ecx
  1045.  
  1046.         DEBUGF  1, "(number: %u)\n", ecx
  1047.  
  1048. ; Fill in PID
  1049.         mov     ebx, [TASK_BASE]
  1050.         mov     ebx, [ebx + TASKDATA.pid]
  1051.  
  1052.         mov     [eax + SOCKET.PID], ebx
  1053.  
  1054. ; add socket to the list by changing pointers
  1055.  
  1056.         mov     ebx, [net_sockets + SOCKET.NextPtr]
  1057.  
  1058.         mov     [eax + SOCKET.PrevPtr], net_sockets
  1059.         mov     [eax + SOCKET.NextPtr], ebx
  1060.  
  1061.         or      ebx, ebx
  1062.         jz      @f
  1063.         add     ebx, SOCKET.lock        ; lock the next socket
  1064.         call    wait_mutex
  1065.         sub     ebx, SOCKET.lock
  1066.         mov     [ebx + SOCKET.PrevPtr], eax
  1067.         mov     [ebx + SOCKET.lock], 0
  1068.        @@:
  1069.  
  1070.         mov     [net_sockets + SOCKET.NextPtr], eax
  1071.  
  1072.         mov     edi, ecx
  1073.         or      eax, eax        ; used to clear zero flag
  1074.   .exit:
  1075.         pop     ebx ecx
  1076.  
  1077.         ret
  1078.  
  1079.  
  1080. ;----------------------------------------------------
  1081. ;
  1082. ; SOCKET_free
  1083. ;
  1084. ; Free socket data memory and remove socket from the list
  1085. ;
  1086. ; IN:  eax = socket ptr
  1087. ; OUT: /
  1088. ;
  1089. ;----------------------------------------------------
  1090. align 4
  1091. SOCKET_free:
  1092.  
  1093.         DEBUGF  1, "socket_free: %x\n", eax
  1094.  
  1095.         call    SOCKET_check
  1096.         jz      .error
  1097.  
  1098.         push    ebx
  1099.         lea     ebx, [eax + SOCKET.lock]
  1100.         call    wait_mutex
  1101.  
  1102.         DEBUGF  1, "freeing socket..\n"
  1103.  
  1104.         push    eax                             ; this will be passed to kernel_free
  1105.         mov     ebx, [eax + SOCKET.NextPtr]
  1106.         mov     eax, [eax + SOCKET.PrevPtr]
  1107.  
  1108.         DEBUGF  1, "linking socket %x to socket %x\n", eax, ebx
  1109.  
  1110.         test    eax, eax
  1111.         jz      @f
  1112.         mov     [eax + SOCKET.NextPtr], ebx
  1113.        @@:
  1114.  
  1115.         test    ebx, ebx
  1116.         jz      @f
  1117.         mov     [ebx + SOCKET.PrevPtr], eax
  1118.        @@:
  1119.  
  1120.         call    kernel_free
  1121.         pop     ebx
  1122.  
  1123.         DEBUGF  1, "socket is gone!\n"
  1124.  
  1125.   .error:
  1126.         ret
  1127.  
  1128.  
  1129. ;---------------------------------------------------
  1130. ;
  1131. ; SOCKET_num_to_ptr
  1132. ;
  1133. ; Get socket structure address by its number
  1134. ;
  1135. ; IN:  ecx = socket number
  1136. ; OUT: ecx = 0 on error, socket ptr otherwise
  1137. ;       ZF = set on error
  1138. ;
  1139. ;---------------------------------------------------
  1140. align 4
  1141. SOCKET_num_to_ptr:
  1142.  
  1143.         DEBUGF  1,"socket_num_to_ptr: %u ", ecx
  1144.  
  1145.         mov     eax, net_sockets
  1146.  
  1147.   .next_socket:
  1148.         mov     eax, [eax + SOCKET.NextPtr]
  1149.         or      eax, eax
  1150.         jz      .error
  1151.         cmp     [eax + SOCKET.Number], ecx
  1152.         jne     .next_socket
  1153.  
  1154.         test    eax, eax
  1155.  
  1156.         DEBUGF  1,"(%x)\n", eax
  1157.   .error:
  1158.         ret
  1159.  
  1160.  
  1161. ;---------------------------------------------------
  1162. ;
  1163. ; SOCKET_ptr_to_num
  1164. ;
  1165. ; Get socket number by its address
  1166. ;
  1167. ; IN:  eax = socket ptr
  1168. ; OUT: eax = 0 on error, socket num otherwise
  1169. ;       ZF = set on error
  1170. ;
  1171. ;---------------------------------------------------
  1172. align 4
  1173. SOCKET_ptr_to_num:
  1174.  
  1175.         DEBUGF  1,"socket_ptr_to_num: %x ", eax
  1176.  
  1177.         call    SOCKET_check
  1178.         jz      .error
  1179.  
  1180.         mov     eax, [eax + SOCKET.Number]
  1181.  
  1182.         DEBUGF  1,"(%u)\n", eax
  1183.  
  1184.   .error:
  1185.         ret
  1186.  
  1187.  
  1188. ;---------------------------------------------------
  1189. ;
  1190. ; SOCKET_check
  1191. ;
  1192. ; checks if the given value is really a socket ptr
  1193. ;
  1194. ; IN:  eax = socket ptr
  1195. ; OUT: eax = 0 on error, unchanged otherwise
  1196. ;       ZF = set on error
  1197. ;
  1198. ;---------------------------------------------------
  1199. align 4
  1200. SOCKET_check:
  1201.  
  1202.         DEBUGF  1,"socket_check\n"
  1203.  
  1204.         push    ebx
  1205.         mov     ebx, net_sockets
  1206.  
  1207.   .next_socket:
  1208.         mov     ebx, [ebx + SOCKET.NextPtr]
  1209.         or      ebx, ebx
  1210.         jz      .done
  1211.         cmp     ebx, eax
  1212.         jnz     .next_socket
  1213.  
  1214.   .done:
  1215.         mov     eax, ebx
  1216.         test    eax, eax
  1217.         pop     ebx
  1218.  
  1219.         ret
  1220.  
  1221.  
  1222.  
  1223. ;---------------------------------------------------
  1224. ;
  1225. ; SOCKET_check_owner
  1226. ;
  1227. ; checks if the caller application owns the socket
  1228. ;
  1229. ; IN:  eax = socket ptr
  1230. ; OUT:  ZF = true/false
  1231. ;
  1232. ;---------------------------------------------------
  1233. align 4
  1234. SOCKET_check_owner:
  1235.  
  1236.         DEBUGF  1,"socket_check_owner\n"
  1237.  
  1238.         push    ebx
  1239.         mov     ebx, [TASK_BASE]
  1240.         mov     ebx, [ecx + TASKDATA.pid]
  1241.         cmp     [eax + SOCKET.PID], ebx
  1242.         pop      ebx
  1243.  
  1244.         ret
  1245.  
  1246.  
  1247.  
  1248.  
  1249. ;---------------------------------------------------
  1250. ;
  1251. ; SOCKET_process_end
  1252. ;
  1253. ; Kernel calls this function when a certain process ends
  1254. ; This function will check if the process had any open sockets
  1255. ; And update them accordingly
  1256. ;
  1257. ; IN:  eax = pid
  1258. ; OUT: /
  1259. ;
  1260. ;------------------------------------------------------
  1261. align 4
  1262. SOCKET_process_end:
  1263.  
  1264.         DEBUGF  1,"socket_process_end: %x\n", eax
  1265.  
  1266.         push    ebx
  1267.         mov     ebx, net_sockets
  1268.  
  1269.   .next_socket:
  1270.  
  1271.         mov     ebx, [ebx + SOCKET.NextPtr]
  1272.   .test_socket:
  1273.         test    ebx, ebx
  1274.         jz      .done
  1275.  
  1276.         cmp     [ebx + SOCKET.PID], eax
  1277.         jne     .next_socket
  1278.  
  1279.         DEBUGF  1,"closing socket %x", eax, ebx
  1280.  
  1281.         mov     [ebx + SOCKET.PID], 0
  1282.  
  1283.         cmp     [ebx + SOCKET.Type], IP_PROTO_UDP
  1284.         je      .udp
  1285.  
  1286.         cmp     [ebx + SOCKET.Type], IP_PROTO_TCP
  1287.         je      .tcp
  1288.  
  1289.         jmp     .next_socket    ; kill all sockets for given PID
  1290.  
  1291.   .udp:
  1292.         mov     eax, ebx
  1293.         mov     ebx, [ebx + SOCKET.NextPtr]
  1294.         call    SOCKET_free
  1295.         jmp     .test_socket
  1296.  
  1297.   .tcp:
  1298.  
  1299.         jmp     .next_socket
  1300.  
  1301.   .done:
  1302.         pop     ebx
  1303.  
  1304.         ret
  1305.