Adding debian version 3.70~pre8+dfsg-1.
[syslinux-debian/hramrach.git] / dnsresolv.inc
blobf31c578b82af335bbf064635c125d05c7181dcc5
1 ; -*- fundamental -*-
2 ; -----------------------------------------------------------------------
4 ;   Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
6 ;   This program is free software; you can redistribute it and/or modify
7 ;   it under the terms of the GNU General Public License as published by
8 ;   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 ;   Bostom MA 02111-1307, USA; either version 2 of the License, or
10 ;   (at your option) any later version; incorporated herein by reference.
12 ; -----------------------------------------------------------------------
15 ; dnsresolv.inc
17 ; Very simple DNS resolver (assumes recursion-enabled DNS server;
18 ; this should be the normal thing for client-serving DNS servers.)
21 DNS_PORT        equ htons(53)           ; Default DNS port
22 DNS_MAX_PACKET  equ 512                 ; Defined by protocol
23 ; TFTP uses the range 49152-57343
24 DNS_LOCAL_PORT  equ htons(60053)        ; All local DNS queries come from this port #
25 DNS_MAX_SERVERS equ 4                   ; Max no of DNS servers
27                 section .text
30 ; Turn a string in DS:SI into a DNS "label set" in ES:DI.
31 ; On return, DI points to the first byte after the label set,
32 ; and SI to the terminating byte.
34 ; On return, DX contains the number of dots encountered.
36 dns_mangle:
37                 push ax
38                 push bx
39                 xor dx,dx
40 .isdot:
41                 inc dx
42                 xor al,al
43                 mov bx,di
44                 stosb
45 .getbyte:
46                 lodsb
47                 and al,al
48                 jz .endstring
49                 cmp al,':'
50                 jz .endstring
51                 cmp al,'.'
52                 je .isdot
53                 inc byte [es:bx]
54                 stosb
55                 jmp .getbyte
56 .endstring:
57                 dec si
58                 dec dx                  ; We always counted one high
59                 cmp byte [es:bx],0
60                 jz .done
61                 xor al,al
62                 stosb
63 .done:
64                 pop bx
65                 pop ax
66                 ret
69 ; Compare two sets of DNS labels, in DS:SI and ES:DI; the one in SI
70 ; is allowed pointers relative to a packet in DNSRecvBuf.
72 ; Assumes DS == ES.  ZF = 1 if same; no registers changed.
73 ; (Note: change reference to [di] to [es:di] to remove DS == ES assumption)
75 dns_compare:
76                 pusha
77 %if 0
79 .label:
80                 lodsb
81                 cmp al,0C0h
82                 jb .noptr
83                 and al,03Fh                     ; Get pointer value
84                 mov ah,al                       ; ... in network byte order!
85                 lodsb
86                 mov si,DNSRecvBuf
87                 add si,ax
88                 jmp .label
89 .noptr:
90                 cmp al,[di]
91                 jne .done                       ; Mismatch
92                 inc di
93                 movzx cx,al                     ; End label?
94                 and cx,cx                       ; ZF = 1 if match
95                 jz .done
97                 ; We have a string of bytes that need to match now
98                 repe cmpsb
99                 je .label
101 .done:
102 %else
103                 xor ax,ax
104 %endif
105                 popa
106                 ret
109 ; Skip past a DNS label set in DS:SI.
111 dns_skiplabel:
112                 push ax
113                 xor ax,ax                       ; AH == 0
114 .loop:
115                 lodsb
116                 cmp al,0C0h                     ; Pointer?
117                 jae .ptr
118                 and al,al
119                 jz .done
120                 add si,ax
121                 jmp .loop
122 .ptr:
123                 inc si                          ; Pointer is two bytes
124 .done:
125                 pop ax
126                 ret
128                 ; DNS header format
129                 struc dnshdr
130 .id:            resw 1
131 .flags:         resw 1
132 .qdcount:       resw 1
133 .ancount:       resw 1
134 .nscount:       resw 1
135 .arcount:       resw 1
136                 endstruc
138                 ; DNS query
139                 struc dnsquery
140 .qtype:         resw 1
141 .qclass:        resw 1
142                 endstruc
144                 ; DNS RR
145                 struc dnsrr
146 .type:          resw 1
147 .class:         resw 1
148 .ttl:           resd 1
149 .rdlength:      resw 1
150 .rdata:         equ $
151                 endstruc
153                 section .bss2
154                 alignb 2
155 DNSSendBuf      resb DNS_MAX_PACKET
156 DNSRecvBuf      resb DNS_MAX_PACKET
157 LocalDomain     resb 256                ; Max possible length
158 DNSServers      resd DNS_MAX_SERVERS
160                 section .data
161 pxe_udp_write_pkt_dns:
162 .status:        dw 0                    ; Status
163 .sip:           dd 0                    ; Server IP
164 .gip:           dd 0                    ; Gateway IP
165 .lport:         dw DNS_LOCAL_PORT       ; Local port
166 .rport:         dw DNS_PORT             ; Remote port
167 .buffersize:    dw 0                    ; Size of packet
168 .buffer:        dw DNSSendBuf, 0        ; off, seg of buffer
170 pxe_udp_read_pkt_dns:
171 .status:        dw 0                    ; Status
172 .sip:           dd 0                    ; Source IP
173 .dip:           dd 0                    ; Destination (our) IP
174 .rport:         dw DNS_PORT             ; Remote port
175 .lport:         dw DNS_LOCAL_PORT       ; Local port
176 .buffersize:    dw DNS_MAX_PACKET       ; Max packet size
177 .buffer:        dw DNSRecvBuf, 0        ; off, seg of buffer
179 LastDNSServer   dw DNSServers
181 ; Actual resolver function
182 ; Points to a null-terminated or :-terminated string in DS:SI
183 ; and returns the name in EAX if it exists and can be found.
184 ; If EAX = 0 on exit, the lookup failed.
186 ; No segment assumptions permitted.
188                 section .text
189 dns_resolv:
190                 push ds
191                 push es
192                 push di
193                 push cx
194                 push dx
196                 push cs
197                 pop es                  ; ES <- CS
199                 ; First, build query packet
200                 mov di,DNSSendBuf+dnshdr.flags
201                 inc word [es:di-2]      ; New query ID
202                 mov ax,htons(0100h)     ; Recursion requested
203                 stosw
204                 mov ax,htons(1)         ; One question
205                 stosw
206                 xor ax,ax               ; No answers, NS or ARs
207                 stosw
208                 stosw
209                 stosw
211                 call dns_mangle         ; Convert name to DNS labels
213                 push cs                 ; DS <- CS
214                 pop ds
216                 push si                 ; Save pointer to after DNS string
218                 ; Initialize...
219                 mov eax,[MyIP]
220                 mov [pxe_udp_read_pkt_dns.dip],eax
222                 and dx,dx
223                 jnz .fqdn               ; If we have dots, assume it's FQDN
224                 dec di                  ; Remove final null
225                 mov si,LocalDomain
226                 call strcpy             ; Uncompressed DNS label set so it ends in null
227 .fqdn:
229                 mov ax,htons(1)
230                 stosw                   ; QTYPE  = 1 = A
231                 stosw                   ; QCLASS = 1 = IN
233                 sub di,DNSSendBuf
234                 mov [pxe_udp_write_pkt_dns.buffersize],di
236                 ; Now, send it to the nameserver(s)
237                 ; Outer loop: exponential backoff
238                 ; Inner loop: scan the various DNS servers
240                 mov dx,PKT_TIMEOUT
241                 mov cx,PKT_RETRY
242 .backoff:
243                 mov si,DNSServers
244 .servers:
245                 cmp si,[LastDNSServer]
246                 jb .moreservers
248 .nomoreservers:
249                 add dx,dx                       ; Exponential backoff
250                 loop .backoff
252                 xor eax,eax                     ; Nothing...
253 .done:
254                 pop si
255                 pop dx
256                 pop cx
257                 pop di
258                 pop es
259                 pop ds
260                 ret
262 .moreservers:
263                 lodsd                           ; EAX <- next server
264                 push si
265                 push cx
266                 push dx
268                 mov word [pxe_udp_write_pkt_dns.status],0
270                 mov [pxe_udp_write_pkt_dns.sip],eax
271                 mov [pxe_udp_read_pkt_dns.sip],eax
272                 xor eax,[MyIP]
273                 and eax,[Netmask]
274                 jz .nogw
275                 mov eax,[Gateway]
276 .nogw:
277                 mov [pxe_udp_write_pkt_dns.gip],eax
279                 mov di,pxe_udp_write_pkt_dns
280                 mov bx,PXENV_UDP_WRITE
281                 call pxenv
282                 jc .timeout                             ; Treat failed transmit as timeout
283                 cmp word [pxe_udp_write_pkt_dns.status],0
284                 jne .timeout
286                 mov cx,[BIOS_timer]
287 .waitrecv:
288                 mov ax,[BIOS_timer]
289                 sub ax,cx
290                 cmp ax,dx
291                 jae .timeout
293                 mov word [pxe_udp_read_pkt_dns.status],0
294                 mov word [pxe_udp_read_pkt_dns.buffersize],DNS_MAX_PACKET
295                 mov di,pxe_udp_read_pkt_dns
296                 mov bx,PXENV_UDP_READ
297                 call pxenv
298                 and ax,ax
299                 jnz .waitrecv
300                 cmp [pxe_udp_read_pkt_dns.status],ax
301                 jnz .waitrecv
303                 ; Got a packet, deal with it...
304                 mov si,DNSRecvBuf
305                 lodsw
306                 cmp ax,[DNSSendBuf]             ; ID
307                 jne .waitrecv                   ; Not ours
309                 lodsw                           ; flags
310                 xor al,80h                      ; Query#/Answer bit
311                 test ax,htons(0F80Fh)
312                 jnz .badness
314                 lodsw
315                 xchg ah,al                      ; ntohs
316                 mov cx,ax                       ; Questions echoed
317                 lodsw
318                 xchg ah,al                      ; ntohs
319                 push ax                         ; Replies
320                 lodsw                           ; NS records
321                 lodsw                           ; Authority records
323                 jcxz .qskipped
324 .skipq:
325                 call dns_skiplabel              ; Skip name
326                 add si,4                        ; Skip question trailer
327                 loop .skipq
329 .qskipped:
330                 pop cx                          ; Number of replies
331                 jcxz .badness
333 .parseanswer:
334                 mov di,DNSSendBuf+dnshdr_size
335                 call dns_compare
336                 pushf
337                 call dns_skiplabel
338                 mov ax,[si+8]                   ; RDLENGTH
339                 xchg ah,al                      ; ntohs
340                 popf
341                 jnz .notsame
342                 cmp dword [si],htons(1)*0x10001 ; TYPE = A, CLASS = IN?
343                 jne .notsame
344                 cmp ax,4                        ; RDLENGTH = 4?
345                 jne .notsame
346                 ;
347                 ; We hit paydirt here...
348                 ;
349                 mov eax,[si+10]
350 .gotresult:
351                 add sp,6                        ; Drop timeout information
352                 jmp .done
354 .notsame:
355                 add si,10
356                 add si,ax
357                 loop .parseanswer
359 .badness:
360                 ; We got back no data from this server.  Unfortunately, for a recursive,
361                 ; non-authoritative query there is no such thing as an NXDOMAIN reply,
362                 ; which technically means we can't draw any conclusions.  However,
363                 ; in practice that means the domain doesn't exist.  If this turns out
364                 ; to be a problem, we may want to add code to go through all the servers
365                 ; before giving up.
367                 ; If the DNS server wasn't capable of recursion, and isn't capable
368                 ; of giving us an authoritative reply (i.e. neither AA or RA set),
369                 ; then at least try a different setver...
370                 test word [DNSRecvBuf+dnshdr.flags],htons(0480h)
371                 jz .timeout
373                 xor eax,eax
374                 jmp .gotresult
376 .timeout:
377                 pop dx
378                 pop cx
379                 pop si
380                 jmp .servers