Subversion Repositories Kolibri OS

Rev

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

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