Subversion Repositories Kolibri OS

Rev

Rev 1249 | 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
 
1206 hidnplayr 20
$Revision: 1251 $
1159 hidnplayr 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
1249 hidnplayr 36
AF_UNIX 	equ 1
1159 hidnplayr 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
 
1200 hidnplayr 53
; Socket types
54
SOCK_STREAM	= 1
55
SOCK_DGRAM	= 2
56
SOCK_RAW	= 3
57
 
1185 hidnplayr 58
; TCP opening modes
59
SOCKET_PASSIVE	equ 0
60
SOCKET_ACTIVE	equ 1
1159 hidnplayr 61
 
62
include "queue.inc"
1187 hidnplayr 63
include "ARP.inc"
64
include "IPv4.inc"
1159 hidnplayr 65
include "ethernet.inc"
66
include "socket.inc"
1249 hidnplayr 67
include "tcp.inc"
1185 hidnplayr 68
include "udp.inc"
69
include "icmp.inc"
1159 hidnplayr 70
 
71
;-----------------------------------------------
72
;
73
; stack_init
74
;
75
;  This function calls all network init procedures
76
;
77
;  IN:  /
78
;  OUT: /
79
;
80
;-----------------------------------------------
81
 
82
align 4
83
stack_init:
84
 
85
	call	ETH_init
86
	call	IPv4_init
87
	call	ARP_init
88
	call	UDP_init
1249 hidnplayr 89
	call	TCP_init
1159 hidnplayr 90
	call	ICMP_init
91
	call	socket_init
92
 
93
	mov	al, 0x0 		; set up 1s timer
94
	out	0x70, al
95
	in	al, 0x71
96
	mov	[last_1sTick], al
97
 
98
	ret
99
 
100
 
101
 
102
;-----------------------------------------------
103
;
104
; stack_handler
105
;
106
;  This function calls all network init procedures
107
;
108
;  IN:  /
109
;  OUT: /
110
;
111
;-----------------------------------------------
112
 
113
align 4
114
stack_handler:
115
 
116
    cmp     [ETH_RUNNING], 0
117
    je	    .exit
118
 
1249 hidnplayr 119
    ; Test for 10ms tick
1159 hidnplayr 120
    mov     eax, [timer_ticks]
121
    cmp     eax, [last_1hsTick]
1185 hidnplayr 122
    je	    .exit
1159 hidnplayr 123
 
124
    mov     [last_1hsTick], eax
125
 
1249 hidnplayr 126
    call    ETH_handler 		; handle all queued ethernet packets
127
    call    ETH_send_queued
128
    call    TCP_send_queued
129
 
1159 hidnplayr 130
  .sec_tick:
131
 
1249 hidnplayr 132
    ; Test for 1 second event
1159 hidnplayr 133
    mov     al, 0x0   ;second
134
    out     0x70, al
135
    in	    al, 0x71
136
    cmp     al, [last_1sTick]
137
    je	    .exit
138
 
139
    mov     [last_1sTick], al
140
 
1187 hidnplayr 141
    call    ARP_decrease_entry_ttls
1159 hidnplayr 142
    call    IPv4_decrease_fragment_ttls
1249 hidnplayr 143
    call    TCP_decrease_socket_ttls
1159 hidnplayr 144
 
145
  .exit:
146
    ret
147
 
148
 
149
 
150
 
151
 
1249 hidnplayr 152
;-----------------------------------------------------------------
153
;
154
; checksum_1
155
;
156
;  This is the first of two functions needed to calculate the TCP checksum.
157
;
158
;  IN:  edx = start offeset for semi-checksum
159
;       esi = pointer to data
160
;       ecx = data size
161
;  OUT: edx = semi-checksum
162
;
163
;-----------------------------------------------------------------
1159 hidnplayr 164
 
1249 hidnplayr 165
align 4
166
checksum_1:
1159 hidnplayr 167
 
1249 hidnplayr 168
	xor	eax, eax
169
	shr	ecx, 1
170
	pushf
171
.loop:
172
	lodsw
173
	xchg	al, ah
174
	add	edx, eax
175
	loop	.loop
1159 hidnplayr 176
 
1249 hidnplayr 177
	popf
178
	jnc	.end
1159 hidnplayr 179
 
1251 clevermous 180
	add	dh, [esi]
181
	adc	edx, 0
1159 hidnplayr 182
 
1249 hidnplayr 183
.end:
184
 
185
	ret
186
 
187
 
188
 
189
;-----------------------------------------------------------------
190
;
191
; checksum_2
192
;
193
;  This function calculates the final ip/tcp/udp checksum for you
194
;
195
;  IN:  edx = semi-checksum
196
;  OUT: dx = checksum (in INET byte order)
197
;
198
;-----------------------------------------------------------------
199
 
200
align 4
201
checksum_2:
202
 
203
	mov	ecx, edx
204
	shr	ecx, 16
205
	and	edx, 0xffff
206
	add	edx, ecx
207
	mov	eax, edx
208
	shr	eax, 16
209
	add	edx, eax
210
 
211
	not	dx
