Subversion Repositories Kolibri OS

Rev

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

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