Subject: Re: Can I connect to a local socket?
From: rpw3@rpw3.org (Rob Warnock)
Date: Tue, 16 Sep 2003 07:19:40 -0500
Newsgroups: comp.lang.lisp
Message-ID: <c2ydnXpA2bpBYvuiXTWc-w@speakeasy.net>
Charlie Burrows  <charlie.burrows@riskdecisions.com> wrote:
+---------------
| Thank-you. I'm using CLISP, it's (socket:socket-connect port) must have
| a port number. I can't see anything about local sockets in the CLISP
| documentation but maybe I have just missed it. I guess I'm going to have
| to use internet ports as I'm set on using clisp and I don't want to get
| into FFI at this stage.
+---------------

Well, before giving up on local sockets completely you may want to
take a look at the CLX code for CLISP, or should I say, the code in
"${CLISP-2.29}/src/socket.d" (which turns into a ".c" during the build).
It contains code to do an AF_UNIX connect, but *only* to the X server
[look at what routine "connect_to_x_server(host,display)" does when
fed a host of "unix"].

Unfortunately, they didn't expose this to ordinary mortals; that
particular routine has the paths for X local sockets hard-coded
into it. But you should be able to clone the code easily enough
and pass an arbitrary local socket path to the clone.

Hmmm... Or, if are lucky enough to be able to pick the local socket path
yourself, then just use the "connect_to_x_server()" routine as is, and
call it with a very high number (much higher than the number of X displays
on on your system -- but be careful, SSH starts with #10 and up for its
tunneled X connections!). For example, if you call:

	connect_to_x_server("unix", 100)

it would try to connect to "/tmp/.X11-unix/X100". You can in fact do this
[I just tried it!] by calling:

	(system::make-socket-stream "unix" 100)

Caveat: You get back an #<IO UNBUFFERED X11-SOCKET-STREAM ("unix" 100)>,
which cannot be written on by WRITE-CHAR or WRITE-LINE or WRITE-SEQUENCE --
you have to use WRITE-BYTE. But using a loop wrapped around this this:

	(defun test-write-line (line s)
	  (loop for i across line do
	    (write-byte (char-code i) s))
	    (write-byte 13 s)
	    (write-byte 10 s)
	    (force-output s)
	    line)

I was able to push an HTTP request to a "mod_lisp"-like server.

CAVEAT#2: I couldn't figure out any way to call "shutdown(sock, SHUT_WR)"
on the stream, so protocols that depend on the other end seeing an EOF
from you before they reply [such as the above-mentioned "mod_lisp"-like
server!] may be a bit hard to use...   :-(

Good luck!


-Rob

-----
Rob Warnock, PP-ASEL-IA		<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607