Subversion Repositories Kolibri OS

Rev

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

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