Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1159 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
3
;; Copyright (C) KolibriOS team 2004-2008. All rights reserved. ;;
4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;  STACK.INC                                                   ;;
7
;;                                                              ;;
8
;;  TCP/IP stack for Menuet OS                                  ;;
9
;;                                                              ;;
10
;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net            ;;
11
;;                                                              ;;
12
;;  See file COPYING for details                                ;;
13
;;                                                              ;;
14
;; Version 0.7                                                  ;;
15
;;      Added a timer per socket to allow delays when rx window ;;
16
;;      gets below 1KB                                          ;;
17
;;                                                              ;;
18
;;10.01.2007 Bugfix for checksum function from Paolo Franchetti ;;
19
;;                                                              ;;
20
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21
 
22
$Revision: 983 $
23
 
24
 
25
;*******************************************************************
26
;   Interface
27
;      The interfaces defined in ETHERNET.INC plus:
28
;      stack_init
29
;      stack_handler
30
;      app_stack_handler
31
;      app_socket_handler
32
;      checksum
33
;
34
;*******************************************************************
35
 
36
uglobal
37
	last_1sTick	db ?
38
	last_1hsTick	dd ?
39
endg
40
 
41
MAX_NET_DEVICES equ 16
42
 
43
; TCP opening modes
44
SOCKET_PASSIVE	equ 0
45
SOCKET_ACTIVE	equ 1
46
 
47
;AF_UNSPEC       equ 0
48
;AF_UNIX         equ 1
49
AF_INET4	equ 2
50
;AF_AX25         equ 3
51
;AF_IPX          equ 4
52
;AF_APPLETALK    equ 5
53
;AF_NETROM       equ 6
54
;AF_BRIDGE       equ 7
55
;AF_AAL5         equ 8
56
;AF_X25          equ 9
57
;AF_INET6        equ 10
58
;AF_MAX          equ 12
59
 
60
IP_PROTO_IP	equ 0
61
IP_PROTO_ICMP	equ 1
62
IP_PROTO_TCP	equ 6
63
IP_PROTO_UDP	equ 17
64
 
65
MIN_EPHEMERAL_PORT equ 49152
66
MAX_EPHEMERAL_PORT equ 61000
67
 
68
include "queue.inc"
69
include "ARP.inc"
70
include "IPv4.inc"
71
include "ethernet.inc"
72
include "socket.inc"
73
;include "TCP.inc"
74
include "UDP.inc"
75
include "ICMP.inc"
76
 
77
;-----------------------------------------------
78
;
79
; stack_init
80
;
81
;  This function calls all network init procedures
82
;
83
;  IN:  /
84
;  OUT: /
85
;
86
;-----------------------------------------------
87
 
88
align 4
89
stack_init:
90
 
91
	call	ETH_init
92
	call	IPv4_init
93
	call	ARP_init
94
	call	UDP_init
95
	call	ICMP_init
96
	call	socket_init
97
 
98
	mov	al, 0x0 		; set up 1s timer
99
	out	0x70, al
100
	in	al, 0x71
101
	mov	[last_1sTick], al
102
 
103
	ret
104
 
105
 
106
 
107
;-----------------------------------------------
108
;
109
; stack_handler
110
;
111
;  This function calls all network init procedures
112
;
113
;  IN:  /
114
;  OUT: /
115
;
116
;-----------------------------------------------
117
 
118
align 4
119
stack_handler:
120
 
121
    cmp     [ETH_RUNNING], 0
122
    je	    .exit
123
 
124
    call    ETH_Handler 		; handle all queued ethernet packets
125
    call    ETH_send_queued
126
 
127
    ; Test for 10ms tick, call tcp timer
128
    mov     eax, [timer_ticks]
129
    cmp     eax, [last_1hsTick]
130
    je	    .sec_tick
131
 
132
    mov     [last_1hsTick], eax
133
;    call    tcp_tx_handler
134
 
135
  .sec_tick:
136
 
137
    ; Test for 1 second event, call 1s timer functions
138
    mov     al, 0x0   ;second
139
    out     0x70, al
140
    in	    al, 0x71
141
    cmp     al, [last_1sTick]
142
    je	    .exit
143
 
144
    mov     [last_1sTick], al
145
 
146
    stdcall arp_table_manager, ARP_TABLE_TIMER, 0, 0
