1 ;;; dns.el --- Domain Name Service lookups
2 ;; Copyright (C) 2002 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
28 (defvar dns-query-types
49 "Names of query types and their values.")
56 "Classes of queries.")
58 (defun dns-write-bytes (value &optional length)
60 (dotimes (i (or length 1))
61 (push (% value 256) bytes)
62 (setq value (/ value 256)))
66 (defun dns-read-bytes (length)
69 (setq value (logior (* value 256) (following-char)))
73 (defun dns-get (type spec)
74 (cadr (assq type spec)))
76 (defun dns-inverse-get (value spec)
78 (while (and (not found)
80 (if (eq value (cadr (car spec)))
81 (setq found (caar spec))
85 (defun dns-write-name (name)
86 (dolist (part (split-string name "\\."))
87 (dns-write-bytes (length part))
91 (defun dns-read-string-name (string buffer)
92 (mm-with-unibyte-buffer
94 (goto-char (point-min))
95 (dns-read-name buffer)))
97 (defun dns-read-name (&optional buffer)
102 (setq length (dns-read-bytes 1))
103 (if (= 192 (logand length (lsh 3 6)))
104 (let ((offset (+ (* (logand 63 length) 256)
105 (dns-read-bytes 1))))
109 (goto-char (1+ offset))
110 (setq ended (dns-read-name buffer))))
113 (push (buffer-substring (point)
114 (progn (forward-char length) (point)))
117 (concat (mapconcat 'identity (nreverse name) ".") "." ended)
118 (mapconcat 'identity (nreverse name) "."))))
120 (defun dns-write (spec)
121 "Write a DNS packet according to SPEC.
122 \(dns-write '((id 2) (opcode query) (queries (("www.gnus.org")))))"
124 (dns-write-bytes (dns-get 'id spec) 2)
127 (lsh (if (dns-get 'response-p spec) 1 0) -7)
130 ((eq (dns-get 'opcode spec) 'query) 0)
131 ((eq (dns-get 'opcode spec) 'inverse-query) 1)
132 ((eq (dns-get 'opcode spec) 'status) 2)
133 (t (error "No such opcode: %s" (dns-get 'opcode spec))))
135 (lsh (if (dns-get 'authoritative-p spec) 1 0) -2)
136 (lsh (if (dns-get 'truncated-p spec) 1 0) -1)
137 (lsh (if (dns-get 'recursion-desired-p spec) 1 0) 0)))
140 ((eq (dns-get 'response-code spec) 'no-error) 0)
141 ((eq (dns-get 'response-code spec) 'format-error) 1)
142 ((eq (dns-get 'response-code spec) 'server-failure) 2)
143 ((eq (dns-get 'response-code spec) 'name-error) 3)
144 ((eq (dns-get 'response-code spec) 'not-implemented) 4)
145 ((eq (dns-get 'response-code spec) 'refused) 5)
147 (dns-write-bytes (length (dns-get 'queries spec)) 2)
148 (dns-write-bytes (length (dns-get 'answers spec)) 2)
149 (dns-write-bytes (length (dns-get 'authorities spec)) 2)
150 (dns-write-bytes (length (dns-get 'additionals spec)) 2)
151 (dolist (query (dns-get 'queries spec))
152 (dns-write-name (car query))
153 (dns-write-bytes (cadr (assq (or (dns-get 'type query) 'A)
155 (dns-write-bytes (cadr (assq (or (dns-get 'class query) 'IN)
157 (dolist (slot '(answers authorities additionals))
158 (dolist (resource (dns-get slot spec))
159 (dns-write-name (car resource))
160 (dns-write-bytes (cadr (assq (dns-get 'type resource) dns-query-types))
162 (dns-write-bytes (cadr (assq (dns-get 'class resource) dns-classes))
164 (dns-write-bytes (dns-get 'ttl resource) 4)
165 (dns-write-bytes (length (dns-get 'data resource)) 2)
166 (insert (dns-get 'data resource))))
169 (defun dns-read (packet)
170 (mm-with-unibyte-buffer
172 queries answers authorities additionals)
174 (goto-char (point-min))
175 (push (list 'id (dns-read-bytes 2)) spec)
176 (let ((byte (dns-read-bytes 1)))
177 (push (list 'response-p (if (zerop (logand byte (lsh 1 7))) nil t))
179 (let ((opcode (logand byte (lsh 7 3))))
181 (cond ((eq opcode 0) 'query)
182 ((eq opcode 1) 'inverse-query)
183 ((eq opcode 2) 'status)))
185 (push (list 'authoritative-p (if (zerop (logand byte (lsh 1 2)))
187 (push (list 'truncated-p (if (zerop (logand byte (lsh 1 2))) nil t))
189 (push (list 'recursion-desired-p
190 (if (zerop (logand byte (lsh 1 0))) nil t)) spec))
191 (let ((rc (logand (dns-read-bytes 1) 15)))
192 (push (list 'response-code
194 ((eq rc 0) 'no-error)
195 ((eq rc 1) 'format-error)
196 ((eq rc 2) 'server-failure)
197 ((eq rc 3) 'name-error)
198 ((eq rc 4) 'not-implemented)
199 ((eq rc 5) 'refused)))
201 (setq queries (dns-read-bytes 2))
202 (setq answers (dns-read-bytes 2))
203 (setq authorities (dns-read-bytes 2))
204 (setq additionals (dns-read-bytes 2))
207 (push (list (dns-read-name)
208 (list 'type (dns-inverse-get (dns-read-bytes 2)
210 (list 'class (dns-inverse-get (dns-read-bytes 2)
213 (push (list 'queries qs) spec))
214 (dolist (slot '(answers authorities additionals))
217 (dotimes (i (symbol-value slot))
218 (push (list (dns-read-name)
220 (setq type (dns-inverse-get (dns-read-bytes 2)
222 (list 'class (dns-inverse-get (dns-read-bytes 2)
224 (list 'ttl (dns-read-bytes 4))
225 (let ((length (dns-read-bytes 2)))
230 (progn (forward-char length) (point)))
233 (push (list slot qs) spec)))
236 (defun dns-read-type (string type)
237 (let ((buffer (current-buffer))
240 (mm-with-unibyte-buffer
242 (goto-char (point-min))
247 (push (dns-read-bytes 1) bytes))
248 (mapconcat 'number-to-string (nreverse bytes) ".")))
250 (dns-read-string-name string buffer))
254 ;;; Interface functions.
256 (defun query-dns (name &optional type)
257 "Query a DNS server for NAME of TYPE."
258 (mm-with-unibyte-buffer
259 (let ((coding-system-for-read 'binary)
260 (coding-system-for-write 'binary))
262 (make-network-process
265 :buffer (current-buffer)
266 :host "ns2.netfonds.no"
274 (recursion-desired-p t))))
275 (while (zerop (buffer-size))
276 (accept-process-output process 1))
277 (dns-read (buffer-string))))))