54 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			54 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| // EXAMPLE:
 | |
| // node bin/dns-parse.js samples/a-0.mdns.bin
 | |
| 
 | |
| // pass a terminal arg
 | |
| var filename = process.argv[2];
 | |
| if (!filename) {
 | |
|   console.error("Usage: node bin/dns-parse.js <path/to/sample.bin>");
 | |
|   console.error("Example: node bin/dns-parse.js ./samples/services-0.mdns.bin");
 | |
|   process.exit(1);
 | |
| }
 | |
| 
 | |
| 
 | |
| var PromiseA = require('bluebird');
 | |
| var fs = PromiseA.promisifyAll(require('fs'));
 | |
| var pdns = require('../');
 | |
| 
 | |
| fs.readFileAsync(filename, null).then(function (nb) {
 | |
|   //
 | |
|   // current reference impl
 | |
|   //
 | |
|   //console.log(require('native-dns-packet').parse(nb));
 | |
| 
 | |
| 
 | |
|   //
 | |
|   // other reference impl
 | |
|   //
 | |
|   //console.log(require('dns-js').DNSPacket.parse(nb));
 | |
| 
 | |
|   // nb is a Uint8Array (ArrayBufferView) for nb.buffer
 | |
|   // nb.buffer is the actual ArrayBuffer
 | |
|   var packet = pdns.unpack(nb.buffer);
 | |
| 
 | |
|   function tryParseRdata(record) {
 | |
|     record.data = null;
 | |
|     record.error = null;
 | |
| 
 | |
|     try {
 | |
|       record.data = pdns.unpackRdata(nb.buffer, packet, record);
 | |
|     } catch (e) {
 | |
|       console.error(e.stack);
 | |
|       record.error = e;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   packet.answers.forEach(tryParseRdata);
 | |
|   packet.authority.forEach(tryParseRdata);
 | |
|   packet.additional.forEach(tryParseRdata);
 | |
| 
 | |
|   console.log('[packet]', nb.byteLength, 'bytes:');
 | |
|   console.log(JSON.stringify(packet, null, 2));
 | |
| });
 |