Skip to content

How to read from unix socket file descriptor? #8328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
g00dnatur3 opened this issue Aug 29, 2016 · 4 comments
Closed

How to read from unix socket file descriptor? #8328

g00dnatur3 opened this issue Aug 29, 2016 · 4 comments
Labels
net Issues and PRs related to the net subsystem. question Issues that look for answers.

Comments

@g00dnatur3
Copy link

  • Version:
  • Platform:
  • Subsystem:

this is the code:

console.log( fs.readlinkSync('/proc/self/fd/' + fd) );

var readStream = fs.createReadStream(null, {fd: fd});

readStream.on('error', function(err) {
    console.log(err);
});
readStream.on('data', function(data) {
    console.log(data.toString());
});

this is the output:

socket:[12357]
{ Error: EAGAIN: resource temporarily unavailable, read
at Error (native) errno: -11, code: 'EAGAIN', syscall: 'read' }

I have also tried this:

var socket = new net.Socket({fd: fd});

the error i get is:

TypeError: Unsupported fd type: UNKNOWN

Now, I can write to the fd using createWriteStream (and receive data on the other end), but I am unable to read.

If i were to read this socket using this c function it will work:

ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
documentation here: http://linux.die.net/man/2/recvmsg

But i want to know how to read from unix socket descriptor using nodejs.

Keep in mind this code:

console.log( fs.readlinkSync('/proc/self/fd/' + fd) );

produces:

socket:[12357]

So I am just trying to read a simple unix socket, I would be shocked if this is not supported in node.

that is why I am asking the question, how do i read from a unix socket descritpor in nodejs?

Thank you for any help.

@mscdex mscdex added question Issues that look for answers. net Issues and PRs related to the net subsystem. labels Aug 29, 2016
@mscdex
Copy link
Contributor

mscdex commented Aug 29, 2016

Where is the socket fd created? Is the socket a stream or datagram socket?

@john-bouchard
Copy link

Inside the Bluez source code.

bluez/btio/btio.c,

look for the create_io function:
static GIOChannel *create_io(gboolean server, struct set_opts *opts, GError **err)

My bluetooth connection type is RFCOMM, so if we look at the source code we see this:

        case BT_IO_RFCOMM:
                sock = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
                if (sock < 0) {
                        ERROR_FAILED(err, "socket(STREAM, RFCOMM)", errno);
                        return NULL;
                }
                if (rfcomm_bind(sock, &opts->src,
                                        server ? opts->channel : 0, err) < 0)
                        goto failed;
                if (!rfcomm_set(sock, opts->sec_level, opts->master, err))
                        goto failed;
                break;

So I'm pretty sure it is a stream socket.

Also, you can see from the source code, Bluez is using the gio channel lib,
here is the doc for that: https://developer.gnome.org/glib/stable/glib-IO-Channels.html

Thanks for any help you may provide :)

@bnoordhuis
Copy link
Member

Libuv, the I/O library that node.js uses, restricts its support to AF_INET/AF_INET6 and AF_UNIX for reasons of portability.

What you could do perhaps is to turn off non-blocking mode on the file descriptor with (for example) the ffi module and then wrap it with fs.createReadStream().

@g00dnatur3
Copy link
Author

Thank you for your help + advice, thought this was the case.

Im not sure i want to have blocking io, going to try and create a gyp binding to gio-channel instead.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
net Issues and PRs related to the net subsystem. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

4 participants