Skip to content

Commit

Permalink
Merge 63ccada into 0e1b4ac
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Jan 10, 2022
2 parents 0e1b4ac + 63ccada commit 80e237d
Show file tree
Hide file tree
Showing 9 changed files with 1,801 additions and 149 deletions.
27 changes: 27 additions & 0 deletions lib/inet.js
Expand Up @@ -55,6 +55,16 @@ const TABLE = new Int8Array([
* Presentation to Network
*/

/**
* Convert IPv4 network string to network format.
* @param {String} src - IP String
* @param {Buffer} dst - buffer where to decode to
* @param {Number} off - from where to start.
* @returns {Number} - number of bits, either imputed classfully or specified
* with /CIDR, or -1 if some failure occurred (check errno). ENOENT means it
* was not an IPv4 network specification.
*/

function pton4(src, dst, off) {
if (dst == null)
dst = null;
Expand Down Expand Up @@ -256,6 +266,16 @@ function pton4(src, dst, off) {
return bits;
}

/**
* Convert IPv6 network string to network format.
* @param {String} src - IP String
* @param {Buffer} dst - buffer where to decode to
* @param {Number} off - from where to start.
* @returns {Number} - number of bits, either imputed classfully or specified
* with /CIDR, or -1 if some failure occurred (check errno). ENOENT means it
* was not an IPv6 network specification.
*/

function pton6(src, dst, off) {
if (dst == null)
dst = null;
Expand Down Expand Up @@ -591,6 +611,13 @@ function family(str) {
return 0;
}

/**
* IPv4 Mapped - RFC 2765
* @param {Buffer} raw
* @param {Number} off
* @returns {Boolean}
*/

function mapped(raw, off) {
if (off == null)
off = 0;
Expand Down
30 changes: 22 additions & 8 deletions lib/ip.js
Expand Up @@ -34,6 +34,7 @@ const binet = exports;
*/

const ZERO_IP = Buffer.from('00000000000000000000000000000000', 'hex');
const ZERO_IPV4 = Buffer.from('00000000000000000000ffff00000000', 'hex');
const LOCAL_IP = Buffer.from('00000000000000000000000000000001', 'hex');
const RFC6052 = Buffer.from('0064ff9b0000000000000000', 'hex');
const RFC4862 = Buffer.from('fe80000000000000', 'hex');
Expand Down Expand Up @@ -210,7 +211,7 @@ binet.write = function write(dst, str, off, size) {
off += 10;
dst[off++] = 0xff;
dst[off++] = 0xff;
return off;
return off + 4;
}

if (inet.pton6(str, dst, off) >= 0)
Expand All @@ -232,7 +233,18 @@ binet.write = function write(dst, str, off, size) {

binet.writeBW = function writeBW(bw, str, size) {
assert(bw && typeof bw === 'object');
bw.offset = binet.write(bw.data, str, bw.offset, size);

// StaticWriter
if (bw.data) {
bw.offset = binet.write(bw.data, str, bw.offset, size);
return bw;
}

const buf = Buffer.alloc(size);
const off = binet.write(buf, str, 0, size);
// these must always match.
assert(off === size);
bw.writeBytes(buf);
return bw;
};

Expand Down Expand Up @@ -1300,9 +1312,9 @@ binet.getReachability = function getReachability(src, dest) {
const destNet = binet.getNetwork(dest);

switch (destNet) {
case networks.IPV4:
case networks.INET4:
switch (srcNet) {
case networks.IPV4:
case networks.INET4:
return IPV4;
default:
return DEFAULT;
Expand All @@ -1312,7 +1324,7 @@ binet.getReachability = function getReachability(src, dest) {
switch (srcNet) {
case networks.TEREDO:
return TEREDO;
case networks.IPV4:
case networks.INET4:
return IPV4;
case networks.INET6:
if (binet.isRFC3964(src)
Expand All @@ -1328,7 +1340,7 @@ binet.getReachability = function getReachability(src, dest) {
break;
case networks.ONION:
switch (srcNet) {
case networks.IPV4:
case networks.INET4:
return IPV4;
case networks.ONION:
return PRIVATE;
Expand All @@ -1342,7 +1354,7 @@ binet.getReachability = function getReachability(src, dest) {
return TEREDO;
case networks.INET6:
return IPV6_WEAK;
case networks.IPV4:
case networks.INET4:
return IPV4;
default:
return DEFAULT;
Expand All @@ -1354,7 +1366,7 @@ binet.getReachability = function getReachability(src, dest) {
return TEREDO;
case networks.INET6:
return IPV6_WEAK;
case networks.IPV4:
case networks.INET4:
return IPV4;
case networks.ONION:
return PRIVATE;
Expand Down Expand Up @@ -1602,5 +1614,7 @@ binet.ip = binet;
binet.types = types;
binet.networks = networks;
binet.ZERO_IP = ZERO_IP;
binet.ZERO_IPV6 = ZERO_IP;
binet.ZERO_IPV4 = ZERO_IPV4;
binet.onion = onion;
binet.inet = inet;
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -17,7 +17,7 @@
"author": "Christopher Jeffrey <chjjeffrey@gmail.com>",
"main": "./lib/binet.js",
"scripts": {
"lint": "eslint lib/ test/ || exit 0",
"lint": "eslint lib/ test/",
"test": "bmocha --reporter spec test/*-test.js",
"test-ci": "nyc --reporter lcov bmocha -- --reporter spec test/*.js"
},
Expand All @@ -26,7 +26,8 @@
"bsert": "~0.0.10"
},
"devDependencies": {
"bmocha": "^2.1.0"
"bmocha": "^2.1.0",
"bufio": "^1.0.7"
},
"engines": {
"node": ">=8.0.0"
Expand Down
139 changes: 0 additions & 139 deletions test/binet-test.js

This file was deleted.

0 comments on commit 80e237d

Please sign in to comment.