Subversion Repositories Kolibri OS

Rev

Rev 1200 | 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: 1206 $
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
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
 
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"
1185 hidnplayr 67
;include "tcp.inc"
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
89
	call	ICMP_init
90
	call	socket_init
91
 
92
	mov	al, 0x0 		; set up 1s timer
93
	out	0x70, al
94
	in	al, 0x71
95
	mov	[last_1sTick], al
96
 
97
	ret
98
 
99
 
100
 
101
;-----------------------------------------------
102
;
103
; stack_handler
104
;
105
;  This function calls all network init procedures
106
;
107
;  IN:  /
108
;  OUT: /
109
;
110
;-----------------------------------------------
111
 
112
align 4
113
stack_handler:
114
 
115
    cmp     [ETH_RUNNING], 0
116
    je	    .exit
117
 
1196 hidnplayr 118
    call    ETH_handler 		; handle all queued ethernet packets
1159 hidnplayr 119
    call    ETH_send_queued
120
 
121
    ; Test for 10ms tick, call tcp timer
122
    mov     eax, [timer_ticks]
123
    cmp     eax, [last_1hsTick]
1185 hidnplayr 124
    je	    .exit
1159 hidnplayr 125
 
126
    mov     [last_1hsTick], eax
1193 hidnplayr 127
;    call    tcp_tx_handler
1159 hidnplayr 128
 
129
  .sec_tick:
130
 
131
    ; Test for 1 second event, call 1s timer functions
132
    mov     al, 0x0   ;second
133
    out     0x70, al
134
    in	    al, 0x71
135
    cmp     al, [last_1sTick]
136
    je	    .exit
137
 
138
    mov     [last_1sTick], al
139
 
1187 hidnplayr 140
    call    ARP_decrease_entry_ttls
1159 hidnplayr 141
    call    IPv4_decrease_fragment_ttls
1193 hidnplayr 142
;    call    tcp_tcb_handler
1159 hidnplayr 143
 
144
  .exit:
145
    ret
146
 
147
 
148
 
149
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150
;; Checksum [by Johnny_B]
151
;;  IN:
152
;;    buf_ptr=POINTER to buffer
153
;;    buf_size=SIZE of buffer
154
;;  OUT:
155
;;    AX=16-bit checksum
156
;;              Saves all used registers
157
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
158
proc checksum_jb stdcall uses ebx esi ecx,\
159
     buf_ptr:DWORD, buf_size:DWORD
160
 
161
    xor     eax, eax
162
    xor     ebx, ebx  ;accumulator
163
    mov     esi, dword[buf_ptr]
164
    mov     ecx, dword[buf_size]
165
    shr     ecx, 1  ; ecx=ecx/2
166
    jnc     @f	    ; if CF==0 then size is even number
167
    mov     bh, byte[esi + ecx*2]
168
  @@:
169
    cld
170
 
171
  .loop:
172
    lodsw		;eax=word[esi],esi=esi+2
173
    xchg    ah,al	;cause must be a net byte-order
174
    add     ebx, eax
175
    loop    .loop
176
 
177
    mov     eax, ebx
178
    shr     eax, 16
179
    add     ax, bx
180
    not     ax
181
 
182
    ret
183
endp
184
 
185
 
186
 
187
;----------------------------------------------------------------
188
;
1171 hidnplayr 189
;  System function to work with network devices (73)
1159 hidnplayr 190
;
191
;----------------------------------------------------------------
192
 
193
align 4
194
sys_network:
195
 
1196 hidnplayr 196
	cmp	ebx, -1
197
	jne	@f
198
 
199
	mov	eax, [ETH_RUNNING]
200
	jmp	.return
201
 
202
   @@:
1159 hidnplayr 203
	cmp	bh, MAX_NET_DEVICES		 ; Check if device number exists
204
	jge	.doesnt_exist
205
 
206
	mov	esi, ebx
207
	and	esi, 0x0000ff00
