Subversion Repositories Kolibri OS

Rev

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

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