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 ResourceRecord = require('./resourcerecord'),
|
26 | ipaddr = require('ipaddr.js'),
|
27 | util = require('util'),
|
28 | fields = require('./fields'),
|
29 | consts = require('./consts'),
|
30 | name = require('./name');
|
31 |
|
32 | var RDataType = function (vals, type) {
|
33 | ResourceRecord.call(this, vals);
|
34 | this.type = type;
|
35 | };
|
36 | util.inherits(RDataType, ResourceRecord);
|
37 |
|
38 | var TypeMap = module.exports = function () {
|
39 | this.exported = {};
|
40 | this.map = {};
|
41 | };
|
42 |
|
43 | TypeMap.prototype.registerType = function (name, fields) {
|
44 | var newType = function (vals) {
|
45 | this._rdata_fields = fields;
|
46 | RDataType.call(this, vals, consts.NAME_TO_QTYPE[name]);
|
47 | };
|
48 | util.inherits(newType, RDataType);
|
49 | this.map[consts.NAME_TO_QTYPE[name]] = newType;
|
50 | this.exported[name] = newType;
|
51 | };
|
52 |
|
53 | TypeMap.prototype.fromQtype = function (qtype) {
|
54 | return this.map[qtype];
|
55 | };
|
56 |
|
57 | var types = new TypeMap();
|
58 |
|
59 | types.registerType('SOA',
|
60 | [
|
61 | fields.Label('primary'),
|
62 | fields.Label('admin'),
|
63 | fields.Struct('serial', 'I'),
|
64 | fields.Struct('refresh', 'i'),
|
65 | fields.Struct('retry', 'i'),
|
66 | fields.Struct('expiration', 'i'),
|
67 | fields.Struct('minimum', 'i'),
|
68 | ]);
|
69 |
|
70 | types.registerType('A', [fields.IPAddress('address', 4)]);
|
71 | types.registerType('AAAA', [fields.IPAddress('address', 6)]);
|
72 |
|
73 | types.registerType('MX',
|
74 | [
|
75 | fields.Struct('priority', 'H'),
|
76 | fields.Label('exchange'),
|
77 | ]);
|
78 |
|
79 | types.registerType('TXT', [fields.CharString('data')]);
|
80 |
|
81 | types.registerType('SRV',
|
82 | [
|
83 | fields.Struct('priority', 'H'),
|
84 | fields.Struct('weight', 'H'),
|
85 | fields.Struct('port', 'H'),
|
86 | fields.Label('target'),
|
87 | ]);
|
88 |
|
89 | types.registerType('NS', [ fields.Label('data'), ]);
|
90 | types.registerType('CNAME', [ fields.Label('data'), ]);
|
91 | types.registerType('PTR', [ fields.Label('data') ]);
|
92 |
|
93 | ResourceRecord.prototype.promote = function () {
|
94 | var Type = types.fromQtype(this.type), new_type;
|
95 |
|
96 | if (!Type) {
|
97 | console.log("couldn't promote type:", this.type);
|
98 | return this;
|
99 | }
|
100 |
|
101 | new_type = new Type();
|
102 | new_type.unpack(this.raw_, this.record_position_);
|
103 | new_type.unpackRData();
|
104 |
|
105 | return new_type;
|
106 | };
|
107 |
|
108 | module.exports = types;
|