Skip to content
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

added address option #2

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ const net = require('net')

exports.udp = async function bindUDP (ports = 0, opts = {}) {
const allowAny = opts.allowAny !== false
const address = opts.address || ''
const socket = dgram.createSocket(opts.ipv6 ? 'udp6' : 'udp4')

let error = null

for (const port of expandPorts(ports, allowAny, allowAny)) {
try {
await bind(socket, false, port)
await bind(socket, false, port, address)
return socket
} catch (err) {
error = err
Expand All @@ -21,13 +22,14 @@ exports.udp = async function bindUDP (ports = 0, opts = {}) {

exports.tcp = async function bindTCP (ports = 0, opts = {}) {
const allowAny = opts.allowAny !== false
const address = opts.address || ''
const server = net.createServer()

let error = null

for (const port of expandPorts(ports, allowAny, allowAny)) {
try {
await bind(server, true, port)
await bind(server, true, port, address)
return server
} catch (err) {
error = err
Expand All @@ -39,6 +41,7 @@ exports.tcp = async function bindTCP (ports = 0, opts = {}) {

exports.dual = async function bindDual (ports = 0, opts = {}) {
const allowAny = opts.allowAny !== false
const address = opts.address || ''
const type = opts.ipv6 ? 'udp6' : 'udp4'

let server = net.createServer()
Expand All @@ -47,14 +50,14 @@ exports.dual = async function bindDual (ports = 0, opts = {}) {

for (const port of expandPorts(ports, allowAny, false)) {
try {
await bind(socket, false, port)
await bind(socket, false, port, address)
} catch (err) {
error = err
continue
}

try {
await bind(server, true, socket.address().port)
await bind(server, true, socket.address().port, address)
} catch (err) {
error = err
await close(socket)
Expand All @@ -68,10 +71,10 @@ exports.dual = async function bindDual (ports = 0, opts = {}) {
if (allowAny) {
for (let i = 0; i < 5; i++) {
// First try free udp port
await bind(socket, false, 0)
await bind(socket, false, 0, address)

try {
await bind(server, true, socket.address().port)
await bind(server, true, socket.address().port, address)
} catch (err) {
error = err
await close(socket)
Expand All @@ -81,7 +84,7 @@ exports.dual = async function bindDual (ports = 0, opts = {}) {
await bind(server, true, 0)

try {
await bind(socket, false, server.address().port)
await bind(socket, false, server.address().port, address)
} catch (err) {
error = err
await close(server)
Expand Down Expand Up @@ -120,13 +123,13 @@ function close (server) {
})
}

function bind (socket, isTCP, port) {
function bind (socket, isTCP, port, address) {
return new Promise(function (resolve, reject) {
socket.on('listening', onlistening)
socket.on('error', done)

if (isTCP) socket.listen(port)
else socket.bind(port)
if (isTCP) socket.listen(port, address)
else socket.bind(port, address)

function onlistening () {
done(null)
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ test('dual - range taken but allowAny', async function (t) {
socket.close()
})

test('dual - with address', async function (t) {
const { server, socket } = await bind.dual(0, { address: '127.0.0.1' })

t.is(server.address().port, socket.address().port)
t.is(server.address().address, socket.address().address)
t.ok(server.address().port > 0, 'is bound')
t.is(server.address().address, '127.0.0.1')

server.close()
socket.close()
})

test('all taken, no allowAny', async function (t) {
t.exception(bind.udp([], { allowAny: false }))
t.exception(bind.tcp([], { allowAny: false }))
Expand Down