diff --git a/lib/server.js b/lib/server.js index fd59f52..cf9f0ed 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,4 +1,5 @@ var assert = require('assert'); +var net = require('net'); var dgram = require('dgram'); var EventEmitter = require('events').EventEmitter; var util = require('util'); @@ -6,7 +7,9 @@ var util = require('util'); var Query = require('./query'); var DnsError = require('./errors'); - +function getFamily(ipVersion) { + return (ipVersion == 4) ? 'udp4' : 'udp6'; +} ///--- Globals @@ -50,7 +53,13 @@ Server.prototype.listen = function listen(port, address, callback) { var self = this; - this._socket = dgram.createSocket('udp6'); + var ip_version = net.isIP(address); + var ip_family = getFamily(ip_version); + + if (!ip_version) + self.emit('error', new Error("Invalid IP address")); + + this._socket = dgram.createSocket(ip_family); this._socket.once('listening', function () { self.emit('listening'); if (typeof (callback) === 'function') @@ -71,7 +80,7 @@ Server.prototype.listen = function listen(port, address, callback) { }; var src = { - family: 'udp6', + family: ip_family, address: rinfo.address, port: rinfo.port };