Large simplification in (vc-deduce-fileset) logic.
[emacs.git] / lisp / net / dns.el
blob00e48c6480f84fb9f7bfd25c131336bc0fc63df2
1 ;;; dns.el --- Domain Name Service lookups
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: network
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (defvar dns-timeout 5
29 "How many seconds to wait when doing DNS queries.")
31 (defvar dns-servers nil
32 "Which DNS servers to query.
33 If nil, /etc/resolv.conf will be consulted.")
35 ;;; Internal code:
37 (defvar dns-query-types
38 '((A 1)
39 (NS 2)
40 (MD 3)
41 (MF 4)
42 (CNAME 5)
43 (SOA 6)
44 (MB 7)
45 (MG 8)
46 (MR 9)
47 (NULL 10)
48 (WKS 11)
49 (PTR 12)
50 (HINFO 13)
51 (MINFO 14)
52 (MX 15)
53 (TXT 16)
54 (AAAA 28) ; RFC3596
55 (SRV 33) ; RFC2782
56 (AXFR 252)
57 (MAILB 253)
58 (MAILA 254)
59 (* 255))
60 "Names of query types and their values.")
62 (defvar dns-classes
63 '((IN 1)
64 (CS 2)
65 (CH 3)
66 (HS 4))
67 "Classes of queries.")
69 (defun dns-write-bytes (value &optional length)
70 (let (bytes)
71 (dotimes (i (or length 1))
72 (push (% value 256) bytes)
73 (setq value (/ value 256)))
74 (dolist (byte bytes)
75 (insert byte))))
77 (defun dns-read-bytes (length)
78 (let ((value 0))
79 (dotimes (i length)
80 (setq value (logior (* value 256) (following-char)))
81 (forward-char 1))
82 value))
84 (defun dns-get (type spec)
85 (cadr (assq type spec)))
87 (defun dns-inverse-get (value spec)
88 (let ((found nil))
89 (while (and (not found)
90 spec)
91 (if (eq value (cadr (car spec)))
92 (setq found (caar spec))
93 (pop spec)))
94 found))
96 (defun dns-write-name (name)
97 (dolist (part (split-string name "\\."))
98 (dns-write-bytes (length part))
99 (insert part))
100 (dns-write-bytes 0))
102 (defun dns-read-string-name (string buffer)
103 (with-temp-buffer
104 (set-buffer-multibyte nil)
105 (insert string)
106 (goto-char (point-min))
107 (dns-read-name buffer)))
109 (defun dns-read-name (&optional buffer)
110 (let ((ended nil)
111 (name nil)
112 length)
113 (while (not ended)
114 (setq length (dns-read-bytes 1))
115 (if (= 192 (logand length (lsh 3 6)))
116 (let ((offset (+ (* (logand 63 length) 256)
117 (dns-read-bytes 1))))
118 (save-excursion
119 (when buffer
120 (set-buffer buffer))
121 (goto-char (1+ offset))
122 (setq ended (dns-read-name buffer))))
123 (if (zerop length)
124 (setq ended t)
125 (push (buffer-substring (point)
126 (progn (forward-char length) (point)))
127 name))))
128 (if (stringp ended)
129 (if (null name)
130 ended
131 (concat (mapconcat 'identity (nreverse name) ".") "." ended))
132 (mapconcat 'identity (nreverse name) "."))))
134 (defun dns-write (spec &optional tcp-p)
135 "Write a DNS packet according to SPEC.
136 If TCP-P, the first two bytes of the package with be the length field."
137 (with-temp-buffer
138 (set-buffer-multibyte nil)
139 (dns-write-bytes (dns-get 'id spec) 2)
140 (dns-write-bytes
141 (logior
142 (lsh (if (dns-get 'response-p spec) 1 0) -7)
143 (lsh
144 (cond
145 ((eq (dns-get 'opcode spec) 'query) 0)
146 ((eq (dns-get 'opcode spec) 'inverse-query) 1)
147 ((eq (dns-get 'opcode spec) 'status) 2)
148 (t (error "No such opcode: %s" (dns-get 'opcode spec))))
150 (lsh (if (dns-get 'authoritative-p spec) 1 0) -2)
151 (lsh (if (dns-get 'truncated-p spec) 1 0) -1)
152 (lsh (if (dns-get 'recursion-desired-p spec) 1 0) 0)))
153 (dns-write-bytes
154 (cond
155 ((eq (dns-get 'response-code spec) 'no-error) 0)
156 ((eq (dns-get 'response-code spec) 'format-error) 1)
157 ((eq (dns-get 'response-code spec) 'server-failure) 2)
158 ((eq (dns-get 'response-code spec) 'name-error) 3)
159 ((eq (dns-get 'response-code spec) 'not-implemented) 4)
160 ((eq (dns-get 'response-code spec) 'refused) 5)
161 (t 0)))
162 (dns-write-bytes (length (dns-get 'queries spec)) 2)
163 (dns-write-bytes (length (dns-get 'answers spec)) 2)
164 (dns-write-bytes (length (dns-get 'authorities spec)) 2)
165 (dns-write-bytes (length (dns-get 'additionals spec)) 2)
166 (dolist (query (dns-get 'queries spec))
167 (dns-write-name (car query))
168 (dns-write-bytes (cadr (assq (or (dns-get 'type query) 'A)
169 dns-query-types)) 2)
170 (dns-write-bytes (cadr (assq (or (dns-get 'class query) 'IN)
171 dns-classes)) 2))
172 (dolist (slot '(answers authorities additionals))
173 (dolist (resource (dns-get slot spec))
174 (dns-write-name (car resource))
175 (dns-write-bytes (cadr (assq (dns-get 'type resource) dns-query-types))
177 (dns-write-bytes (cadr (assq (dns-get 'class resource) dns-classes))
179 (dns-write-bytes (dns-get 'ttl resource) 4)
180 (dns-write-bytes (length (dns-get 'data resource)) 2)
181 (insert (dns-get 'data resource))))
182 (when tcp-p
183 (goto-char (point-min))
184 (dns-write-bytes (buffer-size) 2))
185 (buffer-string)))
187 (defun dns-read (packet)
188 (with-temp-buffer
189 (set-buffer-multibyte nil)
190 (let ((spec nil)
191 queries answers authorities additionals)
192 (insert packet)
193 (goto-char (point-min))
194 (push (list 'id (dns-read-bytes 2)) spec)
195 (let ((byte (dns-read-bytes 1)))
196 (push (list 'response-p (if (zerop (logand byte (lsh 1 7))) nil t))
197 spec)
198 (let ((opcode (logand byte (lsh 7 3))))
199 (push (list 'opcode
200 (cond ((eq opcode 0) 'query)
201 ((eq opcode 1) 'inverse-query)
202 ((eq opcode 2) 'status)))
203 spec))
204 (push (list 'authoritative-p (if (zerop (logand byte (lsh 1 2)))
205 nil t)) spec)
206 (push (list 'truncated-p (if (zerop (logand byte (lsh 1 2))) nil t))
207 spec)
208 (push (list 'recursion-desired-p
209 (if (zerop (logand byte (lsh 1 0))) nil t)) spec))
210 (let ((rc (logand (dns-read-bytes 1) 15)))
211 (push (list 'response-code
212 (cond
213 ((eq rc 0) 'no-error)
214 ((eq rc 1) 'format-error)
215 ((eq rc 2) 'server-failure)
216 ((eq rc 3) 'name-error)
217 ((eq rc 4) 'not-implemented)
218 ((eq rc 5) 'refused)))
219 spec))
220 (setq queries (dns-read-bytes 2))
221 (setq answers (dns-read-bytes 2))
222 (setq authorities (dns-read-bytes 2))
223 (setq additionals (dns-read-bytes 2))
224 (let ((qs nil))
225 (dotimes (i queries)
226 (push (list (dns-read-name)
227 (list 'type (dns-inverse-get (dns-read-bytes 2)
228 dns-query-types))
229 (list 'class (dns-inverse-get (dns-read-bytes 2)
230 dns-classes)))
231 qs))
232 (push (list 'queries qs) spec))
233 (dolist (slot '(answers authorities additionals))
234 (let ((qs nil)
235 type)
236 (dotimes (i (symbol-value slot))
237 (push (list (dns-read-name)
238 (list 'type
239 (setq type (dns-inverse-get (dns-read-bytes 2)
240 dns-query-types)))
241 (list 'class (dns-inverse-get (dns-read-bytes 2)
242 dns-classes))
243 (list 'ttl (dns-read-bytes 4))
244 (let ((length (dns-read-bytes 2)))
245 (list 'data
246 (dns-read-type
247 (buffer-substring
248 (point)
249 (progn (forward-char length) (point)))
250 type))))
251 qs))
252 (push (list slot qs) spec)))
253 (nreverse spec))))
255 (defun dns-read-int32 ()
256 ;; Full 32 bit Integers can't be handled by Emacs. If we use
257 ;; floats, it works.
258 (format "%.0f" (+ (* (dns-read-bytes 1) 16777216.0)
259 (dns-read-bytes 3))))
261 (defun dns-read-type (string type)
262 (let ((buffer (current-buffer))
263 (point (point)))
264 (prog1
265 (with-temp-buffer
266 (set-buffer-multibyte nil)
267 (insert string)
268 (goto-char (point-min))
269 (cond
270 ((eq type 'A)
271 (let ((bytes nil))
272 (dotimes (i 4)
273 (push (dns-read-bytes 1) bytes))
274 (mapconcat 'number-to-string (nreverse bytes) ".")))
275 ((eq type 'AAAA)
276 (let (hextets)
277 (dotimes (i 8)
278 (push (dns-read-bytes 2) hextets))
279 (mapconcat (lambda (n) (format "%x" n))
280 (nreverse hextets) ":")))
281 ((eq type 'SOA)
282 (list (list 'mname (dns-read-name buffer))
283 (list 'rname (dns-read-name buffer))
284 (list 'serial (dns-read-int32))
285 (list 'refresh (dns-read-int32))
286 (list 'retry (dns-read-int32))
287 (list 'expire (dns-read-int32))
288 (list 'minimum (dns-read-int32))))
289 ((eq type 'SRV)
290 (list (list 'priority (dns-read-bytes 2))
291 (list 'weight (dns-read-bytes 2))
292 (list 'port (dns-read-bytes 2))
293 (list 'target (dns-read-name buffer))))
294 ((eq type 'MX)
295 (cons (dns-read-bytes 2) (dns-read-name buffer)))
296 ((or (eq type 'CNAME) (eq type 'NS) (eq type 'PTR))
297 (dns-read-string-name string buffer))
298 (t string)))
299 (goto-char point))))
301 (defun dns-parse-resolv-conf ()
302 (when (file-exists-p "/etc/resolv.conf")
303 (with-temp-buffer
304 (insert-file-contents "/etc/resolv.conf")
305 (goto-char (point-min))
306 (while (re-search-forward "^nameserver[\t ]+\\([^ \t\n]+\\)" nil t)
307 (push (match-string 1) dns-servers))
308 (setq dns-servers (nreverse dns-servers)))))
310 (defun dns-read-txt (string)
311 (if (> (length string) 1)
312 (substring string 1)
313 string))
315 (defun dns-get-txt-answer (answers)
316 (let ((result "")
317 (do-next nil))
318 (dolist (answer answers)
319 (dolist (elem answer)
320 (when (consp elem)
321 (cond
322 ((eq (car elem) 'type)
323 (setq do-next (eq (cadr elem) 'TXT)))
324 ((eq (car elem) 'data)
325 (when do-next
326 (setq result (concat result (dns-read-txt (cadr elem))))))))))
327 result))
329 ;;; Interface functions.
330 (defmacro dns-make-network-process (server)
331 (if (featurep 'xemacs)
332 `(let ((coding-system-for-read 'binary)
333 (coding-system-for-write 'binary))
334 (open-network-stream "dns" (current-buffer)
335 ,server "domain" 'udp))
336 `(let ((server ,server)
337 (coding-system-for-read 'binary)
338 (coding-system-for-write 'binary))
339 (if (fboundp 'make-network-process)
340 (make-network-process
341 :name "dns"
342 :coding 'binary
343 :buffer (current-buffer)
344 :host server
345 :service "domain"
346 :type 'datagram)
347 ;; Older versions of Emacs doesn't have
348 ;; `make-network-process', so we fall back on opening a TCP
349 ;; connection to the DNS server.
350 (open-network-stream "dns" (current-buffer) server "domain")))))
352 (defvar dns-cache (make-vector 4096 0))
354 (defun query-dns-cached (name &optional type fullp reversep)
355 (let* ((key (format "%s:%s:%s:%s" name type fullp reversep))
356 (sym (intern-soft key dns-cache)))
357 (if (and sym
358 (boundp sym))
359 (symbol-value sym)
360 (let ((result (query-dns name type fullp reversep)))
361 (set (intern key dns-cache) result)
362 result))))
364 (defun query-dns (name &optional type fullp reversep)
365 "Query a DNS server for NAME of TYPE.
366 If FULLP, return the entire record returned.
367 If REVERSEP, look up an IP address."
368 (setq type (or type 'A))
369 (unless dns-servers
370 (dns-parse-resolv-conf))
372 (when reversep
373 (setq name (concat
374 (mapconcat 'identity (nreverse (split-string name "\\.")) ".")
375 ".in-addr.arpa")
376 type 'PTR))
378 (if (not dns-servers)
379 (message "No DNS server configuration found")
380 (with-temp-buffer
381 (set-buffer-multibyte nil)
382 (let ((process (condition-case ()
383 (dns-make-network-process (car dns-servers))
384 (error
385 (message
386 "dns: Got an error while trying to talk to %s"
387 (car dns-servers))
388 nil)))
389 (tcp-p (and (not (fboundp 'make-network-process))
390 (not (featurep 'xemacs))))
391 (step 100)
392 (times (* dns-timeout 1000))
393 (id (random 65000)))
394 (when process
395 (process-send-string
396 process
397 (dns-write `((id ,id)
398 (opcode query)
399 (queries ((,name (type ,type))))
400 (recursion-desired-p t))
401 tcp-p))
402 (while (and (zerop (buffer-size))
403 (> times 0))
404 (sit-for (/ step 1000.0))
405 (accept-process-output process 0 step)
406 (setq times (- times step)))
407 (condition-case nil
408 (delete-process process)
409 (error nil))
410 (when (and tcp-p
411 (>= (buffer-size) 2))
412 (goto-char (point-min))
413 (delete-region (point) (+ (point) 2)))
414 (when (and (>= (buffer-size) 2)
415 ;; We had a time-out.
416 (> times 0))
417 (let ((result (dns-read (buffer-string))))
418 (if fullp
419 result
420 (let ((answer (car (dns-get 'answers result))))
421 (when (eq type (dns-get 'type answer))
422 (if (eq type 'TXT)
423 (dns-get-txt-answer (dns-get 'answers result))
424 (dns-get 'data answer))))))))))))
426 (provide 'dns)
428 ;; arch-tag: d0edd0c4-4cce-4538-ae92-06c3356ee80a
429 ;;; dns.el ends here