DNS:code rewrite

This commit is contained in:
Rémy GIBERT 2016-03-15 18:16:14 +01:00
parent 08067f9fe5
commit 93fade265f
4 changed files with 222 additions and 179 deletions

View File

@ -64,6 +64,8 @@ LIBTCPIP.HST.GETBYADDR .EQ 74
ERR.OOS .EQ $BF ERR.OOS .EQ $BF
ERR.SBUSY .EQ $BE ERR.SBUSY .EQ $BE
ERR.SBAD .EQ $BD ERR.SBAD .EQ $BD
ERR.ARPPENDING .EQ $B1
ERR.DNSPENDING .EQ $B0
*-------------------------------------- *--------------------------------------
TCP.PORT.TELNET .EQ 23 TCP.PORT.TELNET .EQ 23
UDP.PORT.DNS .EQ 53 UDP.PORT.DNS .EQ 53
@ -101,26 +103,15 @@ S.ARPCACHE.MAC .EQ 6
S.ARPCACHE .EQ 12 S.ARPCACHE .EQ 12
*-------------------------------------- *--------------------------------------
S.DNSCACHE.STATUS .EQ 0 S.DNSCACHE.STATUS .EQ 0
S.DNSCACHE.STATUS.PENDING .EQ 64 S.DNSCACHE.STATUS.PENDING .EQ 1
S.DNSCACHE.STATUS.STATIC .EQ 2
S.DNSCACHE.STATUS.RESOLVED .EQ 128 S.DNSCACHE.STATUS.RESOLVED .EQ 128
S.DNSCACHE.hNAME .EQ 1 S.DNSCACHE.hNAME .EQ 1
S.DNSCACHE.TYPE .EQ 2 S.DNSCACHE.ID .EQ 2 Store Message.Id in cache (pending req.)
S.DNSCACHE.TYPE.A .EQ 1 S.DNSCACHE.TTL .EQ 4
S.DNSCACHE.TYPE.NS .EQ 2 S.DNSCACHE.IP .EQ 8
S.DNSCACHE.TYPE.CNAME .EQ 5
S.DNSCACHE.TYPE.SOA .EQ 6
S.DNSCACHE.TYPE.WKS .EQ 11
S.DNSCACHE.TYPE.PTR .EQ 12
S.DNSCACHE.TYPE.MX .EQ 15
S.DNSCACHE.TYPE.SRV .EQ 33
S.DNSCACHE.CLASS .EQ 4
S.DNSCACHE.TTL .EQ 6
S.DNSCACHE.RDLENGTH .EQ 10
S.DNSCACHE.RDATA .EQ 12
S.DNSCACHE.ID .EQ 12 Store Message.Id in cache (pending req.)
S.DNSCACHE.DYNPORT .EQ 14 Store Dyn.Port in cache (pending req.)
* *
S.DNSCACHE .EQ 16 S.DNSCACHE .EQ 12
*-------------------------------------- *--------------------------------------
S.SOCKET.SOCK .EQ 0 S.SOCKET.SOCK .EQ 0
S.SOCKET.SOCK.DGRAM .EQ 1 S.SOCKET.SOCK.DGRAM .EQ 1

View File

