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
*--------------------------------------
TIMEOUT.MAX		.EQ 100					20 sec.
*--------------------------------------
* Zero Page Segment, up to 32 bytes
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPIPCfgPtr		.BS 2
hResp			.BS 1
ZPRespPtr		.BS 2
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
				.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.MSG.TCPIPERR	.DA MSG.TCPIPERR
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.UNKNOWN	.DA MSG.UNKNOWN
L.MSG.SESSION	.DA MSG.SESSION
L.SMB.NegReq	.DA SMB.NegReq
L.SMB.SessSetup	.DA SMB.SessSetup
L.SMB.TreeConnReq	.DA SMB.TreeConnReq
L.SMB.FindFirstReq	.DA SMB.FindFirstReq
				.DA 0
*--------------------------------------
* Called once at process creation
* Put code for loading LIB here
*--------------------------------------
CS.INIT			>LDYA L.LIBTCPIP
				>SYSCALL LoadLib
				bcs .9
				sta hLIBTCPIP
				
				clc
.9				rts
*--------------------------------------
* Called until exit with CS
* if RUN exits with CC, RUN entered again
*--------------------------------------
CS.RUN			>LDA.G hSocket
				bne .8

				>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
				rts

.1				jsr CS.RUN.CheckArgs
				bcs .90

				jsr CS.RUN.Connect

				>SLEEP

				jsr CS.RUN.Negotiate
				bcs .9
				
				>SLEEP
				
				jsr CS.RUN.SessSetup
				bcs .9
				
				>SLEEP
				
				jsr CS.RUN.TreeConn
				bcs .9
				
				>SLEEP
				
				jsr CIFS.OpenDir
				bcs .9
				
	
.8				lda #0
				clc
.9				rts

.90				>LDYA L.MSG.USAGE
				>SYSCALL PutS
				lda #E.SYN
				sec
				rts
*--------------------------------------
CS.RUN.CheckArgs
				ldy #S.PS.ARGC
				lda (pPS),y
				cmp #3
				bcc .9

				lda #TIMEOUT.MAX
				sta TimeOut
				
.1				>PUSHEA.G SA.REMOTE+S.SOCKADDR.ADDR
				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>LIBCALL hLIBTCPIP,LIBTCPIP.HST.GETBYNAME
				bcc .2
				
				>SLEEP
				lda TimeOut
				bne .1
				
				>PUSHW L.MSG.UNKNOWN
				lda #1
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI 2
				>SYSCALL PrintF
				sec
.9				rts

.2				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.REMOTE+S.SOCKADDR.PORT
.4				pla
				sta (pData),y
				iny
				cpy #SA.REMOTE+S.SOCKADDR.PORT+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.Connect	>PUSHBI 0				no protocol
				lda #S.SOCKET.T.SEQPKT
				>LIBCALL hLIBTCPIP,LIBTCPIP.Socket
				bcs .9

				>STA.G hSocket

				>PUSHEA.G SA.LOCAL
				>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Bind
				bcs .9
				
				>PUSHEA.G SA.REMOTE
				>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Connect
.9				rts
*--------------------------------------
CS.RUN.Negotiate
				lda #TIMEOUT.MAX
				sta TimeOut

.1				>PUSHWI SMB.NegReq.end-SMB.NegReq
				>PUSHW L.SMB.NegReq
				>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Send
				bcc .2
				
				>SLEEP
				ldx TimeOut
				bne .1
				
				bra .99
		
.2				>SLEEP

				>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Recv
				bcc .3

				tay
				beq .2

				bra .99

.3				sta hResp
				>SYSCALL GetMemPtr
				>STYA ZPRespPtr
				
				ldy #$3f
				lda (ZPRespPtr),y
				bne .9
				
				ldy #$5B
				lda (ZPRespPtr),y
				cmp #5
				bne .9
				
				
				lda hResp
				>SYSCALL FreeMem
*				clc
				rts
				
.9				lda hResp
				>SYSCALL FreeMem
				
.99				lda #MLI.E.INVPATH
				sec
				rts
*--------------------------------------
CS.RUN.SessSetup
				lda #TIMEOUT.MAX
				sta TimeOut

.1				>PUSHWI SMB.SessSetup.end-SMB.SessSetup
				>PUSHW L.SMB.SessSetup
				>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Send
				bcc .2
				
				>SLEEP
				ldx TimeOut
				bne .1
				
				bra .99
		
.2				>SLEEP

				>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Recv
				bcc .3

				tay
				beq .2

				bra .99

.3				sta hResp
				>SYSCALL GetMemPtr
				>STYA ZPRespPtr
				
				ldy #$3f
				lda (ZPRespPtr),y
				bne .9

				ldy #$56
				lda (ZPRespPtr),y
				sta SMB.TreeConnReq.UID
				iny
				lda (ZPRespPtr),y
				sta SMB.TreeConnReq.UID+1
				
				>PUSHW L.MSG.SESSION
				lda #$63
				clc
				adc ZPRespPtr
				tay
				lda #0
				adc ZPRespPtr+1
				>PUSHYA
				
				>PUSHBI 2
				>SYSCALL Printf
				
				lda hResp
				>SYSCALL FreeMem
