Subversion Repositories Kolibri OS

Rev

Rev 8103 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2021. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  loopback.inc                                                   ;;
  7. ;;                                                                 ;;
  8. ;;  LoopBack device for KolibriOS                                  ;;
  9. ;;                                                                 ;;
  10. ;;    Written by hidnplayr@kolibrios.org                           ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. $Revision: 9054 $
  18.  
  19. iglobal
  20. align 4
  21.  
  22. LOOPBACK_DEVICE:
  23.  
  24.         .device_type     dd NET_DEVICE_LOOPBACK
  25.         .mtu             dd NET_BUFFER_SIZE - NET_BUFF.data
  26.         .name            dd .namestr
  27.  
  28.         .unload          dd loop_dummy
  29.         .reset           dd loop_dummy
  30.         .transmit        dd loop_input
  31.  
  32.         .link_state      dd -1
  33.         .hwacc           dd NET_HWACC_TCP_IPv4_IN + NET_HWACC_TCP_IPv4_OUT
  34.  
  35.         .bytes_tx        dq ?
  36.         .bytes_rx        dq ?
  37.  
  38.         .packets_tx      dd ?
  39.         .packets_tx_err  dd ?
  40.         .packets_tx_drop dd ?
  41.         .packets_tx_ovr  dd ?
  42.  
  43.         .packets_rx      dd ?
  44.         .packets_rx_err  dd ?
  45.         .packets_rx_drop dd ?
  46.         .packets_rx_ovr  dd ?
  47.  
  48.         .namestr         db 'loopback', 0
  49.  
  50. endg
  51.  
  52.  
  53. macro   loop_init {
  54. local   .fail
  55.  
  56.         mov     ebx, LOOPBACK_DEVICE
  57.         call    net_add_device
  58.  
  59.         cmp     eax, -1
  60.         je      .fail
  61.  
  62.         mov     [IPv4_address], 127 + 1 shl 24
  63.         mov     [IPv4_subnet], 255
  64.         mov     [IPv4_broadcast], 0xffffff00 + 127
  65.  
  66.   .fail:
  67. }
  68.  
  69. ;-----------------------------------------------------------------;
  70. ;                                                                 ;
  71. ; loop_dummy                                                      ;
  72. ;                                                                 ;
  73. ;   IN: /                                                         ;
  74. ;                                                                 ;
  75. ;  OUT: /                                                         ;
  76. ;                                                                 ;
  77. ;-----------------------------------------------------------------;
  78. align 4
  79. loop_dummy:
  80.         ret
  81.  
  82. ;-----------------------------------------------------------------;
  83. ;                                                                 ;
  84. ; loop_input                                                      ;
  85. ;                                                                 ;
  86. ;   IN: [esp+4] = Pointer to buffer                               ;
  87. ;                                                                 ;
  88. ;  OUT: eax = 0 on success, errorcode otherwise                   ;
  89. ;                                                                 ;
  90. ;-----------------------------------------------------------------;
  91. align 4
  92. loop_input:
  93.  
  94.         mov     eax, [esp+4]
  95.  
  96. ; Update stats
  97.         inc     [LOOPBACK_DEVICE.packets_tx]
  98.         inc     [LOOPBACK_DEVICE.packets_rx]
  99.  
  100.         mov     ecx, [eax + NET_BUFF.length]
  101.         add     dword[LOOPBACK_DEVICE.bytes_rx], ecx
  102.         adc     dword[LOOPBACK_DEVICE.bytes_rx + 4], 0
  103.         add     dword[LOOPBACK_DEVICE.bytes_tx], ecx
  104.         adc     dword[LOOPBACK_DEVICE.bytes_tx + 4], 0
  105.  
  106.         DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_input: ptr=%x size=%u\n", eax, ecx
  107.  
  108. ; Reverse buffptr and returnaddr on stack
  109.         pop     edx edi
  110.         push    edx .done edi
  111.  
  112. ; Set registers for protocol handlers
  113.         lea     edx, [eax + NET_BUFF.data]
  114.         mov     ebx, [eax + NET_BUFF.device]
  115.         mov     eax, [eax + NET_BUFF.type]
  116.  
  117. ; Place protocol handlers here
  118.         cmp     eax, AF_INET4
  119.         je      ipv4_input
  120.  
  121.         DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_input: Unknown packet type=%x\n", eax
  122.  
  123.   .dump:
  124.         DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_input: dumping\n"
  125.         call    net_buff_free
  126.  
  127.         or      eax, -1
  128.         ret
  129.  
  130.   .done:
  131.         xor     eax, eax
  132.         ret
  133.  
  134.  
  135. ;-----------------------------------------------------------------;
  136. ;                                                                 ;
  137. ; loop_output                                                     ;
  138. ;                                                                 ;
  139. ;  IN:  ecx = packet size                                         ;
  140. ;       edi = address family                                      ;
  141. ;                                                                 ;
  142. ; OUT:  eax = start of net frame / 0 on error                     ;
  143. ;       ebx = to device structure                                 ;
  144. ;       ecx = unchanged (packet size of embedded data)            ;
  145. ;       edi = start of payload                                    ;
  146. ;                                                                 ;
  147. ;-----------------------------------------------------------------;
  148. align 4
  149. loop_output:
  150.  
  151.         DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_output\n"
  152.  
  153.         cmp     ecx, [LOOPBACK_DEVICE.mtu]
  154.         ja      .too_large
  155.  
  156.         push    ecx edi
  157.         add     ecx, NET_BUFF.data
  158.         stdcall net_buff_alloc, ecx
  159.         test    eax, eax
  160.         jz      .out_of_ram
  161.  
  162.         pop     edi
  163.         mov     [eax + NET_BUFF.type], edi
  164.         mov     ebx, LOOPBACK_DEVICE
  165.         mov     [eax + NET_BUFF.device], ebx
  166.         pop     ecx
  167.         mov     [eax + NET_BUFF.length], ecx
  168.         lea     edi, [eax + NET_BUFF.data]
  169.  
  170.         DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_output: ptr=%x size=%u\n", eax, ecx
  171.         ret
  172.  
  173.   .too_large:
  174.         DEBUGF  DEBUG_NETWORK_ERROR, "LOOP_output: packet is too large\n"
  175.         xor     eax, eax
  176.         ret
  177.  
  178.   .out_of_ram:
  179.         DEBUGF  DEBUG_NETWORK_ERROR, "LOOP_output: out of memory\n"
  180.         add     esp, 4+4
  181.         xor     eax, eax
  182.         ret
  183.  
  184.  
  185.