Unix sockets with SWI

Is there a built-in predicate in SWI to make a connection to an AF_UNIX socket? I tried searching around, but the only function I see providing that is in XPCE, but that isn’t actually exported.

1 Like

Right. Should be added …

1 Like

I started making my own foreign wrapper, but if it’s desired, I can make a PR to add it to library(socket).


PR opened https://github.com/SWI-Prolog/packages-clib/pull/29

1 Like

I noticed when running under windows the new predicates return false, don’t you think it would be better to throw an exception?

Oh right, thanks, I meant to change that; thanks for the catch!

Thanks. Probably a resource error, but Jan or Paulo may think of a better error.

1 Like

Considering the ISO Prolog standard classification of errors, resource_error/1 is the sensible choice.

The current policy is not to implement predicates that cannot be realised on some platform. That allows for

:- if(current_predate(unix_socket/2)).
:- else.
:- endif.

As these beans are known as unix domain sockets I think I have a preference for that name (unix_domain_socket). An alternative could be tcp_bind(unix_domain(File))? (or are we too late in that case, I don’t have the API in my head).

Sure, that was going to be my initial approach, but I wasn’t sure how to conditionally export the predicates from socket.pl. In this case, would I do something like :- if current_prolog_flag(windows, false). :- export([unix_socket/1, unix_connect/2]). : endif. instead of having the predicates in the module list?

As you say, it is too late by the point, because the socket has already been created with the AF_INET domain.

This should do:

:- if(current_predicate(unix_socket/1)).
:- export(( unix_socket/1,
:- endif.

Do we really need a separate unix_connect/1?

1 Like

The current tcp_connect assumes the address is an inet address, but I could presumably change that to do things differently depending on the type of either the socket given to it or the address.

1 Like

Updated; now there’s just one new predicated added, unix_domain_socket/1 (which doesn’t exist on Windows) and tcp_connect/2 now checks whether its first argument is an AF_UNIX socket or otherwise to determine how to construct the sockaddr to pass to nbio_connect.

Currently, I’ve made all the changes in socket.c, but presumably some of the logic could be moved to nonblockio.c instead (i.e. move all the logic from pl_connect to nbio_get_sockaddr, although that would involve changing the argument type of the latter (from sockaddr_in * to sockaddr*).