@ -23,57 +23,37 @@ DNS.CLEAR ldx #K.DNSCACHE.SIZE*S.DNSCACHE
DNS.QUERY >PULLW ZPPtrDNS Get host string DNS.QUERY >PULLW ZPPtrDNS Get host string
>PULLW ZPPtrIP Get IP address to fill >PULLW ZPPtrIP Get IP address to fill
DNS.QUERY.I jsr HST.PSTR2DNS.I DNS.QUERY.I jsr DNS.FIND.BY.NAME
ldx #0 bcs DNS.REQUEST
.1 lda DNS.CACHE,x lda (ZPCachePtr) get status...
beq .6 empty DNS cache entry? bpl .9 Pending...
phx lda ZPCachePtr
lda DNS.CACHE+S.DNSCACHE.hNAME,x clc
>SYSCALL SYS.GetMemPtrA adc S.DNSCACHE.IP
>STYA ZPPtrDNS sta ZPCachePtr
plx bcc .1
inc ZPCachePtr+1
ldy #0 .1 ldy #3
.2 lda (ZPCachePtr),y
.2 lda DNSHostname,y
bne .21
lda (ZPPtrDNS),y
beq .22
.21 cmp (ZPPtrDNS),y
bne .6
iny
bne .2
.22 lda DNS.CACHE,x get back status...
bpl .4 Pending...
ldy #0
.3 lda DNS.CACHE+S.DNSCACHE.RDATA,x
sta (ZPPtrIP),y sta (ZPPtrIP),y
inx dey
iny bpl .2
cpy #4
bne .3
clc clc
rts rts
.4 sec .9 lda #ERR.DNSPENDING
sec
rts rts
.6 txa
clc
adc #S.DNSCACHE
tax
cmp #K.DNSCACHE.SIZE*S.DNSCACHE
bne .1
*-------------------------------------- *--------------------------------------
DNS.REQUEST ldx #0 DNS.REQUEST jsr HST.PSTR2DNS.I
ldy #12
.1 lda DNSHostname,x ldx #0
ldy #DNS.MSG.NAME-DNS.MSG
.1 lda DNS.Hostname,x
sta DNS.MSG,y sta DNS.MSG,y
beq .2 beq .2
inx inx
@ -104,7 +84,15 @@ DNS.REQUEST ldx #0
beq .9 beq .9
>LDYA A2osX.RANDOM16 >LDYA A2osX.RANDOM16
>STYA DNS.MSG >STYA DNS.MSG.ID
>STYA DNS.TmpCache+S.DNSCACHE.ID
lda #5
sta DNS.TmpCache+S.DNSCACHE.TTL
lda #S.DNSCACHE.STATUS.PENDING
jsr DNS.ADD.I
bcs .9
>PUSHW DNS.MSG.LEN >PUSHW DNS.MSG.LEN
>PUSHW L.DNS.MSG >PUSHW L.DNS.MSG
@ -112,16 +100,14 @@ DNS.REQUEST ldx #0
jsr SKT.SEND jsr SKT.SEND
lda hDNSSocket2 lda hDNSSocket2
beq .9 beq .3
>LDYA A2osX.RANDOM16
>STYA DNS.MSG+S.DNS.ID
>PUSHW DNS.MSG.LEN >PUSHW DNS.MSG.LEN
>PUSHW L.DNS.MSG >PUSHW L.DNS.MSG
>PUSHB hDNSSocket2 >PUSHB hDNSSocket2
jsr SKT.SEND jsr SKT.SEND
.3 lda #ERR.DNSPENDING
.9 sec .9 sec
rts rts
*-------------------------------------- *--------------------------------------
@ -133,120 +119,48 @@ DNS.REQUEST ldx #0
DNS.ADD >PULLW ZPPtrDNS Get host string DNS.ADD >PULLW ZPPtrDNS Get host string
>PULLW ZPPtrIP Get host IP address >PULLW ZPPtrIP Get host IP address
jsr HST.PSTR2DNS.I lda #S.DNSCACHE.STATUS.RESOLVED+S.DNSCACHE.STATUS.STATIC
lda #$80 DNS.ADD.I sta DNS.TmpCache
sta DNSHostTTL+3
stz DNSHostTTL+2
stz DNSHostTTL+1
stz DNSHostTTL
lda #S.DNSCACHE.STATUS.RESOLVED jsr DNS.FIND.BY.NAME
bra DNS.ADD.I bcc .5 Found!!! update this entry
DNS.ADD.PENDING.I jsr DNS.FIND.FREE
lda #S.DNSCACHE.STATUS.PENDING
DNS.ADD.I sta Status lda (ZPPtrDNS)
ldx #0 iny add 1 for len byte
.1 lda DNS.CACHE,x
beq DNS.ADD.ENTRY free ?
lda DNS.CACHE+S.DNSCACHE.hNAME,x
phx
>SYSCALL SYS.GetMemPtrA
>STYA ZPPtrDNS
plx
ldy #0
.2 lda DNSHostname,y
cmp (ZPPtrDNS),y
bne .4
and #$FF End Of String?
beq DNS.UPD.ENTRY
iny
bne .2
.4 txa
clc
adc #S.DNSCACHE
tax
cmp #K.DNSCACHE.SIZE*S.DNSCACHE
bne .1
lda DNS.CACHE+S.DNSCACHE.hNAME discard oldest entry hNAME
>SYSCALL SYS.FreeMemA
ldx #K.DNSCACHE.SIZE*S.DNSCACHE-S.DNSCACHE
.6 lda DNS.CACHE+S.DNSCACHE-1,x
sta DNS.CACHE-1,x
dex
bne .6
ldx #K.DNSCACHE.SIZE*S.DNSCACHE-S.DNSCACHE
DNS.ADD.ENTRY phx
ldy DNSHostnameLen
iny add 1 for Ending 00
lda #0 lda #0
>PUSHYA push PSTR len >PUSHYA push PSTR len
>PUSHBI 0 no option >PUSHBI 0 no option
>SYSCALL SYS.GetMem >SYSCALL SYS.GetMem
bcc .10 bcs .9
plx
rts
.10 >STYA ZPPtrDNS >STYA ZPTmpPtr1
txa stx DNS.TmpCache+S.DNSCACHE.hNAME
plx
sta DNS.CACHE+S.DNSCACHE.hNAME,x
ldy #0 lda (ZPPtrDNS)
.1 lda DNSHostname,y sta (ZPTmpPtr1)
sta (ZPPtrDNS),y tay
iny
cpy DNSHostnameLen .1 lda (ZPPtrDNS),y
sta (ZPTmpPtr1),y
dey
bne .1 bne .1
lda #0 ldy #S.DNSCACHE-1
sta (ZPPtrDNS),y .2 lda DNS.TmpCache,y
sta (ZPCachePtr),y
DNS.UPD.ENTRY lda Status dey
sta DNS.CACHE+S.DNSCACHE.STATUS,x bpl .2
bmi .1 pending?
* lda DNSMessageID
* sta DNS.CACHE+S.DNSCACHE.ID,x
* lda DNSMessageID+1
* sta DNS.CACHE+S.DNSCACHE.ID+1,x
bra .9
.1 lda DNSHostTTL
sta DNS.CACHE+S.DNSCACHE.TTL,x
lda DNSHostTTL+1
sta DNS.CACHE+S.DNSCACHE.TTL+1,x
lda DNSHostTTL+2
sta DNS.CACHE+S.DNSCACHE.TTL+2,x
lda DNSHostTTL+3
sta DNS.CACHE+S.DNSCACHE.TTL+3,x
ldy #0
.2 lda (ZPPtrIP),y
sta DNS.CACHE+S.DNSCACHE.RDATA,x
inx
iny
cpy #4
bne .2
clc clc
rts rts
.5
clc
rts
.9 sec .9 sec
rts rts
*-------------------------------------- *--------------------------------------
@ -254,6 +168,143 @@ DNS.GETCACHE >LDYA L.DNS.CACHE
clc clc
rts rts
*-------------------------------------- *--------------------------------------
DNS.FIND.BY.NAME
>LDYA L.DNS.CACHE
>STYA ZPCachePtr
lda #K.DNSCACHE.SIZE
sta TmpOffset
.1 lda (ZPCachePtr)
beq .6 empty DNS cache entry?
ldy #S.DNSCACHE.hNAME
lda (ZPCachePtr),y
>SYSCALL SYS.GetMemPtrA
>STYA ZPTmpPtr1
lda (ZPPtrDNS)
cmp (ZPTmpPtr1)
bne .6
tay
.2 lda (ZPPtrDNS),y
cmp (ZPTmpPtr1),y
bne .6
dey
bne .2
clc
rts
.6 lda ZPCachePtr
clc
adc #S.DNSCACHE
sta ZPCachePtr
bcc .7
inc ZPCachePtr+1
.7 dec TmpOffset
bne .1
sec
rts
*--------------------------------------
DNS.FIND.FREE >LDYA L.DNS.CACHE
>STYA ZPCachePtr
lda #K.DNSCACHE.SIZE
sta TmpOffset
lda #$ff
sta TmpDWord
sta TmpDWord+1
sta TmpDWord+2
sta TmpDWord+3
.1 lda (ZPCachePtr)
beq .8 empty DNS cache entry?
bpl .6 pending.....skip
ldy #S.DNSCACHE.TTL
ldx #0
sec
.2 lda TmpDWord,x is save TTL greater then actual TTL?
sbc (ZPCachePtr),y
inx
iny
cpy #S.DNSCACHE.TTL+4
bne .2
bcc .6 no,
ldy #S.DNSCACHE.TTL
ldx #0
.3 lda (ZPCachePtr),y Save new lowest TTL
sta TmpDWord,x
inx
iny
cpy #S.DNSCACHE.TTL+4
bne .3
>LDYA ZPCachePtr
>STYA ZPTmpPtr1 save lowest TTL slot ...
.6 lda ZPCachePtr
clc
adc #S.DNSCACHE
sta ZPCachePtr
bcc .7
inc ZPCachePtr+1
.7 dec TmpOffset
bne .1 no empty slot found, discard lowest TTL
>LDYA ZPTmpPtr1
>STYA ZPCachePtr
jmp DNS.FREE
.8 clc
rts
*--------------------------------------
DNS.EXPIRE >LDYA L.DNS.CACHE
>STYA ZPCachePtr
lda #K.DNSCACHE.SIZE
sta TmpOffset
.1 lda (ZPCachePtr)
beq .7 empty DNS cache entry?
ldy #S.DNSCACHE.TTL
ldx #4
clc
.2 lda (ZPCachePtr),y
sbc #0
sta (ZPCachePtr),y
iny
dex
bne .2
bcs .7
jsr DNS.FREE
.7 lda ZPCachePtr
clc
adc #S.DNSCACHE
sta ZPCachePtr
bcc .8
inc ZPCachePtr+1
.8 dec TmpOffset
bne .1
clc
rts
*--------------------------------------
DNS.FREE ldy #S.DNSCACHE.hNAME
lda (ZPCachePtr),y
>SYSCALL SYS.FreeMemA
lda #0
sta (ZPCachePtr)
rts
*--------------------------------------
DNS.SKTPOLL lda hDNSSocket1 DNS.SKTPOLL lda hDNSSocket1
beq .8 beq .8
jsr SKT.RCVDA jsr SKT.RCVDA