208
	shr	esi, 6
209
 
210
	cmp	dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
211
	je	.doesnt_exist
212
 
213
	test	bl, bl			; 0 = Get device type (ethernet/token ring/...)
214
	jnz	@f
1196 hidnplayr 215
					 ; todo
1192 hidnplayr 216
	xor	eax, eax
217
	jmp	.return
1159 hidnplayr 218
 
219
 
220
  @@:
221
	dec	bl			; 1 = Get device name
222
	jnz	@f
223
 
224
	mov	esi, [esi + ETH_DRV_LIST]
225
	mov	esi, [esi + ETH_DEVICE.name]
226
	mov	edi, ecx
227
 
228
	mov	ecx, 64 ; max length
229
	repnz	movsb
230
 
1192 hidnplayr 231
	xor	eax, eax
232
	jmp	.return
1159 hidnplayr 233
 
1192 hidnplayr 234
  @@:
1159 hidnplayr 235
 
1192 hidnplayr 236
	dec	bl			; 2 = Reset the device
237
	jnz	@f
238
 
239
	mov	esi, [esi + ETH_DRV_LIST]
240
	call	[esi + ETH_DEVICE.reset]
241
	jmp	.return
242
 
1159 hidnplayr 243
  @@:
1192 hidnplayr 244
 
245
	dec	bl			; 3 = Stop driver for this device
246
	jnz	@f
247
 
248
	mov	esi, [esi + ETH_DRV_LIST]
249
	call	[esi + ETH_DEVICE.unload]
250
	jmp	.return
251
 
252
  @@:
253
 
1159 hidnplayr 254
  .doesnt_exist:
255
	DEBUGF	1,"sys_network: invalid device/function specified!\n"
256
	mov	eax, -1
257
 
1192 hidnplayr 258
  .return:
259
	mov	[esp+28+4], eax
1159 hidnplayr 260
	ret
261
 
262
 
263
;----------------------------------------------------------------
264
;
1171 hidnplayr 265
;  System Function To work with Protocols  (75)
1159 hidnplayr 266
;
267
;----------------------------------------------------------------
268
 
269
align 4
270
sys_protocols:
271
	cmp	bh, MAX_NET_DEVICES		; Check if device number exists
272
	jge	.doesnt_exist
273
 
274
	mov	esi, ebx
275
	and	esi, 0x0000ff00
276
	shr	esi, 6
1171 hidnplayr 277
	cmp	dword [esi + ETH_DRV_LIST], 0	; check if driver is running TODO: check other lists too
1159 hidnplayr 278
	je	.doesnt_exist
279
 
280
	push	.return 			; return address (we will be using jumps instead of calls)
281
 
282
	mov	eax, ebx			; set ax to protocol number
283
	shr	eax, 16 			;
284
 
285
	cmp	ax , IP_PROTO_IP
286
	je	IPv4_API
287
 
288
	cmp	ax , IP_PROTO_ICMP
289
	je	ICMP_API
290
 
291
	cmp	ax , IP_PROTO_UDP
292
	je	UDP_API
293
 
1171 hidnplayr 294
	cmp	ax , IP_PROTO_TCP
1159 hidnplayr 295
;        je      TCP_API
296
 
1171 hidnplayr 297
	cmp	ax , ETHER_ARP
1159 hidnplayr 298
	je	ARP_API
299
 
1185 hidnplayr 300
	cmp	ax , ETHER
1159 hidnplayr 301
	je	ETH_API
302
 
1171 hidnplayr 303
	add	esp, 4				 ; if we reached here, no function was called, so we need to balance stack
1159 hidnplayr 304
 
305
  .doesnt_exist:
1171 hidnplayr 306
	DEBUGF	1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh
1159 hidnplayr 307
	mov	eax, -1
308
 
309
  .return:
1171 hidnplayr 310
	mov	[esp+28+4], eax
1159 hidnplayr 311
	ret