1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | "use strict";
|
24 |
|
25 | var Header = require('./header'),
|
26 | Question = require('./question'),
|
27 | ResourceRecord = require('./resourcerecord');
|
28 |
|
29 | var Packet = exports.Packet = function (socket) {
|
30 | this._socket = socket;
|
31 |
|
32 | this.raw_ = undefined;
|
33 |
|
34 | this.label_index_ = {};
|
35 |
|
36 | this.header = new Header();
|
37 |
|
38 | Object.defineProperty(this, 'rcode', {
|
39 | get: function () {
|
40 | return this.header.rcode;
|
41 | },
|
42 | set: function (value) {
|
43 | this.header.rcode = value;
|
44 | },
|
45 | configurable: true,
|
46 | });
|
47 |
|
48 | this.clearResources();
|
49 | };
|
50 |
|
51 | Packet.prototype.clearResources = function () {
|
52 | this.question = [];
|
53 | this.answer = [];
|
54 | this.authority = [];
|
55 | this.additional = [];
|
56 | };
|
57 |
|
58 | Packet.prototype.estimateSize = function () {
|
59 | var size = this.header.estimateSize();
|
60 |
|
61 | var estimate = function (rr) {
|
62 | size += rr.estimateSize();
|
63 | };
|
64 |
|
65 | this.question.forEach(estimate);
|
66 | this.answer.forEach(estimate);
|
67 | this.authority.forEach(estimate);
|
68 | this.additional.forEach(estimate);
|
69 |
|
70 | size += 4;
|
71 | return size;
|
72 | };
|
73 |
|
74 | Packet.prototype.send = function () {
|
75 | var buff = new Buffer(this.estimateSize());
|
76 | var len, pbuff;
|
77 |
|
78 | if (this._socket.tcp) {
|
79 | pbuff = buff.slice(2);
|
80 | } else {
|
81 | pbuff = buff;
|
82 | }
|
83 |
|
84 | len = this.pack(pbuff, 0);
|
85 |
|
86 | if (this._socket.tcp) {
|
87 | buff.writeUInt16BE(len, 0);
|
88 | len += 2;
|
89 | }
|
90 |
|
91 | this._socket.send(buff.slice(0, len));
|
92 | };
|
93 |
|
94 | Packet.prototype.pack = function (buff, pos) {
|
95 | var message, append, spos = pos, self = this;
|
96 |
|
97 | this.header.qdcount = this.question.length;
|
98 | this.header.ancount = this.answer.length;
|
99 | this.header.nscount = this.authority.length;
|
100 | this.header.arcount = this.additional.length;
|
101 |
|
102 | pos += this.header.pack(buff, pos);
|
103 |
|
104 | append = function (a) {
|
105 | a.parent = self;
|
106 | pos += a.pack(buff, pos);
|
107 | };
|
108 |
|
109 | this.question.forEach(append);
|
110 | this.answer.forEach(append);
|
111 | this.authority.forEach(append);
|
112 | this.additional.forEach(append);
|
113 |
|
114 | return pos - spos;
|
115 | };
|
116 |
|
117 | Packet.prototype.unpack = function (msg, promote) {
|
118 | var pos = 0, parse_section, read;
|
119 |
|
120 | msg = new Buffer(msg);
|
121 | this.raw_ = msg;
|
122 |
|
123 | this.header = new Header();
|
124 | read = this.header.unpack(msg, pos);
|
125 | pos += read;
|
126 |
|
127 | parse_section = function (count, Type, p) {
|
128 | var i, t, read, ret = [];
|
129 |
|
130 | for (i = 0; i < count; i++) {
|
131 | t = new Type();
|
132 | read = t.unpack(msg, pos);
|
133 | pos += read;
|
134 |
|
135 | if (p) {
|
136 | t = t.promote();
|
137 | }
|
138 |
|
139 | ret.push(t);
|
140 | }
|
141 |
|
142 | return ret;
|
143 | };
|
144 |
|
145 | this.question = parse_section(this.header.qdcount, Question);
|
146 | this.answer = parse_section(this.header.ancount, ResourceRecord, promote);
|
147 | this.authority = parse_section(this.header.nscount, ResourceRecord, promote);
|
148 | this.additional = parse_section(this.header.arcount, ResourceRecord, promote);
|
149 | };
|
150 |
|
151 | Packet.prototype.isEDNS = function () {
|
152 | return false;
|
153 | };
|
154 |
|
155 | Packet.prototype.promote = function () {
|
156 | return this;
|
157 | };
|
158 |
|
159 | Packet.prototype.compare = function (obj) {
|
160 | var same = true, self = this;
|
161 |
|
162 | same = this.header.compare(obj.header);
|
163 |
|
164 | var cmp = function (field, rr, idx) {
|
165 | if (same) {
|
166 | same = rr.compare(obj[field][idx]);
|
167 | if (!same) {
|
168 | console.log('mismatch', field);
|
169 | }
|
170 | }
|
171 | };
|
172 |
|
173 | this.question.forEach(function (rr, idx) { cmp('question', rr, idx); });
|
174 | this.answer.forEach(function (rr, idx) { cmp('answer', rr, idx); });
|
175 | this.authority.forEach(function (rr, idx) { cmp('authority', rr, idx); });
|
176 | this.additional.forEach(function (rr, idx) { cmp('additional', rr, idx); });
|
177 |
|
178 | return same;
|
179 | };
|
180 |
|
181 | module.exports = Packet;
|