Subversion Repositories Kolibri OS

Rev

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