Permalink
Browse files

peer

  • Loading branch information...
1 parent dd830ab commit 9f3025ecd80bd45ecd92bc1b8a296896a7a68975 @usoban committed Sep 25, 2011
Showing with 55 additions and 0 deletions.
  1. +10 −0 lib/bencode.js
  2. +45 −0 lib/common/nbo.js
View
10 lib/bencode.js
@@ -0,0 +1,10 @@
+function bdecode(buffer, encoding){
+ // UTF-8 is default encoding
+ encoding = encoding || 'binary';
+
+ if( ! Buffer.isBuffer(buffer) ){
+ buffer = new Buffer(buffer, encoding);
+ }
+
+ var buffLen = buffer.
+}
View
45 lib/common/nbo.js
@@ -0,0 +1,45 @@
+module.exports.ntohl = ntohl;
+module.exports.ntoipv4 = ntoipv4;
+module.exports.ntoport = ntoport;
+
+/**
+ * Converts network byte order to host order (32bit address)
+ * @param {Buffer} nbo
+ */
+function ntohl(nbo){
+ if( ! Buffer.isBuffer(nbo) ){
+ throw 'Parameter is not a buffer';
+ }
+
+ return (
+ ((0xff & nbo[0]) << 24) | ((0xff & nbo[1]) << 16) |
+ ((0xff & nbo[2]) << 8) | (0xff & nbo[3])
+ );
+}
+
+/**
+ * Converts network byte order directly into IPv4 string
+ * @param {Buffer} nbo
+ */
+function ntoipv4(nbo){
+ var str = '';
+
+ for(var i = 0, ii = nbo.length; i < ii; i++){
+ str += nbo[i] + ((i < 3) ? '.' : '');
+ }
+
+ return str;
+}
+
+/**
+ * Converts 2 binary bytes to decimal port number
+ */
+function ntoport(bytes){
+ if( ! Buffer.isBuffer(bytes) ){
+ throw 'Parameter is not a buffer';
+ }
+
+ var hexStr = bytes.toString();
+
+ return parseInt( bytes[0].toString(16) + bytes[1].toString(16) , 16);
+}

0 comments on commit 9f3025e

Please sign in to comment.