A2osX/BIN/TELNET.S.txt
2018-10-16 17:48:03 +02:00

314 lines
6.2 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.

NEW
PREFIX /A2OSX.BUILD
AUTO 4,1
.LIST OFF
.OP 65C02
.OR $2000
.TF BIN/TELNET
*--------------------------------------
.INB INC/MACROS.I
.INB INC/A2OSX.I
.INB INC/ETH.I
.INB INC/LIBTCPIP.I
.INB INC/NET.TELNET.I
*--------------------------------------
TIMEOUT.MAX .EQ 40 4 sec.
BUFSIZE .EQ 256
*--------------------------------------
ZPIPCfgPtr .EQ ZPBIN
ZPBufPtr .EQ ZPBIN+2
ZPBufLen .EQ ZPBIN+4
*--------------------------------------
* File Header (16 Bytes)
*--------------------------------------
CS.START cld
jmp (.1,x)
.DA #$61 6502,Level 1 (65c02)
.DA #1 BIN Layout Version 1
.DA #S.PS.F.EVENT S.PS.F
.DA #0
.DA CS.END-CS.START Code Length To Relocate
.DA DS.END-DS.START Data Segment to Allocate
.DA #16 SS
.DA #6 ZP
.DA 0
*--------------------------------------
.1 .DA CS.INIT
.DA CS.RUN
.DA CS.DOEVENT
.DA CS.QUIT
L.LIBTCPIP .DA LIBTCPIP
L.SA.LOCAL .DA SA.LOCAL
L.SA.LOCAL.AD .DA SA.LOCAL+S.SOCKADDR.ADDR
L.SA.REMOTE .DA SA.REMOTE
L.SA.REMOTE.AD .DA SA.REMOTE+S.SOCKADDR.ADDR
L.MSG.IPKO .DA MSG.IPKO
L.MSG.USAGE .DA MSG.USAGE
L.MSG.UNKNOWN .DA MSG.UNKNOWN
L.MSG.HOSTOK .DA MSG.HOSTOK
L.MSG.SKTKO .DA MSG.SKTKO
L.MSG.SKTOK .DA MSG.SKTOK
L.MSG.SKTERR .DA MSG.SKTERR
L.MSG.IOERR .DA MSG.IOERR
L.MSG.USER .DA MSG.USER
.DA 0
*--------------------------------------
CS.INIT >LDYA L.LIBTCPIP
>SYSCALL LoadLib
bcs .9
sta hLIBTCPIP
clc
.9 rts
*--------------------------------------
CS.RUN >LIBCALL hLIBTCPIP,LIBTCPIP.GET.IPCFG is TCPIP loaded ?
>STYA ZPIPCfgPtr
lda (ZPIPCfgPtr) Configured ?
bmi CS.RUN.IPOK
>LDYA L.MSG.IPKO
>SYSCALL puts
lda #K.E.SYN
sec
rts
CS.RUN.IPOK ldy #S.PS.ARGC
lda (pPs),y
beq .9
jsr Init.Timeout
.2 >PUSHW L.SA.REMOTE.AD
lda #1
>SYSCALL ArgV
>PUSHYA
>LIBCALL hLIBTCPIP,LIBTCPIP.HST.GETBYNAME
bcc CS.RUN.HOSTOK
>SLEEP
jsr Wait.Timeout
bcc .2
lda #1
>SYSCALL ArgV
>PUSHYA
>PUSHBI 2
>LDYA L.MSG.UNKNOWN
>SYSCALL printf
bra .99
.9 >LDYA L.MSG.USAGE
>SYSCALL puts
.99 lda #K.E.SYN
sec
rts
CS.RUN.HOSTOK ldy #S.PS.ARGC
lda (pPs),y
cmp #2
bcc CS.RUN.PORTOK
lda #2
>SYSCALL ArgV
>SYSCALL atoi
>STYA SA.REMOTE+S.SOCKADDR.PORT
CS.RUN.PORTOK lda #1
>SYSCALL ArgV
>PUSHYA
>PUSHW SA.REMOTE+S.SOCKADDR.PORT
ldx #3
.1 >PUSHB SA.REMOTE+S.SOCKADDR.ADDR,x
dex
bpl .1
>PUSHBI 8
>LDYA L.MSG.HOSTOK
>SYSCALL printf
CS.RUN.OPENSKT >PUSHBI 0 no protocol
lda #S.SOCKET.T.STREAM
>LIBCALL hLIBTCPIP,LIBTCPIP.Socket
bcs .9
>STA.G hSocket
pha
>PUSHW L.SA.LOCAL
pla
>LIBCALL hLIBTCPIP,LIBTCPIP.Bind
bcc .2
.9 >LDYA L.MSG.SKTKO
>SYSCALL puts
lda #K.E.SYN
sec
rts
.2 >PUSHW L.SA.REMOTE
>LDA.G hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.Connect
>LDYA L.MSG.SKTOK
>SYSCALL puts
>LDYAI BUFSIZE
>SYSCALL getmem
bcs .9
>STYA ZPBufPtr
txa
>STA.G hBuf
CS.RUN.LOOP >SLEEP
>PUSHWI BUFSIZE
>PUSHW ZPBufPtr
>LDA.G hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.Read
bcc .1
tay
beq .2 no char
jmp CS.RUN.SKTERR
.1 >PUSHYA len
>PUSHW ZPBufPtr
ldy #S.PS.hStdOut
lda (pPs),y
>SYSCALL write
bcs .11
.2 >PUSHWI BUFSIZE
>PUSHW ZPBufPtr
ldy #S.PS.hStdIn
lda (pPs),y
>SYSCALL read
bcc .3
tay
beq CS.RUN.LOOP no char from STDIN
.11 jmp CS.RUN.IOERR
.3 >STYA ZPBufLen
lda (ZPBufPtr)
cmp #3 Ctrl-C
beq CS.RUN.USER
>PUSHW ZPBufLen
>PUSHW ZPBufPtr
>LDA.G hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.Write
bcs CS.RUN.SKTERR
jmp CS.RUN.LOOP
.9 lda #0
sec
rts
*--------------------------------------
CS.RUN.SKTERR pha
>PUSHA
>PUSHBI 1
>LDYA L.MSG.SKTERR
>SYSCALL printf
pla
sec
rts
*--------------------------------------
CS.RUN.IOERR pha
>PUSHA
>PUSHBI 1
>LDYA L.MSG.IOERR
>SYSCALL printf
pla
sec
rts
*--------------------------------------
CS.RUN.USER >LDYA L.MSG.USER
>SYSCALL puts
lda #3
sec
rts
*--------------------------------------
CS.DOEVENT lda (pEvent)
bpl .9 is it a TIMER event?
ldy #TimeOut
lda (pData),y
beq .9
dec
sta (pData),y
.9 sec do not discard TIMER event
rts
*--------------------------------------
CS.QUIT >LDA.G hSocket
beq .1
>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown
.1 >LDA.G hBuf
beq .2
>SYSCALL freemem
.2 lda hLIBTCPIP
>SYSCALL UnloadLib
clc
rts
*--------------------------------------
Init.Timeout ldy #TimeOut
lda #TIMEOUT.MAX
sta (pData),y
rts
*--------------------------------------
Wait.TimeOut sec
ldy #TimeOut
lda (pData),y
beq .9
clc
.9 rts
*--------------------------------------
CS.END
LIBTCPIP .AZ "libtcpip"
hLIBTCPIP .BS 1
MSG.IPKO .AZ "TCP/IP Not Loaded/Configured."
MSG.USAGE .AZ "Usage : TELNET <ip|host> [port]"
MSG.UNKNOWN .AZ "%s: Unknown host\r\n"
MSG.HOSTOK .AZ "Connecting to %d.%d.%d.%d:%D (%s)..."
MSG.SKTKO .AZ "Failed to Open Socket."
MSG.SKTOK .AZ "Connected."
MSG.SKTERR .AZ "Socket Error : $%h\r\n"
MSG.IOERR .AZ "I/O Error : $%h\r\n"
MSG.USER .AZ "User interrupt."
*--------------------------------------
SA.LOCAL .DA #AF.INET S.SOCKADDR.AF
.BS 1
.BS 4 S.SOCKADDR.ADDR
.BS 2 S.SOCKADDR.PORT
*--------------------------------------
SA.REMOTE .DA #AF.INET S.SOCKADDR.AF
.BS 1
.BS 4 S.SOCKADDR.ADDR
.DA TCP.PORT.TELNET
*--------------------------------------
.DUMMY
.OR 0
DS.START
hBuf .BS 1
hSocket .BS 1
TimeOut .BS 1
DS.END
.ED
*--------------------------------------
MAN
SAVE /A2OSX.SRC/BIN/TELNET.S
ASM