UNPKG

4.55 kBJavaScriptView Raw
1/*
2Copyright 2011 Timothy J Fontaine <tjfontaine@gmail.com>
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
21*/
22
23"use strict";
24
25var Header = require('./header'),
26 Question = require('./question'),
27 ResourceRecord = require('./resourcerecord');
28
29var 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
51Packet.prototype.clearResources = function () {
52 this.question = [];
53 this.answer = [];
54 this.authority = [];
55 this.additional = [];
56};
57
58Packet.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
74Packet.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
94Packet.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
117Packet.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
151Packet.prototype.isEDNS = function () {
152 return false;
153};
154
155Packet.prototype.promote = function () {
156 return this;
157};
158
159Packet.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
181module.exports = Packet;