X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=contrib%2Fsb-bsd-sockets%2Fname-service.lisp;h=d55ee8dba1205c34135302f573d20ec7f4badf11;hb=c03ebb54770cfa613d4b706a80e5be231786a5d0;hp=5f038595820f4622c36374d94363a771530bf3a3;hpb=10d2c05ea44ca0837091434fe2223c0c31687615;p=sbcl.git diff --git a/contrib/sb-bsd-sockets/name-service.lisp b/contrib/sb-bsd-sockets/name-service.lisp index 5f03859..d55ee8d 100644 --- a/contrib/sb-bsd-sockets/name-service.lisp +++ b/contrib/sb-bsd-sockets/name-service.lisp @@ -1,21 +1,19 @@ (in-package :sb-bsd-sockets) -#||

Name Service

- -

Presently name service is implemented by calling whatever -gethostbyname(2) uses. This may be any or all of /etc/hosts, NIS, DNS, -or something completely different. Typically it's controlled by -/etc/nsswitch.conf - -

Direct links to the asynchronous resolver(3) routines would be nice to have -eventually, so that we can do DNS lookups in parallel with other things -|# (defclass host-ent () ((name :initarg :name :accessor host-ent-name) (aliases :initarg :aliases :accessor host-ent-aliases) (address-type :initarg :type :accessor host-ent-address-type) - ; presently always AF_INET - (addresses :initarg :addresses :accessor host-ent-addresses))) + ; presently always AF_INET + (addresses :initarg :addresses :accessor host-ent-addresses)) + ;; FIXME: Our Texinfo documentation extracter need at least his to spit + ;; out the signature. Real documentation would be better... + (:documentation "")) + +(defgeneric host-ent-address (host-ent) + ;; FIXME: Our Texinfo documentation extracter need at least his to spit + ;; out the signature. Real documentation would be better... + (:documentation "")) (defmethod host-ent-address ((host-ent host-ent)) (car (host-ent-addresses host-ent))) @@ -25,50 +23,51 @@ eventually, so that we can do DNS lookups in parallel with other things ;(define-condition no-recovery-error (socket-error)) ; name server error ;(define-condition try-again-error (socket-error)) ; temporary +(defun make-host-ent (h) + (if (sb-alien:null-alien h) (name-service-error "gethostbyname")) + (let* ((length (sockint::hostent-length h)) + (aliases (loop for i = 0 then (1+ i) + for al = (sb-alien:deref (sockint::hostent-aliases h) i) + while al + collect al)) + (addresses + (loop for i = 0 then (1+ i) + for ad = (sb-alien:deref (sockint::hostent-addresses h) i) + until (sb-alien:null-alien ad) + collect (ecase (sockint::hostent-type h) + (#.sockint::af-inet + (assert (= length 4)) + (let ((addr (make-array 4 :element-type '(unsigned-byte 8)))) + (loop for i from 0 below length + do (setf (elt addr i) (sb-alien:deref ad i))) + addr)) + #-win32 + (#.sockint::af-local + (sb-alien:cast ad sb-alien:c-string)))))) + (make-instance 'host-ent + :name (sockint::hostent-name h) + :type (sockint::hostent-type h) + :aliases aliases + :addresses addresses))) + (defun get-host-by-name (host-name) "Returns a HOST-ENT instance for HOST-NAME or throws some kind of condition. HOST-NAME may also be an IP address in dotted quad notation or some other weird stuff - see gethostbyname(3) for grisly details." - (let ((h (sockint::gethostbyname host-name))) - (make-host-ent h))) + (make-host-ent (sockint::gethostbyname host-name))) (defun get-host-by-address (address) "Returns a HOST-ENT instance for ADDRESS, which should be a vector of -(integer 0 255), or throws some kind of error. See gethostbyaddr(3) for + (integer 0 255), or throws some kind of error. See gethostbyaddr(3) for grisly details." - (let ((packed-addr (sockint::allocate-in-addr))) - (loop for i from 0 to 3 - do (setf (sockint::in-addr-addr packed-addr i) (elt address i))) - (make-host-ent - (sb-sys:without-gcing - (sockint::gethostbyaddr (sockint::array-data-address packed-addr) - 4 - sockint::af-inet))))) - -(defun make-host-ent (h) - (if (sockint::foreign-nullp h) (name-service-error "gethostbyname")) - (let* ((local-h (sockint::foreign-vector h 1 sockint::size-of-hostent)) - (length (sockint::hostent-length local-h)) - (aliases - (loop for i = 0 then (1+ i) - for al = (sb-sys:sap-ref-sap - (sb-sys:int-sap (sockint::hostent-aliases local-h)) - (* i 4)) - until (= (sb-sys:sap-int al) 0) - collect (sb-c-call::%naturalize-c-string al))) - (address0 (sb-sys:sap-ref-sap (sb-sys:int-sap (sockint::hostent-addresses local-h)) 0)) - (addresses - (loop for i = 0 then (+ length i) - for ad = (sb-sys:sap-ref-32 address0 i) - while (> ad 0) - collect - (sockint::foreign-vector (sb-sys:sap+ address0 i) 1 length)))) - (make-instance 'host-ent - :name (sb-c-call::%naturalize-c-string - (sb-sys:int-sap (sockint::hostent-name local-h))) - :type (sockint::hostent-type local-h) - :aliases aliases - :addresses addresses))) + (sockint::with-in-addr packed-addr () + (let ((addr-vector (coerce address 'vector))) + (loop for i from 0 below (length addr-vector) + do (setf (sb-alien:deref (sockint::in-addr-addr packed-addr) i) + (elt addr-vector i))) + (make-host-ent (sockint::gethostbyaddr packed-addr + 4 + sockint::af-inet))))) ;;; The remainder is my fault - gw @@ -77,28 +76,32 @@ grisly details." GET-NAME-SERVICE-ERRNO") (defun name-service-error (where) + ;; FIXME: Our Texinfo documentation extracter need at least his to spit + ;; out the signature. Real documentation would be better... + "" (get-name-service-errno) ;; Comment next to NETDB_INTERNAL in netdb.h says "See errno.". ;; This special case treatment hasn't actually been tested yet. + #-win32 (if (= *name-service-errno* sockint::NETDB-INTERNAL) (socket-error where) (let ((condition - (condition-for-name-service-errno *name-service-errno*))) + (condition-for-name-service-errno *name-service-errno*))) (error condition :errno *name-service-errno* :syscall where)))) (define-condition name-service-error (condition) ((errno :initform nil - :initarg :errno - :reader name-service-error-errno) + :initarg :errno + :reader name-service-error-errno) (symbol :initform nil :initarg :symbol :reader name-service-error-symbol) (syscall :initform "an unknown location" :initarg :syscall :reader name-service-error-syscall)) (:report (lambda (c s) - (let ((num (name-service-error-errno c))) - (format s "Name service error in \"~A\": ~A (~A)" - (name-service-error-syscall c) - (or (name-service-error-symbol c) - (name-service-error-errno c)) - (get-name-service-error-message num)))))) + (let ((num (name-service-error-errno c))) + (format s "Name service error in \"~A\": ~A (~A)" + (name-service-error-syscall c) + (or (name-service-error-symbol c) + (name-service-error-errno c)) + (get-name-service-error-message num)))))) (defmacro define-name-service-condition (symbol name) `(progn @@ -108,7 +111,9 @@ GET-NAME-SERVICE-ERRNO") (defparameter *conditions-for-name-service-errno* nil) +#-win32 (define-name-service-condition sockint::NETDB-INTERNAL netdb-internal-error) +#-win32 (define-name-service-condition sockint::NETDB-SUCCESS netdb-success-error) (define-name-service-condition sockint::HOST-NOT-FOUND host-not-found-error) (define-name-service-condition sockint::TRY-AGAIN try-again-error) @@ -121,16 +126,17 @@ GET-NAME-SERVICE-ERRNO") (or (cdr (assoc err *conditions-for-name-service-errno* :test #'eql)) 'name-service)) - - (defun get-name-service-errno () (setf *name-service-errno* - (sb-alien:alien-funcall - (sb-alien:extern-alien "get_h_errno" (function integer))))) + (sb-alien:alien-funcall + #-win32 + (sb-alien:extern-alien "get_h_errno" (function integer)) + #+win32 + (sb-alien:extern-alien "WSAGetLastError" (function integer))))) -#-solaris +#-(and cmu solaris) (progn - #+sbcl + #+(and sbcl (not win32)) (sb-alien:define-alien-routine "hstrerror" sb-c-call:c-string (errno integer)) @@ -142,3 +148,6 @@ GET-NAME-SERVICE-ERRNO") (hstrerror num)) ) +;;; placeholder for hstrerror on windows +#+(and sbcl win32) +(defun hstrerror () 0)