NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/ipconfig
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/KERNEL.I
				.INB INC/MLI.I
				.INB INC/MLI.E.I
				.INB INC/NIC.I
				.INB INC/ETH.I
				.INB INC/LIB.NET.I
				.INB INC/LIBTCPIP.I
				.INB INC/NET.DHCP.I
*--------------------------------------
TIMEOUT.MAX		.EQ 200					20 sec.
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
pIPCFG			.BS 2
ZPFrameBase		.BS 2
ZPFramePtr		.BS 2
ZPTmpPtr1		.BS 2
ZPTmpPtr2		.BS 2
ZPTmpPtr3		.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 Segment Size
				.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.USAGE		.DA MSG.USAGE
L.MSG.NODEV		.DA MSG.NODEV
L.MSG1.DEV		.DA MSG1.DEV
L.MSG1.DEV.ARP	.DA MSG1.DEV.ARP
L.MSG1.DEV.IP	.DA MSG1.DEV.IP
L.MSG1.LINK.OK	.DA MSG1.LINK.OK
L.MSG1.LINK.KO	.DA MSG1.LINK.KO
L.MSG1.LINKSPEED .DA MSG1.LINKSPEED
L.MSG1.DPLX.FD	.DA MSG1.DPLX.FD
L.MSG1.DPLX.HD	.DA MSG1.DPLX.HD
L.MSG2			.DA MSG2
L.MSG2.C		.DA MSG2.C
L.MSG2.U		.DA MSG2.U
L.MSG2.DHCPSRVR	.DA MSG2.DHCPSRVR
L.MSG2.IPMASK	.DA MSG2.IPMASK
L.MSG2.GW		.DA MSG2.GW
L.MSG2.DNS		.DA MSG2.DNS
L.MSG2.HOSTNAME	.DA MSG2.HOSTNAME
L.MSG.CFG		.DA MSG.CFG
L.MSG.CFG.KO	.DA MSG.CFG.KO
L.MSG.CFG.OK	.DA MSG.CFG.OK
L.HOSTNAME		.DA HOSTNAME
L.TCPIP.CONF	.DA TCPIP.CONF
L.HOSTS			.DA HOSTS
L.IPCFG			.DA IPCFG
L.IPCFG.HOSTNAME	.DA IPCFG+S.IPCFG.HOSTNAME
L.IPCFG.DOMAIN	.DA IPCFG+S.IPCFG.DOMAIN
L.CFG.IP		.DA CFG.IP
L.CFG.IP.SScanF	.DA CFG.IP.SScanF
L.CFG.HOST.SScanF	.DA CFG.HOST.SScanF
L.CFG.DefaultHost	.DA CFG.DefaultHost
L.CFG.Keywords	.DA CFG.Keywords
J.CFG.Keywords	.DA CFG.Read.address
				.DA CFG.Read.netmask
				.DA CFG.Read.gateway
				.DA CFG.Read.nameserver
				.DA CFG.Read.dnsdomain
*--------------------------------------
* DHCP
*--------------------------------------
L.SA.LOCAL		.DA SA.LOCAL
L.SA.REMOTE		.DA SA.REMOTE
L.SA.DEST		.DA SA.DEST
L.MSG.DHCP.CFG1	.DA MSG.DHCP.CFG1
L.MSG.DHCP.CFG2	.DA MSG.DHCP.CFG2
L.MSG.DHCP.KO	.DA MSG.DHCP.KO
L.MSG.DHCP.OK	.DA MSG.DHCP.OK
L.MSG.DHCP.NA	.DA MSG.DHCP.NA
L.DHCP.DISC		.DA DHCP.DISC
L.DHCP.REQ		.DA DHCP.REQ
				.DA 0
*--------------------------------------
CS.INIT			>LDYA L.LIBTCPIP
				>SYSCALL LoadLib
				bcs .9
				sta hLIBTCPIP

.9				rts
*--------------------------------------
CS.RUN			>LIBCALL hLIBTCPIP,LIBTCPIP.GETCFG
				>STYA pIPCFG

				ldy #S.IPCFG-1
.1				lda (pIPCFG),y
				sta IPCFG,y
				dey
				bpl .1

				lda IPCFG+S.NETCFG.DevID
				bne CS.RUN.SETUP
				>LDYA L.MSG.NODEV
				>SYSCALL puts
				lda #MLI.E.NODEV
				sec
				rts
CS.RUN.SETUP	
.1				>INC.G ArgIndex
				>SYSCALL ArgV
				bcs .4

				>STYA ZPTmpPtr1
				lda (ZPTmpPtr1)
				cmp #'-'
				bne .99

				ldy #1 
				lda (ZPTmpPtr1),y

				ldx #OptionVars-OptionList-1
				
