NEW
  AUTO 3,1								Enable MASM3 auto line num
				.LIST OFF
				.OP	65C02				Target CPU, must match CPU level in header
				.OR	$2000				usualy $2000, but any value > $100 allowed
				.TF sbin/cifsd
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/mli.e.i
				.INB inc/eth.i
				.INB inc/libtcpip.i
				.INB inc/net.smb.i
				.INB inc/libcifs.i
*--------------------------------------
TIMEOUT.MAX		.EQ 250					25 sec.
*--------------------------------------
* Zero Page Segment, up to 32 bytes
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPIPCfgPtr		.BS 2
hSocket			.BS 1
TimeOut			.BS 1
hSMBContext		.BS 1
hMount			.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
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #64					Stack Size
				.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.LIBCIFS		.DA LIBCIFS
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.TCPIPERR	.DA MSG.TCPIPERR
L.MSG.UNKNOWN	.DA MSG.UNKNOWN
L.MSG.CONNECTED	.DA MSG.CONNECTED
L.MSG.NOCONN	.DA MSG.NOCONN
L.MSG.MOUNTED	.DA MSG.MOUNTED
				.DA 0
*--------------------------------------
* Called once at process creation
* Put code for loading LIB here
*--------------------------------------
CS.INIT			>LDYA L.LIBTCPIP
				>SYSCALL LoadLib
				bcs .9

				sta hLIBTCPIP

				>LDYA L.LIBCIFS
				>SYSCALL LoadLib
				bcs .9

				sta hLIBCIFS

*				clc
.9
CS.INIT.RTS		rts
*--------------------------------------
* Called until exit with CS
* if RUN exits with CC, RUN entered again
*--------------------------------------
CS.RUN			lda hSocket
				beq .1

				lda #0
				clc
				rts

.1				jsr CS.RUN.CheckTCPIP
				bcs CS.INIT.RTS

				jsr CS.RUN.CheckArgs
				bcs CS.INIT.RTS

				jsr CS.RUN.Connect
				bcs CS.INIT.RTS

				>PUSHB hSocket
				>PUSHBI 0
				>LIBCALL hLIBCIFS,LIBCIFS.Negotiate
				bcs CS.INIT.RTS

				sta hSMBContext

				>PUSHB hSocket
				>PUSHB hSMBContext
				>PUSHWI 0
				>PUSHWI 0
				>PUSHBI 0
				>LIBCALL hLIBCIFS,LIBCIFS.SessionSetup
				bcs CS.INIT.RTS

				>PUSHB hSocket
				>PUSHB hSMBContext
				lda #2
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI 0
				>LIBCALL hLIBCIFS,LIBCIFS.TreeConnect
				bcs .9

				>PUSHB hSocket
				>PUSHB hSMBContext
				lda #3
				>SYSCALL ArgV
				>PUSHYA
				>LIBCALL hLIBCIFS,LIBCIFS.Mount
				bcs .9

				sta hMount

				>PUSHW L.MSG.MOUNTED

				lda #2
				>SYSCALL ArgV
				>PUSHYA

				lda #3
				>SYSCALL ArgV
				>PUSHYA

				>PUSHBI 4
				>SYSCALL PrintF

				lda #0
				clc
.9				rts
*--------------------------------------
CS.RUN.CheckTCPIP
				>LIBCALL hLIBTCPIP,LIBTCPIP.GETCFG	is TCPIP loaded ?
				bcs .9

				>STYA ZPIPCfgPtr
				lda (ZPIPCfgPtr)		Configured ?
				bmi .1

				>LDYA L.MSG.TCPIPERR
				>SYSCALL PutS

				lda #E.SYN
				sec
.9				rts

.1				lda #AF.INET
				>STA.G SA.LOCAL
				>STA.G SA.REMOTE

				ldy #S.IPCFG.IP+3

.3				lda (ZPIPCfgPtr),y
				pha
				dey
				cpy #S.IPCFG.IP-1
				bne .3

				ldy #SA.LOCAL+S.SOCKADDR.ADDR

.4				pla
				sta (pData),y
				iny
				cpy #SA.LOCAL+S.SOCKADDR.ADDR+4
				bne .4

				lda #TCP.PORT.CIFS
				>STA.G SA.REMOTE+S.SOCKADDR.PORT
				iny
				lda /TCP.PORT.CIFS
				sta (pData),y

				clc
				rts
*--------------------------------------
CS.RUN.CheckArgs
				ldy #S.PS.ARGC
				lda (pPS),y
				cmp #3
				bcs .1

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

.1				lda #TIMEOUT.MAX
				sta TimeOut

.2				>PUSHEA.G SA.REMOTE+S.SOCKADDR.ADDR
				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>LIBCALL hLIBTCPIP,LIBTCPIP.HST.GetByName
				bcc .3

				>SLEEP
				lda TimeOut
				bne .2

				>PUSHW L.MSG.UNKNOWN
				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI 2
				>SYSCALL PrintF
				lda #ERR.SKT.NOCONN
				sec
				rts

.3				lda #2
				>SYSCALL ArgV

				clc
CS.RUN.CheckArgs.RTS
				rts
*--------------------------------------
CS.RUN.Connect	>PUSHBI S.SOCKET.T.SEQPKT
				>PUSHBI 0				no protocol
				>LIBCALL hLIBTCPIP,LIBTCPIP.Socket
				bcs .9

				sta hSocket

				>PUSHA
				>PUSHEA.G SA.LOCAL
				>LIBCALL hLIBTCPIP,LIBTCPIP.Bind
				bcs .9

				lda #TIMEOUT.MAX
				sta TimeOut

.1				>SLEEP

				>PUSHB hSocket
				>PUSHEA.G SA.REMOTE
				>LIBCALL hLIBTCPIP,LIBTCPIP.Connect
				bcc .8

				ldx TimeOut
				bne .1

.9				pha

				>PUSHW L.MSG.NOCONN
				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI 2

				pla
				sec
				rts

.8				>PUSHW L.MSG.CONNECTED
				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI 2
				>SYSCALL PrintF
				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 hMount
				beq .1

				>SYSCALL UMount

.1				lda hSocket
				beq .2

				>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown

.2				lda hLIBCIFS
				beq .3

				>SYSCALL UnloadLib

.3				lda hLIBTCPIP
				beq .8

				>SYSCALL UnloadLib

.8				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
* Initialized DATA
*--------------------------------------
LIBTCPIP		.AZ "libtcpip"
hLIBTCPIP		.BS 1
LIBCIFS			.AZ "libcifs"
hLIBCIFS		.BS 1
MSG.USAGE		.AZ "Usage : CIFSD <ip|host> sharename mountpoint"
MSG.TCPIPERR	.AZ "CIFSD:TCP/IP Not initialized properly."
MSG.UNKNOWN		.AZ "CIFSD:%s: Unknown host\r\n"
MSG.NOCONN		.AZ "CIFSD:No Connection To %s\r\n"
MSG.CONNECTED	.AZ "CIFSD:Connected To %s\r\n"
MSG.MOUNTED		.AZ "CIFSD:%s Mounted As %s\r\n"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
SA.LOCAL		.BS 1					S.SOCKADDR.AF
				.BS 1
				.BS 4					S.SOCKADDR.ADDR
				.BS 2					S.SOCKADDR.PORT
SA.REMOTE		.BS 1					S.SOCKADDR.AF
				.BS 1
				.BS 4					S.SOCKADDR.ADDR
				.BS 2					S.SOCKADDR.PORT
DS.END
				.ED
*--------------------------------------
MAN
SAVE usr/src/sbin/cifsd.s
ASM
