Skip to content
Open
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
16 changes: 14 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ var ExceptionError = DnsError.ExceptionError;
var ProtocolError = DnsError.ProtocolError;


///--- IP regex

const ip4re = /^(\d{1,3}\.){3,3}\d{1,3}$/
const ip6re = /^(::)?(((\d{1,3}\.){3}(\d{1,3}){1})?([0-9a-f]){0,4}:{0,2}){1,8}(::)?$/i


///--- API

Expand Down Expand Up @@ -48,9 +53,16 @@ Server.prototype.listen = function listen(port, address, callback) {
address = '0.0.0.0';
}

let fam = ''
Copy link

@zbjornson zbjornson Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.js's "net" module (since v0.3.0) has this ability built in, no need for the regexes:

const net = require("net");
const ipversion = net.isIP(address);
if (ipversion === 0) throw new TypeError("address is neither valid v4 or v6");
const fam = ipversion === 4 ? "udp4" : "upd6";

https://nodejs.org/api/net.html#net_net_isip_input


edit This library already uses that actually:

IPv4: function(v) {
return net.isIPv4(v);
},
IPv6: function(v) {
return net.isIPv6(v);
},


edit 2 #26 and #29 make an equivalent fix using the net module.


if (address.match(ip4re)) fam = 'udp4'
if (!fam && address.match(ip6re)) fam = 'udp6'

if (!fam) throw new TypeError('IP is neither valid v4 or v6')

var self = this;

this._socket = dgram.createSocket('udp6');
this._socket = dgram.createSocket(fam);
this._socket.once('listening', function () {
self.emit('listening');
if (typeof (callback) === 'function')
Expand All @@ -71,7 +83,7 @@ Server.prototype.listen = function listen(port, address, callback) {
};

var src = {
family: 'udp6',
family: fam,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I think this will match the socket's type, but you could also make this rinfo.family === "IPv6" ? 'udp6' : "udp4".)

address: rinfo.address,
port: rinfo.port
};
Expand Down