.2				cmp OptionList,x
				beq .3
				
				dex
				bpl .2

.99				>PUSHW L.MSG.USAGE
				>PUSHBI 0
				>SYSCALL printf
				lda #E.SYN
				sec
				rts

.3				ldy OptionVars,x
				lda #$80
				sta (pData),y
				bra .1
*--------------------------------------
.4				>LDA.G bSET
				bne .41					Force ...

				lda IPCFG+S.NETCFG.STATUS
				bne .5

.41				lda #$ff					no config, force all
				>STA.G bETC
				>STA.G bDHCP

.5				>LDA.G bETC
				>ORA.G bDHCP
				beq CS.RUN.DUMP

				>LDA.G bETC
				bpl .6
				jsr CFG.EtcFiles

.6				>LDA.G bDHCP
				bpl .8

				jsr DHCP.INIT
				bcs .8

				jsr DHCP.RUN

				jsr DHCP.QUIT

.8				lda #0
				sec
				rts
*--------------------------------------
CS.RUN.DUMP		>PUSHEA.G DIB
				lda IPCFG+S.NETCFG.DevID
				>SYSCALL GetDevStatus
				>LEA.G DCB.NIC
				>STYA.G IOCTL+S.IOCTL.BUFPTR
				lda #S.IOCTL.STATCODE.GETDCB
				>STA.G IOCTL+S.IOCTL.STATCODE

				>PUSHEA.G IOCTL
				>PUSHBI IOCTL.STATUS
				lda IPCFG+S.NETCFG.DevID
				>SYSCALL IOCTL

CS.RUN.DUMPNIC	>PUSHW L.MSG1.DEV

				lda IPCFG+S.NETCFG.DevID
				>PUSHA

				ldx IPCFG+S.NETCFG.DevID
				lda Dev.Table-1,x
				clc
				adc #S.FD.DEV
				tay
				lda Dev.Table,x
				adc /S.FD.DEV
				>PUSHYA

				>PUSHEA.G DIB+S.DIB.IDS 

				ldy #DCB.NIC+S.DCB.NIC.MAC
				ldx #6
.1				lda (pData),y
				>PUSHA
				iny
				dex 
				bne .1

				>PUSHBI 11
				
				>SYSCALL printf

				>PUSHW L.MSG1.DEV.ARP
				
				>LDA.G DCB.NIC+S.DCB.NIC.FLAGS
				and #S.DCB.NIC.FLAGS.ARPOFFLOAD
				beq .2
				lda #1
.2				>PUSHA
				>PUSHBI 1	
				
				>SYSCALL printf
				
				>PUSHW L.MSG1.DEV.IP
				
				>LDA.G DCB.NIC+S.DCB.NIC.FLAGS
				and #S.DCB.NIC.FLAGS.IPOFFLOAD

				beq .3

				lda #1
.3
				>PUSHA		
				>PUSHBI 1	
				
				>SYSCALL printf

				>LDA.G DCB.NIC+S.DCB.NIC.LINK
				and #S.DCB.NIC.LINK.OK
				bne CS.RUN.LINK
				>LDYA L.MSG1.LINK.KO
				>SYSCALL puts
				bra CS.RUN.DUMPIP

CS.RUN.LINK		>LDYA L.MSG1.LINK.OK
				>SYSCALL puts
				
				>PUSHW L.MSG1.LINKSPEED
				>PUSHBI 0
				>SYSCALL printf
				
				>LDA.G DCB.NIC+S.DCB.NIC.SPEED
				and #$0F
				tax
.4				phx
				lda #'0'
				>SYSCALL PutChar
				plx
				dex
				bne .4
				>LDA.G DCB.NIC+S.DCB.NIC.LINK
				and #S.DCB.NIC.LINK.FD
				bne .5
				>LDYA L.MSG1.DPLX.HD
				bra .6
.5				>LDYA L.MSG1.DPLX.FD
.6				>SYSCALL puts

CS.RUN.DUMPIP	>PUSHW L.MSG2
				>PUSHBI 0
				>SYSCALL printf

				lda IPCFG+S.NETCFG.STATUS
				and #S.IPCFG.STATUS.OK
				beq .11
				>LDYA L.MSG2.C
				bra .10
.11				>LDYA L.MSG2.U
.10				>SYSCALL puts

				>PUSHW L.MSG2.DHCPSRVR
				
				ldx #0
