1 ;;; dns.el --- Domain Name Service lookups
2 ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl))
33 "How many seconds to wait when doing DNS queries.")
35 (defvar dns-servers nil
36 "Which DNS servers to query.
37 If nil, /etc/resolv.conf will be consulted.")
41 (defvar dns-query-types
64 "Names of query types and their values.")
71 "Classes of queries.")
73 (defun dns-write-bytes (value &optional length)
75 (dotimes (i (or length 1))
76 (push (% value 256) bytes)
77 (setq value (/ value 256)))
81 (defun dns-read-bytes (length)
84 (setq value (logior (* value 256) (following-char)))
88 (defun dns-get (type spec)
89 (cadr (assq type spec)))
91 (defun dns-inverse-get (value spec)
93 (while (and (not found)
95 (if (eq value (cadr (car spec)))
96 (setq found (caar spec))
100 (defun dns-write-name (name)
101 (dolist (part (split-string name "\\."))
102 (dns-write-bytes (length part))
106 (defun dns-read-string-name (string buffer)
107 (mm-with-unibyte-buffer
109 (goto-char (point-min))
110 (dns-read-name buffer)))
112 (defun dns-read-name (&optional buffer)
117 (setq length (dns-read-bytes 1))
118 (if (= 192 (logand length (lsh 3 6)))
119 (let ((offset (+ (* (logand 63 length) 256)
120 (dns-read-bytes 1))))
124 (goto-char (1+ offset))
125 (setq ended (dns-read-name buffer))))
128 (push (buffer-substring (point)
129 (progn (forward-char length) (point)))
134 (concat (mapconcat 'identity (nreverse name) ".") "." ended))
135 (mapconcat 'identity (nreverse name) "."))))
137 (defun dns-write (spec &optional tcp-p)
138 "Write a DNS packet according to SPEC.
139 If TCP-P, the first two bytes of the package with be the length field."
141 (dns-write-bytes (dns-get 'id spec) 2)
144 (lsh (if (dns-get 'response-p spec) 1 0) -7)
147 ((eq (dns-get 'opcode spec) 'query) 0)
148 ((eq (dns-get 'opcode spec) 'inverse-query) 1)
149 ((eq (dns-get 'opcode spec) 'status) 2)
150 (t (error "No such opcode: %s" (dns-get 'opcode spec))))
152 (lsh (if (dns-get 'authoritative-p spec) 1 0) -2)
153 (lsh (if (dns-get 'truncated-p spec) 1 0) -1)
154 (lsh (if (dns-get 'recursion-desired-p spec) 1 0) 0)))
157 ((eq (dns-get 'response-code spec) 'no-error) 0)
158 ((eq (dns-get 'response-code spec) 'format-error) 1)
159 ((eq (dns-get 'response-code spec) 'server-failure) 2)
160 ((eq (dns-get 'response-code spec) 'name-error) 3)
161 ((eq (dns-get 'response-code spec) 'not-implemented) 4)
162 ((eq (dns-get 'response-code spec) 'refused) 5)
164 (dns-write-bytes (length (dns-get 'queries spec)) 2)
165 (dns-write-bytes (length (dns-get 'answers spec)) 2)
166 (dns-write-bytes (length (dns-get 'authorities spec)) 2)
167 (dns-write-bytes (length (dns-get 'additionals spec)) 2)
168 (dolist (query (dns-get 'queries spec))
169 (dns-write-name (car query))
170 (dns-write-bytes (cadr (assq (or (dns-get 'type query) 'A)
172 (dns-write-bytes (cadr (assq (or (dns-get 'class query) 'IN)
174 (dolist (slot '(answers authorities additionals))
175 (dolist (resource (dns-get slot spec))
176 (dns-write-name (car resource))
177 (dns-write-bytes (cadr (assq (dns-get 'type resource) dns-query-types))
179 (dns-write-bytes (cadr (assq (dns-get 'class resource) dns-classes))
181 (dns-write-bytes (dns-get 'ttl resource) 4)
182 (dns-write-bytes (length (dns-get 'data resource)) 2)
183 (insert (dns-get 'data resource))))
185 (goto-char (point-min))
186 (dns-write-bytes (buffer-size) 2))
189 (defun dns-read (packet)
190 (mm-with-unibyte-buffer
192 queries answers authorities additionals)
194 (goto-char (point-min))
195 (push (list 'id (dns-read-bytes 2)) spec)
196 (let ((byte (dns-read-bytes 1)))
197 (push (list 'response-p (if (zerop (logand byte (lsh 1 7))) nil t))
199 (let ((opcode (logand byte (lsh 7 3))))
201 (cond ((eq opcode 0) 'query)
202 ((eq opcode 1) 'inverse-query)
203 ((eq opcode 2) 'status)))
205 (push (list 'authoritative-p (if (zerop (logand byte (lsh 1 2)))
207 (push (list 'truncated-p (if (zerop (logand byte (lsh 1 2))) nil t))
209 (push (list 'recursion-desired-p
210 (if (zerop (logand byte (lsh 1 0))) nil t)) spec))
211 (let ((rc (logand (dns-read-bytes 1) 15)))
212 (push (list 'response-code
214 ((eq rc 0) 'no-error)
215 ((eq rc 1) 'format-error)
216 ((eq rc 2) 'server-failure)
217 ((eq rc 3) 'name-error)
218 ((eq rc 4) 'not-implemented)
219 ((eq rc 5) 'refused)))
221 (setq queries (dns-read-bytes 2))
222 (setq answers (dns-read-bytes 2))
223 (setq authorities (dns-read-bytes 2))
224 (setq additionals (dns-read-bytes 2))
227 (push (list (dns-read-name)
228 (list 'type (dns-inverse-get (dns-read-bytes 2)
230 (list 'class (dns-inverse-get (dns-read-bytes 2)
233 (push (list 'queries qs) spec))
234 (dolist (slot '(answers authorities additionals))
237 (dotimes (i (symbol-value slot))
238 (push (list (dns-read-name)
240 (setq type (dns-inverse-get (dns-read-bytes 2)
242 (list 'class (dns-inverse-get (dns-read-bytes 2)
244 (list 'ttl (dns-read-bytes 4))
245 (let ((length (dns-read-bytes 2)))
250 (progn (forward-char length) (point)))
253 (push (list slot qs) spec)))
256 (defun dns-read-int32 ()
257 ;; Full 32 bit Integers can't be handled by Emacs. If we use
259 (format "%.0f" (+ (* (dns-read-bytes 1) 16777216.0)
260 (dns-read-bytes 3))))
262 (defun dns-read-type (string type)
263 (let ((buffer (current-buffer))
266 (mm-with-unibyte-buffer
268 (goto-char (point-min))
273 (push (dns-read-bytes 1) bytes))
274 (mapconcat 'number-to-string (nreverse bytes) ".")))
278 (push (dns-read-bytes 2) hextets))
279 (mapconcat (lambda (n) (format "%x" n)) (nreverse hextets) ":")))
281 (list (list 'mname (dns-read-name buffer))
282 (list 'rname (dns-read-name buffer))
283 (list 'serial (dns-read-int32))
284 (list 'refresh (dns-read-int32))
285 (list 'retry (dns-read-int32))
286 (list 'expire (dns-read-int32))
287 (list 'minimum (dns-read-int32))))
289 (list (list 'priority (dns-read-bytes 2))
290 (list 'weight (dns-read-bytes 2))
291 (list 'port (dns-read-bytes 2))
292 (list 'target (dns-read-name buffer))))
294 (cons (dns-read-bytes 2) (dns-read-name buffer)))
295 ((or (eq type 'CNAME) (eq type 'NS) (eq type 'PTR))
296 (dns-read-string-name string buffer))
300 (defun dns-parse-resolv-conf ()
301 (when (file-exists-p "/etc/resolv.conf")
303 (insert-file-contents "/etc/resolv.conf")
304 (goto-char (point-min))
305 (while (re-search-forward "^nameserver[\t ]+\\([^ \t\n]+\\)" nil t)
306 (push (match-string 1) dns-servers))
307 (setq dns-servers (nreverse dns-servers)))))
309 (defun dns-read-txt (string)
310 (if (> (length string) 1)
314 (defun dns-get-txt-answer (answers)
317 (dolist (answer answers)
318 (dolist (elem answer)
321 ((eq (car elem) 'type)
322 (setq do-next (eq (cadr elem) 'TXT)))
323 ((eq (car elem) 'data)
325 (setq result (concat result (dns-read-txt (cadr elem))))))))))
328 ;;; Interface functions.
329 (defmacro dns-make-network-process (server)
330 (if (featurep 'xemacs)
331 `(let ((coding-system-for-read 'binary)
332 (coding-system-for-write 'binary))
333 (open-network-stream "dns" (current-buffer)
334 ,server "domain" 'udp))
335 `(let ((server ,server)
336 (coding-system-for-read 'binary)
337 (coding-system-for-write 'binary)
338 (default-process-coding-system '(binary . binary))
339 program-coding-system-alist)
340 (if (fboundp 'make-network-process)
341 (make-network-process
344 :buffer (current-buffer)
348 ;; Older versions of Emacs doesn't have
349 ;; `make-network-process', so we fall back on opening a TCP
350 ;; connection to the DNS server.
351 (open-network-stream "dns" (current-buffer) server "domain")))))
353 (defun query-dns (name &optional type fullp)
354 "Query a DNS server for NAME of TYPE.
355 If FULLP, return the entire record returned."
356 (setq type (or type 'A))
358 (dns-parse-resolv-conf))
360 (if (not dns-servers)
361 (message "No DNS server configuration found")
362 (mm-with-unibyte-buffer
363 (let ((process (condition-case ()
364 (dns-make-network-process (car dns-servers))
366 (message "dns: Got an error while trying to talk to %s"
369 (tcp-p (and (not (fboundp 'make-network-process))
370 (not (featurep 'xemacs))))
372 (times (* dns-timeout 1000))
377 (dns-write `((id ,id)
379 (queries ((,name (type ,type))))
380 (recursion-desired-p t))
382 (while (and (zerop (buffer-size))
384 (accept-process-output process 0 step)
387 (delete-process process))
389 (goto-char (point-min))
390 (delete-region (point) (+ (point) 2)))
391 (unless (zerop (buffer-size))
392 (let ((result (dns-read (buffer-string))))
395 (let ((answer (car (dns-get 'answers result))))
396 (when (eq type (dns-get 'type answer))
398 (dns-get-txt-answer (dns-get 'answers result))
399 (dns-get 'data answer))))))))))))