Use IGNORE-PARSE-ERRORS where appropriate.
[iolib.git] / sockets / dns / nameservers.lisp
blob6266ca312e87e0d79e19889e083f225156a29b7e
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; nameservers.lisp --- Nameservers management.
4 ;;;
5 ;;; Copyright (C) 2006-2007, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;;
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
13 ;;;
14 ;;; This program 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.
18 ;;;
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :net.sockets)
26 (defvar *resolv.conf-file* "/etc/resolv.conf")
28 (defvar *dns-nameservers* nil
29 "List of the DNS nameservers to use.")
31 (defvar *dns-domain* nil
32 "The current machine's domain.")
34 (defvar *dns-search-domain* nil
35 "A domain name to be appended to the name to be searched when
36 the latter does not contain dots.")
38 (defvar *resolvconf-lock* (bt:make-lock "/etc/resolv.conf lock"))
40 ;;; Only parses NAMESERVER, DOMAIN and SEARCH directives, for now.
41 (defun parse-/etc/resolv.conf (file)
42 (let (nameservers domain search-domain)
43 (flet ((parse-one-line (tokens)
44 (when (< (length tokens) 2) (error 'parse-error))
45 (destructuring-bind (option value &rest more-values) tokens
46 (switch (option :test #'string-equal)
47 ("nameserver" (ignore-parse-errors
48 (push (ensure-address value)
49 nameservers)))
50 ("domain" (setf domain value))
51 ("search" (setf search-domain (cons value more-values)))))))
52 (iterate ((tokens (serialize-etc-file file)))
53 (ignore-errors (parse-one-line tokens)))
54 (values (nreverse nameservers) domain search-domain))))
56 (defun update-dns-parameters (file)
57 (multiple-value-bind (ns domain search)
58 (parse-/etc/resolv.conf file)
59 (setf *dns-nameservers* (or ns +ipv4-loopback+)
60 ;; everything after the first dot
61 *dns-domain* (cdr (split-sequence #\. domain :count 2))
62 *dns-search-domain* search)))
64 (defvar *resolv.conf-monitor*
65 (make-instance 'file-monitor
66 :file *resolv.conf-file*
67 :update-fn 'update-dns-parameters
68 :lock *resolvconf-lock*))