*				clc
				rts
				
.9				lda hResp
				>SYSCALL FreeMem
				
.99				lda #MLI.E.INVPATH
				sec
				rts
*--------------------------------------
CS.RUN.TreeConn	lda #TIMEOUT.MAX
				sta TimeOut

.1				>PUSHWI SMB.TreeConnReq.end-SMB.TreeConnReq
				>PUSHW L.SMB.TreeConnReq
				>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Send
				bcc .2
	
				>SLEEP
				ldx TimeOut
				bne .1
				
				bra .99
		
.2				>SLEEP

				>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Recv
				bcc .3

				tay
				beq .2

				bra .99

.3				sta hResp
				>SYSCALL GetMemPtr
				>STYA ZPRespPtr
				
				ldy #$3f
				lda (ZPRespPtr),y
				bne .9
				
				ldy #$52
				lda (ZPRespPtr),y
				sta SMB.FindFirstReq.TID
				iny
				lda (ZPRespPtr),y
				sta SMB.FindFirstReq.TID+1
				
				ldy #$56
				lda (ZPRespPtr),y
				sta SMB.FindFirstReq.UID
				iny
				lda (ZPRespPtr),y
				sta SMB.FindFirstReq.UID+1
				
				lda hResp
				>SYSCALL FreeMem
*				clc
				rts
				
.9				lda hResp
				>SYSCALL FreeMem
				
.99				lda #MLI.E.INVPATH
				sec
				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
*--------------------------------------
* Called once, when RUN exited with CS
* Put code for unloading LIB here
*--------------------------------------
CS.QUIT			>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown
				
				lda hLIBTCPIP
				beq .8
				>SYSCALL UnloadLib

.8				clc
				rts
*--------------------------------------
CIFS.OpenDir	lda #TIMEOUT.MAX
				sta TimeOut

.1				>PUSHWI SMB.FindFirstReq.end-SMB.FindFirstReq
				>PUSHW L.SMB.FindFirstReq
				>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Send
				bcc .2
	
				>SLEEP
				ldx TimeOut
				bne .1
				
				bra .99
		
.2				>SLEEP

				>LDA.G hSocket
				>LIBCALL hLIBTCPIP,LIBTCPIP.Recv
				bcc .3

				tay
				beq .2

				bra .99

.3				sta hResp
				>SYSCALL GetMemPtr
				>STYA ZPRespPtr
				
				ldy #$3f
				lda (ZPRespPtr),y
				bne .9
				
				lda hResp
				>SYSCALL FreeMem
*				clc
				rts
				
.9				lda hResp
				>SYSCALL FreeMem
				
.99				lda #MLI.E.INVPATH
				sec
				rts
*--------------------------------------
CIFS.ReadDir
*--------------------------------------
CIFS.CloseDir
				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
* Initialized DATA
*--------------------------------------
LIBTCPIP		.AZ "libtcpip"
hLIBTCPIP		.BS 1
MSG.TCPIPERR	.AZ "HTTPD:TCP/IP Not initialized properly."
MSG.USAGE		.AZ "Usage : CIFSD <ip|host> sharename mountpoint"
MSG.UNKNOWN		.AZ "%s: Unknown host\r\n"
MSG.SESSION		.AZ "Connected to: %s\r\n"
*--------------------------------------
SMB.NegReq		.DA #0					DIRECT TCP TRANSPORT
				.DA #0					24 BITS LENGTH
				.DA /SMB.NegReq.end-1-*
				.DA #SMB.NegReq.end-1-*
				.DA #$FF
				.AS "SMB"
				.DA #S.SMB.H.CMD.NEGOTIATE
				.HS 00000000
				.DA #0					S.SMB.H.FLAGS
				.DA S.SMB.H.FLAGS2.LONGNAMES
				.DA 0					PIDHI
				.HS 0000000000000000	SECFEAT
				.DA 0					RSVD
				.DA $FFFF				TID
				.DA $FEFF				PIDLO
				.DA 0					UID
				.DA 0					MID

				.DA #0					WORD COUNT

				.DA #SMB.NegReq.end-2-*
				.DA /SMB.NegReq.end-2-*

				.DA #2
				.AZ "PC NETWORK PROGRAM 1.0"
				.DA #2
				.AZ "LANMAN1.0"
				.DA #2
				.AZ "Windows for Workgroups 3.1a"
				.DA #2
				.AZ "LM1.2X002"
				.DA #2
				.AZ "LANMAN2.1"
				.DA #2
				.AZ "NT LM 0.12"
				.DA #2
				.AZ "SMB 2.002"
				.DA #2
				.AZ "SMB 2.???"
