0.8.7.49:
[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 get-host-by-name (host-name)
31   "Returns a HOST-ENT instance for HOST-NAME or throws some kind of condition.
32 HOST-NAME may also be an IP address in dotted quad notation or some other
33 weird stuff - see gethostbyname(3) for grisly details."
34   (let ((h (sockint::gethostbyname host-name)))
35     (make-host-ent h)))
36
37 (defun get-host-by-address (address)
38   "Returns a HOST-ENT instance for ADDRESS, which should be a vector of
39 (integer 0 255), or throws some kind of error.  See gethostbyaddr(3) for
40 grisly details."
41   (let ((packed-addr (sockint::allocate-in-addr)))
42     (loop for i from 0 to 3 
43           do (setf (sockint::in-addr-addr packed-addr i) (elt address i)))
44     (make-host-ent
45      (sb-sys:with-pinned-objects (packed-addr)
46       (sockint::gethostbyaddr (sb-grovel::array-data-address packed-addr)
47                               4
48                               sockint::af-inet)))))
49
50 (defun make-host-ent (h)
51   (if (sb-grovel::foreign-nullp h) (name-service-error "gethostbyname"))
52   (let* ((local-h (sb-grovel::foreign-vector h 1 sockint::size-of-hostent))
53          (length (sockint::hostent-length local-h))
54          (aliases 
55           (loop for i = 0 then (1+ i)
56                 for al = (sb-sys:sap-ref-sap
57                           (sb-sys:int-sap (sockint::hostent-aliases local-h))
58                           (* i 4))
59                 until (= (sb-sys:sap-int al) 0) 
60                 collect (sb-c-call::%naturalize-c-string al)))
61          (address0 (sb-sys:sap-ref-sap (sb-sys:int-sap (sockint::hostent-addresses local-h)) 0))
62          (addresses 
63           (loop for i = 0 then (+ length i)
64                 for ad = (sb-sys:sap-ref-32 address0 i)
65                 while (> ad 0)
66                 collect
67                 (sb-grovel::foreign-vector (sb-sys:sap+ address0 i) 1 length))))
68     (make-instance 'host-ent
69                    :name (sb-c-call::%naturalize-c-string
70                           (sb-sys:int-sap (sockint::hostent-name local-h)))
71                    :type (sockint::hostent-type local-h)
72                    :aliases aliases
73                    :addresses addresses)))
74
75 ;;; The remainder is my fault - gw
76
77 (defvar *name-service-errno* 0
78   "The value of h_errno, after it's been fetched from Unix-land by calling
79 GET-NAME-SERVICE-ERRNO")
80
81 (defun name-service-error (where)
82   (get-name-service-errno)
83   ;; Comment next to NETDB_INTERNAL in netdb.h says "See errno.".
84   ;; This special case treatment hasn't actually been tested yet.
85   (if (= *name-service-errno* sockint::NETDB-INTERNAL)
86       (socket-error where)
87     (let ((condition
88            (condition-for-name-service-errno *name-service-errno*)))
89       (error condition :errno *name-service-errno* :syscall where))))
90
91 (define-condition name-service-error (condition)
92   ((errno :initform nil
93           :initarg :errno
94           :reader name-service-error-errno)
95    (symbol :initform nil :initarg :symbol :reader name-service-error-symbol)
96    (syscall :initform "an unknown location" :initarg :syscall :reader name-service-error-syscall))
97   (:report (lambda (c s)
98              (let ((num (name-service-error-errno c)))
99                (format s "Name service error in \"~A\": ~A (~A)"
100                        (name-service-error-syscall c)
101                        (or (name-service-error-symbol c)
102                            (name-service-error-errno c))
103                        (get-name-service-error-message num))))))
104
105 (defmacro define-name-service-condition (symbol name)
106   `(progn
107      (define-condition ,name (name-service-error)
108        ((symbol :reader name-service-error-symbol :initform (quote ,symbol))))
109      (push (cons ,symbol (quote ,name)) *conditions-for-name-service-errno*)))
110
111 (defparameter *conditions-for-name-service-errno* nil)
112
113 (define-name-service-condition sockint::NETDB-INTERNAL netdb-internal-error)
114 (define-name-service-condition sockint::NETDB-SUCCESS netdb-success-error)
115 (define-name-service-condition sockint::HOST-NOT-FOUND host-not-found-error)
116 (define-name-service-condition sockint::TRY-AGAIN try-again-error)
117 (define-name-service-condition sockint::NO-RECOVERY no-recovery-error)
118 ;; this is the same as the next one
119 ;;(define-name-service-condition sockint::NO-DATA no-data-error)
120 (define-name-service-condition sockint::NO-ADDRESS no-address-error)
121
122 (defun condition-for-name-service-errno (err)
123   (or (cdr (assoc err *conditions-for-name-service-errno* :test #'eql))
124       'name-service))
125
126
127
128 (defun get-name-service-errno ()
129   (setf *name-service-errno*
130         (sb-alien:alien-funcall
131          (sb-alien:extern-alien "get_h_errno" (function integer)))))
132
133 #-(and cmu solaris)
134 (progn
135   #+sbcl
136   (sb-alien:define-alien-routine "hstrerror"
137       sb-c-call:c-string
138     (errno integer))
139   #+cmu
140   (alien:def-alien-routine "hstrerror"
141       sb-c-call:c-string
142     (errno integer))
143   (defun get-name-service-error-message (num)
144   (hstrerror num))
145 )
146