Subversion Repositories Kolibri OS

Rev

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

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