Exporting SerializeName for usage in RR data

This commit is contained in:
Fabian Stamm 2018-05-13 22:27:16 +02:00
parent 8c66f9a41b
commit e5c55b402b
5 changed files with 51 additions and 50 deletions

2
lib/request.d.ts vendored
View File

@ -90,6 +90,7 @@ export declare enum QueryTypes {
*/ */
ANY = 255, ANY = 255,
} }
export declare function SerializeName(name: string): Buffer;
export declare class Request implements Message { export declare class Request implements Message {
private sendCallback; private sendCallback;
_header: MessageHeader; _header: MessageHeader;
@ -107,7 +108,6 @@ export declare class Request implements Message {
private serializeHeader(); private serializeHeader();
private serializeQuestion(question); private serializeQuestion(question);
private serializeResourceRecord(record); private serializeResourceRecord(record);
private serializeName(name);
} }
export declare class RecourceRecord implements MessageRecourceRecord { export declare class RecourceRecord implements MessageRecourceRecord {
NAME: string; NAME: string;

View File

@ -140,6 +140,28 @@ function parseQuestions(count, packet) {
length: count length: count
}).parse(packet).questions; }).parse(packet).questions;
} }
function SerializeName(name) {
let length = 0;
let parts = name.split(".");
parts.forEach(e => {
// Length of part and byte that holds the length information
if (e.length > MAX_LABEL_SIZE)
throw new Error("Label to large");
length += e.length + 1;
});
length += 1; //Adding last 0 length octet
let data = Buffer.alloc(length);
let offset = 0;
parts.forEach(e => {
data.writeUInt8(e.length, offset);
offset++;
data.write(e, offset, e.length);
offset += e.length;
});
data.writeUInt8(0, offset);
return data;
}
exports.SerializeName = SerializeName;
class Request { class Request {
constructor(packet, sendCallback) { constructor(packet, sendCallback) {
this.sendCallback = sendCallback; this.sendCallback = sendCallback;
@ -232,7 +254,7 @@ class Request {
return data; return data;
} }
serializeQuestion(question) { serializeQuestion(question) {
let qname = this.serializeName(question.QNAME); let qname = SerializeName(question.QNAME);
let data = Buffer.alloc(qname.length + 4); let data = Buffer.alloc(qname.length + 4);
qname.copy(data, 0, 0, qname.length); qname.copy(data, 0, 0, qname.length);
let offset = qname.length; let offset = qname.length;
@ -243,7 +265,7 @@ class Request {
} }
serializeResourceRecord(record) { serializeResourceRecord(record) {
// TODO: Implement compression // TODO: Implement compression
let name = this.serializeName(record.NAME); let name = SerializeName(record.NAME);
let data = Buffer.alloc(name.length + 10 + record.RDLENGTH); // For TYPE, CLASS, TTL, RLENGTH let data = Buffer.alloc(name.length + 10 + record.RDLENGTH); // For TYPE, CLASS, TTL, RLENGTH
name.copy(data, 0, 0, name.length); name.copy(data, 0, 0, name.length);
let offset = name.length; let offset = name.length;
@ -258,27 +280,6 @@ class Request {
record.RDATA.copy(data, offset, 0, record.RDLENGTH); record.RDATA.copy(data, offset, 0, record.RDLENGTH);
return data; return data;
} }
serializeName(name) {
let length = 0;
let parts = name.split(".");
parts.forEach(e => {
// Length of part and byte that holds the length information
if (e.length > MAX_LABEL_SIZE)
throw new Error("Label to large");
length += e.length + 1;
});
length += 1; //Adding last 0 length octet
let data = Buffer.alloc(length);
let offset = 0;
parts.forEach(e => {
data.writeUInt8(e.length, offset);
offset++;
data.write(e, offset, e.length);
offset += e.length;
});
data.writeUInt8(0, offset);
return data;
}
} }
exports.Request = Request; exports.Request = Request;
class RecourceRecord { class RecourceRecord {

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{ {
"name": "nodename-core", "name": "nodename-core",
"version": "0.1.6", "version": "0.1.7",
"main": "lib/listener.js", "main": "lib/listener.js",
"types": "lib/listener.d.ts", "types": "lib/listener.d.ts",
"author": "Fabian Stamm <dev@fabianstamm.de>", "author": "Fabian Stamm <dev@fabianstamm.de>",

View File

@ -165,6 +165,28 @@ function parseQuestions(count: number, packet: Buffer): MessageQuestion[] {
}).parse(packet).questions; }).parse(packet).questions;
} }
export function SerializeName(name: string) {
let length = 0;
let parts = name.split(".");
parts.forEach(e => {
// Length of part and byte that holds the length information
if (e.length > MAX_LABEL_SIZE) throw new Error("Label to large");
length += e.length + 1;
})
length += 1; //Adding last 0 length octet
let data = Buffer.alloc(length);
let offset = 0;
parts.forEach(e => {
data.writeUInt8(e.length, offset)
offset++
data.write(e, offset, e.length)
offset += e.length
})
data.writeUInt8(0, offset);
return data;
}
export class Request implements Message { export class Request implements Message {
_header: MessageHeader; _header: MessageHeader;
get header() { get header() {
@ -280,7 +302,7 @@ export class Request implements Message {
} }
private serializeQuestion(question: MessageQuestion) { private serializeQuestion(question: MessageQuestion) {
let qname = this.serializeName(question.QNAME); let qname = SerializeName(question.QNAME);
let data = Buffer.alloc(qname.length + 4); let data = Buffer.alloc(qname.length + 4);
qname.copy(data, 0, 0, qname.length); qname.copy(data, 0, 0, qname.length);
let offset = qname.length; let offset = qname.length;
@ -292,7 +314,7 @@ export class Request implements Message {
private serializeResourceRecord(record: MessageRecourceRecord) { private serializeResourceRecord(record: MessageRecourceRecord) {
// TODO: Implement compression // TODO: Implement compression
let name = this.serializeName(record.NAME); let name = SerializeName(record.NAME);
let data = Buffer.alloc(name.length + 10 + record.RDLENGTH) // For TYPE, CLASS, TTL, RLENGTH let data = Buffer.alloc(name.length + 10 + record.RDLENGTH) // For TYPE, CLASS, TTL, RLENGTH
name.copy(data, 0, 0, name.length); name.copy(data, 0, 0, name.length);
let offset = name.length; let offset = name.length;
@ -307,28 +329,6 @@ export class Request implements Message {
record.RDATA.copy(data, offset, 0, record.RDLENGTH) record.RDATA.copy(data, offset, 0, record.RDLENGTH)
return data; return data;
} }
private serializeName(name: string) {
let length = 0;
let parts = name.split(".");
parts.forEach(e => {
// Length of part and byte that holds the length information
if (e.length > MAX_LABEL_SIZE) throw new Error("Label to large");
length += e.length + 1;
})
length += 1; //Adding last 0 length octet
let data = Buffer.alloc(length);
let offset = 0;
parts.forEach(e => {
data.writeUInt8(e.length, offset)
offset++
data.write(e, offset, e.length)
offset += e.length
})
data.writeUInt8(0, offset);
return data;
}
} }
export class RecourceRecord implements MessageRecourceRecord { export class RecourceRecord implements MessageRecourceRecord {