0.7.12.28
[sbcl.git] / contrib / sb-bsd-sockets / unix.lisp
1 (in-package :sb-bsd-sockets)
2
3 #|| <h2>File-domain sockets</h2>
4
5 File-domain (AF_FILE) sockets are also known as Unix-domain sockets, but were
6 renamed by POSIX presumably on the basis that they may be
7 available on other systems too.  
8
9 A file-domain socket address is a string, which is used to create a node
10 in the local filesystem.  This means of course that they cannot be used across
11 a network.
12
13 ||#
14
15 (defclass unix-socket (socket)
16   ((family :initform sockint::af-unix)))
17
18 (defmethod make-sockaddr-for ((socket unix-socket) &optional sockaddr &rest address &aux (filename (first address)))
19   (let ((sockaddr (or sockaddr (sockint::allocate-sockaddr-un))))
20     (setf (sockint::sockaddr-un-family sockaddr) sockint::af-unix)
21     (when filename
22       (loop for c across filename
23             ;; XXX magic constant ew ew ew.  should grovel this from
24             ;; system headers
25             for i from 0 to (min 107 (1- (length filename)))
26             do (setf (sockint::sockaddr-un-path sockaddr i) (char-code c))
27             finally
28             (setf (sockint::sockaddr-un-path sockaddr (1+ i)) 0)))
29     sockaddr))
30
31 (defmethod size-of-sockaddr ((socket unix-socket))
32   sockint::size-of-sockaddr-un)
33
34 (defmethod bits-of-sockaddr ((socket unix-socket) sockaddr)
35   "Returns filename of SOCKADDR"
36   (let ((name (sb-c-call::%naturalize-c-string
37                (sb-sys:sap+ (sockint::array-data-address sockaddr)
38                             sockint::offset-of-sockaddr-un-path))))
39     (if (zerop (length name)) nil name)))
40