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