View File

@ -37,10 +37,10 @@ HST.PSTR2DNS.I lda (ZPPtrDNS)
tay tay
inc inc
sta DNSHostNameLen sta DNS.HostNameLen
lda #0 Ending 0 lda #0 Ending 0
sta DNSHostName+1,y sta DNS.HostName+1,y
ldx #0 ldx #0
@ -61,10 +61,10 @@ HST.PSTR2DNS.I lda (ZPPtrDNS)
.2 txa .2 txa
ldx #0 ldx #0
.3 sta DNSHostName,y .3 sta DNS.HostName,y
dey dey
bne .1 bne .1
stx DNSHostName stx DNS.HostName
clc clc
rts rts

View File

@ -24,6 +24,7 @@ ZPTmpPtr2 .EQ ZPLIB+22
ZPTmpPtr3 .EQ ZPLIB+24 ZPTmpPtr3 .EQ ZPLIB+24
ZPDataPtr .EQ ZPLIB+26 ZPDataPtr .EQ ZPLIB+26
ZPDataLen .EQ ZPLIB+28 ZPDataLen .EQ ZPLIB+28
ZPCachePtr .EQ ZPLIB+30
*-------------------------------------- *--------------------------------------
* File Header (16 Bytes) * File Header (16 Bytes)
*-------------------------------------- *--------------------------------------
@ -319,36 +320,36 @@ DNS.SOCKET.DP .DA UDP.PORT.DNS
.BS 16 .BS 16
*-------------------------------------- *--------------------------------------
DNS.MSG.LEN .BS 2 DNS.MSG.LEN .BS 2
DNS.MSG .BS 2 ID DNS.MSG
DNS.MSG.ID .BS 2
.DA /S.DNS.F.RD Flags=query .DA /S.DNS.F.RD Flags=query
.DA #S.DNS.F.RD Flags=query .DA #S.DNS.F.RD Flags=query
.DA $100 QDCOUNT .DA $100 QDCOUNT
.BS 6 ANCOUNT,NSCOUNT,ARCOUNT .BS 6 ANCOUNT,NSCOUNT,ARCOUNT
.BS K.DNS.MAXLEN+6 DNS.MSG+12 DNS.MSG.NAME .BS K.DNS.MAXLEN+6
*-------------------------------------- *--------------------------------------
ARP.CACHE .BS K.ARPCACHE.SIZE*S.ARPCACHE ARP.CACHE .BS K.ARPCACHE.SIZE*S.ARPCACHE
DNS.CACHE .BS K.DNSCACHE.SIZE*S.DNSCACHE DNS.CACHE .BS K.DNSCACHE.SIZE*S.DNSCACHE
SKT.TABLE .BS K.SKTTABLE.SIZE*S.SOCKET SKT.TABLE .BS K.SKTTABLE.SIZE*S.SOCKET
*-------------------------------------- *--------------------------------------
bFirstLoad .DA #0 bFirstLoad .DA #0
Status .BS 1
DevFlags .BS 1 DevFlags .BS 1
DYNPORT.LAST .BS 2
hDNSSocket1 .BS 1 hDNSSocket1 .BS 1
hDNSSocket2 .BS 1 hDNSSocket2 .BS 1
DNSHostTTL .BS 4 DNS.HostNameLen .BS 1
DNSHostNameLen .BS 1 DNS.HostName .BS K.DNS.MAXLEN+1
DNSHostName .BS K.DNS.MAXLEN+1 DNS.TmpCache .BS S.DNSCACHE
IP.CHECKSUM .BS 4
TmpMAC .BS 6 TmpMAC .BS 6
TmpIP .BS 4 TmpIP .BS 4
TmpDWord .BS 4 TmpDWord .BS 4
TmpSocket .BS S.SOCKET TmpSocket .BS S.SOCKET
hFrame1 .BS 1 hFrame1 .BS 1
hSocket .BS 1 hSocket .BS 1
DYNPORT.LAST .BS 2
TmpOffset .BS 2 TmpOffset .BS 2
HST.DecStr .BS 4 HST.DecStr .BS 4
HST.IP .BS 5 HST.IP .BS 5
IP.CHECKSUM .BS 4
*-------------------------------------- *--------------------------------------
MAN MAN
SAVE LIB/LIBTCPIP.S SAVE LIB/LIBTCPIP.S