.2				>PUSHB IPCFG+S.IPCFG.DHCPSRVR,x
				inx
				cpx #4
				bne .2
				
				>PUSHBI 4
				
				>SYSCALL printf
				
				>PUSHW L.MSG2.IPMASK
				
				ldx #0					IP/MASK
.3				>PUSHB IPCFG+S.IPCFG.IP,x
				inx
				cpx #8
				bne .3
				>PUSHBI 8
				
				>SYSCALL printf
				
				>PUSHW L.MSG2.GW
				
				ldx #0
.4				>PUSHB IPCFG+S.IPCFG.GW,x
				inx
				cpx #4
				bne .4
				>PUSHBI 4
				>SYSCALL printf
				
				>PUSHW L.MSG2.DNS
				
				ldx #0
.5				>PUSHB IPCFG+S.IPCFG.DNS1,x
				inx
				cpx #8
				bne .5
				>PUSHBI 8
				
				>SYSCALL printf
				
				>PUSHW L.MSG2.HOSTNAME
				>PUSHW L.IPCFG.HOSTNAME
				>PUSHW L.IPCFG.DOMAIN
				
				>PUSHBI 4
				>SYSCALL printf
				lda #0					tell TSKMGR that all done ok, but 
				sec						we do not want to stay in memory
				rts
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9					is it a TIMER event?
				>LDA.G TimeOut
				beq .9
				dec
				sta (pData),y
.9				sec						do not discard TIMER event
				rts
*--------------------------------------
CS.QUIT			lda hLIBTCPIP
				beq .8
				>SYSCALL UnloadLib
.8				clc
				rts
*--------------------------------------
Init.Timeout	ldy #TimeOut
				lda #TIMEOUT.MAX
				sta (pData),y
				rts
*--------------------------------------
Wait.TimeOut	ldy #TimeOut
				lda (pData),y
				rts
*--------------------------------------
				.INB USR/SRC/SBIN/IPCONFIG.S.DHCP
				.INB USR/SRC/SBIN/IPCONFIG.S.ETC
*--------------------------------------
CS.END
LIBTCPIP		.AZ "libtcpip"
hLIBTCPIP		.BS	1
*--------------------------------------
OptionList		.AS "DdEeSs"
OptionVars		.DA #bDHCP,#bDHCP,#bETC,#bETC,#bSET,#bSET
*--------------------------------------
MSG.USAGE		.AS "Usage : IPCONFIG <switches>\r\n"
				.AS "   -D : Try to get IP address from DHCP\r\n"
				.AS "   -E : Read ETC files\r\n"
				.AZ "   -S : Set/Reset TCPIP configuration (-E, then -D if required)\r\n"
*--------------------------------------
MSG.NODEV		.AZ "TCP/IP not bound to any device."
*--------------------------------------
MSG1.DEV		.AS "NIC Device Configuration :\r\n"
				.AS	" Device ID   : $%h\r\n"
				.AS " Device Name : %s\r\n"
				.AS " Device Type : %S\r\n"
				.AZ " HW Address  : %02h:%02h:%02h:%02h:%02h:%02h\r\n"
MSG1.DEV.ARP	.AZ " ARP Offload : %d\r\n"
MSG1.DEV.IP		.AS " IP Offload  : %d\r\n"
				.AZ " Link Status : "
MSG1.LINK.OK	.AZ "OK"
MSG1.LINK.KO	.AZ "Media Disconnected"
MSG1.LINKSPEED	.AZ " Link Speed  : 1"
MSG1.DPLX.FD	.AZ " Mbit/s,Full Duplex"
MSG1.DPLX.HD	.AZ " Mbit/s,Half Duplex"
*--------------------------------------
MSG2			.AZ "IP Configuration : "
MSG2.C			.AZ "Configured"
MSG2.U			.AZ "Not Configured"
MSG2.DHCPSRVR	.AZ " DHCP Server : %d.%d.%d.%d\r\n"
MSG2.IPMASK		.AZ " IP/Mask     : %d.%d.%d.%d/%d.%d.%d.%d\r\n"
MSG2.GW			.AZ " Gateway     : %d.%d.%d.%d\r\n"
MSG2.DNS		.AZ " DNS         : %d.%d.%d.%d,%d.%d.%d.%d\r\n"
MSG2.HOSTNAME	.AZ " Hostname    : %s.%s\r\n"
*--------------------------------------
MSG.CFG			.AZ "IPCONFIG:Reading %s..."
MSG.CFG.KO		.AZ "Not Found. [%h]\r\n"
MSG.CFG.OK		.AZ "OK."
*--------------------------------------
HOSTNAME		.AZ "${ROOT}etc/hostname"
TCPIP.CONF		.AZ "${ROOT}etc/tcpip.conf"
HOSTS			.AZ "${ROOT}etc/hosts"
*--------------------------------------
CFG.DefaultHost	.AZ "a2osx-%H"
CFG.Keywords	>PSTR "address"
				>PSTR "netmask"
				>PSTR "gateway"
				>PSTR "nameserver"
				>PSTR "dnsdomain"
				.DA #0
