Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
431 serge 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
3
;; Copyright (C) KolibriOS team 2004-2007. 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
;;  Version 0.7  4th July 2004                                  ;;
11
;;                                                              ;;
12
;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net            ;;
13
;;                                                              ;;
14
;;  See file COPYING for details                                ;;
15
;;                                                              ;;
16
;; Version 0.7                                                  ;;
17
;;      Added a timer per socket to allow delays when rx window ;;
18
;;      gets below 1KB                                          ;;
19
;;                                                              ;;
20
;;10.01.2007 Bugfix for checksum function from Paolo Franchetti ;;
21
;;                                                              ;;
22
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1 ha 23
 
593 mikedld 24
$Revision: 913 $
1 ha 25
 
261 hidnplayr 26
 
1 ha 27
;*******************************************************************
28
;   Interface
29
;      The interfaces defined in ETHERNET.INC plus:
30
;      stack_init
31
;      stack_handler
32
;      app_stack_handler
33
;      app_socket_handler
34
;      checksum
35
;
36
;*******************************************************************
37
 
261 hidnplayr 38
uglobal
39
StackCounters:
907 mikedld 40
  dumped_rx_count     dd  0
261 hidnplayr 41
  arp_tx_count:       dd  0
42
  arp_rx_count:       dd  0
302 hidnplayr 43
  ip_rx_count:	      dd  0
44
  ip_tx_count:	      dd  0
261 hidnplayr 45
endg
1 ha 46
 
261 hidnplayr 47
; socket buffers
302 hidnplayr 48
SOCKETBUFFSIZE	   equ	      4096  ; state + config + buffer.
907 mikedld 49
SOCKETHEADERSIZE   equ	      76+8+8  ; thus 4096 - SOCKETHEADERSIZE bytes data
1 ha 50
 
907 mikedld 51
;NUM_SOCKETS        equ        16    ; Number of open sockets supported. Was 20
1 ha 52
 
261 hidnplayr 53
; IPBUFF status values
302 hidnplayr 54
BUFF_EMPTY	   equ	   0
55
BUFF_RX_FULL	   equ	   1
56
BUFF_ALLOCATED	   equ	   2
57
BUFF_TX_FULL	   equ	   3
1 ha 58
 
302 hidnplayr 59
NUM_IPBUFFERS	   equ	   20	 ; buffers allocated for TX/RX
1 ha 60
 
302 hidnplayr 61
NUMQUEUES	   equ	      4
907 mikedld 62
 
302 hidnplayr 63
EMPTY_QUEUE	   equ	      0
64
IPIN_QUEUE	   equ	      1
65
IPOUT_QUEUE	   equ	      2
66
NET1OUT_QUEUE	   equ	      3
1 ha 67
 
302 hidnplayr 68
NO_BUFFER	   equ	      0xFFFF
69
IPBUFFSIZE	   equ	      1500		  ; MTU of an ethernet packet
70
NUMQUEUEENTRIES    equ	      NUM_IPBUFFERS
71
NUMRESENDENTRIES    equ 	18		; Buffers for TCP resend packets
1 ha 72
 
73
; These are the 0x40 function codes for application access to the stack
74
STACK_DRIVER_STATUS  equ   52
75
SOCKET_INTERFACE     equ   53
76
 
77
 
78
; 128KB allocated for the stack and network driver buffers and other
79
; data requirements
412 serge 80
;stack_data_start     equ   0x700000
81
;eth_data_start       equ   0x700000
82
;stack_data           equ   0x704000
83
;stack_data_end       equ   0x71ffff
1 ha 84
 
85
; 32 bit word
302 hidnplayr 86
stack_config	     equ   stack_data
261 hidnplayr 87
 
1 ha 88
; 32 bit word - IP Address in network format
302 hidnplayr 89
stack_ip	     equ   stack_data + 4
261 hidnplayr 90
 
1 ha 91
; 1 byte. 0 == inactive, 1 = active
92
ethernet_active      equ   stack_data + 9
261 hidnplayr 93
 
94
 
95
; TODO :: empty memory area
96
 
1 ha 97
; Address of selected socket
907 mikedld 98
;sktAddr              equ   stack_data + 32
1 ha 99
; Parameter to checksum routine - data ptr
302 hidnplayr 100
checkAdd1	     equ   stack_data + 36
1 ha 101
; Parameter to checksum routine - 2nd data ptr
302 hidnplayr 102
checkAdd2	     equ   stack_data + 40
1 ha 103
; Parameter to checksum routine - data size
302 hidnplayr 104
checkSize1	     equ   stack_data + 44
1 ha 105
; Parameter to checksum routine - 2nd data size
302 hidnplayr 106
checkSize2	     equ   stack_data + 46
1 ha 107
; result of checksum routine
302 hidnplayr 108
checkResult	     equ   stack_data + 48
1 ha 109
 
110
; holds the TCP/UDP pseudo header. SA|DA|0|prot|UDP len|
302 hidnplayr 111
pseudoHeader	     equ   stack_data + 50
1 ha 112
 
113
; receive and transmit IP buffer allocation
907 mikedld 114
;sockets              equ   stack_data + 62
115
Next_free2 equ stack_data + 62;Next_free2           equ   sockets + (SOCKETBUFFSIZE * NUM_SOCKETS)
1 ha 116
; 1560 byte buffer for rx / tx ethernet packets
302 hidnplayr 117
Ether_buffer	     equ   Next_free2
118
Next_free3	     equ   Ether_buffer + 1518
261 hidnplayr 119
last_1sTick	     equ   Next_free3
302 hidnplayr 120
IPbuffs 	     equ   Next_free3 + 1
121
queues		     equ   IPbuffs + ( NUM_IPBUFFERS * IPBUFFSIZE )
122
queueList	     equ   queues + (2 * NUMQUEUES)
123
last_1hsTick	     equ   queueList + ( 2 * NUMQUEUEENTRIES )
1 ha 124
 
125
;resendQ              equ   queueList + ( 2 * NUMQUEUEENTRIES )
126
;resendBuffer         equ    resendQ + ( 4 * NUMRESENDENTRIES ) ; for TCP
127
;                    equ    resendBuffer + ( IPBUFFSIZE * NUMRESENDENTRIES )
128
 
129
 
130
 
412 serge 131
;resendQ             equ     0x770000
907 mikedld 132
;resendBuffer        equ     resendQ + ( 4 * NUMRESENDENTRIES ) ; for TCP        ; XTODO: validate size
133
resendBuffer	    equ     resendQ + ( 8 * NUMRESENDENTRIES ) ; for TCP
1 ha 134
 
135
 
907 mikedld 136
uglobal
137
net_sockets rd 2
138
endg
139
 
261 hidnplayr 140
; simple macro for memory set operation
141
macro _memset_dw adr,value,amount
142
{
302 hidnplayr 143
	mov	edi, adr
144
	mov	ecx, amount
261 hidnplayr 145
	if value = 0
146
		xor	eax, eax
147
	else
148
		mov	eax, value
149
	end if
150
	cld
302 hidnplayr 151
	rep	stosd
261 hidnplayr 152
}
153
 
154
 
155
; Below, the main network layer source code is included
156
;
157
include "queue.inc"
158
include "eth_drv/ethernet.inc"
159
include "ip.inc"
160
include "socket.inc"
161
 
1 ha 162
;***************************************************************************
163
;   Function
164
;      stack_init
165
;
166
;   Description
167
;      Clear all allocated memory to zero. This ensures that
168
;       on startup, the stack is inactive, and consumes no resources
169
;       This is a kernel function, called prior to the OS main loop
170
;       in set_variables
171
;
172
;***************************************************************************
261 hidnplayr 173
 
1 ha 174
stack_init:
261 hidnplayr 175
	; Init two address spaces with default values
176
	_memset_dw	stack_data_start, 0, 0x20000/4
913 mikedld 177
	_memset_dw	resendQ, 0, NUMRESENDENTRIES * 2
1 ha 178
 
909 mikedld 179
	mov	[net_sockets], 0
180
	mov	[net_sockets + 4], 0
181
 
261 hidnplayr 182
	; Queries initialization
183
	call	queueInit
1 ha 184
 
261 hidnplayr 185
	; The following block sets up the 1s timer
186
	mov	al, 0x0
187
	out	0x70, al
188
	in	al, 0x71
189
	mov	[last_1sTick], al
190
ret
1 ha 191
 
192
 
193
 
194
;***************************************************************************
195
;   Function
196
;      stack_handler
197
;
198
;   Description
199
;       The kernel loop routine for the stack
200
;       This is a kernel function, called in the main loop
201
;
202
;***************************************************************************
203
stack_handler:
204
 
205
    call    ethernet_driver
206
    call    ip_rx
207
 
208
 
209
    ; Test for 10ms tick, call tcp timer
210
    mov     eax, [timer_ticks] ;[0xfdf0]
211
    cmp     eax, [last_1hsTick]
302 hidnplayr 212
    je	    sh_001
1 ha 213
 
214
    mov     [last_1hsTick], eax
215
    call    tcp_tx_handler
216
 
217
sh_001:
218
 
219
    ; Test for 1 second event, call 1s timer functions
261 hidnplayr 220
    mov     al, 0x0   ;second
221
    out     0x70, al
302 hidnplayr 222
    in	    al, 0x71
1 ha 223
    cmp     al, [last_1sTick]
302 hidnplayr 224
    je	    sh_exit
1 ha 225
 
226
    mov     [last_1sTick], al
227
 
379 serge 228
    stdcall arp_table_manager, ARP_TABLE_TIMER, 0, 0
1 ha 229
    call    tcp_tcb_handler
230
 
231
sh_exit:
232
    ret
233
 
261 hidnplayr 234
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
235
;; Checksum [by Johnny_B]
236
;;  IN:
237
;;    buf_ptr=POINTER to buffer
238
;;    buf_size=SIZE of buffer
239
;;  OUT:
240
;;    AX=16-bit checksum
241
;;              Saves all used registers
242
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243
proc checksum_jb stdcall uses ebx esi ecx,\
244
     buf_ptr:DWORD, buf_size:DWORD
1 ha 245
 
261 hidnplayr 246
    xor     eax, eax
247
    xor     ebx, ebx  ;accumulator
248
    mov     esi, dword[buf_ptr]
249
    mov     ecx, dword[buf_size]
250
    shr     ecx, 1  ; ecx=ecx/2
302 hidnplayr 251
    jnc     @f	    ; if CF==0 then size is even number
261 hidnplayr 252
    mov     bh, byte[esi + ecx*2]
253
  @@:
254
    cld
1 ha 255
 
261 hidnplayr 256
  .loop:
257
    lodsw   ;eax=word[esi],esi=esi+2
258
    xchg    ah,al    ;cause must be a net byte-order
259
    add     ebx, eax
260
    loop    .loop
1 ha 261
 
261 hidnplayr 262
    mov     eax, ebx
263
    shr     eax, 16
264
    add     ax, bx
265
    not     ax
1 ha 266
 
267
    ret
261 hidnplayr 268
endp
1 ha 269
 
270
;***************************************************************************
271
;   Function
272
;      checksum
273
;
274
;   Description
275
;       checkAdd1,checkAdd2, checkSize1, checkSize2, checkResult
276
;       Dont break anything; Most registers are used by the caller
277
;       This code is derived from the 'C' source, cksum.c, in the book
278
;       Internetworking with TCP/IP Volume II by D.E. Comer
279
;
280
;***************************************************************************
261 hidnplayr 281
 
282
 
1 ha 283
checksum:
284
    pusha
323 hidnplayr 285
    mov     eax, [checkAdd1]
302 hidnplayr 286
    xor     edx, edx		      ; edx is the accumulative checksum
1 ha 287
    xor     ebx, ebx
288
    mov     cx, [checkSize1]
289
    shr     cx, 1
302 hidnplayr 290
    jz	    cs1_1
1 ha 291
 
292
cs1:
293
    mov     bh, [eax]
294
    mov     bl, [eax + 1]
295
 
296
    add     eax, 2
297
    add     edx, ebx
298
 
299
    loopw   cs1
300
 
301
cs1_1:
302
    and     word [checkSize1], 0x01
302 hidnplayr 303
    jz	    cs_test2
1 ha 304
 
305
    mov     bh, [eax]
306
    xor     bl, bl
307
 
308
    add     edx, ebx
309
 
310
cs_test2:
311
    mov     cx, [checkSize2]
312
    cmp     cx, 0
302 hidnplayr 313
    jz	    cs_exit			; Finished if no 2nd buffer
1 ha 314
 
323 hidnplayr 315
    mov     eax, [checkAdd2]
316
 
1 ha 317
    shr     cx, 1
302 hidnplayr 318
    jz	    cs2_1
1 ha 319
 
320
cs2:
321
    mov     bh, [eax]
322
    mov     bl, [eax + 1]
323
 
324
    add     eax, 2
325
    add     edx, ebx
326
 
327
    loopw   cs2
328
 
329
cs2_1:
330
    and     word [checkSize2], 0x01
302 hidnplayr 331
    jz	    cs_exit
1 ha 332
 
333
    mov     bh, [eax]
334
    xor     bl, bl
335
 
336
    add     edx, ebx
337
 
338
cs_exit:
339
    mov     ebx, edx
340
 
341
    shr     ebx, 16
342
    and     edx, 0xffff
343
    add     edx, ebx
344
    mov     eax, edx
345
    shr     eax, 16
346
    add     edx, eax
347
    not     dx
348
 
349
    mov     [checkResult], dx
350
    popa
351
    ret
352
 
353
 
354
 
355
 
356
;***************************************************************************
357
;   Function
358
;      app_stack_handler
359
;
360
;   Description
261 hidnplayr 361
;       This is an application service, called by int 0x40, function 52
1 ha 362
;       It provides application access to the network interface layer
363
;
364
;***************************************************************************
365
app_stack_handler:
366
    cmp     eax, 0
367
    jnz     not0
261 hidnplayr 368
    ; Read the configuration word
1 ha 369
    mov     eax, [stack_config]
370
    ret
371
 
372
not0:
373
    cmp     eax, 1
374
    jnz     not1
375
    ; read the IP address
376
 
377
    mov     eax, [stack_ip]
378
    ret
379
 
380
not1:
381
    cmp     eax, 2
382
    jnz     not2
383
 
384
    ; write the configuration word
385
    mov     [stack_config], ebx
386
 
387
    ; 
388
    ; If ethernet now enabled, probe for the card, reset it and empty
389
    ; the packet buffer
390
    ; If all successfull, enable the card.
391
    ; If ethernet now disabled, set it as disabled. Should really
392
    ; empty the tcpip data area too.
393
 
394
    ; ethernet interface is '3' in ls 7 bits
395
    and     bl, 0x7f
396
    cmp     bl, 3
397
 
302 hidnplayr 398
    je	     ash_eth_enable
1 ha 399
    ; Ethernet isn't enabled, so make sure that the card is disabled
400
    mov     [ethernet_active], byte 0
401
 
402
    ret
403
 
404
ash_eth_enable:
405
    ; Probe for the card. This will reset it and enable the interface
406
    ; if found
407
    call    eth_probe
408
    cmp     eax, 0
302 hidnplayr 409
    je	    ash_eth_done	    ; Abort if no hardware found
1 ha 410
 
411
    mov     [ethernet_active], byte 1
412
 
413
ash_eth_done:
414
    ret
415
 
416
not2:
417
    cmp     eax, 3
418
    jnz     not3
419
    ; write the IP Address
420
    mov     [stack_ip], ebx
421
    ret
422
 
261 hidnplayr 423
;old functions was deleted
424
 
1 ha 425
not3:
426
    cmp     eax, 6
427
    jnz     not6
428
 
429
    ; Insert an IP packet into the stacks received packet queue
430
    call    stack_insert_packet
431
    ret
432
 
433
not6:
434
    cmp     eax, 7
435
    jnz     not7
436
 
437
    ; Test for any packets queued for transmission over the network
438
 
439
not7:
440
    cmp     eax, 8
441
    jnz     not8
442
 
443
    call    stack_get_packet
444
    ; Extract a packet queued for transmission by the network
445
    ret
446
 
447
not8:
448
    cmp     eax, 9
449
    jnz     not9
450
 
451
    ; read the gateway IP address
452
 
453
    mov     eax, [gateway_ip]
454
    ret
455
 
456
not9:
457
    cmp     eax, 10
458
    jnz     not10
459
 
460
    ; read the subnet mask
461
 
462
    mov     eax, [subnet_mask]
463
    ret
464
 
465
not10:
466
    cmp     eax, 11
467
    jnz     not11
468
 
469
    ; write the gateway IP Address
470
    mov     [gateway_ip], ebx
471
 
472
    ret
473
 
474
not11:
475
    cmp     eax, 12
476
    jnz     not12
477
 
478
    ; write the subnet mask
479
    mov     [subnet_mask], ebx
480
 
481
 
482
not12:
483
    cmp     eax, 13
484
    jnz     not13
485
 
486
    ; read the dns
487
 
488
    mov     eax, [dns_ip]
489
    ret
490
 
491
not13:
492
    cmp     eax, 14
261 hidnplayr 493
    jnz     not14
1 ha 494
 
495
    ; write the dns IP Address
496
    mov     [dns_ip], ebx
497
 
379 serge 498
    ret
499
 
261 hidnplayr 500
;
501
not14:
502
	cmp	eax, 15
503
	jnz	not15
504
 
505
    ; in ebx we need 4 to read the last 2 bytes
506
	cmp	ebx, dword 4
507
	je	read
379 serge 508
 
261 hidnplayr 509
    ; or we need 0 to read the first 4 bytes
510
	cmp	ebx, dword 0
511
	jnz	param_error
512
 
513
    ; read MAC, returned (in mirrored byte order) in eax
514
read:
515
	mov	eax, [node_addr + ebx]
516
	jmp	@f
379 serge 517
 
518
param_error:
261 hidnplayr 519
	mov	eax, -1     ; params not accepted
520
@@:
521
	ret
522
 
379 serge 523
 
261 hidnplayr 524
; 0 -> arp_probe
525
; 1 -> arp_announce
526
; 2 -> arp_responce (not supported yet)
527
 
528
not15:					; ARP stuff
529
	cmp	eax, 16
530
	jnz	not16
379 serge 531
 
261 hidnplayr 532
	cmp	ebx, 0
533
	je	a_probe
379 serge 534
 
261 hidnplayr 535
	cmp	ebx, 1
536
	je	a_ann			; arp announce
379 serge 537
 
302 hidnplayr 538
;       cmp     ebx,2
539
;       jne     a_resp                  ; arp response
261 hidnplayr 540
 
541
	jmp	param15_error
379 serge 542
 
543
 
261 hidnplayr 544
; arp probe, sender IP must be set to 0.0.0.0, target IP is set to address being probed
545
; ecx: pointer to target MAC, MAC should set to 0 by application
546
; edx: target IP
547
a_probe:
548
	push	dword [stack_ip]
379 serge 549
 
261 hidnplayr 550
	mov	edx, [stack_ip]
551
	mov	[stack_ip], dword 0
379 serge 552
	mov	esi, ecx		; pointer to target MAC address
261 hidnplayr 553
	call	arp_request
379 serge 554
 
555
	pop	dword [stack_ip]
302 hidnplayr 556
	jmp	@f
379 serge 557
 
261 hidnplayr 558
; arp announce, sender IP must be set to target IP
559
; ecx: pointer to target MAC
560
a_ann:
561
	mov	edx, [stack_ip]
562
	mov	esi, ecx		; pointer to target MAC address
563
	call	arp_request
564
	jmp	@f
379 serge 565
 
261 hidnplayr 566
param15_error:
567
	mov	eax, -1
379 serge 568
 
261 hidnplayr 569
@@:
570
	ret
571
;
572
; modified by [smb]
573
 
574
;
575
; ARPTable manager interface
576
not16:
577
    cmp     eax, 17
578
    jnz     stack_driver_end
579
 
580
    ;see "proc arp_table_manager" for more details
581
    stdcall arp_table_manager,ebx,ecx,edx  ;Opcode,Index,Extra
582
 
1 ha 583
    ret
261 hidnplayr 584
;
1 ha 585
 
586
stack_driver_end:
261 hidnplayr 587
	ret
1 ha 588
 
589
 
590
 
