Subversion Repositories Kolibri OS

Rev

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

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