2002-06-08 11:07:01 -04:00
|
|
|
;
|
|
|
|
; dns.scm
|
|
|
|
;
|
2006-11-12 12:48:36 -05:00
|
|
|
; Client-Implementation of the RFC 1035
|
2002-08-27 05:03:22 -04:00
|
|
|
|
|
|
|
;;; This file is part of the Scheme Untergrund Networking package.
|
|
|
|
|
|
|
|
;;; Copyright (c) 2002 by Marcus Crestani.
|
2003-01-17 13:29:47 -05:00
|
|
|
;;; Copyright (c) 2002-2003 by Martin Gasbichler
|
2006-11-12 12:48:36 -05:00
|
|
|
;;; Copyright (c) 2005-2006 by Norbert Freudemann
|
2002-08-27 05:03:22 -04:00
|
|
|
;;; For copyright information, see the file COPYING which comes with
|
|
|
|
;;; the distribution.
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
; domain names - implementation and specification
|
|
|
|
; based on the PLT-implementation.
|
|
|
|
;
|
|
|
|
;
|
|
|
|
; TODO:
|
|
|
|
; - test, test, test
|
2006-11-12 12:48:36 -05:00
|
|
|
; - could be a good thing to merge dns.scm with the dnsd/resolver.scm?
|
2002-06-08 11:07:01 -04:00
|
|
|
; - types from newer RFCs (41, unknown)
|
|
|
|
; - more documentation
|
|
|
|
;
|
2006-11-12 12:48:36 -05:00
|
|
|
;
|
2002-06-25 05:11:45 -04:00
|
|
|
; ---
|
|
|
|
; sample usage & documentation:
|
2002-06-08 11:07:01 -04:00
|
|
|
;
|
2002-07-07 13:43:32 -04:00
|
|
|
; <ip-address32> is a 32bit integer internet->address, shortly address32.
|
|
|
|
; <ip-string> is a string in standard dot notation "xxx.xxx.xxx.xxx".
|
|
|
|
; <name> is a string
|
2002-06-08 11:07:01 -04:00
|
|
|
;
|
2002-07-07 13:43:32 -04:00
|
|
|
; <nameserver> can either be a domainname, an ip-string or an ip-address32.
|
2002-06-25 05:11:45 -04:00
|
|
|
; if it is a domainname, its ip is looked up on a nameserver listed in
|
|
|
|
; /etc/resolv.conf.
|
|
|
|
;
|
2003-08-19 10:36:08 -04:00
|
|
|
; (dns-find-nameserver-list) --> <ip-string list>
|
|
|
|
; this parses the /etc/resolv.conf file and returns the found
|
|
|
|
; nameserver in a list of dotted strings.
|
|
|
|
;
|
|
|
|
; (dns-find-nameserver) --> <ip-string>
|
2002-06-25 05:11:45 -04:00
|
|
|
; this parses the /etc/resolv.conf file and returns the first found
|
2003-08-19 10:36:08 -04:00
|
|
|
; nameserver in dotted string notation.
|
2002-06-25 05:11:45 -04:00
|
|
|
;
|
2003-08-19 10:36:08 -04:00
|
|
|
; (dns-check-namservers [nameserver list]) --> undefined
|
|
|
|
; checks if the given nameservers are reachable. If no argument is given,
|
|
|
|
; the nameservers in /etc/resolv.conf are checked.
|
2002-06-25 05:11:45 -04:00
|
|
|
;
|
|
|
|
;
|
2003-08-19 10:36:08 -04:00
|
|
|
; (dns-lookup-name <name> [nameserver list] [use-cache?]) --> <ip-address32>
|
|
|
|
; (dns-lookup-ip <ip-string | ip-address32> [nameserver list] [use-cache?])
|
|
|
|
; --> <name>
|
|
|
|
; (dns-lookup-nameserver <name> [nameserver list] [use-cache?])
|
2002-07-07 13:43:32 -04:00
|
|
|
; --> <list of ip-address32s of authoritative nameservers>
|
2003-08-19 10:36:08 -04:00
|
|
|
; (dns-lookup-mail-exchanger <name> [nameserver list] [use-cache?])
|
2002-07-07 13:43:32 -04:00
|
|
|
; --> <list of names of mail-exchangers>
|
2002-06-25 05:11:45 -04:00
|
|
|
;
|
|
|
|
; dns-lookup-name, dns-lookup-ip, dns-lookup-nameserver and
|
|
|
|
; dns-lookup-mail-exchanger are "simple lookup functions",
|
|
|
|
; they return the wanted information or #f.
|
2002-07-07 13:43:32 -04:00
|
|
|
; dns-lookup-ip can either be given an ip-string or an ip-address32.
|
2002-06-08 11:07:01 -04:00
|
|
|
;
|
2002-07-07 13:43:32 -04:00
|
|
|
; concurrent dns lookup:
|
2002-07-23 04:48:47 -04:00
|
|
|
; if a list of nameservers is given to the optional <nameserver> argument,
|
|
|
|
; a concurrent lookup to all nameservers in this list is started.
|
|
|
|
; The nameservers in this list could either be ip-strings or ip-address32s.
|
|
|
|
; example: (dns-lookup-name "www.uni-tuebingen.de" (dns-find-nameserver-list))
|
|
|
|
; starts an concurrent lookup which contacts all nameservers in
|
|
|
|
; /etc/resolv.conf.
|
2002-06-25 05:11:45 -04:00
|
|
|
;
|
2002-07-07 13:43:32 -04:00
|
|
|
;
|
2003-08-19 10:36:08 -04:00
|
|
|
; (dns-lookup <name | ip-string | ip-address32> <type> [nameserver list])
|
2002-07-07 13:43:32 -04:00
|
|
|
; --> <dns-message>
|
2002-06-08 11:07:01 -04:00
|
|
|
; (show-dns-message <dns-message) --> the whole message, human readable
|
|
|
|
;
|
2002-06-25 05:11:45 -04:00
|
|
|
; a <dns-message> is a record, with several entries, which holds the whole
|
|
|
|
; query/response dialog. the simplest way to get detailed information about
|
|
|
|
; the record structure is to view the result of show-dns-message.
|
|
|
|
;
|
|
|
|
; dns-lookup returns much more information than the simple lookup functions,
|
|
|
|
; only useful in very special cases.
|
|
|
|
;
|
|
|
|
;
|
2002-06-08 11:07:01 -04:00
|
|
|
; some lookups return a hostname (e.g. mx).
|
|
|
|
; many applications need instead of a hostname a ip address.
|
|
|
|
; force-ip and force-ip-list guarantee that a ip address is
|
|
|
|
; returned.
|
|
|
|
;
|
2002-07-07 13:43:32 -04:00
|
|
|
; (force-ip <name>) --> <ip>
|
2002-06-08 11:07:01 -04:00
|
|
|
; (force-ip-list <list of names>) --> <list of ips>
|
2002-07-07 13:43:32 -04:00
|
|
|
;
|
|
|
|
;
|
|
|
|
; useful converters:
|
|
|
|
;
|
|
|
|
; (address32->ip-string <ip-address32>) -> <ip-string>
|
|
|
|
; (ip-string->address32 <ip-string>) -> <ip-address32>
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
;; --- error conditions
|
2002-09-16 12:16:04 -04:00
|
|
|
|
|
|
|
;; supertype of all errors signaled by this library
|
|
|
|
(define-condition-type 'dns-error '(error))
|
|
|
|
(define dns-error? (condition-predicate 'dns-error))
|
|
|
|
|
|
|
|
(define-condition-type 'parse-error '(dns-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(define parse-error? (condition-predicate 'parse))
|
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
(define-condition-type 'unexpected-eof-from-server '(dns-error))
|
2006-11-12 12:48:36 -05:00
|
|
|
(define unexpected-eof-from-server?
|
|
|
|
(condition-predicate 'unexpected-eof-from-server))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
(define-condition-type 'bad-address '(dns-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(define bad-address? (condition-predicate 'bad-address))
|
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
(define-condition-type 'no-nameservers '(dns-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(define no-nameservers? (condition-predicate 'no-nameservers))
|
|
|
|
|
2003-07-09 16:45:20 -04:00
|
|
|
(define-condition-type 'no-nameserver-given '(dns-error))
|
|
|
|
(define no-nameserver-given? (condition-predicate 'no-nameserver-given))
|
|
|
|
|
2002-11-25 11:47:00 -05:00
|
|
|
(define-condition-type 'bad-nameserver '(dns-error))
|
|
|
|
(define bad-nameserver? (condition-predicate 'bad-nameserver))
|
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
(define-condition-type 'not-a-hostname '(dns-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(define not-a-hostname? (condition-predicate 'not-a-hostname))
|
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
(define-condition-type 'not-a-ip '(dns-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(define not-a-ip? (condition-predicate 'not-a-ip))
|
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
;; supertype of all errors signaled if the dns server returned a non-sero
|
|
|
|
;; reply code
|
|
|
|
(define-condition-type 'dns-server-error '(dns-error))
|
|
|
|
(define dns-server-error? (condition-predicate 'dns-server-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
(define-condition-type 'dns-format-error '(dns-server-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(define dns-format-error? (condition-predicate 'dns-format-error))
|
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
(define-condition-type 'dns-server-failure '(dns-server-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(define dns-server-failure? (condition-predicate 'dns-server-failure))
|
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
(define-condition-type 'dns-name-error '(dns-server-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(define dns-name-error? (condition-predicate 'dns-name-error))
|
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
(define-condition-type 'dns-not-implemented '(dns-server-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(define dns-not-implemented? (condition-predicate 'dns-not-implemented))
|
|
|
|
|
2002-09-16 12:16:04 -04:00
|
|
|
(define-condition-type 'dns-refused '(dns-server-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(define dns-refused? (condition-predicate 'dns-refused))
|
|
|
|
|
2002-11-25 11:47:00 -05:00
|
|
|
(define (dns-error condition . stuff)
|
|
|
|
(apply signal condition (dns-error->string condition) stuff))
|
|
|
|
|
|
|
|
(define (dns-error->string condition)
|
|
|
|
(string-append
|
|
|
|
"dns-error: "
|
|
|
|
(case condition
|
|
|
|
((parse-error)
|
|
|
|
"parse: error parsing server message")
|
|
|
|
((unexpected-eof-from-server)
|
|
|
|
"send-receive-message: unexpected EOF from server")
|
|
|
|
((bad-address)
|
|
|
|
"dns-get-information: bad address (in combination with query type)")
|
|
|
|
((no-nameservers)
|
|
|
|
"dns-find-nameserver: no nameservers found in /etc/resolv.conf")
|
2003-07-09 16:45:20 -04:00
|
|
|
((no-nameserver-given)
|
|
|
|
"dns-find-nameserver: no nameservers given")
|
2002-11-25 11:47:00 -05:00
|
|
|
((bad-nameserver)
|
2006-11-12 12:48:36 -05:00
|
|
|
"send-receive-message: could not establish connection to server(s)")
|
2002-11-25 11:47:00 -05:00
|
|
|
((not-a-hostname)
|
|
|
|
"no hostname given")
|
|
|
|
((not-a-ip)
|
|
|
|
"no ip given")
|
2006-11-12 12:48:36 -05:00
|
|
|
((dns-format-error)
|
2002-11-25 11:47:00 -05:00
|
|
|
"error from server: (1) format error")
|
|
|
|
((dns-server-failure)
|
|
|
|
"error from server: (2) server failure")
|
|
|
|
((dns-name-error)
|
|
|
|
"error from server: (3) name error")
|
|
|
|
((dns-not-implemented)
|
|
|
|
"error from server: (4) not implemented")
|
|
|
|
((dns-refused)
|
|
|
|
"error from server: (5) refused")
|
|
|
|
(else (error "Unknown dns-error" condition)))))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
;; --- (Q)Type and (Q)Class of a Message: ---
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
;; Message Type:
|
|
|
|
;; The IANA assigned type-values as in RFC 1035 and 3596 (IPv6) including the
|
|
|
|
;; super-set QTYPE starting at value 252+ that are needed for some queries.
|
|
|
|
(define-finite-type message-type :message-type
|
|
|
|
(value)
|
2003-01-16 15:08:14 -05:00
|
|
|
message-type?
|
2006-11-12 12:48:36 -05:00
|
|
|
the-message-type
|
2003-01-16 15:08:14 -05:00
|
|
|
message-type-name
|
2006-11-12 12:48:36 -05:00
|
|
|
message-type-index
|
|
|
|
(value message-type-number)
|
|
|
|
((unknown 0) ; Types which are not yet implemented
|
|
|
|
(a 1) ; A host address
|
|
|
|
(ns 2) ; An authoritative name server
|
|
|
|
(md 3) ; Obsolete
|
|
|
|
(mf 4) ; Obsolete
|
|
|
|
(cname 5) ; The canonical name for an alias
|
|
|
|
(soa 6) ; Marks the start of a zone of authority
|
|
|
|
(mb 7) ; Experimental
|
|
|
|
(mg 8) ; Experimental
|
|
|
|
(mr 9) ; Experimental
|
|
|
|
(null 10) ; Experimental
|
|
|
|
(wks 11) ; A well known service description (deprecated in RFC 1123)
|
|
|
|
(ptr 12) ; A domain name pointer
|
|
|
|
(hinfo 13) ; Host information
|
|
|
|
(minfo 14) ; Experimental
|
|
|
|
(mx 15) ; Mail exchange
|
|
|
|
(txt 16) ; Text strings
|
|
|
|
(aaaa 28) ; An IPv6 host address
|
|
|
|
(axfr 252) ; A request for a zone-transfer
|
|
|
|
(mailb 253) ; A request for mailbox-related records
|
|
|
|
(maila 254) ; Obsolete
|
|
|
|
(* 255))) ; A request for all records
|
|
|
|
|
|
|
|
|
|
|
|
;; Search for a message type, given the IANA-assigned value.
|
|
|
|
;; TYPE: number -> message-type or #f
|
|
|
|
(define (message-type-number->type number)
|
|
|
|
(let loop ((n (vector-length the-message-type)))
|
|
|
|
(if (= n 0)
|
|
|
|
#f
|
|
|
|
(let ((m (- n 1)))
|
|
|
|
(if (= number (message-type-number (vector-ref the-message-type m)))
|
|
|
|
(vector-ref the-message-type m)
|
|
|
|
(loop m))))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Returns the message-type for a symbol
|
|
|
|
;; TYPE: symbol -> message-type or #f
|
|
|
|
(define (message-type-symbol->type symbol)
|
|
|
|
(let loop ((n (vector-length the-message-type)))
|
|
|
|
(if (= n 0)
|
|
|
|
#f
|
|
|
|
(let ((m (- n 1)))
|
|
|
|
(if (eq? symbol (message-type-name (vector-ref the-message-type m)))
|
|
|
|
(vector-ref the-message-type m)
|
|
|
|
(loop m))))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Message Classes:
|
|
|
|
;; The resource-record class-values including the super-set QCLASS
|
|
|
|
;; (* 255)...
|
|
|
|
(define-finite-type message-class :message-class
|
|
|
|
(value)
|
2003-01-16 15:08:14 -05:00
|
|
|
message-class?
|
2006-11-12 12:48:36 -05:00
|
|
|
the-message-class
|
2003-01-16 15:08:14 -05:00
|
|
|
message-class-name
|
2006-11-12 12:48:36 -05:00
|
|
|
message-class-index
|
|
|
|
(value message-class-number)
|
|
|
|
((in 1) ; Internet
|
|
|
|
(cs 2) ; Obsolete
|
|
|
|
(ch 3) ; CHAOS
|
|
|
|
(hs 4) ; Hesiod
|
|
|
|
(* 255))) ; A QCLASS value: search for any class-entries.
|
|
|
|
|
|
|
|
|
|
|
|
;; Search for a message class, given the IANA-assigned value.
|
|
|
|
;; TYPE: number -> message-class or #f
|
|
|
|
(define (message-class-number->type number)
|
|
|
|
(let loop ((n (vector-length the-message-class)))
|
|
|
|
(if (= n 0)
|
|
|
|
#f
|
|
|
|
(let ((m (- n 1)))
|
|
|
|
(if (= number (message-class-number (vector-ref the-message-class m)))
|
|
|
|
(vector-ref the-message-class m)
|
|
|
|
(loop m))))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Returns the message-class for a symbol
|
|
|
|
;; TYPE: symbol -> message-class or #f
|
|
|
|
(define (message-class-symbol->type symbol)
|
|
|
|
(let loop ((n (vector-length the-message-class)))
|
|
|
|
(if (= n 0)
|
|
|
|
#f
|
|
|
|
(let ((m (- n 1)))
|
|
|
|
(if (eq? symbol (message-class-name (vector-ref the-message-class m)))
|
|
|
|
(vector-ref the-message-class m)
|
|
|
|
(loop m))))))
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;; -- useful stuff
|
|
|
|
|
2002-11-29 12:08:14 -05:00
|
|
|
;; number: 0<= x < 256
|
|
|
|
;; octet-pair: (char char)
|
|
|
|
;; octet-quad: (char char char char)
|
|
|
|
;; name: string *{"." string}
|
|
|
|
;; octets: *{(char *char)} nullchar
|
|
|
|
;; octet-ip: (char char char char)
|
|
|
|
;; address32: 0 <= x < 2^32-1
|
|
|
|
;; ip-string: "www.xxx.yyy.zzz"
|
|
|
|
;; ip-string-arpa: "zzz.yyy.xxx.www.in-addr.arpa"
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
;; encodes numbers (16bit) to octets
|
|
|
|
(define (number->octet-pair n)
|
|
|
|
(list (ascii->char (arithmetic-shift n -8))
|
|
|
|
(ascii->char (modulo n 256))))
|
|
|
|
|
|
|
|
;; decodes octets to numbers (16bit)
|
|
|
|
(define (octet-pair->number a b)
|
|
|
|
(+ (arithmetic-shift (char->ascii a) 8)
|
|
|
|
(char->ascii b)))
|
|
|
|
|
|
|
|
;; encodes numbers (32bit) to octets, needed for ttl
|
|
|
|
(define (number->octet-quad n)
|
|
|
|
(list (ascii->char (arithmetic-shift n -24))
|
|
|
|
(ascii->char (modulo (arithmetic-shift n -16) 256))
|
|
|
|
(ascii->char (modulo (arithmetic-shift n -8) 256))
|
|
|
|
(ascii->char (modulo n 256))))
|
|
|
|
|
|
|
|
;; decodes octets to numbers, needed for 32bit ttl
|
|
|
|
(define (octet-quad->number a b c d)
|
|
|
|
(+ (arithmetic-shift (char->ascii a) 24)
|
|
|
|
(arithmetic-shift (char->ascii b) 16)
|
|
|
|
(arithmetic-shift (char->ascii c) 8)
|
|
|
|
(char->ascii d)))
|
|
|
|
|
|
|
|
;; encodes a domain-name string to octets
|
|
|
|
(define (name->octets s)
|
|
|
|
(define (encode-portion s)
|
|
|
|
(cons
|
|
|
|
(ascii->char (string-length s))
|
|
|
|
(string->list s)))
|
2006-11-12 12:48:36 -05:00
|
|
|
|
|
|
|
(define *nul* (ascii->char 0))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
(let loop ((s s))
|
|
|
|
(cond
|
2002-11-27 04:36:21 -05:00
|
|
|
((regexp-search (rx (: bos (submatch (* (~ "."))) "." (submatch (* any))))
|
|
|
|
s)
|
2002-06-08 11:07:01 -04:00
|
|
|
=> (lambda (match)
|
|
|
|
(append
|
|
|
|
(encode-portion (match:substring match 1))
|
|
|
|
(loop (match:substring match 2)))))
|
|
|
|
(else
|
|
|
|
(if (= 0 (string-length s))
|
|
|
|
(list *nul*)
|
2002-11-29 12:08:14 -05:00
|
|
|
;;; TODO isn't this case an error?
|
2002-06-08 11:07:01 -04:00
|
|
|
(append
|
|
|
|
(encode-portion s)
|
|
|
|
(list *nul*)))))))
|
|
|
|
|
2002-11-27 04:36:21 -05:00
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
;; for tcp: message has to be tagged with its length
|
|
|
|
(define (add-size-tag m)
|
|
|
|
(append (number->octet-pair (length m)) m))
|
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
;; checks if v is an address32
|
2002-07-07 13:43:32 -04:00
|
|
|
(define (address32? v)
|
2006-11-12 12:48:36 -05:00
|
|
|
(and (number? v) (<= 0 v #xffffffff)))
|
2002-11-27 04:36:21 -05:00
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
;; filters types in a list of rrs
|
|
|
|
(define (filter-type list type)
|
|
|
|
(filter (lambda (rr)
|
2003-02-10 03:04:17 -05:00
|
|
|
(eq? (resource-record-type rr) type))
|
2002-06-08 11:07:01 -04:00
|
|
|
list))
|
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
;; sorts a mx-resource-record-list by preference. needed for dns-lookup-mail-exchanger.
|
2002-06-08 11:07:01 -04:00
|
|
|
(define (sort-by-preference mx-list)
|
|
|
|
(sort-list mx-list
|
|
|
|
(lambda (a b)
|
2006-11-12 12:48:36 -05:00
|
|
|
(< (resource-record-data-mx-preference (resource-record-data a))
|
|
|
|
(resource-record-data-mx-preference (resource-record-data b))))))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
;; returns an IP if available (additonal type-a processing)
|
2002-06-08 11:07:01 -04:00
|
|
|
(define (force-ip name)
|
|
|
|
(let loop ((result (dns-lookup-name name)))
|
2002-07-07 13:43:32 -04:00
|
|
|
(if (ip-string? result)
|
2002-06-08 11:07:01 -04:00
|
|
|
result
|
|
|
|
(loop (dns-lookup-name result)))))
|
|
|
|
|
|
|
|
;; returns a list of IPs (additional type-a processing)
|
|
|
|
(define (force-ip-list names)
|
|
|
|
(map (lambda (elem) (force-ip elem)) names))
|
|
|
|
|
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
;; ----------------------------------
|
|
|
|
;; --- Dealing with domain names: ---
|
|
|
|
;; ----------------------------------
|
|
|
|
|
|
|
|
;; Regexp matching a label of a name
|
|
|
|
(define label-regexp
|
|
|
|
(rx (: (| (: bos "." eos)
|
|
|
|
(:
|
|
|
|
bos
|
|
|
|
(submatch (| "*" (: alphanumeric (* (| alphanumeric "-")))))
|
|
|
|
"."
|
|
|
|
(submatch (| "" (: alphanumeric (* any))))
|
|
|
|
eos)))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Splits a name in its first label an the rest
|
|
|
|
;; Note: (dn-split? "de." will be ("de" . "")
|
|
|
|
;; TYPE: string -> (string . string) or #f
|
|
|
|
(define (dn-split? name)
|
|
|
|
(cond
|
|
|
|
((string=? name ".") #f)
|
|
|
|
((regexp-search label-regexp name)
|
|
|
|
=> (lambda (match)
|
|
|
|
(cons (match:substring match 1)
|
|
|
|
(match:substring match 2))))
|
|
|
|
(else #f)))
|
|
|
|
|
|
|
|
|
|
|
|
;; Look if a string is a FQDN
|
|
|
|
;; TYPE: string -> boolean
|
|
|
|
(define (fqdn? name)
|
|
|
|
(let loop ((name name))
|
|
|
|
(cond
|
|
|
|
((dn-split? name)
|
|
|
|
=> (lambda (p)
|
|
|
|
(let ((cp (cdr p)))
|
|
|
|
(if (string=? cp "")
|
|
|
|
#t
|
|
|
|
(loop (cdr p))))))
|
|
|
|
(else #f))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Cuts the first label from a FQDN and returns the rest of the name.
|
|
|
|
;; TYPE: string -> string
|
|
|
|
(define (cut-name name)
|
|
|
|
(cond
|
|
|
|
((regexp-search label-regexp name)
|
|
|
|
=> (lambda (match)
|
|
|
|
(let ((m (match:substring match 2)))
|
|
|
|
(if (string=? m "") "." m))))
|
|
|
|
(else #f)))
|
|
|
|
|
|
|
|
|
|
|
|
; Adds an ending dot to a string if neccessary.
|
|
|
|
; TYPE: string -> string
|
|
|
|
(define (make-fqdn-name name)
|
|
|
|
(let ((last-char (string-ref name (- (string-length name) 1))))
|
|
|
|
(if (not (char=? #\. last-char))
|
|
|
|
(string-append name ".")
|
|
|
|
name)))
|
|
|
|
|
|
|
|
|
|
|
|
; --- Message Constructors: ---
|
|
|
|
|
|
|
|
; A standard query header, usefull for most queries.
|
|
|
|
; See RFC 1035 (4.1.1) for details.
|
2003-01-22 07:27:50 -05:00
|
|
|
(define (make-std-query-header id question-count)
|
2006-11-12 12:48:36 -05:00
|
|
|
(let* ((qr 'query) ; querytype: query 0, response 1
|
|
|
|
(opcode 0) ; opcode: query 0, iquery 1 (OBSOLETE: see RFC 3425), status 2
|
2003-01-22 07:27:50 -05:00
|
|
|
(aa #f) ; authorative answer (in answers only)
|
|
|
|
(tc #f) ; truncation (size matters only with UDP)
|
|
|
|
(rd #t) ; recursion desired: nameserver pursues the query recursivly (optional)
|
|
|
|
(ra #f) ; recursion available (in answers only)
|
2006-11-12 12:48:36 -05:00
|
|
|
(zero 0) ; future use
|
|
|
|
(response-code 0) ; response code: error conditions (in answers only)
|
2003-01-17 13:29:47 -05:00
|
|
|
(question-count question-count)
|
2006-11-12 12:48:36 -05:00
|
|
|
(answer-count 0) ; answer count (in answers only)
|
2003-01-17 13:29:47 -05:00
|
|
|
(nameserver-count 0) ; name server resources (in answers only)
|
|
|
|
(additional-count 0)) ; additional records (in answers only)
|
2003-01-22 07:27:50 -05:00
|
|
|
|
|
|
|
(make-header
|
|
|
|
id
|
|
|
|
(make-flags qr opcode aa tc rd ra zero response-code)
|
|
|
|
question-count answer-count nameserver-count additional-count)))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
;; makes a query-message (header and question only)
|
2003-01-22 07:27:50 -05:00
|
|
|
;; TODO does this really work for several questions as well?
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (make-query-message header question . questions)
|
|
|
|
(let ((questions (cons question questions)))
|
|
|
|
(make-message header questions '() '() '()
|
|
|
|
(apply
|
|
|
|
append
|
|
|
|
(header->octets header)
|
|
|
|
(map question->octets questions)))))
|
|
|
|
|
|
|
|
(define (make-simple-query-message name type class)
|
2006-11-12 12:48:36 -05:00
|
|
|
(make-query-message (make-std-query-header (random-integer 65536) 1)
|
2003-02-10 03:04:17 -05:00
|
|
|
(make-question name type class)))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;; -- parsed message records
|
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
;; dns-message: complete data-structure of an dns-lookup
|
2003-01-15 07:17:54 -05:00
|
|
|
(define-record-type dns-message :dns-message
|
|
|
|
(make-dns-message query reply cache? protocol tried-nameservers)
|
|
|
|
dns-message?
|
|
|
|
(query dns-message-query)
|
|
|
|
(reply dns-message-reply)
|
|
|
|
(cache? dns-message-cache?)
|
|
|
|
(protocol dns-message-protocol)
|
|
|
|
(tried-nameservers dns-message-tried-nameservers))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; message
|
2003-01-15 07:17:54 -05:00
|
|
|
(define-record-type message :message
|
|
|
|
(make-message header questions answers nameservers additionals source)
|
|
|
|
message?
|
|
|
|
(header message-header)
|
|
|
|
(questions message-questions)
|
|
|
|
(answers message-answers)
|
|
|
|
(nameservers message-nameservers)
|
|
|
|
(additionals message-additionals)
|
2006-11-12 12:48:36 -05:00
|
|
|
(source message-source set-message-source!)) ; The bite-encoded message
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; header
|
2003-01-15 07:17:54 -05:00
|
|
|
(define-record-type header :header
|
2003-01-17 13:29:47 -05:00
|
|
|
(make-header id flags question-count answer-count nameserver-count
|
|
|
|
additional-count)
|
2003-01-15 07:17:54 -05:00
|
|
|
header?
|
|
|
|
(id header-id)
|
|
|
|
(flags header-flags)
|
2003-01-17 13:29:47 -05:00
|
|
|
(question-count header-question-count)
|
2006-11-12 12:48:36 -05:00
|
|
|
(answer-count header-answer-count set-header-answer-count!)
|
|
|
|
(nameserver-count header-nameserver-count set-header-nameserver-count!)
|
|
|
|
(additional-count header-additional-count set-header-additional-count!))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2003-01-22 07:27:50 -05:00
|
|
|
;;; -- message constructors: encode to octet-messages
|
|
|
|
|
|
|
|
;; makes an message header
|
|
|
|
(define (header->octets header)
|
|
|
|
(let* ((header-id (number->octet-pair (header-id header)))
|
|
|
|
(header-question-count (number->octet-pair (header-question-count header)))
|
|
|
|
(header-answer-count (number->octet-pair (header-answer-count header)))
|
|
|
|
(header-nameserver-count (number->octet-pair
|
|
|
|
(header-nameserver-count header)))
|
|
|
|
(header-additional-count (number->octet-pair
|
|
|
|
(header-additional-count header))))
|
|
|
|
(append header-id
|
|
|
|
(flags->octets (header-flags header))
|
|
|
|
header-question-count
|
|
|
|
header-answer-count
|
|
|
|
header-nameserver-count
|
|
|
|
header-additional-count)))
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
;; flags
|
2003-01-15 07:17:54 -05:00
|
|
|
(define-record-type flags :flags
|
2003-01-21 11:09:41 -05:00
|
|
|
(make-flags query-type opcode authoritative? truncated? recursion-desired?
|
2003-01-17 13:29:47 -05:00
|
|
|
recursion-available? zero response-code)
|
2003-01-15 07:17:54 -05:00
|
|
|
flags?
|
2003-01-21 09:33:54 -05:00
|
|
|
(query-type flags-query-type)
|
2003-01-17 13:29:47 -05:00
|
|
|
(opcode flags-opcode)
|
2006-11-12 12:48:36 -05:00
|
|
|
(authoritative? flags-authoritative? set-flags-authoritative!)
|
|
|
|
(truncated? flags-truncated? set-flags-truncated!)
|
2003-01-17 13:29:47 -05:00
|
|
|
(recursion-desired? flags-recursion-desired?)
|
2006-11-12 12:48:36 -05:00
|
|
|
(recursion-available? flags-recursion-available? set-flags-recursion-available!)
|
2003-01-17 13:29:47 -05:00
|
|
|
(zero flags-zero)
|
2006-11-12 12:48:36 -05:00
|
|
|
(response-code flags-response-code set-flags-response-code!))
|
2003-01-17 13:29:47 -05:00
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
(define (make-flags-from-numbers
|
|
|
|
querytype opcode authoritative? truncated? recursion-desired?
|
|
|
|
recursion-available? zero response-code)
|
2003-01-17 13:29:47 -05:00
|
|
|
(make-flags
|
|
|
|
(if (zero? querytype) 'query 'response)
|
|
|
|
opcode
|
|
|
|
(not (zero? authoritative?))
|
|
|
|
(not (zero? truncated?))
|
|
|
|
(not (zero? recursion-desired?))
|
|
|
|
(not (zero? recursion-available?))
|
|
|
|
zero
|
|
|
|
(case response-code
|
|
|
|
((0) 'dns-no-error)
|
|
|
|
((1) 'dns-format-error)
|
|
|
|
((2) 'dns-server-failure)
|
|
|
|
((3) 'dns-name-error)
|
|
|
|
((4) 'dns-not-implemented)
|
|
|
|
((5) 'dns-refused))))
|
|
|
|
|
2003-01-22 07:27:50 -05:00
|
|
|
(define (flags->octets flags)
|
|
|
|
(define (boolean->0/1 bool)
|
|
|
|
(if bool 1 0))
|
|
|
|
(list
|
2006-11-12 12:48:36 -05:00
|
|
|
(ascii->char
|
|
|
|
(+ (arithmetic-shift
|
|
|
|
(if (eq? (flags-query-type flags) 'query) 0 1) 7)
|
|
|
|
(arithmetic-shift (flags-opcode flags) 3)
|
|
|
|
(arithmetic-shift
|
|
|
|
(boolean->0/1 (flags-authoritative? flags)) 2)
|
|
|
|
(arithmetic-shift
|
|
|
|
(boolean->0/1 (flags-truncated? flags)) 1)
|
|
|
|
(boolean->0/1 (flags-recursion-desired? flags))))
|
|
|
|
(ascii->char
|
|
|
|
(+ (arithmetic-shift
|
|
|
|
(boolean->0/1 (flags-recursion-available? flags)) 7)
|
|
|
|
(arithmetic-shift (flags-zero flags) 4)
|
|
|
|
(let ((response-code (flags-response-code flags)))
|
|
|
|
(if (number? response-code)
|
|
|
|
response-code
|
|
|
|
(if (symbol? response-code)
|
|
|
|
(case response-code
|
|
|
|
((dns-no-error) 0)
|
|
|
|
((dns-format-error) 1)
|
|
|
|
((dns-server-failure) 2)
|
|
|
|
((dns-name-error) 3)
|
|
|
|
((dns-not-implemented) 4)
|
|
|
|
((dns-refused) 5)))))))))
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; question
|
2003-01-15 07:17:54 -05:00
|
|
|
(define-record-type question :question
|
2006-11-12 12:48:36 -05:00
|
|
|
(really-make-question name type class)
|
2003-01-15 07:17:54 -05:00
|
|
|
question?
|
|
|
|
(name question-name)
|
|
|
|
(type question-type)
|
|
|
|
(class question-class))
|
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
; Ensures the internal representation of a domain-name:
|
|
|
|
(define (make-question name type class)
|
|
|
|
(really-make-question (make-fqdn-name name) type class))
|
|
|
|
|
|
|
|
|
2003-01-22 07:27:50 -05:00
|
|
|
;; makes a question (name, type, class)
|
|
|
|
(define (question->octets q)
|
|
|
|
(let* ((qname (name->octets (question-name q)))
|
|
|
|
(qtype (number->octet-pair
|
|
|
|
(message-type-number (question-type q))))
|
|
|
|
(qclass (number->octet-pair
|
|
|
|
(message-class-number (question-class q)))))
|
|
|
|
(append qname qtype qclass)))
|
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
;;type resource-record
|
|
|
|
(define-record-type resource-record :resource-record
|
|
|
|
(make-resource-record name type class ttl data)
|
|
|
|
resource-record?
|
|
|
|
(name resource-record-name)
|
|
|
|
(type resource-record-type)
|
|
|
|
(class resource-record-class)
|
|
|
|
(ttl resource-record-ttl)
|
|
|
|
(data resource-record-data))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; cache
|
2003-01-15 07:17:54 -05:00
|
|
|
(define-record-type cache :cache
|
|
|
|
(make-cache answer ttl time)
|
|
|
|
cache?
|
|
|
|
(answer cache-answer)
|
|
|
|
(ttl cache-ttl)
|
|
|
|
(time cache-time))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;;; -- message parser
|
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
;; parses a domain-name in an message.
|
|
|
|
;; returns the name and the rest of the message.
|
2002-06-08 11:07:01 -04:00
|
|
|
(define (parse-name start message)
|
|
|
|
(let ((v (char->ascii (car start))))
|
|
|
|
(cond
|
|
|
|
((zero? v)
|
|
|
|
;; End of name
|
2006-11-12 12:48:36 -05:00
|
|
|
(values "." (cdr start)))
|
2002-06-08 11:07:01 -04:00
|
|
|
((zero? (bitwise-and #xc0 v))
|
|
|
|
;; Normal label
|
|
|
|
(let loop ((len v)
|
|
|
|
(start (cdr start))
|
|
|
|
(accum '()))
|
|
|
|
(cond
|
|
|
|
((zero? len)
|
|
|
|
(call-with-values
|
|
|
|
(lambda () (parse-name start message))
|
|
|
|
(lambda (s start)
|
|
|
|
(let ((s0 (list->string (reverse! accum))))
|
2006-11-12 12:48:36 -05:00
|
|
|
(values (if (string-ci=? s ".")
|
|
|
|
(string-append s0 ".")
|
|
|
|
(string-append s0 "." s))
|
2002-06-08 11:07:01 -04:00
|
|
|
start)))))
|
|
|
|
(else (loop (- len 1)
|
|
|
|
(cdr start)
|
|
|
|
(cons (car start) accum))))))
|
|
|
|
(else
|
|
|
|
;; Compression offset
|
|
|
|
(let ((offset (+ (arithmetic-shift (bitwise-and #x3f v) 8)
|
|
|
|
(char->ascii (cadr start)))))
|
|
|
|
(call-with-values
|
|
|
|
(lambda () (parse-name (list-tail message offset) message))
|
|
|
|
(lambda (s ignore-start)
|
|
|
|
(values s (cddr start)))))))))
|
|
|
|
|
|
|
|
;; parses a question in a message. returns the question and the rest of the message.
|
|
|
|
(define (parse-question start message)
|
|
|
|
(call-with-values
|
|
|
|
(lambda () (parse-name start message))
|
|
|
|
(lambda (name start)
|
2006-11-12 12:48:36 -05:00
|
|
|
(let ((type (message-type-number->type
|
|
|
|
(octet-pair->number (car start) (cadr start))))
|
2002-06-08 11:07:01 -04:00
|
|
|
(start (cddr start)))
|
2006-11-12 12:48:36 -05:00
|
|
|
(let ((class (message-class-number->type
|
|
|
|
(octet-pair->number (car start) (cadr start))))
|
2002-06-08 11:07:01 -04:00
|
|
|
(start (cddr start)))
|
|
|
|
(values (make-question name type class) start))))))
|
|
|
|
|
2004-03-24 15:52:12 -05:00
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
;; parses a resource-record in a message. returns the rr and the
|
|
|
|
;; rest of the message.
|
2002-06-08 11:07:01 -04:00
|
|
|
(define (parse-rr start message)
|
2006-11-12 12:48:36 -05:00
|
|
|
|
|
|
|
(define (type-number->type type-number)
|
|
|
|
(let ((type (message-type-number->type type-number)))
|
|
|
|
(if type
|
|
|
|
type
|
|
|
|
'unsupported-message-type)))
|
|
|
|
|
|
|
|
(define (class-number->class class-number)
|
|
|
|
(let ((class (message-class-number->type class-number)))
|
|
|
|
(if class
|
|
|
|
class
|
|
|
|
'unsupported-message-class)))
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
(call-with-values
|
2006-11-12 12:48:36 -05:00
|
|
|
(lambda () (parse-name start message))
|
|
|
|
(lambda (name start)
|
|
|
|
(let ((type (type-number->type
|
|
|
|
(octet-pair->number (car start) (cadr start))))
|
|
|
|
(start (cddr start)))
|
|
|
|
(let ((class (class-number->class
|
|
|
|
(octet-pair->number (car start) (cadr start))))
|
|
|
|
(start (cddr start)))
|
|
|
|
(let ((ttl (octet-quad->number (car start) (cadr start)
|
|
|
|
(caddr start) (cadddr start)))
|
|
|
|
(start (cddddr start)))
|
|
|
|
(let ((len (octet-pair->number (car start) (cadr start)))
|
|
|
|
(start (cddr start)))
|
|
|
|
;; Extract next len bytes of data:
|
|
|
|
(let loop ((len len)
|
|
|
|
(start start)
|
|
|
|
(accum '()))
|
|
|
|
(if (zero? len)
|
|
|
|
(values (make-resource-record
|
|
|
|
name type class ttl
|
|
|
|
(parse-resource-record-data
|
|
|
|
type class (reverse! accum) message)) start)
|
|
|
|
(loop (- len 1)
|
|
|
|
(cdr start)
|
|
|
|
(cons (car start) accum)))))))))))
|
|
|
|
|
|
|
|
|
|
|
|
;; -- Resource-Record records: ---
|
|
|
|
|
|
|
|
;; Data-Fields for the supported types of resource records:
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define-record-type resource-record-data-a :resource-record-data-a
|
|
|
|
(make-resource-record-data-a ip)
|
|
|
|
resource-record-data-a?
|
|
|
|
(ip resource-record-data-a-ip))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
(define-record-type resource-record-data-aaaa :resource-record-data-aaaa
|
|
|
|
(make-resource-record-data-aaaa ipv6)
|
|
|
|
resource-record-data-aaaa?
|
|
|
|
(ipv6 resource-record-data-aaaa-ipv6))
|
|
|
|
|
|
|
|
(define-record-type resource-record-data-ns :resource-record-data-ns
|
2003-02-10 03:04:17 -05:00
|
|
|
(make-resource-record-data-ns name)
|
|
|
|
resource-record-data-ns?
|
|
|
|
(name resource-record-data-ns-name))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define-record-type resource-record-data-cname :resource-record-data-cname
|
|
|
|
(make-resource-record-data-cname name)
|
|
|
|
resource-record-data-cname?
|
|
|
|
(name resource-record-data-cname-name))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define-record-type resource-record-data-hinfo :resource-record-data-hinfo
|
|
|
|
(make-resource-record-data-hinfo data)
|
|
|
|
resource-record-data-hinfo?
|
|
|
|
(data resource-record-data-hinfo-data))
|
|
|
|
|
|
|
|
(define-record-type resource-record-data-mx :resource-record-data-mx
|
|
|
|
(make-resource-record-data-mx preference exchanger)
|
|
|
|
resource-record-data-mx?
|
|
|
|
(preference resource-record-data-mx-preference)
|
|
|
|
(exchanger resource-record-data-mx-exchanger))
|
|
|
|
|
|
|
|
(define-record-type resource-record-data-ptr :resource-record-data-ptr
|
|
|
|
(make-resource-record-data-ptr name)
|
|
|
|
resource-record-data-ptr?
|
|
|
|
(name resource-record-data-ptr-name))
|
|
|
|
|
|
|
|
(define-record-type resource-record-data-soa :resource-record-data-soa
|
|
|
|
(make-resource-record-data-soa mname rname serial refresh retry expire minimum)
|
|
|
|
resource-record-data-soa?
|
|
|
|
(mname resource-record-data-soa-mname)
|
|
|
|
(rname resource-record-data-soa-rname)
|
|
|
|
(serial resource-record-data-soa-serial)
|
|
|
|
(refresh resource-record-data-soa-refresh)
|
|
|
|
(retry resource-record-data-soa-retry)
|
|
|
|
(expire resource-record-data-soa-expire)
|
|
|
|
(minimum resource-record-data-soa-minimum))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define-record-type resource-record-data-txt :resource-record-data-txt
|
|
|
|
(make-resource-record-data-txt text)
|
|
|
|
resource-record-data-txt?
|
|
|
|
(text resource-record-data-txt-text))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
;; ## not corretly implemented (deprecated in RFC 1123)
|
2003-02-10 03:04:17 -05:00
|
|
|
(define-record-type resource-record-data-wks :resource-record-data-wks
|
|
|
|
(make-resource-record-data-wks data)
|
|
|
|
resource-record-data-wks?
|
|
|
|
(data resource-record-data-wks-data))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
|
|
|
|
;; --------------------------------------------------
|
|
|
|
;; --- Message-(Un)-Parser / message-compression ---
|
|
|
|
;; --------------------------------------------------
|
|
|
|
|
|
|
|
;; FQDNs are stored together with the assiciated pointer-number in
|
|
|
|
;; an association-list (a-list)
|
|
|
|
;; The octet-message is stored in the o-list
|
|
|
|
|
|
|
|
|
|
|
|
;; Returns an (if possible) compressed octet-name
|
|
|
|
;; TYPE: string x l-of-octets x l-of-names-> l-of-octets x l-of-names
|
|
|
|
(define (mc-name->octets name o-list a-list)
|
|
|
|
;; TYPE: string -> octets
|
|
|
|
(define (encode-label s)
|
|
|
|
(cons (ascii->char (string-length s)) (string->list s)))
|
|
|
|
;; Returns elements from the list-of-names who matches name
|
|
|
|
(define (lookup? name a-list)
|
|
|
|
(let ((res
|
|
|
|
(fold-right
|
|
|
|
(lambda (e l)
|
|
|
|
(if (string-ci=? name (car e))
|
|
|
|
(cons e l)
|
|
|
|
l))
|
|
|
|
'()
|
|
|
|
a-list)))
|
|
|
|
(if (null? res) #f res)))
|
|
|
|
;; Create a pointer to another byte of an octet-message
|
|
|
|
(define (make-name-pointer number)
|
|
|
|
(if (<= number #x3FFF)
|
|
|
|
(number->octet-pair (bitwise-ior #xC000 number))
|
|
|
|
(error "This must be a very long message... .")))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
;; Nothing left?
|
|
|
|
((or (string=? name "") (string=? name "."))
|
|
|
|
(values (append o-list (list (ascii->char 0))) a-list))
|
|
|
|
;; Matching FQDN?
|
|
|
|
((lookup? name a-list)
|
|
|
|
=> (lambda (e)
|
|
|
|
(values (append o-list (make-name-pointer (cdar e)))
|
|
|
|
a-list)))
|
|
|
|
;; Cut-and-retry:
|
|
|
|
(else
|
|
|
|
(let ((dn-pair (dn-split? name)))
|
|
|
|
(if dn-pair
|
|
|
|
(mc-name->octets (cdr dn-pair)
|
|
|
|
(append o-list (encode-label (car dn-pair)))
|
|
|
|
(cons (cons name (length o-list)) a-list))
|
|
|
|
(error "mc-name->octets: Not a FQDN!"))))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Converts a message-question-record-type into octets
|
|
|
|
;; TYPE: msg-question x l-of-octets x l-of-names -> l-of-octets x l-of-names
|
|
|
|
(define (mc-question->octets q o-list a-list)
|
|
|
|
(receive (o-list a-list)
|
|
|
|
(mc-name->octets (question-name q) o-list a-list)
|
|
|
|
(values (append o-list
|
|
|
|
(number->octet-pair
|
|
|
|
(message-type-number (question-type q)))
|
|
|
|
(number->octet-pair
|
|
|
|
(message-class-number (question-class q))))
|
|
|
|
a-list)))
|
|
|
|
|
|
|
|
|
|
|
|
;; Translate the resource-record-data-*-content into octets.
|
|
|
|
;; TYPE: RR x list-of-octets x list-of-names -> list-of-octets x list-of-names
|
|
|
|
(define (mc-rdata->octets rr o-list a-list)
|
|
|
|
;; TXT and HINFO strings
|
|
|
|
(define (string-list->char-string-list l)
|
|
|
|
(let loop ((l l)
|
|
|
|
(r '()))
|
|
|
|
(if (null? l)
|
|
|
|
r
|
|
|
|
(let ((len (string-length (car l))))
|
|
|
|
(loop (cdr l)
|
|
|
|
(append
|
|
|
|
r
|
|
|
|
(list (ascii->char len))
|
|
|
|
(string->list (car l))))))))
|
|
|
|
|
|
|
|
(let ((rr-data (resource-record-data rr)))
|
|
|
|
(cond
|
|
|
|
;; Parser fallback:
|
|
|
|
((list? rr-data)
|
|
|
|
(values (append o-list rr-data) a-list))
|
|
|
|
;; Only IN supported...
|
|
|
|
((eq? (message-class in) (resource-record-class rr))
|
|
|
|
(cond
|
|
|
|
((resource-record-data-a? rr-data)
|
|
|
|
(values (append o-list
|
|
|
|
(address32->octet-ip
|
|
|
|
(resource-record-data-a-ip rr-data)))
|
|
|
|
a-list))
|
|
|
|
((resource-record-data-aaaa? rr-data) ;; TODO!
|
|
|
|
(values (append o-list (resource-record-data-aaaa-ipv6 rr-data))
|
|
|
|
a-list))
|
|
|
|
((resource-record-data-ns? rr-data)
|
|
|
|
(mc-name->octets (resource-record-data-ns-name rr-data) o-list a-list))
|
|
|
|
((resource-record-data-cname? rr-data)
|
|
|
|
(mc-name->octets (resource-record-data-cname-name rr-data)
|
|
|
|
o-list a-list))
|
|
|
|
((resource-record-data-mx? rr-data)
|
|
|
|
(mc-name->octets (resource-record-data-mx-exchanger rr-data)
|
|
|
|
(append o-list
|
|
|
|
(number->octet-pair
|
|
|
|
(resource-record-data-mx-preference rr-data)))
|
|
|
|
a-list))
|
|
|
|
((resource-record-data-ptr? rr-data)
|
|
|
|
(mc-name->octets (resource-record-data-ptr-name rr-data) o-list a-list))
|
|
|
|
((resource-record-data-soa? rr-data)
|
|
|
|
(receive (o-list a-list)
|
|
|
|
(mc-name->octets (resource-record-data-soa-mname rr-data)
|
|
|
|
o-list a-list)
|
|
|
|
(receive (o-list a-list)
|
|
|
|
(mc-name->octets
|
|
|
|
(resource-record-data-soa-rname rr-data)
|
|
|
|
o-list a-list)
|
|
|
|
(values
|
|
|
|
(append o-list
|
|
|
|
(number->octet-quad
|
|
|
|
(resource-record-data-soa-serial rr-data))
|
|
|
|
(number->octet-quad
|
|
|
|
(resource-record-data-soa-refresh rr-data))
|
|
|
|
(number->octet-quad
|
|
|
|
(resource-record-data-soa-retry rr-data))
|
|
|
|
(number->octet-quad
|
|
|
|
(resource-record-data-soa-expire rr-data))
|
|
|
|
(number->octet-quad
|
|
|
|
(resource-record-data-soa-minimum rr-data)))
|
|
|
|
a-list))))
|
|
|
|
((resource-record-data-txt? rr-data)
|
|
|
|
(values (append o-list (string-list->char-string-list
|
|
|
|
(resource-record-data-txt-text rr-data)))
|
|
|
|
a-list))
|
|
|
|
((resource-record-data-hinfo? rr-data)
|
|
|
|
(values (append o-list (string-list->char-string-list
|
|
|
|
(resource-record-data-hinfo-data rr-data))
|
|
|
|
a-list)))
|
|
|
|
((resource-record-data-wks? rr-data) ;; Deprecated
|
|
|
|
(values (append o-list (resource-record-data-wks-data rr-data) a-list)))
|
|
|
|
(else (error "unknown-resource-record"))))
|
|
|
|
(else (error "class not supported")))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Handle the conversion of a RR into octets:
|
|
|
|
;; TYPE: RR x list-of-octets x list-of-names -> list-of-octets x list-of-names
|
|
|
|
(define (mc-resource-record->octets r o-list a-list)
|
|
|
|
(receive (o-list a-list)
|
|
|
|
(mc-name->octets (resource-record-name r) o-list a-list)
|
|
|
|
(let* ((o-list
|
|
|
|
(append o-list
|
|
|
|
(number->octet-pair
|
|
|
|
(message-type-number (resource-record-type r)))
|
|
|
|
(number->octet-pair
|
|
|
|
(message-class-number (resource-record-class r)))
|
|
|
|
(number->octet-quad (resource-record-ttl r))))
|
|
|
|
;; A placeholder value for RDLENGTH:
|
|
|
|
(o-list-w (append o-list (number->octet-pair 0))))
|
|
|
|
(receive (o-list-1 a-list-1)
|
|
|
|
(mc-rdata->octets r o-list-w a-list)
|
|
|
|
(let* ((l-ol (length o-list-w))
|
|
|
|
(oc-data (list-tail o-list-1 l-ol)))
|
|
|
|
(values (append o-list
|
|
|
|
(number->octet-pair (length oc-data))
|
|
|
|
oc-data)
|
|
|
|
a-list-1))))))
|
|
|
|
|
|
|
|
|
|
|
|
;; Converts a message-record-type into octets:
|
|
|
|
;; TYPE: message -> octet-message
|
|
|
|
(define (mc-message->octets m)
|
|
|
|
;; Convert the RRs:
|
|
|
|
(define fold-proc-for-rrs
|
|
|
|
(lambda (e p)
|
|
|
|
(receive (o-list a-list)
|
|
|
|
(mc-resource-record->octets e (car p) (cdr p))
|
|
|
|
(cons o-list a-list))))
|
|
|
|
|
|
|
|
;;(pretty-print-dns-message m)
|
|
|
|
(let* ((oct-header (header->octets (message-header m)))
|
|
|
|
(oct-q-pair
|
|
|
|
(fold ;; this is fold-left (srfi-1)
|
|
|
|
(lambda (e p)
|
|
|
|
(receive (o-list a-list)
|
|
|
|
(mc-question->octets e (car p) (cdr p))
|
|
|
|
(cons o-list a-list)))
|
|
|
|
(cons oct-header '())
|
|
|
|
(message-questions m)))
|
|
|
|
(oct-answ-pair
|
|
|
|
(fold fold-proc-for-rrs oct-q-pair (message-answers m)))
|
|
|
|
(oct-auth-pair
|
|
|
|
(fold fold-proc-for-rrs oct-answ-pair (message-nameservers m)))
|
|
|
|
(oct-addi-pair
|
|
|
|
(fold fold-proc-for-rrs oct-auth-pair (message-additionals m))))
|
|
|
|
(car oct-addi-pair)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; ---
|
|
|
|
|
|
|
|
;; Parse the resource records data-field:
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (parse-resource-record-data type class data message)
|
2006-11-12 12:48:36 -05:00
|
|
|
|
|
|
|
(define (list-head l k)
|
|
|
|
(if (<= k (length l))
|
|
|
|
(let loop ((k k)
|
|
|
|
(l l)
|
|
|
|
(r '()))
|
|
|
|
(if (= 0 k)
|
|
|
|
r
|
|
|
|
(loop (- k 1)
|
|
|
|
(cdr l)
|
|
|
|
(append r (list (car l))))))
|
|
|
|
(error "list-head")))
|
|
|
|
|
|
|
|
(define (parse-char-strings data)
|
|
|
|
(let loop ((len (char->ascii (car data)))
|
|
|
|
(l (cdr data))
|
|
|
|
(res '()))
|
|
|
|
(let ((str (list->string (list-head l len))))
|
|
|
|
(if (> (length l) len)
|
|
|
|
(let ((tail (list-tail l len)))
|
|
|
|
(loop (char->ascii (car tail))
|
|
|
|
(cdr tail)
|
|
|
|
(append res (list str))))
|
|
|
|
(append res (list str))))))
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
(cond
|
2003-01-16 15:08:14 -05:00
|
|
|
((eq? type (message-type a))
|
2003-02-10 03:04:17 -05:00
|
|
|
(make-resource-record-data-a (octet-ip->address32 data)))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
((eq? type (message-type aaaa))
|
|
|
|
(make-resource-record-data-aaaa data)) ; TODO ...
|
|
|
|
|
|
|
|
|
2003-01-16 15:08:14 -05:00
|
|
|
((eq? type (message-type ns))
|
2003-02-10 03:04:17 -05:00
|
|
|
(make-resource-record-data-ns (call-with-values
|
2002-06-08 11:07:01 -04:00
|
|
|
(lambda () (parse-name data message))
|
|
|
|
(lambda (name rest) name))))
|
|
|
|
|
2003-01-16 15:08:14 -05:00
|
|
|
((eq? type (message-type cname))
|
2003-02-10 03:04:17 -05:00
|
|
|
(make-resource-record-data-cname (call-with-values
|
2002-06-08 11:07:01 -04:00
|
|
|
(lambda () (parse-name data message))
|
|
|
|
(lambda (name rest) name))))
|
|
|
|
|
2003-01-16 15:08:14 -05:00
|
|
|
((eq? type (message-type mx))
|
2003-02-10 03:04:17 -05:00
|
|
|
(make-resource-record-data-mx (octet-pair->number (car data) (cadr data))
|
2002-06-08 11:07:01 -04:00
|
|
|
(call-with-values
|
|
|
|
(lambda ()(parse-name (cddr data) message))
|
|
|
|
(lambda (name rest) name))))
|
|
|
|
|
2003-01-16 15:08:14 -05:00
|
|
|
((eq? type (message-type ptr))
|
2003-02-10 03:04:17 -05:00
|
|
|
(make-resource-record-data-ptr (call-with-values
|
2002-06-08 11:07:01 -04:00
|
|
|
(lambda () (parse-name data message))
|
|
|
|
(lambda (name rest) name))))
|
|
|
|
|
2003-01-16 15:08:14 -05:00
|
|
|
((eq? type (message-type soa))
|
2002-06-08 11:07:01 -04:00
|
|
|
(call-with-values
|
|
|
|
(lambda () (parse-name data message))
|
|
|
|
(lambda (mname rest)
|
|
|
|
(call-with-values
|
|
|
|
(lambda () (parse-name rest message))
|
|
|
|
(lambda (rname rest)
|
|
|
|
(let ((serial (octet-quad->number (car rest) (cadr rest) (caddr rest) (cadddr rest)))
|
|
|
|
(rest (cddddr rest)))
|
|
|
|
(let ((refresh (octet-quad->number (car rest) (cadr rest) (caddr rest) (cadddr rest)))
|
|
|
|
(rest (cddddr rest)))
|
|
|
|
(let ((retry (octet-quad->number (car rest) (cadr rest) (caddr rest) (cadddr rest)))
|
|
|
|
(rest (cddddr rest)))
|
|
|
|
(let ((expire (octet-quad->number (car rest) (cadr rest) (caddr rest) (cadddr rest)))
|
|
|
|
(rest (cddddr rest)))
|
|
|
|
(let ((minimum (octet-quad->number (car rest) (cadr rest) (caddr rest) (cadddr rest)))
|
|
|
|
(rest (cddddr rest)))
|
2003-02-10 03:04:17 -05:00
|
|
|
(make-resource-record-data-soa mname rname serial refresh retry expire minimum)))))))))))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2006-11-12 12:48:36 -05:00
|
|
|
;; The TXT&Hinfo-Type contains one or more <character-strings>
|
|
|
|
;; (see. Ch.3.3// RFC 1035
|
2003-01-16 15:08:14 -05:00
|
|
|
((eq? type (message-type txt))
|
2006-11-12 12:48:36 -05:00
|
|
|
(make-resource-record-data-txt
|
|
|
|
(parse-char-strings data)))
|
|
|
|
|
|
|
|
((eq? type (message-type hinfo))
|
|
|
|
(parse-char-strings data))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2003-01-16 15:08:14 -05:00
|
|
|
((eq? type (message-type wks))
|
2003-02-10 03:04:17 -05:00
|
|
|
(make-resource-record-data-wks data))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
(else (list data))))
|
|
|
|
|
|
|
|
;; parses n-times a message with parse. returns a list of parse-returns.
|
|
|
|
(define (parse-n parse start message n)
|
|
|
|
(let loop ((n n) (start start) (accum '()))
|
|
|
|
(if (zero? n)
|
|
|
|
(values (reverse! accum) start)
|
|
|
|
(call-with-values
|
|
|
|
(lambda () (parse start message))
|
|
|
|
(lambda (rr start)
|
|
|
|
(loop (- n 1) start (cons rr accum)))))))
|
|
|
|
|
|
|
|
;; parses a message-headers flags. returns the flags.
|
|
|
|
(define (parse-flags message)
|
|
|
|
(let ((v0 (list-ref message 2))
|
|
|
|
(v1 (list-ref message 3)))
|
|
|
|
;; Check for error code:
|
2003-01-17 13:29:47 -05:00
|
|
|
(let ((response-code (bitwise-and #xf (char->ascii v1)))
|
|
|
|
(zero (arithmetic-shift (bitwise-and 112 (char->ascii v1)) -4))
|
2002-06-08 11:07:01 -04:00
|
|
|
(ra (arithmetic-shift (bitwise-and 64 (char->ascii v1)) -7))
|
|
|
|
(rd (bitwise-and 1 (char->ascii v0)))
|
|
|
|
(tc (arithmetic-shift (bitwise-and 2 (char->ascii v0)) -1))
|
|
|
|
(aa (arithmetic-shift (bitwise-and 4 (char->ascii v0)) -2))
|
|
|
|
(opcode (arithmetic-shift (bitwise-and 120 (char->ascii v0)) -3))
|
|
|
|
(qr (arithmetic-shift (bitwise-and 128 (char->ascii v0)) -7)))
|
2003-01-17 13:29:47 -05:00
|
|
|
(make-flags-from-numbers qr opcode aa tc rd ra zero response-code))))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
;; parses a message-header. returns the header.
|
|
|
|
(define (parse-header message)
|
|
|
|
(let ((id (octet-pair->number (list-ref message 0) (list-ref message 1)))
|
|
|
|
(flags (parse-flags message))
|
2003-01-17 13:29:47 -05:00
|
|
|
(question-count (octet-pair->number (list-ref message 4) (list-ref message 5)))
|
2002-06-08 11:07:01 -04:00
|
|
|
(an-count (octet-pair->number (list-ref message 6) (list-ref message 7)))
|
|
|
|
(ns-count (octet-pair->number (list-ref message 8) (list-ref message 9)))
|
|
|
|
(ar-count (octet-pair->number (list-ref message 10) (list-ref message 11))))
|
2003-01-17 13:29:47 -05:00
|
|
|
(make-header id flags question-count an-count ns-count ar-count)))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
;; parses a message. returns the parsed message.
|
|
|
|
(define (parse message)
|
|
|
|
(let* ((header (parse-header message))
|
|
|
|
(start (list-tail message 12)))
|
|
|
|
(call-with-values
|
2003-01-17 13:29:47 -05:00
|
|
|
(lambda () (parse-n parse-question start message (header-question-count header)))
|
2002-06-08 11:07:01 -04:00
|
|
|
(lambda (qds start)
|
|
|
|
(call-with-values
|
2003-01-17 13:29:47 -05:00
|
|
|
(lambda () (parse-n parse-rr start message (header-answer-count header)))
|
2002-06-08 11:07:01 -04:00
|
|
|
(lambda (ans start)
|
|
|
|
(call-with-values
|
2003-01-17 13:29:47 -05:00
|
|
|
(lambda () (parse-n parse-rr start message (header-nameserver-count header)))
|
2002-06-08 11:07:01 -04:00
|
|
|
(lambda (nss start)
|
|
|
|
(call-with-values
|
2003-01-17 13:29:47 -05:00
|
|
|
(lambda () (parse-n parse-rr start message (header-additional-count header)))
|
2002-06-08 11:07:01 -04:00
|
|
|
(lambda (ars start)
|
|
|
|
(if (not (null? start))
|
2002-11-25 11:47:00 -05:00
|
|
|
(dns-error 'parse-error))
|
2002-06-08 11:07:01 -04:00
|
|
|
(make-message header qds ans nss ars message)))))))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; -- send, receive and validate message
|
|
|
|
|
|
|
|
;; checks if the received reply is valid. returns #t or error-msg.
|
|
|
|
(define (reply-acceptable? reply query)
|
|
|
|
;; Check correct id
|
2003-01-22 07:27:50 -05:00
|
|
|
(if (not (= (header-id (message-header reply))
|
|
|
|
(header-id (message-header query))))
|
|
|
|
;; TODO replace error
|
|
|
|
(error "send-receive-message: bad reply id from server"))
|
2002-06-08 11:07:01 -04:00
|
|
|
;; Check for error code:
|
2003-01-22 07:27:50 -05:00
|
|
|
(let ((response-code (flags-response-code
|
|
|
|
(header-flags (message-header reply)))))
|
2003-01-17 13:29:47 -05:00
|
|
|
(if (not (eq? response-code 'dns-no-error))
|
|
|
|
(dns-error response-code))))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2003-07-09 16:45:20 -04:00
|
|
|
(define *max-tries* 3)
|
|
|
|
(define *timeout* 1)
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
;; connects to nameserver and sends and receives messages. returns the reply.
|
|
|
|
;; here: via TCP
|
2006-11-12 12:48:36 -05:00
|
|
|
(define (send-receive-message-tcp nameserver query)
|
|
|
|
(send-receive-message-tcp-int nameserver query reply-acceptable?))
|
|
|
|
|
|
|
|
(define (send-receive-message-tcp-int nameservers query accept?)
|
2002-11-25 11:47:00 -05:00
|
|
|
(receive (reply hit-ns other-nss)
|
2003-08-19 10:36:08 -04:00
|
|
|
(let ((sockets (map
|
2003-07-09 16:45:20 -04:00
|
|
|
(lambda (nameserver)
|
|
|
|
(let ((sock (create-socket protocol-family/internet
|
|
|
|
socket-type/stream))
|
|
|
|
(addr (internet-address->socket-address
|
|
|
|
nameserver 53)))
|
|
|
|
;; we ignore the return value and select
|
|
|
|
;; unconditionally later
|
|
|
|
(call-with-current-continuation
|
|
|
|
(lambda (k)
|
|
|
|
(with-handler (lambda (cond more)
|
|
|
|
(k #f))
|
|
|
|
(lambda ()
|
|
|
|
(connect-socket-no-wait sock addr)
|
|
|
|
sock))))))
|
2003-08-19 10:36:08 -04:00
|
|
|
nameservers)))
|
|
|
|
(let* ((nameservers
|
|
|
|
(let loop ((sockets sockets)
|
|
|
|
(nss nameservers))
|
|
|
|
(cond
|
|
|
|
((or (null? sockets) (null? nss)) '())
|
|
|
|
((socket? (car sockets))
|
|
|
|
(cons (car nss) (loop (cdr sockets) (cdr nss))))
|
|
|
|
(else (loop (cdr sockets) (cdr nss))))))
|
|
|
|
|
|
|
|
(sockets (filter socket? sockets))
|
|
|
|
(ws (map socket:outport sockets))
|
2002-11-25 11:47:00 -05:00
|
|
|
(wport-nameserver-alist (map cons ws nameservers))
|
|
|
|
(wport-socket-alist (map cons ws sockets)))
|
|
|
|
(dynamic-wind
|
2003-07-09 16:45:20 -04:00
|
|
|
(lambda ()
|
|
|
|
'nothing-to-be-done-before)
|
2002-11-25 11:47:00 -05:00
|
|
|
(lambda ()
|
2003-07-09 16:45:20 -04:00
|
|
|
(let loop-port-channels ((tried-channels '())
|
|
|
|
(number-tries 1))
|
|
|
|
(letrec ((delete-list
|
|
|
|
(lambda (elems list)
|
|
|
|
(cond
|
|
|
|
((null? elems) list)
|
|
|
|
((null? list) '())
|
|
|
|
(else (delete-list (cdr elems) (delete (car elems) list))))))
|
|
|
|
(ws-new (delete-list tried-channels ws)))
|
|
|
|
(if (or (null? ws-new) (>= number-tries *max-tries*))
|
|
|
|
(dns-error 'bad-nameserver)
|
2006-11-12 12:48:36 -05:00
|
|
|
(let ((ready (apply select-ports *timeout* ws)))
|
|
|
|
;; (let ((ready (apply select-port-channels *timeout* ws)))
|
2003-07-09 16:45:20 -04:00
|
|
|
(if (= (length tried-channels) (length ws))
|
|
|
|
(dns-error 'bad-nameserver)
|
|
|
|
(let loop-ready-channels ((ready-channels ready))
|
|
|
|
(if (null? ready-channels)
|
|
|
|
(loop-port-channels (append tried-channels ready) (+ number-tries 1))
|
|
|
|
(let* ((w (car ready-channels))
|
|
|
|
(hit-ns (cdr (assoc w wport-nameserver-alist)))
|
|
|
|
(sock (cdr (assoc w wport-socket-alist))))
|
|
|
|
(if (not (connect-socket-successful? sock))
|
|
|
|
(loop-ready-channels (cdr ready-channels))
|
|
|
|
(let ((query-string
|
|
|
|
(list->string (add-size-tag (message-source query))))
|
|
|
|
(r (socket:inport sock)))
|
|
|
|
(call-with-current-continuation
|
|
|
|
(lambda (k)
|
2004-01-27 11:29:42 -05:00
|
|
|
(with-handler (lambda (cond more)
|
2003-07-09 16:45:20 -04:00
|
|
|
(k (loop-ready-channels (cdr ready-channels))))
|
|
|
|
(lambda ()
|
|
|
|
(display query-string w)
|
|
|
|
(force-output w)
|
|
|
|
(let ((a (read-char r))
|
|
|
|
(b (read-char r)))
|
|
|
|
(let ((len (octet-pair->number a b)))
|
|
|
|
(let ((s (read-string len r)))
|
|
|
|
(if (and (not (= 0 (string-length s)))
|
|
|
|
(not (= len (string-length s))))
|
|
|
|
(dns-error 'unexpected-eof-from-server))
|
|
|
|
(values (parse (string->list s))
|
|
|
|
hit-ns
|
|
|
|
(delete hit-ns nameservers))))))))))))))))))))
|
2002-11-25 11:47:00 -05:00
|
|
|
(lambda ()
|
|
|
|
(for-each close-socket sockets)))))
|
2006-11-12 12:48:36 -05:00
|
|
|
(accept? reply query)
|
|
|
|
(values reply hit-ns other-nss)))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; here: via UDP
|
2006-11-12 12:48:36 -05:00
|
|
|
(define (send-receive-message-udp nameserver query)
|
|
|
|
(send-receive-message-udp-int nameserver query reply-acceptable?))
|
|
|
|
|
|
|
|
(define (send-receive-message-udp-int nameservers query accept?)
|
2002-11-25 11:47:00 -05:00
|
|
|
(receive (reply hit-ns other-nss)
|
|
|
|
(let ((sockets (map (lambda (nameserver)
|
|
|
|
(let ((sock (create-socket protocol-family/internet
|
|
|
|
socket-type/datagram))
|
|
|
|
(addr (internet-address->socket-address
|
|
|
|
nameserver 53)))
|
|
|
|
(connect-socket sock addr)
|
|
|
|
sock))
|
|
|
|
nameservers)))
|
|
|
|
(let ((rs (map socket:inport sockets))
|
|
|
|
(ws (map socket:outport sockets)))
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda ()
|
|
|
|
'nothing-to-be-done-before)
|
|
|
|
(lambda ()
|
2003-01-22 07:27:50 -05:00
|
|
|
(let ((query-string (list->string (message-source query)))
|
2002-11-25 11:47:00 -05:00
|
|
|
(rsv (list->vector rs))
|
|
|
|
(rport-nameserver-alist (map cons rs nameservers))
|
|
|
|
(rport-socket-alist (map cons rs sockets)))
|
|
|
|
(for-each (lambda (w) (display query-string w)) ws)
|
|
|
|
(for-each force-output ws)
|
2003-07-09 16:45:20 -04:00
|
|
|
(let loop-port-channels ((tried-channels '())
|
|
|
|
(number-tries 1))
|
|
|
|
(letrec ((delete-list
|
|
|
|
(lambda (elems list)
|
|
|
|
(cond
|
|
|
|
((null? elems) list)
|
|
|
|
((null? list) '())
|
|
|
|
(else (delete-list (cdr elems) (delete (car elems) list))))))
|
|
|
|
(rs-new (delete-list tried-channels rs)))
|
|
|
|
(if (or (null? rs-new) (>= number-tries *max-tries*))
|
|
|
|
(dns-error 'bad-nameserver)
|
2006-11-12 12:48:36 -05:00
|
|
|
(let ((ready (apply select-ports *timeout* rs-new)))
|
|
|
|
;; (let ((ready (apply select-port-channels *timeout* rs-new)))
|
2003-07-09 16:45:20 -04:00
|
|
|
(if (= (length tried-channels) (length rs))
|
|
|
|
(dns-error 'bad-nameserver)
|
|
|
|
(let loop-ready-channels ((ready-channels ready))
|
|
|
|
(if (null? ready-channels)
|
|
|
|
(loop-port-channels (append tried-channels ready) (+ number-tries 1))
|
|
|
|
(let* ((r (car ready-channels))
|
|
|
|
(hit-ns (cdr (assoc r rport-nameserver-alist))))
|
|
|
|
(if (not (connect-socket-successful? (cdr (assoc r rport-socket-alist))))
|
|
|
|
(loop-ready-channels (cdr ready-channels))
|
|
|
|
;; 512 is the maximum udp-message size:
|
|
|
|
(let ((answer (string->list (read-string/partial 512 r))))
|
|
|
|
(if (null? answer)
|
|
|
|
(loop-ready-channels (cdr ready-channels))
|
|
|
|
(values (parse answer)
|
|
|
|
hit-ns
|
|
|
|
(delete hit-ns nameservers)))))))))))))))
|
2002-11-25 11:47:00 -05:00
|
|
|
(lambda ()
|
|
|
|
(for-each close-socket sockets)))))
|
2006-11-12 12:48:36 -05:00
|
|
|
(accept? reply query)
|
2003-07-09 16:45:20 -04:00
|
|
|
(if (flags-truncated? (header-flags (message-header reply)))
|
2006-11-12 12:48:36 -05:00
|
|
|
(send-receive-message-tcp-int nameservers query accept?)
|
|
|
|
(values reply hit-ns other-nss))))
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;; -- cache
|
|
|
|
|
2002-11-25 11:47:00 -05:00
|
|
|
;; creates the cache, an empty string-table
|
2006-11-12 12:48:36 -05:00
|
|
|
(define *cache* (make-string-table))
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
;; resets the cache
|
2003-01-17 13:29:47 -05:00
|
|
|
(define (dns-clear-cache!)
|
2006-11-12 12:48:36 -05:00
|
|
|
(set! *cache* (make-string-table)))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; searches in a dns-msg for the shortest ttl. this is needed for cache-management.
|
|
|
|
(define (find-shortest-ttl dns-msg)
|
|
|
|
(letrec ((minimum #f)
|
|
|
|
(find-shortest-ttl-1
|
|
|
|
(lambda (dns-msg)
|
|
|
|
(cond
|
|
|
|
((dns-message? dns-msg)
|
2003-01-15 07:17:54 -05:00
|
|
|
(find-shortest-ttl-1 (dns-message-reply dns-msg)))
|
2002-06-08 11:07:01 -04:00
|
|
|
((message? dns-msg)
|
2003-01-15 07:17:54 -05:00
|
|
|
(for-each (lambda (x) (find-shortest-ttl-1 x)) (message-answers dns-msg))
|
|
|
|
(for-each (lambda (x) (find-shortest-ttl-1 x)) (message-nameservers dns-msg))
|
|
|
|
(for-each (lambda (x) (find-shortest-ttl-1 x)) (message-additionals dns-msg))
|
2002-06-08 11:07:01 -04:00
|
|
|
minimum)
|
2003-02-10 03:04:17 -05:00
|
|
|
((resource-record? dns-msg)
|
2002-06-08 11:07:01 -04:00
|
|
|
(cond
|
2003-02-10 03:04:17 -05:00
|
|
|
((not minimum) (set! minimum (resource-record-ttl dns-msg)))
|
2002-06-08 11:07:01 -04:00
|
|
|
(else
|
2003-02-10 03:04:17 -05:00
|
|
|
(if (and (not minimum) (> minimum (resource-record-ttl dns-msg)))
|
|
|
|
(set! minimum (resource-record-ttl dns-msg))))))))))
|
2002-06-08 11:07:01 -04:00
|
|
|
(find-shortest-ttl-1 dns-msg)))
|
|
|
|
|
2002-11-19 17:41:37 -05:00
|
|
|
|
|
|
|
(define (make-key qds nameserver)
|
|
|
|
(let*;; cache-key relevant data
|
2003-01-15 07:17:54 -05:00
|
|
|
((name (question-name (car qds)))
|
|
|
|
(type (question-type (car qds)))
|
|
|
|
(class (question-class (car qds))))
|
2003-01-16 15:08:14 -05:00
|
|
|
(format #f "~a;~a;~a;~a"
|
|
|
|
nameserver
|
|
|
|
name
|
|
|
|
(message-type-name type)
|
|
|
|
(message-class-name class))))
|
2002-11-19 17:41:37 -05:00
|
|
|
|
|
|
|
(define (lookup-cache qds nameserver)
|
|
|
|
(let* ((key (make-key qds nameserver))
|
2006-11-12 12:48:36 -05:00
|
|
|
(found-data (table-ref *cache* key)))
|
2002-11-19 17:41:37 -05:00
|
|
|
(cond
|
|
|
|
((and found-data
|
|
|
|
;; checks if cached-data is still valid
|
2003-01-15 07:17:54 -05:00
|
|
|
(< (time) (+ (cache-time found-data) (cache-ttl found-data))))
|
2002-11-19 17:41:37 -05:00
|
|
|
found-data)
|
|
|
|
(else #f))))
|
|
|
|
|
|
|
|
(define (update-cache! key entry)
|
2006-11-12 12:48:36 -05:00
|
|
|
(table-set! *cache* key entry))
|
2002-11-19 17:41:37 -05:00
|
|
|
|
2003-01-22 07:27:50 -05:00
|
|
|
(define (dns-query-no-cache query protocol nameservers tried)
|
2002-11-19 17:41:37 -05:00
|
|
|
;; returns new retrieved data
|
|
|
|
(receive (dns-msg hit-ns nss-with-no-reply)
|
2003-01-22 07:27:50 -05:00
|
|
|
(send-receive-message nameservers query protocol)
|
2002-11-19 17:41:37 -05:00
|
|
|
(values
|
2003-01-22 07:27:50 -05:00
|
|
|
(make-dns-message query dns-msg #f protocol (reverse tried))
|
2002-11-19 17:41:37 -05:00
|
|
|
hit-ns
|
2002-11-25 11:47:00 -05:00
|
|
|
nss-with-no-reply)))
|
2002-11-19 17:41:37 -05:00
|
|
|
|
2003-01-22 07:27:50 -05:00
|
|
|
(define (dns-query-with-cache query protocol nameservers tried)
|
|
|
|
(let ((qds (message-questions query)))
|
2002-11-19 17:41:37 -05:00
|
|
|
(let lp ((ns nameservers))
|
|
|
|
(if (null? ns)
|
2006-11-12 12:48:36 -05:00
|
|
|
(receive
|
|
|
|
(reply-msg hit-ns nss-with-no-reply)
|
|
|
|
(send-receive-message nameservers query protocol)
|
|
|
|
(update-cache! (make-key qds hit-ns)
|
|
|
|
(make-cache reply-msg (find-shortest-ttl reply-msg) (time)))
|
|
|
|
;; returns new retrieved data and updates cache
|
|
|
|
(values (make-dns-message query reply-msg #f protocol (reverse tried))
|
|
|
|
hit-ns
|
|
|
|
nss-with-no-reply))
|
2002-11-19 17:41:37 -05:00
|
|
|
(cond ((lookup-cache qds (car ns))
|
|
|
|
=> (lambda (found-data)
|
|
|
|
;; returns cached data
|
2003-01-22 07:27:50 -05:00
|
|
|
(values (make-dns-message query (cache-answer found-data) #t protocol '())
|
2002-11-19 17:41:37 -05:00
|
|
|
#f
|
|
|
|
nameservers)))
|
|
|
|
(else (lp (cdr ns))))))))
|
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define-enumerated-type network-protocol :network-protocol
|
|
|
|
network-protocol?
|
|
|
|
network-protocols
|
|
|
|
network-protocol-name
|
|
|
|
network-protocol-index
|
|
|
|
(udp tcp))
|
|
|
|
|
2003-01-22 07:27:50 -05:00
|
|
|
(define (send-receive-message nameservers query protocol)
|
2002-11-19 17:41:37 -05:00
|
|
|
((cond
|
2003-02-10 03:04:17 -05:00
|
|
|
((eq? protocol (network-protocol tcp)) send-receive-message-tcp)
|
|
|
|
((eq? protocol (network-protocol udp)) send-receive-message-udp))
|
2003-01-22 07:27:50 -05:00
|
|
|
nameservers query))
|
2002-11-19 17:41:37 -05:00
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
;; makes a dns-query. optional cache-check.
|
|
|
|
;; returns a dns-message with cache-flag and either cache-data or new received data.
|
2003-01-22 07:27:50 -05:00
|
|
|
(define (dns-query/cache query use-cache? protocol nameservers tried)
|
2002-06-08 11:07:01 -04:00
|
|
|
(if use-cache?
|
2003-01-22 07:27:50 -05:00
|
|
|
(dns-query-with-cache query protocol nameservers tried)
|
|
|
|
(dns-query-no-cache query protocol nameservers tried)))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; dns and recursion
|
|
|
|
;; recursion means, if the demanded information is not available from the
|
|
|
|
;; nameserver, another nameserver (usualy an authority) has to be contacted.
|
|
|
|
;; normally the recursion is done for us by the nameserver istself, but
|
|
|
|
;; this feature is technically optional (RFC 1035).
|
|
|
|
;; dns-get-information implements the resovler-side recursion.
|
2006-11-12 12:48:36 -05:00
|
|
|
;; it returns a dns-message.
|
|
|
|
; (The means of trying recursion are given by the check-answer proc)
|
|
|
|
|
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (dns-get-information query protocol check-answer . args)
|
|
|
|
(receive (nameservers use-cache?) (lookup-optional-args args)
|
|
|
|
(let lp ((tried '()) (nss nameservers))
|
2006-11-12 12:48:36 -05:00
|
|
|
(display "Tried nameservers: ")
|
|
|
|
(map (lambda (x) (display (address32->ip-string x)) (display " ")) tried)(newline)
|
|
|
|
(display "Nameserver list: ")
|
|
|
|
(map (lambda (x) (display (address32->ip-string x)) (display " ")) nss)(newline)
|
2003-02-10 03:04:17 -05:00
|
|
|
(if (null? nss)
|
2003-07-09 16:45:20 -04:00
|
|
|
(if (null? tried)
|
|
|
|
(dns-error 'no-nameserver-given)
|
|
|
|
(dns-error 'bad-address))
|
2006-11-12 12:48:36 -05:00
|
|
|
(receive
|
|
|
|
(dns-msg hit-ns nss-with-no-reply)
|
|
|
|
(dns-query/cache query use-cache? protocol nss tried)
|
|
|
|
(if (check-answer dns-msg)
|
|
|
|
dns-msg
|
|
|
|
(let ((auth? (flags-authoritative?
|
|
|
|
(header-flags
|
|
|
|
(message-header
|
|
|
|
(dns-message-reply dns-msg))))))
|
|
|
|
(if auth?
|
|
|
|
(dns-error 'bad-address)
|
|
|
|
;; other nameservers names are found in the nameserver-part,
|
|
|
|
;; but their ip-adresses are found in the additonal-rrs
|
|
|
|
(let* ((other-nameservers-rr
|
|
|
|
(filter (lambda (elem)
|
|
|
|
(eq? (resource-record-type elem)
|
|
|
|
(message-type a)))
|
|
|
|
(message-additionals
|
|
|
|
(dns-message-reply dns-msg))))
|
|
|
|
(other-nameservers
|
|
|
|
(map (lambda (e)
|
|
|
|
(resource-record-data-a-ip
|
|
|
|
(resource-record-data e)))
|
|
|
|
other-nameservers-rr)))
|
|
|
|
(lp (if (not (member hit-ns tried))
|
|
|
|
(cons hit-ns tried)
|
|
|
|
tried)
|
|
|
|
(lset-union equal?
|
|
|
|
nss-with-no-reply
|
|
|
|
(lset-difference equal?
|
|
|
|
other-nameservers
|
|
|
|
tried))))))))))))
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2002-12-03 11:02:17 -05:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Parsing of /etc/resolv.conf
|
|
|
|
|
2003-02-10 05:48:46 -05:00
|
|
|
(define-condition-type 'resolv.conf-parse-error '(dns-error))
|
|
|
|
(define resolv.conf-parse-error? (condition-predicate 'resolv.conf-parse-error))
|
|
|
|
|
2002-12-03 11:02:17 -05:00
|
|
|
(define (parse-nameserver rest-of-line)
|
|
|
|
(let ((match (regexp-search
|
|
|
|
(rx (: (submatch (** 1 3 digit) "."
|
|
|
|
(** 1 3 digit) "."
|
|
|
|
(** 1 3 digit) "."
|
|
|
|
(** 1 3 digit))
|
|
|
|
(* white))); don't complain about tailing white space
|
|
|
|
rest-of-line)))
|
|
|
|
(if match
|
|
|
|
(cons 'nameserver (match:substring match 1))
|
|
|
|
(signal 'resolv.conf-parse-error))))
|
|
|
|
|
|
|
|
; could be more restrictive...
|
|
|
|
(define domain-name-regexp (rx (+ (| alphanum #\. #\-))))
|
|
|
|
|
|
|
|
(define (parse-domain rest-of-line)
|
|
|
|
(let ((match (regexp-search
|
|
|
|
(rx (: (submatch ,domain-name-regexp)
|
|
|
|
(* white))); don't complain about tailing white space
|
|
|
|
rest-of-line)))
|
|
|
|
(if match
|
|
|
|
(cons 'domain (match:substring match 1))
|
|
|
|
(signal 'resolv.conf-parse-error))))
|
|
|
|
(define (parse-search rest-of-line)
|
|
|
|
(let ((domains (regexp-fold-right domain-name-regexp
|
|
|
|
(lambda (match junk accu)
|
|
|
|
(cons (match:substring match 0) accu))
|
|
|
|
'()
|
|
|
|
rest-of-line)))
|
|
|
|
(if (null? domains)
|
|
|
|
(signal 'resolv.conf-parse-error)
|
|
|
|
(cons 'search domains))))
|
|
|
|
|
|
|
|
(define (parse-sortlist rest-of-line)
|
|
|
|
(let ((netmask-pairs (regexp-fold-right (rx (+ (| digit #\. #\/)))
|
|
|
|
(lambda (match junk accu)
|
|
|
|
(cons (match:substring match 0) accu))
|
|
|
|
'()
|
|
|
|
rest-of-line)))
|
|
|
|
(if (null? netmask-pairs)
|
|
|
|
(signal 'resolv.conf-parse-error)
|
|
|
|
(cons 'sortlist netmask-pairs))))
|
|
|
|
|
|
|
|
(define (parse-options rest-of-line)
|
|
|
|
(regexp-fold-right
|
|
|
|
(rx (| "debug" "no_tld_query" (: "ndots:" (submatch digit))))
|
|
|
|
(lambda (match junk accu)
|
|
|
|
(let ((str (match:substring match 0)))
|
|
|
|
(cond ((string=? str "debug")
|
|
|
|
(cons 'debug accu))
|
|
|
|
((string=? str "no_tld_query")
|
|
|
|
(cons 'no_tld_query accu))
|
|
|
|
(else (cons (cons 'ndots
|
|
|
|
(string->number (match:substring match 1))) accu)))))
|
|
|
|
'()
|
|
|
|
rest-of-line))
|
|
|
|
|
2002-12-22 08:45:38 -05:00
|
|
|
(define *resolv.conf-cache*)
|
|
|
|
(define *resolv.conf-cache-date* 0)
|
2003-06-30 06:57:26 -04:00
|
|
|
(define *resolv.conf-file* "/etc/resolv.conf")
|
2002-12-22 08:45:38 -05:00
|
|
|
|
2003-01-16 08:07:14 -05:00
|
|
|
(define (resolv.conf)
|
2003-06-30 06:57:26 -04:00
|
|
|
(let ((actual-m-time (file-info:mtime (file-info *resolv.conf-file*))))
|
2002-12-22 08:45:38 -05:00
|
|
|
(if (> actual-m-time *resolv.conf-cache-date*)
|
2003-01-16 08:07:14 -05:00
|
|
|
(parse-resolv.conf!))
|
|
|
|
*resolv.conf-cache*))
|
|
|
|
|
|
|
|
(define (parse-resolv.conf!)
|
2003-06-30 06:57:26 -04:00
|
|
|
(let ((actual-m-time (file-info:mtime (file-info *resolv.conf-file*)))
|
|
|
|
(contents (really-parse-resolv.conf *resolv.conf-file*)))
|
2003-01-16 08:07:14 -05:00
|
|
|
(set! *resolv.conf-cache* contents)
|
|
|
|
(set! *resolv.conf-cache-date* actual-m-time)))
|
|
|
|
|
2002-12-22 08:45:38 -05:00
|
|
|
(define (really-parse-resolv.conf file-name)
|
2002-12-06 07:52:23 -05:00
|
|
|
|
|
|
|
;; accumulate nameserver entries
|
|
|
|
;; domain and search are mutual exclusive, take the last
|
2002-12-03 11:02:17 -05:00
|
|
|
(define (adjust-result rev-result have-search-or-domain? nameservers)
|
|
|
|
(cond ((null? rev-result)
|
|
|
|
(if (null? nameservers)
|
|
|
|
'()
|
|
|
|
(list (cons 'nameserver nameservers))))
|
|
|
|
((eq? (caar rev-result) 'domain)
|
|
|
|
(if have-search-or-domain?
|
|
|
|
(adjust-result (cdr rev-result) have-search-or-domain? nameservers)
|
|
|
|
(cons (car rev-result)
|
|
|
|
(adjust-result (cdr rev-result)
|
|
|
|
#t
|
|
|
|
nameservers))))
|
|
|
|
((eq? (caar rev-result) 'search)
|
|
|
|
(if have-search-or-domain?
|
|
|
|
(adjust-result (cdr rev-result) have-search-or-domain? nameservers)
|
|
|
|
(cons (car rev-result)
|
|
|
|
(adjust-result (cdr rev-result)
|
|
|
|
#t
|
|
|
|
nameservers))))
|
|
|
|
((eq? (caar rev-result) 'nameserver)
|
|
|
|
(adjust-result (cdr rev-result)
|
|
|
|
have-search-or-domain?
|
|
|
|
(cons (cdar rev-result)
|
|
|
|
nameservers)))
|
|
|
|
(else (cons (car rev-result)
|
|
|
|
(adjust-result (cdr rev-result)
|
|
|
|
have-search-or-domain?
|
|
|
|
nameservers)))))
|
|
|
|
|
2002-12-22 08:45:38 -05:00
|
|
|
(with-input-from-file file-name
|
2002-06-08 11:07:01 -04:00
|
|
|
(lambda ()
|
2002-12-03 11:02:17 -05:00
|
|
|
(let loop ((rev-result '()))
|
2002-06-08 11:07:01 -04:00
|
|
|
(let ((l (read-line)))
|
|
|
|
(cond
|
|
|
|
((eof-object? l)
|
2002-12-03 11:02:17 -05:00
|
|
|
(adjust-result rev-result #f '()))
|
|
|
|
((regexp-search
|
2004-02-02 05:21:20 -05:00
|
|
|
(rx (: bos (| "#" ";")))
|
2003-06-30 06:57:26 -04:00
|
|
|
l)
|
|
|
|
(loop rev-result))
|
|
|
|
((regexp-search
|
|
|
|
(rx (: bos "nameserver" (+ (| " " "\t")
|
2002-12-03 11:02:17 -05:00
|
|
|
(submatch (* any))
|
|
|
|
eos)))
|
|
|
|
l)
|
|
|
|
=> (lambda (match)
|
|
|
|
(loop (cons (parse-nameserver (match:substring match 1))
|
|
|
|
rev-result))))
|
|
|
|
((regexp-search
|
2003-06-30 06:57:26 -04:00
|
|
|
(rx (: bos "domain" (+ (| " " "\t")
|
2002-12-03 11:02:17 -05:00
|
|
|
(submatch (* any))
|
|
|
|
eos)))
|
|
|
|
l)
|
|
|
|
=> (lambda (match)
|
|
|
|
(loop (cons (parse-domain (match:substring match 1))
|
|
|
|
rev-result))))
|
|
|
|
((regexp-search
|
2003-06-30 06:57:26 -04:00
|
|
|
(rx (: bos "search" (+ (| " " "\t")
|
2002-12-03 11:02:17 -05:00
|
|
|
(submatch (* any))
|
|
|
|
eos)))
|
|
|
|
l)
|
|
|
|
=> (lambda (match)
|
|
|
|
(loop (cons (parse-search (match:substring match 1))
|
|
|
|
rev-result))))
|
|
|
|
|
2002-11-27 04:36:21 -05:00
|
|
|
((regexp-search
|
2003-06-30 06:57:26 -04:00
|
|
|
(rx (: bos "sortlist" (+ (| " " "\t")
|
2002-12-03 11:02:17 -05:00
|
|
|
(submatch (* any))
|
|
|
|
eos)))
|
2002-11-27 04:36:21 -05:00
|
|
|
l)
|
2002-06-08 11:07:01 -04:00
|
|
|
=> (lambda (match)
|
2003-06-30 06:57:26 -04:00
|
|
|
(loop (cons (parse-sortlist (match:substring match 1))
|
|
|
|
rev-result))))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
|
2002-12-03 11:02:17 -05:00
|
|
|
((regexp-search
|
2003-06-30 06:57:26 -04:00
|
|
|
(rx (: bos "options" (+ (| " " "\t")
|
2002-12-03 11:02:17 -05:00
|
|
|
(submatch (* any))
|
|
|
|
eos)))
|
|
|
|
l)
|
|
|
|
=> (lambda (match)
|
2003-06-30 06:57:26 -04:00
|
|
|
(loop (cons (parse-options (match:substring match 1))
|
|
|
|
rev-result))))
|
|
|
|
;; skips lines with parse errors, instead of
|
|
|
|
;; raising a 'resolv.conf-parse-error
|
|
|
|
(else (loop rev-result))))))))
|
|
|
|
|
2002-12-03 11:02:17 -05:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Figure out the default name servers
|
|
|
|
|
|
|
|
(define (dns-find-nameserver-list)
|
2003-01-16 08:07:14 -05:00
|
|
|
(cond ((assoc 'nameserver (resolv.conf))
|
2002-12-03 11:02:17 -05:00
|
|
|
=> (lambda (nameserver.list)
|
|
|
|
(cdr nameserver.list)))
|
2003-06-30 06:57:26 -04:00
|
|
|
(else '("127.0.0.1"))))
|
2002-12-03 11:02:17 -05:00
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
;; returns the first found nameserver
|
|
|
|
(define (dns-find-nameserver)
|
|
|
|
(let ((ns (dns-find-nameserver-list)))
|
|
|
|
(if (null? ns)
|
2002-11-25 11:47:00 -05:00
|
|
|
(dns-error 'no-nameservers)
|
2002-06-08 11:07:01 -04:00
|
|
|
(car ns))))
|
|
|
|
|
2003-08-19 10:36:08 -04:00
|
|
|
;; checks if the nameservers are working, prints a summary
|
|
|
|
(define (dns-check-nameservers . args)
|
|
|
|
(let* ((print-summary
|
|
|
|
(lambda (working-channels non-working-channels)
|
|
|
|
(for-each (lambda (channel)
|
|
|
|
(display "FAIL: ")(display channel)
|
|
|
|
(display " - host not reachable.")(newline))
|
|
|
|
non-working-channels)
|
|
|
|
(for-each (lambda (channel)
|
|
|
|
(display "PASS: ")(display channel)
|
|
|
|
(display " - connection established.")(newline))
|
|
|
|
working-channels)
|
|
|
|
(if (null? working-channels)
|
|
|
|
(begin (display "ERROR: no working nameserver found.")(newline)))))
|
|
|
|
|
|
|
|
(nameservers (if (null? args)
|
|
|
|
(dns-find-nameserver-list)
|
|
|
|
(car args)))
|
|
|
|
(sockets (map
|
|
|
|
(lambda (nameserver)
|
|
|
|
(let ((sock (create-socket protocol-family/internet
|
|
|
|
socket-type/stream))
|
|
|
|
(addr (internet-address->socket-address
|
|
|
|
(ip-string->address32 nameserver) 53)))
|
|
|
|
(call-with-current-continuation
|
|
|
|
(lambda (k)
|
|
|
|
(with-handler (lambda (cond more)
|
|
|
|
(display "FAIL: ")
|
|
|
|
(display nameserver)
|
|
|
|
(display " - no DNS Service available.")
|
|
|
|
(newline)
|
|
|
|
(k #f))
|
|
|
|
(lambda ()
|
|
|
|
(connect-socket-no-wait sock addr)
|
|
|
|
sock))))))
|
|
|
|
nameservers)))
|
|
|
|
|
|
|
|
(let* ((nameservers
|
|
|
|
(let loop ((sockets sockets)
|
|
|
|
(nss nameservers))
|
|
|
|
(cond
|
|
|
|
((or (null? sockets) (null? nss)) '())
|
|
|
|
((socket? (car sockets))
|
|
|
|
(cons (car nss) (loop (cdr sockets) (cdr nss))))
|
|
|
|
(else (loop (cdr sockets) (cdr nss))))))
|
|
|
|
|
|
|
|
(sockets (filter socket? sockets))
|
|
|
|
(ws (map socket:outport sockets))
|
|
|
|
(wport-nameserver-alist (map cons ws nameservers))
|
|
|
|
(wport-socket-alist (map cons ws sockets)))
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda ()
|
|
|
|
'nothing-to-be-done-before)
|
|
|
|
(lambda ()
|
|
|
|
(let loop-port-channels ((working-channels '())
|
|
|
|
(non-working-channels '())
|
|
|
|
(tried-channels '())
|
|
|
|
(number-tries 1))
|
|
|
|
(letrec ((delete-list
|
|
|
|
(lambda (elems list)
|
|
|
|
(cond
|
|
|
|
((null? elems) list)
|
|
|
|
((null? list) '())
|
|
|
|
(else (delete-list (cdr elems) (delete (car elems) list))))))
|
|
|
|
(ready (delete-list tried-channels
|
|
|
|
(apply select-port-channels *timeout* ws))))
|
|
|
|
|
|
|
|
(if (or (>= number-tries *max-tries*))
|
|
|
|
(print-summary working-channels
|
|
|
|
(delete-list working-channels
|
|
|
|
(delete-list non-working-channels
|
|
|
|
nameservers)))
|
|
|
|
(let loop-ready-channels ((working-channels working-channels)
|
|
|
|
(non-working-channels non-working-channels)
|
|
|
|
(ready-channels ready))
|
|
|
|
(if (null? ready-channels)
|
|
|
|
(loop-port-channels working-channels
|
|
|
|
non-working-channels
|
|
|
|
(append tried-channels ready)
|
|
|
|
(+ number-tries 1))
|
|
|
|
(let* ((w (car ready-channels))
|
|
|
|
(hit-ns (cdr (assoc w wport-nameserver-alist)))
|
|
|
|
(sock (cdr (assoc w wport-socket-alist))))
|
|
|
|
(if (connect-socket-successful? sock)
|
|
|
|
(loop-ready-channels (append working-channels (list hit-ns))
|
|
|
|
non-working-channels (cdr ready-channels))
|
|
|
|
(loop-ready-channels working-channels
|
|
|
|
(append non-working-channels
|
|
|
|
(list hit-ns))
|
|
|
|
(cdr ready-channels))))))))))
|
|
|
|
(lambda ()
|
|
|
|
(for-each close-socket sockets))))))
|
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
;; computes the nameservers argument of the lookup functions.
|
2002-06-08 11:07:01 -04:00
|
|
|
;; if a nameserver-name is given and not a nameserver-ip
|
|
|
|
;; (dns-lookup-name nameserver) is called.
|
2003-02-10 03:04:17 -05:00
|
|
|
;; use-cache? defaults to #t
|
|
|
|
(define (lookup-optional-args args)
|
2003-02-19 12:37:35 -05:00
|
|
|
(if (null? args)
|
2003-02-10 03:04:17 -05:00
|
|
|
(values (map ip-string->address32 (dns-find-nameserver-list)) #t)
|
|
|
|
(values
|
|
|
|
(map (lambda (nameserver)
|
|
|
|
(cond
|
|
|
|
((address32? nameserver) nameserver)
|
|
|
|
((ip-string? nameserver) (ip-string->address32 nameserver))
|
2003-02-19 12:37:35 -05:00
|
|
|
(else (map dns-lookup-name (dns-find-nameserver-list)))))
|
2003-02-10 03:04:17 -05:00
|
|
|
(car args))
|
|
|
|
(if (null? (cdr args))
|
|
|
|
#t
|
|
|
|
(cadr args)))))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; dns-lookup with more options than dns-lookup-*
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (dns-lookup name type . args)
|
|
|
|
(receive (nameservers use-cache?) (lookup-optional-args args)
|
|
|
|
(let* ((maybe-ip-string (if (address32? name)
|
2003-02-10 07:30:43 -05:00
|
|
|
(ip-string->in-addr.arpa-string (address32->ip-string name))
|
|
|
|
(ip-string->in-addr.arpa-string name)))
|
2003-02-10 03:04:17 -05:00
|
|
|
(query (if maybe-ip-string ; if name is a ip-addr, the query is a in-addr.arpa address
|
|
|
|
(make-simple-query-message
|
|
|
|
maybe-ip-string type (message-class in))
|
|
|
|
(make-simple-query-message name type (message-class in))))
|
|
|
|
(protocol (network-protocol udp))
|
|
|
|
(check-answer (lambda (dns-msg) #t))
|
|
|
|
(dns-msg (dns-get-information query protocol check-answer nameservers use-cache?))
|
|
|
|
(answers (message-answers (dns-message-reply dns-msg))))
|
|
|
|
dns-msg)))
|
2002-11-25 11:47:00 -05:00
|
|
|
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; looks up a hostname, returns an ip.
|
2002-11-19 17:41:37 -05:00
|
|
|
;; (dns-lookup-name <name> nameservers)
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (dns-lookup-name name . args)
|
|
|
|
(receive (nameservers use-cache?) (lookup-optional-args args)
|
|
|
|
(let* ((maybe-ip-string (if (address32? name)
|
2003-02-10 07:30:43 -05:00
|
|
|
(ip-string->in-addr.arpa-string (address32->ip-string name))
|
|
|
|
(ip-string->in-addr.arpa-string name)))
|
2003-02-10 03:04:17 -05:00
|
|
|
(query (if maybe-ip-string ; if name is a ip-addr, the query is a in-addr.arpa address
|
|
|
|
(dns-error 'not-a-hostname)
|
|
|
|
(make-simple-query-message name (message-type a) (message-class in))))
|
|
|
|
(protocol (network-protocol udp))
|
|
|
|
(check-answer (lambda (dns-msg)
|
|
|
|
(let* ((reply (dns-message-reply dns-msg))
|
|
|
|
(answers (message-answers reply)))
|
|
|
|
(not (null? (filter-type answers (message-type a)))))))
|
|
|
|
(dns-msg (dns-get-information query protocol check-answer nameservers use-cache?))
|
|
|
|
(answers (filter-type (message-answers (dns-message-reply dns-msg)) (message-type a))))
|
|
|
|
(resource-record-data-a-ip (resource-record-data (car answers))))))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; looks up an ip, returns a hostname
|
|
|
|
;; (dns-inverse-lookup <name> [nameserver])
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (dns-lookup-ip ip . args)
|
|
|
|
(receive (nameservers use-cache?) (lookup-optional-args args)
|
|
|
|
(let* ((maybe-ip-string (if (address32? ip)
|
2003-02-10 07:30:43 -05:00
|
|
|
(ip-string->in-addr.arpa-string (address32->ip-string ip))
|
|
|
|
(ip-string->in-addr.arpa-string ip)))
|
2003-02-10 03:04:17 -05:00
|
|
|
(query (if maybe-ip-string ; if name is a ip-addr, the query is a in-addr.arpa address
|
|
|
|
(make-simple-query-message maybe-ip-string (message-type ptr) (message-class in))
|
|
|
|
(dns-error 'not-a-ip)))
|
|
|
|
(protocol (network-protocol udp))
|
|
|
|
(check-answer (lambda (dns-msg)
|
|
|
|
(let* ((reply (dns-message-reply dns-msg))
|
|
|
|
(answers (message-answers reply)))
|
|
|
|
(not (null? (filter-type answers (message-type ptr)))))))
|
|
|
|
(dns-msg (dns-get-information query protocol check-answer nameservers use-cache?))
|
|
|
|
(answers (filter-type (message-answers (dns-message-reply dns-msg)) (message-type ptr))))
|
|
|
|
(resource-record-data-ptr-name (resource-record-data (car answers))))))
|
2002-07-07 13:43:32 -04:00
|
|
|
|
|
|
|
(define dns-inverse-lookup dns-lookup-ip)
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; looks up an authoritative nameserver for a hostname
|
|
|
|
;; returns a list of nameservers
|
|
|
|
;; (dns-lookup-nameserver <name> [nameserver])
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (dns-lookup-nameserver name . args)
|
|
|
|
(receive (nameservers use-cache?) (lookup-optional-args args)
|
|
|
|
(let* ((maybe-ip-string (if (address32? name)
|
2003-02-10 07:30:43 -05:00
|
|
|
(ip-string->in-addr.arpa-string (address32->ip-string name))
|
|
|
|
(ip-string->in-addr.arpa-string name)))
|
2003-02-10 03:04:17 -05:00
|
|
|
(query (if maybe-ip-string ; if name is a ip-addr, the query is a in-addr.arpa address
|
|
|
|
(dns-error 'not-a-hostname)
|
|
|
|
(make-simple-query-message
|
|
|
|
name (message-type ns) (message-class in))))
|
|
|
|
(protocol (network-protocol udp))
|
|
|
|
(check-answer (lambda (dns-msg)
|
|
|
|
(let* ((reply (dns-message-reply dns-msg))
|
|
|
|
(answers (message-answers reply))
|
|
|
|
(nameservers (message-nameservers reply)))
|
|
|
|
(or (not (null? (filter-type nameservers (message-type soa))))
|
|
|
|
(not (null? (filter-type answers (message-type ns))))))))
|
|
|
|
(dns-msg (dns-get-information query protocol check-answer nameservers use-cache?))
|
|
|
|
(reply (dns-message-reply dns-msg))
|
|
|
|
(soa (filter-type (message-nameservers reply) (message-type soa)))
|
|
|
|
(nss (filter-type (message-answers reply) (message-type ns)))
|
|
|
|
(add (filter-type (message-additionals reply) (message-type a))))
|
|
|
|
(if (null? nss)
|
|
|
|
(list (dns-lookup-name (resource-record-data-soa-mname (resource-record-data (car soa)))))
|
|
|
|
(map (lambda (elem) (resource-record-data-a-ip (resource-record-data elem))) add)))))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;; looks up a mail-exchanger for a hostname.
|
|
|
|
;; returns a list of mail-exchanger, sorted by their preference
|
|
|
|
;; if there are no mx-records in the answer-section,
|
|
|
|
;; implementation based on RFC2821
|
|
|
|
;; (dns-lookup-mail-exchanger <name> [nameserver])
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (dns-lookup-mail-exchanger name . args)
|
|
|
|
(receive (nameservers use-cache?) (lookup-optional-args args)
|
|
|
|
(let* ((ip-string (if (address32? name)
|
2003-02-10 07:30:43 -05:00
|
|
|
(ip-string->in-addr.arpa-string (address32->ip-string name))
|
|
|
|
(ip-string->in-addr.arpa-string name)))
|
2003-02-10 03:04:17 -05:00
|
|
|
(query (if ip-string ; if name is a ip-addr, the query is a in-addr.arpa address
|
|
|
|
(dns-error 'not-a-hostname)
|
|
|
|
(make-simple-query-message
|
|
|
|
name (message-type mx) (message-class in))))
|
|
|
|
(protocol (network-protocol tcp))
|
|
|
|
(check-answer (lambda (dns-msg)
|
|
|
|
(let* ((reply (dns-message-reply dns-msg))
|
|
|
|
(answers (message-answers reply))
|
|
|
|
(nameservers (message-nameservers reply)))
|
|
|
|
(or (not (null? (filter-type answers (message-type mx))))
|
|
|
|
(not (null? (filter-type answers (message-type cname))))
|
|
|
|
(not (null? (filter-type answers (message-type a))))))))
|
|
|
|
(dns-msg (dns-get-information query protocol check-answer nameservers use-cache?))
|
|
|
|
(reply (dns-message-reply dns-msg))
|
|
|
|
(mx (filter-type (message-answers reply) (message-type mx)))
|
|
|
|
(soa (filter-type (message-nameservers reply)(message-type soa)))
|
|
|
|
(cname (filter-type (message-answers reply) (message-type cname)))
|
|
|
|
(a (filter-type (message-answers reply) (message-type a))))
|
|
|
|
|
|
|
|
(cond
|
|
|
|
((not (null? a))
|
|
|
|
(list (resource-record-data-a-ip (resource-record-data (car a)))))
|
|
|
|
((not (null? cname))
|
|
|
|
(dns-lookup-mail-exchanger (resource-record-data-cname-name (resource-record-data (car cname)))))
|
|
|
|
((null? mx)
|
|
|
|
(list (resource-record-data-soa-rname (resource-record-data (car soa)))))
|
|
|
|
(else
|
|
|
|
(map (lambda (elem) (resource-record-data-mx-exchanger (resource-record-data elem))) (sort-by-preference mx)))))))
|
2002-06-08 11:07:01 -04:00
|
|
|
|
|
|
|
;;; pretty-prints a dns-msg
|
2003-01-17 13:29:47 -05:00
|
|
|
(define (pretty-print-dns-message dns-msg . maybe-port)
|
|
|
|
(let ((d
|
|
|
|
(lambda (n s1 s2)
|
|
|
|
(letrec ((loop (lambda (n)
|
|
|
|
(if (zero? n)
|
|
|
|
""
|
|
|
|
(string-append " " (loop (- n 1)))))))
|
|
|
|
(display (loop n))
|
|
|
|
(display s1)
|
|
|
|
(display ": ")
|
|
|
|
(display s2)
|
|
|
|
(newline)))))
|
2003-01-20 09:20:53 -05:00
|
|
|
(with-current-output-port*
|
2003-01-17 13:29:47 -05:00
|
|
|
(if (null? maybe-port)
|
|
|
|
(current-output-port)
|
|
|
|
(car maybe-port))
|
|
|
|
(lambda ()
|
|
|
|
(define (show-dns-message dns-msg)
|
|
|
|
(cond
|
|
|
|
((dns-message? dns-msg)
|
|
|
|
(begin
|
|
|
|
(d 0 "DNS-MESSAGE" "")
|
|
|
|
(d 1 "QUERY" "")(show-dns-message (dns-message-query dns-msg))(newline)
|
|
|
|
(d 1 "REPLY" "")(show-dns-message (dns-message-reply dns-msg))(newline)
|
|
|
|
(d 1 "CACHE?" (if (dns-message-cache? dns-msg)
|
|
|
|
"found in cache"
|
|
|
|
"not found in cache"))
|
|
|
|
(d 1 "PROTOCOL" (let ((protocol (dns-message-protocol dns-msg)))
|
|
|
|
(cond
|
2003-02-10 03:04:17 -05:00
|
|
|
((eq? protocol (network-protocol tcp)) "TCP")
|
|
|
|
((eq? protocol (network-protocol udp)) "UDP"))))
|
2003-01-17 13:29:47 -05:00
|
|
|
(d 1 "TRIED-NAMESERVERS" (if (> (length (dns-message-tried-nameservers dns-msg)) 1)
|
|
|
|
(begin
|
|
|
|
(display " had perform recursion: ")
|
|
|
|
(dns-message-tried-nameservers dns-msg))
|
|
|
|
(begin
|
|
|
|
(display " without recursion: ")
|
|
|
|
(dns-message-tried-nameservers dns-msg))))))
|
|
|
|
((message? dns-msg)
|
|
|
|
(begin
|
|
|
|
(d 2 "MESSAGE" "")
|
|
|
|
(d 3 "Header " "")(show-dns-message (message-header dns-msg))
|
|
|
|
(d 3 "Questions " "")(for-each (lambda (x) (show-dns-message x)(newline)) (message-questions dns-msg))
|
|
|
|
(d 3 "Answers " "")(for-each (lambda (x) (show-dns-message x)(newline)) (message-answers dns-msg))
|
|
|
|
(d 3 "Nameservers" "")(for-each (lambda (x) (show-dns-message x)(newline)) (message-nameservers dns-msg))
|
|
|
|
(d 3 "Additionals" "")(for-each (lambda (x) (show-dns-message x)(newline)) (message-additionals dns-msg))))
|
|
|
|
((header? dns-msg)
|
|
|
|
(begin
|
|
|
|
(d 4 "id" (header-id dns-msg))
|
|
|
|
(d 4 "Flags" "")(show-dns-message (header-flags dns-msg))
|
|
|
|
(d 4 "question-count " (header-question-count dns-msg))
|
|
|
|
(d 4 "answer-count " (header-answer-count dns-msg))
|
|
|
|
(d 4 "nameserver-count " (header-nameserver-count dns-msg))
|
|
|
|
(d 4 "additional-count " (header-additional-count dns-msg))))
|
|
|
|
((flags? dns-msg)
|
|
|
|
(begin
|
2003-01-21 11:09:41 -05:00
|
|
|
(d 5 "querytype" (flags-query-type dns-msg))
|
2003-01-17 13:29:47 -05:00
|
|
|
(d 5 "opcode" (flags-opcode dns-msg))
|
|
|
|
(d 5 "authoritative?" (flags-authoritative? dns-msg))
|
|
|
|
(d 5 "truncated?" (flags-truncated? dns-msg))
|
|
|
|
(d 5 "recursion-desired?" (flags-recursion-desired? dns-msg))
|
|
|
|
(d 5 "recursion-available?" (flags-recursion-available? dns-msg))
|
|
|
|
(d 5 "zero" (flags-zero dns-msg))
|
|
|
|
(d 5 "response-code" (flags-response-code dns-msg))))
|
|
|
|
((question? dns-msg)
|
|
|
|
(begin
|
|
|
|
(d 4 "name " (question-name dns-msg))
|
|
|
|
(d 4 "type " (message-type-name (question-type dns-msg)))
|
|
|
|
(d 4 "class" (message-class-name (question-class dns-msg)))))
|
2003-02-10 03:04:17 -05:00
|
|
|
((resource-record? dns-msg)
|
2003-01-17 13:29:47 -05:00
|
|
|
(begin
|
2003-02-10 03:04:17 -05:00
|
|
|
(d 4 "name " (resource-record-name dns-msg))
|
|
|
|
(d 4 "type "(message-type-name (resource-record-type dns-msg)))
|
|
|
|
(d 4 "class" (message-class-name (resource-record-class dns-msg)))
|
|
|
|
(d 4 "ttl " (resource-record-ttl dns-msg))
|
|
|
|
(d 4 "data " "") (show-dns-message (resource-record-data dns-msg))))
|
|
|
|
((resource-record-data-a? dns-msg)
|
2006-11-12 12:48:36 -05:00
|
|
|
(d 5 "ip " (address32->ip-string (resource-record-data-a-ip dns-msg))))
|
|
|
|
((resource-record-data-aaaa? dns-msg)
|
|
|
|
(d 5 "ipv6 " (resource-record-data-aaaa-ipv6 dns-msg)))
|
2003-02-10 03:04:17 -05:00
|
|
|
((resource-record-data-ns? dns-msg)
|
|
|
|
(d 5 "name " (resource-record-data-ns-name dns-msg)))
|
|
|
|
((resource-record-data-cname? dns-msg)
|
|
|
|
(d 5 "name " (resource-record-data-cname-name dns-msg)))
|
|
|
|
((resource-record-data-mx? dns-msg)
|
2003-01-17 13:29:47 -05:00
|
|
|
(begin
|
2003-02-10 03:04:17 -05:00
|
|
|
(d 5 "preference " (resource-record-data-mx-preference dns-msg))
|
|
|
|
(d 5 "exchanger " (resource-record-data-mx-exchanger dns-msg))))
|
|
|
|
((resource-record-data-ptr? dns-msg)
|
|
|
|
(d 5 "name " (resource-record-data-ptr-name dns-msg)))
|
|
|
|
((resource-record-data-soa? dns-msg)
|
2003-01-17 13:29:47 -05:00
|
|
|
(begin
|
2003-02-10 03:04:17 -05:00
|
|
|
(d 5 "mname " (resource-record-data-soa-mname dns-msg))
|
|
|
|
(d 5 "rname " (resource-record-data-soa-rname dns-msg))
|
|
|
|
(d 5 "serial " (resource-record-data-soa-serial dns-msg))
|
|
|
|
(d 5 "refresh " (resource-record-data-soa-refresh dns-msg))
|
2006-11-12 12:48:36 -05:00
|
|
|
(d 5 "retry " (resource-record-data-soa-retry dns-msg))
|
2003-02-10 03:04:17 -05:00
|
|
|
(d 5 "expire " (resource-record-data-soa-expire dns-msg))
|
2006-11-12 12:48:36 -05:00
|
|
|
(d 5 "minimum " (resource-record-data-soa-minimum dns-msg))))
|
2003-01-17 13:29:47 -05:00
|
|
|
;; ###
|
2003-02-10 03:04:17 -05:00
|
|
|
((resource-record-data-hinfo? dns-msg)
|
|
|
|
(d 5 "data " (resource-record-data-hinfo-data dns-msg)))
|
|
|
|
((resource-record-data-txt? dns-msg)
|
|
|
|
(d 5 "text " (resource-record-data-txt-text dns-msg)))
|
|
|
|
((resource-record-data-wks? dns-msg)
|
|
|
|
(d 5 "data " (resource-record-data-wks-data dns-msg)))
|
2003-01-17 13:29:47 -05:00
|
|
|
))
|
|
|
|
(show-dns-message dns-msg)))))
|
2002-09-05 05:25:42 -04:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (socket-address->fqdn addr . args)
|
2002-09-05 05:25:42 -04:00
|
|
|
(receive (ip32 port)
|
|
|
|
(socket-address->internet-address addr)
|
2003-02-10 03:04:17 -05:00
|
|
|
(apply dns-lookup-ip ip32 args)))
|
2002-09-05 05:25:42 -04:00
|
|
|
|
2003-02-10 08:13:26 -05:00
|
|
|
;; Some code to test the components of domain names
|
|
|
|
;;(define label-regexp
|
|
|
|
;; (rx (: alpha (? (* (| alphanumeric "-")) alphanumeric))))
|
|
|
|
;;(define (unqualified-hostname name)
|
|
|
|
;; (regexp-search? (rx (: ,label-regexp)) name))
|
|
|
|
|
2002-12-03 11:02:17 -05:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (maybe-dns-lookup-name name . args)
|
2002-12-03 11:02:17 -05:00
|
|
|
(call-with-current-continuation
|
|
|
|
(lambda (k)
|
|
|
|
(with-handler (lambda (cond more)
|
|
|
|
(if (dns-error? cond)
|
|
|
|
(k #f)
|
|
|
|
(more)))
|
|
|
|
(lambda ()
|
2003-02-10 08:13:26 -05:00
|
|
|
(apply dns-lookup-name name args))))))
|
2002-12-03 11:02:17 -05:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (maybe-dns-lookup-ip ip-addr . args)
|
2003-01-16 08:07:14 -05:00
|
|
|
(call-with-current-continuation
|
|
|
|
(lambda (k)
|
|
|
|
(with-handler (lambda (cond more)
|
|
|
|
(if (dns-error? cond)
|
|
|
|
(k #f)
|
|
|
|
(more)))
|
|
|
|
(lambda ()
|
2003-02-10 03:04:17 -05:00
|
|
|
(apply dns-lookup-ip ip-addr args))))))
|
2003-01-16 08:07:14 -05:00
|
|
|
|
2002-12-03 11:02:17 -05:00
|
|
|
(define (domains-for-search)
|
2003-01-16 15:08:14 -05:00
|
|
|
(cond ((assoc 'domain (resolv.conf))
|
|
|
|
=> (lambda (pair)
|
|
|
|
(list (cdr pair))))
|
|
|
|
((assoc 'search (resolv.conf))
|
|
|
|
=> (lambda (pair)
|
|
|
|
(cdr pair)))
|
|
|
|
(else '())))
|
2002-12-03 11:02:17 -05:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (host-fqdn name-or-socket-address . args)
|
2002-09-05 05:38:37 -04:00
|
|
|
(if (socket-address? name-or-socket-address)
|
2003-02-10 03:04:17 -05:00
|
|
|
(apply socket-address->fqdn name-or-socket-address args)
|
2002-12-03 11:02:17 -05:00
|
|
|
(let ((name name-or-socket-address))
|
2003-02-10 08:13:26 -05:00
|
|
|
(cond ((apply maybe-dns-lookup-name name args)
|
|
|
|
=> (lambda (ip)
|
|
|
|
(apply dns-lookup-ip ip args)))
|
|
|
|
(else
|
|
|
|
(let lp ((domains (domains-for-search)))
|
|
|
|
(if (null? domains)
|
|
|
|
#f
|
|
|
|
(cond ((apply maybe-dns-lookup-name
|
|
|
|
(string-append name "." (car domains)) args)
|
|
|
|
=> (lambda (ip)
|
|
|
|
(apply dns-lookup-ip ip args)))
|
|
|
|
(else (lp (cdr domains)))))))))))
|
2002-12-03 11:02:17 -05:00
|
|
|
|
2003-02-10 03:04:17 -05:00
|
|
|
(define (system-fqdn . args)
|
|
|
|
(apply host-fqdn (system-name) args))
|