WIP add cp, add server
This commit is contained in:
		
							parent
							
								
									b5af1bfe24
								
							
						
					
					
						commit
						5fff05a76f
					
				
							
								
								
									
										100
									
								
								bin/debug.js
									
									
									
									
									
								
							
							
						
						
									
										100
									
								
								bin/debug.js
									
									
									
									
									
								
							| @ -1,3 +1,8 @@ | |||||||
|  | #!/usr/bin/env node
 | ||||||
|  | 
 | ||||||
|  | // node bin/debug.js ../dig.js/blar.DOEsNteXiST.AJ.OnEal.DAplIE.me.any.0.bin
 | ||||||
|  | // node bin/debugd.js ../dig.js/blar.DOEsNteXiST.AJ.OnEal.DAplIE.me.any.0.bin
 | ||||||
|  | // dig @localhost -p 65053 ANY DOEsNteXiST.AJ.OnEal.DAplIE.me
 | ||||||
| (function () { | (function () { | ||||||
| 'use strict'; | 'use strict'; | ||||||
| 
 | 
 | ||||||
| @ -17,6 +22,10 @@ console.log('//    HEADER    //'); | |||||||
| console.log('//////////////////'); | console.log('//////////////////'); | ||||||
| console.log(''); | console.log(''); | ||||||
| 
 | 
 | ||||||
|  | console.log(''); | ||||||
|  | console.log('byte index 0x00'); | ||||||
|  | console.log(''); | ||||||
|  | 
 | ||||||
| console.log('id (int)', dv.getUint16(0)); | console.log('id (int)', dv.getUint16(0)); | ||||||
| 
 | 
 | ||||||
| var flags = dv.getUint16(2, false); | var flags = dv.getUint16(2, false); | ||||||
| @ -45,9 +54,14 @@ console.log('ancount', ancount); | |||||||
| console.log('nscount', nscount); | console.log('nscount', nscount); | ||||||
| console.log('arcount', arcount); | console.log('arcount', arcount); | ||||||
| 
 | 
 | ||||||
| function unpackQuestion(dv, total, len) { | function unpackQuestionLabels(opts) { | ||||||
|  |   var dv = opts.dv; | ||||||
|  |   //var origTotal = opts.total;
 | ||||||
|  |   var total = opts.total; | ||||||
|  |   var len = opts.len; | ||||||
|   var qnames = []; |   var qnames = []; | ||||||
|   var labelLen; |   var labelLen; | ||||||
|  | 
 | ||||||
|   while (true) { |   while (true) { | ||||||
|     if (total >= len) { |     if (total >= len) { | ||||||
|       break; |       break; | ||||||
| @ -58,29 +72,46 @@ function unpackQuestion(dv, total, len) { | |||||||
|       break; |       break; | ||||||
|     } |     } | ||||||
|     if (192 === labelLen) { |     if (192 === labelLen) { | ||||||
|       console.log('ERROR: Compression pointer found'); |       var pointer = dv.getUint8(total, false); | ||||||
|       console.log(dv.getUint8(total, false)); |       console.log('Found a pointer to' | ||||||
|       console.log(dv.getUint8(dv.getUint8(total + 0, false), false)); |         + ' 0x' + pointer.toString(16) + ' (' + pointer + ')' | ||||||
|       console.log(dv.getUint8(dv.getUint8(total + 1, false), false)); |         + ' at byte index' | ||||||
|       console.log(dv.getUint8(dv.getUint8(total + 2, false), false)); |         + ' 0x' + leftPad(total.toString(16)) + ' (' + total + ')' | ||||||
|  |         + ' which back-references:' | ||||||
|  |       ); | ||||||
|  |       console.log('-4', dv.getUint8(pointer - 4, false)); | ||||||
|  |       console.log('-3', dv.getUint8(pointer - 3, false)); | ||||||
|  |       console.log('-2', dv.getUint8(pointer - 2, false)); | ||||||
|  |       console.log('-1', dv.getUint8(pointer - 1, false)); | ||||||
|  |       console.log(' 0', dv.getUint8(pointer + 0, false)); | ||||||
|  |       console.log('+1', dv.getUint8(pointer + 1, false)); | ||||||
|  |       console.log('+2', dv.getUint8(pointer + 2, false)); | ||||||
|  |       console.log('bytes near the pointer itself (not what it points to) are:'); | ||||||
|  |       console.log('-2', dv.getUint8(total - 2, false)); | ||||||
|  |       console.log('-1', dv.getUint8(total - 1, false)); | ||||||
|  |       console.log(' 0', dv.getUint8(total + 0, false)); | ||||||
|  |       console.log('+1', dv.getUint8(total + 1, false)); | ||||||
|  |       console.log('+2', dv.getUint8(total + 2, false)); | ||||||
|  |       console.log('+3', dv.getUint8(total + 3, false)); | ||||||
|  |       console.log('+4', dv.getUint8(total + 4, false)); | ||||||
|  |       //total += 1;
 | ||||||
|  |       opts.cp = true; | ||||||
|  |       qnames.push(unpackQuestionLabels({ dv: dv, total: pointer, len: len }).qname); | ||||||
|  |       total += 1; | ||||||
|       break; |       break; | ||||||
|  |     } else { | ||||||
|  |       var i; | ||||||
|  |       var label = ''; | ||||||
|  |       console.log('label len', labelLen); | ||||||
|  |       for (i = 0; i < labelLen; i += 1) { | ||||||
|  |         //console.log('total:', total, i);
 | ||||||
|  |         label += String.fromCharCode(dv.getUint8(total + i, false)); | ||||||
|  |       } | ||||||
|  |       total += label.length; | ||||||
|  |       console.log('label:', label); | ||||||
|  |       qnames.push(label); | ||||||
|     } |     } | ||||||
|     var i; |  | ||||||
|     var label = ''; |  | ||||||
|     console.log('label len', labelLen); |  | ||||||
|     for (i = 0; i < labelLen; i += 1) { |  | ||||||
|       label += String.fromCharCode(dv.getUint8(total + i, false)); |  | ||||||
|     } |  | ||||||
|     total += label.length; |  | ||||||
|     console.log('label:', label); |  | ||||||
|     qnames.push(label); |  | ||||||
|   } |   } | ||||||
|   // leading length and (potentially) trailing null
 |  | ||||||
|   console.log('QNAME (len ' + (qnames.join('.').length + 1 + (labelLen ? 0 : 1)) + '):', qnames.join('.')); |  | ||||||
|   console.log('QTYPE (len 2):', dv.getUint16(total, false)); |  | ||||||
|   total += 2; |  | ||||||
|   console.log('QCLASS (len 2):', dv.getUint16(total, false)); |  | ||||||
|   total += 2; |  | ||||||
| 
 | 
 | ||||||
|   return { |   return { | ||||||
|     total: total |     total: total | ||||||
| @ -88,8 +119,23 @@ function unpackQuestion(dv, total, len) { | |||||||
|   }; |   }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | function unpackQuestion(dv, total, len) { | ||||||
|  | 
 | ||||||
|  |   var result = unpackQuestionLabels({ dv: dv, total: total, len: len }); | ||||||
|  | 
 | ||||||
|  |   // leading length and (potentially) trailing null
 | ||||||
|  |   console.log('QNAME (len ' + (result.total - total) + '):', result.qname, result.cp ? '(pointer)' : ''); | ||||||
|  |   console.log('QTYPE (len 2):', dv.getUint16(result.total, false)); | ||||||
|  |   result.total += 2; | ||||||
|  |   console.log('QCLASS (len 2):', dv.getUint16(result.total, false)); | ||||||
|  |   result.total += 2; | ||||||
|  | 
 | ||||||
|  |   return result; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| var q = { total: 12 }; | var q = { total: 12 }; | ||||||
| function mapChar(ch) { return JSON.stringify(String.fromCharCode(ch)) + '(' + ch + ')'; } | function mapChar(ch) { return JSON.stringify(String.fromCharCode(ch)) + '(' + ch + ')'; } | ||||||
|  | function leftPad(ch) { ch = ch.toString(); while (ch.length < 2) { ch = '0' + ch; } return ch; } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| console.log(''); | console.log(''); | ||||||
| @ -99,7 +145,7 @@ console.log('//////////////////'); | |||||||
| console.log(''); | console.log(''); | ||||||
| for (count = 0; count < qdcount; count += 1) { | for (count = 0; count < qdcount; count += 1) { | ||||||
|   console.log(''); |   console.log(''); | ||||||
|   console.log('Question ' + (count + 1) + ' of ' + qdcount + ' (byte index: ' + q.total + ')'); |   console.log('Question ' + (count + 1) + ' of ' + qdcount + ' (byte index: 0x' + leftPad(q.total.toString(16)) + ' ' + q.total + ')'); | ||||||
|   q = unpackQuestion(dv, q.total, ab.byteLength); |   q = unpackQuestion(dv, q.total, ab.byteLength); | ||||||
|   console.log(''); |   console.log(''); | ||||||
| } | } | ||||||
| @ -111,7 +157,7 @@ console.log('//////////////////'); | |||||||
| console.log(''); | console.log(''); | ||||||
| for (count = 0; count < ancount; count += 1) { | for (count = 0; count < ancount; count += 1) { | ||||||
|   console.log(''); |   console.log(''); | ||||||
|   console.log('Answer', count + 1, 'of', ancount + ' (byte index: ' + q.total + ')'); |   console.log('Answer', count + 1, 'of', ancount + ' (byte index: 0x' + q.total.toString(16) + ' ' + q.total + ')'); | ||||||
|   q = unpackQuestion(dv, q.total, ab.byteLength); |   q = unpackQuestion(dv, q.total, ab.byteLength); | ||||||
|   console.log(''); |   console.log(''); | ||||||
| } | } | ||||||
| @ -124,7 +170,7 @@ console.log('//////////////////'); | |||||||
| console.log(''); | console.log(''); | ||||||
| for (count = 0; count < nscount; count += 1) { | for (count = 0; count < nscount; count += 1) { | ||||||
|   console.log(''); |   console.log(''); | ||||||
|   console.log('Authority', count + 1, 'of', nscount + ' (byte index: ' + q.total + ')'); |   console.log('Authority', count + 1, 'of', nscount + ' (byte index: 0x' + q.total.toString(16) + ' ' + q.total + ')'); | ||||||
|   q = unpackQuestion(dv, q.total, ab.byteLength); |   q = unpackQuestion(dv, q.total, ab.byteLength); | ||||||
|   console.log('ttl (len 4):', dv.getUint32(q.total, false)); |   console.log('ttl (len 4):', dv.getUint32(q.total, false)); | ||||||
|   q.total += 4; |   q.total += 4; | ||||||
| @ -144,10 +190,14 @@ console.log('//////////////////'); | |||||||
| console.log(''); | console.log(''); | ||||||
| for (count = 0; count < arcount; count += 1) { | for (count = 0; count < arcount; count += 1) { | ||||||
|   console.log(''); |   console.log(''); | ||||||
|   console.log('Additional', count + 1, 'of', arcount + ' (byte index: ' + q.total + ')'); |   console.log('Additional', count + 1, 'of', arcount + ' (byte index: 0x' + q.total.toString(16) + ' ' + q.total + ')'); | ||||||
|   q = unpackQuestion(dv, q.total, ab.byteLength); |   q = unpackQuestion(dv, q.total, ab.byteLength); | ||||||
|   console.log(''); |   console.log(''); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | console.log(''); | ||||||
|  | console.log('total bytes', dv.byteLength); | ||||||
|  | console.log('total bytes read', q.total); | ||||||
|  | console.log(''); | ||||||
| 
 | 
 | ||||||
| }()); | }()); | ||||||
|  | |||||||
							
								
								
									
										45
									
								
								bin/debugd.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								bin/debugd.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,45 @@ | |||||||
|  | #!/usr/bin/env
 | ||||||
|  | (function () { | ||||||
|  | 'use strict'; | ||||||
|  | 
 | ||||||
|  | var cli = { port: 65053, address: null, udp6: false, bin: process.argv[2] }; | ||||||
|  | var dgram = require('dgram'); | ||||||
|  | var server = dgram.createSocket({ | ||||||
|  |   type: cli.udp6 ? 'udp6' : 'udp4' | ||||||
|  | , reuseAddr: true | ||||||
|  | }); | ||||||
|  | var handlers = {}; | ||||||
|  | var bin = require('fs').readFileSync(cli.bin, null); | ||||||
|  | 
 | ||||||
|  | handlers.onMessage = function (nb, rinfo) { | ||||||
|  |   console.log('[DEBUG] got a message'); | ||||||
|  | 
 | ||||||
|  |   // replace the id to match
 | ||||||
|  |   bin[0] = nb[0]; | ||||||
|  |   bin[1] = nb[1]; | ||||||
|  |   server.send(bin, rinfo.port, rinfo.address, function () { | ||||||
|  |     console.log('[DEBUG] sent response'); | ||||||
|  |   }); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | handlers.onListening = function () { | ||||||
|  |   /*jshint validthis:true*/ | ||||||
|  |   var server = this; | ||||||
|  | 
 | ||||||
|  |   if (cli.mdns || '224.0.0.251' === cli.nameserver) { | ||||||
|  |     server.setBroadcast(true); | ||||||
|  |     server.addMembership(cli.nameserver); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   console.log(''); | ||||||
|  |   console.log('Bound and Listening:'); | ||||||
|  |   console.log(server.address().address + '#' + server.address().port + ' (' + server.type + ')'); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | server.bind({ | ||||||
|  |   port: cli.port | ||||||
|  | , address: cli.address | ||||||
|  | }); | ||||||
|  | server.on('listening', handlers.onListening); | ||||||
|  | 
 | ||||||
|  | }()); | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user