0.8.12.40:
[sbcl.git] / contrib / sb-bsd-sockets / name-service.lisp
1 (in-package :sb-bsd-sockets)
2 #|| <a name="name-service"><h2>Name Service</h2></a>
3
4 <p>Presently name service is implemented by calling whatever
5 gethostbyname(2) uses.  This may be any or all of /etc/hosts, NIS, DNS,
6 or something completely different.  Typically it's controlled by
7 /etc/nsswitch.conf
8
9 <p> Direct links to the asynchronous resolver(3) routines would be nice to have
10 eventually, so that we can do DNS lookups in parallel with other things
11 |#
12
13 (defclass host-ent ()
14   ((name :initarg :name :accessor host-ent-name)
15    (aliases :initarg :aliases :accessor host-ent-aliases)
16    (address-type :initarg :type :accessor host-ent-address-type)
17                                         ; presently always AF_INET
18    (addresses :initarg :addresses :accessor host-ent-addresses)))
19
20 (defgeneric host-ent-address (host-ent))
21
22 (defmethod host-ent-address ((host-ent host-ent))
23   (car (host-ent-addresses host-ent)))
24
25 ;(define-condition host-not-found-error (socket-error)) ; host unknown
26 ;(define-condition no-address-error (socket-error)) ; valid name but no IP address
27 ;(define-condition no-recovery-error (socket-error)) ; name server error
28 ;(define-condition try-again-error (socket-error)) ; temporary
29
30 (defun make-host-ent (h)
31   (if (sb-grovel::foreign-nullp h) (name-service-error "gethostbyname"))
32   (let* ((length (sockint::hostent-length h))
33          (aliases (loop for i = 0 then (1+ i)
34                         for al = (sb-alien:deref (sockint::hostent-aliases h) i)
35                         while al
36                         collect al))
37          (addresses 
38           (loop for i = 0 then (1+ i)
39                 for ad = (sb-alien:deref (sockint::hostent-addresses h) i)
40                 until (sb-alien:null-alien ad)
41                 collect (ecase (sockint::hostent-type h)
42                           (#.sockint::af-inet
43                            (loop for i from 0 below length
44                                  collect (sb-alien:deref ad i)))
45                           (#.sockint::af-local
46                            (sb-alien:cast ad sb-alien:c-string))))))
47     (make-instance 'host-ent
48                    :name (sockint::hostent-name h)
49                    :type (sockint::hostent-type h)
50                    :aliases aliases
51                    :addresses addresses)))
52
53 (defun get-host-by-name (host-name)
54   "Returns a HOST-ENT instance for HOST-NAME or throws some kind of condition.
55 HOST-NAME may also be an IP address in dotted quad notation or some other
56 weird stuff - see gethostbyname(3) for grisly details."
57   (make-host-ent (sockint::gethostbyname host-name)))
58
59 (defun get-host-by-address (address)
60   "Returns a HOST-ENT instance for ADDRESS, which should be a vector of
61  (integer 0 255), or throws some kind of error.  See gethostbyaddr(3) for
62 grisly details."
63   (sockint::with-in-addr packed-addr ()
64     (let ((addr-vector (coerce address 'vector)))
65       (loop for i from 0 below (length addr-vector)
66             do (setf (sb-alien:deref (sockint::in-addr-addr packed-addr) i)
67                      (elt addr-vector i)))
68       (make-host-ent (sockint::gethostbyaddr packed-addr
69                                              4
70                                              sockint::af-inet)))))
71
72 ;;; The remainder is my fault - gw
73
74 (defvar *name-service-errno* 0
75   "The value of h_errno, after it's been fetched from Unix-land by calling
76 GET-NAME-SERVICE-ERRNO")
77
78 (defun name-service-error (where)
79   (get-name-service-errno)
80   ;; Comment next to NETDB_INTERNAL in netdb.h says "See errno.".
81   ;; This special case treatment hasn't actually been tested yet.
82   (if (= *name-service-errno* sockint::NETDB-INTERNAL)
83       (socket-error where)
84     (let ((condition
85            (condition-for-name-service-errno *name-service-errno*)))
86       (error condition :errno *name-service-errno* :syscall where))))
87
88 (define-condition name-service-error (condition)
89   ((errno :initform nil
90           :initarg :errno
91           :reader name-service-error-errno)
92    (symbol :initform nil :initarg :symbol :reader name-service-error-symbol)
93    (syscall :initform "an unknown location" :initarg :syscall :reader name-service-error-syscall))
94   (:report (lambda (c s)
95              (let ((num (name-service-error-errno c)))
96                (format s "Name service error in \"~A\": ~A (~A)"
97                        (name-service-error-syscall c)
98                        (or (name-service-error-symbol c)
99                            (name-service-error-errno c))
100                        (get-name-service-error-message num))))))
101
102 (defmacro define-name-service-condition (symbol name)
103   `(progn
104      (define-condition ,name (name-service-error)
105        ((symbol :reader name-service-error-symbol :initform (quote ,symbol))))
106      (push (cons ,symbol (quote ,name)) *conditions-for-name-service-errno*)))
107
108 (defparameter *conditions-for-name-service-errno* nil)
109
110 (define-name-service-condition sockint::NETDB-INTERNAL netdb-internal-error)
111 (define-name-service-condition sockint::NETDB-SUCCESS netdb-success-error)
112 (define-name-service-condition sockint::HOST-NOT-FOUND host-not-found-error)
113 (define-name-service-condition sockint::TRY-AGAIN try-again-error)
114 (define-name-service-condition sockint::NO-RECOVERY no-recovery-error)
115 ;; this is the same as the next one
116 ;;(define-name-service-condition sockint::NO-DATA no-data-error)
117 (define-name-service-condition sockint::NO-ADDRESS no-address-error)
118
119 (defun condition-for-name-service-errno (err)
120   (or (cdr (assoc err *conditions-for-name-service-errno* :test #'eql))
121       'name-service))
122
123
124
125 (defun get-name-service-errno ()
126   (setf *name-service-errno*
127         (sb-alien:alien-funcall
128          (sb-alien:extern-alien "get_h_errno" (function integer)))))
129
130 #-(and cmu solaris)
131 (progn
132   #+sbcl
133   (sb-alien:define-alien-routine "hstrerror"
134       sb-c-call:c-string
135     (errno integer))
136   #+cmu
137   (alien:def-alien-routine "hstrerror"
138       sb-c-call:c-string
139     (errno integer))
140   (defun get-name-service-error-message (num)
141   (hstrerror num))
142 )