b5f975554336eccd1978f829a6a6cccdc1780775
[sbcl.git] / contrib / sb-bsd-sockets / name-service.lisp
1 (in-package :sb-bsd-sockets)
2
3 (defclass host-ent ()
4   ((name :initarg :name :reader host-ent-name
5          :documentation "The name of the host")
6    ;; Deliberately not documented, since this isn't very useful,
7    ;; and the data isn't available when using getaddrinfo(). Unfortunately
8    ;; it is exported.
9    (aliases :initarg :aliases :reader host-ent-aliases)
10    ;; presently always AF_INET. Not exported.
11    (address-type :initarg :type :reader host-ent-address-type)
12    (addresses :initarg :addresses :reader host-ent-addresses
13               :documentation "A list of addresses for this host."))
14   (:documentation "This class represents the results of an address lookup."))
15
16 (defgeneric host-ent-address (host-ent)
17   (:documentation "Returns some valid address for HOST-ENT."))
18
19 (defmethod host-ent-address ((host-ent host-ent))
20   (car (host-ent-addresses host-ent)))
21
22 (defun make-host-ent (h &optional errno)
23   (when (sb-grovel::foreign-nullp h)
24     (name-service-error "gethostbyname" errno))
25   (let* ((length (sockint::hostent-length h))
26          (aliases (loop for i = 0 then (1+ i)
27                         for al = (sb-alien:deref (sockint::hostent-aliases h) i)
28                         while al
29                         collect al))
30          (addresses
31           (loop for i = 0 then (1+ i)
32                 for ad = (sb-alien:deref (sockint::hostent-addresses h) i)
33                 until (sb-alien:null-alien ad)
34                 collect (ecase (sockint::hostent-type h)
35                           (#.sockint::af-inet
36                            ;; CLH: Work around x86-64 darwin bug here.
37                            ;; The length is reported as 8, when it should be 4.
38                            #+(and darwin x86-64)
39                            (progn
40                              (assert (or (= length 4) (= length 8)))
41                              (naturalize-unsigned-byte-8-array ad 4))
42                            #-(and darwin x86-64)
43                            (progn
44                              (assert (= length 4))
45                              (naturalize-unsigned-byte-8-array ad length)))
46                           #-win32
47                           (#.sockint::af-local
48                            (sb-alien:cast ad sb-alien:c-string))))))
49     (make-instance 'host-ent
50                    :name (sockint::hostent-name h)
51                    :type (sockint::hostent-type h)
52                    :aliases aliases
53                    :addresses addresses)))
54
55 (defun naturalize-unsigned-byte-8-array (array length)
56   (let ((addr (make-array 4 :element-type '(unsigned-byte 8))))
57     (dotimes (i length)
58       (setf (elt addr i) (sb-alien:deref array i)))
59     addr))
60
61 ;;; Resolving
62
63 (defun get-host-by-name (host-name)
64   "Returns a HOST-ENT instance for HOST-NAME or signals a NAME-SERVICE-ERROR.
65 HOST-NAME may also be an IP address in dotted quad notation or some other
66 weird stuff - see gethostbyname(3) or getaddrinfo(3) for the details."
67   #+sb-bsd-sockets-addrinfo
68   (get-address-info host-name)
69   #-sb-bsd-sockets-addrinfo
70   (make-host-ent (sockint::gethostbyname host-name)))
71
72 (defun get-host-by-address (address)
73   "Returns a HOST-ENT instance for ADDRESS, which should be a vector of
74  (integer 0 255), or signals a NAME-SERVICE-ERROR.  See gethostbyaddr(3)
75  or gethostinfo(3) for details."
76   #+sb-bsd-sockets-addrinfo
77   (get-name-info address)
78   #-sb-bsd-sockets-addrinfo
79   (sockint::with-in-addr packed-addr ()
80     (let ((addr-vector (coerce address 'vector)))
81       (loop for i from 0 below (length addr-vector)
82             do (setf (sb-alien:deref (sockint::in-addr-addr packed-addr) i)
83                      (elt addr-vector i)))
84       (make-host-ent (sockint::gethostbyaddr packed-addr
85                                              4
86                                              sockint::af-inet)))))
87
88 ;;; Emulate the above two functions with getaddrinfo / getnameinfo
89
90 #+sb-bsd-sockets-addrinfo
91 (defun get-address-info (node)
92   (sb-alien:with-alien ((res (* (* sockint::addrinfo)) :local
93                              (sb-alien:make-alien (* sockint::addrinfo))))
94     (let ((err (sockint::getaddrinfo node nil nil res)))
95       (if (zerop err)
96           (let ((host-ent (make-instance 'host-ent
97                                          :name node
98                                          :type sockint::af-inet
99                                          :aliases nil
100                                          :addresses nil)))
101             (loop for sap = (sb-alien:deref res) then (sockint::addrinfo-next info)
102                   until (sb-alien::null-alien sap)
103                   for info = (sb-alien:cast sap (* sockint::addrinfo))
104                   ;; Only handle AF_INET currently.
105                   do (when (eq (sockint::addrinfo-family info) sockint::af-inet)
106                        (let* ((sockaddr (sockint::addrinfo-addr info))
107                               (address (sockint::sockaddr-in-addr sockaddr)))
108                          ;; The same effective result can be multiple time
109                          ;; in the list, with different socktypes. Only record
110                          ;; each address once.
111                          (setf (slot-value host-ent 'addresses)
112                                (adjoin (naturalize-unsigned-byte-8-array address
113                                                                          4)
114                                        (host-ent-addresses host-ent)
115                                        :test 'equalp)))))
116             (sockint::free-addrinfo (sb-alien:deref res))
117             host-ent)
118           (addrinfo-error "getaddrinfo" err)))))
119
120 (defconstant ni-max-host 1025)
121
122 #+sb-bsd-sockets-addrinfo
123 (defun get-name-info (address)
124   (assert (= (length address) 4))
125   (sockint::with-sockaddr-in sockaddr ()
126     (sb-alien:with-alien ((host-buf (array char #.ni-max-host)))
127       (setf (sockint::sockaddr-in-family sockaddr) sockint::af-inet)
128       (dotimes (i 4)
129         (setf (sb-alien:deref (sockint::sockaddr-in-addr sockaddr) i)
130               (aref address i)))
131       (let ((err (sockint::getnameinfo (sb-alien:alien-sap sockaddr)
132                                        (sb-alien:alien-size sockint::sockaddr-in :bytes)
133                                        (sb-alien:cast host-buf (* char)) ni-max-host
134                                        nil 0
135                                        sockint::ni-namereqd)))
136         (if (zerop err)
137             (make-instance 'host-ent
138                            :name (sb-alien::c-string-to-string
139                                   (sb-alien:alien-sap host-buf)
140                                   (sb-impl::default-external-format)
141                                   'character)
142                            :type sockint::af-inet
143                            :aliases nil
144                            :addresses (list address))
145             (addrinfo-error "getnameinfo" err))))))
146
147 ;;; Error handling
148
149 (defvar *name-service-errno* 0
150   "The value of h_errno, after it's been fetched from Unix-land by calling
151 GET-NAME-SERVICE-ERRNO")
152
153 (defun name-service-error (where &optional errno)
154   ;; There was a dummy docstring here for the texinfo extractor, but I
155   ;; see no reason for this to be documented in the manual, and removed
156   ;; it. -- JES
157   (let ((*name-service-errno* (get-name-service-errno errno)))
158     ;; Comment next to NETDB_INTERNAL in netdb.h says "See errno.".
159     ;; This special case treatment hasn't actually been tested yet.
160     #-win32
161     (if (= *name-service-errno* sockint::NETDB-INTERNAL)
162         (socket-error where)
163         (let ((condition
164                (condition-for-name-service-errno *name-service-errno*)))
165           (error condition :errno *name-service-errno* :syscall where)))))
166
167 (defun addrinfo-error (where error-code)
168   (let ((condition (condition-for-name-service-error-code error-code)))
169     (error condition :error-code error-code :syscall where)))
170
171 (define-condition name-service-error (condition)
172   ((errno :initform nil :initarg :errno :reader name-service-error-errno)
173    (error-code :initform nil :initarg :error-code
174                :reader name-service-error-error-code)
175    (symbol :initform nil :initarg :symbol :reader name-service-error-symbol)
176    (syscall :initform "an unknown location" :initarg :syscall :reader name-service-error-syscall))
177   (:report (lambda (c s)
178              (let* ((errno (name-service-error-errno c))
179                     (error-code (name-service-error-error-code c)))
180                (format s "Name service error in \"~A\": ~A (~A)"
181                        (name-service-error-syscall c)
182                        (or (name-service-error-symbol c)
183                            errno
184                            error-code)
185                        (get-name-service-error-message errno error-code))))))
186
187 (defparameter *conditions-for-name-service-errno* nil)
188 ;; getaddrinfo and getnameinfo return an error code, rather than using
189 ;; h_errno.  While on Linux there's no overlap between their possible
190 ;; values, this doesn't seem to be guaranteed on all systems.
191 (defparameter *conditions-for-name-service-error-code* nil)
192
193 ;; Define a special name-service-error for variour error cases, and associate
194 ;; them with the matching h_errno / error code.
195 (defmacro define-name-service-condition (errno-symbol error-code-symbol name)
196   `(progn
197      (define-condition ,name (name-service-error)
198        ((errno-symbol :reader name-service-error-errno-symbol
199                       :initform (quote ,errno-symbol))
200         (error-code-symbol :reader name-service-error-error-code-symbol
201                            :initform (quote ,error-code-symbol))))
202      (push (cons ,errno-symbol (quote ,name))
203            *conditions-for-name-service-errno*)
204      #+sb-bsd-sockets-addrinfo
205      (push (cons ,error-code-symbol (quote ,name))
206            *conditions-for-name-service-error-code*)))
207
208 #-win32
209 (define-name-service-condition
210     sockint::NETDB-INTERNAL
211     nil ;; Doesn't map directly to any getaddrinfo error code
212     netdb-internal-error)
213 #-win32
214 (define-name-service-condition
215     sockint::NETDB-SUCCESS
216     nil ;; Doesn't map directly to any getaddrinfo error code
217     netdb-success-error)
218 (define-name-service-condition
219     sockint::HOST-NOT-FOUND
220     sockint::EAI-NONAME
221     host-not-found-error)
222 (define-name-service-condition
223     sockint::TRY-AGAIN
224     sockint::EAI-AGAIN
225     try-again-error)
226 (define-name-service-condition
227     sockint::NO-RECOVERY
228     sockint::EAI-FAIL
229     no-recovery-error)
230 (define-name-service-condition
231     sockint::NO-ADDRESS  ;; Also defined as NO-DATA, with the same value
232     #-freebsd sockint::EAI-NODATA #+freebsd nil
233     no-address-error)
234
235 (defun condition-for-name-service-errno (err)
236   (or (cdr (assoc err *conditions-for-name-service-errno* :test #'eql))
237       'name-service-error))
238
239 (defun condition-for-name-service-error-code (err)
240   (or (cdr (assoc err *conditions-for-name-service-error-code* :test #'eql))
241       'name-service-error))
242
243 (defun get-name-service-errno (&optional errno)
244   (setf *name-service-errno*
245         (or errno
246             (sb-alien:alien-funcall
247              #-win32
248              (sb-alien:extern-alien "get_h_errno" (function integer))
249              #+win32
250              (sb-alien:extern-alien "WSAGetLastError" (function integer))))))
251
252 (defun get-name-service-error-message (errno error-code)
253   #-win32
254   (if errno
255       (sockint::h-strerror errno)
256       (sockint::gai-strerror error-code)))