SMB.NegReq.end	.EQ *
*--------------------------------------
SMB.SessSetup	.DA #0					DIRECT TCP TRANSPORT
				.DA #0					24 BITS LENGTH
				.DA /SMB.SessSetup.end-1-*
				.DA #SMB.SessSetup.end-1-*
				.DA #$FF
				.AS "SMB"
				.DA #S.SMB.H.CMD.SESSION.SETUP.ANDX
				.HS 00000000
				.DA #0					S.SMB.H.FLAGS
				.DA S.SMB.H.FLAGS2.LONGNAMES
				.DA 0					PIDHI
				.HS 0000000000000000	SECFEAT
				.DA 0					RSVD
				.DA $FFFF				TID
				.DA $FEFF				PIDLO
				.DA 0					UID
				.DA 0					MID

				.DA #13					WORD COUNT
				
				.HS FF000000			NO MORE CMD
				.DA 1280				MAX BUFFER
				.DA 2					MAX MPX COUNT
				.HS 0000				VC NUMBER
				.HS 00000000			SESSION KEY
				.DA 0					CI PASS LEN
				.DA 0					CS PASS LEN
				.HS 00000000
				.HS 00000000			CAPABILITIES

				.DA #SMB.SessSetup.end-2-*
				.DA /SMB.SessSetup.end-2-*

SMB.SessSetup.SecBlob
*				.AZ ""					Password
				.AZ "GUEST"				Account Name
				.AZ "?"					Primary Domain
SMB.SessSetup.SecBlob.LEN .EQ *-SMB.SessSetup.SecBlob
				.AZ "A2osX"				Native OS
				.AZ	"A2osX-CIFSD"		Native LANMAN
SMB.SessSetup.end	.EQ *
*--------------------------------------
SMB.TreeConnReq	.DA #0					DIRECT TCP TRANSPORT
				.DA #0					24 BITS LENGTH
				.DA /SMB.TreeConnReq.end-1-*
				.DA #SMB.TreeConnReq.end-1-*
				.DA #$FF
				.AS "SMB"
				.DA #S.SMB.H.CMD.TREE.CONNECT.ANDX
				.HS 00000000
				.DA #0					S.SMB.H.FLAGS
				.DA S.SMB.H.FLAGS2.LONGNAMES
				.DA 0					PIDHI
				.HS 0000000000000000	SECFEAT
				.DA 0					RSVD
SMB.TreeConnReq.TID
				.DA $FFFF				TID
				.DA $FEFF				PIDLO
SMB.TreeConnReq.UID
				.DA 0					UID
				.DA 0					MID
				
				.DA #4					WORD COUNT
				.HS FF000000			NO MORE CMD
				.DA 0 
				.DA 1					PASSWORD LEN
				
				.DA #SMB.TreeConnReq.end-2-*
				.DA /SMB.TreeConnReq.end-2-*

				.AZ ""					PASSWORD
				.AZ	"\\SV-W2K-3\Apple"	PATH
				.AZ "?????"
SMB.TreeConnReq.end	.EQ *
*--------------------------------------
SMB.FindFirstReq
				.DA #0					DIRECT TCP TRANSPORT
				.DA #0					24 BITS LENGTH
				.DA /SMB.TreeConnReq.end-1-*
				.DA #SMB.TreeConnReq.end-1-*
				.DA #$FF
				.AS "SMB"
				.DA #S.SMB.H.CMD.TRANSACTION2
				.HS 00000000
				.DA #0					S.SMB.H.FLAGS
				.DA S.SMB.H.FLAGS2.LONGNAMES
				.DA 0					PIDHI
				.HS 0000000000000000	SECFEAT
				.DA 0					RSVD
SMB.FindFirstReq.TID
				.DA $FFFF				TID
				.DA $FEFF				PIDLO
SMB.FindFirstReq.UID
				.DA 0					UID
				.DA 0					MID				
				
				.DA #$0F				WORD COUNT
				.DA 0					TotalParamCount
				.DA 0					TotalDataCount
				.DA 256					MaxParameterCount
				.DA 1024				MaxDataCount
				.DA #1					MaxSetupCount
				.HS 00					Reserved1
				.DA 0					Flags
				.HS 00000000			Timeout
				.HS 0000				Reserved2
				.DA 18					ParameterCount
				.DA 68					ParameterOffset
				.HS 0000				DataCount
				.HS 0000				DataOffset

				.DA #1					SetupCount
				.HS 00					Reserved3
				
				.DA 1					TRANS2_FIND_FIRST2

				.DA SMB.FindFirstReq.end-2-*

				.HS 000000				Padding

				.DA $373E				SearchAttributes
				.DA 1024				SearchCount
				.DA 0					Flags
				.DA $0104				InformationLevel
				.HS 00000000			SearchStorageType
				.DA "\*"				FileName
SMB.FindFirstReq.end
*--------------------------------------
				.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
hSocket			.BS 1
DS.END
				.ED
*--------------------------------------
MAN
SAVE usr/src/sbin/cifsd
ASM
