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
*--------------------------------------
AUTH			.EQ 0
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
				.INB inc/mli.e.i
				.INB inc/eth.i
				.INB inc/libtcpip.i
				.INB inc/net.tcpip.i
				.INB inc/net.smb.i
				.DO AUTH
				.INB inc/libcrypt.i
				.FIN
*--------------------------------------
TIMEOUT.MAX		.EQ 250					25 sec.
*--------------------------------------
* Zero Page Segment, up to 32 bytes
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPCtxPtr		.BS 2
ZPReqPtr		.BS 2
ZPReqLen		.BS 2
ZPRespPtr		.BS 2

ZPPtr1			.BS 2
ZPPtr2			.BS 2
pPath			.BS 2
pFD				.BS 2

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
				.DO AUTH
L.LIBCRYPT		.DA LIBCRYPT
				.FIN
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
*--------------------------------------
L.NETBIOS.H		.DA NETBIOS.H
L.IO.CallBack	.DA IO.CallBack
*--------------------------------------
J.IO.Q			.DA A2osX.BADCALL
				.DA IO.Q.ChMod
				.DA IO.Q.FStat
				.DA IO.Q.Stat
				.DA IO.Q.MKDir
				.DA A2osX.BADCALL		MKNod
				.DA A2osX.BADCALL		MKFIFO
				.DA A2osX.BADCALL		Pipe

				.DA IO.Q.OpenDir
				.DA IO.Q.ReadDir
				.DA IO.Q.CloseDir

				.DA A2osX.BADCALL		Open
				.DA A2osX.BADCALL		Close
				.DA A2osX.BADCALL		Read
				.DA A2osX.BADCALL		Write
				.DA IO.Q.ChOwn

				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL

				.DA IO.Q.FOpen
				.DA IO.Q.FClose
				.DA IO.Q.FRead
				.DA IO.Q.FWrite
				.DA IO.Q.FFlush
				.DA IO.Q.FSeek
				.DA IO.Q.FTell
				.DA IO.Q.FEOF

				.DA IO.Q.Remove
				.DA IO.Q.Rename
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL

				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA IO.Q.ChTyp
				.DA IO.Q.ChAux
				.DA IO.Q.SetAttr
*--------------------------------------
J.IO.R			.DA A2osX.BADCALL
				.DA IO.R.ChMod
				.DA IO.R.FStat
				.DA IO.R.Stat
				.DA IO.R.MKDir
				.DA A2osX.BADCALL		MKNod
				.DA A2osX.BADCALL		MKFIFO
				.DA A2osX.BADCALL		Pipe

				.DA A2osX.BADCALL		IO.R.OpenDir
				.DA IO.R.ReadDir
				.DA A2osX.BADCALL		IO.R.CloseDir

				.DA A2osX.BADCALL		Open
				.DA A2osX.BADCALL		Close
				.DA A2osX.BADCALL		Read
				.DA A2osX.BADCALL		Write
				.DA IO.R.ChOwn

				.DA A2osX.BADCALL		FPutC...
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL		...UngetC

				.DA IO.R.FOpen
				.DA IO.R.FClose
				.DA IO.R.FRead
				.DA IO.R.FWrite
				.DA IO.R.FFlush
				.DA A2osX.BADCALL		IO.R.FSeek
				.DA A2osX.BADCALL		IO.R.FTell
				.DA IO.R.FEOF

				.DA IO.R.Remove
				.DA IO.R.Rename
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL

				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA A2osX.BADCALL
				.DA IO.R.ChTyp
				.DA IO.R.ChAux
				.DA IO.R.SetAttr

				.DA 0
*--------------------------------------
* Called once at process creation
* Put code for loading LIB here
*--------------------------------------
CS.INIT			>LDYA L.LIBTCPIP
				>SYSCALL LoadLib
				bcs .9

				sta hLIBTCPIP
				>SYSCALL2 GetMemPtr
				>STYA pLIBTCPIP

				.DO AUTH
				>LDYA L.LIBCRYPT
				>SYSCALL LoadLib
				bcs .9

				sta hLIBCRYPT
				.FIN
*				clc
.9				rts
*--------------------------------------
* Called until exit with CS
* if RUN exits with CC, RUN entered again
*--------------------------------------
CS.RUN			lda hSocket
				bne .8

				jsr CS.RUN.CheckTCPIP
				bcs .9

				jsr CS.RUN.CheckArgs
				bcs .9

				jsr CS.RUN.Connect
				bcs .9

				jsr INIT.Setup
				bcs .9

				>PUSHW L.MSG.MOUNTED

				lda #2
				>SYSCALL ArgV
				>PUSHYA

				lda #3
				>SYSCALL ArgV
				>PUSHYA

				>PUSHBI 4
				>SYSCALL PrintF

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

				>STYA ZPPtr1
				lda (ZPPtr1)		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 (ZPPtr1),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 #4
				>SYSCALL ArgV
				bcc .4



.4

.8				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 hSMBContext
				beq .3

				>SYSCALL FreeMem

.3				.DO AUTH
				lda hLIBCRYPT
				jsr .7
				.FIN

				lda hLIBTCPIP
