|Allegro CL version 9.0|
Significantly revised from 8.2.
This document contains the following sections:1.0 Introduction and background
Sockets are a mechanism for interprocess communication designed at U.C. Berkeley for use in their version of Unix. Sockets have been added to many other versions of Unix and there is an implementation of sockets for Windows called Winsock. This document describes the Allegro interface to sockets. This interface works on Unix and on Windows.
Symbols naming objects in the socket utility are in the
acl-socket package. It has the nickname
The socket module is not included in all versions of Allegro CL. If it is present, it is (by default) included in a development image (one built with the include-devel-env argument to build-lisp-image specified true). To load the socket module if it is not present in an image, evaluate
Note that runtime images cannot include the development environment
(so include-devel-env must be specified
nil when a runtime image is being built). If the
socket module is needed, it must be loaded when the image is
built. See runtime.htm,
delivery.htm for more information.
Allegro CL supports Internet Protocol version 6 sockets (IPv6
sockets). As part of this support, several new functions have been
added and several functions have been modified. The new functions are
ipv6, get-ip-interfaces, ipaddrp, ipaddr-equalp, ipv6-address-p, and (added in a June,
s006 update) dotted-address-p. The modified functions are
dns-query, lookup-hostname, make-socket, and send-to. There is also a new
:ipv6 is added
*features* list to indicate IPv6 support.
Because of apparent bugs in Mac OS X 64-bit, certain IPv6 functionality may be restricted or unusable. In particular:
Throughout the socket documentation, we make use of the term IP address. But what exactly is an IP address? Unless further clarified in the context in which it is used, an IP address is either an unsigned 32-bit integer or an ipv6 address structure.
The function socket:ipaddrp returns true when passed an IP adress. That function can be used to identify an object as an IP address. (Unsigned 32 bit integers obviously have other uses that representing IP addresses. The function simply determines whether the type and form of its argument is suitable as an IP address.)
There are three independent characteristics of sockets:
In order to send to another socket the socket must have a name.
On Unix, port numbers less than 1024 can only be allocated by a
process with the user id of root. A
Note that the current version of the socket interface on Windows
(Winsock, version 1.1), does not support the
This isn't a property of the Unix
socket implementation but is instead something we've added for the Common Lisp
implementation since a Lisp stream is either binary (supports
Note on bivalent format:
Starting in release 5.0.1, the bivalent format is accepted for stream sockets. Bivalent means that the stream will accept text and binary stream functions. That is, you can write-byte or write-char, read-byte or read-char. A bivalent stream is useful in the http protocol (used between web browsers and web servers) since in that protocol the header data is sent in text format and the body can be in binary data (image files, for example).
Internally a bivalent socket stream is configured like a binary socket stream with 8 bit bytes. Character position is not maintained.
Bivalent socket streams have very efficient read-sequence and
write-sequence implementations (as long as the sequence is either a
vector of element-type
Bivalent socket streams also support the chunking protocol found in http/1.1. This protocol allows the sender to signal end of file without closing down the stream.
Stream sockets have a fourth characteristic called connect,
with a value
:passive. In order to use stream sockets you have
to set up a link between two of them. That link is called a
connection. You set up a connection in this way:
(setq s-a (make-socket :connect :passive :local-port port-b))
(setq s-b (make-socket :remote-host "machine-a" :remote-port port-b))
(setq str-a (accept-connection s-a))
s-bto send messages to machine A.
Note that steps 2 and 3 can occur in either order.
Note the asymmetry: a passive socket is not a Lisp stream (you can't do read and write to it). An active socket is a Lisp stream.
accept-connection is called on a
passive socket, it does not return until a connection is made to the
passive socket. The value
accept-connection returns is a
As long as the passive socket is not closed, new connections can still be made to the port of that socket.
An active socket can be used for only one connection. Once that connection has been made, the socket should be closed and a new active socket created.
Host naming conventions: this package supports three conventions for naming a host:
|hostname||A string using the domain naming convention, e.g. "ftp.franz.com". The domain naming system is case-insensitive.|
A string which is the printed representation of the numeric address:
|ipaddr||An unsigned 32-bit number, representing the IPv4 address in the native byte order for the host. Thus 184.108.40.206 is 192*2^24 + 132*2^16 + 95*2^8 + 84 = 3229900628.|
|IPv6||An IPv6 address structure.|
The variables defined by the interface are:
Please provide the value of this variable when asking for technical support with sockets as it tells us whether you have the latest version.
This variable controls whether the socket printing code converts the ip address of a socket into a hostname. This is usually what you want, however this can be a slow process (taking up to a minute to accomplish). The default value for this variable is
t. See the full description for a discussion of the causes of the possible slowdown when the value is
Specifies the default value of the ipv6 keyword argument to lookup-hostname and make-socket.
The first table shows general functions defined by the interface and the second shows accessors.
|Function||Arguments||Notes (follow function link for full description)|
||Generic function. Establishes a connection. If wait is
||Function. Converts a string like "220.127.116.11" or
similar format to an unsigned 32-bit IP address.
IPv6 "colon hex" address notation, including the %scopeid extension is also supported as is IPv4-mapped IPv6 address notation (::ffff:w.x.y.z).
||Function. Returns true if its argument is a string in dotted IP address form.|
|get-ip-interfaces||Function. Returns a list of conses of interface id's and names.|
||Function. Convert a 32-bit unsigned IP address,
||Function. Returns true if its two internet address arguments match.|
|ipaddr-to-hostname||ipaddr||Function. Returns, as a string, the hostname of the machine with address ipaddr. ipaddr should be a 32-bit IP address or an IPv6 address structure or IPv6 colon hex strings.|
||Function. Returns true if its argument is an IP address.|
||Generic function. Returns true if its argument is an IPv6 socket.|
||Function. Returns true if its argument is an IPv6 address structure.|
|lookup-hostname||hostname||Given a string naming a host, a 32-bit IP address, a string in dotted form, or a IPv6 address structure or IPv6 colon hex strings, return the 32-bit IP address for the host.|
||Function. Finds the port number using the symbolic name and the protocol.|
||Function. See the full description for details.|
||Macro. See the full description for details.|
||Generic function. This is used to read from a datagram socket.|
||Generic function with methods for internet-datagram-sockets and file-datagram-sockets|
||Generic function for modifying existing sockets.|
||Generic function that closes down the specified half of the bidirectional socket connection.|
||This function modifies the state of the socket stream, controlling input and output chunking.|
||Generic function. Return the operating system file descriptor associated with this socket.|
These functions retrieve slot values from socket instances. The values of these slots are set when the socket is created.
|Function||Arguments||Notes (follow function link for full description)|
||Generic function. Returns an IP address.|
||Generic function. Returns an IP address.|
All are generic functions. All return the values of the particular attribute for socket.
Note: Both internet stream and internet datagram sockets use 16-bit port numbers.
Note that stream (tcp) port N is totally distinct from datagram (udp) port N.
When errors are raised by the socket interface, Lisp conditions are signaled. This section describes those conditions.
condition is a CLOS class and thus fits into
the hierarchy of CLOS classes. The condition
socket-error is a subclass of the condition
socket-error is the
superclass for all socket related errors.
See More on
cl:stream-error in errors.htm.
socket-error denotes operating system
detected socket errors. It has the following slots:
||stream-error-identifier||Symbol denoting this error (see table below)|
||stream-error-code||Operating system dependent error code (if any)|
||stream-error-action||String describing the operation in progress when the error occurred|
Handling socket error is difficult because the error returned in exceptional situations can depend on the operating system and the address of the other side of the connection. For example, attempting to make a connection to a machine that is down may result in a "Connection Timed Out" or a "Host Unreachable" error, or maybe something else on certain systems.
The error codes assigned to socket errors vary from operating
system to operating system. We translate a large set of the common
error codes from a machine dependent number to a symbol which we call
identifier to make it easier for you to
write portable code. Condition handling code should check the
identifier field (using stream-error-identifier) If the
identifier value is
:unknown then this is not a
common socket error and the operating system dependent code value of
the condition must be used.
identifier values and their meanings:
||Local socket address already in use|
||Local socket address not available|
||Network is down|
||Network has been reset|
||Connection reset by peer|
||No buffer space|
||Connection shut down|
||Connection timed out|
||Host is down|
||Host is unreachable|
||Protocol not available|
|Create an active stream socket connection to a socket that just prints characters to
whomever connects to it. After connecting, read the first five characters and print them
USER(1): (let ((s (make-socket :remote-host "vapor" :remote-port "chargen"))) (dotimes (i 5) (print (read-char s))) (close s)) #\space #\! #\" #\# #\$
Sending a message from frisky to vapor:
USER(1): (print (read (accept-connection (make-socket :connect :passive :local-port 9933)))) .. this hangs ...
USER(1): (let ((s (make-socket :remote-host "vapor" :remote-port 9933))) (format s "Secret-message~%") (close s))
Then you see on vapor:
Secret-message Secret-message USER(2):
A flaw in this example is that on vapor we've left the socket and the stream open and we lost track of the objects to close them. So, while concise, this is not a good programming style.
Another problem with this example is that when we created the port on vapor we used a
specific port number (9933). This means our program will fail if port 9933 is already in
use. If possible, it is best to let the system choose a port number (this is done by not
specifying a :
If we just want to send a simple message then datagrams might be more appropriate (although the program must guarantee that the message made it because datagram communication is unreliable).
user(2): (setq s (make-socket :type :datagram :local-port 9999)) #<text datagram socket waiting for connection at */9999 @ #x20664e82> user(3):
user(10): (setq x (make-socket :type :datagram)) #<text datagram socket waiting for connection at */45602 @ #x20717fb2> user(11): (send-to x "foo-the-bar" 11 :remote-host "vapor" :remote-port 9999) 11 user(12):
user(3): (receive-from s 100 :extract t)
Allegro CL supports Secure Socket layers as described in this section. See also aserve/aserve.html, which describes Webserver support in Allegro CL.
Libraries for SSL are supplied with the distribution on all platforms
where SSL is supported.
:ssl-support is included on
*features* list on
all platforms that provide SSL support. Except on Windows, no special
installation is required. See Installing OpenSSL libraries on
Windows in installation.htm for
information on Windows installation.
The SSL verion number is part of the installation executable on Windows and specified in the file [Allegro directory]/OpenSSL_version.txt on UNIX platforms.
As required under its license, the source code for OpenSSL libraries is provided in the distribution in the openssl/ subdirectory. The sources are not needed to use the SSL facility.
The SSL functionality is in the ssl module. To
ensure it is loaded, evaluate
:ssl). Calling either of the two SSL functions, make-ssl-client-stream and
loads that module. But note if you are including the SSL facility in an
application intended for delivery, be sure to include the module by
adding the keyword
:ssl to the list which is the
value of the input-file to generate-application.
In 1994 Netscape Corporation designed the Secure Socket Layer (SSL) protocol to provide a means of safely and securely transporting private data (such as credit card numbers) between a Web Browser and a Web Server. Rather than tie SSL to the http protocol, Netscape wrote it as a protocol for making any TCP/IP connection secure.
At the end of 1994 version 2 of SSL was introduced and this was the first version shipped with a commercial web browser (Netscape Navigator (r)). In 1995 version 3 of SSL was introduced. At that point an international standards organization (IETF) took over work on SSL and introduced Transport Layer Security (or TLS) protocol (which is based on SSL but has a different handshake protocol). The IETF introduced TLS version 1.0 in 1999.
Allegro CL, starting in release 6.0, provides an interface that supports SSL version 2, SSL version 3 and TLS version 1. When we use the name SSL, we mean SSL or TLS.
A secure TCP connection exists between two processes when both agree on the following:
These three items are determined via negotiation when the connection is made and the first data is to be sent.
In an SSL connection, one side is the client and the other side is the server. In the http environment, the web browser is the client and the web server is the server.
When a secure connection is started, the client starts the negotation by telling the server all the possible ways that it can communicate securely. The server then chooses one of the possible ways and informs the client.
Then the server sends its certificate and possibly other certificates if they are needed to prove that its certificate can be trusted. The important item in the certificate is the public key for the server. The client will use this public key to encrypt a random value which will be used by both the client and server to create the keys needed for the cipher chosen for data transmission.
In theory a certificate isn't necessary if both the client and server side support a key exchange algorithm that can generate a public key on the fly. The SSL libraries we use do not have this capability, thus you must always supply a server certificate.
Once both sides know the keys the other side will use to transmit, the secure data transmission can occur.
The SSL protocol also permits each side of the connection to declare who they are. This is done by the exchange of certificates. The server must send a certificate describing itself to the client. The server can request that the client send a certificate to the server (although in the use of SSL on the web this is never done).
A certificate is a digital document that stores information about an entity in such a way that it can be verified to be true. The primary use of certificates is to store the public key that can be used to send encrypted messages to the entity.
In the SSL protocol certificates have two uses:
Strictly speaking a certificate isn't required for SSL communication if both sides support a certain key exchange protocol. The OpenSSL libraries we use do not support this protocol thus whenever you create a server SSL stream you must supply a certificate (if you don't have your own we supply one in <Allegro directory>/examples/ssl/server.pem that you can use).
While certificates support authentication, the SSL protocol doesn't require that you take advantage of this facility.
A certificate contains the following:
A certificate is a combination of text and binary data and in order to make it easy to transport certificates they are usually encoded in a form called PEM which turns them into a sequence of printable characters.
When a web browser connects to a site via SSL (which is caused by the use of the 'https:' at the beginning of the url), it checks three things about the certificate:
https://www.foo.com/whateverthen the certificate must be for
www.foo.com. The convention used is to store the name of the server machine in the CommonName slot of the Subject Identifier field of the certificate.
If all three tests pass then the web browser silently accepts the certificate and does a secure web page access. If any of the tests fail then the web browser notifies the user and waits for a response. Each browser displays the failure differently. For example, the Microsoft Internet Explorer (r) shows which of the three tests passed and which failed while the Netscape Navigator (r) just says that it received an invalid certificate. In both cases the person using the web browser is given the option of continuing with the web access. Transmission will still be secure if it is elected to continue. The only issue in doubt is the authenticity of the web server.
The SSL implementation includes certificate revocation list (CRL) support. CRL checking is controlled by the crl-check and crl-file keyword arguments to make-ssl-client-stream and make-ssl-server-stream.
If you enable CRL checking, you must supply a proper PEM-encoded CRL, even if it contains zero revocations. If you do not supply a CRL, peer verification will never succeed.
The following operators and one class comprise the SSL API. make-ssl-client-stream and make-ssl-server-stream create the streams that are used for communication.
The file <Allegro directory>/examples/ssl/server.pem is a sample certificate and private key file. You can use this file when starting the server side of an SSL connection. The AllegroServe facility uses SSL. It is described in aserve/aserve.html.
Copyright (c) 1998-2012, Franz Inc. Oakland, CA., USA. All rights reserved.
This page has had significant revisions compared to the 8.2 page.
|Allegro CL version 9.0|
Significantly revised from 8.2.