1 ;;; dns.el --- Domain Name Service lookups
2 ;; Copyright (C) 2002 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.
31 "How many seconds to wait when doing DNS queries.")
35 (defvar dns-query-types
56 "Names of query types and their values.")
63 "Classes of queries.")
65 (defun dns-write-bytes (value &optional length)
67 (dotimes (i (or length 1))
68 (push (% value 256) bytes)
69 (setq value (/ value 256)))
73 (defun dns-read-bytes (length)
76 (setq value (logior (* value 256) (following-char)))
80 (defun dns-get (type spec)
81 (cadr (assq type spec)))
83 (defun dns-inverse-get (value spec)
85 (while (and (not found)
87 (if (eq value (cadr (car spec)))
88 (setq found (caar spec))
92 (defun dns-write-name (name)
93 (dolist (part (split-string name "\\."))
94 (dns-write-bytes (length part))
98 (defun dns-read-string-name (string buffer)
99 (mm-with-unibyte-buffer
101 (goto-char (point-min))
102 (dns-read-name buffer)))
104 (defun dns-read-name (&optional buffer)
109 (setq length (dns-read-bytes 1))
110 (if (= 192 (logand length (lsh 3 6)))
111 (let ((offset (+ (* (logand 63 length) 256)
112 (dns-read-bytes 1))))
116 (goto-char (1+ offset))
117 (setq ended (dns-read-name buffer))))
120 (push (buffer-substring (point)
121 (progn (forward-char length) (point)))
126 (concat (mapconcat 'identity (nreverse name) ".") "." ended))
127 (mapconcat 'identity (nreverse name) "."))))
129 (defun dns-write (spec)
130 "Write a DNS packet according to SPEC."
132 (dns-write-bytes (dns-get 'id spec) 2)
135 (lsh (if (dns-get 'response-p spec) 1 0) -7)
138 ((eq (dns-get 'opcode spec) 'query) 0)
139 ((eq (dns-get 'opcode spec) 'inverse-query) 1)
140 ((eq (dns-get 'opcode spec) 'status) 2)
141 (t (error "No such opcode: %s" (dns-get 'opcode spec))))
143 (lsh (if (dns-get 'authoritative-p spec) 1 0) -2)
144 (lsh (if (dns-get 'truncated-p spec) 1 0) -1)
145 (lsh (if (dns-get 'recursion-desired-p spec) 1 0) 0)))
148 ((eq (dns-get 'response-code spec) 'no-error) 0)
149 ((eq (dns-get 'response-code spec) 'format-error) 1)
150 ((eq (dns-get 'response-code spec) 'server-failure) 2)
151 ((eq (dns-get 'response-code spec) 'name-error) 3)
152 ((eq (dns-get 'response-code spec) 'not-implemented) 4)
153 ((eq (dns-get 'response-code spec) 'refused) 5)
155 (dns-write-bytes (length (dns-get 'queries spec)) 2)
156 (dns-write-bytes (length (dns-get 'answers spec)) 2)
157 (dns-write-bytes (length (dns-get 'authorities spec)) 2)
158 (dns-write-bytes (length (dns-get 'additionals spec)) 2)
159 (dolist (query (dns-get 'queries spec))
160 (dns-write-name (car query))
161 (dns-write-bytes (cadr (assq (or (dns-get 'type query) 'A)
163 (dns-write-bytes (cadr (assq (or (dns-get 'class query) 'IN)
165 (dolist (slot '(answers authorities additionals))
166 (dolist (resource (dns-get slot spec))
167 (dns-write-name (car resource))
168 (dns-write-bytes (cadr (assq (dns-get 'type resource) dns-query-types))
170 (dns-write-bytes (cadr (assq (dns-get 'class resource) dns-classes))
172 (dns-write-bytes (dns-get 'ttl resource) 4)
173 (dns-write-bytes (length (dns-get 'data resource)) 2)
174 (insert (dns-get 'data resource))))
177 (defun dns-read (packet)
178 (mm-with-unibyte-buffer
180 queries answers authorities additionals)
182 (goto-char (point-min))
183 (push (list 'id (dns-read-bytes 2)) spec)
184 (let ((byte (dns-read-bytes 1)))
185 (push (list 'response-p (if (zerop (logand byte (lsh 1 7))) nil t))
187 (let ((opcode (logand byte (lsh 7 3))))
189 (cond ((eq opcode 0) 'query)
190 ((eq opcode 1) 'inverse-query)
191 ((eq opcode 2) 'status)))
193 (push (list 'authoritative-p (if (zerop (logand byte (lsh 1 2)))
195 (push (list 'truncated-p (if (zerop (logand byte (lsh 1 2))) nil t))
197 (push (list 'recursion-desired-p
198 (if (zerop (logand byte (lsh 1 0))) nil t)) spec))
199 (let ((rc (logand (dns-read-bytes 1) 15)))
200 (push (list 'response-code
202 ((eq rc 0) 'no-error)
203 ((eq rc 1) 'format-error)
204 ((eq rc 2) 'server-failure)
205 ((eq rc 3) 'name-error)
206 ((eq rc 4) 'not-implemented)
207 ((eq rc 5) 'refused)))
209 (setq queries (dns-read-bytes 2))
210 (setq answers (dns-read-bytes 2))
211 (setq authorities (dns-read-bytes 2))
212 (setq additionals (dns-read-bytes 2))
215 (push (list (dns-read-name)
216 (list 'type (dns-inverse-get (dns-read-bytes 2)
218 (list 'class (dns-inverse-get (dns-read-bytes 2)
221 (push (list 'queries qs) spec))
222 (dolist (slot '(answers authorities additionals))
225 (dotimes (i (symbol-value slot))
226 (push (list (dns-read-name)
228 (setq type (dns-inverse-get (dns-read-bytes 2)
230 (list 'class (dns-inverse-get (dns-read-bytes 2)
232 (list 'ttl (dns-read-bytes 4))
233 (let ((length (dns-read-bytes 2)))
238 (progn (forward-char length) (point)))
241 (push (list slot qs) spec)))
244 (defun dns-read-type (string type)
245 (let ((buffer (current-buffer))
248 (mm-with-unibyte-buffer
250 (goto-char (point-min))
255 (push (dns-read-bytes 1) bytes))
256 (mapconcat 'number-to-string (nreverse bytes) ".")))
258 (dns-read-string-name string buffer))
260 (dns-read-string-name string buffer))
264 ;;; Interface functions.
266 (defun query-dns (name &optional type fullp)
267 "Query a DNS server for NAME of TYPE.
268 If FULLP, return the entire record returned."
269 (setq type (or type 'A))
270 (mm-with-unibyte-buffer
271 (let ((coding-system-for-read 'binary)
272 (coding-system-for-write 'binary))
274 (make-network-process
277 :buffer (current-buffer)
278 :host "ns2.netfonds.no"
282 (times (* dns-timeout 1000))
286 (dns-write `((id ,id)
288 (queries ((,name (type ,type))))
289 (recursion-desired-p t))))
290 (while (and (zerop (buffer-size))
292 (accept-process-output process 0 step)
295 (delete-process process))
296 (let ((result (dns-read (buffer-string))))
299 (let ((answer (car (dns-get 'answers result))))
300 (when (eq type (dns-get 'type answer))
301 (dns-get 'data answer)))))))))