annotate lisp/net/dns.el @ 91461:27d290bd441c

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