1 <html><head><title>db-sockets API Reference</title></head><body>
2 <h1>Package SOCKETS</h1>
5 A thinly-disguised BSD socket API for SBCL. Ideas stolen from the BSD
6 socket API for C and Graham Barr's IO::Socket classes for Perl.
8 We represent sockets as CLOS objects, and rename a lot of methods and
9 arguments to fit Lisp style more closely.
17 <li> Methods applicable to all <a href="#socket">sockets</a>
18 <li> <a href="#sockopt">Socket Options</a>
19 <li> Methods applicable to a particular subclass
21 <li> <a href="#internet">INET-SOCKET</a> - Internet Protocol (TCP, UDP, raw) sockets
22 <li> Methods on <a href="#UNIX-SOCKET">UNIX-SOCKET</a> - Unix-domain sockets
24 <li> <a href="#name-service">Name resolution</a> (DNS, /etc/hosts, &c)
27 <h2>General concepts</h2>
29 <p>Most of the functions are modelled on the BSD socket API. BSD sockets
30 are widely supported, portably <i>(well, fairly portably)</i>
31 available on a variety of systems, and documented. There are some
32 differences in approach where we have taken advantage of some of the more useful features of Common Lisp - briefly
35 <li> Where the C API would typically return -1 and set errno, db-sockets
36 signals an error. All the errors are subclasses of SOCKET-CONDITION
37 and generally correspond one for one with possible <tt>errno</tt> values
39 <li> We use multiple return values in many places where the C API would use p[ass-by-reference values
41 <li> We can often avoid supplying an explicit <i>length</i> argument to
42 functions because we already know how long the argument is.
44 <li> IP addresses and ports are represented in slightly friendlier fashion
45 than "network-endian integers". See the section on <a href="#internet"
46 >Internet domain</a> sockets for details.
52 <p><a name="SOCKET"><i>Class: </i><b>SOCKET</b></a>
53 <p><b>Slots:</b><ul><li>FILE-DESCRIPTOR : </li>
58 </ul><p><a name="SOCKET-BIND"><table width="100%"><tr><td width="80%">(socket-bind <i> (s <a href="#socket">socket</a>) &rest address</i>)</td><td align=right>Generic Function</td></tr></table>
59 <p><a name="SOCKET-ACCEPT"><table width="100%"><tr><td width="80%">(socket-accept <i> (socket <a href="#socket">socket</a>)</i>)</td><td align=right>Method</td></tr></table>
60 <blockquote>Perform the accept(2) call, returning a newly-created connected socket
61 and the peer address as multiple values</blockquote>
62 <p><a name="SOCKET-CONNECT"><table width="100%"><tr><td width="80%">(socket-connect <i> (s <a href="#socket">socket</a>) &rest address</i>)</td><td align=right>Generic Function</td></tr></table>
63 <p><a name="SOCKET-PEERNAME"><table width="100%"><tr><td width="80%">(socket-peername <i> (socket <a href="#socket">socket</a>)</i>)</td><td align=right>Method</td></tr></table>
64 <blockquote>Return the socket's peer; depending on the address family this may return multiple values</blockquote>
65 <p><a name="SOCKET-NAME"><table width="100%"><tr><td width="80%">(socket-name <i> (socket <a href="#socket">socket</a>)</i>)</td><td align=right>Method</td></tr></table>
66 <blockquote>Return the address (as vector of bytes) and port that the socket is bound to, as multiple values</blockquote>
67 <p><a name="SOCKET-RECEIVE"><table width="100%"><tr><td width="80%">(socket-receive <i> (socket <a href="#socket">socket</a>) buffer length &key oob peek waitall (element-type
68 'character)</i>)</td><td align=right>Method</td></tr></table>
69 <blockquote>Read LENGTH octets from <a href="#SOCKET">SOCKET</a> into BUFFER (or a freshly-consed buffer if
70 NIL), using recvfrom(2). If LENGTH is NIL, the length of BUFFER is
71 used, so at least one of these two arguments must be non-NIL. If
72 BUFFER is supplied, it had better be of an element type one octet wide.
73 Returns the buffer, its length, and the address of the peer
74 that sent it, as multiple values. On datagram sockets, sets MSG_TRUNC
75 so that the actual packet length is returned even if the buffer was too
77 <p><a name="SOCKET-LISTEN"><table width="100%"><tr><td width="80%">(socket-listen <i> (socket <a href="#socket">socket</a>) backlog</i>)</td><td align=right>Method</td></tr></table>
78 <blockquote>Mark <a href="#SOCKET">SOCKET</a> as willing to accept incoming connections. BACKLOG
79 defines the maximum length that the queue of pending connections may
80 grow to before new connection attempts are refused. See also listen(2)</blockquote>
81 <p><a name="SOCKET-CLOSE"><table width="100%"><tr><td width="80%">(socket-close <i> (socket <a href="#socket">socket</a>)</i>)</td><td align=right>Method</td></tr></table>
82 <blockquote>Close <a href="#SOCKET">SOCKET</a>. May throw any kind of error that write(2) would have
83 thrown. If <a href="#SOCKET-MAKE-STREAM">SOCKET-MAKE-STREAM</a> has been called, calls CLOSE on that
84 stream instead</blockquote>
85 <p><a name="SOCKET-MAKE-STREAM"><table width="100%"><tr><td width="80%">(socket-make-stream <i> (socket <a href="#socket">socket</a>) &rest args</i>)</td><td align=right>Method</td></tr></table>
86 <blockquote>Find or create a STREAM that can be used for IO on <a href="#SOCKET">SOCKET</a> (which
87 must be connected). ARGS are passed onto SB-SYS:MAKE-FD-STREAM.</blockquote>
89 <H2> Socket Options </h2>
90 <a name="sockopt"> </a>
91 <p> A subset of socket options are supported, using a fairly
92 general framework which should make it simple to add more as required
93 - see sockopt.lisp for details. The name mapping from C is fairly
94 straightforward: <tt>SO_RCVLOWAT</tt> becomes
95 <tt>sockopt-receive-low-water</tt> and <tt>(setf
96 sockopt-receive-low-water)</tt>.
97 |<p><a name="SOCKOPT-REUSE-ADDRESS"><table width="100%"><tr><td width="80%">(sockopt-reuse-address <i> (socket <a href="#socket">socket</a>) argument</i>)</td><td align=right>Accessor</td></tr></table>
98 <blockquote>Return the value of the SO-REUSEADDR socket option for <a href="#SOCKET">SOCKET</a>. This can also be updated with SETF.</blockquote>
99 <p><a name="SOCKOPT-KEEP-ALIVE"><table width="100%"><tr><td width="80%">(sockopt-keep-alive <i> (socket <a href="#socket">socket</a>) argument</i>)</td><td align=right>Accessor</td></tr></table>
100 <blockquote>Return the value of the SO-KEEPALIVE socket option for <a href="#SOCKET">SOCKET</a>. This can also be updated with SETF.</blockquote>
101 <p><a name="SOCKOPT-OOB-INLINE"><table width="100%"><tr><td width="80%">(sockopt-oob-inline <i> (socket <a href="#socket">socket</a>) argument</i>)</td><td align=right>Accessor</td></tr></table>
102 <blockquote>Return the value of the SO-OOBINLINE socket option for <a href="#SOCKET">SOCKET</a>. This can also be updated with SETF.</blockquote>
103 <p><a name="SOCKOPT-BSD-COMPATIBLE"><table width="100%"><tr><td width="80%">(sockopt-bsd-compatible <i> (socket <a href="#socket">socket</a>) argument</i>)</td><td align=right>Accessor</td></tr></table>
104 <blockquote>Return the value of the SO-BSDCOMPAT socket option for <a href="#SOCKET">SOCKET</a>. This can also be updated with SETF.</blockquote>
105 <p><a name="SOCKOPT-PASS-CREDENTIALS"><table width="100%"><tr><td width="80%">(sockopt-pass-credentials <i> (socket <a href="#socket">socket</a>) argument</i>)</td><td align=right>Accessor</td></tr></table>
106 <blockquote>Return the value of the SO-PASSCRED socket option for <a href="#SOCKET">SOCKET</a>. This can also be updated with SETF.</blockquote>
107 <p><a name="SOCKOPT-DEBUG"><table width="100%"><tr><td width="80%">(sockopt-debug <i> (socket <a href="#socket">socket</a>) argument</i>)</td><td align=right>Accessor</td></tr></table>
108 <blockquote>Return the value of the SO-DEBUG socket option for <a href="#SOCKET">SOCKET</a>. This can also be updated with SETF.</blockquote>
109 <p><a name="SOCKOPT-DONT-ROUTE"><table width="100%"><tr><td width="80%">(sockopt-dont-route <i> (socket <a href="#socket">socket</a>) argument</i>)</td><td align=right>Accessor</td></tr></table>
110 <blockquote>Return the value of the SO-DONTROUTE socket option for <a href="#SOCKET">SOCKET</a>. This can also be updated with SETF.</blockquote>
111 <p><a name="SOCKOPT-BROADCAST"><table width="100%"><tr><td width="80%">(sockopt-broadcast <i> (socket <a href="#socket">socket</a>) argument</i>)</td><td align=right>Accessor</td></tr></table>
112 <blockquote>Return the value of the SO-BROADCAST socket option for <a href="#SOCKET">SOCKET</a>. This can also be updated with SETF.</blockquote>
113 <p><a name="SOCKOPT-TCP-NODELAY"><table width="100%"><tr><td width="80%">(sockopt-tcp-nodelay <i> (socket <a href="#socket">socket</a>) argument</i>)</td><td align=right>Accessor</td></tr></table>
114 <blockquote>Return the value of the TCP-NODELAY socket option for <a href="#SOCKET">SOCKET</a>. This can also be updated with SETF.</blockquote>
115 <hr> <h2>INET-domain sockets</h2>
117 <p>The TCP and UDP sockets that you know and love. Some representation issues:
119 <li>These functions do not accept hostnames directly: see <a href="#name-service">name resolution</a>
120 <li>Internet <b>addresses</b> are represented by vectors of <tt>(unsigned-byte 8)</tt> - viz. <tt>#(127 0 0 1)</tt>. <b>Ports</b> are just integers: <tt>6010</tt>. No conversion between network- and host-order data is needed from the user of this package.
121 <li><b><i>socket addresses</i></b> are represented by the two values for <b>address</b> and <b>port</b>, so for example, <tt>(<a href="#SOCKET-CONNECT">socket-connect</a> s #(192 168 1 1) 80)</tt>
124 <p><a name="INET-SOCKET"><i>Class: </i><b>INET-SOCKET</b></a>
125 <p><b>Slots:</b><ul><li>FAMILY : </li>
126 </ul><p><a name="MAKE-INET-ADDRESS"><table width="100%"><tr><td width="80%">(make-inet-address <i> dotted-quads</i>)</td><td align=right>Function</td></tr></table>
127 <blockquote>Return a vector of octets given a string DOTTED-QUADS in the format
128 "127.0.0.1"</blockquote>
129 <p><a name="GET-PROTOCOL-BY-NAME"><table width="100%"><tr><td width="80%">(get-protocol-by-name <i> name</i>)</td><td align=right>Function</td></tr></table>
130 <blockquote>Returns the network protocol number associated with the string NAME,
131 using getprotobyname(2) which typically looks in NIS or /etc/protocols</blockquote>
132 <p><a name="MAKE-INET-SOCKET"><table width="100%"><tr><td width="80%">(make-inet-socket <i> type protocol</i>)</td><td align=right>Function</td></tr></table>
133 <blockquote>Make an INET socket. Deprecated in favour of make-instance</blockquote>
134 <hr> <h2>File-domain sockets</h2>
136 File-domain (AF_FILE) sockets are also known as Unix-domain sockets, but were
137 renamed by POSIX presumably on the basis that they may be
138 available on other systems too.
140 A file-domain socket address is a string, which is used to create a node
141 in the local filesystem. This means of course that they cannot be used across
144 |<p><a name="UNIX-SOCKET"><i>Class: </i><b>UNIX-SOCKET</b></a>
145 <p><b>Slots:</b><ul><li>FAMILY : </li>
146 </ul><hr> <a name="name-service"><h2>Name Service</h2></a>
148 <p>Presently name service is implemented by calling whatever
149 gethostbyname(2) uses. This may be any or all of /etc/hosts, NIS, DNS,
150 or something completely different. Typically it's controlled by
153 <p> Direct links to the asynchronous resolver(3) routines would be nice to have
154 eventually, so that we can do DNS lookups in parallel with other things
155 <p><a name="HOST-ENT"><i>Class: </i><b>HOST-ENT</b></a>
156 <p><b>Slots:</b><ul><li>NAME : </li>
158 <li>ADDRESS-TYPE : </li>
159 <li>ADDRESSES : </li>
160 </ul><p><a name="HOST-ENT-ADDRESS"><table width="100%"><tr><td width="80%">(host-ent-address <i> (host-ent <a href="#host-ent">host-ent</a>)</i>)</td><td align=right>Method</td></tr></table>
161 <p><a name="GET-HOST-BY-NAME"><table width="100%"><tr><td width="80%">(get-host-by-name <i> host-name</i>)</td><td align=right>Function</td></tr></table>
162 <blockquote>Returns a <a href="#HOST-ENT">HOST-ENT</a> instance for HOST-NAME or throws some kind of condition.
163 HOST-NAME may also be an IP address in dotted quad notation or some other
164 weird stuff - see gethostbyname(3) for grisly details.</blockquote>
165 <p><a name="GET-HOST-BY-ADDRESS"><table width="100%"><tr><td width="80%">(get-host-by-address <i> address</i>)</td><td align=right>Function</td></tr></table>
166 <blockquote>Returns a <a href="#HOST-ENT">HOST-ENT</a> instance for ADDRESS, which should be a vector of
167 (integer 0 255), or throws some kind of error. See gethostbyaddr(3) for
168 grisly details.</blockquote>
169 <p><a name="NAME-SERVICE-ERROR"><table width="100%"><tr><td width="80%">(name-service-error <i> where</i>)</td><td align=right>Function</td></tr></table>
170 <hr><p><a name="NON-BLOCKING-MODE"><table width="100%"><tr><td width="80%">(non-blocking-mode <i> (socket <a href="#socket">socket</a>)</i>)</td><td align=right>Method</td></tr></table>
171 <blockquote>Is <a href="#SOCKET">SOCKET</a> in non-blocking mode?</blockquote>
176 There should be at least one test for pretty much everything you can do
177 with the package. In some places I've been more diligent than others; more
178 tests gratefully accepted.
180 Tests are in the file <tt>tests.lisp</tt> and also make good examples.
183 <h2>Unix-domain sockets</h2>
185 A fairly rudimentary test that connects to the syslog socket and sends a
186 message. Priority 7 is kern.debug; you'll probably want to look at
187 /etc/syslog.conf or local equivalent to find out where the message ended up