derhuerst / node-sockopt

getsockopt & setsockopt for Node.js sockets.
https://github.com/derhuerst/node-sockopt#sockopt
ISC License
8 stars 2 forks source link

TypeError: getsockopt failed: Operation not supported (95) #7

Open digipigeon opened 1 year ago

digipigeon commented 1 year ago

Hello, I am starting a simple UDP listener and trying to adjust the receive buffer size, however even on reading the information, it seems to throw an error.

Code Snippet

const dgram = require('dgram');
const {getsockopt, setsockopt} = require('sockopt')
const SO_RCVBUF = 0x1002
const SOL_SOCKET = 0xffff

function startUDPServer (port = 9060, address = defaultBindIP) {
    const socket = dgram.createSocket('udp4')

    socket.on('error', (err) => log('UDP Error', err.message))
    socket.on('listening', () => {
        log('UDP Started', socket.address().address, socket.address().port)

        console.log('SO_RCVBUF is', getsockopt(socket, SOL_SOCKET, SO_RCVBUF));
        setsockopt(socket, SOL_SOCKET, SO_RCVBUF, 1024); // 1MB
        console.log('SO_RCVBUF is now', getsockopt(socket, SOL_SOCKET, SO_RCVBUF));
    })
    socket.on('close', () => log('UDP Socket Closed', socket.address().address, socket.address().port))

    socket.on('message', (msg, socket) => {
        udpMsg++;
        processMessage(msg, socket);
    })

    socket.bind(port, address)
}

This works fine without the getsockopt bits used.

Which causes the following error:

node_modules/sockopt/index.js:17
        return _getsockopt(fd(socket), level, flagName)
               ^

TypeError: getsockopt failed: Operation not supported (95)
    at getsockopt (/var/apps/hep-connexcs/node_modules/sockopt/index.js:17:9)
    at Socket.<anonymous> (/var/apps/hep-connexcs/modules/ingress.js:32:32)
    at Socket.emit (events.js:400:28)
    at startListening (dgram.js:172:10)
    at dgram.js:364:7
    at processTicksAndRejections (internal/process/task_queues.js:83:21)
digipigeon commented 1 year ago

Just incase anyone else is looking for the same thing, it appears my specific use case can be done natively:

https://nodejs.org/api/dgram.html#socketsetrecvbuffersizesize

derhuerst commented 11 months ago

Even though setting SO_RCVBUF can be done natively via socket.setRecvBufferSize(), in order to investigate if this is a bug in node-sockopt, can you provide more details about your environment?

I cannot reproduce the error; For me, assuming defaultBindIp is 0.0.0.0, with Node.js v20.6.1 on macOS 12.6.9, the output looks as follows:

UDP Started 0.0.0.0 9060
SO_RCVBUF is 786896
SO_RCVBUF is now 1024
(node:12424) [DEP0112] DeprecationWarning: Socket.prototype._handle is deprecated
(Use `node --trace-deprecation ...` to show where the warning was created)
derhuerst commented 6 months ago

@digipigeon Do you have any news about this?