A2osX/BIN/PING.S.txt

368 lines
7.1 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

PR#3
PREFIX /DATA/A2OSX
NEW
INC 1
AUTO 6
.LIST OFF
.OP 65C02
.OR $2000
.TF BIN/PING
*--------------------------------------
.INB INC/MACROS.I
.INB INC/A2OSX.I
.INB INC/KERNEL.I
.INB INC/LIBSTR.I
.INB INC/LIBTCPIP.I
*--------------------------------------
PING.TIMEOUT .EQ 40 40*100ms = 4 sec.
*--------------------------------------
ZPFrameBase1 .EQ ZPBIN
ZPFrameLen1 .EQ ZPBIN+2
ZPFramePtr1 .EQ ZPBIN+4
*--------------------------------------
* Main entry point
*--------------------------------------
* Code signature and INIT table
*--------------------------------------
* CLD $D8
* JMP (*,x) $7C
* #JMPTABLE
* /JMPTABLE
*--------------------------------------
CS.START cld
jmp (.1,x)
.1 .DA CS.INIT
.DA CS.RUN
.DA CS.EVENT
.DA CS.QUIT
L.LIBSTR .DA LIBSTR
L.LIBTCPIP .DA LIBTCPIP
L.SSCANF.IP .DA SSCANF.IP
L.DST.IP .DA DST.IP
L.MSG0 .DA MSG0
L.MSG1 .DA MSG1
L.MSG2 .DA MSG2
L.MSG3 .DA MSG3
.DA 0
.DA CS.END-CS.START Code Length To Relocate
.DA DS.END-DS.START Data Segment to Allocate
*--------------------------------------
CS.INIT ldy #S.PS.hARGS
lda (pPsContext),y
bne .1
lda #SYSMGR.ERRSYN
sec
rts
.1 >LIBLOADP L.LIBSTR
sta hLIBSTR
>LIBLOADP L.LIBTCPIP
sta hLIBTCPIP
>LIBCALL hLIBTCPIP,LIBTCPIP.GET.IPCFG is TCPIP loaded ?
bcs .9
>STYA ZPQuickPtr1
lda (ZPQuickPtr1) Configured ?
bpl .9
ldy #S.IPCFG.HDEV
lda (ZPQuickPtr1),y
sta hDev
lda A2OSX.RANDOM16
sta Identifier
lda A2OSX.RANDOM16+1
sta Identifier+1
stz Sequence
stz Sequence+1
lda (pPsContext)
ora #S.PS.F.EVENT Now accept events
sta (pPsContext)
clc
.9 rts
*--------------------------------------
CS.RUN >PUSHW L.DST.IP
>PUSHW L.SSCANF.IP
ldy #S.PS.hARGS
lda (pPsContext),y
>SYSCALL SYS.GetMemPtrA
>PUSHYA
>LIBCALL hLIBSTR,LIBSTR.SSCANF
bcc CS.RUN.IPOK
lda #PING.TIMEOUT
sta TimeOut
CS.RUN.DNSQUERY >PUSHW L.DST.IP
ldy #S.PS.hARGS
lda (pPsContext),y
>PUSHA
>LIBCALL hLIBTCPIP,LIBTCPIP.DNS.QUERY
bcc CS.RUN.IPOK
jsr A2osX.SLEEP
lda TimeOut
bne CS.RUN.DNSQUERY
ldy #S.PS.hARGS
lda (pPsContext),y
>SYSCALL SYS.GetMemPtrA
>PUSHYA
>PUSHW L.MSG0
>LIBCALL hLIBSTR,LIBSTR.PRINTF
sec
CS.RUN.RTS rts
CS.RUN.IPOK ldy #S.PS.hARGS
lda (pPsContext),y
>SYSCALL SYS.GetMemPtrA
>PUSHYA
>PUSHB DST.IP+3
>PUSHB DST.IP+2
>PUSHB DST.IP+1
>PUSHB DST.IP
>PUSHW L.MSG1
>LIBCALL hLIBSTR,LIBSTR.PRINTF
CS.RUN.ECHO >LIBCALL hLIBTCPIP,LIBTCPIP.NEW.ICMP.FRAME
bcs CS.RUN.RTS
>STYA ZPFrameBase1
stx hFrame
ldx #3
ldy #S.IP.DST+3
.1 lda DST.IP,x
sta (ZPFrameBase1),y
dey
dex
bpl .1
ldy #S.ICMP.TYPE
lda #S.ICMP.TYPE.ECHOREQ
sta (ZPFrameBase1),y
ldy #S.ICMP.IDENTIFIER
lda Identifier
sta (ZPFrameBase1),y
iny
lda Identifier+1
sta (ZPFrameBase1),y
inc Sequence
bne .2
inc Sequence+1
.2 ldy #S.ICMP.SEQUENCE
lda Sequence
sta (ZPFrameBase1),y
iny
lda Sequence+1
sta (ZPFrameBase1),y
lda #PING.TIMEOUT
sta TimeOut
.3 >PUSHWI S.ICMP+32
lda hFrame
>SYSCALL SYS.GetMemPtrA
>PUSHYA
>LIBCALL hLIBTCPIP,LIBTCPIP.SEND.ICMP.FRAME
bcc .4
jsr A2osX.SLEEP
lda TimeOut
bne .3
.4 lda hFrame
>SYSCALL SYS.FreeMemA
stz bReply
lda #PING.TIMEOUT
sta TimeOut
CS.RUN.WAIT jsr A2osX.SLEEP
lda bReply
bne .1
lda bCTRLC
bne CS.RUN.ERROR
lda TimeOut
bne CS.RUN.WAIT
>PUSHB DST.IP+3
>PUSHB DST.IP+2
>PUSHB DST.IP+1
>PUSHB DST.IP
>PUSHW L.MSG2
>LIBCALL hLIBSTR,LIBSTR.PRINTF
.1 lda bCTRLC
bne CS.RUN.ERROR
lda TimeOut
beq .2
jsr A2osX.SLEEP
bra .1
.2 jmp CS.RUN.ECHO
CS.RUN.ERROR sec
rts
*--------------------------------------
CS.EVENT lda (pEvent)
and #S.EVT.F.TIMER is it a TIMER event?
beq .1 no....
lda TimeOut
beq .10
dec TimeOut
.10 sec do not discard TIMER event
rts
.1 lda (pEvent)
and #S.EVT.F.KEY is it a KEY event?
beq .2
ldy #S.EVT.hDEV is Event from active IN device?
lda (pEvent),y
ldy #S.PS.hINDEV
cmp (pPsContext),y
bne .9
ldy #S.EVT.DATAHI is it an O or SAPPLE key ?
lda (pEvent),y
bne .9
ldy #S.EVT.DATALO
lda (pEvent),y
cmp #$03 Ctrl-C
bne .8
lda #$FF
sta bCTRLC
bra .8
.2 lda (pEvent)
and #S.EVT.F.NET
beq .9
ldy #S.EVT.hDEV
lda (pEvent),y
cmp hDev
bne .9
ldy #S.EVT.DATALO Get Frame hMem
lda (pEvent),y
>SYSCALL SYS.GetMemPtrA
>STYA ZPFrameBase1
ldy #S.IP.PROTOCOL
lda (ZPFrameBase1),y
cmp #S.IP.PROTOCOL.ICMP
bne .9
ldy #S.ICMP.TYPE
lda (ZPFrameBase1),y
cmp #S.ICMP.TYPE.ECHOREP
bne .9
ldy #S.ICMP.IDENTIFIER
lda (ZPFrameBase1),y
cmp Identifier
bne .9
iny
lda (ZPFrameBase1),y
cmp Identifier+1
bne .9
jsr CS.EVENT.REPLY
.8 >SYSCALL SYS.DestroyEvent
clc
rts
.9 sec
rts
*--------------------------------------
CS.EVENT.REPLY lda #PING.TIMEOUT
sec
sbc TimeOut
stz TimeOut.ms
stz TimeOut.ms+1
tay
beq .3
.1 lda TimeOut.ms
clc
adc #K.TIMER16RESOLUTION*20
sta TimeOut.ms
bcc .2
inc TimeOut+1+1
.2 dey
bne .1
.3 >PUSHW TimeOut.ms
ldy #S.IP.TTL
>PUSHB (ZPFrameBase1),y
ldy #S.ICMP.SEQUENCE+1
>PUSHB (ZPFrameBase1),y
dey
>PUSHB (ZPFrameBase1),y
ldy #S.IP.SRC+3
>PUSHB (ZPFrameBase1),y
dey
>PUSHB (ZPFrameBase1),y
dey
>PUSHB (ZPFrameBase1),y
dey
>PUSHB (ZPFrameBase1),y
>PUSHW L.MSG3
>LIBCALL hLIBSTR,LIBSTR.PRINTF
inc bReply
rts
*--------------------------------------
CS.QUIT lda hLIBTCPIP
>SYSCALL SYS.UnloadLibA
lda hLIBSTR
>SYSCALL SYS.UnloadLibA
clc
rts
*--------------------------------------
CS.END
LIBSTR >PSTRING "libstr.o"
LIBTCPIP >PSTRING "libtcpip.o"
SSCANF.IP >PSTRING "%d.%d.%d.%d"
MSG0 >CSTRING "%S: Unknown host\n"
MSG1 >CSTRING "PING %d.%d.%d.%d (%S)\n"
MSG2 >CSTRING "%d.%d.%d.%d: TimeOut/Host unreachable\n"
MSG3 >CSTRING "32 bytes from %d.%d.%d.%d, icmp_seq=%D, ttl=%d, time=%D ms\n"
*--------------------------------------
DS.START
hLIBSTR .BS 1
hLIBTCPIP .BS 1
hDev .BS 1
DST.IP .BS 4
hFrame .BS 1
TimeOut .BS 1
TimeOut.ms .BS 2
Identifier .BS 2
Sequence .BS 2
bCTRLC .BS 1
bReply .BS 1
DS.END
*--------------------------------------
MAN
SAVE BIN/PING.S
ASM