2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2019-04-09 07:55:53 +00:00
|
|
|
const { Object } = primordials;
|
|
|
|
|
2017-05-09 18:18:35 +00:00
|
|
|
const errors = require('internal/errors');
|
2018-07-21 05:17:21 +00:00
|
|
|
const {
|
|
|
|
kStateSymbol,
|
|
|
|
_createSocketHandle,
|
2018-07-29 14:41:11 +00:00
|
|
|
newHandle,
|
2018-07-21 05:17:21 +00:00
|
|
|
} = require('internal/dgram');
|
2019-04-18 02:46:39 +00:00
|
|
|
const { guessHandleType } = internalBinding('util');
|
2019-03-16 22:03:48 +00:00
|
|
|
const {
|
|
|
|
isLegalPort,
|
|
|
|
} = require('internal/net');
|
2018-02-27 13:55:32 +00:00
|
|
|
const {
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_MISSING_ARGS,
|
|
|
|
ERR_SOCKET_ALREADY_BOUND,
|
|
|
|
ERR_SOCKET_BAD_BUFFER_SIZE,
|
|
|
|
ERR_SOCKET_BAD_PORT,
|
|
|
|
ERR_SOCKET_BUFFER_SIZE,
|
|
|
|
ERR_SOCKET_CANNOT_SEND,
|
2019-03-16 22:03:48 +00:00
|
|
|
ERR_SOCKET_DGRAM_IS_CONNECTED,
|
|
|
|
ERR_SOCKET_DGRAM_NOT_CONNECTED,
|
2018-07-29 14:41:11 +00:00
|
|
|
ERR_SOCKET_DGRAM_NOT_RUNNING,
|
|
|
|
ERR_INVALID_FD_TYPE
|
2018-02-27 13:55:32 +00:00
|
|
|
} = errors.codes;
|
2018-08-03 15:23:26 +00:00
|
|
|
const {
|
|
|
|
isInt32,
|
|
|
|
validateString,
|
|
|
|
validateNumber
|
|
|
|
} = require('internal/validators');
|
2017-10-07 14:50:42 +00:00
|
|
|
const { Buffer } = require('buffer');
|
2019-03-21 15:21:22 +00:00
|
|
|
const { deprecate } = require('internal/util');
|
2017-09-28 07:16:41 +00:00
|
|
|
const { isUint8Array } = require('internal/util/types');
|
2015-09-16 22:45:29 +00:00
|
|
|
const EventEmitter = require('events');
|
2018-02-11 21:35:59 +00:00
|
|
|
const {
|
|
|
|
defaultTriggerAsyncIdScope,
|
2018-07-27 12:35:39 +00:00
|
|
|
symbols: { async_id_symbol, owner_symbol }
|
2018-02-11 21:35:59 +00:00
|
|
|
} = require('internal/async_hooks');
|
2018-10-14 23:41:32 +00:00
|
|
|
const { UV_UDP_REUSEADDR } = internalBinding('constants').os;
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2018-10-21 07:59:38 +00:00
|
|
|
const {
|
|
|
|
constants: { UV_UDP_IPV6ONLY },
|
|
|
|
UDP,
|
|
|
|
SendWrap
|
|
|
|
} = internalBinding('udp_wrap');
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const BIND_STATE_UNBOUND = 0;
|
|
|
|
const BIND_STATE_BINDING = 1;
|
|
|
|
const BIND_STATE_BOUND = 2;
|
2012-12-31 16:42:54 +00:00
|
|
|
|
2019-03-16 22:03:48 +00:00
|
|
|
const CONNECT_STATE_DISCONNECTED = 0;
|
|
|
|
const CONNECT_STATE_CONNECTING = 1;
|
|
|
|
const CONNECT_STATE_CONNECTED = 2;
|
|
|
|
|
2017-09-20 04:55:53 +00:00
|
|
|
const RECV_BUFFER = true;
|
|
|
|
const SEND_BUFFER = false;
|
|
|
|
|
2017-07-11 13:23:38 +00:00
|
|
|
// Lazily loaded
|
2013-01-22 22:52:20 +00:00
|
|
|
var cluster = null;
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2018-02-03 09:09:15 +00:00
|
|
|
const errnoException = errors.errnoException;
|
|
|
|
const exceptionWithHostPort = errors.exceptionWithHostPort;
|
2011-08-20 01:47:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
function Socket(type, listener) {
|
2015-09-16 22:45:29 +00:00
|
|
|
EventEmitter.call(this);
|
2017-08-01 00:38:06 +00:00
|
|
|
var lookup;
|
2018-07-21 04:56:12 +00:00
|
|
|
let recvBufferSize;
|
|
|
|
let sendBufferSize;
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2016-08-08 09:32:50 +00:00
|
|
|
if (type !== null && typeof type === 'object') {
|
2014-04-08 23:04:53 +00:00
|
|
|
var options = type;
|
|
|
|
type = options.type;
|
2017-08-01 00:38:06 +00:00
|
|
|
lookup = options.lookup;
|
2018-07-21 04:56:12 +00:00
|
|
|
recvBufferSize = options.recvBufferSize;
|
|
|
|
sendBufferSize = options.sendBufferSize;
|
2014-04-08 23:04:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 04:21:27 +00:00
|
|
|
const handle = newHandle(type, lookup);
|
2018-07-27 12:35:39 +00:00
|
|
|
handle[owner_symbol] = this;
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
this[async_id_symbol] = handle.getAsyncId();
|
2011-08-20 01:47:40 +00:00
|
|
|
this.type = type;
|
|
|
|
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof listener === 'function')
|
2011-08-20 01:47:40 +00:00
|
|
|
this.on('message', listener);
|
2018-07-21 04:56:12 +00:00
|
|
|
|
|
|
|
this[kStateSymbol] = {
|
|
|
|
handle,
|
|
|
|
receiving: false,
|
|
|
|
bindState: BIND_STATE_UNBOUND,
|
2019-03-16 22:03:48 +00:00
|
|
|
connectState: CONNECT_STATE_DISCONNECTED,
|
2018-07-21 04:56:12 +00:00
|
|
|
queue: undefined,
|
|
|
|
reuseAddr: options && options.reuseAddr, // Use UV_UDP_REUSEADDR if true.
|
2018-10-21 07:59:38 +00:00
|
|
|
ipv6Only: options && options.ipv6Only,
|
2018-07-21 04:56:12 +00:00
|
|
|
recvBufferSize,
|
|
|
|
sendBufferSize
|
|
|
|
};
|
2011-08-20 01:47:40 +00:00
|
|
|
}
|
2018-11-30 16:55:48 +00:00
|
|
|
Object.setPrototypeOf(Socket.prototype, EventEmitter.prototype);
|
2018-12-02 14:03:01 +00:00
|
|
|
Object.setPrototypeOf(Socket, EventEmitter);
|
2011-08-20 01:47:40 +00:00
|
|
|
|
|
|
|
|
2017-03-05 16:42:03 +00:00
|
|
|
function createSocket(type, listener) {
|
2011-08-20 01:47:40 +00:00
|
|
|
return new Socket(type, listener);
|
2017-03-05 16:42:03 +00:00
|
|
|
}
|
2011-08-20 01:47:40 +00:00
|
|
|
|
|
|
|
|
2013-01-22 22:52:20 +00:00
|
|
|
function startListening(socket) {
|
2018-07-21 04:56:12 +00:00
|
|
|
const state = socket[kStateSymbol];
|
|
|
|
|
|
|
|
state.handle.onmessage = onMessage;
|
2013-01-22 22:52:20 +00:00
|
|
|
// Todo: handle errors
|
2018-07-21 04:56:12 +00:00
|
|
|
state.handle.recvStart();
|
|
|
|
state.receiving = true;
|
|
|
|
state.bindState = BIND_STATE_BOUND;
|
2013-01-22 22:52:20 +00:00
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
if (state.recvBufferSize)
|
|
|
|
bufferSize(socket, state.recvBufferSize, RECV_BUFFER);
|
2017-06-11 21:58:53 +00:00
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
if (state.sendBufferSize)
|
|
|
|
bufferSize(socket, state.sendBufferSize, SEND_BUFFER);
|
2017-06-11 21:58:53 +00:00
|
|
|
|
2013-01-22 22:52:20 +00:00
|
|
|
socket.emit('listening');
|
|
|
|
}
|
|
|
|
|
2013-02-27 18:31:24 +00:00
|
|
|
function replaceHandle(self, newHandle) {
|
2018-07-21 04:56:12 +00:00
|
|
|
const state = self[kStateSymbol];
|
|
|
|
const oldHandle = state.handle;
|
2013-01-22 22:52:20 +00:00
|
|
|
|
2013-02-27 18:31:24 +00:00
|
|
|
// Set up the handle that we got from master.
|
2018-07-21 04:56:12 +00:00
|
|
|
newHandle.lookup = oldHandle.lookup;
|
|
|
|
newHandle.bind = oldHandle.bind;
|
|
|
|
newHandle.send = oldHandle.send;
|
2018-07-27 12:35:39 +00:00
|
|
|
newHandle[owner_symbol] = self;
|
2013-02-27 18:31:24 +00:00
|
|
|
|
|
|
|
// Replace the existing handle by the handle we got from master.
|
2018-07-21 04:56:12 +00:00
|
|
|
oldHandle.close();
|
|
|
|
state.handle = newHandle;
|
2019-03-16 22:03:48 +00:00
|
|
|
// Check if the udp handle was connected and set the state accordingly
|
|
|
|
if (isConnected(self))
|
|
|
|
state.connectState = CONNECT_STATE_CONNECTED;
|
2013-02-27 18:31:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:58:53 +00:00
|
|
|
function bufferSize(self, size, buffer) {
|
|
|
|
if (size >>> 0 !== size)
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_SOCKET_BAD_BUFFER_SIZE();
|
2017-06-11 21:58:53 +00:00
|
|
|
|
2017-10-28 06:24:15 +00:00
|
|
|
const ctx = {};
|
2018-07-21 04:56:12 +00:00
|
|
|
const ret = self[kStateSymbol].handle.bufferSize(size, buffer, ctx);
|
2017-10-28 06:24:15 +00:00
|
|
|
if (ret === undefined) {
|
2018-03-20 16:46:30 +00:00
|
|
|
throw new ERR_SOCKET_BUFFER_SIZE(ctx);
|
2017-06-11 21:58:53 +00:00
|
|
|
}
|
2017-10-28 06:24:15 +00:00
|
|
|
return ret;
|
2017-06-11 21:58:53 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 14:41:11 +00:00
|
|
|
// Query master process to get the server handle and utilize it.
|
|
|
|
function bindServerHandle(self, options, errCb) {
|
|
|
|
if (!cluster)
|
|
|
|
cluster = require('cluster');
|
|
|
|
|
|
|
|
const state = self[kStateSymbol];
|
|
|
|
cluster._getServer(self, options, (err, handle) => {
|
|
|
|
if (err) {
|
|
|
|
errCb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!state.handle) {
|
|
|
|
// Handle has been closed in the mean time.
|
|
|
|
return handle.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
replaceHandle(self, handle);
|
|
|
|
startListening(self);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-25 14:27:38 +00:00
|
|
|
Socket.prototype.bind = function(port_, address_ /* , callback */) {
|
2016-01-10 22:52:13 +00:00
|
|
|
let port = port_;
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2018-07-21 03:27:40 +00:00
|
|
|
healthCheck(this);
|
2018-07-21 04:56:12 +00:00
|
|
|
const state = this[kStateSymbol];
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
if (state.bindState !== BIND_STATE_UNBOUND)
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_SOCKET_ALREADY_BOUND();
|
2013-01-22 22:52:20 +00:00
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
state.bindState = BIND_STATE_BINDING;
|
2013-01-22 22:52:20 +00:00
|
|
|
|
2017-02-08 14:02:33 +00:00
|
|
|
if (arguments.length && typeof arguments[arguments.length - 1] === 'function')
|
2017-02-08 17:18:07 +00:00
|
|
|
this.once('listening', arguments[arguments.length - 1]);
|
2013-02-27 18:31:24 +00:00
|
|
|
|
2014-08-22 20:51:53 +00:00
|
|
|
if (port instanceof UDP) {
|
2017-02-08 17:18:07 +00:00
|
|
|
replaceHandle(this, port);
|
|
|
|
startListening(this);
|
|
|
|
return this;
|
2013-02-27 18:31:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 14:41:11 +00:00
|
|
|
// Open an existing fd instead of creating a new one.
|
|
|
|
if (port !== null && typeof port === 'object' &&
|
|
|
|
isInt32(port.fd) && port.fd > 0) {
|
|
|
|
const fd = port.fd;
|
|
|
|
const exclusive = !!port.exclusive;
|
|
|
|
const state = this[kStateSymbol];
|
|
|
|
|
|
|
|
if (!cluster)
|
|
|
|
cluster = require('cluster');
|
|
|
|
|
|
|
|
if (cluster.isWorker && !exclusive) {
|
|
|
|
bindServerHandle(this, {
|
|
|
|
address: null,
|
|
|
|
port: null,
|
|
|
|
addressType: this.type,
|
|
|
|
fd,
|
|
|
|
flags: null
|
|
|
|
}, (err) => {
|
|
|
|
// Callback to handle error.
|
|
|
|
const ex = errnoException(err, 'open');
|
|
|
|
this.emit('error', ex);
|
|
|
|
state.bindState = BIND_STATE_UNBOUND;
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const type = guessHandleType(fd);
|
|
|
|
if (type !== 'UDP')
|
|
|
|
throw new ERR_INVALID_FD_TYPE(type);
|
|
|
|
const err = state.handle.open(fd);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
throw errnoException(err, 'open');
|
|
|
|
|
2019-03-16 22:03:48 +00:00
|
|
|
// Check if the udp handle was connected and set the state accordingly
|
|
|
|
if (isConnected(this))
|
|
|
|
state.connectState = CONNECT_STATE_CONNECTED;
|
|
|
|
|
2018-07-29 14:41:11 +00:00
|
|
|
startListening(this);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-08-22 20:51:53 +00:00
|
|
|
var address;
|
|
|
|
var exclusive;
|
|
|
|
|
2015-01-29 01:05:53 +00:00
|
|
|
if (port !== null && typeof port === 'object') {
|
2014-08-22 20:51:53 +00:00
|
|
|
address = port.address || '';
|
|
|
|
exclusive = !!port.exclusive;
|
|
|
|
port = port.port;
|
|
|
|
} else {
|
2017-02-08 14:02:33 +00:00
|
|
|
address = typeof address_ === 'function' ? '' : address_;
|
2014-08-22 20:51:53 +00:00
|
|
|
exclusive = false;
|
|
|
|
}
|
2012-07-09 15:53:48 +00:00
|
|
|
|
2019-01-21 00:22:27 +00:00
|
|
|
// Defaulting address for bind to all interfaces
|
2017-08-01 00:38:06 +00:00
|
|
|
if (!address) {
|
|
|
|
if (this.type === 'udp4')
|
|
|
|
address = '0.0.0.0';
|
|
|
|
else
|
|
|
|
address = '::';
|
2016-02-29 18:47:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 02:44:26 +00:00
|
|
|
// Resolve address first
|
2018-07-21 04:56:12 +00:00
|
|
|
state.handle.lookup(address, (err, ip) => {
|
2011-08-20 01:47:40 +00:00
|
|
|
if (err) {
|
2018-07-21 04:56:12 +00:00
|
|
|
state.bindState = BIND_STATE_UNBOUND;
|
2017-02-08 17:18:07 +00:00
|
|
|
this.emit('error', err);
|
2012-07-09 15:53:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-22 22:52:20 +00:00
|
|
|
if (!cluster)
|
|
|
|
cluster = require('cluster');
|
2012-07-09 15:53:48 +00:00
|
|
|
|
2015-08-25 19:24:41 +00:00
|
|
|
var flags = 0;
|
2018-07-21 04:56:12 +00:00
|
|
|
if (state.reuseAddr)
|
2016-05-02 17:27:12 +00:00
|
|
|
flags |= UV_UDP_REUSEADDR;
|
2018-10-21 07:59:38 +00:00
|
|
|
if (state.ipv6Only)
|
|
|
|
flags |= UV_UDP_IPV6ONLY;
|
2015-08-25 19:24:41 +00:00
|
|
|
|
2014-08-22 20:51:53 +00:00
|
|
|
if (cluster.isWorker && !exclusive) {
|
2018-07-29 14:41:11 +00:00
|
|
|
bindServerHandle(this, {
|
2015-08-25 19:24:41 +00:00
|
|
|
address: ip,
|
|
|
|
port: port,
|
2017-02-08 17:18:07 +00:00
|
|
|
addressType: this.type,
|
2015-08-25 19:24:41 +00:00
|
|
|
fd: -1,
|
|
|
|
flags: flags
|
2018-07-29 14:41:11 +00:00
|
|
|
}, (err) => {
|
|
|
|
// Callback to handle error.
|
|
|
|
const ex = exceptionWithHostPort(err, 'bind', ip, port);
|
|
|
|
this.emit('error', ex);
|
|
|
|
state.bindState = BIND_STATE_UNBOUND;
|
|
|
|
});
|
2013-01-22 22:52:20 +00:00
|
|
|
} else {
|
2018-07-21 04:56:12 +00:00
|
|
|
if (!state.handle)
|
2019-03-07 00:03:53 +00:00
|
|
|
return; // Handle has been closed in the mean time
|
2013-01-22 22:52:20 +00:00
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
const err = state.handle.bind(ip, port || 0, flags);
|
2013-07-18 21:18:50 +00:00
|
|
|
if (err) {
|
2015-01-08 19:14:44 +00:00
|
|
|
var ex = exceptionWithHostPort(err, 'bind', ip, port);
|
2017-02-08 17:18:07 +00:00
|
|
|
this.emit('error', ex);
|
2018-07-21 04:56:12 +00:00
|
|
|
state.bindState = BIND_STATE_UNBOUND;
|
2013-01-22 22:52:20 +00:00
|
|
|
// Todo: close?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:18:07 +00:00
|
|
|
startListening(this);
|
2013-01-22 22:52:20 +00:00
|
|
|
}
|
|
|
|
});
|
2014-12-29 20:03:24 +00:00
|
|
|
|
2017-02-08 17:18:07 +00:00
|
|
|
return this;
|
2011-08-20 01:47:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-03-16 22:03:48 +00:00
|
|
|
function validatePort(port) {
|
|
|
|
const legal = isLegalPort(port);
|
|
|
|
if (legal)
|
|
|
|
port = port | 0;
|
|
|
|
|
|
|
|
if (!legal || port === 0)
|
|
|
|
throw new ERR_SOCKET_BAD_PORT(port);
|
|
|
|
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.connect = function(port, address, callback) {
|
|
|
|
port = validatePort(port);
|
|
|
|
if (typeof address === 'function') {
|
|
|
|
callback = address;
|
|
|
|
address = '';
|
|
|
|
} else if (address === undefined) {
|
|
|
|
address = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
validateString(address, 'address');
|
|
|
|
|
|
|
|
const state = this[kStateSymbol];
|
|
|
|
|
|
|
|
if (state.connectState !== CONNECT_STATE_DISCONNECTED)
|
|
|
|
throw new ERR_SOCKET_DGRAM_IS_CONNECTED();
|
|
|
|
|
|
|
|
state.connectState = CONNECT_STATE_CONNECTING;
|
|
|
|
if (state.bindState === BIND_STATE_UNBOUND)
|
|
|
|
this.bind({ port: 0, exclusive: true }, null);
|
|
|
|
|
|
|
|
if (state.bindState !== BIND_STATE_BOUND) {
|
|
|
|
enqueue(this, _connect.bind(this, port, address, callback));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_connect.call(this, port, address, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function _connect(port, address, callback) {
|
|
|
|
const state = this[kStateSymbol];
|
|
|
|
if (callback)
|
|
|
|
this.once('connect', callback);
|
|
|
|
|
|
|
|
const afterDns = (ex, ip) => {
|
|
|
|
defaultTriggerAsyncIdScope(
|
|
|
|
this[async_id_symbol],
|
|
|
|
doConnect,
|
|
|
|
ex, this, ip, address, port, callback
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
state.handle.lookup(address, afterDns);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function doConnect(ex, self, ip, address, port, callback) {
|
|
|
|
const state = self[kStateSymbol];
|
|
|
|
if (!state.handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!ex) {
|
|
|
|
const err = state.handle.connect(ip, port);
|
|
|
|
if (err) {
|
|
|
|
ex = exceptionWithHostPort(err, 'connect', address, port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ex) {
|
|
|
|
state.connectState = CONNECT_STATE_DISCONNECTED;
|
|
|
|
return process.nextTick(() => {
|
|
|
|
if (callback) {
|
|
|
|
self.removeListener('connect', callback);
|
|
|
|
callback(ex);
|
|
|
|
} else {
|
|
|
|
self.emit('error', ex);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
state.connectState = CONNECT_STATE_CONNECTED;
|
|
|
|
process.nextTick(() => self.emit('connect'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.disconnect = function() {
|
|
|
|
const state = this[kStateSymbol];
|
|
|
|
if (state.connectState !== CONNECT_STATE_CONNECTED)
|
|
|
|
throw new ERR_SOCKET_DGRAM_NOT_CONNECTED();
|
|
|
|
|
|
|
|
const err = state.handle.disconnect();
|
|
|
|
if (err)
|
|
|
|
throw errnoException(err, 'connect');
|
|
|
|
else
|
|
|
|
state.connectState = CONNECT_STATE_DISCONNECTED;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Thin wrapper around `send`, here for compatibility with dgram_legacy.js
|
2011-08-20 01:47:40 +00:00
|
|
|
Socket.prototype.sendto = function(buffer,
|
|
|
|
offset,
|
|
|
|
length,
|
|
|
|
port,
|
|
|
|
address,
|
|
|
|
callback) {
|
2018-08-03 15:23:26 +00:00
|
|
|
validateNumber(offset, 'offset');
|
|
|
|
validateNumber(length, 'length');
|
|
|
|
validateNumber(port, 'port');
|
2018-08-02 22:51:02 +00:00
|
|
|
validateString(address, 'address');
|
2011-08-20 01:47:40 +00:00
|
|
|
|
|
|
|
this.send(buffer, offset, length, port, address, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-01-29 13:18:27 +00:00
|
|
|
function sliceBuffer(buffer, offset, length) {
|
2017-03-22 06:17:05 +00:00
|
|
|
if (typeof buffer === 'string') {
|
2016-01-25 23:00:06 +00:00
|
|
|
buffer = Buffer.from(buffer);
|
2017-03-22 06:17:05 +00:00
|
|
|
} else if (!isUint8Array(buffer)) {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('buffer',
|
2018-03-19 12:33:46 +00:00
|
|
|
['Buffer', 'Uint8Array', 'string'], buffer);
|
2017-03-22 06:17:05 +00:00
|
|
|
}
|
2016-01-29 13:18:27 +00:00
|
|
|
|
|
|
|
offset = offset >>> 0;
|
|
|
|
length = length >>> 0;
|
|
|
|
|
|
|
|
return buffer.slice(offset, offset + length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-17 10:36:54 +00:00
|
|
|
function fixBufferList(list) {
|
|
|
|
const newlist = new Array(list.length);
|
|
|
|
|
|
|
|
for (var i = 0, l = list.length; i < l; i++) {
|
|
|
|
var buf = list[i];
|
2016-01-29 13:18:27 +00:00
|
|
|
if (typeof buf === 'string')
|
2016-05-17 10:36:54 +00:00
|
|
|
newlist[i] = Buffer.from(buf);
|
2017-03-22 06:17:05 +00:00
|
|
|
else if (!isUint8Array(buf))
|
2016-05-17 10:36:54 +00:00
|
|
|
return null;
|
|
|
|
else
|
|
|
|
newlist[i] = buf;
|
2016-01-29 13:18:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 10:36:54 +00:00
|
|
|
return newlist;
|
2016-01-29 13:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function enqueue(self, toEnqueue) {
|
2018-07-21 04:56:12 +00:00
|
|
|
const state = self[kStateSymbol];
|
|
|
|
|
2016-01-29 13:18:27 +00:00
|
|
|
// If the send queue hasn't been initialized yet, do it, and install an
|
|
|
|
// event handler that flushes the send queue after binding is done.
|
2018-07-21 04:56:12 +00:00
|
|
|
if (state.queue === undefined) {
|
|
|
|
state.queue = [];
|
2017-01-27 14:54:31 +00:00
|
|
|
self.once('error', onListenError);
|
|
|
|
self.once('listening', onListenSuccess);
|
2016-01-29 13:18:27 +00:00
|
|
|
}
|
2018-07-21 04:56:12 +00:00
|
|
|
state.queue.push(toEnqueue);
|
2016-01-29 13:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-27 14:54:31 +00:00
|
|
|
function onListenSuccess() {
|
|
|
|
this.removeListener('error', onListenError);
|
|
|
|
clearQueue.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function onListenError(err) {
|
|
|
|
this.removeListener('listening', onListenSuccess);
|
2018-07-21 04:56:12 +00:00
|
|
|
this[kStateSymbol].queue = undefined;
|
2018-02-27 13:55:32 +00:00
|
|
|
this.emit('error', new ERR_SOCKET_CANNOT_SEND());
|
2017-01-27 14:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-30 16:29:27 +00:00
|
|
|
function clearQueue() {
|
2018-07-21 04:56:12 +00:00
|
|
|
const state = this[kStateSymbol];
|
|
|
|
const queue = state.queue;
|
|
|
|
state.queue = undefined;
|
2016-05-30 16:29:27 +00:00
|
|
|
|
|
|
|
// Flush the send queue.
|
|
|
|
for (var i = 0; i < queue.length; i++)
|
|
|
|
queue[i]();
|
|
|
|
}
|
|
|
|
|
2019-03-16 22:03:48 +00:00
|
|
|
function isConnected(self) {
|
|
|
|
try {
|
|
|
|
this.remoteAddress();
|
|
|
|
return true;
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 16:29:27 +00:00
|
|
|
|
2016-02-24 08:12:59 +00:00
|
|
|
// valid combinations
|
2019-03-16 22:03:48 +00:00
|
|
|
// For connectionless sockets
|
2016-02-24 08:12:59 +00:00
|
|
|
// send(buffer, offset, length, port, address, callback)
|
|
|
|
// send(buffer, offset, length, port, address)
|
2017-01-13 06:10:26 +00:00
|
|
|
// send(buffer, offset, length, port, callback)
|
2016-02-24 08:12:59 +00:00
|
|
|
// send(buffer, offset, length, port)
|
|
|
|
// send(bufferOrList, port, address, callback)
|
|
|
|
// send(bufferOrList, port, address)
|
2017-01-13 06:10:26 +00:00
|
|
|
// send(bufferOrList, port, callback)
|
2016-02-24 08:12:59 +00:00
|
|
|
// send(bufferOrList, port)
|
2019-03-16 22:03:48 +00:00
|
|
|
// For connected sockets
|
|
|
|
// send(buffer, offset, length, callback)
|
|
|
|
// send(buffer, offset, length)
|
|
|
|
// send(bufferOrList, callback)
|
|
|
|
// send(bufferOrList)
|
2011-08-20 01:47:40 +00:00
|
|
|
Socket.prototype.send = function(buffer,
|
|
|
|
offset,
|
|
|
|
length,
|
|
|
|
port,
|
|
|
|
address,
|
|
|
|
callback) {
|
|
|
|
|
2019-03-16 22:03:48 +00:00
|
|
|
let list;
|
|
|
|
const state = this[kStateSymbol];
|
|
|
|
const connected = state.connectState === CONNECT_STATE_CONNECTED;
|
|
|
|
if (!connected) {
|
|
|
|
if (address || (port && typeof port !== 'function')) {
|
|
|
|
buffer = sliceBuffer(buffer, offset, length);
|
|
|
|
} else {
|
|
|
|
callback = port;
|
|
|
|
port = offset;
|
|
|
|
address = length;
|
|
|
|
}
|
|
|
|
} else if (typeof length === 'number') {
|
2016-01-29 13:18:27 +00:00
|
|
|
buffer = sliceBuffer(buffer, offset, length);
|
2019-03-16 22:03:48 +00:00
|
|
|
if (typeof port === 'function') {
|
|
|
|
callback = port;
|
|
|
|
port = null;
|
|
|
|
} else if (port || address) {
|
|
|
|
throw new ERR_SOCKET_DGRAM_IS_CONNECTED();
|
|
|
|
}
|
2016-01-29 13:18:27 +00:00
|
|
|
} else {
|
2019-03-16 22:03:48 +00:00
|
|
|
callback = offset;
|
2016-01-29 13:18:27 +00:00
|
|
|
}
|
2012-02-23 00:51:27 +00:00
|
|
|
|
2016-01-29 13:18:27 +00:00
|
|
|
if (!Array.isArray(buffer)) {
|
|
|
|
if (typeof buffer === 'string') {
|
2016-05-17 10:36:54 +00:00
|
|
|
list = [ Buffer.from(buffer) ];
|
2017-03-22 06:17:05 +00:00
|
|
|
} else if (!isUint8Array(buffer)) {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('buffer',
|
2018-03-19 12:33:46 +00:00
|
|
|
['Buffer', 'Uint8Array', 'string'],
|
|
|
|
buffer);
|
2016-01-29 13:18:27 +00:00
|
|
|
} else {
|
2016-05-17 10:36:54 +00:00
|
|
|
list = [ buffer ];
|
2016-01-29 13:18:27 +00:00
|
|
|
}
|
2016-05-17 10:36:54 +00:00
|
|
|
} else if (!(list = fixBufferList(buffer))) {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('buffer list arguments',
|
2018-03-19 12:33:46 +00:00
|
|
|
['Buffer', 'string'], buffer);
|
2016-01-29 13:18:27 +00:00
|
|
|
}
|
2013-08-09 00:33:40 +00:00
|
|
|
|
2019-03-16 22:03:48 +00:00
|
|
|
if (!connected)
|
|
|
|
port = validatePort(port);
|
2012-02-23 00:51:27 +00:00
|
|
|
|
2013-08-09 02:48:10 +00:00
|
|
|
// Normalize callback so it's either a function or undefined but not anything
|
|
|
|
// else.
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof callback !== 'function')
|
2013-08-09 02:48:10 +00:00
|
|
|
callback = undefined;
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2017-01-13 06:10:26 +00:00
|
|
|
if (typeof address === 'function') {
|
|
|
|
callback = address;
|
|
|
|
address = undefined;
|
|
|
|
} else if (address && typeof address !== 'string') {
|
2018-03-19 12:33:46 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('address', ['string', 'falsy'], address);
|
2017-01-13 06:10:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 03:27:40 +00:00
|
|
|
healthCheck(this);
|
2012-07-09 15:53:48 +00:00
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
if (state.bindState === BIND_STATE_UNBOUND)
|
2017-07-11 00:55:21 +00:00
|
|
|
this.bind({ port: 0, exclusive: true }, null);
|
2012-12-31 16:42:54 +00:00
|
|
|
|
2016-05-17 10:36:54 +00:00
|
|
|
if (list.length === 0)
|
2016-09-23 20:55:53 +00:00
|
|
|
list.push(Buffer.alloc(0));
|
2016-05-17 10:36:54 +00:00
|
|
|
|
2013-01-28 21:19:02 +00:00
|
|
|
// If the socket hasn't been bound yet, push the outbound packet onto the
|
|
|
|
// send queue and send after binding is complete.
|
2018-07-21 04:56:12 +00:00
|
|
|
if (state.bindState !== BIND_STATE_BOUND) {
|
2017-02-08 17:18:07 +00:00
|
|
|
enqueue(this, this.send.bind(this, list, port, address, callback));
|
2012-07-09 15:53:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2017-02-08 17:18:07 +00:00
|
|
|
const afterDns = (ex, ip) => {
|
2017-11-22 17:41:00 +00:00
|
|
|
defaultTriggerAsyncIdScope(
|
|
|
|
this[async_id_symbol],
|
2018-01-05 14:03:10 +00:00
|
|
|
doSend,
|
|
|
|
ex, this, ip, list, address, port, callback
|
2017-11-22 17:41:00 +00:00
|
|
|
);
|
2017-02-08 17:18:07 +00:00
|
|
|
};
|
|
|
|
|
2019-03-16 22:03:48 +00:00
|
|
|
if (!connected) {
|
|
|
|
state.handle.lookup(address, afterDns);
|
|
|
|
} else {
|
|
|
|
afterDns(null, null);
|
|
|
|
}
|
2011-08-20 01:47:40 +00:00
|
|
|
};
|
|
|
|
|
2016-05-17 10:36:54 +00:00
|
|
|
function doSend(ex, self, ip, list, address, port, callback) {
|
2018-07-21 04:56:12 +00:00
|
|
|
const state = self[kStateSymbol];
|
|
|
|
|
2016-01-29 13:18:27 +00:00
|
|
|
if (ex) {
|
|
|
|
if (typeof callback === 'function') {
|
2017-11-22 17:41:00 +00:00
|
|
|
process.nextTick(callback, ex);
|
2016-01-29 13:18:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 17:41:00 +00:00
|
|
|
process.nextTick(() => self.emit('error', ex));
|
2016-01-29 13:18:27 +00:00
|
|
|
return;
|
2018-07-21 04:56:12 +00:00
|
|
|
} else if (!state.handle) {
|
2016-01-29 13:18:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-26 04:21:27 +00:00
|
|
|
const req = new SendWrap();
|
2016-05-17 10:36:54 +00:00
|
|
|
req.list = list; // Keep reference alive.
|
2016-01-29 13:18:27 +00:00
|
|
|
req.address = address;
|
|
|
|
req.port = port;
|
|
|
|
if (callback) {
|
|
|
|
req.callback = callback;
|
|
|
|
req.oncomplete = afterSend;
|
|
|
|
}
|
2017-11-22 17:41:00 +00:00
|
|
|
|
2019-03-16 22:03:48 +00:00
|
|
|
let err;
|
|
|
|
if (port)
|
|
|
|
err = state.handle.send(req, list, list.length, port, ip, !!callback);
|
|
|
|
else
|
|
|
|
err = state.handle.send(req, list, list.length, !!callback);
|
2017-11-22 17:41:00 +00:00
|
|
|
|
2016-01-29 13:18:27 +00:00
|
|
|
if (err && callback) {
|
2018-12-10 12:27:32 +00:00
|
|
|
// Don't emit as error, dgram_legacy.js compatibility
|
2016-01-29 04:08:57 +00:00
|
|
|
const ex = exceptionWithHostPort(err, 'send', address, port);
|
2017-11-22 17:41:00 +00:00
|
|
|
process.nextTick(callback, ex);
|
2016-01-29 13:18:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function afterSend(err, sent) {
|
2015-01-08 19:14:44 +00:00
|
|
|
if (err) {
|
|
|
|
err = exceptionWithHostPort(err, 'send', this.address, this.port);
|
2016-03-28 01:08:29 +00:00
|
|
|
} else {
|
|
|
|
err = null;
|
2015-01-08 19:14:44 +00:00
|
|
|
}
|
2016-03-28 01:08:29 +00:00
|
|
|
|
2016-01-29 13:18:27 +00:00
|
|
|
this.callback(err, sent);
|
2011-08-20 01:47:40 +00:00
|
|
|
}
|
|
|
|
|
2014-12-30 05:30:03 +00:00
|
|
|
Socket.prototype.close = function(callback) {
|
2018-07-21 04:56:12 +00:00
|
|
|
const state = this[kStateSymbol];
|
|
|
|
const queue = state.queue;
|
|
|
|
|
2015-01-26 17:28:18 +00:00
|
|
|
if (typeof callback === 'function')
|
2014-12-30 05:30:03 +00:00
|
|
|
this.on('close', callback);
|
2016-05-30 16:29:27 +00:00
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
if (queue !== undefined) {
|
|
|
|
queue.push(this.close.bind(this));
|
2016-05-30 16:29:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-07-21 03:27:40 +00:00
|
|
|
healthCheck(this);
|
|
|
|
stopReceiving(this);
|
2018-07-21 04:56:12 +00:00
|
|
|
state.handle.close();
|
|
|
|
state.handle = null;
|
2018-03-05 11:20:48 +00:00
|
|
|
defaultTriggerAsyncIdScope(this[async_id_symbol],
|
|
|
|
process.nextTick,
|
|
|
|
socketCloseNT,
|
|
|
|
this);
|
2014-12-29 20:12:30 +00:00
|
|
|
|
|
|
|
return this;
|
2011-08-20 01:47:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 21:07:27 +00:00
|
|
|
function socketCloseNT(self) {
|
|
|
|
self.emit('close');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-20 01:47:40 +00:00
|
|
|
Socket.prototype.address = function() {
|
2018-07-21 03:27:40 +00:00
|
|
|
healthCheck(this);
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2019-03-26 04:21:27 +00:00
|
|
|
const out = {};
|
|
|
|
const err = this[kStateSymbol].handle.getsockname(out);
|
2013-07-18 21:18:50 +00:00
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'getsockname');
|
|
|
|
}
|
2011-08-20 01:47:40 +00:00
|
|
|
|
2013-07-18 21:18:50 +00:00
|
|
|
return out;
|
2011-08-20 01:47:40 +00:00
|
|
|
};
|
|
|
|
|
2019-03-16 22:03:48 +00:00
|
|
|
Socket.prototype.remoteAddress = function() {
|
|
|
|
healthCheck(this);
|
|
|
|
|
|
|
|
const state = this[kStateSymbol];
|
|
|
|
if (state.connectState !== CONNECT_STATE_CONNECTED)
|
|
|
|
throw new ERR_SOCKET_DGRAM_NOT_CONNECTED();
|
|
|
|
|
|
|
|
var out = {};
|
|
|
|
var err = state.handle.getpeername(out);
|
|
|
|
if (err)
|
|
|
|
throw errnoException(err, 'getpeername');
|
|
|
|
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2011-08-20 01:47:40 +00:00
|
|
|
|
|
|
|
Socket.prototype.setBroadcast = function(arg) {
|
2019-03-26 04:21:27 +00:00
|
|
|
const err = this[kStateSymbol].handle.setBroadcast(arg ? 1 : 0);
|
2013-07-18 21:18:50 +00:00
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'setBroadcast');
|
2011-10-27 14:48:38 +00:00
|
|
|
}
|
2011-08-20 01:47:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-17 13:59:20 +00:00
|
|
|
Socket.prototype.setTTL = function(ttl) {
|
2018-08-03 15:23:26 +00:00
|
|
|
validateNumber(ttl, 'ttl');
|
2012-01-23 22:52:08 +00:00
|
|
|
|
2019-03-26 04:21:27 +00:00
|
|
|
const err = this[kStateSymbol].handle.setTTL(ttl);
|
2013-07-18 21:18:50 +00:00
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'setTTL');
|
2012-01-23 22:52:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 13:59:20 +00:00
|
|
|
return ttl;
|
2011-08-20 01:47:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-17 13:59:20 +00:00
|
|
|
Socket.prototype.setMulticastTTL = function(ttl) {
|
2018-08-03 15:23:26 +00:00
|
|
|
validateNumber(ttl, 'ttl');
|
2012-01-23 20:09:56 +00:00
|
|
|
|
2019-03-26 04:21:27 +00:00
|
|
|
const err = this[kStateSymbol].handle.setMulticastTTL(ttl);
|
2013-07-18 21:18:50 +00:00
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'setMulticastTTL');
|
2011-11-22 21:04:40 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 13:59:20 +00:00
|
|
|
return ttl;
|
2011-08-20 01:47:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.setMulticastLoopback = function(arg) {
|
2019-03-26 04:21:27 +00:00
|
|
|
const err = this[kStateSymbol].handle.setMulticastLoopback(arg ? 1 : 0);
|
2013-07-18 21:18:50 +00:00
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'setMulticastLoopback');
|
2012-01-23 22:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return arg; // 0.4 compatibility
|
2011-08-20 01:47:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-07-23 20:30:58 +00:00
|
|
|
Socket.prototype.setMulticastInterface = function(interfaceAddress) {
|
2018-07-21 03:27:40 +00:00
|
|
|
healthCheck(this);
|
2018-08-02 22:51:02 +00:00
|
|
|
validateString(interfaceAddress, 'interfaceAddress');
|
2016-07-23 20:30:58 +00:00
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
const err = this[kStateSymbol].handle.setMulticastInterface(interfaceAddress);
|
2016-07-23 20:30:58 +00:00
|
|
|
if (err) {
|
|
|
|
throw errnoException(err, 'setMulticastInterface');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-20 01:47:40 +00:00
|
|
|
Socket.prototype.addMembership = function(multicastAddress,
|
2011-10-12 17:55:15 +00:00
|
|
|
interfaceAddress) {
|
2018-07-21 03:27:40 +00:00
|
|
|
healthCheck(this);
|
2011-10-12 17:55:15 +00:00
|
|
|
|
|
|
|
if (!multicastAddress) {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_MISSING_ARGS('multicastAddress');
|
2011-10-12 17:55:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
const { handle } = this[kStateSymbol];
|
2019-03-26 04:21:27 +00:00
|
|
|
const err = handle.addMembership(multicastAddress, interfaceAddress);
|
2013-07-18 21:18:50 +00:00
|
|
|
if (err) {
|
2014-08-13 04:03:25 +00:00
|
|
|
throw errnoException(err, 'addMembership');
|
2012-01-23 20:36:48 +00:00
|
|
|
}
|
2011-08-20 01:47:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.dropMembership = function(multicastAddress,
|
2011-10-12 17:55:15 +00:00
|
|
|
interfaceAddress) {
|
2018-07-21 03:27:40 +00:00
|
|
|
healthCheck(this);
|
2011-10-12 17:55:15 +00:00
|
|
|
|
|
|
|
if (!multicastAddress) {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_MISSING_ARGS('multicastAddress');
|
2011-10-12 17:55:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
const { handle } = this[kStateSymbol];
|
2019-03-26 04:21:27 +00:00
|
|
|
const err = handle.dropMembership(multicastAddress, interfaceAddress);
|
2013-07-18 21:18:50 +00:00
|
|
|
if (err) {
|
2014-08-13 04:03:25 +00:00
|
|
|
throw errnoException(err, 'dropMembership');
|
2012-01-23 20:36:48 +00:00
|
|
|
}
|
2011-08-20 01:47:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-07-21 03:27:40 +00:00
|
|
|
function healthCheck(socket) {
|
2018-07-21 04:56:12 +00:00
|
|
|
if (!socket[kStateSymbol].handle) {
|
2017-05-09 18:18:35 +00:00
|
|
|
// Error message from dgram_legacy.js.
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_SOCKET_DGRAM_NOT_RUNNING();
|
2017-05-09 18:18:35 +00:00
|
|
|
}
|
2018-07-21 03:27:40 +00:00
|
|
|
}
|
2011-08-20 01:47:40 +00:00
|
|
|
|
|
|
|
|
2018-07-21 03:27:40 +00:00
|
|
|
function stopReceiving(socket) {
|
2018-07-21 04:56:12 +00:00
|
|
|
const state = socket[kStateSymbol];
|
|
|
|
|
|
|
|
if (!state.receiving)
|
2011-08-20 01:47:40 +00:00
|
|
|
return;
|
|
|
|
|
2018-07-21 04:56:12 +00:00
|
|
|
state.handle.recvStop();
|
|
|
|
state.receiving = false;
|
2018-07-21 03:27:40 +00:00
|
|
|
}
|
2011-08-20 01:47:40 +00:00
|
|
|
|
|
|
|
|
2013-07-18 21:18:50 +00:00
|
|
|
function onMessage(nread, handle, buf, rinfo) {
|
2019-03-26 04:21:27 +00:00
|
|
|
const self = handle[owner_symbol];
|
2013-07-18 21:18:50 +00:00
|
|
|
if (nread < 0) {
|
|
|
|
return self.emit('error', errnoException(nread, 'recvmsg'));
|
2013-02-28 16:50:14 +00:00
|
|
|
}
|
2013-06-21 18:35:29 +00:00
|
|
|
rinfo.size = buf.length; // compatibility
|
|
|
|
self.emit('message', buf, rinfo);
|
2011-08-20 01:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-13 01:26:04 +00:00
|
|
|
Socket.prototype.ref = function() {
|
2018-07-21 04:56:12 +00:00
|
|
|
const handle = this[kStateSymbol].handle;
|
|
|
|
|
|
|
|
if (handle)
|
|
|
|
handle.ref();
|
2015-05-22 16:35:57 +00:00
|
|
|
|
|
|
|
return this;
|
2012-07-13 01:26:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.unref = function() {
|
2018-07-21 04:56:12 +00:00
|
|
|
const handle = this[kStateSymbol].handle;
|
|
|
|
|
|
|
|
if (handle)
|
|
|
|
handle.unref();
|
2015-05-22 16:35:57 +00:00
|
|
|
|
|
|
|
return this;
|
2012-07-13 01:26:04 +00:00
|
|
|
};
|
2017-03-05 16:42:03 +00:00
|
|
|
|
2017-06-11 21:58:53 +00:00
|
|
|
|
|
|
|
Socket.prototype.setRecvBufferSize = function(size) {
|
2017-09-20 04:55:53 +00:00
|
|
|
bufferSize(this, size, RECV_BUFFER);
|
2017-06-11 21:58:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.setSendBufferSize = function(size) {
|
2017-09-20 04:55:53 +00:00
|
|
|
bufferSize(this, size, SEND_BUFFER);
|
2017-06-11 21:58:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.getRecvBufferSize = function() {
|
2017-09-20 04:55:53 +00:00
|
|
|
return bufferSize(this, 0, RECV_BUFFER);
|
2017-06-11 21:58:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Socket.prototype.getSendBufferSize = function() {
|
2017-09-20 04:55:53 +00:00
|
|
|
return bufferSize(this, 0, SEND_BUFFER);
|
2017-06-11 21:58:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-07-21 14:29:39 +00:00
|
|
|
// Deprecated private APIs.
|
2018-07-21 14:29:39 +00:00
|
|
|
Object.defineProperty(Socket.prototype, '_handle', {
|
2019-03-21 15:21:22 +00:00
|
|
|
get: deprecate(function() {
|
2018-07-21 14:29:39 +00:00
|
|
|
return this[kStateSymbol].handle;
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._handle is deprecated', 'DEP0112'),
|
2019-03-21 15:21:22 +00:00
|
|
|
set: deprecate(function(val) {
|
2018-07-21 14:29:39 +00:00
|
|
|
this[kStateSymbol].handle = val;
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._handle is deprecated', 'DEP0112')
|
2018-07-21 14:29:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(Socket.prototype, '_receiving', {
|
2019-03-21 15:21:22 +00:00
|
|
|
get: deprecate(function() {
|
2018-07-21 14:29:39 +00:00
|
|
|
return this[kStateSymbol].receiving;
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._receiving is deprecated', 'DEP0112'),
|
2019-03-21 15:21:22 +00:00
|
|
|
set: deprecate(function(val) {
|
2018-07-21 14:29:39 +00:00
|
|
|
this[kStateSymbol].receiving = val;
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._receiving is deprecated', 'DEP0112')
|
2018-07-21 14:29:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(Socket.prototype, '_bindState', {
|
2019-03-21 15:21:22 +00:00
|
|
|
get: deprecate(function() {
|
2018-07-21 14:29:39 +00:00
|
|
|
return this[kStateSymbol].bindState;
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._bindState is deprecated', 'DEP0112'),
|
2019-03-21 15:21:22 +00:00
|
|
|
set: deprecate(function(val) {
|
2018-07-21 14:29:39 +00:00
|
|
|
this[kStateSymbol].bindState = val;
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._bindState is deprecated', 'DEP0112')
|
2018-07-21 14:29:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(Socket.prototype, '_queue', {
|
2019-03-21 15:21:22 +00:00
|
|
|
get: deprecate(function() {
|
2018-07-21 14:29:39 +00:00
|
|
|
return this[kStateSymbol].queue;
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._queue is deprecated', 'DEP0112'),
|
2019-03-21 15:21:22 +00:00
|
|
|
set: deprecate(function(val) {
|
2018-07-21 14:29:39 +00:00
|
|
|
this[kStateSymbol].queue = val;
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._queue is deprecated', 'DEP0112')
|
2018-07-21 14:29:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(Socket.prototype, '_reuseAddr', {
|
2019-03-21 15:21:22 +00:00
|
|
|
get: deprecate(function() {
|
2018-07-21 14:29:39 +00:00
|
|
|
return this[kStateSymbol].reuseAddr;
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._reuseAddr is deprecated', 'DEP0112'),
|
2019-03-21 15:21:22 +00:00
|
|
|
set: deprecate(function(val) {
|
2018-07-21 14:29:39 +00:00
|
|
|
this[kStateSymbol].reuseAddr = val;
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._reuseAddr is deprecated', 'DEP0112')
|
2018-07-21 14:29:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2019-03-21 15:21:22 +00:00
|
|
|
Socket.prototype._healthCheck = deprecate(function() {
|
2018-07-21 14:29:39 +00:00
|
|
|
healthCheck(this);
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._healthCheck() is deprecated', 'DEP0112');
|
2018-07-21 14:29:39 +00:00
|
|
|
|
|
|
|
|
2019-03-21 15:21:22 +00:00
|
|
|
Socket.prototype._stopReceiving = deprecate(function() {
|
2018-07-21 14:29:39 +00:00
|
|
|
stopReceiving(this);
|
2018-07-21 14:29:39 +00:00
|
|
|
}, 'Socket.prototype._stopReceiving() is deprecated', 'DEP0112');
|
2018-07-21 14:29:39 +00:00
|
|
|
|
|
|
|
|
2018-07-27 12:35:39 +00:00
|
|
|
// Legacy alias on the C++ wrapper object. This is not public API, so we may
|
|
|
|
// want to runtime-deprecate it at some point. There's no hurry, though.
|
|
|
|
Object.defineProperty(UDP.prototype, 'owner', {
|
|
|
|
get() { return this[owner_symbol]; },
|
|
|
|
set(v) { return this[owner_symbol] = v; }
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-03-05 16:42:03 +00:00
|
|
|
module.exports = {
|
2019-03-21 15:21:22 +00:00
|
|
|
_createSocketHandle: deprecate(
|
2018-07-21 14:29:39 +00:00
|
|
|
_createSocketHandle,
|
|
|
|
'dgram._createSocketHandle() is deprecated',
|
|
|
|
'DEP0112'
|
|
|
|
),
|
2017-03-05 16:42:03 +00:00
|
|
|
createSocket,
|
|
|
|
Socket
|
|
|
|
};
|