emailler/ip65/dns.s

462 lines
12 KiB
ArmAsm
Raw Normal View History

2013-12-13 21:24:03 +00:00
; minimal dns implementation - requires a DNS server that supports recursion
MAX_DNS_MESSAGES_SENT = 8 ; timeout after sending 8 messages will be about 7 seconds (1+2+3+4+5+6+7+8)/4
2013-12-13 21:24:03 +00:00
.include "zeropage.inc"
2013-12-13 21:24:03 +00:00
.include "../inc/common.i"
2013-12-13 21:24:03 +00:00
.ifndef KPR_API_VERSION_NUMBER
.define EQU =
2013-12-13 21:24:03 +00:00
.include "../inc/kipper_constants.i"
.endif
.export dns_set_hostname
.export dns_resolve
.export dns_ip
.export dns_hostname_is_dotted_quad
.import ip65_error
.import cfg_dns
.import parse_dotted_quad
.import dotted_quad_value
.import ip65_process
.import udp_add_listener
.import udp_remove_listener
.import udp_callback
.import udp_send
.import udp_inp
.import output_buffer
.importzp udp_data
.import udp_send_dest
.import udp_send_src_port
.import udp_send_dest_port
.import udp_send_len
.import check_for_abort_key
.import timer_read
dns_hostname = ptr1
.bss
2013-12-13 21:24:03 +00:00
; dns packet offsets
dns_inp = udp_inp + udp_data
dns_id = 0
dns_flags = 2
dns_qdcount = 4
dns_ancount = 6
dns_nscount = 8
dns_arcount = 10
dns_qname = 12
2013-12-13 21:24:03 +00:00
dns_server_port = 53
dns_client_port_low_byte: .res 1
2013-12-13 21:24:03 +00:00
dns_ip: .res 4 ; will be contain ip address of hostname after succesful exection of dns_resolve
2013-12-13 21:24:03 +00:00
dns_msg_id: .res 2
2013-12-13 21:24:03 +00:00
dns_current_label_length: .res 1
dns_current_label_offset: .res 1
2013-12-13 21:24:03 +00:00
dns_message_sent_count: .res 1
2013-12-13 21:24:03 +00:00
dns_packed_hostname: .res 128
2013-12-13 21:24:03 +00:00
; dns state machine
dns_initializing = 1 ; initial state
dns_query_sent = 2 ; sent a query, waiting for a response
dns_complete = 3 ; got a good response
dns_failed = 4 ; got either a 'no such name' or 'recursion declined' response
dns_state: .res 1 ; flag indicating the current stage in the dns resolution process
dns_timer: .res 1
dns_loop_count: .res 1
2013-12-13 21:24:03 +00:00
dns_break_polling_loop: .res 1
hostname_copied: .res 1
questions_in_response: .res 1
dns_hostname_is_dotted_quad: .res 1
2013-12-13 21:24:03 +00:00
.code
2013-12-13 21:24:03 +00:00
; sets up for resolution of a hostname to an ip address
; inputs:
; AX = pointer to null terminated string that contains either a dns hostname
; (e.g. "host.example.com",0) or an address in "dotted quad" format,
; (e.g. "192.168.1.0",0)
; outputs:
; carry flag is set on error (i.e. hostname too long), clear otherwise
; dns_hostname_is_dotted_quad: 1 if the hostname is an address in "dotted quad" format, 0 otherwise
; dns_ip: set to the ip address of the hostname (if dns_hostname_is_dotted_quad is 1)
dns_set_hostname:
stax dns_hostname
; copy the hostname into a buffer suitable to copy directly into the qname field
; we need to split on dots
jsr parse_dotted_quad ; if we are passed an IP address instead of a hostname, don't bother looking it up in dns
2013-12-13 21:24:03 +00:00
bcs @wasnt_dotted_quad
; if the string was a dotted quad, then copy the parsed 4 bytes in to dns_ip
2013-12-13 21:24:03 +00:00
lda #1
sta dns_hostname_is_dotted_quad
ldx #3 ; set destination address
2013-12-13 21:24:03 +00:00
: lda dotted_quad_value,x
sta dns_ip,x
dex
bpl :-
rts ; done!
2013-12-13 21:24:03 +00:00
@wasnt_dotted_quad:
ldy #0 ; input pointer
ldx #1 ; output pointer (start at 1, to skip first length offset, which will be filled in later)
sty dns_hostname_is_dotted_quad
2013-12-13 21:24:03 +00:00
sty dns_current_label_length
sty dns_current_label_offset
sty hostname_copied
@next_hostname_byte:
lda (dns_hostname),y ; get next char in hostname
2013-12-13 21:24:03 +00:00
beq @end_of_hostname
cmp #'/' ; allow hostnames to be terminated by "/" or ":" to help with URL parsing
2013-12-13 21:24:03 +00:00
beq @end_of_hostname
cmp #':'
bne :+
@end_of_hostname:
2013-12-13 21:24:03 +00:00
inc hostname_copied
bne @set_length_of_last_label
2017-05-02 18:39:27 +00:00
: cmp #'.' ; do we need to split the labels?
2013-12-13 21:24:03 +00:00
bne @not_a_dot
@set_length_of_last_label:
2013-12-13 21:24:03 +00:00
txa
pha
lda dns_current_label_length
2013-12-13 21:24:03 +00:00
ldx dns_current_label_offset
sta dns_packed_hostname,x
2013-12-13 21:24:03 +00:00
lda #0
sta dns_current_label_length
pla
tax
stx dns_current_label_offset
lda hostname_copied
beq @update_counters
2013-12-13 21:24:03 +00:00
jmp @hostname_done
@not_a_dot:
sta dns_packed_hostname,x
inc dns_current_label_length
@update_counters:
iny
2013-12-13 21:24:03 +00:00
inx
bmi @hostname_too_long ; don't allow a hostname of more than 128 bytes
2013-12-13 21:24:03 +00:00
jmp @next_hostname_byte
@hostname_done:
lda dns_packed_hostname-1,x ; get the last byte we wrote out
beq :+ ; was it a zero?
2013-12-13 21:24:03 +00:00
lda #0
sta dns_packed_hostname,x ; write a trailing zero (i.e. a zero length label)
2013-12-13 21:24:03 +00:00
inx
: clc ; no error
2013-12-13 21:24:03 +00:00
rts
@hostname_too_long:
lda #KPR_ERROR_INPUT_TOO_LARGE
sta ip65_error
sec
rts
; resolve a string containing a hostname (or a dotted quad) to an ip address
; inputs:
; cfg_dns must point to a DNS server that supports recursion
; dns_set_hostname must have been called to load the string to be resolved
; outputs:
; carry flag is set if there was an error, clear otherwise
; dns_ip: set to the ip address of the hostname (if no error)
dns_resolve:
lda dns_hostname_is_dotted_quad
2013-12-13 21:24:03 +00:00
beq @hostname_not_dotted_quad
clc
rts ; we already set dns_ip when copying the hostname
2013-12-13 21:24:03 +00:00
@hostname_not_dotted_quad:
ldax #dns_in
stax udp_callback
2013-12-13 21:24:03 +00:00
lda #53
inc dns_client_port_low_byte ; each call to resolve uses a different client address
ldx dns_client_port_low_byte ; so we don't get confused by late replies to a previous call
jsr udp_add_listener
bcc :+
rts
: lda #dns_initializing
2013-12-13 21:24:03 +00:00
sta dns_state
lda #0 ; reset the "message sent" counter
2013-12-13 21:24:03 +00:00
sta dns_message_sent_count
2013-12-13 21:24:03 +00:00
jsr send_dns_query
2013-12-13 21:24:03 +00:00
@dns_polling_loop:
lda dns_message_sent_count
adc #1
sta dns_loop_count ; we wait a bit longer between each resend
@outer_delay_loop:
2013-12-13 21:24:03 +00:00
lda #0
sta dns_break_polling_loop
jsr timer_read
stx dns_timer ; we only care about the high byte
@inner_delay_loop:
2013-12-13 21:24:03 +00:00
jsr ip65_process
jsr check_for_abort_key
bcc @no_abort
lda #KPR_ERROR_ABORTED_BY_USER
sta ip65_error
rts
@no_abort:
2013-12-13 21:24:03 +00:00
lda dns_state
cmp #dns_complete
beq @complete
cmp #dns_failed
beq @failed
2013-12-13 21:24:03 +00:00
lda #0
cmp dns_break_polling_loop
bne @break_polling_loop
jsr timer_read
cpx dns_timer ; this will tick over after about 1/4 of a second
2013-12-13 21:24:03 +00:00
beq @inner_delay_loop
2013-12-13 21:24:03 +00:00
dec dns_loop_count
bne @outer_delay_loop
2013-12-13 21:24:03 +00:00
@break_polling_loop:
jsr send_dns_query
inc dns_message_sent_count
2013-12-13 21:24:03 +00:00
lda dns_message_sent_count
cmp #MAX_DNS_MESSAGES_SENT-1
bpl @too_many_messages_sent
jmp @dns_polling_loop
@too_many_messages_sent:
lda #KPR_ERROR_TIMEOUT_ON_RECEIVE
bne @error ; always
2013-12-13 21:24:03 +00:00
@failed:
lda #KPR_ERROR_DNS_LOOKUP_FAILED
@error:
sta ip65_error
sec ; signal an error
bcs @done ; always
@complete:
clc ; signal success
@done:
php
2013-12-13 21:24:03 +00:00
lda #53
ldx dns_client_port_low_byte
jsr udp_remove_listener
plp
2013-12-13 21:24:03 +00:00
rts
send_dns_query:
ldax dns_msg_id
2013-12-13 21:24:03 +00:00
inx
adc #0
stax dns_msg_id
stax output_buffer+dns_id
2013-12-13 21:24:03 +00:00
ldax #$0001 ; QR =0 (query), opcode = 0 (query), AA = 0, TC = 0,RD = 1,RA = 0,Z = 0,RCODE = 0
2013-12-13 21:24:03 +00:00
stax output_buffer+dns_flags
ldax #$0100 ; we ask 1 question
2013-12-13 21:24:03 +00:00
stax output_buffer+dns_qdcount
ldax #$0000
stax output_buffer+dns_ancount ; we send no answers
stax output_buffer+dns_nscount ; we send no name servers
stax output_buffer+dns_arcount ; we send no authorative records
2013-12-13 21:24:03 +00:00
ldx #0
: lda dns_packed_hostname,x
2013-12-13 21:24:03 +00:00
sta output_buffer+dns_qname,x
inx
bpl @hostname_still_ok
lda #KPR_ERROR_INPUT_TOO_LARGE
sta ip65_error
jmp @error_on_send ; if we got past 128 bytes, there's a problem
@hostname_still_ok:
2013-12-13 21:24:03 +00:00
cmp #0
bne :- ; keep looping until we have a zero byte.
2013-12-13 21:24:03 +00:00
lda #0
sta output_buffer+dns_qname,x ; high byte of QTYPE = 1 (A)
sta output_buffer+dns_qname+2,x ; high byte of QLASS = 1 (IN)
2013-12-13 21:24:03 +00:00
lda #1
sta output_buffer+dns_qname+1,x ; low byte of QTYPE = 1 (A)
sta output_buffer+dns_qname+3,x ; low byte of QLASS = 1 (IN)
2013-12-13 21:24:03 +00:00
txa
clc
adc #(dns_qname+4)
ldx #0
stax udp_send_len
2013-12-13 21:24:03 +00:00
lda #53
ldx dns_client_port_low_byte
stax udp_send_src_port
2013-12-13 21:24:03 +00:00
ldx #3 ; set destination address
2013-12-13 21:24:03 +00:00
: lda cfg_dns,x
sta udp_send_dest,x
dex
bpl :-
2013-12-13 21:24:03 +00:00
ldax #dns_server_port ; set destination port
stax udp_send_dest_port
2013-12-13 21:24:03 +00:00
ldax #output_buffer
jsr udp_send
2013-12-13 21:24:03 +00:00
bcs @error_on_send
lda #dns_query_sent
sta dns_state
rts
@error_on_send:
2013-12-13 21:24:03 +00:00
sec
rts
2013-12-13 21:24:03 +00:00
dns_in:
lda dns_inp+dns_flags+1
and #$0f ; get the RCODE
cmp #0
2013-12-13 21:24:03 +00:00
beq @not_an_error_response
2013-12-13 21:24:03 +00:00
lda #dns_failed
sta dns_state
rts
@not_an_error_response:
lda dns_inp+dns_qdcount+1
sta questions_in_response
cmp #1 ; should be exactly 1 Q in the response (i.e. the one we sent)
2013-12-13 21:24:03 +00:00
beq :+
jmp @error_in_response
: lda dns_inp+dns_ancount+1
2013-12-13 21:24:03 +00:00
bne :+
jmp @error_in_response ; should be at least 1 answer in response
: ; we need to skip over the question (we will assume it's the question we just asked)
ldx #dns_qname
: lda dns_inp,x ; get next length byte in question
beq :+ ; we're done if length == 0
2013-12-13 21:24:03 +00:00
clc
txa
adc dns_inp,x ; add length of next label to ptr
adc #1 ; +1 for the length byte itself
2013-12-13 21:24:03 +00:00
tax
bcs @error_in_response ; if we overflowed x, then message is too big
2013-12-13 21:24:03 +00:00
bcc :-
: inx ; skip past the nul byte
2013-12-13 21:24:03 +00:00
lda dns_inp+1,x
cmp #1 ; QTYPE should 1
2013-12-13 21:24:03 +00:00
lda dns_inp+3,x
cmp #1 ; QCLASS should 1
bne @error_in_response
inx ; skip past the QTYPE/QCLASS
2013-12-13 21:24:03 +00:00
inx
inx
inx
; x now points to the start of the answers
lda dns_inp,x
bpl @error_in_response ; we are expecting the high bit to be set (we assume the server will send us back the answer to the question we just asked)
inx ; skip past the compression
2013-12-13 21:24:03 +00:00
inx
; we are now pointing at the TYPE field
lda dns_inp+1,x
2013-12-13 21:24:03 +00:00
cmp #5 ; is this a CNAME?
bne @not_a_cname
2013-12-13 21:24:03 +00:00
txa
clc
adc #10 ; skip 2 bytes TYPE, 2 bytes CLASS, 4 bytes TTL, 2 bytes RDLENGTH
2013-12-13 21:24:03 +00:00
tax
; we're now pointing at the CNAME record
ldy #0 ; start of CNAME hostname
: lda dns_inp,x
2013-12-13 21:24:03 +00:00
beq @last_byte_of_cname
bmi @found_compression_marker
2013-12-13 21:24:03 +00:00
sta dns_packed_hostname,y
inx
iny
bmi @error_in_response ; if we go past 128 bytes, something is wrong
bpl :- ; go get next byte
2013-12-13 21:24:03 +00:00
@last_byte_of_cname:
sta dns_packed_hostname,y
2013-12-13 21:24:03 +00:00
lda #1
sta dns_break_polling_loop
rts ; finished processing - the main dns polling loop should now resend a query, this time for the hostname from the CNAME record
2013-12-13 21:24:03 +00:00
@found_compression_marker:
lda dns_inp+1,x
tax
jmp :-
@not_a_cname:
2013-12-13 21:24:03 +00:00
cmp #1 ; should be 1 (A record)
bne @error_in_response
txa
clc
adc #10 ; skip 2 bytes TYPE, 2 bytes CLASS, 4 bytes TTL, 2 bytes RDLENGTH
2013-12-13 21:24:03 +00:00
tax
; we're now pointing at the answer!
lda dns_inp,x
2013-12-13 21:24:03 +00:00
sta dns_ip
lda dns_inp+1,x
2013-12-13 21:24:03 +00:00
sta dns_ip+1
lda dns_inp+2,x
2013-12-13 21:24:03 +00:00
sta dns_ip+2
lda dns_inp+3,x
sta dns_ip+3
2013-12-13 21:24:03 +00:00
lda #dns_complete
sta dns_state
2013-12-13 21:24:03 +00:00
lda #1
sta dns_break_polling_loop
@error_in_response:
rts
; -- LICENSE FOR dns.s --
2013-12-13 21:24:03 +00:00
; The contents of this file are subject to the Mozilla Public License
; Version 1.1 (the "License"); you may not use this file except in
; compliance with the License. You may obtain a copy of the License at
; http://www.mozilla.org/MPL/
;
2013-12-13 21:24:03 +00:00
; Software distributed under the License is distributed on an "AS IS"
; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
; License for the specific language governing rights and limitations
; under the License.
;
2013-12-13 21:24:03 +00:00
; The Original Code is ip65.
;
2013-12-13 21:24:03 +00:00
; The Initial Developer of the Original Code is Jonno Downes,
; jonno@jamtronix.com.
; Portions created by the Initial Developer are Copyright (C) 2009
; Jonno Downes. All Rights Reserved.
2013-12-13 21:24:03 +00:00
; -- LICENSE END --