diff --git a/README.md b/README.md index 1d4e911..0b62d2b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Node-named is a lightweight DNS server written in pure javascript. It has limited support for the DNS spec, but aims to implement all of the *common* -functionality that is in use today. +functionality that is in use today. ** This project is not actively maintained ** I've received a lot of great PRs for this project, but I don't have the capacity to actively maintain this library at the moment. I feel strongly about maintaining backwards compatibility for people who rely on it, so any PRs would also need to adhere to keeping the API sane, or contribute to some improvement in performance. @@ -29,15 +29,15 @@ server.on('query', function(query) { ``` ## Creating DNS Records -node-named provides helper functions for creating DNS records. +node-named provides helper functions for creating DNS records. The records are available under 'named.record.NAME' where NAME is one -of ['A', 'AAAA', 'CNAME', 'SOA', 'MX', 'NS', 'TXT, 'SRV']. It is important to -remember that these DNS records are not permanently added to the server. +of ['A', 'AAAA', 'CNAME', 'PTR', 'SOA', 'MX', 'NS', 'TXT, 'SRV']. It is important to +remember that these DNS records are not permanently added to the server. They only exist for the length of the particular request. After that, they are destroyed. This means you have to create your own lookup mechanism. ```javascript var named = require('node-named'); - + var soaRecord = new named.SOARecord('example.com', {serial: 201205150000}); console.log(soaRecord); ``` @@ -48,6 +48,7 @@ The following record types are supported * A (ipv4) * AAAA (ipv6) * CNAME (aliases) + * PTR (reverse lookups) * SOA (start of authority) * MX (mail server records) * NS (nameserver entries) @@ -84,5 +85,5 @@ basis). ## Looking up Records -There are a few handy ways to lookup DNS records in node. +There are a few handy ways to lookup DNS records in node. https://github.com/LCMApps/dns-lookup-cache diff --git a/docs/index.md b/docs/index.md index 455b054..44d19a1 100644 --- a/docs/index.md +++ b/docs/index.md @@ -243,6 +243,11 @@ Create an IPv6 resource record. Create an Alias record. When these records are sent to the client, the client will often make an additional request for the alias itself. +### named.PTRRecord(target) + +Create an Alias record. When these records are sent to the client, the client +will often make an additional request for the alias itself. + ### named.MXRecord(exchange, options) Create a Mail Server record. A client making this request will often make an diff --git a/examples/reflect.js b/examples/reflect.js index 8f50a21..b61eefe 100644 --- a/examples/reflect.js +++ b/examples/reflect.js @@ -22,6 +22,10 @@ server.on('query', function(query) { var record = new named.CNAMERecord('cname.example.com'); query.addAnswer(domain, record, 300); break; + case 'PTR': + var record = new named.PTRRecord('ptr.example.com'); + query.addAnswer(domain, record, 300); + break; case 'MX': var record = new named.MXRecord('smtp.example.com'); query.addAnswer(domain, record, 300); diff --git a/lib/index.js b/lib/index.js index 343da57..edd9c1e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -62,16 +62,3 @@ fs.readdirSync(subdir).forEach(function (f) { module.exports[k] = require(path.join(subdir, f)); } }); -// [ -// 'A', -// 'MX', -// 'SOA', -// 'SRV', -// 'TXT', -// 'AAAA', -// 'CNAME' -// ].forEach(function (r) { -// var lcr = r.toLowerCase(); -// var k = lcr.charAt(0).toUpperCase() + lcr.slice(1) + 'Record'; -// module.exports[k] = require(path.join(__dirname, 'records/' + lcr))[r]; -// }); diff --git a/lib/protocol.js b/lib/protocol.js index 9d4879b..604f744 100644 --- a/lib/protocol.js +++ b/lib/protocol.js @@ -290,6 +290,9 @@ var serializers = { case queryTypes['CNAME']: r = serializers['_nsName'].encoder(v.target); break; + case queryTypes['PTR']: + r = serializers['_nsName'].encoder(v.target); + break; case queryTypes['NS']: r = serializers['_nsName'].encoder(v.target); break; diff --git a/lib/query.js b/lib/query.js index 04ecaab..b42d865 100644 --- a/lib/query.js +++ b/lib/query.js @@ -72,8 +72,10 @@ Query.prototype.encode = function encode() { flags: this._flags, qdCount: this._qdCount, anCount: this._anCount, - nsCount: this._nsCount, - srCount: this._srCount + // nsCount: this._nsCount, + // srCount: this._srCount + nsCount: this._authority ? this._authority.length : 0, + srCount: this._additional ? this._additional.length : 0 }, question: this._question, answers: this._answers, diff --git a/lib/records/ptr.js b/lib/records/ptr.js new file mode 100644 index 0000000..552bad5 --- /dev/null +++ b/lib/records/ptr.js @@ -0,0 +1,21 @@ +var validators = require('../validators'); + + + +function PTR(target) { + if (typeof(target) !== 'string') + throw new TypeError('target (string) is required'); + + this.target = target; + this._type = 'PTR'; +} +module.exports = PTR; + + +PTR.prototype.valid = function valid() { + var self = this, model = {}; + model = { + target: validators.nsName + }; + return validators.validate(self, model); +}; diff --git a/lib/server.js b/lib/server.js index fd59f52..987e608 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,5 +1,6 @@ var assert = require('assert'); var dgram = require('dgram'); +var net = require('net'); var EventEmitter = require('events').EventEmitter; var util = require('util'); @@ -47,10 +48,17 @@ Server.prototype.listen = function listen(port, address, callback) { callback = address; address = '0.0.0.0'; } + + var address_type = net.isIP(address); + + if(!address_type) + throw new TypeError('ip address is invalid'); + + var socket_type = address_type == 4 ? 'udp4' : 'udp6' var self = this; - this._socket = dgram.createSocket('udp6'); + this._socket = dgram.createSocket(socket_type); this._socket.once('listening', function () { self.emit('listening'); if (typeof (callback) === 'function') @@ -71,7 +79,7 @@ Server.prototype.listen = function listen(port, address, callback) { }; var src = { - family: 'udp6', + family: socket_type, address: rinfo.address, port: rinfo.port }; diff --git a/test/named.test.js b/test/named.test.js index 03a13b7..96f0c1f 100644 --- a/test/named.test.js +++ b/test/named.test.js @@ -41,6 +41,10 @@ before(function (callback) { var record = new named.CNAMERecord('cname.example.com'); query.addAnswer(domain, record); break; + case 'PTR': + var record = new named.PTRRecord('ptr.example.com'); + query.addAnswer(domain, record); + break; case 'NS': var record = new named.NSRecord('ns.example.com'); query.addAnswer(domain, record); @@ -140,6 +144,19 @@ test('answer query: example.com (CNAME)', function (t) { }); }); +test('answer query: example.com (PTR)', function (t) { + dig('example.com', 'PTR', options, function (err, results) { + t.ifError(err); + t.deepEqual(results.answers, [{ + name: 'example.com.', + ttl: 5, + type: 'PTR', + target: 'ptr.example.com.' + }]); + t.end(); + }); +}); + test('answer query: example.com (NS)', function (t) { dig('example.com', 'NS', options, function (err, results) { t.ifError(err); diff --git a/test/records.test.js b/test/records.test.js index dbe396d..6848a46 100644 --- a/test/records.test.js +++ b/test/records.test.js @@ -35,6 +35,11 @@ test('create a valid record (CNAME)', function(t) { testRecord(record, t); }); +test('create a valid record (PTR)', function(t) { + var record = new named.PTRRecord('ptr.example.com'); + testRecord(record, t); +}); + test('create a valid record (NS)', function(t) { var record = new named.NSRecord('ns.example.com'); testRecord(record, t);