Subversion Repositories Kolibri OS

Rev

Rev 7678 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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