Subversion Repositories Kolibri OS

Rev

Rev 1206 | 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: 1249 $
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
 
1249 hidnplayr 180
	lodsb
181
	shl	ax, 8
182
	add	edx, eax
1159 hidnplayr 183
 
1249 hidnplayr 184
.end:
185
 
186
	ret
187
 
188
 
189
 
190
;-----------------------------------------------------------------
191
;
192
; checksum_2
193
;
194
;  This function calculates the final ip/tcp/udp checksum for you
195
;
196
;  IN:  edx = semi-checksum
197
;  OUT: dx = checksum (in INET byte order)
198
;
199
;-----------------------------------------------------------------
200
 
201
align 4
202
checksum_2:
203
 
204
	mov	ecx, edx
205
	shr	ecx, 16
206
	and	edx, 0xffff
207
	add	edx, ecx
208
	mov	eax, edx
209
	shr	eax, 16
210
	add	edx, eax
211
 
212
	not	dx
213
	jnz	.not_zero
214
	dec	dx
215
  .not_zero:
216
	xchg	dl, dh
217
 
218
	DEBUGF 1,"Checksum: %x\n",dx
219
 
220
	ret
221
 
222
 
223
 
1159 hidnplayr 224
;----------------------------------------------------------------
225
;
1171 hidnplayr 226
;  System function to work with network devices (73)
1159 hidnplayr 227
;
228
;----------------------------------------------------------------
229
 
230
align 4
231
sys_network:
232
 
1196 hidnplayr 233
	cmp	ebx, -1
234
	jne	@f
235
 
236
	mov	eax, [ETH_RUNNING]
237
	jmp	.return
238
 
239
   @@:
1159 hidnplayr 240
	cmp	bh, MAX_NET_DEVICES		 ; Check if device number exists
241
	jge	.doesnt_exist
242
 
243
	mov	esi, ebx
244
	and	esi, 0x0000ff00
245
	shr	esi, 6
246
 
247
	cmp	dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
248
	je	.doesnt_exist
249
 
250
	test	bl, bl			; 0 = Get device type (ethernet/token ring/...)
251
	jnz	@f
1196 hidnplayr 252
					 ; todo
1192 hidnplayr 253
	xor	eax, eax
254
	jmp	.return
1159 hidnplayr 255
 
256
 
257
  @@:
258
	dec	bl			; 1 = Get device name
259
	jnz	@f
260
 
261
	mov	esi, [esi + ETH_DRV_LIST]
262
	mov	esi, [esi + ETH_DEVICE.name]
263
	mov	edi, ecx
264
 
265
	mov	ecx, 64 ; max length
266
	repnz	movsb
267
 
1192 hidnplayr 268
	xor	eax, eax
269
	jmp	.return
1159 hidnplayr 270
 
1192 hidnplayr 271
  @@:
1159 hidnplayr 272
 
1192 hidnplayr 273
	dec	bl			; 2 = Reset the device
274
	jnz	@f
275
 
276
	mov	esi, [esi + ETH_DRV_LIST]
277
	call	[esi + ETH_DEVICE.reset]
278
	jmp	.return
279
 
1159 hidnplayr 280
  @@:
1192 hidnplayr 281
 
282
	dec	bl			; 3 = Stop driver for this device
283
	jnz	@f
284
 
285
	mov	esi, [esi + ETH_DRV_LIST]
286
	call	[esi + ETH_DEVICE.unload]
287
	jmp	.return
288
 
289
  @@:
1249 hidnplayr 290
	dec	bl			; 4 = Get driver pointer
291
	jnz	@f
1192 hidnplayr 292
 
1249 hidnplayr 293
	; ..;
294
 
295
 
296
  @@:
297
;  ...                                   ; 5 Get driver name
298
 
1159 hidnplayr 299
  .doesnt_exist:
300
	DEBUGF	1,"sys_network: invalid device/function specified!\n"
301
	mov	eax, -1
302
 
1192 hidnplayr 303
  .return:
304
	mov	[esp+28+4], eax
1159 hidnplayr 305
	ret
306
 
307
 
308
;----------------------------------------------------------------
309
;
1171 hidnplayr 310
;  System Function To work with Protocols  (75)
1159 hidnplayr 311
;
312
;----------------------------------------------------------------
313
 
314
align 4
315
sys_protocols:
316
	cmp	bh, MAX_NET_DEVICES		; Check if device number exists
317
	jge	.doesnt_exist
318
 
319
	mov	esi, ebx
320
	and	esi, 0x0000ff00
321
	shr	esi, 6
1171 hidnplayr 322
	cmp	dword [esi + ETH_DRV_LIST], 0	; check if driver is running TODO: check other lists too
1159 hidnplayr 323
	je	.doesnt_exist
324
 
325
	push	.return 			; return address (we will be using jumps instead of calls)
326
 
327
	mov	eax, ebx			; set ax to protocol number
328
	shr	eax, 16 			;
329
 
330
	cmp	ax , IP_PROTO_IP
331
	je	IPv4_API
332
 
333
	cmp	ax , IP_PROTO_ICMP
334
	je	ICMP_API
335
 
336
	cmp	ax , IP_PROTO_UDP
337
	je	UDP_API
338
 
1171 hidnplayr 339
	cmp	ax , IP_PROTO_TCP
1159 hidnplayr 340
;        je      TCP_API
341
 
1171 hidnplayr 342
	cmp	ax , ETHER_ARP
1159 hidnplayr 343
	je	ARP_API
344
 
1185 hidnplayr 345
	cmp	ax , ETHER
1159 hidnplayr 346
	je	ETH_API
347
 
1171 hidnplayr 348
	add	esp, 4				 ; if we reached here, no function was called, so we need to balance stack
1159 hidnplayr 349
 
350
  .doesnt_exist:
1171 hidnplayr 351
	DEBUGF	1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh
1159 hidnplayr 352
	mov	eax, -1
353
 
354
  .return:
1171 hidnplayr 355
	mov	[esp+28+4], eax
1159 hidnplayr 356
	ret