212
	jnz	.not_zero
213
	dec	dx
214
  .not_zero:
215
	xchg	dl, dh
216
 
217
	DEBUGF 1,"Checksum: %x\n",dx
218
 
219
	ret
220
 
221
 
222
 
1159 hidnplayr 223
;----------------------------------------------------------------
224
;
1171 hidnplayr 225
;  System function to work with network devices (73)
1159 hidnplayr 226
;
227
;----------------------------------------------------------------
228
 
229
align 4
230
sys_network:
231
 
1196 hidnplayr 232
	cmp	ebx, -1
233
	jne	@f
234
 
235
	mov	eax, [ETH_RUNNING]
236
	jmp	.return
237
 
238
   @@:
1159 hidnplayr 239
	cmp	bh, MAX_NET_DEVICES		 ; Check if device number exists
240
	jge	.doesnt_exist
241
 
242
	mov	esi, ebx
243
	and	esi, 0x0000ff00
244
	shr	esi, 6
245
 
246
	cmp	dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
247
	je	.doesnt_exist
248
 
249
	test	bl, bl			; 0 = Get device type (ethernet/token ring/...)
250
	jnz	@f
1196 hidnplayr 251
					 ; todo
1192 hidnplayr 252
	xor	eax, eax
253
	jmp	.return
1159 hidnplayr 254
 
255
 
256
  @@:
257
	dec	bl			; 1 = Get device name
258
	jnz	@f
259
 
260
	mov	esi, [esi + ETH_DRV_LIST]
261
	mov	esi, [esi + ETH_DEVICE.name]
262
	mov	edi, ecx
263
 
264
	mov	ecx, 64 ; max length
265
	repnz	movsb
266
 
1192 hidnplayr 267
	xor	eax, eax
268
	jmp	.return
1159 hidnplayr 269
 
1192 hidnplayr 270
  @@:
1159 hidnplayr 271
 
1192 hidnplayr 272
	dec	bl			; 2 = Reset the device
273
	jnz	@f
274
 
275
	mov	esi, [esi + ETH_DRV_LIST]
276
	call	[esi + ETH_DEVICE.reset]
277
	jmp	.return
278
 
1159 hidnplayr 279
  @@:
1192 hidnplayr 280
 
281
	dec	bl			; 3 = Stop driver for this device
282
	jnz	@f
283
 
284
	mov	esi, [esi + ETH_DRV_LIST]
285
	call	[esi + ETH_DEVICE.unload]
286
	jmp	.return
287
 
288
  @@:
1249 hidnplayr 289
	dec	bl			; 4 = Get driver pointer
290
	jnz	@f
1192 hidnplayr 291
 
1249 hidnplayr 292
	; ..;
293
 
294
 
295
  @@:
296
;  ...                                   ; 5 Get driver name
297
 
1159 hidnplayr 298
  .doesnt_exist:
299
	DEBUGF	1,"sys_network: invalid device/function specified!\n"
300
	mov	eax, -1
301
 
1192 hidnplayr 302
  .return:
303
	mov	[esp+28+4], eax
1159 hidnplayr 304
	ret
305
 
306
 
307
;----------------------------------------------------------------
308
;
1171 hidnplayr 309
;  System Function To work with Protocols  (75)
1159 hidnplayr 310
;
311
;----------------------------------------------------------------
312
 
313
align 4
314
sys_protocols:
315
	cmp	bh, MAX_NET_DEVICES		; Check if device number exists
316
	jge	.doesnt_exist
317
 
318
	mov	esi, ebx
319
	and	esi, 0x0000ff00
320
	shr	esi, 6
1171 hidnplayr 321
	cmp	dword [esi + ETH_DRV_LIST], 0	; check if driver is running TODO: check other lists too
1159 hidnplayr 322
	je	.doesnt_exist
323
 
324
	push	.return 			; return address (we will be using jumps instead of calls)
325
 
326
	mov	eax, ebx			; set ax to protocol number
327
	shr	eax, 16 			;
328
 
329
	cmp	ax , IP_PROTO_IP
330
	je	IPv4_API
331
 
332
	cmp	ax , IP_PROTO_ICMP
333
	je	ICMP_API
334
 
335
	cmp	ax , IP_PROTO_UDP
336
	je	UDP_API
337
 
1171 hidnplayr 338
	cmp	ax , IP_PROTO_TCP
1159 hidnplayr 339
;        je      TCP_API
340
 
1171 hidnplayr 341
	cmp	ax , ETHER_ARP
1159 hidnplayr 342
	je	ARP_API
343
 
1185 hidnplayr 344
	cmp	ax , ETHER
1159 hidnplayr 345
	je	ETH_API
346
 
1171 hidnplayr 347
	add	esp, 4				 ; if we reached here, no function was called, so we need to balance stack
1159 hidnplayr 348
 
349
  .doesnt_exist:
1171 hidnplayr 350
	DEBUGF	1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh
1159 hidnplayr 351
	mov	eax, -1
352
 
353
  .return:
1171 hidnplayr 354
	mov	[esp+28+4], eax
1159 hidnplayr 355
	ret