*--------------------------------------
CFG.IP.SScanF	.AZ "%d.%d.%d.%d"
CFG.HOST.SScanF	.AZ "%d.%d.%d.%d %s %s"
*--------------------------------------
MSG.DHCP.CFG1	.AZ "IPCONFIG:Searching for DHCP servers..."
MSG.DHCP.CFG2	.AZ "IPCONFIG:Reply from DHCP Server at %d.%d.%d.%d, requesting...\r\n"
MSG.DHCP.KO		.AZ "IPCONFIG:Time Out [%h].\r\n"
MSG.DHCP.OK		.AZ "IPCONFIG:Success:[IP=%d.%d.%d.%d/%d.%d.%d.%d]\r\n"
MSG.DHCP.NA		.AZ "IPCONFIG:TCPIP Already configured, exiting."
*--------------------------------------
IPCFG			.BS S.IPCFG
CFG.hBuf1		.BS 1
CFG.hBuf2		.BS 1
CFG.hBuf3		.BS 1
CFG.hCfgFile	.BS 1
CFG.IP			.BS 4
*--------------------------------------
SA.LOCAL		.DA #AF.INET			S.SOCKADDR.AF
				.BS 1
				.HS 00.00.00.00			S.SOCKADDR.ADDR
				.DA UDP.PORT.DHCPC		S.SOCKADDR.PORT
*--------------------------------------
SA.REMOTE		.DA #AF.INET			S.SOCKADDR.AF
				.BS 1
				.HS 00.00.00.00			S.SOCKADDR.ADDR
				.DA UDP.PORT.DHCPS		S.SOCKADDR.PORT
*--------------------------------------
SA.DEST			.DA #AF.INET			S.SOCKADDR.AF
				.BS 1
				.HS FF.FF.FF.FF			S.SOCKADDR.ADDR
				.DA UDP.PORT.DHCPS		S.SOCKADDR.PORT
*--------------------------------------
DHCP.DISC		.HS 01010600			OP,HTYPE,HLEN,HOPS
DHCP.DISC.XID	.BS 4
				.HS 0000				SECS
				.DA S.DHCP.FLAGS.BRDCST
				.HS	00000000			CIADDR
DHCP.DISC.YIADDR	.HS 00000000
				.HS 00000000			SIADDR
DHCP.DISC.GIADDR	.HS 00000000			
DHCP.DISC.CHADDR	.HS 00000000.00000000.00000000.00000000
				.BS 64					SNAME
				.BS 128					FILE
				.HS 63825363			COOKIE
				.HS 3501				OPT
				.DA #S.DHCP.OPTIONS.DHCPDiscover
				.HS 37040103060FFF
DHCP.DISC.LEN	.EQ *-DHCP.DISC
*--------------------------------------
DHCP.REQ		.HS 01010600			OP,HTYPE,HLEN,HOPS
DHCP.REQ.XID	.BS 4
				.HS 0000				SECS
				.DA S.DHCP.FLAGS.BRDCST
				.HS	00000000			CIADDR
DHCP.REQ.YIADDR	.HS 00000000
				.HS 00000000			SIADDR
				.HS 00000000			GIADDR
DHCP.REQ.CHADDR	.HS 00000000.00000000.00000000.00000000
				.BS 64					SNAME
				.BS 128					FILE
				.HS 63825363			COOKIE
				.HS 3501				OPT
				.DA #S.DHCP.OPTIONS.DHCPRequest
				.HS 3204
DHCP.REQ.OPT.REQIP	.BS 4
				.HS 3604
DHCP.REQ.OPT.SVRIP	.BS 4
				.HS	FF
DHCP.REQ.LEN 	.EQ *-DHCP.REQ
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
ArgIndex		.BS 1
bDHCP			.BS 1
bETC			.BS 1
bSET			.BS 1
DIB				.BS S.DIB
IOCTL			.BS S.IOCTL
DCB.NIC			.BS S.DCB.NIC
TimeOut			.BS 1
hSocket			.BS 1
hFrame			.BS 1
DS.END			.ED
*--------------------------------------
MAN
SAVE USR/SRC/SBIN/IPCONFIG.S
ASM
