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.e.i
				.INB inc/nic.i
				.INB inc/eth.i
				.INB inc/lib.net.i
				.INB inc/libtcpip.i
				.INB inc/net.tcpip.i
				.INB inc/net.dhcp.i
*--------------------------------------
RETRY.MAX		.EQ 3
TIMEOUT.MAX		.EQ 50					5 sec.
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
pIPCFG			.BS 2
ZPTmpPtr1		.BS 2
ZPTmpPtr2		.BS 2
ZPTmpPtr3		.BS 2

ArgIndex		.BS 1
TimeOut			.BS 1
hSocket			.BS 1
hFrame			.BS 1
ZPFrameBase		.BS 2
ZPFramePtr		.BS 2

RetryCount		.BS 1

bDHCP			.BS 1
bETC			.BS 1
bSET			.BS 1

CFG.hBuf1		.BS 1
CFG.hBuf2		.BS 1
CFG.hBuf3		.BS 1
CFG.hCfgFile	.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 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 ArgIndex
				lda 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 #$ff
				sta $0,y
				bra .1
*--------------------------------------
.4				lda bSET
				bne .41					Force ...

				lda IPCFG+S.NETCFG.STATUS
				bne .5

.41				lda #$ff					no config, force all
				sta bETC
				sta bDHCP

.5				lda bETC
				ora bDHCP
				beq CS.RUN.DUMP

				lda bETC
				bpl .6

				jsr ETC.ReadFiles

.6				lda bDHCP
				bpl .80

				jsr DHCP.INIT
				bcs CS.RUN.SETUP.RTS

				lda #RETRY.MAX
				sta RetryCount

.7				jsr DHCP.RUN
				bcc .8

				dec RetryCount
				bne .7

				pha
				jsr DHCP.QUIT
				pla
				sec
				rts

.8				jsr DHCP.QUIT

.80				lda #0
				sec

CS.RUN.SETUP.RTS
				rts
*--------------------------------------
CS.RUN.DUMP		>LEA.G DCB.NIC

				ldx #S.IOCTL.S.GETDCB
				jsr CS.RUN.IOCTL
				bcs CS.RUN.SETUP.RTS

				>LEA.G DIB

				ldx #S.IOCTL.S.GETDIB
				jsr CS.RUN.IOCTL
				bcs CS.RUN.SETUP.RTS

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

				lda IPCFG+S.NETCFG.DevID
				>PUSHA

				tax
				lda FDs.pLO-1,x
				clc
				adc #S.FD.DEV
				tay
				lda FDs.pHI-1,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.RUN.IOCTL	>STYA.G DEV.IOCTL+S.IOCTL.BUFPTR

				txa
				>STA.G DEV.IOCTL+S.IOCTL.S

				>PUSHB IPCFG+S.NETCFG.DevID
				>PUSHBI IOCTL.STATUS
				>PUSHEA.G DEV.IOCTL
				>SYSCALL IOCTL
				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 hLIBTCPIP
				beq .8

				>SYSCALL UnloadLib

.8				clc
				rts
*--------------------------------------
				.INB usr/src/bin/ipconfig.s.dhcp
				.INB usr/src/bin/ipconfig.s.etc
*--------------------------------------
CS.END
LIBTCPIP		.AZ "libtcpip"
hLIBTCPIP		.BS	1
*--------------------------------------
OptionList		.AS "DdEeSs"
OptionVars		.DA #bDHCP,#bDHCP,#bETC,#bETC,#bSET,#bSET
*--------------------------------------
MSG.USAGE		.CS "Usage : IPCONFIG <switches>\r\n"
				.CS "   -D : Try to get IP address from DHCP\r\n"
				.CS "   -E : Read ETC files\r\n"
				.CZ "   -S : Set/Reset TCPIP configuration (-E, then -D if required)\r\n"
*--------------------------------------
MSG.NODEV		.AZ "TCP/IP not bound to any device."
*--------------------------------------
MSG1.DEV		.CS "NIC Device Configuration :\r\n"
				.CS	" Device ID   : %d\r\n"
				.CS " Device Name : %s\r\n"
				.CS " Device Type : %S\r\n"
				.CZ " HW Address  : %02h:%02h:%02h:%02h:%02h:%02h\r\n"
MSG1.DEV.ARP	.CZ " ARP Offload : %d\r\n"
MSG1.DEV.IP		.CS " IP Offload  : %d\r\n"
				.CZ " Link Status : "
MSG1.LINK.OK	.CZ "OK"
MSG1.LINK.KO	.CZ "Media Disconnected"
MSG1.LINKSPEED	.CZ " Link Speed  : 1"
MSG1.DPLX.FD	.CZ " Mbit/s,Full Duplex"
MSG1.DPLX.HD	.CZ " Mbit/s,Half Duplex"
*--------------------------------------
MSG2			.CZ "IP Configuration : "
MSG2.C			.CZ "Configured"
MSG2.U			.CZ "Not Configured"
MSG2.DHCPSRVR	.CZ " DHCP Server : %d.%d.%d.%d\r\n"
MSG2.IPMASK		.CZ " IP/Mask     : %d.%d.%d.%d/%d.%d.%d.%d\r\n"
MSG2.GW			.CZ " Gateway     : %d.%d.%d.%d\r\n"
MSG2.DNS		.CZ " DNS         : %d.%d.%d.%d,%d.%d.%d.%d\r\n"
MSG2.HOSTNAME	.CZ " Hostname    : %s.%s\r\n"
*--------------------------------------
MSG.CFG			.CZ "IPCONFIG:Reading %s..."
MSG.CFG.KO		.CZ "Not Found. [%h]\r\n"
MSG.CFG.OK		.CZ "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	.PS "address"
				.PS "netmask"
				.PS "gateway"
				.PS "nameserver"
				.PS "dnsdomain"
				.DA #0
*--------------------------------------
CFG.IP.SScanF	.AZ "%d.%d.%d.%d"
CFG.HOST.SScanF	.AZ "%d.%d.%d.%d %s %s"
*--------------------------------------
MSG.DHCP.CFG1	.CZ "IPCONFIG:Searching for DHCP servers..."
MSG.DHCP.CFG2	.CZ "IPCONFIG:Reply from DHCP Server at %d.%d.%d.%d, requesting...\r\n"
MSG.DHCP.KO		.CZ "IPCONFIG:Time Out [%h].\r\n"
MSG.DHCP.OK		.CZ "IPCONFIG:Success:[IP=%d.%d.%d.%d/%d.%d.%d.%d]\r\n"
MSG.DHCP.NA		.CZ "IPCONFIG:TCPIP Already configured, exiting."
*--------------------------------------
IPCFG			.BS S.IPCFG
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
DIB				.BS S.DIB
DEV.IOCTL		.BS S.IOCTL
DCB.NIC			.BS S.DCB.NIC
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/ipconfig.s
ASM
