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.SBUSY .EQ $BE
ERR.SBAD .EQ $BD
ERR.ARPPENDING .EQ $B1
ERR.DNSPENDING .EQ $B0
*--------------------------------------
TCP.PORT.TELNET .EQ 23
UDP.PORT.DNS .EQ 53
@ -101,26 +103,15 @@ S.ARPCACHE.MAC .EQ 6
S.ARPCACHE .EQ 12
*--------------------------------------
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.hNAME .EQ 1
S.DNSCACHE.TYPE .EQ 2
S.DNSCACHE.TYPE.A .EQ 1
S.DNSCACHE.TYPE.NS .EQ 2
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.ID .EQ 2 Store Message.Id in cache (pending req.)
S.DNSCACHE.TTL .EQ 4
S.DNSCACHE.IP .EQ 8
*
S.DNSCACHE .EQ 16
S.DNSCACHE .EQ 12
*--------------------------------------
S.SOCKET.SOCK .EQ 0
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
>PULLW ZPPtrIP Get IP address to fill
DNS.QUERY.I jsr HST.PSTR2DNS.I
ldx #0
DNS.QUERY.I jsr DNS.FIND.BY.NAME
bcs DNS.REQUEST
.1 lda DNS.CACHE,x
beq .6 empty DNS cache entry?
lda (ZPCachePtr) get status...
bpl .9 Pending...
phx
lda DNS.CACHE+S.DNSCACHE.hNAME,x
>SYSCALL SYS.GetMemPtrA
>STYA ZPPtrDNS
plx
lda ZPCachePtr
clc
adc S.DNSCACHE.IP
sta ZPCachePtr
bcc .1
inc ZPCachePtr+1
ldy #0
.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
.1 ldy #3
.2 lda (ZPCachePtr),y
sta (ZPPtrIP),y
inx
iny
cpy #4
bne .3
dey
bpl .2
clc
rts
.4 sec
.9 lda #ERR.DNSPENDING
sec
rts
.6 txa
clc
adc #S.DNSCACHE
tax
cmp #K.DNSCACHE.SIZE*S.DNSCACHE
bne .1
*--------------------------------------
DNS.REQUEST ldx #0
ldy #12
DNS.REQUEST jsr HST.PSTR2DNS.I
.1 lda DNSHostname,x
ldx #0
ldy #DNS.MSG.NAME-DNS.MSG
.1 lda DNS.Hostname,x
sta DNS.MSG,y
beq .2
inx
@ -104,7 +84,15 @@ DNS.REQUEST ldx #0
beq .9
>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 L.DNS.MSG
@ -112,16 +100,14 @@ DNS.REQUEST ldx #0
jsr SKT.SEND
lda hDNSSocket2
beq .9
>LDYA A2osX.RANDOM16
>STYA DNS.MSG+S.DNS.ID
beq .3
>PUSHW DNS.MSG.LEN
>PUSHW L.DNS.MSG
>PUSHB hDNSSocket2
jsr SKT.SEND
.3 lda #ERR.DNSPENDING
.9 sec
rts
*--------------------------------------
@ -133,120 +119,48 @@ DNS.REQUEST ldx #0
DNS.ADD >PULLW ZPPtrDNS Get host string
>PULLW ZPPtrIP Get host IP address
jsr HST.PSTR2DNS.I
lda #S.DNSCACHE.STATUS.RESOLVED+S.DNSCACHE.STATUS.STATIC
lda #$80
sta DNSHostTTL+3
stz DNSHostTTL+2
stz DNSHostTTL+1
stz DNSHostTTL
DNS.ADD.I sta DNS.TmpCache
lda #S.DNSCACHE.STATUS.RESOLVED
bra DNS.ADD.I
jsr DNS.FIND.BY.NAME
bcc .5 Found!!! update this entry
DNS.ADD.PENDING.I
lda #S.DNSCACHE.STATUS.PENDING
jsr DNS.FIND.FREE
DNS.ADD.I sta Status
ldx #0
.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 (ZPPtrDNS)
iny add 1 for len byte
lda #0
>PUSHYA push PSTR len
>PUSHBI 0 no option
>SYSCALL SYS.GetMem
bcc .10
plx
rts
bcs .9
.10 >STYA ZPPtrDNS
txa
plx
sta DNS.CACHE+S.DNSCACHE.hNAME,x
>STYA ZPTmpPtr1
stx DNS.TmpCache+S.DNSCACHE.hNAME
ldy #0
.1 lda DNSHostname,y
sta (ZPPtrDNS),y
iny
cpy DNSHostnameLen
lda (ZPPtrDNS)
sta (ZPTmpPtr1)
tay
.1 lda (ZPPtrDNS),y
sta (ZPTmpPtr1),y
dey
bne .1
lda #0
sta (ZPPtrDNS),y
DNS.UPD.ENTRY lda Status
sta DNS.CACHE+S.DNSCACHE.STATUS,x
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
ldy #S.DNSCACHE-1
.2 lda DNS.TmpCache,y
sta (ZPCachePtr),y
dey
bpl .2
clc
rts
.5
clc
rts
.9 sec
rts
*--------------------------------------
@ -254,6 +168,143 @@ DNS.GETCACHE >LDYA L.DNS.CACHE
clc
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
beq .8
jsr SKT.RCVDA

View File

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

View File

@ -24,6 +24,7 @@ ZPTmpPtr2 .EQ ZPLIB+22
ZPTmpPtr3 .EQ ZPLIB+24
ZPDataPtr .EQ ZPLIB+26
ZPDataLen .EQ ZPLIB+28
ZPCachePtr .EQ ZPLIB+30
*--------------------------------------
* File Header (16 Bytes)
*--------------------------------------
@ -319,36 +320,36 @@ DNS.SOCKET.DP .DA UDP.PORT.DNS
.BS 16
*--------------------------------------
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 $100 QDCOUNT
.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
DNS.CACHE .BS K.DNSCACHE.SIZE*S.DNSCACHE
SKT.TABLE .BS K.SKTTABLE.SIZE*S.SOCKET
*--------------------------------------
bFirstLoad .DA #0
Status .BS 1
DevFlags .BS 1
DYNPORT.LAST .BS 2
hDNSSocket1 .BS 1
hDNSSocket2 .BS 1
DNSHostTTL .BS 4
DNSHostNameLen .BS 1
DNSHostName .BS K.DNS.MAXLEN+1
IP.CHECKSUM .BS 4
DNS.HostNameLen .BS 1
DNS.HostName .BS K.DNS.MAXLEN+1
DNS.TmpCache .BS S.DNSCACHE
TmpMAC .BS 6
TmpIP .BS 4
TmpDWord .BS 4
TmpSocket .BS S.SOCKET
hFrame1 .BS 1
hSocket .BS 1
DYNPORT.LAST .BS 2
TmpOffset .BS 2
HST.DecStr .BS 4
HST.IP .BS 5
IP.CHECKSUM .BS 4
*--------------------------------------
MAN
SAVE LIB/LIBTCPIP.S