-
Notifications
You must be signed in to change notification settings - Fork 100
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
base: master
Are you sure you want to change the base?
Conversation
@@ -48,9 +53,16 @@ Server.prototype.listen = function listen(port, address, callback) { | |||
address = '0.0.0.0'; | |||
} | |||
|
|||
let fam = '' |
There was a problem hiding this comment.
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:
Lines 57 to 62 in 2b1adaa
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, |
There was a problem hiding this comment.
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"
.)
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