1 ;;; dns.el --- Domain Name Service lookups
3 ;; Copyright (C) 2002-2011 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: network comm
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 "How many seconds to wait when doing DNS queries.")
30 (defvar dns-servers nil
31 "List of DNS servers to query.
32 If nil, /etc/resolv.conf and nslookup will be consulted.")
36 (defvar dns-query-types
59 "Names of query types and their values.")
66 "Classes of queries.")
68 (defun dns-write-bytes (value &optional length)
70 (dotimes (i (or length 1))
71 (push (% value 256) bytes)
72 (setq value (/ value 256)))
76 (defun dns-read-bytes (length)
79 (setq value (logior (* value 256) (following-char)))
83 (defun dns-get (type spec)
84 (cadr (assq type spec)))
86 (defun dns-inverse-get (value spec)
88 (while (and (not found)
90 (if (eq value (cadr (car spec)))
91 (setq found (caar spec))
95 (defun dns-write-name (name)
96 (dolist (part (split-string name "\\."))
97 (dns-write-bytes (length part))
101 (defun dns-read-string-name (string buffer)
103 (unless (featurep 'xemacs) (set-buffer-multibyte nil))
105 (goto-char (point-min))
106 (dns-read-name buffer)))
108 (defun dns-read-name (&optional buffer)
113 (setq length (dns-read-bytes 1))
114 (if (= 192 (logand length (lsh 3 6)))
115 (let ((offset (+ (* (logand 63 length) 256)
116 (dns-read-bytes 1))))
120 (goto-char (1+ offset))
121 (setq ended (dns-read-name buffer))))
124 (push (buffer-substring (point)
125 (progn (forward-char length) (point)))
130 (concat (mapconcat 'identity (nreverse name) ".") "." ended))
131 (mapconcat 'identity (nreverse name) "."))))
133 (defun dns-write (spec &optional tcp-p)
134 "Write a DNS packet according to SPEC.
135 If TCP-P, the first two bytes of the package with be the length field."
137 (unless (featurep 'xemacs) (set-buffer-multibyte nil))
138 (dns-write-bytes (dns-get 'id spec) 2)
141 (lsh (if (dns-get 'response-p spec) 1 0) -7)
144 ((eq (dns-get 'opcode spec) 'query) 0)
145 ((eq (dns-get 'opcode spec) 'inverse-query) 1)
146 ((eq (dns-get 'opcode spec) 'status) 2)
147 (t (error "No such opcode: %s" (dns-get 'opcode spec))))
149 (lsh (if (dns-get 'authoritative-p spec) 1 0) -2)
150 (lsh (if (dns-get 'truncated-p spec) 1 0) -1)
151 (lsh (if (dns-get 'recursion-desired-p spec) 1 0) 0)))
154 ((eq (dns-get 'response-code spec) 'no-error) 0)
155 ((eq (dns-get 'response-code spec) 'format-error) 1)
156 ((eq (dns-get 'response-code spec) 'server-failure) 2)
157 ((eq (dns-get 'response-code spec) 'name-error) 3)
158 ((eq (dns-get 'response-code spec) 'not-implemented) 4)
159 ((eq (dns-get 'response-code spec) 'refused) 5)
161 (dns-write-bytes (length (dns-get 'queries spec)) 2)
162 (dns-write-bytes (length (dns-get 'answers spec)) 2)
163 (dns-write-bytes (length (dns-get 'authorities spec)) 2)
164 (dns-write-bytes (length (dns-get 'additionals spec)) 2)
165 (dolist (query (dns-get 'queries spec))
166 (dns-write-name (car query))
167 (dns-write-bytes (cadr (assq (or (dns-get 'type query) 'A)
169 (dns-write-bytes (cadr (assq (or (dns-get 'class query) 'IN)
171 (dolist (slot '(answers authorities additionals))
172 (dolist (resource (dns-get slot spec))
173 (dns-write-name (car resource))
174 (dns-write-bytes (cadr (assq (dns-get 'type resource) dns-query-types))
176 (dns-write-bytes (cadr (assq (dns-get 'class resource) dns-classes))
178 (dns-write-bytes (dns-get 'ttl resource) 4)
179 (dns-write-bytes (length (dns-get 'data resource)) 2)
180 (insert (dns-get 'data resource))))
182 (goto-char (point-min))
183 (dns-write-bytes (buffer-size) 2))