Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move Packet.toString logic into examples/request

  • Loading branch information...
commit 9a3511cf5983b95785a571400e8eabf242aecb48 1 parent 92f4e5a
@tjfontaine authored
Showing with 92 additions and 91 deletions.
  1. +92 −1 examples/request.js
  2. +0 −90 lib/packet.js
View
93 examples/request.js
@@ -19,7 +19,7 @@ req.on('timeout', function () {
});
req.on('message', function (err, answer) {
- console.log(answer.toString());
+ console.log(toDig.call(answer));
});
req.on('end', function () {
@@ -28,3 +28,94 @@ req.on('end', function () {
});
req.send();
+
+toDig = function() {
+ var ret = [], tmp, flags = [];
+
+ tmp = ';; ->>HEADER<<- opcode: ';
+ switch (this.header.opcode) {
+ case 0:
+ tmp += 'QUERY';
+ break;
+ case 1:
+ tmp += 'IQUERY';
+ break;
+ case 2:
+ tmp += 'STATUS';
+ break;
+ default:
+ tmp += 'UNKNOWN';
+ break;
+ }
+ tmp += ', status: ' + dns.consts.RCODE_TO_NAME[this.header.rcode];
+ tmp += ', id: ' + this.header.id;
+ ret.push(tmp);
+
+ tmp = ';; flags: ';
+
+ if (this.header.qr)
+ flags.push('qr');
+ if (this.header.rd)
+ flags.push('rd');
+ if (this.header.aa)
+ flags.push('aa');
+ if (this.header.tc)
+ flags.push('tc');
+ if (this.header.ra)
+ flags.push('ra');
+
+ tmp += flags.join(' ') + ';';
+
+ tmp += ' QUESTON: ' + this.question.length;
+ tmp += ', ANSWER: ' + this.answer.length;
+ tmp += ', AUTHORITY: ' + this.authority.length;
+ tmp += ', ADDITIONAL: ' + this.additional.length;
+
+ ret.push(tmp);
+ ret.push('');
+
+ var pushit = function(p) {
+ ret.push([
+ p.name,
+ dns.consts.QCLASS_TO_NAME[p.class],
+ dns.consts.QTYPE_TO_NAME[p.type],
+ p.address || p.data || '',
+ ].join('\t'));
+ };
+
+ if (this.question.length) {
+ ret.push(';; QUESTION SECTION:');
+ this.question.forEach(function(q) {
+ ret.push('; ' + [q.name,
+ dns.consts.QCLASS_TO_NAME[q.class],
+ dns.consts.QTYPE_TO_NAME[q.type]
+ ].join('\t'));
+ });
+ ret.push('');
+ }
+
+ if (this.answer.length) {
+ ret.push(';; ANSWER SECTION:');
+ this.answer.forEach(pushit);
+ ret.push('');
+ }
+
+ if (this.authority.length) {
+ ret.push(';; AUTHORITY SECTION:');
+ this.authority.forEach(pushit);
+ ret.push('');
+ }
+
+ if (this.additional.length) {
+ if (this.additional[0].type !== dns.consts.NAME_TO_QTYPE.OPT) {
+ ret.push(';; ADDITIONAL SECTION:');
+ this.additional.forEach(pushit);
+ ret.push('');
+ }
+ }
+
+ ret.push(';; END');
+
+ return ret.join('\n');
+};
+
View
90 lib/packet.js
@@ -66,96 +66,6 @@ Packet.prototype.send = function() {
this._socket.send(buff.slice(0, len));
};
-Packet.prototype.toString = function() {
- var ret = [], tmp, flags = [];
-
- tmp = ';; ->>HEADER<<- opcode: ';
- switch (this.header.opcode) {
- case 0:
- tmp += 'QUERY';
- break;
- case 1:
- tmp += 'IQUERY';
- break;
- case 2:
- tmp += 'STATUS';
- break;
- default:
- tmp += 'UNKNOWN';
- break;
- }
- tmp += ', status: ' + consts.RCODE_TO_NAME[this.header.rcode];
- tmp += ', id: ' + this.header.id;
- ret.push(tmp);
-
- tmp = ';; flags: ';
-
- if (this.header.qr)
- flags.push('qr');
- if (this.header.rd)
- flags.push('rd');
- if (this.header.aa)
- flags.push('aa');
- if (this.header.tc)
- flags.push('tc');
- if (this.header.ra)
- flags.push('ra');
-
- tmp += flags.join(' ') + ';';
-
- tmp += ' QUESTON: ' + this.question.length;
- tmp += ', ANSWER: ' + this.answer.length;
- tmp += ', AUTHORITY: ' + this.authority.length;
- tmp += ', ADDITIONAL: ' + this.additional.length;
-
- ret.push(tmp);
- ret.push('');
-
- var pushit = function(p) {
- ret.push([
- p.name,
- consts.QCLASS_TO_NAME[p.class],
- consts.QTYPE_TO_NAME[p.type],
- p.address || p.data || '',
- ].join('\t'));
- };
-
- if (this.question.length) {
- ret.push(';; QUESTION SECTION:');
- this.question.forEach(function(q) {
- ret.push('; ' + [q.name,
- consts.QCLASS_TO_NAME[q.class],
- consts.QTYPE_TO_NAME[q.type]
- ].join('\t'));
- });
- ret.push('');
- }
-
- if (this.answer.length) {
- ret.push(';; ANSWER SECTION:');
- this.answer.forEach(pushit);
- ret.push('');
- }
-
- if (this.authority.length) {
- ret.push(';; AUTHORITY SECTION:');
- this.authority.forEach(pushit);
- ret.push('');
- }
-
- if (this.additional.length) {
- if (this.additional[0].type !== consts.NAME_TO_QTYPE.OPT) {
- ret.push(';; ADDITIONAL SECTION:');
- this.additional.forEach(pushit);
- ret.push('');
- }
- }
-
- ret.push(';; END');
-
- return ret.join('\n');
-};
-
/*
var EDNSPacket = exports.EDNSPacket = function(socket, rinfo) {
Packet.call(this, socket, rinfo);
Please sign in to comment.
Something went wrong with that request. Please try again.