A2osX/SBIN/DHCPCLNT.S.txt
2018-11-09 21:30:56 +01:00

522 lines
10 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

NEW
PREFIX /A2OSX.BUILD
AUTO 4,1
.LIST OFF
.OP 65C02
.OR $2000
.TF SBIN/DHCPCLNT
*--------------------------------------
.INB INC/MACROS.I
.INB INC/A2OSX.I
.INB INC/ETH.I
.INB INC/LIBTCPIP.I
.INB INC/NET.DHCP.I
*--------------------------------------
TIMEOUT.MAX .EQ 200 20 sec.
*--------------------------------------
ZPIPCfgPtr .EQ ZPBIN
ZPFrameBase .EQ ZPBIN+2
ZPFramePtr .EQ ZPBIN+4
*--------------------------------------
* 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 CS
.DA DS.END-DS.START DS
.DA #64 SS
.DA #6 ZP
.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1 .DA CS.INIT
.DA CS.RUN
.DA CS.DOEVENT
.DA CS.QUIT
L.LIBTCPIP .DA LIBTCPIP
L.IPCFG .DA IPCFG
L.SA.LOCAL .DA SA.LOCAL
L.SA.REMOTE .DA SA.REMOTE
L.MSG.CFG1 .DA MSG.CFG1
L.MSG.CFG2 .DA MSG.CFG2
L.MSG.CFG.KO .DA MSG.CFG.KO
L.MSG.CFG.OK .DA MSG.CFG.OK
L.MSG.CFG.ND .DA MSG.CFG.ND
L.MSG.CFG.NA .DA MSG.CFG.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
>LIBCALL hLIBTCPIP,LIBTCPIP.GET.IPCFG
bcs .9
>STYA ZPIPCfgPtr
lda (ZPIPCfgPtr)
and #S.IPCFG.STATUS.OK
bne .99 Already Configured, exit
ldy #S.IPCFG.hFD
lda (ZPIPCfgPtr),y
beq .98
ldy #S.IPCFG-1
.1 lda (ZPIPCfgPtr),y
sta IPCFG,y
dey
bne .1
stz IPCFG+S.IPCFG.STATUS
ldx #3
.11 lda A2osX.TIMER16,x
sta DHCP.DISC.XID,x
sta DHCP.REQ.XID,x
dex
bpl .11
ldx #5
.2 lda IPCFG+S.IPCFG.MAC,x
sta DHCP.DISC.CHADDR,x
sta DHCP.REQ.CHADDR,x
dex
bpl .2
clc
.9 rts
.98 >LDYA L.MSG.CFG.ND
bra .90
.99 >LDYA L.MSG.CFG.NA
.90 >SYSCALL puts
lda #0
sec
rts
*--------------------------------------
CS.RUN >PUSHBI 0 no protocol
lda #S.SOCKET.T.DGRAM
>LIBCALL hLIBTCPIP,LIBTCPIP.Socket
bcc .1
.9 jmp CS.RUN.KO
.1 >STA.G hSocket
pha
>PUSHW L.SA.LOCAL
pla
>LIBCALL hLIBTCPIP,LIBTCPIP.Bind
bcs .9
>PUSHWI S.SOCKET.O.BROADCAST
>LDA.G hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.setsockopt
>PUSHW L.SA.REMOTE
>LDA.G hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.Connect
bcs .9
>LDYA L.MSG.CFG1
>SYSCALL puts
jsr Init.Timeout
CS.RUN.SDISC >PUSHWI DHCP.DISC.LEN
>PUSHW L.DHCP.DISC
>LDA.G hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.Send
bcc .1
jmp CS.RUN.KO
.1 lda #S.IPCFG.STATUS.SDISC
sta IPCFG
CS.RUN.ROFFER >SLEEP
jsr CS.RUN.SKT.RCVD
bcc .1
jsr Wait.Timeout
bne CS.RUN.ROFFER
lda #1
jmp CS.RUN.KO
.1 jsr CheckDHCPOffer
bcs CS.RUN.ROFFER
lda #S.IPCFG.STATUS.ROFFER
sta IPCFG
CS.RUN.SREQ ldx #3
.1 >PUSHB IPCFG+S.IPCFG.DHCPSRVR,x
dex
bpl .1
>PUSHBI 4
>LDYA L.MSG.CFG2
>SYSCALL printf
>PUSHWI DHCP.REQ.LEN
>PUSHW L.DHCP.REQ
>LDA.G hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.Send
bcs CS.RUN.KO
lda #S.IPCFG.STATUS.SREQ
sta IPCFG
CS.RUN.RACK >SLEEP
jsr CS.RUN.SKT.RCVD
bcc .1
jsr Wait.Timeout
bne CS.RUN.RACK
lda #2
bra CS.RUN.KO
.1 jsr CheckDHCPAck
bcs CS.RUN.RACK
CS.RUN.OK lda #S.IPCFG.STATUS.OK
sta IPCFG
>PUSHW L.IPCFG
>LIBCALL hLIBTCPIP,LIBTCPIP.SET.IPCFG
ldx #3
.1 lda IPCFG+S.IPCFG.MASK,x
>PUSHA
dex
bpl .1
ldx #3
.2 lda IPCFG+S.IPCFG.IP,x
>PUSHA
dex
bpl .2
>PUSHBI 8
>LDYA L.MSG.CFG.OK
>SYSCALL printf
lda #0 Leave with NO ERROR
sec
rts
CS.RUN.KO pha
>PUSHA
>PUSHBI 1
>LDYA L.MSG.CFG.KO
>SYSCALL printf
pla
sec
rts
*--------------------------------------
CS.RUN.SKT.RCVD >LDA.G hSocket
>LIBCALL hLIBTCPIP,LIBTCPIP.Recv
bcs .9
>STA.G hFrame
>SYSCALL GetMemPtr
>STYA ZPFrameBase
.9 rts
*--------------------------------------
CS.DOEVENT lda (pEvent)
bpl .9 is it a TIMER event?
ldy #TimeOut
lda (pData),y
beq .9
dec
sta (pData),y
.9 sec do not discard TIMER event
rts
*--------------------------------------
CS.QUIT >LDA.G hSocket
beq .1
>LIBCALL hLIBTCPIP,LIBTCPIP.Shutdown
.1 lda hLIBTCPIP
>SYSCALL UnloadLib
clc
rts
*--------------------------------------
CheckDHCPOffer jsr CheckDHCPXID
bcs .9
lda ZPFrameBase
clc
adc #S.DHCP.OPTIONS
sta ZPFramePtr
lda ZPFrameBase+1
adc /S.DHCP.OPTIONS
sta ZPFramePtr+1
lda (ZPFramePtr)
cmp #S.DHCP.OPTIONS.MSGTYPE
bne .9
ldy #2
lda (ZPFramePtr),y
cmp #S.DHCP.OPTIONS.DHCPOffer
bne .9
ldy #S.DHCP.GIADDR+3
ldx #3
.1 lda (ZPFrameBase),y
sta DHCP.REQ.GIADDR,x
dey
dex
bpl .1
ldy #S.DHCP.YIADDR+3
ldx #3
.2 lda (ZPFrameBase),y
sta DHCP.REQ.OPT.REQIP,x
sta IPCFG+S.IPCFG.IP,x
dey
dex
bpl .2
ldy #S.IP.SRC+3
ldx #3
.3 lda (ZPFrameBase),y
sta DHCP.REQ.OPT.SVRIP,x
sta IPCFG+S.IPCFG.DHCPSRVR,x
dey
dex
bpl .3
.5 inc ZPFramePtr skip Option 53 (DHCPOffer:530102)
bne .6
inc ZPFramePtr+1
.6 lda (ZPFramePtr)
sec
adc ZPFramePtr add option len + 1
sta ZPFramePtr
bcc .7
inc ZPFramePtr+1
.7 lda (ZPFramePtr)
cmp #S.DHCP.OPTIONS.END
beq .8
jsr GetDHCPOption
bra .5
.8 jsr CS.RUN.DISCARD
clc
rts
.9 jsr CS.RUN.DISCARD
sec
rts
*--------------------------------------
CS.RUN.DISCARD >LDA.G hFrame
>SYSCALL FreeMem
rts
*--------------------------------------
GetDHCPOption cmp #S.DHCP.OPTIONS.MASK
bne .1
ldx #S.IPCFG.MASK
bra GetDHCPOption.CopyIP
.1 cmp #S.DHCP.OPTIONS.GW
bne .2
ldx #S.IPCFG.GW
bra GetDHCPOption.CopyIP
.2 cmp #S.DHCP.OPTIONS.DNS
bne .3
ldx #S.IPCFG.DNS1
bra GetDHCPOption.CopyIP
.3 cmp #S.DHCP.OPTIONS.DOMAIN
bne .9
ldy #1
lda (ZPFramePtr),y Get String len
cmp #80
bcc .4
lda #79
.4 sta IPCFG+S.IPCFG.DOMAIN store len
tax
.5 iny
lda (ZPFramePtr),y
sta IPCFG+S.IPCFG.DOMAIN-1,y
dex
bne .5
.9 rts
*--------------------------------------
GetDHCPOption.CopyIP
ldy #1
lda (ZPFramePtr),y Get Byte count
cmp #9
bcc .1
lda #8 max 8 bytes (2 DNS)
.1 iny
.2 pha
lda (ZPFramePtr),y
sta IPCFG,x
iny
inx
pla
dec
bne .2
rts
*--------------------------------------
CheckDHCPAck jsr CheckDHCPXID
bcs .9
lda ZPFrameBase
clc
adc #S.DHCP.OPTIONS
sta ZPFramePtr
lda ZPFrameBase+1
adc /S.DHCP.OPTIONS
sta ZPFramePtr+1
lda (ZPFramePtr)
cmp #S.DHCP.OPTIONS.MSGTYPE
bne .9
ldy #2
lda (ZPFramePtr),y
cmp #S.DHCP.OPTIONS.DHCPAck
bne .9
jsr CS.RUN.DISCARD
clc
rts
.9 jsr CS.RUN.DISCARD
sec
rts
*--------------------------------------
CheckDHCPXID ldy #S.DHCP.XID+3
ldx #3
.1 lda (ZPFrameBase),y
cmp DHCP.DISC.XID,x same XID ?
bne .9
dey
dex
bpl .1
clc
rts
.9 sec
rts
*--------------------------------------
Init.Timeout ldy #TimeOut
lda #TIMEOUT.MAX
sta (pData),y
rts
*--------------------------------------
Wait.TimeOut ldy #TimeOut
lda (pData),y
rts
*--------------------------------------
CS.END
LIBTCPIP .AZ "libtcpip"
MSG.CFG1 .AZ "DHCPCLNT:Searching for DHCP servers..."
MSG.CFG2 .AZ "DHCPCLNT:Reply from DHCP Server at %d.%d.%d.%d, requesting...\r\n"
MSG.CFG.KO .AZ "DHCPCLNT:Time Out [%h].\r\n"
MSG.CFG.OK .AZ "DHCPCLNT:Success:[IP=%d.%d.%d.%d/%d.%d.%d.%d]\r\n"
MSG.CFG.ND .AZ "DHCPCLNT:No Device, exiting."
MSG.CFG.NA .AZ "DHCPCLNT:TCPIP Already configured, exiting."
hLIBTCPIP .BS 1
IPCFG .BS S.IPCFG
*--------------------------------------
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 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
DHCP.REQ.GIADDR .HS 00000000
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
*--------------------------------------
DS.START
TimeOut .BS 1
hSocket .BS 1
hFrame .BS 1
DS.END
MAN
SAVE /A2OSX.SRC/SBIN/DHCPCLNT.S
ASM