1 (in-package :sb-bsd-sockets)
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
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."))
16 (defgeneric host-ent-address (host-ent)
17 (:documentation "Returns some valid address for HOST-ENT."))
19 (defmethod host-ent-address ((host-ent host-ent))
20 (car (host-ent-addresses host-ent)))
22 (defun make-host-ent (h &optional errno)
23 (when (sb-alien:null-alien 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)
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)
36 ;; CLH: Work around x86-64 darwin bug here.
37 ;; The length is reported as 8, when it should be 4.
38 ;; FIXME: this is rumored to be fixed in 10.5
41 (assert (or (= length 4) (= length 8)))
42 (naturalize-unsigned-byte-8-array ad 4))
46 (naturalize-unsigned-byte-8-array ad length)))
49 (sb-alien:cast ad sb-alien:c-string))))))
50 (make-instance 'host-ent
51 :name (sockint::hostent-name h)
52 :type (sockint::hostent-type h)
54 :addresses addresses)))
56 (defun naturalize-unsigned-byte-8-array (array length)
57 (let ((addr (make-array 4 :element-type '(unsigned-byte 8))))
59 (setf (elt addr i) (sb-alien:deref array i)))
64 #-sb-bsd-sockets-addrinfo
65 (sb-ext:defglobal **gethostby-lock** (sb-thread:make-mutex :name "gethostby lock"))
67 (defun get-host-by-name (host-name)
68 "Returns a HOST-ENT instance for HOST-NAME or signals a NAME-SERVICE-ERROR.
69 HOST-NAME may also be an IP address in dotted quad notation or some other
70 weird stuff - see gethostbyname(3) or getaddrinfo(3) for the details."
71 #+sb-bsd-sockets-addrinfo
72 (get-address-info host-name)
73 #-sb-bsd-sockets-addrinfo
74 (sb-thread::with-system-mutex (**gethostby-lock** :allow-with-interrupts t)
75 (make-host-ent (sockint::gethostbyname host-name))))
77 (defun get-host-by-address (address)
78 "Returns a HOST-ENT instance for ADDRESS, which should be a vector of
79 (integer 0 255), or signals a NAME-SERVICE-ERROR. See gethostbyaddr(3)
80 or gethostinfo(3) for details."
81 #+sb-bsd-sockets-addrinfo
82 (get-name-info address)
83 #-sb-bsd-sockets-addrinfo
84 (sb-thread::with-system-mutex (**gethostby-lock** :allow-with-interrupts t)
85 (sockint::with-in-addr packed-addr ()
86 (let ((addr-vector (coerce address 'vector)))
87 (loop for i from 0 below (length addr-vector)
88 do (setf (sb-alien:deref (sockint::in-addr-addr packed-addr) i)
90 (make-host-ent (sockint::gethostbyaddr packed-addr
92 sockint::af-inet))))))
94 ;;; Emulate the above two functions with getaddrinfo / getnameinfo
96 #+sb-bsd-sockets-addrinfo
97 (defun get-address-info (node)
98 (sb-alien:with-alien ((buf (sb-alien:array (* sockint::addrinfo) 1)))
99 (let* ((res (sb-alien:addr (sb-alien:deref buf 0)))
100 (err (sockint::getaddrinfo node nil nil res)))
102 (let ((host-ent (make-instance 'host-ent
104 :type sockint::af-inet
107 (loop for sap = (sb-alien:deref res) then (sockint::addrinfo-next info)
108 until (sb-alien::null-alien sap)
109 for info = (sb-alien:cast sap (* sockint::addrinfo))
110 ;; Only handle AF_INET currently.
111 do (when (eq (sockint::addrinfo-family info) sockint::af-inet)
112 (let* ((sockaddr (sockint::addrinfo-addr info))
113 (address (sockint::sockaddr-in-addr sockaddr)))
114 ;; The same effective result can be multiple time
115 ;; in the list, with different socktypes. Only record
116 ;; each address once.
117 (setf (slot-value host-ent 'addresses)
118 (adjoin (naturalize-unsigned-byte-8-array address
120 (host-ent-addresses host-ent)
122 (sockint::freeaddrinfo (sb-alien:deref res))
124 (addrinfo-error "getaddrinfo" err)))))
126 (defconstant ni-max-host 1025)
128 #+sb-bsd-sockets-addrinfo
129 (defun get-name-info (address)
130 (assert (= (length address) 4))
131 (sockint::with-sockaddr-in sockaddr ()
132 (sb-alien:with-alien ((host-buf (array char #.ni-max-host)))
133 #+darwin (setf (sockint::sockaddr-in-len sockaddr) 16)
134 (setf (sockint::sockaddr-in-family sockaddr) sockint::af-inet)
136 (setf (sb-alien:deref (sockint::sockaddr-in-addr sockaddr) i)
138 (let ((err (sockint::getnameinfo (sb-alien:alien-sap sockaddr)
139 (sb-alien:alien-size sockint::sockaddr-in :bytes)
140 (sb-alien:cast host-buf (* char)) ni-max-host
142 sockint::ni-namereqd)))
144 (make-instance 'host-ent
145 :name (sb-alien::c-string-to-string
146 (sb-alien:alien-sap host-buf)
147 (sb-impl::default-external-format)
149 :type sockint::af-inet
151 :addresses (list address))
152 (addrinfo-error "getnameinfo" err))))))
156 (defvar *name-service-errno* 0
157 "The value of h_errno, after it's been fetched from Unix-land by calling
158 GET-NAME-SERVICE-ERRNO")
160 (defun name-service-error (where &optional errno)
161 ;; There was a dummy docstring here for the texinfo extractor, but I
162 ;; see no reason for this to be documented in the manual, and removed
164 (let ((*name-service-errno* (get-name-service-errno errno)))
165 ;; Comment next to NETDB_INTERNAL in netdb.h says "See errno.".
166 ;; This special case treatment hasn't actually been tested yet.
168 (if (= *name-service-errno* sockint::NETDB-INTERNAL)
171 (condition-for-name-service-errno *name-service-errno*)))
172 (error condition :errno *name-service-errno* :syscall where)))))
174 (defun addrinfo-error (where error-code)
175 (let ((condition (condition-for-name-service-error-code error-code)))
176 (error condition :error-code error-code :syscall where)))
178 (define-condition name-service-error (error)
179 ((errno :initform nil :initarg :errno :reader name-service-error-errno)
180 (error-code :initform nil :initarg :error-code
181 :reader name-service-error-error-code)
182 (symbol :initform nil :initarg :symbol :reader name-service-error-symbol)
183 (syscall :initform "an unknown location" :initarg :syscall :reader name-service-error-syscall))
184 (:report (lambda (c s)
185 (let* ((errno (name-service-error-errno c))
186 (error-code (name-service-error-error-code c)))
187 (format s "Name service error in \"~A\": ~A (~A)"
188 (name-service-error-syscall c)
189 (or (name-service-error-symbol c)
192 (get-name-service-error-message errno error-code))))))
194 (defparameter *conditions-for-name-service-errno* nil)
195 ;; getaddrinfo and getnameinfo return an error code, rather than using
196 ;; h_errno. While on Linux there's no overlap between their possible
197 ;; values, this doesn't seem to be guaranteed on all systems.
198 (defparameter *conditions-for-name-service-error-code* nil)
200 ;; Define a special name-service-error for variour error cases, and associate
201 ;; them with the matching h_errno / error code.
202 (defmacro define-name-service-condition (errno-symbol error-code-symbol name)
204 (define-condition ,name (name-service-error)
205 ((errno-symbol :reader name-service-error-errno-symbol
206 :initform (quote ,errno-symbol))
207 (error-code-symbol :reader name-service-error-error-code-symbol
208 :initform (quote ,error-code-symbol))))
209 (push (cons ,errno-symbol (quote ,name))
210 *conditions-for-name-service-errno*)
211 #+sb-bsd-sockets-addrinfo
212 (push (cons ,error-code-symbol (quote ,name))
213 *conditions-for-name-service-error-code*)))
216 (define-name-service-condition
217 sockint::NETDB-INTERNAL
218 nil ;; Doesn't map directly to any getaddrinfo error code
219 netdb-internal-error)
221 (define-name-service-condition
222 sockint::NETDB-SUCCESS
223 nil ;; Doesn't map directly to any getaddrinfo error code
225 (define-name-service-condition
226 sockint::HOST-NOT-FOUND
228 host-not-found-error)
229 (define-name-service-condition
233 (define-name-service-condition
237 (define-name-service-condition
238 ;; Also defined as NO-DATA, with the same value
240 ;; getaddrinfo() as of RFC 3493 can no longer distinguish between
241 ;; host no found and address not found
245 (defun condition-for-name-service-errno (err)
246 (or (cdr (assoc err *conditions-for-name-service-errno* :test #'eql))
247 'name-service-error))
249 (defun condition-for-name-service-error-code (err)
250 (or (cdr (assoc err *conditions-for-name-service-error-code* :test #'eql))
251 'name-service-error))
253 (defun get-name-service-errno (&optional errno)
254 (setf *name-service-errno*
256 (sb-alien:alien-funcall
258 (sb-alien:extern-alien "get_h_errno" (function integer))
260 (sb-alien:extern-alien "WSAGetLastError" (function integer))))))
262 (defun get-name-service-error-message (errno error-code)
265 (sockint::h-strerror errno)
266 (sockint::gai-strerror error-code)))