.7				beq .8

				>SYSCALL UnloadLib

.8				clc
				rts
*--------------------------------------
				.INB usr/src/sbin/cifsd.s.init
				.INB usr/src/sbin/cifsd.s.core
				.INB usr/src/sbin/cifsd.s.io
*--------------------------------------
CS.END
*--------------------------------------
* Initialized DATA
*--------------------------------------
LIBTCPIP		.AZ "libtcpip"
hLIBTCPIP		.BS 1
pLIBTCPIP		.BS 2
				.DO AUTH
LIBCRYPT		.AZ "libcrypt"
hLIBCRYPT		.BS 1
				.FIN
*--------------------------------------
MSG.USAGE		.AZ "Usage : CIFSD <ip|host> sharename mountpoint [[domain\]user]"
MSG.TCPIPERR	.CZ "CIFSD:TCP/IP Not initialized properly."
MSG.UNKNOWN		.CZ "CIFSD:%s: Unknown host\r\n"
MSG.NOCONN		.CZ "CIFSD:No Connection To %s\r\n"
MSG.CONNECTED	.CZ "CIFSD:Connected To %s\r\n"
MSG.MOUNTED		.CZ "CIFSD:%s Mounted As %s\r\n"
*--------------------------------------
PRODOS.FT.HEX	.HS 0406FAFCFDCBCCCFFF
PRODOS.FT.TXT	.AZ "txt"
				.AZ "bin"
				.AZ "asm"
				.AZ "bas"
				.AZ "var"
				.AZ "pix"
				.AZ "fon"
				.AZ "pak"
				.AZ "sys"
PRODOS.FT.END	.EQ *-PRODOS.FT.TXT
*--------------------------------------
SMBERR01		.DA #2,#3,#4,#5,#12
				.DA #32
MLIERR01		.DA #MLI.E.FNOTFND,#MLI.E.PNOTFND,#MLI.E.FCBFULL,#MLI.E.WRTPROT,#MLI.E.WRTPROT
				.DA #MLI.E.OPEN
*--------------------------------------
NETBIOS.H		.DA #0					DIRECT TCP TRANSPORT
				.DA #0					24 BITS LENGTH
				.DA /SMB.NegReq.end-SMB.H
				.DA #SMB.NegReq.end-SMB.H
*--------------------------------------
SMB.H			.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 $FFFE				PIDLO
				.BS 2					UID
				.DA 0					MID
*--------------------------------------
SMB.NegReq		.DA #0					WORD COUNT

				.DA #SMB.NegReq.end-2-*	ByteCount
				.DA #0

*				.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.SessionSetup.H
				.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
			.BS 2						CI PASS LEN (ANSI)
				.DA 0					CS PASS LEN (UNICODE)
				.HS 00000000
				.HS 00000000			CAPABILITIES
SMB.SessionSetup.H.Len .EQ *-SMB.SessionSetup.H
*				.BS 2					ByteCount
SMB.SessionSetup.Guest
*				.AZ ""					Password
				.AZ "Guest"				Account Name
				.AZ "?"					Primary Domain
SMB.SessionSetup.Guest.Len	.EQ *-SMB.SessionSetup.Guest
SMB.SessionSetup.T
				.AZ "A2osX"				Native OS
				.AZ	"A2osX-CIFSD"		Native LANMAN
SMB.SessionSetup.T.Len		.EQ *-SMB.SessionSetup.T
*--------------------------------------
SMB.TreeConnect.H
				.DA #4					WORD COUNT
				.HS FF000000			NO MORE CMD
				.DA 0
				.DA 1					PASSWORD LEN (1 for "\0")
SMB.TreeConnect.H.Len	.EQ *-SMB.TreeConnect.H
*				.BS 2					ByteCount
*				.AZ ""					PASSWORD
*				.AZ	"_temp"				PATH
SMB.TreeConnect.T
				.AZ "A:"				Disk Share
SMB.TreeConnect.T.Len	.EQ *-SMB.TreeConnect.T
*--------------------------------------
SMB.ComCreate.H	.DA #24					WORD COUNT
				.HS FF000000			NO MORE CMD
				.HS 00					Reserved
			.BS 2						NameLength
				.HS 00000000			Flags
				.HS 00000000			RootDirectoryFID
				.HS 000000C0			DesiredAccess = GENERIC_READ+GENERIC_WRITE
				.HS 0000000000000000	AllocationSize
				.HS 00000000			ExtFileAttributes
				.HS 00000000			ShareAccess
			.BS 4						CreateDisposition
				.HS 00000000			CreateOptions
				.HS 00000000			ImpersonationLevel
				.HS	00					SecurityFlags
SMB.ComCreate.H.Len .EQ *-SMB.ComCreate.H
*				.BS 2					ByteCount
*				.AZ "path\file"
*--------------------------------------
SMB.ComClose.H	.DA #3					WORD COUNT
			.BS 2						FID
				.HS 00000000			LastTimeModified
				.HS 0000				ByteCount
