0

I'm rewriting a PHP application in Node.js. A C program runs on the Linux OS that also hosts PHP.

In short, the C program handles sending data from the PHP web interface to some proprietary hardware. It also handles the creation of the socket file, and various other tasks.

In the legacy code, PHP runs as expected by binding to the socket and transmits/receives data as expected.

I have written both a Node.js Server and Client script to see that I can create and connect to Unix Domain sockets using the native 'net' module. This works as expected.

However, when I attempt to connect the node.js socket client to the socket file created by the C program, I see the following error:

{
    Error: connect EPROTOTYPE /var/run/arcontroller-master.socket
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
    code: 'EPROTOTYPE',
    errno: 'EPROTOTYPE',
    syscall: 'connect',
    address: '/var/run/arcontroller-master.socket'
}

Research has revealed that EPROTOTYPE: Protocol wrong type for socket. No information is given in the net module documentation for why the protocol is wrong or how to define the correct protocol.

Can you define a different protocol in node.js? Perhaps this is a misunderstanding on my part but I thought IPC was just a IP connection through a unix domain socket. I am mistaken here?

Further Information: The C Program uses the sys/socket lib under the hood, an abstract of the socket creation process is outlined below:

// Open up the socket we're listening on    
struct sockaddr_un server_address;

server_address.sun_family = AF_UNIX;
strcpy(server_address.sun_path, socketFile);

sock = socket(AF_UNIX, SOCK_DGRAM, 0);

Further reading shows SOCK_DGRAM is a UDP socket - what am I missing here?

bruno
  • 32,421
  • 7
  • 25
  • 37
wntwrk
  • 317
  • 1
  • 2
  • 10
  • 1
    `SOCK_DGRAM` is a datagram socket. Protocol IP + `SOCK_DGRAM` is UDP. Protocol Unix + `SOCK_DGRAM` is a Unix datagram socket, not UDP. – Maxim Egorushkin Jan 31 '19 at 17:47

1 Answers1

1

See man unix(7):

EPROTOTYPE Remote socket does not match the local socket type (SOCK_DGRAM versus SOCK_STREAM).


I can create and connect to Unix Domain sockets using the native 'net' module.

net module provides stream sockets, not datagram. This is the reason you observe the error.

You need to use a datagram socket in your node.js code. See node.js documentation for UDP / Datagram Sockets.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • Thanks for the explanation. It appears Node dropped support for UDP across Unix Domain sockets many versions ago. This package can be used in its place although mileage may vary compared to native modules: https://github.com/bnoordhuis/node-unix-dgram – wntwrk Feb 05 '19 at 12:08
  • @wntwrk Hmm, that link I posted is for node.js version 9. – Maxim Egorushkin Feb 05 '19 at 12:34
  • Yes UDP works with the Datagram module as you suggest but it only accepts IP Address / Port values with no reference to accepting a socket file path. Testing was unsuccessful with socket file path – wntwrk Feb 06 '19 at 08:58