591
;***************************************************************************
592
;   Function
593
;      app_socket_handler
594
;
595
;   Description
261 hidnplayr 596
;       This is an application service, called by int 0x40, function 53
1 ha 597
;       It provides application access to stack socket services
598
;       such as opening sockets
599
;
600
;***************************************************************************
601
app_socket_handler:
602
    cmp     eax, 0
603
    jnz     nots0
604
 
605
    call    socket_open
606
    ret
607
 
608
nots0:
609
    cmp     eax, 1
610
    jnz     nots1
611
 
612
    call    socket_close
613
    ret
614
 
615
nots1:
616
    cmp     eax, 2
617
    jnz     nots2
618
 
619
    call    socket_poll
620
    ret
621
 
622
nots2:
623
    cmp     eax, 3
624
    jnz     nots3
625
 
626
    call    socket_read
627
    ret
628
 
629
nots3:
630
    cmp     eax, 4
631
    jnz     nots4
632
 
633
    call    socket_write
634
    ret
635
 
636
nots4:
637
    cmp     eax, 5
638
    jnz     nots5
639
 
640
    call    socket_open_tcp
641
    ret
642
 
643
nots5:
644
    cmp     eax, 6
645
    jnz     nots6
646
 
647
    call    socket_status
648
    ret
649
 
650
nots6:
651
    cmp     eax, 7
652
    jnz     nots7
653
 
654
    call    socket_write_tcp
655
    ret
656
 
657
nots7:
658
    cmp     eax, 8
659
    jnz     nots8
660
 
661
    call    socket_close_tcp
662
    ret
663
 
664
nots8:
665
    cmp     eax, 9
666
    jnz     nots9
667
 
668
    call    is_localport_unused
669
    ret
670
 
671
nots9:
302 hidnplayr 672
    cmp     eax, 10
673
    jnz     nots10
674
 
675
    mov     eax,dword[drvr_cable]
676
    test    eax,eax
677
    jnz     @f		      ; if function is not implented, return -1
678
    mov     al,-1
679
    ret
680
 
681
   @@:
682
    call    dword[drvr_cable]
683
    ret
684
 
685
 
686
nots10:
323 hidnplayr 687
    cmp     eax, 11
688
    jnz     nots11
689
 
690
    call    socket_read_packet
691
    ret
692
 
693
nots11:
1 ha 694
    cmp     eax, 254
695
    jnz     notdump
696
 
697
    ret
698
 
699
notdump:
700
    cmp     eax, 255
701
    jnz     notsdebug
702
 
703
    ; This sub function allows access to debugging information on the stack
704
    ; ebx holds the request:
705
    ;  100 : return length of empty queue
706
    ;  101 : return length of IPOUT QUEUE
707
    ;  102 : return length of IPIN QUEUE
708
    ;  103 : return length of NET1OUT QUEUE
709
    ; 200 : return # of ARP entries
