comparison lisp/net/dns.el @ 100993:a16e9f7c2536

Merge from gnus--devo--0 Revision: emacs@sv.gnu.org/emacs--devo--0--patch-1513
author Miles Bader <miles@gnu.org>
date Fri, 09 Jan 2009 03:01:50 +0000
parents a9dc0e7c3f2b
children 2e34067186f4
comparison
equal deleted inserted replaced
100992:5cb6d276b93a 100993:a16e9f7c2536
27 27
28 (defvar dns-timeout 5 28 (defvar dns-timeout 5
29 "How many seconds to wait when doing DNS queries.") 29 "How many seconds to wait when doing DNS queries.")
30 30
31 (defvar dns-servers nil 31 (defvar dns-servers nil
32 "Which DNS servers to query. 32 "List of DNS servers to query.
33 If nil, /etc/resolv.conf will be consulted.") 33 If nil, /etc/resolv.conf and nslookup will be consulted.")
34 34
35 ;;; Internal code: 35 ;;; Internal code:
36 36
37 (defvar dns-query-types 37 (defvar dns-query-types
38 '((A 1) 38 '((A 1)
296 ((or (eq type 'CNAME) (eq type 'NS) (eq type 'PTR)) 296 ((or (eq type 'CNAME) (eq type 'NS) (eq type 'PTR))
297 (dns-read-string-name string buffer)) 297 (dns-read-string-name string buffer))
298 (t string))) 298 (t string)))
299 (goto-char point)))) 299 (goto-char point))))
300 300
301 (defun dns-parse-resolv-conf () 301 (defun dns-set-servers ()
302 (when (file-exists-p "/etc/resolv.conf") 302 "Set `dns-servers' to a list of DNS servers or nil if none are found.
303 (with-temp-buffer 303 Parses \"/etc/resolv.conf\" or calls \"nslookup\"."
304 (insert-file-contents "/etc/resolv.conf") 304 (or (when (file-exists-p "/etc/resolv.conf")
305 (goto-char (point-min)) 305 (setq dns-servers nil)
306 (while (re-search-forward "^nameserver[\t ]+\\([^ \t\n]+\\)" nil t) 306 (with-temp-buffer
307 (push (match-string 1) dns-servers)) 307 (insert-file-contents "/etc/resolv.conf")
308 (setq dns-servers (nreverse dns-servers))))) 308 (goto-char (point-min))
309 (while (re-search-forward "^nameserver[\t ]+\\([^ \t\n]+\\)" nil t)
310 (push (match-string 1) dns-servers))
311 (setq dns-servers (nreverse dns-servers))))
312 (when (executable-find "nslookup")
313 (with-temp-buffer
314 (call-process "nslookup" nil t nil "localhost")
315 (goto-char (point-min))
316 (re-search-forward
317 "^Address:[ \t]*\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
318 (setq dns-servers (list (match-string 1)))))))
309 319
310 (defun dns-read-txt (string) 320 (defun dns-read-txt (string)
311 (if (> (length string) 1) 321 (if (> (length string) 1)
312 (substring string 1) 322 (substring string 1)
313 string)) 323 string))
349 ;; connection to the DNS server. 359 ;; connection to the DNS server.
350 (open-network-stream "dns" (current-buffer) server "domain"))))) 360 (open-network-stream "dns" (current-buffer) server "domain")))))
351 361
352 (defvar dns-cache (make-vector 4096 0)) 362 (defvar dns-cache (make-vector 4096 0))
353 363
354 (defun query-dns-cached (name &optional type fullp reversep) 364 (defun dns-query-cached (name &optional type fullp reversep)
355 (let* ((key (format "%s:%s:%s:%s" name type fullp reversep)) 365 (let* ((key (format "%s:%s:%s:%s" name type fullp reversep))
356 (sym (intern-soft key dns-cache))) 366 (sym (intern-soft key dns-cache)))
357 (if (and sym 367 (if (and sym
358 (boundp sym)) 368 (boundp sym))
359 (symbol-value sym) 369 (symbol-value sym)
360 (let ((result (query-dns name type fullp reversep))) 370 (let ((result (dns-query name type fullp reversep)))
361 (set (intern key dns-cache) result) 371 (set (intern key dns-cache) result)
362 result)))) 372 result))))
363 373
364 (defun query-dns (name &optional type fullp reversep) 374 ;; The old names `query-dns' and `query-dns-cached' weren't used in Emacs 23
375 ;; yet, so no alias are provided. --rsteib
376
377 (defun dns-query (name &optional type fullp reversep)
365 "Query a DNS server for NAME of TYPE. 378 "Query a DNS server for NAME of TYPE.
366 If FULLP, return the entire record returned. 379 If FULLP, return the entire record returned.
367 If REVERSEP, look up an IP address." 380 If REVERSEP, look up an IP address."
368 (setq type (or type 'A)) 381 (setq type (or type 'A))
369 (unless dns-servers 382 (unless dns-servers
370 (dns-parse-resolv-conf)) 383 (dns-set-servers))
371 384
372 (when reversep 385 (when reversep
373 (setq name (concat 386 (setq name (concat
374 (mapconcat 'identity (nreverse (split-string name "\\.")) ".") 387 (mapconcat 'identity (nreverse (split-string name "\\.")) ".")
375 ".in-addr.arpa") 388 ".in-addr.arpa")