Function: dns-write
dns-write is a byte-compiled function defined in dns.el.gz.
Signature
(dns-write SPEC &optional TCP-P)
Documentation
Write a DNS packet according to SPEC.
If TCP-P, the first two bytes of the packet will be the length field.
Source Code
;; Defined in /usr/src/emacs/lisp/net/dns.el.gz
(defun dns-write (spec &optional tcp-p)
"Write a DNS packet according to SPEC.
If TCP-P, the first two bytes of the packet will be the length field."
(with-temp-buffer
(set-buffer-multibyte nil)
(dns-write-bytes (dns-get 'id spec) 2)
(dns-write-bytes
(logior
(ash (if (dns-get 'response-p spec) 1 0) 7)
(ash
(cond
((eq (dns-get 'opcode spec) 'query) 0)
((eq (dns-get 'opcode spec) 'inverse-query) 1)
((eq (dns-get 'opcode spec) 'status) 2)
(t (error "No such opcode: %s" (dns-get 'opcode spec))))
3)
(ash (if (dns-get 'authoritative-p spec) 1 0) 2)
(ash (if (dns-get 'truncated-p spec) 1 0) 1)
(ash (if (dns-get 'recursion-desired-p spec) 1 0) 0)))
(dns-write-bytes
(cond
((eq (dns-get 'response-code spec) 'no-error) 0)
((eq (dns-get 'response-code spec) 'format-error) 1)
((eq (dns-get 'response-code spec) 'server-failure) 2)
((eq (dns-get 'response-code spec) 'name-error) 3)
((eq (dns-get 'response-code spec) 'not-implemented) 4)
((eq (dns-get 'response-code spec) 'refused) 5)
(t 0)))
(dns-write-bytes (length (dns-get 'queries spec)) 2)
(dns-write-bytes (length (dns-get 'answers spec)) 2)
(dns-write-bytes (length (dns-get 'authorities spec)) 2)
(dns-write-bytes (length (dns-get 'additionals spec)) 2)
(dolist (query (dns-get 'queries spec))
(dns-write-name (car query))
(dns-write-bytes (cadr (assq (or (dns-get 'type query) 'A)
dns-query-types)) 2)
(dns-write-bytes (cadr (assq (or (dns-get 'class query) 'IN)
dns-classes)) 2))
(dolist (slot '(answers authorities additionals))
(dolist (resource (dns-get slot spec))
(dns-write-name (car resource))
(dns-write-bytes (cadr (assq (dns-get 'type resource) dns-query-types))
2)
(dns-write-bytes (cadr (assq (dns-get 'class resource) dns-classes))
2)
(dns-write-bytes (dns-get 'ttl resource) 4)
(dns-write-bytes (length (dns-get 'data resource)) 2)
(insert (dns-get 'data resource))))
(when tcp-p
(goto-char (point-min))
(dns-write-bytes (buffer-size) 2))
(buffer-string)))