Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AF_UNIX equivalent for Windows [duplicate]

I want to know how to use something that behaves like Unix Domain Socket on Windows.

The behaviour is: A process will be a "server" and receive connections from other processes and it can keep and use connections from different processes, as a TCP socket does.

IP socket is not a solution, because it requires to choose a port, processes from other applications may need the chosen port, and the open port may be seen in the network.
I do not know if named pipes can receive and keep multiple clients, but I did not see how to distinguish different clients. And it does not provide a way to wait data from multiple connections with something like select.

An old question says about using named pipes, but it does not explain how to use named pipes to get the desired behaviour. AF_UNIX in windows
I did not see how to get the handle of a specific client.

like image 950
Squall Avatar asked Jan 27 '12 05:01

Squall


2 Answers

Windows has recently (Windows 10 Insider build 17063) implemented support for AF_UNIX, so you can use it in future windows builds.

However not all of it is implemented, the below features don't work.

  • AF_UNIX datagram (SOCK_DGRAM) or sequence packet (SOCK_SEQPACKET) socket type.
  • Ancillary data: Linux's unix socket implementation supports passing ancillary data such as passing file descriptors (SCM_RIGHTS) or credentials (‘SCM_CREDENTIALS`) over the socket. There is no support for ancillary data in the Windows unix socket implementation.
  • Autobind feature (see the section on ‘sockaddr_un’ for details).
  • socketpair: socketpair socket API is not supported in Winsock 2.0.

Source: https://blogs.msdn.microsoft.com/commandline/2017/12/19/af_unix-comes-to-windows/

like image 166
TheWhoAreYouPerson Avatar answered Oct 17 '22 03:10

TheWhoAreYouPerson


Probably not the answer you want to hear, but COM is one of several standard mechanisms to achieve inter-process communication in Windows. It has it's issues that annoy developers - but it works quite well for all the requirements you listed.

As for IP sockets, you mentioned the issue of "can be seen in the network". This is not the case if you just simply bind your server socket to the localhost address (127.0.0.1). (2021 disclaimer - there is a potential vulnerability - see comments below).

SOCKET s;
const DWORD LOCAL_HOST_IP = 0x7f000001; // 127.0.0.1
sockaddr_in addrLocal = {};
   
s = socket(AF_INET, SOCK_STREAM, 0);
addrLocal.sin_family = AF_INET;
addrLocal.sin_port = htons(YOUR_APPLICATION_PORT);
addrLocal.sin_addr.s_addr = htonl(LOCAL_HOST_IP);
s = SOCKET(AF_INET, SOCK_STREAM, 0);
bind(s, (sockadr*)&addrLocal, sizeof(addrLocal));
like image 35
selbie Avatar answered Oct 17 '22 01:10

selbie