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