Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
```
Expand All @@ -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)
Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions examples/reflect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 0 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
// });
3 changes: 3 additions & 0 deletions lib/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions lib/records/ptr.js
Original file line number Diff line number Diff line change
@@ -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);
};
12 changes: 10 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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')
Expand All @@ -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
};
Expand Down
17 changes: 17 additions & 0 deletions test/named.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions test/records.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down