147
    call    IPv4_decrease_fragment_ttls
148
;    call    tcp_tcb_handler
149
 
150
  .exit:
151
    ret
152
 
153
 
154
 
155
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
156
;; Checksum [by Johnny_B]
157
;;  IN:
158
;;    buf_ptr=POINTER to buffer
159
;;    buf_size=SIZE of buffer
160
;;  OUT:
161
;;    AX=16-bit checksum
162
;;              Saves all used registers
163
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164
proc checksum_jb stdcall uses ebx esi ecx,\
165
     buf_ptr:DWORD, buf_size:DWORD
166
 
167
    xor     eax, eax
168
    xor     ebx, ebx  ;accumulator
169
    mov     esi, dword[buf_ptr]
170
    mov     ecx, dword[buf_size]
171
    shr     ecx, 1  ; ecx=ecx/2
172
    jnc     @f	    ; if CF==0 then size is even number
173
    mov     bh, byte[esi + ecx*2]
174
  @@:
175
    cld
176
 
177
  .loop:
178
    lodsw		;eax=word[esi],esi=esi+2
179
    xchg    ah,al	;cause must be a net byte-order
180
    add     ebx, eax
181
    loop    .loop
182
 
183
    mov     eax, ebx
184
    shr     eax, 16
185
    add     ax, bx
186
    not     ax
187
 
188
    ret
189
endp
190
 
191
 
192
 
193
;----------------------------------------------------------------
194
;
195
;
196
;
197
;----------------------------------------------------------------
198
 
199
align 4
200
sys_network:
201
 
202
	cmp	bh, MAX_NET_DEVICES		 ; Check if device number exists
203
	jge	.doesnt_exist
204
 
205
	mov	esi, ebx
206
	and	esi, 0x0000ff00
207
	shr	esi, 6
208
 
209
	cmp	dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
210
	je	.doesnt_exist
211
 
212
	test	bl, bl			; 0 = Get device type (ethernet/token ring/...)
213
	jnz	@f
214
 
215
	;TODO: write code here
216
 
217
 
218
  @@:
219
	dec	bl			; 1 = Get device name
220
	jnz	@f
221
 
222
	mov	esi, [esi + ETH_DRV_LIST]
223
	mov	esi, [esi + ETH_DEVICE.name]
224
	mov	edi, ecx
225
 
226
	mov	ecx, 64 ; max length
227
	repnz	movsb
228
 
229
	ret
230
 
231
	; TODO: create function wich outputs number of active network devices
232
 
233
  @@:
234
  .doesnt_exist:
235
	DEBUGF	1,"sys_network: invalid device/function specified!\n"
236
	mov	eax, -1
237
 
238
	ret
239
 
240
 
241
 
242
 
243
;----------------------------------------------------------------
244
;
245
;
246
;
247
;----------------------------------------------------------------
248
 
249
align 4
250
sys_protocols:
251
 
252
	cmp	bh, MAX_NET_DEVICES		; Check if device number exists
253
	jge	.doesnt_exist
254
 
255
	mov	esi, ebx
256
	and	esi, 0x0000ff00
257
	shr	esi, 6
258
 
259
	cmp	dword [esi + ETH_DRV_LIST], 0	; check if driver is running TODO: check otehr lists too
260
	je	.doesnt_exist
261
 
262
	push	.return 			; return address (we will be using jumps instead of calls)
263
 
264
	mov	eax, ebx			; set ax to protocol number
265
	shr	eax, 16 			;
266
 
267
	cmp	ax , IP_PROTO_IP
268
	je	IPv4_API
269
 
270
	cmp	ax , IP_PROTO_ICMP
271
	je	ICMP_API
272
 
273
	cmp	ax , IP_PROTO_UDP
274
	je	UDP_API
275
 
276
;        cmp     ax , IP_PROTO_TCP
277
;        je      TCP_API
278
 
279
	cmp	ax, ETHER_ARP
280
	je	ARP_API
281
 
282
	cmp	ax, 1337
283
	je	ETH_API
284
 
285
	add	esp,4				; if we reached here, no function was called, so we need to balance stack
286
 
287
  .doesnt_exist:
288
	DEBUGF	1,"sys_protocols: invalid device specified!\n"
289
	mov	eax, -1
290
 
291
  .return:
292
	mov	[esp+32], eax
293
	ret