NEW
  AUTO 3,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 200					20 sec.
BUFSIZE			.EQ 256
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPIPCfgPtr		.BS 2
ZPBufPtr		.BS 2
ZPBufLen		.BS 2
hBuf			.BS 1
hSocket			.BS 1
TimeOut			.BS 1
ZS.END			.ED
*--------------------------------------
*			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 Size (without Constants)
				.DA DS.END-DS.START		Data Segment Size
				.DA #64					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.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.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.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

.9				rts
*--------------------------------------
CS.RUN			>LIBCALL hLIBTCPIP,LIBTCPIP.GETCFG	is TCPIP loaded ?
				>STYA ZPIPCfgPtr
				lda (ZPIPCfgPtr)		Configured ?
				bmi CS.RUN.IPOK

				>LDYA L.MSG.IPKO
				>SYSCALL PutS
				lda #E.SYN
				sec
				rts

CS.RUN.IPOK		ldy #S.PS.ARGC
				lda (pPS),y
				cmp #1
				bcc .9

				ldy #S.IPCFG.IP+3
				ldx #3

.1				lda (ZPIPCfgPtr),y
				sta SA.LOCAL+S.SOCKADDR.ADDR,x
				dey
				dex
				bpl .1

				lda #TIMEOUT.MAX
				sta TimeOut

.2				>SLEEP

				>PUSHW L.SA.REMOTE.AD
				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>LIBCALL hLIBTCPIP,LIBTCPIP.HST.GetByName
				bcc CS.RUN.HOSTOK
				lda TimeOut
				bne .2

				>PUSHW L.MSG.UNKNOWN
				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI 2
				>SYSCALL PrintF
				bra .99

.9				>LDYA L.MSG.USAGE
				>SYSCALL PutS

.99				lda #E.SYN
				sec
				rts

CS.RUN.HOSTOK	lda #2
				>SYSCALL ArgV
				bcs CS.RUN.PORTOK

				>SYSCALL AToI
				>STYA SA.REMOTE+S.SOCKADDR.PORT

CS.RUN.PORTOK	>PUSHW L.MSG.HOSTOK
				ldx #0

.1				>PUSHB SA.REMOTE+S.SOCKADDR.ADDR,x
				inx
				cpx #4
				bne .1

				>PUSHW SA.REMOTE+S.SOCKADDR.PORT
				lda #1
				>SYSCALL ArgV
				>PUSHYA

				>PUSHBI 8
				>SYSCALL PrintF

CS.RUN.OPENSKT	>PUSHBI S.SOCKET.T.STREAM
				>PUSHBI 0				no protocol
				>LIBCALL hLIBTCPIP,LIBTCPIP.Socket
				bcs .9

.1				sta hSocket

				>PUSHA
				>PUSHW L.SA.LOCAL
				>LIBCALL hLIBTCPIP,LIBTCPIP.Bind
				bcc .2

.9				jmp CS.RUN.SKTERR

.2				lda #TIMEOUT.MAX
				sta TimeOut

.3				>SLEEP

				>PUSHB hSocket
				>PUSHW L.SA.REMOTE
				>LIBCALL hLIBTCPIP,LIBTCPIP.Connect
				bcc .4

				cmp #ERR.SKT.NOCONN
				bne .9

				lda TimeOut
				bne .3

				lda #ERR.SKT.NOCONN
				bra .9

.4				>LDYA L.MSG.SKTOK
				>SYSCALL PutS

				>LDYAI BUFSIZE
				>SYSCALL GetMem
				bcc .5

.5				>STYA ZPBufPtr
				stx hBuf
*--------------------------------------
CS.RUN.LOOP		>SLEEP

				lda hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.EOF
				bcs CS.RUN.SKTERR

				tay
				bne .2					EOF, no char

				lda hSocket
				jsr CS.RUN.PushRead
				>LIBCALL hLIBTCPIP,LIBTCPIP.Read
				bcs CS.RUN.SKTERR

				>STYA ZPBufLen

				ldy #S.PS.hStdOut
				lda (pPS),y
				jsr CS.RUN.PushWrite
				>SYSCALL FWrite
				bcs CS.RUN.IOERR

.2				ldy #S.PS.hStdIn
				lda (pPS),y
				>SYSCALL FEOF
				bcs CS.RUN.IOERR

				tay
				bne CS.RUN.LOOP			EOF = true, no char from STDIN

				ldy #S.PS.hStdIn
				lda (pPS),y
				jsr CS.RUN.PushRead
				>SYSCALL FRead
				bcs CS.RUN.IOERR

				>STYA ZPBufLen

				lda (ZPBufPtr)
				cmp #$14				Ctrl-T
				beq CS.RUN.USERINT

				lda hSocket
				jsr CS.RUN.PushWrite
				>LIBCALL hLIBTCPIP,LIBTCPIP.Write
				bcc CS.RUN.LOOP
*--------------------------------------
CS.RUN.SKTERR	ldx #0

				bra CS.RUN.ERR
*--------------------------------------
CS.RUN.IOERR	ldx #2

CS.RUN.ERR		pha
				>PUSHW L.MSG.SKTERR,x
				pla
				pha
				>PUSHA
				>PUSHBI 1
				>SYSCALL PrintF
				pla
				sec
				rts
*--------------------------------------
CS.RUN.USERINT	>LDYA L.MSG.USER
				>SYSCALL PutS
				lda #0
				sec
				rts
*--------------------------------------
CS.RUN.PushRead	>PUSHA
				>PUSHW ZPBufPtr
				>PUSHWI BUFSIZE
				rts
*--------------------------------------
CS.RUN.PushWrite
				>PUSHA
				>PUSHW ZPBufPtr
				>PUSHW ZPBufLen
				rts
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9					is it a TIMER event?

				lda TimeOut
				beq .9

				dec TimeOut

.9				sec						do not discard TIMER event
				rts
*--------------------------------------
CS.QUIT			lda hSocket
				beq .1

				>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown

.1				lda hBuf
				beq .2

				>SYSCALL FreeMem

.2				lda hLIBTCPIP
				beq .8

				>SYSCALL UnloadLib

.8				clc
				rts
*--------------------------------------
CS.END
LIBTCPIP		.AZ "libtcpip"
hLIBTCPIP		.BS 1
MSG.IPKO		.CZ "TCP/IP Not Loaded/Configured."
MSG.USAGE		.CZ "Usage : TELNET <ip|host> [port]"
MSG.UNKNOWN		.CZ "%s: Unknown host\r\n"
MSG.HOSTOK		.CZ "Connecting to %d.%d.%d.%d:%D (%s)..."
MSG.SKTOK		.CS "Connected\r\n"
				.CZ "(Exit key is Ctrl-T)"
MSG.SKTERR		.CZ "Socket Error : $%h\r\n"
MSG.IOERR		.CZ "I/O Error : $%h\r\n"
MSG.USER		.CZ "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
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/telnet.s
ASM
