« Previous  |  Next »

Unix Domain Sockets on Pharo

27 July 2013

A Unix domain socket "or IPC (inter-process communication) socket is a data communications endpoint for exchanging data between processes executing within the same host operating system."

The Squeak/Pharo VM's socket plugin has had support for Unix domain sockets, as per SocketPrims.pdf, written by Ian Piumarta, dated 2007, "The old API assumes 32-bit INET4 (host) addresses and numbered service (port) addresses throughought. The new API supports arbitray host address sizes and both numbered and symbolic host and service names. [...] SQ_SOCKET_FAMILY_LOCAL The lookup should only find local (some- times called "Unix") domain host addresses. In this case, the host name should be empty and the service name should be a path to a node in the filesystem associated with a named socket."

And in code, platforms/unix/plugins/SocketPlugin/sqUnixSocket.c:

sqInt sqResolverGetAddressInfoFamily(void)
{
  if (!addrInfo)
    {
      interpreterProxy->success(false);
      return 0;
    }

  switch (addrInfo->ai_family)
    {
    case AF_UNIX:	return SQ_SOCKET_FAMILY_LOCAL;
    case AF_INET:	return SQ_SOCKET_FAMILY_INET4;
    case AF_INET6:	return SQ_SOCKET_FAMILY_INET6;
    }

  return SQ_SOCKET_FAMILY_UNSPECIFIED;
}

On the Smalltalk side, the Socket class has no support though. The key part is in the following fragment in "Socket>>initialize":

socketHandle := self
  primSocketCreateNetwork: 0
  type: socketType
  receiveBufferSize: 8000
  sendBufSize: 8000
  semaIndex: semaIndex
  readSemaIndex: readSemaIndex
  writeSemaIndex: writeSemaIndex.

Socket>>primSocketCreateNetwork:type:receiveBufferSize:sendBufSize:semdIndex: comments, "Return a new socket handle for a socket of the given type and buffer sizes. Return nil if socket creation fails. The netType parameter is platform dependent and can be used to encode both the protocol type (IP, Xerox XNS, etc.) and/or the physical network interface to use if this host is connected to multiple networks. A zero netType means to use IP protocols and the primary (or only) network interface."

Meaning, this bit "primSocketCreateNetwork: 0" hardcodes the use of TCP/IP. Easy enough to fix; let's rename Socket>>initialize to Socket>>initialize:withDomain:, changing the key part:

initialize: socketType withDomain: socketDomain

    [...]
    socketHandle := self
      primSocketCreateNetwork: socketDomain
      type: socketType
      receiveBufferSize: 8000
      sendBufSize: 8000
      semaIndex: semaIndex
      readSemaIndex: readSemaIndex
      writeSemaIndex: writeSemaIndex.
    [...]

Change Socket>>initialize to preserve the default behaviour:

initialize: socketType
  self initialize: socketType withDomain: 0

Next, create Socket class>>newIPC like this:

newIPC
  "Create a socket and initialise it for IPC aka Unix domain."
  self initializeNetwork.
  ^[ super new initialize: TCPSocketType withDomain: 1 ]
   repeatWithGCIf: [ :socket | socket isValid not ]

Here "TCPSocketType" really means SOCK_STREAM in the Unix C API parlance.

We'll need Socket>>connectTo: as well:

connectTo: socketAddress
  | status |
   
  status := self primSocketConnectionStatus: socketHandle.
  (status == Unconnected)
    ifFalse: [InvalidSocketStatusException signal: 'Socket status must Unconnected before opening a new connection'].
   
  self primSocket: socketHandle connectTo: socketAddress

Now, how to get a Unix domain socket address? After some experimentation, this works:

NetNameResolver primGetAddressInfoHost: '' service: '/tmp/sp' flags: 0 family: 1 type: 0 protocol: 0.
s := NetNameResolver primGetAddressInfoSize.
sa := SocketAddress new: s withAll: 0.
NetNameResolver primGetAddressInfoResult: sa.

To test, use some known-working Unix domain socket test programs. The following is a slightly modified, rather self-explanatory Python server from here:

import socket,os
sp = "/tmp/sp"
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
    os.remove(sp)
except OSError:
    pass
s.bind(sp)
s.listen(1)
conn, addr = s.accept()
while 1:
    data = conn.recv(1024)
    if not data: break
    print data
    conn.send(data)
conn.close()

There is a corresponding client program. Run the two to verify that the server works.

Back to Smalltalk, the following code run in a workspace will connect to above Python server, send, receive, then print "Hello, from Smalltalk!" on the transcript. Tested on OSX and Linux with Pharo 2.0.

| s sa sock |
NetNameResolver primGetAddressInfoHost: '' service: '/tmp/sp' flags: 0 family: 1 type: 0 protocol: 0.
s := NetNameResolver primGetAddressInfoSize.
sa := SocketAddress new: s withAll: 0.
NetNameResolver primGetAddressInfoResult: sa.
sock := Socket newIPC.
sock ifNotNil: [
    sock connectTo: sa.
    sock sendData: 'Hello, from Smalltalk!'.
    Transcript show: sock receiveData; cr.
    sock closeAndDestroy ]
Blog comments powered by Disqus