0.9.11.16:
[sbcl.git] / contrib / sb-bsd-sockets / name-service.lisp
1 (in-package :sb-bsd-sockets)
2
3 (defclass host-ent ()
4   ((name :initarg :name :accessor host-ent-name)
5    (aliases :initarg :aliases :accessor host-ent-aliases)
6    (address-type :initarg :type :accessor host-ent-address-type)
7                                         ; presently always AF_INET
8    (addresses :initarg :addresses :accessor host-ent-addresses))
9   ;; FIXME: Our Texinfo documentation extracter need at least his to spit
10   ;; out the signature. Real documentation would be better...
11   (:documentation ""))
12
13 (defgeneric host-ent-address (host-ent)
14   ;; FIXME: Our Texinfo documentation extracter need at least his to spit
15   ;; out the signature. Real documentation would be better...
16   (:documentation ""))
17
18 (defmethod host-ent-address ((host-ent host-ent))
19   (car (host-ent-addresses host-ent)))
20
21 ;(define-condition host-not-found-error (socket-error)) ; host unknown
22 ;(define-condition no-address-error (socket-error)) ; valid name but no IP address
23 ;(define-condition no-recovery-error (socket-error)) ; name server error
24 ;(define-condition try-again-error (socket-error)) ; temporary
25
26 (defun make-host-ent (h)
27   (if (sb-alien:null-alien h) (name-service-error "gethostbyname"))
28   (let* ((length (sockint::hostent-length h))
29          (aliases (loop for i = 0 then (1+ i)
30                         for al = (sb-alien:deref (sockint::hostent-aliases h) i)
31                         while al
32                         collect al))
33          (addresses
34           (loop for i = 0 then (1+ i)
35                 for ad = (sb-alien:deref (sockint::hostent-addresses h) i)
36                 until (sb-alien:null-alien ad)
37                 collect (ecase (sockint::hostent-type h)
38                           (#.sockint::af-inet
39                              (assert (= length 4))
40                              (let ((addr (make-array 4 :element-type '(unsigned-byte 8))))
41                                (loop for i from 0 below length
42                                      do (setf (elt addr i) (sb-alien:deref ad i)))
43                                addr))
44                           #-win32
45                           (#.sockint::af-local
46                            (sb-alien:cast ad sb-alien:c-string))))))
47     (make-instance 'host-ent
48                    :name (sockint::hostent-name h)
49                    :type (sockint::hostent-type h)
50                    :aliases aliases
51                    :addresses addresses)))
52
53 (defun get-host-by-name (host-name)
54   "Returns a HOST-ENT instance for HOST-NAME or throws some kind of condition.
55 HOST-NAME may also be an IP address in dotted quad notation or some other
56 weird stuff - see gethostbyname(3) for grisly details."
57   (make-host-ent (sockint::gethostbyname host-name)))
58
59 (defun get-host-by-address (address)
60   "Returns a HOST-ENT instance for ADDRESS, which should be a vector of
61  (integer 0 255), or throws some kind of error.  See gethostbyaddr(3) for
62 grisly details."
63   (sockint::with-in-addr packed-addr ()
64     (let ((addr-vector (coerce address 'vector)))
65       (loop for i from 0 below (length addr-vector)
66             do (setf (sb-alien:deref (sockint::in-addr-addr packed-addr) i)
67                      (elt addr-vector i)))
68       (make-host-ent (sockint::gethostbyaddr packed-addr
69                                              4
70                                              sockint::af-inet)))))
71
72 ;;; The remainder is my fault - gw
73
74 (defvar *name-service-errno* 0
75   "The value of h_errno, after it's been fetched from Unix-land by calling
76 GET-NAME-SERVICE-ERRNO")
77
78 (defun name-service-error (where)
79   ;; FIXME: Our Texinfo documentation extracter need at least his to spit
80   ;; out the signature. Real documentation would be better...
81   ""
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   #-win32
86   (if (= *name-service-errno* sockint::NETDB-INTERNAL)
87       (socket-error where)
88     (let ((condition
89            (condition-for-name-service-errno *name-service-errno*)))
90       (error condition :errno *name-service-errno* :syscall where))))
91
92 (define-condition name-service-error (condition)
93   ((errno :initform nil
94           :initarg :errno
95           :reader name-service-error-errno)
96    (symbol :initform nil :initarg :symbol :reader name-service-error-symbol)
97    (syscall :initform "an unknown location" :initarg :syscall :reader name-service-error-syscall))
98   (:report (lambda (c s)
99              (let ((num (name-service-error-errno c)))
100                (format s "Name service error in \"~A\": ~A (~A)"
101                        (name-service-error-syscall c)
102                        (or (name-service-error-symbol c)
103                            (name-service-error-errno c))
104                        (get-name-service-error-message num))))))
105
106 (defmacro define-name-service-condition (symbol name)
107   `(progn
108      (define-condition ,name (name-service-error)
109        ((symbol :reader name-service-error-symbol :initform (quote ,symbol))))
110      (push (cons ,symbol (quote ,name)) *conditions-for-name-service-errno*)))
111
112 (defparameter *conditions-for-name-service-errno* nil)
113
114 #-win32
115 (define-name-service-condition sockint::NETDB-INTERNAL netdb-internal-error)
116 #-win32
117 (define-name-service-condition sockint::NETDB-SUCCESS netdb-success-error)
118 (define-name-service-condition sockint::HOST-NOT-FOUND host-not-found-error)
119 (define-name-service-condition sockint::TRY-AGAIN try-again-error)
120 (define-name-service-condition sockint::NO-RECOVERY no-recovery-error)
121 ;; this is the same as the next one
122 ;;(define-name-service-condition sockint::NO-DATA no-data-error)
123 (define-name-service-condition sockint::NO-ADDRESS no-address-error)
124
125 (defun condition-for-name-service-errno (err)
126   (or (cdr (assoc err *conditions-for-name-service-errno* :test #'eql))
127       'name-service))
128
129 (defun get-name-service-errno ()
130   (setf *name-service-errno*
131         (sb-alien:alien-funcall
132          #-win32
133          (sb-alien:extern-alien "get_h_errno" (function integer))
134          #+win32
135          (sb-alien:extern-alien "WSAGetLastError" (function integer)))))
136
137 #-(and cmu solaris)
138 (progn
139   #+(and sbcl (not win32))
140   (sb-alien:define-alien-routine "hstrerror"
141       sb-c-call:c-string
142     (errno integer))
143   #+cmu
144   (alien:def-alien-routine "hstrerror"
145       sb-c-call:c-string
146     (errno integer))
147   (defun get-name-service-error-message (num)
148   (hstrerror num))
149 )
150
151 ;;; placeholder for hstrerror on windows
152 #+(and sbcl win32)
153 (defun hstrerror () 0)