Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2013. 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: 2891 $
  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 .dummy_fn
  29.         .reset          dd .dummy_fn
  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.         .dummy_fn:
  43.         ret
  44.  
  45. endg
  46.  
  47.  
  48. macro   LOOP_init {
  49. local   .fail
  50.  
  51.         mov     ebx, LOOPBACK_DEVICE
  52.         call    NET_add_device
  53.  
  54.         cmp     eax, -1
  55.         je      .fail
  56.  
  57.         mov     [IP_LIST], 127 + 1 shl 24
  58.         mov     [SUBNET_LIST], 255
  59.         mov     [BROADCAST_LIST], 0xffffff00 + 127
  60.  
  61.   .fail:
  62. }
  63.  
  64. ;-----------------------------------------------------------------
  65. ;
  66. ; LOOP_input
  67. ;
  68. ;  IN:  [esp+4] = Pointer to buffer
  69. ;       [esp+8] = size of buffer
  70. ;
  71. ;  OUT: /
  72. ;
  73. ;-----------------------------------------------------------------
  74. align 4
  75. LOOP_input:
  76.         pop     ebx
  77.         pop     eax
  78.         pop     ecx
  79.  
  80.         push    ebx
  81.         push    ecx
  82.         push    eax
  83.  
  84.         inc     [LOOPBACK_DEVICE.packets_rx]
  85.         add     dword[LOOPBACK_DEVICE.bytes_rx], ecx
  86.         adc     dword[LOOPBACK_DEVICE.bytes_rx + 4], 0
  87.  
  88.         DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_input: size=%u\n", ecx
  89.         lea     edx, [eax + 4]
  90.         mov     eax, dword[eax]
  91.         mov     ebx, LOOPBACK_DEVICE
  92.  
  93.         cmp     eax, AF_INET4
  94.         je      IPv4_input
  95.  
  96.         DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_input: Unknown packet type=%x\n", ax
  97.  
  98.   .dump:
  99.         DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_input: dumping\n"
  100.         call    NET_packet_free
  101.         add     esp, 4
  102.         ret
  103.  
  104. ;-----------------------------------------------------------------
  105. ;
  106. ; LOOP_output
  107. ;
  108. ; IN:
  109. ;       ecx = packet size
  110. ;       edi = address family
  111. ;
  112. ; OUT:  edi = 0 on error, pointer to buffer otherwise
  113. ;       eax = buffer start
  114. ;       ebx = to device structure
  115. ;       ecx = unchanged (packet size of embedded data)
  116. ;       edx = size of complete buffer
  117. ;
  118. ;-----------------------------------------------------------------
  119. align 4
  120. LOOP_output:
  121.  
  122.         DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_output\n"
  123.  
  124.         push    ecx
  125.         push    edi
  126.  
  127.         add     ecx, 4
  128.         cmp     ecx, [LOOPBACK_DEVICE.mtu]
  129.         ja      .out_of_ram
  130.         stdcall kernel_alloc, ecx
  131.         test    eax, eax
  132.         jz      .out_of_ram
  133.         mov     edi, eax
  134.         pop     eax
  135.         stosd
  136.  
  137.         lea     eax, [edi - 4]  ; Set eax to buffer start
  138.         pop     ecx
  139.         lea     edx, [ecx + 4]  ; Set edx to complete buffer size
  140.         mov     ebx, LOOPBACK_DEVICE
  141.  
  142.         inc     [LOOPBACK_DEVICE.packets_tx]
  143.         add     dword[LOOPBACK_DEVICE.bytes_tx], ecx
  144.         adc     dword[LOOPBACK_DEVICE.bytes_tx + 4], 0
  145.  
  146.         DEBUGF  DEBUG_NETWORK_VERBOSE, "LOOP_output: ptr=%x size=%u\n", eax, edx
  147.         ret
  148.  
  149.   .out_of_ram:
  150.         DEBUGF  DEBUG_NETWORK_ERROR, "LOOP_output: out of memory\n"
  151.         add     esp, 4+4
  152.         xor     edi, edi
  153.         ret
  154.  
  155.  
  156.