Subversion Repositories Kolibri OS

Rev

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

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