710
    ; 201 : return size of ARP table ( max # entries )
711
    ; 202 : select ARP table entry #
712
    ; 203 : return IP of selected table entry
713
    ; 204 : return High 4 bytes of MAC address of selected table entry
714
    ; 205 : return low  2 bytes of MAC address of selected table entry
715
    ; 206 : return status word of selected table entry
716
    ; 207 : return Time to live of selected table entry
717
 
718
 
719
    ;  2 : return number of IP packets received
720
    ;  3 : return number of packets transmitted
721
    ;  4 : return number of received packets dumped
722
    ;  5 : return number of arp packets received
723
    ;  6 : return status of packet driver
724
    ;      ( 0 == not active, FFFFFFFF = successful )
725
 
726
    call    stack_internal_status
727
    ret
728
 
729
notsdebug:
730
    ; Invalid Option
731
    ret
732
 
733
 
734
uglobal
735
  ARPTmp:
736
  times 14 db 0
737
endg
738
 
739
;***************************************************************************
740
;   Function
741
;      stack_internal_status
742
;
743
;   Description
744
;       Returns information about the internal status of the stack
745
;       This is only useful for debugging
746
;       It works with the ethernet driver
747
;       sub function in ebx
748
;       return requested data in eax
749
;
750
;***************************************************************************
751
stack_internal_status:
752
    cmp     ebx, 100
753
    jnz     notsis100
754
 
755
    ;  100 : return length of EMPTY QUEUE
756
    mov     ebx, EMPTY_QUEUE
757
    call    queueSize
758
    ret
759
 
760
notsis100:
761
    cmp     ebx, 101
762
    jnz     notsis101
763
 
764
    ;  101 : return length of IPOUT QUEUE
765
    mov     ebx, IPOUT_QUEUE
766
    call    queueSize
767
    ret
768
 
769
notsis101:
770
    cmp     ebx, 102
771
    jnz     notsis102
772
 
773
    ;  102 : return length of IPIN QUEUE
774
    mov     ebx, IPIN_QUEUE
775
    call    queueSize
776
    ret
777
 
778
notsis102:
779
    cmp     ebx, 103
780
    jnz     notsis103
781
 
782
    ;  103 : return length of NET1OUT QUEUE
783
    mov     ebx, NET1OUT_QUEUE
784
    call    queueSize
785
    ret
786
 
787
notsis103:
788
    cmp     ebx, 200
789
    jnz     notsis200
790
 
791
    ; 200 : return num entries in arp table
792
    movzx   eax, byte [NumARP]
793
    ret
794
 
795
notsis200:
796
    cmp     ebx, 201
797
    jnz     notsis201
798
 
799
    ; 201 : return arp table size
800
    mov     eax, 20 ; ARP_TABLE_SIZE
801
    ret
802
 
803
notsis201:
804
    cmp     ebx, 202
805
    jnz     notsis202
806
 
807
    ; 202 - read the requested table entry
808
    ; into a temporary buffer
809
    ; ecx holds the entry number
810
 
811
    mov     eax, ecx
812
    mov     ecx, 14 ; ARP_ENTRY_SIZE
813
    mul     ecx
814
 
815
    mov     ecx, [eax + ARPTable]
816
    mov     [ARPTmp], ecx
817
    mov     ecx, [eax + ARPTable+4]
818
    mov     [ARPTmp+4], ecx
819
    mov     ecx, [eax + ARPTable+8]
820
    mov     [ARPTmp+8], ecx
821
    mov     cx, [eax + ARPTable+12]
822
    mov     [ARPTmp+12], cx
823
    ret
824
 
825
notsis202:
826
    cmp     ebx, 203
827
    jnz     notsis203
828
 
829
    ; 203 - return IP address
830
    mov     eax, [ARPTmp]
831
    ret
832
 
833
notsis203:
834
    cmp     ebx, 204
835
    jnz     notsis204
836
 
837
    ; 204 - return MAC high dword
838
    mov     eax, [ARPTmp+4]
839
    ret
840
 
841
notsis204:
842
    cmp     ebx, 205
843
    jnz     notsis205
844
 
845
    ; 205 - return MAC ls word
846
    movzx   eax, word [ARPTmp+8]
847
    ret
848
 
849
notsis205:
850
    cmp     ebx, 206
851
    jnz     notsis206
852
 
853
    ; 206 - return status word
854
    movzx   eax, word [ARPTmp+10]
855
    ret
856
 
857
notsis206:
858
    cmp     ebx, 207
859
    jnz     notsis207
860
 
861
    ; 207 - return ttl word
862
    movzx   eax, word [ARPTmp+12]
863
    ret
864
 
865
notsis207:
866
    cmp     ebx, 2
867
    jnz     notsis2
868
 
869
    ;  2 : return number of IP packets received
870
    mov     eax, [ip_rx_count]
871
    ret
872
 
873
notsis2:
874
    cmp     ebx, 3
875
    jnz     notsis3
876
 
877
    ;  3 : return number of packets transmitted
878
    mov     eax, [ip_tx_count]
879
    ret
880
 
881
notsis3:
882
    cmp     ebx, 4
883
    jnz     notsis4
884
 
885
    ;  4 : return number of received packets dumped
886
    mov     eax, [dumped_rx_count]
887
    ret
888
 
889
notsis4:
890
    cmp     ebx, 5
891
    jnz     notsis5
892
 
893
    ;  5 : return number of arp packets received
894
    mov     eax, [arp_rx_count]
895
    ret
896
 
897
notsis5:
898
    cmp     ebx, 6
899
    jnz     notsis6
900
 
901
    ;  6 : return status of packet driver
902
    ;  ( 0 == not active, FFFFFFFF = successful )
903
    mov     eax, [eth_status]
904
    ret
905
 
906
notsis6:
907
    xor     eax, eax
908
    ret
909
 
910
 
911
 
912
;***************************************************************************
913
;   Function
914
;      stack_get_packet
915
;
916
;   Description
917
;       extracts an IP packet from the NET1 output queue
918
;       and sends the data to the calling process
919
;       pointer to data in edx
920
;       returns number of bytes read in eax
921
;
922
;***************************************************************************
923
stack_get_packet:
924
    ; Look for a buffer to tx
925
    mov     eax, NET1OUT_QUEUE
926
    call    dequeue
927
    cmp     ax, NO_BUFFER
302 hidnplayr 928
    je	    sgp_non_exit	    ; Exit if no buffer available
1 ha 929
 
302 hidnplayr 930
    push    eax 		    ; Save buffer number for freeing at end
1 ha 931
 
932
    push    edx
933
    ; convert buffer pointer eax to the absolute address
934
    mov     ecx, IPBUFFSIZE
935
    mul     ecx
936
    add     eax, IPbuffs
937
    pop     edx
938
 
302 hidnplayr 939
    push    eax 		    ; save address of IP data
1 ha 940
    ; Get the address of the callers data
379 serge 941
    mov     edi,[TASK_BASE]
115 poddubny 942
    add     edi,TASKDATA.mem_start
1 ha 943
    add     edx,[edi]
944
    mov     edi, edx
945
    pop     eax
946
 
302 hidnplayr 947
    mov     ecx, 1500		; should get the actual number of bytes to write
1 ha 948
    mov     esi, eax
949
    cld
302 hidnplayr 950
    rep     movsb		; copy the data across
1 ha 951
 
952
    ; And finally, return the buffer to the free queue
953
    pop     eax
954
    call    freeBuff
955
 
956
    mov     eax, 1500
957
    ret
958
 
959
sgp_non_exit:
960
    xor     eax, eax
961
    ret
962
 
963
 
964
 
965
;***************************************************************************
966
;   Function
967
;      stack_insert_packet
968
;
969
;   Description
970
;       writes an IP packet into the stacks receive queue
971
;       # of bytes to write in ecx
972
;       pointer to data in edx
973
;       returns 0 in eax ok, -1 == failed
974
;
975
;***************************************************************************
976
stack_insert_packet:
977
 
978
    mov     eax, EMPTY_QUEUE
979
    call    dequeue
980
    cmp     ax, NO_BUFFER
302 hidnplayr 981
    je	    sip_err_exit
1 ha 982
 
983
    push    eax
984
 
985
    ; save the pointers to the data buffer & size
986
    push    edx
987
    push    ecx
988
 
989
    ; convert buffer pointer eax to the absolute address
990
    mov     ecx, IPBUFFSIZE
991
    mul     ecx
992
    add     eax, IPbuffs
993
 
994
    mov     edx, eax
995
 
996
    ; So, edx holds the IPbuffer ptr
997
 
302 hidnplayr 998
    pop     ecx 		    ; count of bytes to send
999
    mov     ebx, ecx		    ; need the length later
1000
    pop     eax 		    ; get callers ptr to data to send
1 ha 1001
 
1002
    ; Get the address of the callers data
379 serge 1003
    mov     edi,[TASK_BASE]
115 poddubny 1004
    add     edi,TASKDATA.mem_start
1 ha 1005
    add     eax,[edi]
1006
    mov     esi, eax
1007
 
1008
    mov     edi, edx
1009
    cld
302 hidnplayr 1010
    rep     movsb		; copy the data across
1 ha 1011
 
1012
    pop     ebx
1013
 
1014
    mov     eax, IPIN_QUEUE
1015
    call    queue
1016
 
1017
    inc     dword [ip_rx_count]
1018
 
1019
    mov     eax, 0
1020
    ret
1021
 
1022
sip_err_exit:
1023
    mov     eax, 0xFFFFFFFF
1024
    ret
1025