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

Dynamically detect v4/v6 #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

mkg20001
Copy link

@mkg20001 mkg20001 commented Jun 2, 2018

By default an IPv6-only socket is created which makes it impossible to use this server with IPv4 hosts.
I've added a bit of code that checks the IP version via regex and creates the appropriate socket for it

@@ -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.

@@ -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".)

@eqyiel eqyiel mentioned this pull request Oct 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants