NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF sbin/telnetd
*--------------------------------------
				.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.
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPIPCfgPtr		.BS 2
ZPSktPtr		.BS 2
TimeOut			.BS 1
hSrvSocket		.BS 1
hClientSocket	.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		CS
				.DA DS.END-DS.START		DS
				.DA #32					SS
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.LIBTCPIP		.DA LIBTCPIP
L.SA.LOCAL		.DA SA.LOCAL
L.MSG.TCPWAIT	.DA MSG.TCPWAIT
L.MSG.INITOK	.DA MSG.INITOK
L.MSG.TCPIPERR	.DA MSG.TCPIPERR
L.MSG.SKTERR	.DA MSG.SKTERR
L.MSG.INCOMING	.DA MSG.INCOMING
L.MSG.EXECERR	.DA MSG.EXECERR
L.NOD.Template	.DA NOD.Template
L.CMD.Template	.DA CMD.Template
				.DA 0
*--------------------------------------
CS.INIT			>LDYA L.LIBTCPIP
				>SYSCALL LoadLib
				bcs .9
				sta hLIBTCPIP

.9				rts
*--------------------------------------
CS.RUN			lda #TIMEOUT.MAX
				sta TimeOut

				>LDYA L.MSG.TCPWAIT
				>SYSCALL PutS

.1				>SLEEP
				>LIBCALL hLIBTCPIP,LIBTCPIP.GETCFG	is TCPIP loaded ?
				bcs .99

				>STYA ZPIPCfgPtr

				lda (ZPIPCfgPtr)		Configured ?
				bmi CS.RUN.INIT

				lda TimeOut
				beq .99

				ldy #S.PS.hStdIn
				lda (pPS),y
				>SYSCALL FEOF
				bcs .9					I/O err

				tay
				bne .1

				>SYSCALL GetChar
				bcs .9

				cmp #$03
				bne .1
*				sec
				rts

.99				pha
				>LDYA L.MSG.TCPIPERR
				>SYSCALL PutS
				pla
				sec
.9				rts

CS.RUN.INIT		ldx #3
				ldy #S.IPCFG.IP+3

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

				lda #1
				>SYSCALL ArgV
				bcs CS.RUN.PORTOK
				>SYSCALL AToI
				>STYA SA.LOCAL+S.SOCKADDR.PORT

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

				sta hSrvSocket

				>PUSHA
				>PUSHW L.SA.LOCAL
				>LIBCALL hLIBTCPIP,LIBTCPIP.Bind
				bcs .9

				lda hSrvSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Listen
				bcs .9

				>LDYA L.MSG.INITOK
				>SYSCALL PutS

.2				>SLEEP

				lda hSrvSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Accept
				bcs .2

				pha
				jsr CS.RUN.CLIENT
				pla

				bcc .2
				>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown
				bra .2

.9				pha
				>LDYA L.MSG.SKTERR
				>SYSCALL PutS
				pla
				sec
				rts
*--------------------------------------
CS.RUN.CLIENT	>SLEEP					give some time for TCPIP SYN/ACK

				sta hClientSocket
				>SYSCALL GetMemPtr
				>STYA ZPSktPtr

				>PUSHW L.MSG.INCOMING

				>PUSHB hClientSocket

				ldx #3
				ldy #S.SOCKET.REM.ADDR

.1				>PUSHB (ZPSktPtr),y
				iny
				dex
				bpl .1

				>PUSHBI 5
				>SYSCALL PrintF

				>PUSHEA.G NodBuf
				>PUSHW L.NOD.Template
				>PUSHB hClientSocket
				>PUSHBI 1
				>SYSCALL SPrintF

CS.RUN.CLIENT1	>PUSHEA.G NodBuf+5
				>PUSHW 0
				>PUSHB hClientSocket
				>SYSCALL MKNod
				bcs .9

				>PUSHEA.G CmdBuf
				>PUSHW L.CMD.Template
				>PUSHEA.G NodBuf
				>PUSHBI 2
				>SYSCALL SPrintF

				>PUSHEA.G CmdBuf
				>PUSHBI 0
				>SYSCALL ExecL
				bcc .8

				pha
				>PUSHW L.MSG.EXECERR
				pla
				>PUSHA
				>PUSHBI 1
				>SYSCALL PrintF

.8				clc
.9				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 hSrvSocket
				beq .3

				>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown

.3				lda hLIBTCPIP
				beq .8

				>SYSCALL UnloadLib

.8				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
hLIBTCPIP		.BS 1
LIBTCPIP		.AZ "libtcpip"
MSG.TCPWAIT		.AZ "TELNETD:Waiting for TCP/IP initializing..."
MSG.INITOK		.AZ "TELNETD:Init Ok, Listening."
MSG.TCPIPERR	.AZ "TELNETD:TCP/IP Not initialized properly."
MSG.SKTERR		.AZ "TELNETD:Listen Error."
MSG.INCOMING	.AZ "TELNETD:Incoming Connection [SKT=%h] From : %d.%d.%d.%d\r\n"
MSG.EXECERR		.AZ "TELNETD:ExecL failed $%h."
*--------------------------------------
NOD.Template	.AZ "/dev/tty%h"
CMD.Template	.AZ "${ROOT}sbin/getty -E %s ${ROOT}sbin/login"
*--------------------------------------
SA.LOCAL		.DA #AF.INET			S.SOCKADDR.AF
				.BS 1
				.BS 4					S.SOCKADDR.ADDR
				.DA TCP.PORT.TELNET
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
NodBuf			.BS 11
CmdBuf			.BS 65
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/sbin/telnetd.s
ASM