SMB.ComClose.H.Len	.EQ *-SMB.ComClose.H
*--------------------------------------
SMB.ComFlush.H	.DA #1					WORD COUNT
			.BS 2						FID
				.HS 0000				ByteCount
SMB.ComFlush.H.Len	.EQ *-SMB.ComFlush.H
*--------------------------------------
SMB.ComDelete.H	.DA #1					WORD COUNT
				.HS 0000				SearchAttributes
SMB.ComDelete.H.Len	.EQ *-SMB.ComDelete.H
*			.BS 2						ByteCount
*			.HS 04						BufferFormat
*			.AZ "path\file"
*--------------------------------------
SMB.ComRename.H	.DA #1					WORD COUNT
				.HS 0000				SearchAttributes
SMB.ComRename.H.Len	.EQ *-SMB.ComRename.H
*			.BS 2						ByteCount
*			.HS 04						BufferFormat
*			.AZ "oldpath"
*			.HS 04						BufferFormat
*			.AZ "newpath"
*--------------------------------------
SMB.ComRead.H	.DA #10					WORD COUNT
				.HS FF000000			NO MORE CMD
			.BS 2						FID
			.BS 4						Offset
			.BS 2						MaxCountOfBytesToReturn
				.HS 0000				MinCountOfBytesToReturn
				.HS 00000000			Timeout
				.HS 0000				Remaining
SMB.ComRead.H.Len	.EQ *-SMB.ComRead.H
*			.BS 2						ByteCount
*--------------------------------------
SMB.ComWrite.H	.DA #12					WORD COUNT
				.HS FF000000			NO MORE CMD
			.BS 2						FID
			.BS 4						Offset
				.HS 00000000			Timeout
				.HS 0000
*				.HS 0100				WriteMode=WritethroughMode
				.HS 0000				Remaining
				.HS 0000				Reserved
			.BS 2						DataLength
				.DA S.SMB.H+SMB.ComWrite.H.Len+2	DataOffset (+2 skip ByteCount)
SMB.ComWrite.H.Len	.EQ *-SMB.ComWrite.H
*			.BS 2						ByteCount
*			DATA.....
*--------------------------------------
SMB.Trans2.H	.DA #15					WORD COUNT
			.BS 2					TotalParamCount
				.DA 0					TotalDataCount
				.DA 10					MaxParameterCount
				.DA 450					MaxDataCount
				.DA #0					MaxSetupCount
				.HS 00					Reserved1
				.DA 0					Flags
				.HS 00000000			Timeout
				.HS 0000				Reserved2
			.BS 2					ParameterCount
				.DA 68					ParameterOffset
				.HS 0000				DataCount
				.HS 0000				DataOffset
				.DA #1					SetupCount
				.HS 00					Reserved3
SMB.Trans2.H.Len	.EQ *-SMB.Trans2.H
*--------------------------------------
SMB.FindFirst2	.DA 1					TRANS2_FIND_FIRST2
			.BS 2						PrmCnt
				.HS 000000				Padding
				.DA $0037				SearchAttributes
				.DA 1024				SearchCount
				.DA 2					Flags
				.DA SMB_INFO_STANDARD	InformationLevel
				.HS 00000000			SearchStorageType
*				.AZ "path\*"
SMB.FindFirst2.Len .EQ *-SMB.FindFirst2
*--------------------------------------
SMB.FindNext2	.DA 2					TRANS2_FIND_NEXT2
			.BS 2						PrmCnt
				.HS 000000				Padding
			.BS 2						SID
				.DA 1024				SearchCount
				.DA SMB_INFO_STANDARD	InformationLevel
				.HS	00000000			ResumeKey
				.DA 2+8					Flags
*				.AZ "path\*"
SMB.FindNext2.Len .EQ *-SMB.FindNext2
*--------------------------------------
SMB.QueryPathInfo
				.DA 5					TRANS2_QUERY_PATH_INFORMATION
			.BS 2						PrmCnt
				.HS 000000				Padding
				.DA SMB_INFO_STANDARD	InformationLevel
				.HS 00000000			SearchStorageType
*				.AZ "filename"
SMB.QueryPathInfo.Len	.EQ *-SMB.QueryPathInfo
*--------------------------------------
SMB.CreateDirectory
				.DA 13					TRANS2_CREATE_DIRECTORY
			.BS 2						PrmCnt
				.HS 000000				Padding
				.HS 00000000			Reserved
*				.AZ "DirName"
SMB.CreateDirectory.Len	.EQ *-SMB.CreateDirectory
*--------------------------------------
CIFS.Status		.BS 1
CIFS.PID		.BS 1
CIFS.Cnt		.BS 2
*--------------------------------------
hSocket			.BS 1
hSMBContext		.BS 1
hMount			.BS 1
hReq			.BS 1
hResp			.BS 1
hBuf			.BS 1
hFD				.BS 1
hFILE			.BS 1

TimeOut			.BS 1
bFlag			.BS 1
ByteCountOfs	.BS 1

StatBuf			.BS S.STAT
SaveYA			.BS 2
SaveZP			.BS ZS.END-ZS.START
*--------------------------------------
				.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
